recog.c (split_all_insns): Remove dead code.
[official-gcc.git] / gcc / cp / pt.c
blob8a6d451a69e0ed0e85942b8b9d20f6d0cab85921
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) : CP_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 || extra_args == error_mark_node)
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 /* Someday C++0x may allow for enum template specialization. */
911 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
912 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
913 pedwarn (input_location, OPT_pedantic, "template specialization "
914 "of %qD not allowed by ISO C++", type);
915 else
917 error ("explicit specialization of non-template %qT", type);
918 return error_mark_node;
922 return type;
925 /* Returns nonzero if we can optimize the retrieval of specializations
926 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
927 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
929 static inline bool
930 optimize_specialization_lookup_p (tree tmpl)
932 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
933 && DECL_CLASS_SCOPE_P (tmpl)
934 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
935 parameter. */
936 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
937 /* The optimized lookup depends on the fact that the
938 template arguments for the member function template apply
939 purely to the containing class, which is not true if the
940 containing class is an explicit or partial
941 specialization. */
942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
943 && !DECL_MEMBER_TEMPLATE_P (tmpl)
944 && !DECL_CONV_FN_P (tmpl)
945 /* It is possible to have a template that is not a member
946 template and is not a member of a template class:
948 template <typename T>
949 struct S { friend A::f(); };
951 Here, the friend function is a template, but the context does
952 not have template information. The optimized lookup relies
953 on having ARGS be the template arguments for both the class
954 and the function template. */
955 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
958 /* Retrieve the specialization (in the sense of [temp.spec] - a
959 specialization is either an instantiation or an explicit
960 specialization) of TMPL for the given template ARGS. If there is
961 no such specialization, return NULL_TREE. The ARGS are a vector of
962 arguments, or a vector of vectors of arguments, in the case of
963 templates with more than one level of parameters.
965 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
966 then we search for a partial specialization matching ARGS. This
967 parameter is ignored if TMPL is not a class template. */
969 static tree
970 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
972 if (args == error_mark_node)
973 return NULL_TREE;
975 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
977 /* There should be as many levels of arguments as there are
978 levels of parameters. */
979 gcc_assert (TMPL_ARGS_DEPTH (args)
980 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
982 if (optimize_specialization_lookup_p (tmpl))
984 tree class_template;
985 tree class_specialization;
986 VEC(tree,gc) *methods;
987 tree fns;
988 int idx;
990 /* The template arguments actually apply to the containing
991 class. Find the class specialization with those
992 arguments. */
993 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
994 class_specialization
995 = retrieve_specialization (class_template, args, 0);
996 if (!class_specialization)
997 return NULL_TREE;
998 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
999 for the specialization. */
1000 idx = class_method_index_for_fn (class_specialization, tmpl);
1001 if (idx == -1)
1002 return NULL_TREE;
1003 /* Iterate through the methods with the indicated name, looking
1004 for the one that has an instance of TMPL. */
1005 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1006 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1008 tree fn = OVL_CURRENT (fns);
1009 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1010 /* using-declarations can add base methods to the method vec,
1011 and we don't want those here. */
1012 && DECL_CONTEXT (fn) == class_specialization)
1013 return fn;
1015 return NULL_TREE;
1017 else
1019 spec_entry *found;
1020 spec_entry elt;
1021 htab_t specializations;
1023 elt.tmpl = tmpl;
1024 elt.args = args;
1025 elt.spec = NULL_TREE;
1027 if (DECL_CLASS_TEMPLATE_P (tmpl))
1028 specializations = type_specializations;
1029 else
1030 specializations = decl_specializations;
1032 if (hash == 0)
1033 hash = hash_specialization (&elt);
1034 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1035 if (found)
1036 return found->spec;
1039 return NULL_TREE;
1042 /* Like retrieve_specialization, but for local declarations. */
1044 static tree
1045 retrieve_local_specialization (tree tmpl)
1047 tree spec;
1049 if (local_specializations == NULL)
1050 return NULL_TREE;
1052 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1053 htab_hash_pointer (tmpl));
1054 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1057 /* Returns nonzero iff DECL is a specialization of TMPL. */
1060 is_specialization_of (tree decl, tree tmpl)
1062 tree t;
1064 if (TREE_CODE (decl) == FUNCTION_DECL)
1066 for (t = decl;
1067 t != NULL_TREE;
1068 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1069 if (t == tmpl)
1070 return 1;
1072 else
1074 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1076 for (t = TREE_TYPE (decl);
1077 t != NULL_TREE;
1078 t = CLASSTYPE_USE_TEMPLATE (t)
1079 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1080 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1081 return 1;
1084 return 0;
1087 /* Returns nonzero iff DECL is a specialization of friend declaration
1088 FRIEND_DECL according to [temp.friend]. */
1090 bool
1091 is_specialization_of_friend (tree decl, tree friend_decl)
1093 bool need_template = true;
1094 int template_depth;
1096 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1097 || TREE_CODE (decl) == TYPE_DECL);
1099 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1100 of a template class, we want to check if DECL is a specialization
1101 if this. */
1102 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1103 && DECL_TEMPLATE_INFO (friend_decl)
1104 && !DECL_USE_TEMPLATE (friend_decl))
1106 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1107 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1108 need_template = false;
1110 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1111 && !PRIMARY_TEMPLATE_P (friend_decl))
1112 need_template = false;
1114 /* There is nothing to do if this is not a template friend. */
1115 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1116 return false;
1118 if (is_specialization_of (decl, friend_decl))
1119 return true;
1121 /* [temp.friend/6]
1122 A member of a class template may be declared to be a friend of a
1123 non-template class. In this case, the corresponding member of
1124 every specialization of the class template is a friend of the
1125 class granting friendship.
1127 For example, given a template friend declaration
1129 template <class T> friend void A<T>::f();
1131 the member function below is considered a friend
1133 template <> struct A<int> {
1134 void f();
1137 For this type of template friend, TEMPLATE_DEPTH below will be
1138 nonzero. To determine if DECL is a friend of FRIEND, we first
1139 check if the enclosing class is a specialization of another. */
1141 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1142 if (template_depth
1143 && DECL_CLASS_SCOPE_P (decl)
1144 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1145 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1147 /* Next, we check the members themselves. In order to handle
1148 a few tricky cases, such as when FRIEND_DECL's are
1150 template <class T> friend void A<T>::g(T t);
1151 template <class T> template <T t> friend void A<T>::h();
1153 and DECL's are
1155 void A<int>::g(int);
1156 template <int> void A<int>::h();
1158 we need to figure out ARGS, the template arguments from
1159 the context of DECL. This is required for template substitution
1160 of `T' in the function parameter of `g' and template parameter
1161 of `h' in the above examples. Here ARGS corresponds to `int'. */
1163 tree context = DECL_CONTEXT (decl);
1164 tree args = NULL_TREE;
1165 int current_depth = 0;
1167 while (current_depth < template_depth)
1169 if (CLASSTYPE_TEMPLATE_INFO (context))
1171 if (current_depth == 0)
1172 args = TYPE_TI_ARGS (context);
1173 else
1174 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1175 current_depth++;
1177 context = TYPE_CONTEXT (context);
1180 if (TREE_CODE (decl) == FUNCTION_DECL)
1182 bool is_template;
1183 tree friend_type;
1184 tree decl_type;
1185 tree friend_args_type;
1186 tree decl_args_type;
1188 /* Make sure that both DECL and FRIEND_DECL are templates or
1189 non-templates. */
1190 is_template = DECL_TEMPLATE_INFO (decl)
1191 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1192 if (need_template ^ is_template)
1193 return false;
1194 else if (is_template)
1196 /* If both are templates, check template parameter list. */
1197 tree friend_parms
1198 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1199 args, tf_none);
1200 if (!comp_template_parms
1201 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1202 friend_parms))
1203 return false;
1205 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1207 else
1208 decl_type = TREE_TYPE (decl);
1210 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1211 tf_none, NULL_TREE);
1212 if (friend_type == error_mark_node)
1213 return false;
1215 /* Check if return types match. */
1216 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1217 return false;
1219 /* Check if function parameter types match, ignoring the
1220 `this' parameter. */
1221 friend_args_type = TYPE_ARG_TYPES (friend_type);
1222 decl_args_type = TYPE_ARG_TYPES (decl_type);
1223 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1224 friend_args_type = TREE_CHAIN (friend_args_type);
1225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1226 decl_args_type = TREE_CHAIN (decl_args_type);
1228 return compparms (decl_args_type, friend_args_type);
1230 else
1232 /* DECL is a TYPE_DECL */
1233 bool is_template;
1234 tree decl_type = TREE_TYPE (decl);
1236 /* Make sure that both DECL and FRIEND_DECL are templates or
1237 non-templates. */
1238 is_template
1239 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1240 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1242 if (need_template ^ is_template)
1243 return false;
1244 else if (is_template)
1246 tree friend_parms;
1247 /* If both are templates, check the name of the two
1248 TEMPLATE_DECL's first because is_friend didn't. */
1249 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1250 != DECL_NAME (friend_decl))
1251 return false;
1253 /* Now check template parameter list. */
1254 friend_parms
1255 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1256 args, tf_none);
1257 return comp_template_parms
1258 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1259 friend_parms);
1261 else
1262 return (DECL_NAME (decl)
1263 == DECL_NAME (friend_decl));
1266 return false;
1269 /* Register the specialization SPEC as a specialization of TMPL with
1270 the indicated ARGS. IS_FRIEND indicates whether the specialization
1271 is actually just a friend declaration. Returns SPEC, or an
1272 equivalent prior declaration, if available. */
1274 static tree
1275 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1276 hashval_t hash)
1278 tree fn;
1279 spec_entry **slot = NULL;
1280 spec_entry elt;
1282 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1284 if (TREE_CODE (spec) == FUNCTION_DECL
1285 && uses_template_parms (DECL_TI_ARGS (spec)))
1286 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1287 register it; we want the corresponding TEMPLATE_DECL instead.
1288 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1289 the more obvious `uses_template_parms (spec)' to avoid problems
1290 with default function arguments. In particular, given
1291 something like this:
1293 template <class T> void f(T t1, T t = T())
1295 the default argument expression is not substituted for in an
1296 instantiation unless and until it is actually needed. */
1297 return spec;
1299 if (optimize_specialization_lookup_p (tmpl))
1300 /* We don't put these specializations in the hash table, but we might
1301 want to give an error about a mismatch. */
1302 fn = retrieve_specialization (tmpl, args, 0);
1303 else
1305 elt.tmpl = tmpl;
1306 elt.args = args;
1307 elt.spec = spec;
1309 if (hash == 0)
1310 hash = hash_specialization (&elt);
1312 slot = (spec_entry **)
1313 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1314 if (*slot)
1315 fn = (*slot)->spec;
1316 else
1317 fn = NULL_TREE;
1320 /* We can sometimes try to re-register a specialization that we've
1321 already got. In particular, regenerate_decl_from_template calls
1322 duplicate_decls which will update the specialization list. But,
1323 we'll still get called again here anyhow. It's more convenient
1324 to simply allow this than to try to prevent it. */
1325 if (fn == spec)
1326 return spec;
1327 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1329 if (DECL_TEMPLATE_INSTANTIATION (fn))
1331 if (DECL_ODR_USED (fn)
1332 || DECL_EXPLICIT_INSTANTIATION (fn))
1334 error ("specialization of %qD after instantiation",
1335 fn);
1336 return error_mark_node;
1338 else
1340 tree clone;
1341 /* This situation should occur only if the first
1342 specialization is an implicit instantiation, the
1343 second is an explicit specialization, and the
1344 implicit instantiation has not yet been used. That
1345 situation can occur if we have implicitly
1346 instantiated a member function and then specialized
1347 it later.
1349 We can also wind up here if a friend declaration that
1350 looked like an instantiation turns out to be a
1351 specialization:
1353 template <class T> void foo(T);
1354 class S { friend void foo<>(int) };
1355 template <> void foo(int);
1357 We transform the existing DECL in place so that any
1358 pointers to it become pointers to the updated
1359 declaration.
1361 If there was a definition for the template, but not
1362 for the specialization, we want this to look as if
1363 there were no definition, and vice versa. */
1364 DECL_INITIAL (fn) = NULL_TREE;
1365 duplicate_decls (spec, fn, is_friend);
1366 /* The call to duplicate_decls will have applied
1367 [temp.expl.spec]:
1369 An explicit specialization of a function template
1370 is inline only if it is explicitly declared to be,
1371 and independently of whether its function template
1374 to the primary function; now copy the inline bits to
1375 the various clones. */
1376 FOR_EACH_CLONE (clone, fn)
1378 DECL_DECLARED_INLINE_P (clone)
1379 = DECL_DECLARED_INLINE_P (fn);
1380 DECL_SOURCE_LOCATION (clone)
1381 = DECL_SOURCE_LOCATION (fn);
1383 check_specialization_namespace (fn);
1385 return fn;
1388 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1390 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1391 /* Dup decl failed, but this is a new definition. Set the
1392 line number so any errors match this new
1393 definition. */
1394 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1396 return fn;
1399 else if (fn)
1400 return duplicate_decls (spec, fn, is_friend);
1402 /* A specialization must be declared in the same namespace as the
1403 template it is specializing. */
1404 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1405 && !check_specialization_namespace (tmpl))
1406 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1408 if (!optimize_specialization_lookup_p (tmpl))
1410 gcc_assert (tmpl && args && spec);
1411 *slot = ggc_alloc_spec_entry ();
1412 **slot = elt;
1413 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1414 && PRIMARY_TEMPLATE_P (tmpl)
1415 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1416 /* TMPL is a forward declaration of a template function; keep a list
1417 of all specializations in case we need to reassign them to a friend
1418 template later in tsubst_friend_function. */
1419 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1420 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1423 return spec;
1426 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1427 TMPL and ARGS members, ignores SPEC. */
1429 static int
1430 eq_specializations (const void *p1, const void *p2)
1432 const spec_entry *e1 = (const spec_entry *)p1;
1433 const spec_entry *e2 = (const spec_entry *)p2;
1435 return (e1->tmpl == e2->tmpl
1436 && comp_template_args (e1->args, e2->args));
1439 /* Returns a hash for a template TMPL and template arguments ARGS. */
1441 static hashval_t
1442 hash_tmpl_and_args (tree tmpl, tree args)
1444 hashval_t val = DECL_UID (tmpl);
1445 return iterative_hash_template_arg (args, val);
1448 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1449 ignoring SPEC. */
1451 static hashval_t
1452 hash_specialization (const void *p)
1454 const spec_entry *e = (const spec_entry *)p;
1455 return hash_tmpl_and_args (e->tmpl, e->args);
1458 /* Recursively calculate a hash value for a template argument ARG, for use
1459 in the hash tables of template specializations. */
1461 static hashval_t
1462 iterative_hash_template_arg (tree arg, hashval_t val)
1464 unsigned HOST_WIDE_INT i;
1465 enum tree_code code;
1466 char tclass;
1468 if (arg == NULL_TREE)
1469 return iterative_hash_object (arg, val);
1471 if (!TYPE_P (arg))
1472 STRIP_NOPS (arg);
1474 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1475 /* We can get one of these when re-hashing a previous entry in the middle
1476 of substituting into a pack expansion. Just look through it. */
1477 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1479 code = TREE_CODE (arg);
1480 tclass = TREE_CODE_CLASS (code);
1482 val = iterative_hash_object (code, val);
1484 switch (code)
1486 case ERROR_MARK:
1487 return val;
1489 case IDENTIFIER_NODE:
1490 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1492 case TREE_VEC:
1494 int i, len = TREE_VEC_LENGTH (arg);
1495 for (i = 0; i < len; ++i)
1496 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1497 return val;
1500 case TYPE_PACK_EXPANSION:
1501 case EXPR_PACK_EXPANSION:
1502 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1504 case TYPE_ARGUMENT_PACK:
1505 case NONTYPE_ARGUMENT_PACK:
1506 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1508 case TREE_LIST:
1509 for (; arg; arg = TREE_CHAIN (arg))
1510 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1511 return val;
1513 case OVERLOAD:
1514 for (; arg; arg = OVL_CHAIN (arg))
1515 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1516 return val;
1518 case CONSTRUCTOR:
1520 tree field, value;
1521 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1523 val = iterative_hash_template_arg (field, val);
1524 val = iterative_hash_template_arg (value, val);
1526 return val;
1529 case PARM_DECL:
1530 if (!DECL_ARTIFICIAL (arg))
1531 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1532 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1534 case TARGET_EXPR:
1535 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1537 case PTRMEM_CST:
1538 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1539 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1541 case TEMPLATE_PARM_INDEX:
1542 val = iterative_hash_template_arg
1543 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1544 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1545 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1547 case TRAIT_EXPR:
1548 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1549 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1550 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1552 case BASELINK:
1553 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1554 val);
1555 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1556 val);
1558 case MODOP_EXPR:
1559 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1560 code = TREE_CODE (TREE_OPERAND (arg, 1));
1561 val = iterative_hash_object (code, val);
1562 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1564 case ARRAY_TYPE:
1565 /* layout_type sets structural equality for arrays of
1566 incomplete type, so we can't rely on the canonical type
1567 for hashing. */
1568 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1569 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1571 case LAMBDA_EXPR:
1572 /* A lambda can't appear in a template arg, but don't crash on
1573 erroneous input. */
1574 gcc_assert (seen_error ());
1575 return val;
1577 case CAST_EXPR:
1578 case STATIC_CAST_EXPR:
1579 case REINTERPRET_CAST_EXPR:
1580 case CONST_CAST_EXPR:
1581 case DYNAMIC_CAST_EXPR:
1582 case NEW_EXPR:
1583 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1584 /* Now hash operands as usual. */
1585 break;
1587 default:
1588 break;
1591 switch (tclass)
1593 case tcc_type:
1594 if (TYPE_CANONICAL (arg))
1595 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1596 val);
1597 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1598 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1599 /* Otherwise just compare the types during lookup. */
1600 return val;
1602 case tcc_declaration:
1603 case tcc_constant:
1604 return iterative_hash_expr (arg, val);
1606 default:
1607 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1609 unsigned n = TREE_OPERAND_LENGTH (arg);
1610 for (i = 0; i < n; ++i)
1611 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1612 return val;
1615 gcc_unreachable ();
1616 return 0;
1619 /* Unregister the specialization SPEC as a specialization of TMPL.
1620 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1621 if the SPEC was listed as a specialization of TMPL.
1623 Note that SPEC has been ggc_freed, so we can't look inside it. */
1625 bool
1626 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1628 spec_entry **slot;
1629 spec_entry elt;
1631 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1632 elt.args = TI_ARGS (tinfo);
1633 elt.spec = NULL_TREE;
1635 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1636 if (*slot)
1638 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1639 gcc_assert (new_spec != NULL_TREE);
1640 (*slot)->spec = new_spec;
1641 return 1;
1644 return 0;
1647 /* Compare an entry in the local specializations hash table P1 (which
1648 is really a pointer to a TREE_LIST) with P2 (which is really a
1649 DECL). */
1651 static int
1652 eq_local_specializations (const void *p1, const void *p2)
1654 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1657 /* Hash P1, an entry in the local specializations table. */
1659 static hashval_t
1660 hash_local_specialization (const void* p1)
1662 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1665 /* Like register_specialization, but for local declarations. We are
1666 registering SPEC, an instantiation of TMPL. */
1668 static void
1669 register_local_specialization (tree spec, tree tmpl)
1671 void **slot;
1673 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1674 htab_hash_pointer (tmpl), INSERT);
1675 *slot = build_tree_list (spec, tmpl);
1678 /* TYPE is a class type. Returns true if TYPE is an explicitly
1679 specialized class. */
1681 bool
1682 explicit_class_specialization_p (tree type)
1684 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1685 return false;
1686 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1689 /* Print the list of functions at FNS, going through all the overloads
1690 for each element of the list. Alternatively, FNS can not be a
1691 TREE_LIST, in which case it will be printed together with all the
1692 overloads.
1694 MORE and *STR should respectively be FALSE and NULL when the function
1695 is called from the outside. They are used internally on recursive
1696 calls. print_candidates manages the two parameters and leaves NULL
1697 in *STR when it ends. */
1699 static void
1700 print_candidates_1 (tree fns, bool more, const char **str)
1702 tree fn, fn2;
1703 char *spaces = NULL;
1705 for (fn = fns; fn; fn = OVL_NEXT (fn))
1706 if (TREE_CODE (fn) == TREE_LIST)
1708 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1709 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1710 print_candidates_1 (TREE_VALUE (fn2),
1711 TREE_CHAIN (fn2) || more, str);
1713 else
1715 if (!*str)
1717 /* Pick the prefix string. */
1718 if (!more && !OVL_NEXT (fns))
1720 error ("candidate is: %+#D", OVL_CURRENT (fn));
1721 continue;
1724 *str = _("candidates are:");
1725 spaces = get_spaces (*str);
1727 error ("%s %+#D", *str, OVL_CURRENT (fn));
1728 *str = spaces ? spaces : *str;
1731 if (!more)
1733 free (spaces);
1734 *str = NULL;
1738 /* Print the list of candidate FNS in an error message. */
1740 void
1741 print_candidates (tree fns)
1743 const char *str = NULL;
1744 print_candidates_1 (fns, false, &str);
1745 gcc_assert (str == NULL);
1748 /* Returns the template (one of the functions given by TEMPLATE_ID)
1749 which can be specialized to match the indicated DECL with the
1750 explicit template args given in TEMPLATE_ID. The DECL may be
1751 NULL_TREE if none is available. In that case, the functions in
1752 TEMPLATE_ID are non-members.
1754 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1755 specialization of a member template.
1757 The TEMPLATE_COUNT is the number of references to qualifying
1758 template classes that appeared in the name of the function. See
1759 check_explicit_specialization for a more accurate description.
1761 TSK indicates what kind of template declaration (if any) is being
1762 declared. TSK_TEMPLATE indicates that the declaration given by
1763 DECL, though a FUNCTION_DECL, has template parameters, and is
1764 therefore a template function.
1766 The template args (those explicitly specified and those deduced)
1767 are output in a newly created vector *TARGS_OUT.
1769 If it is impossible to determine the result, an error message is
1770 issued. The error_mark_node is returned to indicate failure. */
1772 static tree
1773 determine_specialization (tree template_id,
1774 tree decl,
1775 tree* targs_out,
1776 int need_member_template,
1777 int template_count,
1778 tmpl_spec_kind tsk)
1780 tree fns;
1781 tree targs;
1782 tree explicit_targs;
1783 tree candidates = NULL_TREE;
1784 /* A TREE_LIST of templates of which DECL may be a specialization.
1785 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1786 corresponding TREE_PURPOSE is the set of template arguments that,
1787 when used to instantiate the template, would produce a function
1788 with the signature of DECL. */
1789 tree templates = NULL_TREE;
1790 int header_count;
1791 struct cp_binding_level *b;
1793 *targs_out = NULL_TREE;
1795 if (template_id == error_mark_node || decl == error_mark_node)
1796 return error_mark_node;
1798 fns = TREE_OPERAND (template_id, 0);
1799 explicit_targs = TREE_OPERAND (template_id, 1);
1801 if (fns == error_mark_node)
1802 return error_mark_node;
1804 /* Check for baselinks. */
1805 if (BASELINK_P (fns))
1806 fns = BASELINK_FUNCTIONS (fns);
1808 if (!is_overloaded_fn (fns))
1810 error ("%qD is not a function template", fns);
1811 return error_mark_node;
1814 /* Count the number of template headers specified for this
1815 specialization. */
1816 header_count = 0;
1817 for (b = current_binding_level;
1818 b->kind == sk_template_parms;
1819 b = b->level_chain)
1820 ++header_count;
1822 for (; fns; fns = OVL_NEXT (fns))
1824 tree fn = OVL_CURRENT (fns);
1826 if (TREE_CODE (fn) == TEMPLATE_DECL)
1828 tree decl_arg_types;
1829 tree fn_arg_types;
1831 /* In case of explicit specialization, we need to check if
1832 the number of template headers appearing in the specialization
1833 is correct. This is usually done in check_explicit_specialization,
1834 but the check done there cannot be exhaustive when specializing
1835 member functions. Consider the following code:
1837 template <> void A<int>::f(int);
1838 template <> template <> void A<int>::f(int);
1840 Assuming that A<int> is not itself an explicit specialization
1841 already, the first line specializes "f" which is a non-template
1842 member function, whilst the second line specializes "f" which
1843 is a template member function. So both lines are syntactically
1844 correct, and check_explicit_specialization does not reject
1845 them.
1847 Here, we can do better, as we are matching the specialization
1848 against the declarations. We count the number of template
1849 headers, and we check if they match TEMPLATE_COUNT + 1
1850 (TEMPLATE_COUNT is the number of qualifying template classes,
1851 plus there must be another header for the member template
1852 itself).
1854 Notice that if header_count is zero, this is not a
1855 specialization but rather a template instantiation, so there
1856 is no check we can perform here. */
1857 if (header_count && header_count != template_count + 1)
1858 continue;
1860 /* Check that the number of template arguments at the
1861 innermost level for DECL is the same as for FN. */
1862 if (current_binding_level->kind == sk_template_parms
1863 && !current_binding_level->explicit_spec_p
1864 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1865 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1866 (current_template_parms))))
1867 continue;
1869 /* DECL might be a specialization of FN. */
1870 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1871 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1873 /* For a non-static member function, we need to make sure
1874 that the const qualification is the same. Since
1875 get_bindings does not try to merge the "this" parameter,
1876 we must do the comparison explicitly. */
1877 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1878 && !same_type_p (TREE_VALUE (fn_arg_types),
1879 TREE_VALUE (decl_arg_types)))
1880 continue;
1882 /* Skip the "this" parameter and, for constructors of
1883 classes with virtual bases, the VTT parameter. A
1884 full specialization of a constructor will have a VTT
1885 parameter, but a template never will. */
1886 decl_arg_types
1887 = skip_artificial_parms_for (decl, decl_arg_types);
1888 fn_arg_types
1889 = skip_artificial_parms_for (fn, fn_arg_types);
1891 /* Check that the number of function parameters matches.
1892 For example,
1893 template <class T> void f(int i = 0);
1894 template <> void f<int>();
1895 The specialization f<int> is invalid but is not caught
1896 by get_bindings below. */
1897 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1898 continue;
1900 /* Function templates cannot be specializations; there are
1901 no partial specializations of functions. Therefore, if
1902 the type of DECL does not match FN, there is no
1903 match. */
1904 if (tsk == tsk_template)
1906 if (compparms (fn_arg_types, decl_arg_types))
1907 candidates = tree_cons (NULL_TREE, fn, candidates);
1908 continue;
1911 /* See whether this function might be a specialization of this
1912 template. */
1913 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1915 if (!targs)
1916 /* We cannot deduce template arguments that when used to
1917 specialize TMPL will produce DECL. */
1918 continue;
1920 /* Save this template, and the arguments deduced. */
1921 templates = tree_cons (targs, fn, templates);
1923 else if (need_member_template)
1924 /* FN is an ordinary member function, and we need a
1925 specialization of a member template. */
1927 else if (TREE_CODE (fn) != FUNCTION_DECL)
1928 /* We can get IDENTIFIER_NODEs here in certain erroneous
1929 cases. */
1931 else if (!DECL_FUNCTION_MEMBER_P (fn))
1932 /* This is just an ordinary non-member function. Nothing can
1933 be a specialization of that. */
1935 else if (DECL_ARTIFICIAL (fn))
1936 /* Cannot specialize functions that are created implicitly. */
1938 else
1940 tree decl_arg_types;
1942 /* This is an ordinary member function. However, since
1943 we're here, we can assume it's enclosing class is a
1944 template class. For example,
1946 template <typename T> struct S { void f(); };
1947 template <> void S<int>::f() {}
1949 Here, S<int>::f is a non-template, but S<int> is a
1950 template class. If FN has the same type as DECL, we
1951 might be in business. */
1953 if (!DECL_TEMPLATE_INFO (fn))
1954 /* Its enclosing class is an explicit specialization
1955 of a template class. This is not a candidate. */
1956 continue;
1958 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1959 TREE_TYPE (TREE_TYPE (fn))))
1960 /* The return types differ. */
1961 continue;
1963 /* Adjust the type of DECL in case FN is a static member. */
1964 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1965 if (DECL_STATIC_FUNCTION_P (fn)
1966 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1967 decl_arg_types = TREE_CHAIN (decl_arg_types);
1969 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1970 decl_arg_types))
1971 /* They match! */
1972 candidates = tree_cons (NULL_TREE, fn, candidates);
1976 if (templates && TREE_CHAIN (templates))
1978 /* We have:
1980 [temp.expl.spec]
1982 It is possible for a specialization with a given function
1983 signature to be instantiated from more than one function
1984 template. In such cases, explicit specification of the
1985 template arguments must be used to uniquely identify the
1986 function template specialization being specialized.
1988 Note that here, there's no suggestion that we're supposed to
1989 determine which of the candidate templates is most
1990 specialized. However, we, also have:
1992 [temp.func.order]
1994 Partial ordering of overloaded function template
1995 declarations is used in the following contexts to select
1996 the function template to which a function template
1997 specialization refers:
1999 -- when an explicit specialization refers to a function
2000 template.
2002 So, we do use the partial ordering rules, at least for now.
2003 This extension can only serve to make invalid programs valid,
2004 so it's safe. And, there is strong anecdotal evidence that
2005 the committee intended the partial ordering rules to apply;
2006 the EDG front end has that behavior, and John Spicer claims
2007 that the committee simply forgot to delete the wording in
2008 [temp.expl.spec]. */
2009 tree tmpl = most_specialized_instantiation (templates);
2010 if (tmpl != error_mark_node)
2012 templates = tmpl;
2013 TREE_CHAIN (templates) = NULL_TREE;
2017 if (templates == NULL_TREE && candidates == NULL_TREE)
2019 error ("template-id %qD for %q+D does not match any template "
2020 "declaration", template_id, decl);
2021 if (header_count && header_count != template_count + 1)
2022 inform (input_location, "saw %d %<template<>%>, need %d for "
2023 "specializing a member function template",
2024 header_count, template_count + 1);
2025 return error_mark_node;
2027 else if ((templates && TREE_CHAIN (templates))
2028 || (candidates && TREE_CHAIN (candidates))
2029 || (templates && candidates))
2031 error ("ambiguous template specialization %qD for %q+D",
2032 template_id, decl);
2033 candidates = chainon (candidates, templates);
2034 print_candidates (candidates);
2035 return error_mark_node;
2038 /* We have one, and exactly one, match. */
2039 if (candidates)
2041 tree fn = TREE_VALUE (candidates);
2042 *targs_out = copy_node (DECL_TI_ARGS (fn));
2043 /* DECL is a re-declaration or partial instantiation of a template
2044 function. */
2045 if (TREE_CODE (fn) == TEMPLATE_DECL)
2046 return fn;
2047 /* It was a specialization of an ordinary member function in a
2048 template class. */
2049 return DECL_TI_TEMPLATE (fn);
2052 /* It was a specialization of a template. */
2053 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2054 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2056 *targs_out = copy_node (targs);
2057 SET_TMPL_ARGS_LEVEL (*targs_out,
2058 TMPL_ARGS_DEPTH (*targs_out),
2059 TREE_PURPOSE (templates));
2061 else
2062 *targs_out = TREE_PURPOSE (templates);
2063 return TREE_VALUE (templates);
2066 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2067 but with the default argument values filled in from those in the
2068 TMPL_TYPES. */
2070 static tree
2071 copy_default_args_to_explicit_spec_1 (tree spec_types,
2072 tree tmpl_types)
2074 tree new_spec_types;
2076 if (!spec_types)
2077 return NULL_TREE;
2079 if (spec_types == void_list_node)
2080 return void_list_node;
2082 /* Substitute into the rest of the list. */
2083 new_spec_types =
2084 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2085 TREE_CHAIN (tmpl_types));
2087 /* Add the default argument for this parameter. */
2088 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2089 TREE_VALUE (spec_types),
2090 new_spec_types);
2093 /* DECL is an explicit specialization. Replicate default arguments
2094 from the template it specializes. (That way, code like:
2096 template <class T> void f(T = 3);
2097 template <> void f(double);
2098 void g () { f (); }
2100 works, as required.) An alternative approach would be to look up
2101 the correct default arguments at the call-site, but this approach
2102 is consistent with how implicit instantiations are handled. */
2104 static void
2105 copy_default_args_to_explicit_spec (tree decl)
2107 tree tmpl;
2108 tree spec_types;
2109 tree tmpl_types;
2110 tree new_spec_types;
2111 tree old_type;
2112 tree new_type;
2113 tree t;
2114 tree object_type = NULL_TREE;
2115 tree in_charge = NULL_TREE;
2116 tree vtt = NULL_TREE;
2118 /* See if there's anything we need to do. */
2119 tmpl = DECL_TI_TEMPLATE (decl);
2120 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2121 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2122 if (TREE_PURPOSE (t))
2123 break;
2124 if (!t)
2125 return;
2127 old_type = TREE_TYPE (decl);
2128 spec_types = TYPE_ARG_TYPES (old_type);
2130 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2132 /* Remove the this pointer, but remember the object's type for
2133 CV quals. */
2134 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2135 spec_types = TREE_CHAIN (spec_types);
2136 tmpl_types = TREE_CHAIN (tmpl_types);
2138 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2140 /* DECL may contain more parameters than TMPL due to the extra
2141 in-charge parameter in constructors and destructors. */
2142 in_charge = spec_types;
2143 spec_types = TREE_CHAIN (spec_types);
2145 if (DECL_HAS_VTT_PARM_P (decl))
2147 vtt = spec_types;
2148 spec_types = TREE_CHAIN (spec_types);
2152 /* Compute the merged default arguments. */
2153 new_spec_types =
2154 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2156 /* Compute the new FUNCTION_TYPE. */
2157 if (object_type)
2159 if (vtt)
2160 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2161 TREE_VALUE (vtt),
2162 new_spec_types);
2164 if (in_charge)
2165 /* Put the in-charge parameter back. */
2166 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2167 TREE_VALUE (in_charge),
2168 new_spec_types);
2170 new_type = build_method_type_directly (object_type,
2171 TREE_TYPE (old_type),
2172 new_spec_types);
2174 else
2175 new_type = build_function_type (TREE_TYPE (old_type),
2176 new_spec_types);
2177 new_type = cp_build_type_attribute_variant (new_type,
2178 TYPE_ATTRIBUTES (old_type));
2179 new_type = build_exception_variant (new_type,
2180 TYPE_RAISES_EXCEPTIONS (old_type));
2181 TREE_TYPE (decl) = new_type;
2184 /* Check to see if the function just declared, as indicated in
2185 DECLARATOR, and in DECL, is a specialization of a function
2186 template. We may also discover that the declaration is an explicit
2187 instantiation at this point.
2189 Returns DECL, or an equivalent declaration that should be used
2190 instead if all goes well. Issues an error message if something is
2191 amiss. Returns error_mark_node if the error is not easily
2192 recoverable.
2194 FLAGS is a bitmask consisting of the following flags:
2196 2: The function has a definition.
2197 4: The function is a friend.
2199 The TEMPLATE_COUNT is the number of references to qualifying
2200 template classes that appeared in the name of the function. For
2201 example, in
2203 template <class T> struct S { void f(); };
2204 void S<int>::f();
2206 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2207 classes are not counted in the TEMPLATE_COUNT, so that in
2209 template <class T> struct S {};
2210 template <> struct S<int> { void f(); }
2211 template <> void S<int>::f();
2213 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2214 invalid; there should be no template <>.)
2216 If the function is a specialization, it is marked as such via
2217 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2218 is set up correctly, and it is added to the list of specializations
2219 for that template. */
2221 tree
2222 check_explicit_specialization (tree declarator,
2223 tree decl,
2224 int template_count,
2225 int flags)
2227 int have_def = flags & 2;
2228 int is_friend = flags & 4;
2229 int specialization = 0;
2230 int explicit_instantiation = 0;
2231 int member_specialization = 0;
2232 tree ctype = DECL_CLASS_CONTEXT (decl);
2233 tree dname = DECL_NAME (decl);
2234 tmpl_spec_kind tsk;
2236 if (is_friend)
2238 if (!processing_specialization)
2239 tsk = tsk_none;
2240 else
2241 tsk = tsk_excessive_parms;
2243 else
2244 tsk = current_tmpl_spec_kind (template_count);
2246 switch (tsk)
2248 case tsk_none:
2249 if (processing_specialization)
2251 specialization = 1;
2252 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2254 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2256 if (is_friend)
2257 /* This could be something like:
2259 template <class T> void f(T);
2260 class S { friend void f<>(int); } */
2261 specialization = 1;
2262 else
2264 /* This case handles bogus declarations like template <>
2265 template <class T> void f<int>(); */
2267 error ("template-id %qD in declaration of primary template",
2268 declarator);
2269 return decl;
2272 break;
2274 case tsk_invalid_member_spec:
2275 /* The error has already been reported in
2276 check_specialization_scope. */
2277 return error_mark_node;
2279 case tsk_invalid_expl_inst:
2280 error ("template parameter list used in explicit instantiation");
2282 /* Fall through. */
2284 case tsk_expl_inst:
2285 if (have_def)
2286 error ("definition provided for explicit instantiation");
2288 explicit_instantiation = 1;
2289 break;
2291 case tsk_excessive_parms:
2292 case tsk_insufficient_parms:
2293 if (tsk == tsk_excessive_parms)
2294 error ("too many template parameter lists in declaration of %qD",
2295 decl);
2296 else if (template_header_count)
2297 error("too few template parameter lists in declaration of %qD", decl);
2298 else
2299 error("explicit specialization of %qD must be introduced by "
2300 "%<template <>%>", decl);
2302 /* Fall through. */
2303 case tsk_expl_spec:
2304 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2305 if (ctype)
2306 member_specialization = 1;
2307 else
2308 specialization = 1;
2309 break;
2311 case tsk_template:
2312 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2314 /* This case handles bogus declarations like template <>
2315 template <class T> void f<int>(); */
2317 if (uses_template_parms (declarator))
2318 error ("function template partial specialization %qD "
2319 "is not allowed", declarator);
2320 else
2321 error ("template-id %qD in declaration of primary template",
2322 declarator);
2323 return decl;
2326 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2327 /* This is a specialization of a member template, without
2328 specialization the containing class. Something like:
2330 template <class T> struct S {
2331 template <class U> void f (U);
2333 template <> template <class U> void S<int>::f(U) {}
2335 That's a specialization -- but of the entire template. */
2336 specialization = 1;
2337 break;
2339 default:
2340 gcc_unreachable ();
2343 if (specialization || member_specialization)
2345 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2346 for (; t; t = TREE_CHAIN (t))
2347 if (TREE_PURPOSE (t))
2349 permerror (input_location,
2350 "default argument specified in explicit specialization");
2351 break;
2355 if (specialization || member_specialization || explicit_instantiation)
2357 tree tmpl = NULL_TREE;
2358 tree targs = NULL_TREE;
2360 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2361 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2363 tree fns;
2365 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2366 if (ctype)
2367 fns = dname;
2368 else
2370 /* If there is no class context, the explicit instantiation
2371 must be at namespace scope. */
2372 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2374 /* Find the namespace binding, using the declaration
2375 context. */
2376 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2377 false, true);
2378 if (fns == error_mark_node || !is_overloaded_fn (fns))
2380 error ("%qD is not a template function", dname);
2381 fns = error_mark_node;
2383 else
2385 tree fn = OVL_CURRENT (fns);
2386 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2387 CP_DECL_CONTEXT (fn)))
2388 error ("%qD is not declared in %qD",
2389 decl, current_namespace);
2393 declarator = lookup_template_function (fns, NULL_TREE);
2396 if (declarator == error_mark_node)
2397 return error_mark_node;
2399 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2401 if (!explicit_instantiation)
2402 /* A specialization in class scope. This is invalid,
2403 but the error will already have been flagged by
2404 check_specialization_scope. */
2405 return error_mark_node;
2406 else
2408 /* It's not valid to write an explicit instantiation in
2409 class scope, e.g.:
2411 class C { template void f(); }
2413 This case is caught by the parser. However, on
2414 something like:
2416 template class C { void f(); };
2418 (which is invalid) we can get here. The error will be
2419 issued later. */
2423 return decl;
2425 else if (ctype != NULL_TREE
2426 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2427 IDENTIFIER_NODE))
2429 /* Find the list of functions in ctype that have the same
2430 name as the declared function. */
2431 tree name = TREE_OPERAND (declarator, 0);
2432 tree fns = NULL_TREE;
2433 int idx;
2435 if (constructor_name_p (name, ctype))
2437 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2439 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2440 : !CLASSTYPE_DESTRUCTORS (ctype))
2442 /* From [temp.expl.spec]:
2444 If such an explicit specialization for the member
2445 of a class template names an implicitly-declared
2446 special member function (clause _special_), the
2447 program is ill-formed.
2449 Similar language is found in [temp.explicit]. */
2450 error ("specialization of implicitly-declared special member function");
2451 return error_mark_node;
2454 name = is_constructor ? ctor_identifier : dtor_identifier;
2457 if (!DECL_CONV_FN_P (decl))
2459 idx = lookup_fnfields_1 (ctype, name);
2460 if (idx >= 0)
2461 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2463 else
2465 VEC(tree,gc) *methods;
2466 tree ovl;
2468 /* For a type-conversion operator, we cannot do a
2469 name-based lookup. We might be looking for `operator
2470 int' which will be a specialization of `operator T'.
2471 So, we find *all* the conversion operators, and then
2472 select from them. */
2473 fns = NULL_TREE;
2475 methods = CLASSTYPE_METHOD_VEC (ctype);
2476 if (methods)
2477 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2478 VEC_iterate (tree, methods, idx, ovl);
2479 ++idx)
2481 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2482 /* There are no more conversion functions. */
2483 break;
2485 /* Glue all these conversion functions together
2486 with those we already have. */
2487 for (; ovl; ovl = OVL_NEXT (ovl))
2488 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2492 if (fns == NULL_TREE)
2494 error ("no member function %qD declared in %qT", name, ctype);
2495 return error_mark_node;
2497 else
2498 TREE_OPERAND (declarator, 0) = fns;
2501 /* Figure out what exactly is being specialized at this point.
2502 Note that for an explicit instantiation, even one for a
2503 member function, we cannot tell apriori whether the
2504 instantiation is for a member template, or just a member
2505 function of a template class. Even if a member template is
2506 being instantiated, the member template arguments may be
2507 elided if they can be deduced from the rest of the
2508 declaration. */
2509 tmpl = determine_specialization (declarator, decl,
2510 &targs,
2511 member_specialization,
2512 template_count,
2513 tsk);
2515 if (!tmpl || tmpl == error_mark_node)
2516 /* We couldn't figure out what this declaration was
2517 specializing. */
2518 return error_mark_node;
2519 else
2521 tree gen_tmpl = most_general_template (tmpl);
2523 if (explicit_instantiation)
2525 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2526 is done by do_decl_instantiation later. */
2528 int arg_depth = TMPL_ARGS_DEPTH (targs);
2529 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2531 if (arg_depth > parm_depth)
2533 /* If TMPL is not the most general template (for
2534 example, if TMPL is a friend template that is
2535 injected into namespace scope), then there will
2536 be too many levels of TARGS. Remove some of them
2537 here. */
2538 int i;
2539 tree new_targs;
2541 new_targs = make_tree_vec (parm_depth);
2542 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2543 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2544 = TREE_VEC_ELT (targs, i);
2545 targs = new_targs;
2548 return instantiate_template (tmpl, targs, tf_error);
2551 /* If we thought that the DECL was a member function, but it
2552 turns out to be specializing a static member function,
2553 make DECL a static member function as well. */
2554 if (DECL_STATIC_FUNCTION_P (tmpl)
2555 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2556 revert_static_member_fn (decl);
2558 /* If this is a specialization of a member template of a
2559 template class, we want to return the TEMPLATE_DECL, not
2560 the specialization of it. */
2561 if (tsk == tsk_template)
2563 tree result = DECL_TEMPLATE_RESULT (tmpl);
2564 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2565 DECL_INITIAL (result) = NULL_TREE;
2566 if (have_def)
2568 tree parm;
2569 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2570 DECL_SOURCE_LOCATION (result)
2571 = DECL_SOURCE_LOCATION (decl);
2572 /* We want to use the argument list specified in the
2573 definition, not in the original declaration. */
2574 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2575 for (parm = DECL_ARGUMENTS (result); parm;
2576 parm = DECL_CHAIN (parm))
2577 DECL_CONTEXT (parm) = result;
2579 return register_specialization (tmpl, gen_tmpl, targs,
2580 is_friend, 0);
2583 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2584 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2586 /* Inherit default function arguments from the template
2587 DECL is specializing. */
2588 copy_default_args_to_explicit_spec (decl);
2590 /* This specialization has the same protection as the
2591 template it specializes. */
2592 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2593 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2595 /* 7.1.1-1 [dcl.stc]
2597 A storage-class-specifier shall not be specified in an
2598 explicit specialization...
2600 The parser rejects these, so unless action is taken here,
2601 explicit function specializations will always appear with
2602 global linkage.
2604 The action recommended by the C++ CWG in response to C++
2605 defect report 605 is to make the storage class and linkage
2606 of the explicit specialization match the templated function:
2608 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2610 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2612 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2613 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2615 /* This specialization has the same linkage and visibility as
2616 the function template it specializes. */
2617 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2618 if (! TREE_PUBLIC (decl))
2620 DECL_INTERFACE_KNOWN (decl) = 1;
2621 DECL_NOT_REALLY_EXTERN (decl) = 1;
2623 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2624 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2626 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2627 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2631 /* If DECL is a friend declaration, declared using an
2632 unqualified name, the namespace associated with DECL may
2633 have been set incorrectly. For example, in:
2635 template <typename T> void f(T);
2636 namespace N {
2637 struct S { friend void f<int>(int); }
2640 we will have set the DECL_CONTEXT for the friend
2641 declaration to N, rather than to the global namespace. */
2642 if (DECL_NAMESPACE_SCOPE_P (decl))
2643 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2645 if (is_friend && !have_def)
2646 /* This is not really a declaration of a specialization.
2647 It's just the name of an instantiation. But, it's not
2648 a request for an instantiation, either. */
2649 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2650 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2651 /* This is indeed a specialization. In case of constructors
2652 and destructors, we need in-charge and not-in-charge
2653 versions in V3 ABI. */
2654 clone_function_decl (decl, /*update_method_vec_p=*/0);
2656 /* Register this specialization so that we can find it
2657 again. */
2658 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2662 return decl;
2665 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2666 parameters. These are represented in the same format used for
2667 DECL_TEMPLATE_PARMS. */
2670 comp_template_parms (const_tree parms1, const_tree parms2)
2672 const_tree p1;
2673 const_tree p2;
2675 if (parms1 == parms2)
2676 return 1;
2678 for (p1 = parms1, p2 = parms2;
2679 p1 != NULL_TREE && p2 != NULL_TREE;
2680 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2682 tree t1 = TREE_VALUE (p1);
2683 tree t2 = TREE_VALUE (p2);
2684 int i;
2686 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2687 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2689 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2690 return 0;
2692 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2694 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2695 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2697 /* If either of the template parameters are invalid, assume
2698 they match for the sake of error recovery. */
2699 if (parm1 == error_mark_node || parm2 == error_mark_node)
2700 return 1;
2702 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2703 return 0;
2705 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2706 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2707 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2708 continue;
2709 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2710 return 0;
2714 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2715 /* One set of parameters has more parameters lists than the
2716 other. */
2717 return 0;
2719 return 1;
2722 /* Determine whether PARM is a parameter pack. */
2724 bool
2725 template_parameter_pack_p (const_tree parm)
2727 /* Determine if we have a non-type template parameter pack. */
2728 if (TREE_CODE (parm) == PARM_DECL)
2729 return (DECL_TEMPLATE_PARM_P (parm)
2730 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2732 /* If this is a list of template parameters, we could get a
2733 TYPE_DECL or a TEMPLATE_DECL. */
2734 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2735 parm = TREE_TYPE (parm);
2737 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2738 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2739 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2742 /* Determine if T is a function parameter pack. */
2744 bool
2745 function_parameter_pack_p (const_tree t)
2747 if (t && TREE_CODE (t) == PARM_DECL)
2748 return FUNCTION_PARAMETER_PACK_P (t);
2749 return false;
2752 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2753 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2755 tree
2756 get_function_template_decl (const_tree primary_func_tmpl_inst)
2758 if (! primary_func_tmpl_inst
2759 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2760 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2761 return NULL;
2763 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2766 /* Return true iff the function parameter PARAM_DECL was expanded
2767 from the function parameter pack PACK. */
2769 bool
2770 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2772 if (DECL_ARTIFICIAL (param_decl)
2773 || !function_parameter_pack_p (pack))
2774 return false;
2776 /* The parameter pack and its pack arguments have the same
2777 DECL_PARM_INDEX. */
2778 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2781 /* Determine whether ARGS describes a variadic template args list,
2782 i.e., one that is terminated by a template argument pack. */
2784 static bool
2785 template_args_variadic_p (tree args)
2787 int nargs;
2788 tree last_parm;
2790 if (args == NULL_TREE)
2791 return false;
2793 args = INNERMOST_TEMPLATE_ARGS (args);
2794 nargs = TREE_VEC_LENGTH (args);
2796 if (nargs == 0)
2797 return false;
2799 last_parm = TREE_VEC_ELT (args, nargs - 1);
2801 return ARGUMENT_PACK_P (last_parm);
2804 /* Generate a new name for the parameter pack name NAME (an
2805 IDENTIFIER_NODE) that incorporates its */
2807 static tree
2808 make_ith_pack_parameter_name (tree name, int i)
2810 /* Munge the name to include the parameter index. */
2811 #define NUMBUF_LEN 128
2812 char numbuf[NUMBUF_LEN];
2813 char* newname;
2814 int newname_len;
2816 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2817 newname_len = IDENTIFIER_LENGTH (name)
2818 + strlen (numbuf) + 2;
2819 newname = (char*)alloca (newname_len);
2820 snprintf (newname, newname_len,
2821 "%s#%i", IDENTIFIER_POINTER (name), i);
2822 return get_identifier (newname);
2825 /* Return true if T is a primary function
2826 or class template instantiation. */
2828 bool
2829 primary_template_instantiation_p (const_tree t)
2831 if (!t)
2832 return false;
2834 if (TREE_CODE (t) == FUNCTION_DECL)
2835 return DECL_LANG_SPECIFIC (t)
2836 && DECL_TEMPLATE_INSTANTIATION (t)
2837 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2838 else if (CLASS_TYPE_P (t))
2839 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2840 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2841 return false;
2844 /* Return true if PARM is a template template parameter. */
2846 bool
2847 template_template_parameter_p (const_tree parm)
2849 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2852 /* Return the template parameters of T if T is a
2853 primary template instantiation, NULL otherwise. */
2855 tree
2856 get_primary_template_innermost_parameters (const_tree t)
2858 tree parms = NULL, template_info = NULL;
2860 if ((template_info = get_template_info (t))
2861 && primary_template_instantiation_p (t))
2862 parms = INNERMOST_TEMPLATE_PARMS
2863 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2865 return parms;
2868 /* Return the template parameters of the LEVELth level from the full list
2869 of template parameters PARMS. */
2871 tree
2872 get_template_parms_at_level (tree parms, int level)
2874 tree p;
2875 if (!parms
2876 || TREE_CODE (parms) != TREE_LIST
2877 || level > TMPL_PARMS_DEPTH (parms))
2878 return NULL_TREE;
2880 for (p = parms; p; p = TREE_CHAIN (p))
2881 if (TMPL_PARMS_DEPTH (p) == level)
2882 return p;
2884 return NULL_TREE;
2887 /* Returns the template arguments of T if T is a template instantiation,
2888 NULL otherwise. */
2890 tree
2891 get_template_innermost_arguments (const_tree t)
2893 tree args = NULL, template_info = NULL;
2895 if ((template_info = get_template_info (t))
2896 && TI_ARGS (template_info))
2897 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2899 return args;
2902 /* Return the argument pack elements of T if T is a template argument pack,
2903 NULL otherwise. */
2905 tree
2906 get_template_argument_pack_elems (const_tree t)
2908 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2909 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2910 return NULL;
2912 return ARGUMENT_PACK_ARGS (t);
2915 /* Structure used to track the progress of find_parameter_packs_r. */
2916 struct find_parameter_pack_data
2918 /* TREE_LIST that will contain all of the parameter packs found by
2919 the traversal. */
2920 tree* parameter_packs;
2922 /* Set of AST nodes that have been visited by the traversal. */
2923 struct pointer_set_t *visited;
2926 /* Identifies all of the argument packs that occur in a template
2927 argument and appends them to the TREE_LIST inside DATA, which is a
2928 find_parameter_pack_data structure. This is a subroutine of
2929 make_pack_expansion and uses_parameter_packs. */
2930 static tree
2931 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2933 tree t = *tp;
2934 struct find_parameter_pack_data* ppd =
2935 (struct find_parameter_pack_data*)data;
2936 bool parameter_pack_p = false;
2938 /* Identify whether this is a parameter pack or not. */
2939 switch (TREE_CODE (t))
2941 case TEMPLATE_PARM_INDEX:
2942 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2943 parameter_pack_p = true;
2944 break;
2946 case TEMPLATE_TYPE_PARM:
2947 case TEMPLATE_TEMPLATE_PARM:
2948 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2949 parameter_pack_p = true;
2950 break;
2952 case PARM_DECL:
2953 if (FUNCTION_PARAMETER_PACK_P (t))
2955 /* We don't want to walk into the type of a PARM_DECL,
2956 because we don't want to see the type parameter pack. */
2957 *walk_subtrees = 0;
2958 parameter_pack_p = true;
2960 break;
2962 default:
2963 /* Not a parameter pack. */
2964 break;
2967 if (parameter_pack_p)
2969 /* Add this parameter pack to the list. */
2970 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2973 if (TYPE_P (t))
2974 cp_walk_tree (&TYPE_CONTEXT (t),
2975 &find_parameter_packs_r, ppd, ppd->visited);
2977 /* This switch statement will return immediately if we don't find a
2978 parameter pack. */
2979 switch (TREE_CODE (t))
2981 case TEMPLATE_PARM_INDEX:
2982 return NULL_TREE;
2984 case BOUND_TEMPLATE_TEMPLATE_PARM:
2985 /* Check the template itself. */
2986 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2987 &find_parameter_packs_r, ppd, ppd->visited);
2988 /* Check the template arguments. */
2989 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2990 ppd->visited);
2991 *walk_subtrees = 0;
2992 return NULL_TREE;
2994 case TEMPLATE_TYPE_PARM:
2995 case TEMPLATE_TEMPLATE_PARM:
2996 return NULL_TREE;
2998 case PARM_DECL:
2999 return NULL_TREE;
3001 case RECORD_TYPE:
3002 if (TYPE_PTRMEMFUNC_P (t))
3003 return NULL_TREE;
3004 /* Fall through. */
3006 case UNION_TYPE:
3007 case ENUMERAL_TYPE:
3008 if (TYPE_TEMPLATE_INFO (t))
3009 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3010 &find_parameter_packs_r, ppd, ppd->visited);
3012 *walk_subtrees = 0;
3013 return NULL_TREE;
3015 case TEMPLATE_DECL:
3016 cp_walk_tree (&TREE_TYPE (t),
3017 &find_parameter_packs_r, ppd, ppd->visited);
3018 return NULL_TREE;
3020 case TYPENAME_TYPE:
3021 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3022 ppd, ppd->visited);
3023 *walk_subtrees = 0;
3024 return NULL_TREE;
3026 case TYPE_PACK_EXPANSION:
3027 case EXPR_PACK_EXPANSION:
3028 *walk_subtrees = 0;
3029 return NULL_TREE;
3031 case INTEGER_TYPE:
3032 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3033 ppd, ppd->visited);
3034 *walk_subtrees = 0;
3035 return NULL_TREE;
3037 case IDENTIFIER_NODE:
3038 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3039 ppd->visited);
3040 *walk_subtrees = 0;
3041 return NULL_TREE;
3043 default:
3044 return NULL_TREE;
3047 return NULL_TREE;
3050 /* Determines if the expression or type T uses any parameter packs. */
3051 bool
3052 uses_parameter_packs (tree t)
3054 tree parameter_packs = NULL_TREE;
3055 struct find_parameter_pack_data ppd;
3056 ppd.parameter_packs = &parameter_packs;
3057 ppd.visited = pointer_set_create ();
3058 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3059 pointer_set_destroy (ppd.visited);
3060 return parameter_packs != NULL_TREE;
3063 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3064 representation a base-class initializer into a parameter pack
3065 expansion. If all goes well, the resulting node will be an
3066 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3067 respectively. */
3068 tree
3069 make_pack_expansion (tree arg)
3071 tree result;
3072 tree parameter_packs = NULL_TREE;
3073 bool for_types = false;
3074 struct find_parameter_pack_data ppd;
3076 if (!arg || arg == error_mark_node)
3077 return arg;
3079 if (TREE_CODE (arg) == TREE_LIST)
3081 /* The only time we will see a TREE_LIST here is for a base
3082 class initializer. In this case, the TREE_PURPOSE will be a
3083 _TYPE node (representing the base class expansion we're
3084 initializing) and the TREE_VALUE will be a TREE_LIST
3085 containing the initialization arguments.
3087 The resulting expansion looks somewhat different from most
3088 expansions. Rather than returning just one _EXPANSION, we
3089 return a TREE_LIST whose TREE_PURPOSE is a
3090 TYPE_PACK_EXPANSION containing the bases that will be
3091 initialized. The TREE_VALUE will be identical to the
3092 original TREE_VALUE, which is a list of arguments that will
3093 be passed to each base. We do not introduce any new pack
3094 expansion nodes into the TREE_VALUE (although it is possible
3095 that some already exist), because the TREE_PURPOSE and
3096 TREE_VALUE all need to be expanded together with the same
3097 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3098 resulting TREE_PURPOSE will mention the parameter packs in
3099 both the bases and the arguments to the bases. */
3100 tree purpose;
3101 tree value;
3102 tree parameter_packs = NULL_TREE;
3104 /* Determine which parameter packs will be used by the base
3105 class expansion. */
3106 ppd.visited = pointer_set_create ();
3107 ppd.parameter_packs = &parameter_packs;
3108 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3109 &ppd, ppd.visited);
3111 if (parameter_packs == NULL_TREE)
3113 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3114 pointer_set_destroy (ppd.visited);
3115 return error_mark_node;
3118 if (TREE_VALUE (arg) != void_type_node)
3120 /* Collect the sets of parameter packs used in each of the
3121 initialization arguments. */
3122 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3124 /* Determine which parameter packs will be expanded in this
3125 argument. */
3126 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3127 &ppd, ppd.visited);
3131 pointer_set_destroy (ppd.visited);
3133 /* Create the pack expansion type for the base type. */
3134 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3135 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3136 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3138 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3139 they will rarely be compared to anything. */
3140 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3142 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3145 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3146 for_types = true;
3148 /* Build the PACK_EXPANSION_* node. */
3149 result = for_types
3150 ? cxx_make_type (TYPE_PACK_EXPANSION)
3151 : make_node (EXPR_PACK_EXPANSION);
3152 SET_PACK_EXPANSION_PATTERN (result, arg);
3153 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3155 /* Propagate type and const-expression information. */
3156 TREE_TYPE (result) = TREE_TYPE (arg);
3157 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3159 else
3160 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3161 they will rarely be compared to anything. */
3162 SET_TYPE_STRUCTURAL_EQUALITY (result);
3164 /* Determine which parameter packs will be expanded. */
3165 ppd.parameter_packs = &parameter_packs;
3166 ppd.visited = pointer_set_create ();
3167 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3168 pointer_set_destroy (ppd.visited);
3170 /* Make sure we found some parameter packs. */
3171 if (parameter_packs == NULL_TREE)
3173 if (TYPE_P (arg))
3174 error ("expansion pattern %<%T%> contains no argument packs", arg);
3175 else
3176 error ("expansion pattern %<%E%> contains no argument packs", arg);
3177 return error_mark_node;
3179 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3181 return result;
3184 /* Checks T for any "bare" parameter packs, which have not yet been
3185 expanded, and issues an error if any are found. This operation can
3186 only be done on full expressions or types (e.g., an expression
3187 statement, "if" condition, etc.), because we could have expressions like:
3189 foo(f(g(h(args)))...)
3191 where "args" is a parameter pack. check_for_bare_parameter_packs
3192 should not be called for the subexpressions args, h(args),
3193 g(h(args)), or f(g(h(args))), because we would produce erroneous
3194 error messages.
3196 Returns TRUE and emits an error if there were bare parameter packs,
3197 returns FALSE otherwise. */
3198 bool
3199 check_for_bare_parameter_packs (tree t)
3201 tree parameter_packs = NULL_TREE;
3202 struct find_parameter_pack_data ppd;
3204 if (!processing_template_decl || !t || t == error_mark_node)
3205 return false;
3207 if (TREE_CODE (t) == TYPE_DECL)
3208 t = TREE_TYPE (t);
3210 ppd.parameter_packs = &parameter_packs;
3211 ppd.visited = pointer_set_create ();
3212 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3213 pointer_set_destroy (ppd.visited);
3215 if (parameter_packs)
3217 error ("parameter packs not expanded with %<...%>:");
3218 while (parameter_packs)
3220 tree pack = TREE_VALUE (parameter_packs);
3221 tree name = NULL_TREE;
3223 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3224 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3225 name = TYPE_NAME (pack);
3226 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3227 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3228 else
3229 name = DECL_NAME (pack);
3231 if (name)
3232 inform (input_location, " %qD", name);
3233 else
3234 inform (input_location, " <anonymous>");
3236 parameter_packs = TREE_CHAIN (parameter_packs);
3239 return true;
3242 return false;
3245 /* Expand any parameter packs that occur in the template arguments in
3246 ARGS. */
3247 tree
3248 expand_template_argument_pack (tree args)
3250 tree result_args = NULL_TREE;
3251 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3252 int num_result_args = -1;
3253 int non_default_args_count = -1;
3255 /* First, determine if we need to expand anything, and the number of
3256 slots we'll need. */
3257 for (in_arg = 0; in_arg < nargs; ++in_arg)
3259 tree arg = TREE_VEC_ELT (args, in_arg);
3260 if (arg == NULL_TREE)
3261 return args;
3262 if (ARGUMENT_PACK_P (arg))
3264 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3265 if (num_result_args < 0)
3266 num_result_args = in_arg + num_packed;
3267 else
3268 num_result_args += num_packed;
3270 else
3272 if (num_result_args >= 0)
3273 num_result_args++;
3277 /* If no expansion is necessary, we're done. */
3278 if (num_result_args < 0)
3279 return args;
3281 /* Expand arguments. */
3282 result_args = make_tree_vec (num_result_args);
3283 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3284 non_default_args_count =
3285 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3286 for (in_arg = 0; in_arg < nargs; ++in_arg)
3288 tree arg = TREE_VEC_ELT (args, in_arg);
3289 if (ARGUMENT_PACK_P (arg))
3291 tree packed = ARGUMENT_PACK_ARGS (arg);
3292 int i, num_packed = TREE_VEC_LENGTH (packed);
3293 for (i = 0; i < num_packed; ++i, ++out_arg)
3294 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3295 if (non_default_args_count > 0)
3296 non_default_args_count += num_packed;
3298 else
3300 TREE_VEC_ELT (result_args, out_arg) = arg;
3301 ++out_arg;
3304 if (non_default_args_count >= 0)
3305 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3306 return result_args;
3309 /* Checks if DECL shadows a template parameter.
3311 [temp.local]: A template-parameter shall not be redeclared within its
3312 scope (including nested scopes).
3314 Emits an error and returns TRUE if the DECL shadows a parameter,
3315 returns FALSE otherwise. */
3317 bool
3318 check_template_shadow (tree decl)
3320 tree olddecl;
3322 /* If we're not in a template, we can't possibly shadow a template
3323 parameter. */
3324 if (!current_template_parms)
3325 return true;
3327 /* Figure out what we're shadowing. */
3328 if (TREE_CODE (decl) == OVERLOAD)
3329 decl = OVL_CURRENT (decl);
3330 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3332 /* If there's no previous binding for this name, we're not shadowing
3333 anything, let alone a template parameter. */
3334 if (!olddecl)
3335 return true;
3337 /* If we're not shadowing a template parameter, we're done. Note
3338 that OLDDECL might be an OVERLOAD (or perhaps even an
3339 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3340 node. */
3341 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3342 return true;
3344 /* We check for decl != olddecl to avoid bogus errors for using a
3345 name inside a class. We check TPFI to avoid duplicate errors for
3346 inline member templates. */
3347 if (decl == olddecl
3348 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3349 return true;
3351 error ("declaration of %q+#D", decl);
3352 error (" shadows template parm %q+#D", olddecl);
3353 return false;
3356 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3357 ORIG_LEVEL, DECL, and TYPE. */
3359 static tree
3360 build_template_parm_index (int index,
3361 int level,
3362 int orig_level,
3363 tree decl,
3364 tree type)
3366 tree t = make_node (TEMPLATE_PARM_INDEX);
3367 TEMPLATE_PARM_IDX (t) = index;
3368 TEMPLATE_PARM_LEVEL (t) = level;
3369 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3370 TEMPLATE_PARM_DECL (t) = decl;
3371 TREE_TYPE (t) = type;
3372 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3373 TREE_READONLY (t) = TREE_READONLY (decl);
3375 return t;
3378 /* Find the canonical type parameter for the given template type
3379 parameter. Returns the canonical type parameter, which may be TYPE
3380 if no such parameter existed. */
3381 static tree
3382 canonical_type_parameter (tree type)
3384 tree list;
3385 int idx = TEMPLATE_TYPE_IDX (type);
3386 if (!canonical_template_parms)
3387 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3389 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3390 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3392 list = VEC_index (tree, canonical_template_parms, idx);
3393 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3394 list = TREE_CHAIN (list);
3396 if (list)
3397 return TREE_VALUE (list);
3398 else
3400 VEC_replace(tree, canonical_template_parms, idx,
3401 tree_cons (NULL_TREE, type,
3402 VEC_index (tree, canonical_template_parms, idx)));
3403 return type;
3407 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3408 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3409 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3410 new one is created. */
3412 static tree
3413 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3414 tsubst_flags_t complain)
3416 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3417 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3418 != TEMPLATE_PARM_LEVEL (index) - levels)
3419 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3421 tree orig_decl = TEMPLATE_PARM_DECL (index);
3422 tree decl, t;
3424 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3425 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3426 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3427 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3428 DECL_ARTIFICIAL (decl) = 1;
3429 SET_DECL_TEMPLATE_PARM_P (decl);
3431 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3432 TEMPLATE_PARM_LEVEL (index) - levels,
3433 TEMPLATE_PARM_ORIG_LEVEL (index),
3434 decl, type);
3435 TEMPLATE_PARM_DESCENDANTS (index) = t;
3436 TEMPLATE_PARM_PARAMETER_PACK (t)
3437 = TEMPLATE_PARM_PARAMETER_PACK (index);
3439 /* Template template parameters need this. */
3440 if (TREE_CODE (decl) == TEMPLATE_DECL)
3441 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3442 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3443 args, complain);
3446 return TEMPLATE_PARM_DESCENDANTS (index);
3449 /* Process information from new template parameter PARM and append it to the
3450 LIST being built. This new parameter is a non-type parameter iff
3451 IS_NON_TYPE is true. This new parameter is a parameter
3452 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3453 PARM_LOC. */
3455 tree
3456 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3457 bool is_parameter_pack)
3459 tree decl = 0;
3460 tree defval;
3461 tree err_parm_list;
3462 int idx = 0;
3464 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3465 defval = TREE_PURPOSE (parm);
3467 if (list)
3469 tree p = tree_last (list);
3471 if (p && TREE_VALUE (p) != error_mark_node)
3473 p = TREE_VALUE (p);
3474 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3475 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3476 else
3477 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3480 ++idx;
3482 else
3483 idx = 0;
3485 if (is_non_type)
3487 parm = TREE_VALUE (parm);
3489 SET_DECL_TEMPLATE_PARM_P (parm);
3491 if (TREE_TYPE (parm) == error_mark_node)
3493 err_parm_list = build_tree_list (defval, parm);
3494 TREE_VALUE (err_parm_list) = error_mark_node;
3495 return chainon (list, err_parm_list);
3497 else
3499 /* [temp.param]
3501 The top-level cv-qualifiers on the template-parameter are
3502 ignored when determining its type. */
3503 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3504 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3506 err_parm_list = build_tree_list (defval, parm);
3507 TREE_VALUE (err_parm_list) = error_mark_node;
3508 return chainon (list, err_parm_list);
3511 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3513 /* This template parameter is not a parameter pack, but it
3514 should be. Complain about "bare" parameter packs. */
3515 check_for_bare_parameter_packs (TREE_TYPE (parm));
3517 /* Recover by calling this a parameter pack. */
3518 is_parameter_pack = true;
3522 /* A template parameter is not modifiable. */
3523 TREE_CONSTANT (parm) = 1;
3524 TREE_READONLY (parm) = 1;
3525 decl = build_decl (parm_loc,
3526 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3527 TREE_CONSTANT (decl) = 1;
3528 TREE_READONLY (decl) = 1;
3529 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3530 = build_template_parm_index (idx, processing_template_decl,
3531 processing_template_decl,
3532 decl, TREE_TYPE (parm));
3534 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3535 = is_parameter_pack;
3537 else
3539 tree t;
3540 parm = TREE_VALUE (TREE_VALUE (parm));
3542 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3544 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3545 /* This is for distinguishing between real templates and template
3546 template parameters */
3547 TREE_TYPE (parm) = t;
3548 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3549 decl = parm;
3551 else
3553 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3554 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3555 decl = build_decl (parm_loc,
3556 TYPE_DECL, parm, t);
3559 TYPE_NAME (t) = decl;
3560 TYPE_STUB_DECL (t) = decl;
3561 parm = decl;
3562 TEMPLATE_TYPE_PARM_INDEX (t)
3563 = build_template_parm_index (idx, processing_template_decl,
3564 processing_template_decl,
3565 decl, TREE_TYPE (parm));
3566 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3567 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3569 DECL_ARTIFICIAL (decl) = 1;
3570 SET_DECL_TEMPLATE_PARM_P (decl);
3571 pushdecl (decl);
3572 parm = build_tree_list (defval, parm);
3573 return chainon (list, parm);
3576 /* The end of a template parameter list has been reached. Process the
3577 tree list into a parameter vector, converting each parameter into a more
3578 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3579 as PARM_DECLs. */
3581 tree
3582 end_template_parm_list (tree parms)
3584 int nparms;
3585 tree parm, next;
3586 tree saved_parmlist = make_tree_vec (list_length (parms));
3588 current_template_parms
3589 = tree_cons (size_int (processing_template_decl),
3590 saved_parmlist, current_template_parms);
3592 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3594 next = TREE_CHAIN (parm);
3595 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3596 TREE_CHAIN (parm) = NULL_TREE;
3597 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3598 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
3599 current_template_parms;
3602 --processing_template_parmlist;
3604 return saved_parmlist;
3607 /* end_template_decl is called after a template declaration is seen. */
3609 void
3610 end_template_decl (void)
3612 reset_specialization ();
3614 if (! processing_template_decl)
3615 return;
3617 /* This matches the pushlevel in begin_template_parm_list. */
3618 finish_scope ();
3620 --processing_template_decl;
3621 current_template_parms = TREE_CHAIN (current_template_parms);
3624 /* Within the declaration of a template, return all levels of template
3625 parameters that apply. The template parameters are represented as
3626 a TREE_VEC, in the form documented in cp-tree.h for template
3627 arguments. */
3629 static tree
3630 current_template_args (void)
3632 tree header;
3633 tree args = NULL_TREE;
3634 int length = TMPL_PARMS_DEPTH (current_template_parms);
3635 int l = length;
3637 /* If there is only one level of template parameters, we do not
3638 create a TREE_VEC of TREE_VECs. Instead, we return a single
3639 TREE_VEC containing the arguments. */
3640 if (length > 1)
3641 args = make_tree_vec (length);
3643 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3645 tree a = copy_node (TREE_VALUE (header));
3646 int i;
3648 TREE_TYPE (a) = NULL_TREE;
3649 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3651 tree t = TREE_VEC_ELT (a, i);
3653 /* T will be a list if we are called from within a
3654 begin/end_template_parm_list pair, but a vector directly
3655 if within a begin/end_member_template_processing pair. */
3656 if (TREE_CODE (t) == TREE_LIST)
3658 t = TREE_VALUE (t);
3660 if (!error_operand_p (t))
3662 if (TREE_CODE (t) == TYPE_DECL
3663 || TREE_CODE (t) == TEMPLATE_DECL)
3665 t = TREE_TYPE (t);
3667 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3669 /* Turn this argument into a TYPE_ARGUMENT_PACK
3670 with a single element, which expands T. */
3671 tree vec = make_tree_vec (1);
3672 #ifdef ENABLE_CHECKING
3673 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3674 (vec, TREE_VEC_LENGTH (vec));
3675 #endif
3676 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3678 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3679 SET_ARGUMENT_PACK_ARGS (t, vec);
3682 else
3684 t = DECL_INITIAL (t);
3686 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3688 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3689 with a single element, which expands T. */
3690 tree vec = make_tree_vec (1);
3691 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3692 #ifdef ENABLE_CHECKING
3693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3694 (vec, TREE_VEC_LENGTH (vec));
3695 #endif
3696 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3698 t = make_node (NONTYPE_ARGUMENT_PACK);
3699 SET_ARGUMENT_PACK_ARGS (t, vec);
3700 TREE_TYPE (t) = type;
3703 TREE_VEC_ELT (a, i) = t;
3708 #ifdef ENABLE_CHECKING
3709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3710 #endif
3712 if (length > 1)
3713 TREE_VEC_ELT (args, --l) = a;
3714 else
3715 args = a;
3718 return args;
3721 /* Update the declared TYPE by doing any lookups which were thought to be
3722 dependent, but are not now that we know the SCOPE of the declarator. */
3724 tree
3725 maybe_update_decl_type (tree orig_type, tree scope)
3727 tree type = orig_type;
3729 if (type == NULL_TREE)
3730 return type;
3732 if (TREE_CODE (orig_type) == TYPE_DECL)
3733 type = TREE_TYPE (type);
3735 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3736 && dependent_type_p (type)
3737 /* Don't bother building up the args in this case. */
3738 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3740 /* tsubst in the args corresponding to the template parameters,
3741 including auto if present. Most things will be unchanged, but
3742 make_typename_type and tsubst_qualified_id will resolve
3743 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3744 tree args = current_template_args ();
3745 tree auto_node = type_uses_auto (type);
3746 tree pushed;
3747 if (auto_node)
3749 tree auto_vec = make_tree_vec (1);
3750 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3751 args = add_to_template_args (args, auto_vec);
3753 pushed = push_scope (scope);
3754 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3755 if (pushed)
3756 pop_scope (scope);
3759 if (type == error_mark_node)
3760 return orig_type;
3762 if (TREE_CODE (orig_type) == TYPE_DECL)
3764 if (same_type_p (type, TREE_TYPE (orig_type)))
3765 type = orig_type;
3766 else
3767 type = TYPE_NAME (type);
3769 return type;
3772 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3773 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3774 a member template. Used by push_template_decl below. */
3776 static tree
3777 build_template_decl (tree decl, tree parms, bool member_template_p)
3779 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3780 DECL_TEMPLATE_PARMS (tmpl) = parms;
3781 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3782 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3784 return tmpl;
3787 struct template_parm_data
3789 /* The level of the template parameters we are currently
3790 processing. */
3791 int level;
3793 /* The index of the specialization argument we are currently
3794 processing. */
3795 int current_arg;
3797 /* An array whose size is the number of template parameters. The
3798 elements are nonzero if the parameter has been used in any one
3799 of the arguments processed so far. */
3800 int* parms;
3802 /* An array whose size is the number of template arguments. The
3803 elements are nonzero if the argument makes use of template
3804 parameters of this level. */
3805 int* arg_uses_template_parms;
3808 /* Subroutine of push_template_decl used to see if each template
3809 parameter in a partial specialization is used in the explicit
3810 argument list. If T is of the LEVEL given in DATA (which is
3811 treated as a template_parm_data*), then DATA->PARMS is marked
3812 appropriately. */
3814 static int
3815 mark_template_parm (tree t, void* data)
3817 int level;
3818 int idx;
3819 struct template_parm_data* tpd = (struct template_parm_data*) data;
3821 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3823 level = TEMPLATE_PARM_LEVEL (t);
3824 idx = TEMPLATE_PARM_IDX (t);
3826 else
3828 level = TEMPLATE_TYPE_LEVEL (t);
3829 idx = TEMPLATE_TYPE_IDX (t);
3832 if (level == tpd->level)
3834 tpd->parms[idx] = 1;
3835 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3838 /* Return zero so that for_each_template_parm will continue the
3839 traversal of the tree; we want to mark *every* template parm. */
3840 return 0;
3843 /* Process the partial specialization DECL. */
3845 static tree
3846 process_partial_specialization (tree decl)
3848 tree type = TREE_TYPE (decl);
3849 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3850 tree specargs = CLASSTYPE_TI_ARGS (type);
3851 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3852 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3853 tree inner_parms;
3854 tree inst;
3855 int nargs = TREE_VEC_LENGTH (inner_args);
3856 int ntparms;
3857 int i;
3858 bool did_error_intro = false;
3859 struct template_parm_data tpd;
3860 struct template_parm_data tpd2;
3862 gcc_assert (current_template_parms);
3864 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3865 ntparms = TREE_VEC_LENGTH (inner_parms);
3867 /* We check that each of the template parameters given in the
3868 partial specialization is used in the argument list to the
3869 specialization. For example:
3871 template <class T> struct S;
3872 template <class T> struct S<T*>;
3874 The second declaration is OK because `T*' uses the template
3875 parameter T, whereas
3877 template <class T> struct S<int>;
3879 is no good. Even trickier is:
3881 template <class T>
3882 struct S1
3884 template <class U>
3885 struct S2;
3886 template <class U>
3887 struct S2<T>;
3890 The S2<T> declaration is actually invalid; it is a
3891 full-specialization. Of course,
3893 template <class U>
3894 struct S2<T (*)(U)>;
3896 or some such would have been OK. */
3897 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3898 tpd.parms = XALLOCAVEC (int, ntparms);
3899 memset (tpd.parms, 0, sizeof (int) * ntparms);
3901 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
3902 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3903 for (i = 0; i < nargs; ++i)
3905 tpd.current_arg = i;
3906 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3907 &mark_template_parm,
3908 &tpd,
3909 NULL,
3910 /*include_nondeduced_p=*/false);
3912 for (i = 0; i < ntparms; ++i)
3913 if (tpd.parms[i] == 0)
3915 /* One of the template parms was not used in the
3916 specialization. */
3917 if (!did_error_intro)
3919 error ("template parameters not used in partial specialization:");
3920 did_error_intro = true;
3923 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3926 if (did_error_intro)
3927 return error_mark_node;
3929 /* [temp.class.spec]
3931 The argument list of the specialization shall not be identical to
3932 the implicit argument list of the primary template. */
3933 if (comp_template_args
3934 (inner_args,
3935 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3936 (maintmpl)))))
3937 error ("partial specialization %qT does not specialize any template arguments", type);
3939 /* [temp.class.spec]
3941 A partially specialized non-type argument expression shall not
3942 involve template parameters of the partial specialization except
3943 when the argument expression is a simple identifier.
3945 The type of a template parameter corresponding to a specialized
3946 non-type argument shall not be dependent on a parameter of the
3947 specialization.
3949 Also, we verify that pack expansions only occur at the
3950 end of the argument list. */
3951 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3952 tpd2.parms = 0;
3953 for (i = 0; i < nargs; ++i)
3955 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3956 tree arg = TREE_VEC_ELT (inner_args, i);
3957 tree packed_args = NULL_TREE;
3958 int j, len = 1;
3960 if (ARGUMENT_PACK_P (arg))
3962 /* Extract the arguments from the argument pack. We'll be
3963 iterating over these in the following loop. */
3964 packed_args = ARGUMENT_PACK_ARGS (arg);
3965 len = TREE_VEC_LENGTH (packed_args);
3968 for (j = 0; j < len; j++)
3970 if (packed_args)
3971 /* Get the Jth argument in the parameter pack. */
3972 arg = TREE_VEC_ELT (packed_args, j);
3974 if (PACK_EXPANSION_P (arg))
3976 /* Pack expansions must come at the end of the
3977 argument list. */
3978 if ((packed_args && j < len - 1)
3979 || (!packed_args && i < nargs - 1))
3981 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3982 error ("parameter pack argument %qE must be at the "
3983 "end of the template argument list", arg);
3984 else
3985 error ("parameter pack argument %qT must be at the "
3986 "end of the template argument list", arg);
3990 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3991 /* We only care about the pattern. */
3992 arg = PACK_EXPANSION_PATTERN (arg);
3994 if (/* These first two lines are the `non-type' bit. */
3995 !TYPE_P (arg)
3996 && TREE_CODE (arg) != TEMPLATE_DECL
3997 /* This next line is the `argument expression is not just a
3998 simple identifier' condition and also the `specialized
3999 non-type argument' bit. */
4000 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4002 if ((!packed_args && tpd.arg_uses_template_parms[i])
4003 || (packed_args && uses_template_parms (arg)))
4004 error ("template argument %qE involves template parameter(s)",
4005 arg);
4006 else
4008 /* Look at the corresponding template parameter,
4009 marking which template parameters its type depends
4010 upon. */
4011 tree type = TREE_TYPE (parm);
4013 if (!tpd2.parms)
4015 /* We haven't yet initialized TPD2. Do so now. */
4016 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4017 /* The number of parameters here is the number in the
4018 main template, which, as checked in the assertion
4019 above, is NARGS. */
4020 tpd2.parms = XALLOCAVEC (int, nargs);
4021 tpd2.level =
4022 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4025 /* Mark the template parameters. But this time, we're
4026 looking for the template parameters of the main
4027 template, not in the specialization. */
4028 tpd2.current_arg = i;
4029 tpd2.arg_uses_template_parms[i] = 0;
4030 memset (tpd2.parms, 0, sizeof (int) * nargs);
4031 for_each_template_parm (type,
4032 &mark_template_parm,
4033 &tpd2,
4034 NULL,
4035 /*include_nondeduced_p=*/false);
4037 if (tpd2.arg_uses_template_parms [i])
4039 /* The type depended on some template parameters.
4040 If they are fully specialized in the
4041 specialization, that's OK. */
4042 int j;
4043 int count = 0;
4044 for (j = 0; j < nargs; ++j)
4045 if (tpd2.parms[j] != 0
4046 && tpd.arg_uses_template_parms [j])
4047 ++count;
4048 if (count != 0)
4049 error_n (input_location, count,
4050 "type %qT of template argument %qE depends "
4051 "on a template parameter",
4052 "type %qT of template argument %qE depends "
4053 "on template parameters",
4054 type,
4055 arg);
4062 /* We should only get here once. */
4063 gcc_assert (!COMPLETE_TYPE_P (type));
4065 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4066 = tree_cons (specargs, inner_parms,
4067 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4068 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4070 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4071 inst = TREE_CHAIN (inst))
4073 tree inst_type = TREE_VALUE (inst);
4074 if (COMPLETE_TYPE_P (inst_type)
4075 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4077 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4078 if (spec && TREE_TYPE (spec) == type)
4079 permerror (input_location,
4080 "partial specialization of %qT after instantiation "
4081 "of %qT", type, inst_type);
4085 return decl;
4088 /* Check that a template declaration's use of default arguments and
4089 parameter packs is not invalid. Here, PARMS are the template
4090 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4091 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4092 specialization.
4095 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4096 declaration (but not a definition); 1 indicates a declaration, 2
4097 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4098 emitted for extraneous default arguments.
4100 Returns TRUE if there were no errors found, FALSE otherwise. */
4102 bool
4103 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4104 int is_partial, int is_friend_decl)
4106 const char *msg;
4107 int last_level_to_check;
4108 tree parm_level;
4109 bool no_errors = true;
4111 /* [temp.param]
4113 A default template-argument shall not be specified in a
4114 function template declaration or a function template definition, nor
4115 in the template-parameter-list of the definition of a member of a
4116 class template. */
4118 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4119 /* You can't have a function template declaration in a local
4120 scope, nor you can you define a member of a class template in a
4121 local scope. */
4122 return true;
4124 if (current_class_type
4125 && !TYPE_BEING_DEFINED (current_class_type)
4126 && DECL_LANG_SPECIFIC (decl)
4127 && DECL_DECLARES_FUNCTION_P (decl)
4128 /* If this is either a friend defined in the scope of the class
4129 or a member function. */
4130 && (DECL_FUNCTION_MEMBER_P (decl)
4131 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4132 : DECL_FRIEND_CONTEXT (decl)
4133 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4134 : false)
4135 /* And, if it was a member function, it really was defined in
4136 the scope of the class. */
4137 && (!DECL_FUNCTION_MEMBER_P (decl)
4138 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4139 /* We already checked these parameters when the template was
4140 declared, so there's no need to do it again now. This function
4141 was defined in class scope, but we're processing it's body now
4142 that the class is complete. */
4143 return true;
4145 /* Core issue 226 (C++0x only): the following only applies to class
4146 templates. */
4147 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4149 /* [temp.param]
4151 If a template-parameter has a default template-argument, all
4152 subsequent template-parameters shall have a default
4153 template-argument supplied. */
4154 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4156 tree inner_parms = TREE_VALUE (parm_level);
4157 int ntparms = TREE_VEC_LENGTH (inner_parms);
4158 int seen_def_arg_p = 0;
4159 int i;
4161 for (i = 0; i < ntparms; ++i)
4163 tree parm = TREE_VEC_ELT (inner_parms, i);
4165 if (parm == error_mark_node)
4166 continue;
4168 if (TREE_PURPOSE (parm))
4169 seen_def_arg_p = 1;
4170 else if (seen_def_arg_p
4171 && !template_parameter_pack_p (TREE_VALUE (parm)))
4173 error ("no default argument for %qD", TREE_VALUE (parm));
4174 /* For better subsequent error-recovery, we indicate that
4175 there should have been a default argument. */
4176 TREE_PURPOSE (parm) = error_mark_node;
4177 no_errors = false;
4179 else if (is_primary
4180 && !is_partial
4181 && !is_friend_decl
4182 /* Don't complain about an enclosing partial
4183 specialization. */
4184 && parm_level == parms
4185 && TREE_CODE (decl) == TYPE_DECL
4186 && i < ntparms - 1
4187 && template_parameter_pack_p (TREE_VALUE (parm)))
4189 /* A primary class template can only have one
4190 parameter pack, at the end of the template
4191 parameter list. */
4193 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4194 error ("parameter pack %qE must be at the end of the"
4195 " template parameter list", TREE_VALUE (parm));
4196 else
4197 error ("parameter pack %qT must be at the end of the"
4198 " template parameter list",
4199 TREE_TYPE (TREE_VALUE (parm)));
4201 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4202 = error_mark_node;
4203 no_errors = false;
4209 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4210 || is_partial
4211 || !is_primary
4212 || is_friend_decl)
4213 /* For an ordinary class template, default template arguments are
4214 allowed at the innermost level, e.g.:
4215 template <class T = int>
4216 struct S {};
4217 but, in a partial specialization, they're not allowed even
4218 there, as we have in [temp.class.spec]:
4220 The template parameter list of a specialization shall not
4221 contain default template argument values.
4223 So, for a partial specialization, or for a function template
4224 (in C++98/C++03), we look at all of them. */
4226 else
4227 /* But, for a primary class template that is not a partial
4228 specialization we look at all template parameters except the
4229 innermost ones. */
4230 parms = TREE_CHAIN (parms);
4232 /* Figure out what error message to issue. */
4233 if (is_friend_decl == 2)
4234 msg = G_("default template arguments may not be used in function template "
4235 "friend re-declaration");
4236 else if (is_friend_decl)
4237 msg = G_("default template arguments may not be used in function template "
4238 "friend declarations");
4239 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4240 msg = G_("default template arguments may not be used in function templates "
4241 "without -std=c++0x or -std=gnu++0x");
4242 else if (is_partial)
4243 msg = G_("default template arguments may not be used in "
4244 "partial specializations");
4245 else
4246 msg = G_("default argument for template parameter for class enclosing %qD");
4248 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4249 /* If we're inside a class definition, there's no need to
4250 examine the parameters to the class itself. On the one
4251 hand, they will be checked when the class is defined, and,
4252 on the other, default arguments are valid in things like:
4253 template <class T = double>
4254 struct S { template <class U> void f(U); };
4255 Here the default argument for `S' has no bearing on the
4256 declaration of `f'. */
4257 last_level_to_check = template_class_depth (current_class_type) + 1;
4258 else
4259 /* Check everything. */
4260 last_level_to_check = 0;
4262 for (parm_level = parms;
4263 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4264 parm_level = TREE_CHAIN (parm_level))
4266 tree inner_parms = TREE_VALUE (parm_level);
4267 int i;
4268 int ntparms;
4270 ntparms = TREE_VEC_LENGTH (inner_parms);
4271 for (i = 0; i < ntparms; ++i)
4273 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4274 continue;
4276 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4278 if (msg)
4280 no_errors = false;
4281 if (is_friend_decl == 2)
4282 return no_errors;
4284 error (msg, decl);
4285 msg = 0;
4288 /* Clear out the default argument so that we are not
4289 confused later. */
4290 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4294 /* At this point, if we're still interested in issuing messages,
4295 they must apply to classes surrounding the object declared. */
4296 if (msg)
4297 msg = G_("default argument for template parameter for class "
4298 "enclosing %qD");
4301 return no_errors;
4304 /* Worker for push_template_decl_real, called via
4305 for_each_template_parm. DATA is really an int, indicating the
4306 level of the parameters we are interested in. If T is a template
4307 parameter of that level, return nonzero. */
4309 static int
4310 template_parm_this_level_p (tree t, void* data)
4312 int this_level = *(int *)data;
4313 int level;
4315 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4316 level = TEMPLATE_PARM_LEVEL (t);
4317 else
4318 level = TEMPLATE_TYPE_LEVEL (t);
4319 return level == this_level;
4322 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4323 parameters given by current_template_args, or reuses a
4324 previously existing one, if appropriate. Returns the DECL, or an
4325 equivalent one, if it is replaced via a call to duplicate_decls.
4327 If IS_FRIEND is true, DECL is a friend declaration. */
4329 tree
4330 push_template_decl_real (tree decl, bool is_friend)
4332 tree tmpl;
4333 tree args;
4334 tree info;
4335 tree ctx;
4336 int primary;
4337 int is_partial;
4338 int new_template_p = 0;
4339 /* True if the template is a member template, in the sense of
4340 [temp.mem]. */
4341 bool member_template_p = false;
4343 if (decl == error_mark_node || !current_template_parms)
4344 return error_mark_node;
4346 /* See if this is a partial specialization. */
4347 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4348 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4349 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4351 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4352 is_friend = true;
4354 if (is_friend)
4355 /* For a friend, we want the context of the friend function, not
4356 the type of which it is a friend. */
4357 ctx = CP_DECL_CONTEXT (decl);
4358 else if (CP_DECL_CONTEXT (decl)
4359 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4360 /* In the case of a virtual function, we want the class in which
4361 it is defined. */
4362 ctx = CP_DECL_CONTEXT (decl);
4363 else
4364 /* Otherwise, if we're currently defining some class, the DECL
4365 is assumed to be a member of the class. */
4366 ctx = current_scope ();
4368 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4369 ctx = NULL_TREE;
4371 if (!DECL_CONTEXT (decl))
4372 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4374 /* See if this is a primary template. */
4375 if (is_friend && ctx)
4376 /* A friend template that specifies a class context, i.e.
4377 template <typename T> friend void A<T>::f();
4378 is not primary. */
4379 primary = 0;
4380 else
4381 primary = template_parm_scope_p ();
4383 if (primary)
4385 if (DECL_CLASS_SCOPE_P (decl))
4386 member_template_p = true;
4387 if (TREE_CODE (decl) == TYPE_DECL
4388 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4390 error ("template class without a name");
4391 return error_mark_node;
4393 else if (TREE_CODE (decl) == FUNCTION_DECL)
4395 if (DECL_DESTRUCTOR_P (decl))
4397 /* [temp.mem]
4399 A destructor shall not be a member template. */
4400 error ("destructor %qD declared as member template", decl);
4401 return error_mark_node;
4403 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4404 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4405 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4406 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4407 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4408 == void_list_node)))
4410 /* [basic.stc.dynamic.allocation]
4412 An allocation function can be a function
4413 template. ... Template allocation functions shall
4414 have two or more parameters. */
4415 error ("invalid template declaration of %qD", decl);
4416 return error_mark_node;
4419 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4420 && CLASS_TYPE_P (TREE_TYPE (decl)))
4421 /* OK */;
4422 else
4424 error ("template declaration of %q#D", decl);
4425 return error_mark_node;
4429 /* Check to see that the rules regarding the use of default
4430 arguments are not being violated. */
4431 check_default_tmpl_args (decl, current_template_parms,
4432 primary, is_partial, /*is_friend_decl=*/0);
4434 /* Ensure that there are no parameter packs in the type of this
4435 declaration that have not been expanded. */
4436 if (TREE_CODE (decl) == FUNCTION_DECL)
4438 /* Check each of the arguments individually to see if there are
4439 any bare parameter packs. */
4440 tree type = TREE_TYPE (decl);
4441 tree arg = DECL_ARGUMENTS (decl);
4442 tree argtype = TYPE_ARG_TYPES (type);
4444 while (arg && argtype)
4446 if (!FUNCTION_PARAMETER_PACK_P (arg)
4447 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4449 /* This is a PARM_DECL that contains unexpanded parameter
4450 packs. We have already complained about this in the
4451 check_for_bare_parameter_packs call, so just replace
4452 these types with ERROR_MARK_NODE. */
4453 TREE_TYPE (arg) = error_mark_node;
4454 TREE_VALUE (argtype) = error_mark_node;
4457 arg = DECL_CHAIN (arg);
4458 argtype = TREE_CHAIN (argtype);
4461 /* Check for bare parameter packs in the return type and the
4462 exception specifiers. */
4463 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4464 /* Errors were already issued, set return type to int
4465 as the frontend doesn't expect error_mark_node as
4466 the return type. */
4467 TREE_TYPE (type) = integer_type_node;
4468 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4469 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4471 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4473 TREE_TYPE (decl) = error_mark_node;
4474 return error_mark_node;
4477 if (is_partial)
4478 return process_partial_specialization (decl);
4480 args = current_template_args ();
4482 if (!ctx
4483 || TREE_CODE (ctx) == FUNCTION_DECL
4484 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4485 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4487 if (DECL_LANG_SPECIFIC (decl)
4488 && DECL_TEMPLATE_INFO (decl)
4489 && DECL_TI_TEMPLATE (decl))
4490 tmpl = DECL_TI_TEMPLATE (decl);
4491 /* If DECL is a TYPE_DECL for a class-template, then there won't
4492 be DECL_LANG_SPECIFIC. The information equivalent to
4493 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4494 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4495 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4496 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4498 /* Since a template declaration already existed for this
4499 class-type, we must be redeclaring it here. Make sure
4500 that the redeclaration is valid. */
4501 redeclare_class_template (TREE_TYPE (decl),
4502 current_template_parms);
4503 /* We don't need to create a new TEMPLATE_DECL; just use the
4504 one we already had. */
4505 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4507 else
4509 tmpl = build_template_decl (decl, current_template_parms,
4510 member_template_p);
4511 new_template_p = 1;
4513 if (DECL_LANG_SPECIFIC (decl)
4514 && DECL_TEMPLATE_SPECIALIZATION (decl))
4516 /* A specialization of a member template of a template
4517 class. */
4518 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4519 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4520 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4524 else
4526 tree a, t, current, parms;
4527 int i;
4528 tree tinfo = get_template_info (decl);
4530 if (!tinfo)
4532 error ("template definition of non-template %q#D", decl);
4533 return error_mark_node;
4536 tmpl = TI_TEMPLATE (tinfo);
4538 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4539 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4540 && DECL_TEMPLATE_SPECIALIZATION (decl)
4541 && DECL_MEMBER_TEMPLATE_P (tmpl))
4543 tree new_tmpl;
4545 /* The declaration is a specialization of a member
4546 template, declared outside the class. Therefore, the
4547 innermost template arguments will be NULL, so we
4548 replace them with the arguments determined by the
4549 earlier call to check_explicit_specialization. */
4550 args = DECL_TI_ARGS (decl);
4552 new_tmpl
4553 = build_template_decl (decl, current_template_parms,
4554 member_template_p);
4555 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4556 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4557 DECL_TI_TEMPLATE (decl) = new_tmpl;
4558 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4559 DECL_TEMPLATE_INFO (new_tmpl)
4560 = build_template_info (tmpl, args);
4562 register_specialization (new_tmpl,
4563 most_general_template (tmpl),
4564 args,
4565 is_friend, 0);
4566 return decl;
4569 /* Make sure the template headers we got make sense. */
4571 parms = DECL_TEMPLATE_PARMS (tmpl);
4572 i = TMPL_PARMS_DEPTH (parms);
4573 if (TMPL_ARGS_DEPTH (args) != i)
4575 error ("expected %d levels of template parms for %q#D, got %d",
4576 i, decl, TMPL_ARGS_DEPTH (args));
4578 else
4579 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4581 a = TMPL_ARGS_LEVEL (args, i);
4582 t = INNERMOST_TEMPLATE_PARMS (parms);
4584 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4586 if (current == decl)
4587 error ("got %d template parameters for %q#D",
4588 TREE_VEC_LENGTH (a), decl);
4589 else
4590 error ("got %d template parameters for %q#T",
4591 TREE_VEC_LENGTH (a), current);
4592 error (" but %d required", TREE_VEC_LENGTH (t));
4593 return error_mark_node;
4596 if (current == decl)
4597 current = ctx;
4598 else if (current == NULL_TREE)
4599 /* Can happen in erroneous input. */
4600 break;
4601 else
4602 current = (TYPE_P (current)
4603 ? TYPE_CONTEXT (current)
4604 : DECL_CONTEXT (current));
4607 /* Check that the parms are used in the appropriate qualifying scopes
4608 in the declarator. */
4609 if (!comp_template_args
4610 (TI_ARGS (tinfo),
4611 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4613 error ("\
4614 template arguments to %qD do not match original template %qD",
4615 decl, DECL_TEMPLATE_RESULT (tmpl));
4616 if (!uses_template_parms (TI_ARGS (tinfo)))
4617 inform (input_location, "use template<> for an explicit specialization");
4618 /* Avoid crash in import_export_decl. */
4619 DECL_INTERFACE_KNOWN (decl) = 1;
4620 return error_mark_node;
4624 DECL_TEMPLATE_RESULT (tmpl) = decl;
4625 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4627 /* Push template declarations for global functions and types. Note
4628 that we do not try to push a global template friend declared in a
4629 template class; such a thing may well depend on the template
4630 parameters of the class. */
4631 if (new_template_p && !ctx
4632 && !(is_friend && template_class_depth (current_class_type) > 0))
4634 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4635 if (tmpl == error_mark_node)
4636 return error_mark_node;
4638 /* Hide template friend classes that haven't been declared yet. */
4639 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4641 DECL_ANTICIPATED (tmpl) = 1;
4642 DECL_FRIEND_P (tmpl) = 1;
4646 if (primary)
4648 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4649 int i;
4651 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4652 if (DECL_CONV_FN_P (tmpl))
4654 int depth = TMPL_PARMS_DEPTH (parms);
4656 /* It is a conversion operator. See if the type converted to
4657 depends on innermost template operands. */
4659 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4660 depth))
4661 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4664 /* Give template template parms a DECL_CONTEXT of the template
4665 for which they are a parameter. */
4666 parms = INNERMOST_TEMPLATE_PARMS (parms);
4667 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4669 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4670 if (TREE_CODE (parm) == TEMPLATE_DECL)
4671 DECL_CONTEXT (parm) = tmpl;
4675 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4676 back to its most general template. If TMPL is a specialization,
4677 ARGS may only have the innermost set of arguments. Add the missing
4678 argument levels if necessary. */
4679 if (DECL_TEMPLATE_INFO (tmpl))
4680 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4682 info = build_template_info (tmpl, args);
4684 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4685 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4686 else if (DECL_LANG_SPECIFIC (decl))
4687 DECL_TEMPLATE_INFO (decl) = info;
4689 return DECL_TEMPLATE_RESULT (tmpl);
4692 tree
4693 push_template_decl (tree decl)
4695 return push_template_decl_real (decl, false);
4698 /* Called when a class template TYPE is redeclared with the indicated
4699 template PARMS, e.g.:
4701 template <class T> struct S;
4702 template <class T> struct S {}; */
4704 bool
4705 redeclare_class_template (tree type, tree parms)
4707 tree tmpl;
4708 tree tmpl_parms;
4709 int i;
4711 if (!TYPE_TEMPLATE_INFO (type))
4713 error ("%qT is not a template type", type);
4714 return false;
4717 tmpl = TYPE_TI_TEMPLATE (type);
4718 if (!PRIMARY_TEMPLATE_P (tmpl))
4719 /* The type is nested in some template class. Nothing to worry
4720 about here; there are no new template parameters for the nested
4721 type. */
4722 return true;
4724 if (!parms)
4726 error ("template specifiers not specified in declaration of %qD",
4727 tmpl);
4728 return false;
4731 parms = INNERMOST_TEMPLATE_PARMS (parms);
4732 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4734 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4736 error_n (input_location, TREE_VEC_LENGTH (parms),
4737 "redeclared with %d template parameter",
4738 "redeclared with %d template parameters",
4739 TREE_VEC_LENGTH (parms));
4740 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4741 "previous declaration %q+D used %d template parameter",
4742 "previous declaration %q+D used %d template parameters",
4743 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4744 return false;
4747 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4749 tree tmpl_parm;
4750 tree parm;
4751 tree tmpl_default;
4752 tree parm_default;
4754 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4755 || TREE_VEC_ELT (parms, i) == error_mark_node)
4756 continue;
4758 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4759 if (tmpl_parm == error_mark_node)
4760 return false;
4762 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4763 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4764 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4766 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4767 TEMPLATE_DECL. */
4768 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4769 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4770 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4771 || (TREE_CODE (tmpl_parm) != PARM_DECL
4772 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4773 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4774 || (TREE_CODE (tmpl_parm) == PARM_DECL
4775 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4776 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4778 error ("template parameter %q+#D", tmpl_parm);
4779 error ("redeclared here as %q#D", parm);
4780 return false;
4783 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4785 /* We have in [temp.param]:
4787 A template-parameter may not be given default arguments
4788 by two different declarations in the same scope. */
4789 error_at (input_location, "redefinition of default argument for %q#D", parm);
4790 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4791 "original definition appeared here");
4792 return false;
4795 if (parm_default != NULL_TREE)
4796 /* Update the previous template parameters (which are the ones
4797 that will really count) with the new default value. */
4798 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4799 else if (tmpl_default != NULL_TREE)
4800 /* Update the new parameters, too; they'll be used as the
4801 parameters for any members. */
4802 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4805 return true;
4808 /* Simplify EXPR if it is a non-dependent expression. Returns the
4809 (possibly simplified) expression. */
4811 static tree
4812 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
4814 if (expr == NULL_TREE)
4815 return NULL_TREE;
4817 /* If we're in a template, but EXPR isn't value dependent, simplify
4818 it. We're supposed to treat:
4820 template <typename T> void f(T[1 + 1]);
4821 template <typename T> void f(T[2]);
4823 as two declarations of the same function, for example. */
4824 if (processing_template_decl
4825 && !type_dependent_expression_p (expr)
4826 && !value_dependent_expression_p (expr))
4828 HOST_WIDE_INT saved_processing_template_decl;
4830 saved_processing_template_decl = processing_template_decl;
4831 processing_template_decl = 0;
4832 expr = tsubst_copy_and_build (expr,
4833 /*args=*/NULL_TREE,
4834 complain,
4835 /*in_decl=*/NULL_TREE,
4836 /*function_p=*/false,
4837 /*integral_constant_expression_p=*/true);
4838 processing_template_decl = saved_processing_template_decl;
4840 return expr;
4843 tree
4844 fold_non_dependent_expr (tree expr)
4846 return fold_non_dependent_expr_sfinae (expr, tf_error);
4849 /* EXPR is an expression which is used in a constant-expression context.
4850 For instance, it could be a VAR_DECL with a constant initializer.
4851 Extract the innermost constant expression.
4853 This is basically a more powerful version of
4854 integral_constant_value, which can be used also in templates where
4855 initializers can maintain a syntactic rather than semantic form
4856 (even if they are non-dependent, for access-checking purposes). */
4858 static tree
4859 fold_decl_constant_value (tree expr)
4861 tree const_expr = expr;
4864 expr = fold_non_dependent_expr (const_expr);
4865 const_expr = integral_constant_value (expr);
4867 while (expr != const_expr);
4869 return expr;
4872 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4873 must be a function or a pointer-to-function type, as specified
4874 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4875 and check that the resulting function has external linkage. */
4877 static tree
4878 convert_nontype_argument_function (tree type, tree expr)
4880 tree fns = expr;
4881 tree fn, fn_no_ptr;
4883 fn = instantiate_type (type, fns, tf_none);
4884 if (fn == error_mark_node)
4885 return error_mark_node;
4887 fn_no_ptr = fn;
4888 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4889 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4890 if (TREE_CODE (fn_no_ptr) == BASELINK)
4891 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4893 /* [temp.arg.nontype]/1
4895 A template-argument for a non-type, non-template template-parameter
4896 shall be one of:
4897 [...]
4898 -- the address of an object or function with external linkage. */
4899 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4901 error ("%qE is not a valid template argument for type %qT "
4902 "because function %qD has not external linkage",
4903 expr, type, fn_no_ptr);
4904 return NULL_TREE;
4907 return fn;
4910 /* Subroutine of convert_nontype_argument.
4911 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4912 Emit an error otherwise. */
4914 static bool
4915 check_valid_ptrmem_cst_expr (tree type, tree expr)
4917 STRIP_NOPS (expr);
4918 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4919 return true;
4920 error ("%qE is not a valid template argument for type %qT",
4921 expr, type);
4922 error ("it must be a pointer-to-member of the form `&X::Y'");
4923 return false;
4926 /* Returns TRUE iff the address of OP is value-dependent.
4928 14.6.2.4 [temp.dep.temp]:
4929 A non-integral non-type template-argument is dependent if its type is
4930 dependent or it has either of the following forms
4931 qualified-id
4932 & qualified-id
4933 and contains a nested-name-specifier which specifies a class-name that
4934 names a dependent type.
4936 We generalize this to just say that the address of a member of a
4937 dependent class is value-dependent; the above doesn't cover the
4938 address of a static data member named with an unqualified-id. */
4940 static bool
4941 has_value_dependent_address (tree op)
4943 /* We could use get_inner_reference here, but there's no need;
4944 this is only relevant for template non-type arguments, which
4945 can only be expressed as &id-expression. */
4946 if (DECL_P (op))
4948 tree ctx = CP_DECL_CONTEXT (op);
4949 if (TYPE_P (ctx) && dependent_type_p (ctx))
4950 return true;
4953 return false;
4956 /* Attempt to convert the non-type template parameter EXPR to the
4957 indicated TYPE. If the conversion is successful, return the
4958 converted value. If the conversion is unsuccessful, return
4959 NULL_TREE if we issued an error message, or error_mark_node if we
4960 did not. We issue error messages for out-and-out bad template
4961 parameters, but not simply because the conversion failed, since we
4962 might be just trying to do argument deduction. Both TYPE and EXPR
4963 must be non-dependent.
4965 The conversion follows the special rules described in
4966 [temp.arg.nontype], and it is much more strict than an implicit
4967 conversion.
4969 This function is called twice for each template argument (see
4970 lookup_template_class for a more accurate description of this
4971 problem). This means that we need to handle expressions which
4972 are not valid in a C++ source, but can be created from the
4973 first call (for instance, casts to perform conversions). These
4974 hacks can go away after we fix the double coercion problem. */
4976 static tree
4977 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
4979 tree expr_type;
4981 /* Detect immediately string literals as invalid non-type argument.
4982 This special-case is not needed for correctness (we would easily
4983 catch this later), but only to provide better diagnostic for this
4984 common user mistake. As suggested by DR 100, we do not mention
4985 linkage issues in the diagnostic as this is not the point. */
4986 /* FIXME we're making this OK. */
4987 if (TREE_CODE (expr) == STRING_CST)
4989 if (complain & tf_error)
4990 error ("%qE is not a valid template argument for type %qT "
4991 "because string literals can never be used in this context",
4992 expr, type);
4993 return NULL_TREE;
4996 /* Add the ADDR_EXPR now for the benefit of
4997 value_dependent_expression_p. */
4998 if (TYPE_PTROBV_P (type))
4999 expr = decay_conversion (expr);
5001 /* If we are in a template, EXPR may be non-dependent, but still
5002 have a syntactic, rather than semantic, form. For example, EXPR
5003 might be a SCOPE_REF, rather than the VAR_DECL to which the
5004 SCOPE_REF refers. Preserving the qualifying scope is necessary
5005 so that access checking can be performed when the template is
5006 instantiated -- but here we need the resolved form so that we can
5007 convert the argument. */
5008 if (TYPE_REF_OBJ_P (type)
5009 && has_value_dependent_address (expr))
5010 /* If we want the address and it's value-dependent, don't fold. */;
5011 else if (!type_unknown_p (expr))
5012 expr = fold_non_dependent_expr_sfinae (expr, complain);
5013 if (error_operand_p (expr))
5014 return error_mark_node;
5015 expr_type = TREE_TYPE (expr);
5016 if (TREE_CODE (type) == REFERENCE_TYPE)
5017 expr = mark_lvalue_use (expr);
5018 else
5019 expr = mark_rvalue_use (expr);
5021 /* HACK: Due to double coercion, we can get a
5022 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5023 which is the tree that we built on the first call (see
5024 below when coercing to reference to object or to reference to
5025 function). We just strip everything and get to the arg.
5026 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5027 for examples. */
5028 if (TREE_CODE (expr) == NOP_EXPR)
5030 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5032 /* ??? Maybe we could use convert_from_reference here, but we
5033 would need to relax its constraints because the NOP_EXPR
5034 could actually change the type to something more cv-qualified,
5035 and this is not folded by convert_from_reference. */
5036 tree addr = TREE_OPERAND (expr, 0);
5037 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5038 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5039 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5040 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5041 (TREE_TYPE (expr_type),
5042 TREE_TYPE (TREE_TYPE (addr))));
5044 expr = TREE_OPERAND (addr, 0);
5045 expr_type = TREE_TYPE (expr);
5048 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5049 parameter is a pointer to object, through decay and
5050 qualification conversion. Let's strip everything. */
5051 else if (TYPE_PTROBV_P (type))
5053 STRIP_NOPS (expr);
5054 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5055 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5056 /* Skip the ADDR_EXPR only if it is part of the decay for
5057 an array. Otherwise, it is part of the original argument
5058 in the source code. */
5059 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5060 expr = TREE_OPERAND (expr, 0);
5061 expr_type = TREE_TYPE (expr);
5065 /* [temp.arg.nontype]/5, bullet 1
5067 For a non-type template-parameter of integral or enumeration type,
5068 integral promotions (_conv.prom_) and integral conversions
5069 (_conv.integral_) are applied. */
5070 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5072 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5073 t = fold_decl_constant_value (t);
5074 if (t != error_mark_node)
5075 expr = t;
5077 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5078 return error_mark_node;
5080 /* Conversion was allowed: fold it to a bare integer constant. */
5081 expr = fold (expr);
5083 /* Notice that there are constant expressions like '4 % 0' which
5084 do not fold into integer constants. */
5085 if (TREE_CODE (expr) != INTEGER_CST)
5087 if (complain & tf_error)
5088 error ("%qE is not a valid template argument for type %qT "
5089 "because it is a non-constant expression", expr, type);
5090 return NULL_TREE;
5093 /* [temp.arg.nontype]/5, bullet 2
5095 For a non-type template-parameter of type pointer to object,
5096 qualification conversions (_conv.qual_) and the array-to-pointer
5097 conversion (_conv.array_) are applied. */
5098 else if (TYPE_PTROBV_P (type))
5100 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5102 A template-argument for a non-type, non-template template-parameter
5103 shall be one of: [...]
5105 -- the name of a non-type template-parameter;
5106 -- the address of an object or function with external linkage, [...]
5107 expressed as "& id-expression" where the & is optional if the name
5108 refers to a function or array, or if the corresponding
5109 template-parameter is a reference.
5111 Here, we do not care about functions, as they are invalid anyway
5112 for a parameter of type pointer-to-object. */
5114 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5115 /* Non-type template parameters are OK. */
5117 else if (TREE_CODE (expr) != ADDR_EXPR
5118 && TREE_CODE (expr_type) != ARRAY_TYPE)
5120 if (TREE_CODE (expr) == VAR_DECL)
5122 error ("%qD is not a valid template argument "
5123 "because %qD is a variable, not the address of "
5124 "a variable",
5125 expr, expr);
5126 return NULL_TREE;
5128 /* Other values, like integer constants, might be valid
5129 non-type arguments of some other type. */
5130 return error_mark_node;
5132 else
5134 tree decl;
5136 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5137 ? TREE_OPERAND (expr, 0) : expr);
5138 if (TREE_CODE (decl) != VAR_DECL)
5140 error ("%qE is not a valid template argument of type %qT "
5141 "because %qE is not a variable",
5142 expr, type, decl);
5143 return NULL_TREE;
5145 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5147 error ("%qE is not a valid template argument of type %qT "
5148 "because %qD does not have external linkage",
5149 expr, type, decl);
5150 return NULL_TREE;
5154 expr = decay_conversion (expr);
5155 if (expr == error_mark_node)
5156 return error_mark_node;
5158 expr = perform_qualification_conversions (type, expr);
5159 if (expr == error_mark_node)
5160 return error_mark_node;
5162 /* [temp.arg.nontype]/5, bullet 3
5164 For a non-type template-parameter of type reference to object, no
5165 conversions apply. The type referred to by the reference may be more
5166 cv-qualified than the (otherwise identical) type of the
5167 template-argument. The template-parameter is bound directly to the
5168 template-argument, which must be an lvalue. */
5169 else if (TYPE_REF_OBJ_P (type))
5171 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5172 expr_type))
5173 return error_mark_node;
5175 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5177 error ("%qE is not a valid template argument for type %qT "
5178 "because of conflicts in cv-qualification", expr, type);
5179 return NULL_TREE;
5182 if (!real_lvalue_p (expr))
5184 error ("%qE is not a valid template argument for type %qT "
5185 "because it is not an lvalue", expr, type);
5186 return NULL_TREE;
5189 /* [temp.arg.nontype]/1
5191 A template-argument for a non-type, non-template template-parameter
5192 shall be one of: [...]
5194 -- the address of an object or function with external linkage. */
5195 if (TREE_CODE (expr) == INDIRECT_REF
5196 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5198 expr = TREE_OPERAND (expr, 0);
5199 if (DECL_P (expr))
5201 error ("%q#D is not a valid template argument for type %qT "
5202 "because a reference variable does not have a constant "
5203 "address", expr, type);
5204 return NULL_TREE;
5208 if (!DECL_P (expr))
5210 error ("%qE is not a valid template argument for type %qT "
5211 "because it is not an object with external linkage",
5212 expr, type);
5213 return NULL_TREE;
5216 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5218 error ("%qE is not a valid template argument for type %qT "
5219 "because object %qD has not external linkage",
5220 expr, type, expr);
5221 return NULL_TREE;
5224 expr = build_nop (type, build_address (expr));
5226 /* [temp.arg.nontype]/5, bullet 4
5228 For a non-type template-parameter of type pointer to function, only
5229 the function-to-pointer conversion (_conv.func_) is applied. If the
5230 template-argument represents a set of overloaded functions (or a
5231 pointer to such), the matching function is selected from the set
5232 (_over.over_). */
5233 else if (TYPE_PTRFN_P (type))
5235 /* If the argument is a template-id, we might not have enough
5236 context information to decay the pointer. */
5237 if (!type_unknown_p (expr_type))
5239 expr = decay_conversion (expr);
5240 if (expr == error_mark_node)
5241 return error_mark_node;
5244 expr = convert_nontype_argument_function (type, expr);
5245 if (!expr || expr == error_mark_node)
5246 return expr;
5248 if (TREE_CODE (expr) != ADDR_EXPR)
5250 error ("%qE is not a valid template argument for type %qT", expr, type);
5251 error ("it must be the address of a function with external linkage");
5252 return NULL_TREE;
5255 /* [temp.arg.nontype]/5, bullet 5
5257 For a non-type template-parameter of type reference to function, no
5258 conversions apply. If the template-argument represents a set of
5259 overloaded functions, the matching function is selected from the set
5260 (_over.over_). */
5261 else if (TYPE_REFFN_P (type))
5263 if (TREE_CODE (expr) == ADDR_EXPR)
5265 error ("%qE is not a valid template argument for type %qT "
5266 "because it is a pointer", expr, type);
5267 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5268 return NULL_TREE;
5271 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5272 if (!expr || expr == error_mark_node)
5273 return expr;
5275 expr = build_nop (type, build_address (expr));
5277 /* [temp.arg.nontype]/5, bullet 6
5279 For a non-type template-parameter of type pointer to member function,
5280 no conversions apply. If the template-argument represents a set of
5281 overloaded member functions, the matching member function is selected
5282 from the set (_over.over_). */
5283 else if (TYPE_PTRMEMFUNC_P (type))
5285 expr = instantiate_type (type, expr, tf_none);
5286 if (expr == error_mark_node)
5287 return error_mark_node;
5289 /* [temp.arg.nontype] bullet 1 says the pointer to member
5290 expression must be a pointer-to-member constant. */
5291 if (!check_valid_ptrmem_cst_expr (type, expr))
5292 return error_mark_node;
5294 /* There is no way to disable standard conversions in
5295 resolve_address_of_overloaded_function (called by
5296 instantiate_type). It is possible that the call succeeded by
5297 converting &B::I to &D::I (where B is a base of D), so we need
5298 to reject this conversion here.
5300 Actually, even if there was a way to disable standard conversions,
5301 it would still be better to reject them here so that we can
5302 provide a superior diagnostic. */
5303 if (!same_type_p (TREE_TYPE (expr), type))
5305 error ("%qE is not a valid template argument for type %qT "
5306 "because it is of type %qT", expr, type,
5307 TREE_TYPE (expr));
5308 /* If we are just one standard conversion off, explain. */
5309 if (can_convert (type, TREE_TYPE (expr)))
5310 inform (input_location,
5311 "standard conversions are not allowed in this context");
5312 return NULL_TREE;
5315 /* [temp.arg.nontype]/5, bullet 7
5317 For a non-type template-parameter of type pointer to data member,
5318 qualification conversions (_conv.qual_) are applied. */
5319 else if (TYPE_PTRMEM_P (type))
5321 /* [temp.arg.nontype] bullet 1 says the pointer to member
5322 expression must be a pointer-to-member constant. */
5323 if (!check_valid_ptrmem_cst_expr (type, expr))
5324 return error_mark_node;
5326 expr = perform_qualification_conversions (type, expr);
5327 if (expr == error_mark_node)
5328 return expr;
5330 /* A template non-type parameter must be one of the above. */
5331 else
5332 gcc_unreachable ();
5334 /* Sanity check: did we actually convert the argument to the
5335 right type? */
5336 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5337 (type, TREE_TYPE (expr)));
5338 return expr;
5341 /* Subroutine of coerce_template_template_parms, which returns 1 if
5342 PARM_PARM and ARG_PARM match using the rule for the template
5343 parameters of template template parameters. Both PARM and ARG are
5344 template parameters; the rest of the arguments are the same as for
5345 coerce_template_template_parms.
5347 static int
5348 coerce_template_template_parm (tree parm,
5349 tree arg,
5350 tsubst_flags_t complain,
5351 tree in_decl,
5352 tree outer_args)
5354 if (arg == NULL_TREE || arg == error_mark_node
5355 || parm == NULL_TREE || parm == error_mark_node)
5356 return 0;
5358 if (TREE_CODE (arg) != TREE_CODE (parm))
5359 return 0;
5361 switch (TREE_CODE (parm))
5363 case TEMPLATE_DECL:
5364 /* We encounter instantiations of templates like
5365 template <template <template <class> class> class TT>
5366 class C; */
5368 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5369 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5371 if (!coerce_template_template_parms
5372 (parmparm, argparm, complain, in_decl, outer_args))
5373 return 0;
5375 /* Fall through. */
5377 case TYPE_DECL:
5378 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5379 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5380 /* Argument is a parameter pack but parameter is not. */
5381 return 0;
5382 break;
5384 case PARM_DECL:
5385 /* The tsubst call is used to handle cases such as
5387 template <int> class C {};
5388 template <class T, template <T> class TT> class D {};
5389 D<int, C> d;
5391 i.e. the parameter list of TT depends on earlier parameters. */
5392 if (!uses_template_parms (TREE_TYPE (arg))
5393 && !same_type_p
5394 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5395 TREE_TYPE (arg)))
5396 return 0;
5398 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5399 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5400 /* Argument is a parameter pack but parameter is not. */
5401 return 0;
5403 break;
5405 default:
5406 gcc_unreachable ();
5409 return 1;
5413 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5414 template template parameters. Both PARM_PARMS and ARG_PARMS are
5415 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5416 or PARM_DECL.
5418 Consider the example:
5419 template <class T> class A;
5420 template<template <class U> class TT> class B;
5422 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5423 the parameters to A, and OUTER_ARGS contains A. */
5425 static int
5426 coerce_template_template_parms (tree parm_parms,
5427 tree arg_parms,
5428 tsubst_flags_t complain,
5429 tree in_decl,
5430 tree outer_args)
5432 int nparms, nargs, i;
5433 tree parm, arg;
5434 int variadic_p = 0;
5436 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5437 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5439 nparms = TREE_VEC_LENGTH (parm_parms);
5440 nargs = TREE_VEC_LENGTH (arg_parms);
5442 /* Determine whether we have a parameter pack at the end of the
5443 template template parameter's template parameter list. */
5444 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5446 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5448 if (parm == error_mark_node)
5449 return 0;
5451 switch (TREE_CODE (parm))
5453 case TEMPLATE_DECL:
5454 case TYPE_DECL:
5455 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5456 variadic_p = 1;
5457 break;
5459 case PARM_DECL:
5460 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5461 variadic_p = 1;
5462 break;
5464 default:
5465 gcc_unreachable ();
5469 if (nargs != nparms
5470 && !(variadic_p && nargs >= nparms - 1))
5471 return 0;
5473 /* Check all of the template parameters except the parameter pack at
5474 the end (if any). */
5475 for (i = 0; i < nparms - variadic_p; ++i)
5477 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5478 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5479 continue;
5481 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5482 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5484 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5485 outer_args))
5486 return 0;
5490 if (variadic_p)
5492 /* Check each of the template parameters in the template
5493 argument against the template parameter pack at the end of
5494 the template template parameter. */
5495 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5496 return 0;
5498 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5500 for (; i < nargs; ++i)
5502 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5503 continue;
5505 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5507 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5508 outer_args))
5509 return 0;
5513 return 1;
5516 /* Verifies that the deduced template arguments (in TARGS) for the
5517 template template parameters (in TPARMS) represent valid bindings,
5518 by comparing the template parameter list of each template argument
5519 to the template parameter list of its corresponding template
5520 template parameter, in accordance with DR150. This
5521 routine can only be called after all template arguments have been
5522 deduced. It will return TRUE if all of the template template
5523 parameter bindings are okay, FALSE otherwise. */
5524 bool
5525 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5527 int i, ntparms = TREE_VEC_LENGTH (tparms);
5528 bool ret = true;
5530 /* We're dealing with template parms in this process. */
5531 ++processing_template_decl;
5533 targs = INNERMOST_TEMPLATE_ARGS (targs);
5535 for (i = 0; i < ntparms; ++i)
5537 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5538 tree targ = TREE_VEC_ELT (targs, i);
5540 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5542 tree packed_args = NULL_TREE;
5543 int idx, len = 1;
5545 if (ARGUMENT_PACK_P (targ))
5547 /* Look inside the argument pack. */
5548 packed_args = ARGUMENT_PACK_ARGS (targ);
5549 len = TREE_VEC_LENGTH (packed_args);
5552 for (idx = 0; idx < len; ++idx)
5554 tree targ_parms = NULL_TREE;
5556 if (packed_args)
5557 /* Extract the next argument from the argument
5558 pack. */
5559 targ = TREE_VEC_ELT (packed_args, idx);
5561 if (PACK_EXPANSION_P (targ))
5562 /* Look at the pattern of the pack expansion. */
5563 targ = PACK_EXPANSION_PATTERN (targ);
5565 /* Extract the template parameters from the template
5566 argument. */
5567 if (TREE_CODE (targ) == TEMPLATE_DECL)
5568 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5569 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5570 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5572 /* Verify that we can coerce the template template
5573 parameters from the template argument to the template
5574 parameter. This requires an exact match. */
5575 if (targ_parms
5576 && !coerce_template_template_parms
5577 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5578 targ_parms,
5579 tf_none,
5580 tparm,
5581 targs))
5583 ret = false;
5584 goto out;
5590 out:
5592 --processing_template_decl;
5593 return ret;
5596 /* Convert the indicated template ARG as necessary to match the
5597 indicated template PARM. Returns the converted ARG, or
5598 error_mark_node if the conversion was unsuccessful. Error and
5599 warning messages are issued under control of COMPLAIN. This
5600 conversion is for the Ith parameter in the parameter list. ARGS is
5601 the full set of template arguments deduced so far. */
5603 static tree
5604 convert_template_argument (tree parm,
5605 tree arg,
5606 tree args,
5607 tsubst_flags_t complain,
5608 int i,
5609 tree in_decl)
5611 tree orig_arg;
5612 tree val;
5613 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5615 if (TREE_CODE (arg) == TREE_LIST
5616 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5618 /* The template argument was the name of some
5619 member function. That's usually
5620 invalid, but static members are OK. In any
5621 case, grab the underlying fields/functions
5622 and issue an error later if required. */
5623 orig_arg = TREE_VALUE (arg);
5624 TREE_TYPE (arg) = unknown_type_node;
5627 orig_arg = arg;
5629 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5630 requires_type = (TREE_CODE (parm) == TYPE_DECL
5631 || requires_tmpl_type);
5633 /* When determining whether an argument pack expansion is a template,
5634 look at the pattern. */
5635 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5636 arg = PACK_EXPANSION_PATTERN (arg);
5638 /* Deal with an injected-class-name used as a template template arg. */
5639 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5641 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5642 if (TREE_CODE (t) == TEMPLATE_DECL)
5644 if (complain & tf_warning_or_error)
5645 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5646 " used as template template argument", TYPE_NAME (arg));
5647 else if (flag_pedantic_errors)
5648 t = arg;
5650 arg = t;
5654 is_tmpl_type =
5655 ((TREE_CODE (arg) == TEMPLATE_DECL
5656 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5657 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5658 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5660 if (is_tmpl_type
5661 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5662 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5663 arg = TYPE_STUB_DECL (arg);
5665 is_type = TYPE_P (arg) || is_tmpl_type;
5667 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5668 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5670 permerror (input_location, "to refer to a type member of a template parameter, "
5671 "use %<typename %E%>", orig_arg);
5673 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5674 TREE_OPERAND (arg, 1),
5675 typename_type,
5676 complain & tf_error);
5677 arg = orig_arg;
5678 is_type = 1;
5680 if (is_type != requires_type)
5682 if (in_decl)
5684 if (complain & tf_error)
5686 error ("type/value mismatch at argument %d in template "
5687 "parameter list for %qD",
5688 i + 1, in_decl);
5689 if (is_type)
5690 error (" expected a constant of type %qT, got %qT",
5691 TREE_TYPE (parm),
5692 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5693 else if (requires_tmpl_type)
5694 error (" expected a class template, got %qE", orig_arg);
5695 else
5696 error (" expected a type, got %qE", orig_arg);
5699 return error_mark_node;
5701 if (is_tmpl_type ^ requires_tmpl_type)
5703 if (in_decl && (complain & tf_error))
5705 error ("type/value mismatch at argument %d in template "
5706 "parameter list for %qD",
5707 i + 1, in_decl);
5708 if (is_tmpl_type)
5709 error (" expected a type, got %qT", DECL_NAME (arg));
5710 else
5711 error (" expected a class template, got %qT", orig_arg);
5713 return error_mark_node;
5716 if (is_type)
5718 if (requires_tmpl_type)
5720 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5721 /* The number of argument required is not known yet.
5722 Just accept it for now. */
5723 val = TREE_TYPE (arg);
5724 else
5726 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5727 tree argparm;
5729 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5731 if (coerce_template_template_parms (parmparm, argparm,
5732 complain, in_decl,
5733 args))
5735 val = arg;
5737 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5738 TEMPLATE_DECL. */
5739 if (val != error_mark_node)
5741 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5742 val = TREE_TYPE (val);
5743 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5744 val = make_pack_expansion (val);
5747 else
5749 if (in_decl && (complain & tf_error))
5751 error ("type/value mismatch at argument %d in "
5752 "template parameter list for %qD",
5753 i + 1, in_decl);
5754 error (" expected a template of type %qD, got %qT",
5755 parm, orig_arg);
5758 val = error_mark_node;
5762 else
5763 val = orig_arg;
5764 /* We only form one instance of each template specialization.
5765 Therefore, if we use a non-canonical variant (i.e., a
5766 typedef), any future messages referring to the type will use
5767 the typedef, which is confusing if those future uses do not
5768 themselves also use the typedef. */
5769 if (TYPE_P (val))
5770 val = strip_typedefs (val);
5772 else
5774 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5776 if (invalid_nontype_parm_type_p (t, complain))
5777 return error_mark_node;
5779 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5781 if (same_type_p (t, TREE_TYPE (orig_arg)))
5782 val = orig_arg;
5783 else
5785 /* Not sure if this is reachable, but it doesn't hurt
5786 to be robust. */
5787 error ("type mismatch in nontype parameter pack");
5788 val = error_mark_node;
5791 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5792 /* We used to call digest_init here. However, digest_init
5793 will report errors, which we don't want when complain
5794 is zero. More importantly, digest_init will try too
5795 hard to convert things: for example, `0' should not be
5796 converted to pointer type at this point according to
5797 the standard. Accepting this is not merely an
5798 extension, since deciding whether or not these
5799 conversions can occur is part of determining which
5800 function template to call, or whether a given explicit
5801 argument specification is valid. */
5802 val = convert_nontype_argument (t, orig_arg, complain);
5803 else
5804 val = orig_arg;
5806 if (val == NULL_TREE)
5807 val = error_mark_node;
5808 else if (val == error_mark_node && (complain & tf_error))
5809 error ("could not convert template argument %qE to %qT", orig_arg, t);
5811 if (TREE_CODE (val) == SCOPE_REF)
5813 /* Strip typedefs from the SCOPE_REF. */
5814 tree type = strip_typedefs (TREE_TYPE (val));
5815 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5816 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5817 QUALIFIED_NAME_IS_TEMPLATE (val));
5821 return val;
5824 /* Coerces the remaining template arguments in INNER_ARGS (from
5825 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5826 Returns the coerced argument pack. PARM_IDX is the position of this
5827 parameter in the template parameter list. ARGS is the original
5828 template argument list. */
5829 static tree
5830 coerce_template_parameter_pack (tree parms,
5831 int parm_idx,
5832 tree args,
5833 tree inner_args,
5834 int arg_idx,
5835 tree new_args,
5836 int* lost,
5837 tree in_decl,
5838 tsubst_flags_t complain)
5840 tree parm = TREE_VEC_ELT (parms, parm_idx);
5841 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5842 tree packed_args;
5843 tree argument_pack;
5844 tree packed_types = NULL_TREE;
5846 if (arg_idx > nargs)
5847 arg_idx = nargs;
5849 packed_args = make_tree_vec (nargs - arg_idx);
5851 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5852 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5854 /* When the template parameter is a non-type template
5855 parameter pack whose type uses parameter packs, we need
5856 to look at each of the template arguments
5857 separately. Build a vector of the types for these
5858 non-type template parameters in PACKED_TYPES. */
5859 tree expansion
5860 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5861 packed_types = tsubst_pack_expansion (expansion, args,
5862 complain, in_decl);
5864 if (packed_types == error_mark_node)
5865 return error_mark_node;
5867 /* Check that we have the right number of arguments. */
5868 if (arg_idx < nargs
5869 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5870 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5872 int needed_parms
5873 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5874 error ("wrong number of template arguments (%d, should be %d)",
5875 nargs, needed_parms);
5876 return error_mark_node;
5879 /* If we aren't able to check the actual arguments now
5880 (because they haven't been expanded yet), we can at least
5881 verify that all of the types used for the non-type
5882 template parameter pack are, in fact, valid for non-type
5883 template parameters. */
5884 if (arg_idx < nargs
5885 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5887 int j, len = TREE_VEC_LENGTH (packed_types);
5888 for (j = 0; j < len; ++j)
5890 tree t = TREE_VEC_ELT (packed_types, j);
5891 if (invalid_nontype_parm_type_p (t, complain))
5892 return error_mark_node;
5897 /* Convert the remaining arguments, which will be a part of the
5898 parameter pack "parm". */
5899 for (; arg_idx < nargs; ++arg_idx)
5901 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5902 tree actual_parm = TREE_VALUE (parm);
5904 if (packed_types && !PACK_EXPANSION_P (arg))
5906 /* When we have a vector of types (corresponding to the
5907 non-type template parameter pack that uses parameter
5908 packs in its type, as mention above), and the
5909 argument is not an expansion (which expands to a
5910 currently unknown number of arguments), clone the
5911 parm and give it the next type in PACKED_TYPES. */
5912 actual_parm = copy_node (actual_parm);
5913 TREE_TYPE (actual_parm) =
5914 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5917 if (arg != error_mark_node)
5918 arg = convert_template_argument (actual_parm,
5919 arg, new_args, complain, parm_idx,
5920 in_decl);
5921 if (arg == error_mark_node)
5922 (*lost)++;
5923 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5926 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5927 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5928 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5929 else
5931 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5932 TREE_TYPE (argument_pack)
5933 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5934 TREE_CONSTANT (argument_pack) = 1;
5937 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5938 #ifdef ENABLE_CHECKING
5939 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
5940 TREE_VEC_LENGTH (packed_args));
5941 #endif
5942 return argument_pack;
5945 /* Convert all template arguments to their appropriate types, and
5946 return a vector containing the innermost resulting template
5947 arguments. If any error occurs, return error_mark_node. Error and
5948 warning messages are issued under control of COMPLAIN.
5950 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5951 for arguments not specified in ARGS. Otherwise, if
5952 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5953 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5954 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5955 ARGS. */
5957 static tree
5958 coerce_template_parms (tree parms,
5959 tree args,
5960 tree in_decl,
5961 tsubst_flags_t complain,
5962 bool require_all_args,
5963 bool use_default_args)
5965 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5966 tree inner_args;
5967 tree new_args;
5968 tree new_inner_args;
5969 int saved_unevaluated_operand;
5970 int saved_inhibit_evaluation_warnings;
5972 /* When used as a boolean value, indicates whether this is a
5973 variadic template parameter list. Since it's an int, we can also
5974 subtract it from nparms to get the number of non-variadic
5975 parameters. */
5976 int variadic_p = 0;
5978 if (args == error_mark_node)
5979 return error_mark_node;
5981 nparms = TREE_VEC_LENGTH (parms);
5983 /* Determine if there are any parameter packs. */
5984 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5986 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5987 if (template_parameter_pack_p (tparm))
5988 ++variadic_p;
5991 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5992 /* If there are 0 or 1 parameter packs, we need to expand any argument
5993 packs so that we can deduce a parameter pack from some non-packed args
5994 followed by an argument pack, as in variadic85.C. If there are more
5995 than that, we need to leave argument packs intact so the arguments are
5996 assigned to the right parameter packs. This should only happen when
5997 dealing with a nested class inside a partial specialization of a class
5998 template, as in variadic92.C. */
5999 if (variadic_p <= 1)
6000 inner_args = expand_template_argument_pack (inner_args);
6002 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6003 if ((nargs > nparms && !variadic_p)
6004 || (nargs < nparms - variadic_p
6005 && require_all_args
6006 && (!use_default_args
6007 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6008 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6010 if (complain & tf_error)
6012 if (variadic_p)
6014 --nparms;
6015 error ("wrong number of template arguments "
6016 "(%d, should be %d or more)", nargs, nparms);
6018 else
6019 error ("wrong number of template arguments "
6020 "(%d, should be %d)", nargs, nparms);
6022 if (in_decl)
6023 error ("provided for %q+D", in_decl);
6026 return error_mark_node;
6029 /* We need to evaluate the template arguments, even though this
6030 template-id may be nested within a "sizeof". */
6031 saved_unevaluated_operand = cp_unevaluated_operand;
6032 cp_unevaluated_operand = 0;
6033 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6034 c_inhibit_evaluation_warnings = 0;
6035 new_inner_args = make_tree_vec (nparms);
6036 new_args = add_outermost_template_args (args, new_inner_args);
6037 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6039 tree arg;
6040 tree parm;
6042 /* Get the Ith template parameter. */
6043 parm = TREE_VEC_ELT (parms, parm_idx);
6045 if (parm == error_mark_node)
6047 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6048 continue;
6051 /* Calculate the next argument. */
6052 if (arg_idx < nargs)
6053 arg = TREE_VEC_ELT (inner_args, arg_idx);
6054 else
6055 arg = NULL_TREE;
6057 if (template_parameter_pack_p (TREE_VALUE (parm))
6058 && !(arg && ARGUMENT_PACK_P (arg)))
6060 /* All remaining arguments will be placed in the
6061 template parameter pack PARM. */
6062 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6063 inner_args, arg_idx,
6064 new_args, &lost,
6065 in_decl, complain);
6067 /* Store this argument. */
6068 if (arg == error_mark_node)
6069 lost++;
6070 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6072 /* We are done with all of the arguments. */
6073 arg_idx = nargs;
6075 continue;
6077 else if (arg)
6079 if (PACK_EXPANSION_P (arg))
6081 if (complain & tf_error)
6083 /* FIXME this restriction was removed by N2555; see
6084 bug 35722. */
6085 /* If ARG is a pack expansion, but PARM is not a
6086 template parameter pack (if it were, we would have
6087 handled it above), we're trying to expand into a
6088 fixed-length argument list. */
6089 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6090 sorry ("cannot expand %<%E%> into a fixed-length "
6091 "argument list", arg);
6092 else
6093 sorry ("cannot expand %<%T%> into a fixed-length "
6094 "argument list", arg);
6096 return error_mark_node;
6099 else if (require_all_args)
6101 /* There must be a default arg in this case. */
6102 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6103 complain, in_decl);
6104 /* The position of the first default template argument,
6105 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6106 Record that. */
6107 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6108 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6110 else
6111 break;
6113 if (arg == error_mark_node)
6115 if (complain & tf_error)
6116 error ("template argument %d is invalid", arg_idx + 1);
6118 else if (!arg)
6119 /* This only occurs if there was an error in the template
6120 parameter list itself (which we would already have
6121 reported) that we are trying to recover from, e.g., a class
6122 template with a parameter list such as
6123 template<typename..., typename>. */
6124 return error_mark_node;
6125 else
6126 arg = convert_template_argument (TREE_VALUE (parm),
6127 arg, new_args, complain,
6128 parm_idx, in_decl);
6130 if (arg == error_mark_node)
6131 lost++;
6132 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6134 cp_unevaluated_operand = saved_unevaluated_operand;
6135 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6137 if (lost)
6138 return error_mark_node;
6140 #ifdef ENABLE_CHECKING
6141 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6142 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6143 TREE_VEC_LENGTH (new_inner_args));
6144 #endif
6146 return new_inner_args;
6149 /* Returns 1 if template args OT and NT are equivalent. */
6151 static int
6152 template_args_equal (tree ot, tree nt)
6154 if (nt == ot)
6155 return 1;
6157 if (TREE_CODE (nt) == TREE_VEC)
6158 /* For member templates */
6159 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6160 else if (PACK_EXPANSION_P (ot))
6161 return PACK_EXPANSION_P (nt)
6162 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6163 PACK_EXPANSION_PATTERN (nt));
6164 else if (ARGUMENT_PACK_P (ot))
6166 int i, len;
6167 tree opack, npack;
6169 if (!ARGUMENT_PACK_P (nt))
6170 return 0;
6172 opack = ARGUMENT_PACK_ARGS (ot);
6173 npack = ARGUMENT_PACK_ARGS (nt);
6174 len = TREE_VEC_LENGTH (opack);
6175 if (TREE_VEC_LENGTH (npack) != len)
6176 return 0;
6177 for (i = 0; i < len; ++i)
6178 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6179 TREE_VEC_ELT (npack, i)))
6180 return 0;
6181 return 1;
6183 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6185 /* We get here probably because we are in the middle of substituting
6186 into the pattern of a pack expansion. In that case the
6187 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6188 interested in. So we want to use the initial pack argument for
6189 the comparison. */
6190 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6191 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6192 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6193 return template_args_equal (ot, nt);
6195 else if (TYPE_P (nt))
6196 return TYPE_P (ot) && same_type_p (ot, nt);
6197 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6198 return 0;
6199 else
6200 return cp_tree_equal (ot, nt);
6203 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6204 of template arguments. Returns 0 otherwise. */
6207 comp_template_args (tree oldargs, tree newargs)
6209 int i;
6211 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6212 return 0;
6214 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6216 tree nt = TREE_VEC_ELT (newargs, i);
6217 tree ot = TREE_VEC_ELT (oldargs, i);
6219 if (! template_args_equal (ot, nt))
6220 return 0;
6222 return 1;
6225 static void
6226 add_pending_template (tree d)
6228 tree ti = (TYPE_P (d)
6229 ? CLASSTYPE_TEMPLATE_INFO (d)
6230 : DECL_TEMPLATE_INFO (d));
6231 struct pending_template *pt;
6232 int level;
6234 if (TI_PENDING_TEMPLATE_FLAG (ti))
6235 return;
6237 /* We are called both from instantiate_decl, where we've already had a
6238 tinst_level pushed, and instantiate_template, where we haven't.
6239 Compensate. */
6240 level = !current_tinst_level || current_tinst_level->decl != d;
6242 if (level)
6243 push_tinst_level (d);
6245 pt = ggc_alloc_pending_template ();
6246 pt->next = NULL;
6247 pt->tinst = current_tinst_level;
6248 if (last_pending_template)
6249 last_pending_template->next = pt;
6250 else
6251 pending_templates = pt;
6253 last_pending_template = pt;
6255 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6257 if (level)
6258 pop_tinst_level ();
6262 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6263 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6264 documentation for TEMPLATE_ID_EXPR. */
6266 tree
6267 lookup_template_function (tree fns, tree arglist)
6269 tree type;
6271 if (fns == error_mark_node || arglist == error_mark_node)
6272 return error_mark_node;
6274 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6275 gcc_assert (fns && (is_overloaded_fn (fns)
6276 || TREE_CODE (fns) == IDENTIFIER_NODE));
6278 if (BASELINK_P (fns))
6280 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6281 unknown_type_node,
6282 BASELINK_FUNCTIONS (fns),
6283 arglist);
6284 return fns;
6287 type = TREE_TYPE (fns);
6288 if (TREE_CODE (fns) == OVERLOAD || !type)
6289 type = unknown_type_node;
6291 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6294 /* Within the scope of a template class S<T>, the name S gets bound
6295 (in build_self_reference) to a TYPE_DECL for the class, not a
6296 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6297 or one of its enclosing classes, and that type is a template,
6298 return the associated TEMPLATE_DECL. Otherwise, the original
6299 DECL is returned.
6301 Also handle the case when DECL is a TREE_LIST of ambiguous
6302 injected-class-names from different bases. */
6304 tree
6305 maybe_get_template_decl_from_type_decl (tree decl)
6307 if (decl == NULL_TREE)
6308 return decl;
6310 /* DR 176: A lookup that finds an injected-class-name (10.2
6311 [class.member.lookup]) can result in an ambiguity in certain cases
6312 (for example, if it is found in more than one base class). If all of
6313 the injected-class-names that are found refer to specializations of
6314 the same class template, and if the name is followed by a
6315 template-argument-list, the reference refers to the class template
6316 itself and not a specialization thereof, and is not ambiguous. */
6317 if (TREE_CODE (decl) == TREE_LIST)
6319 tree t, tmpl = NULL_TREE;
6320 for (t = decl; t; t = TREE_CHAIN (t))
6322 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6323 if (!tmpl)
6324 tmpl = elt;
6325 else if (tmpl != elt)
6326 break;
6328 if (tmpl && t == NULL_TREE)
6329 return tmpl;
6330 else
6331 return decl;
6334 return (decl != NULL_TREE
6335 && DECL_SELF_REFERENCE_P (decl)
6336 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6337 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6340 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6341 parameters, find the desired type.
6343 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6345 IN_DECL, if non-NULL, is the template declaration we are trying to
6346 instantiate.
6348 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6349 the class we are looking up.
6351 Issue error and warning messages under control of COMPLAIN.
6353 If the template class is really a local class in a template
6354 function, then the FUNCTION_CONTEXT is the function in which it is
6355 being instantiated.
6357 ??? Note that this function is currently called *twice* for each
6358 template-id: the first time from the parser, while creating the
6359 incomplete type (finish_template_type), and the second type during the
6360 real instantiation (instantiate_template_class). This is surely something
6361 that we want to avoid. It also causes some problems with argument
6362 coercion (see convert_nontype_argument for more information on this). */
6364 tree
6365 lookup_template_class (tree d1,
6366 tree arglist,
6367 tree in_decl,
6368 tree context,
6369 int entering_scope,
6370 tsubst_flags_t complain)
6372 tree templ = NULL_TREE, parmlist;
6373 tree t;
6374 spec_entry **slot;
6375 spec_entry *entry;
6376 spec_entry elt;
6377 hashval_t hash;
6379 timevar_push (TV_NAME_LOOKUP);
6381 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6383 tree value = innermost_non_namespace_value (d1);
6384 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6385 templ = value;
6386 else
6388 if (context)
6389 push_decl_namespace (context);
6390 templ = lookup_name (d1);
6391 templ = maybe_get_template_decl_from_type_decl (templ);
6392 if (context)
6393 pop_decl_namespace ();
6395 if (templ)
6396 context = DECL_CONTEXT (templ);
6398 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6400 tree type = TREE_TYPE (d1);
6402 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6403 an implicit typename for the second A. Deal with it. */
6404 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6405 type = TREE_TYPE (type);
6407 if (CLASSTYPE_TEMPLATE_INFO (type))
6409 templ = CLASSTYPE_TI_TEMPLATE (type);
6410 d1 = DECL_NAME (templ);
6413 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6414 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6416 templ = TYPE_TI_TEMPLATE (d1);
6417 d1 = DECL_NAME (templ);
6419 else if (TREE_CODE (d1) == TEMPLATE_DECL
6420 && DECL_TEMPLATE_RESULT (d1)
6421 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6423 templ = d1;
6424 d1 = DECL_NAME (templ);
6425 context = DECL_CONTEXT (templ);
6428 /* Issue an error message if we didn't find a template. */
6429 if (! templ)
6431 if (complain & tf_error)
6432 error ("%qT is not a template", d1);
6433 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6436 if (TREE_CODE (templ) != TEMPLATE_DECL
6437 /* Make sure it's a user visible template, if it was named by
6438 the user. */
6439 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6440 && !PRIMARY_TEMPLATE_P (templ)))
6442 if (complain & tf_error)
6444 error ("non-template type %qT used as a template", d1);
6445 if (in_decl)
6446 error ("for template declaration %q+D", in_decl);
6448 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6451 complain &= ~tf_user;
6453 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6455 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6456 template arguments */
6458 tree parm;
6459 tree arglist2;
6460 tree outer;
6462 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6464 /* Consider an example where a template template parameter declared as
6466 template <class T, class U = std::allocator<T> > class TT
6468 The template parameter level of T and U are one level larger than
6469 of TT. To proper process the default argument of U, say when an
6470 instantiation `TT<int>' is seen, we need to build the full
6471 arguments containing {int} as the innermost level. Outer levels,
6472 available when not appearing as default template argument, can be
6473 obtained from the arguments of the enclosing template.
6475 Suppose that TT is later substituted with std::vector. The above
6476 instantiation is `TT<int, std::allocator<T> >' with TT at
6477 level 1, and T at level 2, while the template arguments at level 1
6478 becomes {std::vector} and the inner level 2 is {int}. */
6480 outer = DECL_CONTEXT (templ);
6481 if (outer)
6482 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6483 else if (current_template_parms)
6484 /* This is an argument of the current template, so we haven't set
6485 DECL_CONTEXT yet. */
6486 outer = current_template_args ();
6488 if (outer)
6489 arglist = add_to_template_args (outer, arglist);
6491 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6492 complain,
6493 /*require_all_args=*/true,
6494 /*use_default_args=*/true);
6495 if (arglist2 == error_mark_node
6496 || (!uses_template_parms (arglist2)
6497 && check_instantiated_args (templ, arglist2, complain)))
6498 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6500 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6501 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6503 else
6505 tree template_type = TREE_TYPE (templ);
6506 tree gen_tmpl;
6507 tree type_decl;
6508 tree found = NULL_TREE;
6509 int arg_depth;
6510 int parm_depth;
6511 int is_dependent_type;
6512 int use_partial_inst_tmpl = false;
6514 gen_tmpl = most_general_template (templ);
6515 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6516 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6517 arg_depth = TMPL_ARGS_DEPTH (arglist);
6519 if (arg_depth == 1 && parm_depth > 1)
6521 /* We've been given an incomplete set of template arguments.
6522 For example, given:
6524 template <class T> struct S1 {
6525 template <class U> struct S2 {};
6526 template <class U> struct S2<U*> {};
6529 we will be called with an ARGLIST of `U*', but the
6530 TEMPLATE will be `template <class T> template
6531 <class U> struct S1<T>::S2'. We must fill in the missing
6532 arguments. */
6533 arglist
6534 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6535 arglist);
6536 arg_depth = TMPL_ARGS_DEPTH (arglist);
6539 /* Now we should have enough arguments. */
6540 gcc_assert (parm_depth == arg_depth);
6542 /* From here on, we're only interested in the most general
6543 template. */
6545 /* Calculate the BOUND_ARGS. These will be the args that are
6546 actually tsubst'd into the definition to create the
6547 instantiation. */
6548 if (parm_depth > 1)
6550 /* We have multiple levels of arguments to coerce, at once. */
6551 int i;
6552 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6554 tree bound_args = make_tree_vec (parm_depth);
6556 for (i = saved_depth,
6557 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6558 i > 0 && t != NULL_TREE;
6559 --i, t = TREE_CHAIN (t))
6561 tree a;
6562 if (i == saved_depth)
6563 a = coerce_template_parms (TREE_VALUE (t),
6564 arglist, gen_tmpl,
6565 complain,
6566 /*require_all_args=*/true,
6567 /*use_default_args=*/true);
6568 else
6569 /* Outer levels should have already been coerced. */
6570 a = TMPL_ARGS_LEVEL (arglist, i);
6572 /* Don't process further if one of the levels fails. */
6573 if (a == error_mark_node)
6575 /* Restore the ARGLIST to its full size. */
6576 TREE_VEC_LENGTH (arglist) = saved_depth;
6577 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6580 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6582 /* We temporarily reduce the length of the ARGLIST so
6583 that coerce_template_parms will see only the arguments
6584 corresponding to the template parameters it is
6585 examining. */
6586 TREE_VEC_LENGTH (arglist)--;
6589 /* Restore the ARGLIST to its full size. */
6590 TREE_VEC_LENGTH (arglist) = saved_depth;
6592 arglist = bound_args;
6594 else
6595 arglist
6596 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6597 INNERMOST_TEMPLATE_ARGS (arglist),
6598 gen_tmpl,
6599 complain,
6600 /*require_all_args=*/true,
6601 /*use_default_args=*/true);
6603 if (arglist == error_mark_node)
6604 /* We were unable to bind the arguments. */
6605 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6607 /* In the scope of a template class, explicit references to the
6608 template class refer to the type of the template, not any
6609 instantiation of it. For example, in:
6611 template <class T> class C { void f(C<T>); }
6613 the `C<T>' is just the same as `C'. Outside of the
6614 class, however, such a reference is an instantiation. */
6615 if ((entering_scope
6616 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6617 || currently_open_class (template_type))
6618 /* comp_template_args is expensive, check it last. */
6619 && comp_template_args (TYPE_TI_ARGS (template_type),
6620 arglist))
6621 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6623 /* If we already have this specialization, return it. */
6624 elt.tmpl = gen_tmpl;
6625 elt.args = arglist;
6626 hash = hash_specialization (&elt);
6627 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6628 &elt, hash);
6630 if (entry)
6631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6633 is_dependent_type = uses_template_parms (arglist);
6635 /* If the deduced arguments are invalid, then the binding
6636 failed. */
6637 if (!is_dependent_type
6638 && check_instantiated_args (gen_tmpl,
6639 INNERMOST_TEMPLATE_ARGS (arglist),
6640 complain))
6641 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6643 if (!is_dependent_type
6644 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6645 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6646 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6648 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6649 DECL_NAME (gen_tmpl),
6650 /*tag_scope=*/ts_global);
6651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6654 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6655 complain, in_decl);
6656 if (!context)
6657 context = global_namespace;
6659 /* Create the type. */
6660 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6662 if (!is_dependent_type)
6664 set_current_access_from_decl (TYPE_NAME (template_type));
6665 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
6666 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6667 arglist, complain, in_decl),
6668 SCOPED_ENUM_P (template_type), NULL);
6670 else
6672 /* We don't want to call start_enum for this type, since
6673 the values for the enumeration constants may involve
6674 template parameters. And, no one should be interested
6675 in the enumeration constants for such a type. */
6676 t = cxx_make_type (ENUMERAL_TYPE);
6677 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6679 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
6681 else
6683 t = make_class_type (TREE_CODE (template_type));
6684 CLASSTYPE_DECLARED_CLASS (t)
6685 = CLASSTYPE_DECLARED_CLASS (template_type);
6686 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6687 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6689 /* A local class. Make sure the decl gets registered properly. */
6690 if (context == current_function_decl)
6691 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6693 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6694 /* This instantiation is another name for the primary
6695 template type. Set the TYPE_CANONICAL field
6696 appropriately. */
6697 TYPE_CANONICAL (t) = template_type;
6698 else if (any_template_arguments_need_structural_equality_p (arglist))
6699 /* Some of the template arguments require structural
6700 equality testing, so this template class requires
6701 structural equality testing. */
6702 SET_TYPE_STRUCTURAL_EQUALITY (t);
6705 /* If we called start_enum or pushtag above, this information
6706 will already be set up. */
6707 if (!TYPE_NAME (t))
6709 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6711 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6712 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6713 DECL_SOURCE_LOCATION (type_decl)
6714 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6716 else
6717 type_decl = TYPE_NAME (t);
6719 TREE_PRIVATE (type_decl)
6720 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6721 TREE_PROTECTED (type_decl)
6722 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6723 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6725 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6726 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6729 /* Let's consider the explicit specialization of a member
6730 of a class template specialization that is implicitely instantiated,
6731 e.g.:
6732 template<class T>
6733 struct S
6735 template<class U> struct M {}; //#0
6738 template<>
6739 template<>
6740 struct S<int>::M<char> //#1
6742 int i;
6744 [temp.expl.spec]/4 says this is valid.
6746 In this case, when we write:
6747 S<int>::M<char> m;
6749 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6750 the one of #0.
6752 When we encounter #1, we want to store the partial instantiation
6753 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6755 For all cases other than this "explicit specialization of member of a
6756 class template", we just want to store the most general template into
6757 the CLASSTYPE_TI_TEMPLATE of M.
6759 This case of "explicit specialization of member of a class template"
6760 only happens when:
6761 1/ the enclosing class is an instantiation of, and therefore not
6762 the same as, the context of the most general template, and
6763 2/ we aren't looking at the partial instantiation itself, i.e.
6764 the innermost arguments are not the same as the innermost parms of
6765 the most general template.
6767 So it's only when 1/ and 2/ happens that we want to use the partial
6768 instantiation of the member template in lieu of its most general
6769 template. */
6771 if (PRIMARY_TEMPLATE_P (gen_tmpl)
6772 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
6773 /* the enclosing class must be an instantiation... */
6774 && CLASS_TYPE_P (context)
6775 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
6777 tree partial_inst_args;
6778 TREE_VEC_LENGTH (arglist)--;
6779 ++processing_template_decl;
6780 partial_inst_args =
6781 tsubst (INNERMOST_TEMPLATE_ARGS
6782 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
6783 arglist, complain, NULL_TREE);
6784 --processing_template_decl;
6785 TREE_VEC_LENGTH (arglist)++;
6786 use_partial_inst_tmpl =
6787 /*...and we must not be looking at the partial instantiation
6788 itself. */
6789 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
6790 partial_inst_args);
6793 if (!use_partial_inst_tmpl)
6794 /* This case is easy; there are no member templates involved. */
6795 found = gen_tmpl;
6796 else
6798 /* This is a full instantiation of a member template. Find
6799 the partial instantiation of which this is an instance. */
6801 /* Temporarily reduce by one the number of levels in the ARGLIST
6802 so as to avoid comparing the last set of arguments. */
6803 TREE_VEC_LENGTH (arglist)--;
6804 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6805 TREE_VEC_LENGTH (arglist)++;
6806 found = CLASSTYPE_TI_TEMPLATE (found);
6809 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6811 elt.spec = t;
6812 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6813 &elt, hash, INSERT);
6814 *slot = ggc_alloc_spec_entry ();
6815 **slot = elt;
6817 /* Note this use of the partial instantiation so we can check it
6818 later in maybe_process_partial_specialization. */
6819 DECL_TEMPLATE_INSTANTIATIONS (templ)
6820 = tree_cons (arglist, t,
6821 DECL_TEMPLATE_INSTANTIATIONS (templ));
6823 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
6824 /* Now that the type has been registered on the instantiations
6825 list, we set up the enumerators. Because the enumeration
6826 constants may involve the enumeration type itself, we make
6827 sure to register the type first, and then create the
6828 constants. That way, doing tsubst_expr for the enumeration
6829 constants won't result in recursive calls here; we'll find
6830 the instantiation and exit above. */
6831 tsubst_enum (template_type, t, arglist);
6833 if (is_dependent_type)
6834 /* If the type makes use of template parameters, the
6835 code that generates debugging information will crash. */
6836 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6838 /* Possibly limit visibility based on template args. */
6839 TREE_PUBLIC (type_decl) = 1;
6840 determine_visibility (type_decl);
6842 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6844 timevar_pop (TV_NAME_LOOKUP);
6847 struct pair_fn_data
6849 tree_fn_t fn;
6850 void *data;
6851 /* True when we should also visit template parameters that occur in
6852 non-deduced contexts. */
6853 bool include_nondeduced_p;
6854 struct pointer_set_t *visited;
6857 /* Called from for_each_template_parm via walk_tree. */
6859 static tree
6860 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6862 tree t = *tp;
6863 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6864 tree_fn_t fn = pfd->fn;
6865 void *data = pfd->data;
6867 if (TYPE_P (t)
6868 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6869 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6870 pfd->include_nondeduced_p))
6871 return error_mark_node;
6873 switch (TREE_CODE (t))
6875 case RECORD_TYPE:
6876 if (TYPE_PTRMEMFUNC_P (t))
6877 break;
6878 /* Fall through. */
6880 case UNION_TYPE:
6881 case ENUMERAL_TYPE:
6882 if (!TYPE_TEMPLATE_INFO (t))
6883 *walk_subtrees = 0;
6884 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6885 fn, data, pfd->visited,
6886 pfd->include_nondeduced_p))
6887 return error_mark_node;
6888 break;
6890 case INTEGER_TYPE:
6891 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6892 fn, data, pfd->visited,
6893 pfd->include_nondeduced_p)
6894 || for_each_template_parm (TYPE_MAX_VALUE (t),
6895 fn, data, pfd->visited,
6896 pfd->include_nondeduced_p))
6897 return error_mark_node;
6898 break;
6900 case METHOD_TYPE:
6901 /* Since we're not going to walk subtrees, we have to do this
6902 explicitly here. */
6903 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6904 pfd->visited, pfd->include_nondeduced_p))
6905 return error_mark_node;
6906 /* Fall through. */
6908 case FUNCTION_TYPE:
6909 /* Check the return type. */
6910 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6911 pfd->include_nondeduced_p))
6912 return error_mark_node;
6914 /* Check the parameter types. Since default arguments are not
6915 instantiated until they are needed, the TYPE_ARG_TYPES may
6916 contain expressions that involve template parameters. But,
6917 no-one should be looking at them yet. And, once they're
6918 instantiated, they don't contain template parameters, so
6919 there's no point in looking at them then, either. */
6921 tree parm;
6923 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6924 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6925 pfd->visited, pfd->include_nondeduced_p))
6926 return error_mark_node;
6928 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6929 want walk_tree walking into them itself. */
6930 *walk_subtrees = 0;
6932 break;
6934 case TYPEOF_TYPE:
6935 if (pfd->include_nondeduced_p
6936 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6937 pfd->visited,
6938 pfd->include_nondeduced_p))
6939 return error_mark_node;
6940 break;
6942 case FUNCTION_DECL:
6943 case VAR_DECL:
6944 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6945 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6946 pfd->visited, pfd->include_nondeduced_p))
6947 return error_mark_node;
6948 /* Fall through. */
6950 case PARM_DECL:
6951 case CONST_DECL:
6952 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6953 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6954 pfd->visited, pfd->include_nondeduced_p))
6955 return error_mark_node;
6956 if (DECL_CONTEXT (t)
6957 && pfd->include_nondeduced_p
6958 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6959 pfd->visited, pfd->include_nondeduced_p))
6960 return error_mark_node;
6961 break;
6963 case BOUND_TEMPLATE_TEMPLATE_PARM:
6964 /* Record template parameters such as `T' inside `TT<T>'. */
6965 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6966 pfd->include_nondeduced_p))
6967 return error_mark_node;
6968 /* Fall through. */
6970 case TEMPLATE_TEMPLATE_PARM:
6971 case TEMPLATE_TYPE_PARM:
6972 case TEMPLATE_PARM_INDEX:
6973 if (fn && (*fn)(t, data))
6974 return error_mark_node;
6975 else if (!fn)
6976 return error_mark_node;
6977 break;
6979 case TEMPLATE_DECL:
6980 /* A template template parameter is encountered. */
6981 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6982 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6983 pfd->include_nondeduced_p))
6984 return error_mark_node;
6986 /* Already substituted template template parameter */
6987 *walk_subtrees = 0;
6988 break;
6990 case TYPENAME_TYPE:
6991 if (!fn
6992 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6993 data, pfd->visited,
6994 pfd->include_nondeduced_p))
6995 return error_mark_node;
6996 break;
6998 case CONSTRUCTOR:
6999 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7000 && pfd->include_nondeduced_p
7001 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7002 (TREE_TYPE (t)), fn, data,
7003 pfd->visited, pfd->include_nondeduced_p))
7004 return error_mark_node;
7005 break;
7007 case INDIRECT_REF:
7008 case COMPONENT_REF:
7009 /* If there's no type, then this thing must be some expression
7010 involving template parameters. */
7011 if (!fn && !TREE_TYPE (t))
7012 return error_mark_node;
7013 break;
7015 case MODOP_EXPR:
7016 case CAST_EXPR:
7017 case REINTERPRET_CAST_EXPR:
7018 case CONST_CAST_EXPR:
7019 case STATIC_CAST_EXPR:
7020 case DYNAMIC_CAST_EXPR:
7021 case ARROW_EXPR:
7022 case DOTSTAR_EXPR:
7023 case TYPEID_EXPR:
7024 case PSEUDO_DTOR_EXPR:
7025 if (!fn)
7026 return error_mark_node;
7027 break;
7029 default:
7030 break;
7033 /* We didn't find any template parameters we liked. */
7034 return NULL_TREE;
7037 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7038 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7039 call FN with the parameter and the DATA.
7040 If FN returns nonzero, the iteration is terminated, and
7041 for_each_template_parm returns 1. Otherwise, the iteration
7042 continues. If FN never returns a nonzero value, the value
7043 returned by for_each_template_parm is 0. If FN is NULL, it is
7044 considered to be the function which always returns 1.
7046 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7047 parameters that occur in non-deduced contexts. When false, only
7048 visits those template parameters that can be deduced. */
7050 static int
7051 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7052 struct pointer_set_t *visited,
7053 bool include_nondeduced_p)
7055 struct pair_fn_data pfd;
7056 int result;
7058 /* Set up. */
7059 pfd.fn = fn;
7060 pfd.data = data;
7061 pfd.include_nondeduced_p = include_nondeduced_p;
7063 /* Walk the tree. (Conceptually, we would like to walk without
7064 duplicates, but for_each_template_parm_r recursively calls
7065 for_each_template_parm, so we would need to reorganize a fair
7066 bit to use walk_tree_without_duplicates, so we keep our own
7067 visited list.) */
7068 if (visited)
7069 pfd.visited = visited;
7070 else
7071 pfd.visited = pointer_set_create ();
7072 result = cp_walk_tree (&t,
7073 for_each_template_parm_r,
7074 &pfd,
7075 pfd.visited) != NULL_TREE;
7077 /* Clean up. */
7078 if (!visited)
7080 pointer_set_destroy (pfd.visited);
7081 pfd.visited = 0;
7084 return result;
7087 /* Returns true if T depends on any template parameter. */
7090 uses_template_parms (tree t)
7092 bool dependent_p;
7093 int saved_processing_template_decl;
7095 saved_processing_template_decl = processing_template_decl;
7096 if (!saved_processing_template_decl)
7097 processing_template_decl = 1;
7098 if (TYPE_P (t))
7099 dependent_p = dependent_type_p (t);
7100 else if (TREE_CODE (t) == TREE_VEC)
7101 dependent_p = any_dependent_template_arguments_p (t);
7102 else if (TREE_CODE (t) == TREE_LIST)
7103 dependent_p = (uses_template_parms (TREE_VALUE (t))
7104 || uses_template_parms (TREE_CHAIN (t)));
7105 else if (TREE_CODE (t) == TYPE_DECL)
7106 dependent_p = dependent_type_p (TREE_TYPE (t));
7107 else if (DECL_P (t)
7108 || EXPR_P (t)
7109 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7110 || TREE_CODE (t) == OVERLOAD
7111 || TREE_CODE (t) == BASELINK
7112 || TREE_CODE (t) == IDENTIFIER_NODE
7113 || TREE_CODE (t) == TRAIT_EXPR
7114 || TREE_CODE (t) == CONSTRUCTOR
7115 || CONSTANT_CLASS_P (t))
7116 dependent_p = (type_dependent_expression_p (t)
7117 || value_dependent_expression_p (t));
7118 else
7120 gcc_assert (t == error_mark_node);
7121 dependent_p = false;
7124 processing_template_decl = saved_processing_template_decl;
7126 return dependent_p;
7129 /* Returns true if T depends on any template parameter with level LEVEL. */
7132 uses_template_parms_level (tree t, int level)
7134 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7135 /*include_nondeduced_p=*/true);
7138 static int tinst_depth;
7139 extern int max_tinst_depth;
7140 #ifdef GATHER_STATISTICS
7141 int depth_reached;
7142 #endif
7143 static int tinst_level_tick;
7144 static int last_template_error_tick;
7146 /* We're starting to instantiate D; record the template instantiation context
7147 for diagnostics and to restore it later. */
7150 push_tinst_level (tree d)
7152 struct tinst_level *new_level;
7154 if (tinst_depth >= max_tinst_depth)
7156 /* If the instantiation in question still has unbound template parms,
7157 we don't really care if we can't instantiate it, so just return.
7158 This happens with base instantiation for implicit `typename'. */
7159 if (uses_template_parms (d))
7160 return 0;
7162 last_template_error_tick = tinst_level_tick;
7163 error ("template instantiation depth exceeds maximum of %d (use "
7164 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7165 max_tinst_depth, d);
7167 print_instantiation_context ();
7169 return 0;
7172 new_level = ggc_alloc_tinst_level ();
7173 new_level->decl = d;
7174 new_level->locus = input_location;
7175 new_level->in_system_header_p = in_system_header;
7176 new_level->next = current_tinst_level;
7177 current_tinst_level = new_level;
7179 ++tinst_depth;
7180 #ifdef GATHER_STATISTICS
7181 if (tinst_depth > depth_reached)
7182 depth_reached = tinst_depth;
7183 #endif
7185 ++tinst_level_tick;
7186 return 1;
7189 /* We're done instantiating this template; return to the instantiation
7190 context. */
7192 void
7193 pop_tinst_level (void)
7195 /* Restore the filename and line number stashed away when we started
7196 this instantiation. */
7197 input_location = current_tinst_level->locus;
7198 current_tinst_level = current_tinst_level->next;
7199 --tinst_depth;
7200 ++tinst_level_tick;
7203 /* We're instantiating a deferred template; restore the template
7204 instantiation context in which the instantiation was requested, which
7205 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7207 static tree
7208 reopen_tinst_level (struct tinst_level *level)
7210 struct tinst_level *t;
7212 tinst_depth = 0;
7213 for (t = level; t; t = t->next)
7214 ++tinst_depth;
7216 current_tinst_level = level;
7217 pop_tinst_level ();
7218 return level->decl;
7221 /* Returns the TINST_LEVEL which gives the original instantiation
7222 context. */
7224 struct tinst_level *
7225 outermost_tinst_level (void)
7227 struct tinst_level *level = current_tinst_level;
7228 if (level)
7229 while (level->next)
7230 level = level->next;
7231 return level;
7234 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7236 bool
7237 parameter_of_template_p (tree parm, tree templ)
7239 tree parms;
7240 int i;
7242 if (!parm || !templ)
7243 return false;
7245 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7246 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7248 parms = DECL_TEMPLATE_PARMS (templ);
7249 parms = INNERMOST_TEMPLATE_PARMS (parms);
7251 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7252 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7253 return true;
7255 return false;
7258 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7259 vector of template arguments, as for tsubst.
7261 Returns an appropriate tsubst'd friend declaration. */
7263 static tree
7264 tsubst_friend_function (tree decl, tree args)
7266 tree new_friend;
7268 if (TREE_CODE (decl) == FUNCTION_DECL
7269 && DECL_TEMPLATE_INSTANTIATION (decl)
7270 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7271 /* This was a friend declared with an explicit template
7272 argument list, e.g.:
7274 friend void f<>(T);
7276 to indicate that f was a template instantiation, not a new
7277 function declaration. Now, we have to figure out what
7278 instantiation of what template. */
7280 tree template_id, arglist, fns;
7281 tree new_args;
7282 tree tmpl;
7283 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7285 /* Friend functions are looked up in the containing namespace scope.
7286 We must enter that scope, to avoid finding member functions of the
7287 current class with same name. */
7288 push_nested_namespace (ns);
7289 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7290 tf_warning_or_error, NULL_TREE,
7291 /*integral_constant_expression_p=*/false);
7292 pop_nested_namespace (ns);
7293 arglist = tsubst (DECL_TI_ARGS (decl), args,
7294 tf_warning_or_error, NULL_TREE);
7295 template_id = lookup_template_function (fns, arglist);
7297 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7298 tmpl = determine_specialization (template_id, new_friend,
7299 &new_args,
7300 /*need_member_template=*/0,
7301 TREE_VEC_LENGTH (args),
7302 tsk_none);
7303 return instantiate_template (tmpl, new_args, tf_error);
7306 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7308 /* The NEW_FRIEND will look like an instantiation, to the
7309 compiler, but is not an instantiation from the point of view of
7310 the language. For example, we might have had:
7312 template <class T> struct S {
7313 template <class U> friend void f(T, U);
7316 Then, in S<int>, template <class U> void f(int, U) is not an
7317 instantiation of anything. */
7318 if (new_friend == error_mark_node)
7319 return error_mark_node;
7321 DECL_USE_TEMPLATE (new_friend) = 0;
7322 if (TREE_CODE (decl) == TEMPLATE_DECL)
7324 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7325 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7326 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7329 /* The mangled name for the NEW_FRIEND is incorrect. The function
7330 is not a template instantiation and should not be mangled like
7331 one. Therefore, we forget the mangling here; we'll recompute it
7332 later if we need it. */
7333 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7335 SET_DECL_RTL (new_friend, NULL);
7336 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7339 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7341 tree old_decl;
7342 tree new_friend_template_info;
7343 tree new_friend_result_template_info;
7344 tree ns;
7345 int new_friend_is_defn;
7347 /* We must save some information from NEW_FRIEND before calling
7348 duplicate decls since that function will free NEW_FRIEND if
7349 possible. */
7350 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7351 new_friend_is_defn =
7352 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7353 (template_for_substitution (new_friend)))
7354 != NULL_TREE);
7355 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7357 /* This declaration is a `primary' template. */
7358 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7360 new_friend_result_template_info
7361 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7363 else
7364 new_friend_result_template_info = NULL_TREE;
7366 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7367 if (new_friend_is_defn)
7368 DECL_INITIAL (new_friend) = error_mark_node;
7370 /* Inside pushdecl_namespace_level, we will push into the
7371 current namespace. However, the friend function should go
7372 into the namespace of the template. */
7373 ns = decl_namespace_context (new_friend);
7374 push_nested_namespace (ns);
7375 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7376 pop_nested_namespace (ns);
7378 if (old_decl == error_mark_node)
7379 return error_mark_node;
7381 if (old_decl != new_friend)
7383 /* This new friend declaration matched an existing
7384 declaration. For example, given:
7386 template <class T> void f(T);
7387 template <class U> class C {
7388 template <class T> friend void f(T) {}
7391 the friend declaration actually provides the definition
7392 of `f', once C has been instantiated for some type. So,
7393 old_decl will be the out-of-class template declaration,
7394 while new_friend is the in-class definition.
7396 But, if `f' was called before this point, the
7397 instantiation of `f' will have DECL_TI_ARGS corresponding
7398 to `T' but not to `U', references to which might appear
7399 in the definition of `f'. Previously, the most general
7400 template for an instantiation of `f' was the out-of-class
7401 version; now it is the in-class version. Therefore, we
7402 run through all specialization of `f', adding to their
7403 DECL_TI_ARGS appropriately. In particular, they need a
7404 new set of outer arguments, corresponding to the
7405 arguments for this class instantiation.
7407 The same situation can arise with something like this:
7409 friend void f(int);
7410 template <class T> class C {
7411 friend void f(T) {}
7414 when `C<int>' is instantiated. Now, `f(int)' is defined
7415 in the class. */
7417 if (!new_friend_is_defn)
7418 /* On the other hand, if the in-class declaration does
7419 *not* provide a definition, then we don't want to alter
7420 existing definitions. We can just leave everything
7421 alone. */
7423 else
7425 tree new_template = TI_TEMPLATE (new_friend_template_info);
7426 tree new_args = TI_ARGS (new_friend_template_info);
7428 /* Overwrite whatever template info was there before, if
7429 any, with the new template information pertaining to
7430 the declaration. */
7431 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7433 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7435 /* We should have called reregister_specialization in
7436 duplicate_decls. */
7437 gcc_assert (retrieve_specialization (new_template,
7438 new_args, 0)
7439 == old_decl);
7441 /* Instantiate it if the global has already been used. */
7442 if (DECL_ODR_USED (old_decl))
7443 instantiate_decl (old_decl, /*defer_ok=*/true,
7444 /*expl_inst_class_mem_p=*/false);
7446 else
7448 tree t;
7450 /* Indicate that the old function template is a partial
7451 instantiation. */
7452 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7453 = new_friend_result_template_info;
7455 gcc_assert (new_template
7456 == most_general_template (new_template));
7457 gcc_assert (new_template != old_decl);
7459 /* Reassign any specializations already in the hash table
7460 to the new more general template, and add the
7461 additional template args. */
7462 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7463 t != NULL_TREE;
7464 t = TREE_CHAIN (t))
7466 tree spec = TREE_VALUE (t);
7467 spec_entry elt;
7469 elt.tmpl = old_decl;
7470 elt.args = DECL_TI_ARGS (spec);
7471 elt.spec = NULL_TREE;
7473 htab_remove_elt (decl_specializations, &elt);
7475 DECL_TI_ARGS (spec)
7476 = add_outermost_template_args (new_args,
7477 DECL_TI_ARGS (spec));
7479 register_specialization
7480 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7483 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7487 /* The information from NEW_FRIEND has been merged into OLD_DECL
7488 by duplicate_decls. */
7489 new_friend = old_decl;
7492 else
7494 tree context = DECL_CONTEXT (new_friend);
7495 bool dependent_p;
7497 /* In the code
7498 template <class T> class C {
7499 template <class U> friend void C1<U>::f (); // case 1
7500 friend void C2<T>::f (); // case 2
7502 we only need to make sure CONTEXT is a complete type for
7503 case 2. To distinguish between the two cases, we note that
7504 CONTEXT of case 1 remains dependent type after tsubst while
7505 this isn't true for case 2. */
7506 ++processing_template_decl;
7507 dependent_p = dependent_type_p (context);
7508 --processing_template_decl;
7510 if (!dependent_p
7511 && !complete_type_or_else (context, NULL_TREE))
7512 return error_mark_node;
7514 if (COMPLETE_TYPE_P (context))
7516 /* Check to see that the declaration is really present, and,
7517 possibly obtain an improved declaration. */
7518 tree fn = check_classfn (context,
7519 new_friend, NULL_TREE);
7521 if (fn)
7522 new_friend = fn;
7526 return new_friend;
7529 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7530 template arguments, as for tsubst.
7532 Returns an appropriate tsubst'd friend type or error_mark_node on
7533 failure. */
7535 static tree
7536 tsubst_friend_class (tree friend_tmpl, tree args)
7538 tree friend_type;
7539 tree tmpl;
7540 tree context;
7542 context = CP_DECL_CONTEXT (friend_tmpl);
7544 if (context != global_namespace)
7546 if (TREE_CODE (context) == NAMESPACE_DECL)
7547 push_nested_namespace (context);
7548 else
7549 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7552 /* Look for a class template declaration. We look for hidden names
7553 because two friend declarations of the same template are the
7554 same. For example, in:
7556 struct A {
7557 template <typename> friend class F;
7559 template <typename> struct B {
7560 template <typename> friend class F;
7563 both F templates are the same. */
7564 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7565 /*block_p=*/true, 0,
7566 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7568 /* But, if we don't find one, it might be because we're in a
7569 situation like this:
7571 template <class T>
7572 struct S {
7573 template <class U>
7574 friend struct S;
7577 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7578 for `S<int>', not the TEMPLATE_DECL. */
7579 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7581 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7582 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7585 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7587 /* The friend template has already been declared. Just
7588 check to see that the declarations match, and install any new
7589 default parameters. We must tsubst the default parameters,
7590 of course. We only need the innermost template parameters
7591 because that is all that redeclare_class_template will look
7592 at. */
7593 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7594 > TMPL_ARGS_DEPTH (args))
7596 tree parms;
7597 location_t saved_input_location;
7598 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7599 args, tf_warning_or_error);
7601 saved_input_location = input_location;
7602 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7603 redeclare_class_template (TREE_TYPE (tmpl), parms);
7604 input_location = saved_input_location;
7608 friend_type = TREE_TYPE (tmpl);
7610 else
7612 /* The friend template has not already been declared. In this
7613 case, the instantiation of the template class will cause the
7614 injection of this template into the global scope. */
7615 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7616 if (tmpl == error_mark_node)
7617 return error_mark_node;
7619 /* The new TMPL is not an instantiation of anything, so we
7620 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7621 the new type because that is supposed to be the corresponding
7622 template decl, i.e., TMPL. */
7623 DECL_USE_TEMPLATE (tmpl) = 0;
7624 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7625 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7626 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7627 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7629 /* Inject this template into the global scope. */
7630 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7633 if (context != global_namespace)
7635 if (TREE_CODE (context) == NAMESPACE_DECL)
7636 pop_nested_namespace (context);
7637 else
7638 pop_nested_class ();
7641 return friend_type;
7644 /* Returns zero if TYPE cannot be completed later due to circularity.
7645 Otherwise returns one. */
7647 static int
7648 can_complete_type_without_circularity (tree type)
7650 if (type == NULL_TREE || type == error_mark_node)
7651 return 0;
7652 else if (COMPLETE_TYPE_P (type))
7653 return 1;
7654 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7655 return can_complete_type_without_circularity (TREE_TYPE (type));
7656 else if (CLASS_TYPE_P (type)
7657 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7658 return 0;
7659 else
7660 return 1;
7663 /* Apply any attributes which had to be deferred until instantiation
7664 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7665 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7667 static void
7668 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7669 tree args, tsubst_flags_t complain, tree in_decl)
7671 tree last_dep = NULL_TREE;
7672 tree t;
7673 tree *p;
7675 for (t = attributes; t; t = TREE_CHAIN (t))
7676 if (ATTR_IS_DEPENDENT (t))
7678 last_dep = t;
7679 attributes = copy_list (attributes);
7680 break;
7683 if (DECL_P (*decl_p))
7685 if (TREE_TYPE (*decl_p) == error_mark_node)
7686 return;
7687 p = &DECL_ATTRIBUTES (*decl_p);
7689 else
7690 p = &TYPE_ATTRIBUTES (*decl_p);
7692 if (last_dep)
7694 tree late_attrs = NULL_TREE;
7695 tree *q = &late_attrs;
7697 for (*p = attributes; *p; )
7699 t = *p;
7700 if (ATTR_IS_DEPENDENT (t))
7702 *p = TREE_CHAIN (t);
7703 TREE_CHAIN (t) = NULL_TREE;
7704 /* If the first attribute argument is an identifier, don't
7705 pass it through tsubst. Attributes like mode, format,
7706 cleanup and several target specific attributes expect it
7707 unmodified. */
7708 if (TREE_VALUE (t)
7709 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7710 && TREE_VALUE (TREE_VALUE (t))
7711 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7712 == IDENTIFIER_NODE))
7714 tree chain
7715 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7716 in_decl,
7717 /*integral_constant_expression_p=*/false);
7718 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7719 TREE_VALUE (t)
7720 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7721 chain);
7723 else
7724 TREE_VALUE (t)
7725 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7726 /*integral_constant_expression_p=*/false);
7727 *q = t;
7728 q = &TREE_CHAIN (t);
7730 else
7731 p = &TREE_CHAIN (t);
7734 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7738 /* Perform (or defer) access check for typedefs that were referenced
7739 from within the template TMPL code.
7740 This is a subroutine of instantiate_template and instantiate_class_template.
7741 TMPL is the template to consider and TARGS is the list of arguments of
7742 that template. */
7744 static void
7745 perform_typedefs_access_check (tree tmpl, tree targs)
7747 location_t saved_location;
7748 int i;
7749 qualified_typedef_usage_t *iter;
7751 if (!tmpl
7752 || (!CLASS_TYPE_P (tmpl)
7753 && TREE_CODE (tmpl) != FUNCTION_DECL))
7754 return;
7756 saved_location = input_location;
7757 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
7758 get_types_needing_access_check (tmpl),
7759 i, iter)
7761 tree type_decl = iter->typedef_decl;
7762 tree type_scope = iter->context;
7764 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7765 continue;
7767 if (uses_template_parms (type_decl))
7768 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7769 if (uses_template_parms (type_scope))
7770 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7772 /* Make access check error messages point to the location
7773 of the use of the typedef. */
7774 input_location = iter->locus;
7775 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7776 type_decl, type_decl);
7778 input_location = saved_location;
7781 tree
7782 instantiate_class_template (tree type)
7784 tree templ, args, pattern, t, member;
7785 tree typedecl;
7786 tree pbinfo;
7787 tree base_list;
7788 unsigned int saved_maximum_field_alignment;
7790 if (type == error_mark_node)
7791 return error_mark_node;
7793 if (COMPLETE_OR_OPEN_TYPE_P (type)
7794 || uses_template_parms (type))
7795 return type;
7797 /* Figure out which template is being instantiated. */
7798 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7799 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7801 /* Determine what specialization of the original template to
7802 instantiate. */
7803 t = most_specialized_class (type, templ, tf_warning_or_error);
7804 if (t == error_mark_node)
7806 TYPE_BEING_DEFINED (type) = 1;
7807 return error_mark_node;
7809 else if (t)
7811 /* This TYPE is actually an instantiation of a partial
7812 specialization. We replace the innermost set of ARGS with
7813 the arguments appropriate for substitution. For example,
7814 given:
7816 template <class T> struct S {};
7817 template <class T> struct S<T*> {};
7819 and supposing that we are instantiating S<int*>, ARGS will
7820 presently be {int*} -- but we need {int}. */
7821 pattern = TREE_TYPE (t);
7822 args = TREE_PURPOSE (t);
7824 else
7826 pattern = TREE_TYPE (templ);
7827 args = CLASSTYPE_TI_ARGS (type);
7830 /* If the template we're instantiating is incomplete, then clearly
7831 there's nothing we can do. */
7832 if (!COMPLETE_TYPE_P (pattern))
7833 return type;
7835 /* If we've recursively instantiated too many templates, stop. */
7836 if (! push_tinst_level (type))
7837 return type;
7839 /* Now we're really doing the instantiation. Mark the type as in
7840 the process of being defined. */
7841 TYPE_BEING_DEFINED (type) = 1;
7843 /* We may be in the middle of deferred access check. Disable
7844 it now. */
7845 push_deferring_access_checks (dk_no_deferred);
7847 push_to_top_level ();
7848 /* Use #pragma pack from the template context. */
7849 saved_maximum_field_alignment = maximum_field_alignment;
7850 maximum_field_alignment = TYPE_PRECISION (pattern);
7852 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7854 /* Set the input location to the most specialized template definition.
7855 This is needed if tsubsting causes an error. */
7856 typedecl = TYPE_MAIN_DECL (pattern);
7857 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
7858 DECL_SOURCE_LOCATION (typedecl);
7860 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7861 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7862 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7863 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7864 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
7865 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
7866 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
7867 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
7868 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7869 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7870 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7871 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7872 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7873 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7874 if (ANON_AGGR_TYPE_P (pattern))
7875 SET_ANON_AGGR_TYPE_P (type);
7876 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7878 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7879 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7882 pbinfo = TYPE_BINFO (pattern);
7884 /* We should never instantiate a nested class before its enclosing
7885 class; we need to look up the nested class by name before we can
7886 instantiate it, and that lookup should instantiate the enclosing
7887 class. */
7888 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7889 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
7891 base_list = NULL_TREE;
7892 if (BINFO_N_BASE_BINFOS (pbinfo))
7894 tree pbase_binfo;
7895 tree pushed_scope;
7896 int i;
7898 /* We must enter the scope containing the type, as that is where
7899 the accessibility of types named in dependent bases are
7900 looked up from. */
7901 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
7903 /* Substitute into each of the bases to determine the actual
7904 basetypes. */
7905 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7907 tree base;
7908 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7909 tree expanded_bases = NULL_TREE;
7910 int idx, len = 1;
7912 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7914 expanded_bases =
7915 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7916 args, tf_error, NULL_TREE);
7917 if (expanded_bases == error_mark_node)
7918 continue;
7920 len = TREE_VEC_LENGTH (expanded_bases);
7923 for (idx = 0; idx < len; idx++)
7925 if (expanded_bases)
7926 /* Extract the already-expanded base class. */
7927 base = TREE_VEC_ELT (expanded_bases, idx);
7928 else
7929 /* Substitute to figure out the base class. */
7930 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7931 NULL_TREE);
7933 if (base == error_mark_node)
7934 continue;
7936 base_list = tree_cons (access, base, base_list);
7937 if (BINFO_VIRTUAL_P (pbase_binfo))
7938 TREE_TYPE (base_list) = integer_type_node;
7942 /* The list is now in reverse order; correct that. */
7943 base_list = nreverse (base_list);
7945 if (pushed_scope)
7946 pop_scope (pushed_scope);
7948 /* Now call xref_basetypes to set up all the base-class
7949 information. */
7950 xref_basetypes (type, base_list);
7952 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7953 (int) ATTR_FLAG_TYPE_IN_PLACE,
7954 args, tf_error, NULL_TREE);
7955 fixup_attribute_variants (type);
7957 /* Now that our base classes are set up, enter the scope of the
7958 class, so that name lookups into base classes, etc. will work
7959 correctly. This is precisely analogous to what we do in
7960 begin_class_definition when defining an ordinary non-template
7961 class, except we also need to push the enclosing classes. */
7962 push_nested_class (type);
7964 /* Now members are processed in the order of declaration. */
7965 for (member = CLASSTYPE_DECL_LIST (pattern);
7966 member; member = TREE_CHAIN (member))
7968 tree t = TREE_VALUE (member);
7970 if (TREE_PURPOSE (member))
7972 if (TYPE_P (t))
7974 /* Build new CLASSTYPE_NESTED_UTDS. */
7976 tree newtag;
7977 bool class_template_p;
7979 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7980 && TYPE_LANG_SPECIFIC (t)
7981 && CLASSTYPE_IS_TEMPLATE (t));
7982 /* If the member is a class template, then -- even after
7983 substitution -- there may be dependent types in the
7984 template argument list for the class. We increment
7985 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7986 that function will assume that no types are dependent
7987 when outside of a template. */
7988 if (class_template_p)
7989 ++processing_template_decl;
7990 newtag = tsubst (t, args, tf_error, NULL_TREE);
7991 if (class_template_p)
7992 --processing_template_decl;
7993 if (newtag == error_mark_node)
7994 continue;
7996 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7998 tree name = TYPE_IDENTIFIER (t);
8000 if (class_template_p)
8001 /* Unfortunately, lookup_template_class sets
8002 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8003 instantiation (i.e., for the type of a member
8004 template class nested within a template class.)
8005 This behavior is required for
8006 maybe_process_partial_specialization to work
8007 correctly, but is not accurate in this case;
8008 the TAG is not an instantiation of anything.
8009 (The corresponding TEMPLATE_DECL is an
8010 instantiation, but the TYPE is not.) */
8011 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8013 /* Now, we call pushtag to put this NEWTAG into the scope of
8014 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8015 pushtag calling push_template_decl. We don't have to do
8016 this for enums because it will already have been done in
8017 tsubst_enum. */
8018 if (name)
8019 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8020 pushtag (name, newtag, /*tag_scope=*/ts_current);
8023 else if (TREE_CODE (t) == FUNCTION_DECL
8024 || DECL_FUNCTION_TEMPLATE_P (t))
8026 /* Build new TYPE_METHODS. */
8027 tree r;
8029 if (TREE_CODE (t) == TEMPLATE_DECL)
8030 ++processing_template_decl;
8031 r = tsubst (t, args, tf_error, NULL_TREE);
8032 if (TREE_CODE (t) == TEMPLATE_DECL)
8033 --processing_template_decl;
8034 set_current_access_from_decl (r);
8035 finish_member_declaration (r);
8037 else
8039 /* Build new TYPE_FIELDS. */
8040 if (TREE_CODE (t) == STATIC_ASSERT)
8042 tree condition =
8043 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8044 tf_warning_or_error, NULL_TREE,
8045 /*integral_constant_expression_p=*/true);
8046 finish_static_assert (condition,
8047 STATIC_ASSERT_MESSAGE (t),
8048 STATIC_ASSERT_SOURCE_LOCATION (t),
8049 /*member_p=*/true);
8051 else if (TREE_CODE (t) != CONST_DECL)
8053 tree r;
8055 /* The file and line for this declaration, to
8056 assist in error message reporting. Since we
8057 called push_tinst_level above, we don't need to
8058 restore these. */
8059 input_location = DECL_SOURCE_LOCATION (t);
8061 if (TREE_CODE (t) == TEMPLATE_DECL)
8062 ++processing_template_decl;
8063 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8064 if (TREE_CODE (t) == TEMPLATE_DECL)
8065 --processing_template_decl;
8066 if (TREE_CODE (r) == VAR_DECL)
8068 /* In [temp.inst]:
8070 [t]he initialization (and any associated
8071 side-effects) of a static data member does
8072 not occur unless the static data member is
8073 itself used in a way that requires the
8074 definition of the static data member to
8075 exist.
8077 Therefore, we do not substitute into the
8078 initialized for the static data member here. */
8079 finish_static_data_member_decl
8081 /*init=*/NULL_TREE,
8082 /*init_const_expr_p=*/false,
8083 /*asmspec_tree=*/NULL_TREE,
8084 /*flags=*/0);
8085 if (DECL_INITIALIZED_IN_CLASS_P (r))
8086 check_static_variable_definition (r, TREE_TYPE (r));
8088 else if (TREE_CODE (r) == FIELD_DECL)
8090 /* Determine whether R has a valid type and can be
8091 completed later. If R is invalid, then it is
8092 replaced by error_mark_node so that it will not be
8093 added to TYPE_FIELDS. */
8094 tree rtype = TREE_TYPE (r);
8095 if (can_complete_type_without_circularity (rtype))
8096 complete_type (rtype);
8098 if (!COMPLETE_TYPE_P (rtype))
8100 cxx_incomplete_type_error (r, rtype);
8101 r = error_mark_node;
8105 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8106 such a thing will already have been added to the field
8107 list by tsubst_enum in finish_member_declaration in the
8108 CLASSTYPE_NESTED_UTDS case above. */
8109 if (!(TREE_CODE (r) == TYPE_DECL
8110 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8111 && DECL_ARTIFICIAL (r)))
8113 set_current_access_from_decl (r);
8114 finish_member_declaration (r);
8119 else
8121 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8123 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8125 tree friend_type = t;
8126 bool adjust_processing_template_decl = false;
8128 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8130 /* template <class T> friend class C; */
8131 friend_type = tsubst_friend_class (friend_type, args);
8132 adjust_processing_template_decl = true;
8134 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8136 /* template <class T> friend class C::D; */
8137 friend_type = tsubst (friend_type, args,
8138 tf_warning_or_error, NULL_TREE);
8139 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8140 friend_type = TREE_TYPE (friend_type);
8141 adjust_processing_template_decl = true;
8143 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8145 /* This could be either
8147 friend class T::C;
8149 when dependent_type_p is false or
8151 template <class U> friend class T::C;
8153 otherwise. */
8154 friend_type = tsubst (friend_type, args,
8155 tf_warning_or_error, NULL_TREE);
8156 /* Bump processing_template_decl for correct
8157 dependent_type_p calculation. */
8158 ++processing_template_decl;
8159 if (dependent_type_p (friend_type))
8160 adjust_processing_template_decl = true;
8161 --processing_template_decl;
8163 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8164 && hidden_name_p (TYPE_NAME (friend_type)))
8166 /* friend class C;
8168 where C hasn't been declared yet. Let's lookup name
8169 from namespace scope directly, bypassing any name that
8170 come from dependent base class. */
8171 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8173 /* The call to xref_tag_from_type does injection for friend
8174 classes. */
8175 push_nested_namespace (ns);
8176 friend_type =
8177 xref_tag_from_type (friend_type, NULL_TREE,
8178 /*tag_scope=*/ts_current);
8179 pop_nested_namespace (ns);
8181 else if (uses_template_parms (friend_type))
8182 /* friend class C<T>; */
8183 friend_type = tsubst (friend_type, args,
8184 tf_warning_or_error, NULL_TREE);
8185 /* Otherwise it's
8187 friend class C;
8189 where C is already declared or
8191 friend class C<int>;
8193 We don't have to do anything in these cases. */
8195 if (adjust_processing_template_decl)
8196 /* Trick make_friend_class into realizing that the friend
8197 we're adding is a template, not an ordinary class. It's
8198 important that we use make_friend_class since it will
8199 perform some error-checking and output cross-reference
8200 information. */
8201 ++processing_template_decl;
8203 if (friend_type != error_mark_node)
8204 make_friend_class (type, friend_type, /*complain=*/false);
8206 if (adjust_processing_template_decl)
8207 --processing_template_decl;
8209 else
8211 /* Build new DECL_FRIENDLIST. */
8212 tree r;
8214 /* The file and line for this declaration, to
8215 assist in error message reporting. Since we
8216 called push_tinst_level above, we don't need to
8217 restore these. */
8218 input_location = DECL_SOURCE_LOCATION (t);
8220 if (TREE_CODE (t) == TEMPLATE_DECL)
8222 ++processing_template_decl;
8223 push_deferring_access_checks (dk_no_check);
8226 r = tsubst_friend_function (t, args);
8227 add_friend (type, r, /*complain=*/false);
8228 if (TREE_CODE (t) == TEMPLATE_DECL)
8230 pop_deferring_access_checks ();
8231 --processing_template_decl;
8237 /* Set the file and line number information to whatever is given for
8238 the class itself. This puts error messages involving generated
8239 implicit functions at a predictable point, and the same point
8240 that would be used for non-template classes. */
8241 input_location = DECL_SOURCE_LOCATION (typedecl);
8243 unreverse_member_declarations (type);
8244 finish_struct_1 (type);
8245 TYPE_BEING_DEFINED (type) = 0;
8247 /* We don't instantiate default arguments for member functions. 14.7.1:
8249 The implicit instantiation of a class template specialization causes
8250 the implicit instantiation of the declarations, but not of the
8251 definitions or default arguments, of the class member functions,
8252 member classes, static data members and member templates.... */
8254 /* Some typedefs referenced from within the template code need to be access
8255 checked at template instantiation time, i.e now. These types were
8256 added to the template at parsing time. Let's get those and perform
8257 the access checks then. */
8258 perform_typedefs_access_check (pattern, args);
8259 perform_deferred_access_checks ();
8260 pop_nested_class ();
8261 maximum_field_alignment = saved_maximum_field_alignment;
8262 pop_from_top_level ();
8263 pop_deferring_access_checks ();
8264 pop_tinst_level ();
8266 /* The vtable for a template class can be emitted in any translation
8267 unit in which the class is instantiated. When there is no key
8268 method, however, finish_struct_1 will already have added TYPE to
8269 the keyed_classes list. */
8270 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8271 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8273 return type;
8276 static tree
8277 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8279 tree r;
8281 if (!t)
8282 r = t;
8283 else if (TYPE_P (t))
8284 r = tsubst (t, args, complain, in_decl);
8285 else
8287 if (!(complain & tf_warning))
8288 ++c_inhibit_evaluation_warnings;
8289 r = tsubst_expr (t, args, complain, in_decl,
8290 /*integral_constant_expression_p=*/true);
8291 if (!(complain & tf_warning))
8292 --c_inhibit_evaluation_warnings;
8294 return r;
8297 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8298 NONTYPE_ARGUMENT_PACK. */
8300 static tree
8301 make_fnparm_pack (tree spec_parm)
8303 /* Collect all of the extra "packed" parameters into an
8304 argument pack. */
8305 tree parmvec;
8306 tree parmtypevec;
8307 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8308 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8309 int i, len = list_length (spec_parm);
8311 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8312 parmvec = make_tree_vec (len);
8313 parmtypevec = make_tree_vec (len);
8314 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8316 TREE_VEC_ELT (parmvec, i) = spec_parm;
8317 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8320 /* Build the argument packs. */
8321 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8322 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8323 TREE_TYPE (argpack) = argtypepack;
8325 return argpack;
8328 /* Substitute ARGS into T, which is an pack expansion
8329 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8330 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8331 (if only a partial substitution could be performed) or
8332 ERROR_MARK_NODE if there was an error. */
8333 tree
8334 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8335 tree in_decl)
8337 tree pattern;
8338 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8339 int i, len = -1;
8340 tree result;
8341 int incomplete = 0;
8342 htab_t saved_local_specializations = NULL;
8344 gcc_assert (PACK_EXPANSION_P (t));
8345 pattern = PACK_EXPANSION_PATTERN (t);
8347 /* Determine the argument packs that will instantiate the parameter
8348 packs used in the expansion expression. While we're at it,
8349 compute the number of arguments to be expanded and make sure it
8350 is consistent. */
8351 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8352 pack = TREE_CHAIN (pack))
8354 tree parm_pack = TREE_VALUE (pack);
8355 tree arg_pack = NULL_TREE;
8356 tree orig_arg = NULL_TREE;
8358 if (TREE_CODE (parm_pack) == PARM_DECL)
8360 if (!cp_unevaluated_operand)
8361 arg_pack = retrieve_local_specialization (parm_pack);
8362 else
8364 /* We can't rely on local_specializations for a parameter
8365 name used later in a function declaration (such as in a
8366 late-specified return type). Even if it exists, it might
8367 have the wrong value for a recursive call. Just make a
8368 dummy decl, since it's only used for its type. */
8369 arg_pack = tsubst_decl (parm_pack, args, complain);
8370 arg_pack = make_fnparm_pack (arg_pack);
8373 else
8375 int level, idx, levels;
8376 template_parm_level_and_index (parm_pack, &level, &idx);
8378 levels = TMPL_ARGS_DEPTH (args);
8379 if (level <= levels)
8380 arg_pack = TMPL_ARG (args, level, idx);
8383 orig_arg = arg_pack;
8384 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8385 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8387 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8388 /* This can only happen if we forget to expand an argument
8389 pack somewhere else. Just return an error, silently. */
8391 result = make_tree_vec (1);
8392 TREE_VEC_ELT (result, 0) = error_mark_node;
8393 return result;
8396 if (arg_pack
8397 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8398 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8400 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8401 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8402 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8403 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8404 /* The argument pack that the parameter maps to is just an
8405 expansion of the parameter itself, such as one would
8406 find in the implicit typedef of a class inside the
8407 class itself. Consider this parameter "unsubstituted",
8408 so that we will maintain the outer pack expansion. */
8409 arg_pack = NULL_TREE;
8412 if (arg_pack)
8414 int my_len =
8415 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8417 /* It's all-or-nothing with incomplete argument packs. */
8418 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8419 return error_mark_node;
8421 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8422 incomplete = 1;
8424 if (len < 0)
8425 len = my_len;
8426 else if (len != my_len)
8428 if (incomplete)
8429 /* We got explicit args for some packs but not others;
8430 do nothing now and try again after deduction. */
8431 return t;
8432 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8433 error ("mismatched argument pack lengths while expanding "
8434 "%<%T%>",
8435 pattern);
8436 else
8437 error ("mismatched argument pack lengths while expanding "
8438 "%<%E%>",
8439 pattern);
8440 return error_mark_node;
8443 /* Keep track of the parameter packs and their corresponding
8444 argument packs. */
8445 packs = tree_cons (parm_pack, arg_pack, packs);
8446 TREE_TYPE (packs) = orig_arg;
8448 else
8449 /* We can't substitute for this parameter pack. */
8450 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8451 TREE_VALUE (pack),
8452 unsubstituted_packs);
8455 /* We cannot expand this expansion expression, because we don't have
8456 all of the argument packs we need. Substitute into the pattern
8457 and return a PACK_EXPANSION_*. The caller will need to deal with
8458 that. */
8459 if (unsubstituted_packs)
8461 tree new_pat;
8462 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8463 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8464 /*integral_constant_expression_p=*/false);
8465 else
8466 new_pat = tsubst (pattern, args, complain, in_decl);
8467 return make_pack_expansion (new_pat);
8470 /* We could not find any argument packs that work. */
8471 if (len < 0)
8472 return error_mark_node;
8474 if (cp_unevaluated_operand)
8476 /* We're in a late-specified return type, so create our own local
8477 specializations table; the current table is either NULL or (in the
8478 case of recursive unification) might have bindings that we don't
8479 want to use or alter. */
8480 saved_local_specializations = local_specializations;
8481 local_specializations = htab_create (37,
8482 hash_local_specialization,
8483 eq_local_specializations,
8484 NULL);
8487 /* For each argument in each argument pack, substitute into the
8488 pattern. */
8489 result = make_tree_vec (len + incomplete);
8490 for (i = 0; i < len + incomplete; ++i)
8492 /* For parameter pack, change the substitution of the parameter
8493 pack to the ith argument in its argument pack, then expand
8494 the pattern. */
8495 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8497 tree parm = TREE_PURPOSE (pack);
8499 if (TREE_CODE (parm) == PARM_DECL)
8501 /* Select the Ith argument from the pack. */
8502 tree arg = make_node (ARGUMENT_PACK_SELECT);
8503 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8504 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8505 mark_used (parm);
8506 register_local_specialization (arg, parm);
8508 else
8510 tree value = parm;
8511 int idx, level;
8512 template_parm_level_and_index (parm, &level, &idx);
8514 if (i < len)
8516 /* Select the Ith argument from the pack. */
8517 value = make_node (ARGUMENT_PACK_SELECT);
8518 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8519 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8522 /* Update the corresponding argument. */
8523 TMPL_ARG (args, level, idx) = value;
8527 /* Substitute into the PATTERN with the altered arguments. */
8528 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8529 TREE_VEC_ELT (result, i) =
8530 tsubst_expr (pattern, args, complain, in_decl,
8531 /*integral_constant_expression_p=*/false);
8532 else
8533 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8535 if (i == len)
8536 /* When we have incomplete argument packs, the last "expanded"
8537 result is itself a pack expansion, which allows us
8538 to deduce more arguments. */
8539 TREE_VEC_ELT (result, i) =
8540 make_pack_expansion (TREE_VEC_ELT (result, i));
8542 if (TREE_VEC_ELT (result, i) == error_mark_node)
8544 result = error_mark_node;
8545 break;
8549 /* Update ARGS to restore the substitution from parameter packs to
8550 their argument packs. */
8551 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8553 tree parm = TREE_PURPOSE (pack);
8555 if (TREE_CODE (parm) == PARM_DECL)
8556 register_local_specialization (TREE_TYPE (pack), parm);
8557 else
8559 int idx, level;
8560 template_parm_level_and_index (parm, &level, &idx);
8562 /* Update the corresponding argument. */
8563 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8564 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8565 TREE_TYPE (pack);
8566 else
8567 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8571 if (saved_local_specializations)
8573 htab_delete (local_specializations);
8574 local_specializations = saved_local_specializations;
8577 return result;
8580 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8581 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8582 parameter packs; all parms generated from a function parameter pack will
8583 have the same DECL_PARM_INDEX. */
8585 tree
8586 get_pattern_parm (tree parm, tree tmpl)
8588 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8589 tree patparm;
8591 if (DECL_ARTIFICIAL (parm))
8593 for (patparm = DECL_ARGUMENTS (pattern);
8594 patparm; patparm = DECL_CHAIN (patparm))
8595 if (DECL_ARTIFICIAL (patparm)
8596 && DECL_NAME (parm) == DECL_NAME (patparm))
8597 break;
8599 else
8601 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8602 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8603 gcc_assert (DECL_PARM_INDEX (patparm)
8604 == DECL_PARM_INDEX (parm));
8607 return patparm;
8610 /* Substitute ARGS into the vector or list of template arguments T. */
8612 static tree
8613 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8615 tree orig_t = t;
8616 int len = TREE_VEC_LENGTH (t);
8617 int need_new = 0, i, expanded_len_adjust = 0, out;
8618 tree *elts = XALLOCAVEC (tree, len);
8620 for (i = 0; i < len; i++)
8622 tree orig_arg = TREE_VEC_ELT (t, i);
8623 tree new_arg;
8625 if (TREE_CODE (orig_arg) == TREE_VEC)
8626 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8627 else if (PACK_EXPANSION_P (orig_arg))
8629 /* Substitute into an expansion expression. */
8630 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8632 if (TREE_CODE (new_arg) == TREE_VEC)
8633 /* Add to the expanded length adjustment the number of
8634 expanded arguments. We subtract one from this
8635 measurement, because the argument pack expression
8636 itself is already counted as 1 in
8637 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8638 the argument pack is empty. */
8639 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8641 else if (ARGUMENT_PACK_P (orig_arg))
8643 /* Substitute into each of the arguments. */
8644 new_arg = TYPE_P (orig_arg)
8645 ? cxx_make_type (TREE_CODE (orig_arg))
8646 : make_node (TREE_CODE (orig_arg));
8648 SET_ARGUMENT_PACK_ARGS (
8649 new_arg,
8650 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8651 args, complain, in_decl));
8653 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8654 new_arg = error_mark_node;
8656 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8657 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8658 complain, in_decl);
8659 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8661 if (TREE_TYPE (new_arg) == error_mark_node)
8662 new_arg = error_mark_node;
8665 else
8666 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8668 if (new_arg == error_mark_node)
8669 return error_mark_node;
8671 elts[i] = new_arg;
8672 if (new_arg != orig_arg)
8673 need_new = 1;
8676 if (!need_new)
8677 return t;
8679 /* Make space for the expanded arguments coming from template
8680 argument packs. */
8681 t = make_tree_vec (len + expanded_len_adjust);
8682 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8683 arguments for a member template.
8684 In that case each TREE_VEC in ORIG_T represents a level of template
8685 arguments, and ORIG_T won't carry any non defaulted argument count.
8686 It will rather be the nested TREE_VECs that will carry one.
8687 In other words, ORIG_T carries a non defaulted argument count only
8688 if it doesn't contain any nested TREE_VEC. */
8689 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
8691 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
8692 count += expanded_len_adjust;
8693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
8695 for (i = 0, out = 0; i < len; i++)
8697 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8698 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8699 && TREE_CODE (elts[i]) == TREE_VEC)
8701 int idx;
8703 /* Now expand the template argument pack "in place". */
8704 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8705 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8707 else
8709 TREE_VEC_ELT (t, out) = elts[i];
8710 out++;
8714 return t;
8717 /* Return the result of substituting ARGS into the template parameters
8718 given by PARMS. If there are m levels of ARGS and m + n levels of
8719 PARMS, then the result will contain n levels of PARMS. For
8720 example, if PARMS is `template <class T> template <class U>
8721 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8722 result will be `template <int*, double, class V>'. */
8724 static tree
8725 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8727 tree r = NULL_TREE;
8728 tree* new_parms;
8730 /* When substituting into a template, we must set
8731 PROCESSING_TEMPLATE_DECL as the template parameters may be
8732 dependent if they are based on one-another, and the dependency
8733 predicates are short-circuit outside of templates. */
8734 ++processing_template_decl;
8736 for (new_parms = &r;
8737 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8738 new_parms = &(TREE_CHAIN (*new_parms)),
8739 parms = TREE_CHAIN (parms))
8741 tree new_vec =
8742 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8743 int i;
8745 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8747 tree tuple;
8748 tree default_value;
8749 tree parm_decl;
8751 if (parms == error_mark_node)
8752 continue;
8754 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8756 if (tuple == error_mark_node)
8757 continue;
8759 default_value = TREE_PURPOSE (tuple);
8760 parm_decl = TREE_VALUE (tuple);
8762 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8763 if (TREE_CODE (parm_decl) == PARM_DECL
8764 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8765 parm_decl = error_mark_node;
8766 default_value = tsubst_template_arg (default_value, args,
8767 complain, NULL_TREE);
8769 tuple = build_tree_list (default_value, parm_decl);
8770 TREE_VEC_ELT (new_vec, i) = tuple;
8773 *new_parms =
8774 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8775 - TMPL_ARGS_DEPTH (args)),
8776 new_vec, NULL_TREE);
8779 --processing_template_decl;
8781 return r;
8784 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8785 type T. If T is not an aggregate or enumeration type, it is
8786 handled as if by tsubst. IN_DECL is as for tsubst. If
8787 ENTERING_SCOPE is nonzero, T is the context for a template which
8788 we are presently tsubst'ing. Return the substituted value. */
8790 static tree
8791 tsubst_aggr_type (tree t,
8792 tree args,
8793 tsubst_flags_t complain,
8794 tree in_decl,
8795 int entering_scope)
8797 if (t == NULL_TREE)
8798 return NULL_TREE;
8800 switch (TREE_CODE (t))
8802 case RECORD_TYPE:
8803 if (TYPE_PTRMEMFUNC_P (t))
8804 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8806 /* Else fall through. */
8807 case ENUMERAL_TYPE:
8808 case UNION_TYPE:
8809 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8811 tree argvec;
8812 tree context;
8813 tree r;
8814 int saved_unevaluated_operand;
8815 int saved_inhibit_evaluation_warnings;
8817 /* In "sizeof(X<I>)" we need to evaluate "I". */
8818 saved_unevaluated_operand = cp_unevaluated_operand;
8819 cp_unevaluated_operand = 0;
8820 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8821 c_inhibit_evaluation_warnings = 0;
8823 /* First, determine the context for the type we are looking
8824 up. */
8825 context = TYPE_CONTEXT (t);
8826 if (context)
8828 context = tsubst_aggr_type (context, args, complain,
8829 in_decl, /*entering_scope=*/1);
8830 /* If context is a nested class inside a class template,
8831 it may still need to be instantiated (c++/33959). */
8832 if (TYPE_P (context))
8833 context = complete_type (context);
8836 /* Then, figure out what arguments are appropriate for the
8837 type we are trying to find. For example, given:
8839 template <class T> struct S;
8840 template <class T, class U> void f(T, U) { S<U> su; }
8842 and supposing that we are instantiating f<int, double>,
8843 then our ARGS will be {int, double}, but, when looking up
8844 S we only want {double}. */
8845 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8846 complain, in_decl);
8847 if (argvec == error_mark_node)
8848 r = error_mark_node;
8849 else
8851 r = lookup_template_class (t, argvec, in_decl, context,
8852 entering_scope, complain);
8853 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
8856 cp_unevaluated_operand = saved_unevaluated_operand;
8857 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8859 return r;
8861 else
8862 /* This is not a template type, so there's nothing to do. */
8863 return t;
8865 default:
8866 return tsubst (t, args, complain, in_decl);
8870 /* Substitute into the default argument ARG (a default argument for
8871 FN), which has the indicated TYPE. */
8873 tree
8874 tsubst_default_argument (tree fn, tree type, tree arg)
8876 tree saved_class_ptr = NULL_TREE;
8877 tree saved_class_ref = NULL_TREE;
8879 /* This can happen in invalid code. */
8880 if (TREE_CODE (arg) == DEFAULT_ARG)
8881 return arg;
8883 /* This default argument came from a template. Instantiate the
8884 default argument here, not in tsubst. In the case of
8885 something like:
8887 template <class T>
8888 struct S {
8889 static T t();
8890 void f(T = t());
8893 we must be careful to do name lookup in the scope of S<T>,
8894 rather than in the current class. */
8895 push_access_scope (fn);
8896 /* The "this" pointer is not valid in a default argument. */
8897 if (cfun)
8899 saved_class_ptr = current_class_ptr;
8900 cp_function_chain->x_current_class_ptr = NULL_TREE;
8901 saved_class_ref = current_class_ref;
8902 cp_function_chain->x_current_class_ref = NULL_TREE;
8905 push_deferring_access_checks(dk_no_deferred);
8906 /* The default argument expression may cause implicitly defined
8907 member functions to be synthesized, which will result in garbage
8908 collection. We must treat this situation as if we were within
8909 the body of function so as to avoid collecting live data on the
8910 stack. */
8911 ++function_depth;
8912 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8913 tf_warning_or_error, NULL_TREE,
8914 /*integral_constant_expression_p=*/false);
8915 --function_depth;
8916 pop_deferring_access_checks();
8918 /* Restore the "this" pointer. */
8919 if (cfun)
8921 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8922 cp_function_chain->x_current_class_ref = saved_class_ref;
8925 /* Make sure the default argument is reasonable. */
8926 arg = check_default_argument (type, arg);
8928 pop_access_scope (fn);
8930 return arg;
8933 /* Substitute into all the default arguments for FN. */
8935 static void
8936 tsubst_default_arguments (tree fn)
8938 tree arg;
8939 tree tmpl_args;
8941 tmpl_args = DECL_TI_ARGS (fn);
8943 /* If this function is not yet instantiated, we certainly don't need
8944 its default arguments. */
8945 if (uses_template_parms (tmpl_args))
8946 return;
8947 /* Don't do this again for clones. */
8948 if (DECL_CLONED_FUNCTION_P (fn))
8949 return;
8951 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8952 arg;
8953 arg = TREE_CHAIN (arg))
8954 if (TREE_PURPOSE (arg))
8955 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8956 TREE_VALUE (arg),
8957 TREE_PURPOSE (arg));
8960 /* Substitute the ARGS into the T, which is a _DECL. Return the
8961 result of the substitution. Issue error and warning messages under
8962 control of COMPLAIN. */
8964 static tree
8965 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8967 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8968 location_t saved_loc;
8969 tree r = NULL_TREE;
8970 tree in_decl = t;
8971 hashval_t hash = 0;
8973 /* Set the filename and linenumber to improve error-reporting. */
8974 saved_loc = input_location;
8975 input_location = DECL_SOURCE_LOCATION (t);
8977 switch (TREE_CODE (t))
8979 case TEMPLATE_DECL:
8981 /* We can get here when processing a member function template,
8982 member class template, or template template parameter. */
8983 tree decl = DECL_TEMPLATE_RESULT (t);
8984 tree spec;
8985 tree tmpl_args;
8986 tree full_args;
8988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8990 /* Template template parameter is treated here. */
8991 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8992 if (new_type == error_mark_node)
8993 RETURN (error_mark_node);
8995 r = copy_decl (t);
8996 DECL_CHAIN (r) = NULL_TREE;
8997 TREE_TYPE (r) = new_type;
8998 DECL_TEMPLATE_RESULT (r)
8999 = build_decl (DECL_SOURCE_LOCATION (decl),
9000 TYPE_DECL, DECL_NAME (decl), new_type);
9001 DECL_TEMPLATE_PARMS (r)
9002 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9003 complain);
9004 TYPE_NAME (new_type) = r;
9005 break;
9008 /* We might already have an instance of this template.
9009 The ARGS are for the surrounding class type, so the
9010 full args contain the tsubst'd args for the context,
9011 plus the innermost args from the template decl. */
9012 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9013 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9014 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9015 /* Because this is a template, the arguments will still be
9016 dependent, even after substitution. If
9017 PROCESSING_TEMPLATE_DECL is not set, the dependency
9018 predicates will short-circuit. */
9019 ++processing_template_decl;
9020 full_args = tsubst_template_args (tmpl_args, args,
9021 complain, in_decl);
9022 --processing_template_decl;
9023 if (full_args == error_mark_node)
9024 RETURN (error_mark_node);
9026 /* If this is a default template template argument,
9027 tsubst might not have changed anything. */
9028 if (full_args == tmpl_args)
9029 RETURN (t);
9031 hash = hash_tmpl_and_args (t, full_args);
9032 spec = retrieve_specialization (t, full_args, hash);
9033 if (spec != NULL_TREE)
9035 r = spec;
9036 break;
9039 /* Make a new template decl. It will be similar to the
9040 original, but will record the current template arguments.
9041 We also create a new function declaration, which is just
9042 like the old one, but points to this new template, rather
9043 than the old one. */
9044 r = copy_decl (t);
9045 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9046 DECL_CHAIN (r) = NULL_TREE;
9048 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9050 if (TREE_CODE (decl) == TYPE_DECL)
9052 tree new_type;
9053 ++processing_template_decl;
9054 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9055 --processing_template_decl;
9056 if (new_type == error_mark_node)
9057 RETURN (error_mark_node);
9059 TREE_TYPE (r) = new_type;
9060 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9061 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9062 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9063 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9065 else
9067 tree new_decl;
9068 ++processing_template_decl;
9069 new_decl = tsubst (decl, args, complain, in_decl);
9070 --processing_template_decl;
9071 if (new_decl == error_mark_node)
9072 RETURN (error_mark_node);
9074 DECL_TEMPLATE_RESULT (r) = new_decl;
9075 DECL_TI_TEMPLATE (new_decl) = r;
9076 TREE_TYPE (r) = TREE_TYPE (new_decl);
9077 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9078 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9081 SET_DECL_IMPLICIT_INSTANTIATION (r);
9082 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9083 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9085 /* The template parameters for this new template are all the
9086 template parameters for the old template, except the
9087 outermost level of parameters. */
9088 DECL_TEMPLATE_PARMS (r)
9089 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9090 complain);
9092 if (PRIMARY_TEMPLATE_P (t))
9093 DECL_PRIMARY_TEMPLATE (r) = r;
9095 if (TREE_CODE (decl) != TYPE_DECL)
9096 /* Record this non-type partial instantiation. */
9097 register_specialization (r, t,
9098 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9099 false, hash);
9101 break;
9103 case FUNCTION_DECL:
9105 tree ctx;
9106 tree argvec = NULL_TREE;
9107 tree *friends;
9108 tree gen_tmpl;
9109 tree type;
9110 int member;
9111 int args_depth;
9112 int parms_depth;
9114 /* Nobody should be tsubst'ing into non-template functions. */
9115 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9117 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9119 tree spec;
9120 bool dependent_p;
9122 /* If T is not dependent, just return it. We have to
9123 increment PROCESSING_TEMPLATE_DECL because
9124 value_dependent_expression_p assumes that nothing is
9125 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9126 ++processing_template_decl;
9127 dependent_p = value_dependent_expression_p (t);
9128 --processing_template_decl;
9129 if (!dependent_p)
9130 RETURN (t);
9132 /* Calculate the most general template of which R is a
9133 specialization, and the complete set of arguments used to
9134 specialize R. */
9135 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9136 argvec = tsubst_template_args (DECL_TI_ARGS
9137 (DECL_TEMPLATE_RESULT
9138 (DECL_TI_TEMPLATE (t))),
9139 args, complain, in_decl);
9141 /* Check to see if we already have this specialization. */
9142 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9143 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9145 if (spec)
9147 r = spec;
9148 break;
9151 /* We can see more levels of arguments than parameters if
9152 there was a specialization of a member template, like
9153 this:
9155 template <class T> struct S { template <class U> void f(); }
9156 template <> template <class U> void S<int>::f(U);
9158 Here, we'll be substituting into the specialization,
9159 because that's where we can find the code we actually
9160 want to generate, but we'll have enough arguments for
9161 the most general template.
9163 We also deal with the peculiar case:
9165 template <class T> struct S {
9166 template <class U> friend void f();
9168 template <class U> void f() {}
9169 template S<int>;
9170 template void f<double>();
9172 Here, the ARGS for the instantiation of will be {int,
9173 double}. But, we only need as many ARGS as there are
9174 levels of template parameters in CODE_PATTERN. We are
9175 careful not to get fooled into reducing the ARGS in
9176 situations like:
9178 template <class T> struct S { template <class U> void f(U); }
9179 template <class T> template <> void S<T>::f(int) {}
9181 which we can spot because the pattern will be a
9182 specialization in this case. */
9183 args_depth = TMPL_ARGS_DEPTH (args);
9184 parms_depth =
9185 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9186 if (args_depth > parms_depth
9187 && !DECL_TEMPLATE_SPECIALIZATION (t))
9188 args = get_innermost_template_args (args, parms_depth);
9190 else
9192 /* This special case arises when we have something like this:
9194 template <class T> struct S {
9195 friend void f<int>(int, double);
9198 Here, the DECL_TI_TEMPLATE for the friend declaration
9199 will be an IDENTIFIER_NODE. We are being called from
9200 tsubst_friend_function, and we want only to create a
9201 new decl (R) with appropriate types so that we can call
9202 determine_specialization. */
9203 gen_tmpl = NULL_TREE;
9206 if (DECL_CLASS_SCOPE_P (t))
9208 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9209 member = 2;
9210 else
9211 member = 1;
9212 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9213 complain, t, /*entering_scope=*/1);
9215 else
9217 member = 0;
9218 ctx = DECL_CONTEXT (t);
9220 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9221 if (type == error_mark_node)
9222 RETURN (error_mark_node);
9224 /* We do NOT check for matching decls pushed separately at this
9225 point, as they may not represent instantiations of this
9226 template, and in any case are considered separate under the
9227 discrete model. */
9228 r = copy_decl (t);
9229 DECL_USE_TEMPLATE (r) = 0;
9230 TREE_TYPE (r) = type;
9231 /* Clear out the mangled name and RTL for the instantiation. */
9232 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9233 SET_DECL_RTL (r, NULL);
9234 /* Leave DECL_INITIAL set on deleted instantiations. */
9235 if (!DECL_DELETED_FN (r))
9236 DECL_INITIAL (r) = NULL_TREE;
9237 DECL_CONTEXT (r) = ctx;
9239 if (member && DECL_CONV_FN_P (r))
9240 /* Type-conversion operator. Reconstruct the name, in
9241 case it's the name of one of the template's parameters. */
9242 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9244 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9245 complain, t);
9246 DECL_RESULT (r) = NULL_TREE;
9248 TREE_STATIC (r) = 0;
9249 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9250 DECL_EXTERNAL (r) = 1;
9251 /* If this is an instantiation of a function with internal
9252 linkage, we already know what object file linkage will be
9253 assigned to the instantiation. */
9254 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9255 DECL_DEFER_OUTPUT (r) = 0;
9256 DECL_CHAIN (r) = NULL_TREE;
9257 DECL_PENDING_INLINE_INFO (r) = 0;
9258 DECL_PENDING_INLINE_P (r) = 0;
9259 DECL_SAVED_TREE (r) = NULL_TREE;
9260 DECL_STRUCT_FUNCTION (r) = NULL;
9261 TREE_USED (r) = 0;
9262 /* We'll re-clone as appropriate in instantiate_template. */
9263 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9265 /* If we aren't complaining now, return on error before we register
9266 the specialization so that we'll complain eventually. */
9267 if ((complain & tf_error) == 0
9268 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9269 && !grok_op_properties (r, /*complain=*/false))
9270 RETURN (error_mark_node);
9272 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9273 this in the special friend case mentioned above where
9274 GEN_TMPL is NULL. */
9275 if (gen_tmpl)
9277 DECL_TEMPLATE_INFO (r)
9278 = build_template_info (gen_tmpl, argvec);
9279 SET_DECL_IMPLICIT_INSTANTIATION (r);
9280 register_specialization (r, gen_tmpl, argvec, false, hash);
9282 /* We're not supposed to instantiate default arguments
9283 until they are called, for a template. But, for a
9284 declaration like:
9286 template <class T> void f ()
9287 { extern void g(int i = T()); }
9289 we should do the substitution when the template is
9290 instantiated. We handle the member function case in
9291 instantiate_class_template since the default arguments
9292 might refer to other members of the class. */
9293 if (!member
9294 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9295 && !uses_template_parms (argvec))
9296 tsubst_default_arguments (r);
9298 else
9299 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9301 /* Copy the list of befriending classes. */
9302 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9303 *friends;
9304 friends = &TREE_CHAIN (*friends))
9306 *friends = copy_node (*friends);
9307 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9308 args, complain,
9309 in_decl);
9312 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9314 maybe_retrofit_in_chrg (r);
9315 if (DECL_CONSTRUCTOR_P (r))
9316 grok_ctor_properties (ctx, r);
9317 /* If this is an instantiation of a member template, clone it.
9318 If it isn't, that'll be handled by
9319 clone_constructors_and_destructors. */
9320 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9321 clone_function_decl (r, /*update_method_vec_p=*/0);
9323 else if ((complain & tf_error) != 0
9324 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9325 && !grok_op_properties (r, /*complain=*/true))
9326 RETURN (error_mark_node);
9328 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9329 SET_DECL_FRIEND_CONTEXT (r,
9330 tsubst (DECL_FRIEND_CONTEXT (t),
9331 args, complain, in_decl));
9333 /* Possibly limit visibility based on template args. */
9334 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9335 if (DECL_VISIBILITY_SPECIFIED (t))
9337 DECL_VISIBILITY_SPECIFIED (r) = 0;
9338 DECL_ATTRIBUTES (r)
9339 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9341 determine_visibility (r);
9342 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9343 && !processing_template_decl)
9344 defaulted_late_check (r);
9346 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9347 args, complain, in_decl);
9349 break;
9351 case PARM_DECL:
9353 tree type = NULL_TREE;
9354 int i, len = 1;
9355 tree expanded_types = NULL_TREE;
9356 tree prev_r = NULL_TREE;
9357 tree first_r = NULL_TREE;
9359 if (FUNCTION_PARAMETER_PACK_P (t))
9361 /* If there is a local specialization that isn't a
9362 parameter pack, it means that we're doing a "simple"
9363 substitution from inside tsubst_pack_expansion. Just
9364 return the local specialization (which will be a single
9365 parm). */
9366 tree spec = retrieve_local_specialization (t);
9367 if (spec
9368 && TREE_CODE (spec) == PARM_DECL
9369 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9370 RETURN (spec);
9372 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9373 the parameters in this function parameter pack. */
9374 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9375 complain, in_decl);
9376 if (TREE_CODE (expanded_types) == TREE_VEC)
9378 len = TREE_VEC_LENGTH (expanded_types);
9380 /* Zero-length parameter packs are boring. Just substitute
9381 into the chain. */
9382 if (len == 0)
9383 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9384 TREE_CHAIN (t)));
9386 else
9388 /* All we did was update the type. Make a note of that. */
9389 type = expanded_types;
9390 expanded_types = NULL_TREE;
9394 /* Loop through all of the parameter's we'll build. When T is
9395 a function parameter pack, LEN is the number of expanded
9396 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9397 r = NULL_TREE;
9398 for (i = 0; i < len; ++i)
9400 prev_r = r;
9401 r = copy_node (t);
9402 if (DECL_TEMPLATE_PARM_P (t))
9403 SET_DECL_TEMPLATE_PARM_P (r);
9405 /* An argument of a function parameter pack is not a parameter
9406 pack. */
9407 FUNCTION_PARAMETER_PACK_P (r) = false;
9409 if (expanded_types)
9410 /* We're on the Ith parameter of the function parameter
9411 pack. */
9413 /* Get the Ith type. */
9414 type = TREE_VEC_ELT (expanded_types, i);
9416 if (DECL_NAME (r))
9417 /* Rename the parameter to include the index. */
9418 DECL_NAME (r) =
9419 make_ith_pack_parameter_name (DECL_NAME (r), i);
9421 else if (!type)
9422 /* We're dealing with a normal parameter. */
9423 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9425 type = type_decays_to (type);
9426 TREE_TYPE (r) = type;
9427 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9429 if (DECL_INITIAL (r))
9431 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9432 DECL_INITIAL (r) = TREE_TYPE (r);
9433 else
9434 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9435 complain, in_decl);
9438 DECL_CONTEXT (r) = NULL_TREE;
9440 if (!DECL_TEMPLATE_PARM_P (r))
9441 DECL_ARG_TYPE (r) = type_passed_as (type);
9443 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9444 args, complain, in_decl);
9446 /* Keep track of the first new parameter we
9447 generate. That's what will be returned to the
9448 caller. */
9449 if (!first_r)
9450 first_r = r;
9452 /* Build a proper chain of parameters when substituting
9453 into a function parameter pack. */
9454 if (prev_r)
9455 DECL_CHAIN (prev_r) = r;
9458 if (DECL_CHAIN (t))
9459 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9460 complain, DECL_CHAIN (t));
9462 /* FIRST_R contains the start of the chain we've built. */
9463 r = first_r;
9465 break;
9467 case FIELD_DECL:
9469 tree type;
9471 r = copy_decl (t);
9472 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9473 if (type == error_mark_node)
9474 RETURN (error_mark_node);
9475 TREE_TYPE (r) = type;
9476 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9478 /* DECL_INITIAL gives the number of bits in a bit-field. */
9479 DECL_INITIAL (r)
9480 = tsubst_expr (DECL_INITIAL (t), args,
9481 complain, in_decl,
9482 /*integral_constant_expression_p=*/true);
9483 /* We don't have to set DECL_CONTEXT here; it is set by
9484 finish_member_declaration. */
9485 DECL_CHAIN (r) = NULL_TREE;
9486 if (VOID_TYPE_P (type))
9487 error ("instantiation of %q+D as type %qT", r, type);
9489 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9490 args, complain, in_decl);
9492 break;
9494 case USING_DECL:
9495 /* We reach here only for member using decls. */
9496 if (DECL_DEPENDENT_P (t))
9498 r = do_class_using_decl
9499 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9500 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9501 if (!r)
9502 r = error_mark_node;
9503 else
9505 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9506 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9509 else
9511 r = copy_node (t);
9512 DECL_CHAIN (r) = NULL_TREE;
9514 break;
9516 case TYPE_DECL:
9517 case VAR_DECL:
9519 tree argvec = NULL_TREE;
9520 tree gen_tmpl = NULL_TREE;
9521 tree spec;
9522 tree tmpl = NULL_TREE;
9523 tree ctx;
9524 tree type = NULL_TREE;
9525 bool local_p;
9527 if (TREE_CODE (t) == TYPE_DECL
9528 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9530 /* If this is the canonical decl, we don't have to
9531 mess with instantiations, and often we can't (for
9532 typename, template type parms and such). Note that
9533 TYPE_NAME is not correct for the above test if
9534 we've copied the type for a typedef. */
9535 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9536 if (type == error_mark_node)
9537 RETURN (error_mark_node);
9538 r = TYPE_NAME (type);
9539 break;
9542 /* Check to see if we already have the specialization we
9543 need. */
9544 spec = NULL_TREE;
9545 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9547 /* T is a static data member or namespace-scope entity.
9548 We have to substitute into namespace-scope variables
9549 (even though such entities are never templates) because
9550 of cases like:
9552 template <class T> void f() { extern T t; }
9554 where the entity referenced is not known until
9555 instantiation time. */
9556 local_p = false;
9557 ctx = DECL_CONTEXT (t);
9558 if (DECL_CLASS_SCOPE_P (t))
9560 ctx = tsubst_aggr_type (ctx, args,
9561 complain,
9562 in_decl, /*entering_scope=*/1);
9563 /* If CTX is unchanged, then T is in fact the
9564 specialization we want. That situation occurs when
9565 referencing a static data member within in its own
9566 class. We can use pointer equality, rather than
9567 same_type_p, because DECL_CONTEXT is always
9568 canonical. */
9569 if (ctx == DECL_CONTEXT (t))
9570 spec = t;
9573 if (!spec)
9575 tmpl = DECL_TI_TEMPLATE (t);
9576 gen_tmpl = most_general_template (tmpl);
9577 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9578 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9579 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9582 else
9584 /* A local variable. */
9585 local_p = true;
9586 /* Subsequent calls to pushdecl will fill this in. */
9587 ctx = NULL_TREE;
9588 spec = retrieve_local_specialization (t);
9590 /* If we already have the specialization we need, there is
9591 nothing more to do. */
9592 if (spec)
9594 r = spec;
9595 break;
9598 /* Create a new node for the specialization we need. */
9599 r = copy_decl (t);
9600 if (type == NULL_TREE)
9602 if (is_typedef_decl (t))
9603 type = DECL_ORIGINAL_TYPE (t);
9604 else
9605 type = TREE_TYPE (t);
9606 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9607 type = strip_array_domain (type);
9608 type = tsubst (type, args, complain, in_decl);
9610 if (TREE_CODE (r) == VAR_DECL)
9612 /* Even if the original location is out of scope, the
9613 newly substituted one is not. */
9614 DECL_DEAD_FOR_LOCAL (r) = 0;
9615 DECL_INITIALIZED_P (r) = 0;
9616 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9617 if (type == error_mark_node)
9618 RETURN (error_mark_node);
9619 if (TREE_CODE (type) == FUNCTION_TYPE)
9621 /* It may seem that this case cannot occur, since:
9623 typedef void f();
9624 void g() { f x; }
9626 declares a function, not a variable. However:
9628 typedef void f();
9629 template <typename T> void g() { T t; }
9630 template void g<f>();
9632 is an attempt to declare a variable with function
9633 type. */
9634 error ("variable %qD has function type",
9635 /* R is not yet sufficiently initialized, so we
9636 just use its name. */
9637 DECL_NAME (r));
9638 RETURN (error_mark_node);
9640 type = complete_type (type);
9641 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9642 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9643 type = check_var_type (DECL_NAME (r), type);
9645 if (DECL_HAS_VALUE_EXPR_P (t))
9647 tree ve = DECL_VALUE_EXPR (t);
9648 ve = tsubst_expr (ve, args, complain, in_decl,
9649 /*constant_expression_p=*/false);
9650 SET_DECL_VALUE_EXPR (r, ve);
9653 else if (DECL_SELF_REFERENCE_P (t))
9654 SET_DECL_SELF_REFERENCE_P (r);
9655 TREE_TYPE (r) = type;
9656 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9657 DECL_CONTEXT (r) = ctx;
9658 /* Clear out the mangled name and RTL for the instantiation. */
9659 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9660 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9661 SET_DECL_RTL (r, NULL);
9662 /* The initializer must not be expanded until it is required;
9663 see [temp.inst]. */
9664 DECL_INITIAL (r) = NULL_TREE;
9665 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9666 SET_DECL_RTL (r, NULL);
9667 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9668 if (TREE_CODE (r) == VAR_DECL)
9670 /* Possibly limit visibility based on template args. */
9671 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9672 if (DECL_VISIBILITY_SPECIFIED (t))
9674 DECL_VISIBILITY_SPECIFIED (r) = 0;
9675 DECL_ATTRIBUTES (r)
9676 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9678 determine_visibility (r);
9681 if (!local_p)
9683 /* A static data member declaration is always marked
9684 external when it is declared in-class, even if an
9685 initializer is present. We mimic the non-template
9686 processing here. */
9687 DECL_EXTERNAL (r) = 1;
9689 register_specialization (r, gen_tmpl, argvec, false, hash);
9690 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9691 SET_DECL_IMPLICIT_INSTANTIATION (r);
9693 else if (cp_unevaluated_operand)
9695 /* We're substituting this var in a decltype outside of its
9696 scope, such as for a lambda return type. Don't add it to
9697 local_specializations, do perform auto deduction. */
9698 tree auto_node = type_uses_auto (type);
9699 tree init
9700 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9701 /*constant_expression_p=*/false);
9703 if (auto_node && init && describable_type (init))
9705 type = do_auto_deduction (type, init, auto_node);
9706 TREE_TYPE (r) = type;
9709 else
9710 register_local_specialization (r, t);
9712 DECL_CHAIN (r) = NULL_TREE;
9714 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9715 /*flags=*/0,
9716 args, complain, in_decl);
9718 /* Preserve a typedef that names a type. */
9719 if (is_typedef_decl (r))
9721 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9722 set_underlying_type (r);
9725 layout_decl (r, 0);
9727 break;
9729 default:
9730 gcc_unreachable ();
9732 #undef RETURN
9734 out:
9735 /* Restore the file and line information. */
9736 input_location = saved_loc;
9738 return r;
9741 /* Substitute into the ARG_TYPES of a function type. */
9743 static tree
9744 tsubst_arg_types (tree arg_types,
9745 tree args,
9746 tsubst_flags_t complain,
9747 tree in_decl)
9749 tree remaining_arg_types;
9750 tree type = NULL_TREE;
9751 int i = 1;
9752 tree expanded_args = NULL_TREE;
9753 tree default_arg;
9755 if (!arg_types || arg_types == void_list_node)
9756 return arg_types;
9758 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9759 args, complain, in_decl);
9760 if (remaining_arg_types == error_mark_node)
9761 return error_mark_node;
9763 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9765 /* For a pack expansion, perform substitution on the
9766 entire expression. Later on, we'll handle the arguments
9767 one-by-one. */
9768 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9769 args, complain, in_decl);
9771 if (TREE_CODE (expanded_args) == TREE_VEC)
9772 /* So that we'll spin through the parameters, one by one. */
9773 i = TREE_VEC_LENGTH (expanded_args);
9774 else
9776 /* We only partially substituted into the parameter
9777 pack. Our type is TYPE_PACK_EXPANSION. */
9778 type = expanded_args;
9779 expanded_args = NULL_TREE;
9783 while (i > 0) {
9784 --i;
9786 if (expanded_args)
9787 type = TREE_VEC_ELT (expanded_args, i);
9788 else if (!type)
9789 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9791 if (type == error_mark_node)
9792 return error_mark_node;
9793 if (VOID_TYPE_P (type))
9795 if (complain & tf_error)
9797 error ("invalid parameter type %qT", type);
9798 if (in_decl)
9799 error ("in declaration %q+D", in_decl);
9801 return error_mark_node;
9804 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9805 top-level qualifiers as required. */
9806 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9808 /* We do not substitute into default arguments here. The standard
9809 mandates that they be instantiated only when needed, which is
9810 done in build_over_call. */
9811 default_arg = TREE_PURPOSE (arg_types);
9813 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9815 /* We've instantiated a template before its default arguments
9816 have been parsed. This can happen for a nested template
9817 class, and is not an error unless we require the default
9818 argument in a call of this function. */
9819 remaining_arg_types =
9820 tree_cons (default_arg, type, remaining_arg_types);
9821 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9822 remaining_arg_types);
9824 else
9825 remaining_arg_types =
9826 hash_tree_cons (default_arg, type, remaining_arg_types);
9829 return remaining_arg_types;
9832 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9833 *not* handle the exception-specification for FNTYPE, because the
9834 initial substitution of explicitly provided template parameters
9835 during argument deduction forbids substitution into the
9836 exception-specification:
9838 [temp.deduct]
9840 All references in the function type of the function template to the
9841 corresponding template parameters are replaced by the specified tem-
9842 plate argument values. If a substitution in a template parameter or
9843 in the function type of the function template results in an invalid
9844 type, type deduction fails. [Note: The equivalent substitution in
9845 exception specifications is done only when the function is instanti-
9846 ated, at which point a program is ill-formed if the substitution
9847 results in an invalid type.] */
9849 static tree
9850 tsubst_function_type (tree t,
9851 tree args,
9852 tsubst_flags_t complain,
9853 tree in_decl)
9855 tree return_type;
9856 tree arg_types;
9857 tree fntype;
9859 /* The TYPE_CONTEXT is not used for function/method types. */
9860 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9862 /* Substitute the return type. */
9863 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9864 if (return_type == error_mark_node)
9865 return error_mark_node;
9866 /* The standard does not presently indicate that creation of a
9867 function type with an invalid return type is a deduction failure.
9868 However, that is clearly analogous to creating an array of "void"
9869 or a reference to a reference. This is core issue #486. */
9870 if (TREE_CODE (return_type) == ARRAY_TYPE
9871 || TREE_CODE (return_type) == FUNCTION_TYPE)
9873 if (complain & tf_error)
9875 if (TREE_CODE (return_type) == ARRAY_TYPE)
9876 error ("function returning an array");
9877 else
9878 error ("function returning a function");
9880 return error_mark_node;
9883 /* Substitute the argument types. */
9884 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9885 complain, in_decl);
9886 if (arg_types == error_mark_node)
9887 return error_mark_node;
9889 /* Construct a new type node and return it. */
9890 if (TREE_CODE (t) == FUNCTION_TYPE)
9892 fntype = build_function_type (return_type, arg_types);
9893 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
9895 else
9897 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9898 if (! MAYBE_CLASS_TYPE_P (r))
9900 /* [temp.deduct]
9902 Type deduction may fail for any of the following
9903 reasons:
9905 -- Attempting to create "pointer to member of T" when T
9906 is not a class type. */
9907 if (complain & tf_error)
9908 error ("creating pointer to member function of non-class type %qT",
9910 return error_mark_node;
9913 fntype = build_method_type_directly (r, return_type,
9914 TREE_CHAIN (arg_types));
9916 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9918 return fntype;
9921 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9922 ARGS into that specification, and return the substituted
9923 specification. If there is no specification, return NULL_TREE. */
9925 static tree
9926 tsubst_exception_specification (tree fntype,
9927 tree args,
9928 tsubst_flags_t complain,
9929 tree in_decl)
9931 tree specs;
9932 tree new_specs;
9934 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9935 new_specs = NULL_TREE;
9936 if (specs && TREE_PURPOSE (specs))
9938 /* A noexcept-specifier. */
9939 new_specs = tsubst_copy_and_build
9940 (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false,
9941 /*integral_constant_expression_p=*/true);
9942 new_specs = build_noexcept_spec (new_specs, complain);
9944 else if (specs)
9946 if (! TREE_VALUE (specs))
9947 new_specs = specs;
9948 else
9949 while (specs)
9951 tree spec;
9952 int i, len = 1;
9953 tree expanded_specs = NULL_TREE;
9955 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9957 /* Expand the pack expansion type. */
9958 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9959 args, complain,
9960 in_decl);
9962 if (expanded_specs == error_mark_node)
9963 return error_mark_node;
9964 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9965 len = TREE_VEC_LENGTH (expanded_specs);
9966 else
9968 /* We're substituting into a member template, so
9969 we got a TYPE_PACK_EXPANSION back. Add that
9970 expansion and move on. */
9971 gcc_assert (TREE_CODE (expanded_specs)
9972 == TYPE_PACK_EXPANSION);
9973 new_specs = add_exception_specifier (new_specs,
9974 expanded_specs,
9975 complain);
9976 specs = TREE_CHAIN (specs);
9977 continue;
9981 for (i = 0; i < len; ++i)
9983 if (expanded_specs)
9984 spec = TREE_VEC_ELT (expanded_specs, i);
9985 else
9986 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9987 if (spec == error_mark_node)
9988 return spec;
9989 new_specs = add_exception_specifier (new_specs, spec,
9990 complain);
9993 specs = TREE_CHAIN (specs);
9996 return new_specs;
9999 /* Take the tree structure T and replace template parameters used
10000 therein with the argument vector ARGS. IN_DECL is an associated
10001 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10002 Issue error and warning messages under control of COMPLAIN. Note
10003 that we must be relatively non-tolerant of extensions here, in
10004 order to preserve conformance; if we allow substitutions that
10005 should not be allowed, we may allow argument deductions that should
10006 not succeed, and therefore report ambiguous overload situations
10007 where there are none. In theory, we could allow the substitution,
10008 but indicate that it should have failed, and allow our caller to
10009 make sure that the right thing happens, but we don't try to do this
10010 yet.
10012 This function is used for dealing with types, decls and the like;
10013 for expressions, use tsubst_expr or tsubst_copy. */
10015 tree
10016 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10018 enum tree_code code;
10019 tree type, r;
10021 if (t == NULL_TREE || t == error_mark_node
10022 || t == integer_type_node
10023 || t == void_type_node
10024 || t == char_type_node
10025 || t == unknown_type_node
10026 || TREE_CODE (t) == NAMESPACE_DECL
10027 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10028 return t;
10030 if (DECL_P (t))
10031 return tsubst_decl (t, args, complain);
10033 if (args == NULL_TREE)
10034 return t;
10036 code = TREE_CODE (t);
10038 if (code == IDENTIFIER_NODE)
10039 type = IDENTIFIER_TYPE_VALUE (t);
10040 else
10041 type = TREE_TYPE (t);
10043 gcc_assert (type != unknown_type_node);
10045 /* Reuse typedefs. We need to do this to handle dependent attributes,
10046 such as attribute aligned. */
10047 if (TYPE_P (t)
10048 && typedef_variant_p (t))
10050 tree decl = TYPE_NAME (t);
10052 if (DECL_CLASS_SCOPE_P (decl)
10053 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10054 && uses_template_parms (DECL_CONTEXT (decl)))
10056 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10057 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10058 r = retrieve_specialization (tmpl, gen_args, 0);
10060 else if (DECL_FUNCTION_SCOPE_P (decl)
10061 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10062 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10063 r = retrieve_local_specialization (decl);
10064 else
10065 /* The typedef is from a non-template context. */
10066 return t;
10068 if (r)
10070 r = TREE_TYPE (r);
10071 r = cp_build_qualified_type_real
10072 (r, cp_type_quals (t) | cp_type_quals (r),
10073 complain | tf_ignore_bad_quals);
10074 return r;
10076 /* Else we must be instantiating the typedef, so fall through. */
10079 if (type
10080 && code != TYPENAME_TYPE
10081 && code != TEMPLATE_TYPE_PARM
10082 && code != IDENTIFIER_NODE
10083 && code != FUNCTION_TYPE
10084 && code != METHOD_TYPE)
10085 type = tsubst (type, args, complain, in_decl);
10086 if (type == error_mark_node)
10087 return error_mark_node;
10089 switch (code)
10091 case RECORD_TYPE:
10092 case UNION_TYPE:
10093 case ENUMERAL_TYPE:
10094 return tsubst_aggr_type (t, args, complain, in_decl,
10095 /*entering_scope=*/0);
10097 case ERROR_MARK:
10098 case IDENTIFIER_NODE:
10099 case VOID_TYPE:
10100 case REAL_TYPE:
10101 case COMPLEX_TYPE:
10102 case VECTOR_TYPE:
10103 case BOOLEAN_TYPE:
10104 case NULLPTR_TYPE:
10105 case LANG_TYPE:
10106 return t;
10108 case INTEGER_TYPE:
10109 if (t == integer_type_node)
10110 return t;
10112 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10113 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10114 return t;
10117 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10119 max = tsubst_expr (omax, args, complain, in_decl,
10120 /*integral_constant_expression_p=*/false);
10122 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10123 needed. */
10124 if (TREE_CODE (max) == NOP_EXPR
10125 && TREE_SIDE_EFFECTS (omax)
10126 && !TREE_TYPE (max))
10127 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10129 max = mark_rvalue_use (max);
10130 max = fold_decl_constant_value (max);
10132 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10133 with TREE_SIDE_EFFECTS that indicates this is not an integral
10134 constant expression. */
10135 if (processing_template_decl
10136 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10138 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10139 TREE_SIDE_EFFECTS (max) = 1;
10142 if (TREE_CODE (max) != INTEGER_CST
10143 && !at_function_scope_p ()
10144 && !TREE_SIDE_EFFECTS (max)
10145 && !value_dependent_expression_p (max))
10147 if (complain & tf_error)
10148 error ("array bound is not an integer constant");
10149 return error_mark_node;
10152 /* [temp.deduct]
10154 Type deduction may fail for any of the following
10155 reasons:
10157 Attempting to create an array with a size that is
10158 zero or negative. */
10159 if (integer_zerop (max) && !(complain & tf_error))
10160 /* We must fail if performing argument deduction (as
10161 indicated by the state of complain), so that
10162 another substitution can be found. */
10163 return error_mark_node;
10164 else if (TREE_CODE (max) == INTEGER_CST
10165 && INT_CST_LT (max, integer_zero_node))
10167 if (complain & tf_error)
10168 error ("creating array with negative size (%qE)", max);
10170 return error_mark_node;
10173 return compute_array_index_type (NULL_TREE, max);
10176 case TEMPLATE_TYPE_PARM:
10177 case TEMPLATE_TEMPLATE_PARM:
10178 case BOUND_TEMPLATE_TEMPLATE_PARM:
10179 case TEMPLATE_PARM_INDEX:
10181 int idx;
10182 int level;
10183 int levels;
10184 tree arg = NULL_TREE;
10186 r = NULL_TREE;
10188 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10189 template_parm_level_and_index (t, &level, &idx);
10191 levels = TMPL_ARGS_DEPTH (args);
10192 if (level <= levels)
10194 arg = TMPL_ARG (args, level, idx);
10196 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10197 /* See through ARGUMENT_PACK_SELECT arguments. */
10198 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10201 if (arg == error_mark_node)
10202 return error_mark_node;
10203 else if (arg != NULL_TREE)
10205 if (ARGUMENT_PACK_P (arg))
10206 /* If ARG is an argument pack, we don't actually want to
10207 perform a substitution here, because substitutions
10208 for argument packs are only done
10209 element-by-element. We can get to this point when
10210 substituting the type of a non-type template
10211 parameter pack, when that type actually contains
10212 template parameter packs from an outer template, e.g.,
10214 template<typename... Types> struct A {
10215 template<Types... Values> struct B { };
10216 }; */
10217 return t;
10219 if (code == TEMPLATE_TYPE_PARM)
10221 int quals;
10222 gcc_assert (TYPE_P (arg));
10224 quals = cp_type_quals (arg) | cp_type_quals (t);
10226 return cp_build_qualified_type_real
10227 (arg, quals, complain | tf_ignore_bad_quals);
10229 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10231 /* We are processing a type constructed from a
10232 template template parameter. */
10233 tree argvec = tsubst (TYPE_TI_ARGS (t),
10234 args, complain, in_decl);
10235 if (argvec == error_mark_node)
10236 return error_mark_node;
10238 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10239 are resolving nested-types in the signature of a
10240 member function templates. Otherwise ARG is a
10241 TEMPLATE_DECL and is the real template to be
10242 instantiated. */
10243 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10244 arg = TYPE_NAME (arg);
10246 r = lookup_template_class (arg,
10247 argvec, in_decl,
10248 DECL_CONTEXT (arg),
10249 /*entering_scope=*/0,
10250 complain);
10251 return cp_build_qualified_type_real
10252 (r, cp_type_quals (t), complain);
10254 else
10255 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10256 return unshare_expr (arg);
10259 if (level == 1)
10260 /* This can happen during the attempted tsubst'ing in
10261 unify. This means that we don't yet have any information
10262 about the template parameter in question. */
10263 return t;
10265 /* If we get here, we must have been looking at a parm for a
10266 more deeply nested template. Make a new version of this
10267 template parameter, but with a lower level. */
10268 switch (code)
10270 case TEMPLATE_TYPE_PARM:
10271 case TEMPLATE_TEMPLATE_PARM:
10272 case BOUND_TEMPLATE_TEMPLATE_PARM:
10273 if (cp_type_quals (t))
10275 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10276 r = cp_build_qualified_type_real
10277 (r, cp_type_quals (t),
10278 complain | (code == TEMPLATE_TYPE_PARM
10279 ? tf_ignore_bad_quals : 0));
10281 else
10283 r = copy_type (t);
10284 TEMPLATE_TYPE_PARM_INDEX (r)
10285 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10286 r, levels, args, complain);
10287 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10288 TYPE_MAIN_VARIANT (r) = r;
10289 TYPE_POINTER_TO (r) = NULL_TREE;
10290 TYPE_REFERENCE_TO (r) = NULL_TREE;
10292 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10293 /* We have reduced the level of the template
10294 template parameter, but not the levels of its
10295 template parameters, so canonical_type_parameter
10296 will not be able to find the canonical template
10297 template parameter for this level. Thus, we
10298 require structural equality checking to compare
10299 TEMPLATE_TEMPLATE_PARMs. */
10300 SET_TYPE_STRUCTURAL_EQUALITY (r);
10301 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10302 SET_TYPE_STRUCTURAL_EQUALITY (r);
10303 else
10304 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10306 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10308 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10309 complain, in_decl);
10310 if (argvec == error_mark_node)
10311 return error_mark_node;
10313 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10314 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10317 break;
10319 case TEMPLATE_PARM_INDEX:
10320 r = reduce_template_parm_level (t, type, levels, args, complain);
10321 break;
10323 default:
10324 gcc_unreachable ();
10327 return r;
10330 case TREE_LIST:
10332 tree purpose, value, chain;
10334 if (t == void_list_node)
10335 return t;
10337 purpose = TREE_PURPOSE (t);
10338 if (purpose)
10340 purpose = tsubst (purpose, args, complain, in_decl);
10341 if (purpose == error_mark_node)
10342 return error_mark_node;
10344 value = TREE_VALUE (t);
10345 if (value)
10347 value = tsubst (value, args, complain, in_decl);
10348 if (value == error_mark_node)
10349 return error_mark_node;
10351 chain = TREE_CHAIN (t);
10352 if (chain && chain != void_type_node)
10354 chain = tsubst (chain, args, complain, in_decl);
10355 if (chain == error_mark_node)
10356 return error_mark_node;
10358 if (purpose == TREE_PURPOSE (t)
10359 && value == TREE_VALUE (t)
10360 && chain == TREE_CHAIN (t))
10361 return t;
10362 return hash_tree_cons (purpose, value, chain);
10365 case TREE_BINFO:
10366 /* We should never be tsubsting a binfo. */
10367 gcc_unreachable ();
10369 case TREE_VEC:
10370 /* A vector of template arguments. */
10371 gcc_assert (!type);
10372 return tsubst_template_args (t, args, complain, in_decl);
10374 case POINTER_TYPE:
10375 case REFERENCE_TYPE:
10377 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10378 return t;
10380 /* [temp.deduct]
10382 Type deduction may fail for any of the following
10383 reasons:
10385 -- Attempting to create a pointer to reference type.
10386 -- Attempting to create a reference to a reference type or
10387 a reference to void.
10389 Core issue 106 says that creating a reference to a reference
10390 during instantiation is no longer a cause for failure. We
10391 only enforce this check in strict C++98 mode. */
10392 if ((TREE_CODE (type) == REFERENCE_TYPE
10393 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10394 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10396 static location_t last_loc;
10398 /* We keep track of the last time we issued this error
10399 message to avoid spewing a ton of messages during a
10400 single bad template instantiation. */
10401 if (complain & tf_error
10402 && last_loc != input_location)
10404 if (TREE_CODE (type) == VOID_TYPE)
10405 error ("forming reference to void");
10406 else if (code == POINTER_TYPE)
10407 error ("forming pointer to reference type %qT", type);
10408 else
10409 error ("forming reference to reference type %qT", type);
10410 last_loc = input_location;
10413 return error_mark_node;
10415 else if (code == POINTER_TYPE)
10417 r = build_pointer_type (type);
10418 if (TREE_CODE (type) == METHOD_TYPE)
10419 r = build_ptrmemfunc_type (r);
10421 else if (TREE_CODE (type) == REFERENCE_TYPE)
10422 /* In C++0x, during template argument substitution, when there is an
10423 attempt to create a reference to a reference type, reference
10424 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10426 "If a template-argument for a template-parameter T names a type
10427 that is a reference to a type A, an attempt to create the type
10428 'lvalue reference to cv T' creates the type 'lvalue reference to
10429 A,' while an attempt to create the type type rvalue reference to
10430 cv T' creates the type T"
10432 r = cp_build_reference_type
10433 (TREE_TYPE (type),
10434 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10435 else
10436 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10437 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10439 if (r != error_mark_node)
10440 /* Will this ever be needed for TYPE_..._TO values? */
10441 layout_type (r);
10443 return r;
10445 case OFFSET_TYPE:
10447 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10448 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10450 /* [temp.deduct]
10452 Type deduction may fail for any of the following
10453 reasons:
10455 -- Attempting to create "pointer to member of T" when T
10456 is not a class type. */
10457 if (complain & tf_error)
10458 error ("creating pointer to member of non-class type %qT", r);
10459 return error_mark_node;
10461 if (TREE_CODE (type) == REFERENCE_TYPE)
10463 if (complain & tf_error)
10464 error ("creating pointer to member reference type %qT", type);
10465 return error_mark_node;
10467 if (TREE_CODE (type) == VOID_TYPE)
10469 if (complain & tf_error)
10470 error ("creating pointer to member of type void");
10471 return error_mark_node;
10473 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10474 if (TREE_CODE (type) == FUNCTION_TYPE)
10476 /* The type of the implicit object parameter gets its
10477 cv-qualifiers from the FUNCTION_TYPE. */
10478 tree memptr;
10479 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10480 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10481 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10482 complain);
10484 else
10485 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10486 cp_type_quals (t),
10487 complain);
10489 case FUNCTION_TYPE:
10490 case METHOD_TYPE:
10492 tree fntype;
10493 tree specs;
10494 fntype = tsubst_function_type (t, args, complain, in_decl);
10495 if (fntype == error_mark_node)
10496 return error_mark_node;
10498 /* Substitute the exception specification. */
10499 specs = tsubst_exception_specification (t, args, complain,
10500 in_decl);
10501 if (specs == error_mark_node)
10502 return error_mark_node;
10503 if (specs)
10504 fntype = build_exception_variant (fntype, specs);
10505 return fntype;
10507 case ARRAY_TYPE:
10509 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10510 if (domain == error_mark_node)
10511 return error_mark_node;
10513 /* As an optimization, we avoid regenerating the array type if
10514 it will obviously be the same as T. */
10515 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10516 return t;
10518 /* These checks should match the ones in grokdeclarator.
10520 [temp.deduct]
10522 The deduction may fail for any of the following reasons:
10524 -- Attempting to create an array with an element type that
10525 is void, a function type, or a reference type, or [DR337]
10526 an abstract class type. */
10527 if (TREE_CODE (type) == VOID_TYPE
10528 || TREE_CODE (type) == FUNCTION_TYPE
10529 || TREE_CODE (type) == REFERENCE_TYPE)
10531 if (complain & tf_error)
10532 error ("creating array of %qT", type);
10533 return error_mark_node;
10535 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10537 if (complain & tf_error)
10538 error ("creating array of %qT, which is an abstract class type",
10539 type);
10540 return error_mark_node;
10543 r = build_cplus_array_type (type, domain);
10545 if (TYPE_USER_ALIGN (t))
10547 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10548 TYPE_USER_ALIGN (r) = 1;
10551 return r;
10554 case TYPENAME_TYPE:
10556 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10557 in_decl, /*entering_scope=*/1);
10558 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10559 complain, in_decl);
10561 if (ctx == error_mark_node || f == error_mark_node)
10562 return error_mark_node;
10564 if (!MAYBE_CLASS_TYPE_P (ctx))
10566 if (complain & tf_error)
10567 error ("%qT is not a class, struct, or union type", ctx);
10568 return error_mark_node;
10570 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10572 /* Normally, make_typename_type does not require that the CTX
10573 have complete type in order to allow things like:
10575 template <class T> struct S { typename S<T>::X Y; };
10577 But, such constructs have already been resolved by this
10578 point, so here CTX really should have complete type, unless
10579 it's a partial instantiation. */
10580 if (!(complain & tf_no_class_instantiations))
10581 ctx = complete_type (ctx);
10582 if (!COMPLETE_TYPE_P (ctx))
10584 if (complain & tf_error)
10585 cxx_incomplete_type_error (NULL_TREE, ctx);
10586 return error_mark_node;
10590 f = make_typename_type (ctx, f, typename_type,
10591 (complain & tf_error) | tf_keep_type_decl);
10592 if (f == error_mark_node)
10593 return f;
10594 if (TREE_CODE (f) == TYPE_DECL)
10596 complain |= tf_ignore_bad_quals;
10597 f = TREE_TYPE (f);
10600 if (TREE_CODE (f) != TYPENAME_TYPE)
10602 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10603 error ("%qT resolves to %qT, which is not an enumeration type",
10604 t, f);
10605 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10606 error ("%qT resolves to %qT, which is is not a class type",
10607 t, f);
10610 return cp_build_qualified_type_real
10611 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10614 case UNBOUND_CLASS_TEMPLATE:
10616 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10617 in_decl, /*entering_scope=*/1);
10618 tree name = TYPE_IDENTIFIER (t);
10619 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10621 if (ctx == error_mark_node || name == error_mark_node)
10622 return error_mark_node;
10624 if (parm_list)
10625 parm_list = tsubst_template_parms (parm_list, args, complain);
10626 return make_unbound_class_template (ctx, name, parm_list, complain);
10629 case TYPEOF_TYPE:
10631 tree type;
10633 ++cp_unevaluated_operand;
10634 ++c_inhibit_evaluation_warnings;
10636 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10637 complain, in_decl,
10638 /*integral_constant_expression_p=*/false);
10640 --cp_unevaluated_operand;
10641 --c_inhibit_evaluation_warnings;
10643 type = finish_typeof (type);
10644 return cp_build_qualified_type_real (type,
10645 cp_type_quals (t)
10646 | cp_type_quals (type),
10647 complain);
10650 case DECLTYPE_TYPE:
10652 tree type;
10654 ++cp_unevaluated_operand;
10655 ++c_inhibit_evaluation_warnings;
10657 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10658 complain, in_decl,
10659 /*integral_constant_expression_p=*/false);
10661 --cp_unevaluated_operand;
10662 --c_inhibit_evaluation_warnings;
10664 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10665 type = lambda_capture_field_type (type);
10666 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10667 type = lambda_return_type (type);
10668 else
10669 type = finish_decltype_type
10670 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10671 return cp_build_qualified_type_real (type,
10672 cp_type_quals (t)
10673 | cp_type_quals (type),
10674 complain);
10677 case TYPE_ARGUMENT_PACK:
10678 case NONTYPE_ARGUMENT_PACK:
10680 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
10681 tree packed_out =
10682 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10683 args,
10684 complain,
10685 in_decl);
10686 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10688 /* For template nontype argument packs, also substitute into
10689 the type. */
10690 if (code == NONTYPE_ARGUMENT_PACK)
10691 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10693 return r;
10695 break;
10697 case INTEGER_CST:
10698 case REAL_CST:
10699 case STRING_CST:
10700 case PLUS_EXPR:
10701 case MINUS_EXPR:
10702 case NEGATE_EXPR:
10703 case NOP_EXPR:
10704 case INDIRECT_REF:
10705 case ADDR_EXPR:
10706 case CALL_EXPR:
10707 case ARRAY_REF:
10708 case SCOPE_REF:
10709 /* We should use one of the expression tsubsts for these codes. */
10710 gcc_unreachable ();
10712 default:
10713 sorry ("use of %qs in template", tree_code_name [(int) code]);
10714 return error_mark_node;
10718 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10719 type of the expression on the left-hand side of the "." or "->"
10720 operator. */
10722 static tree
10723 tsubst_baselink (tree baselink, tree object_type,
10724 tree args, tsubst_flags_t complain, tree in_decl)
10726 tree name;
10727 tree qualifying_scope;
10728 tree fns;
10729 tree optype;
10730 tree template_args = 0;
10731 bool template_id_p = false;
10733 /* A baselink indicates a function from a base class. Both the
10734 BASELINK_ACCESS_BINFO and the base class referenced may
10735 indicate bases of the template class, rather than the
10736 instantiated class. In addition, lookups that were not
10737 ambiguous before may be ambiguous now. Therefore, we perform
10738 the lookup again. */
10739 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10740 qualifying_scope = tsubst (qualifying_scope, args,
10741 complain, in_decl);
10742 fns = BASELINK_FUNCTIONS (baselink);
10743 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10744 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10746 template_id_p = true;
10747 template_args = TREE_OPERAND (fns, 1);
10748 fns = TREE_OPERAND (fns, 0);
10749 if (template_args)
10750 template_args = tsubst_template_args (template_args, args,
10751 complain, in_decl);
10753 name = DECL_NAME (get_first_fn (fns));
10754 if (IDENTIFIER_TYPENAME_P (name))
10755 name = mangle_conv_op_name_for_type (optype);
10756 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10757 if (!baselink)
10758 return error_mark_node;
10760 /* If lookup found a single function, mark it as used at this
10761 point. (If it lookup found multiple functions the one selected
10762 later by overload resolution will be marked as used at that
10763 point.) */
10764 if (BASELINK_P (baselink))
10765 fns = BASELINK_FUNCTIONS (baselink);
10766 if (!template_id_p && !really_overloaded_fn (fns))
10767 mark_used (OVL_CURRENT (fns));
10769 /* Add back the template arguments, if present. */
10770 if (BASELINK_P (baselink) && template_id_p)
10771 BASELINK_FUNCTIONS (baselink)
10772 = build_nt (TEMPLATE_ID_EXPR,
10773 BASELINK_FUNCTIONS (baselink),
10774 template_args);
10775 /* Update the conversion operator type. */
10776 BASELINK_OPTYPE (baselink) = optype;
10778 if (!object_type)
10779 object_type = current_class_type;
10780 return adjust_result_of_qualified_name_lookup (baselink,
10781 qualifying_scope,
10782 object_type);
10785 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10786 true if the qualified-id will be a postfix-expression in-and-of
10787 itself; false if more of the postfix-expression follows the
10788 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10789 of "&". */
10791 static tree
10792 tsubst_qualified_id (tree qualified_id, tree args,
10793 tsubst_flags_t complain, tree in_decl,
10794 bool done, bool address_p)
10796 tree expr;
10797 tree scope;
10798 tree name;
10799 bool is_template;
10800 tree template_args;
10802 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10804 /* Figure out what name to look up. */
10805 name = TREE_OPERAND (qualified_id, 1);
10806 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10808 is_template = true;
10809 template_args = TREE_OPERAND (name, 1);
10810 if (template_args)
10811 template_args = tsubst_template_args (template_args, args,
10812 complain, in_decl);
10813 name = TREE_OPERAND (name, 0);
10815 else
10817 is_template = false;
10818 template_args = NULL_TREE;
10821 /* Substitute into the qualifying scope. When there are no ARGS, we
10822 are just trying to simplify a non-dependent expression. In that
10823 case the qualifying scope may be dependent, and, in any case,
10824 substituting will not help. */
10825 scope = TREE_OPERAND (qualified_id, 0);
10826 if (args)
10828 scope = tsubst (scope, args, complain, in_decl);
10829 expr = tsubst_copy (name, args, complain, in_decl);
10831 else
10832 expr = name;
10834 if (dependent_scope_p (scope))
10835 return build_qualified_name (NULL_TREE, scope, expr,
10836 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10838 if (!BASELINK_P (name) && !DECL_P (expr))
10840 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10842 /* A BIT_NOT_EXPR is used to represent a destructor. */
10843 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10845 error ("qualifying type %qT does not match destructor name ~%qT",
10846 scope, TREE_OPERAND (expr, 0));
10847 expr = error_mark_node;
10849 else
10850 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10851 /*is_type_p=*/0, false);
10853 else
10854 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10855 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10856 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10858 if (complain & tf_error)
10860 error ("dependent-name %qE is parsed as a non-type, but "
10861 "instantiation yields a type", qualified_id);
10862 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10864 return error_mark_node;
10868 if (DECL_P (expr))
10870 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10871 scope);
10872 /* Remember that there was a reference to this entity. */
10873 mark_used (expr);
10876 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10878 if (complain & tf_error)
10879 qualified_name_lookup_error (scope,
10880 TREE_OPERAND (qualified_id, 1),
10881 expr, input_location);
10882 return error_mark_node;
10885 if (is_template)
10886 expr = lookup_template_function (expr, template_args);
10888 if (expr == error_mark_node && complain & tf_error)
10889 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10890 expr, input_location);
10891 else if (TYPE_P (scope))
10893 expr = (adjust_result_of_qualified_name_lookup
10894 (expr, scope, current_class_type));
10895 expr = (finish_qualified_id_expr
10896 (scope, expr, done, address_p,
10897 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10898 /*template_arg_p=*/false));
10901 /* Expressions do not generally have reference type. */
10902 if (TREE_CODE (expr) != SCOPE_REF
10903 /* However, if we're about to form a pointer-to-member, we just
10904 want the referenced member referenced. */
10905 && TREE_CODE (expr) != OFFSET_REF)
10906 expr = convert_from_reference (expr);
10908 return expr;
10911 /* Like tsubst, but deals with expressions. This function just replaces
10912 template parms; to finish processing the resultant expression, use
10913 tsubst_expr. */
10915 static tree
10916 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10918 enum tree_code code;
10919 tree r;
10921 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10922 return t;
10924 code = TREE_CODE (t);
10926 switch (code)
10928 case PARM_DECL:
10929 r = retrieve_local_specialization (t);
10931 if (r == NULL)
10933 tree c;
10934 /* This can happen for a parameter name used later in a function
10935 declaration (such as in a late-specified return type). Just
10936 make a dummy decl, since it's only used for its type. */
10937 gcc_assert (cp_unevaluated_operand != 0);
10938 /* We copy T because want to tsubst the PARM_DECL only,
10939 not the following PARM_DECLs that are chained to T. */
10940 c = copy_node (t);
10941 r = tsubst_decl (c, args, complain);
10942 /* Give it the template pattern as its context; its true context
10943 hasn't been instantiated yet and this is good enough for
10944 mangling. */
10945 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10948 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10949 r = ARGUMENT_PACK_SELECT_ARG (r);
10950 mark_used (r);
10951 return r;
10953 case CONST_DECL:
10955 tree enum_type;
10956 tree v;
10958 if (DECL_TEMPLATE_PARM_P (t))
10959 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10960 /* There is no need to substitute into namespace-scope
10961 enumerators. */
10962 if (DECL_NAMESPACE_SCOPE_P (t))
10963 return t;
10964 /* If ARGS is NULL, then T is known to be non-dependent. */
10965 if (args == NULL_TREE)
10966 return integral_constant_value (t);
10968 /* Unfortunately, we cannot just call lookup_name here.
10969 Consider:
10971 template <int I> int f() {
10972 enum E { a = I };
10973 struct S { void g() { E e = a; } };
10976 When we instantiate f<7>::S::g(), say, lookup_name is not
10977 clever enough to find f<7>::a. */
10978 enum_type
10979 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10980 /*entering_scope=*/0);
10982 for (v = TYPE_VALUES (enum_type);
10983 v != NULL_TREE;
10984 v = TREE_CHAIN (v))
10985 if (TREE_PURPOSE (v) == DECL_NAME (t))
10986 return TREE_VALUE (v);
10988 /* We didn't find the name. That should never happen; if
10989 name-lookup found it during preliminary parsing, we
10990 should find it again here during instantiation. */
10991 gcc_unreachable ();
10993 return t;
10995 case FIELD_DECL:
10996 if (DECL_CONTEXT (t))
10998 tree ctx;
11000 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11001 /*entering_scope=*/1);
11002 if (ctx != DECL_CONTEXT (t))
11004 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11005 if (!r)
11007 if (complain & tf_error)
11008 error ("using invalid field %qD", t);
11009 return error_mark_node;
11011 return r;
11015 return t;
11017 case VAR_DECL:
11018 case FUNCTION_DECL:
11019 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11020 || local_variable_p (t))
11021 t = tsubst (t, args, complain, in_decl);
11022 mark_used (t);
11023 return t;
11025 case OVERLOAD:
11026 /* An OVERLOAD will always be a non-dependent overload set; an
11027 overload set from function scope will just be represented with an
11028 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11029 gcc_assert (!uses_template_parms (t));
11030 return t;
11032 case BASELINK:
11033 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11035 case TEMPLATE_DECL:
11036 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11037 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11038 args, complain, in_decl);
11039 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11040 return tsubst (t, args, complain, in_decl);
11041 else if (DECL_CLASS_SCOPE_P (t)
11042 && uses_template_parms (DECL_CONTEXT (t)))
11044 /* Template template argument like the following example need
11045 special treatment:
11047 template <template <class> class TT> struct C {};
11048 template <class T> struct D {
11049 template <class U> struct E {};
11050 C<E> c; // #1
11052 D<int> d; // #2
11054 We are processing the template argument `E' in #1 for
11055 the template instantiation #2. Originally, `E' is a
11056 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11057 have to substitute this with one having context `D<int>'. */
11059 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11060 return lookup_field (context, DECL_NAME(t), 0, false);
11062 else
11063 /* Ordinary template template argument. */
11064 return t;
11066 case CAST_EXPR:
11067 case REINTERPRET_CAST_EXPR:
11068 case CONST_CAST_EXPR:
11069 case STATIC_CAST_EXPR:
11070 case DYNAMIC_CAST_EXPR:
11071 case NOP_EXPR:
11072 return build1
11073 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11074 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11076 case SIZEOF_EXPR:
11077 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11079 /* We only want to compute the number of arguments. */
11080 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11081 complain, in_decl);
11082 int len = 0;
11084 if (TREE_CODE (expanded) == TREE_VEC)
11085 len = TREE_VEC_LENGTH (expanded);
11087 if (expanded == error_mark_node)
11088 return error_mark_node;
11089 else if (PACK_EXPANSION_P (expanded)
11090 || (TREE_CODE (expanded) == TREE_VEC
11091 && len > 0
11092 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11094 if (TREE_CODE (expanded) == TREE_VEC)
11095 expanded = TREE_VEC_ELT (expanded, len - 1);
11097 if (TYPE_P (expanded))
11098 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11099 complain & tf_error);
11100 else
11101 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11102 complain & tf_error);
11104 else
11105 return build_int_cst (size_type_node, len);
11107 /* Fall through */
11109 case INDIRECT_REF:
11110 case NEGATE_EXPR:
11111 case TRUTH_NOT_EXPR:
11112 case BIT_NOT_EXPR:
11113 case ADDR_EXPR:
11114 case UNARY_PLUS_EXPR: /* Unary + */
11115 case ALIGNOF_EXPR:
11116 case AT_ENCODE_EXPR:
11117 case ARROW_EXPR:
11118 case THROW_EXPR:
11119 case TYPEID_EXPR:
11120 case REALPART_EXPR:
11121 case IMAGPART_EXPR:
11122 return build1
11123 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11124 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11126 case COMPONENT_REF:
11128 tree object;
11129 tree name;
11131 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11132 name = TREE_OPERAND (t, 1);
11133 if (TREE_CODE (name) == BIT_NOT_EXPR)
11135 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11136 complain, in_decl);
11137 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11139 else if (TREE_CODE (name) == SCOPE_REF
11140 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11142 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11143 complain, in_decl);
11144 name = TREE_OPERAND (name, 1);
11145 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11146 complain, in_decl);
11147 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11148 name = build_qualified_name (/*type=*/NULL_TREE,
11149 base, name,
11150 /*template_p=*/false);
11152 else if (TREE_CODE (name) == BASELINK)
11153 name = tsubst_baselink (name,
11154 non_reference (TREE_TYPE (object)),
11155 args, complain,
11156 in_decl);
11157 else
11158 name = tsubst_copy (name, args, complain, in_decl);
11159 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11162 case PLUS_EXPR:
11163 case MINUS_EXPR:
11164 case MULT_EXPR:
11165 case TRUNC_DIV_EXPR:
11166 case CEIL_DIV_EXPR:
11167 case FLOOR_DIV_EXPR:
11168 case ROUND_DIV_EXPR:
11169 case EXACT_DIV_EXPR:
11170 case BIT_AND_EXPR:
11171 case BIT_IOR_EXPR:
11172 case BIT_XOR_EXPR:
11173 case TRUNC_MOD_EXPR:
11174 case FLOOR_MOD_EXPR:
11175 case TRUTH_ANDIF_EXPR:
11176 case TRUTH_ORIF_EXPR:
11177 case TRUTH_AND_EXPR:
11178 case TRUTH_OR_EXPR:
11179 case RSHIFT_EXPR:
11180 case LSHIFT_EXPR:
11181 case RROTATE_EXPR:
11182 case LROTATE_EXPR:
11183 case EQ_EXPR:
11184 case NE_EXPR:
11185 case MAX_EXPR:
11186 case MIN_EXPR:
11187 case LE_EXPR:
11188 case GE_EXPR:
11189 case LT_EXPR:
11190 case GT_EXPR:
11191 case COMPOUND_EXPR:
11192 case DOTSTAR_EXPR:
11193 case MEMBER_REF:
11194 case PREDECREMENT_EXPR:
11195 case PREINCREMENT_EXPR:
11196 case POSTDECREMENT_EXPR:
11197 case POSTINCREMENT_EXPR:
11198 return build_nt
11199 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11200 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11202 case SCOPE_REF:
11203 return build_qualified_name (/*type=*/NULL_TREE,
11204 tsubst_copy (TREE_OPERAND (t, 0),
11205 args, complain, in_decl),
11206 tsubst_copy (TREE_OPERAND (t, 1),
11207 args, complain, in_decl),
11208 QUALIFIED_NAME_IS_TEMPLATE (t));
11210 case ARRAY_REF:
11211 return build_nt
11212 (ARRAY_REF,
11213 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11214 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11215 NULL_TREE, NULL_TREE);
11217 case CALL_EXPR:
11219 int n = VL_EXP_OPERAND_LENGTH (t);
11220 tree result = build_vl_exp (CALL_EXPR, n);
11221 int i;
11222 for (i = 0; i < n; i++)
11223 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11224 complain, in_decl);
11225 return result;
11228 case COND_EXPR:
11229 case MODOP_EXPR:
11230 case PSEUDO_DTOR_EXPR:
11232 r = build_nt
11233 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11234 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11235 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11236 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11237 return r;
11240 case NEW_EXPR:
11242 r = build_nt
11243 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11244 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11245 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11246 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11247 return r;
11250 case DELETE_EXPR:
11252 r = build_nt
11253 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11254 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11255 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11256 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11257 return r;
11260 case TEMPLATE_ID_EXPR:
11262 /* Substituted template arguments */
11263 tree fn = TREE_OPERAND (t, 0);
11264 tree targs = TREE_OPERAND (t, 1);
11266 fn = tsubst_copy (fn, args, complain, in_decl);
11267 if (targs)
11268 targs = tsubst_template_args (targs, args, complain, in_decl);
11270 return lookup_template_function (fn, targs);
11273 case TREE_LIST:
11275 tree purpose, value, chain;
11277 if (t == void_list_node)
11278 return t;
11280 purpose = TREE_PURPOSE (t);
11281 if (purpose)
11282 purpose = tsubst_copy (purpose, args, complain, in_decl);
11283 value = TREE_VALUE (t);
11284 if (value)
11285 value = tsubst_copy (value, args, complain, in_decl);
11286 chain = TREE_CHAIN (t);
11287 if (chain && chain != void_type_node)
11288 chain = tsubst_copy (chain, args, complain, in_decl);
11289 if (purpose == TREE_PURPOSE (t)
11290 && value == TREE_VALUE (t)
11291 && chain == TREE_CHAIN (t))
11292 return t;
11293 return tree_cons (purpose, value, chain);
11296 case RECORD_TYPE:
11297 case UNION_TYPE:
11298 case ENUMERAL_TYPE:
11299 case INTEGER_TYPE:
11300 case TEMPLATE_TYPE_PARM:
11301 case TEMPLATE_TEMPLATE_PARM:
11302 case BOUND_TEMPLATE_TEMPLATE_PARM:
11303 case TEMPLATE_PARM_INDEX:
11304 case POINTER_TYPE:
11305 case REFERENCE_TYPE:
11306 case OFFSET_TYPE:
11307 case FUNCTION_TYPE:
11308 case METHOD_TYPE:
11309 case ARRAY_TYPE:
11310 case TYPENAME_TYPE:
11311 case UNBOUND_CLASS_TEMPLATE:
11312 case TYPEOF_TYPE:
11313 case DECLTYPE_TYPE:
11314 case TYPE_DECL:
11315 return tsubst (t, args, complain, in_decl);
11317 case IDENTIFIER_NODE:
11318 if (IDENTIFIER_TYPENAME_P (t))
11320 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11321 return mangle_conv_op_name_for_type (new_type);
11323 else
11324 return t;
11326 case CONSTRUCTOR:
11327 /* This is handled by tsubst_copy_and_build. */
11328 gcc_unreachable ();
11330 case VA_ARG_EXPR:
11331 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11332 in_decl),
11333 tsubst (TREE_TYPE (t), args, complain, in_decl));
11335 case CLEANUP_POINT_EXPR:
11336 /* We shouldn't have built any of these during initial template
11337 generation. Instead, they should be built during instantiation
11338 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11339 gcc_unreachable ();
11341 case OFFSET_REF:
11342 r = build2
11343 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11344 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11345 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11346 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11347 mark_used (TREE_OPERAND (r, 1));
11348 return r;
11350 case EXPR_PACK_EXPANSION:
11351 error ("invalid use of pack expansion expression");
11352 return error_mark_node;
11354 case NONTYPE_ARGUMENT_PACK:
11355 error ("use %<...%> to expand argument pack");
11356 return error_mark_node;
11358 case INTEGER_CST:
11359 case REAL_CST:
11360 case STRING_CST:
11362 /* Instantiate any typedefs in the type. */
11363 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11364 r = fold_convert (type, t);
11365 gcc_assert (TREE_CODE (r) == code);
11366 return r;
11369 case PTRMEM_CST:
11370 /* These can sometimes show up in a partial instantiation, but never
11371 involve template parms. */
11372 gcc_assert (!uses_template_parms (t));
11373 return t;
11375 default:
11376 gcc_unreachable ();
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 (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 = tsubst (scope, args, complain, in_decl);
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) == LABEL_DECL)
12268 return finish_label_address_expr (DECL_NAME (op1),
12269 EXPR_LOCATION (op1));
12270 if (TREE_CODE (op1) == SCOPE_REF)
12271 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12272 /*done=*/true, /*address_p=*/true);
12273 else
12274 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12275 in_decl);
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 AT_ENCODE_EXPR:
12364 op1 = TREE_OPERAND (t, 0);
12365 ++cp_unevaluated_operand;
12366 ++c_inhibit_evaluation_warnings;
12367 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12368 /*function_p=*/false,
12369 /*integral_constant_expression_p=*/false);
12370 --cp_unevaluated_operand;
12371 --c_inhibit_evaluation_warnings;
12372 return objc_build_encode_expr (op1);
12375 case NOEXCEPT_EXPR:
12376 op1 = TREE_OPERAND (t, 0);
12377 ++cp_unevaluated_operand;
12378 ++c_inhibit_evaluation_warnings;
12379 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12380 /*function_p=*/false,
12381 /*integral_constant_expression_p=*/false);
12382 --cp_unevaluated_operand;
12383 --c_inhibit_evaluation_warnings;
12384 return finish_noexcept_expr (op1, complain);
12386 case MODOP_EXPR:
12388 tree r = build_x_modify_expr
12389 (RECUR (TREE_OPERAND (t, 0)),
12390 TREE_CODE (TREE_OPERAND (t, 1)),
12391 RECUR (TREE_OPERAND (t, 2)),
12392 complain);
12393 /* TREE_NO_WARNING must be set if either the expression was
12394 parenthesized or it uses an operator such as >>= rather
12395 than plain assignment. In the former case, it was already
12396 set and must be copied. In the latter case,
12397 build_x_modify_expr sets it and it must not be reset
12398 here. */
12399 if (TREE_NO_WARNING (t))
12400 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12401 return r;
12404 case ARROW_EXPR:
12405 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12406 args, complain, in_decl);
12407 /* Remember that there was a reference to this entity. */
12408 if (DECL_P (op1))
12409 mark_used (op1);
12410 return build_x_arrow (op1);
12412 case NEW_EXPR:
12414 tree placement = RECUR (TREE_OPERAND (t, 0));
12415 tree init = RECUR (TREE_OPERAND (t, 3));
12416 VEC(tree,gc) *placement_vec;
12417 VEC(tree,gc) *init_vec;
12418 tree ret;
12420 if (placement == NULL_TREE)
12421 placement_vec = NULL;
12422 else
12424 placement_vec = make_tree_vector ();
12425 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12426 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12429 /* If there was an initializer in the original tree, but it
12430 instantiated to an empty list, then we should pass a
12431 non-NULL empty vector to tell build_new that it was an
12432 empty initializer() rather than no initializer. This can
12433 only happen when the initializer is a pack expansion whose
12434 parameter packs are of length zero. */
12435 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12436 init_vec = NULL;
12437 else
12439 init_vec = make_tree_vector ();
12440 if (init == void_zero_node)
12441 gcc_assert (init_vec != NULL);
12442 else
12444 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12445 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12449 ret = build_new (&placement_vec,
12450 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
12451 RECUR (TREE_OPERAND (t, 2)),
12452 &init_vec,
12453 NEW_EXPR_USE_GLOBAL (t),
12454 complain);
12456 if (placement_vec != NULL)
12457 release_tree_vector (placement_vec);
12458 if (init_vec != NULL)
12459 release_tree_vector (init_vec);
12461 return ret;
12464 case DELETE_EXPR:
12465 return delete_sanity
12466 (RECUR (TREE_OPERAND (t, 0)),
12467 RECUR (TREE_OPERAND (t, 1)),
12468 DELETE_EXPR_USE_VEC (t),
12469 DELETE_EXPR_USE_GLOBAL (t));
12471 case COMPOUND_EXPR:
12472 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12473 RECUR (TREE_OPERAND (t, 1)),
12474 complain);
12476 case CALL_EXPR:
12478 tree function;
12479 VEC(tree,gc) *call_args;
12480 unsigned int nargs, i;
12481 bool qualified_p;
12482 bool koenig_p;
12483 tree ret;
12485 function = CALL_EXPR_FN (t);
12486 /* When we parsed the expression, we determined whether or
12487 not Koenig lookup should be performed. */
12488 koenig_p = KOENIG_LOOKUP_P (t);
12489 if (TREE_CODE (function) == SCOPE_REF)
12491 qualified_p = true;
12492 function = tsubst_qualified_id (function, args, complain, in_decl,
12493 /*done=*/false,
12494 /*address_p=*/false);
12496 else
12498 if (TREE_CODE (function) == COMPONENT_REF)
12500 tree op = TREE_OPERAND (function, 1);
12502 qualified_p = (TREE_CODE (op) == SCOPE_REF
12503 || (BASELINK_P (op)
12504 && BASELINK_QUALIFIED_P (op)));
12506 else
12507 qualified_p = false;
12509 function = tsubst_copy_and_build (function, args, complain,
12510 in_decl,
12511 !qualified_p,
12512 integral_constant_expression_p);
12514 if (BASELINK_P (function))
12515 qualified_p = true;
12518 nargs = call_expr_nargs (t);
12519 call_args = make_tree_vector ();
12520 for (i = 0; i < nargs; ++i)
12522 tree arg = CALL_EXPR_ARG (t, i);
12524 if (!PACK_EXPANSION_P (arg))
12525 VEC_safe_push (tree, gc, call_args,
12526 RECUR (CALL_EXPR_ARG (t, i)));
12527 else
12529 /* Expand the pack expansion and push each entry onto
12530 CALL_ARGS. */
12531 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12532 if (TREE_CODE (arg) == TREE_VEC)
12534 unsigned int len, j;
12536 len = TREE_VEC_LENGTH (arg);
12537 for (j = 0; j < len; ++j)
12539 tree value = TREE_VEC_ELT (arg, j);
12540 if (value != NULL_TREE)
12541 value = convert_from_reference (value);
12542 VEC_safe_push (tree, gc, call_args, value);
12545 else
12547 /* A partial substitution. Add one entry. */
12548 VEC_safe_push (tree, gc, call_args, arg);
12553 /* We do not perform argument-dependent lookup if normal
12554 lookup finds a non-function, in accordance with the
12555 expected resolution of DR 218. */
12556 if (koenig_p
12557 && ((is_overloaded_fn (function)
12558 /* If lookup found a member function, the Koenig lookup is
12559 not appropriate, even if an unqualified-name was used
12560 to denote the function. */
12561 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12562 || TREE_CODE (function) == IDENTIFIER_NODE)
12563 /* Only do this when substitution turns a dependent call
12564 into a non-dependent call. */
12565 && type_dependent_expression_p_push (t)
12566 && !any_type_dependent_arguments_p (call_args))
12567 function = perform_koenig_lookup (function, call_args, false);
12569 if (TREE_CODE (function) == IDENTIFIER_NODE)
12571 unqualified_name_lookup_error (function);
12572 release_tree_vector (call_args);
12573 return error_mark_node;
12576 /* Remember that there was a reference to this entity. */
12577 if (DECL_P (function))
12578 mark_used (function);
12580 if (TREE_CODE (function) == OFFSET_REF)
12581 ret = build_offset_ref_call_from_tree (function, &call_args);
12582 else if (TREE_CODE (function) == COMPONENT_REF)
12584 tree instance = TREE_OPERAND (function, 0);
12585 tree fn = TREE_OPERAND (function, 1);
12587 if (processing_template_decl
12588 && (type_dependent_expression_p (instance)
12589 || (!BASELINK_P (fn)
12590 && TREE_CODE (fn) != FIELD_DECL)
12591 || type_dependent_expression_p (fn)
12592 || any_type_dependent_arguments_p (call_args)))
12593 ret = build_nt_call_vec (function, call_args);
12594 else if (!BASELINK_P (fn))
12595 ret = finish_call_expr (function, &call_args,
12596 /*disallow_virtual=*/false,
12597 /*koenig_p=*/false,
12598 complain);
12599 else
12600 ret = (build_new_method_call
12601 (instance, fn,
12602 &call_args, NULL_TREE,
12603 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12604 /*fn_p=*/NULL,
12605 complain));
12607 else
12608 ret = finish_call_expr (function, &call_args,
12609 /*disallow_virtual=*/qualified_p,
12610 koenig_p,
12611 complain);
12613 release_tree_vector (call_args);
12615 return ret;
12618 case COND_EXPR:
12619 return build_x_conditional_expr
12620 (RECUR (TREE_OPERAND (t, 0)),
12621 RECUR (TREE_OPERAND (t, 1)),
12622 RECUR (TREE_OPERAND (t, 2)),
12623 complain);
12625 case PSEUDO_DTOR_EXPR:
12626 return finish_pseudo_destructor_expr
12627 (RECUR (TREE_OPERAND (t, 0)),
12628 RECUR (TREE_OPERAND (t, 1)),
12629 RECUR (TREE_OPERAND (t, 2)));
12631 case TREE_LIST:
12633 tree purpose, value, chain;
12635 if (t == void_list_node)
12636 return t;
12638 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12639 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12641 /* We have pack expansions, so expand those and
12642 create a new list out of it. */
12643 tree purposevec = NULL_TREE;
12644 tree valuevec = NULL_TREE;
12645 tree chain;
12646 int i, len = -1;
12648 /* Expand the argument expressions. */
12649 if (TREE_PURPOSE (t))
12650 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12651 complain, in_decl);
12652 if (TREE_VALUE (t))
12653 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12654 complain, in_decl);
12656 /* Build the rest of the list. */
12657 chain = TREE_CHAIN (t);
12658 if (chain && chain != void_type_node)
12659 chain = RECUR (chain);
12661 /* Determine the number of arguments. */
12662 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12664 len = TREE_VEC_LENGTH (purposevec);
12665 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12667 else if (TREE_CODE (valuevec) == TREE_VEC)
12668 len = TREE_VEC_LENGTH (valuevec);
12669 else
12671 /* Since we only performed a partial substitution into
12672 the argument pack, we only return a single list
12673 node. */
12674 if (purposevec == TREE_PURPOSE (t)
12675 && valuevec == TREE_VALUE (t)
12676 && chain == TREE_CHAIN (t))
12677 return t;
12679 return tree_cons (purposevec, valuevec, chain);
12682 /* Convert the argument vectors into a TREE_LIST */
12683 i = len;
12684 while (i > 0)
12686 /* Grab the Ith values. */
12687 i--;
12688 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12689 : NULL_TREE;
12690 value
12691 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12692 : NULL_TREE;
12694 /* Build the list (backwards). */
12695 chain = tree_cons (purpose, value, chain);
12698 return chain;
12701 purpose = TREE_PURPOSE (t);
12702 if (purpose)
12703 purpose = RECUR (purpose);
12704 value = TREE_VALUE (t);
12705 if (value)
12706 value = RECUR (value);
12707 chain = TREE_CHAIN (t);
12708 if (chain && chain != void_type_node)
12709 chain = RECUR (chain);
12710 if (purpose == TREE_PURPOSE (t)
12711 && value == TREE_VALUE (t)
12712 && chain == TREE_CHAIN (t))
12713 return t;
12714 return tree_cons (purpose, value, chain);
12717 case COMPONENT_REF:
12719 tree object;
12720 tree object_type;
12721 tree member;
12723 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12724 args, complain, in_decl);
12725 /* Remember that there was a reference to this entity. */
12726 if (DECL_P (object))
12727 mark_used (object);
12728 object_type = TREE_TYPE (object);
12730 member = TREE_OPERAND (t, 1);
12731 if (BASELINK_P (member))
12732 member = tsubst_baselink (member,
12733 non_reference (TREE_TYPE (object)),
12734 args, complain, in_decl);
12735 else
12736 member = tsubst_copy (member, args, complain, in_decl);
12737 if (member == error_mark_node)
12738 return error_mark_node;
12740 if (object_type && !CLASS_TYPE_P (object_type))
12742 if (SCALAR_TYPE_P (object_type))
12744 tree s = NULL_TREE;
12745 tree dtor = member;
12747 if (TREE_CODE (dtor) == SCOPE_REF)
12749 s = TREE_OPERAND (dtor, 0);
12750 dtor = TREE_OPERAND (dtor, 1);
12752 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12754 dtor = TREE_OPERAND (dtor, 0);
12755 if (TYPE_P (dtor))
12756 return finish_pseudo_destructor_expr (object, s, dtor);
12760 else if (TREE_CODE (member) == SCOPE_REF
12761 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12763 tree tmpl;
12764 tree args;
12766 /* Lookup the template functions now that we know what the
12767 scope is. */
12768 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12769 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12770 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12771 /*is_type_p=*/false,
12772 /*complain=*/false);
12773 if (BASELINK_P (member))
12775 BASELINK_FUNCTIONS (member)
12776 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12777 args);
12778 member = (adjust_result_of_qualified_name_lookup
12779 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12780 object_type));
12782 else
12784 qualified_name_lookup_error (object_type, tmpl, member,
12785 input_location);
12786 return error_mark_node;
12789 else if (TREE_CODE (member) == SCOPE_REF
12790 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12791 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12793 if (complain & tf_error)
12795 if (TYPE_P (TREE_OPERAND (member, 0)))
12796 error ("%qT is not a class or namespace",
12797 TREE_OPERAND (member, 0));
12798 else
12799 error ("%qD is not a class or namespace",
12800 TREE_OPERAND (member, 0));
12802 return error_mark_node;
12804 else if (TREE_CODE (member) == FIELD_DECL)
12805 return finish_non_static_data_member (member, object, NULL_TREE);
12807 return finish_class_member_access_expr (object, member,
12808 /*template_p=*/false,
12809 complain);
12812 case THROW_EXPR:
12813 return build_throw
12814 (RECUR (TREE_OPERAND (t, 0)));
12816 case CONSTRUCTOR:
12818 VEC(constructor_elt,gc) *n;
12819 constructor_elt *ce;
12820 unsigned HOST_WIDE_INT idx;
12821 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12822 bool process_index_p;
12823 int newlen;
12824 bool need_copy_p = false;
12825 tree r;
12827 if (type == error_mark_node)
12828 return error_mark_node;
12830 /* digest_init will do the wrong thing if we let it. */
12831 if (type && TYPE_PTRMEMFUNC_P (type))
12832 return t;
12834 /* We do not want to process the index of aggregate
12835 initializers as they are identifier nodes which will be
12836 looked up by digest_init. */
12837 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12839 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12840 newlen = VEC_length (constructor_elt, n);
12841 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
12843 if (ce->index && process_index_p)
12844 ce->index = RECUR (ce->index);
12846 if (PACK_EXPANSION_P (ce->value))
12848 /* Substitute into the pack expansion. */
12849 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12850 in_decl);
12852 if (ce->value == error_mark_node)
12854 else if (TREE_VEC_LENGTH (ce->value) == 1)
12855 /* Just move the argument into place. */
12856 ce->value = TREE_VEC_ELT (ce->value, 0);
12857 else
12859 /* Update the length of the final CONSTRUCTOR
12860 arguments vector, and note that we will need to
12861 copy.*/
12862 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12863 need_copy_p = true;
12866 else
12867 ce->value = RECUR (ce->value);
12870 if (need_copy_p)
12872 VEC(constructor_elt,gc) *old_n = n;
12874 n = VEC_alloc (constructor_elt, gc, newlen);
12875 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
12877 if (TREE_CODE (ce->value) == TREE_VEC)
12879 int i, len = TREE_VEC_LENGTH (ce->value);
12880 for (i = 0; i < len; ++i)
12881 CONSTRUCTOR_APPEND_ELT (n, 0,
12882 TREE_VEC_ELT (ce->value, i));
12884 else
12885 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12889 r = build_constructor (init_list_type_node, n);
12890 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12892 if (TREE_HAS_CONSTRUCTOR (t))
12893 return finish_compound_literal (type, r);
12895 return r;
12898 case TYPEID_EXPR:
12900 tree operand_0 = TREE_OPERAND (t, 0);
12901 if (TYPE_P (operand_0))
12903 operand_0 = tsubst (operand_0, args, complain, in_decl);
12904 return get_typeid (operand_0);
12906 else
12908 operand_0 = RECUR (operand_0);
12909 return build_typeid (operand_0);
12913 case VAR_DECL:
12914 if (!args)
12915 return t;
12916 /* Fall through */
12918 case PARM_DECL:
12920 tree r = tsubst_copy (t, args, complain, in_decl);
12922 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12923 /* If the original type was a reference, we'll be wrapped in
12924 the appropriate INDIRECT_REF. */
12925 r = convert_from_reference (r);
12926 return r;
12929 case VA_ARG_EXPR:
12930 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12931 tsubst_copy (TREE_TYPE (t), args, complain,
12932 in_decl));
12934 case OFFSETOF_EXPR:
12935 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12937 case TRAIT_EXPR:
12939 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12940 complain, in_decl);
12942 tree type2 = TRAIT_EXPR_TYPE2 (t);
12943 if (type2)
12944 type2 = tsubst_copy (type2, args, complain, in_decl);
12946 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12949 case STMT_EXPR:
12951 tree old_stmt_expr = cur_stmt_expr;
12952 tree stmt_expr = begin_stmt_expr ();
12954 cur_stmt_expr = stmt_expr;
12955 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12956 integral_constant_expression_p);
12957 stmt_expr = finish_stmt_expr (stmt_expr, false);
12958 cur_stmt_expr = old_stmt_expr;
12960 /* If the resulting list of expression statement is empty,
12961 fold it further into void_zero_node. */
12962 if (empty_expr_stmt_p (stmt_expr))
12963 stmt_expr = void_zero_node;
12965 return stmt_expr;
12968 case CONST_DECL:
12969 t = tsubst_copy (t, args, complain, in_decl);
12970 /* As in finish_id_expression, we resolve enumeration constants
12971 to their underlying values. */
12972 if (TREE_CODE (t) == CONST_DECL)
12974 used_types_insert (TREE_TYPE (t));
12975 return DECL_INITIAL (t);
12977 return t;
12979 case LAMBDA_EXPR:
12981 tree r = build_lambda_expr ();
12983 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12984 TREE_TYPE (r) = type;
12985 CLASSTYPE_LAMBDA_EXPR (type) = r;
12987 LAMBDA_EXPR_LOCATION (r)
12988 = LAMBDA_EXPR_LOCATION (t);
12989 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12990 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12991 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12992 LAMBDA_EXPR_DISCRIMINATOR (r)
12993 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12994 LAMBDA_EXPR_CAPTURE_LIST (r)
12995 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12996 LAMBDA_EXPR_THIS_CAPTURE (r)
12997 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12998 LAMBDA_EXPR_EXTRA_SCOPE (r)
12999 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
13001 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13002 determine_visibility (TYPE_NAME (type));
13003 /* Now that we know visibility, instantiate the type so we have a
13004 declaration of the op() for later calls to lambda_function. */
13005 complete_type (type);
13007 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13008 if (type)
13009 apply_lambda_return_type (r, type);
13011 return build_lambda_object (r);
13014 default:
13015 /* Handle Objective-C++ constructs, if appropriate. */
13017 tree subst
13018 = objcp_tsubst_copy_and_build (t, args, complain,
13019 in_decl, /*function_p=*/false);
13020 if (subst)
13021 return subst;
13023 return tsubst_copy (t, args, complain, in_decl);
13026 #undef RECUR
13029 /* Verify that the instantiated ARGS are valid. For type arguments,
13030 make sure that the type's linkage is ok. For non-type arguments,
13031 make sure they are constants if they are integral or enumerations.
13032 Emit an error under control of COMPLAIN, and return TRUE on error. */
13034 static bool
13035 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13037 if (ARGUMENT_PACK_P (t))
13039 tree vec = ARGUMENT_PACK_ARGS (t);
13040 int len = TREE_VEC_LENGTH (vec);
13041 bool result = false;
13042 int i;
13044 for (i = 0; i < len; ++i)
13045 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13046 result = true;
13047 return result;
13049 else if (TYPE_P (t))
13051 /* [basic.link]: A name with no linkage (notably, the name
13052 of a class or enumeration declared in a local scope)
13053 shall not be used to declare an entity with linkage.
13054 This implies that names with no linkage cannot be used as
13055 template arguments
13057 DR 757 relaxes this restriction for C++0x. */
13058 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13059 : no_linkage_check (t, /*relaxed_p=*/false));
13061 if (nt)
13063 /* DR 488 makes use of a type with no linkage cause
13064 type deduction to fail. */
13065 if (complain & tf_error)
13067 if (TYPE_ANONYMOUS_P (nt))
13068 error ("%qT is/uses anonymous type", t);
13069 else
13070 error ("template argument for %qD uses local type %qT",
13071 tmpl, t);
13073 return true;
13075 /* In order to avoid all sorts of complications, we do not
13076 allow variably-modified types as template arguments. */
13077 else if (variably_modified_type_p (t, NULL_TREE))
13079 if (complain & tf_error)
13080 error ("%qT is a variably modified type", t);
13081 return true;
13084 /* A non-type argument of integral or enumerated type must be a
13085 constant. */
13086 else if (TREE_TYPE (t)
13087 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13088 && !TREE_CONSTANT (t))
13090 if (complain & tf_error)
13091 error ("integral expression %qE is not constant", t);
13092 return true;
13094 return false;
13097 static bool
13098 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13100 int ix, len = DECL_NTPARMS (tmpl);
13101 bool result = false;
13103 for (ix = 0; ix != len; ix++)
13105 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13106 result = true;
13108 if (result && (complain & tf_error))
13109 error (" trying to instantiate %qD", tmpl);
13110 return result;
13113 /* Instantiate the indicated variable or function template TMPL with
13114 the template arguments in TARG_PTR. */
13116 tree
13117 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13119 tree targ_ptr = orig_args;
13120 tree fndecl;
13121 tree gen_tmpl;
13122 tree spec;
13123 HOST_WIDE_INT saved_processing_template_decl;
13125 if (tmpl == error_mark_node)
13126 return error_mark_node;
13128 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13130 /* If this function is a clone, handle it specially. */
13131 if (DECL_CLONED_FUNCTION_P (tmpl))
13133 tree spec;
13134 tree clone;
13136 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13137 DECL_CLONED_FUNCTION. */
13138 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13139 targ_ptr, complain);
13140 if (spec == error_mark_node)
13141 return error_mark_node;
13143 /* Look for the clone. */
13144 FOR_EACH_CLONE (clone, spec)
13145 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13146 return clone;
13147 /* We should always have found the clone by now. */
13148 gcc_unreachable ();
13149 return NULL_TREE;
13152 /* Check to see if we already have this specialization. */
13153 gen_tmpl = most_general_template (tmpl);
13154 if (tmpl != gen_tmpl)
13155 /* The TMPL is a partial instantiation. To get a full set of
13156 arguments we must add the arguments used to perform the
13157 partial instantiation. */
13158 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13159 targ_ptr);
13161 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13162 but it doesn't seem to be on the hot path. */
13163 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13165 gcc_assert (tmpl == gen_tmpl
13166 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13167 == spec)
13168 || fndecl == NULL_TREE);
13170 if (spec != NULL_TREE)
13171 return spec;
13173 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13174 complain))
13175 return error_mark_node;
13177 /* We are building a FUNCTION_DECL, during which the access of its
13178 parameters and return types have to be checked. However this
13179 FUNCTION_DECL which is the desired context for access checking
13180 is not built yet. We solve this chicken-and-egg problem by
13181 deferring all checks until we have the FUNCTION_DECL. */
13182 push_deferring_access_checks (dk_deferred);
13184 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13185 (because, for example, we have encountered a non-dependent
13186 function call in the body of a template function and must now
13187 determine which of several overloaded functions will be called),
13188 within the instantiation itself we are not processing a
13189 template. */
13190 saved_processing_template_decl = processing_template_decl;
13191 processing_template_decl = 0;
13192 /* Substitute template parameters to obtain the specialization. */
13193 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13194 targ_ptr, complain, gen_tmpl);
13195 processing_template_decl = saved_processing_template_decl;
13196 if (fndecl == error_mark_node)
13197 return error_mark_node;
13199 /* Now we know the specialization, compute access previously
13200 deferred. */
13201 push_access_scope (fndecl);
13203 /* Some typedefs referenced from within the template code need to be access
13204 checked at template instantiation time, i.e now. These types were
13205 added to the template at parsing time. Let's get those and perfom
13206 the acces checks then. */
13207 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13208 perform_deferred_access_checks ();
13209 pop_access_scope (fndecl);
13210 pop_deferring_access_checks ();
13212 /* The DECL_TI_TEMPLATE should always be the immediate parent
13213 template, not the most general template. */
13214 DECL_TI_TEMPLATE (fndecl) = tmpl;
13216 /* If we've just instantiated the main entry point for a function,
13217 instantiate all the alternate entry points as well. We do this
13218 by cloning the instantiation of the main entry point, not by
13219 instantiating the template clones. */
13220 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13221 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13223 return fndecl;
13226 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13227 NARGS elements of the arguments that are being used when calling
13228 it. TARGS is a vector into which the deduced template arguments
13229 are placed.
13231 Return zero for success, 2 for an incomplete match that doesn't resolve
13232 all the types, and 1 for complete failure. An error message will be
13233 printed only for an incomplete match.
13235 If FN is a conversion operator, or we are trying to produce a specific
13236 specialization, RETURN_TYPE is the return type desired.
13238 The EXPLICIT_TARGS are explicit template arguments provided via a
13239 template-id.
13241 The parameter STRICT is one of:
13243 DEDUCE_CALL:
13244 We are deducing arguments for a function call, as in
13245 [temp.deduct.call].
13247 DEDUCE_CONV:
13248 We are deducing arguments for a conversion function, as in
13249 [temp.deduct.conv].
13251 DEDUCE_EXACT:
13252 We are deducing arguments when doing an explicit instantiation
13253 as in [temp.explicit], when determining an explicit specialization
13254 as in [temp.expl.spec], or when taking the address of a function
13255 template, as in [temp.deduct.funcaddr]. */
13258 fn_type_unification (tree fn,
13259 tree explicit_targs,
13260 tree targs,
13261 const tree *args,
13262 unsigned int nargs,
13263 tree return_type,
13264 unification_kind_t strict,
13265 int flags)
13267 tree parms;
13268 tree fntype;
13269 int result;
13270 bool incomplete_argument_packs_p = false;
13272 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13274 fntype = TREE_TYPE (fn);
13275 if (explicit_targs)
13277 /* [temp.deduct]
13279 The specified template arguments must match the template
13280 parameters in kind (i.e., type, nontype, template), and there
13281 must not be more arguments than there are parameters;
13282 otherwise type deduction fails.
13284 Nontype arguments must match the types of the corresponding
13285 nontype template parameters, or must be convertible to the
13286 types of the corresponding nontype parameters as specified in
13287 _temp.arg.nontype_, otherwise type deduction fails.
13289 All references in the function type of the function template
13290 to the corresponding template parameters are replaced by the
13291 specified template argument values. If a substitution in a
13292 template parameter or in the function type of the function
13293 template results in an invalid type, type deduction fails. */
13294 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13295 int i, len = TREE_VEC_LENGTH (tparms);
13296 tree converted_args;
13297 bool incomplete = false;
13299 if (explicit_targs == error_mark_node)
13300 return 1;
13302 converted_args
13303 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13304 /*require_all_args=*/false,
13305 /*use_default_args=*/false));
13306 if (converted_args == error_mark_node)
13307 return 1;
13309 /* Substitute the explicit args into the function type. This is
13310 necessary so that, for instance, explicitly declared function
13311 arguments can match null pointed constants. If we were given
13312 an incomplete set of explicit args, we must not do semantic
13313 processing during substitution as we could create partial
13314 instantiations. */
13315 for (i = 0; i < len; i++)
13317 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13318 bool parameter_pack = false;
13320 /* Dig out the actual parm. */
13321 if (TREE_CODE (parm) == TYPE_DECL
13322 || TREE_CODE (parm) == TEMPLATE_DECL)
13324 parm = TREE_TYPE (parm);
13325 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13327 else if (TREE_CODE (parm) == PARM_DECL)
13329 parm = DECL_INITIAL (parm);
13330 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13333 if (parameter_pack)
13335 int level, idx;
13336 tree targ;
13337 template_parm_level_and_index (parm, &level, &idx);
13339 /* Mark the argument pack as "incomplete". We could
13340 still deduce more arguments during unification. */
13341 targ = TMPL_ARG (converted_args, level, idx);
13342 if (targ)
13344 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13345 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13346 = ARGUMENT_PACK_ARGS (targ);
13349 /* We have some incomplete argument packs. */
13350 incomplete_argument_packs_p = true;
13354 if (incomplete_argument_packs_p)
13355 /* Any substitution is guaranteed to be incomplete if there
13356 are incomplete argument packs, because we can still deduce
13357 more arguments. */
13358 incomplete = 1;
13359 else
13360 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13362 processing_template_decl += incomplete;
13363 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13364 processing_template_decl -= incomplete;
13366 if (fntype == error_mark_node)
13367 return 1;
13369 /* Place the explicitly specified arguments in TARGS. */
13370 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13371 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13374 /* Never do unification on the 'this' parameter. */
13375 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13377 if (return_type)
13379 tree *new_args;
13381 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13382 new_args = XALLOCAVEC (tree, nargs + 1);
13383 new_args[0] = return_type;
13384 memcpy (new_args + 1, args, nargs * sizeof (tree));
13385 args = new_args;
13386 ++nargs;
13389 /* We allow incomplete unification without an error message here
13390 because the standard doesn't seem to explicitly prohibit it. Our
13391 callers must be ready to deal with unification failures in any
13392 event. */
13393 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13394 targs, parms, args, nargs, /*subr=*/0,
13395 strict, flags);
13397 if (result == 0 && incomplete_argument_packs_p)
13399 int i, len = NUM_TMPL_ARGS (targs);
13401 /* Clear the "incomplete" flags on all argument packs. */
13402 for (i = 0; i < len; i++)
13404 tree arg = TREE_VEC_ELT (targs, i);
13405 if (ARGUMENT_PACK_P (arg))
13407 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13408 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13413 /* Now that we have bindings for all of the template arguments,
13414 ensure that the arguments deduced for the template template
13415 parameters have compatible template parameter lists. We cannot
13416 check this property before we have deduced all template
13417 arguments, because the template parameter types of a template
13418 template parameter might depend on prior template parameters
13419 deduced after the template template parameter. The following
13420 ill-formed example illustrates this issue:
13422 template<typename T, template<T> class C> void f(C<5>, T);
13424 template<int N> struct X {};
13426 void g() {
13427 f(X<5>(), 5l); // error: template argument deduction fails
13430 The template parameter list of 'C' depends on the template type
13431 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13432 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13433 time that we deduce 'C'. */
13434 if (result == 0
13435 && !template_template_parm_bindings_ok_p
13436 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13437 return 1;
13439 if (result == 0)
13440 /* All is well so far. Now, check:
13442 [temp.deduct]
13444 When all template arguments have been deduced, all uses of
13445 template parameters in nondeduced contexts are replaced with
13446 the corresponding deduced argument values. If the
13447 substitution results in an invalid type, as described above,
13448 type deduction fails. */
13450 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13451 if (substed == error_mark_node)
13452 return 1;
13454 /* If we're looking for an exact match, check that what we got
13455 is indeed an exact match. It might not be if some template
13456 parameters are used in non-deduced contexts. */
13457 if (strict == DEDUCE_EXACT)
13459 unsigned int i;
13461 tree sarg
13462 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13463 if (return_type)
13464 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13465 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13466 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13467 return 1;
13471 return result;
13474 /* Adjust types before performing type deduction, as described in
13475 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13476 sections are symmetric. PARM is the type of a function parameter
13477 or the return type of the conversion function. ARG is the type of
13478 the argument passed to the call, or the type of the value
13479 initialized with the result of the conversion function.
13480 ARG_EXPR is the original argument expression, which may be null. */
13482 static int
13483 maybe_adjust_types_for_deduction (unification_kind_t strict,
13484 tree* parm,
13485 tree* arg,
13486 tree arg_expr)
13488 int result = 0;
13490 switch (strict)
13492 case DEDUCE_CALL:
13493 break;
13495 case DEDUCE_CONV:
13497 /* Swap PARM and ARG throughout the remainder of this
13498 function; the handling is precisely symmetric since PARM
13499 will initialize ARG rather than vice versa. */
13500 tree* temp = parm;
13501 parm = arg;
13502 arg = temp;
13503 break;
13506 case DEDUCE_EXACT:
13507 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13508 too, but here handle it by stripping the reference from PARM
13509 rather than by adding it to ARG. */
13510 if (TREE_CODE (*parm) == REFERENCE_TYPE
13511 && TYPE_REF_IS_RVALUE (*parm)
13512 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13513 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13514 && TREE_CODE (*arg) == REFERENCE_TYPE
13515 && !TYPE_REF_IS_RVALUE (*arg))
13516 *parm = TREE_TYPE (*parm);
13517 /* Nothing else to do in this case. */
13518 return 0;
13520 default:
13521 gcc_unreachable ();
13524 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13526 /* [temp.deduct.call]
13528 If P is not a reference type:
13530 --If A is an array type, the pointer type produced by the
13531 array-to-pointer standard conversion (_conv.array_) is
13532 used in place of A for type deduction; otherwise,
13534 --If A is a function type, the pointer type produced by
13535 the function-to-pointer standard conversion
13536 (_conv.func_) is used in place of A for type deduction;
13537 otherwise,
13539 --If A is a cv-qualified type, the top level
13540 cv-qualifiers of A's type are ignored for type
13541 deduction. */
13542 if (TREE_CODE (*arg) == ARRAY_TYPE)
13543 *arg = build_pointer_type (TREE_TYPE (*arg));
13544 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13545 *arg = build_pointer_type (*arg);
13546 else
13547 *arg = TYPE_MAIN_VARIANT (*arg);
13550 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13551 of the form T&&, where T is a template parameter, and the argument
13552 is an lvalue, T is deduced as A& */
13553 if (TREE_CODE (*parm) == REFERENCE_TYPE
13554 && TYPE_REF_IS_RVALUE (*parm)
13555 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13556 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13557 && arg_expr && real_lvalue_p (arg_expr))
13558 *arg = build_reference_type (*arg);
13560 /* [temp.deduct.call]
13562 If P is a cv-qualified type, the top level cv-qualifiers
13563 of P's type are ignored for type deduction. If P is a
13564 reference type, the type referred to by P is used for
13565 type deduction. */
13566 *parm = TYPE_MAIN_VARIANT (*parm);
13567 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13569 *parm = TREE_TYPE (*parm);
13570 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13573 /* DR 322. For conversion deduction, remove a reference type on parm
13574 too (which has been swapped into ARG). */
13575 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13576 *arg = TREE_TYPE (*arg);
13578 return result;
13581 /* Most parms like fn_type_unification.
13583 If SUBR is 1, we're being called recursively (to unify the
13584 arguments of a function or method parameter of a function
13585 template). */
13587 static int
13588 type_unification_real (tree tparms,
13589 tree targs,
13590 tree xparms,
13591 const tree *xargs,
13592 unsigned int xnargs,
13593 int subr,
13594 unification_kind_t strict,
13595 int flags)
13597 tree parm, arg, arg_expr;
13598 int i;
13599 int ntparms = TREE_VEC_LENGTH (tparms);
13600 int sub_strict;
13601 int saw_undeduced = 0;
13602 tree parms;
13603 const tree *args;
13604 unsigned int nargs;
13605 unsigned int ia;
13607 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13608 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13609 gcc_assert (ntparms > 0);
13611 /* Reset the number of non-defaulted template arguments contained
13612 in in TARGS. */
13613 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13615 switch (strict)
13617 case DEDUCE_CALL:
13618 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13619 | UNIFY_ALLOW_DERIVED);
13620 break;
13622 case DEDUCE_CONV:
13623 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13624 break;
13626 case DEDUCE_EXACT:
13627 sub_strict = UNIFY_ALLOW_NONE;
13628 break;
13630 default:
13631 gcc_unreachable ();
13634 again:
13635 parms = xparms;
13636 args = xargs;
13637 nargs = xnargs;
13639 ia = 0;
13640 while (parms && parms != void_list_node
13641 && ia < nargs)
13643 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13644 break;
13646 parm = TREE_VALUE (parms);
13647 parms = TREE_CHAIN (parms);
13648 arg = args[ia];
13649 ++ia;
13650 arg_expr = NULL;
13652 if (arg == error_mark_node)
13653 return 1;
13654 if (arg == unknown_type_node)
13655 /* We can't deduce anything from this, but we might get all the
13656 template args from other function args. */
13657 continue;
13659 /* Conversions will be performed on a function argument that
13660 corresponds with a function parameter that contains only
13661 non-deducible template parameters and explicitly specified
13662 template parameters. */
13663 if (!uses_template_parms (parm))
13665 tree type;
13667 if (!TYPE_P (arg))
13668 type = TREE_TYPE (arg);
13669 else
13670 type = arg;
13672 if (same_type_p (parm, type))
13673 continue;
13674 if (strict != DEDUCE_EXACT
13675 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13676 flags))
13677 continue;
13679 return 1;
13682 if (!TYPE_P (arg))
13684 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13685 if (type_unknown_p (arg))
13687 /* [temp.deduct.type]
13689 A template-argument can be deduced from a pointer to
13690 function or pointer to member function argument if
13691 the set of overloaded functions does not contain
13692 function templates and at most one of a set of
13693 overloaded functions provides a unique match. */
13694 if (resolve_overloaded_unification
13695 (tparms, targs, parm, arg, strict, sub_strict))
13696 continue;
13698 return 1;
13700 arg_expr = arg;
13701 arg = unlowered_expr_type (arg);
13702 if (arg == error_mark_node)
13703 return 1;
13707 int arg_strict = sub_strict;
13709 if (!subr)
13710 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13711 arg_expr);
13713 if (arg == init_list_type_node && arg_expr)
13714 arg = arg_expr;
13715 if (unify (tparms, targs, parm, arg, arg_strict))
13716 return 1;
13721 if (parms
13722 && parms != void_list_node
13723 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13725 /* Unify the remaining arguments with the pack expansion type. */
13726 tree argvec;
13727 tree parmvec = make_tree_vec (1);
13729 /* Allocate a TREE_VEC and copy in all of the arguments */
13730 argvec = make_tree_vec (nargs - ia);
13731 for (i = 0; ia < nargs; ++ia, ++i)
13732 TREE_VEC_ELT (argvec, i) = args[ia];
13734 /* Copy the parameter into parmvec. */
13735 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13736 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13737 /*call_args_p=*/true, /*subr=*/subr))
13738 return 1;
13740 /* Advance to the end of the list of parameters. */
13741 parms = TREE_CHAIN (parms);
13744 /* Fail if we've reached the end of the parm list, and more args
13745 are present, and the parm list isn't variadic. */
13746 if (ia < nargs && parms == void_list_node)
13747 return 1;
13748 /* Fail if parms are left and they don't have default values. */
13749 if (parms && parms != void_list_node
13750 && TREE_PURPOSE (parms) == NULL_TREE)
13751 return 1;
13753 if (!subr)
13754 for (i = 0; i < ntparms; i++)
13755 if (!TREE_VEC_ELT (targs, i))
13757 tree tparm;
13759 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13760 continue;
13762 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13764 /* If this is an undeduced nontype parameter that depends on
13765 a type parameter, try another pass; its type may have been
13766 deduced from a later argument than the one from which
13767 this parameter can be deduced. */
13768 if (TREE_CODE (tparm) == PARM_DECL
13769 && uses_template_parms (TREE_TYPE (tparm))
13770 && !saw_undeduced++)
13771 goto again;
13773 /* Core issue #226 (C++0x) [temp.deduct]:
13775 If a template argument has not been deduced, its
13776 default template argument, if any, is used.
13778 When we are in C++98 mode, TREE_PURPOSE will either
13779 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13780 to explicitly check cxx_dialect here. */
13781 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13783 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13784 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13785 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13786 arg = convert_template_argument (parm, arg, targs, tf_none,
13787 i, NULL_TREE);
13788 if (arg == error_mark_node)
13789 return 1;
13790 else
13792 TREE_VEC_ELT (targs, i) = arg;
13793 /* The position of the first default template argument,
13794 is also the number of non-defaulted arguments in TARGS.
13795 Record that. */
13796 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
13798 continue;
13802 /* If the type parameter is a parameter pack, then it will
13803 be deduced to an empty parameter pack. */
13804 if (template_parameter_pack_p (tparm))
13806 tree arg;
13808 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13810 arg = make_node (NONTYPE_ARGUMENT_PACK);
13811 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13812 TREE_CONSTANT (arg) = 1;
13814 else
13815 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13817 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13819 TREE_VEC_ELT (targs, i) = arg;
13820 continue;
13823 return 2;
13825 #ifdef ENABLE_CHECKING
13826 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
13828 #endif
13830 return 0;
13833 /* Subroutine of type_unification_real. Args are like the variables
13834 at the call site. ARG is an overloaded function (or template-id);
13835 we try deducing template args from each of the overloads, and if
13836 only one succeeds, we go with that. Modifies TARGS and returns
13837 true on success. */
13839 static bool
13840 resolve_overloaded_unification (tree tparms,
13841 tree targs,
13842 tree parm,
13843 tree arg,
13844 unification_kind_t strict,
13845 int sub_strict)
13847 tree tempargs = copy_node (targs);
13848 int good = 0;
13849 tree goodfn = NULL_TREE;
13850 bool addr_p;
13852 if (TREE_CODE (arg) == ADDR_EXPR)
13854 arg = TREE_OPERAND (arg, 0);
13855 addr_p = true;
13857 else
13858 addr_p = false;
13860 if (TREE_CODE (arg) == COMPONENT_REF)
13861 /* Handle `&x' where `x' is some static or non-static member
13862 function name. */
13863 arg = TREE_OPERAND (arg, 1);
13865 if (TREE_CODE (arg) == OFFSET_REF)
13866 arg = TREE_OPERAND (arg, 1);
13868 /* Strip baselink information. */
13869 if (BASELINK_P (arg))
13870 arg = BASELINK_FUNCTIONS (arg);
13872 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13874 /* If we got some explicit template args, we need to plug them into
13875 the affected templates before we try to unify, in case the
13876 explicit args will completely resolve the templates in question. */
13878 tree expl_subargs = TREE_OPERAND (arg, 1);
13879 arg = TREE_OPERAND (arg, 0);
13881 for (; arg; arg = OVL_NEXT (arg))
13883 tree fn = OVL_CURRENT (arg);
13884 tree subargs, elem;
13886 if (TREE_CODE (fn) != TEMPLATE_DECL)
13887 continue;
13889 ++processing_template_decl;
13890 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13891 expl_subargs, /*check_ret=*/false);
13892 if (subargs)
13894 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13895 if (try_one_overload (tparms, targs, tempargs, parm,
13896 elem, strict, sub_strict, addr_p)
13897 && (!goodfn || !decls_match (goodfn, elem)))
13899 goodfn = elem;
13900 ++good;
13903 --processing_template_decl;
13906 else if (TREE_CODE (arg) != OVERLOAD
13907 && TREE_CODE (arg) != FUNCTION_DECL)
13908 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13909 -- but the deduction does not succeed because the expression is
13910 not just the function on its own. */
13911 return false;
13912 else
13913 for (; arg; arg = OVL_NEXT (arg))
13914 if (try_one_overload (tparms, targs, tempargs, parm,
13915 TREE_TYPE (OVL_CURRENT (arg)),
13916 strict, sub_strict, addr_p)
13917 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13919 goodfn = OVL_CURRENT (arg);
13920 ++good;
13923 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13924 to function or pointer to member function argument if the set of
13925 overloaded functions does not contain function templates and at most
13926 one of a set of overloaded functions provides a unique match.
13928 So if we found multiple possibilities, we return success but don't
13929 deduce anything. */
13931 if (good == 1)
13933 int i = TREE_VEC_LENGTH (targs);
13934 for (; i--; )
13935 if (TREE_VEC_ELT (tempargs, i))
13936 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13938 if (good)
13939 return true;
13941 return false;
13944 /* Core DR 115: In contexts where deduction is done and fails, or in
13945 contexts where deduction is not done, if a template argument list is
13946 specified and it, along with any default template arguments, identifies
13947 a single function template specialization, then the template-id is an
13948 lvalue for the function template specialization. */
13950 tree
13951 resolve_nondeduced_context (tree orig_expr)
13953 tree expr, offset, baselink;
13954 bool addr;
13956 if (!type_unknown_p (orig_expr))
13957 return orig_expr;
13959 expr = orig_expr;
13960 addr = false;
13961 offset = NULL_TREE;
13962 baselink = NULL_TREE;
13964 if (TREE_CODE (expr) == ADDR_EXPR)
13966 expr = TREE_OPERAND (expr, 0);
13967 addr = true;
13969 if (TREE_CODE (expr) == OFFSET_REF)
13971 offset = expr;
13972 expr = TREE_OPERAND (expr, 1);
13974 if (TREE_CODE (expr) == BASELINK)
13976 baselink = expr;
13977 expr = BASELINK_FUNCTIONS (expr);
13980 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13982 int good = 0;
13983 tree goodfn = NULL_TREE;
13985 /* If we got some explicit template args, we need to plug them into
13986 the affected templates before we try to unify, in case the
13987 explicit args will completely resolve the templates in question. */
13989 tree expl_subargs = TREE_OPERAND (expr, 1);
13990 tree arg = TREE_OPERAND (expr, 0);
13991 tree badfn = NULL_TREE;
13992 tree badargs = NULL_TREE;
13994 for (; arg; arg = OVL_NEXT (arg))
13996 tree fn = OVL_CURRENT (arg);
13997 tree subargs, elem;
13999 if (TREE_CODE (fn) != TEMPLATE_DECL)
14000 continue;
14002 ++processing_template_decl;
14003 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14004 expl_subargs, /*check_ret=*/false);
14005 if (subargs && !any_dependent_template_arguments_p (subargs))
14007 elem = instantiate_template (fn, subargs, tf_none);
14008 if (elem == error_mark_node)
14010 badfn = fn;
14011 badargs = subargs;
14013 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
14015 goodfn = elem;
14016 ++good;
14019 --processing_template_decl;
14021 if (good == 1)
14023 expr = goodfn;
14024 if (baselink)
14025 expr = build_baselink (BASELINK_BINFO (baselink),
14026 BASELINK_ACCESS_BINFO (baselink),
14027 expr, BASELINK_OPTYPE (baselink));
14028 if (offset)
14029 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
14030 TREE_OPERAND (offset, 0), expr);
14031 if (addr)
14032 expr = build_address (expr);
14033 return expr;
14035 else if (good == 0 && badargs)
14036 /* There were no good options and at least one bad one, so let the
14037 user know what the problem is. */
14038 instantiate_template (badfn, badargs, tf_warning_or_error);
14040 return orig_expr;
14043 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14044 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14045 different overloads deduce different arguments for a given parm.
14046 ADDR_P is true if the expression for which deduction is being
14047 performed was of the form "& fn" rather than simply "fn".
14049 Returns 1 on success. */
14051 static int
14052 try_one_overload (tree tparms,
14053 tree orig_targs,
14054 tree targs,
14055 tree parm,
14056 tree arg,
14057 unification_kind_t strict,
14058 int sub_strict,
14059 bool addr_p)
14061 int nargs;
14062 tree tempargs;
14063 int i;
14065 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14066 to function or pointer to member function argument if the set of
14067 overloaded functions does not contain function templates and at most
14068 one of a set of overloaded functions provides a unique match.
14070 So if this is a template, just return success. */
14072 if (uses_template_parms (arg))
14073 return 1;
14075 if (TREE_CODE (arg) == METHOD_TYPE)
14076 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14077 else if (addr_p)
14078 arg = build_pointer_type (arg);
14080 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14082 /* We don't copy orig_targs for this because if we have already deduced
14083 some template args from previous args, unify would complain when we
14084 try to deduce a template parameter for the same argument, even though
14085 there isn't really a conflict. */
14086 nargs = TREE_VEC_LENGTH (targs);
14087 tempargs = make_tree_vec (nargs);
14089 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14090 return 0;
14092 /* First make sure we didn't deduce anything that conflicts with
14093 explicitly specified args. */
14094 for (i = nargs; i--; )
14096 tree elt = TREE_VEC_ELT (tempargs, i);
14097 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14099 if (!elt)
14100 /*NOP*/;
14101 else if (uses_template_parms (elt))
14102 /* Since we're unifying against ourselves, we will fill in
14103 template args used in the function parm list with our own
14104 template parms. Discard them. */
14105 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14106 else if (oldelt && !template_args_equal (oldelt, elt))
14107 return 0;
14110 for (i = nargs; i--; )
14112 tree elt = TREE_VEC_ELT (tempargs, i);
14114 if (elt)
14115 TREE_VEC_ELT (targs, i) = elt;
14118 return 1;
14121 /* PARM is a template class (perhaps with unbound template
14122 parameters). ARG is a fully instantiated type. If ARG can be
14123 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14124 TARGS are as for unify. */
14126 static tree
14127 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14129 tree copy_of_targs;
14131 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14132 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14133 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14134 return NULL_TREE;
14136 /* We need to make a new template argument vector for the call to
14137 unify. If we used TARGS, we'd clutter it up with the result of
14138 the attempted unification, even if this class didn't work out.
14139 We also don't want to commit ourselves to all the unifications
14140 we've already done, since unification is supposed to be done on
14141 an argument-by-argument basis. In other words, consider the
14142 following pathological case:
14144 template <int I, int J, int K>
14145 struct S {};
14147 template <int I, int J>
14148 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14150 template <int I, int J, int K>
14151 void f(S<I, J, K>, S<I, I, I>);
14153 void g() {
14154 S<0, 0, 0> s0;
14155 S<0, 1, 2> s2;
14157 f(s0, s2);
14160 Now, by the time we consider the unification involving `s2', we
14161 already know that we must have `f<0, 0, 0>'. But, even though
14162 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14163 because there are two ways to unify base classes of S<0, 1, 2>
14164 with S<I, I, I>. If we kept the already deduced knowledge, we
14165 would reject the possibility I=1. */
14166 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14168 /* If unification failed, we're done. */
14169 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14170 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14171 return NULL_TREE;
14173 return arg;
14176 /* Given a template type PARM and a class type ARG, find the unique
14177 base type in ARG that is an instance of PARM. We do not examine
14178 ARG itself; only its base-classes. If there is not exactly one
14179 appropriate base class, return NULL_TREE. PARM may be the type of
14180 a partial specialization, as well as a plain template type. Used
14181 by unify. */
14183 static tree
14184 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14186 tree rval = NULL_TREE;
14187 tree binfo;
14189 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14191 binfo = TYPE_BINFO (complete_type (arg));
14192 if (!binfo)
14193 /* The type could not be completed. */
14194 return NULL_TREE;
14196 /* Walk in inheritance graph order. The search order is not
14197 important, and this avoids multiple walks of virtual bases. */
14198 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14200 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14202 if (r)
14204 /* If there is more than one satisfactory baseclass, then:
14206 [temp.deduct.call]
14208 If they yield more than one possible deduced A, the type
14209 deduction fails.
14211 applies. */
14212 if (rval && !same_type_p (r, rval))
14213 return NULL_TREE;
14215 rval = r;
14219 return rval;
14222 /* Returns the level of DECL, which declares a template parameter. */
14224 static int
14225 template_decl_level (tree decl)
14227 switch (TREE_CODE (decl))
14229 case TYPE_DECL:
14230 case TEMPLATE_DECL:
14231 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14233 case PARM_DECL:
14234 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14236 default:
14237 gcc_unreachable ();
14239 return 0;
14242 /* Decide whether ARG can be unified with PARM, considering only the
14243 cv-qualifiers of each type, given STRICT as documented for unify.
14244 Returns nonzero iff the unification is OK on that basis. */
14246 static int
14247 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14249 int arg_quals = cp_type_quals (arg);
14250 int parm_quals = cp_type_quals (parm);
14252 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14253 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14255 /* Although a CVR qualifier is ignored when being applied to a
14256 substituted template parameter ([8.3.2]/1 for example), that
14257 does not allow us to unify "const T" with "int&" because both
14258 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14259 It is ok when we're allowing additional CV qualifiers
14260 at the outer level [14.8.2.1]/3,1st bullet. */
14261 if ((TREE_CODE (arg) == REFERENCE_TYPE
14262 || TREE_CODE (arg) == FUNCTION_TYPE
14263 || TREE_CODE (arg) == METHOD_TYPE)
14264 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14265 return 0;
14267 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14268 && (parm_quals & TYPE_QUAL_RESTRICT))
14269 return 0;
14272 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14273 && (arg_quals & parm_quals) != parm_quals)
14274 return 0;
14276 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14277 && (parm_quals & arg_quals) != arg_quals)
14278 return 0;
14280 return 1;
14283 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14284 void
14285 template_parm_level_and_index (tree parm, int* level, int* index)
14287 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14288 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14289 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14291 *index = TEMPLATE_TYPE_IDX (parm);
14292 *level = TEMPLATE_TYPE_LEVEL (parm);
14294 else
14296 *index = TEMPLATE_PARM_IDX (parm);
14297 *level = TEMPLATE_PARM_LEVEL (parm);
14301 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14302 expansion at the end of PACKED_PARMS. Returns 0 if the type
14303 deduction succeeds, 1 otherwise. STRICT is the same as in
14304 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14305 call argument list. We'll need to adjust the arguments to make them
14306 types. SUBR tells us if this is from a recursive call to
14307 type_unification_real. */
14309 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14310 tree packed_args, int strict, bool call_args_p,
14311 bool subr)
14313 tree parm
14314 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14315 tree pattern = PACK_EXPANSION_PATTERN (parm);
14316 tree pack, packs = NULL_TREE;
14317 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14318 int len = TREE_VEC_LENGTH (packed_args);
14320 /* Determine the parameter packs we will be deducing from the
14321 pattern, and record their current deductions. */
14322 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14323 pack; pack = TREE_CHAIN (pack))
14325 tree parm_pack = TREE_VALUE (pack);
14326 int idx, level;
14328 /* Determine the index and level of this parameter pack. */
14329 template_parm_level_and_index (parm_pack, &level, &idx);
14331 /* Keep track of the parameter packs and their corresponding
14332 argument packs. */
14333 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14334 TREE_TYPE (packs) = make_tree_vec (len - start);
14337 /* Loop through all of the arguments that have not yet been
14338 unified and unify each with the pattern. */
14339 for (i = start; i < len; i++)
14341 tree parm = pattern;
14343 /* For each parameter pack, clear out the deduced value so that
14344 we can deduce it again. */
14345 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14347 int idx, level;
14348 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14350 TMPL_ARG (targs, level, idx) = NULL_TREE;
14353 /* Unify the pattern with the current argument. */
14355 tree arg = TREE_VEC_ELT (packed_args, i);
14356 tree arg_expr = NULL_TREE;
14357 int arg_strict = strict;
14358 bool skip_arg_p = false;
14360 if (call_args_p)
14362 int sub_strict;
14364 /* This mirrors what we do in type_unification_real. */
14365 switch (strict)
14367 case DEDUCE_CALL:
14368 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14369 | UNIFY_ALLOW_MORE_CV_QUAL
14370 | UNIFY_ALLOW_DERIVED);
14371 break;
14373 case DEDUCE_CONV:
14374 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14375 break;
14377 case DEDUCE_EXACT:
14378 sub_strict = UNIFY_ALLOW_NONE;
14379 break;
14381 default:
14382 gcc_unreachable ();
14385 if (!TYPE_P (arg))
14387 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14388 if (type_unknown_p (arg))
14390 /* [temp.deduct.type] A template-argument can be
14391 deduced from a pointer to function or pointer
14392 to member function argument if the set of
14393 overloaded functions does not contain function
14394 templates and at most one of a set of
14395 overloaded functions provides a unique
14396 match. */
14398 if (resolve_overloaded_unification
14399 (tparms, targs, parm, arg,
14400 (unification_kind_t) strict,
14401 sub_strict)
14402 != 0)
14403 return 1;
14404 skip_arg_p = true;
14407 if (!skip_arg_p)
14409 arg_expr = arg;
14410 arg = unlowered_expr_type (arg);
14411 if (arg == error_mark_node)
14412 return 1;
14416 arg_strict = sub_strict;
14418 if (!subr)
14419 arg_strict |=
14420 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14421 &parm, &arg, arg_expr);
14424 if (!skip_arg_p)
14426 /* For deduction from an init-list we need the actual list. */
14427 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14428 arg = arg_expr;
14429 if (unify (tparms, targs, parm, arg, arg_strict))
14430 return 1;
14434 /* For each parameter pack, collect the deduced value. */
14435 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14437 int idx, level;
14438 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14440 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14441 TMPL_ARG (targs, level, idx);
14445 /* Verify that the results of unification with the parameter packs
14446 produce results consistent with what we've seen before, and make
14447 the deduced argument packs available. */
14448 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14450 tree old_pack = TREE_VALUE (pack);
14451 tree new_args = TREE_TYPE (pack);
14452 int i, len = TREE_VEC_LENGTH (new_args);
14453 int idx, level;
14454 bool nondeduced_p = false;
14456 /* By default keep the original deduced argument pack.
14457 If necessary, more specific code is going to update the
14458 resulting deduced argument later down in this function. */
14459 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14460 TMPL_ARG (targs, level, idx) = old_pack;
14462 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14463 actually deduce anything. */
14464 for (i = 0; i < len && !nondeduced_p; ++i)
14465 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14466 nondeduced_p = true;
14467 if (nondeduced_p)
14468 continue;
14470 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14472 /* Prepend the explicit arguments onto NEW_ARGS. */
14473 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14474 tree old_args = new_args;
14475 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14476 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14478 /* Copy the explicit arguments. */
14479 new_args = make_tree_vec (len);
14480 for (i = 0; i < explicit_len; i++)
14481 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14483 /* Copy the deduced arguments. */
14484 for (; i < len; i++)
14485 TREE_VEC_ELT (new_args, i) =
14486 TREE_VEC_ELT (old_args, i - explicit_len);
14489 if (!old_pack)
14491 tree result;
14492 /* Build the deduced *_ARGUMENT_PACK. */
14493 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14495 result = make_node (NONTYPE_ARGUMENT_PACK);
14496 TREE_TYPE (result) =
14497 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14498 TREE_CONSTANT (result) = 1;
14500 else
14501 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14503 SET_ARGUMENT_PACK_ARGS (result, new_args);
14505 /* Note the deduced argument packs for this parameter
14506 pack. */
14507 TMPL_ARG (targs, level, idx) = result;
14509 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14510 && (ARGUMENT_PACK_ARGS (old_pack)
14511 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14513 /* We only had the explicitly-provided arguments before, but
14514 now we have a complete set of arguments. */
14515 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14517 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14518 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14519 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14521 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14522 new_args))
14523 /* Inconsistent unification of this parameter pack. */
14524 return 1;
14527 return 0;
14530 /* Deduce the value of template parameters. TPARMS is the (innermost)
14531 set of template parameters to a template. TARGS is the bindings
14532 for those template parameters, as determined thus far; TARGS may
14533 include template arguments for outer levels of template parameters
14534 as well. PARM is a parameter to a template function, or a
14535 subcomponent of that parameter; ARG is the corresponding argument.
14536 This function attempts to match PARM with ARG in a manner
14537 consistent with the existing assignments in TARGS. If more values
14538 are deduced, then TARGS is updated.
14540 Returns 0 if the type deduction succeeds, 1 otherwise. The
14541 parameter STRICT is a bitwise or of the following flags:
14543 UNIFY_ALLOW_NONE:
14544 Require an exact match between PARM and ARG.
14545 UNIFY_ALLOW_MORE_CV_QUAL:
14546 Allow the deduced ARG to be more cv-qualified (by qualification
14547 conversion) than ARG.
14548 UNIFY_ALLOW_LESS_CV_QUAL:
14549 Allow the deduced ARG to be less cv-qualified than ARG.
14550 UNIFY_ALLOW_DERIVED:
14551 Allow the deduced ARG to be a template base class of ARG,
14552 or a pointer to a template base class of the type pointed to by
14553 ARG.
14554 UNIFY_ALLOW_INTEGER:
14555 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14556 case for more information.
14557 UNIFY_ALLOW_OUTER_LEVEL:
14558 This is the outermost level of a deduction. Used to determine validity
14559 of qualification conversions. A valid qualification conversion must
14560 have const qualified pointers leading up to the inner type which
14561 requires additional CV quals, except at the outer level, where const
14562 is not required [conv.qual]. It would be normal to set this flag in
14563 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14564 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14565 This is the outermost level of a deduction, and PARM can be more CV
14566 qualified at this point.
14567 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14568 This is the outermost level of a deduction, and PARM can be less CV
14569 qualified at this point. */
14571 static int
14572 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14574 int idx;
14575 tree targ;
14576 tree tparm;
14577 int strict_in = strict;
14579 /* I don't think this will do the right thing with respect to types.
14580 But the only case I've seen it in so far has been array bounds, where
14581 signedness is the only information lost, and I think that will be
14582 okay. */
14583 while (TREE_CODE (parm) == NOP_EXPR)
14584 parm = TREE_OPERAND (parm, 0);
14586 if (arg == error_mark_node)
14587 return 1;
14588 if (arg == unknown_type_node
14589 || arg == init_list_type_node)
14590 /* We can't deduce anything from this, but we might get all the
14591 template args from other function args. */
14592 return 0;
14594 /* If PARM uses template parameters, then we can't bail out here,
14595 even if ARG == PARM, since we won't record unifications for the
14596 template parameters. We might need them if we're trying to
14597 figure out which of two things is more specialized. */
14598 if (arg == parm && !uses_template_parms (parm))
14599 return 0;
14601 /* Handle init lists early, so the rest of the function can assume
14602 we're dealing with a type. */
14603 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14605 tree elt, elttype;
14606 unsigned i;
14607 tree orig_parm = parm;
14609 /* Replace T with std::initializer_list<T> for deduction. */
14610 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14611 && flag_deduce_init_list)
14612 parm = listify (parm);
14614 if (!is_std_init_list (parm))
14615 /* We can only deduce from an initializer list argument if the
14616 parameter is std::initializer_list; otherwise this is a
14617 non-deduced context. */
14618 return 0;
14620 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14622 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14624 int elt_strict = strict;
14626 if (elt == error_mark_node)
14627 return 1;
14629 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14631 tree type = TREE_TYPE (elt);
14632 /* It should only be possible to get here for a call. */
14633 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14634 elt_strict |= maybe_adjust_types_for_deduction
14635 (DEDUCE_CALL, &elttype, &type, elt);
14636 elt = type;
14639 if (unify (tparms, targs, elttype, elt, elt_strict))
14640 return 1;
14643 /* If the std::initializer_list<T> deduction worked, replace the
14644 deduced A with std::initializer_list<A>. */
14645 if (orig_parm != parm)
14647 idx = TEMPLATE_TYPE_IDX (orig_parm);
14648 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14649 targ = listify (targ);
14650 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14652 return 0;
14655 /* Immediately reject some pairs that won't unify because of
14656 cv-qualification mismatches. */
14657 if (TREE_CODE (arg) == TREE_CODE (parm)
14658 && TYPE_P (arg)
14659 /* It is the elements of the array which hold the cv quals of an array
14660 type, and the elements might be template type parms. We'll check
14661 when we recurse. */
14662 && TREE_CODE (arg) != ARRAY_TYPE
14663 /* We check the cv-qualifiers when unifying with template type
14664 parameters below. We want to allow ARG `const T' to unify with
14665 PARM `T' for example, when computing which of two templates
14666 is more specialized, for example. */
14667 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14668 && !check_cv_quals_for_unify (strict_in, arg, parm))
14669 return 1;
14671 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14672 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14673 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14674 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14675 strict &= ~UNIFY_ALLOW_DERIVED;
14676 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14677 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14679 switch (TREE_CODE (parm))
14681 case TYPENAME_TYPE:
14682 case SCOPE_REF:
14683 case UNBOUND_CLASS_TEMPLATE:
14684 /* In a type which contains a nested-name-specifier, template
14685 argument values cannot be deduced for template parameters used
14686 within the nested-name-specifier. */
14687 return 0;
14689 case TEMPLATE_TYPE_PARM:
14690 case TEMPLATE_TEMPLATE_PARM:
14691 case BOUND_TEMPLATE_TEMPLATE_PARM:
14692 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14693 if (tparm == error_mark_node)
14694 return 1;
14696 if (TEMPLATE_TYPE_LEVEL (parm)
14697 != template_decl_level (tparm))
14698 /* The PARM is not one we're trying to unify. Just check
14699 to see if it matches ARG. */
14700 return (TREE_CODE (arg) == TREE_CODE (parm)
14701 && same_type_p (parm, arg)) ? 0 : 1;
14702 idx = TEMPLATE_TYPE_IDX (parm);
14703 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14704 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14706 /* Check for mixed types and values. */
14707 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14708 && TREE_CODE (tparm) != TYPE_DECL)
14709 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14710 && TREE_CODE (tparm) != TEMPLATE_DECL))
14711 return 1;
14713 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14715 /* ARG must be constructed from a template class or a template
14716 template parameter. */
14717 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14718 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14719 return 1;
14722 tree parmvec = TYPE_TI_ARGS (parm);
14723 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14724 tree parm_parms
14725 = DECL_INNERMOST_TEMPLATE_PARMS
14726 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14727 int i, len;
14728 int parm_variadic_p = 0;
14730 /* The resolution to DR150 makes clear that default
14731 arguments for an N-argument may not be used to bind T
14732 to a template template parameter with fewer than N
14733 parameters. It is not safe to permit the binding of
14734 default arguments as an extension, as that may change
14735 the meaning of a conforming program. Consider:
14737 struct Dense { static const unsigned int dim = 1; };
14739 template <template <typename> class View,
14740 typename Block>
14741 void operator+(float, View<Block> const&);
14743 template <typename Block,
14744 unsigned int Dim = Block::dim>
14745 struct Lvalue_proxy { operator float() const; };
14747 void
14748 test_1d (void) {
14749 Lvalue_proxy<Dense> p;
14750 float b;
14751 b + p;
14754 Here, if Lvalue_proxy is permitted to bind to View, then
14755 the global operator+ will be used; if they are not, the
14756 Lvalue_proxy will be converted to float. */
14757 if (coerce_template_parms (parm_parms,
14758 argvec,
14759 TYPE_TI_TEMPLATE (parm),
14760 tf_none,
14761 /*require_all_args=*/true,
14762 /*use_default_args=*/false)
14763 == error_mark_node)
14764 return 1;
14766 /* Deduce arguments T, i from TT<T> or TT<i>.
14767 We check each element of PARMVEC and ARGVEC individually
14768 rather than the whole TREE_VEC since they can have
14769 different number of elements. */
14771 parmvec = expand_template_argument_pack (parmvec);
14772 argvec = expand_template_argument_pack (argvec);
14774 len = TREE_VEC_LENGTH (parmvec);
14776 /* Check if the parameters end in a pack, making them
14777 variadic. */
14778 if (len > 0
14779 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14780 parm_variadic_p = 1;
14782 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14783 return 1;
14785 for (i = 0; i < len - parm_variadic_p; ++i)
14787 if (unify (tparms, targs,
14788 TREE_VEC_ELT (parmvec, i),
14789 TREE_VEC_ELT (argvec, i),
14790 UNIFY_ALLOW_NONE))
14791 return 1;
14794 if (parm_variadic_p
14795 && unify_pack_expansion (tparms, targs,
14796 parmvec, argvec,
14797 UNIFY_ALLOW_NONE,
14798 /*call_args_p=*/false,
14799 /*subr=*/false))
14800 return 1;
14802 arg = TYPE_TI_TEMPLATE (arg);
14804 /* Fall through to deduce template name. */
14807 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14808 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14810 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14812 /* Simple cases: Value already set, does match or doesn't. */
14813 if (targ != NULL_TREE && template_args_equal (targ, arg))
14814 return 0;
14815 else if (targ)
14816 return 1;
14818 else
14820 /* If PARM is `const T' and ARG is only `int', we don't have
14821 a match unless we are allowing additional qualification.
14822 If ARG is `const int' and PARM is just `T' that's OK;
14823 that binds `const int' to `T'. */
14824 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14825 arg, parm))
14826 return 1;
14828 /* Consider the case where ARG is `const volatile int' and
14829 PARM is `const T'. Then, T should be `volatile int'. */
14830 arg = cp_build_qualified_type_real
14831 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14832 if (arg == error_mark_node)
14833 return 1;
14835 /* Simple cases: Value already set, does match or doesn't. */
14836 if (targ != NULL_TREE && same_type_p (targ, arg))
14837 return 0;
14838 else if (targ)
14839 return 1;
14841 /* Make sure that ARG is not a variable-sized array. (Note
14842 that were talking about variable-sized arrays (like
14843 `int[n]'), rather than arrays of unknown size (like
14844 `int[]').) We'll get very confused by such a type since
14845 the bound of the array will not be computable in an
14846 instantiation. Besides, such types are not allowed in
14847 ISO C++, so we can do as we please here. */
14848 if (variably_modified_type_p (arg, NULL_TREE))
14849 return 1;
14851 /* Strip typedefs as in convert_template_argument. */
14852 arg = strip_typedefs (arg);
14855 /* If ARG is a parameter pack or an expansion, we cannot unify
14856 against it unless PARM is also a parameter pack. */
14857 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14858 && !template_parameter_pack_p (parm))
14859 return 1;
14861 /* If the argument deduction results is a METHOD_TYPE,
14862 then there is a problem.
14863 METHOD_TYPE doesn't map to any real C++ type the result of
14864 the deduction can not be of that type. */
14865 if (TREE_CODE (arg) == METHOD_TYPE)
14866 return 1;
14868 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14869 return 0;
14871 case TEMPLATE_PARM_INDEX:
14872 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14873 if (tparm == error_mark_node)
14874 return 1;
14876 if (TEMPLATE_PARM_LEVEL (parm)
14877 != template_decl_level (tparm))
14878 /* The PARM is not one we're trying to unify. Just check
14879 to see if it matches ARG. */
14880 return !(TREE_CODE (arg) == TREE_CODE (parm)
14881 && cp_tree_equal (parm, arg));
14883 idx = TEMPLATE_PARM_IDX (parm);
14884 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14886 if (targ)
14887 return !cp_tree_equal (targ, arg);
14889 /* [temp.deduct.type] If, in the declaration of a function template
14890 with a non-type template-parameter, the non-type
14891 template-parameter is used in an expression in the function
14892 parameter-list and, if the corresponding template-argument is
14893 deduced, the template-argument type shall match the type of the
14894 template-parameter exactly, except that a template-argument
14895 deduced from an array bound may be of any integral type.
14896 The non-type parameter might use already deduced type parameters. */
14897 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14898 if (!TREE_TYPE (arg))
14899 /* Template-parameter dependent expression. Just accept it for now.
14900 It will later be processed in convert_template_argument. */
14902 else if (same_type_p (TREE_TYPE (arg), tparm))
14903 /* OK */;
14904 else if ((strict & UNIFY_ALLOW_INTEGER)
14905 && (TREE_CODE (tparm) == INTEGER_TYPE
14906 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14907 /* Convert the ARG to the type of PARM; the deduced non-type
14908 template argument must exactly match the types of the
14909 corresponding parameter. */
14910 arg = fold (build_nop (tparm, arg));
14911 else if (uses_template_parms (tparm))
14912 /* We haven't deduced the type of this parameter yet. Try again
14913 later. */
14914 return 0;
14915 else
14916 return 1;
14918 /* If ARG is a parameter pack or an expansion, we cannot unify
14919 against it unless PARM is also a parameter pack. */
14920 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14921 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14922 return 1;
14924 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14925 return 0;
14927 case PTRMEM_CST:
14929 /* A pointer-to-member constant can be unified only with
14930 another constant. */
14931 if (TREE_CODE (arg) != PTRMEM_CST)
14932 return 1;
14934 /* Just unify the class member. It would be useless (and possibly
14935 wrong, depending on the strict flags) to unify also
14936 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14937 arg refer to the same variable, even if through different
14938 classes. For instance:
14940 struct A { int x; };
14941 struct B : A { };
14943 Unification of &A::x and &B::x must succeed. */
14944 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14945 PTRMEM_CST_MEMBER (arg), strict);
14948 case POINTER_TYPE:
14950 if (TREE_CODE (arg) != POINTER_TYPE)
14951 return 1;
14953 /* [temp.deduct.call]
14955 A can be another pointer or pointer to member type that can
14956 be converted to the deduced A via a qualification
14957 conversion (_conv.qual_).
14959 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14960 This will allow for additional cv-qualification of the
14961 pointed-to types if appropriate. */
14963 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14964 /* The derived-to-base conversion only persists through one
14965 level of pointers. */
14966 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14968 return unify (tparms, targs, TREE_TYPE (parm),
14969 TREE_TYPE (arg), strict);
14972 case REFERENCE_TYPE:
14973 if (TREE_CODE (arg) != REFERENCE_TYPE)
14974 return 1;
14975 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14976 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14978 case ARRAY_TYPE:
14979 if (TREE_CODE (arg) != ARRAY_TYPE)
14980 return 1;
14981 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14982 != (TYPE_DOMAIN (arg) == NULL_TREE))
14983 return 1;
14984 if (TYPE_DOMAIN (parm) != NULL_TREE)
14986 tree parm_max;
14987 tree arg_max;
14988 bool parm_cst;
14989 bool arg_cst;
14991 /* Our representation of array types uses "N - 1" as the
14992 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14993 not an integer constant. We cannot unify arbitrarily
14994 complex expressions, so we eliminate the MINUS_EXPRs
14995 here. */
14996 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14997 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14998 if (!parm_cst)
15000 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
15001 parm_max = TREE_OPERAND (parm_max, 0);
15003 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
15004 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
15005 if (!arg_cst)
15007 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15008 trying to unify the type of a variable with the type
15009 of a template parameter. For example:
15011 template <unsigned int N>
15012 void f (char (&) [N]);
15013 int g();
15014 void h(int i) {
15015 char a[g(i)];
15016 f(a);
15019 Here, the type of the ARG will be "int [g(i)]", and
15020 may be a SAVE_EXPR, etc. */
15021 if (TREE_CODE (arg_max) != MINUS_EXPR)
15022 return 1;
15023 arg_max = TREE_OPERAND (arg_max, 0);
15026 /* If only one of the bounds used a MINUS_EXPR, compensate
15027 by adding one to the other bound. */
15028 if (parm_cst && !arg_cst)
15029 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15030 integer_type_node,
15031 parm_max,
15032 integer_one_node);
15033 else if (arg_cst && !parm_cst)
15034 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15035 integer_type_node,
15036 arg_max,
15037 integer_one_node);
15039 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15040 return 1;
15042 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15043 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15045 case REAL_TYPE:
15046 case COMPLEX_TYPE:
15047 case VECTOR_TYPE:
15048 case INTEGER_TYPE:
15049 case BOOLEAN_TYPE:
15050 case ENUMERAL_TYPE:
15051 case VOID_TYPE:
15052 if (TREE_CODE (arg) != TREE_CODE (parm))
15053 return 1;
15055 /* We have already checked cv-qualification at the top of the
15056 function. */
15057 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15058 return 1;
15060 /* As far as unification is concerned, this wins. Later checks
15061 will invalidate it if necessary. */
15062 return 0;
15064 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15065 /* Type INTEGER_CST can come from ordinary constant template args. */
15066 case INTEGER_CST:
15067 while (TREE_CODE (arg) == NOP_EXPR)
15068 arg = TREE_OPERAND (arg, 0);
15070 if (TREE_CODE (arg) != INTEGER_CST)
15071 return 1;
15072 return !tree_int_cst_equal (parm, arg);
15074 case TREE_VEC:
15076 int i;
15077 if (TREE_CODE (arg) != TREE_VEC)
15078 return 1;
15079 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15080 return 1;
15081 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15082 if (unify (tparms, targs,
15083 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15084 UNIFY_ALLOW_NONE))
15085 return 1;
15086 return 0;
15089 case RECORD_TYPE:
15090 case UNION_TYPE:
15091 if (TREE_CODE (arg) != TREE_CODE (parm))
15092 return 1;
15094 if (TYPE_PTRMEMFUNC_P (parm))
15096 if (!TYPE_PTRMEMFUNC_P (arg))
15097 return 1;
15099 return unify (tparms, targs,
15100 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15101 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15102 strict);
15105 if (CLASSTYPE_TEMPLATE_INFO (parm))
15107 tree t = NULL_TREE;
15109 if (strict_in & UNIFY_ALLOW_DERIVED)
15111 /* First, we try to unify the PARM and ARG directly. */
15112 t = try_class_unification (tparms, targs,
15113 parm, arg);
15115 if (!t)
15117 /* Fallback to the special case allowed in
15118 [temp.deduct.call]:
15120 If P is a class, and P has the form
15121 template-id, then A can be a derived class of
15122 the deduced A. Likewise, if P is a pointer to
15123 a class of the form template-id, A can be a
15124 pointer to a derived class pointed to by the
15125 deduced A. */
15126 t = get_template_base (tparms, targs, parm, arg);
15128 if (!t)
15129 return 1;
15132 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15133 && (CLASSTYPE_TI_TEMPLATE (parm)
15134 == CLASSTYPE_TI_TEMPLATE (arg)))
15135 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15136 Then, we should unify `int' and `U'. */
15137 t = arg;
15138 else
15139 /* There's no chance of unification succeeding. */
15140 return 1;
15142 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15143 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15145 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15146 return 1;
15147 return 0;
15149 case METHOD_TYPE:
15150 case FUNCTION_TYPE:
15152 unsigned int nargs;
15153 tree *args;
15154 tree a;
15155 unsigned int i;
15157 if (TREE_CODE (arg) != TREE_CODE (parm))
15158 return 1;
15160 /* CV qualifications for methods can never be deduced, they must
15161 match exactly. We need to check them explicitly here,
15162 because type_unification_real treats them as any other
15163 cv-qualified parameter. */
15164 if (TREE_CODE (parm) == METHOD_TYPE
15165 && (!check_cv_quals_for_unify
15166 (UNIFY_ALLOW_NONE,
15167 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15168 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15169 return 1;
15171 if (unify (tparms, targs, TREE_TYPE (parm),
15172 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15173 return 1;
15175 nargs = list_length (TYPE_ARG_TYPES (arg));
15176 args = XALLOCAVEC (tree, nargs);
15177 for (a = TYPE_ARG_TYPES (arg), i = 0;
15178 a != NULL_TREE && a != void_list_node;
15179 a = TREE_CHAIN (a), ++i)
15180 args[i] = TREE_VALUE (a);
15181 nargs = i;
15183 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15184 args, nargs, 1, DEDUCE_EXACT,
15185 LOOKUP_NORMAL);
15188 case OFFSET_TYPE:
15189 /* Unify a pointer to member with a pointer to member function, which
15190 deduces the type of the member as a function type. */
15191 if (TYPE_PTRMEMFUNC_P (arg))
15193 tree method_type;
15194 tree fntype;
15196 /* Check top-level cv qualifiers */
15197 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15198 return 1;
15200 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15201 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15202 return 1;
15204 /* Determine the type of the function we are unifying against. */
15205 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15206 fntype =
15207 build_function_type (TREE_TYPE (method_type),
15208 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15210 /* Extract the cv-qualifiers of the member function from the
15211 implicit object parameter and place them on the function
15212 type to be restored later. */
15213 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15214 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15217 if (TREE_CODE (arg) != OFFSET_TYPE)
15218 return 1;
15219 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15220 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15221 return 1;
15222 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15223 strict);
15225 case CONST_DECL:
15226 if (DECL_TEMPLATE_PARM_P (parm))
15227 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15228 if (arg != integral_constant_value (parm))
15229 return 1;
15230 return 0;
15232 case FIELD_DECL:
15233 case TEMPLATE_DECL:
15234 /* Matched cases are handled by the ARG == PARM test above. */
15235 return 1;
15237 case VAR_DECL:
15238 /* A non-type template parameter that is a variable should be a
15239 an integral constant, in which case, it whould have been
15240 folded into its (constant) value. So we should not be getting
15241 a variable here. */
15242 gcc_unreachable ();
15244 case TYPE_ARGUMENT_PACK:
15245 case NONTYPE_ARGUMENT_PACK:
15247 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15248 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15249 int i, len = TREE_VEC_LENGTH (packed_parms);
15250 int argslen = TREE_VEC_LENGTH (packed_args);
15251 int parm_variadic_p = 0;
15253 for (i = 0; i < len; ++i)
15255 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15257 if (i == len - 1)
15258 /* We can unify against something with a trailing
15259 parameter pack. */
15260 parm_variadic_p = 1;
15261 else
15262 /* Since there is something following the pack
15263 expansion, we cannot unify this template argument
15264 list. */
15265 return 0;
15270 /* If we don't have enough arguments to satisfy the parameters
15271 (not counting the pack expression at the end), or we have
15272 too many arguments for a parameter list that doesn't end in
15273 a pack expression, we can't unify. */
15274 if (argslen < (len - parm_variadic_p)
15275 || (argslen > len && !parm_variadic_p))
15276 return 1;
15278 /* Unify all of the parameters that precede the (optional)
15279 pack expression. */
15280 for (i = 0; i < len - parm_variadic_p; ++i)
15282 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15283 TREE_VEC_ELT (packed_args, i), strict))
15284 return 1;
15287 if (parm_variadic_p)
15288 return unify_pack_expansion (tparms, targs,
15289 packed_parms, packed_args,
15290 strict, /*call_args_p=*/false,
15291 /*subr=*/false);
15292 return 0;
15295 break;
15297 case TYPEOF_TYPE:
15298 case DECLTYPE_TYPE:
15299 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15300 nodes. */
15301 return 0;
15303 case ERROR_MARK:
15304 /* Unification fails if we hit an error node. */
15305 return 1;
15307 default:
15308 gcc_assert (EXPR_P (parm));
15310 /* We must be looking at an expression. This can happen with
15311 something like:
15313 template <int I>
15314 void foo(S<I>, S<I + 2>);
15316 This is a "nondeduced context":
15318 [deduct.type]
15320 The nondeduced contexts are:
15322 --A type that is a template-id in which one or more of
15323 the template-arguments is an expression that references
15324 a template-parameter.
15326 In these cases, we assume deduction succeeded, but don't
15327 actually infer any unifications. */
15329 if (!uses_template_parms (parm)
15330 && !template_args_equal (parm, arg))
15331 return 1;
15332 else
15333 return 0;
15337 /* Note that DECL can be defined in this translation unit, if
15338 required. */
15340 static void
15341 mark_definable (tree decl)
15343 tree clone;
15344 DECL_NOT_REALLY_EXTERN (decl) = 1;
15345 FOR_EACH_CLONE (clone, decl)
15346 DECL_NOT_REALLY_EXTERN (clone) = 1;
15349 /* Called if RESULT is explicitly instantiated, or is a member of an
15350 explicitly instantiated class. */
15352 void
15353 mark_decl_instantiated (tree result, int extern_p)
15355 SET_DECL_EXPLICIT_INSTANTIATION (result);
15357 /* If this entity has already been written out, it's too late to
15358 make any modifications. */
15359 if (TREE_ASM_WRITTEN (result))
15360 return;
15362 if (TREE_CODE (result) != FUNCTION_DECL)
15363 /* The TREE_PUBLIC flag for function declarations will have been
15364 set correctly by tsubst. */
15365 TREE_PUBLIC (result) = 1;
15367 /* This might have been set by an earlier implicit instantiation. */
15368 DECL_COMDAT (result) = 0;
15370 if (extern_p)
15371 DECL_NOT_REALLY_EXTERN (result) = 0;
15372 else
15374 mark_definable (result);
15375 /* Always make artificials weak. */
15376 if (DECL_ARTIFICIAL (result) && flag_weak)
15377 comdat_linkage (result);
15378 /* For WIN32 we also want to put explicit instantiations in
15379 linkonce sections. */
15380 else if (TREE_PUBLIC (result))
15381 maybe_make_one_only (result);
15384 /* If EXTERN_P, then this function will not be emitted -- unless
15385 followed by an explicit instantiation, at which point its linkage
15386 will be adjusted. If !EXTERN_P, then this function will be
15387 emitted here. In neither circumstance do we want
15388 import_export_decl to adjust the linkage. */
15389 DECL_INTERFACE_KNOWN (result) = 1;
15392 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15393 important template arguments. If any are missing, we check whether
15394 they're important by using error_mark_node for substituting into any
15395 args that were used for partial ordering (the ones between ARGS and END)
15396 and seeing if it bubbles up. */
15398 static bool
15399 check_undeduced_parms (tree targs, tree args, tree end)
15401 bool found = false;
15402 int i;
15403 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15404 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15406 found = true;
15407 TREE_VEC_ELT (targs, i) = error_mark_node;
15409 if (found)
15411 for (; args != end; args = TREE_CHAIN (args))
15413 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15414 if (substed == error_mark_node)
15415 return true;
15418 return false;
15421 /* Given two function templates PAT1 and PAT2, return:
15423 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15424 -1 if PAT2 is more specialized than PAT1.
15425 0 if neither is more specialized.
15427 LEN indicates the number of parameters we should consider
15428 (defaulted parameters should not be considered).
15430 The 1998 std underspecified function template partial ordering, and
15431 DR214 addresses the issue. We take pairs of arguments, one from
15432 each of the templates, and deduce them against each other. One of
15433 the templates will be more specialized if all the *other*
15434 template's arguments deduce against its arguments and at least one
15435 of its arguments *does* *not* deduce against the other template's
15436 corresponding argument. Deduction is done as for class templates.
15437 The arguments used in deduction have reference and top level cv
15438 qualifiers removed. Iff both arguments were originally reference
15439 types *and* deduction succeeds in both directions, the template
15440 with the more cv-qualified argument wins for that pairing (if
15441 neither is more cv-qualified, they both are equal). Unlike regular
15442 deduction, after all the arguments have been deduced in this way,
15443 we do *not* verify the deduced template argument values can be
15444 substituted into non-deduced contexts.
15446 The logic can be a bit confusing here, because we look at deduce1 and
15447 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15448 can find template arguments for pat1 to make arg1 look like arg2, that
15449 means that arg2 is at least as specialized as arg1. */
15452 more_specialized_fn (tree pat1, tree pat2, int len)
15454 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15455 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15456 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15457 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15458 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15459 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15460 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15461 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15462 tree origs1, origs2;
15463 bool lose1 = false;
15464 bool lose2 = false;
15466 /* Remove the this parameter from non-static member functions. If
15467 one is a non-static member function and the other is not a static
15468 member function, remove the first parameter from that function
15469 also. This situation occurs for operator functions where we
15470 locate both a member function (with this pointer) and non-member
15471 operator (with explicit first operand). */
15472 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15474 len--; /* LEN is the number of significant arguments for DECL1 */
15475 args1 = TREE_CHAIN (args1);
15476 if (!DECL_STATIC_FUNCTION_P (decl2))
15477 args2 = TREE_CHAIN (args2);
15479 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15481 args2 = TREE_CHAIN (args2);
15482 if (!DECL_STATIC_FUNCTION_P (decl1))
15484 len--;
15485 args1 = TREE_CHAIN (args1);
15489 /* If only one is a conversion operator, they are unordered. */
15490 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15491 return 0;
15493 /* Consider the return type for a conversion function */
15494 if (DECL_CONV_FN_P (decl1))
15496 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15497 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15498 len++;
15501 processing_template_decl++;
15503 origs1 = args1;
15504 origs2 = args2;
15506 while (len--
15507 /* Stop when an ellipsis is seen. */
15508 && args1 != NULL_TREE && args2 != NULL_TREE)
15510 tree arg1 = TREE_VALUE (args1);
15511 tree arg2 = TREE_VALUE (args2);
15512 int deduce1, deduce2;
15513 int quals1 = -1;
15514 int quals2 = -1;
15516 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15517 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15519 /* When both arguments are pack expansions, we need only
15520 unify the patterns themselves. */
15521 arg1 = PACK_EXPANSION_PATTERN (arg1);
15522 arg2 = PACK_EXPANSION_PATTERN (arg2);
15524 /* This is the last comparison we need to do. */
15525 len = 0;
15528 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15530 arg1 = TREE_TYPE (arg1);
15531 quals1 = cp_type_quals (arg1);
15534 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15536 arg2 = TREE_TYPE (arg2);
15537 quals2 = cp_type_quals (arg2);
15540 if ((quals1 < 0) != (quals2 < 0))
15542 /* Only of the args is a reference, see if we should apply
15543 array/function pointer decay to it. This is not part of
15544 DR214, but is, IMHO, consistent with the deduction rules
15545 for the function call itself, and with our earlier
15546 implementation of the underspecified partial ordering
15547 rules. (nathan). */
15548 if (quals1 >= 0)
15550 switch (TREE_CODE (arg1))
15552 case ARRAY_TYPE:
15553 arg1 = TREE_TYPE (arg1);
15554 /* FALLTHROUGH. */
15555 case FUNCTION_TYPE:
15556 arg1 = build_pointer_type (arg1);
15557 break;
15559 default:
15560 break;
15563 else
15565 switch (TREE_CODE (arg2))
15567 case ARRAY_TYPE:
15568 arg2 = TREE_TYPE (arg2);
15569 /* FALLTHROUGH. */
15570 case FUNCTION_TYPE:
15571 arg2 = build_pointer_type (arg2);
15572 break;
15574 default:
15575 break;
15580 arg1 = TYPE_MAIN_VARIANT (arg1);
15581 arg2 = TYPE_MAIN_VARIANT (arg2);
15583 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15585 int i, len2 = list_length (args2);
15586 tree parmvec = make_tree_vec (1);
15587 tree argvec = make_tree_vec (len2);
15588 tree ta = args2;
15590 /* Setup the parameter vector, which contains only ARG1. */
15591 TREE_VEC_ELT (parmvec, 0) = arg1;
15593 /* Setup the argument vector, which contains the remaining
15594 arguments. */
15595 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15596 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15598 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15599 argvec, UNIFY_ALLOW_NONE,
15600 /*call_args_p=*/false,
15601 /*subr=*/0);
15603 /* We cannot deduce in the other direction, because ARG1 is
15604 a pack expansion but ARG2 is not. */
15605 deduce2 = 0;
15607 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15609 int i, len1 = list_length (args1);
15610 tree parmvec = make_tree_vec (1);
15611 tree argvec = make_tree_vec (len1);
15612 tree ta = args1;
15614 /* Setup the parameter vector, which contains only ARG1. */
15615 TREE_VEC_ELT (parmvec, 0) = arg2;
15617 /* Setup the argument vector, which contains the remaining
15618 arguments. */
15619 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15620 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15622 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15623 argvec, UNIFY_ALLOW_NONE,
15624 /*call_args_p=*/false,
15625 /*subr=*/0);
15627 /* We cannot deduce in the other direction, because ARG2 is
15628 a pack expansion but ARG1 is not.*/
15629 deduce1 = 0;
15632 else
15634 /* The normal case, where neither argument is a pack
15635 expansion. */
15636 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15637 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15640 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15641 arg2, then arg2 is not as specialized as arg1. */
15642 if (!deduce1)
15643 lose2 = true;
15644 if (!deduce2)
15645 lose1 = true;
15647 /* "If, for a given type, deduction succeeds in both directions
15648 (i.e., the types are identical after the transformations above)
15649 and if the type from the argument template is more cv-qualified
15650 than the type from the parameter template (as described above)
15651 that type is considered to be more specialized than the other. If
15652 neither type is more cv-qualified than the other then neither type
15653 is more specialized than the other." */
15655 if (deduce1 && deduce2
15656 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
15658 if ((quals1 & quals2) == quals2)
15659 lose2 = true;
15660 if ((quals1 & quals2) == quals1)
15661 lose1 = true;
15664 if (lose1 && lose2)
15665 /* We've failed to deduce something in either direction.
15666 These must be unordered. */
15667 break;
15669 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15670 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15671 /* We have already processed all of the arguments in our
15672 handing of the pack expansion type. */
15673 len = 0;
15675 args1 = TREE_CHAIN (args1);
15676 args2 = TREE_CHAIN (args2);
15679 /* "In most cases, all template parameters must have values in order for
15680 deduction to succeed, but for partial ordering purposes a template
15681 parameter may remain without a value provided it is not used in the
15682 types being used for partial ordering."
15684 Thus, if we are missing any of the targs1 we need to substitute into
15685 origs1, then pat2 is not as specialized as pat1. This can happen when
15686 there is a nondeduced context. */
15687 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15688 lose2 = true;
15689 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15690 lose1 = true;
15692 processing_template_decl--;
15694 /* All things being equal, if the next argument is a pack expansion
15695 for one function but not for the other, prefer the
15696 non-variadic function. FIXME this is bogus; see c++/41958. */
15697 if (lose1 == lose2
15698 && args1 && TREE_VALUE (args1)
15699 && args2 && TREE_VALUE (args2))
15701 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15702 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15705 if (lose1 == lose2)
15706 return 0;
15707 else if (!lose1)
15708 return 1;
15709 else
15710 return -1;
15713 /* Determine which of two partial specializations is more specialized.
15715 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15716 to the first partial specialization. The TREE_VALUE is the
15717 innermost set of template parameters for the partial
15718 specialization. PAT2 is similar, but for the second template.
15720 Return 1 if the first partial specialization is more specialized;
15721 -1 if the second is more specialized; 0 if neither is more
15722 specialized.
15724 See [temp.class.order] for information about determining which of
15725 two templates is more specialized. */
15727 static int
15728 more_specialized_class (tree pat1, tree pat2)
15730 tree targs;
15731 tree tmpl1, tmpl2;
15732 int winner = 0;
15733 bool any_deductions = false;
15735 tmpl1 = TREE_TYPE (pat1);
15736 tmpl2 = TREE_TYPE (pat2);
15738 /* Just like what happens for functions, if we are ordering between
15739 different class template specializations, we may encounter dependent
15740 types in the arguments, and we need our dependency check functions
15741 to behave correctly. */
15742 ++processing_template_decl;
15743 targs = get_class_bindings (TREE_VALUE (pat1),
15744 CLASSTYPE_TI_ARGS (tmpl1),
15745 CLASSTYPE_TI_ARGS (tmpl2));
15746 if (targs)
15748 --winner;
15749 any_deductions = true;
15752 targs = get_class_bindings (TREE_VALUE (pat2),
15753 CLASSTYPE_TI_ARGS (tmpl2),
15754 CLASSTYPE_TI_ARGS (tmpl1));
15755 if (targs)
15757 ++winner;
15758 any_deductions = true;
15760 --processing_template_decl;
15762 /* In the case of a tie where at least one of the class templates
15763 has a parameter pack at the end, the template with the most
15764 non-packed parameters wins. */
15765 if (winner == 0
15766 && any_deductions
15767 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15768 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15770 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15771 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15772 int len1 = TREE_VEC_LENGTH (args1);
15773 int len2 = TREE_VEC_LENGTH (args2);
15775 /* We don't count the pack expansion at the end. */
15776 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15777 --len1;
15778 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15779 --len2;
15781 if (len1 > len2)
15782 return 1;
15783 else if (len1 < len2)
15784 return -1;
15787 return winner;
15790 /* Return the template arguments that will produce the function signature
15791 DECL from the function template FN, with the explicit template
15792 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15793 also match. Return NULL_TREE if no satisfactory arguments could be
15794 found. */
15796 static tree
15797 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15799 int ntparms = DECL_NTPARMS (fn);
15800 tree targs = make_tree_vec (ntparms);
15801 tree decl_type;
15802 tree decl_arg_types;
15803 tree *args;
15804 unsigned int nargs, ix;
15805 tree arg;
15807 /* Substitute the explicit template arguments into the type of DECL.
15808 The call to fn_type_unification will handle substitution into the
15809 FN. */
15810 decl_type = TREE_TYPE (decl);
15811 if (explicit_args && uses_template_parms (decl_type))
15813 tree tmpl;
15814 tree converted_args;
15816 if (DECL_TEMPLATE_INFO (decl))
15817 tmpl = DECL_TI_TEMPLATE (decl);
15818 else
15819 /* We can get here for some invalid specializations. */
15820 return NULL_TREE;
15822 converted_args
15823 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15824 explicit_args, NULL_TREE,
15825 tf_none,
15826 /*require_all_args=*/false,
15827 /*use_default_args=*/false);
15828 if (converted_args == error_mark_node)
15829 return NULL_TREE;
15831 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15832 if (decl_type == error_mark_node)
15833 return NULL_TREE;
15836 /* Never do unification on the 'this' parameter. */
15837 decl_arg_types = skip_artificial_parms_for (decl,
15838 TYPE_ARG_TYPES (decl_type));
15840 nargs = list_length (decl_arg_types);
15841 args = XALLOCAVEC (tree, nargs);
15842 for (arg = decl_arg_types, ix = 0;
15843 arg != NULL_TREE && arg != void_list_node;
15844 arg = TREE_CHAIN (arg), ++ix)
15845 args[ix] = TREE_VALUE (arg);
15847 if (fn_type_unification (fn, explicit_args, targs,
15848 args, ix,
15849 (check_rettype || DECL_CONV_FN_P (fn)
15850 ? TREE_TYPE (decl_type) : NULL_TREE),
15851 DEDUCE_EXACT, LOOKUP_NORMAL))
15852 return NULL_TREE;
15854 return targs;
15857 /* Return the innermost template arguments that, when applied to a
15858 template specialization whose innermost template parameters are
15859 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15860 ARGS.
15862 For example, suppose we have:
15864 template <class T, class U> struct S {};
15865 template <class T> struct S<T*, int> {};
15867 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15868 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15869 int}. The resulting vector will be {double}, indicating that `T'
15870 is bound to `double'. */
15872 static tree
15873 get_class_bindings (tree tparms, tree spec_args, tree args)
15875 int i, ntparms = TREE_VEC_LENGTH (tparms);
15876 tree deduced_args;
15877 tree innermost_deduced_args;
15879 innermost_deduced_args = make_tree_vec (ntparms);
15880 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15882 deduced_args = copy_node (args);
15883 SET_TMPL_ARGS_LEVEL (deduced_args,
15884 TMPL_ARGS_DEPTH (deduced_args),
15885 innermost_deduced_args);
15887 else
15888 deduced_args = innermost_deduced_args;
15890 if (unify (tparms, deduced_args,
15891 INNERMOST_TEMPLATE_ARGS (spec_args),
15892 INNERMOST_TEMPLATE_ARGS (args),
15893 UNIFY_ALLOW_NONE))
15894 return NULL_TREE;
15896 for (i = 0; i < ntparms; ++i)
15897 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15898 return NULL_TREE;
15900 /* Verify that nondeduced template arguments agree with the type
15901 obtained from argument deduction.
15903 For example:
15905 struct A { typedef int X; };
15906 template <class T, class U> struct C {};
15907 template <class T> struct C<T, typename T::X> {};
15909 Then with the instantiation `C<A, int>', we can deduce that
15910 `T' is `A' but unify () does not check whether `typename T::X'
15911 is `int'. */
15912 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15913 if (spec_args == error_mark_node
15914 /* We only need to check the innermost arguments; the other
15915 arguments will always agree. */
15916 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15917 INNERMOST_TEMPLATE_ARGS (args)))
15918 return NULL_TREE;
15920 /* Now that we have bindings for all of the template arguments,
15921 ensure that the arguments deduced for the template template
15922 parameters have compatible template parameter lists. See the use
15923 of template_template_parm_bindings_ok_p in fn_type_unification
15924 for more information. */
15925 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15926 return NULL_TREE;
15928 return deduced_args;
15931 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15932 Return the TREE_LIST node with the most specialized template, if
15933 any. If there is no most specialized template, the error_mark_node
15934 is returned.
15936 Note that this function does not look at, or modify, the
15937 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15938 returned is one of the elements of INSTANTIATIONS, callers may
15939 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15940 and retrieve it from the value returned. */
15942 tree
15943 most_specialized_instantiation (tree templates)
15945 tree fn, champ;
15947 ++processing_template_decl;
15949 champ = templates;
15950 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15952 int fate = 0;
15954 if (get_bindings (TREE_VALUE (champ),
15955 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15956 NULL_TREE, /*check_ret=*/false))
15957 fate--;
15959 if (get_bindings (TREE_VALUE (fn),
15960 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15961 NULL_TREE, /*check_ret=*/false))
15962 fate++;
15964 if (fate == -1)
15965 champ = fn;
15966 else if (!fate)
15968 /* Equally specialized, move to next function. If there
15969 is no next function, nothing's most specialized. */
15970 fn = TREE_CHAIN (fn);
15971 champ = fn;
15972 if (!fn)
15973 break;
15977 if (champ)
15978 /* Now verify that champ is better than everything earlier in the
15979 instantiation list. */
15980 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15981 if (get_bindings (TREE_VALUE (champ),
15982 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15983 NULL_TREE, /*check_ret=*/false)
15984 || !get_bindings (TREE_VALUE (fn),
15985 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15986 NULL_TREE, /*check_ret=*/false))
15988 champ = NULL_TREE;
15989 break;
15992 processing_template_decl--;
15994 if (!champ)
15995 return error_mark_node;
15997 return champ;
16000 /* If DECL is a specialization of some template, return the most
16001 general such template. Otherwise, returns NULL_TREE.
16003 For example, given:
16005 template <class T> struct S { template <class U> void f(U); };
16007 if TMPL is `template <class U> void S<int>::f(U)' this will return
16008 the full template. This function will not trace past partial
16009 specializations, however. For example, given in addition:
16011 template <class T> struct S<T*> { template <class U> void f(U); };
16013 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16014 `template <class T> template <class U> S<T*>::f(U)'. */
16016 tree
16017 most_general_template (tree decl)
16019 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16020 an immediate specialization. */
16021 if (TREE_CODE (decl) == FUNCTION_DECL)
16023 if (DECL_TEMPLATE_INFO (decl)) {
16024 decl = DECL_TI_TEMPLATE (decl);
16026 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16027 template friend. */
16028 if (TREE_CODE (decl) != TEMPLATE_DECL)
16029 return NULL_TREE;
16030 } else
16031 return NULL_TREE;
16034 /* Look for more and more general templates. */
16035 while (DECL_TEMPLATE_INFO (decl))
16037 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16038 (See cp-tree.h for details.) */
16039 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16040 break;
16042 if (CLASS_TYPE_P (TREE_TYPE (decl))
16043 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16044 break;
16046 /* Stop if we run into an explicitly specialized class template. */
16047 if (!DECL_NAMESPACE_SCOPE_P (decl)
16048 && DECL_CONTEXT (decl)
16049 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16050 break;
16052 decl = DECL_TI_TEMPLATE (decl);
16055 return decl;
16058 /* Return the most specialized of the class template partial
16059 specializations of TMPL which can produce TYPE, a specialization of
16060 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16061 a _TYPE node corresponding to the partial specialization, while the
16062 TREE_PURPOSE is the set of template arguments that must be
16063 substituted into the TREE_TYPE in order to generate TYPE.
16065 If the choice of partial specialization is ambiguous, a diagnostic
16066 is issued, and the error_mark_node is returned. If there are no
16067 partial specializations of TMPL matching TYPE, then NULL_TREE is
16068 returned. */
16070 static tree
16071 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16073 tree list = NULL_TREE;
16074 tree t;
16075 tree champ;
16076 int fate;
16077 bool ambiguous_p;
16078 tree args;
16079 tree outer_args = NULL_TREE;
16081 tmpl = most_general_template (tmpl);
16082 args = CLASSTYPE_TI_ARGS (type);
16084 /* For determining which partial specialization to use, only the
16085 innermost args are interesting. */
16086 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16088 outer_args = strip_innermost_template_args (args, 1);
16089 args = INNERMOST_TEMPLATE_ARGS (args);
16092 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16094 tree partial_spec_args;
16095 tree spec_args;
16096 tree parms = TREE_VALUE (t);
16098 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16100 ++processing_template_decl;
16102 if (outer_args)
16104 int i;
16106 /* Discard the outer levels of args, and then substitute in the
16107 template args from the enclosing class. */
16108 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16109 partial_spec_args = tsubst_template_args
16110 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16112 /* PARMS already refers to just the innermost parms, but the
16113 template parms in partial_spec_args had their levels lowered
16114 by tsubst, so we need to do the same for the parm list. We
16115 can't just tsubst the TREE_VEC itself, as tsubst wants to
16116 treat a TREE_VEC as an argument vector. */
16117 parms = copy_node (parms);
16118 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16119 TREE_VEC_ELT (parms, i) =
16120 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16124 partial_spec_args =
16125 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16126 add_to_template_args (outer_args,
16127 partial_spec_args),
16128 tmpl, tf_none,
16129 /*require_all_args=*/true,
16130 /*use_default_args=*/true);
16132 --processing_template_decl;
16134 if (partial_spec_args == error_mark_node)
16135 return error_mark_node;
16137 spec_args = get_class_bindings (parms,
16138 partial_spec_args,
16139 args);
16140 if (spec_args)
16142 if (outer_args)
16143 spec_args = add_to_template_args (outer_args, spec_args);
16144 list = tree_cons (spec_args, TREE_VALUE (t), list);
16145 TREE_TYPE (list) = TREE_TYPE (t);
16149 if (! list)
16150 return NULL_TREE;
16152 ambiguous_p = false;
16153 t = list;
16154 champ = t;
16155 t = TREE_CHAIN (t);
16156 for (; t; t = TREE_CHAIN (t))
16158 fate = more_specialized_class (champ, t);
16159 if (fate == 1)
16161 else
16163 if (fate == 0)
16165 t = TREE_CHAIN (t);
16166 if (! t)
16168 ambiguous_p = true;
16169 break;
16172 champ = t;
16176 if (!ambiguous_p)
16177 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16179 fate = more_specialized_class (champ, t);
16180 if (fate != 1)
16182 ambiguous_p = true;
16183 break;
16187 if (ambiguous_p)
16189 const char *str;
16190 char *spaces = NULL;
16191 if (!(complain & tf_error))
16192 return error_mark_node;
16193 error ("ambiguous class template instantiation for %q#T", type);
16194 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
16195 for (t = list; t; t = TREE_CHAIN (t))
16197 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16198 spaces = spaces ? spaces : get_spaces (str);
16200 free (spaces);
16201 return error_mark_node;
16204 return champ;
16207 /* Explicitly instantiate DECL. */
16209 void
16210 do_decl_instantiation (tree decl, tree storage)
16212 tree result = NULL_TREE;
16213 int extern_p = 0;
16215 if (!decl || decl == error_mark_node)
16216 /* An error occurred, for which grokdeclarator has already issued
16217 an appropriate message. */
16218 return;
16219 else if (! DECL_LANG_SPECIFIC (decl))
16221 error ("explicit instantiation of non-template %q#D", decl);
16222 return;
16224 else if (TREE_CODE (decl) == VAR_DECL)
16226 /* There is an asymmetry here in the way VAR_DECLs and
16227 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16228 the latter, the DECL we get back will be marked as a
16229 template instantiation, and the appropriate
16230 DECL_TEMPLATE_INFO will be set up. This does not happen for
16231 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16232 should handle VAR_DECLs as it currently handles
16233 FUNCTION_DECLs. */
16234 if (!DECL_CLASS_SCOPE_P (decl))
16236 error ("%qD is not a static data member of a class template", decl);
16237 return;
16239 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16240 if (!result || TREE_CODE (result) != VAR_DECL)
16242 error ("no matching template for %qD found", decl);
16243 return;
16245 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16247 error ("type %qT for explicit instantiation %qD does not match "
16248 "declared type %qT", TREE_TYPE (result), decl,
16249 TREE_TYPE (decl));
16250 return;
16253 else if (TREE_CODE (decl) != FUNCTION_DECL)
16255 error ("explicit instantiation of %q#D", decl);
16256 return;
16258 else
16259 result = decl;
16261 /* Check for various error cases. Note that if the explicit
16262 instantiation is valid the RESULT will currently be marked as an
16263 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16264 until we get here. */
16266 if (DECL_TEMPLATE_SPECIALIZATION (result))
16268 /* DR 259 [temp.spec].
16270 Both an explicit instantiation and a declaration of an explicit
16271 specialization shall not appear in a program unless the explicit
16272 instantiation follows a declaration of the explicit specialization.
16274 For a given set of template parameters, if an explicit
16275 instantiation of a template appears after a declaration of an
16276 explicit specialization for that template, the explicit
16277 instantiation has no effect. */
16278 return;
16280 else if (DECL_EXPLICIT_INSTANTIATION (result))
16282 /* [temp.spec]
16284 No program shall explicitly instantiate any template more
16285 than once.
16287 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16288 the first instantiation was `extern' and the second is not,
16289 and EXTERN_P for the opposite case. */
16290 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16291 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16292 /* If an "extern" explicit instantiation follows an ordinary
16293 explicit instantiation, the template is instantiated. */
16294 if (extern_p)
16295 return;
16297 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16299 error ("no matching template for %qD found", result);
16300 return;
16302 else if (!DECL_TEMPLATE_INFO (result))
16304 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16305 return;
16308 if (storage == NULL_TREE)
16310 else if (storage == ridpointers[(int) RID_EXTERN])
16312 if (!in_system_header && (cxx_dialect == cxx98))
16313 pedwarn (input_location, OPT_pedantic,
16314 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16315 "instantiations");
16316 extern_p = 1;
16318 else
16319 error ("storage class %qD applied to template instantiation", storage);
16321 check_explicit_instantiation_namespace (result);
16322 mark_decl_instantiated (result, extern_p);
16323 if (! extern_p)
16324 instantiate_decl (result, /*defer_ok=*/1,
16325 /*expl_inst_class_mem_p=*/false);
16328 static void
16329 mark_class_instantiated (tree t, int extern_p)
16331 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16332 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16333 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16334 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16335 if (! extern_p)
16337 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16338 rest_of_type_compilation (t, 1);
16342 /* Called from do_type_instantiation through binding_table_foreach to
16343 do recursive instantiation for the type bound in ENTRY. */
16344 static void
16345 bt_instantiate_type_proc (binding_entry entry, void *data)
16347 tree storage = *(tree *) data;
16349 if (MAYBE_CLASS_TYPE_P (entry->type)
16350 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16351 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16354 /* Called from do_type_instantiation to instantiate a member
16355 (a member function or a static member variable) of an
16356 explicitly instantiated class template. */
16357 static void
16358 instantiate_class_member (tree decl, int extern_p)
16360 mark_decl_instantiated (decl, extern_p);
16361 if (! extern_p)
16362 instantiate_decl (decl, /*defer_ok=*/1,
16363 /*expl_inst_class_mem_p=*/true);
16366 /* Perform an explicit instantiation of template class T. STORAGE, if
16367 non-null, is the RID for extern, inline or static. COMPLAIN is
16368 nonzero if this is called from the parser, zero if called recursively,
16369 since the standard is unclear (as detailed below). */
16371 void
16372 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16374 int extern_p = 0;
16375 int nomem_p = 0;
16376 int static_p = 0;
16377 int previous_instantiation_extern_p = 0;
16379 if (TREE_CODE (t) == TYPE_DECL)
16380 t = TREE_TYPE (t);
16382 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16384 error ("explicit instantiation of non-template type %qT", t);
16385 return;
16388 complete_type (t);
16390 if (!COMPLETE_TYPE_P (t))
16392 if (complain & tf_error)
16393 error ("explicit instantiation of %q#T before definition of template",
16395 return;
16398 if (storage != NULL_TREE)
16400 if (!in_system_header)
16402 if (storage == ridpointers[(int) RID_EXTERN])
16404 if (cxx_dialect == cxx98)
16405 pedwarn (input_location, OPT_pedantic,
16406 "ISO C++ 1998 forbids the use of %<extern%> on "
16407 "explicit instantiations");
16409 else
16410 pedwarn (input_location, OPT_pedantic,
16411 "ISO C++ forbids the use of %qE"
16412 " on explicit instantiations", storage);
16415 if (storage == ridpointers[(int) RID_INLINE])
16416 nomem_p = 1;
16417 else if (storage == ridpointers[(int) RID_EXTERN])
16418 extern_p = 1;
16419 else if (storage == ridpointers[(int) RID_STATIC])
16420 static_p = 1;
16421 else
16423 error ("storage class %qD applied to template instantiation",
16424 storage);
16425 extern_p = 0;
16429 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16431 /* DR 259 [temp.spec].
16433 Both an explicit instantiation and a declaration of an explicit
16434 specialization shall not appear in a program unless the explicit
16435 instantiation follows a declaration of the explicit specialization.
16437 For a given set of template parameters, if an explicit
16438 instantiation of a template appears after a declaration of an
16439 explicit specialization for that template, the explicit
16440 instantiation has no effect. */
16441 return;
16443 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16445 /* [temp.spec]
16447 No program shall explicitly instantiate any template more
16448 than once.
16450 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16451 instantiation was `extern'. If EXTERN_P then the second is.
16452 These cases are OK. */
16453 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16455 if (!previous_instantiation_extern_p && !extern_p
16456 && (complain & tf_error))
16457 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16459 /* If we've already instantiated the template, just return now. */
16460 if (!CLASSTYPE_INTERFACE_ONLY (t))
16461 return;
16464 check_explicit_instantiation_namespace (TYPE_NAME (t));
16465 mark_class_instantiated (t, extern_p);
16467 if (nomem_p)
16468 return;
16471 tree tmp;
16473 /* In contrast to implicit instantiation, where only the
16474 declarations, and not the definitions, of members are
16475 instantiated, we have here:
16477 [temp.explicit]
16479 The explicit instantiation of a class template specialization
16480 implies the instantiation of all of its members not
16481 previously explicitly specialized in the translation unit
16482 containing the explicit instantiation.
16484 Of course, we can't instantiate member template classes, since
16485 we don't have any arguments for them. Note that the standard
16486 is unclear on whether the instantiation of the members are
16487 *explicit* instantiations or not. However, the most natural
16488 interpretation is that it should be an explicit instantiation. */
16490 if (! static_p)
16491 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
16492 if (TREE_CODE (tmp) == FUNCTION_DECL
16493 && DECL_TEMPLATE_INSTANTIATION (tmp))
16494 instantiate_class_member (tmp, extern_p);
16496 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
16497 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16498 instantiate_class_member (tmp, extern_p);
16500 if (CLASSTYPE_NESTED_UTDS (t))
16501 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16502 bt_instantiate_type_proc, &storage);
16506 /* Given a function DECL, which is a specialization of TMPL, modify
16507 DECL to be a re-instantiation of TMPL with the same template
16508 arguments. TMPL should be the template into which tsubst'ing
16509 should occur for DECL, not the most general template.
16511 One reason for doing this is a scenario like this:
16513 template <class T>
16514 void f(const T&, int i);
16516 void g() { f(3, 7); }
16518 template <class T>
16519 void f(const T& t, const int i) { }
16521 Note that when the template is first instantiated, with
16522 instantiate_template, the resulting DECL will have no name for the
16523 first parameter, and the wrong type for the second. So, when we go
16524 to instantiate the DECL, we regenerate it. */
16526 static void
16527 regenerate_decl_from_template (tree decl, tree tmpl)
16529 /* The arguments used to instantiate DECL, from the most general
16530 template. */
16531 tree args;
16532 tree code_pattern;
16534 args = DECL_TI_ARGS (decl);
16535 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16537 /* Make sure that we can see identifiers, and compute access
16538 correctly. */
16539 push_access_scope (decl);
16541 if (TREE_CODE (decl) == FUNCTION_DECL)
16543 tree decl_parm;
16544 tree pattern_parm;
16545 tree specs;
16546 int args_depth;
16547 int parms_depth;
16549 args_depth = TMPL_ARGS_DEPTH (args);
16550 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16551 if (args_depth > parms_depth)
16552 args = get_innermost_template_args (args, parms_depth);
16554 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16555 args, tf_error, NULL_TREE);
16556 if (specs)
16557 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16558 specs);
16560 /* Merge parameter declarations. */
16561 decl_parm = skip_artificial_parms_for (decl,
16562 DECL_ARGUMENTS (decl));
16563 pattern_parm
16564 = skip_artificial_parms_for (code_pattern,
16565 DECL_ARGUMENTS (code_pattern));
16566 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16568 tree parm_type;
16569 tree attributes;
16571 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16572 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16573 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16574 NULL_TREE);
16575 parm_type = type_decays_to (parm_type);
16576 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16577 TREE_TYPE (decl_parm) = parm_type;
16578 attributes = DECL_ATTRIBUTES (pattern_parm);
16579 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16581 DECL_ATTRIBUTES (decl_parm) = attributes;
16582 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16584 decl_parm = DECL_CHAIN (decl_parm);
16585 pattern_parm = DECL_CHAIN (pattern_parm);
16587 /* Merge any parameters that match with the function parameter
16588 pack. */
16589 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16591 int i, len;
16592 tree expanded_types;
16593 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16594 the parameters in this function parameter pack. */
16595 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16596 args, tf_error, NULL_TREE);
16597 len = TREE_VEC_LENGTH (expanded_types);
16598 for (i = 0; i < len; i++)
16600 tree parm_type;
16601 tree attributes;
16603 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16604 /* Rename the parameter to include the index. */
16605 DECL_NAME (decl_parm) =
16606 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16607 parm_type = TREE_VEC_ELT (expanded_types, i);
16608 parm_type = type_decays_to (parm_type);
16609 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16610 TREE_TYPE (decl_parm) = parm_type;
16611 attributes = DECL_ATTRIBUTES (pattern_parm);
16612 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16614 DECL_ATTRIBUTES (decl_parm) = attributes;
16615 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16617 decl_parm = DECL_CHAIN (decl_parm);
16620 /* Merge additional specifiers from the CODE_PATTERN. */
16621 if (DECL_DECLARED_INLINE_P (code_pattern)
16622 && !DECL_DECLARED_INLINE_P (decl))
16623 DECL_DECLARED_INLINE_P (decl) = 1;
16625 else if (TREE_CODE (decl) == VAR_DECL)
16627 DECL_INITIAL (decl) =
16628 tsubst_expr (DECL_INITIAL (code_pattern), args,
16629 tf_error, DECL_TI_TEMPLATE (decl),
16630 /*integral_constant_expression_p=*/false);
16631 if (VAR_HAD_UNKNOWN_BOUND (decl))
16632 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
16633 tf_error, DECL_TI_TEMPLATE (decl));
16635 else
16636 gcc_unreachable ();
16638 pop_access_scope (decl);
16641 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16642 substituted to get DECL. */
16644 tree
16645 template_for_substitution (tree decl)
16647 tree tmpl = DECL_TI_TEMPLATE (decl);
16649 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16650 for the instantiation. This is not always the most general
16651 template. Consider, for example:
16653 template <class T>
16654 struct S { template <class U> void f();
16655 template <> void f<int>(); };
16657 and an instantiation of S<double>::f<int>. We want TD to be the
16658 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16659 while (/* An instantiation cannot have a definition, so we need a
16660 more general template. */
16661 DECL_TEMPLATE_INSTANTIATION (tmpl)
16662 /* We must also deal with friend templates. Given:
16664 template <class T> struct S {
16665 template <class U> friend void f() {};
16668 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16669 so far as the language is concerned, but that's still
16670 where we get the pattern for the instantiation from. On
16671 other hand, if the definition comes outside the class, say:
16673 template <class T> struct S {
16674 template <class U> friend void f();
16676 template <class U> friend void f() {}
16678 we don't need to look any further. That's what the check for
16679 DECL_INITIAL is for. */
16680 || (TREE_CODE (decl) == FUNCTION_DECL
16681 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16682 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16684 /* The present template, TD, should not be a definition. If it
16685 were a definition, we should be using it! Note that we
16686 cannot restructure the loop to just keep going until we find
16687 a template with a definition, since that might go too far if
16688 a specialization was declared, but not defined. */
16689 gcc_assert (TREE_CODE (decl) != VAR_DECL
16690 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16692 /* Fetch the more general template. */
16693 tmpl = DECL_TI_TEMPLATE (tmpl);
16696 return tmpl;
16699 /* Returns true if we need to instantiate this template instance even if we
16700 know we aren't going to emit it.. */
16702 bool
16703 always_instantiate_p (tree decl)
16705 /* We always instantiate inline functions so that we can inline them. An
16706 explicit instantiation declaration prohibits implicit instantiation of
16707 non-inline functions. With high levels of optimization, we would
16708 normally inline non-inline functions -- but we're not allowed to do
16709 that for "extern template" functions. Therefore, we check
16710 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16711 return ((TREE_CODE (decl) == FUNCTION_DECL
16712 && DECL_DECLARED_INLINE_P (decl))
16713 /* And we need to instantiate static data members so that
16714 their initializers are available in integral constant
16715 expressions. */
16716 || (TREE_CODE (decl) == VAR_DECL
16717 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16720 /* Produce the definition of D, a _DECL generated from a template. If
16721 DEFER_OK is nonzero, then we don't have to actually do the
16722 instantiation now; we just have to do it sometime. Normally it is
16723 an error if this is an explicit instantiation but D is undefined.
16724 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16725 explicitly instantiated class template. */
16727 tree
16728 instantiate_decl (tree d, int defer_ok,
16729 bool expl_inst_class_mem_p)
16731 tree tmpl = DECL_TI_TEMPLATE (d);
16732 tree gen_args;
16733 tree args;
16734 tree td;
16735 tree code_pattern;
16736 tree spec;
16737 tree gen_tmpl;
16738 bool pattern_defined;
16739 int need_push;
16740 location_t saved_loc = input_location;
16741 bool external_p;
16743 /* This function should only be used to instantiate templates for
16744 functions and static member variables. */
16745 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16746 || TREE_CODE (d) == VAR_DECL);
16748 /* Variables are never deferred; if instantiation is required, they
16749 are instantiated right away. That allows for better code in the
16750 case that an expression refers to the value of the variable --
16751 if the variable has a constant value the referring expression can
16752 take advantage of that fact. */
16753 if (TREE_CODE (d) == VAR_DECL)
16754 defer_ok = 0;
16756 /* Don't instantiate cloned functions. Instead, instantiate the
16757 functions they cloned. */
16758 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16759 d = DECL_CLONED_FUNCTION (d);
16761 if (DECL_TEMPLATE_INSTANTIATED (d)
16762 || DECL_TEMPLATE_SPECIALIZATION (d))
16763 /* D has already been instantiated or explicitly specialized, so
16764 there's nothing for us to do here.
16766 It might seem reasonable to check whether or not D is an explicit
16767 instantiation, and, if so, stop here. But when an explicit
16768 instantiation is deferred until the end of the compilation,
16769 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16770 the instantiation. */
16771 return d;
16773 /* Check to see whether we know that this template will be
16774 instantiated in some other file, as with "extern template"
16775 extension. */
16776 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16778 /* In general, we do not instantiate such templates. */
16779 if (external_p && !always_instantiate_p (d))
16780 return d;
16782 gen_tmpl = most_general_template (tmpl);
16783 gen_args = DECL_TI_ARGS (d);
16785 if (tmpl != gen_tmpl)
16786 /* We should already have the extra args. */
16787 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16788 == TMPL_ARGS_DEPTH (gen_args));
16789 /* And what's in the hash table should match D. */
16790 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16791 || spec == NULL_TREE);
16793 /* This needs to happen before any tsubsting. */
16794 if (! push_tinst_level (d))
16795 return d;
16797 timevar_push (TV_PARSE);
16799 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16800 for the instantiation. */
16801 td = template_for_substitution (d);
16802 code_pattern = DECL_TEMPLATE_RESULT (td);
16804 /* We should never be trying to instantiate a member of a class
16805 template or partial specialization. */
16806 gcc_assert (d != code_pattern);
16808 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16809 || DECL_TEMPLATE_SPECIALIZATION (td))
16810 /* In the case of a friend template whose definition is provided
16811 outside the class, we may have too many arguments. Drop the
16812 ones we don't need. The same is true for specializations. */
16813 args = get_innermost_template_args
16814 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16815 else
16816 args = gen_args;
16818 if (TREE_CODE (d) == FUNCTION_DECL)
16819 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16820 else
16821 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16823 /* We may be in the middle of deferred access check. Disable it now. */
16824 push_deferring_access_checks (dk_no_deferred);
16826 /* Unless an explicit instantiation directive has already determined
16827 the linkage of D, remember that a definition is available for
16828 this entity. */
16829 if (pattern_defined
16830 && !DECL_INTERFACE_KNOWN (d)
16831 && !DECL_NOT_REALLY_EXTERN (d))
16832 mark_definable (d);
16834 input_location = DECL_SOURCE_LOCATION (d);
16836 /* If D is a member of an explicitly instantiated class template,
16837 and no definition is available, treat it like an implicit
16838 instantiation. */
16839 if (!pattern_defined && expl_inst_class_mem_p
16840 && DECL_EXPLICIT_INSTANTIATION (d))
16842 DECL_NOT_REALLY_EXTERN (d) = 0;
16843 DECL_INTERFACE_KNOWN (d) = 0;
16844 SET_DECL_IMPLICIT_INSTANTIATION (d);
16847 /* Recheck the substitutions to obtain any warning messages
16848 about ignoring cv qualifiers. Don't do this for artificial decls,
16849 as it breaks the context-sensitive substitution for lambda op(). */
16850 if (!defer_ok && !DECL_ARTIFICIAL (d))
16852 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16853 tree type = TREE_TYPE (gen);
16855 /* Make sure that we can see identifiers, and compute access
16856 correctly. D is already the target FUNCTION_DECL with the
16857 right context. */
16858 push_access_scope (d);
16860 if (TREE_CODE (gen) == FUNCTION_DECL)
16862 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16863 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16865 /* Don't simply tsubst the function type, as that will give
16866 duplicate warnings about poor parameter qualifications.
16867 The function arguments are the same as the decl_arguments
16868 without the top level cv qualifiers. */
16869 type = TREE_TYPE (type);
16871 tsubst (type, gen_args, tf_warning_or_error, d);
16873 pop_access_scope (d);
16876 /* Defer all other templates, unless we have been explicitly
16877 forbidden from doing so. */
16878 if (/* If there is no definition, we cannot instantiate the
16879 template. */
16880 ! pattern_defined
16881 /* If it's OK to postpone instantiation, do so. */
16882 || defer_ok
16883 /* If this is a static data member that will be defined
16884 elsewhere, we don't want to instantiate the entire data
16885 member, but we do want to instantiate the initializer so that
16886 we can substitute that elsewhere. */
16887 || (external_p && TREE_CODE (d) == VAR_DECL))
16889 /* The definition of the static data member is now required so
16890 we must substitute the initializer. */
16891 if (TREE_CODE (d) == VAR_DECL
16892 && !DECL_INITIAL (d)
16893 && DECL_INITIAL (code_pattern))
16895 tree ns;
16896 tree init;
16898 ns = decl_namespace_context (d);
16899 push_nested_namespace (ns);
16900 push_nested_class (DECL_CONTEXT (d));
16901 init = tsubst_expr (DECL_INITIAL (code_pattern),
16902 args,
16903 tf_warning_or_error, NULL_TREE,
16904 /*integral_constant_expression_p=*/false);
16905 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16906 /*asmspec_tree=*/NULL_TREE,
16907 LOOKUP_ONLYCONVERTING);
16908 pop_nested_class ();
16909 pop_nested_namespace (ns);
16912 /* We restore the source position here because it's used by
16913 add_pending_template. */
16914 input_location = saved_loc;
16916 if (at_eof && !pattern_defined
16917 && DECL_EXPLICIT_INSTANTIATION (d)
16918 && DECL_NOT_REALLY_EXTERN (d))
16919 /* [temp.explicit]
16921 The definition of a non-exported function template, a
16922 non-exported member function template, or a non-exported
16923 member function or static data member of a class template
16924 shall be present in every translation unit in which it is
16925 explicitly instantiated. */
16926 permerror (input_location, "explicit instantiation of %qD "
16927 "but no definition available", d);
16929 /* ??? Historically, we have instantiated inline functions, even
16930 when marked as "extern template". */
16931 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16932 add_pending_template (d);
16933 goto out;
16935 /* Tell the repository that D is available in this translation unit
16936 -- and see if it is supposed to be instantiated here. */
16937 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16939 /* In a PCH file, despite the fact that the repository hasn't
16940 requested instantiation in the PCH it is still possible that
16941 an instantiation will be required in a file that includes the
16942 PCH. */
16943 if (pch_file)
16944 add_pending_template (d);
16945 /* Instantiate inline functions so that the inliner can do its
16946 job, even though we'll not be emitting a copy of this
16947 function. */
16948 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16949 goto out;
16952 need_push = !cfun || !global_bindings_p ();
16953 if (need_push)
16954 push_to_top_level ();
16956 /* Mark D as instantiated so that recursive calls to
16957 instantiate_decl do not try to instantiate it again. */
16958 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16960 /* Regenerate the declaration in case the template has been modified
16961 by a subsequent redeclaration. */
16962 regenerate_decl_from_template (d, td);
16964 /* We already set the file and line above. Reset them now in case
16965 they changed as a result of calling regenerate_decl_from_template. */
16966 input_location = DECL_SOURCE_LOCATION (d);
16968 if (TREE_CODE (d) == VAR_DECL)
16970 tree init;
16972 /* Clear out DECL_RTL; whatever was there before may not be right
16973 since we've reset the type of the declaration. */
16974 SET_DECL_RTL (d, NULL);
16975 DECL_IN_AGGR_P (d) = 0;
16977 /* The initializer is placed in DECL_INITIAL by
16978 regenerate_decl_from_template. Pull it out so that
16979 cp_finish_decl can process it. */
16980 init = DECL_INITIAL (d);
16981 DECL_INITIAL (d) = NULL_TREE;
16982 DECL_INITIALIZED_P (d) = 0;
16984 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16985 initializer. That function will defer actual emission until
16986 we have a chance to determine linkage. */
16987 DECL_EXTERNAL (d) = 0;
16989 /* Enter the scope of D so that access-checking works correctly. */
16990 push_nested_class (DECL_CONTEXT (d));
16991 cp_finish_decl (d, init, false, NULL_TREE, 0);
16992 pop_nested_class ();
16994 else if (TREE_CODE (d) == FUNCTION_DECL)
16996 htab_t saved_local_specializations;
16997 tree subst_decl;
16998 tree tmpl_parm;
16999 tree spec_parm;
17001 /* Save away the current list, in case we are instantiating one
17002 template from within the body of another. */
17003 saved_local_specializations = local_specializations;
17005 /* Set up the list of local specializations. */
17006 local_specializations = htab_create (37,
17007 hash_local_specialization,
17008 eq_local_specializations,
17009 NULL);
17011 /* Set up context. */
17012 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
17014 /* Create substitution entries for the parameters. */
17015 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
17016 tmpl_parm = DECL_ARGUMENTS (subst_decl);
17017 spec_parm = DECL_ARGUMENTS (d);
17018 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17020 register_local_specialization (spec_parm, tmpl_parm);
17021 spec_parm = skip_artificial_parms_for (d, spec_parm);
17022 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17024 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17026 register_local_specialization (spec_parm, tmpl_parm);
17027 tmpl_parm = DECL_CHAIN (tmpl_parm);
17028 spec_parm = DECL_CHAIN (spec_parm);
17030 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17032 /* Register the (value) argument pack as a specialization of
17033 TMPL_PARM, then move on. */
17034 tree argpack = make_fnparm_pack (spec_parm);
17035 register_local_specialization (argpack, tmpl_parm);
17036 tmpl_parm = DECL_CHAIN (tmpl_parm);
17037 spec_parm = NULL_TREE;
17039 gcc_assert (!spec_parm);
17041 /* Substitute into the body of the function. */
17042 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17043 tf_warning_or_error, tmpl,
17044 /*integral_constant_expression_p=*/false);
17046 /* Set the current input_location to the end of the function
17047 so that finish_function knows where we are. */
17048 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17050 /* We don't need the local specializations any more. */
17051 htab_delete (local_specializations);
17052 local_specializations = saved_local_specializations;
17054 /* Finish the function. */
17055 d = finish_function (0);
17056 expand_or_defer_fn (d);
17059 /* We're not deferring instantiation any more. */
17060 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17062 if (need_push)
17063 pop_from_top_level ();
17065 out:
17066 input_location = saved_loc;
17067 pop_deferring_access_checks ();
17068 pop_tinst_level ();
17070 timevar_pop (TV_PARSE);
17072 return d;
17075 /* Run through the list of templates that we wish we could
17076 instantiate, and instantiate any we can. RETRIES is the
17077 number of times we retry pending template instantiation. */
17079 void
17080 instantiate_pending_templates (int retries)
17082 int reconsider;
17083 location_t saved_loc = input_location;
17085 /* Instantiating templates may trigger vtable generation. This in turn
17086 may require further template instantiations. We place a limit here
17087 to avoid infinite loop. */
17088 if (pending_templates && retries >= max_tinst_depth)
17090 tree decl = pending_templates->tinst->decl;
17092 error ("template instantiation depth exceeds maximum of %d"
17093 " instantiating %q+D, possibly from virtual table generation"
17094 " (use -ftemplate-depth= to increase the maximum)",
17095 max_tinst_depth, decl);
17096 if (TREE_CODE (decl) == FUNCTION_DECL)
17097 /* Pretend that we defined it. */
17098 DECL_INITIAL (decl) = error_mark_node;
17099 return;
17104 struct pending_template **t = &pending_templates;
17105 struct pending_template *last = NULL;
17106 reconsider = 0;
17107 while (*t)
17109 tree instantiation = reopen_tinst_level ((*t)->tinst);
17110 bool complete = false;
17112 if (TYPE_P (instantiation))
17114 tree fn;
17116 if (!COMPLETE_TYPE_P (instantiation))
17118 instantiate_class_template (instantiation);
17119 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17120 for (fn = TYPE_METHODS (instantiation);
17122 fn = TREE_CHAIN (fn))
17123 if (! DECL_ARTIFICIAL (fn))
17124 instantiate_decl (fn,
17125 /*defer_ok=*/0,
17126 /*expl_inst_class_mem_p=*/false);
17127 if (COMPLETE_TYPE_P (instantiation))
17128 reconsider = 1;
17131 complete = COMPLETE_TYPE_P (instantiation);
17133 else
17135 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17136 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17138 instantiation
17139 = instantiate_decl (instantiation,
17140 /*defer_ok=*/0,
17141 /*expl_inst_class_mem_p=*/false);
17142 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17143 reconsider = 1;
17146 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17147 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17150 if (complete)
17151 /* If INSTANTIATION has been instantiated, then we don't
17152 need to consider it again in the future. */
17153 *t = (*t)->next;
17154 else
17156 last = *t;
17157 t = &(*t)->next;
17159 tinst_depth = 0;
17160 current_tinst_level = NULL;
17162 last_pending_template = last;
17164 while (reconsider);
17166 input_location = saved_loc;
17169 /* Substitute ARGVEC into T, which is a list of initializers for
17170 either base class or a non-static data member. The TREE_PURPOSEs
17171 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17172 instantiate_decl. */
17174 static tree
17175 tsubst_initializer_list (tree t, tree argvec)
17177 tree inits = NULL_TREE;
17179 for (; t; t = TREE_CHAIN (t))
17181 tree decl;
17182 tree init;
17183 tree expanded_bases = NULL_TREE;
17184 tree expanded_arguments = NULL_TREE;
17185 int i, len = 1;
17187 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17189 tree expr;
17190 tree arg;
17192 /* Expand the base class expansion type into separate base
17193 classes. */
17194 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17195 tf_warning_or_error,
17196 NULL_TREE);
17197 if (expanded_bases == error_mark_node)
17198 continue;
17200 /* We'll be building separate TREE_LISTs of arguments for
17201 each base. */
17202 len = TREE_VEC_LENGTH (expanded_bases);
17203 expanded_arguments = make_tree_vec (len);
17204 for (i = 0; i < len; i++)
17205 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17207 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17208 expand each argument in the TREE_VALUE of t. */
17209 expr = make_node (EXPR_PACK_EXPANSION);
17210 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17211 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17213 if (TREE_VALUE (t) == void_type_node)
17214 /* VOID_TYPE_NODE is used to indicate
17215 value-initialization. */
17217 for (i = 0; i < len; i++)
17218 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17220 else
17222 /* Substitute parameter packs into each argument in the
17223 TREE_LIST. */
17224 in_base_initializer = 1;
17225 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17227 tree expanded_exprs;
17229 /* Expand the argument. */
17230 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17231 expanded_exprs
17232 = tsubst_pack_expansion (expr, argvec,
17233 tf_warning_or_error,
17234 NULL_TREE);
17235 if (expanded_exprs == error_mark_node)
17236 continue;
17238 /* Prepend each of the expanded expressions to the
17239 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17240 for (i = 0; i < len; i++)
17242 TREE_VEC_ELT (expanded_arguments, i) =
17243 tree_cons (NULL_TREE,
17244 TREE_VEC_ELT (expanded_exprs, i),
17245 TREE_VEC_ELT (expanded_arguments, i));
17248 in_base_initializer = 0;
17250 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17251 since we built them backwards. */
17252 for (i = 0; i < len; i++)
17254 TREE_VEC_ELT (expanded_arguments, i) =
17255 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17260 for (i = 0; i < len; ++i)
17262 if (expanded_bases)
17264 decl = TREE_VEC_ELT (expanded_bases, i);
17265 decl = expand_member_init (decl);
17266 init = TREE_VEC_ELT (expanded_arguments, i);
17268 else
17270 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17271 tf_warning_or_error, NULL_TREE);
17273 decl = expand_member_init (decl);
17274 if (decl && !DECL_P (decl))
17275 in_base_initializer = 1;
17277 init = TREE_VALUE (t);
17278 if (init != void_type_node)
17279 init = tsubst_expr (init, argvec,
17280 tf_warning_or_error, NULL_TREE,
17281 /*integral_constant_expression_p=*/false);
17282 in_base_initializer = 0;
17285 if (decl)
17287 init = build_tree_list (decl, init);
17288 TREE_CHAIN (init) = inits;
17289 inits = init;
17293 return inits;
17296 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17298 static void
17299 set_current_access_from_decl (tree decl)
17301 if (TREE_PRIVATE (decl))
17302 current_access_specifier = access_private_node;
17303 else if (TREE_PROTECTED (decl))
17304 current_access_specifier = access_protected_node;
17305 else
17306 current_access_specifier = access_public_node;
17309 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17310 is the instantiation (which should have been created with
17311 start_enum) and ARGS are the template arguments to use. */
17313 static void
17314 tsubst_enum (tree tag, tree newtag, tree args)
17316 tree e;
17318 if (SCOPED_ENUM_P (newtag))
17319 begin_scope (sk_scoped_enum, newtag);
17321 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17323 tree value;
17324 tree decl;
17326 decl = TREE_VALUE (e);
17327 /* Note that in a template enum, the TREE_VALUE is the
17328 CONST_DECL, not the corresponding INTEGER_CST. */
17329 value = tsubst_expr (DECL_INITIAL (decl),
17330 args, tf_warning_or_error, NULL_TREE,
17331 /*integral_constant_expression_p=*/true);
17333 /* Give this enumeration constant the correct access. */
17334 set_current_access_from_decl (decl);
17336 /* Actually build the enumerator itself. */
17337 build_enumerator
17338 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
17341 if (SCOPED_ENUM_P (newtag))
17342 finish_scope ();
17344 finish_enum_value_list (newtag);
17345 finish_enum (newtag);
17347 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17348 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17351 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17352 its type -- but without substituting the innermost set of template
17353 arguments. So, innermost set of template parameters will appear in
17354 the type. */
17356 tree
17357 get_mostly_instantiated_function_type (tree decl)
17359 tree fn_type;
17360 tree tmpl;
17361 tree targs;
17362 tree tparms;
17363 int parm_depth;
17365 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17366 targs = DECL_TI_ARGS (decl);
17367 tparms = DECL_TEMPLATE_PARMS (tmpl);
17368 parm_depth = TMPL_PARMS_DEPTH (tparms);
17370 /* There should be as many levels of arguments as there are levels
17371 of parameters. */
17372 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17374 fn_type = TREE_TYPE (tmpl);
17376 if (parm_depth == 1)
17377 /* No substitution is necessary. */
17379 else
17381 int i, save_access_control;
17382 tree partial_args;
17384 /* Replace the innermost level of the TARGS with NULL_TREEs to
17385 let tsubst know not to substitute for those parameters. */
17386 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17387 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17388 SET_TMPL_ARGS_LEVEL (partial_args, i,
17389 TMPL_ARGS_LEVEL (targs, i));
17390 SET_TMPL_ARGS_LEVEL (partial_args,
17391 TMPL_ARGS_DEPTH (targs),
17392 make_tree_vec (DECL_NTPARMS (tmpl)));
17394 /* Disable access control as this function is used only during
17395 name-mangling. */
17396 save_access_control = flag_access_control;
17397 flag_access_control = 0;
17399 ++processing_template_decl;
17400 /* Now, do the (partial) substitution to figure out the
17401 appropriate function type. */
17402 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17403 --processing_template_decl;
17405 /* Substitute into the template parameters to obtain the real
17406 innermost set of parameters. This step is important if the
17407 innermost set of template parameters contains value
17408 parameters whose types depend on outer template parameters. */
17409 TREE_VEC_LENGTH (partial_args)--;
17410 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17412 flag_access_control = save_access_control;
17415 return fn_type;
17418 /* Return truthvalue if we're processing a template different from
17419 the last one involved in diagnostics. */
17421 problematic_instantiation_changed (void)
17423 return last_template_error_tick != tinst_level_tick;
17426 /* Remember current template involved in diagnostics. */
17427 void
17428 record_last_problematic_instantiation (void)
17430 last_template_error_tick = tinst_level_tick;
17433 struct tinst_level *
17434 current_instantiation (void)
17436 return current_tinst_level;
17439 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17440 type. Return zero for ok, nonzero for disallowed. Issue error and
17441 warning messages under control of COMPLAIN. */
17443 static int
17444 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17446 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17447 return 0;
17448 else if (POINTER_TYPE_P (type))
17449 return 0;
17450 else if (TYPE_PTR_TO_MEMBER_P (type))
17451 return 0;
17452 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17453 return 0;
17454 else if (TREE_CODE (type) == TYPENAME_TYPE)
17455 return 0;
17457 if (complain & tf_error)
17458 error ("%q#T is not a valid type for a template constant parameter", type);
17459 return 1;
17462 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17463 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17465 static bool
17466 dependent_type_p_r (tree type)
17468 tree scope;
17470 /* [temp.dep.type]
17472 A type is dependent if it is:
17474 -- a template parameter. Template template parameters are types
17475 for us (since TYPE_P holds true for them) so we handle
17476 them here. */
17477 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17478 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17479 return true;
17480 /* -- a qualified-id with a nested-name-specifier which contains a
17481 class-name that names a dependent type or whose unqualified-id
17482 names a dependent type. */
17483 if (TREE_CODE (type) == TYPENAME_TYPE)
17484 return true;
17485 /* -- a cv-qualified type where the cv-unqualified type is
17486 dependent. */
17487 type = TYPE_MAIN_VARIANT (type);
17488 /* -- a compound type constructed from any dependent type. */
17489 if (TYPE_PTR_TO_MEMBER_P (type))
17490 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17491 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17492 (type)));
17493 else if (TREE_CODE (type) == POINTER_TYPE
17494 || TREE_CODE (type) == REFERENCE_TYPE)
17495 return dependent_type_p (TREE_TYPE (type));
17496 else if (TREE_CODE (type) == FUNCTION_TYPE
17497 || TREE_CODE (type) == METHOD_TYPE)
17499 tree arg_type;
17501 if (dependent_type_p (TREE_TYPE (type)))
17502 return true;
17503 for (arg_type = TYPE_ARG_TYPES (type);
17504 arg_type;
17505 arg_type = TREE_CHAIN (arg_type))
17506 if (dependent_type_p (TREE_VALUE (arg_type)))
17507 return true;
17508 return false;
17510 /* -- an array type constructed from any dependent type or whose
17511 size is specified by a constant expression that is
17512 value-dependent.
17514 We checked for type- and value-dependence of the bounds in
17515 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17516 if (TREE_CODE (type) == ARRAY_TYPE)
17518 if (TYPE_DOMAIN (type)
17519 && dependent_type_p (TYPE_DOMAIN (type)))
17520 return true;
17521 return dependent_type_p (TREE_TYPE (type));
17524 /* -- a template-id in which either the template name is a template
17525 parameter ... */
17526 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17527 return true;
17528 /* ... or any of the template arguments is a dependent type or
17529 an expression that is type-dependent or value-dependent. */
17530 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17531 && (any_dependent_template_arguments_p
17532 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17533 return true;
17535 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17536 argument of the `typeof' expression is not type-dependent, then
17537 it should already been have resolved. */
17538 if (TREE_CODE (type) == TYPEOF_TYPE
17539 || TREE_CODE (type) == DECLTYPE_TYPE)
17540 return true;
17542 /* A template argument pack is dependent if any of its packed
17543 arguments are. */
17544 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17546 tree args = ARGUMENT_PACK_ARGS (type);
17547 int i, len = TREE_VEC_LENGTH (args);
17548 for (i = 0; i < len; ++i)
17549 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17550 return true;
17553 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17554 be template parameters. */
17555 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17556 return true;
17558 /* The standard does not specifically mention types that are local
17559 to template functions or local classes, but they should be
17560 considered dependent too. For example:
17562 template <int I> void f() {
17563 enum E { a = I };
17564 S<sizeof (E)> s;
17567 The size of `E' cannot be known until the value of `I' has been
17568 determined. Therefore, `E' must be considered dependent. */
17569 scope = TYPE_CONTEXT (type);
17570 if (scope && TYPE_P (scope))
17571 return dependent_type_p (scope);
17572 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17573 return type_dependent_expression_p (scope);
17575 /* Other types are non-dependent. */
17576 return false;
17579 /* Returns TRUE if TYPE is dependent, in the sense of
17580 [temp.dep.type]. */
17582 bool
17583 dependent_type_p (tree type)
17585 /* If there are no template parameters in scope, then there can't be
17586 any dependent types. */
17587 if (!processing_template_decl)
17589 /* If we are not processing a template, then nobody should be
17590 providing us with a dependent type. */
17591 gcc_assert (type);
17592 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17593 return false;
17596 /* If the type is NULL, we have not computed a type for the entity
17597 in question; in that case, the type is dependent. */
17598 if (!type)
17599 return true;
17601 /* Erroneous types can be considered non-dependent. */
17602 if (type == error_mark_node)
17603 return false;
17605 /* If we have not already computed the appropriate value for TYPE,
17606 do so now. */
17607 if (!TYPE_DEPENDENT_P_VALID (type))
17609 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17610 TYPE_DEPENDENT_P_VALID (type) = 1;
17613 return TYPE_DEPENDENT_P (type);
17616 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17617 lookup. In other words, a dependent type that is not the current
17618 instantiation. */
17620 bool
17621 dependent_scope_p (tree scope)
17623 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17624 && !currently_open_class (scope));
17627 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17628 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17629 expression. */
17631 bool
17632 value_dependent_expression_p (tree expression)
17634 if (!processing_template_decl)
17635 return false;
17637 /* A name declared with a dependent type. */
17638 if (DECL_P (expression) && type_dependent_expression_p (expression))
17639 return true;
17641 switch (TREE_CODE (expression))
17643 case IDENTIFIER_NODE:
17644 /* A name that has not been looked up -- must be dependent. */
17645 return true;
17647 case TEMPLATE_PARM_INDEX:
17648 /* A non-type template parm. */
17649 return true;
17651 case CONST_DECL:
17652 /* A non-type template parm. */
17653 if (DECL_TEMPLATE_PARM_P (expression))
17654 return true;
17655 return value_dependent_expression_p (DECL_INITIAL (expression));
17657 case VAR_DECL:
17658 /* A constant with integral or enumeration type and is initialized
17659 with an expression that is value-dependent. */
17660 if (DECL_INITIAL (expression)
17661 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17662 && value_dependent_expression_p (DECL_INITIAL (expression)))
17663 return true;
17664 return false;
17666 case DYNAMIC_CAST_EXPR:
17667 case STATIC_CAST_EXPR:
17668 case CONST_CAST_EXPR:
17669 case REINTERPRET_CAST_EXPR:
17670 case CAST_EXPR:
17671 /* These expressions are value-dependent if the type to which
17672 the cast occurs is dependent or the expression being casted
17673 is value-dependent. */
17675 tree type = TREE_TYPE (expression);
17677 if (dependent_type_p (type))
17678 return true;
17680 /* A functional cast has a list of operands. */
17681 expression = TREE_OPERAND (expression, 0);
17682 if (!expression)
17684 /* If there are no operands, it must be an expression such
17685 as "int()". This should not happen for aggregate types
17686 because it would form non-constant expressions. */
17687 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17689 return false;
17692 if (TREE_CODE (expression) == TREE_LIST)
17693 return any_value_dependent_elements_p (expression);
17695 return value_dependent_expression_p (expression);
17698 case SIZEOF_EXPR:
17699 case ALIGNOF_EXPR:
17700 /* A `sizeof' expression is value-dependent if the operand is
17701 type-dependent or is a pack expansion. */
17702 expression = TREE_OPERAND (expression, 0);
17703 if (PACK_EXPANSION_P (expression))
17704 return true;
17705 else if (TYPE_P (expression))
17706 return dependent_type_p (expression);
17707 return type_dependent_expression_p (expression);
17709 case AT_ENCODE_EXPR:
17710 /* An 'encode' expression is value-dependent if the operand is
17711 type-dependent. */
17712 expression = TREE_OPERAND (expression, 0);
17713 return dependent_type_p (expression);
17715 case NOEXCEPT_EXPR:
17716 expression = TREE_OPERAND (expression, 0);
17717 /* FIXME why check value-dependency? */
17718 return (type_dependent_expression_p (expression)
17719 || value_dependent_expression_p (expression));
17721 case SCOPE_REF:
17723 tree name = TREE_OPERAND (expression, 1);
17724 return value_dependent_expression_p (name);
17727 case COMPONENT_REF:
17728 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17729 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17731 case CALL_EXPR:
17732 /* A CALL_EXPR may appear in a constant expression if it is a
17733 call to a builtin function, e.g., __builtin_constant_p. All
17734 such calls are value-dependent. */
17735 return true;
17737 case NONTYPE_ARGUMENT_PACK:
17738 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17739 is value-dependent. */
17741 tree values = ARGUMENT_PACK_ARGS (expression);
17742 int i, len = TREE_VEC_LENGTH (values);
17744 for (i = 0; i < len; ++i)
17745 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17746 return true;
17748 return false;
17751 case TRAIT_EXPR:
17753 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17754 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17755 || (type2 ? dependent_type_p (type2) : false));
17758 case MODOP_EXPR:
17759 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17760 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17762 case ADDR_EXPR:
17764 tree op = TREE_OPERAND (expression, 0);
17765 return (value_dependent_expression_p (op)
17766 || has_value_dependent_address (op));
17769 default:
17770 /* A constant expression is value-dependent if any subexpression is
17771 value-dependent. */
17772 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17774 case tcc_reference:
17775 case tcc_unary:
17776 return (value_dependent_expression_p
17777 (TREE_OPERAND (expression, 0)));
17779 case tcc_comparison:
17780 case tcc_binary:
17781 return ((value_dependent_expression_p
17782 (TREE_OPERAND (expression, 0)))
17783 || (value_dependent_expression_p
17784 (TREE_OPERAND (expression, 1))));
17786 case tcc_expression:
17787 case tcc_vl_exp:
17789 int i;
17790 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17791 /* In some cases, some of the operands may be missing.
17792 (For example, in the case of PREDECREMENT_EXPR, the
17793 amount to increment by may be missing.) That doesn't
17794 make the expression dependent. */
17795 if (TREE_OPERAND (expression, i)
17796 && (value_dependent_expression_p
17797 (TREE_OPERAND (expression, i))))
17798 return true;
17799 return false;
17802 default:
17803 break;
17807 /* The expression is not value-dependent. */
17808 return false;
17811 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17812 [temp.dep.expr]. */
17814 bool
17815 type_dependent_expression_p (tree expression)
17817 if (!processing_template_decl)
17818 return false;
17820 if (expression == error_mark_node)
17821 return false;
17823 /* An unresolved name is always dependent. */
17824 if (TREE_CODE (expression) == IDENTIFIER_NODE
17825 || TREE_CODE (expression) == USING_DECL)
17826 return true;
17828 /* Some expression forms are never type-dependent. */
17829 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17830 || TREE_CODE (expression) == SIZEOF_EXPR
17831 || TREE_CODE (expression) == ALIGNOF_EXPR
17832 || TREE_CODE (expression) == AT_ENCODE_EXPR
17833 || TREE_CODE (expression) == NOEXCEPT_EXPR
17834 || TREE_CODE (expression) == TRAIT_EXPR
17835 || TREE_CODE (expression) == TYPEID_EXPR
17836 || TREE_CODE (expression) == DELETE_EXPR
17837 || TREE_CODE (expression) == VEC_DELETE_EXPR
17838 || TREE_CODE (expression) == THROW_EXPR)
17839 return false;
17841 /* The types of these expressions depends only on the type to which
17842 the cast occurs. */
17843 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17844 || TREE_CODE (expression) == STATIC_CAST_EXPR
17845 || TREE_CODE (expression) == CONST_CAST_EXPR
17846 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17847 || TREE_CODE (expression) == CAST_EXPR)
17848 return dependent_type_p (TREE_TYPE (expression));
17850 /* The types of these expressions depends only on the type created
17851 by the expression. */
17852 if (TREE_CODE (expression) == NEW_EXPR
17853 || TREE_CODE (expression) == VEC_NEW_EXPR)
17855 /* For NEW_EXPR tree nodes created inside a template, either
17856 the object type itself or a TREE_LIST may appear as the
17857 operand 1. */
17858 tree type = TREE_OPERAND (expression, 1);
17859 if (TREE_CODE (type) == TREE_LIST)
17860 /* This is an array type. We need to check array dimensions
17861 as well. */
17862 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17863 || value_dependent_expression_p
17864 (TREE_OPERAND (TREE_VALUE (type), 1));
17865 else
17866 return dependent_type_p (type);
17869 if (TREE_CODE (expression) == SCOPE_REF)
17871 tree scope = TREE_OPERAND (expression, 0);
17872 tree name = TREE_OPERAND (expression, 1);
17874 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
17875 contains an identifier associated by name lookup with one or more
17876 declarations declared with a dependent type, or...a
17877 nested-name-specifier or qualified-id that names a member of an
17878 unknown specialization. */
17879 return (type_dependent_expression_p (name)
17880 || dependent_scope_p (scope));
17883 if (TREE_CODE (expression) == FUNCTION_DECL
17884 && DECL_LANG_SPECIFIC (expression)
17885 && DECL_TEMPLATE_INFO (expression)
17886 && (any_dependent_template_arguments_p
17887 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17888 return true;
17890 if (TREE_CODE (expression) == TEMPLATE_DECL
17891 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17892 return false;
17894 if (TREE_CODE (expression) == STMT_EXPR)
17895 expression = stmt_expr_value_expr (expression);
17897 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17899 tree elt;
17900 unsigned i;
17902 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17904 if (type_dependent_expression_p (elt))
17905 return true;
17907 return false;
17910 /* A static data member of the current instantiation with incomplete
17911 array type is type-dependent, as the definition and specializations
17912 can have different bounds. */
17913 if (TREE_CODE (expression) == VAR_DECL
17914 && DECL_CLASS_SCOPE_P (expression)
17915 && dependent_type_p (DECL_CONTEXT (expression))
17916 && VAR_HAD_UNKNOWN_BOUND (expression))
17917 return true;
17919 if (TREE_TYPE (expression) == unknown_type_node)
17921 if (TREE_CODE (expression) == ADDR_EXPR)
17922 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17923 if (TREE_CODE (expression) == COMPONENT_REF
17924 || TREE_CODE (expression) == OFFSET_REF)
17926 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17927 return true;
17928 expression = TREE_OPERAND (expression, 1);
17929 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17930 return false;
17932 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17933 if (TREE_CODE (expression) == SCOPE_REF)
17934 return false;
17936 if (TREE_CODE (expression) == BASELINK)
17937 expression = BASELINK_FUNCTIONS (expression);
17939 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17941 if (any_dependent_template_arguments_p
17942 (TREE_OPERAND (expression, 1)))
17943 return true;
17944 expression = TREE_OPERAND (expression, 0);
17946 gcc_assert (TREE_CODE (expression) == OVERLOAD
17947 || TREE_CODE (expression) == FUNCTION_DECL);
17949 while (expression)
17951 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17952 return true;
17953 expression = OVL_NEXT (expression);
17955 return false;
17958 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17960 return (dependent_type_p (TREE_TYPE (expression)));
17963 /* Like type_dependent_expression_p, but it also works while not processing
17964 a template definition, i.e. during substitution or mangling. */
17966 bool
17967 type_dependent_expression_p_push (tree expr)
17969 bool b;
17970 ++processing_template_decl;
17971 b = type_dependent_expression_p (expr);
17972 --processing_template_decl;
17973 return b;
17976 /* Returns TRUE if ARGS contains a type-dependent expression. */
17978 bool
17979 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17981 unsigned int i;
17982 tree arg;
17984 FOR_EACH_VEC_ELT (tree, args, i, arg)
17986 if (type_dependent_expression_p (arg))
17987 return true;
17989 return false;
17992 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17993 expressions) contains any value-dependent expressions. */
17995 bool
17996 any_value_dependent_elements_p (const_tree list)
17998 for (; list; list = TREE_CHAIN (list))
17999 if (value_dependent_expression_p (TREE_VALUE (list)))
18000 return true;
18002 return false;
18005 /* Returns TRUE if the ARG (a template argument) is dependent. */
18007 bool
18008 dependent_template_arg_p (tree arg)
18010 if (!processing_template_decl)
18011 return false;
18013 if (TREE_CODE (arg) == TEMPLATE_DECL
18014 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18015 return dependent_template_p (arg);
18016 else if (ARGUMENT_PACK_P (arg))
18018 tree args = ARGUMENT_PACK_ARGS (arg);
18019 int i, len = TREE_VEC_LENGTH (args);
18020 for (i = 0; i < len; ++i)
18022 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18023 return true;
18026 return false;
18028 else if (TYPE_P (arg))
18029 return dependent_type_p (arg);
18030 else
18031 return (type_dependent_expression_p (arg)
18032 || value_dependent_expression_p (arg));
18035 /* Returns true if ARGS (a collection of template arguments) contains
18036 any types that require structural equality testing. */
18038 bool
18039 any_template_arguments_need_structural_equality_p (tree args)
18041 int i;
18042 int j;
18044 if (!args)
18045 return false;
18046 if (args == error_mark_node)
18047 return true;
18049 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18051 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18052 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18054 tree arg = TREE_VEC_ELT (level, j);
18055 tree packed_args = NULL_TREE;
18056 int k, len = 1;
18058 if (ARGUMENT_PACK_P (arg))
18060 /* Look inside the argument pack. */
18061 packed_args = ARGUMENT_PACK_ARGS (arg);
18062 len = TREE_VEC_LENGTH (packed_args);
18065 for (k = 0; k < len; ++k)
18067 if (packed_args)
18068 arg = TREE_VEC_ELT (packed_args, k);
18070 if (error_operand_p (arg))
18071 return true;
18072 else if (TREE_CODE (arg) == TEMPLATE_DECL
18073 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18074 continue;
18075 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18076 return true;
18077 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18078 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18079 return true;
18084 return false;
18087 /* Returns true if ARGS (a collection of template arguments) contains
18088 any dependent arguments. */
18090 bool
18091 any_dependent_template_arguments_p (const_tree args)
18093 int i;
18094 int j;
18096 if (!args)
18097 return false;
18098 if (args == error_mark_node)
18099 return true;
18101 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18103 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18104 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18105 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18106 return true;
18109 return false;
18112 /* Returns TRUE if the template TMPL is dependent. */
18114 bool
18115 dependent_template_p (tree tmpl)
18117 if (TREE_CODE (tmpl) == OVERLOAD)
18119 while (tmpl)
18121 if (dependent_template_p (OVL_FUNCTION (tmpl)))
18122 return true;
18123 tmpl = OVL_CHAIN (tmpl);
18125 return false;
18128 /* Template template parameters are dependent. */
18129 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18130 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18131 return true;
18132 /* So are names that have not been looked up. */
18133 if (TREE_CODE (tmpl) == SCOPE_REF
18134 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18135 return true;
18136 /* So are member templates of dependent classes. */
18137 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18138 return dependent_type_p (DECL_CONTEXT (tmpl));
18139 return false;
18142 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18144 bool
18145 dependent_template_id_p (tree tmpl, tree args)
18147 return (dependent_template_p (tmpl)
18148 || any_dependent_template_arguments_p (args));
18151 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18152 is dependent. */
18154 bool
18155 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18157 int i;
18159 if (!processing_template_decl)
18160 return false;
18162 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18164 tree decl = TREE_VEC_ELT (declv, i);
18165 tree init = TREE_VEC_ELT (initv, i);
18166 tree cond = TREE_VEC_ELT (condv, i);
18167 tree incr = TREE_VEC_ELT (incrv, i);
18169 if (type_dependent_expression_p (decl))
18170 return true;
18172 if (init && type_dependent_expression_p (init))
18173 return true;
18175 if (type_dependent_expression_p (cond))
18176 return true;
18178 if (COMPARISON_CLASS_P (cond)
18179 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18180 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18181 return true;
18183 if (TREE_CODE (incr) == MODOP_EXPR)
18185 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18186 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18187 return true;
18189 else if (type_dependent_expression_p (incr))
18190 return true;
18191 else if (TREE_CODE (incr) == MODIFY_EXPR)
18193 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18194 return true;
18195 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18197 tree t = TREE_OPERAND (incr, 1);
18198 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18199 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18200 return true;
18205 return false;
18208 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18209 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18210 no such TYPE can be found. Note that this function peers inside
18211 uninstantiated templates and therefore should be used only in
18212 extremely limited situations. ONLY_CURRENT_P restricts this
18213 peering to the currently open classes hierarchy (which is required
18214 when comparing types). */
18216 tree
18217 resolve_typename_type (tree type, bool only_current_p)
18219 tree scope;
18220 tree name;
18221 tree decl;
18222 int quals;
18223 tree pushed_scope;
18224 tree result;
18226 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18228 scope = TYPE_CONTEXT (type);
18229 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18230 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18231 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18232 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18233 identifier of the TYPENAME_TYPE anymore.
18234 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18235 TYPENAME_TYPE instead, we avoid messing up with a possible
18236 typedef variant case. */
18237 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18239 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18240 it first before we can figure out what NAME refers to. */
18241 if (TREE_CODE (scope) == TYPENAME_TYPE)
18242 scope = resolve_typename_type (scope, only_current_p);
18243 /* If we don't know what SCOPE refers to, then we cannot resolve the
18244 TYPENAME_TYPE. */
18245 if (TREE_CODE (scope) == TYPENAME_TYPE)
18246 return type;
18247 /* If the SCOPE is a template type parameter, we have no way of
18248 resolving the name. */
18249 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18250 return type;
18251 /* If the SCOPE is not the current instantiation, there's no reason
18252 to look inside it. */
18253 if (only_current_p && !currently_open_class (scope))
18254 return type;
18255 /* If this is a typedef, we don't want to look inside (c++/11987). */
18256 if (typedef_variant_p (type))
18257 return type;
18258 /* If SCOPE isn't the template itself, it will not have a valid
18259 TYPE_FIELDS list. */
18260 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18261 /* scope is either the template itself or a compatible instantiation
18262 like X<T>, so look up the name in the original template. */
18263 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18264 else
18265 /* scope is a partial instantiation, so we can't do the lookup or we
18266 will lose the template arguments. */
18267 return type;
18268 /* Enter the SCOPE so that name lookup will be resolved as if we
18269 were in the class definition. In particular, SCOPE will no
18270 longer be considered a dependent type. */
18271 pushed_scope = push_scope (scope);
18272 /* Look up the declaration. */
18273 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18275 result = NULL_TREE;
18277 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18278 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18279 if (!decl)
18280 /*nop*/;
18281 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18282 && TREE_CODE (decl) == TYPE_DECL)
18284 result = TREE_TYPE (decl);
18285 if (result == error_mark_node)
18286 result = NULL_TREE;
18288 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18289 && DECL_CLASS_TEMPLATE_P (decl))
18291 tree tmpl;
18292 tree args;
18293 /* Obtain the template and the arguments. */
18294 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18295 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18296 /* Instantiate the template. */
18297 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18298 /*entering_scope=*/0,
18299 tf_error | tf_user);
18300 if (result == error_mark_node)
18301 result = NULL_TREE;
18304 /* Leave the SCOPE. */
18305 if (pushed_scope)
18306 pop_scope (pushed_scope);
18308 /* If we failed to resolve it, return the original typename. */
18309 if (!result)
18310 return type;
18312 /* If lookup found a typename type, resolve that too. */
18313 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18315 /* Ill-formed programs can cause infinite recursion here, so we
18316 must catch that. */
18317 TYPENAME_IS_RESOLVING_P (type) = 1;
18318 result = resolve_typename_type (result, only_current_p);
18319 TYPENAME_IS_RESOLVING_P (type) = 0;
18322 /* Qualify the resulting type. */
18323 quals = cp_type_quals (type);
18324 if (quals)
18325 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18327 return result;
18330 /* EXPR is an expression which is not type-dependent. Return a proxy
18331 for EXPR that can be used to compute the types of larger
18332 expressions containing EXPR. */
18334 tree
18335 build_non_dependent_expr (tree expr)
18337 tree inner_expr;
18339 /* Preserve null pointer constants so that the type of things like
18340 "p == 0" where "p" is a pointer can be determined. */
18341 if (null_ptr_cst_p (expr))
18342 return expr;
18343 /* Preserve OVERLOADs; the functions must be available to resolve
18344 types. */
18345 inner_expr = expr;
18346 if (TREE_CODE (inner_expr) == STMT_EXPR)
18347 inner_expr = stmt_expr_value_expr (inner_expr);
18348 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18349 inner_expr = TREE_OPERAND (inner_expr, 0);
18350 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18351 inner_expr = TREE_OPERAND (inner_expr, 1);
18352 if (is_overloaded_fn (inner_expr)
18353 || TREE_CODE (inner_expr) == OFFSET_REF)
18354 return expr;
18355 /* There is no need to return a proxy for a variable. */
18356 if (TREE_CODE (expr) == VAR_DECL)
18357 return expr;
18358 /* Preserve string constants; conversions from string constants to
18359 "char *" are allowed, even though normally a "const char *"
18360 cannot be used to initialize a "char *". */
18361 if (TREE_CODE (expr) == STRING_CST)
18362 return expr;
18363 /* Preserve arithmetic constants, as an optimization -- there is no
18364 reason to create a new node. */
18365 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18366 return expr;
18367 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18368 There is at least one place where we want to know that a
18369 particular expression is a throw-expression: when checking a ?:
18370 expression, there are special rules if the second or third
18371 argument is a throw-expression. */
18372 if (TREE_CODE (expr) == THROW_EXPR)
18373 return expr;
18375 if (TREE_CODE (expr) == COND_EXPR)
18376 return build3 (COND_EXPR,
18377 TREE_TYPE (expr),
18378 TREE_OPERAND (expr, 0),
18379 (TREE_OPERAND (expr, 1)
18380 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18381 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18382 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18383 if (TREE_CODE (expr) == COMPOUND_EXPR
18384 && !COMPOUND_EXPR_OVERLOADED (expr))
18385 return build2 (COMPOUND_EXPR,
18386 TREE_TYPE (expr),
18387 TREE_OPERAND (expr, 0),
18388 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18390 /* If the type is unknown, it can't really be non-dependent */
18391 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18393 /* Otherwise, build a NON_DEPENDENT_EXPR.
18395 REFERENCE_TYPEs are not stripped for expressions in templates
18396 because doing so would play havoc with mangling. Consider, for
18397 example:
18399 template <typename T> void f<T& g>() { g(); }
18401 In the body of "f", the expression for "g" will have
18402 REFERENCE_TYPE, even though the standard says that it should
18403 not. The reason is that we must preserve the syntactic form of
18404 the expression so that mangling (say) "f<g>" inside the body of
18405 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18406 stripped here. */
18407 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18410 /* ARGS is a vector of expressions as arguments to a function call.
18411 Replace the arguments with equivalent non-dependent expressions.
18412 This modifies ARGS in place. */
18414 void
18415 make_args_non_dependent (VEC(tree,gc) *args)
18417 unsigned int ix;
18418 tree arg;
18420 FOR_EACH_VEC_ELT (tree, args, ix, arg)
18422 tree newarg = build_non_dependent_expr (arg);
18423 if (newarg != arg)
18424 VEC_replace (tree, args, ix, newarg);
18428 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18429 with a level one deeper than the actual template parms. */
18431 tree
18432 make_auto (void)
18434 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18435 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18436 TYPE_DECL, get_identifier ("auto"), au);
18437 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18438 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18439 (0, processing_template_decl + 1, processing_template_decl + 1,
18440 TYPE_NAME (au), NULL_TREE);
18441 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18442 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18443 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18445 return au;
18448 /* Given type ARG, return std::initializer_list<ARG>. */
18450 static tree
18451 listify (tree arg)
18453 tree std_init_list = namespace_binding
18454 (get_identifier ("initializer_list"), std_node);
18455 tree argvec;
18456 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18458 error ("deducing from brace-enclosed initializer list requires "
18459 "#include <initializer_list>");
18460 return error_mark_node;
18462 argvec = make_tree_vec (1);
18463 TREE_VEC_ELT (argvec, 0) = arg;
18464 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18465 NULL_TREE, 0, tf_warning_or_error);
18468 /* Replace auto in TYPE with std::initializer_list<auto>. */
18470 static tree
18471 listify_autos (tree type, tree auto_node)
18473 tree init_auto = listify (auto_node);
18474 tree argvec = make_tree_vec (1);
18475 TREE_VEC_ELT (argvec, 0) = init_auto;
18476 if (processing_template_decl)
18477 argvec = add_to_template_args (current_template_args (), argvec);
18478 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18481 /* walk_tree helper for do_auto_deduction. */
18483 static tree
18484 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18485 void *type)
18487 /* Is this a variable with the type we're looking for? */
18488 if (DECL_P (*tp)
18489 && TREE_TYPE (*tp) == type)
18490 return *tp;
18491 else
18492 return NULL_TREE;
18495 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18496 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18498 tree
18499 do_auto_deduction (tree type, tree init, tree auto_node)
18501 tree parms, tparms, targs;
18502 tree args[1];
18503 tree decl;
18504 int val;
18506 /* The name of the object being declared shall not appear in the
18507 initializer expression. */
18508 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18509 if (decl)
18511 error ("variable %q#D with %<auto%> type used in its own "
18512 "initializer", decl);
18513 return error_mark_node;
18516 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18517 with either a new invented type template parameter U or, if the
18518 initializer is a braced-init-list (8.5.4), with
18519 std::initializer_list<U>. */
18520 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18521 type = listify_autos (type, auto_node);
18523 parms = build_tree_list (NULL_TREE, type);
18524 args[0] = init;
18525 tparms = make_tree_vec (1);
18526 targs = make_tree_vec (1);
18527 TREE_VEC_ELT (tparms, 0)
18528 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18529 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18530 DEDUCE_CALL, LOOKUP_NORMAL);
18531 if (val > 0)
18533 error ("unable to deduce %qT from %qE", type, init);
18534 return error_mark_node;
18537 /* If the list of declarators contains more than one declarator, the type
18538 of each declared variable is determined as described above. If the
18539 type deduced for the template parameter U is not the same in each
18540 deduction, the program is ill-formed. */
18541 if (TREE_TYPE (auto_node)
18542 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18544 error ("inconsistent deduction for %qT: %qT and then %qT",
18545 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18546 return error_mark_node;
18548 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18550 if (processing_template_decl)
18551 targs = add_to_template_args (current_template_args (), targs);
18552 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18555 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18556 result. */
18558 tree
18559 splice_late_return_type (tree type, tree late_return_type)
18561 tree argvec;
18563 if (late_return_type == NULL_TREE)
18564 return type;
18565 argvec = make_tree_vec (1);
18566 TREE_VEC_ELT (argvec, 0) = late_return_type;
18567 if (processing_template_decl)
18568 argvec = add_to_template_args (current_template_args (), argvec);
18569 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18572 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18574 bool
18575 is_auto (const_tree type)
18577 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18578 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18579 return true;
18580 else
18581 return false;
18584 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18585 appear as a type-specifier for the declaration in question, we don't
18586 have to look through the whole type. */
18588 tree
18589 type_uses_auto (tree type)
18591 enum tree_code code;
18592 if (is_auto (type))
18593 return type;
18595 code = TREE_CODE (type);
18597 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18598 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18599 || code == METHOD_TYPE || code == ARRAY_TYPE)
18600 return type_uses_auto (TREE_TYPE (type));
18602 if (TYPE_PTRMEMFUNC_P (type))
18603 return type_uses_auto (TREE_TYPE (TREE_TYPE
18604 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18606 return NULL_TREE;
18609 /* For a given template T, return the vector of typedefs referenced
18610 in T for which access check is needed at T instantiation time.
18611 T is either a FUNCTION_DECL or a RECORD_TYPE.
18612 Those typedefs were added to T by the function
18613 append_type_to_template_for_access_check. */
18615 VEC(qualified_typedef_usage_t,gc)*
18616 get_types_needing_access_check (tree t)
18618 tree ti;
18619 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18621 if (!t || t == error_mark_node)
18622 return NULL;
18624 if (!(ti = get_template_info (t)))
18625 return NULL;
18627 if (CLASS_TYPE_P (t)
18628 || TREE_CODE (t) == FUNCTION_DECL)
18630 if (!TI_TEMPLATE (ti))
18631 return NULL;
18633 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18636 return result;
18639 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18640 tied to T. That list of typedefs will be access checked at
18641 T instantiation time.
18642 T is either a FUNCTION_DECL or a RECORD_TYPE.
18643 TYPE_DECL is a TYPE_DECL node representing a typedef.
18644 SCOPE is the scope through which TYPE_DECL is accessed.
18645 LOCATION is the location of the usage point of TYPE_DECL.
18647 This function is a subroutine of
18648 append_type_to_template_for_access_check. */
18650 static void
18651 append_type_to_template_for_access_check_1 (tree t,
18652 tree type_decl,
18653 tree scope,
18654 location_t location)
18656 qualified_typedef_usage_t typedef_usage;
18657 tree ti;
18659 if (!t || t == error_mark_node)
18660 return;
18662 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18663 || CLASS_TYPE_P (t))
18664 && type_decl
18665 && TREE_CODE (type_decl) == TYPE_DECL
18666 && scope);
18668 if (!(ti = get_template_info (t)))
18669 return;
18671 gcc_assert (TI_TEMPLATE (ti));
18673 typedef_usage.typedef_decl = type_decl;
18674 typedef_usage.context = scope;
18675 typedef_usage.locus = location;
18677 VEC_safe_push (qualified_typedef_usage_t, gc,
18678 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18679 &typedef_usage);
18682 /* Append TYPE_DECL to the template TEMPL.
18683 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18684 At TEMPL instanciation time, TYPE_DECL will be checked to see
18685 if it can be accessed through SCOPE.
18686 LOCATION is the location of the usage point of TYPE_DECL.
18688 e.g. consider the following code snippet:
18690 class C
18692 typedef int myint;
18695 template<class U> struct S
18697 C::myint mi; // <-- usage point of the typedef C::myint
18700 S<char> s;
18702 At S<char> instantiation time, we need to check the access of C::myint
18703 In other words, we need to check the access of the myint typedef through
18704 the C scope. For that purpose, this function will add the myint typedef
18705 and the scope C through which its being accessed to a list of typedefs
18706 tied to the template S. That list will be walked at template instantiation
18707 time and access check performed on each typedefs it contains.
18708 Note that this particular code snippet should yield an error because
18709 myint is private to C. */
18711 void
18712 append_type_to_template_for_access_check (tree templ,
18713 tree type_decl,
18714 tree scope,
18715 location_t location)
18717 qualified_typedef_usage_t *iter;
18718 int i;
18720 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18722 /* Make sure we don't append the type to the template twice. */
18723 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
18724 get_types_needing_access_check (templ),
18725 i, iter)
18726 if (iter->typedef_decl == type_decl && scope == iter->context)
18727 return;
18729 append_type_to_template_for_access_check_1 (templ, type_decl,
18730 scope, location);
18733 /* Set up the hash tables for template instantiations. */
18735 void
18736 init_template_processing (void)
18738 decl_specializations = htab_create_ggc (37,
18739 hash_specialization,
18740 eq_specializations,
18741 ggc_free);
18742 type_specializations = htab_create_ggc (37,
18743 hash_specialization,
18744 eq_specializations,
18745 ggc_free);
18748 /* Print stats about the template hash tables for -fstats. */
18750 void
18751 print_template_statistics (void)
18753 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
18754 "%f collisions\n", (long) htab_size (decl_specializations),
18755 (long) htab_elements (decl_specializations),
18756 htab_collisions (decl_specializations));
18757 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
18758 "%f collisions\n", (long) htab_size (type_specializations),
18759 (long) htab_elements (type_specializations),
18760 htab_collisions (type_specializations));
18763 #include "gt-cp-pt.h"