Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / cp / pt.c
blob5a90bdc6eadf7cd119debf046b13a722c180932b
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "intl.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "tree-iterator.h"
46 #include "vecprim.h"
48 /* The type of functions taking a tree, and some additional data, and
49 returning an int. */
50 typedef int (*tree_fn_t) (tree, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY (()) pending_template {
56 struct pending_template *next;
57 struct tinst_level *tinst;
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) struct tinst_level *current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static htab_t local_specializations;
83 typedef struct GTY(()) spec_entry
85 tree tmpl;
86 tree args;
87 tree spec;
88 } spec_entry;
90 static GTY ((param_is (spec_entry)))
91 htab_t decl_specializations;
93 static GTY ((param_is (spec_entry)))
94 htab_t type_specializations;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) VEC(tree,gc) *canonical_template_parms;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 static void push_access_scope (tree);
112 static void pop_access_scope (tree);
113 static bool resolve_overloaded_unification (tree, tree, tree, tree,
114 unification_kind_t, int);
115 static int try_one_overload (tree, tree, tree, tree, tree,
116 unification_kind_t, int, bool);
117 static int unify (tree, tree, tree, tree, int);
118 static void add_pending_template (tree);
119 static tree reopen_tinst_level (struct tinst_level *);
120 static tree tsubst_initializer_list (tree, tree);
121 static tree get_class_bindings (tree, tree, tree);
122 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
123 bool, bool);
124 static void tsubst_enum (tree, tree, tree);
125 static tree add_to_template_args (tree, tree);
126 static tree add_outermost_template_args (tree, tree);
127 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
128 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
129 tree);
130 static int type_unification_real (tree, tree, tree, const tree *,
131 unsigned int, int, unification_kind_t, int);
132 static void note_template_header (int);
133 static tree convert_nontype_argument_function (tree, tree);
134 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
135 static tree convert_template_argument (tree, tree, tree,
136 tsubst_flags_t, int, tree);
137 static int for_each_template_parm (tree, tree_fn_t, void*,
138 struct pointer_set_t*, bool);
139 static tree expand_template_argument_pack (tree);
140 static tree build_template_parm_index (int, int, int, tree, tree);
141 static bool inline_needs_template_parms (tree);
142 static void push_inline_template_parms_recursive (tree, int);
143 static tree retrieve_local_specialization (tree);
144 static void register_local_specialization (tree, tree);
145 static hashval_t hash_specialization (const void *p);
146 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
147 static int mark_template_parm (tree, void *);
148 static int template_parm_this_level_p (tree, void *);
149 static tree tsubst_friend_function (tree, tree);
150 static tree tsubst_friend_class (tree, tree);
151 static int can_complete_type_without_circularity (tree);
152 static tree get_bindings (tree, tree, tree, bool);
153 static int template_decl_level (tree);
154 static int check_cv_quals_for_unify (int, tree, tree);
155 static void template_parm_level_and_index (tree, int*, int*);
156 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
157 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
158 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
159 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
160 static void regenerate_decl_from_template (tree, tree);
161 static tree most_specialized_class (tree, tree, tsubst_flags_t);
162 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
163 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
165 static bool check_specialization_scope (void);
166 static tree process_partial_specialization (tree);
167 static void set_current_access_from_decl (tree);
168 static tree get_template_base (tree, tree, tree, tree);
169 static tree try_class_unification (tree, tree, tree, tree);
170 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
171 tree, tree);
172 static bool template_template_parm_bindings_ok_p (tree, tree);
173 static int template_args_equal (tree, tree);
174 static void tsubst_default_arguments (tree);
175 static tree for_each_template_parm_r (tree *, int *, void *);
176 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
177 static void copy_default_args_to_explicit_spec (tree);
178 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
179 static int eq_local_specializations (const void *, const void *);
180 static bool dependent_template_arg_p (tree);
181 static bool any_template_arguments_need_structural_equality_p (tree);
182 static bool dependent_type_p_r (tree);
183 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
184 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
185 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_decl (tree, tree, tsubst_flags_t);
187 static void perform_typedefs_access_check (tree tmpl, tree targs);
188 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
189 location_t);
190 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
191 static tree listify (tree);
192 static tree listify_autos (tree, tree);
194 /* Make the current scope suitable for access checking when we are
195 processing T. T can be FUNCTION_DECL for instantiated function
196 template, or VAR_DECL for static member variable (need by
197 instantiate_decl). */
199 static void
200 push_access_scope (tree t)
202 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
203 || TREE_CODE (t) == VAR_DECL);
205 if (DECL_FRIEND_CONTEXT (t))
206 push_nested_class (DECL_FRIEND_CONTEXT (t));
207 else if (DECL_CLASS_SCOPE_P (t))
208 push_nested_class (DECL_CONTEXT (t));
209 else
210 push_to_top_level ();
212 if (TREE_CODE (t) == FUNCTION_DECL)
214 saved_access_scope = tree_cons
215 (NULL_TREE, current_function_decl, saved_access_scope);
216 current_function_decl = t;
220 /* Restore the scope set up by push_access_scope. T is the node we
221 are processing. */
223 static void
224 pop_access_scope (tree t)
226 if (TREE_CODE (t) == FUNCTION_DECL)
228 current_function_decl = TREE_VALUE (saved_access_scope);
229 saved_access_scope = TREE_CHAIN (saved_access_scope);
232 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
233 pop_nested_class ();
234 else
235 pop_from_top_level ();
238 /* Do any processing required when DECL (a member template
239 declaration) is finished. Returns the TEMPLATE_DECL corresponding
240 to DECL, unless it is a specialization, in which case the DECL
241 itself is returned. */
243 tree
244 finish_member_template_decl (tree decl)
246 if (decl == error_mark_node)
247 return error_mark_node;
249 gcc_assert (DECL_P (decl));
251 if (TREE_CODE (decl) == TYPE_DECL)
253 tree type;
255 type = TREE_TYPE (decl);
256 if (type == error_mark_node)
257 return error_mark_node;
258 if (MAYBE_CLASS_TYPE_P (type)
259 && CLASSTYPE_TEMPLATE_INFO (type)
260 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
262 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
263 check_member_template (tmpl);
264 return tmpl;
266 return NULL_TREE;
268 else if (TREE_CODE (decl) == FIELD_DECL)
269 error ("data member %qD cannot be a member template", decl);
270 else if (DECL_TEMPLATE_INFO (decl))
272 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
274 check_member_template (DECL_TI_TEMPLATE (decl));
275 return DECL_TI_TEMPLATE (decl);
277 else
278 return decl;
280 else
281 error ("invalid member template declaration %qD", decl);
283 return error_mark_node;
286 /* Create a template info node. */
288 tree
289 build_template_info (tree template_decl, tree template_args)
291 tree result = make_node (TEMPLATE_INFO);
292 TI_TEMPLATE (result) = template_decl;
293 TI_ARGS (result) = template_args;
294 return result;
297 /* Return the template info node corresponding to T, whatever T is. */
299 tree
300 get_template_info (const_tree t)
302 tree tinfo = NULL_TREE;
304 if (!t || t == error_mark_node)
305 return NULL;
307 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
308 tinfo = DECL_TEMPLATE_INFO (t);
310 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
311 t = TREE_TYPE (t);
313 if (TAGGED_TYPE_P (t))
314 tinfo = TYPE_TEMPLATE_INFO (t);
315 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
316 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
318 return tinfo;
321 /* Returns the template nesting level of the indicated class TYPE.
323 For example, in:
324 template <class T>
325 struct A
327 template <class U>
328 struct B {};
331 A<T>::B<U> has depth two, while A<T> has depth one.
332 Both A<T>::B<int> and A<int>::B<U> have depth one, if
333 they are instantiations, not specializations.
335 This function is guaranteed to return 0 if passed NULL_TREE so
336 that, for example, `template_class_depth (current_class_type)' is
337 always safe. */
340 template_class_depth (tree type)
342 int depth;
344 for (depth = 0;
345 type && TREE_CODE (type) != NAMESPACE_DECL;
346 type = (TREE_CODE (type) == FUNCTION_DECL)
347 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
349 tree tinfo = get_template_info (type);
351 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
352 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
353 ++depth;
356 return depth;
359 /* Subroutine of maybe_begin_member_template_processing.
360 Returns true if processing DECL needs us to push template parms. */
362 static bool
363 inline_needs_template_parms (tree decl)
365 if (! DECL_TEMPLATE_INFO (decl))
366 return false;
368 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
369 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
372 /* Subroutine of maybe_begin_member_template_processing.
373 Push the template parms in PARMS, starting from LEVELS steps into the
374 chain, and ending at the beginning, since template parms are listed
375 innermost first. */
377 static void
378 push_inline_template_parms_recursive (tree parmlist, int levels)
380 tree parms = TREE_VALUE (parmlist);
381 int i;
383 if (levels > 1)
384 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
386 ++processing_template_decl;
387 current_template_parms
388 = tree_cons (size_int (processing_template_decl),
389 parms, current_template_parms);
390 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
392 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
393 NULL);
394 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
396 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
398 if (parm == error_mark_node)
399 continue;
401 gcc_assert (DECL_P (parm));
403 switch (TREE_CODE (parm))
405 case TYPE_DECL:
406 case TEMPLATE_DECL:
407 pushdecl (parm);
408 break;
410 case PARM_DECL:
412 /* Make a CONST_DECL as is done in process_template_parm.
413 It is ugly that we recreate this here; the original
414 version built in process_template_parm is no longer
415 available. */
416 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
417 CONST_DECL, DECL_NAME (parm),
418 TREE_TYPE (parm));
419 DECL_ARTIFICIAL (decl) = 1;
420 TREE_CONSTANT (decl) = 1;
421 TREE_READONLY (decl) = 1;
422 DECL_INITIAL (decl) = DECL_INITIAL (parm);
423 SET_DECL_TEMPLATE_PARM_P (decl);
424 pushdecl (decl);
426 break;
428 default:
429 gcc_unreachable ();
434 /* Restore the template parameter context for a member template or
435 a friend template defined in a class definition. */
437 void
438 maybe_begin_member_template_processing (tree decl)
440 tree parms;
441 int levels = 0;
443 if (inline_needs_template_parms (decl))
445 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
446 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
448 if (DECL_TEMPLATE_SPECIALIZATION (decl))
450 --levels;
451 parms = TREE_CHAIN (parms);
454 push_inline_template_parms_recursive (parms, levels);
457 /* Remember how many levels of template parameters we pushed so that
458 we can pop them later. */
459 VEC_safe_push (int, heap, inline_parm_levels, levels);
462 /* Undo the effects of maybe_begin_member_template_processing. */
464 void
465 maybe_end_member_template_processing (void)
467 int i;
468 int last;
470 if (VEC_length (int, inline_parm_levels) == 0)
471 return;
473 last = VEC_pop (int, inline_parm_levels);
474 for (i = 0; i < last; ++i)
476 --processing_template_decl;
477 current_template_parms = TREE_CHAIN (current_template_parms);
478 poplevel (0, 0, 0);
482 /* Return a new template argument vector which contains all of ARGS,
483 but has as its innermost set of arguments the EXTRA_ARGS. */
485 static tree
486 add_to_template_args (tree args, tree extra_args)
488 tree new_args;
489 int extra_depth;
490 int i;
491 int j;
493 if (args == NULL_TREE)
494 return extra_args;
496 extra_depth = TMPL_ARGS_DEPTH (extra_args);
497 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
499 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
500 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
502 for (j = 1; j <= extra_depth; ++j, ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
505 return new_args;
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
515 static tree
516 add_outermost_template_args (tree args, tree extra_args)
518 tree new_args;
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
524 /* If *all* the new arguments will be the EXTRA_ARGS, just return
525 them. */
526 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
527 return extra_args;
529 /* For the moment, we make ARGS look like it contains fewer levels. */
530 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
532 new_args = add_to_template_args (args, extra_args);
534 /* Now, we restore ARGS to its full dimensions. */
535 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
537 return new_args;
540 /* Return the N levels of innermost template arguments from the ARGS. */
542 tree
543 get_innermost_template_args (tree args, int n)
545 tree new_args;
546 int extra_levels;
547 int i;
549 gcc_assert (n >= 0);
551 /* If N is 1, just return the innermost set of template arguments. */
552 if (n == 1)
553 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
555 /* If we're not removing anything, just return the arguments we were
556 given. */
557 extra_levels = TMPL_ARGS_DEPTH (args) - n;
558 gcc_assert (extra_levels >= 0);
559 if (extra_levels == 0)
560 return args;
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args = make_tree_vec (n);
564 for (i = 1; i <= n; ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i,
566 TMPL_ARGS_LEVEL (args, i + extra_levels));
568 return new_args;
571 /* The inverse of get_innermost_template_args: Return all but the innermost
572 EXTRA_LEVELS levels of template arguments from the ARGS. */
574 static tree
575 strip_innermost_template_args (tree args, int extra_levels)
577 tree new_args;
578 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the outermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, 1);
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
593 /* Make a new set of arguments, not containing the inner arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i));
599 return new_args;
602 /* We've got a template header coming up; push to a new level for storing
603 the parms. */
605 void
606 begin_template_parm_list (void)
608 /* We use a non-tag-transparent scope here, which causes pushtag to
609 put tags in this scope, rather than in the enclosing class or
610 namespace scope. This is the right thing, since we want
611 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
612 global template class, push_template_decl handles putting the
613 TEMPLATE_DECL into top-level scope. For a nested template class,
614 e.g.:
616 template <class T> struct S1 {
617 template <class T> struct S2 {};
620 pushtag contains special code to call pushdecl_with_scope on the
621 TEMPLATE_DECL for S2. */
622 begin_scope (sk_template_parms, NULL);
623 ++processing_template_decl;
624 ++processing_template_parmlist;
625 note_template_header (0);
628 /* This routine is called when a specialization is declared. If it is
629 invalid to declare a specialization here, an error is reported and
630 false is returned, otherwise this routine will return true. */
632 static bool
633 check_specialization_scope (void)
635 tree scope = current_scope ();
637 /* [temp.expl.spec]
639 An explicit specialization shall be declared in the namespace of
640 which the template is a member, or, for member templates, in the
641 namespace of which the enclosing class or enclosing class
642 template is a member. An explicit specialization of a member
643 function, member class or static data member of a class template
644 shall be declared in the namespace of which the class template
645 is a member. */
646 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
648 error ("explicit specialization in non-namespace scope %qD", scope);
649 return false;
652 /* [temp.expl.spec]
654 In an explicit specialization declaration for a member of a class
655 template or a member template that appears in namespace scope,
656 the member template and some of its enclosing class templates may
657 remain unspecialized, except that the declaration shall not
658 explicitly specialize a class member template if its enclosing
659 class templates are not explicitly specialized as well. */
660 if (current_template_parms)
662 error ("enclosing class templates are not explicitly specialized");
663 return false;
666 return true;
669 /* We've just seen template <>. */
671 bool
672 begin_specialization (void)
674 begin_scope (sk_template_spec, NULL);
675 note_template_header (1);
676 return check_specialization_scope ();
679 /* Called at then end of processing a declaration preceded by
680 template<>. */
682 void
683 end_specialization (void)
685 finish_scope ();
686 reset_specialization ();
689 /* Any template <>'s that we have seen thus far are not referring to a
690 function specialization. */
692 void
693 reset_specialization (void)
695 processing_specialization = 0;
696 template_header_count = 0;
699 /* We've just seen a template header. If SPECIALIZATION is nonzero,
700 it was of the form template <>. */
702 static void
703 note_template_header (int specialization)
705 processing_specialization = specialization;
706 template_header_count++;
709 /* We're beginning an explicit instantiation. */
711 void
712 begin_explicit_instantiation (void)
714 gcc_assert (!processing_explicit_instantiation);
715 processing_explicit_instantiation = true;
719 void
720 end_explicit_instantiation (void)
722 gcc_assert (processing_explicit_instantiation);
723 processing_explicit_instantiation = false;
726 /* An explicit specialization or partial specialization TMPL is being
727 declared. Check that the namespace in which the specialization is
728 occurring is permissible. Returns false iff it is invalid to
729 specialize TMPL in the current namespace. */
731 static bool
732 check_specialization_namespace (tree tmpl)
734 tree tpl_ns = decl_namespace_context (tmpl);
736 /* [tmpl.expl.spec]
738 An explicit specialization shall be declared in the namespace of
739 which the template is a member, or, for member templates, in the
740 namespace of which the enclosing class or enclosing class
741 template is a member. An explicit specialization of a member
742 function, member class or static data member of a class template
743 shall be declared in the namespace of which the class template is
744 a member. */
745 if (current_scope() != DECL_CONTEXT (tmpl)
746 && !at_namespace_scope_p ())
748 error ("specialization of %qD must appear at namespace scope", tmpl);
749 return false;
751 if (is_associated_namespace (current_namespace, tpl_ns))
752 /* Same or super-using namespace. */
753 return true;
754 else
756 permerror (input_location, "specialization of %qD in different namespace", tmpl);
757 permerror (input_location, " from definition of %q+#D", tmpl);
758 return false;
762 /* SPEC is an explicit instantiation. Check that it is valid to
763 perform this explicit instantiation in the current namespace. */
765 static void
766 check_explicit_instantiation_namespace (tree spec)
768 tree ns;
770 /* DR 275: An explicit instantiation shall appear in an enclosing
771 namespace of its template. */
772 ns = decl_namespace_context (spec);
773 if (!is_ancestor (current_namespace, ns))
774 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
775 "(which does not enclose namespace %qD)",
776 spec, current_namespace, ns);
779 /* The TYPE is being declared. If it is a template type, that means it
780 is a partial specialization. Do appropriate error-checking. */
782 tree
783 maybe_process_partial_specialization (tree type)
785 tree context;
787 if (type == error_mark_node)
788 return error_mark_node;
790 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
792 error ("name of class shadows template template parameter %qD",
793 TYPE_NAME (type));
794 return error_mark_node;
797 context = TYPE_CONTEXT (type);
799 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
801 /* This is for ordinary explicit specialization and partial
802 specialization of a template class such as:
804 template <> class C<int>;
808 template <class T> class C<T*>;
810 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
812 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
813 && !COMPLETE_TYPE_P (type))
815 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
816 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
817 if (processing_template_decl)
819 if (push_template_decl (TYPE_MAIN_DECL (type))
820 == error_mark_node)
821 return error_mark_node;
824 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
825 error ("specialization of %qT after instantiation", type);
827 else if (CLASS_TYPE_P (type)
828 && !CLASSTYPE_USE_TEMPLATE (type)
829 && CLASSTYPE_TEMPLATE_INFO (type)
830 && context && CLASS_TYPE_P (context)
831 && CLASSTYPE_TEMPLATE_INFO (context))
833 /* This is for an explicit specialization of member class
834 template according to [temp.expl.spec/18]:
836 template <> template <class U> class C<int>::D;
838 The context `C<int>' must be an implicit instantiation.
839 Otherwise this is just a member class template declared
840 earlier like:
842 template <> class C<int> { template <class U> class D; };
843 template <> template <class U> class C<int>::D;
845 In the first case, `C<int>::D' is a specialization of `C<T>::D'
846 while in the second case, `C<int>::D' is a primary template
847 and `C<T>::D' may not exist. */
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
850 && !COMPLETE_TYPE_P (type))
852 tree t;
853 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
855 if (current_namespace
856 != decl_namespace_context (tmpl))
858 permerror (input_location, "specializing %q#T in different namespace", type);
859 permerror (input_location, " from definition of %q+#D", tmpl);
862 /* Check for invalid specialization after instantiation:
864 template <> template <> class C<int>::D<int>;
865 template <> template <class U> class C<int>::D; */
867 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
868 t; t = TREE_CHAIN (t))
870 tree inst = TREE_VALUE (t);
871 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
873 /* We already have a full specialization of this partial
874 instantiation. Reassign it to the new member
875 specialization template. */
876 spec_entry elt;
877 spec_entry **slot;
879 elt.tmpl = most_general_template (tmpl);
880 elt.args = CLASSTYPE_TI_ARGS (inst);
881 elt.spec = inst;
883 htab_remove_elt (type_specializations, &elt);
885 elt.tmpl = tmpl;
886 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
888 slot = (spec_entry **)
889 htab_find_slot (type_specializations, &elt, INSERT);
890 *slot = ggc_alloc_spec_entry ();
891 **slot = elt;
893 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
894 /* But if we've had an implicit instantiation, that's a
895 problem ([temp.expl.spec]/6). */
896 error ("specialization %qT after instantiation %qT",
897 type, inst);
900 /* Mark TYPE as a specialization. And as a result, we only
901 have one level of template argument for the innermost
902 class template. */
903 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
904 CLASSTYPE_TI_ARGS (type)
905 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
908 else if (processing_specialization)
910 error ("explicit specialization of non-template %qT", type);
911 return error_mark_node;
914 return type;
917 /* Returns nonzero if we can optimize the retrieval of specializations
918 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
919 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
921 static inline bool
922 optimize_specialization_lookup_p (tree tmpl)
924 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
925 && DECL_CLASS_SCOPE_P (tmpl)
926 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
927 parameter. */
928 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
929 /* The optimized lookup depends on the fact that the
930 template arguments for the member function template apply
931 purely to the containing class, which is not true if the
932 containing class is an explicit or partial
933 specialization. */
934 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
935 && !DECL_MEMBER_TEMPLATE_P (tmpl)
936 && !DECL_CONV_FN_P (tmpl)
937 /* It is possible to have a template that is not a member
938 template and is not a member of a template class:
940 template <typename T>
941 struct S { friend A::f(); };
943 Here, the friend function is a template, but the context does
944 not have template information. The optimized lookup relies
945 on having ARGS be the template arguments for both the class
946 and the function template. */
947 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
950 /* Retrieve the specialization (in the sense of [temp.spec] - a
951 specialization is either an instantiation or an explicit
952 specialization) of TMPL for the given template ARGS. If there is
953 no such specialization, return NULL_TREE. The ARGS are a vector of
954 arguments, or a vector of vectors of arguments, in the case of
955 templates with more than one level of parameters.
957 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
958 then we search for a partial specialization matching ARGS. This
959 parameter is ignored if TMPL is not a class template. */
961 static tree
962 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
964 if (args == error_mark_node)
965 return NULL_TREE;
967 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
969 /* There should be as many levels of arguments as there are
970 levels of parameters. */
971 gcc_assert (TMPL_ARGS_DEPTH (args)
972 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
974 if (optimize_specialization_lookup_p (tmpl))
976 tree class_template;
977 tree class_specialization;
978 VEC(tree,gc) *methods;
979 tree fns;
980 int idx;
982 /* The template arguments actually apply to the containing
983 class. Find the class specialization with those
984 arguments. */
985 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
986 class_specialization
987 = retrieve_specialization (class_template, args, 0);
988 if (!class_specialization)
989 return NULL_TREE;
990 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
991 for the specialization. */
992 idx = class_method_index_for_fn (class_specialization, tmpl);
993 if (idx == -1)
994 return NULL_TREE;
995 /* Iterate through the methods with the indicated name, looking
996 for the one that has an instance of TMPL. */
997 methods = CLASSTYPE_METHOD_VEC (class_specialization);
998 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1000 tree fn = OVL_CURRENT (fns);
1001 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1002 /* using-declarations can add base methods to the method vec,
1003 and we don't want those here. */
1004 && DECL_CONTEXT (fn) == class_specialization)
1005 return fn;
1007 return NULL_TREE;
1009 else
1011 spec_entry *found;
1012 spec_entry elt;
1013 htab_t specializations;
1015 elt.tmpl = tmpl;
1016 elt.args = args;
1017 elt.spec = NULL_TREE;
1019 if (DECL_CLASS_TEMPLATE_P (tmpl))
1020 specializations = type_specializations;
1021 else
1022 specializations = decl_specializations;
1024 if (hash == 0)
1025 hash = hash_specialization (&elt);
1026 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1027 if (found)
1028 return found->spec;
1031 return NULL_TREE;
1034 /* Like retrieve_specialization, but for local declarations. */
1036 static tree
1037 retrieve_local_specialization (tree tmpl)
1039 tree spec;
1041 if (local_specializations == NULL)
1042 return NULL_TREE;
1044 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1045 htab_hash_pointer (tmpl));
1046 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1049 /* Returns nonzero iff DECL is a specialization of TMPL. */
1052 is_specialization_of (tree decl, tree tmpl)
1054 tree t;
1056 if (TREE_CODE (decl) == FUNCTION_DECL)
1058 for (t = decl;
1059 t != NULL_TREE;
1060 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1061 if (t == tmpl)
1062 return 1;
1064 else
1066 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1068 for (t = TREE_TYPE (decl);
1069 t != NULL_TREE;
1070 t = CLASSTYPE_USE_TEMPLATE (t)
1071 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1072 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1073 return 1;
1076 return 0;
1079 /* Returns nonzero iff DECL is a specialization of friend declaration
1080 FRIEND_DECL according to [temp.friend]. */
1082 bool
1083 is_specialization_of_friend (tree decl, tree friend_decl)
1085 bool need_template = true;
1086 int template_depth;
1088 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1089 || TREE_CODE (decl) == TYPE_DECL);
1091 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1092 of a template class, we want to check if DECL is a specialization
1093 if this. */
1094 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1095 && DECL_TEMPLATE_INFO (friend_decl)
1096 && !DECL_USE_TEMPLATE (friend_decl))
1098 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1099 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1100 need_template = false;
1102 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1103 && !PRIMARY_TEMPLATE_P (friend_decl))
1104 need_template = false;
1106 /* There is nothing to do if this is not a template friend. */
1107 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1108 return false;
1110 if (is_specialization_of (decl, friend_decl))
1111 return true;
1113 /* [temp.friend/6]
1114 A member of a class template may be declared to be a friend of a
1115 non-template class. In this case, the corresponding member of
1116 every specialization of the class template is a friend of the
1117 class granting friendship.
1119 For example, given a template friend declaration
1121 template <class T> friend void A<T>::f();
1123 the member function below is considered a friend
1125 template <> struct A<int> {
1126 void f();
1129 For this type of template friend, TEMPLATE_DEPTH below will be
1130 nonzero. To determine if DECL is a friend of FRIEND, we first
1131 check if the enclosing class is a specialization of another. */
1133 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1134 if (template_depth
1135 && DECL_CLASS_SCOPE_P (decl)
1136 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1137 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1139 /* Next, we check the members themselves. In order to handle
1140 a few tricky cases, such as when FRIEND_DECL's are
1142 template <class T> friend void A<T>::g(T t);
1143 template <class T> template <T t> friend void A<T>::h();
1145 and DECL's are
1147 void A<int>::g(int);
1148 template <int> void A<int>::h();
1150 we need to figure out ARGS, the template arguments from
1151 the context of DECL. This is required for template substitution
1152 of `T' in the function parameter of `g' and template parameter
1153 of `h' in the above examples. Here ARGS corresponds to `int'. */
1155 tree context = DECL_CONTEXT (decl);
1156 tree args = NULL_TREE;
1157 int current_depth = 0;
1159 while (current_depth < template_depth)
1161 if (CLASSTYPE_TEMPLATE_INFO (context))
1163 if (current_depth == 0)
1164 args = TYPE_TI_ARGS (context);
1165 else
1166 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1167 current_depth++;
1169 context = TYPE_CONTEXT (context);
1172 if (TREE_CODE (decl) == FUNCTION_DECL)
1174 bool is_template;
1175 tree friend_type;
1176 tree decl_type;
1177 tree friend_args_type;
1178 tree decl_args_type;
1180 /* Make sure that both DECL and FRIEND_DECL are templates or
1181 non-templates. */
1182 is_template = DECL_TEMPLATE_INFO (decl)
1183 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1184 if (need_template ^ is_template)
1185 return false;
1186 else if (is_template)
1188 /* If both are templates, check template parameter list. */
1189 tree friend_parms
1190 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1191 args, tf_none);
1192 if (!comp_template_parms
1193 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1194 friend_parms))
1195 return false;
1197 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1199 else
1200 decl_type = TREE_TYPE (decl);
1202 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1203 tf_none, NULL_TREE);
1204 if (friend_type == error_mark_node)
1205 return false;
1207 /* Check if return types match. */
1208 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1209 return false;
1211 /* Check if function parameter types match, ignoring the
1212 `this' parameter. */
1213 friend_args_type = TYPE_ARG_TYPES (friend_type);
1214 decl_args_type = TYPE_ARG_TYPES (decl_type);
1215 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1216 friend_args_type = TREE_CHAIN (friend_args_type);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1218 decl_args_type = TREE_CHAIN (decl_args_type);
1220 return compparms (decl_args_type, friend_args_type);
1222 else
1224 /* DECL is a TYPE_DECL */
1225 bool is_template;
1226 tree decl_type = TREE_TYPE (decl);
1228 /* Make sure that both DECL and FRIEND_DECL are templates or
1229 non-templates. */
1230 is_template
1231 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1232 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1234 if (need_template ^ is_template)
1235 return false;
1236 else if (is_template)
1238 tree friend_parms;
1239 /* If both are templates, check the name of the two
1240 TEMPLATE_DECL's first because is_friend didn't. */
1241 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1242 != DECL_NAME (friend_decl))
1243 return false;
1245 /* Now check template parameter list. */
1246 friend_parms
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1248 args, tf_none);
1249 return comp_template_parms
1250 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1251 friend_parms);
1253 else
1254 return (DECL_NAME (decl)
1255 == DECL_NAME (friend_decl));
1258 return false;
1261 /* Register the specialization SPEC as a specialization of TMPL with
1262 the indicated ARGS. IS_FRIEND indicates whether the specialization
1263 is actually just a friend declaration. Returns SPEC, or an
1264 equivalent prior declaration, if available. */
1266 static tree
1267 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1268 hashval_t hash)
1270 tree fn;
1271 spec_entry **slot = NULL;
1272 spec_entry elt;
1274 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1276 if (TREE_CODE (spec) == FUNCTION_DECL
1277 && uses_template_parms (DECL_TI_ARGS (spec)))
1278 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1279 register it; we want the corresponding TEMPLATE_DECL instead.
1280 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1281 the more obvious `uses_template_parms (spec)' to avoid problems
1282 with default function arguments. In particular, given
1283 something like this:
1285 template <class T> void f(T t1, T t = T())
1287 the default argument expression is not substituted for in an
1288 instantiation unless and until it is actually needed. */
1289 return spec;
1291 if (optimize_specialization_lookup_p (tmpl))
1292 /* We don't put these specializations in the hash table, but we might
1293 want to give an error about a mismatch. */
1294 fn = retrieve_specialization (tmpl, args, 0);
1295 else
1297 elt.tmpl = tmpl;
1298 elt.args = args;
1299 elt.spec = spec;
1301 if (hash == 0)
1302 hash = hash_specialization (&elt);
1304 slot = (spec_entry **)
1305 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1306 if (*slot)
1307 fn = (*slot)->spec;
1308 else
1309 fn = NULL_TREE;
1312 /* We can sometimes try to re-register a specialization that we've
1313 already got. In particular, regenerate_decl_from_template calls
1314 duplicate_decls which will update the specialization list. But,
1315 we'll still get called again here anyhow. It's more convenient
1316 to simply allow this than to try to prevent it. */
1317 if (fn == spec)
1318 return spec;
1319 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1321 if (DECL_TEMPLATE_INSTANTIATION (fn))
1323 if (DECL_ODR_USED (fn)
1324 || DECL_EXPLICIT_INSTANTIATION (fn))
1326 error ("specialization of %qD after instantiation",
1327 fn);
1328 return error_mark_node;
1330 else
1332 tree clone;
1333 /* This situation should occur only if the first
1334 specialization is an implicit instantiation, the
1335 second is an explicit specialization, and the
1336 implicit instantiation has not yet been used. That
1337 situation can occur if we have implicitly
1338 instantiated a member function and then specialized
1339 it later.
1341 We can also wind up here if a friend declaration that
1342 looked like an instantiation turns out to be a
1343 specialization:
1345 template <class T> void foo(T);
1346 class S { friend void foo<>(int) };
1347 template <> void foo(int);
1349 We transform the existing DECL in place so that any
1350 pointers to it become pointers to the updated
1351 declaration.
1353 If there was a definition for the template, but not
1354 for the specialization, we want this to look as if
1355 there were no definition, and vice versa. */
1356 DECL_INITIAL (fn) = NULL_TREE;
1357 duplicate_decls (spec, fn, is_friend);
1358 /* The call to duplicate_decls will have applied
1359 [temp.expl.spec]:
1361 An explicit specialization of a function template
1362 is inline only if it is explicitly declared to be,
1363 and independently of whether its function template
1366 to the primary function; now copy the inline bits to
1367 the various clones. */
1368 FOR_EACH_CLONE (clone, fn)
1370 DECL_DECLARED_INLINE_P (clone)
1371 = DECL_DECLARED_INLINE_P (fn);
1372 DECL_SOURCE_LOCATION (clone)
1373 = DECL_SOURCE_LOCATION (fn);
1375 check_specialization_namespace (fn);
1377 return fn;
1380 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1382 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1383 /* Dup decl failed, but this is a new definition. Set the
1384 line number so any errors match this new
1385 definition. */
1386 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1388 return fn;
1391 else if (fn)
1392 return duplicate_decls (spec, fn, is_friend);
1394 /* A specialization must be declared in the same namespace as the
1395 template it is specializing. */
1396 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1397 && !check_specialization_namespace (tmpl))
1398 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1400 if (!optimize_specialization_lookup_p (tmpl))
1402 gcc_assert (tmpl && args && spec);
1403 *slot = ggc_alloc_spec_entry ();
1404 **slot = elt;
1405 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1406 && PRIMARY_TEMPLATE_P (tmpl)
1407 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1408 /* TMPL is a forward declaration of a template function; keep a list
1409 of all specializations in case we need to reassign them to a friend
1410 template later in tsubst_friend_function. */
1411 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1412 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1415 return spec;
1418 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1419 TMPL and ARGS members, ignores SPEC. */
1421 static int
1422 eq_specializations (const void *p1, const void *p2)
1424 const spec_entry *e1 = (const spec_entry *)p1;
1425 const spec_entry *e2 = (const spec_entry *)p2;
1427 return (e1->tmpl == e2->tmpl
1428 && comp_template_args (e1->args, e2->args));
1431 /* Returns a hash for a template TMPL and template arguments ARGS. */
1433 static hashval_t
1434 hash_tmpl_and_args (tree tmpl, tree args)
1436 hashval_t val = DECL_UID (tmpl);
1437 return iterative_hash_template_arg (args, val);
1440 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1441 ignoring SPEC. */
1443 static hashval_t
1444 hash_specialization (const void *p)
1446 const spec_entry *e = (const spec_entry *)p;
1447 return hash_tmpl_and_args (e->tmpl, e->args);
1450 /* Recursively calculate a hash value for a template argument ARG, for use
1451 in the hash tables of template specializations. */
1453 static hashval_t
1454 iterative_hash_template_arg (tree arg, hashval_t val)
1456 unsigned HOST_WIDE_INT i;
1457 enum tree_code code;
1458 char tclass;
1460 if (arg == NULL_TREE)
1461 return iterative_hash_object (arg, val);
1463 if (!TYPE_P (arg))
1464 STRIP_NOPS (arg);
1466 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1467 /* We can get one of these when re-hashing a previous entry in the middle
1468 of substituting into a pack expansion. Just look through it. */
1469 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1471 code = TREE_CODE (arg);
1472 tclass = TREE_CODE_CLASS (code);
1474 val = iterative_hash_object (code, val);
1476 switch (code)
1478 case ERROR_MARK:
1479 return val;
1481 case IDENTIFIER_NODE:
1482 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1484 case TREE_VEC:
1486 int i, len = TREE_VEC_LENGTH (arg);
1487 for (i = 0; i < len; ++i)
1488 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1489 return val;
1492 case TYPE_PACK_EXPANSION:
1493 case EXPR_PACK_EXPANSION:
1494 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1496 case TYPE_ARGUMENT_PACK:
1497 case NONTYPE_ARGUMENT_PACK:
1498 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1500 case TREE_LIST:
1501 for (; arg; arg = TREE_CHAIN (arg))
1502 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1503 return val;
1505 case OVERLOAD:
1506 for (; arg; arg = OVL_CHAIN (arg))
1507 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1508 return val;
1510 case CONSTRUCTOR:
1512 tree field, value;
1513 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1515 val = iterative_hash_template_arg (field, val);
1516 val = iterative_hash_template_arg (value, val);
1518 return val;
1521 case PARM_DECL:
1522 if (!DECL_ARTIFICIAL (arg))
1523 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1524 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1526 case TARGET_EXPR:
1527 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1529 case PTRMEM_CST:
1530 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1531 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1533 case TEMPLATE_PARM_INDEX:
1534 val = iterative_hash_template_arg
1535 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1536 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1537 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1539 case TRAIT_EXPR:
1540 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1541 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1542 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1544 case BASELINK:
1545 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1546 val);
1547 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1548 val);
1550 case MODOP_EXPR:
1551 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1552 code = TREE_CODE (TREE_OPERAND (arg, 1));
1553 val = iterative_hash_object (code, val);
1554 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1556 case ARRAY_TYPE:
1557 /* layout_type sets structural equality for arrays of
1558 incomplete type, so we can't rely on the canonical type
1559 for hashing. */
1560 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1561 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1563 case LAMBDA_EXPR:
1564 /* A lambda can't appear in a template arg, but don't crash on
1565 erroneous input. */
1566 gcc_assert (seen_error ());
1567 return val;
1569 case CAST_EXPR:
1570 case STATIC_CAST_EXPR:
1571 case REINTERPRET_CAST_EXPR:
1572 case CONST_CAST_EXPR:
1573 case DYNAMIC_CAST_EXPR:
1574 case NEW_EXPR:
1575 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1576 /* Now hash operands as usual. */
1577 break;
1579 default:
1580 break;
1583 switch (tclass)
1585 case tcc_type:
1586 if (TYPE_CANONICAL (arg))
1587 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1588 val);
1589 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1590 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1591 /* Otherwise just compare the types during lookup. */
1592 return val;
1594 case tcc_declaration:
1595 case tcc_constant:
1596 return iterative_hash_expr (arg, val);
1598 default:
1599 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1601 unsigned n = TREE_OPERAND_LENGTH (arg);
1602 for (i = 0; i < n; ++i)
1603 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1604 return val;
1607 gcc_unreachable ();
1608 return 0;
1611 /* Unregister the specialization SPEC as a specialization of TMPL.
1612 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1613 if the SPEC was listed as a specialization of TMPL.
1615 Note that SPEC has been ggc_freed, so we can't look inside it. */
1617 bool
1618 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1620 spec_entry **slot;
1621 spec_entry elt;
1623 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1624 elt.args = TI_ARGS (tinfo);
1625 elt.spec = NULL_TREE;
1627 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1628 if (*slot)
1630 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1631 gcc_assert (new_spec != NULL_TREE);
1632 (*slot)->spec = new_spec;
1633 return 1;
1636 return 0;
1639 /* Compare an entry in the local specializations hash table P1 (which
1640 is really a pointer to a TREE_LIST) with P2 (which is really a
1641 DECL). */
1643 static int
1644 eq_local_specializations (const void *p1, const void *p2)
1646 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1649 /* Hash P1, an entry in the local specializations table. */
1651 static hashval_t
1652 hash_local_specialization (const void* p1)
1654 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1657 /* Like register_specialization, but for local declarations. We are
1658 registering SPEC, an instantiation of TMPL. */
1660 static void
1661 register_local_specialization (tree spec, tree tmpl)
1663 void **slot;
1665 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1666 htab_hash_pointer (tmpl), INSERT);
1667 *slot = build_tree_list (spec, tmpl);
1670 /* TYPE is a class type. Returns true if TYPE is an explicitly
1671 specialized class. */
1673 bool
1674 explicit_class_specialization_p (tree type)
1676 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1677 return false;
1678 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1681 /* Print the list of functions at FNS, going through all the overloads
1682 for each element of the list. Alternatively, FNS can not be a
1683 TREE_LIST, in which case it will be printed together with all the
1684 overloads.
1686 MORE and *STR should respectively be FALSE and NULL when the function
1687 is called from the outside. They are used internally on recursive
1688 calls. print_candidates manages the two parameters and leaves NULL
1689 in *STR when it ends. */
1691 static void
1692 print_candidates_1 (tree fns, bool more, const char **str)
1694 tree fn, fn2;
1695 char *spaces = NULL;
1697 for (fn = fns; fn; fn = OVL_NEXT (fn))
1698 if (TREE_CODE (fn) == TREE_LIST)
1700 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1701 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1702 print_candidates_1 (TREE_VALUE (fn2),
1703 TREE_CHAIN (fn2) || more, str);
1705 else
1707 if (!*str)
1709 /* Pick the prefix string. */
1710 if (!more && !OVL_NEXT (fns))
1712 error ("candidate is: %+#D", OVL_CURRENT (fn));
1713 continue;
1716 *str = _("candidates are:");
1717 spaces = get_spaces (*str);
1719 error ("%s %+#D", *str, OVL_CURRENT (fn));
1720 *str = spaces ? spaces : *str;
1723 if (!more)
1725 free (spaces);
1726 *str = NULL;
1730 /* Print the list of candidate FNS in an error message. */
1732 void
1733 print_candidates (tree fns)
1735 const char *str = NULL;
1736 print_candidates_1 (fns, false, &str);
1737 gcc_assert (str == NULL);
1740 /* Returns the template (one of the functions given by TEMPLATE_ID)
1741 which can be specialized to match the indicated DECL with the
1742 explicit template args given in TEMPLATE_ID. The DECL may be
1743 NULL_TREE if none is available. In that case, the functions in
1744 TEMPLATE_ID are non-members.
1746 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1747 specialization of a member template.
1749 The TEMPLATE_COUNT is the number of references to qualifying
1750 template classes that appeared in the name of the function. See
1751 check_explicit_specialization for a more accurate description.
1753 TSK indicates what kind of template declaration (if any) is being
1754 declared. TSK_TEMPLATE indicates that the declaration given by
1755 DECL, though a FUNCTION_DECL, has template parameters, and is
1756 therefore a template function.
1758 The template args (those explicitly specified and those deduced)
1759 are output in a newly created vector *TARGS_OUT.
1761 If it is impossible to determine the result, an error message is
1762 issued. The error_mark_node is returned to indicate failure. */
1764 static tree
1765 determine_specialization (tree template_id,
1766 tree decl,
1767 tree* targs_out,
1768 int need_member_template,
1769 int template_count,
1770 tmpl_spec_kind tsk)
1772 tree fns;
1773 tree targs;
1774 tree explicit_targs;
1775 tree candidates = NULL_TREE;
1776 /* A TREE_LIST of templates of which DECL may be a specialization.
1777 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1778 corresponding TREE_PURPOSE is the set of template arguments that,
1779 when used to instantiate the template, would produce a function
1780 with the signature of DECL. */
1781 tree templates = NULL_TREE;
1782 int header_count;
1783 struct cp_binding_level *b;
1785 *targs_out = NULL_TREE;
1787 if (template_id == error_mark_node || decl == error_mark_node)
1788 return error_mark_node;
1790 fns = TREE_OPERAND (template_id, 0);
1791 explicit_targs = TREE_OPERAND (template_id, 1);
1793 if (fns == error_mark_node)
1794 return error_mark_node;
1796 /* Check for baselinks. */
1797 if (BASELINK_P (fns))
1798 fns = BASELINK_FUNCTIONS (fns);
1800 if (!is_overloaded_fn (fns))
1802 error ("%qD is not a function template", fns);
1803 return error_mark_node;
1806 /* Count the number of template headers specified for this
1807 specialization. */
1808 header_count = 0;
1809 for (b = current_binding_level;
1810 b->kind == sk_template_parms;
1811 b = b->level_chain)
1812 ++header_count;
1814 for (; fns; fns = OVL_NEXT (fns))
1816 tree fn = OVL_CURRENT (fns);
1818 if (TREE_CODE (fn) == TEMPLATE_DECL)
1820 tree decl_arg_types;
1821 tree fn_arg_types;
1823 /* In case of explicit specialization, we need to check if
1824 the number of template headers appearing in the specialization
1825 is correct. This is usually done in check_explicit_specialization,
1826 but the check done there cannot be exhaustive when specializing
1827 member functions. Consider the following code:
1829 template <> void A<int>::f(int);
1830 template <> template <> void A<int>::f(int);
1832 Assuming that A<int> is not itself an explicit specialization
1833 already, the first line specializes "f" which is a non-template
1834 member function, whilst the second line specializes "f" which
1835 is a template member function. So both lines are syntactically
1836 correct, and check_explicit_specialization does not reject
1837 them.
1839 Here, we can do better, as we are matching the specialization
1840 against the declarations. We count the number of template
1841 headers, and we check if they match TEMPLATE_COUNT + 1
1842 (TEMPLATE_COUNT is the number of qualifying template classes,
1843 plus there must be another header for the member template
1844 itself).
1846 Notice that if header_count is zero, this is not a
1847 specialization but rather a template instantiation, so there
1848 is no check we can perform here. */
1849 if (header_count && header_count != template_count + 1)
1850 continue;
1852 /* Check that the number of template arguments at the
1853 innermost level for DECL is the same as for FN. */
1854 if (current_binding_level->kind == sk_template_parms
1855 && !current_binding_level->explicit_spec_p
1856 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1857 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1858 (current_template_parms))))
1859 continue;
1861 /* DECL might be a specialization of FN. */
1862 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1863 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1865 /* For a non-static member function, we need to make sure
1866 that the const qualification is the same. Since
1867 get_bindings does not try to merge the "this" parameter,
1868 we must do the comparison explicitly. */
1869 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1870 && !same_type_p (TREE_VALUE (fn_arg_types),
1871 TREE_VALUE (decl_arg_types)))
1872 continue;
1874 /* Skip the "this" parameter and, for constructors of
1875 classes with virtual bases, the VTT parameter. A
1876 full specialization of a constructor will have a VTT
1877 parameter, but a template never will. */
1878 decl_arg_types
1879 = skip_artificial_parms_for (decl, decl_arg_types);
1880 fn_arg_types
1881 = skip_artificial_parms_for (fn, fn_arg_types);
1883 /* Check that the number of function parameters matches.
1884 For example,
1885 template <class T> void f(int i = 0);
1886 template <> void f<int>();
1887 The specialization f<int> is invalid but is not caught
1888 by get_bindings below. */
1889 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1890 continue;
1892 /* Function templates cannot be specializations; there are
1893 no partial specializations of functions. Therefore, if
1894 the type of DECL does not match FN, there is no
1895 match. */
1896 if (tsk == tsk_template)
1898 if (compparms (fn_arg_types, decl_arg_types))
1899 candidates = tree_cons (NULL_TREE, fn, candidates);
1900 continue;
1903 /* See whether this function might be a specialization of this
1904 template. */
1905 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1907 if (!targs)
1908 /* We cannot deduce template arguments that when used to
1909 specialize TMPL will produce DECL. */
1910 continue;
1912 /* Save this template, and the arguments deduced. */
1913 templates = tree_cons (targs, fn, templates);
1915 else if (need_member_template)
1916 /* FN is an ordinary member function, and we need a
1917 specialization of a member template. */
1919 else if (TREE_CODE (fn) != FUNCTION_DECL)
1920 /* We can get IDENTIFIER_NODEs here in certain erroneous
1921 cases. */
1923 else if (!DECL_FUNCTION_MEMBER_P (fn))
1924 /* This is just an ordinary non-member function. Nothing can
1925 be a specialization of that. */
1927 else if (DECL_ARTIFICIAL (fn))
1928 /* Cannot specialize functions that are created implicitly. */
1930 else
1932 tree decl_arg_types;
1934 /* This is an ordinary member function. However, since
1935 we're here, we can assume it's enclosing class is a
1936 template class. For example,
1938 template <typename T> struct S { void f(); };
1939 template <> void S<int>::f() {}
1941 Here, S<int>::f is a non-template, but S<int> is a
1942 template class. If FN has the same type as DECL, we
1943 might be in business. */
1945 if (!DECL_TEMPLATE_INFO (fn))
1946 /* Its enclosing class is an explicit specialization
1947 of a template class. This is not a candidate. */
1948 continue;
1950 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1951 TREE_TYPE (TREE_TYPE (fn))))
1952 /* The return types differ. */
1953 continue;
1955 /* Adjust the type of DECL in case FN is a static member. */
1956 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1957 if (DECL_STATIC_FUNCTION_P (fn)
1958 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1959 decl_arg_types = TREE_CHAIN (decl_arg_types);
1961 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1962 decl_arg_types))
1963 /* They match! */
1964 candidates = tree_cons (NULL_TREE, fn, candidates);
1968 if (templates && TREE_CHAIN (templates))
1970 /* We have:
1972 [temp.expl.spec]
1974 It is possible for a specialization with a given function
1975 signature to be instantiated from more than one function
1976 template. In such cases, explicit specification of the
1977 template arguments must be used to uniquely identify the
1978 function template specialization being specialized.
1980 Note that here, there's no suggestion that we're supposed to
1981 determine which of the candidate templates is most
1982 specialized. However, we, also have:
1984 [temp.func.order]
1986 Partial ordering of overloaded function template
1987 declarations is used in the following contexts to select
1988 the function template to which a function template
1989 specialization refers:
1991 -- when an explicit specialization refers to a function
1992 template.
1994 So, we do use the partial ordering rules, at least for now.
1995 This extension can only serve to make invalid programs valid,
1996 so it's safe. And, there is strong anecdotal evidence that
1997 the committee intended the partial ordering rules to apply;
1998 the EDG front end has that behavior, and John Spicer claims
1999 that the committee simply forgot to delete the wording in
2000 [temp.expl.spec]. */
2001 tree tmpl = most_specialized_instantiation (templates);
2002 if (tmpl != error_mark_node)
2004 templates = tmpl;
2005 TREE_CHAIN (templates) = NULL_TREE;
2009 if (templates == NULL_TREE && candidates == NULL_TREE)
2011 error ("template-id %qD for %q+D does not match any template "
2012 "declaration", template_id, decl);
2013 if (header_count && header_count != template_count + 1)
2014 inform (input_location, "saw %d %<template<>%>, need %d for "
2015 "specializing a member function template",
2016 header_count, template_count + 1);
2017 return error_mark_node;
2019 else if ((templates && TREE_CHAIN (templates))
2020 || (candidates && TREE_CHAIN (candidates))
2021 || (templates && candidates))
2023 error ("ambiguous template specialization %qD for %q+D",
2024 template_id, decl);
2025 candidates = chainon (candidates, templates);
2026 print_candidates (candidates);
2027 return error_mark_node;
2030 /* We have one, and exactly one, match. */
2031 if (candidates)
2033 tree fn = TREE_VALUE (candidates);
2034 *targs_out = copy_node (DECL_TI_ARGS (fn));
2035 /* DECL is a re-declaration or partial instantiation of a template
2036 function. */
2037 if (TREE_CODE (fn) == TEMPLATE_DECL)
2038 return fn;
2039 /* It was a specialization of an ordinary member function in a
2040 template class. */
2041 return DECL_TI_TEMPLATE (fn);
2044 /* It was a specialization of a template. */
2045 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2046 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2048 *targs_out = copy_node (targs);
2049 SET_TMPL_ARGS_LEVEL (*targs_out,
2050 TMPL_ARGS_DEPTH (*targs_out),
2051 TREE_PURPOSE (templates));
2053 else
2054 *targs_out = TREE_PURPOSE (templates);
2055 return TREE_VALUE (templates);
2058 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2059 but with the default argument values filled in from those in the
2060 TMPL_TYPES. */
2062 static tree
2063 copy_default_args_to_explicit_spec_1 (tree spec_types,
2064 tree tmpl_types)
2066 tree new_spec_types;
2068 if (!spec_types)
2069 return NULL_TREE;
2071 if (spec_types == void_list_node)
2072 return void_list_node;
2074 /* Substitute into the rest of the list. */
2075 new_spec_types =
2076 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2077 TREE_CHAIN (tmpl_types));
2079 /* Add the default argument for this parameter. */
2080 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2081 TREE_VALUE (spec_types),
2082 new_spec_types);
2085 /* DECL is an explicit specialization. Replicate default arguments
2086 from the template it specializes. (That way, code like:
2088 template <class T> void f(T = 3);
2089 template <> void f(double);
2090 void g () { f (); }
2092 works, as required.) An alternative approach would be to look up
2093 the correct default arguments at the call-site, but this approach
2094 is consistent with how implicit instantiations are handled. */
2096 static void
2097 copy_default_args_to_explicit_spec (tree decl)
2099 tree tmpl;
2100 tree spec_types;
2101 tree tmpl_types;
2102 tree new_spec_types;
2103 tree old_type;
2104 tree new_type;
2105 tree t;
2106 tree object_type = NULL_TREE;
2107 tree in_charge = NULL_TREE;
2108 tree vtt = NULL_TREE;
2110 /* See if there's anything we need to do. */
2111 tmpl = DECL_TI_TEMPLATE (decl);
2112 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2113 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2114 if (TREE_PURPOSE (t))
2115 break;
2116 if (!t)
2117 return;
2119 old_type = TREE_TYPE (decl);
2120 spec_types = TYPE_ARG_TYPES (old_type);
2122 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2124 /* Remove the this pointer, but remember the object's type for
2125 CV quals. */
2126 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2127 spec_types = TREE_CHAIN (spec_types);
2128 tmpl_types = TREE_CHAIN (tmpl_types);
2130 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2132 /* DECL may contain more parameters than TMPL due to the extra
2133 in-charge parameter in constructors and destructors. */
2134 in_charge = spec_types;
2135 spec_types = TREE_CHAIN (spec_types);
2137 if (DECL_HAS_VTT_PARM_P (decl))
2139 vtt = spec_types;
2140 spec_types = TREE_CHAIN (spec_types);
2144 /* Compute the merged default arguments. */
2145 new_spec_types =
2146 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2148 /* Compute the new FUNCTION_TYPE. */
2149 if (object_type)
2151 if (vtt)
2152 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2153 TREE_VALUE (vtt),
2154 new_spec_types);
2156 if (in_charge)
2157 /* Put the in-charge parameter back. */
2158 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2159 TREE_VALUE (in_charge),
2160 new_spec_types);
2162 new_type = build_method_type_directly (object_type,
2163 TREE_TYPE (old_type),
2164 new_spec_types);
2166 else
2167 new_type = build_function_type (TREE_TYPE (old_type),
2168 new_spec_types);
2169 new_type = cp_build_type_attribute_variant (new_type,
2170 TYPE_ATTRIBUTES (old_type));
2171 new_type = build_exception_variant (new_type,
2172 TYPE_RAISES_EXCEPTIONS (old_type));
2173 TREE_TYPE (decl) = new_type;
2176 /* Check to see if the function just declared, as indicated in
2177 DECLARATOR, and in DECL, is a specialization of a function
2178 template. We may also discover that the declaration is an explicit
2179 instantiation at this point.
2181 Returns DECL, or an equivalent declaration that should be used
2182 instead if all goes well. Issues an error message if something is
2183 amiss. Returns error_mark_node if the error is not easily
2184 recoverable.
2186 FLAGS is a bitmask consisting of the following flags:
2188 2: The function has a definition.
2189 4: The function is a friend.
2191 The TEMPLATE_COUNT is the number of references to qualifying
2192 template classes that appeared in the name of the function. For
2193 example, in
2195 template <class T> struct S { void f(); };
2196 void S<int>::f();
2198 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2199 classes are not counted in the TEMPLATE_COUNT, so that in
2201 template <class T> struct S {};
2202 template <> struct S<int> { void f(); }
2203 template <> void S<int>::f();
2205 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2206 invalid; there should be no template <>.)
2208 If the function is a specialization, it is marked as such via
2209 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2210 is set up correctly, and it is added to the list of specializations
2211 for that template. */
2213 tree
2214 check_explicit_specialization (tree declarator,
2215 tree decl,
2216 int template_count,
2217 int flags)
2219 int have_def = flags & 2;
2220 int is_friend = flags & 4;
2221 int specialization = 0;
2222 int explicit_instantiation = 0;
2223 int member_specialization = 0;
2224 tree ctype = DECL_CLASS_CONTEXT (decl);
2225 tree dname = DECL_NAME (decl);
2226 tmpl_spec_kind tsk;
2228 if (is_friend)
2230 if (!processing_specialization)
2231 tsk = tsk_none;
2232 else
2233 tsk = tsk_excessive_parms;
2235 else
2236 tsk = current_tmpl_spec_kind (template_count);
2238 switch (tsk)
2240 case tsk_none:
2241 if (processing_specialization)
2243 specialization = 1;
2244 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2246 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2248 if (is_friend)
2249 /* This could be something like:
2251 template <class T> void f(T);
2252 class S { friend void f<>(int); } */
2253 specialization = 1;
2254 else
2256 /* This case handles bogus declarations like template <>
2257 template <class T> void f<int>(); */
2259 error ("template-id %qD in declaration of primary template",
2260 declarator);
2261 return decl;
2264 break;
2266 case tsk_invalid_member_spec:
2267 /* The error has already been reported in
2268 check_specialization_scope. */
2269 return error_mark_node;
2271 case tsk_invalid_expl_inst:
2272 error ("template parameter list used in explicit instantiation");
2274 /* Fall through. */
2276 case tsk_expl_inst:
2277 if (have_def)
2278 error ("definition provided for explicit instantiation");
2280 explicit_instantiation = 1;
2281 break;
2283 case tsk_excessive_parms:
2284 case tsk_insufficient_parms:
2285 if (tsk == tsk_excessive_parms)
2286 error ("too many template parameter lists in declaration of %qD",
2287 decl);
2288 else if (template_header_count)
2289 error("too few template parameter lists in declaration of %qD", decl);
2290 else
2291 error("explicit specialization of %qD must be introduced by "
2292 "%<template <>%>", decl);
2294 /* Fall through. */
2295 case tsk_expl_spec:
2296 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2297 if (ctype)
2298 member_specialization = 1;
2299 else
2300 specialization = 1;
2301 break;
2303 case tsk_template:
2304 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2306 /* This case handles bogus declarations like template <>
2307 template <class T> void f<int>(); */
2309 if (uses_template_parms (declarator))
2310 error ("function template partial specialization %qD "
2311 "is not allowed", declarator);
2312 else
2313 error ("template-id %qD in declaration of primary template",
2314 declarator);
2315 return decl;
2318 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2319 /* This is a specialization of a member template, without
2320 specialization the containing class. Something like:
2322 template <class T> struct S {
2323 template <class U> void f (U);
2325 template <> template <class U> void S<int>::f(U) {}
2327 That's a specialization -- but of the entire template. */
2328 specialization = 1;
2329 break;
2331 default:
2332 gcc_unreachable ();
2335 if (specialization || member_specialization)
2337 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2338 for (; t; t = TREE_CHAIN (t))
2339 if (TREE_PURPOSE (t))
2341 permerror (input_location,
2342 "default argument specified in explicit specialization");
2343 break;
2347 if (specialization || member_specialization || explicit_instantiation)
2349 tree tmpl = NULL_TREE;
2350 tree targs = NULL_TREE;
2352 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2353 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2355 tree fns;
2357 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2358 if (ctype)
2359 fns = dname;
2360 else
2362 /* If there is no class context, the explicit instantiation
2363 must be at namespace scope. */
2364 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2366 /* Find the namespace binding, using the declaration
2367 context. */
2368 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2369 false, true);
2370 if (fns == error_mark_node || !is_overloaded_fn (fns))
2372 error ("%qD is not a template function", dname);
2373 fns = error_mark_node;
2375 else
2377 tree fn = OVL_CURRENT (fns);
2378 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2379 CP_DECL_CONTEXT (fn)))
2380 error ("%qD is not declared in %qD",
2381 decl, current_namespace);
2385 declarator = lookup_template_function (fns, NULL_TREE);
2388 if (declarator == error_mark_node)
2389 return error_mark_node;
2391 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2393 if (!explicit_instantiation)
2394 /* A specialization in class scope. This is invalid,
2395 but the error will already have been flagged by
2396 check_specialization_scope. */
2397 return error_mark_node;
2398 else
2400 /* It's not valid to write an explicit instantiation in
2401 class scope, e.g.:
2403 class C { template void f(); }
2405 This case is caught by the parser. However, on
2406 something like:
2408 template class C { void f(); };
2410 (which is invalid) we can get here. The error will be
2411 issued later. */
2415 return decl;
2417 else if (ctype != NULL_TREE
2418 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2419 IDENTIFIER_NODE))
2421 /* Find the list of functions in ctype that have the same
2422 name as the declared function. */
2423 tree name = TREE_OPERAND (declarator, 0);
2424 tree fns = NULL_TREE;
2425 int idx;
2427 if (constructor_name_p (name, ctype))
2429 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2431 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2432 : !CLASSTYPE_DESTRUCTORS (ctype))
2434 /* From [temp.expl.spec]:
2436 If such an explicit specialization for the member
2437 of a class template names an implicitly-declared
2438 special member function (clause _special_), the
2439 program is ill-formed.
2441 Similar language is found in [temp.explicit]. */
2442 error ("specialization of implicitly-declared special member function");
2443 return error_mark_node;
2446 name = is_constructor ? ctor_identifier : dtor_identifier;
2449 if (!DECL_CONV_FN_P (decl))
2451 idx = lookup_fnfields_1 (ctype, name);
2452 if (idx >= 0)
2453 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2455 else
2457 VEC(tree,gc) *methods;
2458 tree ovl;
2460 /* For a type-conversion operator, we cannot do a
2461 name-based lookup. We might be looking for `operator
2462 int' which will be a specialization of `operator T'.
2463 So, we find *all* the conversion operators, and then
2464 select from them. */
2465 fns = NULL_TREE;
2467 methods = CLASSTYPE_METHOD_VEC (ctype);
2468 if (methods)
2469 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2470 VEC_iterate (tree, methods, idx, ovl);
2471 ++idx)
2473 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2474 /* There are no more conversion functions. */
2475 break;
2477 /* Glue all these conversion functions together
2478 with those we already have. */
2479 for (; ovl; ovl = OVL_NEXT (ovl))
2480 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2484 if (fns == NULL_TREE)
2486 error ("no member function %qD declared in %qT", name, ctype);
2487 return error_mark_node;
2489 else
2490 TREE_OPERAND (declarator, 0) = fns;
2493 /* Figure out what exactly is being specialized at this point.
2494 Note that for an explicit instantiation, even one for a
2495 member function, we cannot tell apriori whether the
2496 instantiation is for a member template, or just a member
2497 function of a template class. Even if a member template is
2498 being instantiated, the member template arguments may be
2499 elided if they can be deduced from the rest of the
2500 declaration. */
2501 tmpl = determine_specialization (declarator, decl,
2502 &targs,
2503 member_specialization,
2504 template_count,
2505 tsk);
2507 if (!tmpl || tmpl == error_mark_node)
2508 /* We couldn't figure out what this declaration was
2509 specializing. */
2510 return error_mark_node;
2511 else
2513 tree gen_tmpl = most_general_template (tmpl);
2515 if (explicit_instantiation)
2517 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2518 is done by do_decl_instantiation later. */
2520 int arg_depth = TMPL_ARGS_DEPTH (targs);
2521 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2523 if (arg_depth > parm_depth)
2525 /* If TMPL is not the most general template (for
2526 example, if TMPL is a friend template that is
2527 injected into namespace scope), then there will
2528 be too many levels of TARGS. Remove some of them
2529 here. */
2530 int i;
2531 tree new_targs;
2533 new_targs = make_tree_vec (parm_depth);
2534 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2535 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2536 = TREE_VEC_ELT (targs, i);
2537 targs = new_targs;
2540 return instantiate_template (tmpl, targs, tf_error);
2543 /* If we thought that the DECL was a member function, but it
2544 turns out to be specializing a static member function,
2545 make DECL a static member function as well. */
2546 if (DECL_STATIC_FUNCTION_P (tmpl)
2547 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2548 revert_static_member_fn (decl);
2550 /* If this is a specialization of a member template of a
2551 template class, we want to return the TEMPLATE_DECL, not
2552 the specialization of it. */
2553 if (tsk == tsk_template)
2555 tree result = DECL_TEMPLATE_RESULT (tmpl);
2556 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2557 DECL_INITIAL (result) = NULL_TREE;
2558 if (have_def)
2560 tree parm;
2561 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2562 DECL_SOURCE_LOCATION (result)
2563 = DECL_SOURCE_LOCATION (decl);
2564 /* We want to use the argument list specified in the
2565 definition, not in the original declaration. */
2566 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2567 for (parm = DECL_ARGUMENTS (result); parm;
2568 parm = DECL_CHAIN (parm))
2569 DECL_CONTEXT (parm) = result;
2571 return register_specialization (tmpl, gen_tmpl, targs,
2572 is_friend, 0);
2575 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2576 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2578 /* Inherit default function arguments from the template
2579 DECL is specializing. */
2580 copy_default_args_to_explicit_spec (decl);
2582 /* This specialization has the same protection as the
2583 template it specializes. */
2584 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2585 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2587 /* 7.1.1-1 [dcl.stc]
2589 A storage-class-specifier shall not be specified in an
2590 explicit specialization...
2592 The parser rejects these, so unless action is taken here,
2593 explicit function specializations will always appear with
2594 global linkage.
2596 The action recommended by the C++ CWG in response to C++
2597 defect report 605 is to make the storage class and linkage
2598 of the explicit specialization match the templated function:
2600 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2602 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2604 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2605 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2607 /* This specialization has the same linkage and visibility as
2608 the function template it specializes. */
2609 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2610 if (! TREE_PUBLIC (decl))
2612 DECL_INTERFACE_KNOWN (decl) = 1;
2613 DECL_NOT_REALLY_EXTERN (decl) = 1;
2615 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2616 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2618 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2619 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2623 /* If DECL is a friend declaration, declared using an
2624 unqualified name, the namespace associated with DECL may
2625 have been set incorrectly. For example, in:
2627 template <typename T> void f(T);
2628 namespace N {
2629 struct S { friend void f<int>(int); }
2632 we will have set the DECL_CONTEXT for the friend
2633 declaration to N, rather than to the global namespace. */
2634 if (DECL_NAMESPACE_SCOPE_P (decl))
2635 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2637 if (is_friend && !have_def)
2638 /* This is not really a declaration of a specialization.
2639 It's just the name of an instantiation. But, it's not
2640 a request for an instantiation, either. */
2641 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2642 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2643 /* This is indeed a specialization. In case of constructors
2644 and destructors, we need in-charge and not-in-charge
2645 versions in V3 ABI. */
2646 clone_function_decl (decl, /*update_method_vec_p=*/0);
2648 /* Register this specialization so that we can find it
2649 again. */
2650 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2654 return decl;
2657 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2658 parameters. These are represented in the same format used for
2659 DECL_TEMPLATE_PARMS. */
2662 comp_template_parms (const_tree parms1, const_tree parms2)
2664 const_tree p1;
2665 const_tree p2;
2667 if (parms1 == parms2)
2668 return 1;
2670 for (p1 = parms1, p2 = parms2;
2671 p1 != NULL_TREE && p2 != NULL_TREE;
2672 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2674 tree t1 = TREE_VALUE (p1);
2675 tree t2 = TREE_VALUE (p2);
2676 int i;
2678 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2679 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2681 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2682 return 0;
2684 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2686 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2687 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2689 /* If either of the template parameters are invalid, assume
2690 they match for the sake of error recovery. */
2691 if (parm1 == error_mark_node || parm2 == error_mark_node)
2692 return 1;
2694 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2695 return 0;
2697 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2698 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2699 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2700 continue;
2701 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2702 return 0;
2706 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2707 /* One set of parameters has more parameters lists than the
2708 other. */
2709 return 0;
2711 return 1;
2714 /* Determine whether PARM is a parameter pack. */
2716 bool
2717 template_parameter_pack_p (const_tree parm)
2719 /* Determine if we have a non-type template parameter pack. */
2720 if (TREE_CODE (parm) == PARM_DECL)
2721 return (DECL_TEMPLATE_PARM_P (parm)
2722 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2724 /* If this is a list of template parameters, we could get a
2725 TYPE_DECL or a TEMPLATE_DECL. */
2726 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2727 parm = TREE_TYPE (parm);
2729 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2730 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2731 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2734 /* Determine if T is a function parameter pack. */
2736 bool
2737 function_parameter_pack_p (const_tree t)
2739 if (t && TREE_CODE (t) == PARM_DECL)
2740 return FUNCTION_PARAMETER_PACK_P (t);
2741 return false;
2744 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2745 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2747 tree
2748 get_function_template_decl (const_tree primary_func_tmpl_inst)
2750 if (! primary_func_tmpl_inst
2751 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2752 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2753 return NULL;
2755 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2758 /* Return true iff the function parameter PARAM_DECL was expanded
2759 from the function parameter pack PACK. */
2761 bool
2762 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2764 if (DECL_ARTIFICIAL (param_decl)
2765 || !function_parameter_pack_p (pack))
2766 return false;
2768 /* The parameter pack and its pack arguments have the same
2769 DECL_PARM_INDEX. */
2770 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2773 /* Determine whether ARGS describes a variadic template args list,
2774 i.e., one that is terminated by a template argument pack. */
2776 static bool
2777 template_args_variadic_p (tree args)
2779 int nargs;
2780 tree last_parm;
2782 if (args == NULL_TREE)
2783 return false;
2785 args = INNERMOST_TEMPLATE_ARGS (args);
2786 nargs = TREE_VEC_LENGTH (args);
2788 if (nargs == 0)
2789 return false;
2791 last_parm = TREE_VEC_ELT (args, nargs - 1);
2793 return ARGUMENT_PACK_P (last_parm);
2796 /* Generate a new name for the parameter pack name NAME (an
2797 IDENTIFIER_NODE) that incorporates its */
2799 static tree
2800 make_ith_pack_parameter_name (tree name, int i)
2802 /* Munge the name to include the parameter index. */
2803 #define NUMBUF_LEN 128
2804 char numbuf[NUMBUF_LEN];
2805 char* newname;
2806 int newname_len;
2808 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2809 newname_len = IDENTIFIER_LENGTH (name)
2810 + strlen (numbuf) + 2;
2811 newname = (char*)alloca (newname_len);
2812 snprintf (newname, newname_len,
2813 "%s#%i", IDENTIFIER_POINTER (name), i);
2814 return get_identifier (newname);
2817 /* Return true if T is a primary function
2818 or class template instantiation. */
2820 bool
2821 primary_template_instantiation_p (const_tree t)
2823 if (!t)
2824 return false;
2826 if (TREE_CODE (t) == FUNCTION_DECL)
2827 return DECL_LANG_SPECIFIC (t)
2828 && DECL_TEMPLATE_INSTANTIATION (t)
2829 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2830 else if (CLASS_TYPE_P (t))
2831 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2832 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2833 return false;
2836 /* Return true if PARM is a template template parameter. */
2838 bool
2839 template_template_parameter_p (const_tree parm)
2841 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2844 /* Return the template parameters of T if T is a
2845 primary template instantiation, NULL otherwise. */
2847 tree
2848 get_primary_template_innermost_parameters (const_tree t)
2850 tree parms = NULL, template_info = NULL;
2852 if ((template_info = get_template_info (t))
2853 && primary_template_instantiation_p (t))
2854 parms = INNERMOST_TEMPLATE_PARMS
2855 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2857 return parms;
2860 /* Return the template parameters of the LEVELth level from the full list
2861 of template parameters PARMS. */
2863 tree
2864 get_template_parms_at_level (tree parms, int level)
2866 tree p;
2867 if (!parms
2868 || TREE_CODE (parms) != TREE_LIST
2869 || level > TMPL_PARMS_DEPTH (parms))
2870 return NULL_TREE;
2872 for (p = parms; p; p = TREE_CHAIN (p))
2873 if (TMPL_PARMS_DEPTH (p) == level)
2874 return p;
2876 return NULL_TREE;
2879 /* Returns the template arguments of T if T is a template instantiation,
2880 NULL otherwise. */
2882 tree
2883 get_template_innermost_arguments (const_tree t)
2885 tree args = NULL, template_info = NULL;
2887 if ((template_info = get_template_info (t))
2888 && TI_ARGS (template_info))
2889 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2891 return args;
2894 /* Return the argument pack elements of T if T is a template argument pack,
2895 NULL otherwise. */
2897 tree
2898 get_template_argument_pack_elems (const_tree t)
2900 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2901 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2902 return NULL;
2904 return ARGUMENT_PACK_ARGS (t);
2907 /* Structure used to track the progress of find_parameter_packs_r. */
2908 struct find_parameter_pack_data
2910 /* TREE_LIST that will contain all of the parameter packs found by
2911 the traversal. */
2912 tree* parameter_packs;
2914 /* Set of AST nodes that have been visited by the traversal. */
2915 struct pointer_set_t *visited;
2918 /* Identifies all of the argument packs that occur in a template
2919 argument and appends them to the TREE_LIST inside DATA, which is a
2920 find_parameter_pack_data structure. This is a subroutine of
2921 make_pack_expansion and uses_parameter_packs. */
2922 static tree
2923 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2925 tree t = *tp;
2926 struct find_parameter_pack_data* ppd =
2927 (struct find_parameter_pack_data*)data;
2928 bool parameter_pack_p = false;
2930 /* Identify whether this is a parameter pack or not. */
2931 switch (TREE_CODE (t))
2933 case TEMPLATE_PARM_INDEX:
2934 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2935 parameter_pack_p = true;
2936 break;
2938 case TEMPLATE_TYPE_PARM:
2939 case TEMPLATE_TEMPLATE_PARM:
2940 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2941 parameter_pack_p = true;
2942 break;
2944 case PARM_DECL:
2945 if (FUNCTION_PARAMETER_PACK_P (t))
2947 /* We don't want to walk into the type of a PARM_DECL,
2948 because we don't want to see the type parameter pack. */
2949 *walk_subtrees = 0;
2950 parameter_pack_p = true;
2952 break;
2954 default:
2955 /* Not a parameter pack. */
2956 break;
2959 if (parameter_pack_p)
2961 /* Add this parameter pack to the list. */
2962 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2965 if (TYPE_P (t))
2966 cp_walk_tree (&TYPE_CONTEXT (t),
2967 &find_parameter_packs_r, ppd, ppd->visited);
2969 /* This switch statement will return immediately if we don't find a
2970 parameter pack. */
2971 switch (TREE_CODE (t))
2973 case TEMPLATE_PARM_INDEX:
2974 return NULL_TREE;
2976 case BOUND_TEMPLATE_TEMPLATE_PARM:
2977 /* Check the template itself. */
2978 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2979 &find_parameter_packs_r, ppd, ppd->visited);
2980 /* Check the template arguments. */
2981 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2982 ppd->visited);
2983 *walk_subtrees = 0;
2984 return NULL_TREE;
2986 case TEMPLATE_TYPE_PARM:
2987 case TEMPLATE_TEMPLATE_PARM:
2988 return NULL_TREE;
2990 case PARM_DECL:
2991 return NULL_TREE;
2993 case RECORD_TYPE:
2994 if (TYPE_PTRMEMFUNC_P (t))
2995 return NULL_TREE;
2996 /* Fall through. */
2998 case UNION_TYPE:
2999 case ENUMERAL_TYPE:
3000 if (TYPE_TEMPLATE_INFO (t))
3001 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3002 &find_parameter_packs_r, ppd, ppd->visited);
3004 *walk_subtrees = 0;
3005 return NULL_TREE;
3007 case TEMPLATE_DECL:
3008 cp_walk_tree (&TREE_TYPE (t),
3009 &find_parameter_packs_r, ppd, ppd->visited);
3010 return NULL_TREE;
3012 case TYPENAME_TYPE:
3013 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3014 ppd, ppd->visited);
3015 *walk_subtrees = 0;
3016 return NULL_TREE;
3018 case TYPE_PACK_EXPANSION:
3019 case EXPR_PACK_EXPANSION:
3020 *walk_subtrees = 0;
3021 return NULL_TREE;
3023 case INTEGER_TYPE:
3024 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3025 ppd, ppd->visited);
3026 *walk_subtrees = 0;
3027 return NULL_TREE;
3029 case IDENTIFIER_NODE:
3030 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3031 ppd->visited);
3032 *walk_subtrees = 0;
3033 return NULL_TREE;
3035 default:
3036 return NULL_TREE;
3039 return NULL_TREE;
3042 /* Determines if the expression or type T uses any parameter packs. */
3043 bool
3044 uses_parameter_packs (tree t)
3046 tree parameter_packs = NULL_TREE;
3047 struct find_parameter_pack_data ppd;
3048 ppd.parameter_packs = &parameter_packs;
3049 ppd.visited = pointer_set_create ();
3050 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3051 pointer_set_destroy (ppd.visited);
3052 return parameter_packs != NULL_TREE;
3055 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3056 representation a base-class initializer into a parameter pack
3057 expansion. If all goes well, the resulting node will be an
3058 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3059 respectively. */
3060 tree
3061 make_pack_expansion (tree arg)
3063 tree result;
3064 tree parameter_packs = NULL_TREE;
3065 bool for_types = false;
3066 struct find_parameter_pack_data ppd;
3068 if (!arg || arg == error_mark_node)
3069 return arg;
3071 if (TREE_CODE (arg) == TREE_LIST)
3073 /* The only time we will see a TREE_LIST here is for a base
3074 class initializer. In this case, the TREE_PURPOSE will be a
3075 _TYPE node (representing the base class expansion we're
3076 initializing) and the TREE_VALUE will be a TREE_LIST
3077 containing the initialization arguments.
3079 The resulting expansion looks somewhat different from most
3080 expansions. Rather than returning just one _EXPANSION, we
3081 return a TREE_LIST whose TREE_PURPOSE is a
3082 TYPE_PACK_EXPANSION containing the bases that will be
3083 initialized. The TREE_VALUE will be identical to the
3084 original TREE_VALUE, which is a list of arguments that will
3085 be passed to each base. We do not introduce any new pack
3086 expansion nodes into the TREE_VALUE (although it is possible
3087 that some already exist), because the TREE_PURPOSE and
3088 TREE_VALUE all need to be expanded together with the same
3089 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3090 resulting TREE_PURPOSE will mention the parameter packs in
3091 both the bases and the arguments to the bases. */
3092 tree purpose;
3093 tree value;
3094 tree parameter_packs = NULL_TREE;
3096 /* Determine which parameter packs will be used by the base
3097 class expansion. */
3098 ppd.visited = pointer_set_create ();
3099 ppd.parameter_packs = &parameter_packs;
3100 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3101 &ppd, ppd.visited);
3103 if (parameter_packs == NULL_TREE)
3105 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3106 pointer_set_destroy (ppd.visited);
3107 return error_mark_node;
3110 if (TREE_VALUE (arg) != void_type_node)
3112 /* Collect the sets of parameter packs used in each of the
3113 initialization arguments. */
3114 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3116 /* Determine which parameter packs will be expanded in this
3117 argument. */
3118 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3119 &ppd, ppd.visited);
3123 pointer_set_destroy (ppd.visited);
3125 /* Create the pack expansion type for the base type. */
3126 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3127 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3128 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3130 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3131 they will rarely be compared to anything. */
3132 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3134 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3137 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3138 for_types = true;
3140 /* Build the PACK_EXPANSION_* node. */
3141 result = for_types
3142 ? cxx_make_type (TYPE_PACK_EXPANSION)
3143 : make_node (EXPR_PACK_EXPANSION);
3144 SET_PACK_EXPANSION_PATTERN (result, arg);
3145 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3147 /* Propagate type and const-expression information. */
3148 TREE_TYPE (result) = TREE_TYPE (arg);
3149 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3151 else
3152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3153 they will rarely be compared to anything. */
3154 SET_TYPE_STRUCTURAL_EQUALITY (result);
3156 /* Determine which parameter packs will be expanded. */
3157 ppd.parameter_packs = &parameter_packs;
3158 ppd.visited = pointer_set_create ();
3159 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3160 pointer_set_destroy (ppd.visited);
3162 /* Make sure we found some parameter packs. */
3163 if (parameter_packs == NULL_TREE)
3165 if (TYPE_P (arg))
3166 error ("expansion pattern %<%T%> contains no argument packs", arg);
3167 else
3168 error ("expansion pattern %<%E%> contains no argument packs", arg);
3169 return error_mark_node;
3171 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3173 return result;
3176 /* Checks T for any "bare" parameter packs, which have not yet been
3177 expanded, and issues an error if any are found. This operation can
3178 only be done on full expressions or types (e.g., an expression
3179 statement, "if" condition, etc.), because we could have expressions like:
3181 foo(f(g(h(args)))...)
3183 where "args" is a parameter pack. check_for_bare_parameter_packs
3184 should not be called for the subexpressions args, h(args),
3185 g(h(args)), or f(g(h(args))), because we would produce erroneous
3186 error messages.
3188 Returns TRUE and emits an error if there were bare parameter packs,
3189 returns FALSE otherwise. */
3190 bool
3191 check_for_bare_parameter_packs (tree t)
3193 tree parameter_packs = NULL_TREE;
3194 struct find_parameter_pack_data ppd;
3196 if (!processing_template_decl || !t || t == error_mark_node)
3197 return false;
3199 if (TREE_CODE (t) == TYPE_DECL)
3200 t = TREE_TYPE (t);
3202 ppd.parameter_packs = &parameter_packs;
3203 ppd.visited = pointer_set_create ();
3204 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3205 pointer_set_destroy (ppd.visited);
3207 if (parameter_packs)
3209 error ("parameter packs not expanded with %<...%>:");
3210 while (parameter_packs)
3212 tree pack = TREE_VALUE (parameter_packs);
3213 tree name = NULL_TREE;
3215 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3216 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3217 name = TYPE_NAME (pack);
3218 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3219 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3220 else
3221 name = DECL_NAME (pack);
3223 if (name)
3224 inform (input_location, " %qD", name);
3225 else
3226 inform (input_location, " <anonymous>");
3228 parameter_packs = TREE_CHAIN (parameter_packs);
3231 return true;
3234 return false;
3237 /* Expand any parameter packs that occur in the template arguments in
3238 ARGS. */
3239 tree
3240 expand_template_argument_pack (tree args)
3242 tree result_args = NULL_TREE;
3243 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3244 int num_result_args = -1;
3245 int non_default_args_count = -1;
3247 /* First, determine if we need to expand anything, and the number of
3248 slots we'll need. */
3249 for (in_arg = 0; in_arg < nargs; ++in_arg)
3251 tree arg = TREE_VEC_ELT (args, in_arg);
3252 if (arg == NULL_TREE)
3253 return args;
3254 if (ARGUMENT_PACK_P (arg))
3256 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3257 if (num_result_args < 0)
3258 num_result_args = in_arg + num_packed;
3259 else
3260 num_result_args += num_packed;
3262 else
3264 if (num_result_args >= 0)
3265 num_result_args++;
3269 /* If no expansion is necessary, we're done. */
3270 if (num_result_args < 0)
3271 return args;
3273 /* Expand arguments. */
3274 result_args = make_tree_vec (num_result_args);
3275 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3276 non_default_args_count =
3277 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3278 for (in_arg = 0; in_arg < nargs; ++in_arg)
3280 tree arg = TREE_VEC_ELT (args, in_arg);
3281 if (ARGUMENT_PACK_P (arg))
3283 tree packed = ARGUMENT_PACK_ARGS (arg);
3284 int i, num_packed = TREE_VEC_LENGTH (packed);
3285 for (i = 0; i < num_packed; ++i, ++out_arg)
3286 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3287 if (non_default_args_count > 0)
3288 non_default_args_count += num_packed;
3290 else
3292 TREE_VEC_ELT (result_args, out_arg) = arg;
3293 ++out_arg;
3296 if (non_default_args_count >= 0)
3297 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3298 return result_args;
3301 /* Checks if DECL shadows a template parameter.
3303 [temp.local]: A template-parameter shall not be redeclared within its
3304 scope (including nested scopes).
3306 Emits an error and returns TRUE if the DECL shadows a parameter,
3307 returns FALSE otherwise. */
3309 bool
3310 check_template_shadow (tree decl)
3312 tree olddecl;
3314 /* If we're not in a template, we can't possibly shadow a template
3315 parameter. */
3316 if (!current_template_parms)
3317 return true;
3319 /* Figure out what we're shadowing. */
3320 if (TREE_CODE (decl) == OVERLOAD)
3321 decl = OVL_CURRENT (decl);
3322 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3324 /* If there's no previous binding for this name, we're not shadowing
3325 anything, let alone a template parameter. */
3326 if (!olddecl)
3327 return true;
3329 /* If we're not shadowing a template parameter, we're done. Note
3330 that OLDDECL might be an OVERLOAD (or perhaps even an
3331 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3332 node. */
3333 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3334 return true;
3336 /* We check for decl != olddecl to avoid bogus errors for using a
3337 name inside a class. We check TPFI to avoid duplicate errors for
3338 inline member templates. */
3339 if (decl == olddecl
3340 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3341 return true;
3343 error ("declaration of %q+#D", decl);
3344 error (" shadows template parm %q+#D", olddecl);
3345 return false;
3348 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3349 ORIG_LEVEL, DECL, and TYPE. */
3351 static tree
3352 build_template_parm_index (int index,
3353 int level,
3354 int orig_level,
3355 tree decl,
3356 tree type)
3358 tree t = make_node (TEMPLATE_PARM_INDEX);
3359 TEMPLATE_PARM_IDX (t) = index;
3360 TEMPLATE_PARM_LEVEL (t) = level;
3361 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3362 TEMPLATE_PARM_DECL (t) = decl;
3363 TREE_TYPE (t) = type;
3364 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3365 TREE_READONLY (t) = TREE_READONLY (decl);
3367 return t;
3370 /* Find the canonical type parameter for the given template type
3371 parameter. Returns the canonical type parameter, which may be TYPE
3372 if no such parameter existed. */
3373 static tree
3374 canonical_type_parameter (tree type)
3376 tree list;
3377 int idx = TEMPLATE_TYPE_IDX (type);
3378 if (!canonical_template_parms)
3379 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3381 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3382 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3384 list = VEC_index (tree, canonical_template_parms, idx);
3385 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3386 list = TREE_CHAIN (list);
3388 if (list)
3389 return TREE_VALUE (list);
3390 else
3392 VEC_replace(tree, canonical_template_parms, idx,
3393 tree_cons (NULL_TREE, type,
3394 VEC_index (tree, canonical_template_parms, idx)));
3395 return type;
3399 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3400 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3401 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3402 new one is created. */
3404 static tree
3405 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3406 tsubst_flags_t complain)
3408 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3409 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3410 != TEMPLATE_PARM_LEVEL (index) - levels)
3411 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3413 tree orig_decl = TEMPLATE_PARM_DECL (index);
3414 tree decl, t;
3416 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3417 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3418 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3419 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3420 DECL_ARTIFICIAL (decl) = 1;
3421 SET_DECL_TEMPLATE_PARM_P (decl);
3423 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3424 TEMPLATE_PARM_LEVEL (index) - levels,
3425 TEMPLATE_PARM_ORIG_LEVEL (index),
3426 decl, type);
3427 TEMPLATE_PARM_DESCENDANTS (index) = t;
3428 TEMPLATE_PARM_PARAMETER_PACK (t)
3429 = TEMPLATE_PARM_PARAMETER_PACK (index);
3431 /* Template template parameters need this. */
3432 if (TREE_CODE (decl) == TEMPLATE_DECL)
3433 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3434 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3435 args, complain);
3438 return TEMPLATE_PARM_DESCENDANTS (index);
3441 /* Process information from new template parameter PARM and append it to the
3442 LIST being built. This new parameter is a non-type parameter iff
3443 IS_NON_TYPE is true. This new parameter is a parameter
3444 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3445 PARM_LOC. */
3447 tree
3448 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3449 bool is_parameter_pack)
3451 tree decl = 0;
3452 tree defval;
3453 tree err_parm_list;
3454 int idx = 0;
3456 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3457 defval = TREE_PURPOSE (parm);
3459 if (list)
3461 tree p = tree_last (list);
3463 if (p && TREE_VALUE (p) != error_mark_node)
3465 p = TREE_VALUE (p);
3466 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3467 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3468 else
3469 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3472 ++idx;
3474 else
3475 idx = 0;
3477 if (is_non_type)
3479 parm = TREE_VALUE (parm);
3481 SET_DECL_TEMPLATE_PARM_P (parm);
3483 if (TREE_TYPE (parm) == error_mark_node)
3485 err_parm_list = build_tree_list (defval, parm);
3486 TREE_VALUE (err_parm_list) = error_mark_node;
3487 return chainon (list, err_parm_list);
3489 else
3491 /* [temp.param]
3493 The top-level cv-qualifiers on the template-parameter are
3494 ignored when determining its type. */
3495 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3496 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3498 err_parm_list = build_tree_list (defval, parm);
3499 TREE_VALUE (err_parm_list) = error_mark_node;
3500 return chainon (list, err_parm_list);
3503 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3505 /* This template parameter is not a parameter pack, but it
3506 should be. Complain about "bare" parameter packs. */
3507 check_for_bare_parameter_packs (TREE_TYPE (parm));
3509 /* Recover by calling this a parameter pack. */
3510 is_parameter_pack = true;
3514 /* A template parameter is not modifiable. */
3515 TREE_CONSTANT (parm) = 1;
3516 TREE_READONLY (parm) = 1;
3517 decl = build_decl (parm_loc,
3518 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3519 TREE_CONSTANT (decl) = 1;
3520 TREE_READONLY (decl) = 1;
3521 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3522 = build_template_parm_index (idx, processing_template_decl,
3523 processing_template_decl,
3524 decl, TREE_TYPE (parm));
3526 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3527 = is_parameter_pack;
3529 else
3531 tree t;
3532 parm = TREE_VALUE (TREE_VALUE (parm));
3534 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3536 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3537 /* This is for distinguishing between real templates and template
3538 template parameters */
3539 TREE_TYPE (parm) = t;
3540 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3541 decl = parm;
3543 else
3545 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3546 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3547 decl = build_decl (parm_loc,
3548 TYPE_DECL, parm, t);
3551 TYPE_NAME (t) = decl;
3552 TYPE_STUB_DECL (t) = decl;
3553 parm = decl;
3554 TEMPLATE_TYPE_PARM_INDEX (t)
3555 = build_template_parm_index (idx, processing_template_decl,
3556 processing_template_decl,
3557 decl, TREE_TYPE (parm));
3558 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3559 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3561 DECL_ARTIFICIAL (decl) = 1;
3562 SET_DECL_TEMPLATE_PARM_P (decl);
3563 pushdecl (decl);
3564 parm = build_tree_list (defval, parm);
3565 return chainon (list, parm);
3568 /* The end of a template parameter list has been reached. Process the
3569 tree list into a parameter vector, converting each parameter into a more
3570 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3571 as PARM_DECLs. */
3573 tree
3574 end_template_parm_list (tree parms)
3576 int nparms;
3577 tree parm, next;
3578 tree saved_parmlist = make_tree_vec (list_length (parms));
3580 current_template_parms
3581 = tree_cons (size_int (processing_template_decl),
3582 saved_parmlist, current_template_parms);
3584 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3586 next = TREE_CHAIN (parm);
3587 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3588 TREE_CHAIN (parm) = NULL_TREE;
3589 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3590 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
3591 current_template_parms;
3594 --processing_template_parmlist;
3596 return saved_parmlist;
3599 /* end_template_decl is called after a template declaration is seen. */
3601 void
3602 end_template_decl (void)
3604 reset_specialization ();
3606 if (! processing_template_decl)
3607 return;
3609 /* This matches the pushlevel in begin_template_parm_list. */
3610 finish_scope ();
3612 --processing_template_decl;
3613 current_template_parms = TREE_CHAIN (current_template_parms);
3616 /* Within the declaration of a template, return all levels of template
3617 parameters that apply. The template parameters are represented as
3618 a TREE_VEC, in the form documented in cp-tree.h for template
3619 arguments. */
3621 static tree
3622 current_template_args (void)
3624 tree header;
3625 tree args = NULL_TREE;
3626 int length = TMPL_PARMS_DEPTH (current_template_parms);
3627 int l = length;
3629 /* If there is only one level of template parameters, we do not
3630 create a TREE_VEC of TREE_VECs. Instead, we return a single
3631 TREE_VEC containing the arguments. */
3632 if (length > 1)
3633 args = make_tree_vec (length);
3635 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3637 tree a = copy_node (TREE_VALUE (header));
3638 int i;
3640 TREE_TYPE (a) = NULL_TREE;
3641 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3643 tree t = TREE_VEC_ELT (a, i);
3645 /* T will be a list if we are called from within a
3646 begin/end_template_parm_list pair, but a vector directly
3647 if within a begin/end_member_template_processing pair. */
3648 if (TREE_CODE (t) == TREE_LIST)
3650 t = TREE_VALUE (t);
3652 if (!error_operand_p (t))
3654 if (TREE_CODE (t) == TYPE_DECL
3655 || TREE_CODE (t) == TEMPLATE_DECL)
3657 t = TREE_TYPE (t);
3659 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3661 /* Turn this argument into a TYPE_ARGUMENT_PACK
3662 with a single element, which expands T. */
3663 tree vec = make_tree_vec (1);
3664 #ifdef ENABLE_CHECKING
3665 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3666 (vec, TREE_VEC_LENGTH (vec));
3667 #endif
3668 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3670 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3671 SET_ARGUMENT_PACK_ARGS (t, vec);
3674 else
3676 t = DECL_INITIAL (t);
3678 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3680 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3681 with a single element, which expands T. */
3682 tree vec = make_tree_vec (1);
3683 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3684 #ifdef ENABLE_CHECKING
3685 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3686 (vec, TREE_VEC_LENGTH (vec));
3687 #endif
3688 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3690 t = make_node (NONTYPE_ARGUMENT_PACK);
3691 SET_ARGUMENT_PACK_ARGS (t, vec);
3692 TREE_TYPE (t) = type;
3695 TREE_VEC_ELT (a, i) = t;
3700 #ifdef ENABLE_CHECKING
3701 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3702 #endif
3704 if (length > 1)
3705 TREE_VEC_ELT (args, --l) = a;
3706 else
3707 args = a;
3710 return args;
3713 /* Update the declared TYPE by doing any lookups which were thought to be
3714 dependent, but are not now that we know the SCOPE of the declarator. */
3716 tree
3717 maybe_update_decl_type (tree orig_type, tree scope)
3719 tree type = orig_type;
3721 if (type == NULL_TREE)
3722 return type;
3724 if (TREE_CODE (orig_type) == TYPE_DECL)
3725 type = TREE_TYPE (type);
3727 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3728 && dependent_type_p (type)
3729 /* Don't bother building up the args in this case. */
3730 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3732 /* tsubst in the args corresponding to the template parameters,
3733 including auto if present. Most things will be unchanged, but
3734 make_typename_type and tsubst_qualified_id will resolve
3735 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3736 tree args = current_template_args ();
3737 tree auto_node = type_uses_auto (type);
3738 tree pushed;
3739 if (auto_node)
3741 tree auto_vec = make_tree_vec (1);
3742 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3743 args = add_to_template_args (args, auto_vec);
3745 pushed = push_scope (scope);
3746 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3747 if (pushed)
3748 pop_scope (scope);
3751 if (type == error_mark_node)
3752 return orig_type;
3754 if (TREE_CODE (orig_type) == TYPE_DECL)
3756 if (same_type_p (type, TREE_TYPE (orig_type)))
3757 type = orig_type;
3758 else
3759 type = TYPE_NAME (type);
3761 return type;
3764 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3765 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3766 a member template. Used by push_template_decl below. */
3768 static tree
3769 build_template_decl (tree decl, tree parms, bool member_template_p)
3771 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3772 DECL_TEMPLATE_PARMS (tmpl) = parms;
3773 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3774 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3776 return tmpl;
3779 struct template_parm_data
3781 /* The level of the template parameters we are currently
3782 processing. */
3783 int level;
3785 /* The index of the specialization argument we are currently
3786 processing. */
3787 int current_arg;
3789 /* An array whose size is the number of template parameters. The
3790 elements are nonzero if the parameter has been used in any one
3791 of the arguments processed so far. */
3792 int* parms;
3794 /* An array whose size is the number of template arguments. The
3795 elements are nonzero if the argument makes use of template
3796 parameters of this level. */
3797 int* arg_uses_template_parms;
3800 /* Subroutine of push_template_decl used to see if each template
3801 parameter in a partial specialization is used in the explicit
3802 argument list. If T is of the LEVEL given in DATA (which is
3803 treated as a template_parm_data*), then DATA->PARMS is marked
3804 appropriately. */
3806 static int
3807 mark_template_parm (tree t, void* data)
3809 int level;
3810 int idx;
3811 struct template_parm_data* tpd = (struct template_parm_data*) data;
3813 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3815 level = TEMPLATE_PARM_LEVEL (t);
3816 idx = TEMPLATE_PARM_IDX (t);
3818 else
3820 level = TEMPLATE_TYPE_LEVEL (t);
3821 idx = TEMPLATE_TYPE_IDX (t);
3824 if (level == tpd->level)
3826 tpd->parms[idx] = 1;
3827 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3830 /* Return zero so that for_each_template_parm will continue the
3831 traversal of the tree; we want to mark *every* template parm. */
3832 return 0;
3835 /* Process the partial specialization DECL. */
3837 static tree
3838 process_partial_specialization (tree decl)
3840 tree type = TREE_TYPE (decl);
3841 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3842 tree specargs = CLASSTYPE_TI_ARGS (type);
3843 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3844 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3845 tree inner_parms;
3846 tree inst;
3847 int nargs = TREE_VEC_LENGTH (inner_args);
3848 int ntparms;
3849 int i;
3850 bool did_error_intro = false;
3851 struct template_parm_data tpd;
3852 struct template_parm_data tpd2;
3854 gcc_assert (current_template_parms);
3856 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3857 ntparms = TREE_VEC_LENGTH (inner_parms);
3859 /* We check that each of the template parameters given in the
3860 partial specialization is used in the argument list to the
3861 specialization. For example:
3863 template <class T> struct S;
3864 template <class T> struct S<T*>;
3866 The second declaration is OK because `T*' uses the template
3867 parameter T, whereas
3869 template <class T> struct S<int>;
3871 is no good. Even trickier is:
3873 template <class T>
3874 struct S1
3876 template <class U>
3877 struct S2;
3878 template <class U>
3879 struct S2<T>;
3882 The S2<T> declaration is actually invalid; it is a
3883 full-specialization. Of course,
3885 template <class U>
3886 struct S2<T (*)(U)>;
3888 or some such would have been OK. */
3889 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3890 tpd.parms = XALLOCAVEC (int, ntparms);
3891 memset (tpd.parms, 0, sizeof (int) * ntparms);
3893 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
3894 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3895 for (i = 0; i < nargs; ++i)
3897 tpd.current_arg = i;
3898 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3899 &mark_template_parm,
3900 &tpd,
3901 NULL,
3902 /*include_nondeduced_p=*/false);
3904 for (i = 0; i < ntparms; ++i)
3905 if (tpd.parms[i] == 0)
3907 /* One of the template parms was not used in the
3908 specialization. */
3909 if (!did_error_intro)
3911 error ("template parameters not used in partial specialization:");
3912 did_error_intro = true;
3915 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3918 if (did_error_intro)
3919 return error_mark_node;
3921 /* [temp.class.spec]
3923 The argument list of the specialization shall not be identical to
3924 the implicit argument list of the primary template. */
3925 if (comp_template_args
3926 (inner_args,
3927 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3928 (maintmpl)))))
3929 error ("partial specialization %qT does not specialize any template arguments", type);
3931 /* [temp.class.spec]
3933 A partially specialized non-type argument expression shall not
3934 involve template parameters of the partial specialization except
3935 when the argument expression is a simple identifier.
3937 The type of a template parameter corresponding to a specialized
3938 non-type argument shall not be dependent on a parameter of the
3939 specialization.
3941 Also, we verify that pack expansions only occur at the
3942 end of the argument list. */
3943 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3944 tpd2.parms = 0;
3945 for (i = 0; i < nargs; ++i)
3947 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3948 tree arg = TREE_VEC_ELT (inner_args, i);
3949 tree packed_args = NULL_TREE;
3950 int j, len = 1;
3952 if (ARGUMENT_PACK_P (arg))
3954 /* Extract the arguments from the argument pack. We'll be
3955 iterating over these in the following loop. */
3956 packed_args = ARGUMENT_PACK_ARGS (arg);
3957 len = TREE_VEC_LENGTH (packed_args);
3960 for (j = 0; j < len; j++)
3962 if (packed_args)
3963 /* Get the Jth argument in the parameter pack. */
3964 arg = TREE_VEC_ELT (packed_args, j);
3966 if (PACK_EXPANSION_P (arg))
3968 /* Pack expansions must come at the end of the
3969 argument list. */
3970 if ((packed_args && j < len - 1)
3971 || (!packed_args && i < nargs - 1))
3973 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3974 error ("parameter pack argument %qE must be at the "
3975 "end of the template argument list", arg);
3976 else
3977 error ("parameter pack argument %qT must be at the "
3978 "end of the template argument list", arg);
3982 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3983 /* We only care about the pattern. */
3984 arg = PACK_EXPANSION_PATTERN (arg);
3986 if (/* These first two lines are the `non-type' bit. */
3987 !TYPE_P (arg)
3988 && TREE_CODE (arg) != TEMPLATE_DECL
3989 /* This next line is the `argument expression is not just a
3990 simple identifier' condition and also the `specialized
3991 non-type argument' bit. */
3992 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3994 if ((!packed_args && tpd.arg_uses_template_parms[i])
3995 || (packed_args && uses_template_parms (arg)))
3996 error ("template argument %qE involves template parameter(s)",
3997 arg);
3998 else
4000 /* Look at the corresponding template parameter,
4001 marking which template parameters its type depends
4002 upon. */
4003 tree type = TREE_TYPE (parm);
4005 if (!tpd2.parms)
4007 /* We haven't yet initialized TPD2. Do so now. */
4008 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4009 /* The number of parameters here is the number in the
4010 main template, which, as checked in the assertion
4011 above, is NARGS. */
4012 tpd2.parms = XALLOCAVEC (int, nargs);
4013 tpd2.level =
4014 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4017 /* Mark the template parameters. But this time, we're
4018 looking for the template parameters of the main
4019 template, not in the specialization. */
4020 tpd2.current_arg = i;
4021 tpd2.arg_uses_template_parms[i] = 0;
4022 memset (tpd2.parms, 0, sizeof (int) * nargs);
4023 for_each_template_parm (type,
4024 &mark_template_parm,
4025 &tpd2,
4026 NULL,
4027 /*include_nondeduced_p=*/false);
4029 if (tpd2.arg_uses_template_parms [i])
4031 /* The type depended on some template parameters.
4032 If they are fully specialized in the
4033 specialization, that's OK. */
4034 int j;
4035 int count = 0;
4036 for (j = 0; j < nargs; ++j)
4037 if (tpd2.parms[j] != 0
4038 && tpd.arg_uses_template_parms [j])
4039 ++count;
4040 if (count != 0)
4041 error_n (input_location, count,
4042 "type %qT of template argument %qE depends "
4043 "on a template parameter",
4044 "type %qT of template argument %qE depends "
4045 "on template parameters",
4046 type,
4047 arg);
4054 /* We should only get here once. */
4055 gcc_assert (!COMPLETE_TYPE_P (type));
4057 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4058 = tree_cons (specargs, inner_parms,
4059 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4060 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4062 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4063 inst = TREE_CHAIN (inst))
4065 tree inst_type = TREE_VALUE (inst);
4066 if (COMPLETE_TYPE_P (inst_type)
4067 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4069 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4070 if (spec && TREE_TYPE (spec) == type)
4071 permerror (input_location,
4072 "partial specialization of %qT after instantiation "
4073 "of %qT", type, inst_type);
4077 return decl;
4080 /* Check that a template declaration's use of default arguments and
4081 parameter packs is not invalid. Here, PARMS are the template
4082 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4083 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4084 specialization.
4087 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4088 declaration (but not a definition); 1 indicates a declaration, 2
4089 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4090 emitted for extraneous default arguments.
4092 Returns TRUE if there were no errors found, FALSE otherwise. */
4094 bool
4095 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4096 int is_partial, int is_friend_decl)
4098 const char *msg;
4099 int last_level_to_check;
4100 tree parm_level;
4101 bool no_errors = true;
4103 /* [temp.param]
4105 A default template-argument shall not be specified in a
4106 function template declaration or a function template definition, nor
4107 in the template-parameter-list of the definition of a member of a
4108 class template. */
4110 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4111 /* You can't have a function template declaration in a local
4112 scope, nor you can you define a member of a class template in a
4113 local scope. */
4114 return true;
4116 if (current_class_type
4117 && !TYPE_BEING_DEFINED (current_class_type)
4118 && DECL_LANG_SPECIFIC (decl)
4119 && DECL_DECLARES_FUNCTION_P (decl)
4120 /* If this is either a friend defined in the scope of the class
4121 or a member function. */
4122 && (DECL_FUNCTION_MEMBER_P (decl)
4123 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4124 : DECL_FRIEND_CONTEXT (decl)
4125 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4126 : false)
4127 /* And, if it was a member function, it really was defined in
4128 the scope of the class. */
4129 && (!DECL_FUNCTION_MEMBER_P (decl)
4130 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4131 /* We already checked these parameters when the template was
4132 declared, so there's no need to do it again now. This function
4133 was defined in class scope, but we're processing it's body now
4134 that the class is complete. */
4135 return true;
4137 /* Core issue 226 (C++0x only): the following only applies to class
4138 templates. */
4139 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4141 /* [temp.param]
4143 If a template-parameter has a default template-argument, all
4144 subsequent template-parameters shall have a default
4145 template-argument supplied. */
4146 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4148 tree inner_parms = TREE_VALUE (parm_level);
4149 int ntparms = TREE_VEC_LENGTH (inner_parms);
4150 int seen_def_arg_p = 0;
4151 int i;
4153 for (i = 0; i < ntparms; ++i)
4155 tree parm = TREE_VEC_ELT (inner_parms, i);
4157 if (parm == error_mark_node)
4158 continue;
4160 if (TREE_PURPOSE (parm))
4161 seen_def_arg_p = 1;
4162 else if (seen_def_arg_p
4163 && !template_parameter_pack_p (TREE_VALUE (parm)))
4165 error ("no default argument for %qD", TREE_VALUE (parm));
4166 /* For better subsequent error-recovery, we indicate that
4167 there should have been a default argument. */
4168 TREE_PURPOSE (parm) = error_mark_node;
4169 no_errors = false;
4171 else if (is_primary
4172 && !is_partial
4173 && !is_friend_decl
4174 /* Don't complain about an enclosing partial
4175 specialization. */
4176 && parm_level == parms
4177 && TREE_CODE (decl) == TYPE_DECL
4178 && i < ntparms - 1
4179 && template_parameter_pack_p (TREE_VALUE (parm)))
4181 /* A primary class template can only have one
4182 parameter pack, at the end of the template
4183 parameter list. */
4185 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4186 error ("parameter pack %qE must be at the end of the"
4187 " template parameter list", TREE_VALUE (parm));
4188 else
4189 error ("parameter pack %qT must be at the end of the"
4190 " template parameter list",
4191 TREE_TYPE (TREE_VALUE (parm)));
4193 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4194 = error_mark_node;
4195 no_errors = false;
4201 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4202 || is_partial
4203 || !is_primary
4204 || is_friend_decl)
4205 /* For an ordinary class template, default template arguments are
4206 allowed at the innermost level, e.g.:
4207 template <class T = int>
4208 struct S {};
4209 but, in a partial specialization, they're not allowed even
4210 there, as we have in [temp.class.spec]:
4212 The template parameter list of a specialization shall not
4213 contain default template argument values.
4215 So, for a partial specialization, or for a function template
4216 (in C++98/C++03), we look at all of them. */
4218 else
4219 /* But, for a primary class template that is not a partial
4220 specialization we look at all template parameters except the
4221 innermost ones. */
4222 parms = TREE_CHAIN (parms);
4224 /* Figure out what error message to issue. */
4225 if (is_friend_decl == 2)
4226 msg = G_("default template arguments may not be used in function template "
4227 "friend re-declaration");
4228 else if (is_friend_decl)
4229 msg = G_("default template arguments may not be used in function template "
4230 "friend declarations");
4231 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4232 msg = G_("default template arguments may not be used in function templates "
4233 "without -std=c++0x or -std=gnu++0x");
4234 else if (is_partial)
4235 msg = G_("default template arguments may not be used in "
4236 "partial specializations");
4237 else
4238 msg = G_("default argument for template parameter for class enclosing %qD");
4240 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4241 /* If we're inside a class definition, there's no need to
4242 examine the parameters to the class itself. On the one
4243 hand, they will be checked when the class is defined, and,
4244 on the other, default arguments are valid in things like:
4245 template <class T = double>
4246 struct S { template <class U> void f(U); };
4247 Here the default argument for `S' has no bearing on the
4248 declaration of `f'. */
4249 last_level_to_check = template_class_depth (current_class_type) + 1;
4250 else
4251 /* Check everything. */
4252 last_level_to_check = 0;
4254 for (parm_level = parms;
4255 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4256 parm_level = TREE_CHAIN (parm_level))
4258 tree inner_parms = TREE_VALUE (parm_level);
4259 int i;
4260 int ntparms;
4262 ntparms = TREE_VEC_LENGTH (inner_parms);
4263 for (i = 0; i < ntparms; ++i)
4265 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4266 continue;
4268 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4270 if (msg)
4272 no_errors = false;
4273 if (is_friend_decl == 2)
4274 return no_errors;
4276 error (msg, decl);
4277 msg = 0;
4280 /* Clear out the default argument so that we are not
4281 confused later. */
4282 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4286 /* At this point, if we're still interested in issuing messages,
4287 they must apply to classes surrounding the object declared. */
4288 if (msg)
4289 msg = G_("default argument for template parameter for class "
4290 "enclosing %qD");
4293 return no_errors;
4296 /* Worker for push_template_decl_real, called via
4297 for_each_template_parm. DATA is really an int, indicating the
4298 level of the parameters we are interested in. If T is a template
4299 parameter of that level, return nonzero. */
4301 static int
4302 template_parm_this_level_p (tree t, void* data)
4304 int this_level = *(int *)data;
4305 int level;
4307 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4308 level = TEMPLATE_PARM_LEVEL (t);
4309 else
4310 level = TEMPLATE_TYPE_LEVEL (t);
4311 return level == this_level;
4314 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4315 parameters given by current_template_args, or reuses a
4316 previously existing one, if appropriate. Returns the DECL, or an
4317 equivalent one, if it is replaced via a call to duplicate_decls.
4319 If IS_FRIEND is true, DECL is a friend declaration. */
4321 tree
4322 push_template_decl_real (tree decl, bool is_friend)
4324 tree tmpl;
4325 tree args;
4326 tree info;
4327 tree ctx;
4328 int primary;
4329 int is_partial;
4330 int new_template_p = 0;
4331 /* True if the template is a member template, in the sense of
4332 [temp.mem]. */
4333 bool member_template_p = false;
4335 if (decl == error_mark_node || !current_template_parms)
4336 return error_mark_node;
4338 /* See if this is a partial specialization. */
4339 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4340 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4341 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4343 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4344 is_friend = true;
4346 if (is_friend)
4347 /* For a friend, we want the context of the friend function, not
4348 the type of which it is a friend. */
4349 ctx = DECL_CONTEXT (decl);
4350 else if (CP_DECL_CONTEXT (decl)
4351 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4352 /* In the case of a virtual function, we want the class in which
4353 it is defined. */
4354 ctx = CP_DECL_CONTEXT (decl);
4355 else
4356 /* Otherwise, if we're currently defining some class, the DECL
4357 is assumed to be a member of the class. */
4358 ctx = current_scope ();
4360 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4361 ctx = NULL_TREE;
4363 if (!DECL_CONTEXT (decl))
4364 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4366 /* See if this is a primary template. */
4367 if (is_friend && ctx)
4368 /* A friend template that specifies a class context, i.e.
4369 template <typename T> friend void A<T>::f();
4370 is not primary. */
4371 primary = 0;
4372 else
4373 primary = template_parm_scope_p ();
4375 if (primary)
4377 if (DECL_CLASS_SCOPE_P (decl))
4378 member_template_p = true;
4379 if (TREE_CODE (decl) == TYPE_DECL
4380 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4382 error ("template class without a name");
4383 return error_mark_node;
4385 else if (TREE_CODE (decl) == FUNCTION_DECL)
4387 if (DECL_DESTRUCTOR_P (decl))
4389 /* [temp.mem]
4391 A destructor shall not be a member template. */
4392 error ("destructor %qD declared as member template", decl);
4393 return error_mark_node;
4395 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4396 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4397 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4398 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4399 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4400 == void_list_node)))
4402 /* [basic.stc.dynamic.allocation]
4404 An allocation function can be a function
4405 template. ... Template allocation functions shall
4406 have two or more parameters. */
4407 error ("invalid template declaration of %qD", decl);
4408 return error_mark_node;
4411 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4412 && CLASS_TYPE_P (TREE_TYPE (decl)))
4413 /* OK */;
4414 else
4416 error ("template declaration of %q#D", decl);
4417 return error_mark_node;
4421 /* Check to see that the rules regarding the use of default
4422 arguments are not being violated. */
4423 check_default_tmpl_args (decl, current_template_parms,
4424 primary, is_partial, /*is_friend_decl=*/0);
4426 /* Ensure that there are no parameter packs in the type of this
4427 declaration that have not been expanded. */
4428 if (TREE_CODE (decl) == FUNCTION_DECL)
4430 /* Check each of the arguments individually to see if there are
4431 any bare parameter packs. */
4432 tree type = TREE_TYPE (decl);
4433 tree arg = DECL_ARGUMENTS (decl);
4434 tree argtype = TYPE_ARG_TYPES (type);
4436 while (arg && argtype)
4438 if (!FUNCTION_PARAMETER_PACK_P (arg)
4439 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4441 /* This is a PARM_DECL that contains unexpanded parameter
4442 packs. We have already complained about this in the
4443 check_for_bare_parameter_packs call, so just replace
4444 these types with ERROR_MARK_NODE. */
4445 TREE_TYPE (arg) = error_mark_node;
4446 TREE_VALUE (argtype) = error_mark_node;
4449 arg = DECL_CHAIN (arg);
4450 argtype = TREE_CHAIN (argtype);
4453 /* Check for bare parameter packs in the return type and the
4454 exception specifiers. */
4455 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4456 /* Errors were already issued, set return type to int
4457 as the frontend doesn't expect error_mark_node as
4458 the return type. */
4459 TREE_TYPE (type) = integer_type_node;
4460 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4461 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4463 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4465 TREE_TYPE (decl) = error_mark_node;
4466 return error_mark_node;
4469 if (is_partial)
4470 return process_partial_specialization (decl);
4472 args = current_template_args ();
4474 if (!ctx
4475 || TREE_CODE (ctx) == FUNCTION_DECL
4476 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4477 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4479 if (DECL_LANG_SPECIFIC (decl)
4480 && DECL_TEMPLATE_INFO (decl)
4481 && DECL_TI_TEMPLATE (decl))
4482 tmpl = DECL_TI_TEMPLATE (decl);
4483 /* If DECL is a TYPE_DECL for a class-template, then there won't
4484 be DECL_LANG_SPECIFIC. The information equivalent to
4485 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4486 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4487 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4488 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4490 /* Since a template declaration already existed for this
4491 class-type, we must be redeclaring it here. Make sure
4492 that the redeclaration is valid. */
4493 redeclare_class_template (TREE_TYPE (decl),
4494 current_template_parms);
4495 /* We don't need to create a new TEMPLATE_DECL; just use the
4496 one we already had. */
4497 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4499 else
4501 tmpl = build_template_decl (decl, current_template_parms,
4502 member_template_p);
4503 new_template_p = 1;
4505 if (DECL_LANG_SPECIFIC (decl)
4506 && DECL_TEMPLATE_SPECIALIZATION (decl))
4508 /* A specialization of a member template of a template
4509 class. */
4510 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4511 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4512 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4516 else
4518 tree a, t, current, parms;
4519 int i;
4520 tree tinfo = get_template_info (decl);
4522 if (!tinfo)
4524 error ("template definition of non-template %q#D", decl);
4525 return error_mark_node;
4528 tmpl = TI_TEMPLATE (tinfo);
4530 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4531 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4532 && DECL_TEMPLATE_SPECIALIZATION (decl)
4533 && DECL_MEMBER_TEMPLATE_P (tmpl))
4535 tree new_tmpl;
4537 /* The declaration is a specialization of a member
4538 template, declared outside the class. Therefore, the
4539 innermost template arguments will be NULL, so we
4540 replace them with the arguments determined by the
4541 earlier call to check_explicit_specialization. */
4542 args = DECL_TI_ARGS (decl);
4544 new_tmpl
4545 = build_template_decl (decl, current_template_parms,
4546 member_template_p);
4547 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4548 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4549 DECL_TI_TEMPLATE (decl) = new_tmpl;
4550 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4551 DECL_TEMPLATE_INFO (new_tmpl)
4552 = build_template_info (tmpl, args);
4554 register_specialization (new_tmpl,
4555 most_general_template (tmpl),
4556 args,
4557 is_friend, 0);
4558 return decl;
4561 /* Make sure the template headers we got make sense. */
4563 parms = DECL_TEMPLATE_PARMS (tmpl);
4564 i = TMPL_PARMS_DEPTH (parms);
4565 if (TMPL_ARGS_DEPTH (args) != i)
4567 error ("expected %d levels of template parms for %q#D, got %d",
4568 i, decl, TMPL_ARGS_DEPTH (args));
4570 else
4571 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4573 a = TMPL_ARGS_LEVEL (args, i);
4574 t = INNERMOST_TEMPLATE_PARMS (parms);
4576 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4578 if (current == decl)
4579 error ("got %d template parameters for %q#D",
4580 TREE_VEC_LENGTH (a), decl);
4581 else
4582 error ("got %d template parameters for %q#T",
4583 TREE_VEC_LENGTH (a), current);
4584 error (" but %d required", TREE_VEC_LENGTH (t));
4585 return error_mark_node;
4588 if (current == decl)
4589 current = ctx;
4590 else if (current == NULL_TREE)
4591 /* Can happen in erroneous input. */
4592 break;
4593 else
4594 current = (TYPE_P (current)
4595 ? TYPE_CONTEXT (current)
4596 : DECL_CONTEXT (current));
4599 /* Check that the parms are used in the appropriate qualifying scopes
4600 in the declarator. */
4601 if (!comp_template_args
4602 (TI_ARGS (tinfo),
4603 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4605 error ("\
4606 template arguments to %qD do not match original template %qD",
4607 decl, DECL_TEMPLATE_RESULT (tmpl));
4608 if (!uses_template_parms (TI_ARGS (tinfo)))
4609 inform (input_location, "use template<> for an explicit specialization");
4610 /* Avoid crash in import_export_decl. */
4611 DECL_INTERFACE_KNOWN (decl) = 1;
4612 return error_mark_node;
4616 DECL_TEMPLATE_RESULT (tmpl) = decl;
4617 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4619 /* Push template declarations for global functions and types. Note
4620 that we do not try to push a global template friend declared in a
4621 template class; such a thing may well depend on the template
4622 parameters of the class. */
4623 if (new_template_p && !ctx
4624 && !(is_friend && template_class_depth (current_class_type) > 0))
4626 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4627 if (tmpl == error_mark_node)
4628 return error_mark_node;
4630 /* Hide template friend classes that haven't been declared yet. */
4631 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4633 DECL_ANTICIPATED (tmpl) = 1;
4634 DECL_FRIEND_P (tmpl) = 1;
4638 if (primary)
4640 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4641 int i;
4643 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4644 if (DECL_CONV_FN_P (tmpl))
4646 int depth = TMPL_PARMS_DEPTH (parms);
4648 /* It is a conversion operator. See if the type converted to
4649 depends on innermost template operands. */
4651 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4652 depth))
4653 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4656 /* Give template template parms a DECL_CONTEXT of the template
4657 for which they are a parameter. */
4658 parms = INNERMOST_TEMPLATE_PARMS (parms);
4659 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4661 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4662 if (TREE_CODE (parm) == TEMPLATE_DECL)
4663 DECL_CONTEXT (parm) = tmpl;
4667 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4668 back to its most general template. If TMPL is a specialization,
4669 ARGS may only have the innermost set of arguments. Add the missing
4670 argument levels if necessary. */
4671 if (DECL_TEMPLATE_INFO (tmpl))
4672 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4674 info = build_template_info (tmpl, args);
4676 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4677 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4678 else if (DECL_LANG_SPECIFIC (decl))
4679 DECL_TEMPLATE_INFO (decl) = info;
4681 return DECL_TEMPLATE_RESULT (tmpl);
4684 tree
4685 push_template_decl (tree decl)
4687 return push_template_decl_real (decl, false);
4690 /* Called when a class template TYPE is redeclared with the indicated
4691 template PARMS, e.g.:
4693 template <class T> struct S;
4694 template <class T> struct S {}; */
4696 bool
4697 redeclare_class_template (tree type, tree parms)
4699 tree tmpl;
4700 tree tmpl_parms;
4701 int i;
4703 if (!TYPE_TEMPLATE_INFO (type))
4705 error ("%qT is not a template type", type);
4706 return false;
4709 tmpl = TYPE_TI_TEMPLATE (type);
4710 if (!PRIMARY_TEMPLATE_P (tmpl))
4711 /* The type is nested in some template class. Nothing to worry
4712 about here; there are no new template parameters for the nested
4713 type. */
4714 return true;
4716 if (!parms)
4718 error ("template specifiers not specified in declaration of %qD",
4719 tmpl);
4720 return false;
4723 parms = INNERMOST_TEMPLATE_PARMS (parms);
4724 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4726 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4728 error_n (input_location, TREE_VEC_LENGTH (parms),
4729 "redeclared with %d template parameter",
4730 "redeclared with %d template parameters",
4731 TREE_VEC_LENGTH (parms));
4732 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4733 "previous declaration %q+D used %d template parameter",
4734 "previous declaration %q+D used %d template parameters",
4735 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4736 return false;
4739 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4741 tree tmpl_parm;
4742 tree parm;
4743 tree tmpl_default;
4744 tree parm_default;
4746 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4747 || TREE_VEC_ELT (parms, i) == error_mark_node)
4748 continue;
4750 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4751 if (tmpl_parm == error_mark_node)
4752 return false;
4754 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4755 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4756 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4758 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4759 TEMPLATE_DECL. */
4760 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4761 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4762 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4763 || (TREE_CODE (tmpl_parm) != PARM_DECL
4764 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4765 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4766 || (TREE_CODE (tmpl_parm) == PARM_DECL
4767 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4768 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4770 error ("template parameter %q+#D", tmpl_parm);
4771 error ("redeclared here as %q#D", parm);
4772 return false;
4775 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4777 /* We have in [temp.param]:
4779 A template-parameter may not be given default arguments
4780 by two different declarations in the same scope. */
4781 error_at (input_location, "redefinition of default argument for %q#D", parm);
4782 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4783 "original definition appeared here");
4784 return false;
4787 if (parm_default != NULL_TREE)
4788 /* Update the previous template parameters (which are the ones
4789 that will really count) with the new default value. */
4790 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4791 else if (tmpl_default != NULL_TREE)
4792 /* Update the new parameters, too; they'll be used as the
4793 parameters for any members. */
4794 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4797 return true;
4800 /* Simplify EXPR if it is a non-dependent expression. Returns the
4801 (possibly simplified) expression. */
4803 static tree
4804 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
4806 if (expr == NULL_TREE)
4807 return NULL_TREE;
4809 /* If we're in a template, but EXPR isn't value dependent, simplify
4810 it. We're supposed to treat:
4812 template <typename T> void f(T[1 + 1]);
4813 template <typename T> void f(T[2]);
4815 as two declarations of the same function, for example. */
4816 if (processing_template_decl
4817 && !type_dependent_expression_p (expr)
4818 && !value_dependent_expression_p (expr))
4820 HOST_WIDE_INT saved_processing_template_decl;
4822 saved_processing_template_decl = processing_template_decl;
4823 processing_template_decl = 0;
4824 expr = tsubst_copy_and_build (expr,
4825 /*args=*/NULL_TREE,
4826 complain,
4827 /*in_decl=*/NULL_TREE,
4828 /*function_p=*/false,
4829 /*integral_constant_expression_p=*/true);
4830 processing_template_decl = saved_processing_template_decl;
4832 return expr;
4835 tree
4836 fold_non_dependent_expr (tree expr)
4838 return fold_non_dependent_expr_sfinae (expr, tf_error);
4841 /* EXPR is an expression which is used in a constant-expression context.
4842 For instance, it could be a VAR_DECL with a constant initializer.
4843 Extract the innermost constant expression.
4845 This is basically a more powerful version of
4846 integral_constant_value, which can be used also in templates where
4847 initializers can maintain a syntactic rather than semantic form
4848 (even if they are non-dependent, for access-checking purposes). */
4850 static tree
4851 fold_decl_constant_value (tree expr)
4853 tree const_expr = expr;
4856 expr = fold_non_dependent_expr (const_expr);
4857 const_expr = integral_constant_value (expr);
4859 while (expr != const_expr);
4861 return expr;
4864 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4865 must be a function or a pointer-to-function type, as specified
4866 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4867 and check that the resulting function has external linkage. */
4869 static tree
4870 convert_nontype_argument_function (tree type, tree expr)
4872 tree fns = expr;
4873 tree fn, fn_no_ptr;
4875 fn = instantiate_type (type, fns, tf_none);
4876 if (fn == error_mark_node)
4877 return error_mark_node;
4879 fn_no_ptr = fn;
4880 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4881 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4882 if (TREE_CODE (fn_no_ptr) == BASELINK)
4883 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4885 /* [temp.arg.nontype]/1
4887 A template-argument for a non-type, non-template template-parameter
4888 shall be one of:
4889 [...]
4890 -- the address of an object or function with external linkage. */
4891 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4893 error ("%qE is not a valid template argument for type %qT "
4894 "because function %qD has not external linkage",
4895 expr, type, fn_no_ptr);
4896 return NULL_TREE;
4899 return fn;
4902 /* Subroutine of convert_nontype_argument.
4903 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4904 Emit an error otherwise. */
4906 static bool
4907 check_valid_ptrmem_cst_expr (tree type, tree expr)
4909 STRIP_NOPS (expr);
4910 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4911 return true;
4912 error ("%qE is not a valid template argument for type %qT",
4913 expr, type);
4914 error ("it must be a pointer-to-member of the form `&X::Y'");
4915 return false;
4918 /* Returns TRUE iff the address of OP is value-dependent.
4920 14.6.2.4 [temp.dep.temp]:
4921 A non-integral non-type template-argument is dependent if its type is
4922 dependent or it has either of the following forms
4923 qualified-id
4924 & qualified-id
4925 and contains a nested-name-specifier which specifies a class-name that
4926 names a dependent type.
4928 We generalize this to just say that the address of a member of a
4929 dependent class is value-dependent; the above doesn't cover the
4930 address of a static data member named with an unqualified-id. */
4932 static bool
4933 has_value_dependent_address (tree op)
4935 /* We could use get_inner_reference here, but there's no need;
4936 this is only relevant for template non-type arguments, which
4937 can only be expressed as &id-expression. */
4938 if (DECL_P (op))
4940 tree ctx = CP_DECL_CONTEXT (op);
4941 if (TYPE_P (ctx) && dependent_type_p (ctx))
4942 return true;
4945 return false;
4948 /* Attempt to convert the non-type template parameter EXPR to the
4949 indicated TYPE. If the conversion is successful, return the
4950 converted value. If the conversion is unsuccessful, return
4951 NULL_TREE if we issued an error message, or error_mark_node if we
4952 did not. We issue error messages for out-and-out bad template
4953 parameters, but not simply because the conversion failed, since we
4954 might be just trying to do argument deduction. Both TYPE and EXPR
4955 must be non-dependent.
4957 The conversion follows the special rules described in
4958 [temp.arg.nontype], and it is much more strict than an implicit
4959 conversion.
4961 This function is called twice for each template argument (see
4962 lookup_template_class for a more accurate description of this
4963 problem). This means that we need to handle expressions which
4964 are not valid in a C++ source, but can be created from the
4965 first call (for instance, casts to perform conversions). These
4966 hacks can go away after we fix the double coercion problem. */
4968 static tree
4969 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
4971 tree expr_type;
4973 /* Detect immediately string literals as invalid non-type argument.
4974 This special-case is not needed for correctness (we would easily
4975 catch this later), but only to provide better diagnostic for this
4976 common user mistake. As suggested by DR 100, we do not mention
4977 linkage issues in the diagnostic as this is not the point. */
4978 /* FIXME we're making this OK. */
4979 if (TREE_CODE (expr) == STRING_CST)
4981 if (complain & tf_error)
4982 error ("%qE is not a valid template argument for type %qT "
4983 "because string literals can never be used in this context",
4984 expr, type);
4985 return NULL_TREE;
4988 /* Add the ADDR_EXPR now for the benefit of
4989 value_dependent_expression_p. */
4990 if (TYPE_PTROBV_P (type))
4991 expr = decay_conversion (expr);
4993 /* If we are in a template, EXPR may be non-dependent, but still
4994 have a syntactic, rather than semantic, form. For example, EXPR
4995 might be a SCOPE_REF, rather than the VAR_DECL to which the
4996 SCOPE_REF refers. Preserving the qualifying scope is necessary
4997 so that access checking can be performed when the template is
4998 instantiated -- but here we need the resolved form so that we can
4999 convert the argument. */
5000 if (TYPE_REF_OBJ_P (type)
5001 && has_value_dependent_address (expr))
5002 /* If we want the address and it's value-dependent, don't fold. */;
5003 else if (!type_unknown_p (expr))
5004 expr = fold_non_dependent_expr_sfinae (expr, complain);
5005 if (error_operand_p (expr))
5006 return error_mark_node;
5007 expr_type = TREE_TYPE (expr);
5008 if (TREE_CODE (type) == REFERENCE_TYPE)
5009 expr = mark_lvalue_use (expr);
5010 else
5011 expr = mark_rvalue_use (expr);
5013 /* HACK: Due to double coercion, we can get a
5014 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5015 which is the tree that we built on the first call (see
5016 below when coercing to reference to object or to reference to
5017 function). We just strip everything and get to the arg.
5018 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5019 for examples. */
5020 if (TREE_CODE (expr) == NOP_EXPR)
5022 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5024 /* ??? Maybe we could use convert_from_reference here, but we
5025 would need to relax its constraints because the NOP_EXPR
5026 could actually change the type to something more cv-qualified,
5027 and this is not folded by convert_from_reference. */
5028 tree addr = TREE_OPERAND (expr, 0);
5029 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5030 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5031 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5032 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5033 (TREE_TYPE (expr_type),
5034 TREE_TYPE (TREE_TYPE (addr))));
5036 expr = TREE_OPERAND (addr, 0);
5037 expr_type = TREE_TYPE (expr);
5040 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5041 parameter is a pointer to object, through decay and
5042 qualification conversion. Let's strip everything. */
5043 else if (TYPE_PTROBV_P (type))
5045 STRIP_NOPS (expr);
5046 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5047 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5048 /* Skip the ADDR_EXPR only if it is part of the decay for
5049 an array. Otherwise, it is part of the original argument
5050 in the source code. */
5051 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5052 expr = TREE_OPERAND (expr, 0);
5053 expr_type = TREE_TYPE (expr);
5057 /* [temp.arg.nontype]/5, bullet 1
5059 For a non-type template-parameter of integral or enumeration type,
5060 integral promotions (_conv.prom_) and integral conversions
5061 (_conv.integral_) are applied. */
5062 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5064 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
5065 return error_mark_node;
5067 expr = fold_decl_constant_value (expr);
5068 /* Notice that there are constant expressions like '4 % 0' which
5069 do not fold into integer constants. */
5070 if (TREE_CODE (expr) != INTEGER_CST)
5072 if (complain & tf_error)
5073 error ("%qE is not a valid template argument for type %qT "
5074 "because it is a non-constant expression", expr, type);
5075 return NULL_TREE;
5078 /* At this point, an implicit conversion does what we want,
5079 because we already know that the expression is of integral
5080 type. */
5081 expr = perform_implicit_conversion (type, expr, complain);
5082 if (expr == error_mark_node)
5083 return error_mark_node;
5085 /* Conversion was allowed: fold it to a bare integer constant. */
5086 expr = fold (expr);
5088 /* [temp.arg.nontype]/5, bullet 2
5090 For a non-type template-parameter of type pointer to object,
5091 qualification conversions (_conv.qual_) and the array-to-pointer
5092 conversion (_conv.array_) are applied. */
5093 else if (TYPE_PTROBV_P (type))
5095 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5097 A template-argument for a non-type, non-template template-parameter
5098 shall be one of: [...]
5100 -- the name of a non-type template-parameter;
5101 -- the address of an object or function with external linkage, [...]
5102 expressed as "& id-expression" where the & is optional if the name
5103 refers to a function or array, or if the corresponding
5104 template-parameter is a reference.
5106 Here, we do not care about functions, as they are invalid anyway
5107 for a parameter of type pointer-to-object. */
5109 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5110 /* Non-type template parameters are OK. */
5112 else if (TREE_CODE (expr) != ADDR_EXPR
5113 && TREE_CODE (expr_type) != ARRAY_TYPE)
5115 if (TREE_CODE (expr) == VAR_DECL)
5117 error ("%qD is not a valid template argument "
5118 "because %qD is a variable, not the address of "
5119 "a variable",
5120 expr, expr);
5121 return NULL_TREE;
5123 /* Other values, like integer constants, might be valid
5124 non-type arguments of some other type. */
5125 return error_mark_node;
5127 else
5129 tree decl;
5131 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5132 ? TREE_OPERAND (expr, 0) : expr);
5133 if (TREE_CODE (decl) != VAR_DECL)
5135 error ("%qE is not a valid template argument of type %qT "
5136 "because %qE is not a variable",
5137 expr, type, decl);
5138 return NULL_TREE;
5140 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5142 error ("%qE is not a valid template argument of type %qT "
5143 "because %qD does not have external linkage",
5144 expr, type, decl);
5145 return NULL_TREE;
5149 expr = decay_conversion (expr);
5150 if (expr == error_mark_node)
5151 return error_mark_node;
5153 expr = perform_qualification_conversions (type, expr);
5154 if (expr == error_mark_node)
5155 return error_mark_node;
5157 /* [temp.arg.nontype]/5, bullet 3
5159 For a non-type template-parameter of type reference to object, no
5160 conversions apply. The type referred to by the reference may be more
5161 cv-qualified than the (otherwise identical) type of the
5162 template-argument. The template-parameter is bound directly to the
5163 template-argument, which must be an lvalue. */
5164 else if (TYPE_REF_OBJ_P (type))
5166 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5167 expr_type))
5168 return error_mark_node;
5170 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5172 error ("%qE is not a valid template argument for type %qT "
5173 "because of conflicts in cv-qualification", expr, type);
5174 return NULL_TREE;
5177 if (!real_lvalue_p (expr))
5179 error ("%qE is not a valid template argument for type %qT "
5180 "because it is not an lvalue", expr, type);
5181 return NULL_TREE;
5184 /* [temp.arg.nontype]/1
5186 A template-argument for a non-type, non-template template-parameter
5187 shall be one of: [...]
5189 -- the address of an object or function with external linkage. */
5190 if (TREE_CODE (expr) == INDIRECT_REF
5191 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5193 expr = TREE_OPERAND (expr, 0);
5194 if (DECL_P (expr))
5196 error ("%q#D is not a valid template argument for type %qT "
5197 "because a reference variable does not have a constant "
5198 "address", expr, type);
5199 return NULL_TREE;
5203 if (!DECL_P (expr))
5205 error ("%qE is not a valid template argument for type %qT "
5206 "because it is not an object with external linkage",
5207 expr, type);
5208 return NULL_TREE;
5211 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because object %qD has not external linkage",
5215 expr, type, expr);
5216 return NULL_TREE;
5219 expr = build_nop (type, build_address (expr));
5221 /* [temp.arg.nontype]/5, bullet 4
5223 For a non-type template-parameter of type pointer to function, only
5224 the function-to-pointer conversion (_conv.func_) is applied. If the
5225 template-argument represents a set of overloaded functions (or a
5226 pointer to such), the matching function is selected from the set
5227 (_over.over_). */
5228 else if (TYPE_PTRFN_P (type))
5230 /* If the argument is a template-id, we might not have enough
5231 context information to decay the pointer. */
5232 if (!type_unknown_p (expr_type))
5234 expr = decay_conversion (expr);
5235 if (expr == error_mark_node)
5236 return error_mark_node;
5239 expr = convert_nontype_argument_function (type, expr);
5240 if (!expr || expr == error_mark_node)
5241 return expr;
5243 if (TREE_CODE (expr) != ADDR_EXPR)
5245 error ("%qE is not a valid template argument for type %qT", expr, type);
5246 error ("it must be the address of a function with external linkage");
5247 return NULL_TREE;
5250 /* [temp.arg.nontype]/5, bullet 5
5252 For a non-type template-parameter of type reference to function, no
5253 conversions apply. If the template-argument represents a set of
5254 overloaded functions, the matching function is selected from the set
5255 (_over.over_). */
5256 else if (TYPE_REFFN_P (type))
5258 if (TREE_CODE (expr) == ADDR_EXPR)
5260 error ("%qE is not a valid template argument for type %qT "
5261 "because it is a pointer", expr, type);
5262 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5263 return NULL_TREE;
5266 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5267 if (!expr || expr == error_mark_node)
5268 return expr;
5270 expr = build_nop (type, build_address (expr));
5272 /* [temp.arg.nontype]/5, bullet 6
5274 For a non-type template-parameter of type pointer to member function,
5275 no conversions apply. If the template-argument represents a set of
5276 overloaded member functions, the matching member function is selected
5277 from the set (_over.over_). */
5278 else if (TYPE_PTRMEMFUNC_P (type))
5280 expr = instantiate_type (type, expr, tf_none);
5281 if (expr == error_mark_node)
5282 return error_mark_node;
5284 /* [temp.arg.nontype] bullet 1 says the pointer to member
5285 expression must be a pointer-to-member constant. */
5286 if (!check_valid_ptrmem_cst_expr (type, expr))
5287 return error_mark_node;
5289 /* There is no way to disable standard conversions in
5290 resolve_address_of_overloaded_function (called by
5291 instantiate_type). It is possible that the call succeeded by
5292 converting &B::I to &D::I (where B is a base of D), so we need
5293 to reject this conversion here.
5295 Actually, even if there was a way to disable standard conversions,
5296 it would still be better to reject them here so that we can
5297 provide a superior diagnostic. */
5298 if (!same_type_p (TREE_TYPE (expr), type))
5300 error ("%qE is not a valid template argument for type %qT "
5301 "because it is of type %qT", expr, type,
5302 TREE_TYPE (expr));
5303 /* If we are just one standard conversion off, explain. */
5304 if (can_convert (type, TREE_TYPE (expr)))
5305 inform (input_location,
5306 "standard conversions are not allowed in this context");
5307 return NULL_TREE;
5310 /* [temp.arg.nontype]/5, bullet 7
5312 For a non-type template-parameter of type pointer to data member,
5313 qualification conversions (_conv.qual_) are applied. */
5314 else if (TYPE_PTRMEM_P (type))
5316 /* [temp.arg.nontype] bullet 1 says the pointer to member
5317 expression must be a pointer-to-member constant. */
5318 if (!check_valid_ptrmem_cst_expr (type, expr))
5319 return error_mark_node;
5321 expr = perform_qualification_conversions (type, expr);
5322 if (expr == error_mark_node)
5323 return expr;
5325 /* A template non-type parameter must be one of the above. */
5326 else
5327 gcc_unreachable ();
5329 /* Sanity check: did we actually convert the argument to the
5330 right type? */
5331 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5332 (type, TREE_TYPE (expr)));
5333 return expr;
5336 /* Subroutine of coerce_template_template_parms, which returns 1 if
5337 PARM_PARM and ARG_PARM match using the rule for the template
5338 parameters of template template parameters. Both PARM and ARG are
5339 template parameters; the rest of the arguments are the same as for
5340 coerce_template_template_parms.
5342 static int
5343 coerce_template_template_parm (tree parm,
5344 tree arg,
5345 tsubst_flags_t complain,
5346 tree in_decl,
5347 tree outer_args)
5349 if (arg == NULL_TREE || arg == error_mark_node
5350 || parm == NULL_TREE || parm == error_mark_node)
5351 return 0;
5353 if (TREE_CODE (arg) != TREE_CODE (parm))
5354 return 0;
5356 switch (TREE_CODE (parm))
5358 case TEMPLATE_DECL:
5359 /* We encounter instantiations of templates like
5360 template <template <template <class> class> class TT>
5361 class C; */
5363 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5364 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5366 if (!coerce_template_template_parms
5367 (parmparm, argparm, complain, in_decl, outer_args))
5368 return 0;
5370 /* Fall through. */
5372 case TYPE_DECL:
5373 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5374 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5375 /* Argument is a parameter pack but parameter is not. */
5376 return 0;
5377 break;
5379 case PARM_DECL:
5380 /* The tsubst call is used to handle cases such as
5382 template <int> class C {};
5383 template <class T, template <T> class TT> class D {};
5384 D<int, C> d;
5386 i.e. the parameter list of TT depends on earlier parameters. */
5387 if (!uses_template_parms (TREE_TYPE (arg))
5388 && !same_type_p
5389 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5390 TREE_TYPE (arg)))
5391 return 0;
5393 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5394 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5395 /* Argument is a parameter pack but parameter is not. */
5396 return 0;
5398 break;
5400 default:
5401 gcc_unreachable ();
5404 return 1;
5408 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5409 template template parameters. Both PARM_PARMS and ARG_PARMS are
5410 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5411 or PARM_DECL.
5413 Consider the example:
5414 template <class T> class A;
5415 template<template <class U> class TT> class B;
5417 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5418 the parameters to A, and OUTER_ARGS contains A. */
5420 static int
5421 coerce_template_template_parms (tree parm_parms,
5422 tree arg_parms,
5423 tsubst_flags_t complain,
5424 tree in_decl,
5425 tree outer_args)
5427 int nparms, nargs, i;
5428 tree parm, arg;
5429 int variadic_p = 0;
5431 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5432 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5434 nparms = TREE_VEC_LENGTH (parm_parms);
5435 nargs = TREE_VEC_LENGTH (arg_parms);
5437 /* Determine whether we have a parameter pack at the end of the
5438 template template parameter's template parameter list. */
5439 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5441 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5443 if (parm == error_mark_node)
5444 return 0;
5446 switch (TREE_CODE (parm))
5448 case TEMPLATE_DECL:
5449 case TYPE_DECL:
5450 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5451 variadic_p = 1;
5452 break;
5454 case PARM_DECL:
5455 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5456 variadic_p = 1;
5457 break;
5459 default:
5460 gcc_unreachable ();
5464 if (nargs != nparms
5465 && !(variadic_p && nargs >= nparms - 1))
5466 return 0;
5468 /* Check all of the template parameters except the parameter pack at
5469 the end (if any). */
5470 for (i = 0; i < nparms - variadic_p; ++i)
5472 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5473 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5474 continue;
5476 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5477 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5479 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5480 outer_args))
5481 return 0;
5485 if (variadic_p)
5487 /* Check each of the template parameters in the template
5488 argument against the template parameter pack at the end of
5489 the template template parameter. */
5490 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5491 return 0;
5493 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5495 for (; i < nargs; ++i)
5497 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5498 continue;
5500 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5502 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5503 outer_args))
5504 return 0;
5508 return 1;
5511 /* Verifies that the deduced template arguments (in TARGS) for the
5512 template template parameters (in TPARMS) represent valid bindings,
5513 by comparing the template parameter list of each template argument
5514 to the template parameter list of its corresponding template
5515 template parameter, in accordance with DR150. This
5516 routine can only be called after all template arguments have been
5517 deduced. It will return TRUE if all of the template template
5518 parameter bindings are okay, FALSE otherwise. */
5519 bool
5520 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5522 int i, ntparms = TREE_VEC_LENGTH (tparms);
5523 bool ret = true;
5525 /* We're dealing with template parms in this process. */
5526 ++processing_template_decl;
5528 targs = INNERMOST_TEMPLATE_ARGS (targs);
5530 for (i = 0; i < ntparms; ++i)
5532 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5533 tree targ = TREE_VEC_ELT (targs, i);
5535 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5537 tree packed_args = NULL_TREE;
5538 int idx, len = 1;
5540 if (ARGUMENT_PACK_P (targ))
5542 /* Look inside the argument pack. */
5543 packed_args = ARGUMENT_PACK_ARGS (targ);
5544 len = TREE_VEC_LENGTH (packed_args);
5547 for (idx = 0; idx < len; ++idx)
5549 tree targ_parms = NULL_TREE;
5551 if (packed_args)
5552 /* Extract the next argument from the argument
5553 pack. */
5554 targ = TREE_VEC_ELT (packed_args, idx);
5556 if (PACK_EXPANSION_P (targ))
5557 /* Look at the pattern of the pack expansion. */
5558 targ = PACK_EXPANSION_PATTERN (targ);
5560 /* Extract the template parameters from the template
5561 argument. */
5562 if (TREE_CODE (targ) == TEMPLATE_DECL)
5563 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5564 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5565 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5567 /* Verify that we can coerce the template template
5568 parameters from the template argument to the template
5569 parameter. This requires an exact match. */
5570 if (targ_parms
5571 && !coerce_template_template_parms
5572 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5573 targ_parms,
5574 tf_none,
5575 tparm,
5576 targs))
5578 ret = false;
5579 goto out;
5585 out:
5587 --processing_template_decl;
5588 return ret;
5591 /* Convert the indicated template ARG as necessary to match the
5592 indicated template PARM. Returns the converted ARG, or
5593 error_mark_node if the conversion was unsuccessful. Error and
5594 warning messages are issued under control of COMPLAIN. This
5595 conversion is for the Ith parameter in the parameter list. ARGS is
5596 the full set of template arguments deduced so far. */
5598 static tree
5599 convert_template_argument (tree parm,
5600 tree arg,
5601 tree args,
5602 tsubst_flags_t complain,
5603 int i,
5604 tree in_decl)
5606 tree orig_arg;
5607 tree val;
5608 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5610 if (TREE_CODE (arg) == TREE_LIST
5611 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5613 /* The template argument was the name of some
5614 member function. That's usually
5615 invalid, but static members are OK. In any
5616 case, grab the underlying fields/functions
5617 and issue an error later if required. */
5618 orig_arg = TREE_VALUE (arg);
5619 TREE_TYPE (arg) = unknown_type_node;
5622 orig_arg = arg;
5624 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5625 requires_type = (TREE_CODE (parm) == TYPE_DECL
5626 || requires_tmpl_type);
5628 /* When determining whether an argument pack expansion is a template,
5629 look at the pattern. */
5630 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5631 arg = PACK_EXPANSION_PATTERN (arg);
5633 /* Deal with an injected-class-name used as a template template arg. */
5634 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5636 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5637 if (TREE_CODE (t) == TEMPLATE_DECL)
5639 if (complain & tf_warning_or_error)
5640 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5641 " used as template template argument", TYPE_NAME (arg));
5642 else if (flag_pedantic_errors)
5643 t = arg;
5645 arg = t;
5649 is_tmpl_type =
5650 ((TREE_CODE (arg) == TEMPLATE_DECL
5651 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5652 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5653 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5655 if (is_tmpl_type
5656 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5657 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5658 arg = TYPE_STUB_DECL (arg);
5660 is_type = TYPE_P (arg) || is_tmpl_type;
5662 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5663 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5665 permerror (input_location, "to refer to a type member of a template parameter, "
5666 "use %<typename %E%>", orig_arg);
5668 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5669 TREE_OPERAND (arg, 1),
5670 typename_type,
5671 complain & tf_error);
5672 arg = orig_arg;
5673 is_type = 1;
5675 if (is_type != requires_type)
5677 if (in_decl)
5679 if (complain & tf_error)
5681 error ("type/value mismatch at argument %d in template "
5682 "parameter list for %qD",
5683 i + 1, in_decl);
5684 if (is_type)
5685 error (" expected a constant of type %qT, got %qT",
5686 TREE_TYPE (parm),
5687 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5688 else if (requires_tmpl_type)
5689 error (" expected a class template, got %qE", orig_arg);
5690 else
5691 error (" expected a type, got %qE", orig_arg);
5694 return error_mark_node;
5696 if (is_tmpl_type ^ requires_tmpl_type)
5698 if (in_decl && (complain & tf_error))
5700 error ("type/value mismatch at argument %d in template "
5701 "parameter list for %qD",
5702 i + 1, in_decl);
5703 if (is_tmpl_type)
5704 error (" expected a type, got %qT", DECL_NAME (arg));
5705 else
5706 error (" expected a class template, got %qT", orig_arg);
5708 return error_mark_node;
5711 if (is_type)
5713 if (requires_tmpl_type)
5715 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5716 /* The number of argument required is not known yet.
5717 Just accept it for now. */
5718 val = TREE_TYPE (arg);
5719 else
5721 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5722 tree argparm;
5724 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5726 if (coerce_template_template_parms (parmparm, argparm,
5727 complain, in_decl,
5728 args))
5730 val = arg;
5732 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5733 TEMPLATE_DECL. */
5734 if (val != error_mark_node)
5736 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5737 val = TREE_TYPE (val);
5738 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5739 val = make_pack_expansion (val);
5742 else
5744 if (in_decl && (complain & tf_error))
5746 error ("type/value mismatch at argument %d in "
5747 "template parameter list for %qD",
5748 i + 1, in_decl);
5749 error (" expected a template of type %qD, got %qT",
5750 parm, orig_arg);
5753 val = error_mark_node;
5757 else
5758 val = orig_arg;
5759 /* We only form one instance of each template specialization.
5760 Therefore, if we use a non-canonical variant (i.e., a
5761 typedef), any future messages referring to the type will use
5762 the typedef, which is confusing if those future uses do not
5763 themselves also use the typedef. */
5764 if (TYPE_P (val))
5765 val = strip_typedefs (val);
5767 else
5769 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5771 if (invalid_nontype_parm_type_p (t, complain))
5772 return error_mark_node;
5774 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5776 if (same_type_p (t, TREE_TYPE (orig_arg)))
5777 val = orig_arg;
5778 else
5780 /* Not sure if this is reachable, but it doesn't hurt
5781 to be robust. */
5782 error ("type mismatch in nontype parameter pack");
5783 val = error_mark_node;
5786 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5787 /* We used to call digest_init here. However, digest_init
5788 will report errors, which we don't want when complain
5789 is zero. More importantly, digest_init will try too
5790 hard to convert things: for example, `0' should not be
5791 converted to pointer type at this point according to
5792 the standard. Accepting this is not merely an
5793 extension, since deciding whether or not these
5794 conversions can occur is part of determining which
5795 function template to call, or whether a given explicit
5796 argument specification is valid. */
5797 val = convert_nontype_argument (t, orig_arg, complain);
5798 else
5799 val = orig_arg;
5801 if (val == NULL_TREE)
5802 val = error_mark_node;
5803 else if (val == error_mark_node && (complain & tf_error))
5804 error ("could not convert template argument %qE to %qT", orig_arg, t);
5806 if (TREE_CODE (val) == SCOPE_REF)
5808 /* Strip typedefs from the SCOPE_REF. */
5809 tree type = strip_typedefs (TREE_TYPE (val));
5810 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5811 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5812 QUALIFIED_NAME_IS_TEMPLATE (val));
5816 return val;
5819 /* Coerces the remaining template arguments in INNER_ARGS (from
5820 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5821 Returns the coerced argument pack. PARM_IDX is the position of this
5822 parameter in the template parameter list. ARGS is the original
5823 template argument list. */
5824 static tree
5825 coerce_template_parameter_pack (tree parms,
5826 int parm_idx,
5827 tree args,
5828 tree inner_args,
5829 int arg_idx,
5830 tree new_args,
5831 int* lost,
5832 tree in_decl,
5833 tsubst_flags_t complain)
5835 tree parm = TREE_VEC_ELT (parms, parm_idx);
5836 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5837 tree packed_args;
5838 tree argument_pack;
5839 tree packed_types = NULL_TREE;
5841 if (arg_idx > nargs)
5842 arg_idx = nargs;
5844 packed_args = make_tree_vec (nargs - arg_idx);
5846 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5847 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5849 /* When the template parameter is a non-type template
5850 parameter pack whose type uses parameter packs, we need
5851 to look at each of the template arguments
5852 separately. Build a vector of the types for these
5853 non-type template parameters in PACKED_TYPES. */
5854 tree expansion
5855 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5856 packed_types = tsubst_pack_expansion (expansion, args,
5857 complain, in_decl);
5859 if (packed_types == error_mark_node)
5860 return error_mark_node;
5862 /* Check that we have the right number of arguments. */
5863 if (arg_idx < nargs
5864 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5865 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5867 int needed_parms
5868 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5869 error ("wrong number of template arguments (%d, should be %d)",
5870 nargs, needed_parms);
5871 return error_mark_node;
5874 /* If we aren't able to check the actual arguments now
5875 (because they haven't been expanded yet), we can at least
5876 verify that all of the types used for the non-type
5877 template parameter pack are, in fact, valid for non-type
5878 template parameters. */
5879 if (arg_idx < nargs
5880 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5882 int j, len = TREE_VEC_LENGTH (packed_types);
5883 for (j = 0; j < len; ++j)
5885 tree t = TREE_VEC_ELT (packed_types, j);
5886 if (invalid_nontype_parm_type_p (t, complain))
5887 return error_mark_node;
5892 /* Convert the remaining arguments, which will be a part of the
5893 parameter pack "parm". */
5894 for (; arg_idx < nargs; ++arg_idx)
5896 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5897 tree actual_parm = TREE_VALUE (parm);
5899 if (packed_types && !PACK_EXPANSION_P (arg))
5901 /* When we have a vector of types (corresponding to the
5902 non-type template parameter pack that uses parameter
5903 packs in its type, as mention above), and the
5904 argument is not an expansion (which expands to a
5905 currently unknown number of arguments), clone the
5906 parm and give it the next type in PACKED_TYPES. */
5907 actual_parm = copy_node (actual_parm);
5908 TREE_TYPE (actual_parm) =
5909 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5912 if (arg != error_mark_node)
5913 arg = convert_template_argument (actual_parm,
5914 arg, new_args, complain, parm_idx,
5915 in_decl);
5916 if (arg == error_mark_node)
5917 (*lost)++;
5918 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5921 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5922 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5923 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5924 else
5926 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5927 TREE_TYPE (argument_pack)
5928 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5929 TREE_CONSTANT (argument_pack) = 1;
5932 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5933 #ifdef ENABLE_CHECKING
5934 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
5935 TREE_VEC_LENGTH (packed_args));
5936 #endif
5937 return argument_pack;
5940 /* Convert all template arguments to their appropriate types, and
5941 return a vector containing the innermost resulting template
5942 arguments. If any error occurs, return error_mark_node. Error and
5943 warning messages are issued under control of COMPLAIN.
5945 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5946 for arguments not specified in ARGS. Otherwise, if
5947 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5948 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5949 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5950 ARGS. */
5952 static tree
5953 coerce_template_parms (tree parms,
5954 tree args,
5955 tree in_decl,
5956 tsubst_flags_t complain,
5957 bool require_all_args,
5958 bool use_default_args)
5960 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5961 tree inner_args;
5962 tree new_args;
5963 tree new_inner_args;
5964 int saved_unevaluated_operand;
5965 int saved_inhibit_evaluation_warnings;
5967 /* When used as a boolean value, indicates whether this is a
5968 variadic template parameter list. Since it's an int, we can also
5969 subtract it from nparms to get the number of non-variadic
5970 parameters. */
5971 int variadic_p = 0;
5973 nparms = TREE_VEC_LENGTH (parms);
5975 /* Determine if there are any parameter packs. */
5976 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5978 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5979 if (template_parameter_pack_p (tparm))
5980 ++variadic_p;
5983 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5984 /* If there are 0 or 1 parameter packs, we need to expand any argument
5985 packs so that we can deduce a parameter pack from some non-packed args
5986 followed by an argument pack, as in variadic85.C. If there are more
5987 than that, we need to leave argument packs intact so the arguments are
5988 assigned to the right parameter packs. This should only happen when
5989 dealing with a nested class inside a partial specialization of a class
5990 template, as in variadic92.C. */
5991 if (variadic_p <= 1)
5992 inner_args = expand_template_argument_pack (inner_args);
5994 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5995 if ((nargs > nparms && !variadic_p)
5996 || (nargs < nparms - variadic_p
5997 && require_all_args
5998 && (!use_default_args
5999 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6000 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6002 if (complain & tf_error)
6004 if (variadic_p)
6006 --nparms;
6007 error ("wrong number of template arguments "
6008 "(%d, should be %d or more)", nargs, nparms);
6010 else
6011 error ("wrong number of template arguments "
6012 "(%d, should be %d)", nargs, nparms);
6014 if (in_decl)
6015 error ("provided for %q+D", in_decl);
6018 return error_mark_node;
6021 /* We need to evaluate the template arguments, even though this
6022 template-id may be nested within a "sizeof". */
6023 saved_unevaluated_operand = cp_unevaluated_operand;
6024 cp_unevaluated_operand = 0;
6025 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6026 c_inhibit_evaluation_warnings = 0;
6027 new_inner_args = make_tree_vec (nparms);
6028 new_args = add_outermost_template_args (args, new_inner_args);
6029 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6031 tree arg;
6032 tree parm;
6034 /* Get the Ith template parameter. */
6035 parm = TREE_VEC_ELT (parms, parm_idx);
6037 if (parm == error_mark_node)
6039 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6040 continue;
6043 /* Calculate the next argument. */
6044 if (arg_idx < nargs)
6045 arg = TREE_VEC_ELT (inner_args, arg_idx);
6046 else
6047 arg = NULL_TREE;
6049 if (template_parameter_pack_p (TREE_VALUE (parm))
6050 && !(arg && ARGUMENT_PACK_P (arg)))
6052 /* All remaining arguments will be placed in the
6053 template parameter pack PARM. */
6054 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6055 inner_args, arg_idx,
6056 new_args, &lost,
6057 in_decl, complain);
6059 /* Store this argument. */
6060 if (arg == error_mark_node)
6061 lost++;
6062 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6064 /* We are done with all of the arguments. */
6065 arg_idx = nargs;
6067 continue;
6069 else if (arg)
6071 if (PACK_EXPANSION_P (arg))
6073 if (complain & tf_error)
6075 /* FIXME this restriction was removed by N2555; see
6076 bug 35722. */
6077 /* If ARG is a pack expansion, but PARM is not a
6078 template parameter pack (if it were, we would have
6079 handled it above), we're trying to expand into a
6080 fixed-length argument list. */
6081 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6082 sorry ("cannot expand %<%E%> into a fixed-length "
6083 "argument list", arg);
6084 else
6085 sorry ("cannot expand %<%T%> into a fixed-length "
6086 "argument list", arg);
6088 return error_mark_node;
6091 else if (require_all_args)
6093 /* There must be a default arg in this case. */
6094 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6095 complain, in_decl);
6096 /* The position of the first default template argument,
6097 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6098 Record that. */
6099 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6100 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6102 else
6103 break;
6105 if (arg == error_mark_node)
6107 if (complain & tf_error)
6108 error ("template argument %d is invalid", arg_idx + 1);
6110 else if (!arg)
6111 /* This only occurs if there was an error in the template
6112 parameter list itself (which we would already have
6113 reported) that we are trying to recover from, e.g., a class
6114 template with a parameter list such as
6115 template<typename..., typename>. */
6116 return error_mark_node;
6117 else
6118 arg = convert_template_argument (TREE_VALUE (parm),
6119 arg, new_args, complain,
6120 parm_idx, in_decl);
6122 if (arg == error_mark_node)
6123 lost++;
6124 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6126 cp_unevaluated_operand = saved_unevaluated_operand;
6127 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6129 if (lost)
6130 return error_mark_node;
6132 #ifdef ENABLE_CHECKING
6133 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6134 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6135 TREE_VEC_LENGTH (new_inner_args));
6136 #endif
6138 return new_inner_args;
6141 /* Returns 1 if template args OT and NT are equivalent. */
6143 static int
6144 template_args_equal (tree ot, tree nt)
6146 if (nt == ot)
6147 return 1;
6149 if (TREE_CODE (nt) == TREE_VEC)
6150 /* For member templates */
6151 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6152 else if (PACK_EXPANSION_P (ot))
6153 return PACK_EXPANSION_P (nt)
6154 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6155 PACK_EXPANSION_PATTERN (nt));
6156 else if (ARGUMENT_PACK_P (ot))
6158 int i, len;
6159 tree opack, npack;
6161 if (!ARGUMENT_PACK_P (nt))
6162 return 0;
6164 opack = ARGUMENT_PACK_ARGS (ot);
6165 npack = ARGUMENT_PACK_ARGS (nt);
6166 len = TREE_VEC_LENGTH (opack);
6167 if (TREE_VEC_LENGTH (npack) != len)
6168 return 0;
6169 for (i = 0; i < len; ++i)
6170 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6171 TREE_VEC_ELT (npack, i)))
6172 return 0;
6173 return 1;
6175 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6177 /* We get here probably because we are in the middle of substituting
6178 into the pattern of a pack expansion. In that case the
6179 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6180 interested in. So we want to use the initial pack argument for
6181 the comparison. */
6182 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6183 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6184 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6185 return template_args_equal (ot, nt);
6187 else if (TYPE_P (nt))
6188 return TYPE_P (ot) && same_type_p (ot, nt);
6189 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6190 return 0;
6191 else
6192 return cp_tree_equal (ot, nt);
6195 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6196 of template arguments. Returns 0 otherwise. */
6199 comp_template_args (tree oldargs, tree newargs)
6201 int i;
6203 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6204 return 0;
6206 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6208 tree nt = TREE_VEC_ELT (newargs, i);
6209 tree ot = TREE_VEC_ELT (oldargs, i);
6211 if (! template_args_equal (ot, nt))
6212 return 0;
6214 return 1;
6217 static void
6218 add_pending_template (tree d)
6220 tree ti = (TYPE_P (d)
6221 ? CLASSTYPE_TEMPLATE_INFO (d)
6222 : DECL_TEMPLATE_INFO (d));
6223 struct pending_template *pt;
6224 int level;
6226 if (TI_PENDING_TEMPLATE_FLAG (ti))
6227 return;
6229 /* We are called both from instantiate_decl, where we've already had a
6230 tinst_level pushed, and instantiate_template, where we haven't.
6231 Compensate. */
6232 level = !current_tinst_level || current_tinst_level->decl != d;
6234 if (level)
6235 push_tinst_level (d);
6237 pt = ggc_alloc_pending_template ();
6238 pt->next = NULL;
6239 pt->tinst = current_tinst_level;
6240 if (last_pending_template)
6241 last_pending_template->next = pt;
6242 else
6243 pending_templates = pt;
6245 last_pending_template = pt;
6247 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6249 if (level)
6250 pop_tinst_level ();
6254 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6255 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6256 documentation for TEMPLATE_ID_EXPR. */
6258 tree
6259 lookup_template_function (tree fns, tree arglist)
6261 tree type;
6263 if (fns == error_mark_node || arglist == error_mark_node)
6264 return error_mark_node;
6266 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6267 gcc_assert (fns && (is_overloaded_fn (fns)
6268 || TREE_CODE (fns) == IDENTIFIER_NODE));
6270 if (BASELINK_P (fns))
6272 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6273 unknown_type_node,
6274 BASELINK_FUNCTIONS (fns),
6275 arglist);
6276 return fns;
6279 type = TREE_TYPE (fns);
6280 if (TREE_CODE (fns) == OVERLOAD || !type)
6281 type = unknown_type_node;
6283 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6286 /* Within the scope of a template class S<T>, the name S gets bound
6287 (in build_self_reference) to a TYPE_DECL for the class, not a
6288 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6289 or one of its enclosing classes, and that type is a template,
6290 return the associated TEMPLATE_DECL. Otherwise, the original
6291 DECL is returned.
6293 Also handle the case when DECL is a TREE_LIST of ambiguous
6294 injected-class-names from different bases. */
6296 tree
6297 maybe_get_template_decl_from_type_decl (tree decl)
6299 if (decl == NULL_TREE)
6300 return decl;
6302 /* DR 176: A lookup that finds an injected-class-name (10.2
6303 [class.member.lookup]) can result in an ambiguity in certain cases
6304 (for example, if it is found in more than one base class). If all of
6305 the injected-class-names that are found refer to specializations of
6306 the same class template, and if the name is followed by a
6307 template-argument-list, the reference refers to the class template
6308 itself and not a specialization thereof, and is not ambiguous. */
6309 if (TREE_CODE (decl) == TREE_LIST)
6311 tree t, tmpl = NULL_TREE;
6312 for (t = decl; t; t = TREE_CHAIN (t))
6314 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6315 if (!tmpl)
6316 tmpl = elt;
6317 else if (tmpl != elt)
6318 break;
6320 if (tmpl && t == NULL_TREE)
6321 return tmpl;
6322 else
6323 return decl;
6326 return (decl != NULL_TREE
6327 && DECL_SELF_REFERENCE_P (decl)
6328 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6329 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6332 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6333 parameters, find the desired type.
6335 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6337 IN_DECL, if non-NULL, is the template declaration we are trying to
6338 instantiate.
6340 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6341 the class we are looking up.
6343 Issue error and warning messages under control of COMPLAIN.
6345 If the template class is really a local class in a template
6346 function, then the FUNCTION_CONTEXT is the function in which it is
6347 being instantiated.
6349 ??? Note that this function is currently called *twice* for each
6350 template-id: the first time from the parser, while creating the
6351 incomplete type (finish_template_type), and the second type during the
6352 real instantiation (instantiate_template_class). This is surely something
6353 that we want to avoid. It also causes some problems with argument
6354 coercion (see convert_nontype_argument for more information on this). */
6356 tree
6357 lookup_template_class (tree d1,
6358 tree arglist,
6359 tree in_decl,
6360 tree context,
6361 int entering_scope,
6362 tsubst_flags_t complain)
6364 tree templ = NULL_TREE, parmlist;
6365 tree t;
6366 spec_entry **slot;
6367 spec_entry *entry;
6368 spec_entry elt;
6369 hashval_t hash;
6371 timevar_push (TV_NAME_LOOKUP);
6373 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6375 tree value = innermost_non_namespace_value (d1);
6376 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6377 templ = value;
6378 else
6380 if (context)
6381 push_decl_namespace (context);
6382 templ = lookup_name (d1);
6383 templ = maybe_get_template_decl_from_type_decl (templ);
6384 if (context)
6385 pop_decl_namespace ();
6387 if (templ)
6388 context = DECL_CONTEXT (templ);
6390 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6392 tree type = TREE_TYPE (d1);
6394 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6395 an implicit typename for the second A. Deal with it. */
6396 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6397 type = TREE_TYPE (type);
6399 if (CLASSTYPE_TEMPLATE_INFO (type))
6401 templ = CLASSTYPE_TI_TEMPLATE (type);
6402 d1 = DECL_NAME (templ);
6405 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6406 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6408 templ = TYPE_TI_TEMPLATE (d1);
6409 d1 = DECL_NAME (templ);
6411 else if (TREE_CODE (d1) == TEMPLATE_DECL
6412 && DECL_TEMPLATE_RESULT (d1)
6413 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6415 templ = d1;
6416 d1 = DECL_NAME (templ);
6417 context = DECL_CONTEXT (templ);
6420 /* Issue an error message if we didn't find a template. */
6421 if (! templ)
6423 if (complain & tf_error)
6424 error ("%qT is not a template", d1);
6425 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6428 if (TREE_CODE (templ) != TEMPLATE_DECL
6429 /* Make sure it's a user visible template, if it was named by
6430 the user. */
6431 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6432 && !PRIMARY_TEMPLATE_P (templ)))
6434 if (complain & tf_error)
6436 error ("non-template type %qT used as a template", d1);
6437 if (in_decl)
6438 error ("for template declaration %q+D", in_decl);
6440 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6443 complain &= ~tf_user;
6445 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6447 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6448 template arguments */
6450 tree parm;
6451 tree arglist2;
6452 tree outer;
6454 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6456 /* Consider an example where a template template parameter declared as
6458 template <class T, class U = std::allocator<T> > class TT
6460 The template parameter level of T and U are one level larger than
6461 of TT. To proper process the default argument of U, say when an
6462 instantiation `TT<int>' is seen, we need to build the full
6463 arguments containing {int} as the innermost level. Outer levels,
6464 available when not appearing as default template argument, can be
6465 obtained from the arguments of the enclosing template.
6467 Suppose that TT is later substituted with std::vector. The above
6468 instantiation is `TT<int, std::allocator<T> >' with TT at
6469 level 1, and T at level 2, while the template arguments at level 1
6470 becomes {std::vector} and the inner level 2 is {int}. */
6472 outer = DECL_CONTEXT (templ);
6473 if (outer)
6474 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6475 else if (current_template_parms)
6476 /* This is an argument of the current template, so we haven't set
6477 DECL_CONTEXT yet. */
6478 outer = current_template_args ();
6480 if (outer)
6481 arglist = add_to_template_args (outer, arglist);
6483 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6484 complain,
6485 /*require_all_args=*/true,
6486 /*use_default_args=*/true);
6487 if (arglist2 == error_mark_node
6488 || (!uses_template_parms (arglist2)
6489 && check_instantiated_args (templ, arglist2, complain)))
6490 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6492 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6493 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6495 else
6497 tree template_type = TREE_TYPE (templ);
6498 tree gen_tmpl;
6499 tree type_decl;
6500 tree found = NULL_TREE;
6501 int arg_depth;
6502 int parm_depth;
6503 int is_dependent_type;
6504 int use_partial_inst_tmpl = false;
6506 gen_tmpl = most_general_template (templ);
6507 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6508 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6509 arg_depth = TMPL_ARGS_DEPTH (arglist);
6511 if (arg_depth == 1 && parm_depth > 1)
6513 /* We've been given an incomplete set of template arguments.
6514 For example, given:
6516 template <class T> struct S1 {
6517 template <class U> struct S2 {};
6518 template <class U> struct S2<U*> {};
6521 we will be called with an ARGLIST of `U*', but the
6522 TEMPLATE will be `template <class T> template
6523 <class U> struct S1<T>::S2'. We must fill in the missing
6524 arguments. */
6525 arglist
6526 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6527 arglist);
6528 arg_depth = TMPL_ARGS_DEPTH (arglist);
6531 /* Now we should have enough arguments. */
6532 gcc_assert (parm_depth == arg_depth);
6534 /* From here on, we're only interested in the most general
6535 template. */
6537 /* Calculate the BOUND_ARGS. These will be the args that are
6538 actually tsubst'd into the definition to create the
6539 instantiation. */
6540 if (parm_depth > 1)
6542 /* We have multiple levels of arguments to coerce, at once. */
6543 int i;
6544 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6546 tree bound_args = make_tree_vec (parm_depth);
6548 for (i = saved_depth,
6549 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6550 i > 0 && t != NULL_TREE;
6551 --i, t = TREE_CHAIN (t))
6553 tree a;
6554 if (i == saved_depth)
6555 a = coerce_template_parms (TREE_VALUE (t),
6556 arglist, gen_tmpl,
6557 complain,
6558 /*require_all_args=*/true,
6559 /*use_default_args=*/true);
6560 else
6561 /* Outer levels should have already been coerced. */
6562 a = TMPL_ARGS_LEVEL (arglist, i);
6564 /* Don't process further if one of the levels fails. */
6565 if (a == error_mark_node)
6567 /* Restore the ARGLIST to its full size. */
6568 TREE_VEC_LENGTH (arglist) = saved_depth;
6569 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6572 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6574 /* We temporarily reduce the length of the ARGLIST so
6575 that coerce_template_parms will see only the arguments
6576 corresponding to the template parameters it is
6577 examining. */
6578 TREE_VEC_LENGTH (arglist)--;
6581 /* Restore the ARGLIST to its full size. */
6582 TREE_VEC_LENGTH (arglist) = saved_depth;
6584 arglist = bound_args;
6586 else
6587 arglist
6588 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6589 INNERMOST_TEMPLATE_ARGS (arglist),
6590 gen_tmpl,
6591 complain,
6592 /*require_all_args=*/true,
6593 /*use_default_args=*/true);
6595 if (arglist == error_mark_node)
6596 /* We were unable to bind the arguments. */
6597 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6599 /* In the scope of a template class, explicit references to the
6600 template class refer to the type of the template, not any
6601 instantiation of it. For example, in:
6603 template <class T> class C { void f(C<T>); }
6605 the `C<T>' is just the same as `C'. Outside of the
6606 class, however, such a reference is an instantiation. */
6607 if ((entering_scope
6608 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6609 || currently_open_class (template_type))
6610 /* comp_template_args is expensive, check it last. */
6611 && comp_template_args (TYPE_TI_ARGS (template_type),
6612 arglist))
6613 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6615 /* If we already have this specialization, return it. */
6616 elt.tmpl = gen_tmpl;
6617 elt.args = arglist;
6618 hash = hash_specialization (&elt);
6619 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6620 &elt, hash);
6622 if (entry)
6623 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6625 is_dependent_type = uses_template_parms (arglist);
6627 /* If the deduced arguments are invalid, then the binding
6628 failed. */
6629 if (!is_dependent_type
6630 && check_instantiated_args (gen_tmpl,
6631 INNERMOST_TEMPLATE_ARGS (arglist),
6632 complain))
6633 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6635 if (!is_dependent_type
6636 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6637 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6638 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6640 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6641 DECL_NAME (gen_tmpl),
6642 /*tag_scope=*/ts_global);
6643 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6646 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6647 complain, in_decl);
6648 if (!context)
6649 context = global_namespace;
6651 /* Create the type. */
6652 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6654 if (!is_dependent_type)
6656 set_current_access_from_decl (TYPE_NAME (template_type));
6657 t = start_enum (TYPE_IDENTIFIER (template_type),
6658 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6659 arglist, complain, in_decl),
6660 SCOPED_ENUM_P (template_type));
6662 else
6664 /* We don't want to call start_enum for this type, since
6665 the values for the enumeration constants may involve
6666 template parameters. And, no one should be interested
6667 in the enumeration constants for such a type. */
6668 t = cxx_make_type (ENUMERAL_TYPE);
6669 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6672 else
6674 t = make_class_type (TREE_CODE (template_type));
6675 CLASSTYPE_DECLARED_CLASS (t)
6676 = CLASSTYPE_DECLARED_CLASS (template_type);
6677 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6678 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6680 /* A local class. Make sure the decl gets registered properly. */
6681 if (context == current_function_decl)
6682 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6684 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6685 /* This instantiation is another name for the primary
6686 template type. Set the TYPE_CANONICAL field
6687 appropriately. */
6688 TYPE_CANONICAL (t) = template_type;
6689 else if (any_template_arguments_need_structural_equality_p (arglist))
6690 /* Some of the template arguments require structural
6691 equality testing, so this template class requires
6692 structural equality testing. */
6693 SET_TYPE_STRUCTURAL_EQUALITY (t);
6696 /* If we called start_enum or pushtag above, this information
6697 will already be set up. */
6698 if (!TYPE_NAME (t))
6700 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6702 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6703 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6704 DECL_SOURCE_LOCATION (type_decl)
6705 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6707 else
6708 type_decl = TYPE_NAME (t);
6710 TREE_PRIVATE (type_decl)
6711 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6712 TREE_PROTECTED (type_decl)
6713 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6714 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6716 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6717 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6720 /* Let's consider the explicit specialization of a member
6721 of a class template specialization that is implicitely instantiated,
6722 e.g.:
6723 template<class T>
6724 struct S
6726 template<class U> struct M {}; //#0
6729 template<>
6730 template<>
6731 struct S<int>::M<char> //#1
6733 int i;
6735 [temp.expl.spec]/4 says this is valid.
6737 In this case, when we write:
6738 S<int>::M<char> m;
6740 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6741 the one of #0.
6743 When we encounter #1, we want to store the partial instantiation
6744 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6746 For all cases other than this "explicit specialization of member of a
6747 class template", we just want to store the most general template into
6748 the CLASSTYPE_TI_TEMPLATE of M.
6750 This case of "explicit specialization of member of a class template"
6751 only happens when:
6752 1/ the enclosing class is an instantiation of, and therefore not
6753 the same as, the context of the most general template, and
6754 2/ we aren't looking at the partial instantiation itself, i.e.
6755 the innermost arguments are not the same as the innermost parms of
6756 the most general template.
6758 So it's only when 1/ and 2/ happens that we want to use the partial
6759 instantiation of the member template in lieu of its most general
6760 template. */
6762 if (PRIMARY_TEMPLATE_P (gen_tmpl)
6763 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
6764 /* the enclosing class must be an instantiation... */
6765 && CLASS_TYPE_P (context)
6766 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
6768 tree partial_inst_args;
6769 TREE_VEC_LENGTH (arglist)--;
6770 ++processing_template_decl;
6771 partial_inst_args =
6772 tsubst (INNERMOST_TEMPLATE_ARGS
6773 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
6774 arglist, complain, NULL_TREE);
6775 --processing_template_decl;
6776 TREE_VEC_LENGTH (arglist)++;
6777 use_partial_inst_tmpl =
6778 /*...and we must not be looking at the partial instantiation
6779 itself. */
6780 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
6781 partial_inst_args);
6784 if (!use_partial_inst_tmpl)
6785 /* This case is easy; there are no member templates involved. */
6786 found = gen_tmpl;
6787 else
6789 /* This is a full instantiation of a member template. Find
6790 the partial instantiation of which this is an instance. */
6792 /* Temporarily reduce by one the number of levels in the ARGLIST
6793 so as to avoid comparing the last set of arguments. */
6794 TREE_VEC_LENGTH (arglist)--;
6795 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6796 TREE_VEC_LENGTH (arglist)++;
6797 found = CLASSTYPE_TI_TEMPLATE (found);
6800 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6802 elt.spec = t;
6803 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6804 &elt, hash, INSERT);
6805 *slot = ggc_alloc_spec_entry ();
6806 **slot = elt;
6808 /* Note this use of the partial instantiation so we can check it
6809 later in maybe_process_partial_specialization. */
6810 DECL_TEMPLATE_INSTANTIATIONS (templ)
6811 = tree_cons (arglist, t,
6812 DECL_TEMPLATE_INSTANTIATIONS (templ));
6814 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
6815 /* Now that the type has been registered on the instantiations
6816 list, we set up the enumerators. Because the enumeration
6817 constants may involve the enumeration type itself, we make
6818 sure to register the type first, and then create the
6819 constants. That way, doing tsubst_expr for the enumeration
6820 constants won't result in recursive calls here; we'll find
6821 the instantiation and exit above. */
6822 tsubst_enum (template_type, t, arglist);
6824 if (is_dependent_type)
6825 /* If the type makes use of template parameters, the
6826 code that generates debugging information will crash. */
6827 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6829 /* Possibly limit visibility based on template args. */
6830 TREE_PUBLIC (type_decl) = 1;
6831 determine_visibility (type_decl);
6833 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6835 timevar_pop (TV_NAME_LOOKUP);
6838 struct pair_fn_data
6840 tree_fn_t fn;
6841 void *data;
6842 /* True when we should also visit template parameters that occur in
6843 non-deduced contexts. */
6844 bool include_nondeduced_p;
6845 struct pointer_set_t *visited;
6848 /* Called from for_each_template_parm via walk_tree. */
6850 static tree
6851 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6853 tree t = *tp;
6854 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6855 tree_fn_t fn = pfd->fn;
6856 void *data = pfd->data;
6858 if (TYPE_P (t)
6859 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6860 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6861 pfd->include_nondeduced_p))
6862 return error_mark_node;
6864 switch (TREE_CODE (t))
6866 case RECORD_TYPE:
6867 if (TYPE_PTRMEMFUNC_P (t))
6868 break;
6869 /* Fall through. */
6871 case UNION_TYPE:
6872 case ENUMERAL_TYPE:
6873 if (!TYPE_TEMPLATE_INFO (t))
6874 *walk_subtrees = 0;
6875 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6876 fn, data, pfd->visited,
6877 pfd->include_nondeduced_p))
6878 return error_mark_node;
6879 break;
6881 case INTEGER_TYPE:
6882 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6883 fn, data, pfd->visited,
6884 pfd->include_nondeduced_p)
6885 || for_each_template_parm (TYPE_MAX_VALUE (t),
6886 fn, data, pfd->visited,
6887 pfd->include_nondeduced_p))
6888 return error_mark_node;
6889 break;
6891 case METHOD_TYPE:
6892 /* Since we're not going to walk subtrees, we have to do this
6893 explicitly here. */
6894 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6895 pfd->visited, pfd->include_nondeduced_p))
6896 return error_mark_node;
6897 /* Fall through. */
6899 case FUNCTION_TYPE:
6900 /* Check the return type. */
6901 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6902 pfd->include_nondeduced_p))
6903 return error_mark_node;
6905 /* Check the parameter types. Since default arguments are not
6906 instantiated until they are needed, the TYPE_ARG_TYPES may
6907 contain expressions that involve template parameters. But,
6908 no-one should be looking at them yet. And, once they're
6909 instantiated, they don't contain template parameters, so
6910 there's no point in looking at them then, either. */
6912 tree parm;
6914 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6915 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6916 pfd->visited, pfd->include_nondeduced_p))
6917 return error_mark_node;
6919 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6920 want walk_tree walking into them itself. */
6921 *walk_subtrees = 0;
6923 break;
6925 case TYPEOF_TYPE:
6926 if (pfd->include_nondeduced_p
6927 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6928 pfd->visited,
6929 pfd->include_nondeduced_p))
6930 return error_mark_node;
6931 break;
6933 case FUNCTION_DECL:
6934 case VAR_DECL:
6935 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6936 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6937 pfd->visited, pfd->include_nondeduced_p))
6938 return error_mark_node;
6939 /* Fall through. */
6941 case PARM_DECL:
6942 case CONST_DECL:
6943 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6944 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6945 pfd->visited, pfd->include_nondeduced_p))
6946 return error_mark_node;
6947 if (DECL_CONTEXT (t)
6948 && pfd->include_nondeduced_p
6949 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6950 pfd->visited, pfd->include_nondeduced_p))
6951 return error_mark_node;
6952 break;
6954 case BOUND_TEMPLATE_TEMPLATE_PARM:
6955 /* Record template parameters such as `T' inside `TT<T>'. */
6956 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6957 pfd->include_nondeduced_p))
6958 return error_mark_node;
6959 /* Fall through. */
6961 case TEMPLATE_TEMPLATE_PARM:
6962 case TEMPLATE_TYPE_PARM:
6963 case TEMPLATE_PARM_INDEX:
6964 if (fn && (*fn)(t, data))
6965 return error_mark_node;
6966 else if (!fn)
6967 return error_mark_node;
6968 break;
6970 case TEMPLATE_DECL:
6971 /* A template template parameter is encountered. */
6972 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6973 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6974 pfd->include_nondeduced_p))
6975 return error_mark_node;
6977 /* Already substituted template template parameter */
6978 *walk_subtrees = 0;
6979 break;
6981 case TYPENAME_TYPE:
6982 if (!fn
6983 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6984 data, pfd->visited,
6985 pfd->include_nondeduced_p))
6986 return error_mark_node;
6987 break;
6989 case CONSTRUCTOR:
6990 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6991 && pfd->include_nondeduced_p
6992 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6993 (TREE_TYPE (t)), fn, data,
6994 pfd->visited, pfd->include_nondeduced_p))
6995 return error_mark_node;
6996 break;
6998 case INDIRECT_REF:
6999 case COMPONENT_REF:
7000 /* If there's no type, then this thing must be some expression
7001 involving template parameters. */
7002 if (!fn && !TREE_TYPE (t))
7003 return error_mark_node;
7004 break;
7006 case MODOP_EXPR:
7007 case CAST_EXPR:
7008 case REINTERPRET_CAST_EXPR:
7009 case CONST_CAST_EXPR:
7010 case STATIC_CAST_EXPR:
7011 case DYNAMIC_CAST_EXPR:
7012 case ARROW_EXPR:
7013 case DOTSTAR_EXPR:
7014 case TYPEID_EXPR:
7015 case PSEUDO_DTOR_EXPR:
7016 if (!fn)
7017 return error_mark_node;
7018 break;
7020 default:
7021 break;
7024 /* We didn't find any template parameters we liked. */
7025 return NULL_TREE;
7028 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7029 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7030 call FN with the parameter and the DATA.
7031 If FN returns nonzero, the iteration is terminated, and
7032 for_each_template_parm returns 1. Otherwise, the iteration
7033 continues. If FN never returns a nonzero value, the value
7034 returned by for_each_template_parm is 0. If FN is NULL, it is
7035 considered to be the function which always returns 1.
7037 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7038 parameters that occur in non-deduced contexts. When false, only
7039 visits those template parameters that can be deduced. */
7041 static int
7042 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7043 struct pointer_set_t *visited,
7044 bool include_nondeduced_p)
7046 struct pair_fn_data pfd;
7047 int result;
7049 /* Set up. */
7050 pfd.fn = fn;
7051 pfd.data = data;
7052 pfd.include_nondeduced_p = include_nondeduced_p;
7054 /* Walk the tree. (Conceptually, we would like to walk without
7055 duplicates, but for_each_template_parm_r recursively calls
7056 for_each_template_parm, so we would need to reorganize a fair
7057 bit to use walk_tree_without_duplicates, so we keep our own
7058 visited list.) */
7059 if (visited)
7060 pfd.visited = visited;
7061 else
7062 pfd.visited = pointer_set_create ();
7063 result = cp_walk_tree (&t,
7064 for_each_template_parm_r,
7065 &pfd,
7066 pfd.visited) != NULL_TREE;
7068 /* Clean up. */
7069 if (!visited)
7071 pointer_set_destroy (pfd.visited);
7072 pfd.visited = 0;
7075 return result;
7078 /* Returns true if T depends on any template parameter. */
7081 uses_template_parms (tree t)
7083 bool dependent_p;
7084 int saved_processing_template_decl;
7086 saved_processing_template_decl = processing_template_decl;
7087 if (!saved_processing_template_decl)
7088 processing_template_decl = 1;
7089 if (TYPE_P (t))
7090 dependent_p = dependent_type_p (t);
7091 else if (TREE_CODE (t) == TREE_VEC)
7092 dependent_p = any_dependent_template_arguments_p (t);
7093 else if (TREE_CODE (t) == TREE_LIST)
7094 dependent_p = (uses_template_parms (TREE_VALUE (t))
7095 || uses_template_parms (TREE_CHAIN (t)));
7096 else if (TREE_CODE (t) == TYPE_DECL)
7097 dependent_p = dependent_type_p (TREE_TYPE (t));
7098 else if (DECL_P (t)
7099 || EXPR_P (t)
7100 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7101 || TREE_CODE (t) == OVERLOAD
7102 || TREE_CODE (t) == BASELINK
7103 || TREE_CODE (t) == IDENTIFIER_NODE
7104 || TREE_CODE (t) == TRAIT_EXPR
7105 || TREE_CODE (t) == CONSTRUCTOR
7106 || CONSTANT_CLASS_P (t))
7107 dependent_p = (type_dependent_expression_p (t)
7108 || value_dependent_expression_p (t));
7109 else
7111 gcc_assert (t == error_mark_node);
7112 dependent_p = false;
7115 processing_template_decl = saved_processing_template_decl;
7117 return dependent_p;
7120 /* Returns true if T depends on any template parameter with level LEVEL. */
7123 uses_template_parms_level (tree t, int level)
7125 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7126 /*include_nondeduced_p=*/true);
7129 static int tinst_depth;
7130 extern int max_tinst_depth;
7131 #ifdef GATHER_STATISTICS
7132 int depth_reached;
7133 #endif
7134 static int tinst_level_tick;
7135 static int last_template_error_tick;
7137 /* We're starting to instantiate D; record the template instantiation context
7138 for diagnostics and to restore it later. */
7141 push_tinst_level (tree d)
7143 struct tinst_level *new_level;
7145 if (tinst_depth >= max_tinst_depth)
7147 /* If the instantiation in question still has unbound template parms,
7148 we don't really care if we can't instantiate it, so just return.
7149 This happens with base instantiation for implicit `typename'. */
7150 if (uses_template_parms (d))
7151 return 0;
7153 last_template_error_tick = tinst_level_tick;
7154 error ("template instantiation depth exceeds maximum of %d (use "
7155 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7156 max_tinst_depth, d);
7158 print_instantiation_context ();
7160 return 0;
7163 new_level = ggc_alloc_tinst_level ();
7164 new_level->decl = d;
7165 new_level->locus = input_location;
7166 new_level->in_system_header_p = in_system_header;
7167 new_level->next = current_tinst_level;
7168 current_tinst_level = new_level;
7170 ++tinst_depth;
7171 #ifdef GATHER_STATISTICS
7172 if (tinst_depth > depth_reached)
7173 depth_reached = tinst_depth;
7174 #endif
7176 ++tinst_level_tick;
7177 return 1;
7180 /* We're done instantiating this template; return to the instantiation
7181 context. */
7183 void
7184 pop_tinst_level (void)
7186 /* Restore the filename and line number stashed away when we started
7187 this instantiation. */
7188 input_location = current_tinst_level->locus;
7189 current_tinst_level = current_tinst_level->next;
7190 --tinst_depth;
7191 ++tinst_level_tick;
7194 /* We're instantiating a deferred template; restore the template
7195 instantiation context in which the instantiation was requested, which
7196 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7198 static tree
7199 reopen_tinst_level (struct tinst_level *level)
7201 struct tinst_level *t;
7203 tinst_depth = 0;
7204 for (t = level; t; t = t->next)
7205 ++tinst_depth;
7207 current_tinst_level = level;
7208 pop_tinst_level ();
7209 return level->decl;
7212 /* Returns the TINST_LEVEL which gives the original instantiation
7213 context. */
7215 struct tinst_level *
7216 outermost_tinst_level (void)
7218 struct tinst_level *level = current_tinst_level;
7219 if (level)
7220 while (level->next)
7221 level = level->next;
7222 return level;
7225 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7227 bool
7228 parameter_of_template_p (tree parm, tree templ)
7230 tree parms;
7231 int i;
7233 if (!parm || !templ)
7234 return false;
7236 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7237 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7239 parms = DECL_TEMPLATE_PARMS (templ);
7240 parms = INNERMOST_TEMPLATE_PARMS (parms);
7242 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7243 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7244 return true;
7246 return false;
7249 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7250 vector of template arguments, as for tsubst.
7252 Returns an appropriate tsubst'd friend declaration. */
7254 static tree
7255 tsubst_friend_function (tree decl, tree args)
7257 tree new_friend;
7259 if (TREE_CODE (decl) == FUNCTION_DECL
7260 && DECL_TEMPLATE_INSTANTIATION (decl)
7261 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7262 /* This was a friend declared with an explicit template
7263 argument list, e.g.:
7265 friend void f<>(T);
7267 to indicate that f was a template instantiation, not a new
7268 function declaration. Now, we have to figure out what
7269 instantiation of what template. */
7271 tree template_id, arglist, fns;
7272 tree new_args;
7273 tree tmpl;
7274 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7276 /* Friend functions are looked up in the containing namespace scope.
7277 We must enter that scope, to avoid finding member functions of the
7278 current class with same name. */
7279 push_nested_namespace (ns);
7280 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7281 tf_warning_or_error, NULL_TREE,
7282 /*integral_constant_expression_p=*/false);
7283 pop_nested_namespace (ns);
7284 arglist = tsubst (DECL_TI_ARGS (decl), args,
7285 tf_warning_or_error, NULL_TREE);
7286 template_id = lookup_template_function (fns, arglist);
7288 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7289 tmpl = determine_specialization (template_id, new_friend,
7290 &new_args,
7291 /*need_member_template=*/0,
7292 TREE_VEC_LENGTH (args),
7293 tsk_none);
7294 return instantiate_template (tmpl, new_args, tf_error);
7297 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7299 /* The NEW_FRIEND will look like an instantiation, to the
7300 compiler, but is not an instantiation from the point of view of
7301 the language. For example, we might have had:
7303 template <class T> struct S {
7304 template <class U> friend void f(T, U);
7307 Then, in S<int>, template <class U> void f(int, U) is not an
7308 instantiation of anything. */
7309 if (new_friend == error_mark_node)
7310 return error_mark_node;
7312 DECL_USE_TEMPLATE (new_friend) = 0;
7313 if (TREE_CODE (decl) == TEMPLATE_DECL)
7315 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7316 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7317 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7320 /* The mangled name for the NEW_FRIEND is incorrect. The function
7321 is not a template instantiation and should not be mangled like
7322 one. Therefore, we forget the mangling here; we'll recompute it
7323 later if we need it. */
7324 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7326 SET_DECL_RTL (new_friend, NULL);
7327 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7330 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7332 tree old_decl;
7333 tree new_friend_template_info;
7334 tree new_friend_result_template_info;
7335 tree ns;
7336 int new_friend_is_defn;
7338 /* We must save some information from NEW_FRIEND before calling
7339 duplicate decls since that function will free NEW_FRIEND if
7340 possible. */
7341 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7342 new_friend_is_defn =
7343 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7344 (template_for_substitution (new_friend)))
7345 != NULL_TREE);
7346 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7348 /* This declaration is a `primary' template. */
7349 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7351 new_friend_result_template_info
7352 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7354 else
7355 new_friend_result_template_info = NULL_TREE;
7357 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7358 if (new_friend_is_defn)
7359 DECL_INITIAL (new_friend) = error_mark_node;
7361 /* Inside pushdecl_namespace_level, we will push into the
7362 current namespace. However, the friend function should go
7363 into the namespace of the template. */
7364 ns = decl_namespace_context (new_friend);
7365 push_nested_namespace (ns);
7366 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7367 pop_nested_namespace (ns);
7369 if (old_decl == error_mark_node)
7370 return error_mark_node;
7372 if (old_decl != new_friend)
7374 /* This new friend declaration matched an existing
7375 declaration. For example, given:
7377 template <class T> void f(T);
7378 template <class U> class C {
7379 template <class T> friend void f(T) {}
7382 the friend declaration actually provides the definition
7383 of `f', once C has been instantiated for some type. So,
7384 old_decl will be the out-of-class template declaration,
7385 while new_friend is the in-class definition.
7387 But, if `f' was called before this point, the
7388 instantiation of `f' will have DECL_TI_ARGS corresponding
7389 to `T' but not to `U', references to which might appear
7390 in the definition of `f'. Previously, the most general
7391 template for an instantiation of `f' was the out-of-class
7392 version; now it is the in-class version. Therefore, we
7393 run through all specialization of `f', adding to their
7394 DECL_TI_ARGS appropriately. In particular, they need a
7395 new set of outer arguments, corresponding to the
7396 arguments for this class instantiation.
7398 The same situation can arise with something like this:
7400 friend void f(int);
7401 template <class T> class C {
7402 friend void f(T) {}
7405 when `C<int>' is instantiated. Now, `f(int)' is defined
7406 in the class. */
7408 if (!new_friend_is_defn)
7409 /* On the other hand, if the in-class declaration does
7410 *not* provide a definition, then we don't want to alter
7411 existing definitions. We can just leave everything
7412 alone. */
7414 else
7416 tree new_template = TI_TEMPLATE (new_friend_template_info);
7417 tree new_args = TI_ARGS (new_friend_template_info);
7419 /* Overwrite whatever template info was there before, if
7420 any, with the new template information pertaining to
7421 the declaration. */
7422 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7424 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7426 /* We should have called reregister_specialization in
7427 duplicate_decls. */
7428 gcc_assert (retrieve_specialization (new_template,
7429 new_args, 0)
7430 == old_decl);
7432 /* Instantiate it if the global has already been used. */
7433 if (DECL_ODR_USED (old_decl))
7434 instantiate_decl (old_decl, /*defer_ok=*/true,
7435 /*expl_inst_class_mem_p=*/false);
7437 else
7439 tree t;
7441 /* Indicate that the old function template is a partial
7442 instantiation. */
7443 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7444 = new_friend_result_template_info;
7446 gcc_assert (new_template
7447 == most_general_template (new_template));
7448 gcc_assert (new_template != old_decl);
7450 /* Reassign any specializations already in the hash table
7451 to the new more general template, and add the
7452 additional template args. */
7453 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7454 t != NULL_TREE;
7455 t = TREE_CHAIN (t))
7457 tree spec = TREE_VALUE (t);
7458 spec_entry elt;
7460 elt.tmpl = old_decl;
7461 elt.args = DECL_TI_ARGS (spec);
7462 elt.spec = NULL_TREE;
7464 htab_remove_elt (decl_specializations, &elt);
7466 DECL_TI_ARGS (spec)
7467 = add_outermost_template_args (new_args,
7468 DECL_TI_ARGS (spec));
7470 register_specialization
7471 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7474 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7478 /* The information from NEW_FRIEND has been merged into OLD_DECL
7479 by duplicate_decls. */
7480 new_friend = old_decl;
7483 else
7485 tree context = DECL_CONTEXT (new_friend);
7486 bool dependent_p;
7488 /* In the code
7489 template <class T> class C {
7490 template <class U> friend void C1<U>::f (); // case 1
7491 friend void C2<T>::f (); // case 2
7493 we only need to make sure CONTEXT is a complete type for
7494 case 2. To distinguish between the two cases, we note that
7495 CONTEXT of case 1 remains dependent type after tsubst while
7496 this isn't true for case 2. */
7497 ++processing_template_decl;
7498 dependent_p = dependent_type_p (context);
7499 --processing_template_decl;
7501 if (!dependent_p
7502 && !complete_type_or_else (context, NULL_TREE))
7503 return error_mark_node;
7505 if (COMPLETE_TYPE_P (context))
7507 /* Check to see that the declaration is really present, and,
7508 possibly obtain an improved declaration. */
7509 tree fn = check_classfn (context,
7510 new_friend, NULL_TREE);
7512 if (fn)
7513 new_friend = fn;
7517 return new_friend;
7520 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7521 template arguments, as for tsubst.
7523 Returns an appropriate tsubst'd friend type or error_mark_node on
7524 failure. */
7526 static tree
7527 tsubst_friend_class (tree friend_tmpl, tree args)
7529 tree friend_type;
7530 tree tmpl;
7531 tree context;
7533 context = DECL_CONTEXT (friend_tmpl);
7535 if (context)
7537 if (TREE_CODE (context) == NAMESPACE_DECL)
7538 push_nested_namespace (context);
7539 else
7540 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7543 /* Look for a class template declaration. We look for hidden names
7544 because two friend declarations of the same template are the
7545 same. For example, in:
7547 struct A {
7548 template <typename> friend class F;
7550 template <typename> struct B {
7551 template <typename> friend class F;
7554 both F templates are the same. */
7555 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7556 /*block_p=*/true, 0,
7557 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7559 /* But, if we don't find one, it might be because we're in a
7560 situation like this:
7562 template <class T>
7563 struct S {
7564 template <class U>
7565 friend struct S;
7568 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7569 for `S<int>', not the TEMPLATE_DECL. */
7570 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7572 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7573 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7576 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7578 /* The friend template has already been declared. Just
7579 check to see that the declarations match, and install any new
7580 default parameters. We must tsubst the default parameters,
7581 of course. We only need the innermost template parameters
7582 because that is all that redeclare_class_template will look
7583 at. */
7584 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7585 > TMPL_ARGS_DEPTH (args))
7587 tree parms;
7588 location_t saved_input_location;
7589 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7590 args, tf_warning_or_error);
7592 saved_input_location = input_location;
7593 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7594 redeclare_class_template (TREE_TYPE (tmpl), parms);
7595 input_location = saved_input_location;
7599 friend_type = TREE_TYPE (tmpl);
7601 else
7603 /* The friend template has not already been declared. In this
7604 case, the instantiation of the template class will cause the
7605 injection of this template into the global scope. */
7606 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7607 if (tmpl == error_mark_node)
7608 return error_mark_node;
7610 /* The new TMPL is not an instantiation of anything, so we
7611 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7612 the new type because that is supposed to be the corresponding
7613 template decl, i.e., TMPL. */
7614 DECL_USE_TEMPLATE (tmpl) = 0;
7615 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7616 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7617 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7618 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7620 /* Inject this template into the global scope. */
7621 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7624 if (context)
7626 if (TREE_CODE (context) == NAMESPACE_DECL)
7627 pop_nested_namespace (context);
7628 else
7629 pop_nested_class ();
7632 return friend_type;
7635 /* Returns zero if TYPE cannot be completed later due to circularity.
7636 Otherwise returns one. */
7638 static int
7639 can_complete_type_without_circularity (tree type)
7641 if (type == NULL_TREE || type == error_mark_node)
7642 return 0;
7643 else if (COMPLETE_TYPE_P (type))
7644 return 1;
7645 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7646 return can_complete_type_without_circularity (TREE_TYPE (type));
7647 else if (CLASS_TYPE_P (type)
7648 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7649 return 0;
7650 else
7651 return 1;
7654 /* Apply any attributes which had to be deferred until instantiation
7655 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7656 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7658 static void
7659 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7660 tree args, tsubst_flags_t complain, tree in_decl)
7662 tree last_dep = NULL_TREE;
7663 tree t;
7664 tree *p;
7666 for (t = attributes; t; t = TREE_CHAIN (t))
7667 if (ATTR_IS_DEPENDENT (t))
7669 last_dep = t;
7670 attributes = copy_list (attributes);
7671 break;
7674 if (DECL_P (*decl_p))
7676 if (TREE_TYPE (*decl_p) == error_mark_node)
7677 return;
7678 p = &DECL_ATTRIBUTES (*decl_p);
7680 else
7681 p = &TYPE_ATTRIBUTES (*decl_p);
7683 if (last_dep)
7685 tree late_attrs = NULL_TREE;
7686 tree *q = &late_attrs;
7688 for (*p = attributes; *p; )
7690 t = *p;
7691 if (ATTR_IS_DEPENDENT (t))
7693 *p = TREE_CHAIN (t);
7694 TREE_CHAIN (t) = NULL_TREE;
7695 /* If the first attribute argument is an identifier, don't
7696 pass it through tsubst. Attributes like mode, format,
7697 cleanup and several target specific attributes expect it
7698 unmodified. */
7699 if (TREE_VALUE (t)
7700 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7701 && TREE_VALUE (TREE_VALUE (t))
7702 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7703 == IDENTIFIER_NODE))
7705 tree chain
7706 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7707 in_decl,
7708 /*integral_constant_expression_p=*/false);
7709 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7710 TREE_VALUE (t)
7711 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7712 chain);
7714 else
7715 TREE_VALUE (t)
7716 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7717 /*integral_constant_expression_p=*/false);
7718 *q = t;
7719 q = &TREE_CHAIN (t);
7721 else
7722 p = &TREE_CHAIN (t);
7725 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7729 /* Perform (or defer) access check for typedefs that were referenced
7730 from within the template TMPL code.
7731 This is a subroutine of instantiate_template and instantiate_class_template.
7732 TMPL is the template to consider and TARGS is the list of arguments of
7733 that template. */
7735 static void
7736 perform_typedefs_access_check (tree tmpl, tree targs)
7738 location_t saved_location;
7739 int i;
7740 qualified_typedef_usage_t *iter;
7742 if (!tmpl
7743 || (!CLASS_TYPE_P (tmpl)
7744 && TREE_CODE (tmpl) != FUNCTION_DECL))
7745 return;
7747 saved_location = input_location;
7748 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
7749 get_types_needing_access_check (tmpl),
7750 i, iter)
7752 tree type_decl = iter->typedef_decl;
7753 tree type_scope = iter->context;
7755 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7756 continue;
7758 if (uses_template_parms (type_decl))
7759 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7760 if (uses_template_parms (type_scope))
7761 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7763 /* Make access check error messages point to the location
7764 of the use of the typedef. */
7765 input_location = iter->locus;
7766 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7767 type_decl, type_decl);
7769 input_location = saved_location;
7772 tree
7773 instantiate_class_template (tree type)
7775 tree templ, args, pattern, t, member;
7776 tree typedecl;
7777 tree pbinfo;
7778 tree base_list;
7779 unsigned int saved_maximum_field_alignment;
7781 if (type == error_mark_node)
7782 return error_mark_node;
7784 if (COMPLETE_OR_OPEN_TYPE_P (type)
7785 || uses_template_parms (type))
7786 return type;
7788 /* Figure out which template is being instantiated. */
7789 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7790 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7792 /* Determine what specialization of the original template to
7793 instantiate. */
7794 t = most_specialized_class (type, templ, tf_warning_or_error);
7795 if (t == error_mark_node)
7797 TYPE_BEING_DEFINED (type) = 1;
7798 return error_mark_node;
7800 else if (t)
7802 /* This TYPE is actually an instantiation of a partial
7803 specialization. We replace the innermost set of ARGS with
7804 the arguments appropriate for substitution. For example,
7805 given:
7807 template <class T> struct S {};
7808 template <class T> struct S<T*> {};
7810 and supposing that we are instantiating S<int*>, ARGS will
7811 presently be {int*} -- but we need {int}. */
7812 pattern = TREE_TYPE (t);
7813 args = TREE_PURPOSE (t);
7815 else
7817 pattern = TREE_TYPE (templ);
7818 args = CLASSTYPE_TI_ARGS (type);
7821 /* If the template we're instantiating is incomplete, then clearly
7822 there's nothing we can do. */
7823 if (!COMPLETE_TYPE_P (pattern))
7824 return type;
7826 /* If we've recursively instantiated too many templates, stop. */
7827 if (! push_tinst_level (type))
7828 return type;
7830 /* Now we're really doing the instantiation. Mark the type as in
7831 the process of being defined. */
7832 TYPE_BEING_DEFINED (type) = 1;
7834 /* We may be in the middle of deferred access check. Disable
7835 it now. */
7836 push_deferring_access_checks (dk_no_deferred);
7838 push_to_top_level ();
7839 /* Use #pragma pack from the template context. */
7840 saved_maximum_field_alignment = maximum_field_alignment;
7841 maximum_field_alignment = TYPE_PRECISION (pattern);
7843 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7845 /* Set the input location to the most specialized template definition.
7846 This is needed if tsubsting causes an error. */
7847 typedecl = TYPE_MAIN_DECL (pattern);
7848 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
7849 DECL_SOURCE_LOCATION (typedecl);
7851 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7852 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7853 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7854 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7855 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
7856 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
7857 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
7858 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
7859 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7860 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7861 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7862 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7863 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7864 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7865 if (ANON_AGGR_TYPE_P (pattern))
7866 SET_ANON_AGGR_TYPE_P (type);
7867 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7869 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7870 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7873 pbinfo = TYPE_BINFO (pattern);
7875 /* We should never instantiate a nested class before its enclosing
7876 class; we need to look up the nested class by name before we can
7877 instantiate it, and that lookup should instantiate the enclosing
7878 class. */
7879 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7880 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
7882 base_list = NULL_TREE;
7883 if (BINFO_N_BASE_BINFOS (pbinfo))
7885 tree pbase_binfo;
7886 tree context = TYPE_CONTEXT (type);
7887 tree pushed_scope;
7888 int i;
7890 /* We must enter the scope containing the type, as that is where
7891 the accessibility of types named in dependent bases are
7892 looked up from. */
7893 pushed_scope = push_scope (context ? context : global_namespace);
7895 /* Substitute into each of the bases to determine the actual
7896 basetypes. */
7897 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7899 tree base;
7900 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7901 tree expanded_bases = NULL_TREE;
7902 int idx, len = 1;
7904 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7906 expanded_bases =
7907 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7908 args, tf_error, NULL_TREE);
7909 if (expanded_bases == error_mark_node)
7910 continue;
7912 len = TREE_VEC_LENGTH (expanded_bases);
7915 for (idx = 0; idx < len; idx++)
7917 if (expanded_bases)
7918 /* Extract the already-expanded base class. */
7919 base = TREE_VEC_ELT (expanded_bases, idx);
7920 else
7921 /* Substitute to figure out the base class. */
7922 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7923 NULL_TREE);
7925 if (base == error_mark_node)
7926 continue;
7928 base_list = tree_cons (access, base, base_list);
7929 if (BINFO_VIRTUAL_P (pbase_binfo))
7930 TREE_TYPE (base_list) = integer_type_node;
7934 /* The list is now in reverse order; correct that. */
7935 base_list = nreverse (base_list);
7937 if (pushed_scope)
7938 pop_scope (pushed_scope);
7940 /* Now call xref_basetypes to set up all the base-class
7941 information. */
7942 xref_basetypes (type, base_list);
7944 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7945 (int) ATTR_FLAG_TYPE_IN_PLACE,
7946 args, tf_error, NULL_TREE);
7948 /* Now that our base classes are set up, enter the scope of the
7949 class, so that name lookups into base classes, etc. will work
7950 correctly. This is precisely analogous to what we do in
7951 begin_class_definition when defining an ordinary non-template
7952 class, except we also need to push the enclosing classes. */
7953 push_nested_class (type);
7955 /* Now members are processed in the order of declaration. */
7956 for (member = CLASSTYPE_DECL_LIST (pattern);
7957 member; member = TREE_CHAIN (member))
7959 tree t = TREE_VALUE (member);
7961 if (TREE_PURPOSE (member))
7963 if (TYPE_P (t))
7965 /* Build new CLASSTYPE_NESTED_UTDS. */
7967 tree newtag;
7968 bool class_template_p;
7970 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7971 && TYPE_LANG_SPECIFIC (t)
7972 && CLASSTYPE_IS_TEMPLATE (t));
7973 /* If the member is a class template, then -- even after
7974 substitution -- there may be dependent types in the
7975 template argument list for the class. We increment
7976 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7977 that function will assume that no types are dependent
7978 when outside of a template. */
7979 if (class_template_p)
7980 ++processing_template_decl;
7981 newtag = tsubst (t, args, tf_error, NULL_TREE);
7982 if (class_template_p)
7983 --processing_template_decl;
7984 if (newtag == error_mark_node)
7985 continue;
7987 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7989 tree name = TYPE_IDENTIFIER (t);
7991 if (class_template_p)
7992 /* Unfortunately, lookup_template_class sets
7993 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7994 instantiation (i.e., for the type of a member
7995 template class nested within a template class.)
7996 This behavior is required for
7997 maybe_process_partial_specialization to work
7998 correctly, but is not accurate in this case;
7999 the TAG is not an instantiation of anything.
8000 (The corresponding TEMPLATE_DECL is an
8001 instantiation, but the TYPE is not.) */
8002 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8004 /* Now, we call pushtag to put this NEWTAG into the scope of
8005 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8006 pushtag calling push_template_decl. We don't have to do
8007 this for enums because it will already have been done in
8008 tsubst_enum. */
8009 if (name)
8010 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8011 pushtag (name, newtag, /*tag_scope=*/ts_current);
8014 else if (TREE_CODE (t) == FUNCTION_DECL
8015 || DECL_FUNCTION_TEMPLATE_P (t))
8017 /* Build new TYPE_METHODS. */
8018 tree r;
8020 if (TREE_CODE (t) == TEMPLATE_DECL)
8021 ++processing_template_decl;
8022 r = tsubst (t, args, tf_error, NULL_TREE);
8023 if (TREE_CODE (t) == TEMPLATE_DECL)
8024 --processing_template_decl;
8025 set_current_access_from_decl (r);
8026 finish_member_declaration (r);
8028 else
8030 /* Build new TYPE_FIELDS. */
8031 if (TREE_CODE (t) == STATIC_ASSERT)
8033 tree condition =
8034 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8035 tf_warning_or_error, NULL_TREE,
8036 /*integral_constant_expression_p=*/true);
8037 finish_static_assert (condition,
8038 STATIC_ASSERT_MESSAGE (t),
8039 STATIC_ASSERT_SOURCE_LOCATION (t),
8040 /*member_p=*/true);
8042 else if (TREE_CODE (t) != CONST_DECL)
8044 tree r;
8046 /* The file and line for this declaration, to
8047 assist in error message reporting. Since we
8048 called push_tinst_level above, we don't need to
8049 restore these. */
8050 input_location = DECL_SOURCE_LOCATION (t);
8052 if (TREE_CODE (t) == TEMPLATE_DECL)
8053 ++processing_template_decl;
8054 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8055 if (TREE_CODE (t) == TEMPLATE_DECL)
8056 --processing_template_decl;
8057 if (TREE_CODE (r) == VAR_DECL)
8059 /* In [temp.inst]:
8061 [t]he initialization (and any associated
8062 side-effects) of a static data member does
8063 not occur unless the static data member is
8064 itself used in a way that requires the
8065 definition of the static data member to
8066 exist.
8068 Therefore, we do not substitute into the
8069 initialized for the static data member here. */
8070 finish_static_data_member_decl
8072 /*init=*/NULL_TREE,
8073 /*init_const_expr_p=*/false,
8074 /*asmspec_tree=*/NULL_TREE,
8075 /*flags=*/0);
8076 if (DECL_INITIALIZED_IN_CLASS_P (r))
8077 check_static_variable_definition (r, TREE_TYPE (r));
8079 else if (TREE_CODE (r) == FIELD_DECL)
8081 /* Determine whether R has a valid type and can be
8082 completed later. If R is invalid, then it is
8083 replaced by error_mark_node so that it will not be
8084 added to TYPE_FIELDS. */
8085 tree rtype = TREE_TYPE (r);
8086 if (can_complete_type_without_circularity (rtype))
8087 complete_type (rtype);
8089 if (!COMPLETE_TYPE_P (rtype))
8091 cxx_incomplete_type_error (r, rtype);
8092 r = error_mark_node;
8096 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8097 such a thing will already have been added to the field
8098 list by tsubst_enum in finish_member_declaration in the
8099 CLASSTYPE_NESTED_UTDS case above. */
8100 if (!(TREE_CODE (r) == TYPE_DECL
8101 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8102 && DECL_ARTIFICIAL (r)))
8104 set_current_access_from_decl (r);
8105 finish_member_declaration (r);
8110 else
8112 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8114 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8116 tree friend_type = t;
8117 bool adjust_processing_template_decl = false;
8119 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8121 /* template <class T> friend class C; */
8122 friend_type = tsubst_friend_class (friend_type, args);
8123 adjust_processing_template_decl = true;
8125 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8127 /* template <class T> friend class C::D; */
8128 friend_type = tsubst (friend_type, args,
8129 tf_warning_or_error, NULL_TREE);
8130 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8131 friend_type = TREE_TYPE (friend_type);
8132 adjust_processing_template_decl = true;
8134 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8136 /* This could be either
8138 friend class T::C;
8140 when dependent_type_p is false or
8142 template <class U> friend class T::C;
8144 otherwise. */
8145 friend_type = tsubst (friend_type, args,
8146 tf_warning_or_error, NULL_TREE);
8147 /* Bump processing_template_decl for correct
8148 dependent_type_p calculation. */
8149 ++processing_template_decl;
8150 if (dependent_type_p (friend_type))
8151 adjust_processing_template_decl = true;
8152 --processing_template_decl;
8154 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8155 && hidden_name_p (TYPE_NAME (friend_type)))
8157 /* friend class C;
8159 where C hasn't been declared yet. Let's lookup name
8160 from namespace scope directly, bypassing any name that
8161 come from dependent base class. */
8162 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8164 /* The call to xref_tag_from_type does injection for friend
8165 classes. */
8166 push_nested_namespace (ns);
8167 friend_type =
8168 xref_tag_from_type (friend_type, NULL_TREE,
8169 /*tag_scope=*/ts_current);
8170 pop_nested_namespace (ns);
8172 else if (uses_template_parms (friend_type))
8173 /* friend class C<T>; */
8174 friend_type = tsubst (friend_type, args,
8175 tf_warning_or_error, NULL_TREE);
8176 /* Otherwise it's
8178 friend class C;
8180 where C is already declared or
8182 friend class C<int>;
8184 We don't have to do anything in these cases. */
8186 if (adjust_processing_template_decl)
8187 /* Trick make_friend_class into realizing that the friend
8188 we're adding is a template, not an ordinary class. It's
8189 important that we use make_friend_class since it will
8190 perform some error-checking and output cross-reference
8191 information. */
8192 ++processing_template_decl;
8194 if (friend_type != error_mark_node)
8195 make_friend_class (type, friend_type, /*complain=*/false);
8197 if (adjust_processing_template_decl)
8198 --processing_template_decl;
8200 else
8202 /* Build new DECL_FRIENDLIST. */
8203 tree r;
8205 /* The file and line for this declaration, to
8206 assist in error message reporting. Since we
8207 called push_tinst_level above, we don't need to
8208 restore these. */
8209 input_location = DECL_SOURCE_LOCATION (t);
8211 if (TREE_CODE (t) == TEMPLATE_DECL)
8213 ++processing_template_decl;
8214 push_deferring_access_checks (dk_no_check);
8217 r = tsubst_friend_function (t, args);
8218 add_friend (type, r, /*complain=*/false);
8219 if (TREE_CODE (t) == TEMPLATE_DECL)
8221 pop_deferring_access_checks ();
8222 --processing_template_decl;
8228 /* Set the file and line number information to whatever is given for
8229 the class itself. This puts error messages involving generated
8230 implicit functions at a predictable point, and the same point
8231 that would be used for non-template classes. */
8232 input_location = DECL_SOURCE_LOCATION (typedecl);
8234 unreverse_member_declarations (type);
8235 finish_struct_1 (type);
8236 TYPE_BEING_DEFINED (type) = 0;
8238 /* Now that the class is complete, instantiate default arguments for
8239 any member functions. We don't do this earlier because the
8240 default arguments may reference members of the class. */
8241 if (!PRIMARY_TEMPLATE_P (templ))
8242 for (t = TYPE_METHODS (type); t; t = DECL_CHAIN (t))
8243 if (TREE_CODE (t) == FUNCTION_DECL
8244 /* Implicitly generated member functions will not have template
8245 information; they are not instantiations, but instead are
8246 created "fresh" for each instantiation. */
8247 && DECL_TEMPLATE_INFO (t))
8248 tsubst_default_arguments (t);
8250 /* Some typedefs referenced from within the template code need to be access
8251 checked at template instantiation time, i.e now. These types were
8252 added to the template at parsing time. Let's get those and perform
8253 the access checks then. */
8254 perform_typedefs_access_check (pattern, args);
8255 perform_deferred_access_checks ();
8256 pop_nested_class ();
8257 maximum_field_alignment = saved_maximum_field_alignment;
8258 pop_from_top_level ();
8259 pop_deferring_access_checks ();
8260 pop_tinst_level ();
8262 /* The vtable for a template class can be emitted in any translation
8263 unit in which the class is instantiated. When there is no key
8264 method, however, finish_struct_1 will already have added TYPE to
8265 the keyed_classes list. */
8266 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8267 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8269 return type;
8272 static tree
8273 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8275 tree r;
8277 if (!t)
8278 r = t;
8279 else if (TYPE_P (t))
8280 r = tsubst (t, args, complain, in_decl);
8281 else
8283 if (!(complain & tf_warning))
8284 ++c_inhibit_evaluation_warnings;
8285 r = tsubst_expr (t, args, complain, in_decl,
8286 /*integral_constant_expression_p=*/true);
8287 if (!(complain & tf_warning))
8288 --c_inhibit_evaluation_warnings;
8290 return r;
8293 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8294 NONTYPE_ARGUMENT_PACK. */
8296 static tree
8297 make_fnparm_pack (tree spec_parm)
8299 /* Collect all of the extra "packed" parameters into an
8300 argument pack. */
8301 tree parmvec;
8302 tree parmtypevec;
8303 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8304 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8305 int i, len = list_length (spec_parm);
8307 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8308 parmvec = make_tree_vec (len);
8309 parmtypevec = make_tree_vec (len);
8310 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8312 TREE_VEC_ELT (parmvec, i) = spec_parm;
8313 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8316 /* Build the argument packs. */
8317 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8318 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8319 TREE_TYPE (argpack) = argtypepack;
8321 return argpack;
8324 /* Substitute ARGS into T, which is an pack expansion
8325 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8326 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8327 (if only a partial substitution could be performed) or
8328 ERROR_MARK_NODE if there was an error. */
8329 tree
8330 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8331 tree in_decl)
8333 tree pattern;
8334 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8335 int i, len = -1;
8336 tree result;
8337 int incomplete = 0;
8338 htab_t saved_local_specializations = NULL;
8340 gcc_assert (PACK_EXPANSION_P (t));
8341 pattern = PACK_EXPANSION_PATTERN (t);
8343 /* Determine the argument packs that will instantiate the parameter
8344 packs used in the expansion expression. While we're at it,
8345 compute the number of arguments to be expanded and make sure it
8346 is consistent. */
8347 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8348 pack = TREE_CHAIN (pack))
8350 tree parm_pack = TREE_VALUE (pack);
8351 tree arg_pack = NULL_TREE;
8352 tree orig_arg = NULL_TREE;
8354 if (TREE_CODE (parm_pack) == PARM_DECL)
8356 if (!cp_unevaluated_operand)
8357 arg_pack = retrieve_local_specialization (parm_pack);
8358 else
8360 /* We can't rely on local_specializations for a parameter
8361 name used later in a function declaration (such as in a
8362 late-specified return type). Even if it exists, it might
8363 have the wrong value for a recursive call. Just make a
8364 dummy decl, since it's only used for its type. */
8365 arg_pack = tsubst_decl (parm_pack, args, complain);
8366 arg_pack = make_fnparm_pack (arg_pack);
8369 else
8371 int level, idx, levels;
8372 template_parm_level_and_index (parm_pack, &level, &idx);
8374 levels = TMPL_ARGS_DEPTH (args);
8375 if (level <= levels)
8376 arg_pack = TMPL_ARG (args, level, idx);
8379 orig_arg = arg_pack;
8380 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8381 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8383 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8384 /* This can only happen if we forget to expand an argument
8385 pack somewhere else. Just return an error, silently. */
8387 result = make_tree_vec (1);
8388 TREE_VEC_ELT (result, 0) = error_mark_node;
8389 return result;
8392 if (arg_pack
8393 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8394 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8396 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8397 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8398 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8399 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8400 /* The argument pack that the parameter maps to is just an
8401 expansion of the parameter itself, such as one would
8402 find in the implicit typedef of a class inside the
8403 class itself. Consider this parameter "unsubstituted",
8404 so that we will maintain the outer pack expansion. */
8405 arg_pack = NULL_TREE;
8408 if (arg_pack)
8410 int my_len =
8411 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8413 /* It's all-or-nothing with incomplete argument packs. */
8414 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8415 return error_mark_node;
8417 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8418 incomplete = 1;
8420 if (len < 0)
8421 len = my_len;
8422 else if (len != my_len)
8424 if (incomplete)
8425 /* We got explicit args for some packs but not others;
8426 do nothing now and try again after deduction. */
8427 return t;
8428 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8429 error ("mismatched argument pack lengths while expanding "
8430 "%<%T%>",
8431 pattern);
8432 else
8433 error ("mismatched argument pack lengths while expanding "
8434 "%<%E%>",
8435 pattern);
8436 return error_mark_node;
8439 /* Keep track of the parameter packs and their corresponding
8440 argument packs. */
8441 packs = tree_cons (parm_pack, arg_pack, packs);
8442 TREE_TYPE (packs) = orig_arg;
8444 else
8445 /* We can't substitute for this parameter pack. */
8446 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8447 TREE_VALUE (pack),
8448 unsubstituted_packs);
8451 /* We cannot expand this expansion expression, because we don't have
8452 all of the argument packs we need. Substitute into the pattern
8453 and return a PACK_EXPANSION_*. The caller will need to deal with
8454 that. */
8455 if (unsubstituted_packs)
8457 tree new_pat;
8458 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8459 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8460 /*integral_constant_expression_p=*/false);
8461 else
8462 new_pat = tsubst (pattern, args, complain, in_decl);
8463 return make_pack_expansion (new_pat);
8466 /* We could not find any argument packs that work. */
8467 if (len < 0)
8468 return error_mark_node;
8470 if (cp_unevaluated_operand)
8472 /* We're in a late-specified return type, so create our own local
8473 specializations table; the current table is either NULL or (in the
8474 case of recursive unification) might have bindings that we don't
8475 want to use or alter. */
8476 saved_local_specializations = local_specializations;
8477 local_specializations = htab_create (37,
8478 hash_local_specialization,
8479 eq_local_specializations,
8480 NULL);
8483 /* For each argument in each argument pack, substitute into the
8484 pattern. */
8485 result = make_tree_vec (len + incomplete);
8486 for (i = 0; i < len + incomplete; ++i)
8488 /* For parameter pack, change the substitution of the parameter
8489 pack to the ith argument in its argument pack, then expand
8490 the pattern. */
8491 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8493 tree parm = TREE_PURPOSE (pack);
8495 if (TREE_CODE (parm) == PARM_DECL)
8497 /* Select the Ith argument from the pack. */
8498 tree arg = make_node (ARGUMENT_PACK_SELECT);
8499 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8500 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8501 mark_used (parm);
8502 register_local_specialization (arg, parm);
8504 else
8506 tree value = parm;
8507 int idx, level;
8508 template_parm_level_and_index (parm, &level, &idx);
8510 if (i < len)
8512 /* Select the Ith argument from the pack. */
8513 value = make_node (ARGUMENT_PACK_SELECT);
8514 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8515 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8518 /* Update the corresponding argument. */
8519 TMPL_ARG (args, level, idx) = value;
8523 /* Substitute into the PATTERN with the altered arguments. */
8524 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8525 TREE_VEC_ELT (result, i) =
8526 tsubst_expr (pattern, args, complain, in_decl,
8527 /*integral_constant_expression_p=*/false);
8528 else
8529 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8531 if (i == len)
8532 /* When we have incomplete argument packs, the last "expanded"
8533 result is itself a pack expansion, which allows us
8534 to deduce more arguments. */
8535 TREE_VEC_ELT (result, i) =
8536 make_pack_expansion (TREE_VEC_ELT (result, i));
8538 if (TREE_VEC_ELT (result, i) == error_mark_node)
8540 result = error_mark_node;
8541 break;
8545 /* Update ARGS to restore the substitution from parameter packs to
8546 their argument packs. */
8547 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8549 tree parm = TREE_PURPOSE (pack);
8551 if (TREE_CODE (parm) == PARM_DECL)
8552 register_local_specialization (TREE_TYPE (pack), parm);
8553 else
8555 int idx, level;
8556 template_parm_level_and_index (parm, &level, &idx);
8558 /* Update the corresponding argument. */
8559 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8560 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8561 TREE_TYPE (pack);
8562 else
8563 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8567 if (saved_local_specializations)
8569 htab_delete (local_specializations);
8570 local_specializations = saved_local_specializations;
8573 return result;
8576 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8577 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8578 parameter packs; all parms generated from a function parameter pack will
8579 have the same DECL_PARM_INDEX. */
8581 tree
8582 get_pattern_parm (tree parm, tree tmpl)
8584 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8585 tree patparm;
8587 if (DECL_ARTIFICIAL (parm))
8589 for (patparm = DECL_ARGUMENTS (pattern);
8590 patparm; patparm = DECL_CHAIN (patparm))
8591 if (DECL_ARTIFICIAL (patparm)
8592 && DECL_NAME (parm) == DECL_NAME (patparm))
8593 break;
8595 else
8597 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8598 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8599 gcc_assert (DECL_PARM_INDEX (patparm)
8600 == DECL_PARM_INDEX (parm));
8603 return patparm;
8606 /* Substitute ARGS into the vector or list of template arguments T. */
8608 static tree
8609 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8611 tree orig_t = t;
8612 int len = TREE_VEC_LENGTH (t);
8613 int need_new = 0, i, expanded_len_adjust = 0, out;
8614 tree *elts = XALLOCAVEC (tree, len);
8616 for (i = 0; i < len; i++)
8618 tree orig_arg = TREE_VEC_ELT (t, i);
8619 tree new_arg;
8621 if (TREE_CODE (orig_arg) == TREE_VEC)
8622 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8623 else if (PACK_EXPANSION_P (orig_arg))
8625 /* Substitute into an expansion expression. */
8626 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8628 if (TREE_CODE (new_arg) == TREE_VEC)
8629 /* Add to the expanded length adjustment the number of
8630 expanded arguments. We subtract one from this
8631 measurement, because the argument pack expression
8632 itself is already counted as 1 in
8633 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8634 the argument pack is empty. */
8635 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8637 else if (ARGUMENT_PACK_P (orig_arg))
8639 /* Substitute into each of the arguments. */
8640 new_arg = TYPE_P (orig_arg)
8641 ? cxx_make_type (TREE_CODE (orig_arg))
8642 : make_node (TREE_CODE (orig_arg));
8644 SET_ARGUMENT_PACK_ARGS (
8645 new_arg,
8646 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8647 args, complain, in_decl));
8649 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8650 new_arg = error_mark_node;
8652 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8653 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8654 complain, in_decl);
8655 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8657 if (TREE_TYPE (new_arg) == error_mark_node)
8658 new_arg = error_mark_node;
8661 else
8662 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8664 if (new_arg == error_mark_node)
8665 return error_mark_node;
8667 elts[i] = new_arg;
8668 if (new_arg != orig_arg)
8669 need_new = 1;
8672 if (!need_new)
8673 return t;
8675 /* Make space for the expanded arguments coming from template
8676 argument packs. */
8677 t = make_tree_vec (len + expanded_len_adjust);
8678 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8679 arguments for a member template.
8680 In that case each TREE_VEC in ORIG_T represents a level of template
8681 arguments, and ORIG_T won't carry any non defaulted argument count.
8682 It will rather be the nested TREE_VECs that will carry one.
8683 In other words, ORIG_T carries a non defaulted argument count only
8684 if it doesn't contain any nested TREE_VEC. */
8685 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
8687 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
8688 count += expanded_len_adjust;
8689 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
8691 for (i = 0, out = 0; i < len; i++)
8693 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8694 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8695 && TREE_CODE (elts[i]) == TREE_VEC)
8697 int idx;
8699 /* Now expand the template argument pack "in place". */
8700 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8701 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8703 else
8705 TREE_VEC_ELT (t, out) = elts[i];
8706 out++;
8710 return t;
8713 /* Return the result of substituting ARGS into the template parameters
8714 given by PARMS. If there are m levels of ARGS and m + n levels of
8715 PARMS, then the result will contain n levels of PARMS. For
8716 example, if PARMS is `template <class T> template <class U>
8717 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8718 result will be `template <int*, double, class V>'. */
8720 static tree
8721 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8723 tree r = NULL_TREE;
8724 tree* new_parms;
8726 /* When substituting into a template, we must set
8727 PROCESSING_TEMPLATE_DECL as the template parameters may be
8728 dependent if they are based on one-another, and the dependency
8729 predicates are short-circuit outside of templates. */
8730 ++processing_template_decl;
8732 for (new_parms = &r;
8733 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8734 new_parms = &(TREE_CHAIN (*new_parms)),
8735 parms = TREE_CHAIN (parms))
8737 tree new_vec =
8738 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8739 int i;
8741 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8743 tree tuple;
8744 tree default_value;
8745 tree parm_decl;
8747 if (parms == error_mark_node)
8748 continue;
8750 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8752 if (tuple == error_mark_node)
8753 continue;
8755 default_value = TREE_PURPOSE (tuple);
8756 parm_decl = TREE_VALUE (tuple);
8758 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8759 if (TREE_CODE (parm_decl) == PARM_DECL
8760 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8761 parm_decl = error_mark_node;
8762 default_value = tsubst_template_arg (default_value, args,
8763 complain, NULL_TREE);
8765 tuple = build_tree_list (default_value, parm_decl);
8766 TREE_VEC_ELT (new_vec, i) = tuple;
8769 *new_parms =
8770 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8771 - TMPL_ARGS_DEPTH (args)),
8772 new_vec, NULL_TREE);
8775 --processing_template_decl;
8777 return r;
8780 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8781 type T. If T is not an aggregate or enumeration type, it is
8782 handled as if by tsubst. IN_DECL is as for tsubst. If
8783 ENTERING_SCOPE is nonzero, T is the context for a template which
8784 we are presently tsubst'ing. Return the substituted value. */
8786 static tree
8787 tsubst_aggr_type (tree t,
8788 tree args,
8789 tsubst_flags_t complain,
8790 tree in_decl,
8791 int entering_scope)
8793 if (t == NULL_TREE)
8794 return NULL_TREE;
8796 switch (TREE_CODE (t))
8798 case RECORD_TYPE:
8799 if (TYPE_PTRMEMFUNC_P (t))
8800 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8802 /* Else fall through. */
8803 case ENUMERAL_TYPE:
8804 case UNION_TYPE:
8805 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8807 tree argvec;
8808 tree context;
8809 tree r;
8810 int saved_unevaluated_operand;
8811 int saved_inhibit_evaluation_warnings;
8813 /* In "sizeof(X<I>)" we need to evaluate "I". */
8814 saved_unevaluated_operand = cp_unevaluated_operand;
8815 cp_unevaluated_operand = 0;
8816 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8817 c_inhibit_evaluation_warnings = 0;
8819 /* First, determine the context for the type we are looking
8820 up. */
8821 context = TYPE_CONTEXT (t);
8822 if (context)
8824 context = tsubst_aggr_type (context, args, complain,
8825 in_decl, /*entering_scope=*/1);
8826 /* If context is a nested class inside a class template,
8827 it may still need to be instantiated (c++/33959). */
8828 if (TYPE_P (context))
8829 context = complete_type (context);
8832 /* Then, figure out what arguments are appropriate for the
8833 type we are trying to find. For example, given:
8835 template <class T> struct S;
8836 template <class T, class U> void f(T, U) { S<U> su; }
8838 and supposing that we are instantiating f<int, double>,
8839 then our ARGS will be {int, double}, but, when looking up
8840 S we only want {double}. */
8841 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8842 complain, in_decl);
8843 if (argvec == error_mark_node)
8844 r = error_mark_node;
8845 else
8847 r = lookup_template_class (t, argvec, in_decl, context,
8848 entering_scope, complain);
8849 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
8852 cp_unevaluated_operand = saved_unevaluated_operand;
8853 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8855 return r;
8857 else
8858 /* This is not a template type, so there's nothing to do. */
8859 return t;
8861 default:
8862 return tsubst (t, args, complain, in_decl);
8866 /* Substitute into the default argument ARG (a default argument for
8867 FN), which has the indicated TYPE. */
8869 tree
8870 tsubst_default_argument (tree fn, tree type, tree arg)
8872 tree saved_class_ptr = NULL_TREE;
8873 tree saved_class_ref = NULL_TREE;
8875 /* This default argument came from a template. Instantiate the
8876 default argument here, not in tsubst. In the case of
8877 something like:
8879 template <class T>
8880 struct S {
8881 static T t();
8882 void f(T = t());
8885 we must be careful to do name lookup in the scope of S<T>,
8886 rather than in the current class. */
8887 push_access_scope (fn);
8888 /* The "this" pointer is not valid in a default argument. */
8889 if (cfun)
8891 saved_class_ptr = current_class_ptr;
8892 cp_function_chain->x_current_class_ptr = NULL_TREE;
8893 saved_class_ref = current_class_ref;
8894 cp_function_chain->x_current_class_ref = NULL_TREE;
8897 push_deferring_access_checks(dk_no_deferred);
8898 /* The default argument expression may cause implicitly defined
8899 member functions to be synthesized, which will result in garbage
8900 collection. We must treat this situation as if we were within
8901 the body of function so as to avoid collecting live data on the
8902 stack. */
8903 ++function_depth;
8904 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8905 tf_warning_or_error, NULL_TREE,
8906 /*integral_constant_expression_p=*/false);
8907 --function_depth;
8908 pop_deferring_access_checks();
8910 /* Restore the "this" pointer. */
8911 if (cfun)
8913 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8914 cp_function_chain->x_current_class_ref = saved_class_ref;
8917 /* Make sure the default argument is reasonable. */
8918 arg = check_default_argument (type, arg);
8920 pop_access_scope (fn);
8922 return arg;
8925 /* Substitute into all the default arguments for FN. */
8927 static void
8928 tsubst_default_arguments (tree fn)
8930 tree arg;
8931 tree tmpl_args;
8933 tmpl_args = DECL_TI_ARGS (fn);
8935 /* If this function is not yet instantiated, we certainly don't need
8936 its default arguments. */
8937 if (uses_template_parms (tmpl_args))
8938 return;
8940 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8941 arg;
8942 arg = TREE_CHAIN (arg))
8943 if (TREE_PURPOSE (arg))
8944 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8945 TREE_VALUE (arg),
8946 TREE_PURPOSE (arg));
8949 /* Substitute the ARGS into the T, which is a _DECL. Return the
8950 result of the substitution. Issue error and warning messages under
8951 control of COMPLAIN. */
8953 static tree
8954 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8956 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8957 location_t saved_loc;
8958 tree r = NULL_TREE;
8959 tree in_decl = t;
8960 hashval_t hash = 0;
8962 /* Set the filename and linenumber to improve error-reporting. */
8963 saved_loc = input_location;
8964 input_location = DECL_SOURCE_LOCATION (t);
8966 switch (TREE_CODE (t))
8968 case TEMPLATE_DECL:
8970 /* We can get here when processing a member function template,
8971 member class template, or template template parameter. */
8972 tree decl = DECL_TEMPLATE_RESULT (t);
8973 tree spec;
8974 tree tmpl_args;
8975 tree full_args;
8977 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8979 /* Template template parameter is treated here. */
8980 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8981 if (new_type == error_mark_node)
8982 RETURN (error_mark_node);
8984 r = copy_decl (t);
8985 DECL_CHAIN (r) = NULL_TREE;
8986 TREE_TYPE (r) = new_type;
8987 DECL_TEMPLATE_RESULT (r)
8988 = build_decl (DECL_SOURCE_LOCATION (decl),
8989 TYPE_DECL, DECL_NAME (decl), new_type);
8990 DECL_TEMPLATE_PARMS (r)
8991 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8992 complain);
8993 TYPE_NAME (new_type) = r;
8994 break;
8997 /* We might already have an instance of this template.
8998 The ARGS are for the surrounding class type, so the
8999 full args contain the tsubst'd args for the context,
9000 plus the innermost args from the template decl. */
9001 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9002 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9003 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9004 /* Because this is a template, the arguments will still be
9005 dependent, even after substitution. If
9006 PROCESSING_TEMPLATE_DECL is not set, the dependency
9007 predicates will short-circuit. */
9008 ++processing_template_decl;
9009 full_args = tsubst_template_args (tmpl_args, args,
9010 complain, in_decl);
9011 --processing_template_decl;
9012 if (full_args == error_mark_node)
9013 RETURN (error_mark_node);
9015 /* If this is a default template template argument,
9016 tsubst might not have changed anything. */
9017 if (full_args == tmpl_args)
9018 RETURN (t);
9020 hash = hash_tmpl_and_args (t, full_args);
9021 spec = retrieve_specialization (t, full_args, hash);
9022 if (spec != NULL_TREE)
9024 r = spec;
9025 break;
9028 /* Make a new template decl. It will be similar to the
9029 original, but will record the current template arguments.
9030 We also create a new function declaration, which is just
9031 like the old one, but points to this new template, rather
9032 than the old one. */
9033 r = copy_decl (t);
9034 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9035 DECL_CHAIN (r) = NULL_TREE;
9037 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9039 if (TREE_CODE (decl) == TYPE_DECL)
9041 tree new_type;
9042 ++processing_template_decl;
9043 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9044 --processing_template_decl;
9045 if (new_type == error_mark_node)
9046 RETURN (error_mark_node);
9048 TREE_TYPE (r) = new_type;
9049 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9050 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9051 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9052 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9054 else
9056 tree new_decl;
9057 ++processing_template_decl;
9058 new_decl = tsubst (decl, args, complain, in_decl);
9059 --processing_template_decl;
9060 if (new_decl == error_mark_node)
9061 RETURN (error_mark_node);
9063 DECL_TEMPLATE_RESULT (r) = new_decl;
9064 DECL_TI_TEMPLATE (new_decl) = r;
9065 TREE_TYPE (r) = TREE_TYPE (new_decl);
9066 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9067 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9070 SET_DECL_IMPLICIT_INSTANTIATION (r);
9071 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9072 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9074 /* The template parameters for this new template are all the
9075 template parameters for the old template, except the
9076 outermost level of parameters. */
9077 DECL_TEMPLATE_PARMS (r)
9078 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9079 complain);
9081 if (PRIMARY_TEMPLATE_P (t))
9082 DECL_PRIMARY_TEMPLATE (r) = r;
9084 if (TREE_CODE (decl) != TYPE_DECL)
9085 /* Record this non-type partial instantiation. */
9086 register_specialization (r, t,
9087 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9088 false, hash);
9090 break;
9092 case FUNCTION_DECL:
9094 tree ctx;
9095 tree argvec = NULL_TREE;
9096 tree *friends;
9097 tree gen_tmpl;
9098 tree type;
9099 int member;
9100 int args_depth;
9101 int parms_depth;
9103 /* Nobody should be tsubst'ing into non-template functions. */
9104 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9106 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9108 tree spec;
9109 bool dependent_p;
9111 /* If T is not dependent, just return it. We have to
9112 increment PROCESSING_TEMPLATE_DECL because
9113 value_dependent_expression_p assumes that nothing is
9114 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9115 ++processing_template_decl;
9116 dependent_p = value_dependent_expression_p (t);
9117 --processing_template_decl;
9118 if (!dependent_p)
9119 RETURN (t);
9121 /* Calculate the most general template of which R is a
9122 specialization, and the complete set of arguments used to
9123 specialize R. */
9124 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9125 argvec = tsubst_template_args (DECL_TI_ARGS
9126 (DECL_TEMPLATE_RESULT
9127 (DECL_TI_TEMPLATE (t))),
9128 args, complain, in_decl);
9130 /* Check to see if we already have this specialization. */
9131 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9132 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9134 if (spec)
9136 r = spec;
9137 break;
9140 /* We can see more levels of arguments than parameters if
9141 there was a specialization of a member template, like
9142 this:
9144 template <class T> struct S { template <class U> void f(); }
9145 template <> template <class U> void S<int>::f(U);
9147 Here, we'll be substituting into the specialization,
9148 because that's where we can find the code we actually
9149 want to generate, but we'll have enough arguments for
9150 the most general template.
9152 We also deal with the peculiar case:
9154 template <class T> struct S {
9155 template <class U> friend void f();
9157 template <class U> void f() {}
9158 template S<int>;
9159 template void f<double>();
9161 Here, the ARGS for the instantiation of will be {int,
9162 double}. But, we only need as many ARGS as there are
9163 levels of template parameters in CODE_PATTERN. We are
9164 careful not to get fooled into reducing the ARGS in
9165 situations like:
9167 template <class T> struct S { template <class U> void f(U); }
9168 template <class T> template <> void S<T>::f(int) {}
9170 which we can spot because the pattern will be a
9171 specialization in this case. */
9172 args_depth = TMPL_ARGS_DEPTH (args);
9173 parms_depth =
9174 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9175 if (args_depth > parms_depth
9176 && !DECL_TEMPLATE_SPECIALIZATION (t))
9177 args = get_innermost_template_args (args, parms_depth);
9179 else
9181 /* This special case arises when we have something like this:
9183 template <class T> struct S {
9184 friend void f<int>(int, double);
9187 Here, the DECL_TI_TEMPLATE for the friend declaration
9188 will be an IDENTIFIER_NODE. We are being called from
9189 tsubst_friend_function, and we want only to create a
9190 new decl (R) with appropriate types so that we can call
9191 determine_specialization. */
9192 gen_tmpl = NULL_TREE;
9195 if (DECL_CLASS_SCOPE_P (t))
9197 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9198 member = 2;
9199 else
9200 member = 1;
9201 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9202 complain, t, /*entering_scope=*/1);
9204 else
9206 member = 0;
9207 ctx = DECL_CONTEXT (t);
9209 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9210 if (type == error_mark_node)
9211 RETURN (error_mark_node);
9213 /* We do NOT check for matching decls pushed separately at this
9214 point, as they may not represent instantiations of this
9215 template, and in any case are considered separate under the
9216 discrete model. */
9217 r = copy_decl (t);
9218 DECL_USE_TEMPLATE (r) = 0;
9219 TREE_TYPE (r) = type;
9220 /* Clear out the mangled name and RTL for the instantiation. */
9221 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9222 SET_DECL_RTL (r, NULL);
9223 /* Leave DECL_INITIAL set on deleted instantiations. */
9224 if (!DECL_DELETED_FN (r))
9225 DECL_INITIAL (r) = NULL_TREE;
9226 DECL_CONTEXT (r) = ctx;
9228 if (member && DECL_CONV_FN_P (r))
9229 /* Type-conversion operator. Reconstruct the name, in
9230 case it's the name of one of the template's parameters. */
9231 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9233 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9234 complain, t);
9235 DECL_RESULT (r) = NULL_TREE;
9237 TREE_STATIC (r) = 0;
9238 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9239 DECL_EXTERNAL (r) = 1;
9240 /* If this is an instantiation of a function with internal
9241 linkage, we already know what object file linkage will be
9242 assigned to the instantiation. */
9243 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9244 DECL_DEFER_OUTPUT (r) = 0;
9245 DECL_CHAIN (r) = NULL_TREE;
9246 DECL_PENDING_INLINE_INFO (r) = 0;
9247 DECL_PENDING_INLINE_P (r) = 0;
9248 DECL_SAVED_TREE (r) = NULL_TREE;
9249 DECL_STRUCT_FUNCTION (r) = NULL;
9250 TREE_USED (r) = 0;
9251 /* We'll re-clone as appropriate in instantiate_template. */
9252 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9254 /* If we aren't complaining now, return on error before we register
9255 the specialization so that we'll complain eventually. */
9256 if ((complain & tf_error) == 0
9257 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9258 && !grok_op_properties (r, /*complain=*/false))
9259 RETURN (error_mark_node);
9261 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9262 this in the special friend case mentioned above where
9263 GEN_TMPL is NULL. */
9264 if (gen_tmpl)
9266 DECL_TEMPLATE_INFO (r)
9267 = build_template_info (gen_tmpl, argvec);
9268 SET_DECL_IMPLICIT_INSTANTIATION (r);
9269 register_specialization (r, gen_tmpl, argvec, false, hash);
9271 /* We're not supposed to instantiate default arguments
9272 until they are called, for a template. But, for a
9273 declaration like:
9275 template <class T> void f ()
9276 { extern void g(int i = T()); }
9278 we should do the substitution when the template is
9279 instantiated. We handle the member function case in
9280 instantiate_class_template since the default arguments
9281 might refer to other members of the class. */
9282 if (!member
9283 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9284 && !uses_template_parms (argvec))
9285 tsubst_default_arguments (r);
9287 else
9288 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9290 /* Copy the list of befriending classes. */
9291 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9292 *friends;
9293 friends = &TREE_CHAIN (*friends))
9295 *friends = copy_node (*friends);
9296 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9297 args, complain,
9298 in_decl);
9301 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9303 maybe_retrofit_in_chrg (r);
9304 if (DECL_CONSTRUCTOR_P (r))
9305 grok_ctor_properties (ctx, r);
9306 /* If this is an instantiation of a member template, clone it.
9307 If it isn't, that'll be handled by
9308 clone_constructors_and_destructors. */
9309 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9310 clone_function_decl (r, /*update_method_vec_p=*/0);
9312 else if ((complain & tf_error) != 0
9313 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9314 && !grok_op_properties (r, /*complain=*/true))
9315 RETURN (error_mark_node);
9317 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9318 SET_DECL_FRIEND_CONTEXT (r,
9319 tsubst (DECL_FRIEND_CONTEXT (t),
9320 args, complain, in_decl));
9322 /* Possibly limit visibility based on template args. */
9323 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9324 if (DECL_VISIBILITY_SPECIFIED (t))
9326 DECL_VISIBILITY_SPECIFIED (r) = 0;
9327 DECL_ATTRIBUTES (r)
9328 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9330 determine_visibility (r);
9331 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9332 && !processing_template_decl)
9333 defaulted_late_check (r);
9335 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9336 args, complain, in_decl);
9338 break;
9340 case PARM_DECL:
9342 tree type = NULL_TREE;
9343 int i, len = 1;
9344 tree expanded_types = NULL_TREE;
9345 tree prev_r = NULL_TREE;
9346 tree first_r = NULL_TREE;
9348 if (FUNCTION_PARAMETER_PACK_P (t))
9350 /* If there is a local specialization that isn't a
9351 parameter pack, it means that we're doing a "simple"
9352 substitution from inside tsubst_pack_expansion. Just
9353 return the local specialization (which will be a single
9354 parm). */
9355 tree spec = retrieve_local_specialization (t);
9356 if (spec
9357 && TREE_CODE (spec) == PARM_DECL
9358 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9359 RETURN (spec);
9361 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9362 the parameters in this function parameter pack. */
9363 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9364 complain, in_decl);
9365 if (TREE_CODE (expanded_types) == TREE_VEC)
9367 len = TREE_VEC_LENGTH (expanded_types);
9369 /* Zero-length parameter packs are boring. Just substitute
9370 into the chain. */
9371 if (len == 0)
9372 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9373 TREE_CHAIN (t)));
9375 else
9377 /* All we did was update the type. Make a note of that. */
9378 type = expanded_types;
9379 expanded_types = NULL_TREE;
9383 /* Loop through all of the parameter's we'll build. When T is
9384 a function parameter pack, LEN is the number of expanded
9385 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9386 r = NULL_TREE;
9387 for (i = 0; i < len; ++i)
9389 prev_r = r;
9390 r = copy_node (t);
9391 if (DECL_TEMPLATE_PARM_P (t))
9392 SET_DECL_TEMPLATE_PARM_P (r);
9394 /* An argument of a function parameter pack is not a parameter
9395 pack. */
9396 FUNCTION_PARAMETER_PACK_P (r) = false;
9398 if (expanded_types)
9399 /* We're on the Ith parameter of the function parameter
9400 pack. */
9402 /* Get the Ith type. */
9403 type = TREE_VEC_ELT (expanded_types, i);
9405 if (DECL_NAME (r))
9406 /* Rename the parameter to include the index. */
9407 DECL_NAME (r) =
9408 make_ith_pack_parameter_name (DECL_NAME (r), i);
9410 else if (!type)
9411 /* We're dealing with a normal parameter. */
9412 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9414 type = type_decays_to (type);
9415 TREE_TYPE (r) = type;
9416 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9418 if (DECL_INITIAL (r))
9420 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9421 DECL_INITIAL (r) = TREE_TYPE (r);
9422 else
9423 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9424 complain, in_decl);
9427 DECL_CONTEXT (r) = NULL_TREE;
9429 if (!DECL_TEMPLATE_PARM_P (r))
9430 DECL_ARG_TYPE (r) = type_passed_as (type);
9432 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9433 args, complain, in_decl);
9435 /* Keep track of the first new parameter we
9436 generate. That's what will be returned to the
9437 caller. */
9438 if (!first_r)
9439 first_r = r;
9441 /* Build a proper chain of parameters when substituting
9442 into a function parameter pack. */
9443 if (prev_r)
9444 DECL_CHAIN (prev_r) = r;
9447 if (DECL_CHAIN (t))
9448 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9449 complain, DECL_CHAIN (t));
9451 /* FIRST_R contains the start of the chain we've built. */
9452 r = first_r;
9454 break;
9456 case FIELD_DECL:
9458 tree type;
9460 r = copy_decl (t);
9461 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9462 if (type == error_mark_node)
9463 RETURN (error_mark_node);
9464 TREE_TYPE (r) = type;
9465 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9467 /* DECL_INITIAL gives the number of bits in a bit-field. */
9468 DECL_INITIAL (r)
9469 = tsubst_expr (DECL_INITIAL (t), args,
9470 complain, in_decl,
9471 /*integral_constant_expression_p=*/true);
9472 /* We don't have to set DECL_CONTEXT here; it is set by
9473 finish_member_declaration. */
9474 DECL_CHAIN (r) = NULL_TREE;
9475 if (VOID_TYPE_P (type))
9476 error ("instantiation of %q+D as type %qT", r, type);
9478 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9479 args, complain, in_decl);
9481 break;
9483 case USING_DECL:
9484 /* We reach here only for member using decls. */
9485 if (DECL_DEPENDENT_P (t))
9487 r = do_class_using_decl
9488 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9489 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9490 if (!r)
9491 r = error_mark_node;
9492 else
9494 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9495 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9498 else
9500 r = copy_node (t);
9501 DECL_CHAIN (r) = NULL_TREE;
9503 break;
9505 case TYPE_DECL:
9506 case VAR_DECL:
9508 tree argvec = NULL_TREE;
9509 tree gen_tmpl = NULL_TREE;
9510 tree spec;
9511 tree tmpl = NULL_TREE;
9512 tree ctx;
9513 tree type = NULL_TREE;
9514 bool local_p;
9516 if (TREE_CODE (t) == TYPE_DECL
9517 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9519 /* If this is the canonical decl, we don't have to
9520 mess with instantiations, and often we can't (for
9521 typename, template type parms and such). Note that
9522 TYPE_NAME is not correct for the above test if
9523 we've copied the type for a typedef. */
9524 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9525 if (type == error_mark_node)
9526 RETURN (error_mark_node);
9527 r = TYPE_NAME (type);
9528 break;
9531 /* Check to see if we already have the specialization we
9532 need. */
9533 spec = NULL_TREE;
9534 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9536 /* T is a static data member or namespace-scope entity.
9537 We have to substitute into namespace-scope variables
9538 (even though such entities are never templates) because
9539 of cases like:
9541 template <class T> void f() { extern T t; }
9543 where the entity referenced is not known until
9544 instantiation time. */
9545 local_p = false;
9546 ctx = DECL_CONTEXT (t);
9547 if (DECL_CLASS_SCOPE_P (t))
9549 ctx = tsubst_aggr_type (ctx, args,
9550 complain,
9551 in_decl, /*entering_scope=*/1);
9552 /* If CTX is unchanged, then T is in fact the
9553 specialization we want. That situation occurs when
9554 referencing a static data member within in its own
9555 class. We can use pointer equality, rather than
9556 same_type_p, because DECL_CONTEXT is always
9557 canonical. */
9558 if (ctx == DECL_CONTEXT (t))
9559 spec = t;
9562 if (!spec)
9564 tmpl = DECL_TI_TEMPLATE (t);
9565 gen_tmpl = most_general_template (tmpl);
9566 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9567 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9568 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9571 else
9573 /* A local variable. */
9574 local_p = true;
9575 /* Subsequent calls to pushdecl will fill this in. */
9576 ctx = NULL_TREE;
9577 spec = retrieve_local_specialization (t);
9579 /* If we already have the specialization we need, there is
9580 nothing more to do. */
9581 if (spec)
9583 r = spec;
9584 break;
9587 /* Create a new node for the specialization we need. */
9588 r = copy_decl (t);
9589 if (type == NULL_TREE)
9591 if (is_typedef_decl (t))
9592 type = DECL_ORIGINAL_TYPE (t);
9593 else
9594 type = TREE_TYPE (t);
9595 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9596 type = strip_array_domain (type);
9597 type = tsubst (type, args, complain, in_decl);
9599 if (TREE_CODE (r) == VAR_DECL)
9601 /* Even if the original location is out of scope, the
9602 newly substituted one is not. */
9603 DECL_DEAD_FOR_LOCAL (r) = 0;
9604 DECL_INITIALIZED_P (r) = 0;
9605 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9606 if (type == error_mark_node)
9607 RETURN (error_mark_node);
9608 if (TREE_CODE (type) == FUNCTION_TYPE)
9610 /* It may seem that this case cannot occur, since:
9612 typedef void f();
9613 void g() { f x; }
9615 declares a function, not a variable. However:
9617 typedef void f();
9618 template <typename T> void g() { T t; }
9619 template void g<f>();
9621 is an attempt to declare a variable with function
9622 type. */
9623 error ("variable %qD has function type",
9624 /* R is not yet sufficiently initialized, so we
9625 just use its name. */
9626 DECL_NAME (r));
9627 RETURN (error_mark_node);
9629 type = complete_type (type);
9630 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9631 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9632 type = check_var_type (DECL_NAME (r), type);
9634 if (DECL_HAS_VALUE_EXPR_P (t))
9636 tree ve = DECL_VALUE_EXPR (t);
9637 ve = tsubst_expr (ve, args, complain, in_decl,
9638 /*constant_expression_p=*/false);
9639 SET_DECL_VALUE_EXPR (r, ve);
9642 else if (DECL_SELF_REFERENCE_P (t))
9643 SET_DECL_SELF_REFERENCE_P (r);
9644 TREE_TYPE (r) = type;
9645 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9646 DECL_CONTEXT (r) = ctx;
9647 /* Clear out the mangled name and RTL for the instantiation. */
9648 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9649 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9650 SET_DECL_RTL (r, NULL);
9651 /* The initializer must not be expanded until it is required;
9652 see [temp.inst]. */
9653 DECL_INITIAL (r) = NULL_TREE;
9654 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9655 SET_DECL_RTL (r, NULL);
9656 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9657 if (TREE_CODE (r) == VAR_DECL)
9659 /* Possibly limit visibility based on template args. */
9660 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9661 if (DECL_VISIBILITY_SPECIFIED (t))
9663 DECL_VISIBILITY_SPECIFIED (r) = 0;
9664 DECL_ATTRIBUTES (r)
9665 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9667 determine_visibility (r);
9670 if (!local_p)
9672 /* A static data member declaration is always marked
9673 external when it is declared in-class, even if an
9674 initializer is present. We mimic the non-template
9675 processing here. */
9676 DECL_EXTERNAL (r) = 1;
9678 register_specialization (r, gen_tmpl, argvec, false, hash);
9679 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9680 SET_DECL_IMPLICIT_INSTANTIATION (r);
9682 else if (cp_unevaluated_operand)
9684 /* We're substituting this var in a decltype outside of its
9685 scope, such as for a lambda return type. Don't add it to
9686 local_specializations, do perform auto deduction. */
9687 tree auto_node = type_uses_auto (type);
9688 tree init
9689 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9690 /*constant_expression_p=*/false);
9692 if (auto_node && init && describable_type (init))
9694 type = do_auto_deduction (type, init, auto_node);
9695 TREE_TYPE (r) = type;
9698 else
9699 register_local_specialization (r, t);
9701 DECL_CHAIN (r) = NULL_TREE;
9703 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9704 /*flags=*/0,
9705 args, complain, in_decl);
9707 /* Preserve a typedef that names a type. */
9708 if (is_typedef_decl (r))
9710 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9711 set_underlying_type (r);
9714 layout_decl (r, 0);
9716 break;
9718 default:
9719 gcc_unreachable ();
9721 #undef RETURN
9723 out:
9724 /* Restore the file and line information. */
9725 input_location = saved_loc;
9727 return r;
9730 /* Substitute into the ARG_TYPES of a function type. */
9732 static tree
9733 tsubst_arg_types (tree arg_types,
9734 tree args,
9735 tsubst_flags_t complain,
9736 tree in_decl)
9738 tree remaining_arg_types;
9739 tree type = NULL_TREE;
9740 int i = 1;
9741 tree expanded_args = NULL_TREE;
9742 tree default_arg;
9744 if (!arg_types || arg_types == void_list_node)
9745 return arg_types;
9747 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9748 args, complain, in_decl);
9749 if (remaining_arg_types == error_mark_node)
9750 return error_mark_node;
9752 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9754 /* For a pack expansion, perform substitution on the
9755 entire expression. Later on, we'll handle the arguments
9756 one-by-one. */
9757 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9758 args, complain, in_decl);
9760 if (TREE_CODE (expanded_args) == TREE_VEC)
9761 /* So that we'll spin through the parameters, one by one. */
9762 i = TREE_VEC_LENGTH (expanded_args);
9763 else
9765 /* We only partially substituted into the parameter
9766 pack. Our type is TYPE_PACK_EXPANSION. */
9767 type = expanded_args;
9768 expanded_args = NULL_TREE;
9772 while (i > 0) {
9773 --i;
9775 if (expanded_args)
9776 type = TREE_VEC_ELT (expanded_args, i);
9777 else if (!type)
9778 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9780 if (type == error_mark_node)
9781 return error_mark_node;
9782 if (VOID_TYPE_P (type))
9784 if (complain & tf_error)
9786 error ("invalid parameter type %qT", type);
9787 if (in_decl)
9788 error ("in declaration %q+D", in_decl);
9790 return error_mark_node;
9793 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9794 top-level qualifiers as required. */
9795 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9797 /* We do not substitute into default arguments here. The standard
9798 mandates that they be instantiated only when needed, which is
9799 done in build_over_call. */
9800 default_arg = TREE_PURPOSE (arg_types);
9802 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9804 /* We've instantiated a template before its default arguments
9805 have been parsed. This can happen for a nested template
9806 class, and is not an error unless we require the default
9807 argument in a call of this function. */
9808 remaining_arg_types =
9809 tree_cons (default_arg, type, remaining_arg_types);
9810 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9811 remaining_arg_types);
9813 else
9814 remaining_arg_types =
9815 hash_tree_cons (default_arg, type, remaining_arg_types);
9818 return remaining_arg_types;
9821 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9822 *not* handle the exception-specification for FNTYPE, because the
9823 initial substitution of explicitly provided template parameters
9824 during argument deduction forbids substitution into the
9825 exception-specification:
9827 [temp.deduct]
9829 All references in the function type of the function template to the
9830 corresponding template parameters are replaced by the specified tem-
9831 plate argument values. If a substitution in a template parameter or
9832 in the function type of the function template results in an invalid
9833 type, type deduction fails. [Note: The equivalent substitution in
9834 exception specifications is done only when the function is instanti-
9835 ated, at which point a program is ill-formed if the substitution
9836 results in an invalid type.] */
9838 static tree
9839 tsubst_function_type (tree t,
9840 tree args,
9841 tsubst_flags_t complain,
9842 tree in_decl)
9844 tree return_type;
9845 tree arg_types;
9846 tree fntype;
9848 /* The TYPE_CONTEXT is not used for function/method types. */
9849 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9851 /* Substitute the return type. */
9852 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9853 if (return_type == error_mark_node)
9854 return error_mark_node;
9855 /* The standard does not presently indicate that creation of a
9856 function type with an invalid return type is a deduction failure.
9857 However, that is clearly analogous to creating an array of "void"
9858 or a reference to a reference. This is core issue #486. */
9859 if (TREE_CODE (return_type) == ARRAY_TYPE
9860 || TREE_CODE (return_type) == FUNCTION_TYPE)
9862 if (complain & tf_error)
9864 if (TREE_CODE (return_type) == ARRAY_TYPE)
9865 error ("function returning an array");
9866 else
9867 error ("function returning a function");
9869 return error_mark_node;
9872 /* Substitute the argument types. */
9873 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9874 complain, in_decl);
9875 if (arg_types == error_mark_node)
9876 return error_mark_node;
9878 /* Construct a new type node and return it. */
9879 if (TREE_CODE (t) == FUNCTION_TYPE)
9881 fntype = build_function_type (return_type, arg_types);
9882 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
9884 else
9886 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9887 if (! MAYBE_CLASS_TYPE_P (r))
9889 /* [temp.deduct]
9891 Type deduction may fail for any of the following
9892 reasons:
9894 -- Attempting to create "pointer to member of T" when T
9895 is not a class type. */
9896 if (complain & tf_error)
9897 error ("creating pointer to member function of non-class type %qT",
9899 return error_mark_node;
9902 fntype = build_method_type_directly (r, return_type,
9903 TREE_CHAIN (arg_types));
9905 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9907 return fntype;
9910 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9911 ARGS into that specification, and return the substituted
9912 specification. If there is no specification, return NULL_TREE. */
9914 static tree
9915 tsubst_exception_specification (tree fntype,
9916 tree args,
9917 tsubst_flags_t complain,
9918 tree in_decl)
9920 tree specs;
9921 tree new_specs;
9923 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9924 new_specs = NULL_TREE;
9925 if (specs && TREE_PURPOSE (specs))
9927 /* A noexcept-specifier. */
9928 new_specs = tsubst_copy_and_build
9929 (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false,
9930 /*integral_constant_expression_p=*/true);
9931 new_specs = build_noexcept_spec (new_specs, complain);
9933 else if (specs)
9935 if (! TREE_VALUE (specs))
9936 new_specs = specs;
9937 else
9938 while (specs)
9940 tree spec;
9941 int i, len = 1;
9942 tree expanded_specs = NULL_TREE;
9944 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9946 /* Expand the pack expansion type. */
9947 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9948 args, complain,
9949 in_decl);
9951 if (expanded_specs == error_mark_node)
9952 return error_mark_node;
9953 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9954 len = TREE_VEC_LENGTH (expanded_specs);
9955 else
9957 /* We're substituting into a member template, so
9958 we got a TYPE_PACK_EXPANSION back. Add that
9959 expansion and move on. */
9960 gcc_assert (TREE_CODE (expanded_specs)
9961 == TYPE_PACK_EXPANSION);
9962 new_specs = add_exception_specifier (new_specs,
9963 expanded_specs,
9964 complain);
9965 specs = TREE_CHAIN (specs);
9966 continue;
9970 for (i = 0; i < len; ++i)
9972 if (expanded_specs)
9973 spec = TREE_VEC_ELT (expanded_specs, i);
9974 else
9975 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9976 if (spec == error_mark_node)
9977 return spec;
9978 new_specs = add_exception_specifier (new_specs, spec,
9979 complain);
9982 specs = TREE_CHAIN (specs);
9985 return new_specs;
9988 /* Take the tree structure T and replace template parameters used
9989 therein with the argument vector ARGS. IN_DECL is an associated
9990 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9991 Issue error and warning messages under control of COMPLAIN. Note
9992 that we must be relatively non-tolerant of extensions here, in
9993 order to preserve conformance; if we allow substitutions that
9994 should not be allowed, we may allow argument deductions that should
9995 not succeed, and therefore report ambiguous overload situations
9996 where there are none. In theory, we could allow the substitution,
9997 but indicate that it should have failed, and allow our caller to
9998 make sure that the right thing happens, but we don't try to do this
9999 yet.
10001 This function is used for dealing with types, decls and the like;
10002 for expressions, use tsubst_expr or tsubst_copy. */
10004 tree
10005 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10007 enum tree_code code;
10008 tree type, r;
10010 if (t == NULL_TREE || t == error_mark_node
10011 || t == integer_type_node
10012 || t == void_type_node
10013 || t == char_type_node
10014 || t == unknown_type_node
10015 || TREE_CODE (t) == NAMESPACE_DECL)
10016 return t;
10018 if (DECL_P (t))
10019 return tsubst_decl (t, args, complain);
10021 if (args == NULL_TREE)
10022 return t;
10024 code = TREE_CODE (t);
10026 if (code == IDENTIFIER_NODE)
10027 type = IDENTIFIER_TYPE_VALUE (t);
10028 else
10029 type = TREE_TYPE (t);
10031 gcc_assert (type != unknown_type_node);
10033 /* Reuse typedefs. We need to do this to handle dependent attributes,
10034 such as attribute aligned. */
10035 if (TYPE_P (t)
10036 && TYPE_NAME (t)
10037 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
10039 tree decl = TYPE_NAME (t);
10041 if (DECL_CLASS_SCOPE_P (decl)
10042 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10043 && uses_template_parms (DECL_CONTEXT (decl)))
10045 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10046 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10047 r = retrieve_specialization (tmpl, gen_args, 0);
10049 else if (DECL_FUNCTION_SCOPE_P (decl)
10050 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10051 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10052 r = retrieve_local_specialization (decl);
10053 else
10054 /* The typedef is from a non-template context. */
10055 return t;
10057 if (r)
10059 r = TREE_TYPE (r);
10060 r = cp_build_qualified_type_real
10061 (r, cp_type_quals (t) | cp_type_quals (r),
10062 complain | tf_ignore_bad_quals);
10063 return r;
10065 /* Else we must be instantiating the typedef, so fall through. */
10068 if (type
10069 && code != TYPENAME_TYPE
10070 && code != TEMPLATE_TYPE_PARM
10071 && code != IDENTIFIER_NODE
10072 && code != FUNCTION_TYPE
10073 && code != METHOD_TYPE)
10074 type = tsubst (type, args, complain, in_decl);
10075 if (type == error_mark_node)
10076 return error_mark_node;
10078 switch (code)
10080 case RECORD_TYPE:
10081 case UNION_TYPE:
10082 case ENUMERAL_TYPE:
10083 return tsubst_aggr_type (t, args, complain, in_decl,
10084 /*entering_scope=*/0);
10086 case ERROR_MARK:
10087 case IDENTIFIER_NODE:
10088 case VOID_TYPE:
10089 case REAL_TYPE:
10090 case COMPLEX_TYPE:
10091 case VECTOR_TYPE:
10092 case BOOLEAN_TYPE:
10093 case INTEGER_CST:
10094 case REAL_CST:
10095 case STRING_CST:
10096 return t;
10098 case INTEGER_TYPE:
10099 if (t == integer_type_node)
10100 return t;
10102 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10103 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10104 return t;
10107 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10109 max = tsubst_expr (omax, args, complain, in_decl,
10110 /*integral_constant_expression_p=*/false);
10112 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10113 needed. */
10114 if (TREE_CODE (max) == NOP_EXPR
10115 && TREE_SIDE_EFFECTS (omax)
10116 && !TREE_TYPE (max))
10117 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10119 max = mark_rvalue_use (max);
10120 max = fold_decl_constant_value (max);
10122 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10123 with TREE_SIDE_EFFECTS that indicates this is not an integral
10124 constant expression. */
10125 if (processing_template_decl
10126 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10128 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10129 TREE_SIDE_EFFECTS (max) = 1;
10132 if (TREE_CODE (max) != INTEGER_CST
10133 && !at_function_scope_p ()
10134 && !TREE_SIDE_EFFECTS (max)
10135 && !value_dependent_expression_p (max))
10137 if (complain & tf_error)
10138 error ("array bound is not an integer constant");
10139 return error_mark_node;
10142 /* [temp.deduct]
10144 Type deduction may fail for any of the following
10145 reasons:
10147 Attempting to create an array with a size that is
10148 zero or negative. */
10149 if (integer_zerop (max) && !(complain & tf_error))
10150 /* We must fail if performing argument deduction (as
10151 indicated by the state of complain), so that
10152 another substitution can be found. */
10153 return error_mark_node;
10154 else if (TREE_CODE (max) == INTEGER_CST
10155 && INT_CST_LT (max, integer_zero_node))
10157 if (complain & tf_error)
10158 error ("creating array with negative size (%qE)", max);
10160 return error_mark_node;
10163 return compute_array_index_type (NULL_TREE, max);
10166 case TEMPLATE_TYPE_PARM:
10167 case TEMPLATE_TEMPLATE_PARM:
10168 case BOUND_TEMPLATE_TEMPLATE_PARM:
10169 case TEMPLATE_PARM_INDEX:
10171 int idx;
10172 int level;
10173 int levels;
10174 tree arg = NULL_TREE;
10176 r = NULL_TREE;
10178 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10179 template_parm_level_and_index (t, &level, &idx);
10181 levels = TMPL_ARGS_DEPTH (args);
10182 if (level <= levels)
10184 arg = TMPL_ARG (args, level, idx);
10186 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10187 /* See through ARGUMENT_PACK_SELECT arguments. */
10188 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10191 if (arg == error_mark_node)
10192 return error_mark_node;
10193 else if (arg != NULL_TREE)
10195 if (ARGUMENT_PACK_P (arg))
10196 /* If ARG is an argument pack, we don't actually want to
10197 perform a substitution here, because substitutions
10198 for argument packs are only done
10199 element-by-element. We can get to this point when
10200 substituting the type of a non-type template
10201 parameter pack, when that type actually contains
10202 template parameter packs from an outer template, e.g.,
10204 template<typename... Types> struct A {
10205 template<Types... Values> struct B { };
10206 }; */
10207 return t;
10209 if (code == TEMPLATE_TYPE_PARM)
10211 int quals;
10212 gcc_assert (TYPE_P (arg));
10214 quals = cp_type_quals (arg) | cp_type_quals (t);
10216 return cp_build_qualified_type_real
10217 (arg, quals, complain | tf_ignore_bad_quals);
10219 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10221 /* We are processing a type constructed from a
10222 template template parameter. */
10223 tree argvec = tsubst (TYPE_TI_ARGS (t),
10224 args, complain, in_decl);
10225 if (argvec == error_mark_node)
10226 return error_mark_node;
10228 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10229 are resolving nested-types in the signature of a
10230 member function templates. Otherwise ARG is a
10231 TEMPLATE_DECL and is the real template to be
10232 instantiated. */
10233 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10234 arg = TYPE_NAME (arg);
10236 r = lookup_template_class (arg,
10237 argvec, in_decl,
10238 DECL_CONTEXT (arg),
10239 /*entering_scope=*/0,
10240 complain);
10241 return cp_build_qualified_type_real
10242 (r, cp_type_quals (t), complain);
10244 else
10245 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10246 return unshare_expr (arg);
10249 if (level == 1)
10250 /* This can happen during the attempted tsubst'ing in
10251 unify. This means that we don't yet have any information
10252 about the template parameter in question. */
10253 return t;
10255 /* If we get here, we must have been looking at a parm for a
10256 more deeply nested template. Make a new version of this
10257 template parameter, but with a lower level. */
10258 switch (code)
10260 case TEMPLATE_TYPE_PARM:
10261 case TEMPLATE_TEMPLATE_PARM:
10262 case BOUND_TEMPLATE_TEMPLATE_PARM:
10263 if (cp_type_quals (t))
10265 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10266 r = cp_build_qualified_type_real
10267 (r, cp_type_quals (t),
10268 complain | (code == TEMPLATE_TYPE_PARM
10269 ? tf_ignore_bad_quals : 0));
10271 else
10273 r = copy_type (t);
10274 TEMPLATE_TYPE_PARM_INDEX (r)
10275 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10276 r, levels, args, complain);
10277 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10278 TYPE_MAIN_VARIANT (r) = r;
10279 TYPE_POINTER_TO (r) = NULL_TREE;
10280 TYPE_REFERENCE_TO (r) = NULL_TREE;
10282 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10283 /* We have reduced the level of the template
10284 template parameter, but not the levels of its
10285 template parameters, so canonical_type_parameter
10286 will not be able to find the canonical template
10287 template parameter for this level. Thus, we
10288 require structural equality checking to compare
10289 TEMPLATE_TEMPLATE_PARMs. */
10290 SET_TYPE_STRUCTURAL_EQUALITY (r);
10291 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10292 SET_TYPE_STRUCTURAL_EQUALITY (r);
10293 else
10294 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10296 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10298 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10299 complain, in_decl);
10300 if (argvec == error_mark_node)
10301 return error_mark_node;
10303 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10304 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10307 break;
10309 case TEMPLATE_PARM_INDEX:
10310 r = reduce_template_parm_level (t, type, levels, args, complain);
10311 break;
10313 default:
10314 gcc_unreachable ();
10317 return r;
10320 case TREE_LIST:
10322 tree purpose, value, chain;
10324 if (t == void_list_node)
10325 return t;
10327 purpose = TREE_PURPOSE (t);
10328 if (purpose)
10330 purpose = tsubst (purpose, args, complain, in_decl);
10331 if (purpose == error_mark_node)
10332 return error_mark_node;
10334 value = TREE_VALUE (t);
10335 if (value)
10337 value = tsubst (value, args, complain, in_decl);
10338 if (value == error_mark_node)
10339 return error_mark_node;
10341 chain = TREE_CHAIN (t);
10342 if (chain && chain != void_type_node)
10344 chain = tsubst (chain, args, complain, in_decl);
10345 if (chain == error_mark_node)
10346 return error_mark_node;
10348 if (purpose == TREE_PURPOSE (t)
10349 && value == TREE_VALUE (t)
10350 && chain == TREE_CHAIN (t))
10351 return t;
10352 return hash_tree_cons (purpose, value, chain);
10355 case TREE_BINFO:
10356 /* We should never be tsubsting a binfo. */
10357 gcc_unreachable ();
10359 case TREE_VEC:
10360 /* A vector of template arguments. */
10361 gcc_assert (!type);
10362 return tsubst_template_args (t, args, complain, in_decl);
10364 case POINTER_TYPE:
10365 case REFERENCE_TYPE:
10367 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10368 return t;
10370 /* [temp.deduct]
10372 Type deduction may fail for any of the following
10373 reasons:
10375 -- Attempting to create a pointer to reference type.
10376 -- Attempting to create a reference to a reference type or
10377 a reference to void.
10379 Core issue 106 says that creating a reference to a reference
10380 during instantiation is no longer a cause for failure. We
10381 only enforce this check in strict C++98 mode. */
10382 if ((TREE_CODE (type) == REFERENCE_TYPE
10383 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10384 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10386 static location_t last_loc;
10388 /* We keep track of the last time we issued this error
10389 message to avoid spewing a ton of messages during a
10390 single bad template instantiation. */
10391 if (complain & tf_error
10392 && last_loc != input_location)
10394 if (TREE_CODE (type) == VOID_TYPE)
10395 error ("forming reference to void");
10396 else if (code == POINTER_TYPE)
10397 error ("forming pointer to reference type %qT", type);
10398 else
10399 error ("forming reference to reference type %qT", type);
10400 last_loc = input_location;
10403 return error_mark_node;
10405 else if (code == POINTER_TYPE)
10407 r = build_pointer_type (type);
10408 if (TREE_CODE (type) == METHOD_TYPE)
10409 r = build_ptrmemfunc_type (r);
10411 else if (TREE_CODE (type) == REFERENCE_TYPE)
10412 /* In C++0x, during template argument substitution, when there is an
10413 attempt to create a reference to a reference type, reference
10414 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10416 "If a template-argument for a template-parameter T names a type
10417 that is a reference to a type A, an attempt to create the type
10418 'lvalue reference to cv T' creates the type 'lvalue reference to
10419 A,' while an attempt to create the type type rvalue reference to
10420 cv T' creates the type T"
10422 r = cp_build_reference_type
10423 (TREE_TYPE (type),
10424 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10425 else
10426 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10427 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10429 if (r != error_mark_node)
10430 /* Will this ever be needed for TYPE_..._TO values? */
10431 layout_type (r);
10433 return r;
10435 case OFFSET_TYPE:
10437 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10438 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10440 /* [temp.deduct]
10442 Type deduction may fail for any of the following
10443 reasons:
10445 -- Attempting to create "pointer to member of T" when T
10446 is not a class type. */
10447 if (complain & tf_error)
10448 error ("creating pointer to member of non-class type %qT", r);
10449 return error_mark_node;
10451 if (TREE_CODE (type) == REFERENCE_TYPE)
10453 if (complain & tf_error)
10454 error ("creating pointer to member reference type %qT", type);
10455 return error_mark_node;
10457 if (TREE_CODE (type) == VOID_TYPE)
10459 if (complain & tf_error)
10460 error ("creating pointer to member of type void");
10461 return error_mark_node;
10463 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10464 if (TREE_CODE (type) == FUNCTION_TYPE)
10466 /* The type of the implicit object parameter gets its
10467 cv-qualifiers from the FUNCTION_TYPE. */
10468 tree memptr;
10469 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10470 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10471 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10472 complain);
10474 else
10475 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10476 cp_type_quals (t),
10477 complain);
10479 case FUNCTION_TYPE:
10480 case METHOD_TYPE:
10482 tree fntype;
10483 tree specs;
10484 fntype = tsubst_function_type (t, args, complain, in_decl);
10485 if (fntype == error_mark_node)
10486 return error_mark_node;
10488 /* Substitute the exception specification. */
10489 specs = tsubst_exception_specification (t, args, complain,
10490 in_decl);
10491 if (specs == error_mark_node)
10492 return error_mark_node;
10493 if (specs)
10494 fntype = build_exception_variant (fntype, specs);
10495 return fntype;
10497 case ARRAY_TYPE:
10499 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10500 if (domain == error_mark_node)
10501 return error_mark_node;
10503 /* As an optimization, we avoid regenerating the array type if
10504 it will obviously be the same as T. */
10505 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10506 return t;
10508 /* These checks should match the ones in grokdeclarator.
10510 [temp.deduct]
10512 The deduction may fail for any of the following reasons:
10514 -- Attempting to create an array with an element type that
10515 is void, a function type, or a reference type, or [DR337]
10516 an abstract class type. */
10517 if (TREE_CODE (type) == VOID_TYPE
10518 || TREE_CODE (type) == FUNCTION_TYPE
10519 || TREE_CODE (type) == REFERENCE_TYPE)
10521 if (complain & tf_error)
10522 error ("creating array of %qT", type);
10523 return error_mark_node;
10525 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10527 if (complain & tf_error)
10528 error ("creating array of %qT, which is an abstract class type",
10529 type);
10530 return error_mark_node;
10533 r = build_cplus_array_type (type, domain);
10535 if (TYPE_USER_ALIGN (t))
10537 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10538 TYPE_USER_ALIGN (r) = 1;
10541 return r;
10544 case PLUS_EXPR:
10545 case MINUS_EXPR:
10547 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10548 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10550 if (e1 == error_mark_node || e2 == error_mark_node)
10551 return error_mark_node;
10553 return fold_build2_loc (input_location,
10554 code, TREE_TYPE (t), e1, e2);
10557 case NEGATE_EXPR:
10558 case NOP_EXPR:
10560 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10561 if (e == error_mark_node)
10562 return error_mark_node;
10564 return fold_build1_loc (input_location, code, TREE_TYPE (t), e);
10567 case TYPENAME_TYPE:
10569 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10570 in_decl, /*entering_scope=*/1);
10571 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10572 complain, in_decl);
10574 if (ctx == error_mark_node || f == error_mark_node)
10575 return error_mark_node;
10577 if (!MAYBE_CLASS_TYPE_P (ctx))
10579 if (complain & tf_error)
10580 error ("%qT is not a class, struct, or union type", ctx);
10581 return error_mark_node;
10583 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10585 /* Normally, make_typename_type does not require that the CTX
10586 have complete type in order to allow things like:
10588 template <class T> struct S { typename S<T>::X Y; };
10590 But, such constructs have already been resolved by this
10591 point, so here CTX really should have complete type, unless
10592 it's a partial instantiation. */
10593 if (!(complain & tf_no_class_instantiations))
10594 ctx = complete_type (ctx);
10595 if (!COMPLETE_TYPE_P (ctx))
10597 if (complain & tf_error)
10598 cxx_incomplete_type_error (NULL_TREE, ctx);
10599 return error_mark_node;
10603 f = make_typename_type (ctx, f, typename_type,
10604 (complain & tf_error) | tf_keep_type_decl);
10605 if (f == error_mark_node)
10606 return f;
10607 if (TREE_CODE (f) == TYPE_DECL)
10609 complain |= tf_ignore_bad_quals;
10610 f = TREE_TYPE (f);
10613 if (TREE_CODE (f) != TYPENAME_TYPE)
10615 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10616 error ("%qT resolves to %qT, which is not an enumeration type",
10617 t, f);
10618 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10619 error ("%qT resolves to %qT, which is is not a class type",
10620 t, f);
10623 return cp_build_qualified_type_real
10624 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10627 case UNBOUND_CLASS_TEMPLATE:
10629 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10630 in_decl, /*entering_scope=*/1);
10631 tree name = TYPE_IDENTIFIER (t);
10632 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10634 if (ctx == error_mark_node || name == error_mark_node)
10635 return error_mark_node;
10637 if (parm_list)
10638 parm_list = tsubst_template_parms (parm_list, args, complain);
10639 return make_unbound_class_template (ctx, name, parm_list, complain);
10642 case INDIRECT_REF:
10643 case ADDR_EXPR:
10644 case CALL_EXPR:
10645 gcc_unreachable ();
10647 case ARRAY_REF:
10649 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10650 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10651 /*integral_constant_expression_p=*/false);
10652 if (e1 == error_mark_node || e2 == error_mark_node)
10653 return error_mark_node;
10655 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10658 case SCOPE_REF:
10660 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10661 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10662 if (e1 == error_mark_node || e2 == error_mark_node)
10663 return error_mark_node;
10665 return build_qualified_name (/*type=*/NULL_TREE,
10666 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10669 case TYPEOF_TYPE:
10671 tree type;
10673 ++cp_unevaluated_operand;
10674 ++c_inhibit_evaluation_warnings;
10676 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10677 complain, in_decl,
10678 /*integral_constant_expression_p=*/false);
10680 --cp_unevaluated_operand;
10681 --c_inhibit_evaluation_warnings;
10683 type = finish_typeof (type);
10684 return cp_build_qualified_type_real (type,
10685 cp_type_quals (t)
10686 | cp_type_quals (type),
10687 complain);
10690 case DECLTYPE_TYPE:
10692 tree type;
10694 ++cp_unevaluated_operand;
10695 ++c_inhibit_evaluation_warnings;
10697 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10698 complain, in_decl,
10699 /*integral_constant_expression_p=*/false);
10701 --cp_unevaluated_operand;
10702 --c_inhibit_evaluation_warnings;
10704 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10705 type = lambda_capture_field_type (type);
10706 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10707 type = lambda_return_type (type);
10708 else
10709 type = finish_decltype_type
10710 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10711 return cp_build_qualified_type_real (type,
10712 cp_type_quals (t)
10713 | cp_type_quals (type),
10714 complain);
10717 case TYPE_ARGUMENT_PACK:
10718 case NONTYPE_ARGUMENT_PACK:
10720 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
10721 tree packed_out =
10722 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10723 args,
10724 complain,
10725 in_decl);
10726 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10728 /* For template nontype argument packs, also substitute into
10729 the type. */
10730 if (code == NONTYPE_ARGUMENT_PACK)
10731 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10733 return r;
10735 break;
10737 default:
10738 sorry ("use of %qs in template", tree_code_name [(int) code]);
10739 return error_mark_node;
10743 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10744 type of the expression on the left-hand side of the "." or "->"
10745 operator. */
10747 static tree
10748 tsubst_baselink (tree baselink, tree object_type,
10749 tree args, tsubst_flags_t complain, tree in_decl)
10751 tree name;
10752 tree qualifying_scope;
10753 tree fns;
10754 tree optype;
10755 tree template_args = 0;
10756 bool template_id_p = false;
10758 /* A baselink indicates a function from a base class. Both the
10759 BASELINK_ACCESS_BINFO and the base class referenced may
10760 indicate bases of the template class, rather than the
10761 instantiated class. In addition, lookups that were not
10762 ambiguous before may be ambiguous now. Therefore, we perform
10763 the lookup again. */
10764 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10765 qualifying_scope = tsubst (qualifying_scope, args,
10766 complain, in_decl);
10767 fns = BASELINK_FUNCTIONS (baselink);
10768 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10769 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10771 template_id_p = true;
10772 template_args = TREE_OPERAND (fns, 1);
10773 fns = TREE_OPERAND (fns, 0);
10774 if (template_args)
10775 template_args = tsubst_template_args (template_args, args,
10776 complain, in_decl);
10778 name = DECL_NAME (get_first_fn (fns));
10779 if (IDENTIFIER_TYPENAME_P (name))
10780 name = mangle_conv_op_name_for_type (optype);
10781 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10782 if (!baselink)
10783 return error_mark_node;
10785 /* If lookup found a single function, mark it as used at this
10786 point. (If it lookup found multiple functions the one selected
10787 later by overload resolution will be marked as used at that
10788 point.) */
10789 if (BASELINK_P (baselink))
10790 fns = BASELINK_FUNCTIONS (baselink);
10791 if (!template_id_p && !really_overloaded_fn (fns))
10792 mark_used (OVL_CURRENT (fns));
10794 /* Add back the template arguments, if present. */
10795 if (BASELINK_P (baselink) && template_id_p)
10796 BASELINK_FUNCTIONS (baselink)
10797 = build_nt (TEMPLATE_ID_EXPR,
10798 BASELINK_FUNCTIONS (baselink),
10799 template_args);
10800 /* Update the conversion operator type. */
10801 BASELINK_OPTYPE (baselink) = optype;
10803 if (!object_type)
10804 object_type = current_class_type;
10805 return adjust_result_of_qualified_name_lookup (baselink,
10806 qualifying_scope,
10807 object_type);
10810 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10811 true if the qualified-id will be a postfix-expression in-and-of
10812 itself; false if more of the postfix-expression follows the
10813 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10814 of "&". */
10816 static tree
10817 tsubst_qualified_id (tree qualified_id, tree args,
10818 tsubst_flags_t complain, tree in_decl,
10819 bool done, bool address_p)
10821 tree expr;
10822 tree scope;
10823 tree name;
10824 bool is_template;
10825 tree template_args;
10827 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10829 /* Figure out what name to look up. */
10830 name = TREE_OPERAND (qualified_id, 1);
10831 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10833 is_template = true;
10834 template_args = TREE_OPERAND (name, 1);
10835 if (template_args)
10836 template_args = tsubst_template_args (template_args, args,
10837 complain, in_decl);
10838 name = TREE_OPERAND (name, 0);
10840 else
10842 is_template = false;
10843 template_args = NULL_TREE;
10846 /* Substitute into the qualifying scope. When there are no ARGS, we
10847 are just trying to simplify a non-dependent expression. In that
10848 case the qualifying scope may be dependent, and, in any case,
10849 substituting will not help. */
10850 scope = TREE_OPERAND (qualified_id, 0);
10851 if (args)
10853 scope = tsubst (scope, args, complain, in_decl);
10854 expr = tsubst_copy (name, args, complain, in_decl);
10856 else
10857 expr = name;
10859 if (dependent_scope_p (scope))
10860 return build_qualified_name (NULL_TREE, scope, expr,
10861 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10863 if (!BASELINK_P (name) && !DECL_P (expr))
10865 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10867 /* A BIT_NOT_EXPR is used to represent a destructor. */
10868 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10870 error ("qualifying type %qT does not match destructor name ~%qT",
10871 scope, TREE_OPERAND (expr, 0));
10872 expr = error_mark_node;
10874 else
10875 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10876 /*is_type_p=*/0, false);
10878 else
10879 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10880 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10881 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10883 if (complain & tf_error)
10885 error ("dependent-name %qE is parsed as a non-type, but "
10886 "instantiation yields a type", qualified_id);
10887 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10889 return error_mark_node;
10893 if (DECL_P (expr))
10895 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10896 scope);
10897 /* Remember that there was a reference to this entity. */
10898 mark_used (expr);
10901 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10903 if (complain & tf_error)
10904 qualified_name_lookup_error (scope,
10905 TREE_OPERAND (qualified_id, 1),
10906 expr, input_location);
10907 return error_mark_node;
10910 if (is_template)
10911 expr = lookup_template_function (expr, template_args);
10913 if (expr == error_mark_node && complain & tf_error)
10914 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10915 expr, input_location);
10916 else if (TYPE_P (scope))
10918 expr = (adjust_result_of_qualified_name_lookup
10919 (expr, scope, current_class_type));
10920 expr = (finish_qualified_id_expr
10921 (scope, expr, done, address_p,
10922 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10923 /*template_arg_p=*/false));
10926 /* Expressions do not generally have reference type. */
10927 if (TREE_CODE (expr) != SCOPE_REF
10928 /* However, if we're about to form a pointer-to-member, we just
10929 want the referenced member referenced. */
10930 && TREE_CODE (expr) != OFFSET_REF)
10931 expr = convert_from_reference (expr);
10933 return expr;
10936 /* Like tsubst, but deals with expressions. This function just replaces
10937 template parms; to finish processing the resultant expression, use
10938 tsubst_expr. */
10940 static tree
10941 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10943 enum tree_code code;
10944 tree r;
10946 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10947 return t;
10949 code = TREE_CODE (t);
10951 switch (code)
10953 case PARM_DECL:
10954 r = retrieve_local_specialization (t);
10956 if (r == NULL)
10958 tree c;
10959 /* This can happen for a parameter name used later in a function
10960 declaration (such as in a late-specified return type). Just
10961 make a dummy decl, since it's only used for its type. */
10962 gcc_assert (cp_unevaluated_operand != 0);
10963 /* We copy T because want to tsubst the PARM_DECL only,
10964 not the following PARM_DECLs that are chained to T. */
10965 c = copy_node (t);
10966 r = tsubst_decl (c, args, complain);
10967 /* Give it the template pattern as its context; its true context
10968 hasn't been instantiated yet and this is good enough for
10969 mangling. */
10970 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10973 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10974 r = ARGUMENT_PACK_SELECT_ARG (r);
10975 mark_used (r);
10976 return r;
10978 case CONST_DECL:
10980 tree enum_type;
10981 tree v;
10983 if (DECL_TEMPLATE_PARM_P (t))
10984 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10985 /* There is no need to substitute into namespace-scope
10986 enumerators. */
10987 if (DECL_NAMESPACE_SCOPE_P (t))
10988 return t;
10989 /* If ARGS is NULL, then T is known to be non-dependent. */
10990 if (args == NULL_TREE)
10991 return integral_constant_value (t);
10993 /* Unfortunately, we cannot just call lookup_name here.
10994 Consider:
10996 template <int I> int f() {
10997 enum E { a = I };
10998 struct S { void g() { E e = a; } };
11001 When we instantiate f<7>::S::g(), say, lookup_name is not
11002 clever enough to find f<7>::a. */
11003 enum_type
11004 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11005 /*entering_scope=*/0);
11007 for (v = TYPE_VALUES (enum_type);
11008 v != NULL_TREE;
11009 v = TREE_CHAIN (v))
11010 if (TREE_PURPOSE (v) == DECL_NAME (t))
11011 return TREE_VALUE (v);
11013 /* We didn't find the name. That should never happen; if
11014 name-lookup found it during preliminary parsing, we
11015 should find it again here during instantiation. */
11016 gcc_unreachable ();
11018 return t;
11020 case FIELD_DECL:
11021 if (DECL_CONTEXT (t))
11023 tree ctx;
11025 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11026 /*entering_scope=*/1);
11027 if (ctx != DECL_CONTEXT (t))
11029 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11030 if (!r)
11032 if (complain & tf_error)
11033 error ("using invalid field %qD", t);
11034 return error_mark_node;
11036 return r;
11040 return t;
11042 case VAR_DECL:
11043 case FUNCTION_DECL:
11044 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11045 || local_variable_p (t))
11046 t = tsubst (t, args, complain, in_decl);
11047 mark_used (t);
11048 return t;
11050 case BASELINK:
11051 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11053 case TEMPLATE_DECL:
11054 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11055 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11056 args, complain, in_decl);
11057 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11058 return tsubst (t, args, complain, in_decl);
11059 else if (DECL_CLASS_SCOPE_P (t)
11060 && uses_template_parms (DECL_CONTEXT (t)))
11062 /* Template template argument like the following example need
11063 special treatment:
11065 template <template <class> class TT> struct C {};
11066 template <class T> struct D {
11067 template <class U> struct E {};
11068 C<E> c; // #1
11070 D<int> d; // #2
11072 We are processing the template argument `E' in #1 for
11073 the template instantiation #2. Originally, `E' is a
11074 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11075 have to substitute this with one having context `D<int>'. */
11077 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11078 return lookup_field (context, DECL_NAME(t), 0, false);
11080 else
11081 /* Ordinary template template argument. */
11082 return t;
11084 case CAST_EXPR:
11085 case REINTERPRET_CAST_EXPR:
11086 case CONST_CAST_EXPR:
11087 case STATIC_CAST_EXPR:
11088 case DYNAMIC_CAST_EXPR:
11089 case NOP_EXPR:
11090 return build1
11091 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11092 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11094 case SIZEOF_EXPR:
11095 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11097 /* We only want to compute the number of arguments. */
11098 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11099 complain, in_decl);
11100 int len = 0;
11102 if (TREE_CODE (expanded) == TREE_VEC)
11103 len = TREE_VEC_LENGTH (expanded);
11105 if (expanded == error_mark_node)
11106 return error_mark_node;
11107 else if (PACK_EXPANSION_P (expanded)
11108 || (TREE_CODE (expanded) == TREE_VEC
11109 && len > 0
11110 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11112 if (TREE_CODE (expanded) == TREE_VEC)
11113 expanded = TREE_VEC_ELT (expanded, len - 1);
11115 if (TYPE_P (expanded))
11116 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11117 complain & tf_error);
11118 else
11119 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11120 complain & tf_error);
11122 else
11123 return build_int_cst (size_type_node, len);
11125 /* Fall through */
11127 case INDIRECT_REF:
11128 case NEGATE_EXPR:
11129 case TRUTH_NOT_EXPR:
11130 case BIT_NOT_EXPR:
11131 case ADDR_EXPR:
11132 case UNARY_PLUS_EXPR: /* Unary + */
11133 case ALIGNOF_EXPR:
11134 case ARROW_EXPR:
11135 case THROW_EXPR:
11136 case TYPEID_EXPR:
11137 case REALPART_EXPR:
11138 case IMAGPART_EXPR:
11139 return build1
11140 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11141 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11143 case COMPONENT_REF:
11145 tree object;
11146 tree name;
11148 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11149 name = TREE_OPERAND (t, 1);
11150 if (TREE_CODE (name) == BIT_NOT_EXPR)
11152 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11153 complain, in_decl);
11154 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11156 else if (TREE_CODE (name) == SCOPE_REF
11157 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11159 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11160 complain, in_decl);
11161 name = TREE_OPERAND (name, 1);
11162 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11163 complain, in_decl);
11164 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11165 name = build_qualified_name (/*type=*/NULL_TREE,
11166 base, name,
11167 /*template_p=*/false);
11169 else if (TREE_CODE (name) == BASELINK)
11170 name = tsubst_baselink (name,
11171 non_reference (TREE_TYPE (object)),
11172 args, complain,
11173 in_decl);
11174 else
11175 name = tsubst_copy (name, args, complain, in_decl);
11176 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11179 case PLUS_EXPR:
11180 case MINUS_EXPR:
11181 case MULT_EXPR:
11182 case TRUNC_DIV_EXPR:
11183 case CEIL_DIV_EXPR:
11184 case FLOOR_DIV_EXPR:
11185 case ROUND_DIV_EXPR:
11186 case EXACT_DIV_EXPR:
11187 case BIT_AND_EXPR:
11188 case BIT_IOR_EXPR:
11189 case BIT_XOR_EXPR:
11190 case TRUNC_MOD_EXPR:
11191 case FLOOR_MOD_EXPR:
11192 case TRUTH_ANDIF_EXPR:
11193 case TRUTH_ORIF_EXPR:
11194 case TRUTH_AND_EXPR:
11195 case TRUTH_OR_EXPR:
11196 case RSHIFT_EXPR:
11197 case LSHIFT_EXPR:
11198 case RROTATE_EXPR:
11199 case LROTATE_EXPR:
11200 case EQ_EXPR:
11201 case NE_EXPR:
11202 case MAX_EXPR:
11203 case MIN_EXPR:
11204 case LE_EXPR:
11205 case GE_EXPR:
11206 case LT_EXPR:
11207 case GT_EXPR:
11208 case COMPOUND_EXPR:
11209 case DOTSTAR_EXPR:
11210 case MEMBER_REF:
11211 case PREDECREMENT_EXPR:
11212 case PREINCREMENT_EXPR:
11213 case POSTDECREMENT_EXPR:
11214 case POSTINCREMENT_EXPR:
11215 return build_nt
11216 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11217 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11219 case SCOPE_REF:
11220 return build_qualified_name (/*type=*/NULL_TREE,
11221 tsubst_copy (TREE_OPERAND (t, 0),
11222 args, complain, in_decl),
11223 tsubst_copy (TREE_OPERAND (t, 1),
11224 args, complain, in_decl),
11225 QUALIFIED_NAME_IS_TEMPLATE (t));
11227 case ARRAY_REF:
11228 return build_nt
11229 (ARRAY_REF,
11230 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11231 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11232 NULL_TREE, NULL_TREE);
11234 case CALL_EXPR:
11236 int n = VL_EXP_OPERAND_LENGTH (t);
11237 tree result = build_vl_exp (CALL_EXPR, n);
11238 int i;
11239 for (i = 0; i < n; i++)
11240 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11241 complain, in_decl);
11242 return result;
11245 case COND_EXPR:
11246 case MODOP_EXPR:
11247 case PSEUDO_DTOR_EXPR:
11249 r = build_nt
11250 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11251 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11252 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11253 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11254 return r;
11257 case NEW_EXPR:
11259 r = build_nt
11260 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11261 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11262 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11263 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11264 return r;
11267 case DELETE_EXPR:
11269 r = build_nt
11270 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11271 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11272 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11273 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11274 return r;
11277 case TEMPLATE_ID_EXPR:
11279 /* Substituted template arguments */
11280 tree fn = TREE_OPERAND (t, 0);
11281 tree targs = TREE_OPERAND (t, 1);
11283 fn = tsubst_copy (fn, args, complain, in_decl);
11284 if (targs)
11285 targs = tsubst_template_args (targs, args, complain, in_decl);
11287 return lookup_template_function (fn, targs);
11290 case TREE_LIST:
11292 tree purpose, value, chain;
11294 if (t == void_list_node)
11295 return t;
11297 purpose = TREE_PURPOSE (t);
11298 if (purpose)
11299 purpose = tsubst_copy (purpose, args, complain, in_decl);
11300 value = TREE_VALUE (t);
11301 if (value)
11302 value = tsubst_copy (value, args, complain, in_decl);
11303 chain = TREE_CHAIN (t);
11304 if (chain && chain != void_type_node)
11305 chain = tsubst_copy (chain, args, complain, in_decl);
11306 if (purpose == TREE_PURPOSE (t)
11307 && value == TREE_VALUE (t)
11308 && chain == TREE_CHAIN (t))
11309 return t;
11310 return tree_cons (purpose, value, chain);
11313 case RECORD_TYPE:
11314 case UNION_TYPE:
11315 case ENUMERAL_TYPE:
11316 case INTEGER_TYPE:
11317 case TEMPLATE_TYPE_PARM:
11318 case TEMPLATE_TEMPLATE_PARM:
11319 case BOUND_TEMPLATE_TEMPLATE_PARM:
11320 case TEMPLATE_PARM_INDEX:
11321 case POINTER_TYPE:
11322 case REFERENCE_TYPE:
11323 case OFFSET_TYPE:
11324 case FUNCTION_TYPE:
11325 case METHOD_TYPE:
11326 case ARRAY_TYPE:
11327 case TYPENAME_TYPE:
11328 case UNBOUND_CLASS_TEMPLATE:
11329 case TYPEOF_TYPE:
11330 case DECLTYPE_TYPE:
11331 case TYPE_DECL:
11332 return tsubst (t, args, complain, in_decl);
11334 case IDENTIFIER_NODE:
11335 if (IDENTIFIER_TYPENAME_P (t))
11337 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11338 return mangle_conv_op_name_for_type (new_type);
11340 else
11341 return t;
11343 case CONSTRUCTOR:
11344 /* This is handled by tsubst_copy_and_build. */
11345 gcc_unreachable ();
11347 case VA_ARG_EXPR:
11348 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11349 in_decl),
11350 tsubst (TREE_TYPE (t), args, complain, in_decl));
11352 case CLEANUP_POINT_EXPR:
11353 /* We shouldn't have built any of these during initial template
11354 generation. Instead, they should be built during instantiation
11355 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11356 gcc_unreachable ();
11358 case OFFSET_REF:
11359 r = build2
11360 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11361 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11362 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11363 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11364 mark_used (TREE_OPERAND (r, 1));
11365 return r;
11367 case EXPR_PACK_EXPANSION:
11368 error ("invalid use of pack expansion expression");
11369 return error_mark_node;
11371 case NONTYPE_ARGUMENT_PACK:
11372 error ("use %<...%> to expand argument pack");
11373 return error_mark_node;
11375 default:
11376 return t;
11380 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11382 static tree
11383 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11384 tree in_decl)
11386 tree new_clauses = NULL, nc, oc;
11388 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11390 nc = copy_node (oc);
11391 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11392 new_clauses = nc;
11394 switch (OMP_CLAUSE_CODE (nc))
11396 case OMP_CLAUSE_LASTPRIVATE:
11397 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11399 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11400 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11401 in_decl, /*integral_constant_expression_p=*/false);
11402 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11403 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11405 /* FALLTHRU */
11406 case OMP_CLAUSE_PRIVATE:
11407 case OMP_CLAUSE_SHARED:
11408 case OMP_CLAUSE_FIRSTPRIVATE:
11409 case OMP_CLAUSE_REDUCTION:
11410 case OMP_CLAUSE_COPYIN:
11411 case OMP_CLAUSE_COPYPRIVATE:
11412 case OMP_CLAUSE_IF:
11413 case OMP_CLAUSE_NUM_THREADS:
11414 case OMP_CLAUSE_SCHEDULE:
11415 case OMP_CLAUSE_COLLAPSE:
11416 OMP_CLAUSE_OPERAND (nc, 0)
11417 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11418 in_decl, /*integral_constant_expression_p=*/false);
11419 break;
11420 case OMP_CLAUSE_NOWAIT:
11421 case OMP_CLAUSE_ORDERED:
11422 case OMP_CLAUSE_DEFAULT:
11423 case OMP_CLAUSE_UNTIED:
11424 break;
11425 default:
11426 gcc_unreachable ();
11430 return finish_omp_clauses (nreverse (new_clauses));
11433 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11435 static tree
11436 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11437 tree in_decl)
11439 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11441 tree purpose, value, chain;
11443 if (t == NULL)
11444 return t;
11446 if (TREE_CODE (t) != TREE_LIST)
11447 return tsubst_copy_and_build (t, args, complain, in_decl,
11448 /*function_p=*/false,
11449 /*integral_constant_expression_p=*/false);
11451 if (t == void_list_node)
11452 return t;
11454 purpose = TREE_PURPOSE (t);
11455 if (purpose)
11456 purpose = RECUR (purpose);
11457 value = TREE_VALUE (t);
11458 if (value && TREE_CODE (value) != LABEL_DECL)
11459 value = RECUR (value);
11460 chain = TREE_CHAIN (t);
11461 if (chain && chain != void_type_node)
11462 chain = RECUR (chain);
11463 return tree_cons (purpose, value, chain);
11464 #undef RECUR
11467 /* Substitute one OMP_FOR iterator. */
11469 static void
11470 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11471 tree condv, tree incrv, tree *clauses,
11472 tree args, tsubst_flags_t complain, tree in_decl,
11473 bool integral_constant_expression_p)
11475 #define RECUR(NODE) \
11476 tsubst_expr ((NODE), args, complain, in_decl, \
11477 integral_constant_expression_p)
11478 tree decl, init, cond, incr, auto_node;
11480 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11481 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11482 decl = RECUR (TREE_OPERAND (init, 0));
11483 init = TREE_OPERAND (init, 1);
11484 auto_node = type_uses_auto (TREE_TYPE (decl));
11485 if (auto_node && init)
11487 tree init_expr = init;
11488 if (TREE_CODE (init_expr) == DECL_EXPR)
11489 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11490 init_expr = RECUR (init_expr);
11491 TREE_TYPE (decl)
11492 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11494 gcc_assert (!type_dependent_expression_p (decl));
11496 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11498 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11499 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11500 if (TREE_CODE (incr) == MODIFY_EXPR)
11501 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11502 RECUR (TREE_OPERAND (incr, 1)),
11503 complain);
11504 else
11505 incr = RECUR (incr);
11506 TREE_VEC_ELT (declv, i) = decl;
11507 TREE_VEC_ELT (initv, i) = init;
11508 TREE_VEC_ELT (condv, i) = cond;
11509 TREE_VEC_ELT (incrv, i) = incr;
11510 return;
11513 if (init && TREE_CODE (init) != DECL_EXPR)
11515 tree c;
11516 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11518 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11519 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11520 && OMP_CLAUSE_DECL (c) == decl)
11521 break;
11522 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11523 && OMP_CLAUSE_DECL (c) == decl)
11524 error ("iteration variable %qD should not be firstprivate", decl);
11525 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11526 && OMP_CLAUSE_DECL (c) == decl)
11527 error ("iteration variable %qD should not be reduction", decl);
11529 if (c == NULL)
11531 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11532 OMP_CLAUSE_DECL (c) = decl;
11533 c = finish_omp_clauses (c);
11534 if (c)
11536 OMP_CLAUSE_CHAIN (c) = *clauses;
11537 *clauses = c;
11541 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11542 if (COMPARISON_CLASS_P (cond))
11543 cond = build2 (TREE_CODE (cond), boolean_type_node,
11544 RECUR (TREE_OPERAND (cond, 0)),
11545 RECUR (TREE_OPERAND (cond, 1)));
11546 else
11547 cond = RECUR (cond);
11548 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11549 switch (TREE_CODE (incr))
11551 case PREINCREMENT_EXPR:
11552 case PREDECREMENT_EXPR:
11553 case POSTINCREMENT_EXPR:
11554 case POSTDECREMENT_EXPR:
11555 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11556 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11557 break;
11558 case MODIFY_EXPR:
11559 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11560 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11562 tree rhs = TREE_OPERAND (incr, 1);
11563 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11564 RECUR (TREE_OPERAND (incr, 0)),
11565 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11566 RECUR (TREE_OPERAND (rhs, 0)),
11567 RECUR (TREE_OPERAND (rhs, 1))));
11569 else
11570 incr = RECUR (incr);
11571 break;
11572 case MODOP_EXPR:
11573 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11574 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11576 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11577 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11578 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11579 TREE_TYPE (decl), lhs,
11580 RECUR (TREE_OPERAND (incr, 2))));
11582 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11583 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11584 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11586 tree rhs = TREE_OPERAND (incr, 2);
11587 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11588 RECUR (TREE_OPERAND (incr, 0)),
11589 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11590 RECUR (TREE_OPERAND (rhs, 0)),
11591 RECUR (TREE_OPERAND (rhs, 1))));
11593 else
11594 incr = RECUR (incr);
11595 break;
11596 default:
11597 incr = RECUR (incr);
11598 break;
11601 TREE_VEC_ELT (declv, i) = decl;
11602 TREE_VEC_ELT (initv, i) = init;
11603 TREE_VEC_ELT (condv, i) = cond;
11604 TREE_VEC_ELT (incrv, i) = incr;
11605 #undef RECUR
11608 /* Like tsubst_copy for expressions, etc. but also does semantic
11609 processing. */
11611 static tree
11612 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11613 bool integral_constant_expression_p)
11615 #define RECUR(NODE) \
11616 tsubst_expr ((NODE), args, complain, in_decl, \
11617 integral_constant_expression_p)
11619 tree stmt, tmp;
11621 if (t == NULL_TREE || t == error_mark_node)
11622 return t;
11624 if (EXPR_HAS_LOCATION (t))
11625 input_location = EXPR_LOCATION (t);
11626 if (STATEMENT_CODE_P (TREE_CODE (t)))
11627 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11629 switch (TREE_CODE (t))
11631 case STATEMENT_LIST:
11633 tree_stmt_iterator i;
11634 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11635 RECUR (tsi_stmt (i));
11636 break;
11639 case CTOR_INITIALIZER:
11640 finish_mem_initializers (tsubst_initializer_list
11641 (TREE_OPERAND (t, 0), args));
11642 break;
11644 case RETURN_EXPR:
11645 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11646 break;
11648 case EXPR_STMT:
11649 tmp = RECUR (EXPR_STMT_EXPR (t));
11650 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11651 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11652 else
11653 finish_expr_stmt (tmp);
11654 break;
11656 case USING_STMT:
11657 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11658 break;
11660 case DECL_EXPR:
11662 tree decl;
11663 tree init;
11665 decl = DECL_EXPR_DECL (t);
11666 if (TREE_CODE (decl) == LABEL_DECL)
11667 finish_label_decl (DECL_NAME (decl));
11668 else if (TREE_CODE (decl) == USING_DECL)
11670 tree scope = USING_DECL_SCOPE (decl);
11671 tree name = DECL_NAME (decl);
11672 tree decl;
11674 scope = RECUR (scope);
11675 decl = lookup_qualified_name (scope, name,
11676 /*is_type_p=*/false,
11677 /*complain=*/false);
11678 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11679 qualified_name_lookup_error (scope, name, decl, input_location);
11680 else
11681 do_local_using_decl (decl, scope, name);
11683 else
11685 init = DECL_INITIAL (decl);
11686 decl = tsubst (decl, args, complain, in_decl);
11687 if (decl != error_mark_node)
11689 /* By marking the declaration as instantiated, we avoid
11690 trying to instantiate it. Since instantiate_decl can't
11691 handle local variables, and since we've already done
11692 all that needs to be done, that's the right thing to
11693 do. */
11694 if (TREE_CODE (decl) == VAR_DECL)
11695 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11696 if (TREE_CODE (decl) == VAR_DECL
11697 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11698 /* Anonymous aggregates are a special case. */
11699 finish_anon_union (decl);
11700 else
11702 maybe_push_decl (decl);
11703 if (TREE_CODE (decl) == VAR_DECL
11704 && DECL_PRETTY_FUNCTION_P (decl))
11706 /* For __PRETTY_FUNCTION__ we have to adjust the
11707 initializer. */
11708 const char *const name
11709 = cxx_printable_name (current_function_decl, 2);
11710 init = cp_fname_init (name, &TREE_TYPE (decl));
11712 else
11714 tree t = RECUR (init);
11716 if (init && !t)
11718 /* If we had an initializer but it
11719 instantiated to nothing,
11720 value-initialize the object. This will
11721 only occur when the initializer was a
11722 pack expansion where the parameter packs
11723 used in that expansion were of length
11724 zero. */
11725 init = build_value_init (TREE_TYPE (decl),
11726 complain);
11727 if (TREE_CODE (init) == AGGR_INIT_EXPR)
11728 init = get_target_expr (init);
11730 else
11731 init = t;
11734 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11739 /* A DECL_EXPR can also be used as an expression, in the condition
11740 clause of an if/for/while construct. */
11741 return decl;
11744 case FOR_STMT:
11745 stmt = begin_for_stmt ();
11746 RECUR (FOR_INIT_STMT (t));
11747 finish_for_init_stmt (stmt);
11748 tmp = RECUR (FOR_COND (t));
11749 finish_for_cond (tmp, stmt);
11750 tmp = RECUR (FOR_EXPR (t));
11751 finish_for_expr (tmp, stmt);
11752 RECUR (FOR_BODY (t));
11753 finish_for_stmt (stmt);
11754 break;
11756 case RANGE_FOR_STMT:
11758 tree decl, expr;
11759 stmt = begin_for_stmt ();
11760 decl = RANGE_FOR_DECL (t);
11761 decl = tsubst (decl, args, complain, in_decl);
11762 maybe_push_decl (decl);
11763 expr = RECUR (RANGE_FOR_EXPR (t));
11764 stmt = cp_convert_range_for (stmt, decl, expr);
11765 RECUR (RANGE_FOR_BODY (t));
11766 finish_for_stmt (stmt);
11768 break;
11770 case WHILE_STMT:
11771 stmt = begin_while_stmt ();
11772 tmp = RECUR (WHILE_COND (t));
11773 finish_while_stmt_cond (tmp, stmt);
11774 RECUR (WHILE_BODY (t));
11775 finish_while_stmt (stmt);
11776 break;
11778 case DO_STMT:
11779 stmt = begin_do_stmt ();
11780 RECUR (DO_BODY (t));
11781 finish_do_body (stmt);
11782 tmp = RECUR (DO_COND (t));
11783 finish_do_stmt (tmp, stmt);
11784 break;
11786 case IF_STMT:
11787 stmt = begin_if_stmt ();
11788 tmp = RECUR (IF_COND (t));
11789 finish_if_stmt_cond (tmp, stmt);
11790 RECUR (THEN_CLAUSE (t));
11791 finish_then_clause (stmt);
11793 if (ELSE_CLAUSE (t))
11795 begin_else_clause (stmt);
11796 RECUR (ELSE_CLAUSE (t));
11797 finish_else_clause (stmt);
11800 finish_if_stmt (stmt);
11801 break;
11803 case BIND_EXPR:
11804 if (BIND_EXPR_BODY_BLOCK (t))
11805 stmt = begin_function_body ();
11806 else
11807 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11808 ? BCS_TRY_BLOCK : 0);
11810 RECUR (BIND_EXPR_BODY (t));
11812 if (BIND_EXPR_BODY_BLOCK (t))
11813 finish_function_body (stmt);
11814 else
11815 finish_compound_stmt (stmt);
11816 break;
11818 case BREAK_STMT:
11819 finish_break_stmt ();
11820 break;
11822 case CONTINUE_STMT:
11823 finish_continue_stmt ();
11824 break;
11826 case SWITCH_STMT:
11827 stmt = begin_switch_stmt ();
11828 tmp = RECUR (SWITCH_STMT_COND (t));
11829 finish_switch_cond (tmp, stmt);
11830 RECUR (SWITCH_STMT_BODY (t));
11831 finish_switch_stmt (stmt);
11832 break;
11834 case CASE_LABEL_EXPR:
11835 finish_case_label (EXPR_LOCATION (t),
11836 RECUR (CASE_LOW (t)),
11837 RECUR (CASE_HIGH (t)));
11838 break;
11840 case LABEL_EXPR:
11842 tree decl = LABEL_EXPR_LABEL (t);
11843 tree label;
11845 label = finish_label_stmt (DECL_NAME (decl));
11846 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11847 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11849 break;
11851 case GOTO_EXPR:
11852 tmp = GOTO_DESTINATION (t);
11853 if (TREE_CODE (tmp) != LABEL_DECL)
11854 /* Computed goto's must be tsubst'd into. On the other hand,
11855 non-computed gotos must not be; the identifier in question
11856 will have no binding. */
11857 tmp = RECUR (tmp);
11858 else
11859 tmp = DECL_NAME (tmp);
11860 finish_goto_stmt (tmp);
11861 break;
11863 case ASM_EXPR:
11864 tmp = finish_asm_stmt
11865 (ASM_VOLATILE_P (t),
11866 RECUR (ASM_STRING (t)),
11867 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11868 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11869 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11870 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11872 tree asm_expr = tmp;
11873 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11874 asm_expr = TREE_OPERAND (asm_expr, 0);
11875 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11877 break;
11879 case TRY_BLOCK:
11880 if (CLEANUP_P (t))
11882 stmt = begin_try_block ();
11883 RECUR (TRY_STMTS (t));
11884 finish_cleanup_try_block (stmt);
11885 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11887 else
11889 tree compound_stmt = NULL_TREE;
11891 if (FN_TRY_BLOCK_P (t))
11892 stmt = begin_function_try_block (&compound_stmt);
11893 else
11894 stmt = begin_try_block ();
11896 RECUR (TRY_STMTS (t));
11898 if (FN_TRY_BLOCK_P (t))
11899 finish_function_try_block (stmt);
11900 else
11901 finish_try_block (stmt);
11903 RECUR (TRY_HANDLERS (t));
11904 if (FN_TRY_BLOCK_P (t))
11905 finish_function_handler_sequence (stmt, compound_stmt);
11906 else
11907 finish_handler_sequence (stmt);
11909 break;
11911 case HANDLER:
11913 tree decl = HANDLER_PARMS (t);
11915 if (decl)
11917 decl = tsubst (decl, args, complain, in_decl);
11918 /* Prevent instantiate_decl from trying to instantiate
11919 this variable. We've already done all that needs to be
11920 done. */
11921 if (decl != error_mark_node)
11922 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11924 stmt = begin_handler ();
11925 finish_handler_parms (decl, stmt);
11926 RECUR (HANDLER_BODY (t));
11927 finish_handler (stmt);
11929 break;
11931 case TAG_DEFN:
11932 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11933 break;
11935 case STATIC_ASSERT:
11937 tree condition =
11938 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11939 args,
11940 complain, in_decl,
11941 /*integral_constant_expression_p=*/true);
11942 finish_static_assert (condition,
11943 STATIC_ASSERT_MESSAGE (t),
11944 STATIC_ASSERT_SOURCE_LOCATION (t),
11945 /*member_p=*/false);
11947 break;
11949 case OMP_PARALLEL:
11950 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11951 args, complain, in_decl);
11952 stmt = begin_omp_parallel ();
11953 RECUR (OMP_PARALLEL_BODY (t));
11954 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11955 = OMP_PARALLEL_COMBINED (t);
11956 break;
11958 case OMP_TASK:
11959 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11960 args, complain, in_decl);
11961 stmt = begin_omp_task ();
11962 RECUR (OMP_TASK_BODY (t));
11963 finish_omp_task (tmp, stmt);
11964 break;
11966 case OMP_FOR:
11968 tree clauses, body, pre_body;
11969 tree declv, initv, condv, incrv;
11970 int i;
11972 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11973 args, complain, in_decl);
11974 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11975 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11976 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11977 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11979 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11980 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11981 &clauses, args, complain, in_decl,
11982 integral_constant_expression_p);
11984 stmt = begin_omp_structured_block ();
11986 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11987 if (TREE_VEC_ELT (initv, i) == NULL
11988 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11989 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11990 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11992 tree init = RECUR (TREE_VEC_ELT (initv, i));
11993 gcc_assert (init == TREE_VEC_ELT (declv, i));
11994 TREE_VEC_ELT (initv, i) = NULL_TREE;
11996 else
11998 tree decl_expr = TREE_VEC_ELT (initv, i);
11999 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12000 gcc_assert (init != NULL);
12001 TREE_VEC_ELT (initv, i) = RECUR (init);
12002 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12003 RECUR (decl_expr);
12004 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12007 pre_body = push_stmt_list ();
12008 RECUR (OMP_FOR_PRE_BODY (t));
12009 pre_body = pop_stmt_list (pre_body);
12011 body = push_stmt_list ();
12012 RECUR (OMP_FOR_BODY (t));
12013 body = pop_stmt_list (body);
12015 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12016 body, pre_body, clauses);
12018 add_stmt (finish_omp_structured_block (stmt));
12020 break;
12022 case OMP_SECTIONS:
12023 case OMP_SINGLE:
12024 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12025 stmt = push_stmt_list ();
12026 RECUR (OMP_BODY (t));
12027 stmt = pop_stmt_list (stmt);
12029 t = copy_node (t);
12030 OMP_BODY (t) = stmt;
12031 OMP_CLAUSES (t) = tmp;
12032 add_stmt (t);
12033 break;
12035 case OMP_SECTION:
12036 case OMP_CRITICAL:
12037 case OMP_MASTER:
12038 case OMP_ORDERED:
12039 stmt = push_stmt_list ();
12040 RECUR (OMP_BODY (t));
12041 stmt = pop_stmt_list (stmt);
12043 t = copy_node (t);
12044 OMP_BODY (t) = stmt;
12045 add_stmt (t);
12046 break;
12048 case OMP_ATOMIC:
12049 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12051 tree op1 = TREE_OPERAND (t, 1);
12052 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12053 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12054 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12056 break;
12058 case EXPR_PACK_EXPANSION:
12059 error ("invalid use of pack expansion expression");
12060 return error_mark_node;
12062 case NONTYPE_ARGUMENT_PACK:
12063 error ("use %<...%> to expand argument pack");
12064 return error_mark_node;
12066 default:
12067 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12069 return tsubst_copy_and_build (t, args, complain, in_decl,
12070 /*function_p=*/false,
12071 integral_constant_expression_p);
12074 return NULL_TREE;
12075 #undef RECUR
12078 /* T is a postfix-expression that is not being used in a function
12079 call. Return the substituted version of T. */
12081 static tree
12082 tsubst_non_call_postfix_expression (tree t, tree args,
12083 tsubst_flags_t complain,
12084 tree in_decl)
12086 if (TREE_CODE (t) == SCOPE_REF)
12087 t = tsubst_qualified_id (t, args, complain, in_decl,
12088 /*done=*/false, /*address_p=*/false);
12089 else
12090 t = tsubst_copy_and_build (t, args, complain, in_decl,
12091 /*function_p=*/false,
12092 /*integral_constant_expression_p=*/false);
12094 return t;
12097 /* Like tsubst but deals with expressions and performs semantic
12098 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12100 tree
12101 tsubst_copy_and_build (tree t,
12102 tree args,
12103 tsubst_flags_t complain,
12104 tree in_decl,
12105 bool function_p,
12106 bool integral_constant_expression_p)
12108 #define RECUR(NODE) \
12109 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12110 /*function_p=*/false, \
12111 integral_constant_expression_p)
12113 tree op1;
12115 if (t == NULL_TREE || t == error_mark_node)
12116 return t;
12118 switch (TREE_CODE (t))
12120 case USING_DECL:
12121 t = DECL_NAME (t);
12122 /* Fall through. */
12123 case IDENTIFIER_NODE:
12125 tree decl;
12126 cp_id_kind idk;
12127 bool non_integral_constant_expression_p;
12128 const char *error_msg;
12130 if (IDENTIFIER_TYPENAME_P (t))
12132 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12133 t = mangle_conv_op_name_for_type (new_type);
12136 /* Look up the name. */
12137 decl = lookup_name (t);
12139 /* By convention, expressions use ERROR_MARK_NODE to indicate
12140 failure, not NULL_TREE. */
12141 if (decl == NULL_TREE)
12142 decl = error_mark_node;
12144 decl = finish_id_expression (t, decl, NULL_TREE,
12145 &idk,
12146 integral_constant_expression_p,
12147 /*allow_non_integral_constant_expression_p=*/false,
12148 &non_integral_constant_expression_p,
12149 /*template_p=*/false,
12150 /*done=*/true,
12151 /*address_p=*/false,
12152 /*template_arg_p=*/false,
12153 &error_msg,
12154 input_location);
12155 if (error_msg)
12156 error (error_msg);
12157 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12158 decl = unqualified_name_lookup_error (decl);
12159 return decl;
12162 case TEMPLATE_ID_EXPR:
12164 tree object;
12165 tree templ = RECUR (TREE_OPERAND (t, 0));
12166 tree targs = TREE_OPERAND (t, 1);
12168 if (targs)
12169 targs = tsubst_template_args (targs, args, complain, in_decl);
12171 if (TREE_CODE (templ) == COMPONENT_REF)
12173 object = TREE_OPERAND (templ, 0);
12174 templ = TREE_OPERAND (templ, 1);
12176 else
12177 object = NULL_TREE;
12178 templ = lookup_template_function (templ, targs);
12180 if (object)
12181 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12182 object, templ, NULL_TREE);
12183 else
12184 return baselink_for_fns (templ);
12187 case INDIRECT_REF:
12189 tree r = RECUR (TREE_OPERAND (t, 0));
12191 if (REFERENCE_REF_P (t))
12193 /* A type conversion to reference type will be enclosed in
12194 such an indirect ref, but the substitution of the cast
12195 will have also added such an indirect ref. */
12196 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12197 r = convert_from_reference (r);
12199 else
12200 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12201 return r;
12204 case NOP_EXPR:
12205 return build_nop
12206 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12207 RECUR (TREE_OPERAND (t, 0)));
12209 case CAST_EXPR:
12210 case REINTERPRET_CAST_EXPR:
12211 case CONST_CAST_EXPR:
12212 case DYNAMIC_CAST_EXPR:
12213 case STATIC_CAST_EXPR:
12215 tree type;
12216 tree op;
12218 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12219 if (integral_constant_expression_p
12220 && !cast_valid_in_integral_constant_expression_p (type))
12222 if (complain & tf_error)
12223 error ("a cast to a type other than an integral or "
12224 "enumeration type cannot appear in a constant-expression");
12225 return error_mark_node;
12228 op = RECUR (TREE_OPERAND (t, 0));
12230 switch (TREE_CODE (t))
12232 case CAST_EXPR:
12233 return build_functional_cast (type, op, complain);
12234 case REINTERPRET_CAST_EXPR:
12235 return build_reinterpret_cast (type, op, complain);
12236 case CONST_CAST_EXPR:
12237 return build_const_cast (type, op, complain);
12238 case DYNAMIC_CAST_EXPR:
12239 return build_dynamic_cast (type, op, complain);
12240 case STATIC_CAST_EXPR:
12241 return build_static_cast (type, op, complain);
12242 default:
12243 gcc_unreachable ();
12247 case POSTDECREMENT_EXPR:
12248 case POSTINCREMENT_EXPR:
12249 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12250 args, complain, in_decl);
12251 return build_x_unary_op (TREE_CODE (t), op1, complain);
12253 case PREDECREMENT_EXPR:
12254 case PREINCREMENT_EXPR:
12255 case NEGATE_EXPR:
12256 case BIT_NOT_EXPR:
12257 case ABS_EXPR:
12258 case TRUTH_NOT_EXPR:
12259 case UNARY_PLUS_EXPR: /* Unary + */
12260 case REALPART_EXPR:
12261 case IMAGPART_EXPR:
12262 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12263 complain);
12265 case ADDR_EXPR:
12266 op1 = TREE_OPERAND (t, 0);
12267 if (TREE_CODE (op1) == SCOPE_REF)
12268 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12269 /*done=*/true, /*address_p=*/true);
12270 else
12271 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12272 in_decl);
12273 if (TREE_CODE (op1) == LABEL_DECL)
12274 return finish_label_address_expr (DECL_NAME (op1),
12275 EXPR_LOCATION (op1));
12276 return build_x_unary_op (ADDR_EXPR, op1, complain);
12278 case PLUS_EXPR:
12279 case MINUS_EXPR:
12280 case MULT_EXPR:
12281 case TRUNC_DIV_EXPR:
12282 case CEIL_DIV_EXPR:
12283 case FLOOR_DIV_EXPR:
12284 case ROUND_DIV_EXPR:
12285 case EXACT_DIV_EXPR:
12286 case BIT_AND_EXPR:
12287 case BIT_IOR_EXPR:
12288 case BIT_XOR_EXPR:
12289 case TRUNC_MOD_EXPR:
12290 case FLOOR_MOD_EXPR:
12291 case TRUTH_ANDIF_EXPR:
12292 case TRUTH_ORIF_EXPR:
12293 case TRUTH_AND_EXPR:
12294 case TRUTH_OR_EXPR:
12295 case RSHIFT_EXPR:
12296 case LSHIFT_EXPR:
12297 case RROTATE_EXPR:
12298 case LROTATE_EXPR:
12299 case EQ_EXPR:
12300 case NE_EXPR:
12301 case MAX_EXPR:
12302 case MIN_EXPR:
12303 case LE_EXPR:
12304 case GE_EXPR:
12305 case LT_EXPR:
12306 case GT_EXPR:
12307 case MEMBER_REF:
12308 case DOTSTAR_EXPR:
12309 return build_x_binary_op
12310 (TREE_CODE (t),
12311 RECUR (TREE_OPERAND (t, 0)),
12312 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12313 ? ERROR_MARK
12314 : TREE_CODE (TREE_OPERAND (t, 0))),
12315 RECUR (TREE_OPERAND (t, 1)),
12316 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12317 ? ERROR_MARK
12318 : TREE_CODE (TREE_OPERAND (t, 1))),
12319 /*overloaded_p=*/NULL,
12320 complain);
12322 case SCOPE_REF:
12323 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12324 /*address_p=*/false);
12325 case ARRAY_REF:
12326 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12327 args, complain, in_decl);
12328 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12330 case SIZEOF_EXPR:
12331 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12332 return tsubst_copy (t, args, complain, in_decl);
12333 /* Fall through */
12335 case ALIGNOF_EXPR:
12336 op1 = TREE_OPERAND (t, 0);
12337 if (!args)
12339 /* When there are no ARGS, we are trying to evaluate a
12340 non-dependent expression from the parser. Trying to do
12341 the substitutions may not work. */
12342 if (!TYPE_P (op1))
12343 op1 = TREE_TYPE (op1);
12345 else
12347 ++cp_unevaluated_operand;
12348 ++c_inhibit_evaluation_warnings;
12349 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12350 /*function_p=*/false,
12351 /*integral_constant_expression_p=*/false);
12352 --cp_unevaluated_operand;
12353 --c_inhibit_evaluation_warnings;
12355 if (TYPE_P (op1))
12356 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12357 complain & tf_error);
12358 else
12359 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12360 complain & tf_error);
12362 case NOEXCEPT_EXPR:
12363 op1 = TREE_OPERAND (t, 0);
12364 ++cp_unevaluated_operand;
12365 ++c_inhibit_evaluation_warnings;
12366 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12367 /*function_p=*/false,
12368 /*integral_constant_expression_p=*/false);
12369 --cp_unevaluated_operand;
12370 --c_inhibit_evaluation_warnings;
12371 return finish_noexcept_expr (op1, complain);
12373 case MODOP_EXPR:
12375 tree r = build_x_modify_expr
12376 (RECUR (TREE_OPERAND (t, 0)),
12377 TREE_CODE (TREE_OPERAND (t, 1)),
12378 RECUR (TREE_OPERAND (t, 2)),
12379 complain);
12380 /* TREE_NO_WARNING must be set if either the expression was
12381 parenthesized or it uses an operator such as >>= rather
12382 than plain assignment. In the former case, it was already
12383 set and must be copied. In the latter case,
12384 build_x_modify_expr sets it and it must not be reset
12385 here. */
12386 if (TREE_NO_WARNING (t))
12387 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12388 return r;
12391 case ARROW_EXPR:
12392 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12393 args, complain, in_decl);
12394 /* Remember that there was a reference to this entity. */
12395 if (DECL_P (op1))
12396 mark_used (op1);
12397 return build_x_arrow (op1);
12399 case NEW_EXPR:
12401 tree placement = RECUR (TREE_OPERAND (t, 0));
12402 tree init = RECUR (TREE_OPERAND (t, 3));
12403 VEC(tree,gc) *placement_vec;
12404 VEC(tree,gc) *init_vec;
12405 tree ret;
12407 if (placement == NULL_TREE)
12408 placement_vec = NULL;
12409 else
12411 placement_vec = make_tree_vector ();
12412 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12413 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12416 /* If there was an initializer in the original tree, but it
12417 instantiated to an empty list, then we should pass a
12418 non-NULL empty vector to tell build_new that it was an
12419 empty initializer() rather than no initializer. This can
12420 only happen when the initializer is a pack expansion whose
12421 parameter packs are of length zero. */
12422 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12423 init_vec = NULL;
12424 else
12426 init_vec = make_tree_vector ();
12427 if (init == void_zero_node)
12428 gcc_assert (init_vec != NULL);
12429 else
12431 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12432 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12436 ret = build_new (&placement_vec,
12437 RECUR (TREE_OPERAND (t, 1)),
12438 RECUR (TREE_OPERAND (t, 2)),
12439 &init_vec,
12440 NEW_EXPR_USE_GLOBAL (t),
12441 complain);
12443 if (placement_vec != NULL)
12444 release_tree_vector (placement_vec);
12445 if (init_vec != NULL)
12446 release_tree_vector (init_vec);
12448 return ret;
12451 case DELETE_EXPR:
12452 return delete_sanity
12453 (RECUR (TREE_OPERAND (t, 0)),
12454 RECUR (TREE_OPERAND (t, 1)),
12455 DELETE_EXPR_USE_VEC (t),
12456 DELETE_EXPR_USE_GLOBAL (t));
12458 case COMPOUND_EXPR:
12459 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12460 RECUR (TREE_OPERAND (t, 1)),
12461 complain);
12463 case CALL_EXPR:
12465 tree function;
12466 VEC(tree,gc) *call_args;
12467 unsigned int nargs, i;
12468 bool qualified_p;
12469 bool koenig_p;
12470 tree ret;
12472 function = CALL_EXPR_FN (t);
12473 /* When we parsed the expression, we determined whether or
12474 not Koenig lookup should be performed. */
12475 koenig_p = KOENIG_LOOKUP_P (t);
12476 if (TREE_CODE (function) == SCOPE_REF)
12478 qualified_p = true;
12479 function = tsubst_qualified_id (function, args, complain, in_decl,
12480 /*done=*/false,
12481 /*address_p=*/false);
12483 else
12485 if (TREE_CODE (function) == COMPONENT_REF)
12487 tree op = TREE_OPERAND (function, 1);
12489 qualified_p = (TREE_CODE (op) == SCOPE_REF
12490 || (BASELINK_P (op)
12491 && BASELINK_QUALIFIED_P (op)));
12493 else
12494 qualified_p = false;
12496 function = tsubst_copy_and_build (function, args, complain,
12497 in_decl,
12498 !qualified_p,
12499 integral_constant_expression_p);
12501 if (BASELINK_P (function))
12502 qualified_p = true;
12505 nargs = call_expr_nargs (t);
12506 call_args = make_tree_vector ();
12507 for (i = 0; i < nargs; ++i)
12509 tree arg = CALL_EXPR_ARG (t, i);
12511 if (!PACK_EXPANSION_P (arg))
12512 VEC_safe_push (tree, gc, call_args,
12513 RECUR (CALL_EXPR_ARG (t, i)));
12514 else
12516 /* Expand the pack expansion and push each entry onto
12517 CALL_ARGS. */
12518 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12519 if (TREE_CODE (arg) == TREE_VEC)
12521 unsigned int len, j;
12523 len = TREE_VEC_LENGTH (arg);
12524 for (j = 0; j < len; ++j)
12526 tree value = TREE_VEC_ELT (arg, j);
12527 if (value != NULL_TREE)
12528 value = convert_from_reference (value);
12529 VEC_safe_push (tree, gc, call_args, value);
12532 else
12534 /* A partial substitution. Add one entry. */
12535 VEC_safe_push (tree, gc, call_args, arg);
12540 /* We do not perform argument-dependent lookup if normal
12541 lookup finds a non-function, in accordance with the
12542 expected resolution of DR 218. */
12543 if (koenig_p
12544 && ((is_overloaded_fn (function)
12545 /* If lookup found a member function, the Koenig lookup is
12546 not appropriate, even if an unqualified-name was used
12547 to denote the function. */
12548 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12549 || TREE_CODE (function) == IDENTIFIER_NODE)
12550 /* Only do this when substitution turns a dependent call
12551 into a non-dependent call. */
12552 && type_dependent_expression_p_push (t)
12553 && !any_type_dependent_arguments_p (call_args))
12554 function = perform_koenig_lookup (function, call_args, false);
12556 if (TREE_CODE (function) == IDENTIFIER_NODE)
12558 unqualified_name_lookup_error (function);
12559 release_tree_vector (call_args);
12560 return error_mark_node;
12563 /* Remember that there was a reference to this entity. */
12564 if (DECL_P (function))
12565 mark_used (function);
12567 if (TREE_CODE (function) == OFFSET_REF)
12568 ret = build_offset_ref_call_from_tree (function, &call_args);
12569 else if (TREE_CODE (function) == COMPONENT_REF)
12571 tree instance = TREE_OPERAND (function, 0);
12572 tree fn = TREE_OPERAND (function, 1);
12574 if (processing_template_decl
12575 && (type_dependent_expression_p (instance)
12576 || (!BASELINK_P (fn)
12577 && TREE_CODE (fn) != FIELD_DECL)
12578 || type_dependent_expression_p (fn)
12579 || any_type_dependent_arguments_p (call_args)))
12580 ret = build_nt_call_vec (function, call_args);
12581 else if (!BASELINK_P (fn))
12582 ret = finish_call_expr (function, &call_args,
12583 /*disallow_virtual=*/false,
12584 /*koenig_p=*/false,
12585 complain);
12586 else
12587 ret = (build_new_method_call
12588 (instance, fn,
12589 &call_args, NULL_TREE,
12590 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12591 /*fn_p=*/NULL,
12592 complain));
12594 else
12595 ret = finish_call_expr (function, &call_args,
12596 /*disallow_virtual=*/qualified_p,
12597 koenig_p,
12598 complain);
12600 release_tree_vector (call_args);
12602 return ret;
12605 case COND_EXPR:
12606 return build_x_conditional_expr
12607 (RECUR (TREE_OPERAND (t, 0)),
12608 RECUR (TREE_OPERAND (t, 1)),
12609 RECUR (TREE_OPERAND (t, 2)),
12610 complain);
12612 case PSEUDO_DTOR_EXPR:
12613 return finish_pseudo_destructor_expr
12614 (RECUR (TREE_OPERAND (t, 0)),
12615 RECUR (TREE_OPERAND (t, 1)),
12616 RECUR (TREE_OPERAND (t, 2)));
12618 case TREE_LIST:
12620 tree purpose, value, chain;
12622 if (t == void_list_node)
12623 return t;
12625 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12626 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12628 /* We have pack expansions, so expand those and
12629 create a new list out of it. */
12630 tree purposevec = NULL_TREE;
12631 tree valuevec = NULL_TREE;
12632 tree chain;
12633 int i, len = -1;
12635 /* Expand the argument expressions. */
12636 if (TREE_PURPOSE (t))
12637 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12638 complain, in_decl);
12639 if (TREE_VALUE (t))
12640 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12641 complain, in_decl);
12643 /* Build the rest of the list. */
12644 chain = TREE_CHAIN (t);
12645 if (chain && chain != void_type_node)
12646 chain = RECUR (chain);
12648 /* Determine the number of arguments. */
12649 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12651 len = TREE_VEC_LENGTH (purposevec);
12652 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12654 else if (TREE_CODE (valuevec) == TREE_VEC)
12655 len = TREE_VEC_LENGTH (valuevec);
12656 else
12658 /* Since we only performed a partial substitution into
12659 the argument pack, we only return a single list
12660 node. */
12661 if (purposevec == TREE_PURPOSE (t)
12662 && valuevec == TREE_VALUE (t)
12663 && chain == TREE_CHAIN (t))
12664 return t;
12666 return tree_cons (purposevec, valuevec, chain);
12669 /* Convert the argument vectors into a TREE_LIST */
12670 i = len;
12671 while (i > 0)
12673 /* Grab the Ith values. */
12674 i--;
12675 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12676 : NULL_TREE;
12677 value
12678 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12679 : NULL_TREE;
12681 /* Build the list (backwards). */
12682 chain = tree_cons (purpose, value, chain);
12685 return chain;
12688 purpose = TREE_PURPOSE (t);
12689 if (purpose)
12690 purpose = RECUR (purpose);
12691 value = TREE_VALUE (t);
12692 if (value)
12693 value = RECUR (value);
12694 chain = TREE_CHAIN (t);
12695 if (chain && chain != void_type_node)
12696 chain = RECUR (chain);
12697 if (purpose == TREE_PURPOSE (t)
12698 && value == TREE_VALUE (t)
12699 && chain == TREE_CHAIN (t))
12700 return t;
12701 return tree_cons (purpose, value, chain);
12704 case COMPONENT_REF:
12706 tree object;
12707 tree object_type;
12708 tree member;
12710 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12711 args, complain, in_decl);
12712 /* Remember that there was a reference to this entity. */
12713 if (DECL_P (object))
12714 mark_used (object);
12715 object_type = TREE_TYPE (object);
12717 member = TREE_OPERAND (t, 1);
12718 if (BASELINK_P (member))
12719 member = tsubst_baselink (member,
12720 non_reference (TREE_TYPE (object)),
12721 args, complain, in_decl);
12722 else
12723 member = tsubst_copy (member, args, complain, in_decl);
12724 if (member == error_mark_node)
12725 return error_mark_node;
12727 if (object_type && !CLASS_TYPE_P (object_type))
12729 if (SCALAR_TYPE_P (object_type))
12731 tree s = NULL_TREE;
12732 tree dtor = member;
12734 if (TREE_CODE (dtor) == SCOPE_REF)
12736 s = TREE_OPERAND (dtor, 0);
12737 dtor = TREE_OPERAND (dtor, 1);
12739 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12741 dtor = TREE_OPERAND (dtor, 0);
12742 if (TYPE_P (dtor))
12743 return finish_pseudo_destructor_expr (object, s, dtor);
12747 else if (TREE_CODE (member) == SCOPE_REF
12748 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12750 tree tmpl;
12751 tree args;
12753 /* Lookup the template functions now that we know what the
12754 scope is. */
12755 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12756 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12757 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12758 /*is_type_p=*/false,
12759 /*complain=*/false);
12760 if (BASELINK_P (member))
12762 BASELINK_FUNCTIONS (member)
12763 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12764 args);
12765 member = (adjust_result_of_qualified_name_lookup
12766 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12767 object_type));
12769 else
12771 qualified_name_lookup_error (object_type, tmpl, member,
12772 input_location);
12773 return error_mark_node;
12776 else if (TREE_CODE (member) == SCOPE_REF
12777 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12778 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12780 if (complain & tf_error)
12782 if (TYPE_P (TREE_OPERAND (member, 0)))
12783 error ("%qT is not a class or namespace",
12784 TREE_OPERAND (member, 0));
12785 else
12786 error ("%qD is not a class or namespace",
12787 TREE_OPERAND (member, 0));
12789 return error_mark_node;
12791 else if (TREE_CODE (member) == FIELD_DECL)
12792 return finish_non_static_data_member (member, object, NULL_TREE);
12794 return finish_class_member_access_expr (object, member,
12795 /*template_p=*/false,
12796 complain);
12799 case THROW_EXPR:
12800 return build_throw
12801 (RECUR (TREE_OPERAND (t, 0)));
12803 case CONSTRUCTOR:
12805 VEC(constructor_elt,gc) *n;
12806 constructor_elt *ce;
12807 unsigned HOST_WIDE_INT idx;
12808 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12809 bool process_index_p;
12810 int newlen;
12811 bool need_copy_p = false;
12812 tree r;
12814 if (type == error_mark_node)
12815 return error_mark_node;
12817 /* digest_init will do the wrong thing if we let it. */
12818 if (type && TYPE_PTRMEMFUNC_P (type))
12819 return t;
12821 /* We do not want to process the index of aggregate
12822 initializers as they are identifier nodes which will be
12823 looked up by digest_init. */
12824 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12826 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12827 newlen = VEC_length (constructor_elt, n);
12828 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
12830 if (ce->index && process_index_p)
12831 ce->index = RECUR (ce->index);
12833 if (PACK_EXPANSION_P (ce->value))
12835 /* Substitute into the pack expansion. */
12836 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12837 in_decl);
12839 if (ce->value == error_mark_node)
12841 else if (TREE_VEC_LENGTH (ce->value) == 1)
12842 /* Just move the argument into place. */
12843 ce->value = TREE_VEC_ELT (ce->value, 0);
12844 else
12846 /* Update the length of the final CONSTRUCTOR
12847 arguments vector, and note that we will need to
12848 copy.*/
12849 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12850 need_copy_p = true;
12853 else
12854 ce->value = RECUR (ce->value);
12857 if (need_copy_p)
12859 VEC(constructor_elt,gc) *old_n = n;
12861 n = VEC_alloc (constructor_elt, gc, newlen);
12862 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
12864 if (TREE_CODE (ce->value) == TREE_VEC)
12866 int i, len = TREE_VEC_LENGTH (ce->value);
12867 for (i = 0; i < len; ++i)
12868 CONSTRUCTOR_APPEND_ELT (n, 0,
12869 TREE_VEC_ELT (ce->value, i));
12871 else
12872 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12876 r = build_constructor (init_list_type_node, n);
12877 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12879 if (TREE_HAS_CONSTRUCTOR (t))
12880 return finish_compound_literal (type, r);
12882 return r;
12885 case TYPEID_EXPR:
12887 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12888 if (TYPE_P (operand_0))
12889 return get_typeid (operand_0);
12890 return build_typeid (operand_0);
12893 case VAR_DECL:
12894 if (!args)
12895 return t;
12896 /* Fall through */
12898 case PARM_DECL:
12900 tree r = tsubst_copy (t, args, complain, in_decl);
12902 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12903 /* If the original type was a reference, we'll be wrapped in
12904 the appropriate INDIRECT_REF. */
12905 r = convert_from_reference (r);
12906 return r;
12909 case VA_ARG_EXPR:
12910 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12911 tsubst_copy (TREE_TYPE (t), args, complain,
12912 in_decl));
12914 case OFFSETOF_EXPR:
12915 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12917 case TRAIT_EXPR:
12919 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12920 complain, in_decl);
12922 tree type2 = TRAIT_EXPR_TYPE2 (t);
12923 if (type2)
12924 type2 = tsubst_copy (type2, args, complain, in_decl);
12926 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12929 case STMT_EXPR:
12931 tree old_stmt_expr = cur_stmt_expr;
12932 tree stmt_expr = begin_stmt_expr ();
12934 cur_stmt_expr = stmt_expr;
12935 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12936 integral_constant_expression_p);
12937 stmt_expr = finish_stmt_expr (stmt_expr, false);
12938 cur_stmt_expr = old_stmt_expr;
12940 /* If the resulting list of expression statement is empty,
12941 fold it further into void_zero_node. */
12942 if (empty_expr_stmt_p (stmt_expr))
12943 stmt_expr = void_zero_node;
12945 return stmt_expr;
12948 case CONST_DECL:
12949 t = tsubst_copy (t, args, complain, in_decl);
12950 /* As in finish_id_expression, we resolve enumeration constants
12951 to their underlying values. */
12952 if (TREE_CODE (t) == CONST_DECL)
12954 used_types_insert (TREE_TYPE (t));
12955 return DECL_INITIAL (t);
12957 return t;
12959 case LAMBDA_EXPR:
12961 tree r = build_lambda_expr ();
12963 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12964 TREE_TYPE (r) = type;
12965 CLASSTYPE_LAMBDA_EXPR (type) = r;
12967 LAMBDA_EXPR_LOCATION (r)
12968 = LAMBDA_EXPR_LOCATION (t);
12969 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12970 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12971 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12972 LAMBDA_EXPR_DISCRIMINATOR (r)
12973 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12974 LAMBDA_EXPR_CAPTURE_LIST (r)
12975 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12976 LAMBDA_EXPR_THIS_CAPTURE (r)
12977 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12978 LAMBDA_EXPR_EXTRA_SCOPE (r)
12979 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12981 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12982 determine_visibility (TYPE_NAME (type));
12983 /* Now that we know visibility, instantiate the type so we have a
12984 declaration of the op() for later calls to lambda_function. */
12985 complete_type (type);
12987 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12988 if (type)
12989 apply_lambda_return_type (r, type);
12991 return build_lambda_object (r);
12994 default:
12995 /* Handle Objective-C++ constructs, if appropriate. */
12997 tree subst
12998 = objcp_tsubst_copy_and_build (t, args, complain,
12999 in_decl, /*function_p=*/false);
13000 if (subst)
13001 return subst;
13003 return tsubst_copy (t, args, complain, in_decl);
13006 #undef RECUR
13009 /* Verify that the instantiated ARGS are valid. For type arguments,
13010 make sure that the type's linkage is ok. For non-type arguments,
13011 make sure they are constants if they are integral or enumerations.
13012 Emit an error under control of COMPLAIN, and return TRUE on error. */
13014 static bool
13015 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13017 if (ARGUMENT_PACK_P (t))
13019 tree vec = ARGUMENT_PACK_ARGS (t);
13020 int len = TREE_VEC_LENGTH (vec);
13021 bool result = false;
13022 int i;
13024 for (i = 0; i < len; ++i)
13025 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13026 result = true;
13027 return result;
13029 else if (TYPE_P (t))
13031 /* [basic.link]: A name with no linkage (notably, the name
13032 of a class or enumeration declared in a local scope)
13033 shall not be used to declare an entity with linkage.
13034 This implies that names with no linkage cannot be used as
13035 template arguments
13037 DR 757 relaxes this restriction for C++0x. */
13038 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13039 : no_linkage_check (t, /*relaxed_p=*/false));
13041 if (nt)
13043 /* DR 488 makes use of a type with no linkage cause
13044 type deduction to fail. */
13045 if (complain & tf_error)
13047 if (TYPE_ANONYMOUS_P (nt))
13048 error ("%qT is/uses anonymous type", t);
13049 else
13050 error ("template argument for %qD uses local type %qT",
13051 tmpl, t);
13053 return true;
13055 /* In order to avoid all sorts of complications, we do not
13056 allow variably-modified types as template arguments. */
13057 else if (variably_modified_type_p (t, NULL_TREE))
13059 if (complain & tf_error)
13060 error ("%qT is a variably modified type", t);
13061 return true;
13064 /* A non-type argument of integral or enumerated type must be a
13065 constant. */
13066 else if (TREE_TYPE (t)
13067 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13068 && !TREE_CONSTANT (t))
13070 if (complain & tf_error)
13071 error ("integral expression %qE is not constant", t);
13072 return true;
13074 return false;
13077 static bool
13078 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13080 int ix, len = DECL_NTPARMS (tmpl);
13081 bool result = false;
13083 for (ix = 0; ix != len; ix++)
13085 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13086 result = true;
13088 if (result && (complain & tf_error))
13089 error (" trying to instantiate %qD", tmpl);
13090 return result;
13093 /* Instantiate the indicated variable or function template TMPL with
13094 the template arguments in TARG_PTR. */
13096 tree
13097 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13099 tree targ_ptr = orig_args;
13100 tree fndecl;
13101 tree gen_tmpl;
13102 tree spec;
13103 HOST_WIDE_INT saved_processing_template_decl;
13105 if (tmpl == error_mark_node)
13106 return error_mark_node;
13108 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13110 /* If this function is a clone, handle it specially. */
13111 if (DECL_CLONED_FUNCTION_P (tmpl))
13113 tree spec;
13114 tree clone;
13116 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13117 DECL_CLONED_FUNCTION. */
13118 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13119 targ_ptr, complain);
13120 if (spec == error_mark_node)
13121 return error_mark_node;
13123 /* Look for the clone. */
13124 FOR_EACH_CLONE (clone, spec)
13125 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13126 return clone;
13127 /* We should always have found the clone by now. */
13128 gcc_unreachable ();
13129 return NULL_TREE;
13132 /* Check to see if we already have this specialization. */
13133 gen_tmpl = most_general_template (tmpl);
13134 if (tmpl != gen_tmpl)
13135 /* The TMPL is a partial instantiation. To get a full set of
13136 arguments we must add the arguments used to perform the
13137 partial instantiation. */
13138 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13139 targ_ptr);
13141 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13142 but it doesn't seem to be on the hot path. */
13143 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13145 gcc_assert (tmpl == gen_tmpl
13146 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13147 == spec)
13148 || fndecl == NULL_TREE);
13150 if (spec != NULL_TREE)
13151 return spec;
13153 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13154 complain))
13155 return error_mark_node;
13157 /* We are building a FUNCTION_DECL, during which the access of its
13158 parameters and return types have to be checked. However this
13159 FUNCTION_DECL which is the desired context for access checking
13160 is not built yet. We solve this chicken-and-egg problem by
13161 deferring all checks until we have the FUNCTION_DECL. */
13162 push_deferring_access_checks (dk_deferred);
13164 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13165 (because, for example, we have encountered a non-dependent
13166 function call in the body of a template function and must now
13167 determine which of several overloaded functions will be called),
13168 within the instantiation itself we are not processing a
13169 template. */
13170 saved_processing_template_decl = processing_template_decl;
13171 processing_template_decl = 0;
13172 /* Substitute template parameters to obtain the specialization. */
13173 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13174 targ_ptr, complain, gen_tmpl);
13175 processing_template_decl = saved_processing_template_decl;
13176 if (fndecl == error_mark_node)
13177 return error_mark_node;
13179 /* Now we know the specialization, compute access previously
13180 deferred. */
13181 push_access_scope (fndecl);
13183 /* Some typedefs referenced from within the template code need to be access
13184 checked at template instantiation time, i.e now. These types were
13185 added to the template at parsing time. Let's get those and perfom
13186 the acces checks then. */
13187 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13188 perform_deferred_access_checks ();
13189 pop_access_scope (fndecl);
13190 pop_deferring_access_checks ();
13192 /* The DECL_TI_TEMPLATE should always be the immediate parent
13193 template, not the most general template. */
13194 DECL_TI_TEMPLATE (fndecl) = tmpl;
13196 /* If we've just instantiated the main entry point for a function,
13197 instantiate all the alternate entry points as well. We do this
13198 by cloning the instantiation of the main entry point, not by
13199 instantiating the template clones. */
13200 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13201 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13203 return fndecl;
13206 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13207 NARGS elements of the arguments that are being used when calling
13208 it. TARGS is a vector into which the deduced template arguments
13209 are placed.
13211 Return zero for success, 2 for an incomplete match that doesn't resolve
13212 all the types, and 1 for complete failure. An error message will be
13213 printed only for an incomplete match.
13215 If FN is a conversion operator, or we are trying to produce a specific
13216 specialization, RETURN_TYPE is the return type desired.
13218 The EXPLICIT_TARGS are explicit template arguments provided via a
13219 template-id.
13221 The parameter STRICT is one of:
13223 DEDUCE_CALL:
13224 We are deducing arguments for a function call, as in
13225 [temp.deduct.call].
13227 DEDUCE_CONV:
13228 We are deducing arguments for a conversion function, as in
13229 [temp.deduct.conv].
13231 DEDUCE_EXACT:
13232 We are deducing arguments when doing an explicit instantiation
13233 as in [temp.explicit], when determining an explicit specialization
13234 as in [temp.expl.spec], or when taking the address of a function
13235 template, as in [temp.deduct.funcaddr]. */
13238 fn_type_unification (tree fn,
13239 tree explicit_targs,
13240 tree targs,
13241 const tree *args,
13242 unsigned int nargs,
13243 tree return_type,
13244 unification_kind_t strict,
13245 int flags)
13247 tree parms;
13248 tree fntype;
13249 int result;
13250 bool incomplete_argument_packs_p = false;
13252 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13254 fntype = TREE_TYPE (fn);
13255 if (explicit_targs)
13257 /* [temp.deduct]
13259 The specified template arguments must match the template
13260 parameters in kind (i.e., type, nontype, template), and there
13261 must not be more arguments than there are parameters;
13262 otherwise type deduction fails.
13264 Nontype arguments must match the types of the corresponding
13265 nontype template parameters, or must be convertible to the
13266 types of the corresponding nontype parameters as specified in
13267 _temp.arg.nontype_, otherwise type deduction fails.
13269 All references in the function type of the function template
13270 to the corresponding template parameters are replaced by the
13271 specified template argument values. If a substitution in a
13272 template parameter or in the function type of the function
13273 template results in an invalid type, type deduction fails. */
13274 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13275 int i, len = TREE_VEC_LENGTH (tparms);
13276 tree converted_args;
13277 bool incomplete = false;
13279 if (explicit_targs == error_mark_node)
13280 return 1;
13282 converted_args
13283 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13284 /*require_all_args=*/false,
13285 /*use_default_args=*/false));
13286 if (converted_args == error_mark_node)
13287 return 1;
13289 /* Substitute the explicit args into the function type. This is
13290 necessary so that, for instance, explicitly declared function
13291 arguments can match null pointed constants. If we were given
13292 an incomplete set of explicit args, we must not do semantic
13293 processing during substitution as we could create partial
13294 instantiations. */
13295 for (i = 0; i < len; i++)
13297 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13298 bool parameter_pack = false;
13300 /* Dig out the actual parm. */
13301 if (TREE_CODE (parm) == TYPE_DECL
13302 || TREE_CODE (parm) == TEMPLATE_DECL)
13304 parm = TREE_TYPE (parm);
13305 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13307 else if (TREE_CODE (parm) == PARM_DECL)
13309 parm = DECL_INITIAL (parm);
13310 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13313 if (parameter_pack)
13315 int level, idx;
13316 tree targ;
13317 template_parm_level_and_index (parm, &level, &idx);
13319 /* Mark the argument pack as "incomplete". We could
13320 still deduce more arguments during unification. */
13321 targ = TMPL_ARG (converted_args, level, idx);
13322 if (targ)
13324 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13325 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13326 = ARGUMENT_PACK_ARGS (targ);
13329 /* We have some incomplete argument packs. */
13330 incomplete_argument_packs_p = true;
13334 if (incomplete_argument_packs_p)
13335 /* Any substitution is guaranteed to be incomplete if there
13336 are incomplete argument packs, because we can still deduce
13337 more arguments. */
13338 incomplete = 1;
13339 else
13340 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13342 processing_template_decl += incomplete;
13343 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13344 processing_template_decl -= incomplete;
13346 if (fntype == error_mark_node)
13347 return 1;
13349 /* Place the explicitly specified arguments in TARGS. */
13350 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13351 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13354 /* Never do unification on the 'this' parameter. */
13355 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13357 if (return_type)
13359 tree *new_args;
13361 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13362 new_args = XALLOCAVEC (tree, nargs + 1);
13363 new_args[0] = return_type;
13364 memcpy (new_args + 1, args, nargs * sizeof (tree));
13365 args = new_args;
13366 ++nargs;
13369 /* We allow incomplete unification without an error message here
13370 because the standard doesn't seem to explicitly prohibit it. Our
13371 callers must be ready to deal with unification failures in any
13372 event. */
13373 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13374 targs, parms, args, nargs, /*subr=*/0,
13375 strict, flags);
13377 if (result == 0 && incomplete_argument_packs_p)
13379 int i, len = NUM_TMPL_ARGS (targs);
13381 /* Clear the "incomplete" flags on all argument packs. */
13382 for (i = 0; i < len; i++)
13384 tree arg = TREE_VEC_ELT (targs, i);
13385 if (ARGUMENT_PACK_P (arg))
13387 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13388 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13393 /* Now that we have bindings for all of the template arguments,
13394 ensure that the arguments deduced for the template template
13395 parameters have compatible template parameter lists. We cannot
13396 check this property before we have deduced all template
13397 arguments, because the template parameter types of a template
13398 template parameter might depend on prior template parameters
13399 deduced after the template template parameter. The following
13400 ill-formed example illustrates this issue:
13402 template<typename T, template<T> class C> void f(C<5>, T);
13404 template<int N> struct X {};
13406 void g() {
13407 f(X<5>(), 5l); // error: template argument deduction fails
13410 The template parameter list of 'C' depends on the template type
13411 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13412 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13413 time that we deduce 'C'. */
13414 if (result == 0
13415 && !template_template_parm_bindings_ok_p
13416 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13417 return 1;
13419 if (result == 0)
13420 /* All is well so far. Now, check:
13422 [temp.deduct]
13424 When all template arguments have been deduced, all uses of
13425 template parameters in nondeduced contexts are replaced with
13426 the corresponding deduced argument values. If the
13427 substitution results in an invalid type, as described above,
13428 type deduction fails. */
13430 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13431 if (substed == error_mark_node)
13432 return 1;
13434 /* If we're looking for an exact match, check that what we got
13435 is indeed an exact match. It might not be if some template
13436 parameters are used in non-deduced contexts. */
13437 if (strict == DEDUCE_EXACT)
13439 unsigned int i;
13441 tree sarg
13442 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13443 if (return_type)
13444 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13445 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13446 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13447 return 1;
13451 return result;
13454 /* Adjust types before performing type deduction, as described in
13455 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13456 sections are symmetric. PARM is the type of a function parameter
13457 or the return type of the conversion function. ARG is the type of
13458 the argument passed to the call, or the type of the value
13459 initialized with the result of the conversion function.
13460 ARG_EXPR is the original argument expression, which may be null. */
13462 static int
13463 maybe_adjust_types_for_deduction (unification_kind_t strict,
13464 tree* parm,
13465 tree* arg,
13466 tree arg_expr)
13468 int result = 0;
13470 switch (strict)
13472 case DEDUCE_CALL:
13473 break;
13475 case DEDUCE_CONV:
13477 /* Swap PARM and ARG throughout the remainder of this
13478 function; the handling is precisely symmetric since PARM
13479 will initialize ARG rather than vice versa. */
13480 tree* temp = parm;
13481 parm = arg;
13482 arg = temp;
13483 break;
13486 case DEDUCE_EXACT:
13487 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13488 too, but here handle it by stripping the reference from PARM
13489 rather than by adding it to ARG. */
13490 if (TREE_CODE (*parm) == REFERENCE_TYPE
13491 && TYPE_REF_IS_RVALUE (*parm)
13492 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13493 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13494 && TREE_CODE (*arg) == REFERENCE_TYPE
13495 && !TYPE_REF_IS_RVALUE (*arg))
13496 *parm = TREE_TYPE (*parm);
13497 /* Nothing else to do in this case. */
13498 return 0;
13500 default:
13501 gcc_unreachable ();
13504 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13506 /* [temp.deduct.call]
13508 If P is not a reference type:
13510 --If A is an array type, the pointer type produced by the
13511 array-to-pointer standard conversion (_conv.array_) is
13512 used in place of A for type deduction; otherwise,
13514 --If A is a function type, the pointer type produced by
13515 the function-to-pointer standard conversion
13516 (_conv.func_) is used in place of A for type deduction;
13517 otherwise,
13519 --If A is a cv-qualified type, the top level
13520 cv-qualifiers of A's type are ignored for type
13521 deduction. */
13522 if (TREE_CODE (*arg) == ARRAY_TYPE)
13523 *arg = build_pointer_type (TREE_TYPE (*arg));
13524 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13525 *arg = build_pointer_type (*arg);
13526 else
13527 *arg = TYPE_MAIN_VARIANT (*arg);
13530 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13531 of the form T&&, where T is a template parameter, and the argument
13532 is an lvalue, T is deduced as A& */
13533 if (TREE_CODE (*parm) == REFERENCE_TYPE
13534 && TYPE_REF_IS_RVALUE (*parm)
13535 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13536 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13537 && arg_expr && real_lvalue_p (arg_expr))
13538 *arg = build_reference_type (*arg);
13540 /* [temp.deduct.call]
13542 If P is a cv-qualified type, the top level cv-qualifiers
13543 of P's type are ignored for type deduction. If P is a
13544 reference type, the type referred to by P is used for
13545 type deduction. */
13546 *parm = TYPE_MAIN_VARIANT (*parm);
13547 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13549 *parm = TREE_TYPE (*parm);
13550 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13553 /* DR 322. For conversion deduction, remove a reference type on parm
13554 too (which has been swapped into ARG). */
13555 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13556 *arg = TREE_TYPE (*arg);
13558 return result;
13561 /* Most parms like fn_type_unification.
13563 If SUBR is 1, we're being called recursively (to unify the
13564 arguments of a function or method parameter of a function
13565 template). */
13567 static int
13568 type_unification_real (tree tparms,
13569 tree targs,
13570 tree xparms,
13571 const tree *xargs,
13572 unsigned int xnargs,
13573 int subr,
13574 unification_kind_t strict,
13575 int flags)
13577 tree parm, arg, arg_expr;
13578 int i;
13579 int ntparms = TREE_VEC_LENGTH (tparms);
13580 int sub_strict;
13581 int saw_undeduced = 0;
13582 tree parms;
13583 const tree *args;
13584 unsigned int nargs;
13585 unsigned int ia;
13587 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13588 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13589 gcc_assert (ntparms > 0);
13591 /* Reset the number of non-defaulted template arguments contained
13592 in in TARGS. */
13593 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13595 switch (strict)
13597 case DEDUCE_CALL:
13598 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13599 | UNIFY_ALLOW_DERIVED);
13600 break;
13602 case DEDUCE_CONV:
13603 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13604 break;
13606 case DEDUCE_EXACT:
13607 sub_strict = UNIFY_ALLOW_NONE;
13608 break;
13610 default:
13611 gcc_unreachable ();
13614 again:
13615 parms = xparms;
13616 args = xargs;
13617 nargs = xnargs;
13619 ia = 0;
13620 while (parms && parms != void_list_node
13621 && ia < nargs)
13623 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13624 break;
13626 parm = TREE_VALUE (parms);
13627 parms = TREE_CHAIN (parms);
13628 arg = args[ia];
13629 ++ia;
13630 arg_expr = NULL;
13632 if (arg == error_mark_node)
13633 return 1;
13634 if (arg == unknown_type_node)
13635 /* We can't deduce anything from this, but we might get all the
13636 template args from other function args. */
13637 continue;
13639 /* Conversions will be performed on a function argument that
13640 corresponds with a function parameter that contains only
13641 non-deducible template parameters and explicitly specified
13642 template parameters. */
13643 if (!uses_template_parms (parm))
13645 tree type;
13647 if (!TYPE_P (arg))
13648 type = TREE_TYPE (arg);
13649 else
13650 type = arg;
13652 if (same_type_p (parm, type))
13653 continue;
13654 if (strict != DEDUCE_EXACT
13655 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13656 flags))
13657 continue;
13659 return 1;
13662 if (!TYPE_P (arg))
13664 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13665 if (type_unknown_p (arg))
13667 /* [temp.deduct.type]
13669 A template-argument can be deduced from a pointer to
13670 function or pointer to member function argument if
13671 the set of overloaded functions does not contain
13672 function templates and at most one of a set of
13673 overloaded functions provides a unique match. */
13674 if (resolve_overloaded_unification
13675 (tparms, targs, parm, arg, strict, sub_strict))
13676 continue;
13678 return 1;
13680 arg_expr = arg;
13681 arg = unlowered_expr_type (arg);
13682 if (arg == error_mark_node)
13683 return 1;
13687 int arg_strict = sub_strict;
13689 if (!subr)
13690 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13691 arg_expr);
13693 if (arg == init_list_type_node && arg_expr)
13694 arg = arg_expr;
13695 if (unify (tparms, targs, parm, arg, arg_strict))
13696 return 1;
13701 if (parms
13702 && parms != void_list_node
13703 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13705 /* Unify the remaining arguments with the pack expansion type. */
13706 tree argvec;
13707 tree parmvec = make_tree_vec (1);
13709 /* Allocate a TREE_VEC and copy in all of the arguments */
13710 argvec = make_tree_vec (nargs - ia);
13711 for (i = 0; ia < nargs; ++ia, ++i)
13712 TREE_VEC_ELT (argvec, i) = args[ia];
13714 /* Copy the parameter into parmvec. */
13715 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13716 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13717 /*call_args_p=*/true, /*subr=*/subr))
13718 return 1;
13720 /* Advance to the end of the list of parameters. */
13721 parms = TREE_CHAIN (parms);
13724 /* Fail if we've reached the end of the parm list, and more args
13725 are present, and the parm list isn't variadic. */
13726 if (ia < nargs && parms == void_list_node)
13727 return 1;
13728 /* Fail if parms are left and they don't have default values. */
13729 if (parms && parms != void_list_node
13730 && TREE_PURPOSE (parms) == NULL_TREE)
13731 return 1;
13733 if (!subr)
13734 for (i = 0; i < ntparms; i++)
13735 if (!TREE_VEC_ELT (targs, i))
13737 tree tparm;
13739 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13740 continue;
13742 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13744 /* If this is an undeduced nontype parameter that depends on
13745 a type parameter, try another pass; its type may have been
13746 deduced from a later argument than the one from which
13747 this parameter can be deduced. */
13748 if (TREE_CODE (tparm) == PARM_DECL
13749 && uses_template_parms (TREE_TYPE (tparm))
13750 && !saw_undeduced++)
13751 goto again;
13753 /* Core issue #226 (C++0x) [temp.deduct]:
13755 If a template argument has not been deduced, its
13756 default template argument, if any, is used.
13758 When we are in C++98 mode, TREE_PURPOSE will either
13759 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13760 to explicitly check cxx_dialect here. */
13761 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13763 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13764 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13765 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13766 arg = convert_template_argument (parm, arg, targs, tf_none,
13767 i, NULL_TREE);
13768 if (arg == error_mark_node)
13769 return 1;
13770 else
13772 TREE_VEC_ELT (targs, i) = arg;
13773 /* The position of the first default template argument,
13774 is also the number of non-defaulted arguments in TARGS.
13775 Record that. */
13776 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13777 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
13778 continue;
13782 /* If the type parameter is a parameter pack, then it will
13783 be deduced to an empty parameter pack. */
13784 if (template_parameter_pack_p (tparm))
13786 tree arg;
13788 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13790 arg = make_node (NONTYPE_ARGUMENT_PACK);
13791 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13792 TREE_CONSTANT (arg) = 1;
13794 else
13795 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13797 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13799 TREE_VEC_ELT (targs, i) = arg;
13800 continue;
13803 return 2;
13805 #ifdef ENABLE_CHECKING
13806 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13807 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
13808 #endif
13810 return 0;
13813 /* Subroutine of type_unification_real. Args are like the variables
13814 at the call site. ARG is an overloaded function (or template-id);
13815 we try deducing template args from each of the overloads, and if
13816 only one succeeds, we go with that. Modifies TARGS and returns
13817 true on success. */
13819 static bool
13820 resolve_overloaded_unification (tree tparms,
13821 tree targs,
13822 tree parm,
13823 tree arg,
13824 unification_kind_t strict,
13825 int sub_strict)
13827 tree tempargs = copy_node (targs);
13828 int good = 0;
13829 tree goodfn = NULL_TREE;
13830 bool addr_p;
13832 if (TREE_CODE (arg) == ADDR_EXPR)
13834 arg = TREE_OPERAND (arg, 0);
13835 addr_p = true;
13837 else
13838 addr_p = false;
13840 if (TREE_CODE (arg) == COMPONENT_REF)
13841 /* Handle `&x' where `x' is some static or non-static member
13842 function name. */
13843 arg = TREE_OPERAND (arg, 1);
13845 if (TREE_CODE (arg) == OFFSET_REF)
13846 arg = TREE_OPERAND (arg, 1);
13848 /* Strip baselink information. */
13849 if (BASELINK_P (arg))
13850 arg = BASELINK_FUNCTIONS (arg);
13852 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13854 /* If we got some explicit template args, we need to plug them into
13855 the affected templates before we try to unify, in case the
13856 explicit args will completely resolve the templates in question. */
13858 tree expl_subargs = TREE_OPERAND (arg, 1);
13859 arg = TREE_OPERAND (arg, 0);
13861 for (; arg; arg = OVL_NEXT (arg))
13863 tree fn = OVL_CURRENT (arg);
13864 tree subargs, elem;
13866 if (TREE_CODE (fn) != TEMPLATE_DECL)
13867 continue;
13869 ++processing_template_decl;
13870 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13871 expl_subargs, /*check_ret=*/false);
13872 if (subargs)
13874 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13875 if (try_one_overload (tparms, targs, tempargs, parm,
13876 elem, strict, sub_strict, addr_p)
13877 && (!goodfn || !decls_match (goodfn, elem)))
13879 goodfn = elem;
13880 ++good;
13883 --processing_template_decl;
13886 else if (TREE_CODE (arg) != OVERLOAD
13887 && TREE_CODE (arg) != FUNCTION_DECL)
13888 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13889 -- but the deduction does not succeed because the expression is
13890 not just the function on its own. */
13891 return false;
13892 else
13893 for (; arg; arg = OVL_NEXT (arg))
13894 if (try_one_overload (tparms, targs, tempargs, parm,
13895 TREE_TYPE (OVL_CURRENT (arg)),
13896 strict, sub_strict, addr_p)
13897 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13899 goodfn = OVL_CURRENT (arg);
13900 ++good;
13903 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13904 to function or pointer to member function argument if the set of
13905 overloaded functions does not contain function templates and at most
13906 one of a set of overloaded functions provides a unique match.
13908 So if we found multiple possibilities, we return success but don't
13909 deduce anything. */
13911 if (good == 1)
13913 int i = TREE_VEC_LENGTH (targs);
13914 for (; i--; )
13915 if (TREE_VEC_ELT (tempargs, i))
13916 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13918 if (good)
13919 return true;
13921 return false;
13924 /* Core DR 115: In contexts where deduction is done and fails, or in
13925 contexts where deduction is not done, if a template argument list is
13926 specified and it, along with any default template arguments, identifies
13927 a single function template specialization, then the template-id is an
13928 lvalue for the function template specialization. */
13930 tree
13931 resolve_nondeduced_context (tree orig_expr)
13933 tree expr, offset, baselink;
13934 bool addr;
13936 if (!type_unknown_p (orig_expr))
13937 return orig_expr;
13939 expr = orig_expr;
13940 addr = false;
13941 offset = NULL_TREE;
13942 baselink = NULL_TREE;
13944 if (TREE_CODE (expr) == ADDR_EXPR)
13946 expr = TREE_OPERAND (expr, 0);
13947 addr = true;
13949 if (TREE_CODE (expr) == OFFSET_REF)
13951 offset = expr;
13952 expr = TREE_OPERAND (expr, 1);
13954 if (TREE_CODE (expr) == BASELINK)
13956 baselink = expr;
13957 expr = BASELINK_FUNCTIONS (expr);
13960 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13962 int good = 0;
13963 tree goodfn = NULL_TREE;
13965 /* If we got some explicit template args, we need to plug them into
13966 the affected templates before we try to unify, in case the
13967 explicit args will completely resolve the templates in question. */
13969 tree expl_subargs = TREE_OPERAND (expr, 1);
13970 tree arg = TREE_OPERAND (expr, 0);
13971 tree badfn = NULL_TREE;
13972 tree badargs = NULL_TREE;
13974 for (; arg; arg = OVL_NEXT (arg))
13976 tree fn = OVL_CURRENT (arg);
13977 tree subargs, elem;
13979 if (TREE_CODE (fn) != TEMPLATE_DECL)
13980 continue;
13982 ++processing_template_decl;
13983 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13984 expl_subargs, /*check_ret=*/false);
13985 if (subargs && !any_dependent_template_arguments_p (subargs))
13987 elem = instantiate_template (fn, subargs, tf_none);
13988 if (elem == error_mark_node)
13990 badfn = fn;
13991 badargs = subargs;
13993 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13995 goodfn = elem;
13996 ++good;
13999 --processing_template_decl;
14001 if (good == 1)
14003 expr = goodfn;
14004 if (baselink)
14005 expr = build_baselink (BASELINK_BINFO (baselink),
14006 BASELINK_ACCESS_BINFO (baselink),
14007 expr, BASELINK_OPTYPE (baselink));
14008 if (offset)
14009 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
14010 TREE_OPERAND (offset, 0), expr);
14011 if (addr)
14012 expr = build_address (expr);
14013 return expr;
14015 else if (good == 0 && badargs)
14016 /* There were no good options and at least one bad one, so let the
14017 user know what the problem is. */
14018 instantiate_template (badfn, badargs, tf_warning_or_error);
14020 return orig_expr;
14023 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14024 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14025 different overloads deduce different arguments for a given parm.
14026 ADDR_P is true if the expression for which deduction is being
14027 performed was of the form "& fn" rather than simply "fn".
14029 Returns 1 on success. */
14031 static int
14032 try_one_overload (tree tparms,
14033 tree orig_targs,
14034 tree targs,
14035 tree parm,
14036 tree arg,
14037 unification_kind_t strict,
14038 int sub_strict,
14039 bool addr_p)
14041 int nargs;
14042 tree tempargs;
14043 int i;
14045 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14046 to function or pointer to member function argument if the set of
14047 overloaded functions does not contain function templates and at most
14048 one of a set of overloaded functions provides a unique match.
14050 So if this is a template, just return success. */
14052 if (uses_template_parms (arg))
14053 return 1;
14055 if (TREE_CODE (arg) == METHOD_TYPE)
14056 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14057 else if (addr_p)
14058 arg = build_pointer_type (arg);
14060 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14062 /* We don't copy orig_targs for this because if we have already deduced
14063 some template args from previous args, unify would complain when we
14064 try to deduce a template parameter for the same argument, even though
14065 there isn't really a conflict. */
14066 nargs = TREE_VEC_LENGTH (targs);
14067 tempargs = make_tree_vec (nargs);
14069 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14070 return 0;
14072 /* First make sure we didn't deduce anything that conflicts with
14073 explicitly specified args. */
14074 for (i = nargs; i--; )
14076 tree elt = TREE_VEC_ELT (tempargs, i);
14077 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14079 if (!elt)
14080 /*NOP*/;
14081 else if (uses_template_parms (elt))
14082 /* Since we're unifying against ourselves, we will fill in
14083 template args used in the function parm list with our own
14084 template parms. Discard them. */
14085 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14086 else if (oldelt && !template_args_equal (oldelt, elt))
14087 return 0;
14090 for (i = nargs; i--; )
14092 tree elt = TREE_VEC_ELT (tempargs, i);
14094 if (elt)
14095 TREE_VEC_ELT (targs, i) = elt;
14098 return 1;
14101 /* PARM is a template class (perhaps with unbound template
14102 parameters). ARG is a fully instantiated type. If ARG can be
14103 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14104 TARGS are as for unify. */
14106 static tree
14107 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14109 tree copy_of_targs;
14111 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14112 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14113 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14114 return NULL_TREE;
14116 /* We need to make a new template argument vector for the call to
14117 unify. If we used TARGS, we'd clutter it up with the result of
14118 the attempted unification, even if this class didn't work out.
14119 We also don't want to commit ourselves to all the unifications
14120 we've already done, since unification is supposed to be done on
14121 an argument-by-argument basis. In other words, consider the
14122 following pathological case:
14124 template <int I, int J, int K>
14125 struct S {};
14127 template <int I, int J>
14128 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14130 template <int I, int J, int K>
14131 void f(S<I, J, K>, S<I, I, I>);
14133 void g() {
14134 S<0, 0, 0> s0;
14135 S<0, 1, 2> s2;
14137 f(s0, s2);
14140 Now, by the time we consider the unification involving `s2', we
14141 already know that we must have `f<0, 0, 0>'. But, even though
14142 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14143 because there are two ways to unify base classes of S<0, 1, 2>
14144 with S<I, I, I>. If we kept the already deduced knowledge, we
14145 would reject the possibility I=1. */
14146 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14148 /* If unification failed, we're done. */
14149 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14150 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14151 return NULL_TREE;
14153 return arg;
14156 /* Given a template type PARM and a class type ARG, find the unique
14157 base type in ARG that is an instance of PARM. We do not examine
14158 ARG itself; only its base-classes. If there is not exactly one
14159 appropriate base class, return NULL_TREE. PARM may be the type of
14160 a partial specialization, as well as a plain template type. Used
14161 by unify. */
14163 static tree
14164 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14166 tree rval = NULL_TREE;
14167 tree binfo;
14169 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14171 binfo = TYPE_BINFO (complete_type (arg));
14172 if (!binfo)
14173 /* The type could not be completed. */
14174 return NULL_TREE;
14176 /* Walk in inheritance graph order. The search order is not
14177 important, and this avoids multiple walks of virtual bases. */
14178 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14180 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14182 if (r)
14184 /* If there is more than one satisfactory baseclass, then:
14186 [temp.deduct.call]
14188 If they yield more than one possible deduced A, the type
14189 deduction fails.
14191 applies. */
14192 if (rval && !same_type_p (r, rval))
14193 return NULL_TREE;
14195 rval = r;
14199 return rval;
14202 /* Returns the level of DECL, which declares a template parameter. */
14204 static int
14205 template_decl_level (tree decl)
14207 switch (TREE_CODE (decl))
14209 case TYPE_DECL:
14210 case TEMPLATE_DECL:
14211 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14213 case PARM_DECL:
14214 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14216 default:
14217 gcc_unreachable ();
14219 return 0;
14222 /* Decide whether ARG can be unified with PARM, considering only the
14223 cv-qualifiers of each type, given STRICT as documented for unify.
14224 Returns nonzero iff the unification is OK on that basis. */
14226 static int
14227 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14229 int arg_quals = cp_type_quals (arg);
14230 int parm_quals = cp_type_quals (parm);
14232 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14233 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14235 /* Although a CVR qualifier is ignored when being applied to a
14236 substituted template parameter ([8.3.2]/1 for example), that
14237 does not allow us to unify "const T" with "int&" because both
14238 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14239 It is ok when we're allowing additional CV qualifiers
14240 at the outer level [14.8.2.1]/3,1st bullet. */
14241 if ((TREE_CODE (arg) == REFERENCE_TYPE
14242 || TREE_CODE (arg) == FUNCTION_TYPE
14243 || TREE_CODE (arg) == METHOD_TYPE)
14244 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14245 return 0;
14247 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14248 && (parm_quals & TYPE_QUAL_RESTRICT))
14249 return 0;
14252 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14253 && (arg_quals & parm_quals) != parm_quals)
14254 return 0;
14256 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14257 && (parm_quals & arg_quals) != arg_quals)
14258 return 0;
14260 return 1;
14263 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14264 void
14265 template_parm_level_and_index (tree parm, int* level, int* index)
14267 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14268 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14269 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14271 *index = TEMPLATE_TYPE_IDX (parm);
14272 *level = TEMPLATE_TYPE_LEVEL (parm);
14274 else
14276 *index = TEMPLATE_PARM_IDX (parm);
14277 *level = TEMPLATE_PARM_LEVEL (parm);
14281 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14282 expansion at the end of PACKED_PARMS. Returns 0 if the type
14283 deduction succeeds, 1 otherwise. STRICT is the same as in
14284 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14285 call argument list. We'll need to adjust the arguments to make them
14286 types. SUBR tells us if this is from a recursive call to
14287 type_unification_real. */
14289 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14290 tree packed_args, int strict, bool call_args_p,
14291 bool subr)
14293 tree parm
14294 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14295 tree pattern = PACK_EXPANSION_PATTERN (parm);
14296 tree pack, packs = NULL_TREE;
14297 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14298 int len = TREE_VEC_LENGTH (packed_args);
14300 /* Determine the parameter packs we will be deducing from the
14301 pattern, and record their current deductions. */
14302 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14303 pack; pack = TREE_CHAIN (pack))
14305 tree parm_pack = TREE_VALUE (pack);
14306 int idx, level;
14308 /* Determine the index and level of this parameter pack. */
14309 template_parm_level_and_index (parm_pack, &level, &idx);
14311 /* Keep track of the parameter packs and their corresponding
14312 argument packs. */
14313 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14314 TREE_TYPE (packs) = make_tree_vec (len - start);
14317 /* Loop through all of the arguments that have not yet been
14318 unified and unify each with the pattern. */
14319 for (i = start; i < len; i++)
14321 tree parm = pattern;
14323 /* For each parameter pack, clear out the deduced value so that
14324 we can deduce it again. */
14325 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14327 int idx, level;
14328 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14330 TMPL_ARG (targs, level, idx) = NULL_TREE;
14333 /* Unify the pattern with the current argument. */
14335 tree arg = TREE_VEC_ELT (packed_args, i);
14336 tree arg_expr = NULL_TREE;
14337 int arg_strict = strict;
14338 bool skip_arg_p = false;
14340 if (call_args_p)
14342 int sub_strict;
14344 /* This mirrors what we do in type_unification_real. */
14345 switch (strict)
14347 case DEDUCE_CALL:
14348 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14349 | UNIFY_ALLOW_MORE_CV_QUAL
14350 | UNIFY_ALLOW_DERIVED);
14351 break;
14353 case DEDUCE_CONV:
14354 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14355 break;
14357 case DEDUCE_EXACT:
14358 sub_strict = UNIFY_ALLOW_NONE;
14359 break;
14361 default:
14362 gcc_unreachable ();
14365 if (!TYPE_P (arg))
14367 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14368 if (type_unknown_p (arg))
14370 /* [temp.deduct.type] A template-argument can be
14371 deduced from a pointer to function or pointer
14372 to member function argument if the set of
14373 overloaded functions does not contain function
14374 templates and at most one of a set of
14375 overloaded functions provides a unique
14376 match. */
14378 if (resolve_overloaded_unification
14379 (tparms, targs, parm, arg,
14380 (unification_kind_t) strict,
14381 sub_strict)
14382 != 0)
14383 return 1;
14384 skip_arg_p = true;
14387 if (!skip_arg_p)
14389 arg_expr = arg;
14390 arg = unlowered_expr_type (arg);
14391 if (arg == error_mark_node)
14392 return 1;
14396 arg_strict = sub_strict;
14398 if (!subr)
14399 arg_strict |=
14400 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14401 &parm, &arg, arg_expr);
14404 if (!skip_arg_p)
14406 /* For deduction from an init-list we need the actual list. */
14407 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14408 arg = arg_expr;
14409 if (unify (tparms, targs, parm, arg, arg_strict))
14410 return 1;
14414 /* For each parameter pack, collect the deduced value. */
14415 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14417 int idx, level;
14418 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14420 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14421 TMPL_ARG (targs, level, idx);
14425 /* Verify that the results of unification with the parameter packs
14426 produce results consistent with what we've seen before, and make
14427 the deduced argument packs available. */
14428 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14430 tree old_pack = TREE_VALUE (pack);
14431 tree new_args = TREE_TYPE (pack);
14432 int i, len = TREE_VEC_LENGTH (new_args);
14433 int idx, level;
14434 bool nondeduced_p = false;
14436 /* By default keep the original deduced argument pack.
14437 If necessary, more specific code is going to update the
14438 resulting deduced argument later down in this function. */
14439 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14440 TMPL_ARG (targs, level, idx) = old_pack;
14442 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14443 actually deduce anything. */
14444 for (i = 0; i < len && !nondeduced_p; ++i)
14445 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14446 nondeduced_p = true;
14447 if (nondeduced_p)
14448 continue;
14450 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14452 /* Prepend the explicit arguments onto NEW_ARGS. */
14453 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14454 tree old_args = new_args;
14455 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14456 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14458 /* Copy the explicit arguments. */
14459 new_args = make_tree_vec (len);
14460 for (i = 0; i < explicit_len; i++)
14461 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14463 /* Copy the deduced arguments. */
14464 for (; i < len; i++)
14465 TREE_VEC_ELT (new_args, i) =
14466 TREE_VEC_ELT (old_args, i - explicit_len);
14469 if (!old_pack)
14471 tree result;
14472 /* Build the deduced *_ARGUMENT_PACK. */
14473 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14475 result = make_node (NONTYPE_ARGUMENT_PACK);
14476 TREE_TYPE (result) =
14477 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14478 TREE_CONSTANT (result) = 1;
14480 else
14481 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14483 SET_ARGUMENT_PACK_ARGS (result, new_args);
14485 /* Note the deduced argument packs for this parameter
14486 pack. */
14487 TMPL_ARG (targs, level, idx) = result;
14489 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14490 && (ARGUMENT_PACK_ARGS (old_pack)
14491 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14493 /* We only had the explicitly-provided arguments before, but
14494 now we have a complete set of arguments. */
14495 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14497 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14498 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14499 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14501 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14502 new_args))
14503 /* Inconsistent unification of this parameter pack. */
14504 return 1;
14507 return 0;
14510 /* Deduce the value of template parameters. TPARMS is the (innermost)
14511 set of template parameters to a template. TARGS is the bindings
14512 for those template parameters, as determined thus far; TARGS may
14513 include template arguments for outer levels of template parameters
14514 as well. PARM is a parameter to a template function, or a
14515 subcomponent of that parameter; ARG is the corresponding argument.
14516 This function attempts to match PARM with ARG in a manner
14517 consistent with the existing assignments in TARGS. If more values
14518 are deduced, then TARGS is updated.
14520 Returns 0 if the type deduction succeeds, 1 otherwise. The
14521 parameter STRICT is a bitwise or of the following flags:
14523 UNIFY_ALLOW_NONE:
14524 Require an exact match between PARM and ARG.
14525 UNIFY_ALLOW_MORE_CV_QUAL:
14526 Allow the deduced ARG to be more cv-qualified (by qualification
14527 conversion) than ARG.
14528 UNIFY_ALLOW_LESS_CV_QUAL:
14529 Allow the deduced ARG to be less cv-qualified than ARG.
14530 UNIFY_ALLOW_DERIVED:
14531 Allow the deduced ARG to be a template base class of ARG,
14532 or a pointer to a template base class of the type pointed to by
14533 ARG.
14534 UNIFY_ALLOW_INTEGER:
14535 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14536 case for more information.
14537 UNIFY_ALLOW_OUTER_LEVEL:
14538 This is the outermost level of a deduction. Used to determine validity
14539 of qualification conversions. A valid qualification conversion must
14540 have const qualified pointers leading up to the inner type which
14541 requires additional CV quals, except at the outer level, where const
14542 is not required [conv.qual]. It would be normal to set this flag in
14543 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14544 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14545 This is the outermost level of a deduction, and PARM can be more CV
14546 qualified at this point.
14547 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14548 This is the outermost level of a deduction, and PARM can be less CV
14549 qualified at this point. */
14551 static int
14552 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14554 int idx;
14555 tree targ;
14556 tree tparm;
14557 int strict_in = strict;
14559 /* I don't think this will do the right thing with respect to types.
14560 But the only case I've seen it in so far has been array bounds, where
14561 signedness is the only information lost, and I think that will be
14562 okay. */
14563 while (TREE_CODE (parm) == NOP_EXPR)
14564 parm = TREE_OPERAND (parm, 0);
14566 if (arg == error_mark_node)
14567 return 1;
14568 if (arg == unknown_type_node
14569 || arg == init_list_type_node)
14570 /* We can't deduce anything from this, but we might get all the
14571 template args from other function args. */
14572 return 0;
14574 /* If PARM uses template parameters, then we can't bail out here,
14575 even if ARG == PARM, since we won't record unifications for the
14576 template parameters. We might need them if we're trying to
14577 figure out which of two things is more specialized. */
14578 if (arg == parm && !uses_template_parms (parm))
14579 return 0;
14581 /* Handle init lists early, so the rest of the function can assume
14582 we're dealing with a type. */
14583 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14585 tree elt, elttype;
14586 unsigned i;
14587 tree orig_parm = parm;
14589 /* Replace T with std::initializer_list<T> for deduction. */
14590 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14591 && flag_deduce_init_list)
14592 parm = listify (parm);
14594 if (!is_std_init_list (parm))
14595 /* We can only deduce from an initializer list argument if the
14596 parameter is std::initializer_list; otherwise this is a
14597 non-deduced context. */
14598 return 0;
14600 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14602 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14604 int elt_strict = strict;
14606 if (elt == error_mark_node)
14607 return 1;
14609 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14611 tree type = TREE_TYPE (elt);
14612 /* It should only be possible to get here for a call. */
14613 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14614 elt_strict |= maybe_adjust_types_for_deduction
14615 (DEDUCE_CALL, &elttype, &type, elt);
14616 elt = type;
14619 if (unify (tparms, targs, elttype, elt, elt_strict))
14620 return 1;
14623 /* If the std::initializer_list<T> deduction worked, replace the
14624 deduced A with std::initializer_list<A>. */
14625 if (orig_parm != parm)
14627 idx = TEMPLATE_TYPE_IDX (orig_parm);
14628 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14629 targ = listify (targ);
14630 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14632 return 0;
14635 /* Immediately reject some pairs that won't unify because of
14636 cv-qualification mismatches. */
14637 if (TREE_CODE (arg) == TREE_CODE (parm)
14638 && TYPE_P (arg)
14639 /* It is the elements of the array which hold the cv quals of an array
14640 type, and the elements might be template type parms. We'll check
14641 when we recurse. */
14642 && TREE_CODE (arg) != ARRAY_TYPE
14643 /* We check the cv-qualifiers when unifying with template type
14644 parameters below. We want to allow ARG `const T' to unify with
14645 PARM `T' for example, when computing which of two templates
14646 is more specialized, for example. */
14647 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14648 && !check_cv_quals_for_unify (strict_in, arg, parm))
14649 return 1;
14651 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14652 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14653 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14654 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14655 strict &= ~UNIFY_ALLOW_DERIVED;
14656 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14657 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14659 switch (TREE_CODE (parm))
14661 case TYPENAME_TYPE:
14662 case SCOPE_REF:
14663 case UNBOUND_CLASS_TEMPLATE:
14664 /* In a type which contains a nested-name-specifier, template
14665 argument values cannot be deduced for template parameters used
14666 within the nested-name-specifier. */
14667 return 0;
14669 case TEMPLATE_TYPE_PARM:
14670 case TEMPLATE_TEMPLATE_PARM:
14671 case BOUND_TEMPLATE_TEMPLATE_PARM:
14672 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14673 if (tparm == error_mark_node)
14674 return 1;
14676 if (TEMPLATE_TYPE_LEVEL (parm)
14677 != template_decl_level (tparm))
14678 /* The PARM is not one we're trying to unify. Just check
14679 to see if it matches ARG. */
14680 return (TREE_CODE (arg) == TREE_CODE (parm)
14681 && same_type_p (parm, arg)) ? 0 : 1;
14682 idx = TEMPLATE_TYPE_IDX (parm);
14683 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14684 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14686 /* Check for mixed types and values. */
14687 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14688 && TREE_CODE (tparm) != TYPE_DECL)
14689 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14690 && TREE_CODE (tparm) != TEMPLATE_DECL))
14691 return 1;
14693 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14695 /* ARG must be constructed from a template class or a template
14696 template parameter. */
14697 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14698 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14699 return 1;
14702 tree parmvec = TYPE_TI_ARGS (parm);
14703 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14704 tree parm_parms
14705 = DECL_INNERMOST_TEMPLATE_PARMS
14706 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14707 int i, len;
14708 int parm_variadic_p = 0;
14710 /* The resolution to DR150 makes clear that default
14711 arguments for an N-argument may not be used to bind T
14712 to a template template parameter with fewer than N
14713 parameters. It is not safe to permit the binding of
14714 default arguments as an extension, as that may change
14715 the meaning of a conforming program. Consider:
14717 struct Dense { static const unsigned int dim = 1; };
14719 template <template <typename> class View,
14720 typename Block>
14721 void operator+(float, View<Block> const&);
14723 template <typename Block,
14724 unsigned int Dim = Block::dim>
14725 struct Lvalue_proxy { operator float() const; };
14727 void
14728 test_1d (void) {
14729 Lvalue_proxy<Dense> p;
14730 float b;
14731 b + p;
14734 Here, if Lvalue_proxy is permitted to bind to View, then
14735 the global operator+ will be used; if they are not, the
14736 Lvalue_proxy will be converted to float. */
14737 if (coerce_template_parms (parm_parms,
14738 argvec,
14739 TYPE_TI_TEMPLATE (parm),
14740 tf_none,
14741 /*require_all_args=*/true,
14742 /*use_default_args=*/false)
14743 == error_mark_node)
14744 return 1;
14746 /* Deduce arguments T, i from TT<T> or TT<i>.
14747 We check each element of PARMVEC and ARGVEC individually
14748 rather than the whole TREE_VEC since they can have
14749 different number of elements. */
14751 parmvec = expand_template_argument_pack (parmvec);
14752 argvec = expand_template_argument_pack (argvec);
14754 len = TREE_VEC_LENGTH (parmvec);
14756 /* Check if the parameters end in a pack, making them
14757 variadic. */
14758 if (len > 0
14759 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14760 parm_variadic_p = 1;
14762 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14763 return 1;
14765 for (i = 0; i < len - parm_variadic_p; ++i)
14767 if (unify (tparms, targs,
14768 TREE_VEC_ELT (parmvec, i),
14769 TREE_VEC_ELT (argvec, i),
14770 UNIFY_ALLOW_NONE))
14771 return 1;
14774 if (parm_variadic_p
14775 && unify_pack_expansion (tparms, targs,
14776 parmvec, argvec,
14777 UNIFY_ALLOW_NONE,
14778 /*call_args_p=*/false,
14779 /*subr=*/false))
14780 return 1;
14782 arg = TYPE_TI_TEMPLATE (arg);
14784 /* Fall through to deduce template name. */
14787 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14788 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14790 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14792 /* Simple cases: Value already set, does match or doesn't. */
14793 if (targ != NULL_TREE && template_args_equal (targ, arg))
14794 return 0;
14795 else if (targ)
14796 return 1;
14798 else
14800 /* If PARM is `const T' and ARG is only `int', we don't have
14801 a match unless we are allowing additional qualification.
14802 If ARG is `const int' and PARM is just `T' that's OK;
14803 that binds `const int' to `T'. */
14804 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14805 arg, parm))
14806 return 1;
14808 /* Consider the case where ARG is `const volatile int' and
14809 PARM is `const T'. Then, T should be `volatile int'. */
14810 arg = cp_build_qualified_type_real
14811 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14812 if (arg == error_mark_node)
14813 return 1;
14815 /* Simple cases: Value already set, does match or doesn't. */
14816 if (targ != NULL_TREE && same_type_p (targ, arg))
14817 return 0;
14818 else if (targ)
14819 return 1;
14821 /* Make sure that ARG is not a variable-sized array. (Note
14822 that were talking about variable-sized arrays (like
14823 `int[n]'), rather than arrays of unknown size (like
14824 `int[]').) We'll get very confused by such a type since
14825 the bound of the array will not be computable in an
14826 instantiation. Besides, such types are not allowed in
14827 ISO C++, so we can do as we please here. */
14828 if (variably_modified_type_p (arg, NULL_TREE))
14829 return 1;
14831 /* Strip typedefs as in convert_template_argument. */
14832 arg = strip_typedefs (arg);
14835 /* If ARG is a parameter pack or an expansion, we cannot unify
14836 against it unless PARM is also a parameter pack. */
14837 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14838 && !template_parameter_pack_p (parm))
14839 return 1;
14841 /* If the argument deduction results is a METHOD_TYPE,
14842 then there is a problem.
14843 METHOD_TYPE doesn't map to any real C++ type the result of
14844 the deduction can not be of that type. */
14845 if (TREE_CODE (arg) == METHOD_TYPE)
14846 return 1;
14848 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14849 return 0;
14851 case TEMPLATE_PARM_INDEX:
14852 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14853 if (tparm == error_mark_node)
14854 return 1;
14856 if (TEMPLATE_PARM_LEVEL (parm)
14857 != template_decl_level (tparm))
14858 /* The PARM is not one we're trying to unify. Just check
14859 to see if it matches ARG. */
14860 return !(TREE_CODE (arg) == TREE_CODE (parm)
14861 && cp_tree_equal (parm, arg));
14863 idx = TEMPLATE_PARM_IDX (parm);
14864 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14866 if (targ)
14867 return !cp_tree_equal (targ, arg);
14869 /* [temp.deduct.type] If, in the declaration of a function template
14870 with a non-type template-parameter, the non-type
14871 template-parameter is used in an expression in the function
14872 parameter-list and, if the corresponding template-argument is
14873 deduced, the template-argument type shall match the type of the
14874 template-parameter exactly, except that a template-argument
14875 deduced from an array bound may be of any integral type.
14876 The non-type parameter might use already deduced type parameters. */
14877 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14878 if (!TREE_TYPE (arg))
14879 /* Template-parameter dependent expression. Just accept it for now.
14880 It will later be processed in convert_template_argument. */
14882 else if (same_type_p (TREE_TYPE (arg), tparm))
14883 /* OK */;
14884 else if ((strict & UNIFY_ALLOW_INTEGER)
14885 && (TREE_CODE (tparm) == INTEGER_TYPE
14886 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14887 /* Convert the ARG to the type of PARM; the deduced non-type
14888 template argument must exactly match the types of the
14889 corresponding parameter. */
14890 arg = fold (build_nop (tparm, arg));
14891 else if (uses_template_parms (tparm))
14892 /* We haven't deduced the type of this parameter yet. Try again
14893 later. */
14894 return 0;
14895 else
14896 return 1;
14898 /* If ARG is a parameter pack or an expansion, we cannot unify
14899 against it unless PARM is also a parameter pack. */
14900 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14901 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14902 return 1;
14904 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14905 return 0;
14907 case PTRMEM_CST:
14909 /* A pointer-to-member constant can be unified only with
14910 another constant. */
14911 if (TREE_CODE (arg) != PTRMEM_CST)
14912 return 1;
14914 /* Just unify the class member. It would be useless (and possibly
14915 wrong, depending on the strict flags) to unify also
14916 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14917 arg refer to the same variable, even if through different
14918 classes. For instance:
14920 struct A { int x; };
14921 struct B : A { };
14923 Unification of &A::x and &B::x must succeed. */
14924 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14925 PTRMEM_CST_MEMBER (arg), strict);
14928 case POINTER_TYPE:
14930 if (TREE_CODE (arg) != POINTER_TYPE)
14931 return 1;
14933 /* [temp.deduct.call]
14935 A can be another pointer or pointer to member type that can
14936 be converted to the deduced A via a qualification
14937 conversion (_conv.qual_).
14939 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14940 This will allow for additional cv-qualification of the
14941 pointed-to types if appropriate. */
14943 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14944 /* The derived-to-base conversion only persists through one
14945 level of pointers. */
14946 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14948 return unify (tparms, targs, TREE_TYPE (parm),
14949 TREE_TYPE (arg), strict);
14952 case REFERENCE_TYPE:
14953 if (TREE_CODE (arg) != REFERENCE_TYPE)
14954 return 1;
14955 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14956 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14958 case ARRAY_TYPE:
14959 if (TREE_CODE (arg) != ARRAY_TYPE)
14960 return 1;
14961 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14962 != (TYPE_DOMAIN (arg) == NULL_TREE))
14963 return 1;
14964 if (TYPE_DOMAIN (parm) != NULL_TREE)
14966 tree parm_max;
14967 tree arg_max;
14968 bool parm_cst;
14969 bool arg_cst;
14971 /* Our representation of array types uses "N - 1" as the
14972 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14973 not an integer constant. We cannot unify arbitrarily
14974 complex expressions, so we eliminate the MINUS_EXPRs
14975 here. */
14976 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14977 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14978 if (!parm_cst)
14980 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14981 parm_max = TREE_OPERAND (parm_max, 0);
14983 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14984 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14985 if (!arg_cst)
14987 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14988 trying to unify the type of a variable with the type
14989 of a template parameter. For example:
14991 template <unsigned int N>
14992 void f (char (&) [N]);
14993 int g();
14994 void h(int i) {
14995 char a[g(i)];
14996 f(a);
14999 Here, the type of the ARG will be "int [g(i)]", and
15000 may be a SAVE_EXPR, etc. */
15001 if (TREE_CODE (arg_max) != MINUS_EXPR)
15002 return 1;
15003 arg_max = TREE_OPERAND (arg_max, 0);
15006 /* If only one of the bounds used a MINUS_EXPR, compensate
15007 by adding one to the other bound. */
15008 if (parm_cst && !arg_cst)
15009 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15010 integer_type_node,
15011 parm_max,
15012 integer_one_node);
15013 else if (arg_cst && !parm_cst)
15014 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15015 integer_type_node,
15016 arg_max,
15017 integer_one_node);
15019 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15020 return 1;
15022 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15023 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15025 case REAL_TYPE:
15026 case COMPLEX_TYPE:
15027 case VECTOR_TYPE:
15028 case INTEGER_TYPE:
15029 case BOOLEAN_TYPE:
15030 case ENUMERAL_TYPE:
15031 case VOID_TYPE:
15032 if (TREE_CODE (arg) != TREE_CODE (parm))
15033 return 1;
15035 /* We have already checked cv-qualification at the top of the
15036 function. */
15037 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15038 return 1;
15040 /* As far as unification is concerned, this wins. Later checks
15041 will invalidate it if necessary. */
15042 return 0;
15044 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15045 /* Type INTEGER_CST can come from ordinary constant template args. */
15046 case INTEGER_CST:
15047 while (TREE_CODE (arg) == NOP_EXPR)
15048 arg = TREE_OPERAND (arg, 0);
15050 if (TREE_CODE (arg) != INTEGER_CST)
15051 return 1;
15052 return !tree_int_cst_equal (parm, arg);
15054 case TREE_VEC:
15056 int i;
15057 if (TREE_CODE (arg) != TREE_VEC)
15058 return 1;
15059 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15060 return 1;
15061 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15062 if (unify (tparms, targs,
15063 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15064 UNIFY_ALLOW_NONE))
15065 return 1;
15066 return 0;
15069 case RECORD_TYPE:
15070 case UNION_TYPE:
15071 if (TREE_CODE (arg) != TREE_CODE (parm))
15072 return 1;
15074 if (TYPE_PTRMEMFUNC_P (parm))
15076 if (!TYPE_PTRMEMFUNC_P (arg))
15077 return 1;
15079 return unify (tparms, targs,
15080 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15081 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15082 strict);
15085 if (CLASSTYPE_TEMPLATE_INFO (parm))
15087 tree t = NULL_TREE;
15089 if (strict_in & UNIFY_ALLOW_DERIVED)
15091 /* First, we try to unify the PARM and ARG directly. */
15092 t = try_class_unification (tparms, targs,
15093 parm, arg);
15095 if (!t)
15097 /* Fallback to the special case allowed in
15098 [temp.deduct.call]:
15100 If P is a class, and P has the form
15101 template-id, then A can be a derived class of
15102 the deduced A. Likewise, if P is a pointer to
15103 a class of the form template-id, A can be a
15104 pointer to a derived class pointed to by the
15105 deduced A. */
15106 t = get_template_base (tparms, targs, parm, arg);
15108 if (!t)
15109 return 1;
15112 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15113 && (CLASSTYPE_TI_TEMPLATE (parm)
15114 == CLASSTYPE_TI_TEMPLATE (arg)))
15115 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15116 Then, we should unify `int' and `U'. */
15117 t = arg;
15118 else
15119 /* There's no chance of unification succeeding. */
15120 return 1;
15122 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15123 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15125 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15126 return 1;
15127 return 0;
15129 case METHOD_TYPE:
15130 case FUNCTION_TYPE:
15132 unsigned int nargs;
15133 tree *args;
15134 tree a;
15135 unsigned int i;
15137 if (TREE_CODE (arg) != TREE_CODE (parm))
15138 return 1;
15140 /* CV qualifications for methods can never be deduced, they must
15141 match exactly. We need to check them explicitly here,
15142 because type_unification_real treats them as any other
15143 cv-qualified parameter. */
15144 if (TREE_CODE (parm) == METHOD_TYPE
15145 && (!check_cv_quals_for_unify
15146 (UNIFY_ALLOW_NONE,
15147 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15148 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15149 return 1;
15151 if (unify (tparms, targs, TREE_TYPE (parm),
15152 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15153 return 1;
15155 nargs = list_length (TYPE_ARG_TYPES (arg));
15156 args = XALLOCAVEC (tree, nargs);
15157 for (a = TYPE_ARG_TYPES (arg), i = 0;
15158 a != NULL_TREE && a != void_list_node;
15159 a = TREE_CHAIN (a), ++i)
15160 args[i] = TREE_VALUE (a);
15161 nargs = i;
15163 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15164 args, nargs, 1, DEDUCE_EXACT,
15165 LOOKUP_NORMAL);
15168 case OFFSET_TYPE:
15169 /* Unify a pointer to member with a pointer to member function, which
15170 deduces the type of the member as a function type. */
15171 if (TYPE_PTRMEMFUNC_P (arg))
15173 tree method_type;
15174 tree fntype;
15176 /* Check top-level cv qualifiers */
15177 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15178 return 1;
15180 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15181 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15182 return 1;
15184 /* Determine the type of the function we are unifying against. */
15185 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15186 fntype =
15187 build_function_type (TREE_TYPE (method_type),
15188 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15190 /* Extract the cv-qualifiers of the member function from the
15191 implicit object parameter and place them on the function
15192 type to be restored later. */
15193 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15194 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15197 if (TREE_CODE (arg) != OFFSET_TYPE)
15198 return 1;
15199 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15200 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15201 return 1;
15202 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15203 strict);
15205 case CONST_DECL:
15206 if (DECL_TEMPLATE_PARM_P (parm))
15207 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15208 if (arg != integral_constant_value (parm))
15209 return 1;
15210 return 0;
15212 case FIELD_DECL:
15213 case TEMPLATE_DECL:
15214 /* Matched cases are handled by the ARG == PARM test above. */
15215 return 1;
15217 case VAR_DECL:
15218 /* A non-type template parameter that is a variable should be a
15219 an integral constant, in which case, it whould have been
15220 folded into its (constant) value. So we should not be getting
15221 a variable here. */
15222 gcc_unreachable ();
15224 case TYPE_ARGUMENT_PACK:
15225 case NONTYPE_ARGUMENT_PACK:
15227 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15228 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15229 int i, len = TREE_VEC_LENGTH (packed_parms);
15230 int argslen = TREE_VEC_LENGTH (packed_args);
15231 int parm_variadic_p = 0;
15233 for (i = 0; i < len; ++i)
15235 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15237 if (i == len - 1)
15238 /* We can unify against something with a trailing
15239 parameter pack. */
15240 parm_variadic_p = 1;
15241 else
15242 /* Since there is something following the pack
15243 expansion, we cannot unify this template argument
15244 list. */
15245 return 0;
15250 /* If we don't have enough arguments to satisfy the parameters
15251 (not counting the pack expression at the end), or we have
15252 too many arguments for a parameter list that doesn't end in
15253 a pack expression, we can't unify. */
15254 if (argslen < (len - parm_variadic_p)
15255 || (argslen > len && !parm_variadic_p))
15256 return 1;
15258 /* Unify all of the parameters that precede the (optional)
15259 pack expression. */
15260 for (i = 0; i < len - parm_variadic_p; ++i)
15262 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15263 TREE_VEC_ELT (packed_args, i), strict))
15264 return 1;
15267 if (parm_variadic_p)
15268 return unify_pack_expansion (tparms, targs,
15269 packed_parms, packed_args,
15270 strict, /*call_args_p=*/false,
15271 /*subr=*/false);
15272 return 0;
15275 break;
15277 case TYPEOF_TYPE:
15278 case DECLTYPE_TYPE:
15279 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15280 nodes. */
15281 return 0;
15283 case ERROR_MARK:
15284 /* Unification fails if we hit an error node. */
15285 return 1;
15287 default:
15288 gcc_assert (EXPR_P (parm));
15290 /* We must be looking at an expression. This can happen with
15291 something like:
15293 template <int I>
15294 void foo(S<I>, S<I + 2>);
15296 This is a "nondeduced context":
15298 [deduct.type]
15300 The nondeduced contexts are:
15302 --A type that is a template-id in which one or more of
15303 the template-arguments is an expression that references
15304 a template-parameter.
15306 In these cases, we assume deduction succeeded, but don't
15307 actually infer any unifications. */
15309 if (!uses_template_parms (parm)
15310 && !template_args_equal (parm, arg))
15311 return 1;
15312 else
15313 return 0;
15317 /* Note that DECL can be defined in this translation unit, if
15318 required. */
15320 static void
15321 mark_definable (tree decl)
15323 tree clone;
15324 DECL_NOT_REALLY_EXTERN (decl) = 1;
15325 FOR_EACH_CLONE (clone, decl)
15326 DECL_NOT_REALLY_EXTERN (clone) = 1;
15329 /* Called if RESULT is explicitly instantiated, or is a member of an
15330 explicitly instantiated class. */
15332 void
15333 mark_decl_instantiated (tree result, int extern_p)
15335 SET_DECL_EXPLICIT_INSTANTIATION (result);
15337 /* If this entity has already been written out, it's too late to
15338 make any modifications. */
15339 if (TREE_ASM_WRITTEN (result))
15340 return;
15342 if (TREE_CODE (result) != FUNCTION_DECL)
15343 /* The TREE_PUBLIC flag for function declarations will have been
15344 set correctly by tsubst. */
15345 TREE_PUBLIC (result) = 1;
15347 /* This might have been set by an earlier implicit instantiation. */
15348 DECL_COMDAT (result) = 0;
15350 if (extern_p)
15351 DECL_NOT_REALLY_EXTERN (result) = 0;
15352 else
15354 mark_definable (result);
15355 /* Always make artificials weak. */
15356 if (DECL_ARTIFICIAL (result) && flag_weak)
15357 comdat_linkage (result);
15358 /* For WIN32 we also want to put explicit instantiations in
15359 linkonce sections. */
15360 else if (TREE_PUBLIC (result))
15361 maybe_make_one_only (result);
15364 /* If EXTERN_P, then this function will not be emitted -- unless
15365 followed by an explicit instantiation, at which point its linkage
15366 will be adjusted. If !EXTERN_P, then this function will be
15367 emitted here. In neither circumstance do we want
15368 import_export_decl to adjust the linkage. */
15369 DECL_INTERFACE_KNOWN (result) = 1;
15372 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15373 important template arguments. If any are missing, we check whether
15374 they're important by using error_mark_node for substituting into any
15375 args that were used for partial ordering (the ones between ARGS and END)
15376 and seeing if it bubbles up. */
15378 static bool
15379 check_undeduced_parms (tree targs, tree args, tree end)
15381 bool found = false;
15382 int i;
15383 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15384 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15386 found = true;
15387 TREE_VEC_ELT (targs, i) = error_mark_node;
15389 if (found)
15391 for (; args != end; args = TREE_CHAIN (args))
15393 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15394 if (substed == error_mark_node)
15395 return true;
15398 return false;
15401 /* Given two function templates PAT1 and PAT2, return:
15403 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15404 -1 if PAT2 is more specialized than PAT1.
15405 0 if neither is more specialized.
15407 LEN indicates the number of parameters we should consider
15408 (defaulted parameters should not be considered).
15410 The 1998 std underspecified function template partial ordering, and
15411 DR214 addresses the issue. We take pairs of arguments, one from
15412 each of the templates, and deduce them against each other. One of
15413 the templates will be more specialized if all the *other*
15414 template's arguments deduce against its arguments and at least one
15415 of its arguments *does* *not* deduce against the other template's
15416 corresponding argument. Deduction is done as for class templates.
15417 The arguments used in deduction have reference and top level cv
15418 qualifiers removed. Iff both arguments were originally reference
15419 types *and* deduction succeeds in both directions, the template
15420 with the more cv-qualified argument wins for that pairing (if
15421 neither is more cv-qualified, they both are equal). Unlike regular
15422 deduction, after all the arguments have been deduced in this way,
15423 we do *not* verify the deduced template argument values can be
15424 substituted into non-deduced contexts.
15426 The logic can be a bit confusing here, because we look at deduce1 and
15427 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15428 can find template arguments for pat1 to make arg1 look like arg2, that
15429 means that arg2 is at least as specialized as arg1. */
15432 more_specialized_fn (tree pat1, tree pat2, int len)
15434 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15435 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15436 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15437 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15438 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15439 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15440 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15441 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15442 tree origs1, origs2;
15443 bool lose1 = false;
15444 bool lose2 = false;
15446 /* Remove the this parameter from non-static member functions. If
15447 one is a non-static member function and the other is not a static
15448 member function, remove the first parameter from that function
15449 also. This situation occurs for operator functions where we
15450 locate both a member function (with this pointer) and non-member
15451 operator (with explicit first operand). */
15452 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15454 len--; /* LEN is the number of significant arguments for DECL1 */
15455 args1 = TREE_CHAIN (args1);
15456 if (!DECL_STATIC_FUNCTION_P (decl2))
15457 args2 = TREE_CHAIN (args2);
15459 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15461 args2 = TREE_CHAIN (args2);
15462 if (!DECL_STATIC_FUNCTION_P (decl1))
15464 len--;
15465 args1 = TREE_CHAIN (args1);
15469 /* If only one is a conversion operator, they are unordered. */
15470 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15471 return 0;
15473 /* Consider the return type for a conversion function */
15474 if (DECL_CONV_FN_P (decl1))
15476 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15477 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15478 len++;
15481 processing_template_decl++;
15483 origs1 = args1;
15484 origs2 = args2;
15486 while (len--
15487 /* Stop when an ellipsis is seen. */
15488 && args1 != NULL_TREE && args2 != NULL_TREE)
15490 tree arg1 = TREE_VALUE (args1);
15491 tree arg2 = TREE_VALUE (args2);
15492 int deduce1, deduce2;
15493 int quals1 = -1;
15494 int quals2 = -1;
15496 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15497 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15499 /* When both arguments are pack expansions, we need only
15500 unify the patterns themselves. */
15501 arg1 = PACK_EXPANSION_PATTERN (arg1);
15502 arg2 = PACK_EXPANSION_PATTERN (arg2);
15504 /* This is the last comparison we need to do. */
15505 len = 0;
15508 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15510 arg1 = TREE_TYPE (arg1);
15511 quals1 = cp_type_quals (arg1);
15514 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15516 arg2 = TREE_TYPE (arg2);
15517 quals2 = cp_type_quals (arg2);
15520 if ((quals1 < 0) != (quals2 < 0))
15522 /* Only of the args is a reference, see if we should apply
15523 array/function pointer decay to it. This is not part of
15524 DR214, but is, IMHO, consistent with the deduction rules
15525 for the function call itself, and with our earlier
15526 implementation of the underspecified partial ordering
15527 rules. (nathan). */
15528 if (quals1 >= 0)
15530 switch (TREE_CODE (arg1))
15532 case ARRAY_TYPE:
15533 arg1 = TREE_TYPE (arg1);
15534 /* FALLTHROUGH. */
15535 case FUNCTION_TYPE:
15536 arg1 = build_pointer_type (arg1);
15537 break;
15539 default:
15540 break;
15543 else
15545 switch (TREE_CODE (arg2))
15547 case ARRAY_TYPE:
15548 arg2 = TREE_TYPE (arg2);
15549 /* FALLTHROUGH. */
15550 case FUNCTION_TYPE:
15551 arg2 = build_pointer_type (arg2);
15552 break;
15554 default:
15555 break;
15560 arg1 = TYPE_MAIN_VARIANT (arg1);
15561 arg2 = TYPE_MAIN_VARIANT (arg2);
15563 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15565 int i, len2 = list_length (args2);
15566 tree parmvec = make_tree_vec (1);
15567 tree argvec = make_tree_vec (len2);
15568 tree ta = args2;
15570 /* Setup the parameter vector, which contains only ARG1. */
15571 TREE_VEC_ELT (parmvec, 0) = arg1;
15573 /* Setup the argument vector, which contains the remaining
15574 arguments. */
15575 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15576 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15578 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15579 argvec, UNIFY_ALLOW_NONE,
15580 /*call_args_p=*/false,
15581 /*subr=*/0);
15583 /* We cannot deduce in the other direction, because ARG1 is
15584 a pack expansion but ARG2 is not. */
15585 deduce2 = 0;
15587 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15589 int i, len1 = list_length (args1);
15590 tree parmvec = make_tree_vec (1);
15591 tree argvec = make_tree_vec (len1);
15592 tree ta = args1;
15594 /* Setup the parameter vector, which contains only ARG1. */
15595 TREE_VEC_ELT (parmvec, 0) = arg2;
15597 /* Setup the argument vector, which contains the remaining
15598 arguments. */
15599 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15600 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15602 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15603 argvec, UNIFY_ALLOW_NONE,
15604 /*call_args_p=*/false,
15605 /*subr=*/0);
15607 /* We cannot deduce in the other direction, because ARG2 is
15608 a pack expansion but ARG1 is not.*/
15609 deduce1 = 0;
15612 else
15614 /* The normal case, where neither argument is a pack
15615 expansion. */
15616 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15617 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15620 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15621 arg2, then arg2 is not as specialized as arg1. */
15622 if (!deduce1)
15623 lose2 = true;
15624 if (!deduce2)
15625 lose1 = true;
15627 /* "If, for a given type, deduction succeeds in both directions
15628 (i.e., the types are identical after the transformations above)
15629 and if the type from the argument template is more cv-qualified
15630 than the type from the parameter template (as described above)
15631 that type is considered to be more specialized than the other. If
15632 neither type is more cv-qualified than the other then neither type
15633 is more specialized than the other." */
15635 if (deduce1 && deduce2
15636 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
15638 if ((quals1 & quals2) == quals2)
15639 lose2 = true;
15640 if ((quals1 & quals2) == quals1)
15641 lose1 = true;
15644 if (lose1 && lose2)
15645 /* We've failed to deduce something in either direction.
15646 These must be unordered. */
15647 break;
15649 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15650 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15651 /* We have already processed all of the arguments in our
15652 handing of the pack expansion type. */
15653 len = 0;
15655 args1 = TREE_CHAIN (args1);
15656 args2 = TREE_CHAIN (args2);
15659 /* "In most cases, all template parameters must have values in order for
15660 deduction to succeed, but for partial ordering purposes a template
15661 parameter may remain without a value provided it is not used in the
15662 types being used for partial ordering."
15664 Thus, if we are missing any of the targs1 we need to substitute into
15665 origs1, then pat2 is not as specialized as pat1. This can happen when
15666 there is a nondeduced context. */
15667 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15668 lose2 = true;
15669 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15670 lose1 = true;
15672 processing_template_decl--;
15674 /* All things being equal, if the next argument is a pack expansion
15675 for one function but not for the other, prefer the
15676 non-variadic function. FIXME this is bogus; see c++/41958. */
15677 if (lose1 == lose2
15678 && args1 && TREE_VALUE (args1)
15679 && args2 && TREE_VALUE (args2))
15681 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15682 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15685 if (lose1 == lose2)
15686 return 0;
15687 else if (!lose1)
15688 return 1;
15689 else
15690 return -1;
15693 /* Determine which of two partial specializations is more specialized.
15695 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15696 to the first partial specialization. The TREE_VALUE is the
15697 innermost set of template parameters for the partial
15698 specialization. PAT2 is similar, but for the second template.
15700 Return 1 if the first partial specialization is more specialized;
15701 -1 if the second is more specialized; 0 if neither is more
15702 specialized.
15704 See [temp.class.order] for information about determining which of
15705 two templates is more specialized. */
15707 static int
15708 more_specialized_class (tree pat1, tree pat2)
15710 tree targs;
15711 tree tmpl1, tmpl2;
15712 int winner = 0;
15713 bool any_deductions = false;
15715 tmpl1 = TREE_TYPE (pat1);
15716 tmpl2 = TREE_TYPE (pat2);
15718 /* Just like what happens for functions, if we are ordering between
15719 different class template specializations, we may encounter dependent
15720 types in the arguments, and we need our dependency check functions
15721 to behave correctly. */
15722 ++processing_template_decl;
15723 targs = get_class_bindings (TREE_VALUE (pat1),
15724 CLASSTYPE_TI_ARGS (tmpl1),
15725 CLASSTYPE_TI_ARGS (tmpl2));
15726 if (targs)
15728 --winner;
15729 any_deductions = true;
15732 targs = get_class_bindings (TREE_VALUE (pat2),
15733 CLASSTYPE_TI_ARGS (tmpl2),
15734 CLASSTYPE_TI_ARGS (tmpl1));
15735 if (targs)
15737 ++winner;
15738 any_deductions = true;
15740 --processing_template_decl;
15742 /* In the case of a tie where at least one of the class templates
15743 has a parameter pack at the end, the template with the most
15744 non-packed parameters wins. */
15745 if (winner == 0
15746 && any_deductions
15747 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15748 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15750 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15751 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15752 int len1 = TREE_VEC_LENGTH (args1);
15753 int len2 = TREE_VEC_LENGTH (args2);
15755 /* We don't count the pack expansion at the end. */
15756 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15757 --len1;
15758 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15759 --len2;
15761 if (len1 > len2)
15762 return 1;
15763 else if (len1 < len2)
15764 return -1;
15767 return winner;
15770 /* Return the template arguments that will produce the function signature
15771 DECL from the function template FN, with the explicit template
15772 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15773 also match. Return NULL_TREE if no satisfactory arguments could be
15774 found. */
15776 static tree
15777 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15779 int ntparms = DECL_NTPARMS (fn);
15780 tree targs = make_tree_vec (ntparms);
15781 tree decl_type;
15782 tree decl_arg_types;
15783 tree *args;
15784 unsigned int nargs, ix;
15785 tree arg;
15787 /* Substitute the explicit template arguments into the type of DECL.
15788 The call to fn_type_unification will handle substitution into the
15789 FN. */
15790 decl_type = TREE_TYPE (decl);
15791 if (explicit_args && uses_template_parms (decl_type))
15793 tree tmpl;
15794 tree converted_args;
15796 if (DECL_TEMPLATE_INFO (decl))
15797 tmpl = DECL_TI_TEMPLATE (decl);
15798 else
15799 /* We can get here for some invalid specializations. */
15800 return NULL_TREE;
15802 converted_args
15803 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15804 explicit_args, NULL_TREE,
15805 tf_none,
15806 /*require_all_args=*/false,
15807 /*use_default_args=*/false);
15808 if (converted_args == error_mark_node)
15809 return NULL_TREE;
15811 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15812 if (decl_type == error_mark_node)
15813 return NULL_TREE;
15816 /* Never do unification on the 'this' parameter. */
15817 decl_arg_types = skip_artificial_parms_for (decl,
15818 TYPE_ARG_TYPES (decl_type));
15820 nargs = list_length (decl_arg_types);
15821 args = XALLOCAVEC (tree, nargs);
15822 for (arg = decl_arg_types, ix = 0;
15823 arg != NULL_TREE && arg != void_list_node;
15824 arg = TREE_CHAIN (arg), ++ix)
15825 args[ix] = TREE_VALUE (arg);
15827 if (fn_type_unification (fn, explicit_args, targs,
15828 args, ix,
15829 (check_rettype || DECL_CONV_FN_P (fn)
15830 ? TREE_TYPE (decl_type) : NULL_TREE),
15831 DEDUCE_EXACT, LOOKUP_NORMAL))
15832 return NULL_TREE;
15834 return targs;
15837 /* Return the innermost template arguments that, when applied to a
15838 template specialization whose innermost template parameters are
15839 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15840 ARGS.
15842 For example, suppose we have:
15844 template <class T, class U> struct S {};
15845 template <class T> struct S<T*, int> {};
15847 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15848 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15849 int}. The resulting vector will be {double}, indicating that `T'
15850 is bound to `double'. */
15852 static tree
15853 get_class_bindings (tree tparms, tree spec_args, tree args)
15855 int i, ntparms = TREE_VEC_LENGTH (tparms);
15856 tree deduced_args;
15857 tree innermost_deduced_args;
15859 innermost_deduced_args = make_tree_vec (ntparms);
15860 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15862 deduced_args = copy_node (args);
15863 SET_TMPL_ARGS_LEVEL (deduced_args,
15864 TMPL_ARGS_DEPTH (deduced_args),
15865 innermost_deduced_args);
15867 else
15868 deduced_args = innermost_deduced_args;
15870 if (unify (tparms, deduced_args,
15871 INNERMOST_TEMPLATE_ARGS (spec_args),
15872 INNERMOST_TEMPLATE_ARGS (args),
15873 UNIFY_ALLOW_NONE))
15874 return NULL_TREE;
15876 for (i = 0; i < ntparms; ++i)
15877 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15878 return NULL_TREE;
15880 /* Verify that nondeduced template arguments agree with the type
15881 obtained from argument deduction.
15883 For example:
15885 struct A { typedef int X; };
15886 template <class T, class U> struct C {};
15887 template <class T> struct C<T, typename T::X> {};
15889 Then with the instantiation `C<A, int>', we can deduce that
15890 `T' is `A' but unify () does not check whether `typename T::X'
15891 is `int'. */
15892 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15893 if (spec_args == error_mark_node
15894 /* We only need to check the innermost arguments; the other
15895 arguments will always agree. */
15896 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15897 INNERMOST_TEMPLATE_ARGS (args)))
15898 return NULL_TREE;
15900 /* Now that we have bindings for all of the template arguments,
15901 ensure that the arguments deduced for the template template
15902 parameters have compatible template parameter lists. See the use
15903 of template_template_parm_bindings_ok_p in fn_type_unification
15904 for more information. */
15905 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15906 return NULL_TREE;
15908 return deduced_args;
15911 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15912 Return the TREE_LIST node with the most specialized template, if
15913 any. If there is no most specialized template, the error_mark_node
15914 is returned.
15916 Note that this function does not look at, or modify, the
15917 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15918 returned is one of the elements of INSTANTIATIONS, callers may
15919 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15920 and retrieve it from the value returned. */
15922 tree
15923 most_specialized_instantiation (tree templates)
15925 tree fn, champ;
15927 ++processing_template_decl;
15929 champ = templates;
15930 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15932 int fate = 0;
15934 if (get_bindings (TREE_VALUE (champ),
15935 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15936 NULL_TREE, /*check_ret=*/false))
15937 fate--;
15939 if (get_bindings (TREE_VALUE (fn),
15940 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15941 NULL_TREE, /*check_ret=*/false))
15942 fate++;
15944 if (fate == -1)
15945 champ = fn;
15946 else if (!fate)
15948 /* Equally specialized, move to next function. If there
15949 is no next function, nothing's most specialized. */
15950 fn = TREE_CHAIN (fn);
15951 champ = fn;
15952 if (!fn)
15953 break;
15957 if (champ)
15958 /* Now verify that champ is better than everything earlier in the
15959 instantiation list. */
15960 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15961 if (get_bindings (TREE_VALUE (champ),
15962 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15963 NULL_TREE, /*check_ret=*/false)
15964 || !get_bindings (TREE_VALUE (fn),
15965 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15966 NULL_TREE, /*check_ret=*/false))
15968 champ = NULL_TREE;
15969 break;
15972 processing_template_decl--;
15974 if (!champ)
15975 return error_mark_node;
15977 return champ;
15980 /* If DECL is a specialization of some template, return the most
15981 general such template. Otherwise, returns NULL_TREE.
15983 For example, given:
15985 template <class T> struct S { template <class U> void f(U); };
15987 if TMPL is `template <class U> void S<int>::f(U)' this will return
15988 the full template. This function will not trace past partial
15989 specializations, however. For example, given in addition:
15991 template <class T> struct S<T*> { template <class U> void f(U); };
15993 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15994 `template <class T> template <class U> S<T*>::f(U)'. */
15996 tree
15997 most_general_template (tree decl)
15999 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16000 an immediate specialization. */
16001 if (TREE_CODE (decl) == FUNCTION_DECL)
16003 if (DECL_TEMPLATE_INFO (decl)) {
16004 decl = DECL_TI_TEMPLATE (decl);
16006 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16007 template friend. */
16008 if (TREE_CODE (decl) != TEMPLATE_DECL)
16009 return NULL_TREE;
16010 } else
16011 return NULL_TREE;
16014 /* Look for more and more general templates. */
16015 while (DECL_TEMPLATE_INFO (decl))
16017 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16018 (See cp-tree.h for details.) */
16019 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16020 break;
16022 if (CLASS_TYPE_P (TREE_TYPE (decl))
16023 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16024 break;
16026 /* Stop if we run into an explicitly specialized class template. */
16027 if (!DECL_NAMESPACE_SCOPE_P (decl)
16028 && DECL_CONTEXT (decl)
16029 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16030 break;
16032 decl = DECL_TI_TEMPLATE (decl);
16035 return decl;
16038 /* Return the most specialized of the class template partial
16039 specializations of TMPL which can produce TYPE, a specialization of
16040 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16041 a _TYPE node corresponding to the partial specialization, while the
16042 TREE_PURPOSE is the set of template arguments that must be
16043 substituted into the TREE_TYPE in order to generate TYPE.
16045 If the choice of partial specialization is ambiguous, a diagnostic
16046 is issued, and the error_mark_node is returned. If there are no
16047 partial specializations of TMPL matching TYPE, then NULL_TREE is
16048 returned. */
16050 static tree
16051 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16053 tree list = NULL_TREE;
16054 tree t;
16055 tree champ;
16056 int fate;
16057 bool ambiguous_p;
16058 tree args;
16059 tree outer_args = NULL_TREE;
16061 tmpl = most_general_template (tmpl);
16062 args = CLASSTYPE_TI_ARGS (type);
16064 /* For determining which partial specialization to use, only the
16065 innermost args are interesting. */
16066 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16068 outer_args = strip_innermost_template_args (args, 1);
16069 args = INNERMOST_TEMPLATE_ARGS (args);
16072 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16074 tree partial_spec_args;
16075 tree spec_args;
16076 tree parms = TREE_VALUE (t);
16078 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16080 ++processing_template_decl;
16082 if (outer_args)
16084 int i;
16086 /* Discard the outer levels of args, and then substitute in the
16087 template args from the enclosing class. */
16088 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16089 partial_spec_args = tsubst_template_args
16090 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16092 /* PARMS already refers to just the innermost parms, but the
16093 template parms in partial_spec_args had their levels lowered
16094 by tsubst, so we need to do the same for the parm list. We
16095 can't just tsubst the TREE_VEC itself, as tsubst wants to
16096 treat a TREE_VEC as an argument vector. */
16097 parms = copy_node (parms);
16098 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16099 TREE_VEC_ELT (parms, i) =
16100 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16104 partial_spec_args =
16105 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16106 add_to_template_args (outer_args,
16107 partial_spec_args),
16108 tmpl, tf_none,
16109 /*require_all_args=*/true,
16110 /*use_default_args=*/true);
16112 --processing_template_decl;
16114 if (partial_spec_args == error_mark_node)
16115 return error_mark_node;
16117 spec_args = get_class_bindings (parms,
16118 partial_spec_args,
16119 args);
16120 if (spec_args)
16122 if (outer_args)
16123 spec_args = add_to_template_args (outer_args, spec_args);
16124 list = tree_cons (spec_args, TREE_VALUE (t), list);
16125 TREE_TYPE (list) = TREE_TYPE (t);
16129 if (! list)
16130 return NULL_TREE;
16132 ambiguous_p = false;
16133 t = list;
16134 champ = t;
16135 t = TREE_CHAIN (t);
16136 for (; t; t = TREE_CHAIN (t))
16138 fate = more_specialized_class (champ, t);
16139 if (fate == 1)
16141 else
16143 if (fate == 0)
16145 t = TREE_CHAIN (t);
16146 if (! t)
16148 ambiguous_p = true;
16149 break;
16152 champ = t;
16156 if (!ambiguous_p)
16157 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16159 fate = more_specialized_class (champ, t);
16160 if (fate != 1)
16162 ambiguous_p = true;
16163 break;
16167 if (ambiguous_p)
16169 const char *str;
16170 char *spaces = NULL;
16171 if (!(complain & tf_error))
16172 return error_mark_node;
16173 error ("ambiguous class template instantiation for %q#T", type);
16174 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
16175 for (t = list; t; t = TREE_CHAIN (t))
16177 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16178 spaces = spaces ? spaces : get_spaces (str);
16180 free (spaces);
16181 return error_mark_node;
16184 return champ;
16187 /* Explicitly instantiate DECL. */
16189 void
16190 do_decl_instantiation (tree decl, tree storage)
16192 tree result = NULL_TREE;
16193 int extern_p = 0;
16195 if (!decl || decl == error_mark_node)
16196 /* An error occurred, for which grokdeclarator has already issued
16197 an appropriate message. */
16198 return;
16199 else if (! DECL_LANG_SPECIFIC (decl))
16201 error ("explicit instantiation of non-template %q#D", decl);
16202 return;
16204 else if (TREE_CODE (decl) == VAR_DECL)
16206 /* There is an asymmetry here in the way VAR_DECLs and
16207 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16208 the latter, the DECL we get back will be marked as a
16209 template instantiation, and the appropriate
16210 DECL_TEMPLATE_INFO will be set up. This does not happen for
16211 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16212 should handle VAR_DECLs as it currently handles
16213 FUNCTION_DECLs. */
16214 if (!DECL_CLASS_SCOPE_P (decl))
16216 error ("%qD is not a static data member of a class template", decl);
16217 return;
16219 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16220 if (!result || TREE_CODE (result) != VAR_DECL)
16222 error ("no matching template for %qD found", decl);
16223 return;
16225 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16227 error ("type %qT for explicit instantiation %qD does not match "
16228 "declared type %qT", TREE_TYPE (result), decl,
16229 TREE_TYPE (decl));
16230 return;
16233 else if (TREE_CODE (decl) != FUNCTION_DECL)
16235 error ("explicit instantiation of %q#D", decl);
16236 return;
16238 else
16239 result = decl;
16241 /* Check for various error cases. Note that if the explicit
16242 instantiation is valid the RESULT will currently be marked as an
16243 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16244 until we get here. */
16246 if (DECL_TEMPLATE_SPECIALIZATION (result))
16248 /* DR 259 [temp.spec].
16250 Both an explicit instantiation and a declaration of an explicit
16251 specialization shall not appear in a program unless the explicit
16252 instantiation follows a declaration of the explicit specialization.
16254 For a given set of template parameters, if an explicit
16255 instantiation of a template appears after a declaration of an
16256 explicit specialization for that template, the explicit
16257 instantiation has no effect. */
16258 return;
16260 else if (DECL_EXPLICIT_INSTANTIATION (result))
16262 /* [temp.spec]
16264 No program shall explicitly instantiate any template more
16265 than once.
16267 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16268 the first instantiation was `extern' and the second is not,
16269 and EXTERN_P for the opposite case. */
16270 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16271 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16272 /* If an "extern" explicit instantiation follows an ordinary
16273 explicit instantiation, the template is instantiated. */
16274 if (extern_p)
16275 return;
16277 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16279 error ("no matching template for %qD found", result);
16280 return;
16282 else if (!DECL_TEMPLATE_INFO (result))
16284 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16285 return;
16288 if (storage == NULL_TREE)
16290 else if (storage == ridpointers[(int) RID_EXTERN])
16292 if (!in_system_header && (cxx_dialect == cxx98))
16293 pedwarn (input_location, OPT_pedantic,
16294 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16295 "instantiations");
16296 extern_p = 1;
16298 else
16299 error ("storage class %qD applied to template instantiation", storage);
16301 check_explicit_instantiation_namespace (result);
16302 mark_decl_instantiated (result, extern_p);
16303 if (! extern_p)
16304 instantiate_decl (result, /*defer_ok=*/1,
16305 /*expl_inst_class_mem_p=*/false);
16308 static void
16309 mark_class_instantiated (tree t, int extern_p)
16311 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16312 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16313 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16314 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16315 if (! extern_p)
16317 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16318 rest_of_type_compilation (t, 1);
16322 /* Called from do_type_instantiation through binding_table_foreach to
16323 do recursive instantiation for the type bound in ENTRY. */
16324 static void
16325 bt_instantiate_type_proc (binding_entry entry, void *data)
16327 tree storage = *(tree *) data;
16329 if (MAYBE_CLASS_TYPE_P (entry->type)
16330 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16331 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16334 /* Called from do_type_instantiation to instantiate a member
16335 (a member function or a static member variable) of an
16336 explicitly instantiated class template. */
16337 static void
16338 instantiate_class_member (tree decl, int extern_p)
16340 mark_decl_instantiated (decl, extern_p);
16341 if (! extern_p)
16342 instantiate_decl (decl, /*defer_ok=*/1,
16343 /*expl_inst_class_mem_p=*/true);
16346 /* Perform an explicit instantiation of template class T. STORAGE, if
16347 non-null, is the RID for extern, inline or static. COMPLAIN is
16348 nonzero if this is called from the parser, zero if called recursively,
16349 since the standard is unclear (as detailed below). */
16351 void
16352 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16354 int extern_p = 0;
16355 int nomem_p = 0;
16356 int static_p = 0;
16357 int previous_instantiation_extern_p = 0;
16359 if (TREE_CODE (t) == TYPE_DECL)
16360 t = TREE_TYPE (t);
16362 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16364 error ("explicit instantiation of non-template type %qT", t);
16365 return;
16368 complete_type (t);
16370 if (!COMPLETE_TYPE_P (t))
16372 if (complain & tf_error)
16373 error ("explicit instantiation of %q#T before definition of template",
16375 return;
16378 if (storage != NULL_TREE)
16380 if (!in_system_header)
16382 if (storage == ridpointers[(int) RID_EXTERN])
16384 if (cxx_dialect == cxx98)
16385 pedwarn (input_location, OPT_pedantic,
16386 "ISO C++ 1998 forbids the use of %<extern%> on "
16387 "explicit instantiations");
16389 else
16390 pedwarn (input_location, OPT_pedantic,
16391 "ISO C++ forbids the use of %qE"
16392 " on explicit instantiations", storage);
16395 if (storage == ridpointers[(int) RID_INLINE])
16396 nomem_p = 1;
16397 else if (storage == ridpointers[(int) RID_EXTERN])
16398 extern_p = 1;
16399 else if (storage == ridpointers[(int) RID_STATIC])
16400 static_p = 1;
16401 else
16403 error ("storage class %qD applied to template instantiation",
16404 storage);
16405 extern_p = 0;
16409 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16411 /* DR 259 [temp.spec].
16413 Both an explicit instantiation and a declaration of an explicit
16414 specialization shall not appear in a program unless the explicit
16415 instantiation follows a declaration of the explicit specialization.
16417 For a given set of template parameters, if an explicit
16418 instantiation of a template appears after a declaration of an
16419 explicit specialization for that template, the explicit
16420 instantiation has no effect. */
16421 return;
16423 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16425 /* [temp.spec]
16427 No program shall explicitly instantiate any template more
16428 than once.
16430 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16431 instantiation was `extern'. If EXTERN_P then the second is.
16432 These cases are OK. */
16433 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16435 if (!previous_instantiation_extern_p && !extern_p
16436 && (complain & tf_error))
16437 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16439 /* If we've already instantiated the template, just return now. */
16440 if (!CLASSTYPE_INTERFACE_ONLY (t))
16441 return;
16444 check_explicit_instantiation_namespace (TYPE_NAME (t));
16445 mark_class_instantiated (t, extern_p);
16447 if (nomem_p)
16448 return;
16451 tree tmp;
16453 /* In contrast to implicit instantiation, where only the
16454 declarations, and not the definitions, of members are
16455 instantiated, we have here:
16457 [temp.explicit]
16459 The explicit instantiation of a class template specialization
16460 implies the instantiation of all of its members not
16461 previously explicitly specialized in the translation unit
16462 containing the explicit instantiation.
16464 Of course, we can't instantiate member template classes, since
16465 we don't have any arguments for them. Note that the standard
16466 is unclear on whether the instantiation of the members are
16467 *explicit* instantiations or not. However, the most natural
16468 interpretation is that it should be an explicit instantiation. */
16470 if (! static_p)
16471 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
16472 if (TREE_CODE (tmp) == FUNCTION_DECL
16473 && DECL_TEMPLATE_INSTANTIATION (tmp))
16474 instantiate_class_member (tmp, extern_p);
16476 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
16477 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16478 instantiate_class_member (tmp, extern_p);
16480 if (CLASSTYPE_NESTED_UTDS (t))
16481 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16482 bt_instantiate_type_proc, &storage);
16486 /* Given a function DECL, which is a specialization of TMPL, modify
16487 DECL to be a re-instantiation of TMPL with the same template
16488 arguments. TMPL should be the template into which tsubst'ing
16489 should occur for DECL, not the most general template.
16491 One reason for doing this is a scenario like this:
16493 template <class T>
16494 void f(const T&, int i);
16496 void g() { f(3, 7); }
16498 template <class T>
16499 void f(const T& t, const int i) { }
16501 Note that when the template is first instantiated, with
16502 instantiate_template, the resulting DECL will have no name for the
16503 first parameter, and the wrong type for the second. So, when we go
16504 to instantiate the DECL, we regenerate it. */
16506 static void
16507 regenerate_decl_from_template (tree decl, tree tmpl)
16509 /* The arguments used to instantiate DECL, from the most general
16510 template. */
16511 tree args;
16512 tree code_pattern;
16514 args = DECL_TI_ARGS (decl);
16515 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16517 /* Make sure that we can see identifiers, and compute access
16518 correctly. */
16519 push_access_scope (decl);
16521 if (TREE_CODE (decl) == FUNCTION_DECL)
16523 tree decl_parm;
16524 tree pattern_parm;
16525 tree specs;
16526 int args_depth;
16527 int parms_depth;
16529 args_depth = TMPL_ARGS_DEPTH (args);
16530 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16531 if (args_depth > parms_depth)
16532 args = get_innermost_template_args (args, parms_depth);
16534 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16535 args, tf_error, NULL_TREE);
16536 if (specs)
16537 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16538 specs);
16540 /* Merge parameter declarations. */
16541 decl_parm = skip_artificial_parms_for (decl,
16542 DECL_ARGUMENTS (decl));
16543 pattern_parm
16544 = skip_artificial_parms_for (code_pattern,
16545 DECL_ARGUMENTS (code_pattern));
16546 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16548 tree parm_type;
16549 tree attributes;
16551 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16552 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16553 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16554 NULL_TREE);
16555 parm_type = type_decays_to (parm_type);
16556 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16557 TREE_TYPE (decl_parm) = parm_type;
16558 attributes = DECL_ATTRIBUTES (pattern_parm);
16559 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16561 DECL_ATTRIBUTES (decl_parm) = attributes;
16562 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16564 decl_parm = DECL_CHAIN (decl_parm);
16565 pattern_parm = DECL_CHAIN (pattern_parm);
16567 /* Merge any parameters that match with the function parameter
16568 pack. */
16569 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16571 int i, len;
16572 tree expanded_types;
16573 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16574 the parameters in this function parameter pack. */
16575 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16576 args, tf_error, NULL_TREE);
16577 len = TREE_VEC_LENGTH (expanded_types);
16578 for (i = 0; i < len; i++)
16580 tree parm_type;
16581 tree attributes;
16583 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16584 /* Rename the parameter to include the index. */
16585 DECL_NAME (decl_parm) =
16586 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16587 parm_type = TREE_VEC_ELT (expanded_types, i);
16588 parm_type = type_decays_to (parm_type);
16589 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16590 TREE_TYPE (decl_parm) = parm_type;
16591 attributes = DECL_ATTRIBUTES (pattern_parm);
16592 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16594 DECL_ATTRIBUTES (decl_parm) = attributes;
16595 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16597 decl_parm = DECL_CHAIN (decl_parm);
16600 /* Merge additional specifiers from the CODE_PATTERN. */
16601 if (DECL_DECLARED_INLINE_P (code_pattern)
16602 && !DECL_DECLARED_INLINE_P (decl))
16603 DECL_DECLARED_INLINE_P (decl) = 1;
16605 else if (TREE_CODE (decl) == VAR_DECL)
16607 DECL_INITIAL (decl) =
16608 tsubst_expr (DECL_INITIAL (code_pattern), args,
16609 tf_error, DECL_TI_TEMPLATE (decl),
16610 /*integral_constant_expression_p=*/false);
16611 if (VAR_HAD_UNKNOWN_BOUND (decl))
16612 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
16613 tf_error, DECL_TI_TEMPLATE (decl));
16615 else
16616 gcc_unreachable ();
16618 pop_access_scope (decl);
16621 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16622 substituted to get DECL. */
16624 tree
16625 template_for_substitution (tree decl)
16627 tree tmpl = DECL_TI_TEMPLATE (decl);
16629 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16630 for the instantiation. This is not always the most general
16631 template. Consider, for example:
16633 template <class T>
16634 struct S { template <class U> void f();
16635 template <> void f<int>(); };
16637 and an instantiation of S<double>::f<int>. We want TD to be the
16638 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16639 while (/* An instantiation cannot have a definition, so we need a
16640 more general template. */
16641 DECL_TEMPLATE_INSTANTIATION (tmpl)
16642 /* We must also deal with friend templates. Given:
16644 template <class T> struct S {
16645 template <class U> friend void f() {};
16648 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16649 so far as the language is concerned, but that's still
16650 where we get the pattern for the instantiation from. On
16651 other hand, if the definition comes outside the class, say:
16653 template <class T> struct S {
16654 template <class U> friend void f();
16656 template <class U> friend void f() {}
16658 we don't need to look any further. That's what the check for
16659 DECL_INITIAL is for. */
16660 || (TREE_CODE (decl) == FUNCTION_DECL
16661 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16662 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16664 /* The present template, TD, should not be a definition. If it
16665 were a definition, we should be using it! Note that we
16666 cannot restructure the loop to just keep going until we find
16667 a template with a definition, since that might go too far if
16668 a specialization was declared, but not defined. */
16669 gcc_assert (TREE_CODE (decl) != VAR_DECL
16670 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16672 /* Fetch the more general template. */
16673 tmpl = DECL_TI_TEMPLATE (tmpl);
16676 return tmpl;
16679 /* Returns true if we need to instantiate this template instance even if we
16680 know we aren't going to emit it.. */
16682 bool
16683 always_instantiate_p (tree decl)
16685 /* We always instantiate inline functions so that we can inline them. An
16686 explicit instantiation declaration prohibits implicit instantiation of
16687 non-inline functions. With high levels of optimization, we would
16688 normally inline non-inline functions -- but we're not allowed to do
16689 that for "extern template" functions. Therefore, we check
16690 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16691 return ((TREE_CODE (decl) == FUNCTION_DECL
16692 && DECL_DECLARED_INLINE_P (decl))
16693 /* And we need to instantiate static data members so that
16694 their initializers are available in integral constant
16695 expressions. */
16696 || (TREE_CODE (decl) == VAR_DECL
16697 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16700 /* Produce the definition of D, a _DECL generated from a template. If
16701 DEFER_OK is nonzero, then we don't have to actually do the
16702 instantiation now; we just have to do it sometime. Normally it is
16703 an error if this is an explicit instantiation but D is undefined.
16704 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16705 explicitly instantiated class template. */
16707 tree
16708 instantiate_decl (tree d, int defer_ok,
16709 bool expl_inst_class_mem_p)
16711 tree tmpl = DECL_TI_TEMPLATE (d);
16712 tree gen_args;
16713 tree args;
16714 tree td;
16715 tree code_pattern;
16716 tree spec;
16717 tree gen_tmpl;
16718 bool pattern_defined;
16719 int need_push;
16720 location_t saved_loc = input_location;
16721 bool external_p;
16723 /* This function should only be used to instantiate templates for
16724 functions and static member variables. */
16725 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16726 || TREE_CODE (d) == VAR_DECL);
16728 /* Variables are never deferred; if instantiation is required, they
16729 are instantiated right away. That allows for better code in the
16730 case that an expression refers to the value of the variable --
16731 if the variable has a constant value the referring expression can
16732 take advantage of that fact. */
16733 if (TREE_CODE (d) == VAR_DECL)
16734 defer_ok = 0;
16736 /* Don't instantiate cloned functions. Instead, instantiate the
16737 functions they cloned. */
16738 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16739 d = DECL_CLONED_FUNCTION (d);
16741 if (DECL_TEMPLATE_INSTANTIATED (d)
16742 || DECL_TEMPLATE_SPECIALIZATION (d))
16743 /* D has already been instantiated or explicitly specialized, so
16744 there's nothing for us to do here.
16746 It might seem reasonable to check whether or not D is an explicit
16747 instantiation, and, if so, stop here. But when an explicit
16748 instantiation is deferred until the end of the compilation,
16749 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16750 the instantiation. */
16751 return d;
16753 /* Check to see whether we know that this template will be
16754 instantiated in some other file, as with "extern template"
16755 extension. */
16756 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16758 /* In general, we do not instantiate such templates. */
16759 if (external_p && !always_instantiate_p (d))
16760 return d;
16762 gen_tmpl = most_general_template (tmpl);
16763 gen_args = DECL_TI_ARGS (d);
16765 if (tmpl != gen_tmpl)
16766 /* We should already have the extra args. */
16767 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16768 == TMPL_ARGS_DEPTH (gen_args));
16769 /* And what's in the hash table should match D. */
16770 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16771 || spec == NULL_TREE);
16773 /* This needs to happen before any tsubsting. */
16774 if (! push_tinst_level (d))
16775 return d;
16777 timevar_push (TV_PARSE);
16779 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16780 for the instantiation. */
16781 td = template_for_substitution (d);
16782 code_pattern = DECL_TEMPLATE_RESULT (td);
16784 /* We should never be trying to instantiate a member of a class
16785 template or partial specialization. */
16786 gcc_assert (d != code_pattern);
16788 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16789 || DECL_TEMPLATE_SPECIALIZATION (td))
16790 /* In the case of a friend template whose definition is provided
16791 outside the class, we may have too many arguments. Drop the
16792 ones we don't need. The same is true for specializations. */
16793 args = get_innermost_template_args
16794 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16795 else
16796 args = gen_args;
16798 if (TREE_CODE (d) == FUNCTION_DECL)
16799 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16800 else
16801 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16803 /* We may be in the middle of deferred access check. Disable it now. */
16804 push_deferring_access_checks (dk_no_deferred);
16806 /* Unless an explicit instantiation directive has already determined
16807 the linkage of D, remember that a definition is available for
16808 this entity. */
16809 if (pattern_defined
16810 && !DECL_INTERFACE_KNOWN (d)
16811 && !DECL_NOT_REALLY_EXTERN (d))
16812 mark_definable (d);
16814 input_location = DECL_SOURCE_LOCATION (d);
16816 /* If D is a member of an explicitly instantiated class template,
16817 and no definition is available, treat it like an implicit
16818 instantiation. */
16819 if (!pattern_defined && expl_inst_class_mem_p
16820 && DECL_EXPLICIT_INSTANTIATION (d))
16822 DECL_NOT_REALLY_EXTERN (d) = 0;
16823 DECL_INTERFACE_KNOWN (d) = 0;
16824 SET_DECL_IMPLICIT_INSTANTIATION (d);
16827 /* Recheck the substitutions to obtain any warning messages
16828 about ignoring cv qualifiers. Don't do this for artificial decls,
16829 as it breaks the context-sensitive substitution for lambda op(). */
16830 if (!defer_ok && !DECL_ARTIFICIAL (d))
16832 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16833 tree type = TREE_TYPE (gen);
16835 /* Make sure that we can see identifiers, and compute access
16836 correctly. D is already the target FUNCTION_DECL with the
16837 right context. */
16838 push_access_scope (d);
16840 if (TREE_CODE (gen) == FUNCTION_DECL)
16842 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16843 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16845 /* Don't simply tsubst the function type, as that will give
16846 duplicate warnings about poor parameter qualifications.
16847 The function arguments are the same as the decl_arguments
16848 without the top level cv qualifiers. */
16849 type = TREE_TYPE (type);
16851 tsubst (type, gen_args, tf_warning_or_error, d);
16853 pop_access_scope (d);
16856 /* Defer all other templates, unless we have been explicitly
16857 forbidden from doing so. */
16858 if (/* If there is no definition, we cannot instantiate the
16859 template. */
16860 ! pattern_defined
16861 /* If it's OK to postpone instantiation, do so. */
16862 || defer_ok
16863 /* If this is a static data member that will be defined
16864 elsewhere, we don't want to instantiate the entire data
16865 member, but we do want to instantiate the initializer so that
16866 we can substitute that elsewhere. */
16867 || (external_p && TREE_CODE (d) == VAR_DECL))
16869 /* The definition of the static data member is now required so
16870 we must substitute the initializer. */
16871 if (TREE_CODE (d) == VAR_DECL
16872 && !DECL_INITIAL (d)
16873 && DECL_INITIAL (code_pattern))
16875 tree ns;
16876 tree init;
16878 ns = decl_namespace_context (d);
16879 push_nested_namespace (ns);
16880 push_nested_class (DECL_CONTEXT (d));
16881 init = tsubst_expr (DECL_INITIAL (code_pattern),
16882 args,
16883 tf_warning_or_error, NULL_TREE,
16884 /*integral_constant_expression_p=*/false);
16885 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16886 /*asmspec_tree=*/NULL_TREE,
16887 LOOKUP_ONLYCONVERTING);
16888 pop_nested_class ();
16889 pop_nested_namespace (ns);
16892 /* We restore the source position here because it's used by
16893 add_pending_template. */
16894 input_location = saved_loc;
16896 if (at_eof && !pattern_defined
16897 && DECL_EXPLICIT_INSTANTIATION (d)
16898 && DECL_NOT_REALLY_EXTERN (d))
16899 /* [temp.explicit]
16901 The definition of a non-exported function template, a
16902 non-exported member function template, or a non-exported
16903 member function or static data member of a class template
16904 shall be present in every translation unit in which it is
16905 explicitly instantiated. */
16906 permerror (input_location, "explicit instantiation of %qD "
16907 "but no definition available", d);
16909 /* ??? Historically, we have instantiated inline functions, even
16910 when marked as "extern template". */
16911 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16912 add_pending_template (d);
16913 goto out;
16915 /* Tell the repository that D is available in this translation unit
16916 -- and see if it is supposed to be instantiated here. */
16917 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16919 /* In a PCH file, despite the fact that the repository hasn't
16920 requested instantiation in the PCH it is still possible that
16921 an instantiation will be required in a file that includes the
16922 PCH. */
16923 if (pch_file)
16924 add_pending_template (d);
16925 /* Instantiate inline functions so that the inliner can do its
16926 job, even though we'll not be emitting a copy of this
16927 function. */
16928 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16929 goto out;
16932 need_push = !cfun || !global_bindings_p ();
16933 if (need_push)
16934 push_to_top_level ();
16936 /* Mark D as instantiated so that recursive calls to
16937 instantiate_decl do not try to instantiate it again. */
16938 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16940 /* Regenerate the declaration in case the template has been modified
16941 by a subsequent redeclaration. */
16942 regenerate_decl_from_template (d, td);
16944 /* We already set the file and line above. Reset them now in case
16945 they changed as a result of calling regenerate_decl_from_template. */
16946 input_location = DECL_SOURCE_LOCATION (d);
16948 if (TREE_CODE (d) == VAR_DECL)
16950 tree init;
16952 /* Clear out DECL_RTL; whatever was there before may not be right
16953 since we've reset the type of the declaration. */
16954 SET_DECL_RTL (d, NULL);
16955 DECL_IN_AGGR_P (d) = 0;
16957 /* The initializer is placed in DECL_INITIAL by
16958 regenerate_decl_from_template. Pull it out so that
16959 cp_finish_decl can process it. */
16960 init = DECL_INITIAL (d);
16961 DECL_INITIAL (d) = NULL_TREE;
16962 DECL_INITIALIZED_P (d) = 0;
16964 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16965 initializer. That function will defer actual emission until
16966 we have a chance to determine linkage. */
16967 DECL_EXTERNAL (d) = 0;
16969 /* Enter the scope of D so that access-checking works correctly. */
16970 push_nested_class (DECL_CONTEXT (d));
16971 cp_finish_decl (d, init, false, NULL_TREE, 0);
16972 pop_nested_class ();
16974 else if (TREE_CODE (d) == FUNCTION_DECL)
16976 htab_t saved_local_specializations;
16977 tree subst_decl;
16978 tree tmpl_parm;
16979 tree spec_parm;
16981 /* Save away the current list, in case we are instantiating one
16982 template from within the body of another. */
16983 saved_local_specializations = local_specializations;
16985 /* Set up the list of local specializations. */
16986 local_specializations = htab_create (37,
16987 hash_local_specialization,
16988 eq_local_specializations,
16989 NULL);
16991 /* Set up context. */
16992 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16994 /* Create substitution entries for the parameters. */
16995 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16996 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16997 spec_parm = DECL_ARGUMENTS (d);
16998 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17000 register_local_specialization (spec_parm, tmpl_parm);
17001 spec_parm = skip_artificial_parms_for (d, spec_parm);
17002 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17004 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17006 register_local_specialization (spec_parm, tmpl_parm);
17007 tmpl_parm = DECL_CHAIN (tmpl_parm);
17008 spec_parm = DECL_CHAIN (spec_parm);
17010 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17012 /* Register the (value) argument pack as a specialization of
17013 TMPL_PARM, then move on. */
17014 tree argpack = make_fnparm_pack (spec_parm);
17015 register_local_specialization (argpack, tmpl_parm);
17016 tmpl_parm = DECL_CHAIN (tmpl_parm);
17017 spec_parm = NULL_TREE;
17019 gcc_assert (!spec_parm);
17021 /* Substitute into the body of the function. */
17022 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17023 tf_warning_or_error, tmpl,
17024 /*integral_constant_expression_p=*/false);
17026 /* Set the current input_location to the end of the function
17027 so that finish_function knows where we are. */
17028 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17030 /* We don't need the local specializations any more. */
17031 htab_delete (local_specializations);
17032 local_specializations = saved_local_specializations;
17034 /* Finish the function. */
17035 d = finish_function (0);
17036 expand_or_defer_fn (d);
17039 /* We're not deferring instantiation any more. */
17040 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17042 if (need_push)
17043 pop_from_top_level ();
17045 out:
17046 input_location = saved_loc;
17047 pop_deferring_access_checks ();
17048 pop_tinst_level ();
17050 timevar_pop (TV_PARSE);
17052 return d;
17055 /* Run through the list of templates that we wish we could
17056 instantiate, and instantiate any we can. RETRIES is the
17057 number of times we retry pending template instantiation. */
17059 void
17060 instantiate_pending_templates (int retries)
17062 int reconsider;
17063 location_t saved_loc = input_location;
17065 /* Instantiating templates may trigger vtable generation. This in turn
17066 may require further template instantiations. We place a limit here
17067 to avoid infinite loop. */
17068 if (pending_templates && retries >= max_tinst_depth)
17070 tree decl = pending_templates->tinst->decl;
17072 error ("template instantiation depth exceeds maximum of %d"
17073 " instantiating %q+D, possibly from virtual table generation"
17074 " (use -ftemplate-depth= to increase the maximum)",
17075 max_tinst_depth, decl);
17076 if (TREE_CODE (decl) == FUNCTION_DECL)
17077 /* Pretend that we defined it. */
17078 DECL_INITIAL (decl) = error_mark_node;
17079 return;
17084 struct pending_template **t = &pending_templates;
17085 struct pending_template *last = NULL;
17086 reconsider = 0;
17087 while (*t)
17089 tree instantiation = reopen_tinst_level ((*t)->tinst);
17090 bool complete = false;
17092 if (TYPE_P (instantiation))
17094 tree fn;
17096 if (!COMPLETE_TYPE_P (instantiation))
17098 instantiate_class_template (instantiation);
17099 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17100 for (fn = TYPE_METHODS (instantiation);
17102 fn = TREE_CHAIN (fn))
17103 if (! DECL_ARTIFICIAL (fn))
17104 instantiate_decl (fn,
17105 /*defer_ok=*/0,
17106 /*expl_inst_class_mem_p=*/false);
17107 if (COMPLETE_TYPE_P (instantiation))
17108 reconsider = 1;
17111 complete = COMPLETE_TYPE_P (instantiation);
17113 else
17115 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17116 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17118 instantiation
17119 = instantiate_decl (instantiation,
17120 /*defer_ok=*/0,
17121 /*expl_inst_class_mem_p=*/false);
17122 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17123 reconsider = 1;
17126 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17127 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17130 if (complete)
17131 /* If INSTANTIATION has been instantiated, then we don't
17132 need to consider it again in the future. */
17133 *t = (*t)->next;
17134 else
17136 last = *t;
17137 t = &(*t)->next;
17139 tinst_depth = 0;
17140 current_tinst_level = NULL;
17142 last_pending_template = last;
17144 while (reconsider);
17146 input_location = saved_loc;
17149 /* Substitute ARGVEC into T, which is a list of initializers for
17150 either base class or a non-static data member. The TREE_PURPOSEs
17151 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17152 instantiate_decl. */
17154 static tree
17155 tsubst_initializer_list (tree t, tree argvec)
17157 tree inits = NULL_TREE;
17159 for (; t; t = TREE_CHAIN (t))
17161 tree decl;
17162 tree init;
17163 tree expanded_bases = NULL_TREE;
17164 tree expanded_arguments = NULL_TREE;
17165 int i, len = 1;
17167 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17169 tree expr;
17170 tree arg;
17172 /* Expand the base class expansion type into separate base
17173 classes. */
17174 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17175 tf_warning_or_error,
17176 NULL_TREE);
17177 if (expanded_bases == error_mark_node)
17178 continue;
17180 /* We'll be building separate TREE_LISTs of arguments for
17181 each base. */
17182 len = TREE_VEC_LENGTH (expanded_bases);
17183 expanded_arguments = make_tree_vec (len);
17184 for (i = 0; i < len; i++)
17185 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17187 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17188 expand each argument in the TREE_VALUE of t. */
17189 expr = make_node (EXPR_PACK_EXPANSION);
17190 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17191 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17193 if (TREE_VALUE (t) == void_type_node)
17194 /* VOID_TYPE_NODE is used to indicate
17195 value-initialization. */
17197 for (i = 0; i < len; i++)
17198 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17200 else
17202 /* Substitute parameter packs into each argument in the
17203 TREE_LIST. */
17204 in_base_initializer = 1;
17205 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17207 tree expanded_exprs;
17209 /* Expand the argument. */
17210 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17211 expanded_exprs
17212 = tsubst_pack_expansion (expr, argvec,
17213 tf_warning_or_error,
17214 NULL_TREE);
17215 if (expanded_exprs == error_mark_node)
17216 continue;
17218 /* Prepend each of the expanded expressions to the
17219 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17220 for (i = 0; i < len; i++)
17222 TREE_VEC_ELT (expanded_arguments, i) =
17223 tree_cons (NULL_TREE,
17224 TREE_VEC_ELT (expanded_exprs, i),
17225 TREE_VEC_ELT (expanded_arguments, i));
17228 in_base_initializer = 0;
17230 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17231 since we built them backwards. */
17232 for (i = 0; i < len; i++)
17234 TREE_VEC_ELT (expanded_arguments, i) =
17235 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17240 for (i = 0; i < len; ++i)
17242 if (expanded_bases)
17244 decl = TREE_VEC_ELT (expanded_bases, i);
17245 decl = expand_member_init (decl);
17246 init = TREE_VEC_ELT (expanded_arguments, i);
17248 else
17250 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17251 tf_warning_or_error, NULL_TREE);
17253 decl = expand_member_init (decl);
17254 if (decl && !DECL_P (decl))
17255 in_base_initializer = 1;
17257 init = tsubst_expr (TREE_VALUE (t), argvec,
17258 tf_warning_or_error, NULL_TREE,
17259 /*integral_constant_expression_p=*/false);
17260 in_base_initializer = 0;
17263 if (decl)
17265 init = build_tree_list (decl, init);
17266 TREE_CHAIN (init) = inits;
17267 inits = init;
17271 return inits;
17274 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17276 static void
17277 set_current_access_from_decl (tree decl)
17279 if (TREE_PRIVATE (decl))
17280 current_access_specifier = access_private_node;
17281 else if (TREE_PROTECTED (decl))
17282 current_access_specifier = access_protected_node;
17283 else
17284 current_access_specifier = access_public_node;
17287 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17288 is the instantiation (which should have been created with
17289 start_enum) and ARGS are the template arguments to use. */
17291 static void
17292 tsubst_enum (tree tag, tree newtag, tree args)
17294 tree e;
17296 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17298 tree value;
17299 tree decl;
17301 decl = TREE_VALUE (e);
17302 /* Note that in a template enum, the TREE_VALUE is the
17303 CONST_DECL, not the corresponding INTEGER_CST. */
17304 value = tsubst_expr (DECL_INITIAL (decl),
17305 args, tf_warning_or_error, NULL_TREE,
17306 /*integral_constant_expression_p=*/true);
17308 /* Give this enumeration constant the correct access. */
17309 set_current_access_from_decl (decl);
17311 /* Actually build the enumerator itself. */
17312 build_enumerator
17313 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
17316 finish_enum (newtag);
17317 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17318 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17321 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17322 its type -- but without substituting the innermost set of template
17323 arguments. So, innermost set of template parameters will appear in
17324 the type. */
17326 tree
17327 get_mostly_instantiated_function_type (tree decl)
17329 tree fn_type;
17330 tree tmpl;
17331 tree targs;
17332 tree tparms;
17333 int parm_depth;
17335 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17336 targs = DECL_TI_ARGS (decl);
17337 tparms = DECL_TEMPLATE_PARMS (tmpl);
17338 parm_depth = TMPL_PARMS_DEPTH (tparms);
17340 /* There should be as many levels of arguments as there are levels
17341 of parameters. */
17342 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17344 fn_type = TREE_TYPE (tmpl);
17346 if (parm_depth == 1)
17347 /* No substitution is necessary. */
17349 else
17351 int i, save_access_control;
17352 tree partial_args;
17354 /* Replace the innermost level of the TARGS with NULL_TREEs to
17355 let tsubst know not to substitute for those parameters. */
17356 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17357 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17358 SET_TMPL_ARGS_LEVEL (partial_args, i,
17359 TMPL_ARGS_LEVEL (targs, i));
17360 SET_TMPL_ARGS_LEVEL (partial_args,
17361 TMPL_ARGS_DEPTH (targs),
17362 make_tree_vec (DECL_NTPARMS (tmpl)));
17364 /* Disable access control as this function is used only during
17365 name-mangling. */
17366 save_access_control = flag_access_control;
17367 flag_access_control = 0;
17369 ++processing_template_decl;
17370 /* Now, do the (partial) substitution to figure out the
17371 appropriate function type. */
17372 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17373 --processing_template_decl;
17375 /* Substitute into the template parameters to obtain the real
17376 innermost set of parameters. This step is important if the
17377 innermost set of template parameters contains value
17378 parameters whose types depend on outer template parameters. */
17379 TREE_VEC_LENGTH (partial_args)--;
17380 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17382 flag_access_control = save_access_control;
17385 return fn_type;
17388 /* Return truthvalue if we're processing a template different from
17389 the last one involved in diagnostics. */
17391 problematic_instantiation_changed (void)
17393 return last_template_error_tick != tinst_level_tick;
17396 /* Remember current template involved in diagnostics. */
17397 void
17398 record_last_problematic_instantiation (void)
17400 last_template_error_tick = tinst_level_tick;
17403 struct tinst_level *
17404 current_instantiation (void)
17406 return current_tinst_level;
17409 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17410 type. Return zero for ok, nonzero for disallowed. Issue error and
17411 warning messages under control of COMPLAIN. */
17413 static int
17414 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17416 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17417 return 0;
17418 else if (POINTER_TYPE_P (type))
17419 return 0;
17420 else if (TYPE_PTR_TO_MEMBER_P (type))
17421 return 0;
17422 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17423 return 0;
17424 else if (TREE_CODE (type) == TYPENAME_TYPE)
17425 return 0;
17427 if (complain & tf_error)
17428 error ("%q#T is not a valid type for a template constant parameter", type);
17429 return 1;
17432 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17433 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17435 static bool
17436 dependent_type_p_r (tree type)
17438 tree scope;
17440 /* [temp.dep.type]
17442 A type is dependent if it is:
17444 -- a template parameter. Template template parameters are types
17445 for us (since TYPE_P holds true for them) so we handle
17446 them here. */
17447 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17448 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17449 return true;
17450 /* -- a qualified-id with a nested-name-specifier which contains a
17451 class-name that names a dependent type or whose unqualified-id
17452 names a dependent type. */
17453 if (TREE_CODE (type) == TYPENAME_TYPE)
17454 return true;
17455 /* -- a cv-qualified type where the cv-unqualified type is
17456 dependent. */
17457 type = TYPE_MAIN_VARIANT (type);
17458 /* -- a compound type constructed from any dependent type. */
17459 if (TYPE_PTR_TO_MEMBER_P (type))
17460 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17461 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17462 (type)));
17463 else if (TREE_CODE (type) == POINTER_TYPE
17464 || TREE_CODE (type) == REFERENCE_TYPE)
17465 return dependent_type_p (TREE_TYPE (type));
17466 else if (TREE_CODE (type) == FUNCTION_TYPE
17467 || TREE_CODE (type) == METHOD_TYPE)
17469 tree arg_type;
17471 if (dependent_type_p (TREE_TYPE (type)))
17472 return true;
17473 for (arg_type = TYPE_ARG_TYPES (type);
17474 arg_type;
17475 arg_type = TREE_CHAIN (arg_type))
17476 if (dependent_type_p (TREE_VALUE (arg_type)))
17477 return true;
17478 return false;
17480 /* -- an array type constructed from any dependent type or whose
17481 size is specified by a constant expression that is
17482 value-dependent. */
17483 if (TREE_CODE (type) == ARRAY_TYPE)
17485 if (TYPE_DOMAIN (type)
17486 && dependent_type_p (TYPE_DOMAIN (type)))
17487 return true;
17488 return dependent_type_p (TREE_TYPE (type));
17490 else if (TREE_CODE (type) == INTEGER_TYPE
17491 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17493 /* If this is the TYPE_DOMAIN of an array type, consider it
17494 dependent. We already checked for value-dependence in
17495 compute_array_index_type. */
17496 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17499 /* -- a template-id in which either the template name is a template
17500 parameter ... */
17501 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17502 return true;
17503 /* ... or any of the template arguments is a dependent type or
17504 an expression that is type-dependent or value-dependent. */
17505 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17506 && (any_dependent_template_arguments_p
17507 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17508 return true;
17510 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17511 argument of the `typeof' expression is not type-dependent, then
17512 it should already been have resolved. */
17513 if (TREE_CODE (type) == TYPEOF_TYPE
17514 || TREE_CODE (type) == DECLTYPE_TYPE)
17515 return true;
17517 /* A template argument pack is dependent if any of its packed
17518 arguments are. */
17519 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17521 tree args = ARGUMENT_PACK_ARGS (type);
17522 int i, len = TREE_VEC_LENGTH (args);
17523 for (i = 0; i < len; ++i)
17524 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17525 return true;
17528 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17529 be template parameters. */
17530 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17531 return true;
17533 /* The standard does not specifically mention types that are local
17534 to template functions or local classes, but they should be
17535 considered dependent too. For example:
17537 template <int I> void f() {
17538 enum E { a = I };
17539 S<sizeof (E)> s;
17542 The size of `E' cannot be known until the value of `I' has been
17543 determined. Therefore, `E' must be considered dependent. */
17544 scope = TYPE_CONTEXT (type);
17545 if (scope && TYPE_P (scope))
17546 return dependent_type_p (scope);
17547 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17548 return type_dependent_expression_p (scope);
17550 /* Other types are non-dependent. */
17551 return false;
17554 /* Returns TRUE if TYPE is dependent, in the sense of
17555 [temp.dep.type]. */
17557 bool
17558 dependent_type_p (tree type)
17560 /* If there are no template parameters in scope, then there can't be
17561 any dependent types. */
17562 if (!processing_template_decl)
17564 /* If we are not processing a template, then nobody should be
17565 providing us with a dependent type. */
17566 gcc_assert (type);
17567 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17568 return false;
17571 /* If the type is NULL, we have not computed a type for the entity
17572 in question; in that case, the type is dependent. */
17573 if (!type)
17574 return true;
17576 /* Erroneous types can be considered non-dependent. */
17577 if (type == error_mark_node)
17578 return false;
17580 /* If we have not already computed the appropriate value for TYPE,
17581 do so now. */
17582 if (!TYPE_DEPENDENT_P_VALID (type))
17584 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17585 TYPE_DEPENDENT_P_VALID (type) = 1;
17588 return TYPE_DEPENDENT_P (type);
17591 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17592 lookup. In other words, a dependent type that is not the current
17593 instantiation. */
17595 bool
17596 dependent_scope_p (tree scope)
17598 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17599 && !currently_open_class (scope));
17602 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17603 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17604 expression. */
17606 bool
17607 value_dependent_expression_p (tree expression)
17609 if (!processing_template_decl)
17610 return false;
17612 /* A name declared with a dependent type. */
17613 if (DECL_P (expression) && type_dependent_expression_p (expression))
17614 return true;
17616 switch (TREE_CODE (expression))
17618 case IDENTIFIER_NODE:
17619 /* A name that has not been looked up -- must be dependent. */
17620 return true;
17622 case TEMPLATE_PARM_INDEX:
17623 /* A non-type template parm. */
17624 return true;
17626 case CONST_DECL:
17627 /* A non-type template parm. */
17628 if (DECL_TEMPLATE_PARM_P (expression))
17629 return true;
17630 return value_dependent_expression_p (DECL_INITIAL (expression));
17632 case VAR_DECL:
17633 /* A constant with integral or enumeration type and is initialized
17634 with an expression that is value-dependent. */
17635 if (DECL_INITIAL (expression)
17636 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17637 && value_dependent_expression_p (DECL_INITIAL (expression)))
17638 return true;
17639 return false;
17641 case DYNAMIC_CAST_EXPR:
17642 case STATIC_CAST_EXPR:
17643 case CONST_CAST_EXPR:
17644 case REINTERPRET_CAST_EXPR:
17645 case CAST_EXPR:
17646 /* These expressions are value-dependent if the type to which
17647 the cast occurs is dependent or the expression being casted
17648 is value-dependent. */
17650 tree type = TREE_TYPE (expression);
17652 if (dependent_type_p (type))
17653 return true;
17655 /* A functional cast has a list of operands. */
17656 expression = TREE_OPERAND (expression, 0);
17657 if (!expression)
17659 /* If there are no operands, it must be an expression such
17660 as "int()". This should not happen for aggregate types
17661 because it would form non-constant expressions. */
17662 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17664 return false;
17667 if (TREE_CODE (expression) == TREE_LIST)
17668 return any_value_dependent_elements_p (expression);
17670 return value_dependent_expression_p (expression);
17673 case SIZEOF_EXPR:
17674 case ALIGNOF_EXPR:
17675 /* A `sizeof' expression is value-dependent if the operand is
17676 type-dependent or is a pack expansion. */
17677 expression = TREE_OPERAND (expression, 0);
17678 if (PACK_EXPANSION_P (expression))
17679 return true;
17680 else if (TYPE_P (expression))
17681 return dependent_type_p (expression);
17682 return type_dependent_expression_p (expression);
17684 case NOEXCEPT_EXPR:
17685 expression = TREE_OPERAND (expression, 0);
17686 /* FIXME why check value-dependency? */
17687 return (type_dependent_expression_p (expression)
17688 || value_dependent_expression_p (expression));
17690 case SCOPE_REF:
17692 tree name = TREE_OPERAND (expression, 1);
17693 return value_dependent_expression_p (name);
17696 case COMPONENT_REF:
17697 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17698 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17700 case CALL_EXPR:
17701 /* A CALL_EXPR may appear in a constant expression if it is a
17702 call to a builtin function, e.g., __builtin_constant_p. All
17703 such calls are value-dependent. */
17704 return true;
17706 case NONTYPE_ARGUMENT_PACK:
17707 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17708 is value-dependent. */
17710 tree values = ARGUMENT_PACK_ARGS (expression);
17711 int i, len = TREE_VEC_LENGTH (values);
17713 for (i = 0; i < len; ++i)
17714 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17715 return true;
17717 return false;
17720 case TRAIT_EXPR:
17722 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17723 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17724 || (type2 ? dependent_type_p (type2) : false));
17727 case MODOP_EXPR:
17728 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17729 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17731 case ADDR_EXPR:
17733 tree op = TREE_OPERAND (expression, 0);
17734 return (value_dependent_expression_p (op)
17735 || has_value_dependent_address (op));
17738 default:
17739 /* A constant expression is value-dependent if any subexpression is
17740 value-dependent. */
17741 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17743 case tcc_reference:
17744 case tcc_unary:
17745 return (value_dependent_expression_p
17746 (TREE_OPERAND (expression, 0)));
17748 case tcc_comparison:
17749 case tcc_binary:
17750 return ((value_dependent_expression_p
17751 (TREE_OPERAND (expression, 0)))
17752 || (value_dependent_expression_p
17753 (TREE_OPERAND (expression, 1))));
17755 case tcc_expression:
17756 case tcc_vl_exp:
17758 int i;
17759 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17760 /* In some cases, some of the operands may be missing.
17761 (For example, in the case of PREDECREMENT_EXPR, the
17762 amount to increment by may be missing.) That doesn't
17763 make the expression dependent. */
17764 if (TREE_OPERAND (expression, i)
17765 && (value_dependent_expression_p
17766 (TREE_OPERAND (expression, i))))
17767 return true;
17768 return false;
17771 default:
17772 break;
17776 /* The expression is not value-dependent. */
17777 return false;
17780 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17781 [temp.dep.expr]. */
17783 bool
17784 type_dependent_expression_p (tree expression)
17786 if (!processing_template_decl)
17787 return false;
17789 if (expression == error_mark_node)
17790 return false;
17792 /* An unresolved name is always dependent. */
17793 if (TREE_CODE (expression) == IDENTIFIER_NODE
17794 || TREE_CODE (expression) == USING_DECL)
17795 return true;
17797 /* Some expression forms are never type-dependent. */
17798 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17799 || TREE_CODE (expression) == SIZEOF_EXPR
17800 || TREE_CODE (expression) == ALIGNOF_EXPR
17801 || TREE_CODE (expression) == NOEXCEPT_EXPR
17802 || TREE_CODE (expression) == TRAIT_EXPR
17803 || TREE_CODE (expression) == TYPEID_EXPR
17804 || TREE_CODE (expression) == DELETE_EXPR
17805 || TREE_CODE (expression) == VEC_DELETE_EXPR
17806 || TREE_CODE (expression) == THROW_EXPR)
17807 return false;
17809 /* The types of these expressions depends only on the type to which
17810 the cast occurs. */
17811 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17812 || TREE_CODE (expression) == STATIC_CAST_EXPR
17813 || TREE_CODE (expression) == CONST_CAST_EXPR
17814 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17815 || TREE_CODE (expression) == CAST_EXPR)
17816 return dependent_type_p (TREE_TYPE (expression));
17818 /* The types of these expressions depends only on the type created
17819 by the expression. */
17820 if (TREE_CODE (expression) == NEW_EXPR
17821 || TREE_CODE (expression) == VEC_NEW_EXPR)
17823 /* For NEW_EXPR tree nodes created inside a template, either
17824 the object type itself or a TREE_LIST may appear as the
17825 operand 1. */
17826 tree type = TREE_OPERAND (expression, 1);
17827 if (TREE_CODE (type) == TREE_LIST)
17828 /* This is an array type. We need to check array dimensions
17829 as well. */
17830 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17831 || value_dependent_expression_p
17832 (TREE_OPERAND (TREE_VALUE (type), 1));
17833 else
17834 return dependent_type_p (type);
17837 if (TREE_CODE (expression) == SCOPE_REF)
17839 tree scope = TREE_OPERAND (expression, 0);
17840 tree name = TREE_OPERAND (expression, 1);
17842 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
17843 contains an identifier associated by name lookup with one or more
17844 declarations declared with a dependent type, or...a
17845 nested-name-specifier or qualified-id that names a member of an
17846 unknown specialization. */
17847 return (type_dependent_expression_p (name)
17848 || dependent_scope_p (scope));
17851 if (TREE_CODE (expression) == FUNCTION_DECL
17852 && DECL_LANG_SPECIFIC (expression)
17853 && DECL_TEMPLATE_INFO (expression)
17854 && (any_dependent_template_arguments_p
17855 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17856 return true;
17858 if (TREE_CODE (expression) == TEMPLATE_DECL
17859 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17860 return false;
17862 if (TREE_CODE (expression) == STMT_EXPR)
17863 expression = stmt_expr_value_expr (expression);
17865 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17867 tree elt;
17868 unsigned i;
17870 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17872 if (type_dependent_expression_p (elt))
17873 return true;
17875 return false;
17878 /* A static data member of the current instantiation with incomplete
17879 array type is type-dependent, as the definition and specializations
17880 can have different bounds. */
17881 if (TREE_CODE (expression) == VAR_DECL
17882 && DECL_CLASS_SCOPE_P (expression)
17883 && dependent_type_p (DECL_CONTEXT (expression))
17884 && VAR_HAD_UNKNOWN_BOUND (expression))
17885 return true;
17887 if (TREE_TYPE (expression) == unknown_type_node)
17889 if (TREE_CODE (expression) == ADDR_EXPR)
17890 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17891 if (TREE_CODE (expression) == COMPONENT_REF
17892 || TREE_CODE (expression) == OFFSET_REF)
17894 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17895 return true;
17896 expression = TREE_OPERAND (expression, 1);
17897 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17898 return false;
17900 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17901 if (TREE_CODE (expression) == SCOPE_REF)
17902 return false;
17904 if (TREE_CODE (expression) == BASELINK)
17905 expression = BASELINK_FUNCTIONS (expression);
17907 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17909 if (any_dependent_template_arguments_p
17910 (TREE_OPERAND (expression, 1)))
17911 return true;
17912 expression = TREE_OPERAND (expression, 0);
17914 gcc_assert (TREE_CODE (expression) == OVERLOAD
17915 || TREE_CODE (expression) == FUNCTION_DECL);
17917 while (expression)
17919 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17920 return true;
17921 expression = OVL_NEXT (expression);
17923 return false;
17926 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17928 return (dependent_type_p (TREE_TYPE (expression)));
17931 /* Like type_dependent_expression_p, but it also works while not processing
17932 a template definition, i.e. during substitution or mangling. */
17934 bool
17935 type_dependent_expression_p_push (tree expr)
17937 bool b;
17938 ++processing_template_decl;
17939 b = type_dependent_expression_p (expr);
17940 --processing_template_decl;
17941 return b;
17944 /* Returns TRUE if ARGS contains a type-dependent expression. */
17946 bool
17947 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17949 unsigned int i;
17950 tree arg;
17952 FOR_EACH_VEC_ELT (tree, args, i, arg)
17954 if (type_dependent_expression_p (arg))
17955 return true;
17957 return false;
17960 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17961 expressions) contains any value-dependent expressions. */
17963 bool
17964 any_value_dependent_elements_p (const_tree list)
17966 for (; list; list = TREE_CHAIN (list))
17967 if (value_dependent_expression_p (TREE_VALUE (list)))
17968 return true;
17970 return false;
17973 /* Returns TRUE if the ARG (a template argument) is dependent. */
17975 bool
17976 dependent_template_arg_p (tree arg)
17978 if (!processing_template_decl)
17979 return false;
17981 if (TREE_CODE (arg) == TEMPLATE_DECL
17982 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17983 return dependent_template_p (arg);
17984 else if (ARGUMENT_PACK_P (arg))
17986 tree args = ARGUMENT_PACK_ARGS (arg);
17987 int i, len = TREE_VEC_LENGTH (args);
17988 for (i = 0; i < len; ++i)
17990 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17991 return true;
17994 return false;
17996 else if (TYPE_P (arg))
17997 return dependent_type_p (arg);
17998 else
17999 return (type_dependent_expression_p (arg)
18000 || value_dependent_expression_p (arg));
18003 /* Returns true if ARGS (a collection of template arguments) contains
18004 any types that require structural equality testing. */
18006 bool
18007 any_template_arguments_need_structural_equality_p (tree args)
18009 int i;
18010 int j;
18012 if (!args)
18013 return false;
18014 if (args == error_mark_node)
18015 return true;
18017 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18019 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18020 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18022 tree arg = TREE_VEC_ELT (level, j);
18023 tree packed_args = NULL_TREE;
18024 int k, len = 1;
18026 if (ARGUMENT_PACK_P (arg))
18028 /* Look inside the argument pack. */
18029 packed_args = ARGUMENT_PACK_ARGS (arg);
18030 len = TREE_VEC_LENGTH (packed_args);
18033 for (k = 0; k < len; ++k)
18035 if (packed_args)
18036 arg = TREE_VEC_ELT (packed_args, k);
18038 if (error_operand_p (arg))
18039 return true;
18040 else if (TREE_CODE (arg) == TEMPLATE_DECL
18041 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18042 continue;
18043 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18044 return true;
18045 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18046 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18047 return true;
18052 return false;
18055 /* Returns true if ARGS (a collection of template arguments) contains
18056 any dependent arguments. */
18058 bool
18059 any_dependent_template_arguments_p (const_tree args)
18061 int i;
18062 int j;
18064 if (!args)
18065 return false;
18066 if (args == error_mark_node)
18067 return true;
18069 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18071 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18072 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18073 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18074 return true;
18077 return false;
18080 /* Returns TRUE if the template TMPL is dependent. */
18082 bool
18083 dependent_template_p (tree tmpl)
18085 if (TREE_CODE (tmpl) == OVERLOAD)
18087 while (tmpl)
18089 if (dependent_template_p (OVL_FUNCTION (tmpl)))
18090 return true;
18091 tmpl = OVL_CHAIN (tmpl);
18093 return false;
18096 /* Template template parameters are dependent. */
18097 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18098 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18099 return true;
18100 /* So are names that have not been looked up. */
18101 if (TREE_CODE (tmpl) == SCOPE_REF
18102 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18103 return true;
18104 /* So are member templates of dependent classes. */
18105 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18106 return dependent_type_p (DECL_CONTEXT (tmpl));
18107 return false;
18110 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18112 bool
18113 dependent_template_id_p (tree tmpl, tree args)
18115 return (dependent_template_p (tmpl)
18116 || any_dependent_template_arguments_p (args));
18119 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18120 is dependent. */
18122 bool
18123 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18125 int i;
18127 if (!processing_template_decl)
18128 return false;
18130 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18132 tree decl = TREE_VEC_ELT (declv, i);
18133 tree init = TREE_VEC_ELT (initv, i);
18134 tree cond = TREE_VEC_ELT (condv, i);
18135 tree incr = TREE_VEC_ELT (incrv, i);
18137 if (type_dependent_expression_p (decl))
18138 return true;
18140 if (init && type_dependent_expression_p (init))
18141 return true;
18143 if (type_dependent_expression_p (cond))
18144 return true;
18146 if (COMPARISON_CLASS_P (cond)
18147 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18148 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18149 return true;
18151 if (TREE_CODE (incr) == MODOP_EXPR)
18153 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18154 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18155 return true;
18157 else if (type_dependent_expression_p (incr))
18158 return true;
18159 else if (TREE_CODE (incr) == MODIFY_EXPR)
18161 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18162 return true;
18163 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18165 tree t = TREE_OPERAND (incr, 1);
18166 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18167 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18168 return true;
18173 return false;
18176 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18177 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18178 no such TYPE can be found. Note that this function peers inside
18179 uninstantiated templates and therefore should be used only in
18180 extremely limited situations. ONLY_CURRENT_P restricts this
18181 peering to the currently open classes hierarchy (which is required
18182 when comparing types). */
18184 tree
18185 resolve_typename_type (tree type, bool only_current_p)
18187 tree scope;
18188 tree name;
18189 tree decl;
18190 int quals;
18191 tree pushed_scope;
18192 tree result;
18194 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18196 scope = TYPE_CONTEXT (type);
18197 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18198 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18199 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18200 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18201 identifier of the TYPENAME_TYPE anymore.
18202 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18203 TYPENAME_TYPE instead, we avoid messing up with a possible
18204 typedef variant case. */
18205 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18207 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18208 it first before we can figure out what NAME refers to. */
18209 if (TREE_CODE (scope) == TYPENAME_TYPE)
18210 scope = resolve_typename_type (scope, only_current_p);
18211 /* If we don't know what SCOPE refers to, then we cannot resolve the
18212 TYPENAME_TYPE. */
18213 if (TREE_CODE (scope) == TYPENAME_TYPE)
18214 return type;
18215 /* If the SCOPE is a template type parameter, we have no way of
18216 resolving the name. */
18217 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18218 return type;
18219 /* If the SCOPE is not the current instantiation, there's no reason
18220 to look inside it. */
18221 if (only_current_p && !currently_open_class (scope))
18222 return type;
18223 /* If this is a typedef, we don't want to look inside (c++/11987). */
18224 if (typedef_variant_p (type))
18225 return type;
18226 /* If SCOPE isn't the template itself, it will not have a valid
18227 TYPE_FIELDS list. */
18228 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18229 /* scope is either the template itself or a compatible instantiation
18230 like X<T>, so look up the name in the original template. */
18231 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18232 else
18233 /* scope is a partial instantiation, so we can't do the lookup or we
18234 will lose the template arguments. */
18235 return type;
18236 /* Enter the SCOPE so that name lookup will be resolved as if we
18237 were in the class definition. In particular, SCOPE will no
18238 longer be considered a dependent type. */
18239 pushed_scope = push_scope (scope);
18240 /* Look up the declaration. */
18241 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18243 result = NULL_TREE;
18245 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18246 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18247 if (!decl)
18248 /*nop*/;
18249 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18250 && TREE_CODE (decl) == TYPE_DECL)
18252 result = TREE_TYPE (decl);
18253 if (result == error_mark_node)
18254 result = NULL_TREE;
18256 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18257 && DECL_CLASS_TEMPLATE_P (decl))
18259 tree tmpl;
18260 tree args;
18261 /* Obtain the template and the arguments. */
18262 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18263 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18264 /* Instantiate the template. */
18265 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18266 /*entering_scope=*/0,
18267 tf_error | tf_user);
18268 if (result == error_mark_node)
18269 result = NULL_TREE;
18272 /* Leave the SCOPE. */
18273 if (pushed_scope)
18274 pop_scope (pushed_scope);
18276 /* If we failed to resolve it, return the original typename. */
18277 if (!result)
18278 return type;
18280 /* If lookup found a typename type, resolve that too. */
18281 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18283 /* Ill-formed programs can cause infinite recursion here, so we
18284 must catch that. */
18285 TYPENAME_IS_RESOLVING_P (type) = 1;
18286 result = resolve_typename_type (result, only_current_p);
18287 TYPENAME_IS_RESOLVING_P (type) = 0;
18290 /* Qualify the resulting type. */
18291 quals = cp_type_quals (type);
18292 if (quals)
18293 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18295 return result;
18298 /* EXPR is an expression which is not type-dependent. Return a proxy
18299 for EXPR that can be used to compute the types of larger
18300 expressions containing EXPR. */
18302 tree
18303 build_non_dependent_expr (tree expr)
18305 tree inner_expr;
18307 /* Preserve null pointer constants so that the type of things like
18308 "p == 0" where "p" is a pointer can be determined. */
18309 if (null_ptr_cst_p (expr))
18310 return expr;
18311 /* Preserve OVERLOADs; the functions must be available to resolve
18312 types. */
18313 inner_expr = expr;
18314 if (TREE_CODE (inner_expr) == STMT_EXPR)
18315 inner_expr = stmt_expr_value_expr (inner_expr);
18316 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18317 inner_expr = TREE_OPERAND (inner_expr, 0);
18318 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18319 inner_expr = TREE_OPERAND (inner_expr, 1);
18320 if (is_overloaded_fn (inner_expr)
18321 || TREE_CODE (inner_expr) == OFFSET_REF)
18322 return expr;
18323 /* There is no need to return a proxy for a variable. */
18324 if (TREE_CODE (expr) == VAR_DECL)
18325 return expr;
18326 /* Preserve string constants; conversions from string constants to
18327 "char *" are allowed, even though normally a "const char *"
18328 cannot be used to initialize a "char *". */
18329 if (TREE_CODE (expr) == STRING_CST)
18330 return expr;
18331 /* Preserve arithmetic constants, as an optimization -- there is no
18332 reason to create a new node. */
18333 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18334 return expr;
18335 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18336 There is at least one place where we want to know that a
18337 particular expression is a throw-expression: when checking a ?:
18338 expression, there are special rules if the second or third
18339 argument is a throw-expression. */
18340 if (TREE_CODE (expr) == THROW_EXPR)
18341 return expr;
18343 if (TREE_CODE (expr) == COND_EXPR)
18344 return build3 (COND_EXPR,
18345 TREE_TYPE (expr),
18346 TREE_OPERAND (expr, 0),
18347 (TREE_OPERAND (expr, 1)
18348 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18349 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18350 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18351 if (TREE_CODE (expr) == COMPOUND_EXPR
18352 && !COMPOUND_EXPR_OVERLOADED (expr))
18353 return build2 (COMPOUND_EXPR,
18354 TREE_TYPE (expr),
18355 TREE_OPERAND (expr, 0),
18356 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18358 /* If the type is unknown, it can't really be non-dependent */
18359 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18361 /* Otherwise, build a NON_DEPENDENT_EXPR.
18363 REFERENCE_TYPEs are not stripped for expressions in templates
18364 because doing so would play havoc with mangling. Consider, for
18365 example:
18367 template <typename T> void f<T& g>() { g(); }
18369 In the body of "f", the expression for "g" will have
18370 REFERENCE_TYPE, even though the standard says that it should
18371 not. The reason is that we must preserve the syntactic form of
18372 the expression so that mangling (say) "f<g>" inside the body of
18373 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18374 stripped here. */
18375 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18378 /* ARGS is a vector of expressions as arguments to a function call.
18379 Replace the arguments with equivalent non-dependent expressions.
18380 This modifies ARGS in place. */
18382 void
18383 make_args_non_dependent (VEC(tree,gc) *args)
18385 unsigned int ix;
18386 tree arg;
18388 FOR_EACH_VEC_ELT (tree, args, ix, arg)
18390 tree newarg = build_non_dependent_expr (arg);
18391 if (newarg != arg)
18392 VEC_replace (tree, args, ix, newarg);
18396 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18397 with a level one deeper than the actual template parms. */
18399 tree
18400 make_auto (void)
18402 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18403 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18404 TYPE_DECL, get_identifier ("auto"), au);
18405 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18406 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18407 (0, processing_template_decl + 1, processing_template_decl + 1,
18408 TYPE_NAME (au), NULL_TREE);
18409 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18410 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18411 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18413 return au;
18416 /* Given type ARG, return std::initializer_list<ARG>. */
18418 static tree
18419 listify (tree arg)
18421 tree std_init_list = namespace_binding
18422 (get_identifier ("initializer_list"), std_node);
18423 tree argvec;
18424 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18426 error ("deducing from brace-enclosed initializer list requires "
18427 "#include <initializer_list>");
18428 return error_mark_node;
18430 argvec = make_tree_vec (1);
18431 TREE_VEC_ELT (argvec, 0) = arg;
18432 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18433 NULL_TREE, 0, tf_warning_or_error);
18436 /* Replace auto in TYPE with std::initializer_list<auto>. */
18438 static tree
18439 listify_autos (tree type, tree auto_node)
18441 tree init_auto = listify (auto_node);
18442 tree argvec = make_tree_vec (1);
18443 TREE_VEC_ELT (argvec, 0) = init_auto;
18444 if (processing_template_decl)
18445 argvec = add_to_template_args (current_template_args (), argvec);
18446 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18449 /* walk_tree helper for do_auto_deduction. */
18451 static tree
18452 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18453 void *type)
18455 /* Is this a variable with the type we're looking for? */
18456 if (DECL_P (*tp)
18457 && TREE_TYPE (*tp) == type)
18458 return *tp;
18459 else
18460 return NULL_TREE;
18463 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18464 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18466 tree
18467 do_auto_deduction (tree type, tree init, tree auto_node)
18469 tree parms, tparms, targs;
18470 tree args[1];
18471 tree decl;
18472 int val;
18474 /* The name of the object being declared shall not appear in the
18475 initializer expression. */
18476 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18477 if (decl)
18479 error ("variable %q#D with %<auto%> type used in its own "
18480 "initializer", decl);
18481 return error_mark_node;
18484 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18485 with either a new invented type template parameter U or, if the
18486 initializer is a braced-init-list (8.5.4), with
18487 std::initializer_list<U>. */
18488 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18489 type = listify_autos (type, auto_node);
18491 parms = build_tree_list (NULL_TREE, type);
18492 args[0] = init;
18493 tparms = make_tree_vec (1);
18494 targs = make_tree_vec (1);
18495 TREE_VEC_ELT (tparms, 0)
18496 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18497 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18498 DEDUCE_CALL, LOOKUP_NORMAL);
18499 if (val > 0)
18501 error ("unable to deduce %qT from %qE", type, init);
18502 return error_mark_node;
18505 /* If the list of declarators contains more than one declarator, the type
18506 of each declared variable is determined as described above. If the
18507 type deduced for the template parameter U is not the same in each
18508 deduction, the program is ill-formed. */
18509 if (TREE_TYPE (auto_node)
18510 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18512 error ("inconsistent deduction for %qT: %qT and then %qT",
18513 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18514 return error_mark_node;
18516 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18518 if (processing_template_decl)
18519 targs = add_to_template_args (current_template_args (), targs);
18520 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18523 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18524 result. */
18526 tree
18527 splice_late_return_type (tree type, tree late_return_type)
18529 tree argvec;
18531 if (late_return_type == NULL_TREE)
18532 return type;
18533 argvec = make_tree_vec (1);
18534 TREE_VEC_ELT (argvec, 0) = late_return_type;
18535 if (processing_template_decl)
18536 argvec = add_to_template_args (current_template_args (), argvec);
18537 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18540 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18542 bool
18543 is_auto (const_tree type)
18545 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18546 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18547 return true;
18548 else
18549 return false;
18552 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18553 appear as a type-specifier for the declaration in question, we don't
18554 have to look through the whole type. */
18556 tree
18557 type_uses_auto (tree type)
18559 enum tree_code code;
18560 if (is_auto (type))
18561 return type;
18563 code = TREE_CODE (type);
18565 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18566 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18567 || code == METHOD_TYPE || code == ARRAY_TYPE)
18568 return type_uses_auto (TREE_TYPE (type));
18570 if (TYPE_PTRMEMFUNC_P (type))
18571 return type_uses_auto (TREE_TYPE (TREE_TYPE
18572 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18574 return NULL_TREE;
18577 /* For a given template T, return the vector of typedefs referenced
18578 in T for which access check is needed at T instantiation time.
18579 T is either a FUNCTION_DECL or a RECORD_TYPE.
18580 Those typedefs were added to T by the function
18581 append_type_to_template_for_access_check. */
18583 VEC(qualified_typedef_usage_t,gc)*
18584 get_types_needing_access_check (tree t)
18586 tree ti;
18587 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18589 if (!t || t == error_mark_node)
18590 return NULL;
18592 if (!(ti = get_template_info (t)))
18593 return NULL;
18595 if (CLASS_TYPE_P (t)
18596 || TREE_CODE (t) == FUNCTION_DECL)
18598 if (!TI_TEMPLATE (ti))
18599 return NULL;
18601 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18604 return result;
18607 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18608 tied to T. That list of typedefs will be access checked at
18609 T instantiation time.
18610 T is either a FUNCTION_DECL or a RECORD_TYPE.
18611 TYPE_DECL is a TYPE_DECL node representing a typedef.
18612 SCOPE is the scope through which TYPE_DECL is accessed.
18613 LOCATION is the location of the usage point of TYPE_DECL.
18615 This function is a subroutine of
18616 append_type_to_template_for_access_check. */
18618 static void
18619 append_type_to_template_for_access_check_1 (tree t,
18620 tree type_decl,
18621 tree scope,
18622 location_t location)
18624 qualified_typedef_usage_t typedef_usage;
18625 tree ti;
18627 if (!t || t == error_mark_node)
18628 return;
18630 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18631 || CLASS_TYPE_P (t))
18632 && type_decl
18633 && TREE_CODE (type_decl) == TYPE_DECL
18634 && scope);
18636 if (!(ti = get_template_info (t)))
18637 return;
18639 gcc_assert (TI_TEMPLATE (ti));
18641 typedef_usage.typedef_decl = type_decl;
18642 typedef_usage.context = scope;
18643 typedef_usage.locus = location;
18645 VEC_safe_push (qualified_typedef_usage_t, gc,
18646 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18647 &typedef_usage);
18650 /* Append TYPE_DECL to the template TEMPL.
18651 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18652 At TEMPL instanciation time, TYPE_DECL will be checked to see
18653 if it can be accessed through SCOPE.
18654 LOCATION is the location of the usage point of TYPE_DECL.
18656 e.g. consider the following code snippet:
18658 class C
18660 typedef int myint;
18663 template<class U> struct S
18665 C::myint mi; // <-- usage point of the typedef C::myint
18668 S<char> s;
18670 At S<char> instantiation time, we need to check the access of C::myint
18671 In other words, we need to check the access of the myint typedef through
18672 the C scope. For that purpose, this function will add the myint typedef
18673 and the scope C through which its being accessed to a list of typedefs
18674 tied to the template S. That list will be walked at template instantiation
18675 time and access check performed on each typedefs it contains.
18676 Note that this particular code snippet should yield an error because
18677 myint is private to C. */
18679 void
18680 append_type_to_template_for_access_check (tree templ,
18681 tree type_decl,
18682 tree scope,
18683 location_t location)
18685 qualified_typedef_usage_t *iter;
18686 int i;
18688 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18690 /* Make sure we don't append the type to the template twice. */
18691 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
18692 get_types_needing_access_check (templ),
18693 i, iter)
18694 if (iter->typedef_decl == type_decl && scope == iter->context)
18695 return;
18697 append_type_to_template_for_access_check_1 (templ, type_decl,
18698 scope, location);
18701 /* Set up the hash tables for template instantiations. */
18703 void
18704 init_template_processing (void)
18706 decl_specializations = htab_create_ggc (37,
18707 hash_specialization,
18708 eq_specializations,
18709 ggc_free);
18710 type_specializations = htab_create_ggc (37,
18711 hash_specialization,
18712 eq_specializations,
18713 ggc_free);
18716 /* Print stats about the template hash tables for -fstats. */
18718 void
18719 print_template_statistics (void)
18721 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
18722 "%f collisions\n", (long) htab_size (decl_specializations),
18723 (long) htab_elements (decl_specializations),
18724 htab_collisions (decl_specializations));
18725 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
18726 "%f collisions\n", (long) htab_size (type_specializations),
18727 (long) htab_elements (type_specializations),
18728 htab_collisions (type_specializations));
18731 #include "gt-cp-pt.h"