2009-02-24 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / cp / pt.c
blobdacc8689f38a8cdf7e2f509f0b6b1505932b4e3d
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
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 "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "vecprim.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct pending_template GTY (()) {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
82 local variables. */
83 static htab_t local_specializations;
85 /* Contains canonical template parameter types. The vector is indexed by
86 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
87 TREE_LIST, whose TREE_VALUEs contain the canonical template
88 parameters of various types and levels. */
89 static GTY(()) VEC(tree,gc) *canonical_template_parms;
91 #define UNIFY_ALLOW_NONE 0
92 #define UNIFY_ALLOW_MORE_CV_QUAL 1
93 #define UNIFY_ALLOW_LESS_CV_QUAL 2
94 #define UNIFY_ALLOW_DERIVED 4
95 #define UNIFY_ALLOW_INTEGER 8
96 #define UNIFY_ALLOW_OUTER_LEVEL 16
97 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
98 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
100 static void push_access_scope (tree);
101 static void pop_access_scope (tree);
102 static bool resolve_overloaded_unification (tree, tree, tree, tree,
103 unification_kind_t, int);
104 static int try_one_overload (tree, tree, tree, tree, tree,
105 unification_kind_t, int, bool);
106 static int unify (tree, tree, tree, tree, int);
107 static void add_pending_template (tree);
108 static int push_tinst_level (tree);
109 static void pop_tinst_level (void);
110 static tree reopen_tinst_level (struct tinst_level *);
111 static tree tsubst_initializer_list (tree, tree);
112 static tree get_class_bindings (tree, tree, tree);
113 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
114 bool, bool);
115 static void tsubst_enum (tree, tree, tree);
116 static tree add_to_template_args (tree, tree);
117 static tree add_outermost_template_args (tree, tree);
118 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
119 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
120 tree);
121 static int type_unification_real (tree, tree, tree, tree,
122 int, unification_kind_t, int);
123 static void note_template_header (int);
124 static tree convert_nontype_argument_function (tree, tree);
125 static tree convert_nontype_argument (tree, tree);
126 static tree convert_template_argument (tree, tree, tree,
127 tsubst_flags_t, int, tree);
128 static int for_each_template_parm (tree, tree_fn_t, void*,
129 struct pointer_set_t*, bool);
130 static tree expand_template_argument_pack (tree);
131 static tree build_template_parm_index (int, int, int, tree, tree);
132 static bool inline_needs_template_parms (tree);
133 static void push_inline_template_parms_recursive (tree, int);
134 static tree retrieve_local_specialization (tree);
135 static void register_local_specialization (tree, tree);
136 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
137 static int mark_template_parm (tree, void *);
138 static int template_parm_this_level_p (tree, void *);
139 static tree tsubst_friend_function (tree, tree);
140 static tree tsubst_friend_class (tree, tree);
141 static int can_complete_type_without_circularity (tree);
142 static tree get_bindings (tree, tree, tree, bool);
143 static int template_decl_level (tree);
144 static int check_cv_quals_for_unify (int, tree, tree);
145 static void template_parm_level_and_index (tree, int*, int*);
146 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
147 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
148 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
149 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
150 static void regenerate_decl_from_template (tree, tree);
151 static tree most_specialized_class (tree, tree);
152 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
153 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
154 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
155 static bool check_specialization_scope (void);
156 static tree process_partial_specialization (tree);
157 static void set_current_access_from_decl (tree);
158 static tree get_template_base (tree, tree, tree, tree);
159 static tree try_class_unification (tree, tree, tree, tree);
160 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
161 tree, tree);
162 static bool template_template_parm_bindings_ok_p (tree, tree);
163 static int template_args_equal (tree, tree);
164 static void tsubst_default_arguments (tree);
165 static tree for_each_template_parm_r (tree *, int *, void *);
166 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
167 static void copy_default_args_to_explicit_spec (tree);
168 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
169 static int eq_local_specializations (const void *, const void *);
170 static bool dependent_template_arg_p (tree);
171 static bool any_template_arguments_need_structural_equality_p (tree);
172 static bool dependent_type_p_r (tree);
173 static tree tsubst (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
175 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
178 /* Make the current scope suitable for access checking when we are
179 processing T. T can be FUNCTION_DECL for instantiated function
180 template, or VAR_DECL for static member variable (need by
181 instantiate_decl). */
183 static void
184 push_access_scope (tree t)
186 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
187 || TREE_CODE (t) == VAR_DECL);
189 if (DECL_FRIEND_CONTEXT (t))
190 push_nested_class (DECL_FRIEND_CONTEXT (t));
191 else if (DECL_CLASS_SCOPE_P (t))
192 push_nested_class (DECL_CONTEXT (t));
193 else
194 push_to_top_level ();
196 if (TREE_CODE (t) == FUNCTION_DECL)
198 saved_access_scope = tree_cons
199 (NULL_TREE, current_function_decl, saved_access_scope);
200 current_function_decl = t;
204 /* Restore the scope set up by push_access_scope. T is the node we
205 are processing. */
207 static void
208 pop_access_scope (tree t)
210 if (TREE_CODE (t) == FUNCTION_DECL)
212 current_function_decl = TREE_VALUE (saved_access_scope);
213 saved_access_scope = TREE_CHAIN (saved_access_scope);
216 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
217 pop_nested_class ();
218 else
219 pop_from_top_level ();
222 /* Do any processing required when DECL (a member template
223 declaration) is finished. Returns the TEMPLATE_DECL corresponding
224 to DECL, unless it is a specialization, in which case the DECL
225 itself is returned. */
227 tree
228 finish_member_template_decl (tree decl)
230 if (decl == error_mark_node)
231 return error_mark_node;
233 gcc_assert (DECL_P (decl));
235 if (TREE_CODE (decl) == TYPE_DECL)
237 tree type;
239 type = TREE_TYPE (decl);
240 if (type == error_mark_node)
241 return error_mark_node;
242 if (MAYBE_CLASS_TYPE_P (type)
243 && CLASSTYPE_TEMPLATE_INFO (type)
244 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
246 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
247 check_member_template (tmpl);
248 return tmpl;
250 return NULL_TREE;
252 else if (TREE_CODE (decl) == FIELD_DECL)
253 error ("data member %qD cannot be a member template", decl);
254 else if (DECL_TEMPLATE_INFO (decl))
256 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
258 check_member_template (DECL_TI_TEMPLATE (decl));
259 return DECL_TI_TEMPLATE (decl);
261 else
262 return decl;
264 else
265 error ("invalid member template declaration %qD", decl);
267 return error_mark_node;
270 /* Return the template info node corresponding to T, whatever T is. */
272 tree
273 get_template_info (tree t)
275 tree tinfo = NULL_TREE;
277 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
278 tinfo = DECL_TEMPLATE_INFO (t);
280 if (!tinfo && TREE_CODE (t) == TYPE_DECL)
281 t = TREE_TYPE (t);
283 if (TAGGED_TYPE_P (t))
284 tinfo = TYPE_TEMPLATE_INFO (t);
286 return tinfo;
289 /* Returns the template nesting level of the indicated class TYPE.
291 For example, in:
292 template <class T>
293 struct A
295 template <class U>
296 struct B {};
299 A<T>::B<U> has depth two, while A<T> has depth one.
300 Both A<T>::B<int> and A<int>::B<U> have depth one, if
301 they are instantiations, not specializations.
303 This function is guaranteed to return 0 if passed NULL_TREE so
304 that, for example, `template_class_depth (current_class_type)' is
305 always safe. */
308 template_class_depth (tree type)
310 int depth;
312 for (depth = 0;
313 type && TREE_CODE (type) != NAMESPACE_DECL;
314 type = (TREE_CODE (type) == FUNCTION_DECL)
315 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
317 tree tinfo = get_template_info (type);
319 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
320 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
321 ++depth;
324 return depth;
327 /* Subroutine of maybe_begin_member_template_processing.
328 Returns true if processing DECL needs us to push template parms. */
330 static bool
331 inline_needs_template_parms (tree decl)
333 if (! DECL_TEMPLATE_INFO (decl))
334 return false;
336 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
337 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
340 /* Subroutine of maybe_begin_member_template_processing.
341 Push the template parms in PARMS, starting from LEVELS steps into the
342 chain, and ending at the beginning, since template parms are listed
343 innermost first. */
345 static void
346 push_inline_template_parms_recursive (tree parmlist, int levels)
348 tree parms = TREE_VALUE (parmlist);
349 int i;
351 if (levels > 1)
352 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
354 ++processing_template_decl;
355 current_template_parms
356 = tree_cons (size_int (processing_template_decl),
357 parms, current_template_parms);
358 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
360 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
361 NULL);
362 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
364 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
366 if (parm == error_mark_node)
367 continue;
369 gcc_assert (DECL_P (parm));
371 switch (TREE_CODE (parm))
373 case TYPE_DECL:
374 case TEMPLATE_DECL:
375 pushdecl (parm);
376 break;
378 case PARM_DECL:
380 /* Make a CONST_DECL as is done in process_template_parm.
381 It is ugly that we recreate this here; the original
382 version built in process_template_parm is no longer
383 available. */
384 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
385 TREE_TYPE (parm));
386 DECL_ARTIFICIAL (decl) = 1;
387 TREE_CONSTANT (decl) = 1;
388 TREE_READONLY (decl) = 1;
389 DECL_INITIAL (decl) = DECL_INITIAL (parm);
390 SET_DECL_TEMPLATE_PARM_P (decl);
391 pushdecl (decl);
393 break;
395 default:
396 gcc_unreachable ();
401 /* Restore the template parameter context for a member template or
402 a friend template defined in a class definition. */
404 void
405 maybe_begin_member_template_processing (tree decl)
407 tree parms;
408 int levels = 0;
410 if (inline_needs_template_parms (decl))
412 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
413 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
415 if (DECL_TEMPLATE_SPECIALIZATION (decl))
417 --levels;
418 parms = TREE_CHAIN (parms);
421 push_inline_template_parms_recursive (parms, levels);
424 /* Remember how many levels of template parameters we pushed so that
425 we can pop them later. */
426 VEC_safe_push (int, heap, inline_parm_levels, levels);
429 /* Undo the effects of maybe_begin_member_template_processing. */
431 void
432 maybe_end_member_template_processing (void)
434 int i;
435 int last;
437 if (VEC_length (int, inline_parm_levels) == 0)
438 return;
440 last = VEC_pop (int, inline_parm_levels);
441 for (i = 0; i < last; ++i)
443 --processing_template_decl;
444 current_template_parms = TREE_CHAIN (current_template_parms);
445 poplevel (0, 0, 0);
449 /* Return a new template argument vector which contains all of ARGS,
450 but has as its innermost set of arguments the EXTRA_ARGS. */
452 static tree
453 add_to_template_args (tree args, tree extra_args)
455 tree new_args;
456 int extra_depth;
457 int i;
458 int j;
460 extra_depth = TMPL_ARGS_DEPTH (extra_args);
461 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
463 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
464 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
466 for (j = 1; j <= extra_depth; ++j, ++i)
467 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
469 return new_args;
472 /* Like add_to_template_args, but only the outermost ARGS are added to
473 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
474 (EXTRA_ARGS) levels are added. This function is used to combine
475 the template arguments from a partial instantiation with the
476 template arguments used to attain the full instantiation from the
477 partial instantiation. */
479 static tree
480 add_outermost_template_args (tree args, tree extra_args)
482 tree new_args;
484 /* If there are more levels of EXTRA_ARGS than there are ARGS,
485 something very fishy is going on. */
486 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
488 /* If *all* the new arguments will be the EXTRA_ARGS, just return
489 them. */
490 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
491 return extra_args;
493 /* For the moment, we make ARGS look like it contains fewer levels. */
494 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
496 new_args = add_to_template_args (args, extra_args);
498 /* Now, we restore ARGS to its full dimensions. */
499 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
501 return new_args;
504 /* Return the N levels of innermost template arguments from the ARGS. */
506 tree
507 get_innermost_template_args (tree args, int n)
509 tree new_args;
510 int extra_levels;
511 int i;
513 gcc_assert (n >= 0);
515 /* If N is 1, just return the innermost set of template arguments. */
516 if (n == 1)
517 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
519 /* If we're not removing anything, just return the arguments we were
520 given. */
521 extra_levels = TMPL_ARGS_DEPTH (args) - n;
522 gcc_assert (extra_levels >= 0);
523 if (extra_levels == 0)
524 return args;
526 /* Make a new set of arguments, not containing the outer arguments. */
527 new_args = make_tree_vec (n);
528 for (i = 1; i <= n; ++i)
529 SET_TMPL_ARGS_LEVEL (new_args, i,
530 TMPL_ARGS_LEVEL (args, i + extra_levels));
532 return new_args;
535 /* The inverse of get_innermost_template_args: Return all but the innermost
536 EXTRA_LEVELS levels of template arguments from the ARGS. */
538 static tree
539 strip_innermost_template_args (tree args, int extra_levels)
541 tree new_args;
542 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
543 int i;
545 gcc_assert (n >= 0);
547 /* If N is 1, just return the outermost set of template arguments. */
548 if (n == 1)
549 return TMPL_ARGS_LEVEL (args, 1);
551 /* If we're not removing anything, just return the arguments we were
552 given. */
553 gcc_assert (extra_levels >= 0);
554 if (extra_levels == 0)
555 return args;
557 /* Make a new set of arguments, not containing the inner arguments. */
558 new_args = make_tree_vec (n);
559 for (i = 1; i <= n; ++i)
560 SET_TMPL_ARGS_LEVEL (new_args, i,
561 TMPL_ARGS_LEVEL (args, i));
563 return new_args;
566 /* We've got a template header coming up; push to a new level for storing
567 the parms. */
569 void
570 begin_template_parm_list (void)
572 /* We use a non-tag-transparent scope here, which causes pushtag to
573 put tags in this scope, rather than in the enclosing class or
574 namespace scope. This is the right thing, since we want
575 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
576 global template class, push_template_decl handles putting the
577 TEMPLATE_DECL into top-level scope. For a nested template class,
578 e.g.:
580 template <class T> struct S1 {
581 template <class T> struct S2 {};
584 pushtag contains special code to call pushdecl_with_scope on the
585 TEMPLATE_DECL for S2. */
586 begin_scope (sk_template_parms, NULL);
587 ++processing_template_decl;
588 ++processing_template_parmlist;
589 note_template_header (0);
592 /* This routine is called when a specialization is declared. If it is
593 invalid to declare a specialization here, an error is reported and
594 false is returned, otherwise this routine will return true. */
596 static bool
597 check_specialization_scope (void)
599 tree scope = current_scope ();
601 /* [temp.expl.spec]
603 An explicit specialization shall be declared in the namespace of
604 which the template is a member, or, for member templates, in the
605 namespace of which the enclosing class or enclosing class
606 template is a member. An explicit specialization of a member
607 function, member class or static data member of a class template
608 shall be declared in the namespace of which the class template
609 is a member. */
610 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
612 error ("explicit specialization in non-namespace scope %qD", scope);
613 return false;
616 /* [temp.expl.spec]
618 In an explicit specialization declaration for a member of a class
619 template or a member template that appears in namespace scope,
620 the member template and some of its enclosing class templates may
621 remain unspecialized, except that the declaration shall not
622 explicitly specialize a class member template if its enclosing
623 class templates are not explicitly specialized as well. */
624 if (current_template_parms)
626 error ("enclosing class templates are not explicitly specialized");
627 return false;
630 return true;
633 /* We've just seen template <>. */
635 bool
636 begin_specialization (void)
638 begin_scope (sk_template_spec, NULL);
639 note_template_header (1);
640 return check_specialization_scope ();
643 /* Called at then end of processing a declaration preceded by
644 template<>. */
646 void
647 end_specialization (void)
649 finish_scope ();
650 reset_specialization ();
653 /* Any template <>'s that we have seen thus far are not referring to a
654 function specialization. */
656 void
657 reset_specialization (void)
659 processing_specialization = 0;
660 template_header_count = 0;
663 /* We've just seen a template header. If SPECIALIZATION is nonzero,
664 it was of the form template <>. */
666 static void
667 note_template_header (int specialization)
669 processing_specialization = specialization;
670 template_header_count++;
673 /* We're beginning an explicit instantiation. */
675 void
676 begin_explicit_instantiation (void)
678 gcc_assert (!processing_explicit_instantiation);
679 processing_explicit_instantiation = true;
683 void
684 end_explicit_instantiation (void)
686 gcc_assert (processing_explicit_instantiation);
687 processing_explicit_instantiation = false;
690 /* An explicit specialization or partial specialization TMPL is being
691 declared. Check that the namespace in which the specialization is
692 occurring is permissible. Returns false iff it is invalid to
693 specialize TMPL in the current namespace. */
695 static bool
696 check_specialization_namespace (tree tmpl)
698 tree tpl_ns = decl_namespace_context (tmpl);
700 /* [tmpl.expl.spec]
702 An explicit specialization shall be declared in the namespace of
703 which the template is a member, or, for member templates, in the
704 namespace of which the enclosing class or enclosing class
705 template is a member. An explicit specialization of a member
706 function, member class or static data member of a class template
707 shall be declared in the namespace of which the class template is
708 a member. */
709 if (is_associated_namespace (current_namespace, tpl_ns))
710 /* Same or super-using namespace. */
711 return true;
712 else
714 permerror (input_location, "specialization of %qD in different namespace", tmpl);
715 permerror (input_location, " from definition of %q+#D", tmpl);
716 return false;
720 /* SPEC is an explicit instantiation. Check that it is valid to
721 perform this explicit instantiation in the current namespace. */
723 static void
724 check_explicit_instantiation_namespace (tree spec)
726 tree ns;
728 /* DR 275: An explicit instantiation shall appear in an enclosing
729 namespace of its template. */
730 ns = decl_namespace_context (spec);
731 if (!is_ancestor (current_namespace, ns))
732 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
733 "(which does not enclose namespace %qD)",
734 spec, current_namespace, ns);
737 /* The TYPE is being declared. If it is a template type, that means it
738 is a partial specialization. Do appropriate error-checking. */
740 tree
741 maybe_process_partial_specialization (tree type)
743 tree context;
745 if (type == error_mark_node)
746 return error_mark_node;
748 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
750 error ("name of class shadows template template parameter %qD",
751 TYPE_NAME (type));
752 return error_mark_node;
755 context = TYPE_CONTEXT (type);
757 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
759 /* This is for ordinary explicit specialization and partial
760 specialization of a template class such as:
762 template <> class C<int>;
766 template <class T> class C<T*>;
768 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
770 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
771 && !COMPLETE_TYPE_P (type))
773 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
774 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
775 if (processing_template_decl)
777 if (push_template_decl (TYPE_MAIN_DECL (type))
778 == error_mark_node)
779 return error_mark_node;
782 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
783 error ("specialization of %qT after instantiation", type);
785 else if (CLASS_TYPE_P (type)
786 && !CLASSTYPE_USE_TEMPLATE (type)
787 && CLASSTYPE_TEMPLATE_INFO (type)
788 && context && CLASS_TYPE_P (context)
789 && CLASSTYPE_TEMPLATE_INFO (context))
791 /* This is for an explicit specialization of member class
792 template according to [temp.expl.spec/18]:
794 template <> template <class U> class C<int>::D;
796 The context `C<int>' must be an implicit instantiation.
797 Otherwise this is just a member class template declared
798 earlier like:
800 template <> class C<int> { template <class U> class D; };
801 template <> template <class U> class C<int>::D;
803 In the first case, `C<int>::D' is a specialization of `C<T>::D'
804 while in the second case, `C<int>::D' is a primary template
805 and `C<T>::D' may not exist. */
807 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
808 && !COMPLETE_TYPE_P (type))
810 tree t;
812 if (current_namespace
813 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
815 permerror (input_location, "specializing %q#T in different namespace", type);
816 permerror (input_location, " from definition of %q+#D",
817 CLASSTYPE_TI_TEMPLATE (type));
820 /* Check for invalid specialization after instantiation:
822 template <> template <> class C<int>::D<int>;
823 template <> template <class U> class C<int>::D; */
825 for (t = DECL_TEMPLATE_INSTANTIATIONS
826 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
827 t; t = TREE_CHAIN (t))
828 if (TREE_VALUE (t) != type
829 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
830 error ("specialization %qT after instantiation %qT",
831 type, TREE_VALUE (t));
833 /* Mark TYPE as a specialization. And as a result, we only
834 have one level of template argument for the innermost
835 class template. */
836 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
837 CLASSTYPE_TI_ARGS (type)
838 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
841 else if (processing_specialization)
843 error ("explicit specialization of non-template %qT", type);
844 return error_mark_node;
847 return type;
850 /* Returns nonzero if we can optimize the retrieval of specializations
851 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
852 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
854 static inline bool
855 optimize_specialization_lookup_p (tree tmpl)
857 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
858 && DECL_CLASS_SCOPE_P (tmpl)
859 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
860 parameter. */
861 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
862 /* The optimized lookup depends on the fact that the
863 template arguments for the member function template apply
864 purely to the containing class, which is not true if the
865 containing class is an explicit or partial
866 specialization. */
867 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
868 && !DECL_MEMBER_TEMPLATE_P (tmpl)
869 && !DECL_CONV_FN_P (tmpl)
870 /* It is possible to have a template that is not a member
871 template and is not a member of a template class:
873 template <typename T>
874 struct S { friend A::f(); };
876 Here, the friend function is a template, but the context does
877 not have template information. The optimized lookup relies
878 on having ARGS be the template arguments for both the class
879 and the function template. */
880 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
883 /* Retrieve the specialization (in the sense of [temp.spec] - a
884 specialization is either an instantiation or an explicit
885 specialization) of TMPL for the given template ARGS. If there is
886 no such specialization, return NULL_TREE. The ARGS are a vector of
887 arguments, or a vector of vectors of arguments, in the case of
888 templates with more than one level of parameters.
890 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
891 then we search for a partial specialization matching ARGS. This
892 parameter is ignored if TMPL is not a class template. */
894 static tree
895 retrieve_specialization (tree tmpl, tree args,
896 bool class_specializations_p)
898 if (args == error_mark_node)
899 return NULL_TREE;
901 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
903 /* There should be as many levels of arguments as there are
904 levels of parameters. */
905 gcc_assert (TMPL_ARGS_DEPTH (args)
906 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
908 if (optimize_specialization_lookup_p (tmpl))
910 tree class_template;
911 tree class_specialization;
912 VEC(tree,gc) *methods;
913 tree fns;
914 int idx;
916 /* The template arguments actually apply to the containing
917 class. Find the class specialization with those
918 arguments. */
919 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
920 class_specialization
921 = retrieve_specialization (class_template, args,
922 /*class_specializations_p=*/false);
923 if (!class_specialization)
924 return NULL_TREE;
925 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
926 for the specialization. */
927 idx = class_method_index_for_fn (class_specialization, tmpl);
928 if (idx == -1)
929 return NULL_TREE;
930 /* Iterate through the methods with the indicated name, looking
931 for the one that has an instance of TMPL. */
932 methods = CLASSTYPE_METHOD_VEC (class_specialization);
933 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
935 tree fn = OVL_CURRENT (fns);
936 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
937 return fn;
939 return NULL_TREE;
941 else
943 tree *sp;
944 tree *head;
946 /* Class templates store their instantiations on the
947 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
948 DECL_TEMPLATE_SPECIALIZATIONS list. */
949 if (!class_specializations_p
950 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL
951 && TAGGED_TYPE_P (TREE_TYPE (tmpl)))
952 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
953 else
954 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
955 head = sp;
956 /* Iterate through the list until we find a matching template. */
957 while (*sp != NULL_TREE)
959 tree spec = *sp;
961 if (comp_template_args (TREE_PURPOSE (spec), args))
963 /* Use the move-to-front heuristic to speed up future
964 searches. */
965 if (spec != *head)
967 *sp = TREE_CHAIN (*sp);
968 TREE_CHAIN (spec) = *head;
969 *head = spec;
971 return TREE_VALUE (spec);
973 sp = &TREE_CHAIN (spec);
977 return NULL_TREE;
980 /* Like retrieve_specialization, but for local declarations. */
982 static tree
983 retrieve_local_specialization (tree tmpl)
985 tree spec;
987 if (local_specializations == NULL)
988 return NULL_TREE;
990 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
991 htab_hash_pointer (tmpl));
992 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
995 /* Returns nonzero iff DECL is a specialization of TMPL. */
998 is_specialization_of (tree decl, tree tmpl)
1000 tree t;
1002 if (TREE_CODE (decl) == FUNCTION_DECL)
1004 for (t = decl;
1005 t != NULL_TREE;
1006 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1007 if (t == tmpl)
1008 return 1;
1010 else
1012 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1014 for (t = TREE_TYPE (decl);
1015 t != NULL_TREE;
1016 t = CLASSTYPE_USE_TEMPLATE (t)
1017 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1018 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1019 return 1;
1022 return 0;
1025 /* Returns nonzero iff DECL is a specialization of friend declaration
1026 FRIEND_DECL according to [temp.friend]. */
1028 bool
1029 is_specialization_of_friend (tree decl, tree friend_decl)
1031 bool need_template = true;
1032 int template_depth;
1034 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1035 || TREE_CODE (decl) == TYPE_DECL);
1037 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1038 of a template class, we want to check if DECL is a specialization
1039 if this. */
1040 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1041 && DECL_TEMPLATE_INFO (friend_decl)
1042 && !DECL_USE_TEMPLATE (friend_decl))
1044 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1045 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1046 need_template = false;
1048 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1049 && !PRIMARY_TEMPLATE_P (friend_decl))
1050 need_template = false;
1052 /* There is nothing to do if this is not a template friend. */
1053 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1054 return false;
1056 if (is_specialization_of (decl, friend_decl))
1057 return true;
1059 /* [temp.friend/6]
1060 A member of a class template may be declared to be a friend of a
1061 non-template class. In this case, the corresponding member of
1062 every specialization of the class template is a friend of the
1063 class granting friendship.
1065 For example, given a template friend declaration
1067 template <class T> friend void A<T>::f();
1069 the member function below is considered a friend
1071 template <> struct A<int> {
1072 void f();
1075 For this type of template friend, TEMPLATE_DEPTH below will be
1076 nonzero. To determine if DECL is a friend of FRIEND, we first
1077 check if the enclosing class is a specialization of another. */
1079 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1080 if (template_depth
1081 && DECL_CLASS_SCOPE_P (decl)
1082 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1083 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1085 /* Next, we check the members themselves. In order to handle
1086 a few tricky cases, such as when FRIEND_DECL's are
1088 template <class T> friend void A<T>::g(T t);
1089 template <class T> template <T t> friend void A<T>::h();
1091 and DECL's are
1093 void A<int>::g(int);
1094 template <int> void A<int>::h();
1096 we need to figure out ARGS, the template arguments from
1097 the context of DECL. This is required for template substitution
1098 of `T' in the function parameter of `g' and template parameter
1099 of `h' in the above examples. Here ARGS corresponds to `int'. */
1101 tree context = DECL_CONTEXT (decl);
1102 tree args = NULL_TREE;
1103 int current_depth = 0;
1105 while (current_depth < template_depth)
1107 if (CLASSTYPE_TEMPLATE_INFO (context))
1109 if (current_depth == 0)
1110 args = TYPE_TI_ARGS (context);
1111 else
1112 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1113 current_depth++;
1115 context = TYPE_CONTEXT (context);
1118 if (TREE_CODE (decl) == FUNCTION_DECL)
1120 bool is_template;
1121 tree friend_type;
1122 tree decl_type;
1123 tree friend_args_type;
1124 tree decl_args_type;
1126 /* Make sure that both DECL and FRIEND_DECL are templates or
1127 non-templates. */
1128 is_template = DECL_TEMPLATE_INFO (decl)
1129 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1130 if (need_template ^ is_template)
1131 return false;
1132 else if (is_template)
1134 /* If both are templates, check template parameter list. */
1135 tree friend_parms
1136 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1137 args, tf_none);
1138 if (!comp_template_parms
1139 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1140 friend_parms))
1141 return false;
1143 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1145 else
1146 decl_type = TREE_TYPE (decl);
1148 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1149 tf_none, NULL_TREE);
1150 if (friend_type == error_mark_node)
1151 return false;
1153 /* Check if return types match. */
1154 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1155 return false;
1157 /* Check if function parameter types match, ignoring the
1158 `this' parameter. */
1159 friend_args_type = TYPE_ARG_TYPES (friend_type);
1160 decl_args_type = TYPE_ARG_TYPES (decl_type);
1161 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1162 friend_args_type = TREE_CHAIN (friend_args_type);
1163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1164 decl_args_type = TREE_CHAIN (decl_args_type);
1166 return compparms (decl_args_type, friend_args_type);
1168 else
1170 /* DECL is a TYPE_DECL */
1171 bool is_template;
1172 tree decl_type = TREE_TYPE (decl);
1174 /* Make sure that both DECL and FRIEND_DECL are templates or
1175 non-templates. */
1176 is_template
1177 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1178 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1180 if (need_template ^ is_template)
1181 return false;
1182 else if (is_template)
1184 tree friend_parms;
1185 /* If both are templates, check the name of the two
1186 TEMPLATE_DECL's first because is_friend didn't. */
1187 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1188 != DECL_NAME (friend_decl))
1189 return false;
1191 /* Now check template parameter list. */
1192 friend_parms
1193 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1194 args, tf_none);
1195 return comp_template_parms
1196 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1197 friend_parms);
1199 else
1200 return (DECL_NAME (decl)
1201 == DECL_NAME (friend_decl));
1204 return false;
1207 /* Register the specialization SPEC as a specialization of TMPL with
1208 the indicated ARGS. IS_FRIEND indicates whether the specialization
1209 is actually just a friend declaration. Returns SPEC, or an
1210 equivalent prior declaration, if available. */
1212 static tree
1213 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1215 tree fn;
1217 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1219 if (TREE_CODE (spec) == FUNCTION_DECL
1220 && uses_template_parms (DECL_TI_ARGS (spec)))
1221 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1222 register it; we want the corresponding TEMPLATE_DECL instead.
1223 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1224 the more obvious `uses_template_parms (spec)' to avoid problems
1225 with default function arguments. In particular, given
1226 something like this:
1228 template <class T> void f(T t1, T t = T())
1230 the default argument expression is not substituted for in an
1231 instantiation unless and until it is actually needed. */
1232 return spec;
1234 fn = retrieve_specialization (tmpl, args,
1235 /*class_specializations_p=*/false);
1236 /* We can sometimes try to re-register a specialization that we've
1237 already got. In particular, regenerate_decl_from_template calls
1238 duplicate_decls which will update the specialization list. But,
1239 we'll still get called again here anyhow. It's more convenient
1240 to simply allow this than to try to prevent it. */
1241 if (fn == spec)
1242 return spec;
1243 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1245 if (DECL_TEMPLATE_INSTANTIATION (fn))
1247 if (TREE_USED (fn)
1248 || DECL_EXPLICIT_INSTANTIATION (fn))
1250 error ("specialization of %qD after instantiation",
1251 fn);
1252 return error_mark_node;
1254 else
1256 tree clone;
1257 /* This situation should occur only if the first
1258 specialization is an implicit instantiation, the
1259 second is an explicit specialization, and the
1260 implicit instantiation has not yet been used. That
1261 situation can occur if we have implicitly
1262 instantiated a member function and then specialized
1263 it later.
1265 We can also wind up here if a friend declaration that
1266 looked like an instantiation turns out to be a
1267 specialization:
1269 template <class T> void foo(T);
1270 class S { friend void foo<>(int) };
1271 template <> void foo(int);
1273 We transform the existing DECL in place so that any
1274 pointers to it become pointers to the updated
1275 declaration.
1277 If there was a definition for the template, but not
1278 for the specialization, we want this to look as if
1279 there were no definition, and vice versa. */
1280 DECL_INITIAL (fn) = NULL_TREE;
1281 duplicate_decls (spec, fn, is_friend);
1282 /* The call to duplicate_decls will have applied
1283 [temp.expl.spec]:
1285 An explicit specialization of a function template
1286 is inline only if it is explicitly declared to be,
1287 and independently of whether its function template
1290 to the primary function; now copy the inline bits to
1291 the various clones. */
1292 FOR_EACH_CLONE (clone, fn)
1293 DECL_DECLARED_INLINE_P (clone)
1294 = DECL_DECLARED_INLINE_P (fn);
1295 check_specialization_namespace (fn);
1297 return fn;
1300 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1302 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1303 /* Dup decl failed, but this is a new definition. Set the
1304 line number so any errors match this new
1305 definition. */
1306 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1308 return fn;
1312 /* A specialization must be declared in the same namespace as the
1313 template it is specializing. */
1314 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1315 && !check_specialization_namespace (tmpl))
1316 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1318 if (!optimize_specialization_lookup_p (tmpl))
1319 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1320 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1322 return spec;
1325 /* Unregister the specialization SPEC as a specialization of TMPL.
1326 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1327 if the SPEC was listed as a specialization of TMPL. */
1329 bool
1330 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1332 tree* s;
1334 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1335 *s != NULL_TREE;
1336 s = &TREE_CHAIN (*s))
1337 if (TREE_VALUE (*s) == spec)
1339 if (!new_spec)
1340 *s = TREE_CHAIN (*s);
1341 else
1342 TREE_VALUE (*s) = new_spec;
1343 return 1;
1346 return 0;
1349 /* Compare an entry in the local specializations hash table P1 (which
1350 is really a pointer to a TREE_LIST) with P2 (which is really a
1351 DECL). */
1353 static int
1354 eq_local_specializations (const void *p1, const void *p2)
1356 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1359 /* Hash P1, an entry in the local specializations table. */
1361 static hashval_t
1362 hash_local_specialization (const void* p1)
1364 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1367 /* Like register_specialization, but for local declarations. We are
1368 registering SPEC, an instantiation of TMPL. */
1370 static void
1371 register_local_specialization (tree spec, tree tmpl)
1373 void **slot;
1375 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1376 htab_hash_pointer (tmpl), INSERT);
1377 *slot = build_tree_list (spec, tmpl);
1380 /* TYPE is a class type. Returns true if TYPE is an explicitly
1381 specialized class. */
1383 bool
1384 explicit_class_specialization_p (tree type)
1386 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1387 return false;
1388 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1391 /* Print the list of candidate FNS in an error message. */
1393 void
1394 print_candidates (tree fns)
1396 tree fn;
1398 const char *str = "candidates are:";
1400 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1402 tree f;
1404 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1405 error ("%s %+#D", str, OVL_CURRENT (f));
1406 str = " ";
1410 /* Returns the template (one of the functions given by TEMPLATE_ID)
1411 which can be specialized to match the indicated DECL with the
1412 explicit template args given in TEMPLATE_ID. The DECL may be
1413 NULL_TREE if none is available. In that case, the functions in
1414 TEMPLATE_ID are non-members.
1416 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1417 specialization of a member template.
1419 The TEMPLATE_COUNT is the number of references to qualifying
1420 template classes that appeared in the name of the function. See
1421 check_explicit_specialization for a more accurate description.
1423 TSK indicates what kind of template declaration (if any) is being
1424 declared. TSK_TEMPLATE indicates that the declaration given by
1425 DECL, though a FUNCTION_DECL, has template parameters, and is
1426 therefore a template function.
1428 The template args (those explicitly specified and those deduced)
1429 are output in a newly created vector *TARGS_OUT.
1431 If it is impossible to determine the result, an error message is
1432 issued. The error_mark_node is returned to indicate failure. */
1434 static tree
1435 determine_specialization (tree template_id,
1436 tree decl,
1437 tree* targs_out,
1438 int need_member_template,
1439 int template_count,
1440 tmpl_spec_kind tsk)
1442 tree fns;
1443 tree targs;
1444 tree explicit_targs;
1445 tree candidates = NULL_TREE;
1446 /* A TREE_LIST of templates of which DECL may be a specialization.
1447 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1448 corresponding TREE_PURPOSE is the set of template arguments that,
1449 when used to instantiate the template, would produce a function
1450 with the signature of DECL. */
1451 tree templates = NULL_TREE;
1452 int header_count;
1453 struct cp_binding_level *b;
1455 *targs_out = NULL_TREE;
1457 if (template_id == error_mark_node || decl == error_mark_node)
1458 return error_mark_node;
1460 fns = TREE_OPERAND (template_id, 0);
1461 explicit_targs = TREE_OPERAND (template_id, 1);
1463 if (fns == error_mark_node)
1464 return error_mark_node;
1466 /* Check for baselinks. */
1467 if (BASELINK_P (fns))
1468 fns = BASELINK_FUNCTIONS (fns);
1470 if (!is_overloaded_fn (fns))
1472 error ("%qD is not a function template", fns);
1473 return error_mark_node;
1476 /* Count the number of template headers specified for this
1477 specialization. */
1478 header_count = 0;
1479 for (b = current_binding_level;
1480 b->kind == sk_template_parms;
1481 b = b->level_chain)
1482 ++header_count;
1484 for (; fns; fns = OVL_NEXT (fns))
1486 tree fn = OVL_CURRENT (fns);
1488 if (TREE_CODE (fn) == TEMPLATE_DECL)
1490 tree decl_arg_types;
1491 tree fn_arg_types;
1493 /* In case of explicit specialization, we need to check if
1494 the number of template headers appearing in the specialization
1495 is correct. This is usually done in check_explicit_specialization,
1496 but the check done there cannot be exhaustive when specializing
1497 member functions. Consider the following code:
1499 template <> void A<int>::f(int);
1500 template <> template <> void A<int>::f(int);
1502 Assuming that A<int> is not itself an explicit specialization
1503 already, the first line specializes "f" which is a non-template
1504 member function, whilst the second line specializes "f" which
1505 is a template member function. So both lines are syntactically
1506 correct, and check_explicit_specialization does not reject
1507 them.
1509 Here, we can do better, as we are matching the specialization
1510 against the declarations. We count the number of template
1511 headers, and we check if they match TEMPLATE_COUNT + 1
1512 (TEMPLATE_COUNT is the number of qualifying template classes,
1513 plus there must be another header for the member template
1514 itself).
1516 Notice that if header_count is zero, this is not a
1517 specialization but rather a template instantiation, so there
1518 is no check we can perform here. */
1519 if (header_count && header_count != template_count + 1)
1520 continue;
1522 /* Check that the number of template arguments at the
1523 innermost level for DECL is the same as for FN. */
1524 if (current_binding_level->kind == sk_template_parms
1525 && !current_binding_level->explicit_spec_p
1526 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1527 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1528 (current_template_parms))))
1529 continue;
1531 /* DECL might be a specialization of FN. */
1532 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1533 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1535 /* For a non-static member function, we need to make sure
1536 that the const qualification is the same. Since
1537 get_bindings does not try to merge the "this" parameter,
1538 we must do the comparison explicitly. */
1539 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1540 && !same_type_p (TREE_VALUE (fn_arg_types),
1541 TREE_VALUE (decl_arg_types)))
1542 continue;
1544 /* Skip the "this" parameter and, for constructors of
1545 classes with virtual bases, the VTT parameter. A
1546 full specialization of a constructor will have a VTT
1547 parameter, but a template never will. */
1548 decl_arg_types
1549 = skip_artificial_parms_for (decl, decl_arg_types);
1550 fn_arg_types
1551 = skip_artificial_parms_for (fn, fn_arg_types);
1553 /* Check that the number of function parameters matches.
1554 For example,
1555 template <class T> void f(int i = 0);
1556 template <> void f<int>();
1557 The specialization f<int> is invalid but is not caught
1558 by get_bindings below. */
1559 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1560 continue;
1562 /* Function templates cannot be specializations; there are
1563 no partial specializations of functions. Therefore, if
1564 the type of DECL does not match FN, there is no
1565 match. */
1566 if (tsk == tsk_template)
1568 if (compparms (fn_arg_types, decl_arg_types))
1569 candidates = tree_cons (NULL_TREE, fn, candidates);
1570 continue;
1573 /* See whether this function might be a specialization of this
1574 template. */
1575 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1577 if (!targs)
1578 /* We cannot deduce template arguments that when used to
1579 specialize TMPL will produce DECL. */
1580 continue;
1582 /* Save this template, and the arguments deduced. */
1583 templates = tree_cons (targs, fn, templates);
1585 else if (need_member_template)
1586 /* FN is an ordinary member function, and we need a
1587 specialization of a member template. */
1589 else if (TREE_CODE (fn) != FUNCTION_DECL)
1590 /* We can get IDENTIFIER_NODEs here in certain erroneous
1591 cases. */
1593 else if (!DECL_FUNCTION_MEMBER_P (fn))
1594 /* This is just an ordinary non-member function. Nothing can
1595 be a specialization of that. */
1597 else if (DECL_ARTIFICIAL (fn))
1598 /* Cannot specialize functions that are created implicitly. */
1600 else
1602 tree decl_arg_types;
1604 /* This is an ordinary member function. However, since
1605 we're here, we can assume it's enclosing class is a
1606 template class. For example,
1608 template <typename T> struct S { void f(); };
1609 template <> void S<int>::f() {}
1611 Here, S<int>::f is a non-template, but S<int> is a
1612 template class. If FN has the same type as DECL, we
1613 might be in business. */
1615 if (!DECL_TEMPLATE_INFO (fn))
1616 /* Its enclosing class is an explicit specialization
1617 of a template class. This is not a candidate. */
1618 continue;
1620 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1621 TREE_TYPE (TREE_TYPE (fn))))
1622 /* The return types differ. */
1623 continue;
1625 /* Adjust the type of DECL in case FN is a static member. */
1626 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1627 if (DECL_STATIC_FUNCTION_P (fn)
1628 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1629 decl_arg_types = TREE_CHAIN (decl_arg_types);
1631 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1632 decl_arg_types))
1633 /* They match! */
1634 candidates = tree_cons (NULL_TREE, fn, candidates);
1638 if (templates && TREE_CHAIN (templates))
1640 /* We have:
1642 [temp.expl.spec]
1644 It is possible for a specialization with a given function
1645 signature to be instantiated from more than one function
1646 template. In such cases, explicit specification of the
1647 template arguments must be used to uniquely identify the
1648 function template specialization being specialized.
1650 Note that here, there's no suggestion that we're supposed to
1651 determine which of the candidate templates is most
1652 specialized. However, we, also have:
1654 [temp.func.order]
1656 Partial ordering of overloaded function template
1657 declarations is used in the following contexts to select
1658 the function template to which a function template
1659 specialization refers:
1661 -- when an explicit specialization refers to a function
1662 template.
1664 So, we do use the partial ordering rules, at least for now.
1665 This extension can only serve to make invalid programs valid,
1666 so it's safe. And, there is strong anecdotal evidence that
1667 the committee intended the partial ordering rules to apply;
1668 the EDG front end has that behavior, and John Spicer claims
1669 that the committee simply forgot to delete the wording in
1670 [temp.expl.spec]. */
1671 tree tmpl = most_specialized_instantiation (templates);
1672 if (tmpl != error_mark_node)
1674 templates = tmpl;
1675 TREE_CHAIN (templates) = NULL_TREE;
1679 if (templates == NULL_TREE && candidates == NULL_TREE)
1681 error ("template-id %qD for %q+D does not match any template "
1682 "declaration", template_id, decl);
1683 return error_mark_node;
1685 else if ((templates && TREE_CHAIN (templates))
1686 || (candidates && TREE_CHAIN (candidates))
1687 || (templates && candidates))
1689 error ("ambiguous template specialization %qD for %q+D",
1690 template_id, decl);
1691 chainon (candidates, templates);
1692 print_candidates (candidates);
1693 return error_mark_node;
1696 /* We have one, and exactly one, match. */
1697 if (candidates)
1699 tree fn = TREE_VALUE (candidates);
1700 /* DECL is a re-declaration of a template function. */
1701 if (TREE_CODE (fn) == TEMPLATE_DECL)
1702 return fn;
1703 /* It was a specialization of an ordinary member function in a
1704 template class. */
1705 *targs_out = copy_node (DECL_TI_ARGS (fn));
1706 return DECL_TI_TEMPLATE (fn);
1709 /* It was a specialization of a template. */
1710 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1711 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1713 *targs_out = copy_node (targs);
1714 SET_TMPL_ARGS_LEVEL (*targs_out,
1715 TMPL_ARGS_DEPTH (*targs_out),
1716 TREE_PURPOSE (templates));
1718 else
1719 *targs_out = TREE_PURPOSE (templates);
1720 return TREE_VALUE (templates);
1723 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1724 but with the default argument values filled in from those in the
1725 TMPL_TYPES. */
1727 static tree
1728 copy_default_args_to_explicit_spec_1 (tree spec_types,
1729 tree tmpl_types)
1731 tree new_spec_types;
1733 if (!spec_types)
1734 return NULL_TREE;
1736 if (spec_types == void_list_node)
1737 return void_list_node;
1739 /* Substitute into the rest of the list. */
1740 new_spec_types =
1741 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1742 TREE_CHAIN (tmpl_types));
1744 /* Add the default argument for this parameter. */
1745 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1746 TREE_VALUE (spec_types),
1747 new_spec_types);
1750 /* DECL is an explicit specialization. Replicate default arguments
1751 from the template it specializes. (That way, code like:
1753 template <class T> void f(T = 3);
1754 template <> void f(double);
1755 void g () { f (); }
1757 works, as required.) An alternative approach would be to look up
1758 the correct default arguments at the call-site, but this approach
1759 is consistent with how implicit instantiations are handled. */
1761 static void
1762 copy_default_args_to_explicit_spec (tree decl)
1764 tree tmpl;
1765 tree spec_types;
1766 tree tmpl_types;
1767 tree new_spec_types;
1768 tree old_type;
1769 tree new_type;
1770 tree t;
1771 tree object_type = NULL_TREE;
1772 tree in_charge = NULL_TREE;
1773 tree vtt = NULL_TREE;
1775 /* See if there's anything we need to do. */
1776 tmpl = DECL_TI_TEMPLATE (decl);
1777 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1778 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1779 if (TREE_PURPOSE (t))
1780 break;
1781 if (!t)
1782 return;
1784 old_type = TREE_TYPE (decl);
1785 spec_types = TYPE_ARG_TYPES (old_type);
1787 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1789 /* Remove the this pointer, but remember the object's type for
1790 CV quals. */
1791 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1792 spec_types = TREE_CHAIN (spec_types);
1793 tmpl_types = TREE_CHAIN (tmpl_types);
1795 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1797 /* DECL may contain more parameters than TMPL due to the extra
1798 in-charge parameter in constructors and destructors. */
1799 in_charge = spec_types;
1800 spec_types = TREE_CHAIN (spec_types);
1802 if (DECL_HAS_VTT_PARM_P (decl))
1804 vtt = spec_types;
1805 spec_types = TREE_CHAIN (spec_types);
1809 /* Compute the merged default arguments. */
1810 new_spec_types =
1811 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1813 /* Compute the new FUNCTION_TYPE. */
1814 if (object_type)
1816 if (vtt)
1817 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1818 TREE_VALUE (vtt),
1819 new_spec_types);
1821 if (in_charge)
1822 /* Put the in-charge parameter back. */
1823 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1824 TREE_VALUE (in_charge),
1825 new_spec_types);
1827 new_type = build_method_type_directly (object_type,
1828 TREE_TYPE (old_type),
1829 new_spec_types);
1831 else
1832 new_type = build_function_type (TREE_TYPE (old_type),
1833 new_spec_types);
1834 new_type = cp_build_type_attribute_variant (new_type,
1835 TYPE_ATTRIBUTES (old_type));
1836 new_type = build_exception_variant (new_type,
1837 TYPE_RAISES_EXCEPTIONS (old_type));
1838 TREE_TYPE (decl) = new_type;
1841 /* Check to see if the function just declared, as indicated in
1842 DECLARATOR, and in DECL, is a specialization of a function
1843 template. We may also discover that the declaration is an explicit
1844 instantiation at this point.
1846 Returns DECL, or an equivalent declaration that should be used
1847 instead if all goes well. Issues an error message if something is
1848 amiss. Returns error_mark_node if the error is not easily
1849 recoverable.
1851 FLAGS is a bitmask consisting of the following flags:
1853 2: The function has a definition.
1854 4: The function is a friend.
1856 The TEMPLATE_COUNT is the number of references to qualifying
1857 template classes that appeared in the name of the function. For
1858 example, in
1860 template <class T> struct S { void f(); };
1861 void S<int>::f();
1863 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1864 classes are not counted in the TEMPLATE_COUNT, so that in
1866 template <class T> struct S {};
1867 template <> struct S<int> { void f(); }
1868 template <> void S<int>::f();
1870 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1871 invalid; there should be no template <>.)
1873 If the function is a specialization, it is marked as such via
1874 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1875 is set up correctly, and it is added to the list of specializations
1876 for that template. */
1878 tree
1879 check_explicit_specialization (tree declarator,
1880 tree decl,
1881 int template_count,
1882 int flags)
1884 int have_def = flags & 2;
1885 int is_friend = flags & 4;
1886 int specialization = 0;
1887 int explicit_instantiation = 0;
1888 int member_specialization = 0;
1889 tree ctype = DECL_CLASS_CONTEXT (decl);
1890 tree dname = DECL_NAME (decl);
1891 tmpl_spec_kind tsk;
1893 if (is_friend)
1895 if (!processing_specialization)
1896 tsk = tsk_none;
1897 else
1898 tsk = tsk_excessive_parms;
1900 else
1901 tsk = current_tmpl_spec_kind (template_count);
1903 switch (tsk)
1905 case tsk_none:
1906 if (processing_specialization)
1908 specialization = 1;
1909 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1911 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1913 if (is_friend)
1914 /* This could be something like:
1916 template <class T> void f(T);
1917 class S { friend void f<>(int); } */
1918 specialization = 1;
1919 else
1921 /* This case handles bogus declarations like template <>
1922 template <class T> void f<int>(); */
1924 error ("template-id %qD in declaration of primary template",
1925 declarator);
1926 return decl;
1929 break;
1931 case tsk_invalid_member_spec:
1932 /* The error has already been reported in
1933 check_specialization_scope. */
1934 return error_mark_node;
1936 case tsk_invalid_expl_inst:
1937 error ("template parameter list used in explicit instantiation");
1939 /* Fall through. */
1941 case tsk_expl_inst:
1942 if (have_def)
1943 error ("definition provided for explicit instantiation");
1945 explicit_instantiation = 1;
1946 break;
1948 case tsk_excessive_parms:
1949 case tsk_insufficient_parms:
1950 if (tsk == tsk_excessive_parms)
1951 error ("too many template parameter lists in declaration of %qD",
1952 decl);
1953 else if (template_header_count)
1954 error("too few template parameter lists in declaration of %qD", decl);
1955 else
1956 error("explicit specialization of %qD must be introduced by "
1957 "%<template <>%>", decl);
1959 /* Fall through. */
1960 case tsk_expl_spec:
1961 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1962 if (ctype)
1963 member_specialization = 1;
1964 else
1965 specialization = 1;
1966 break;
1968 case tsk_template:
1969 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1971 /* This case handles bogus declarations like template <>
1972 template <class T> void f<int>(); */
1974 if (uses_template_parms (declarator))
1975 error ("function template partial specialization %qD "
1976 "is not allowed", declarator);
1977 else
1978 error ("template-id %qD in declaration of primary template",
1979 declarator);
1980 return decl;
1983 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1984 /* This is a specialization of a member template, without
1985 specialization the containing class. Something like:
1987 template <class T> struct S {
1988 template <class U> void f (U);
1990 template <> template <class U> void S<int>::f(U) {}
1992 That's a specialization -- but of the entire template. */
1993 specialization = 1;
1994 break;
1996 default:
1997 gcc_unreachable ();
2000 if (specialization || member_specialization)
2002 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2003 for (; t; t = TREE_CHAIN (t))
2004 if (TREE_PURPOSE (t))
2006 permerror (input_location,
2007 "default argument specified in explicit specialization");
2008 break;
2012 if (specialization || member_specialization || explicit_instantiation)
2014 tree tmpl = NULL_TREE;
2015 tree targs = NULL_TREE;
2017 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2018 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2020 tree fns;
2022 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2023 if (ctype)
2024 fns = dname;
2025 else
2027 /* If there is no class context, the explicit instantiation
2028 must be at namespace scope. */
2029 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2031 /* Find the namespace binding, using the declaration
2032 context. */
2033 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2034 false, true);
2035 if (fns == error_mark_node || !is_overloaded_fn (fns))
2037 error ("%qD is not a template function", dname);
2038 fns = error_mark_node;
2040 else
2042 tree fn = OVL_CURRENT (fns);
2043 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2044 CP_DECL_CONTEXT (fn)))
2045 error ("%qD is not declared in %qD",
2046 decl, current_namespace);
2050 declarator = lookup_template_function (fns, NULL_TREE);
2053 if (declarator == error_mark_node)
2054 return error_mark_node;
2056 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2058 if (!explicit_instantiation)
2059 /* A specialization in class scope. This is invalid,
2060 but the error will already have been flagged by
2061 check_specialization_scope. */
2062 return error_mark_node;
2063 else
2065 /* It's not valid to write an explicit instantiation in
2066 class scope, e.g.:
2068 class C { template void f(); }
2070 This case is caught by the parser. However, on
2071 something like:
2073 template class C { void f(); };
2075 (which is invalid) we can get here. The error will be
2076 issued later. */
2080 return decl;
2082 else if (ctype != NULL_TREE
2083 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2084 IDENTIFIER_NODE))
2086 /* Find the list of functions in ctype that have the same
2087 name as the declared function. */
2088 tree name = TREE_OPERAND (declarator, 0);
2089 tree fns = NULL_TREE;
2090 int idx;
2092 if (constructor_name_p (name, ctype))
2094 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2096 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2097 : !CLASSTYPE_DESTRUCTORS (ctype))
2099 /* From [temp.expl.spec]:
2101 If such an explicit specialization for the member
2102 of a class template names an implicitly-declared
2103 special member function (clause _special_), the
2104 program is ill-formed.
2106 Similar language is found in [temp.explicit]. */
2107 error ("specialization of implicitly-declared special member function");
2108 return error_mark_node;
2111 name = is_constructor ? ctor_identifier : dtor_identifier;
2114 if (!DECL_CONV_FN_P (decl))
2116 idx = lookup_fnfields_1 (ctype, name);
2117 if (idx >= 0)
2118 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2120 else
2122 VEC(tree,gc) *methods;
2123 tree ovl;
2125 /* For a type-conversion operator, we cannot do a
2126 name-based lookup. We might be looking for `operator
2127 int' which will be a specialization of `operator T'.
2128 So, we find *all* the conversion operators, and then
2129 select from them. */
2130 fns = NULL_TREE;
2132 methods = CLASSTYPE_METHOD_VEC (ctype);
2133 if (methods)
2134 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2135 VEC_iterate (tree, methods, idx, ovl);
2136 ++idx)
2138 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2139 /* There are no more conversion functions. */
2140 break;
2142 /* Glue all these conversion functions together
2143 with those we already have. */
2144 for (; ovl; ovl = OVL_NEXT (ovl))
2145 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2149 if (fns == NULL_TREE)
2151 error ("no member function %qD declared in %qT", name, ctype);
2152 return error_mark_node;
2154 else
2155 TREE_OPERAND (declarator, 0) = fns;
2158 /* Figure out what exactly is being specialized at this point.
2159 Note that for an explicit instantiation, even one for a
2160 member function, we cannot tell apriori whether the
2161 instantiation is for a member template, or just a member
2162 function of a template class. Even if a member template is
2163 being instantiated, the member template arguments may be
2164 elided if they can be deduced from the rest of the
2165 declaration. */
2166 tmpl = determine_specialization (declarator, decl,
2167 &targs,
2168 member_specialization,
2169 template_count,
2170 tsk);
2172 if (!tmpl || tmpl == error_mark_node)
2173 /* We couldn't figure out what this declaration was
2174 specializing. */
2175 return error_mark_node;
2176 else
2178 tree gen_tmpl = most_general_template (tmpl);
2180 if (explicit_instantiation)
2182 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2183 is done by do_decl_instantiation later. */
2185 int arg_depth = TMPL_ARGS_DEPTH (targs);
2186 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2188 if (arg_depth > parm_depth)
2190 /* If TMPL is not the most general template (for
2191 example, if TMPL is a friend template that is
2192 injected into namespace scope), then there will
2193 be too many levels of TARGS. Remove some of them
2194 here. */
2195 int i;
2196 tree new_targs;
2198 new_targs = make_tree_vec (parm_depth);
2199 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2200 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2201 = TREE_VEC_ELT (targs, i);
2202 targs = new_targs;
2205 return instantiate_template (tmpl, targs, tf_error);
2208 /* If we thought that the DECL was a member function, but it
2209 turns out to be specializing a static member function,
2210 make DECL a static member function as well. */
2211 if (DECL_STATIC_FUNCTION_P (tmpl)
2212 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2213 revert_static_member_fn (decl);
2215 /* If this is a specialization of a member template of a
2216 template class, we want to return the TEMPLATE_DECL, not
2217 the specialization of it. */
2218 if (tsk == tsk_template)
2220 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2221 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2222 if (have_def)
2224 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2225 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2226 = DECL_SOURCE_LOCATION (decl);
2227 /* We want to use the argument list specified in the
2228 definition, not in the original declaration. */
2229 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2230 = DECL_ARGUMENTS (decl);
2232 return tmpl;
2235 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2236 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2238 /* Inherit default function arguments from the template
2239 DECL is specializing. */
2240 copy_default_args_to_explicit_spec (decl);
2242 /* This specialization has the same protection as the
2243 template it specializes. */
2244 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2245 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2247 /* 7.1.1-1 [dcl.stc]
2249 A storage-class-specifier shall not be specified in an
2250 explicit specialization...
2252 The parser rejects these, so unless action is taken here,
2253 explicit function specializations will always appear with
2254 global linkage.
2256 The action recommended by the C++ CWG in response to C++
2257 defect report 605 is to make the storage class and linkage
2258 of the explicit specialization match the templated function:
2260 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2262 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2264 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2265 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2267 /* This specialization has the same linkage and visibility as
2268 the function template it specializes. */
2269 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2270 if (! TREE_PUBLIC (decl))
2272 DECL_INTERFACE_KNOWN (decl) = 1;
2273 DECL_NOT_REALLY_EXTERN (decl) = 1;
2275 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2276 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2278 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2279 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2283 /* If DECL is a friend declaration, declared using an
2284 unqualified name, the namespace associated with DECL may
2285 have been set incorrectly. For example, in:
2287 template <typename T> void f(T);
2288 namespace N {
2289 struct S { friend void f<int>(int); }
2292 we will have set the DECL_CONTEXT for the friend
2293 declaration to N, rather than to the global namespace. */
2294 if (DECL_NAMESPACE_SCOPE_P (decl))
2295 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2297 if (is_friend && !have_def)
2298 /* This is not really a declaration of a specialization.
2299 It's just the name of an instantiation. But, it's not
2300 a request for an instantiation, either. */
2301 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2302 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2303 /* This is indeed a specialization. In case of constructors
2304 and destructors, we need in-charge and not-in-charge
2305 versions in V3 ABI. */
2306 clone_function_decl (decl, /*update_method_vec_p=*/0);
2308 /* Register this specialization so that we can find it
2309 again. */
2310 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2314 return decl;
2317 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2318 parameters. These are represented in the same format used for
2319 DECL_TEMPLATE_PARMS. */
2322 comp_template_parms (const_tree parms1, const_tree parms2)
2324 const_tree p1;
2325 const_tree p2;
2327 if (parms1 == parms2)
2328 return 1;
2330 for (p1 = parms1, p2 = parms2;
2331 p1 != NULL_TREE && p2 != NULL_TREE;
2332 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2334 tree t1 = TREE_VALUE (p1);
2335 tree t2 = TREE_VALUE (p2);
2336 int i;
2338 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2339 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2341 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2342 return 0;
2344 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2346 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2347 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2349 /* If either of the template parameters are invalid, assume
2350 they match for the sake of error recovery. */
2351 if (parm1 == error_mark_node || parm2 == error_mark_node)
2352 return 1;
2354 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2355 return 0;
2357 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2358 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2359 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2360 continue;
2361 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2362 return 0;
2366 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2367 /* One set of parameters has more parameters lists than the
2368 other. */
2369 return 0;
2371 return 1;
2374 /* Determine whether PARM is a parameter pack. */
2375 bool
2376 template_parameter_pack_p (const_tree parm)
2378 /* Determine if we have a non-type template parameter pack. */
2379 if (TREE_CODE (parm) == PARM_DECL)
2380 return (DECL_TEMPLATE_PARM_P (parm)
2381 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2383 /* If this is a list of template parameters, we could get a
2384 TYPE_DECL or a TEMPLATE_DECL. */
2385 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2386 parm = TREE_TYPE (parm);
2388 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2389 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2390 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2393 /* Determine whether ARGS describes a variadic template args list,
2394 i.e., one that is terminated by a template argument pack. */
2395 static bool
2396 template_args_variadic_p (tree args)
2398 int nargs;
2399 tree last_parm;
2401 if (args == NULL_TREE)
2402 return false;
2404 args = INNERMOST_TEMPLATE_ARGS (args);
2405 nargs = TREE_VEC_LENGTH (args);
2407 if (nargs == 0)
2408 return false;
2410 last_parm = TREE_VEC_ELT (args, nargs - 1);
2412 return ARGUMENT_PACK_P (last_parm);
2415 /* Generate a new name for the parameter pack name NAME (an
2416 IDENTIFIER_NODE) that incorporates its */
2417 static tree
2418 make_ith_pack_parameter_name (tree name, int i)
2420 /* Munge the name to include the parameter index. */
2421 char numbuf[128];
2422 char* newname;
2424 sprintf(numbuf, "%i", i);
2425 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2426 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2427 return get_identifier (newname);
2430 /* Structure used to track the progress of find_parameter_packs_r. */
2431 struct find_parameter_pack_data
2433 /* TREE_LIST that will contain all of the parameter packs found by
2434 the traversal. */
2435 tree* parameter_packs;
2437 /* Set of AST nodes that have been visited by the traversal. */
2438 struct pointer_set_t *visited;
2441 /* Identifies all of the argument packs that occur in a template
2442 argument and appends them to the TREE_LIST inside DATA, which is a
2443 find_parameter_pack_data structure. This is a subroutine of
2444 make_pack_expansion and uses_parameter_packs. */
2445 static tree
2446 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2448 tree t = *tp;
2449 struct find_parameter_pack_data* ppd =
2450 (struct find_parameter_pack_data*)data;
2451 bool parameter_pack_p = false;
2453 /* Identify whether this is a parameter pack or not. */
2454 switch (TREE_CODE (t))
2456 case TEMPLATE_PARM_INDEX:
2457 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2458 parameter_pack_p = true;
2459 break;
2461 case TEMPLATE_TYPE_PARM:
2462 case TEMPLATE_TEMPLATE_PARM:
2463 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2464 parameter_pack_p = true;
2465 break;
2467 case PARM_DECL:
2468 if (FUNCTION_PARAMETER_PACK_P (t))
2470 /* We don't want to walk into the type of a PARM_DECL,
2471 because we don't want to see the type parameter pack. */
2472 *walk_subtrees = 0;
2473 parameter_pack_p = true;
2475 break;
2477 default:
2478 /* Not a parameter pack. */
2479 break;
2482 if (parameter_pack_p)
2484 /* Add this parameter pack to the list. */
2485 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2488 if (TYPE_P (t))
2489 cp_walk_tree (&TYPE_CONTEXT (t),
2490 &find_parameter_packs_r, ppd, ppd->visited);
2492 /* This switch statement will return immediately if we don't find a
2493 parameter pack. */
2494 switch (TREE_CODE (t))
2496 case TEMPLATE_PARM_INDEX:
2497 return NULL_TREE;
2499 case BOUND_TEMPLATE_TEMPLATE_PARM:
2500 /* Check the template itself. */
2501 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2502 &find_parameter_packs_r, ppd, ppd->visited);
2503 /* Check the template arguments. */
2504 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2505 ppd->visited);
2506 *walk_subtrees = 0;
2507 return NULL_TREE;
2509 case TEMPLATE_TYPE_PARM:
2510 case TEMPLATE_TEMPLATE_PARM:
2511 return NULL_TREE;
2513 case PARM_DECL:
2514 return NULL_TREE;
2516 case RECORD_TYPE:
2517 if (TYPE_PTRMEMFUNC_P (t))
2518 return NULL_TREE;
2519 /* Fall through. */
2521 case UNION_TYPE:
2522 case ENUMERAL_TYPE:
2523 if (TYPE_TEMPLATE_INFO (t))
2524 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2525 &find_parameter_packs_r, ppd, ppd->visited);
2527 *walk_subtrees = 0;
2528 return NULL_TREE;
2530 case TEMPLATE_DECL:
2531 cp_walk_tree (&TREE_TYPE (t),
2532 &find_parameter_packs_r, ppd, ppd->visited);
2533 return NULL_TREE;
2535 case TYPENAME_TYPE:
2536 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2537 ppd, ppd->visited);
2538 *walk_subtrees = 0;
2539 return NULL_TREE;
2541 case TYPE_PACK_EXPANSION:
2542 case EXPR_PACK_EXPANSION:
2543 *walk_subtrees = 0;
2544 return NULL_TREE;
2546 case INTEGER_TYPE:
2547 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2548 ppd, ppd->visited);
2549 *walk_subtrees = 0;
2550 return NULL_TREE;
2552 case IDENTIFIER_NODE:
2553 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2554 ppd->visited);
2555 *walk_subtrees = 0;
2556 return NULL_TREE;
2558 default:
2559 return NULL_TREE;
2562 return NULL_TREE;
2565 /* Determines if the expression or type T uses any parameter packs. */
2566 bool
2567 uses_parameter_packs (tree t)
2569 tree parameter_packs = NULL_TREE;
2570 struct find_parameter_pack_data ppd;
2571 ppd.parameter_packs = &parameter_packs;
2572 ppd.visited = pointer_set_create ();
2573 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2574 pointer_set_destroy (ppd.visited);
2575 return parameter_packs != NULL_TREE;
2578 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2579 representation a base-class initializer into a parameter pack
2580 expansion. If all goes well, the resulting node will be an
2581 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2582 respectively. */
2583 tree
2584 make_pack_expansion (tree arg)
2586 tree result;
2587 tree parameter_packs = NULL_TREE;
2588 bool for_types = false;
2589 struct find_parameter_pack_data ppd;
2591 if (!arg || arg == error_mark_node)
2592 return arg;
2594 if (TREE_CODE (arg) == TREE_LIST)
2596 /* The only time we will see a TREE_LIST here is for a base
2597 class initializer. In this case, the TREE_PURPOSE will be a
2598 _TYPE node (representing the base class expansion we're
2599 initializing) and the TREE_VALUE will be a TREE_LIST
2600 containing the initialization arguments.
2602 The resulting expansion looks somewhat different from most
2603 expansions. Rather than returning just one _EXPANSION, we
2604 return a TREE_LIST whose TREE_PURPOSE is a
2605 TYPE_PACK_EXPANSION containing the bases that will be
2606 initialized. The TREE_VALUE will be identical to the
2607 original TREE_VALUE, which is a list of arguments that will
2608 be passed to each base. We do not introduce any new pack
2609 expansion nodes into the TREE_VALUE (although it is possible
2610 that some already exist), because the TREE_PURPOSE and
2611 TREE_VALUE all need to be expanded together with the same
2612 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2613 resulting TREE_PURPOSE will mention the parameter packs in
2614 both the bases and the arguments to the bases. */
2615 tree purpose;
2616 tree value;
2617 tree parameter_packs = NULL_TREE;
2619 /* Determine which parameter packs will be used by the base
2620 class expansion. */
2621 ppd.visited = pointer_set_create ();
2622 ppd.parameter_packs = &parameter_packs;
2623 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2624 &ppd, ppd.visited);
2626 if (parameter_packs == NULL_TREE)
2628 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2629 pointer_set_destroy (ppd.visited);
2630 return error_mark_node;
2633 if (TREE_VALUE (arg) != void_type_node)
2635 /* Collect the sets of parameter packs used in each of the
2636 initialization arguments. */
2637 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2639 /* Determine which parameter packs will be expanded in this
2640 argument. */
2641 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2642 &ppd, ppd.visited);
2646 pointer_set_destroy (ppd.visited);
2648 /* Create the pack expansion type for the base type. */
2649 purpose = make_node (TYPE_PACK_EXPANSION);
2650 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2651 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2653 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2654 they will rarely be compared to anything. */
2655 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2657 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2660 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2661 for_types = true;
2663 /* Build the PACK_EXPANSION_* node. */
2664 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2665 SET_PACK_EXPANSION_PATTERN (result, arg);
2666 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2668 /* Propagate type and const-expression information. */
2669 TREE_TYPE (result) = TREE_TYPE (arg);
2670 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2672 else
2673 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2674 they will rarely be compared to anything. */
2675 SET_TYPE_STRUCTURAL_EQUALITY (result);
2677 /* Determine which parameter packs will be expanded. */
2678 ppd.parameter_packs = &parameter_packs;
2679 ppd.visited = pointer_set_create ();
2680 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2681 pointer_set_destroy (ppd.visited);
2683 /* Make sure we found some parameter packs. */
2684 if (parameter_packs == NULL_TREE)
2686 if (TYPE_P (arg))
2687 error ("expansion pattern %<%T%> contains no argument packs", arg);
2688 else
2689 error ("expansion pattern %<%E%> contains no argument packs", arg);
2690 return error_mark_node;
2692 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2694 return result;
2697 /* Checks T for any "bare" parameter packs, which have not yet been
2698 expanded, and issues an error if any are found. This operation can
2699 only be done on full expressions or types (e.g., an expression
2700 statement, "if" condition, etc.), because we could have expressions like:
2702 foo(f(g(h(args)))...)
2704 where "args" is a parameter pack. check_for_bare_parameter_packs
2705 should not be called for the subexpressions args, h(args),
2706 g(h(args)), or f(g(h(args))), because we would produce erroneous
2707 error messages.
2709 Returns TRUE and emits an error if there were bare parameter packs,
2710 returns FALSE otherwise. */
2711 bool
2712 check_for_bare_parameter_packs (tree t)
2714 tree parameter_packs = NULL_TREE;
2715 struct find_parameter_pack_data ppd;
2717 if (!processing_template_decl || !t || t == error_mark_node)
2718 return false;
2720 if (TREE_CODE (t) == TYPE_DECL)
2721 t = TREE_TYPE (t);
2723 ppd.parameter_packs = &parameter_packs;
2724 ppd.visited = pointer_set_create ();
2725 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2726 pointer_set_destroy (ppd.visited);
2728 if (parameter_packs)
2730 error ("parameter packs not expanded with %<...%>:");
2731 while (parameter_packs)
2733 tree pack = TREE_VALUE (parameter_packs);
2734 tree name = NULL_TREE;
2736 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2737 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2738 name = TYPE_NAME (pack);
2739 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2740 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2741 else
2742 name = DECL_NAME (pack);
2744 if (name)
2745 inform (input_location, " %qD", name);
2746 else
2747 inform (input_location, " <anonymous>");
2749 parameter_packs = TREE_CHAIN (parameter_packs);
2752 return true;
2755 return false;
2758 /* Expand any parameter packs that occur in the template arguments in
2759 ARGS. */
2760 tree
2761 expand_template_argument_pack (tree args)
2763 tree result_args = NULL_TREE;
2764 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2765 int num_result_args = -1;
2767 /* First, determine if we need to expand anything, and the number of
2768 slots we'll need. */
2769 for (in_arg = 0; in_arg < nargs; ++in_arg)
2771 tree arg = TREE_VEC_ELT (args, in_arg);
2772 if (ARGUMENT_PACK_P (arg))
2774 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2775 if (num_result_args < 0)
2776 num_result_args = in_arg + num_packed;
2777 else
2778 num_result_args += num_packed;
2780 else
2782 if (num_result_args >= 0)
2783 num_result_args++;
2787 /* If no expansion is necessary, we're done. */
2788 if (num_result_args < 0)
2789 return args;
2791 /* Expand arguments. */
2792 result_args = make_tree_vec (num_result_args);
2793 for (in_arg = 0; in_arg < nargs; ++in_arg)
2795 tree arg = TREE_VEC_ELT (args, in_arg);
2796 if (ARGUMENT_PACK_P (arg))
2798 tree packed = ARGUMENT_PACK_ARGS (arg);
2799 int i, num_packed = TREE_VEC_LENGTH (packed);
2800 for (i = 0; i < num_packed; ++i, ++out_arg)
2801 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2803 else
2805 TREE_VEC_ELT (result_args, out_arg) = arg;
2806 ++out_arg;
2810 return result_args;
2813 /* Checks if DECL shadows a template parameter.
2815 [temp.local]: A template-parameter shall not be redeclared within its
2816 scope (including nested scopes).
2818 Emits an error and returns TRUE if the DECL shadows a parameter,
2819 returns FALSE otherwise. */
2821 bool
2822 check_template_shadow (tree decl)
2824 tree olddecl;
2826 /* If we're not in a template, we can't possibly shadow a template
2827 parameter. */
2828 if (!current_template_parms)
2829 return true;
2831 /* Figure out what we're shadowing. */
2832 if (TREE_CODE (decl) == OVERLOAD)
2833 decl = OVL_CURRENT (decl);
2834 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2836 /* If there's no previous binding for this name, we're not shadowing
2837 anything, let alone a template parameter. */
2838 if (!olddecl)
2839 return true;
2841 /* If we're not shadowing a template parameter, we're done. Note
2842 that OLDDECL might be an OVERLOAD (or perhaps even an
2843 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2844 node. */
2845 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2846 return true;
2848 /* We check for decl != olddecl to avoid bogus errors for using a
2849 name inside a class. We check TPFI to avoid duplicate errors for
2850 inline member templates. */
2851 if (decl == olddecl
2852 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2853 return true;
2855 error ("declaration of %q+#D", decl);
2856 error (" shadows template parm %q+#D", olddecl);
2857 return false;
2860 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2861 ORIG_LEVEL, DECL, and TYPE. */
2863 static tree
2864 build_template_parm_index (int index,
2865 int level,
2866 int orig_level,
2867 tree decl,
2868 tree type)
2870 tree t = make_node (TEMPLATE_PARM_INDEX);
2871 TEMPLATE_PARM_IDX (t) = index;
2872 TEMPLATE_PARM_LEVEL (t) = level;
2873 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2874 TEMPLATE_PARM_DECL (t) = decl;
2875 TREE_TYPE (t) = type;
2876 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2877 TREE_READONLY (t) = TREE_READONLY (decl);
2879 return t;
2882 /* Find the canonical type parameter for the given template type
2883 parameter. Returns the canonical type parameter, which may be TYPE
2884 if no such parameter existed. */
2885 static tree
2886 canonical_type_parameter (tree type)
2888 tree list;
2889 int idx = TEMPLATE_TYPE_IDX (type);
2890 if (!canonical_template_parms)
2891 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2893 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2894 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2896 list = VEC_index (tree, canonical_template_parms, idx);
2897 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2898 list = TREE_CHAIN (list);
2900 if (list)
2901 return TREE_VALUE (list);
2902 else
2904 VEC_replace(tree, canonical_template_parms, idx,
2905 tree_cons (NULL_TREE, type,
2906 VEC_index (tree, canonical_template_parms, idx)));
2907 return type;
2911 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2912 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2913 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2914 new one is created. */
2916 static tree
2917 reduce_template_parm_level (tree index, tree type, int levels, tree args,
2918 tsubst_flags_t complain)
2920 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2921 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2922 != TEMPLATE_PARM_LEVEL (index) - levels))
2924 tree orig_decl = TEMPLATE_PARM_DECL (index);
2925 tree decl, t;
2927 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2928 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2929 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2930 DECL_ARTIFICIAL (decl) = 1;
2931 SET_DECL_TEMPLATE_PARM_P (decl);
2933 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2934 TEMPLATE_PARM_LEVEL (index) - levels,
2935 TEMPLATE_PARM_ORIG_LEVEL (index),
2936 decl, type);
2937 TEMPLATE_PARM_DESCENDANTS (index) = t;
2938 TEMPLATE_PARM_PARAMETER_PACK (t)
2939 = TEMPLATE_PARM_PARAMETER_PACK (index);
2941 /* Template template parameters need this. */
2942 if (TREE_CODE (decl) == TEMPLATE_DECL)
2943 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
2944 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
2945 args, complain);
2948 return TEMPLATE_PARM_DESCENDANTS (index);
2951 /* Process information from new template parameter PARM and append it to the
2952 LIST being built. This new parameter is a non-type parameter iff
2953 IS_NON_TYPE is true. This new parameter is a parameter
2954 pack iff IS_PARAMETER_PACK is true. */
2956 tree
2957 process_template_parm (tree list, tree parm, bool is_non_type,
2958 bool is_parameter_pack)
2960 tree decl = 0;
2961 tree defval;
2962 tree err_parm_list;
2963 int idx = 0;
2965 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2966 defval = TREE_PURPOSE (parm);
2968 if (list)
2970 tree p = tree_last (list);
2972 if (p && TREE_VALUE (p) != error_mark_node)
2974 p = TREE_VALUE (p);
2975 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2976 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2977 else
2978 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2981 ++idx;
2983 else
2984 idx = 0;
2986 if (is_non_type)
2988 parm = TREE_VALUE (parm);
2990 SET_DECL_TEMPLATE_PARM_P (parm);
2992 if (TREE_TYPE (parm) == error_mark_node)
2994 err_parm_list = build_tree_list (defval, parm);
2995 TREE_VALUE (err_parm_list) = error_mark_node;
2996 return chainon (list, err_parm_list);
2998 else
3000 /* [temp.param]
3002 The top-level cv-qualifiers on the template-parameter are
3003 ignored when determining its type. */
3004 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3005 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3007 err_parm_list = build_tree_list (defval, parm);
3008 TREE_VALUE (err_parm_list) = error_mark_node;
3009 return chainon (list, err_parm_list);
3012 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3014 /* This template parameter is not a parameter pack, but it
3015 should be. Complain about "bare" parameter packs. */
3016 check_for_bare_parameter_packs (TREE_TYPE (parm));
3018 /* Recover by calling this a parameter pack. */
3019 is_parameter_pack = true;
3023 /* A template parameter is not modifiable. */
3024 TREE_CONSTANT (parm) = 1;
3025 TREE_READONLY (parm) = 1;
3026 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3027 TREE_CONSTANT (decl) = 1;
3028 TREE_READONLY (decl) = 1;
3029 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3030 = build_template_parm_index (idx, processing_template_decl,
3031 processing_template_decl,
3032 decl, TREE_TYPE (parm));
3034 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3035 = is_parameter_pack;
3037 else
3039 tree t;
3040 parm = TREE_VALUE (TREE_VALUE (parm));
3042 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3044 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3045 /* This is for distinguishing between real templates and template
3046 template parameters */
3047 TREE_TYPE (parm) = t;
3048 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3049 decl = parm;
3051 else
3053 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3054 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3055 decl = build_decl (TYPE_DECL, parm, t);
3058 TYPE_NAME (t) = decl;
3059 TYPE_STUB_DECL (t) = decl;
3060 parm = decl;
3061 TEMPLATE_TYPE_PARM_INDEX (t)
3062 = build_template_parm_index (idx, processing_template_decl,
3063 processing_template_decl,
3064 decl, TREE_TYPE (parm));
3065 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3066 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3068 DECL_ARTIFICIAL (decl) = 1;
3069 SET_DECL_TEMPLATE_PARM_P (decl);
3070 pushdecl (decl);
3071 parm = build_tree_list (defval, parm);
3072 return chainon (list, parm);
3075 /* The end of a template parameter list has been reached. Process the
3076 tree list into a parameter vector, converting each parameter into a more
3077 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3078 as PARM_DECLs. */
3080 tree
3081 end_template_parm_list (tree parms)
3083 int nparms;
3084 tree parm, next;
3085 tree saved_parmlist = make_tree_vec (list_length (parms));
3087 current_template_parms
3088 = tree_cons (size_int (processing_template_decl),
3089 saved_parmlist, current_template_parms);
3091 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3093 next = TREE_CHAIN (parm);
3094 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3095 TREE_CHAIN (parm) = NULL_TREE;
3098 --processing_template_parmlist;
3100 return saved_parmlist;
3103 /* end_template_decl is called after a template declaration is seen. */
3105 void
3106 end_template_decl (void)
3108 reset_specialization ();
3110 if (! processing_template_decl)
3111 return;
3113 /* This matches the pushlevel in begin_template_parm_list. */
3114 finish_scope ();
3116 --processing_template_decl;
3117 current_template_parms = TREE_CHAIN (current_template_parms);
3120 /* Within the declaration of a template, return all levels of template
3121 parameters that apply. The template parameters are represented as
3122 a TREE_VEC, in the form documented in cp-tree.h for template
3123 arguments. */
3125 static tree
3126 current_template_args (void)
3128 tree header;
3129 tree args = NULL_TREE;
3130 int length = TMPL_PARMS_DEPTH (current_template_parms);
3131 int l = length;
3133 /* If there is only one level of template parameters, we do not
3134 create a TREE_VEC of TREE_VECs. Instead, we return a single
3135 TREE_VEC containing the arguments. */
3136 if (length > 1)
3137 args = make_tree_vec (length);
3139 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3141 tree a = copy_node (TREE_VALUE (header));
3142 int i;
3144 TREE_TYPE (a) = NULL_TREE;
3145 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3147 tree t = TREE_VEC_ELT (a, i);
3149 /* T will be a list if we are called from within a
3150 begin/end_template_parm_list pair, but a vector directly
3151 if within a begin/end_member_template_processing pair. */
3152 if (TREE_CODE (t) == TREE_LIST)
3154 t = TREE_VALUE (t);
3156 if (!error_operand_p (t))
3158 if (TREE_CODE (t) == TYPE_DECL
3159 || TREE_CODE (t) == TEMPLATE_DECL)
3161 t = TREE_TYPE (t);
3163 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3165 /* Turn this argument into a TYPE_ARGUMENT_PACK
3166 with a single element, which expands T. */
3167 tree vec = make_tree_vec (1);
3168 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3170 t = make_node (TYPE_ARGUMENT_PACK);
3171 SET_ARGUMENT_PACK_ARGS (t, vec);
3174 else
3176 t = DECL_INITIAL (t);
3178 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3180 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3181 with a single element, which expands T. */
3182 tree vec = make_tree_vec (1);
3183 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3184 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3186 t = make_node (NONTYPE_ARGUMENT_PACK);
3187 SET_ARGUMENT_PACK_ARGS (t, vec);
3188 TREE_TYPE (t) = type;
3191 TREE_VEC_ELT (a, i) = t;
3196 if (length > 1)
3197 TREE_VEC_ELT (args, --l) = a;
3198 else
3199 args = a;
3202 return args;
3205 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3206 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3207 a member template. Used by push_template_decl below. */
3209 static tree
3210 build_template_decl (tree decl, tree parms, bool member_template_p)
3212 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3213 DECL_TEMPLATE_PARMS (tmpl) = parms;
3214 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3215 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3216 if (DECL_LANG_SPECIFIC (decl))
3218 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3219 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3220 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3221 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3222 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3223 if (DECL_OVERLOADED_OPERATOR_P (decl))
3224 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3225 DECL_OVERLOADED_OPERATOR_P (decl));
3228 return tmpl;
3231 struct template_parm_data
3233 /* The level of the template parameters we are currently
3234 processing. */
3235 int level;
3237 /* The index of the specialization argument we are currently
3238 processing. */
3239 int current_arg;
3241 /* An array whose size is the number of template parameters. The
3242 elements are nonzero if the parameter has been used in any one
3243 of the arguments processed so far. */
3244 int* parms;
3246 /* An array whose size is the number of template arguments. The
3247 elements are nonzero if the argument makes use of template
3248 parameters of this level. */
3249 int* arg_uses_template_parms;
3252 /* Subroutine of push_template_decl used to see if each template
3253 parameter in a partial specialization is used in the explicit
3254 argument list. If T is of the LEVEL given in DATA (which is
3255 treated as a template_parm_data*), then DATA->PARMS is marked
3256 appropriately. */
3258 static int
3259 mark_template_parm (tree t, void* data)
3261 int level;
3262 int idx;
3263 struct template_parm_data* tpd = (struct template_parm_data*) data;
3265 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3267 level = TEMPLATE_PARM_LEVEL (t);
3268 idx = TEMPLATE_PARM_IDX (t);
3270 else
3272 level = TEMPLATE_TYPE_LEVEL (t);
3273 idx = TEMPLATE_TYPE_IDX (t);
3276 if (level == tpd->level)
3278 tpd->parms[idx] = 1;
3279 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3282 /* Return zero so that for_each_template_parm will continue the
3283 traversal of the tree; we want to mark *every* template parm. */
3284 return 0;
3287 /* Process the partial specialization DECL. */
3289 static tree
3290 process_partial_specialization (tree decl)
3292 tree type = TREE_TYPE (decl);
3293 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3294 tree specargs = CLASSTYPE_TI_ARGS (type);
3295 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3296 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3297 tree inner_parms;
3298 int nargs = TREE_VEC_LENGTH (inner_args);
3299 int ntparms;
3300 int i;
3301 int did_error_intro = 0;
3302 struct template_parm_data tpd;
3303 struct template_parm_data tpd2;
3305 gcc_assert (current_template_parms);
3307 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3308 ntparms = TREE_VEC_LENGTH (inner_parms);
3310 /* We check that each of the template parameters given in the
3311 partial specialization is used in the argument list to the
3312 specialization. For example:
3314 template <class T> struct S;
3315 template <class T> struct S<T*>;
3317 The second declaration is OK because `T*' uses the template
3318 parameter T, whereas
3320 template <class T> struct S<int>;
3322 is no good. Even trickier is:
3324 template <class T>
3325 struct S1
3327 template <class U>
3328 struct S2;
3329 template <class U>
3330 struct S2<T>;
3333 The S2<T> declaration is actually invalid; it is a
3334 full-specialization. Of course,
3336 template <class U>
3337 struct S2<T (*)(U)>;
3339 or some such would have been OK. */
3340 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3341 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3342 memset (tpd.parms, 0, sizeof (int) * ntparms);
3344 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3345 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3346 for (i = 0; i < nargs; ++i)
3348 tpd.current_arg = i;
3349 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3350 &mark_template_parm,
3351 &tpd,
3352 NULL,
3353 /*include_nondeduced_p=*/false);
3355 for (i = 0; i < ntparms; ++i)
3356 if (tpd.parms[i] == 0)
3358 /* One of the template parms was not used in the
3359 specialization. */
3360 if (!did_error_intro)
3362 error ("template parameters not used in partial specialization:");
3363 did_error_intro = 1;
3366 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3369 /* [temp.class.spec]
3371 The argument list of the specialization shall not be identical to
3372 the implicit argument list of the primary template. */
3373 if (comp_template_args
3374 (inner_args,
3375 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3376 (maintmpl)))))
3377 error ("partial specialization %qT does not specialize any template arguments", type);
3379 /* [temp.class.spec]
3381 A partially specialized non-type argument expression shall not
3382 involve template parameters of the partial specialization except
3383 when the argument expression is a simple identifier.
3385 The type of a template parameter corresponding to a specialized
3386 non-type argument shall not be dependent on a parameter of the
3387 specialization.
3389 Also, we verify that pack expansions only occur at the
3390 end of the argument list. */
3391 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3392 tpd2.parms = 0;
3393 for (i = 0; i < nargs; ++i)
3395 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3396 tree arg = TREE_VEC_ELT (inner_args, i);
3397 tree packed_args = NULL_TREE;
3398 int j, len = 1;
3400 if (ARGUMENT_PACK_P (arg))
3402 /* Extract the arguments from the argument pack. We'll be
3403 iterating over these in the following loop. */
3404 packed_args = ARGUMENT_PACK_ARGS (arg);
3405 len = TREE_VEC_LENGTH (packed_args);
3408 for (j = 0; j < len; j++)
3410 if (packed_args)
3411 /* Get the Jth argument in the parameter pack. */
3412 arg = TREE_VEC_ELT (packed_args, j);
3414 if (PACK_EXPANSION_P (arg))
3416 /* Pack expansions must come at the end of the
3417 argument list. */
3418 if ((packed_args && j < len - 1)
3419 || (!packed_args && i < nargs - 1))
3421 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3422 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3423 else
3424 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3426 if (packed_args)
3427 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3431 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3432 /* We only care about the pattern. */
3433 arg = PACK_EXPANSION_PATTERN (arg);
3435 if (/* These first two lines are the `non-type' bit. */
3436 !TYPE_P (arg)
3437 && TREE_CODE (arg) != TEMPLATE_DECL
3438 /* This next line is the `argument expression is not just a
3439 simple identifier' condition and also the `specialized
3440 non-type argument' bit. */
3441 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3443 if ((!packed_args && tpd.arg_uses_template_parms[i])
3444 || (packed_args && uses_template_parms (arg)))
3445 error ("template argument %qE involves template parameter(s)",
3446 arg);
3447 else
3449 /* Look at the corresponding template parameter,
3450 marking which template parameters its type depends
3451 upon. */
3452 tree type = TREE_TYPE (parm);
3454 if (!tpd2.parms)
3456 /* We haven't yet initialized TPD2. Do so now. */
3457 tpd2.arg_uses_template_parms
3458 = (int *) alloca (sizeof (int) * nargs);
3459 /* The number of parameters here is the number in the
3460 main template, which, as checked in the assertion
3461 above, is NARGS. */
3462 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3463 tpd2.level =
3464 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3467 /* Mark the template parameters. But this time, we're
3468 looking for the template parameters of the main
3469 template, not in the specialization. */
3470 tpd2.current_arg = i;
3471 tpd2.arg_uses_template_parms[i] = 0;
3472 memset (tpd2.parms, 0, sizeof (int) * nargs);
3473 for_each_template_parm (type,
3474 &mark_template_parm,
3475 &tpd2,
3476 NULL,
3477 /*include_nondeduced_p=*/false);
3479 if (tpd2.arg_uses_template_parms [i])
3481 /* The type depended on some template parameters.
3482 If they are fully specialized in the
3483 specialization, that's OK. */
3484 int j;
3485 for (j = 0; j < nargs; ++j)
3486 if (tpd2.parms[j] != 0
3487 && tpd.arg_uses_template_parms [j])
3489 error ("type %qT of template argument %qE depends "
3490 "on template parameter(s)",
3491 type,
3492 arg);
3493 break;
3501 if (retrieve_specialization (maintmpl, specargs,
3502 /*class_specializations_p=*/true))
3503 /* We've already got this specialization. */
3504 return decl;
3506 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3507 = tree_cons (specargs, inner_parms,
3508 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3509 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3510 return decl;
3513 /* Check that a template declaration's use of default arguments and
3514 parameter packs is not invalid. Here, PARMS are the template
3515 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3516 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3517 specialization.
3520 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3521 declaration (but not a definition); 1 indicates a declaration, 2
3522 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3523 emitted for extraneous default arguments.
3525 Returns TRUE if there were no errors found, FALSE otherwise. */
3527 bool
3528 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3529 int is_partial, int is_friend_decl)
3531 const char *msg;
3532 int last_level_to_check;
3533 tree parm_level;
3534 bool no_errors = true;
3536 /* [temp.param]
3538 A default template-argument shall not be specified in a
3539 function template declaration or a function template definition, nor
3540 in the template-parameter-list of the definition of a member of a
3541 class template. */
3543 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3544 /* You can't have a function template declaration in a local
3545 scope, nor you can you define a member of a class template in a
3546 local scope. */
3547 return true;
3549 if (current_class_type
3550 && !TYPE_BEING_DEFINED (current_class_type)
3551 && DECL_LANG_SPECIFIC (decl)
3552 /* If this is either a friend defined in the scope of the class
3553 or a member function. */
3554 && (DECL_FUNCTION_MEMBER_P (decl)
3555 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3556 : DECL_FRIEND_CONTEXT (decl)
3557 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3558 : false)
3559 /* And, if it was a member function, it really was defined in
3560 the scope of the class. */
3561 && (!DECL_FUNCTION_MEMBER_P (decl)
3562 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3563 /* We already checked these parameters when the template was
3564 declared, so there's no need to do it again now. This function
3565 was defined in class scope, but we're processing it's body now
3566 that the class is complete. */
3567 return true;
3569 /* Core issue 226 (C++0x only): the following only applies to class
3570 templates. */
3571 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3573 /* [temp.param]
3575 If a template-parameter has a default template-argument, all
3576 subsequent template-parameters shall have a default
3577 template-argument supplied. */
3578 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3580 tree inner_parms = TREE_VALUE (parm_level);
3581 int ntparms = TREE_VEC_LENGTH (inner_parms);
3582 int seen_def_arg_p = 0;
3583 int i;
3585 for (i = 0; i < ntparms; ++i)
3587 tree parm = TREE_VEC_ELT (inner_parms, i);
3589 if (parm == error_mark_node)
3590 continue;
3592 if (TREE_PURPOSE (parm))
3593 seen_def_arg_p = 1;
3594 else if (seen_def_arg_p
3595 && !template_parameter_pack_p (TREE_VALUE (parm)))
3597 error ("no default argument for %qD", TREE_VALUE (parm));
3598 /* For better subsequent error-recovery, we indicate that
3599 there should have been a default argument. */
3600 TREE_PURPOSE (parm) = error_mark_node;
3601 no_errors = false;
3603 else if (is_primary
3604 && !is_partial
3605 && !is_friend_decl
3606 /* Don't complain about an enclosing partial
3607 specialization. */
3608 && parm_level == parms
3609 && TREE_CODE (decl) == TYPE_DECL
3610 && i < ntparms - 1
3611 && template_parameter_pack_p (TREE_VALUE (parm)))
3613 /* A primary class template can only have one
3614 parameter pack, at the end of the template
3615 parameter list. */
3617 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3618 error ("parameter pack %qE must be at the end of the"
3619 " template parameter list", TREE_VALUE (parm));
3620 else
3621 error ("parameter pack %qT must be at the end of the"
3622 " template parameter list",
3623 TREE_TYPE (TREE_VALUE (parm)));
3625 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3626 = error_mark_node;
3627 no_errors = false;
3633 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3634 || is_partial
3635 || !is_primary
3636 || is_friend_decl)
3637 /* For an ordinary class template, default template arguments are
3638 allowed at the innermost level, e.g.:
3639 template <class T = int>
3640 struct S {};
3641 but, in a partial specialization, they're not allowed even
3642 there, as we have in [temp.class.spec]:
3644 The template parameter list of a specialization shall not
3645 contain default template argument values.
3647 So, for a partial specialization, or for a function template
3648 (in C++98/C++03), we look at all of them. */
3650 else
3651 /* But, for a primary class template that is not a partial
3652 specialization we look at all template parameters except the
3653 innermost ones. */
3654 parms = TREE_CHAIN (parms);
3656 /* Figure out what error message to issue. */
3657 if (is_friend_decl == 2)
3658 msg = "default template arguments may not be used in function template friend re-declaration";
3659 else if (is_friend_decl)
3660 msg = "default template arguments may not be used in function template friend declarations";
3661 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3662 msg = "default template arguments may not be used in function templates";
3663 else if (is_partial)
3664 msg = "default template arguments may not be used in partial specializations";
3665 else
3666 msg = "default argument for template parameter for class enclosing %qD";
3668 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3669 /* If we're inside a class definition, there's no need to
3670 examine the parameters to the class itself. On the one
3671 hand, they will be checked when the class is defined, and,
3672 on the other, default arguments are valid in things like:
3673 template <class T = double>
3674 struct S { template <class U> void f(U); };
3675 Here the default argument for `S' has no bearing on the
3676 declaration of `f'. */
3677 last_level_to_check = template_class_depth (current_class_type) + 1;
3678 else
3679 /* Check everything. */
3680 last_level_to_check = 0;
3682 for (parm_level = parms;
3683 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3684 parm_level = TREE_CHAIN (parm_level))
3686 tree inner_parms = TREE_VALUE (parm_level);
3687 int i;
3688 int ntparms;
3690 ntparms = TREE_VEC_LENGTH (inner_parms);
3691 for (i = 0; i < ntparms; ++i)
3693 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3694 continue;
3696 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3698 if (msg)
3700 no_errors = false;
3701 if (is_friend_decl == 2)
3702 return no_errors;
3704 error (msg, decl);
3705 msg = 0;
3708 /* Clear out the default argument so that we are not
3709 confused later. */
3710 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3714 /* At this point, if we're still interested in issuing messages,
3715 they must apply to classes surrounding the object declared. */
3716 if (msg)
3717 msg = "default argument for template parameter for class enclosing %qD";
3720 return no_errors;
3723 /* Worker for push_template_decl_real, called via
3724 for_each_template_parm. DATA is really an int, indicating the
3725 level of the parameters we are interested in. If T is a template
3726 parameter of that level, return nonzero. */
3728 static int
3729 template_parm_this_level_p (tree t, void* data)
3731 int this_level = *(int *)data;
3732 int level;
3734 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3735 level = TEMPLATE_PARM_LEVEL (t);
3736 else
3737 level = TEMPLATE_TYPE_LEVEL (t);
3738 return level == this_level;
3741 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3742 parameters given by current_template_args, or reuses a
3743 previously existing one, if appropriate. Returns the DECL, or an
3744 equivalent one, if it is replaced via a call to duplicate_decls.
3746 If IS_FRIEND is true, DECL is a friend declaration. */
3748 tree
3749 push_template_decl_real (tree decl, bool is_friend)
3751 tree tmpl;
3752 tree args;
3753 tree info;
3754 tree ctx;
3755 int primary;
3756 int is_partial;
3757 int new_template_p = 0;
3758 /* True if the template is a member template, in the sense of
3759 [temp.mem]. */
3760 bool member_template_p = false;
3762 if (decl == error_mark_node || !current_template_parms)
3763 return error_mark_node;
3765 /* See if this is a partial specialization. */
3766 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3767 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3768 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3770 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3771 is_friend = true;
3773 if (is_friend)
3774 /* For a friend, we want the context of the friend function, not
3775 the type of which it is a friend. */
3776 ctx = DECL_CONTEXT (decl);
3777 else if (CP_DECL_CONTEXT (decl)
3778 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3779 /* In the case of a virtual function, we want the class in which
3780 it is defined. */
3781 ctx = CP_DECL_CONTEXT (decl);
3782 else
3783 /* Otherwise, if we're currently defining some class, the DECL
3784 is assumed to be a member of the class. */
3785 ctx = current_scope ();
3787 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3788 ctx = NULL_TREE;
3790 if (!DECL_CONTEXT (decl))
3791 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3793 /* See if this is a primary template. */
3794 if (is_friend && ctx)
3795 /* A friend template that specifies a class context, i.e.
3796 template <typename T> friend void A<T>::f();
3797 is not primary. */
3798 primary = 0;
3799 else
3800 primary = template_parm_scope_p ();
3802 if (primary)
3804 if (DECL_CLASS_SCOPE_P (decl))
3805 member_template_p = true;
3806 if (TREE_CODE (decl) == TYPE_DECL
3807 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3809 error ("template class without a name");
3810 return error_mark_node;
3812 else if (TREE_CODE (decl) == FUNCTION_DECL)
3814 if (DECL_DESTRUCTOR_P (decl))
3816 /* [temp.mem]
3818 A destructor shall not be a member template. */
3819 error ("destructor %qD declared as member template", decl);
3820 return error_mark_node;
3822 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3823 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3824 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3825 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3826 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3827 == void_list_node)))
3829 /* [basic.stc.dynamic.allocation]
3831 An allocation function can be a function
3832 template. ... Template allocation functions shall
3833 have two or more parameters. */
3834 error ("invalid template declaration of %qD", decl);
3835 return error_mark_node;
3838 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3839 && CLASS_TYPE_P (TREE_TYPE (decl)))
3840 /* OK */;
3841 else
3843 error ("template declaration of %q#D", decl);
3844 return error_mark_node;
3848 /* Check to see that the rules regarding the use of default
3849 arguments are not being violated. */
3850 check_default_tmpl_args (decl, current_template_parms,
3851 primary, is_partial, /*is_friend_decl=*/0);
3853 /* Ensure that there are no parameter packs in the type of this
3854 declaration that have not been expanded. */
3855 if (TREE_CODE (decl) == FUNCTION_DECL)
3857 /* Check each of the arguments individually to see if there are
3858 any bare parameter packs. */
3859 tree type = TREE_TYPE (decl);
3860 tree arg = DECL_ARGUMENTS (decl);
3861 tree argtype = TYPE_ARG_TYPES (type);
3863 while (arg && argtype)
3865 if (!FUNCTION_PARAMETER_PACK_P (arg)
3866 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
3868 /* This is a PARM_DECL that contains unexpanded parameter
3869 packs. We have already complained about this in the
3870 check_for_bare_parameter_packs call, so just replace
3871 these types with ERROR_MARK_NODE. */
3872 TREE_TYPE (arg) = error_mark_node;
3873 TREE_VALUE (argtype) = error_mark_node;
3876 arg = TREE_CHAIN (arg);
3877 argtype = TREE_CHAIN (argtype);
3880 /* Check for bare parameter packs in the return type and the
3881 exception specifiers. */
3882 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
3883 /* Errors were already issued, set return type to int
3884 as the frontend doesn't expect error_mark_node as
3885 the return type. */
3886 TREE_TYPE (type) = integer_type_node;
3887 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
3888 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
3890 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3892 TREE_TYPE (decl) = error_mark_node;
3893 return error_mark_node;
3896 if (is_partial)
3897 return process_partial_specialization (decl);
3899 args = current_template_args ();
3901 if (!ctx
3902 || TREE_CODE (ctx) == FUNCTION_DECL
3903 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3904 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3906 if (DECL_LANG_SPECIFIC (decl)
3907 && DECL_TEMPLATE_INFO (decl)
3908 && DECL_TI_TEMPLATE (decl))
3909 tmpl = DECL_TI_TEMPLATE (decl);
3910 /* If DECL is a TYPE_DECL for a class-template, then there won't
3911 be DECL_LANG_SPECIFIC. The information equivalent to
3912 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3913 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3914 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3915 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3917 /* Since a template declaration already existed for this
3918 class-type, we must be redeclaring it here. Make sure
3919 that the redeclaration is valid. */
3920 redeclare_class_template (TREE_TYPE (decl),
3921 current_template_parms);
3922 /* We don't need to create a new TEMPLATE_DECL; just use the
3923 one we already had. */
3924 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3926 else
3928 tmpl = build_template_decl (decl, current_template_parms,
3929 member_template_p);
3930 new_template_p = 1;
3932 if (DECL_LANG_SPECIFIC (decl)
3933 && DECL_TEMPLATE_SPECIALIZATION (decl))
3935 /* A specialization of a member template of a template
3936 class. */
3937 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3938 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3939 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3943 else
3945 tree a, t, current, parms;
3946 int i;
3947 tree tinfo = get_template_info (decl);
3949 if (!tinfo)
3951 error ("template definition of non-template %q#D", decl);
3952 return error_mark_node;
3955 tmpl = TI_TEMPLATE (tinfo);
3957 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3958 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3959 && DECL_TEMPLATE_SPECIALIZATION (decl)
3960 && DECL_MEMBER_TEMPLATE_P (tmpl))
3962 tree new_tmpl;
3964 /* The declaration is a specialization of a member
3965 template, declared outside the class. Therefore, the
3966 innermost template arguments will be NULL, so we
3967 replace them with the arguments determined by the
3968 earlier call to check_explicit_specialization. */
3969 args = DECL_TI_ARGS (decl);
3971 new_tmpl
3972 = build_template_decl (decl, current_template_parms,
3973 member_template_p);
3974 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3975 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3976 DECL_TI_TEMPLATE (decl) = new_tmpl;
3977 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3978 DECL_TEMPLATE_INFO (new_tmpl)
3979 = tree_cons (tmpl, args, NULL_TREE);
3981 register_specialization (new_tmpl,
3982 most_general_template (tmpl),
3983 args,
3984 is_friend);
3985 return decl;
3988 /* Make sure the template headers we got make sense. */
3990 parms = DECL_TEMPLATE_PARMS (tmpl);
3991 i = TMPL_PARMS_DEPTH (parms);
3992 if (TMPL_ARGS_DEPTH (args) != i)
3994 error ("expected %d levels of template parms for %q#D, got %d",
3995 i, decl, TMPL_ARGS_DEPTH (args));
3997 else
3998 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4000 a = TMPL_ARGS_LEVEL (args, i);
4001 t = INNERMOST_TEMPLATE_PARMS (parms);
4003 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4005 if (current == decl)
4006 error ("got %d template parameters for %q#D",
4007 TREE_VEC_LENGTH (a), decl);
4008 else
4009 error ("got %d template parameters for %q#T",
4010 TREE_VEC_LENGTH (a), current);
4011 error (" but %d required", TREE_VEC_LENGTH (t));
4012 return error_mark_node;
4015 if (current == decl)
4016 current = ctx;
4017 else
4018 current = (TYPE_P (current)
4019 ? TYPE_CONTEXT (current)
4020 : DECL_CONTEXT (current));
4023 /* Check that the parms are used in the appropriate qualifying scopes
4024 in the declarator. */
4025 if (!comp_template_args
4026 (TI_ARGS (tinfo),
4027 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4029 error ("\
4030 template arguments to %qD do not match original template %qD",
4031 decl, DECL_TEMPLATE_RESULT (tmpl));
4032 if (!uses_template_parms (TI_ARGS (tinfo)))
4033 inform (input_location, "use template<> for an explicit specialization");
4034 /* Avoid crash in import_export_decl. */
4035 DECL_INTERFACE_KNOWN (decl) = 1;
4036 return error_mark_node;
4040 DECL_TEMPLATE_RESULT (tmpl) = decl;
4041 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4043 /* Push template declarations for global functions and types. Note
4044 that we do not try to push a global template friend declared in a
4045 template class; such a thing may well depend on the template
4046 parameters of the class. */
4047 if (new_template_p && !ctx
4048 && !(is_friend && template_class_depth (current_class_type) > 0))
4050 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4051 if (tmpl == error_mark_node)
4052 return error_mark_node;
4054 /* Hide template friend classes that haven't been declared yet. */
4055 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4057 DECL_ANTICIPATED (tmpl) = 1;
4058 DECL_FRIEND_P (tmpl) = 1;
4062 if (primary)
4064 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4065 int i;
4067 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4068 if (DECL_CONV_FN_P (tmpl))
4070 int depth = TMPL_PARMS_DEPTH (parms);
4072 /* It is a conversion operator. See if the type converted to
4073 depends on innermost template operands. */
4075 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4076 depth))
4077 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4080 /* Give template template parms a DECL_CONTEXT of the template
4081 for which they are a parameter. */
4082 parms = INNERMOST_TEMPLATE_PARMS (parms);
4083 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4085 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4086 if (TREE_CODE (parm) == TEMPLATE_DECL)
4087 DECL_CONTEXT (parm) = tmpl;
4091 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4092 back to its most general template. If TMPL is a specialization,
4093 ARGS may only have the innermost set of arguments. Add the missing
4094 argument levels if necessary. */
4095 if (DECL_TEMPLATE_INFO (tmpl))
4096 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4098 info = tree_cons (tmpl, args, NULL_TREE);
4100 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4101 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4102 else if (DECL_LANG_SPECIFIC (decl))
4103 DECL_TEMPLATE_INFO (decl) = info;
4105 return DECL_TEMPLATE_RESULT (tmpl);
4108 tree
4109 push_template_decl (tree decl)
4111 return push_template_decl_real (decl, false);
4114 /* Called when a class template TYPE is redeclared with the indicated
4115 template PARMS, e.g.:
4117 template <class T> struct S;
4118 template <class T> struct S {}; */
4120 bool
4121 redeclare_class_template (tree type, tree parms)
4123 tree tmpl;
4124 tree tmpl_parms;
4125 int i;
4127 if (!TYPE_TEMPLATE_INFO (type))
4129 error ("%qT is not a template type", type);
4130 return false;
4133 tmpl = TYPE_TI_TEMPLATE (type);
4134 if (!PRIMARY_TEMPLATE_P (tmpl))
4135 /* The type is nested in some template class. Nothing to worry
4136 about here; there are no new template parameters for the nested
4137 type. */
4138 return true;
4140 if (!parms)
4142 error ("template specifiers not specified in declaration of %qD",
4143 tmpl);
4144 return false;
4147 parms = INNERMOST_TEMPLATE_PARMS (parms);
4148 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4150 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4152 error ("redeclared with %d template parameter(s)",
4153 TREE_VEC_LENGTH (parms));
4154 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4155 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4156 return false;
4159 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4161 tree tmpl_parm;
4162 tree parm;
4163 tree tmpl_default;
4164 tree parm_default;
4166 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4167 || TREE_VEC_ELT (parms, i) == error_mark_node)
4168 continue;
4170 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4171 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4172 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4173 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4175 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4176 TEMPLATE_DECL. */
4177 if (tmpl_parm != error_mark_node
4178 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4179 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4180 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4181 || (TREE_CODE (tmpl_parm) != PARM_DECL
4182 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4183 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4184 || (TREE_CODE (tmpl_parm) == PARM_DECL
4185 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4186 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4188 error ("template parameter %q+#D", tmpl_parm);
4189 error ("redeclared here as %q#D", parm);
4190 return false;
4193 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4195 /* We have in [temp.param]:
4197 A template-parameter may not be given default arguments
4198 by two different declarations in the same scope. */
4199 error ("redefinition of default argument for %q#D", parm);
4200 inform (input_location, "%Joriginal definition appeared here", tmpl_parm);
4201 return false;
4204 if (parm_default != NULL_TREE)
4205 /* Update the previous template parameters (which are the ones
4206 that will really count) with the new default value. */
4207 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4208 else if (tmpl_default != NULL_TREE)
4209 /* Update the new parameters, too; they'll be used as the
4210 parameters for any members. */
4211 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4214 return true;
4217 /* Simplify EXPR if it is a non-dependent expression. Returns the
4218 (possibly simplified) expression. */
4220 tree
4221 fold_non_dependent_expr (tree expr)
4223 if (expr == NULL_TREE)
4224 return NULL_TREE;
4226 /* If we're in a template, but EXPR isn't value dependent, simplify
4227 it. We're supposed to treat:
4229 template <typename T> void f(T[1 + 1]);
4230 template <typename T> void f(T[2]);
4232 as two declarations of the same function, for example. */
4233 if (processing_template_decl
4234 && !type_dependent_expression_p (expr)
4235 && !value_dependent_expression_p (expr))
4237 HOST_WIDE_INT saved_processing_template_decl;
4239 saved_processing_template_decl = processing_template_decl;
4240 processing_template_decl = 0;
4241 expr = tsubst_copy_and_build (expr,
4242 /*args=*/NULL_TREE,
4243 tf_error,
4244 /*in_decl=*/NULL_TREE,
4245 /*function_p=*/false,
4246 /*integral_constant_expression_p=*/true);
4247 processing_template_decl = saved_processing_template_decl;
4249 return expr;
4252 /* EXPR is an expression which is used in a constant-expression context.
4253 For instance, it could be a VAR_DECL with a constant initializer.
4254 Extract the innermost constant expression.
4256 This is basically a more powerful version of
4257 integral_constant_value, which can be used also in templates where
4258 initializers can maintain a syntactic rather than semantic form
4259 (even if they are non-dependent, for access-checking purposes). */
4261 static tree
4262 fold_decl_constant_value (tree expr)
4264 tree const_expr = expr;
4267 expr = fold_non_dependent_expr (const_expr);
4268 const_expr = integral_constant_value (expr);
4270 while (expr != const_expr);
4272 return expr;
4275 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4276 must be a function or a pointer-to-function type, as specified
4277 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4278 and check that the resulting function has external linkage. */
4280 static tree
4281 convert_nontype_argument_function (tree type, tree expr)
4283 tree fns = expr;
4284 tree fn, fn_no_ptr;
4286 fn = instantiate_type (type, fns, tf_none);
4287 if (fn == error_mark_node)
4288 return error_mark_node;
4290 fn_no_ptr = fn;
4291 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4292 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4293 if (TREE_CODE (fn_no_ptr) == BASELINK)
4294 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4296 /* [temp.arg.nontype]/1
4298 A template-argument for a non-type, non-template template-parameter
4299 shall be one of:
4300 [...]
4301 -- the address of an object or function with external linkage. */
4302 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4304 error ("%qE is not a valid template argument for type %qT "
4305 "because function %qD has not external linkage",
4306 expr, type, fn_no_ptr);
4307 return NULL_TREE;
4310 return fn;
4313 /* Attempt to convert the non-type template parameter EXPR to the
4314 indicated TYPE. If the conversion is successful, return the
4315 converted value. If the conversion is unsuccessful, return
4316 NULL_TREE if we issued an error message, or error_mark_node if we
4317 did not. We issue error messages for out-and-out bad template
4318 parameters, but not simply because the conversion failed, since we
4319 might be just trying to do argument deduction. Both TYPE and EXPR
4320 must be non-dependent.
4322 The conversion follows the special rules described in
4323 [temp.arg.nontype], and it is much more strict than an implicit
4324 conversion.
4326 This function is called twice for each template argument (see
4327 lookup_template_class for a more accurate description of this
4328 problem). This means that we need to handle expressions which
4329 are not valid in a C++ source, but can be created from the
4330 first call (for instance, casts to perform conversions). These
4331 hacks can go away after we fix the double coercion problem. */
4333 static tree
4334 convert_nontype_argument (tree type, tree expr)
4336 tree expr_type;
4338 /* Detect immediately string literals as invalid non-type argument.
4339 This special-case is not needed for correctness (we would easily
4340 catch this later), but only to provide better diagnostic for this
4341 common user mistake. As suggested by DR 100, we do not mention
4342 linkage issues in the diagnostic as this is not the point. */
4343 if (TREE_CODE (expr) == STRING_CST)
4345 error ("%qE is not a valid template argument for type %qT "
4346 "because string literals can never be used in this context",
4347 expr, type);
4348 return NULL_TREE;
4351 /* If we are in a template, EXPR may be non-dependent, but still
4352 have a syntactic, rather than semantic, form. For example, EXPR
4353 might be a SCOPE_REF, rather than the VAR_DECL to which the
4354 SCOPE_REF refers. Preserving the qualifying scope is necessary
4355 so that access checking can be performed when the template is
4356 instantiated -- but here we need the resolved form so that we can
4357 convert the argument. */
4358 expr = fold_non_dependent_expr (expr);
4359 if (error_operand_p (expr))
4360 return error_mark_node;
4361 expr_type = TREE_TYPE (expr);
4363 /* HACK: Due to double coercion, we can get a
4364 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4365 which is the tree that we built on the first call (see
4366 below when coercing to reference to object or to reference to
4367 function). We just strip everything and get to the arg.
4368 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4369 for examples. */
4370 if (TREE_CODE (expr) == NOP_EXPR)
4372 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4374 /* ??? Maybe we could use convert_from_reference here, but we
4375 would need to relax its constraints because the NOP_EXPR
4376 could actually change the type to something more cv-qualified,
4377 and this is not folded by convert_from_reference. */
4378 tree addr = TREE_OPERAND (expr, 0);
4379 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4380 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4381 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4382 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4383 (TREE_TYPE (expr_type),
4384 TREE_TYPE (TREE_TYPE (addr))));
4386 expr = TREE_OPERAND (addr, 0);
4387 expr_type = TREE_TYPE (expr);
4390 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4391 parameter is a pointer to object, through decay and
4392 qualification conversion. Let's strip everything. */
4393 else if (TYPE_PTROBV_P (type))
4395 STRIP_NOPS (expr);
4396 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4397 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4398 /* Skip the ADDR_EXPR only if it is part of the decay for
4399 an array. Otherwise, it is part of the original argument
4400 in the source code. */
4401 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4402 expr = TREE_OPERAND (expr, 0);
4403 expr_type = TREE_TYPE (expr);
4407 /* [temp.arg.nontype]/5, bullet 1
4409 For a non-type template-parameter of integral or enumeration type,
4410 integral promotions (_conv.prom_) and integral conversions
4411 (_conv.integral_) are applied. */
4412 if (INTEGRAL_TYPE_P (type))
4414 if (!INTEGRAL_TYPE_P (expr_type))
4415 return error_mark_node;
4417 expr = fold_decl_constant_value (expr);
4418 /* Notice that there are constant expressions like '4 % 0' which
4419 do not fold into integer constants. */
4420 if (TREE_CODE (expr) != INTEGER_CST)
4422 error ("%qE is not a valid template argument for type %qT "
4423 "because it is a non-constant expression", expr, type);
4424 return NULL_TREE;
4427 /* At this point, an implicit conversion does what we want,
4428 because we already know that the expression is of integral
4429 type. */
4430 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4431 if (expr == error_mark_node)
4432 return error_mark_node;
4434 /* Conversion was allowed: fold it to a bare integer constant. */
4435 expr = fold (expr);
4437 /* [temp.arg.nontype]/5, bullet 2
4439 For a non-type template-parameter of type pointer to object,
4440 qualification conversions (_conv.qual_) and the array-to-pointer
4441 conversion (_conv.array_) are applied. */
4442 else if (TYPE_PTROBV_P (type))
4444 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4446 A template-argument for a non-type, non-template template-parameter
4447 shall be one of: [...]
4449 -- the name of a non-type template-parameter;
4450 -- the address of an object or function with external linkage, [...]
4451 expressed as "& id-expression" where the & is optional if the name
4452 refers to a function or array, or if the corresponding
4453 template-parameter is a reference.
4455 Here, we do not care about functions, as they are invalid anyway
4456 for a parameter of type pointer-to-object. */
4458 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4459 /* Non-type template parameters are OK. */
4461 else if (TREE_CODE (expr) != ADDR_EXPR
4462 && TREE_CODE (expr_type) != ARRAY_TYPE)
4464 if (TREE_CODE (expr) == VAR_DECL)
4466 error ("%qD is not a valid template argument "
4467 "because %qD is a variable, not the address of "
4468 "a variable",
4469 expr, expr);
4470 return NULL_TREE;
4472 /* Other values, like integer constants, might be valid
4473 non-type arguments of some other type. */
4474 return error_mark_node;
4476 else
4478 tree decl;
4480 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4481 ? TREE_OPERAND (expr, 0) : expr);
4482 if (TREE_CODE (decl) != VAR_DECL)
4484 error ("%qE is not a valid template argument of type %qT "
4485 "because %qE is not a variable",
4486 expr, type, decl);
4487 return NULL_TREE;
4489 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4491 error ("%qE is not a valid template argument of type %qT "
4492 "because %qD does not have external linkage",
4493 expr, type, decl);
4494 return NULL_TREE;
4498 expr = decay_conversion (expr);
4499 if (expr == error_mark_node)
4500 return error_mark_node;
4502 expr = perform_qualification_conversions (type, expr);
4503 if (expr == error_mark_node)
4504 return error_mark_node;
4506 /* [temp.arg.nontype]/5, bullet 3
4508 For a non-type template-parameter of type reference to object, no
4509 conversions apply. The type referred to by the reference may be more
4510 cv-qualified than the (otherwise identical) type of the
4511 template-argument. The template-parameter is bound directly to the
4512 template-argument, which must be an lvalue. */
4513 else if (TYPE_REF_OBJ_P (type))
4515 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4516 expr_type))
4517 return error_mark_node;
4519 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4521 error ("%qE is not a valid template argument for type %qT "
4522 "because of conflicts in cv-qualification", expr, type);
4523 return NULL_TREE;
4526 if (!real_lvalue_p (expr))
4528 error ("%qE is not a valid template argument for type %qT "
4529 "because it is not an lvalue", expr, type);
4530 return NULL_TREE;
4533 /* [temp.arg.nontype]/1
4535 A template-argument for a non-type, non-template template-parameter
4536 shall be one of: [...]
4538 -- the address of an object or function with external linkage. */
4539 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4541 error ("%qE is not a valid template argument for type %qT "
4542 "because object %qD has not external linkage",
4543 expr, type, expr);
4544 return NULL_TREE;
4547 expr = build_nop (type, build_address (expr));
4549 /* [temp.arg.nontype]/5, bullet 4
4551 For a non-type template-parameter of type pointer to function, only
4552 the function-to-pointer conversion (_conv.func_) is applied. If the
4553 template-argument represents a set of overloaded functions (or a
4554 pointer to such), the matching function is selected from the set
4555 (_over.over_). */
4556 else if (TYPE_PTRFN_P (type))
4558 /* If the argument is a template-id, we might not have enough
4559 context information to decay the pointer. */
4560 if (!type_unknown_p (expr_type))
4562 expr = decay_conversion (expr);
4563 if (expr == error_mark_node)
4564 return error_mark_node;
4567 expr = convert_nontype_argument_function (type, expr);
4568 if (!expr || expr == error_mark_node)
4569 return expr;
4571 if (TREE_CODE (expr) != ADDR_EXPR)
4573 error ("%qE is not a valid template argument for type %qT", expr, type);
4574 error ("it must be the address of a function with external linkage");
4575 return NULL_TREE;
4578 /* [temp.arg.nontype]/5, bullet 5
4580 For a non-type template-parameter of type reference to function, no
4581 conversions apply. If the template-argument represents a set of
4582 overloaded functions, the matching function is selected from the set
4583 (_over.over_). */
4584 else if (TYPE_REFFN_P (type))
4586 if (TREE_CODE (expr) == ADDR_EXPR)
4588 error ("%qE is not a valid template argument for type %qT "
4589 "because it is a pointer", expr, type);
4590 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
4591 return NULL_TREE;
4594 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4595 if (!expr || expr == error_mark_node)
4596 return expr;
4598 expr = build_nop (type, build_address (expr));
4600 /* [temp.arg.nontype]/5, bullet 6
4602 For a non-type template-parameter of type pointer to member function,
4603 no conversions apply. If the template-argument represents a set of
4604 overloaded member functions, the matching member function is selected
4605 from the set (_over.over_). */
4606 else if (TYPE_PTRMEMFUNC_P (type))
4608 expr = instantiate_type (type, expr, tf_none);
4609 if (expr == error_mark_node)
4610 return error_mark_node;
4612 /* There is no way to disable standard conversions in
4613 resolve_address_of_overloaded_function (called by
4614 instantiate_type). It is possible that the call succeeded by
4615 converting &B::I to &D::I (where B is a base of D), so we need
4616 to reject this conversion here.
4618 Actually, even if there was a way to disable standard conversions,
4619 it would still be better to reject them here so that we can
4620 provide a superior diagnostic. */
4621 if (!same_type_p (TREE_TYPE (expr), type))
4623 /* Make sure we are just one standard conversion off. */
4624 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4625 error ("%qE is not a valid template argument for type %qT "
4626 "because it is of type %qT", expr, type,
4627 TREE_TYPE (expr));
4628 inform (input_location, "standard conversions are not allowed in this context");
4629 return NULL_TREE;
4632 /* [temp.arg.nontype]/5, bullet 7
4634 For a non-type template-parameter of type pointer to data member,
4635 qualification conversions (_conv.qual_) are applied. */
4636 else if (TYPE_PTRMEM_P (type))
4638 expr = perform_qualification_conversions (type, expr);
4639 if (expr == error_mark_node)
4640 return expr;
4642 /* A template non-type parameter must be one of the above. */
4643 else
4644 gcc_unreachable ();
4646 /* Sanity check: did we actually convert the argument to the
4647 right type? */
4648 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4649 return expr;
4652 /* Subroutine of coerce_template_template_parms, which returns 1 if
4653 PARM_PARM and ARG_PARM match using the rule for the template
4654 parameters of template template parameters. Both PARM and ARG are
4655 template parameters; the rest of the arguments are the same as for
4656 coerce_template_template_parms.
4658 static int
4659 coerce_template_template_parm (tree parm,
4660 tree arg,
4661 tsubst_flags_t complain,
4662 tree in_decl,
4663 tree outer_args)
4665 if (arg == NULL_TREE || arg == error_mark_node
4666 || parm == NULL_TREE || parm == error_mark_node)
4667 return 0;
4669 if (TREE_CODE (arg) != TREE_CODE (parm))
4670 return 0;
4672 switch (TREE_CODE (parm))
4674 case TEMPLATE_DECL:
4675 /* We encounter instantiations of templates like
4676 template <template <template <class> class> class TT>
4677 class C; */
4679 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4680 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4682 if (!coerce_template_template_parms
4683 (parmparm, argparm, complain, in_decl, outer_args))
4684 return 0;
4686 /* Fall through. */
4688 case TYPE_DECL:
4689 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
4690 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4691 /* Argument is a parameter pack but parameter is not. */
4692 return 0;
4693 break;
4695 case PARM_DECL:
4696 /* The tsubst call is used to handle cases such as
4698 template <int> class C {};
4699 template <class T, template <T> class TT> class D {};
4700 D<int, C> d;
4702 i.e. the parameter list of TT depends on earlier parameters. */
4703 if (!uses_template_parms (TREE_TYPE (arg))
4704 && !same_type_p
4705 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4706 TREE_TYPE (arg)))
4707 return 0;
4709 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
4710 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4711 /* Argument is a parameter pack but parameter is not. */
4712 return 0;
4714 break;
4716 default:
4717 gcc_unreachable ();
4720 return 1;
4724 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4725 template template parameters. Both PARM_PARMS and ARG_PARMS are
4726 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4727 or PARM_DECL.
4729 Consider the example:
4730 template <class T> class A;
4731 template<template <class U> class TT> class B;
4733 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4734 the parameters to A, and OUTER_ARGS contains A. */
4736 static int
4737 coerce_template_template_parms (tree parm_parms,
4738 tree arg_parms,
4739 tsubst_flags_t complain,
4740 tree in_decl,
4741 tree outer_args)
4743 int nparms, nargs, i;
4744 tree parm, arg;
4745 int variadic_p = 0;
4747 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4748 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4750 nparms = TREE_VEC_LENGTH (parm_parms);
4751 nargs = TREE_VEC_LENGTH (arg_parms);
4753 /* Determine whether we have a parameter pack at the end of the
4754 template template parameter's template parameter list. */
4755 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
4757 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
4759 if (parm == error_mark_node)
4760 return 0;
4762 switch (TREE_CODE (parm))
4764 case TEMPLATE_DECL:
4765 case TYPE_DECL:
4766 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4767 variadic_p = 1;
4768 break;
4770 case PARM_DECL:
4771 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4772 variadic_p = 1;
4773 break;
4775 default:
4776 gcc_unreachable ();
4780 if (nargs != nparms
4781 && !(variadic_p && nargs >= nparms - 1))
4782 return 0;
4784 /* Check all of the template parameters except the parameter pack at
4785 the end (if any). */
4786 for (i = 0; i < nparms - variadic_p; ++i)
4788 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4789 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4790 continue;
4792 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4793 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4795 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4796 outer_args))
4797 return 0;
4801 if (variadic_p)
4803 /* Check each of the template parameters in the template
4804 argument against the template parameter pack at the end of
4805 the template template parameter. */
4806 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
4807 return 0;
4809 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4811 for (; i < nargs; ++i)
4813 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4814 continue;
4816 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4818 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4819 outer_args))
4820 return 0;
4824 return 1;
4827 /* Verifies that the deduced template arguments (in TARGS) for the
4828 template template parameters (in TPARMS) represent valid bindings,
4829 by comparing the template parameter list of each template argument
4830 to the template parameter list of its corresponding template
4831 template parameter, in accordance with DR150. This
4832 routine can only be called after all template arguments have been
4833 deduced. It will return TRUE if all of the template template
4834 parameter bindings are okay, FALSE otherwise. */
4835 bool
4836 template_template_parm_bindings_ok_p (tree tparms, tree targs)
4838 int i, ntparms = TREE_VEC_LENGTH (tparms);
4839 bool ret = true;
4841 /* We're dealing with template parms in this process. */
4842 ++processing_template_decl;
4844 targs = INNERMOST_TEMPLATE_ARGS (targs);
4846 for (i = 0; i < ntparms; ++i)
4848 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
4849 tree targ = TREE_VEC_ELT (targs, i);
4851 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
4853 tree packed_args = NULL_TREE;
4854 int idx, len = 1;
4856 if (ARGUMENT_PACK_P (targ))
4858 /* Look inside the argument pack. */
4859 packed_args = ARGUMENT_PACK_ARGS (targ);
4860 len = TREE_VEC_LENGTH (packed_args);
4863 for (idx = 0; idx < len; ++idx)
4865 tree targ_parms = NULL_TREE;
4867 if (packed_args)
4868 /* Extract the next argument from the argument
4869 pack. */
4870 targ = TREE_VEC_ELT (packed_args, idx);
4872 if (PACK_EXPANSION_P (targ))
4873 /* Look at the pattern of the pack expansion. */
4874 targ = PACK_EXPANSION_PATTERN (targ);
4876 /* Extract the template parameters from the template
4877 argument. */
4878 if (TREE_CODE (targ) == TEMPLATE_DECL)
4879 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
4880 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
4881 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
4883 /* Verify that we can coerce the template template
4884 parameters from the template argument to the template
4885 parameter. This requires an exact match. */
4886 if (targ_parms
4887 && !coerce_template_template_parms
4888 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
4889 targ_parms,
4890 tf_none,
4891 tparm,
4892 targs))
4894 ret = false;
4895 goto out;
4901 out:
4903 --processing_template_decl;
4904 return ret;
4907 /* Convert the indicated template ARG as necessary to match the
4908 indicated template PARM. Returns the converted ARG, or
4909 error_mark_node if the conversion was unsuccessful. Error and
4910 warning messages are issued under control of COMPLAIN. This
4911 conversion is for the Ith parameter in the parameter list. ARGS is
4912 the full set of template arguments deduced so far. */
4914 static tree
4915 convert_template_argument (tree parm,
4916 tree arg,
4917 tree args,
4918 tsubst_flags_t complain,
4919 int i,
4920 tree in_decl)
4922 tree orig_arg;
4923 tree val;
4924 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4926 if (TREE_CODE (arg) == TREE_LIST
4927 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4929 /* The template argument was the name of some
4930 member function. That's usually
4931 invalid, but static members are OK. In any
4932 case, grab the underlying fields/functions
4933 and issue an error later if required. */
4934 orig_arg = TREE_VALUE (arg);
4935 TREE_TYPE (arg) = unknown_type_node;
4938 orig_arg = arg;
4940 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4941 requires_type = (TREE_CODE (parm) == TYPE_DECL
4942 || requires_tmpl_type);
4944 /* When determining whether an argument pack expansion is a template,
4945 look at the pattern. */
4946 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
4947 arg = PACK_EXPANSION_PATTERN (arg);
4949 is_tmpl_type =
4950 ((TREE_CODE (arg) == TEMPLATE_DECL
4951 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
4952 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4953 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
4955 if (is_tmpl_type
4956 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4957 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4958 arg = TYPE_STUB_DECL (arg);
4960 is_type = TYPE_P (arg) || is_tmpl_type;
4962 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4963 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4965 permerror (input_location, "to refer to a type member of a template parameter, "
4966 "use %<typename %E%>", orig_arg);
4968 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
4969 TREE_OPERAND (arg, 1),
4970 typename_type,
4971 complain & tf_error);
4972 arg = orig_arg;
4973 is_type = 1;
4975 if (is_type != requires_type)
4977 if (in_decl)
4979 if (complain & tf_error)
4981 error ("type/value mismatch at argument %d in template "
4982 "parameter list for %qD",
4983 i + 1, in_decl);
4984 if (is_type)
4985 error (" expected a constant of type %qT, got %qT",
4986 TREE_TYPE (parm),
4987 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
4988 else if (requires_tmpl_type)
4989 error (" expected a class template, got %qE", orig_arg);
4990 else
4991 error (" expected a type, got %qE", orig_arg);
4994 return error_mark_node;
4996 if (is_tmpl_type ^ requires_tmpl_type)
4998 if (in_decl && (complain & tf_error))
5000 error ("type/value mismatch at argument %d in template "
5001 "parameter list for %qD",
5002 i + 1, in_decl);
5003 if (is_tmpl_type)
5004 error (" expected a type, got %qT", DECL_NAME (arg));
5005 else
5006 error (" expected a class template, got %qT", orig_arg);
5008 return error_mark_node;
5011 if (is_type)
5013 if (requires_tmpl_type)
5015 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5016 /* The number of argument required is not known yet.
5017 Just accept it for now. */
5018 val = TREE_TYPE (arg);
5019 else
5021 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5022 tree argparm;
5024 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5026 if (coerce_template_template_parms (parmparm, argparm,
5027 complain, in_decl,
5028 args))
5030 val = orig_arg;
5032 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5033 TEMPLATE_DECL. */
5034 if (val != error_mark_node)
5036 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5037 val = TREE_TYPE (val);
5038 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5039 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5041 val = TREE_TYPE (arg);
5042 val = make_pack_expansion (val);
5046 else
5048 if (in_decl && (complain & tf_error))
5050 error ("type/value mismatch at argument %d in "
5051 "template parameter list for %qD",
5052 i + 1, in_decl);
5053 error (" expected a template of type %qD, got %qD",
5054 parm, orig_arg);
5057 val = error_mark_node;
5061 else
5062 val = orig_arg;
5063 /* We only form one instance of each template specialization.
5064 Therefore, if we use a non-canonical variant (i.e., a
5065 typedef), any future messages referring to the type will use
5066 the typedef, which is confusing if those future uses do not
5067 themselves also use the typedef. */
5068 if (TYPE_P (val))
5069 val = canonical_type_variant (val);
5071 else
5073 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5075 if (invalid_nontype_parm_type_p (t, complain))
5076 return error_mark_node;
5078 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5080 if (same_type_p (t, TREE_TYPE (orig_arg)))
5081 val = orig_arg;
5082 else
5084 /* Not sure if this is reachable, but it doesn't hurt
5085 to be robust. */
5086 error ("type mismatch in nontype parameter pack");
5087 val = error_mark_node;
5090 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5091 /* We used to call digest_init here. However, digest_init
5092 will report errors, which we don't want when complain
5093 is zero. More importantly, digest_init will try too
5094 hard to convert things: for example, `0' should not be
5095 converted to pointer type at this point according to
5096 the standard. Accepting this is not merely an
5097 extension, since deciding whether or not these
5098 conversions can occur is part of determining which
5099 function template to call, or whether a given explicit
5100 argument specification is valid. */
5101 val = convert_nontype_argument (t, orig_arg);
5102 else
5103 val = orig_arg;
5105 if (val == NULL_TREE)
5106 val = error_mark_node;
5107 else if (val == error_mark_node && (complain & tf_error))
5108 error ("could not convert template argument %qE to %qT", orig_arg, t);
5111 return val;
5114 /* Coerces the remaining template arguments in INNER_ARGS (from
5115 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5116 Returns the coerced argument pack. PARM_IDX is the position of this
5117 parameter in the template parameter list. ARGS is the original
5118 template argument list. */
5119 static tree
5120 coerce_template_parameter_pack (tree parms,
5121 int parm_idx,
5122 tree args,
5123 tree inner_args,
5124 int arg_idx,
5125 tree new_args,
5126 int* lost,
5127 tree in_decl,
5128 tsubst_flags_t complain)
5130 tree parm = TREE_VEC_ELT (parms, parm_idx);
5131 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5132 tree packed_args;
5133 tree argument_pack;
5134 tree packed_types = NULL_TREE;
5136 if (arg_idx > nargs)
5137 arg_idx = nargs;
5139 packed_args = make_tree_vec (nargs - arg_idx);
5141 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5142 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5144 /* When the template parameter is a non-type template
5145 parameter pack whose type uses parameter packs, we need
5146 to look at each of the template arguments
5147 separately. Build a vector of the types for these
5148 non-type template parameters in PACKED_TYPES. */
5149 tree expansion
5150 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5151 packed_types = tsubst_pack_expansion (expansion, args,
5152 complain, in_decl);
5154 if (packed_types == error_mark_node)
5155 return error_mark_node;
5157 /* Check that we have the right number of arguments. */
5158 if (arg_idx < nargs
5159 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5160 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5162 int needed_parms
5163 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5164 error ("wrong number of template arguments (%d, should be %d)",
5165 nargs, needed_parms);
5166 return error_mark_node;
5169 /* If we aren't able to check the actual arguments now
5170 (because they haven't been expanded yet), we can at least
5171 verify that all of the types used for the non-type
5172 template parameter pack are, in fact, valid for non-type
5173 template parameters. */
5174 if (arg_idx < nargs
5175 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5177 int j, len = TREE_VEC_LENGTH (packed_types);
5178 for (j = 0; j < len; ++j)
5180 tree t = TREE_VEC_ELT (packed_types, j);
5181 if (invalid_nontype_parm_type_p (t, complain))
5182 return error_mark_node;
5187 /* Convert the remaining arguments, which will be a part of the
5188 parameter pack "parm". */
5189 for (; arg_idx < nargs; ++arg_idx)
5191 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5192 tree actual_parm = TREE_VALUE (parm);
5194 if (packed_types && !PACK_EXPANSION_P (arg))
5196 /* When we have a vector of types (corresponding to the
5197 non-type template parameter pack that uses parameter
5198 packs in its type, as mention above), and the
5199 argument is not an expansion (which expands to a
5200 currently unknown number of arguments), clone the
5201 parm and give it the next type in PACKED_TYPES. */
5202 actual_parm = copy_node (actual_parm);
5203 TREE_TYPE (actual_parm) =
5204 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5207 if (arg != error_mark_node)
5208 arg = convert_template_argument (actual_parm,
5209 arg, new_args, complain, parm_idx,
5210 in_decl);
5211 if (arg == error_mark_node)
5212 (*lost)++;
5213 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5216 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5217 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5218 argument_pack = make_node (TYPE_ARGUMENT_PACK);
5219 else
5221 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5222 TREE_TYPE (argument_pack)
5223 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5224 TREE_CONSTANT (argument_pack) = 1;
5227 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5228 return argument_pack;
5231 /* Convert all template arguments to their appropriate types, and
5232 return a vector containing the innermost resulting template
5233 arguments. If any error occurs, return error_mark_node. Error and
5234 warning messages are issued under control of COMPLAIN.
5236 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5237 for arguments not specified in ARGS. Otherwise, if
5238 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5239 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5240 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5241 ARGS. */
5243 static tree
5244 coerce_template_parms (tree parms,
5245 tree args,
5246 tree in_decl,
5247 tsubst_flags_t complain,
5248 bool require_all_args,
5249 bool use_default_args)
5251 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5252 tree inner_args;
5253 tree new_args;
5254 tree new_inner_args;
5255 bool saved_skip_evaluation;
5257 /* When used as a boolean value, indicates whether this is a
5258 variadic template parameter list. Since it's an int, we can also
5259 subtract it from nparms to get the number of non-variadic
5260 parameters. */
5261 int variadic_p = 0;
5263 nparms = TREE_VEC_LENGTH (parms);
5265 /* Determine if there are any parameter packs. */
5266 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5268 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5269 if (template_parameter_pack_p (tparm))
5270 ++variadic_p;
5273 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5274 /* If there are 0 or 1 parameter packs, we need to expand any argument
5275 packs so that we can deduce a parameter pack from some non-packed args
5276 followed by an argument pack, as in variadic85.C. If there are more
5277 than that, we need to leave argument packs intact so the arguments are
5278 assigned to the right parameter packs. This should only happen when
5279 dealing with a nested class inside a partial specialization of a class
5280 template, as in variadic92.C. */
5281 if (variadic_p <= 1)
5282 inner_args = expand_template_argument_pack (inner_args);
5284 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5285 if ((nargs > nparms && !variadic_p)
5286 || (nargs < nparms - variadic_p
5287 && require_all_args
5288 && (!use_default_args
5289 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5290 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5292 if (complain & tf_error)
5294 const char *or_more = "";
5295 if (variadic_p)
5297 or_more = " or more";
5298 --nparms;
5301 error ("wrong number of template arguments (%d, should be %d%s)",
5302 nargs, nparms, or_more);
5304 if (in_decl)
5305 error ("provided for %q+D", in_decl);
5308 return error_mark_node;
5311 /* We need to evaluate the template arguments, even though this
5312 template-id may be nested within a "sizeof". */
5313 saved_skip_evaluation = skip_evaluation;
5314 skip_evaluation = false;
5315 new_inner_args = make_tree_vec (nparms);
5316 new_args = add_outermost_template_args (args, new_inner_args);
5317 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5319 tree arg;
5320 tree parm;
5322 /* Get the Ith template parameter. */
5323 parm = TREE_VEC_ELT (parms, parm_idx);
5325 if (parm == error_mark_node)
5327 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5328 continue;
5331 /* Calculate the next argument. */
5332 if (arg_idx < nargs)
5333 arg = TREE_VEC_ELT (inner_args, arg_idx);
5334 else
5335 arg = NULL_TREE;
5337 if (template_parameter_pack_p (TREE_VALUE (parm))
5338 && !(arg && ARGUMENT_PACK_P (arg)))
5340 /* All remaining arguments will be placed in the
5341 template parameter pack PARM. */
5342 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5343 inner_args, arg_idx,
5344 new_args, &lost,
5345 in_decl, complain);
5347 /* Store this argument. */
5348 if (arg == error_mark_node)
5349 lost++;
5350 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5352 /* We are done with all of the arguments. */
5353 arg_idx = nargs;
5355 continue;
5357 else if (arg)
5359 if (PACK_EXPANSION_P (arg))
5361 if (complain & tf_error)
5363 /* FIXME this restriction was removed by N2555; see
5364 bug 35722. */
5365 /* If ARG is a pack expansion, but PARM is not a
5366 template parameter pack (if it were, we would have
5367 handled it above), we're trying to expand into a
5368 fixed-length argument list. */
5369 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5370 sorry ("cannot expand %<%E%> into a fixed-length "
5371 "argument list", arg);
5372 else
5373 sorry ("cannot expand %<%T%> into a fixed-length "
5374 "argument list", arg);
5376 return error_mark_node;
5379 else if (require_all_args)
5380 /* There must be a default arg in this case. */
5381 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5382 complain, in_decl);
5383 else
5384 break;
5386 if (arg == error_mark_node)
5388 if (complain & tf_error)
5389 error ("template argument %d is invalid", arg_idx + 1);
5391 else if (!arg)
5392 /* This only occurs if there was an error in the template
5393 parameter list itself (which we would already have
5394 reported) that we are trying to recover from, e.g., a class
5395 template with a parameter list such as
5396 template<typename..., typename>. */
5397 return error_mark_node;
5398 else
5399 arg = convert_template_argument (TREE_VALUE (parm),
5400 arg, new_args, complain,
5401 parm_idx, in_decl);
5403 if (arg == error_mark_node)
5404 lost++;
5405 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5407 skip_evaluation = saved_skip_evaluation;
5409 if (lost)
5410 return error_mark_node;
5412 return new_inner_args;
5415 /* Returns 1 if template args OT and NT are equivalent. */
5417 static int
5418 template_args_equal (tree ot, tree nt)
5420 if (nt == ot)
5421 return 1;
5423 if (TREE_CODE (nt) == TREE_VEC)
5424 /* For member templates */
5425 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5426 else if (PACK_EXPANSION_P (ot))
5427 return PACK_EXPANSION_P (nt)
5428 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5429 PACK_EXPANSION_PATTERN (nt));
5430 else if (ARGUMENT_PACK_P (ot))
5432 int i, len;
5433 tree opack, npack;
5435 if (!ARGUMENT_PACK_P (nt))
5436 return 0;
5438 opack = ARGUMENT_PACK_ARGS (ot);
5439 npack = ARGUMENT_PACK_ARGS (nt);
5440 len = TREE_VEC_LENGTH (opack);
5441 if (TREE_VEC_LENGTH (npack) != len)
5442 return 0;
5443 for (i = 0; i < len; ++i)
5444 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5445 TREE_VEC_ELT (npack, i)))
5446 return 0;
5447 return 1;
5449 else if (TYPE_P (nt))
5450 return TYPE_P (ot) && same_type_p (ot, nt);
5451 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5452 return 0;
5453 else
5454 return cp_tree_equal (ot, nt);
5457 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5458 of template arguments. Returns 0 otherwise. */
5461 comp_template_args (tree oldargs, tree newargs)
5463 int i;
5465 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5466 return 0;
5468 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5470 tree nt = TREE_VEC_ELT (newargs, i);
5471 tree ot = TREE_VEC_ELT (oldargs, i);
5473 if (! template_args_equal (ot, nt))
5474 return 0;
5476 return 1;
5479 static void
5480 add_pending_template (tree d)
5482 tree ti = (TYPE_P (d)
5483 ? CLASSTYPE_TEMPLATE_INFO (d)
5484 : DECL_TEMPLATE_INFO (d));
5485 struct pending_template *pt;
5486 int level;
5488 if (TI_PENDING_TEMPLATE_FLAG (ti))
5489 return;
5491 /* We are called both from instantiate_decl, where we've already had a
5492 tinst_level pushed, and instantiate_template, where we haven't.
5493 Compensate. */
5494 level = !current_tinst_level || current_tinst_level->decl != d;
5496 if (level)
5497 push_tinst_level (d);
5499 pt = GGC_NEW (struct pending_template);
5500 pt->next = NULL;
5501 pt->tinst = current_tinst_level;
5502 if (last_pending_template)
5503 last_pending_template->next = pt;
5504 else
5505 pending_templates = pt;
5507 last_pending_template = pt;
5509 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5511 if (level)
5512 pop_tinst_level ();
5516 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5517 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5518 documentation for TEMPLATE_ID_EXPR. */
5520 tree
5521 lookup_template_function (tree fns, tree arglist)
5523 tree type;
5525 if (fns == error_mark_node || arglist == error_mark_node)
5526 return error_mark_node;
5528 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5529 gcc_assert (fns && (is_overloaded_fn (fns)
5530 || TREE_CODE (fns) == IDENTIFIER_NODE));
5532 if (BASELINK_P (fns))
5534 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5535 unknown_type_node,
5536 BASELINK_FUNCTIONS (fns),
5537 arglist);
5538 return fns;
5541 type = TREE_TYPE (fns);
5542 if (TREE_CODE (fns) == OVERLOAD || !type)
5543 type = unknown_type_node;
5545 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5548 /* Within the scope of a template class S<T>, the name S gets bound
5549 (in build_self_reference) to a TYPE_DECL for the class, not a
5550 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5551 or one of its enclosing classes, and that type is a template,
5552 return the associated TEMPLATE_DECL. Otherwise, the original
5553 DECL is returned. */
5555 tree
5556 maybe_get_template_decl_from_type_decl (tree decl)
5558 return (decl != NULL_TREE
5559 && TREE_CODE (decl) == TYPE_DECL
5560 && DECL_ARTIFICIAL (decl)
5561 && CLASS_TYPE_P (TREE_TYPE (decl))
5562 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5563 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5566 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5567 parameters, find the desired type.
5569 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5571 IN_DECL, if non-NULL, is the template declaration we are trying to
5572 instantiate.
5574 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5575 the class we are looking up.
5577 Issue error and warning messages under control of COMPLAIN.
5579 If the template class is really a local class in a template
5580 function, then the FUNCTION_CONTEXT is the function in which it is
5581 being instantiated.
5583 ??? Note that this function is currently called *twice* for each
5584 template-id: the first time from the parser, while creating the
5585 incomplete type (finish_template_type), and the second type during the
5586 real instantiation (instantiate_template_class). This is surely something
5587 that we want to avoid. It also causes some problems with argument
5588 coercion (see convert_nontype_argument for more information on this). */
5590 tree
5591 lookup_template_class (tree d1,
5592 tree arglist,
5593 tree in_decl,
5594 tree context,
5595 int entering_scope,
5596 tsubst_flags_t complain)
5598 tree templ = NULL_TREE, parmlist;
5599 tree t;
5601 timevar_push (TV_NAME_LOOKUP);
5603 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5605 tree value = innermost_non_namespace_value (d1);
5606 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5607 templ = value;
5608 else
5610 if (context)
5611 push_decl_namespace (context);
5612 templ = lookup_name (d1);
5613 templ = maybe_get_template_decl_from_type_decl (templ);
5614 if (context)
5615 pop_decl_namespace ();
5617 if (templ)
5618 context = DECL_CONTEXT (templ);
5620 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
5622 tree type = TREE_TYPE (d1);
5624 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5625 an implicit typename for the second A. Deal with it. */
5626 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5627 type = TREE_TYPE (type);
5629 if (CLASSTYPE_TEMPLATE_INFO (type))
5631 templ = CLASSTYPE_TI_TEMPLATE (type);
5632 d1 = DECL_NAME (templ);
5635 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5636 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
5638 templ = TYPE_TI_TEMPLATE (d1);
5639 d1 = DECL_NAME (templ);
5641 else if (TREE_CODE (d1) == TEMPLATE_DECL
5642 && DECL_TEMPLATE_RESULT (d1)
5643 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5645 templ = d1;
5646 d1 = DECL_NAME (templ);
5647 context = DECL_CONTEXT (templ);
5650 /* Issue an error message if we didn't find a template. */
5651 if (! templ)
5653 if (complain & tf_error)
5654 error ("%qT is not a template", d1);
5655 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5658 if (TREE_CODE (templ) != TEMPLATE_DECL
5659 /* Make sure it's a user visible template, if it was named by
5660 the user. */
5661 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
5662 && !PRIMARY_TEMPLATE_P (templ)))
5664 if (complain & tf_error)
5666 error ("non-template type %qT used as a template", d1);
5667 if (in_decl)
5668 error ("for template declaration %q+D", in_decl);
5670 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5673 complain &= ~tf_user;
5675 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
5677 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5678 template arguments */
5680 tree parm;
5681 tree arglist2;
5682 tree outer;
5684 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
5686 /* Consider an example where a template template parameter declared as
5688 template <class T, class U = std::allocator<T> > class TT
5690 The template parameter level of T and U are one level larger than
5691 of TT. To proper process the default argument of U, say when an
5692 instantiation `TT<int>' is seen, we need to build the full
5693 arguments containing {int} as the innermost level. Outer levels,
5694 available when not appearing as default template argument, can be
5695 obtained from the arguments of the enclosing template.
5697 Suppose that TT is later substituted with std::vector. The above
5698 instantiation is `TT<int, std::allocator<T> >' with TT at
5699 level 1, and T at level 2, while the template arguments at level 1
5700 becomes {std::vector} and the inner level 2 is {int}. */
5702 outer = DECL_CONTEXT (templ);
5703 if (outer)
5704 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
5705 else if (current_template_parms)
5706 /* This is an argument of the current template, so we haven't set
5707 DECL_CONTEXT yet. */
5708 outer = current_template_args ();
5710 if (outer)
5711 arglist = add_to_template_args (outer, arglist);
5713 arglist2 = coerce_template_parms (parmlist, arglist, templ,
5714 complain,
5715 /*require_all_args=*/true,
5716 /*use_default_args=*/true);
5717 if (arglist2 == error_mark_node
5718 || (!uses_template_parms (arglist2)
5719 && check_instantiated_args (templ, arglist2, complain)))
5720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5722 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
5723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5725 else
5727 tree template_type = TREE_TYPE (templ);
5728 tree gen_tmpl;
5729 tree type_decl;
5730 tree found = NULL_TREE;
5731 int arg_depth;
5732 int parm_depth;
5733 int is_partial_instantiation;
5735 gen_tmpl = most_general_template (templ);
5736 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5737 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5738 arg_depth = TMPL_ARGS_DEPTH (arglist);
5740 if (arg_depth == 1 && parm_depth > 1)
5742 /* We've been given an incomplete set of template arguments.
5743 For example, given:
5745 template <class T> struct S1 {
5746 template <class U> struct S2 {};
5747 template <class U> struct S2<U*> {};
5750 we will be called with an ARGLIST of `U*', but the
5751 TEMPLATE will be `template <class T> template
5752 <class U> struct S1<T>::S2'. We must fill in the missing
5753 arguments. */
5754 arglist
5755 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
5756 arglist);
5757 arg_depth = TMPL_ARGS_DEPTH (arglist);
5760 /* Now we should have enough arguments. */
5761 gcc_assert (parm_depth == arg_depth);
5763 /* From here on, we're only interested in the most general
5764 template. */
5765 templ = gen_tmpl;
5767 /* Calculate the BOUND_ARGS. These will be the args that are
5768 actually tsubst'd into the definition to create the
5769 instantiation. */
5770 if (parm_depth > 1)
5772 /* We have multiple levels of arguments to coerce, at once. */
5773 int i;
5774 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5776 tree bound_args = make_tree_vec (parm_depth);
5778 for (i = saved_depth,
5779 t = DECL_TEMPLATE_PARMS (templ);
5780 i > 0 && t != NULL_TREE;
5781 --i, t = TREE_CHAIN (t))
5783 tree a = coerce_template_parms (TREE_VALUE (t),
5784 arglist, templ,
5785 complain,
5786 /*require_all_args=*/true,
5787 /*use_default_args=*/true);
5789 /* Don't process further if one of the levels fails. */
5790 if (a == error_mark_node)
5792 /* Restore the ARGLIST to its full size. */
5793 TREE_VEC_LENGTH (arglist) = saved_depth;
5794 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5797 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5799 /* We temporarily reduce the length of the ARGLIST so
5800 that coerce_template_parms will see only the arguments
5801 corresponding to the template parameters it is
5802 examining. */
5803 TREE_VEC_LENGTH (arglist)--;
5806 /* Restore the ARGLIST to its full size. */
5807 TREE_VEC_LENGTH (arglist) = saved_depth;
5809 arglist = bound_args;
5811 else
5812 arglist
5813 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5814 INNERMOST_TEMPLATE_ARGS (arglist),
5815 templ,
5816 complain,
5817 /*require_all_args=*/true,
5818 /*use_default_args=*/true);
5820 if (arglist == error_mark_node)
5821 /* We were unable to bind the arguments. */
5822 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5824 /* In the scope of a template class, explicit references to the
5825 template class refer to the type of the template, not any
5826 instantiation of it. For example, in:
5828 template <class T> class C { void f(C<T>); }
5830 the `C<T>' is just the same as `C'. Outside of the
5831 class, however, such a reference is an instantiation. */
5832 if (comp_template_args (TYPE_TI_ARGS (template_type),
5833 arglist))
5835 found = template_type;
5837 if (!entering_scope && PRIMARY_TEMPLATE_P (templ))
5839 tree ctx;
5841 for (ctx = current_class_type;
5842 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5843 ctx = (TYPE_P (ctx)
5844 ? TYPE_CONTEXT (ctx)
5845 : DECL_CONTEXT (ctx)))
5846 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5847 goto found_ctx;
5849 /* We're not in the scope of the class, so the
5850 TEMPLATE_TYPE is not the type we want after all. */
5851 found = NULL_TREE;
5852 found_ctx:;
5855 if (found)
5856 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5858 /* If we already have this specialization, return it. */
5859 found = retrieve_specialization (templ, arglist,
5860 /*class_specializations_p=*/false);
5861 if (found)
5862 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5864 /* This type is a "partial instantiation" if any of the template
5865 arguments still involve template parameters. Note that we set
5866 IS_PARTIAL_INSTANTIATION for partial specializations as
5867 well. */
5868 is_partial_instantiation = uses_template_parms (arglist);
5870 /* If the deduced arguments are invalid, then the binding
5871 failed. */
5872 if (!is_partial_instantiation
5873 && check_instantiated_args (templ,
5874 INNERMOST_TEMPLATE_ARGS (arglist),
5875 complain))
5876 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5878 if (!is_partial_instantiation
5879 && !PRIMARY_TEMPLATE_P (templ)
5880 && TREE_CODE (CP_DECL_CONTEXT (templ)) == NAMESPACE_DECL)
5882 found = xref_tag_from_type (TREE_TYPE (templ),
5883 DECL_NAME (templ),
5884 /*tag_scope=*/ts_global);
5885 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5888 context = tsubst (DECL_CONTEXT (templ), arglist,
5889 complain, in_decl);
5890 if (!context)
5891 context = global_namespace;
5893 /* Create the type. */
5894 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5896 if (!is_partial_instantiation)
5898 set_current_access_from_decl (TYPE_NAME (template_type));
5899 t = start_enum (TYPE_IDENTIFIER (template_type),
5900 tsubst (ENUM_UNDERLYING_TYPE (template_type),
5901 arglist, complain, in_decl),
5902 SCOPED_ENUM_P (template_type));
5904 else
5906 /* We don't want to call start_enum for this type, since
5907 the values for the enumeration constants may involve
5908 template parameters. And, no one should be interested
5909 in the enumeration constants for such a type. */
5910 t = make_node (ENUMERAL_TYPE);
5911 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
5914 else
5916 t = make_class_type (TREE_CODE (template_type));
5917 CLASSTYPE_DECLARED_CLASS (t)
5918 = CLASSTYPE_DECLARED_CLASS (template_type);
5919 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5920 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5922 /* A local class. Make sure the decl gets registered properly. */
5923 if (context == current_function_decl)
5924 pushtag (DECL_NAME (templ), t, /*tag_scope=*/ts_current);
5926 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5927 /* This instantiation is another name for the primary
5928 template type. Set the TYPE_CANONICAL field
5929 appropriately. */
5930 TYPE_CANONICAL (t) = template_type;
5931 else if (any_template_arguments_need_structural_equality_p (arglist))
5932 /* Some of the template arguments require structural
5933 equality testing, so this template class requires
5934 structural equality testing. */
5935 SET_TYPE_STRUCTURAL_EQUALITY (t);
5938 /* If we called start_enum or pushtag above, this information
5939 will already be set up. */
5940 if (!TYPE_NAME (t))
5942 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5944 type_decl = create_implicit_typedef (DECL_NAME (templ), t);
5945 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5946 TYPE_STUB_DECL (t) = type_decl;
5947 DECL_SOURCE_LOCATION (type_decl)
5948 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5950 else
5951 type_decl = TYPE_NAME (t);
5953 TREE_PRIVATE (type_decl)
5954 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5955 TREE_PROTECTED (type_decl)
5956 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5957 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5959 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5960 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5963 /* Set up the template information. We have to figure out which
5964 template is the immediate parent if this is a full
5965 instantiation. */
5966 if (parm_depth == 1 || is_partial_instantiation
5967 || !PRIMARY_TEMPLATE_P (templ))
5968 /* This case is easy; there are no member templates involved. */
5969 found = templ;
5970 else
5972 /* This is a full instantiation of a member template. Look
5973 for a partial instantiation of which this is an instance. */
5975 for (found = DECL_TEMPLATE_INSTANTIATIONS (templ);
5976 found; found = TREE_CHAIN (found))
5978 int success;
5979 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5981 /* We only want partial instantiations, here, not
5982 specializations or full instantiations. */
5983 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5984 || !uses_template_parms (TREE_VALUE (found)))
5985 continue;
5987 /* Temporarily reduce by one the number of levels in the
5988 ARGLIST and in FOUND so as to avoid comparing the
5989 last set of arguments. */
5990 TREE_VEC_LENGTH (arglist)--;
5991 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5993 /* See if the arguments match. If they do, then TMPL is
5994 the partial instantiation we want. */
5995 success = comp_template_args (TREE_PURPOSE (found), arglist);
5997 /* Restore the argument vectors to their full size. */
5998 TREE_VEC_LENGTH (arglist)++;
5999 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
6001 if (success)
6003 found = tmpl;
6004 break;
6008 if (!found)
6010 /* There was no partial instantiation. This happens
6011 where C<T> is a member template of A<T> and it's used
6012 in something like
6014 template <typename T> struct B { A<T>::C<int> m; };
6015 B<float>;
6017 Create the partial instantiation.
6019 TREE_VEC_LENGTH (arglist)--;
6020 found = tsubst (templ, arglist, complain, NULL_TREE);
6021 TREE_VEC_LENGTH (arglist)++;
6025 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6026 DECL_TEMPLATE_INSTANTIATIONS (templ)
6027 = tree_cons (arglist, t,
6028 DECL_TEMPLATE_INSTANTIATIONS (templ));
6030 if (TREE_CODE (t) == ENUMERAL_TYPE
6031 && !is_partial_instantiation)
6032 /* Now that the type has been registered on the instantiations
6033 list, we set up the enumerators. Because the enumeration
6034 constants may involve the enumeration type itself, we make
6035 sure to register the type first, and then create the
6036 constants. That way, doing tsubst_expr for the enumeration
6037 constants won't result in recursive calls here; we'll find
6038 the instantiation and exit above. */
6039 tsubst_enum (template_type, t, arglist);
6041 if (is_partial_instantiation)
6042 /* If the type makes use of template parameters, the
6043 code that generates debugging information will crash. */
6044 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6046 /* Possibly limit visibility based on template args. */
6047 TREE_PUBLIC (type_decl) = 1;
6048 determine_visibility (type_decl);
6050 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6052 timevar_pop (TV_NAME_LOOKUP);
6055 struct pair_fn_data
6057 tree_fn_t fn;
6058 void *data;
6059 /* True when we should also visit template parameters that occur in
6060 non-deduced contexts. */
6061 bool include_nondeduced_p;
6062 struct pointer_set_t *visited;
6065 /* Called from for_each_template_parm via walk_tree. */
6067 static tree
6068 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6070 tree t = *tp;
6071 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6072 tree_fn_t fn = pfd->fn;
6073 void *data = pfd->data;
6075 if (TYPE_P (t)
6076 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6077 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6078 pfd->include_nondeduced_p))
6079 return error_mark_node;
6081 switch (TREE_CODE (t))
6083 case RECORD_TYPE:
6084 if (TYPE_PTRMEMFUNC_P (t))
6085 break;
6086 /* Fall through. */
6088 case UNION_TYPE:
6089 case ENUMERAL_TYPE:
6090 if (!TYPE_TEMPLATE_INFO (t))
6091 *walk_subtrees = 0;
6092 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6093 fn, data, pfd->visited,
6094 pfd->include_nondeduced_p))
6095 return error_mark_node;
6096 break;
6098 case INTEGER_TYPE:
6099 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6100 fn, data, pfd->visited,
6101 pfd->include_nondeduced_p)
6102 || for_each_template_parm (TYPE_MAX_VALUE (t),
6103 fn, data, pfd->visited,
6104 pfd->include_nondeduced_p))
6105 return error_mark_node;
6106 break;
6108 case METHOD_TYPE:
6109 /* Since we're not going to walk subtrees, we have to do this
6110 explicitly here. */
6111 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6112 pfd->visited, pfd->include_nondeduced_p))
6113 return error_mark_node;
6114 /* Fall through. */
6116 case FUNCTION_TYPE:
6117 /* Check the return type. */
6118 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6119 pfd->include_nondeduced_p))
6120 return error_mark_node;
6122 /* Check the parameter types. Since default arguments are not
6123 instantiated until they are needed, the TYPE_ARG_TYPES may
6124 contain expressions that involve template parameters. But,
6125 no-one should be looking at them yet. And, once they're
6126 instantiated, they don't contain template parameters, so
6127 there's no point in looking at them then, either. */
6129 tree parm;
6131 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6132 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6133 pfd->visited, pfd->include_nondeduced_p))
6134 return error_mark_node;
6136 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6137 want walk_tree walking into them itself. */
6138 *walk_subtrees = 0;
6140 break;
6142 case TYPEOF_TYPE:
6143 if (pfd->include_nondeduced_p
6144 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6145 pfd->visited,
6146 pfd->include_nondeduced_p))
6147 return error_mark_node;
6148 break;
6150 case FUNCTION_DECL:
6151 case VAR_DECL:
6152 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6153 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6154 pfd->visited, pfd->include_nondeduced_p))
6155 return error_mark_node;
6156 /* Fall through. */
6158 case PARM_DECL:
6159 case CONST_DECL:
6160 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6161 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6162 pfd->visited, pfd->include_nondeduced_p))
6163 return error_mark_node;
6164 if (DECL_CONTEXT (t)
6165 && pfd->include_nondeduced_p
6166 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6167 pfd->visited, pfd->include_nondeduced_p))
6168 return error_mark_node;
6169 break;
6171 case BOUND_TEMPLATE_TEMPLATE_PARM:
6172 /* Record template parameters such as `T' inside `TT<T>'. */
6173 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6174 pfd->include_nondeduced_p))
6175 return error_mark_node;
6176 /* Fall through. */
6178 case TEMPLATE_TEMPLATE_PARM:
6179 case TEMPLATE_TYPE_PARM:
6180 case TEMPLATE_PARM_INDEX:
6181 if (fn && (*fn)(t, data))
6182 return error_mark_node;
6183 else if (!fn)
6184 return error_mark_node;
6185 break;
6187 case TEMPLATE_DECL:
6188 /* A template template parameter is encountered. */
6189 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6190 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6191 pfd->include_nondeduced_p))
6192 return error_mark_node;
6194 /* Already substituted template template parameter */
6195 *walk_subtrees = 0;
6196 break;
6198 case TYPENAME_TYPE:
6199 if (!fn
6200 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6201 data, pfd->visited,
6202 pfd->include_nondeduced_p))
6203 return error_mark_node;
6204 break;
6206 case CONSTRUCTOR:
6207 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6208 && pfd->include_nondeduced_p
6209 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6210 (TREE_TYPE (t)), fn, data,
6211 pfd->visited, pfd->include_nondeduced_p))
6212 return error_mark_node;
6213 break;
6215 case INDIRECT_REF:
6216 case COMPONENT_REF:
6217 /* If there's no type, then this thing must be some expression
6218 involving template parameters. */
6219 if (!fn && !TREE_TYPE (t))
6220 return error_mark_node;
6221 break;
6223 case MODOP_EXPR:
6224 case CAST_EXPR:
6225 case REINTERPRET_CAST_EXPR:
6226 case CONST_CAST_EXPR:
6227 case STATIC_CAST_EXPR:
6228 case DYNAMIC_CAST_EXPR:
6229 case ARROW_EXPR:
6230 case DOTSTAR_EXPR:
6231 case TYPEID_EXPR:
6232 case PSEUDO_DTOR_EXPR:
6233 if (!fn)
6234 return error_mark_node;
6235 break;
6237 default:
6238 break;
6241 /* We didn't find any template parameters we liked. */
6242 return NULL_TREE;
6245 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6246 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6247 call FN with the parameter and the DATA.
6248 If FN returns nonzero, the iteration is terminated, and
6249 for_each_template_parm returns 1. Otherwise, the iteration
6250 continues. If FN never returns a nonzero value, the value
6251 returned by for_each_template_parm is 0. If FN is NULL, it is
6252 considered to be the function which always returns 1.
6254 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6255 parameters that occur in non-deduced contexts. When false, only
6256 visits those template parameters that can be deduced. */
6258 static int
6259 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6260 struct pointer_set_t *visited,
6261 bool include_nondeduced_p)
6263 struct pair_fn_data pfd;
6264 int result;
6266 /* Set up. */
6267 pfd.fn = fn;
6268 pfd.data = data;
6269 pfd.include_nondeduced_p = include_nondeduced_p;
6271 /* Walk the tree. (Conceptually, we would like to walk without
6272 duplicates, but for_each_template_parm_r recursively calls
6273 for_each_template_parm, so we would need to reorganize a fair
6274 bit to use walk_tree_without_duplicates, so we keep our own
6275 visited list.) */
6276 if (visited)
6277 pfd.visited = visited;
6278 else
6279 pfd.visited = pointer_set_create ();
6280 result = cp_walk_tree (&t,
6281 for_each_template_parm_r,
6282 &pfd,
6283 pfd.visited) != NULL_TREE;
6285 /* Clean up. */
6286 if (!visited)
6288 pointer_set_destroy (pfd.visited);
6289 pfd.visited = 0;
6292 return result;
6295 /* Returns true if T depends on any template parameter. */
6298 uses_template_parms (tree t)
6300 bool dependent_p;
6301 int saved_processing_template_decl;
6303 saved_processing_template_decl = processing_template_decl;
6304 if (!saved_processing_template_decl)
6305 processing_template_decl = 1;
6306 if (TYPE_P (t))
6307 dependent_p = dependent_type_p (t);
6308 else if (TREE_CODE (t) == TREE_VEC)
6309 dependent_p = any_dependent_template_arguments_p (t);
6310 else if (TREE_CODE (t) == TREE_LIST)
6311 dependent_p = (uses_template_parms (TREE_VALUE (t))
6312 || uses_template_parms (TREE_CHAIN (t)));
6313 else if (TREE_CODE (t) == TYPE_DECL)
6314 dependent_p = dependent_type_p (TREE_TYPE (t));
6315 else if (DECL_P (t)
6316 || EXPR_P (t)
6317 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6318 || TREE_CODE (t) == OVERLOAD
6319 || TREE_CODE (t) == BASELINK
6320 || TREE_CODE (t) == IDENTIFIER_NODE
6321 || TREE_CODE (t) == TRAIT_EXPR
6322 || CONSTANT_CLASS_P (t))
6323 dependent_p = (type_dependent_expression_p (t)
6324 || value_dependent_expression_p (t));
6325 else
6327 gcc_assert (t == error_mark_node);
6328 dependent_p = false;
6331 processing_template_decl = saved_processing_template_decl;
6333 return dependent_p;
6336 /* Returns true if T depends on any template parameter with level LEVEL. */
6339 uses_template_parms_level (tree t, int level)
6341 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6342 /*include_nondeduced_p=*/true);
6345 static int tinst_depth;
6346 extern int max_tinst_depth;
6347 #ifdef GATHER_STATISTICS
6348 int depth_reached;
6349 #endif
6350 static int tinst_level_tick;
6351 static int last_template_error_tick;
6353 /* We're starting to instantiate D; record the template instantiation context
6354 for diagnostics and to restore it later. */
6356 static int
6357 push_tinst_level (tree d)
6359 struct tinst_level *new_level;
6361 if (tinst_depth >= max_tinst_depth)
6363 /* If the instantiation in question still has unbound template parms,
6364 we don't really care if we can't instantiate it, so just return.
6365 This happens with base instantiation for implicit `typename'. */
6366 if (uses_template_parms (d))
6367 return 0;
6369 last_template_error_tick = tinst_level_tick;
6370 error ("template instantiation depth exceeds maximum of %d (use "
6371 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6372 max_tinst_depth, d);
6374 print_instantiation_context ();
6376 return 0;
6379 new_level = GGC_NEW (struct tinst_level);
6380 new_level->decl = d;
6381 new_level->locus = input_location;
6382 new_level->in_system_header_p = in_system_header;
6383 new_level->next = current_tinst_level;
6384 current_tinst_level = new_level;
6386 ++tinst_depth;
6387 #ifdef GATHER_STATISTICS
6388 if (tinst_depth > depth_reached)
6389 depth_reached = tinst_depth;
6390 #endif
6392 ++tinst_level_tick;
6393 return 1;
6396 /* We're done instantiating this template; return to the instantiation
6397 context. */
6399 static void
6400 pop_tinst_level (void)
6402 /* Restore the filename and line number stashed away when we started
6403 this instantiation. */
6404 input_location = current_tinst_level->locus;
6405 current_tinst_level = current_tinst_level->next;
6406 --tinst_depth;
6407 ++tinst_level_tick;
6410 /* We're instantiating a deferred template; restore the template
6411 instantiation context in which the instantiation was requested, which
6412 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6414 static tree
6415 reopen_tinst_level (struct tinst_level *level)
6417 struct tinst_level *t;
6419 tinst_depth = 0;
6420 for (t = level; t; t = t->next)
6421 ++tinst_depth;
6423 current_tinst_level = level;
6424 pop_tinst_level ();
6425 return level->decl;
6428 /* Returns the TINST_LEVEL which gives the original instantiation
6429 context. */
6431 struct tinst_level *
6432 outermost_tinst_level (void)
6434 struct tinst_level *level = current_tinst_level;
6435 if (level)
6436 while (level->next)
6437 level = level->next;
6438 return level;
6441 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6443 bool
6444 parameter_of_template_p (tree parm, tree templ)
6446 tree parms;
6447 int i;
6449 if (!parm || !templ)
6450 return false;
6452 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6453 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6455 parms = DECL_TEMPLATE_PARMS (templ);
6456 parms = INNERMOST_TEMPLATE_PARMS (parms);
6458 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6459 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6460 return true;
6462 return false;
6465 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6466 vector of template arguments, as for tsubst.
6468 Returns an appropriate tsubst'd friend declaration. */
6470 static tree
6471 tsubst_friend_function (tree decl, tree args)
6473 tree new_friend;
6475 if (TREE_CODE (decl) == FUNCTION_DECL
6476 && DECL_TEMPLATE_INSTANTIATION (decl)
6477 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6478 /* This was a friend declared with an explicit template
6479 argument list, e.g.:
6481 friend void f<>(T);
6483 to indicate that f was a template instantiation, not a new
6484 function declaration. Now, we have to figure out what
6485 instantiation of what template. */
6487 tree template_id, arglist, fns;
6488 tree new_args;
6489 tree tmpl;
6490 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6492 /* Friend functions are looked up in the containing namespace scope.
6493 We must enter that scope, to avoid finding member functions of the
6494 current class with same name. */
6495 push_nested_namespace (ns);
6496 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6497 tf_warning_or_error, NULL_TREE,
6498 /*integral_constant_expression_p=*/false);
6499 pop_nested_namespace (ns);
6500 arglist = tsubst (DECL_TI_ARGS (decl), args,
6501 tf_warning_or_error, NULL_TREE);
6502 template_id = lookup_template_function (fns, arglist);
6504 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6505 tmpl = determine_specialization (template_id, new_friend,
6506 &new_args,
6507 /*need_member_template=*/0,
6508 TREE_VEC_LENGTH (args),
6509 tsk_none);
6510 return instantiate_template (tmpl, new_args, tf_error);
6513 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6515 /* The NEW_FRIEND will look like an instantiation, to the
6516 compiler, but is not an instantiation from the point of view of
6517 the language. For example, we might have had:
6519 template <class T> struct S {
6520 template <class U> friend void f(T, U);
6523 Then, in S<int>, template <class U> void f(int, U) is not an
6524 instantiation of anything. */
6525 if (new_friend == error_mark_node)
6526 return error_mark_node;
6528 DECL_USE_TEMPLATE (new_friend) = 0;
6529 if (TREE_CODE (decl) == TEMPLATE_DECL)
6531 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6532 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6533 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6536 /* The mangled name for the NEW_FRIEND is incorrect. The function
6537 is not a template instantiation and should not be mangled like
6538 one. Therefore, we forget the mangling here; we'll recompute it
6539 later if we need it. */
6540 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6542 SET_DECL_RTL (new_friend, NULL_RTX);
6543 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6546 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6548 tree old_decl;
6549 tree new_friend_template_info;
6550 tree new_friend_result_template_info;
6551 tree ns;
6552 int new_friend_is_defn;
6554 /* We must save some information from NEW_FRIEND before calling
6555 duplicate decls since that function will free NEW_FRIEND if
6556 possible. */
6557 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6558 new_friend_is_defn =
6559 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6560 (template_for_substitution (new_friend)))
6561 != NULL_TREE);
6562 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6564 /* This declaration is a `primary' template. */
6565 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6567 new_friend_result_template_info
6568 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6570 else
6571 new_friend_result_template_info = NULL_TREE;
6573 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6574 if (new_friend_is_defn)
6575 DECL_INITIAL (new_friend) = error_mark_node;
6577 /* Inside pushdecl_namespace_level, we will push into the
6578 current namespace. However, the friend function should go
6579 into the namespace of the template. */
6580 ns = decl_namespace_context (new_friend);
6581 push_nested_namespace (ns);
6582 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6583 pop_nested_namespace (ns);
6585 if (old_decl == error_mark_node)
6586 return error_mark_node;
6588 if (old_decl != new_friend)
6590 /* This new friend declaration matched an existing
6591 declaration. For example, given:
6593 template <class T> void f(T);
6594 template <class U> class C {
6595 template <class T> friend void f(T) {}
6598 the friend declaration actually provides the definition
6599 of `f', once C has been instantiated for some type. So,
6600 old_decl will be the out-of-class template declaration,
6601 while new_friend is the in-class definition.
6603 But, if `f' was called before this point, the
6604 instantiation of `f' will have DECL_TI_ARGS corresponding
6605 to `T' but not to `U', references to which might appear
6606 in the definition of `f'. Previously, the most general
6607 template for an instantiation of `f' was the out-of-class
6608 version; now it is the in-class version. Therefore, we
6609 run through all specialization of `f', adding to their
6610 DECL_TI_ARGS appropriately. In particular, they need a
6611 new set of outer arguments, corresponding to the
6612 arguments for this class instantiation.
6614 The same situation can arise with something like this:
6616 friend void f(int);
6617 template <class T> class C {
6618 friend void f(T) {}
6621 when `C<int>' is instantiated. Now, `f(int)' is defined
6622 in the class. */
6624 if (!new_friend_is_defn)
6625 /* On the other hand, if the in-class declaration does
6626 *not* provide a definition, then we don't want to alter
6627 existing definitions. We can just leave everything
6628 alone. */
6630 else
6632 /* Overwrite whatever template info was there before, if
6633 any, with the new template information pertaining to
6634 the declaration. */
6635 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6637 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6638 reregister_specialization (new_friend,
6639 most_general_template (old_decl),
6640 old_decl);
6641 else
6643 tree t;
6644 tree new_friend_args;
6646 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6647 = new_friend_result_template_info;
6649 new_friend_args = TI_ARGS (new_friend_template_info);
6650 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6651 t != NULL_TREE;
6652 t = TREE_CHAIN (t))
6654 tree spec = TREE_VALUE (t);
6656 DECL_TI_ARGS (spec)
6657 = add_outermost_template_args (new_friend_args,
6658 DECL_TI_ARGS (spec));
6661 /* Now, since specializations are always supposed to
6662 hang off of the most general template, we must move
6663 them. */
6664 t = most_general_template (old_decl);
6665 if (t != old_decl)
6667 DECL_TEMPLATE_SPECIALIZATIONS (t)
6668 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6669 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6670 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6675 /* The information from NEW_FRIEND has been merged into OLD_DECL
6676 by duplicate_decls. */
6677 new_friend = old_decl;
6680 else
6682 tree context = DECL_CONTEXT (new_friend);
6683 bool dependent_p;
6685 /* In the code
6686 template <class T> class C {
6687 template <class U> friend void C1<U>::f (); // case 1
6688 friend void C2<T>::f (); // case 2
6690 we only need to make sure CONTEXT is a complete type for
6691 case 2. To distinguish between the two cases, we note that
6692 CONTEXT of case 1 remains dependent type after tsubst while
6693 this isn't true for case 2. */
6694 ++processing_template_decl;
6695 dependent_p = dependent_type_p (context);
6696 --processing_template_decl;
6698 if (!dependent_p
6699 && !complete_type_or_else (context, NULL_TREE))
6700 return error_mark_node;
6702 if (COMPLETE_TYPE_P (context))
6704 /* Check to see that the declaration is really present, and,
6705 possibly obtain an improved declaration. */
6706 tree fn = check_classfn (context,
6707 new_friend, NULL_TREE);
6709 if (fn)
6710 new_friend = fn;
6714 return new_friend;
6717 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6718 template arguments, as for tsubst.
6720 Returns an appropriate tsubst'd friend type or error_mark_node on
6721 failure. */
6723 static tree
6724 tsubst_friend_class (tree friend_tmpl, tree args)
6726 tree friend_type;
6727 tree tmpl;
6728 tree context;
6730 context = DECL_CONTEXT (friend_tmpl);
6732 if (context)
6734 if (TREE_CODE (context) == NAMESPACE_DECL)
6735 push_nested_namespace (context);
6736 else
6737 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6740 /* Look for a class template declaration. We look for hidden names
6741 because two friend declarations of the same template are the
6742 same. For example, in:
6744 struct A {
6745 template <typename> friend class F;
6747 template <typename> struct B {
6748 template <typename> friend class F;
6751 both F templates are the same. */
6752 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6753 /*block_p=*/true, 0,
6754 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6756 /* But, if we don't find one, it might be because we're in a
6757 situation like this:
6759 template <class T>
6760 struct S {
6761 template <class U>
6762 friend struct S;
6765 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6766 for `S<int>', not the TEMPLATE_DECL. */
6767 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6769 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6770 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6773 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6775 /* The friend template has already been declared. Just
6776 check to see that the declarations match, and install any new
6777 default parameters. We must tsubst the default parameters,
6778 of course. We only need the innermost template parameters
6779 because that is all that redeclare_class_template will look
6780 at. */
6781 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6782 > TMPL_ARGS_DEPTH (args))
6784 tree parms;
6785 location_t saved_input_location;
6786 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6787 args, tf_warning_or_error);
6789 saved_input_location = input_location;
6790 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
6791 redeclare_class_template (TREE_TYPE (tmpl), parms);
6792 input_location = saved_input_location;
6796 friend_type = TREE_TYPE (tmpl);
6798 else
6800 /* The friend template has not already been declared. In this
6801 case, the instantiation of the template class will cause the
6802 injection of this template into the global scope. */
6803 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6804 if (tmpl == error_mark_node)
6805 return error_mark_node;
6807 /* The new TMPL is not an instantiation of anything, so we
6808 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6809 the new type because that is supposed to be the corresponding
6810 template decl, i.e., TMPL. */
6811 DECL_USE_TEMPLATE (tmpl) = 0;
6812 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6813 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6814 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6815 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6817 /* Inject this template into the global scope. */
6818 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6821 if (context)
6823 if (TREE_CODE (context) == NAMESPACE_DECL)
6824 pop_nested_namespace (context);
6825 else
6826 pop_nested_class ();
6829 return friend_type;
6832 /* Returns zero if TYPE cannot be completed later due to circularity.
6833 Otherwise returns one. */
6835 static int
6836 can_complete_type_without_circularity (tree type)
6838 if (type == NULL_TREE || type == error_mark_node)
6839 return 0;
6840 else if (COMPLETE_TYPE_P (type))
6841 return 1;
6842 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6843 return can_complete_type_without_circularity (TREE_TYPE (type));
6844 else if (CLASS_TYPE_P (type)
6845 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6846 return 0;
6847 else
6848 return 1;
6851 /* Apply any attributes which had to be deferred until instantiation
6852 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6853 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6855 static void
6856 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
6857 tree args, tsubst_flags_t complain, tree in_decl)
6859 tree last_dep = NULL_TREE;
6860 tree t;
6861 tree *p;
6863 for (t = attributes; t; t = TREE_CHAIN (t))
6864 if (ATTR_IS_DEPENDENT (t))
6866 last_dep = t;
6867 attributes = copy_list (attributes);
6868 break;
6871 if (DECL_P (*decl_p))
6873 if (TREE_TYPE (*decl_p) == error_mark_node)
6874 return;
6875 p = &DECL_ATTRIBUTES (*decl_p);
6877 else
6878 p = &TYPE_ATTRIBUTES (*decl_p);
6880 if (last_dep)
6882 tree late_attrs = NULL_TREE;
6883 tree *q = &late_attrs;
6885 for (*p = attributes; *p; )
6887 t = *p;
6888 if (ATTR_IS_DEPENDENT (t))
6890 *p = TREE_CHAIN (t);
6891 TREE_CHAIN (t) = NULL_TREE;
6892 /* If the first attribute argument is an identifier, don't
6893 pass it through tsubst. Attributes like mode, format,
6894 cleanup and several target specific attributes expect it
6895 unmodified. */
6896 if (TREE_VALUE (t)
6897 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
6898 && TREE_VALUE (TREE_VALUE (t))
6899 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
6900 == IDENTIFIER_NODE))
6902 tree chain
6903 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
6904 in_decl,
6905 /*integral_constant_expression_p=*/false);
6906 if (chain != TREE_CHAIN (TREE_VALUE (t)))
6907 TREE_VALUE (t)
6908 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
6909 chain);
6911 else
6912 TREE_VALUE (t)
6913 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
6914 /*integral_constant_expression_p=*/false);
6915 *q = t;
6916 q = &TREE_CHAIN (t);
6918 else
6919 p = &TREE_CHAIN (t);
6922 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
6926 tree
6927 instantiate_class_template (tree type)
6929 tree templ, args, pattern, t, member;
6930 tree typedecl;
6931 tree pbinfo;
6932 tree base_list;
6934 if (type == error_mark_node)
6935 return error_mark_node;
6937 if (TYPE_BEING_DEFINED (type)
6938 || COMPLETE_TYPE_P (type)
6939 || dependent_type_p (type))
6940 return type;
6942 /* Figure out which template is being instantiated. */
6943 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6944 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6946 /* Determine what specialization of the original template to
6947 instantiate. */
6948 t = most_specialized_class (type, templ);
6949 if (t == error_mark_node)
6951 TYPE_BEING_DEFINED (type) = 1;
6952 return error_mark_node;
6954 else if (t)
6956 /* This TYPE is actually an instantiation of a partial
6957 specialization. We replace the innermost set of ARGS with
6958 the arguments appropriate for substitution. For example,
6959 given:
6961 template <class T> struct S {};
6962 template <class T> struct S<T*> {};
6964 and supposing that we are instantiating S<int*>, ARGS will
6965 presently be {int*} -- but we need {int}. */
6966 pattern = TREE_TYPE (t);
6967 args = TREE_PURPOSE (t);
6969 else
6971 pattern = TREE_TYPE (templ);
6972 args = CLASSTYPE_TI_ARGS (type);
6975 /* If the template we're instantiating is incomplete, then clearly
6976 there's nothing we can do. */
6977 if (!COMPLETE_TYPE_P (pattern))
6978 return type;
6980 /* If we've recursively instantiated too many templates, stop. */
6981 if (! push_tinst_level (type))
6982 return type;
6984 /* Now we're really doing the instantiation. Mark the type as in
6985 the process of being defined. */
6986 TYPE_BEING_DEFINED (type) = 1;
6988 /* We may be in the middle of deferred access check. Disable
6989 it now. */
6990 push_deferring_access_checks (dk_no_deferred);
6992 push_to_top_level ();
6994 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
6996 /* Set the input location to the template definition. This is needed
6997 if tsubsting causes an error. */
6998 typedecl = TYPE_MAIN_DECL (type);
6999 input_location = DECL_SOURCE_LOCATION (typedecl);
7001 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7002 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7003 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7004 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7005 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7006 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7007 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7008 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7009 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7010 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7011 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7012 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7013 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7014 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7015 if (ANON_AGGR_TYPE_P (pattern))
7016 SET_ANON_AGGR_TYPE_P (type);
7017 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7019 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7020 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7023 pbinfo = TYPE_BINFO (pattern);
7025 /* We should never instantiate a nested class before its enclosing
7026 class; we need to look up the nested class by name before we can
7027 instantiate it, and that lookup should instantiate the enclosing
7028 class. */
7029 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7030 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7031 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7033 base_list = NULL_TREE;
7034 if (BINFO_N_BASE_BINFOS (pbinfo))
7036 tree pbase_binfo;
7037 tree context = TYPE_CONTEXT (type);
7038 tree pushed_scope;
7039 int i;
7041 /* We must enter the scope containing the type, as that is where
7042 the accessibility of types named in dependent bases are
7043 looked up from. */
7044 pushed_scope = push_scope (context ? context : global_namespace);
7046 /* Substitute into each of the bases to determine the actual
7047 basetypes. */
7048 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7050 tree base;
7051 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7052 tree expanded_bases = NULL_TREE;
7053 int idx, len = 1;
7055 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7057 expanded_bases =
7058 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7059 args, tf_error, NULL_TREE);
7060 if (expanded_bases == error_mark_node)
7061 continue;
7063 len = TREE_VEC_LENGTH (expanded_bases);
7066 for (idx = 0; idx < len; idx++)
7068 if (expanded_bases)
7069 /* Extract the already-expanded base class. */
7070 base = TREE_VEC_ELT (expanded_bases, idx);
7071 else
7072 /* Substitute to figure out the base class. */
7073 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7074 NULL_TREE);
7076 if (base == error_mark_node)
7077 continue;
7079 base_list = tree_cons (access, base, base_list);
7080 if (BINFO_VIRTUAL_P (pbase_binfo))
7081 TREE_TYPE (base_list) = integer_type_node;
7085 /* The list is now in reverse order; correct that. */
7086 base_list = nreverse (base_list);
7088 if (pushed_scope)
7089 pop_scope (pushed_scope);
7091 /* Now call xref_basetypes to set up all the base-class
7092 information. */
7093 xref_basetypes (type, base_list);
7095 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7096 (int) ATTR_FLAG_TYPE_IN_PLACE,
7097 args, tf_error, NULL_TREE);
7099 /* Now that our base classes are set up, enter the scope of the
7100 class, so that name lookups into base classes, etc. will work
7101 correctly. This is precisely analogous to what we do in
7102 begin_class_definition when defining an ordinary non-template
7103 class, except we also need to push the enclosing classes. */
7104 push_nested_class (type);
7106 /* Now members are processed in the order of declaration. */
7107 for (member = CLASSTYPE_DECL_LIST (pattern);
7108 member; member = TREE_CHAIN (member))
7110 tree t = TREE_VALUE (member);
7112 if (TREE_PURPOSE (member))
7114 if (TYPE_P (t))
7116 /* Build new CLASSTYPE_NESTED_UTDS. */
7118 tree newtag;
7119 bool class_template_p;
7121 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7122 && TYPE_LANG_SPECIFIC (t)
7123 && CLASSTYPE_IS_TEMPLATE (t));
7124 /* If the member is a class template, then -- even after
7125 substitution -- there may be dependent types in the
7126 template argument list for the class. We increment
7127 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7128 that function will assume that no types are dependent
7129 when outside of a template. */
7130 if (class_template_p)
7131 ++processing_template_decl;
7132 newtag = tsubst (t, args, tf_error, NULL_TREE);
7133 if (class_template_p)
7134 --processing_template_decl;
7135 if (newtag == error_mark_node)
7136 continue;
7138 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7140 tree name = TYPE_IDENTIFIER (t);
7142 if (class_template_p)
7143 /* Unfortunately, lookup_template_class sets
7144 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7145 instantiation (i.e., for the type of a member
7146 template class nested within a template class.)
7147 This behavior is required for
7148 maybe_process_partial_specialization to work
7149 correctly, but is not accurate in this case;
7150 the TAG is not an instantiation of anything.
7151 (The corresponding TEMPLATE_DECL is an
7152 instantiation, but the TYPE is not.) */
7153 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7155 /* Now, we call pushtag to put this NEWTAG into the scope of
7156 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7157 pushtag calling push_template_decl. We don't have to do
7158 this for enums because it will already have been done in
7159 tsubst_enum. */
7160 if (name)
7161 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7162 pushtag (name, newtag, /*tag_scope=*/ts_current);
7165 else if (TREE_CODE (t) == FUNCTION_DECL
7166 || DECL_FUNCTION_TEMPLATE_P (t))
7168 /* Build new TYPE_METHODS. */
7169 tree r;
7171 if (TREE_CODE (t) == TEMPLATE_DECL)
7172 ++processing_template_decl;
7173 r = tsubst (t, args, tf_error, NULL_TREE);
7174 if (TREE_CODE (t) == TEMPLATE_DECL)
7175 --processing_template_decl;
7176 set_current_access_from_decl (r);
7177 finish_member_declaration (r);
7179 else
7181 /* Build new TYPE_FIELDS. */
7182 if (TREE_CODE (t) == STATIC_ASSERT)
7184 tree condition =
7185 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7186 tf_warning_or_error, NULL_TREE,
7187 /*integral_constant_expression_p=*/true);
7188 finish_static_assert (condition,
7189 STATIC_ASSERT_MESSAGE (t),
7190 STATIC_ASSERT_SOURCE_LOCATION (t),
7191 /*member_p=*/true);
7193 else if (TREE_CODE (t) != CONST_DECL)
7195 tree r;
7197 /* The file and line for this declaration, to
7198 assist in error message reporting. Since we
7199 called push_tinst_level above, we don't need to
7200 restore these. */
7201 input_location = DECL_SOURCE_LOCATION (t);
7203 if (TREE_CODE (t) == TEMPLATE_DECL)
7204 ++processing_template_decl;
7205 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7206 if (TREE_CODE (t) == TEMPLATE_DECL)
7207 --processing_template_decl;
7208 if (TREE_CODE (r) == VAR_DECL)
7210 /* In [temp.inst]:
7212 [t]he initialization (and any associated
7213 side-effects) of a static data member does
7214 not occur unless the static data member is
7215 itself used in a way that requires the
7216 definition of the static data member to
7217 exist.
7219 Therefore, we do not substitute into the
7220 initialized for the static data member here. */
7221 finish_static_data_member_decl
7223 /*init=*/NULL_TREE,
7224 /*init_const_expr_p=*/false,
7225 /*asmspec_tree=*/NULL_TREE,
7226 /*flags=*/0);
7227 if (DECL_INITIALIZED_IN_CLASS_P (r))
7228 check_static_variable_definition (r, TREE_TYPE (r));
7230 else if (TREE_CODE (r) == FIELD_DECL)
7232 /* Determine whether R has a valid type and can be
7233 completed later. If R is invalid, then it is
7234 replaced by error_mark_node so that it will not be
7235 added to TYPE_FIELDS. */
7236 tree rtype = TREE_TYPE (r);
7237 if (can_complete_type_without_circularity (rtype))
7238 complete_type (rtype);
7240 if (!COMPLETE_TYPE_P (rtype))
7242 cxx_incomplete_type_error (r, rtype);
7243 r = error_mark_node;
7247 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7248 such a thing will already have been added to the field
7249 list by tsubst_enum in finish_member_declaration in the
7250 CLASSTYPE_NESTED_UTDS case above. */
7251 if (!(TREE_CODE (r) == TYPE_DECL
7252 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7253 && DECL_ARTIFICIAL (r)))
7255 set_current_access_from_decl (r);
7256 finish_member_declaration (r);
7261 else
7263 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7265 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7267 tree friend_type = t;
7268 bool adjust_processing_template_decl = false;
7270 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7272 /* template <class T> friend class C; */
7273 friend_type = tsubst_friend_class (friend_type, args);
7274 adjust_processing_template_decl = true;
7276 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7278 /* template <class T> friend class C::D; */
7279 friend_type = tsubst (friend_type, args,
7280 tf_warning_or_error, NULL_TREE);
7281 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7282 friend_type = TREE_TYPE (friend_type);
7283 adjust_processing_template_decl = true;
7285 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7287 /* This could be either
7289 friend class T::C;
7291 when dependent_type_p is false or
7293 template <class U> friend class T::C;
7295 otherwise. */
7296 friend_type = tsubst (friend_type, args,
7297 tf_warning_or_error, NULL_TREE);
7298 /* Bump processing_template_decl for correct
7299 dependent_type_p calculation. */
7300 ++processing_template_decl;
7301 if (dependent_type_p (friend_type))
7302 adjust_processing_template_decl = true;
7303 --processing_template_decl;
7305 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7306 && hidden_name_p (TYPE_NAME (friend_type)))
7308 /* friend class C;
7310 where C hasn't been declared yet. Let's lookup name
7311 from namespace scope directly, bypassing any name that
7312 come from dependent base class. */
7313 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7315 /* The call to xref_tag_from_type does injection for friend
7316 classes. */
7317 push_nested_namespace (ns);
7318 friend_type =
7319 xref_tag_from_type (friend_type, NULL_TREE,
7320 /*tag_scope=*/ts_current);
7321 pop_nested_namespace (ns);
7323 else if (uses_template_parms (friend_type))
7324 /* friend class C<T>; */
7325 friend_type = tsubst (friend_type, args,
7326 tf_warning_or_error, NULL_TREE);
7327 /* Otherwise it's
7329 friend class C;
7331 where C is already declared or
7333 friend class C<int>;
7335 We don't have to do anything in these cases. */
7337 if (adjust_processing_template_decl)
7338 /* Trick make_friend_class into realizing that the friend
7339 we're adding is a template, not an ordinary class. It's
7340 important that we use make_friend_class since it will
7341 perform some error-checking and output cross-reference
7342 information. */
7343 ++processing_template_decl;
7345 if (friend_type != error_mark_node)
7346 make_friend_class (type, friend_type, /*complain=*/false);
7348 if (adjust_processing_template_decl)
7349 --processing_template_decl;
7351 else
7353 /* Build new DECL_FRIENDLIST. */
7354 tree r;
7356 /* The file and line for this declaration, to
7357 assist in error message reporting. Since we
7358 called push_tinst_level above, we don't need to
7359 restore these. */
7360 input_location = DECL_SOURCE_LOCATION (t);
7362 if (TREE_CODE (t) == TEMPLATE_DECL)
7364 ++processing_template_decl;
7365 push_deferring_access_checks (dk_no_check);
7368 r = tsubst_friend_function (t, args);
7369 add_friend (type, r, /*complain=*/false);
7370 if (TREE_CODE (t) == TEMPLATE_DECL)
7372 pop_deferring_access_checks ();
7373 --processing_template_decl;
7379 /* Set the file and line number information to whatever is given for
7380 the class itself. This puts error messages involving generated
7381 implicit functions at a predictable point, and the same point
7382 that would be used for non-template classes. */
7383 input_location = DECL_SOURCE_LOCATION (typedecl);
7385 unreverse_member_declarations (type);
7386 finish_struct_1 (type);
7387 TYPE_BEING_DEFINED (type) = 0;
7389 /* Now that the class is complete, instantiate default arguments for
7390 any member functions. We don't do this earlier because the
7391 default arguments may reference members of the class. */
7392 if (!PRIMARY_TEMPLATE_P (templ))
7393 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7394 if (TREE_CODE (t) == FUNCTION_DECL
7395 /* Implicitly generated member functions will not have template
7396 information; they are not instantiations, but instead are
7397 created "fresh" for each instantiation. */
7398 && DECL_TEMPLATE_INFO (t))
7399 tsubst_default_arguments (t);
7401 pop_nested_class ();
7402 pop_from_top_level ();
7403 pop_deferring_access_checks ();
7404 pop_tinst_level ();
7406 /* The vtable for a template class can be emitted in any translation
7407 unit in which the class is instantiated. When there is no key
7408 method, however, finish_struct_1 will already have added TYPE to
7409 the keyed_classes list. */
7410 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7411 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7413 return type;
7416 static tree
7417 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7419 tree r;
7421 if (!t)
7422 r = t;
7423 else if (TYPE_P (t))
7424 r = tsubst (t, args, complain, in_decl);
7425 else
7427 r = tsubst_expr (t, args, complain, in_decl,
7428 /*integral_constant_expression_p=*/true);
7429 r = fold_non_dependent_expr (r);
7431 return r;
7434 /* Substitute ARGS into T, which is an pack expansion
7435 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7436 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7437 (if only a partial substitution could be performed) or
7438 ERROR_MARK_NODE if there was an error. */
7439 tree
7440 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7441 tree in_decl)
7443 tree pattern;
7444 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7445 tree first_arg_pack; int i, len = -1;
7446 tree result;
7447 int incomplete = 0;
7449 gcc_assert (PACK_EXPANSION_P (t));
7450 pattern = PACK_EXPANSION_PATTERN (t);
7452 /* Determine the argument packs that will instantiate the parameter
7453 packs used in the expansion expression. While we're at it,
7454 compute the number of arguments to be expanded and make sure it
7455 is consistent. */
7456 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7457 pack = TREE_CHAIN (pack))
7459 tree parm_pack = TREE_VALUE (pack);
7460 tree arg_pack = NULL_TREE;
7461 tree orig_arg = NULL_TREE;
7463 if (TREE_CODE (parm_pack) == PARM_DECL)
7464 arg_pack = retrieve_local_specialization (parm_pack);
7465 else
7467 int level, idx, levels;
7468 template_parm_level_and_index (parm_pack, &level, &idx);
7470 levels = TMPL_ARGS_DEPTH (args);
7471 if (level <= levels)
7472 arg_pack = TMPL_ARG (args, level, idx);
7475 orig_arg = arg_pack;
7476 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7477 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7479 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7480 /* This can only happen if we forget to expand an argument
7481 pack somewhere else. Just return an error, silently. */
7483 result = make_tree_vec (1);
7484 TREE_VEC_ELT (result, 0) = error_mark_node;
7485 return result;
7488 if (arg_pack
7489 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7490 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7492 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7493 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7494 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7495 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7496 /* The argument pack that the parameter maps to is just an
7497 expansion of the parameter itself, such as one would
7498 find in the implicit typedef of a class inside the
7499 class itself. Consider this parameter "unsubstituted",
7500 so that we will maintain the outer pack expansion. */
7501 arg_pack = NULL_TREE;
7504 if (arg_pack)
7506 int my_len =
7507 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7509 /* It's all-or-nothing with incomplete argument packs. */
7510 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7511 return error_mark_node;
7513 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7514 incomplete = 1;
7516 if (len < 0)
7518 len = my_len;
7519 first_arg_pack = arg_pack;
7521 else if (len != my_len)
7523 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7524 error ("mismatched argument pack lengths while expanding "
7525 "%<%T%>",
7526 pattern);
7527 else
7528 error ("mismatched argument pack lengths while expanding "
7529 "%<%E%>",
7530 pattern);
7531 return error_mark_node;
7534 /* Keep track of the parameter packs and their corresponding
7535 argument packs. */
7536 packs = tree_cons (parm_pack, arg_pack, packs);
7537 TREE_TYPE (packs) = orig_arg;
7539 else
7540 /* We can't substitute for this parameter pack. */
7541 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7542 TREE_VALUE (pack),
7543 unsubstituted_packs);
7546 /* We cannot expand this expansion expression, because we don't have
7547 all of the argument packs we need. Substitute into the pattern
7548 and return a PACK_EXPANSION_*. The caller will need to deal with
7549 that. */
7550 if (unsubstituted_packs)
7551 return make_pack_expansion (tsubst (pattern, args, complain,
7552 in_decl));
7554 /* We could not find any argument packs that work. */
7555 if (len < 0)
7556 return error_mark_node;
7558 /* For each argument in each argument pack, substitute into the
7559 pattern. */
7560 result = make_tree_vec (len + incomplete);
7561 for (i = 0; i < len + incomplete; ++i)
7563 /* For parameter pack, change the substitution of the parameter
7564 pack to the ith argument in its argument pack, then expand
7565 the pattern. */
7566 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7568 tree parm = TREE_PURPOSE (pack);
7570 if (TREE_CODE (parm) == PARM_DECL)
7572 /* Select the Ith argument from the pack. */
7573 tree arg = make_node (ARGUMENT_PACK_SELECT);
7574 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7575 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7576 mark_used (parm);
7577 register_local_specialization (arg, parm);
7579 else
7581 tree value = parm;
7582 int idx, level;
7583 template_parm_level_and_index (parm, &level, &idx);
7585 if (i < len)
7587 /* Select the Ith argument from the pack. */
7588 value = make_node (ARGUMENT_PACK_SELECT);
7589 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7590 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7593 /* Update the corresponding argument. */
7594 TMPL_ARG (args, level, idx) = value;
7598 /* Substitute into the PATTERN with the altered arguments. */
7599 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7600 TREE_VEC_ELT (result, i) =
7601 tsubst_expr (pattern, args, complain, in_decl,
7602 /*integral_constant_expression_p=*/false);
7603 else
7604 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7606 if (i == len)
7607 /* When we have incomplete argument packs, the last "expanded"
7608 result is itself a pack expansion, which allows us
7609 to deduce more arguments. */
7610 TREE_VEC_ELT (result, i) =
7611 make_pack_expansion (TREE_VEC_ELT (result, i));
7613 if (TREE_VEC_ELT (result, i) == error_mark_node)
7615 result = error_mark_node;
7616 break;
7620 /* Update ARGS to restore the substitution from parameter packs to
7621 their argument packs. */
7622 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7624 tree parm = TREE_PURPOSE (pack);
7626 if (TREE_CODE (parm) == PARM_DECL)
7627 register_local_specialization (TREE_TYPE (pack), parm);
7628 else
7630 int idx, level;
7631 template_parm_level_and_index (parm, &level, &idx);
7633 /* Update the corresponding argument. */
7634 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7635 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7636 TREE_TYPE (pack);
7637 else
7638 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7642 return result;
7645 /* Substitute ARGS into the vector or list of template arguments T. */
7647 static tree
7648 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7650 tree orig_t = t;
7651 int len = TREE_VEC_LENGTH (t);
7652 int need_new = 0, i, expanded_len_adjust = 0, out;
7653 tree *elts = (tree *) alloca (len * sizeof (tree));
7655 for (i = 0; i < len; i++)
7657 tree orig_arg = TREE_VEC_ELT (t, i);
7658 tree new_arg;
7660 if (TREE_CODE (orig_arg) == TREE_VEC)
7661 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7662 else if (PACK_EXPANSION_P (orig_arg))
7664 /* Substitute into an expansion expression. */
7665 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7667 if (TREE_CODE (new_arg) == TREE_VEC)
7668 /* Add to the expanded length adjustment the number of
7669 expanded arguments. We subtract one from this
7670 measurement, because the argument pack expression
7671 itself is already counted as 1 in
7672 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7673 the argument pack is empty. */
7674 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7676 else if (ARGUMENT_PACK_P (orig_arg))
7678 /* Substitute into each of the arguments. */
7679 new_arg = make_node (TREE_CODE (orig_arg));
7681 SET_ARGUMENT_PACK_ARGS (
7682 new_arg,
7683 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7684 args, complain, in_decl));
7686 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7687 new_arg = error_mark_node;
7689 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7690 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7691 complain, in_decl);
7692 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7694 if (TREE_TYPE (new_arg) == error_mark_node)
7695 new_arg = error_mark_node;
7698 else
7699 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7701 if (new_arg == error_mark_node)
7702 return error_mark_node;
7704 elts[i] = new_arg;
7705 if (new_arg != orig_arg)
7706 need_new = 1;
7709 if (!need_new)
7710 return t;
7712 /* Make space for the expanded arguments coming from template
7713 argument packs. */
7714 t = make_tree_vec (len + expanded_len_adjust);
7715 for (i = 0, out = 0; i < len; i++)
7717 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7718 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7719 && TREE_CODE (elts[i]) == TREE_VEC)
7721 int idx;
7723 /* Now expand the template argument pack "in place". */
7724 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7725 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7727 else
7729 TREE_VEC_ELT (t, out) = elts[i];
7730 out++;
7734 return t;
7737 /* Return the result of substituting ARGS into the template parameters
7738 given by PARMS. If there are m levels of ARGS and m + n levels of
7739 PARMS, then the result will contain n levels of PARMS. For
7740 example, if PARMS is `template <class T> template <class U>
7741 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7742 result will be `template <int*, double, class V>'. */
7744 static tree
7745 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7747 tree r = NULL_TREE;
7748 tree* new_parms;
7750 /* When substituting into a template, we must set
7751 PROCESSING_TEMPLATE_DECL as the template parameters may be
7752 dependent if they are based on one-another, and the dependency
7753 predicates are short-circuit outside of templates. */
7754 ++processing_template_decl;
7756 for (new_parms = &r;
7757 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7758 new_parms = &(TREE_CHAIN (*new_parms)),
7759 parms = TREE_CHAIN (parms))
7761 tree new_vec =
7762 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7763 int i;
7765 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7767 tree tuple;
7768 tree default_value;
7769 tree parm_decl;
7771 if (parms == error_mark_node)
7772 continue;
7774 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7776 if (tuple == error_mark_node)
7777 continue;
7779 default_value = TREE_PURPOSE (tuple);
7780 parm_decl = TREE_VALUE (tuple);
7782 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7783 if (TREE_CODE (parm_decl) == PARM_DECL
7784 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7785 parm_decl = error_mark_node;
7786 default_value = tsubst_template_arg (default_value, args,
7787 complain, NULL_TREE);
7789 tuple = build_tree_list (default_value, parm_decl);
7790 TREE_VEC_ELT (new_vec, i) = tuple;
7793 *new_parms =
7794 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7795 - TMPL_ARGS_DEPTH (args)),
7796 new_vec, NULL_TREE);
7799 --processing_template_decl;
7801 return r;
7804 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7805 type T. If T is not an aggregate or enumeration type, it is
7806 handled as if by tsubst. IN_DECL is as for tsubst. If
7807 ENTERING_SCOPE is nonzero, T is the context for a template which
7808 we are presently tsubst'ing. Return the substituted value. */
7810 static tree
7811 tsubst_aggr_type (tree t,
7812 tree args,
7813 tsubst_flags_t complain,
7814 tree in_decl,
7815 int entering_scope)
7817 if (t == NULL_TREE)
7818 return NULL_TREE;
7820 switch (TREE_CODE (t))
7822 case RECORD_TYPE:
7823 if (TYPE_PTRMEMFUNC_P (t))
7824 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7826 /* Else fall through. */
7827 case ENUMERAL_TYPE:
7828 case UNION_TYPE:
7829 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
7831 tree argvec;
7832 tree context;
7833 tree r;
7834 bool saved_skip_evaluation;
7836 /* In "sizeof(X<I>)" we need to evaluate "I". */
7837 saved_skip_evaluation = skip_evaluation;
7838 skip_evaluation = false;
7840 /* First, determine the context for the type we are looking
7841 up. */
7842 context = TYPE_CONTEXT (t);
7843 if (context)
7845 context = tsubst_aggr_type (context, args, complain,
7846 in_decl, /*entering_scope=*/1);
7847 /* If context is a nested class inside a class template,
7848 it may still need to be instantiated (c++/33959). */
7849 if (TYPE_P (context))
7850 context = complete_type (context);
7853 /* Then, figure out what arguments are appropriate for the
7854 type we are trying to find. For example, given:
7856 template <class T> struct S;
7857 template <class T, class U> void f(T, U) { S<U> su; }
7859 and supposing that we are instantiating f<int, double>,
7860 then our ARGS will be {int, double}, but, when looking up
7861 S we only want {double}. */
7862 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7863 complain, in_decl);
7864 if (argvec == error_mark_node)
7865 r = error_mark_node;
7866 else
7868 r = lookup_template_class (t, argvec, in_decl, context,
7869 entering_scope, complain);
7870 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7873 skip_evaluation = saved_skip_evaluation;
7875 return r;
7877 else
7878 /* This is not a template type, so there's nothing to do. */
7879 return t;
7881 default:
7882 return tsubst (t, args, complain, in_decl);
7886 /* Substitute into the default argument ARG (a default argument for
7887 FN), which has the indicated TYPE. */
7889 tree
7890 tsubst_default_argument (tree fn, tree type, tree arg)
7892 tree saved_class_ptr = NULL_TREE;
7893 tree saved_class_ref = NULL_TREE;
7895 /* This default argument came from a template. Instantiate the
7896 default argument here, not in tsubst. In the case of
7897 something like:
7899 template <class T>
7900 struct S {
7901 static T t();
7902 void f(T = t());
7905 we must be careful to do name lookup in the scope of S<T>,
7906 rather than in the current class. */
7907 push_access_scope (fn);
7908 /* The "this" pointer is not valid in a default argument. */
7909 if (cfun)
7911 saved_class_ptr = current_class_ptr;
7912 cp_function_chain->x_current_class_ptr = NULL_TREE;
7913 saved_class_ref = current_class_ref;
7914 cp_function_chain->x_current_class_ref = NULL_TREE;
7917 push_deferring_access_checks(dk_no_deferred);
7918 /* The default argument expression may cause implicitly defined
7919 member functions to be synthesized, which will result in garbage
7920 collection. We must treat this situation as if we were within
7921 the body of function so as to avoid collecting live data on the
7922 stack. */
7923 ++function_depth;
7924 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
7925 tf_warning_or_error, NULL_TREE,
7926 /*integral_constant_expression_p=*/false);
7927 --function_depth;
7928 pop_deferring_access_checks();
7930 /* Restore the "this" pointer. */
7931 if (cfun)
7933 cp_function_chain->x_current_class_ptr = saved_class_ptr;
7934 cp_function_chain->x_current_class_ref = saved_class_ref;
7937 pop_access_scope (fn);
7939 /* Make sure the default argument is reasonable. */
7940 arg = check_default_argument (type, arg);
7942 return arg;
7945 /* Substitute into all the default arguments for FN. */
7947 static void
7948 tsubst_default_arguments (tree fn)
7950 tree arg;
7951 tree tmpl_args;
7953 tmpl_args = DECL_TI_ARGS (fn);
7955 /* If this function is not yet instantiated, we certainly don't need
7956 its default arguments. */
7957 if (uses_template_parms (tmpl_args))
7958 return;
7960 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
7961 arg;
7962 arg = TREE_CHAIN (arg))
7963 if (TREE_PURPOSE (arg))
7964 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
7965 TREE_VALUE (arg),
7966 TREE_PURPOSE (arg));
7969 /* Substitute the ARGS into the T, which is a _DECL. Return the
7970 result of the substitution. Issue error and warning messages under
7971 control of COMPLAIN. */
7973 static tree
7974 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
7976 location_t saved_loc;
7977 tree r = NULL_TREE;
7978 tree in_decl = t;
7980 /* Set the filename and linenumber to improve error-reporting. */
7981 saved_loc = input_location;
7982 input_location = DECL_SOURCE_LOCATION (t);
7984 switch (TREE_CODE (t))
7986 case TEMPLATE_DECL:
7988 /* We can get here when processing a member function template,
7989 member class template, and template template parameter of
7990 a template class. */
7991 tree decl = DECL_TEMPLATE_RESULT (t);
7992 tree spec;
7993 tree tmpl_args;
7994 tree full_args;
7996 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7998 /* Template template parameter is treated here. */
7999 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8000 if (new_type == error_mark_node)
8001 return error_mark_node;
8003 r = copy_decl (t);
8004 TREE_CHAIN (r) = NULL_TREE;
8005 TREE_TYPE (r) = new_type;
8006 DECL_TEMPLATE_RESULT (r)
8007 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
8008 DECL_TEMPLATE_PARMS (r)
8009 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8010 complain);
8011 TYPE_NAME (new_type) = r;
8012 break;
8015 /* We might already have an instance of this template.
8016 The ARGS are for the surrounding class type, so the
8017 full args contain the tsubst'd args for the context,
8018 plus the innermost args from the template decl. */
8019 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8020 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8021 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8022 /* Because this is a template, the arguments will still be
8023 dependent, even after substitution. If
8024 PROCESSING_TEMPLATE_DECL is not set, the dependency
8025 predicates will short-circuit. */
8026 ++processing_template_decl;
8027 full_args = tsubst_template_args (tmpl_args, args,
8028 complain, in_decl);
8029 --processing_template_decl;
8030 if (full_args == error_mark_node)
8031 return error_mark_node;
8033 /* tsubst_template_args doesn't copy the vector if
8034 nothing changed. But, *something* should have
8035 changed. */
8036 gcc_assert (full_args != tmpl_args);
8038 spec = retrieve_specialization (t, full_args,
8039 /*class_specializations_p=*/true);
8040 if (spec != NULL_TREE)
8042 r = spec;
8043 break;
8046 /* Make a new template decl. It will be similar to the
8047 original, but will record the current template arguments.
8048 We also create a new function declaration, which is just
8049 like the old one, but points to this new template, rather
8050 than the old one. */
8051 r = copy_decl (t);
8052 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8053 TREE_CHAIN (r) = NULL_TREE;
8055 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8057 if (TREE_CODE (decl) == TYPE_DECL)
8059 tree new_type;
8060 ++processing_template_decl;
8061 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8062 --processing_template_decl;
8063 if (new_type == error_mark_node)
8064 return error_mark_node;
8066 TREE_TYPE (r) = new_type;
8067 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8068 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8069 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8070 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8072 else
8074 tree new_decl;
8075 ++processing_template_decl;
8076 new_decl = tsubst (decl, args, complain, in_decl);
8077 --processing_template_decl;
8078 if (new_decl == error_mark_node)
8079 return error_mark_node;
8081 DECL_TEMPLATE_RESULT (r) = new_decl;
8082 DECL_TI_TEMPLATE (new_decl) = r;
8083 TREE_TYPE (r) = TREE_TYPE (new_decl);
8084 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8085 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8088 SET_DECL_IMPLICIT_INSTANTIATION (r);
8089 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8090 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8092 /* The template parameters for this new template are all the
8093 template parameters for the old template, except the
8094 outermost level of parameters. */
8095 DECL_TEMPLATE_PARMS (r)
8096 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8097 complain);
8099 if (PRIMARY_TEMPLATE_P (t))
8100 DECL_PRIMARY_TEMPLATE (r) = r;
8102 if (TREE_CODE (decl) != TYPE_DECL)
8103 /* Record this non-type partial instantiation. */
8104 register_specialization (r, t,
8105 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8106 false);
8108 break;
8110 case FUNCTION_DECL:
8112 tree ctx;
8113 tree argvec = NULL_TREE;
8114 tree *friends;
8115 tree gen_tmpl;
8116 tree type;
8117 int member;
8118 int args_depth;
8119 int parms_depth;
8121 /* Nobody should be tsubst'ing into non-template functions. */
8122 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8124 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8126 tree spec;
8127 bool dependent_p;
8129 /* If T is not dependent, just return it. We have to
8130 increment PROCESSING_TEMPLATE_DECL because
8131 value_dependent_expression_p assumes that nothing is
8132 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8133 ++processing_template_decl;
8134 dependent_p = value_dependent_expression_p (t);
8135 --processing_template_decl;
8136 if (!dependent_p)
8137 return t;
8139 /* Calculate the most general template of which R is a
8140 specialization, and the complete set of arguments used to
8141 specialize R. */
8142 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8143 argvec = tsubst_template_args (DECL_TI_ARGS
8144 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8145 args, complain, in_decl);
8147 /* Check to see if we already have this specialization. */
8148 spec = retrieve_specialization (gen_tmpl, argvec,
8149 /*class_specializations_p=*/false);
8151 if (spec)
8153 r = spec;
8154 break;
8157 /* We can see more levels of arguments than parameters if
8158 there was a specialization of a member template, like
8159 this:
8161 template <class T> struct S { template <class U> void f(); }
8162 template <> template <class U> void S<int>::f(U);
8164 Here, we'll be substituting into the specialization,
8165 because that's where we can find the code we actually
8166 want to generate, but we'll have enough arguments for
8167 the most general template.
8169 We also deal with the peculiar case:
8171 template <class T> struct S {
8172 template <class U> friend void f();
8174 template <class U> void f() {}
8175 template S<int>;
8176 template void f<double>();
8178 Here, the ARGS for the instantiation of will be {int,
8179 double}. But, we only need as many ARGS as there are
8180 levels of template parameters in CODE_PATTERN. We are
8181 careful not to get fooled into reducing the ARGS in
8182 situations like:
8184 template <class T> struct S { template <class U> void f(U); }
8185 template <class T> template <> void S<T>::f(int) {}
8187 which we can spot because the pattern will be a
8188 specialization in this case. */
8189 args_depth = TMPL_ARGS_DEPTH (args);
8190 parms_depth =
8191 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8192 if (args_depth > parms_depth
8193 && !DECL_TEMPLATE_SPECIALIZATION (t))
8194 args = get_innermost_template_args (args, parms_depth);
8196 else
8198 /* This special case arises when we have something like this:
8200 template <class T> struct S {
8201 friend void f<int>(int, double);
8204 Here, the DECL_TI_TEMPLATE for the friend declaration
8205 will be an IDENTIFIER_NODE. We are being called from
8206 tsubst_friend_function, and we want only to create a
8207 new decl (R) with appropriate types so that we can call
8208 determine_specialization. */
8209 gen_tmpl = NULL_TREE;
8212 if (DECL_CLASS_SCOPE_P (t))
8214 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8215 member = 2;
8216 else
8217 member = 1;
8218 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8219 complain, t, /*entering_scope=*/1);
8221 else
8223 member = 0;
8224 ctx = DECL_CONTEXT (t);
8226 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8227 if (type == error_mark_node)
8228 return error_mark_node;
8230 /* We do NOT check for matching decls pushed separately at this
8231 point, as they may not represent instantiations of this
8232 template, and in any case are considered separate under the
8233 discrete model. */
8234 r = copy_decl (t);
8235 DECL_USE_TEMPLATE (r) = 0;
8236 TREE_TYPE (r) = type;
8237 /* Clear out the mangled name and RTL for the instantiation. */
8238 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8239 SET_DECL_RTL (r, NULL_RTX);
8240 /* Leave DECL_INITIAL set on deleted instantiations. */
8241 if (!DECL_DELETED_FN (r))
8242 DECL_INITIAL (r) = NULL_TREE;
8243 DECL_CONTEXT (r) = ctx;
8245 if (member && DECL_CONV_FN_P (r))
8246 /* Type-conversion operator. Reconstruct the name, in
8247 case it's the name of one of the template's parameters. */
8248 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8250 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8251 complain, t);
8252 DECL_RESULT (r) = NULL_TREE;
8254 TREE_STATIC (r) = 0;
8255 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8256 DECL_EXTERNAL (r) = 1;
8257 /* If this is an instantiation of a function with internal
8258 linkage, we already know what object file linkage will be
8259 assigned to the instantiation. */
8260 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8261 DECL_DEFER_OUTPUT (r) = 0;
8262 TREE_CHAIN (r) = NULL_TREE;
8263 DECL_PENDING_INLINE_INFO (r) = 0;
8264 DECL_PENDING_INLINE_P (r) = 0;
8265 DECL_SAVED_TREE (r) = NULL_TREE;
8266 DECL_STRUCT_FUNCTION (r) = NULL;
8267 TREE_USED (r) = 0;
8268 if (DECL_CLONED_FUNCTION (r))
8270 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
8271 args, complain, t);
8272 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
8273 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
8276 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8277 this in the special friend case mentioned above where
8278 GEN_TMPL is NULL. */
8279 if (gen_tmpl)
8281 DECL_TEMPLATE_INFO (r)
8282 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8283 SET_DECL_IMPLICIT_INSTANTIATION (r);
8284 register_specialization (r, gen_tmpl, argvec, false);
8286 /* We're not supposed to instantiate default arguments
8287 until they are called, for a template. But, for a
8288 declaration like:
8290 template <class T> void f ()
8291 { extern void g(int i = T()); }
8293 we should do the substitution when the template is
8294 instantiated. We handle the member function case in
8295 instantiate_class_template since the default arguments
8296 might refer to other members of the class. */
8297 if (!member
8298 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8299 && !uses_template_parms (argvec))
8300 tsubst_default_arguments (r);
8302 else
8303 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8305 /* Copy the list of befriending classes. */
8306 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8307 *friends;
8308 friends = &TREE_CHAIN (*friends))
8310 *friends = copy_node (*friends);
8311 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8312 args, complain,
8313 in_decl);
8316 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8318 maybe_retrofit_in_chrg (r);
8319 if (DECL_CONSTRUCTOR_P (r))
8320 grok_ctor_properties (ctx, r);
8321 /* If this is an instantiation of a member template, clone it.
8322 If it isn't, that'll be handled by
8323 clone_constructors_and_destructors. */
8324 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8325 clone_function_decl (r, /*update_method_vec_p=*/0);
8327 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8328 && !grok_op_properties (r, (complain & tf_error) != 0))
8329 return error_mark_node;
8331 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8332 SET_DECL_FRIEND_CONTEXT (r,
8333 tsubst (DECL_FRIEND_CONTEXT (t),
8334 args, complain, in_decl));
8336 /* Possibly limit visibility based on template args. */
8337 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8338 if (DECL_VISIBILITY_SPECIFIED (t))
8340 DECL_VISIBILITY_SPECIFIED (r) = 0;
8341 DECL_ATTRIBUTES (r)
8342 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8344 determine_visibility (r);
8346 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8347 args, complain, in_decl);
8349 break;
8351 case PARM_DECL:
8353 tree type = NULL_TREE;
8354 int i, len = 1;
8355 tree expanded_types = NULL_TREE;
8356 tree prev_r = NULL_TREE;
8357 tree first_r = NULL_TREE;
8359 if (FUNCTION_PARAMETER_PACK_P (t))
8361 /* If there is a local specialization that isn't a
8362 parameter pack, it means that we're doing a "simple"
8363 substitution from inside tsubst_pack_expansion. Just
8364 return the local specialization (which will be a single
8365 parm). */
8366 tree spec = retrieve_local_specialization (t);
8367 if (spec
8368 && TREE_CODE (spec) == PARM_DECL
8369 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8370 return spec;
8372 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8373 the parameters in this function parameter pack. */
8374 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8375 complain, in_decl);
8376 if (TREE_CODE (expanded_types) == TREE_VEC)
8378 len = TREE_VEC_LENGTH (expanded_types);
8380 /* Zero-length parameter packs are boring. Just substitute
8381 into the chain. */
8382 if (len == 0)
8383 return tsubst (TREE_CHAIN (t), args, complain,
8384 TREE_CHAIN (t));
8386 else
8388 /* All we did was update the type. Make a note of that. */
8389 type = expanded_types;
8390 expanded_types = NULL_TREE;
8394 /* Loop through all of the parameter's we'll build. When T is
8395 a function parameter pack, LEN is the number of expanded
8396 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8397 r = NULL_TREE;
8398 for (i = 0; i < len; ++i)
8400 prev_r = r;
8401 r = copy_node (t);
8402 if (DECL_TEMPLATE_PARM_P (t))
8403 SET_DECL_TEMPLATE_PARM_P (r);
8405 if (expanded_types)
8406 /* We're on the Ith parameter of the function parameter
8407 pack. */
8409 /* Get the Ith type. */
8410 type = TREE_VEC_ELT (expanded_types, i);
8412 if (DECL_NAME (r))
8413 /* Rename the parameter to include the index. */
8414 DECL_NAME (r) =
8415 make_ith_pack_parameter_name (DECL_NAME (r), i);
8417 else if (!type)
8418 /* We're dealing with a normal parameter. */
8419 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8421 type = type_decays_to (type);
8422 TREE_TYPE (r) = type;
8423 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8425 if (DECL_INITIAL (r))
8427 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8428 DECL_INITIAL (r) = TREE_TYPE (r);
8429 else
8430 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8431 complain, in_decl);
8434 DECL_CONTEXT (r) = NULL_TREE;
8436 if (!DECL_TEMPLATE_PARM_P (r))
8437 DECL_ARG_TYPE (r) = type_passed_as (type);
8439 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8440 args, complain, in_decl);
8442 /* Keep track of the first new parameter we
8443 generate. That's what will be returned to the
8444 caller. */
8445 if (!first_r)
8446 first_r = r;
8448 /* Build a proper chain of parameters when substituting
8449 into a function parameter pack. */
8450 if (prev_r)
8451 TREE_CHAIN (prev_r) = r;
8454 if (TREE_CHAIN (t))
8455 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8456 complain, TREE_CHAIN (t));
8458 /* FIRST_R contains the start of the chain we've built. */
8459 r = first_r;
8461 break;
8463 case FIELD_DECL:
8465 tree type;
8467 r = copy_decl (t);
8468 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8469 if (type == error_mark_node)
8470 return error_mark_node;
8471 TREE_TYPE (r) = type;
8472 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8474 /* DECL_INITIAL gives the number of bits in a bit-field. */
8475 DECL_INITIAL (r)
8476 = tsubst_expr (DECL_INITIAL (t), args,
8477 complain, in_decl,
8478 /*integral_constant_expression_p=*/true);
8479 /* We don't have to set DECL_CONTEXT here; it is set by
8480 finish_member_declaration. */
8481 TREE_CHAIN (r) = NULL_TREE;
8482 if (VOID_TYPE_P (type))
8483 error ("instantiation of %q+D as type %qT", r, type);
8485 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8486 args, complain, in_decl);
8488 break;
8490 case USING_DECL:
8491 /* We reach here only for member using decls. */
8492 if (DECL_DEPENDENT_P (t))
8494 r = do_class_using_decl
8495 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8496 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8497 if (!r)
8498 r = error_mark_node;
8499 else
8501 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8502 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8505 else
8507 r = copy_node (t);
8508 TREE_CHAIN (r) = NULL_TREE;
8510 break;
8512 case TYPE_DECL:
8513 case VAR_DECL:
8515 tree argvec = NULL_TREE;
8516 tree gen_tmpl = NULL_TREE;
8517 tree spec;
8518 tree tmpl = NULL_TREE;
8519 tree ctx;
8520 tree type = NULL_TREE;
8521 bool local_p;
8523 if (TREE_CODE (t) == TYPE_DECL
8524 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8526 /* If this is the canonical decl, we don't have to
8527 mess with instantiations, and often we can't (for
8528 typename, template type parms and such). Note that
8529 TYPE_NAME is not correct for the above test if
8530 we've copied the type for a typedef. */
8531 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8532 if (type == error_mark_node)
8533 return error_mark_node;
8534 r = TYPE_NAME (type);
8535 break;
8538 /* Check to see if we already have the specialization we
8539 need. */
8540 spec = NULL_TREE;
8541 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8543 /* T is a static data member or namespace-scope entity.
8544 We have to substitute into namespace-scope variables
8545 (even though such entities are never templates) because
8546 of cases like:
8548 template <class T> void f() { extern T t; }
8550 where the entity referenced is not known until
8551 instantiation time. */
8552 local_p = false;
8553 ctx = DECL_CONTEXT (t);
8554 if (DECL_CLASS_SCOPE_P (t))
8556 ctx = tsubst_aggr_type (ctx, args,
8557 complain,
8558 in_decl, /*entering_scope=*/1);
8559 /* If CTX is unchanged, then T is in fact the
8560 specialization we want. That situation occurs when
8561 referencing a static data member within in its own
8562 class. We can use pointer equality, rather than
8563 same_type_p, because DECL_CONTEXT is always
8564 canonical. */
8565 if (ctx == DECL_CONTEXT (t))
8566 spec = t;
8569 if (!spec)
8571 tmpl = DECL_TI_TEMPLATE (t);
8572 gen_tmpl = most_general_template (tmpl);
8573 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8574 spec = (retrieve_specialization
8575 (gen_tmpl, argvec,
8576 /*class_specializations_p=*/false));
8579 else
8581 /* A local variable. */
8582 local_p = true;
8583 /* Subsequent calls to pushdecl will fill this in. */
8584 ctx = NULL_TREE;
8585 spec = retrieve_local_specialization (t);
8587 /* If we already have the specialization we need, there is
8588 nothing more to do. */
8589 if (spec)
8591 r = spec;
8592 break;
8595 /* Create a new node for the specialization we need. */
8596 r = copy_decl (t);
8597 if (type == NULL_TREE)
8598 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8599 if (TREE_CODE (r) == VAR_DECL)
8601 /* Even if the original location is out of scope, the
8602 newly substituted one is not. */
8603 DECL_DEAD_FOR_LOCAL (r) = 0;
8604 DECL_INITIALIZED_P (r) = 0;
8605 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8606 if (type == error_mark_node)
8607 return error_mark_node;
8608 if (TREE_CODE (type) == FUNCTION_TYPE)
8610 /* It may seem that this case cannot occur, since:
8612 typedef void f();
8613 void g() { f x; }
8615 declares a function, not a variable. However:
8617 typedef void f();
8618 template <typename T> void g() { T t; }
8619 template void g<f>();
8621 is an attempt to declare a variable with function
8622 type. */
8623 error ("variable %qD has function type",
8624 /* R is not yet sufficiently initialized, so we
8625 just use its name. */
8626 DECL_NAME (r));
8627 return error_mark_node;
8629 type = complete_type (type);
8630 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8631 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8632 type = check_var_type (DECL_NAME (r), type);
8634 if (DECL_HAS_VALUE_EXPR_P (t))
8636 tree ve = DECL_VALUE_EXPR (t);
8637 ve = tsubst_expr (ve, args, complain, in_decl,
8638 /*constant_expression_p=*/false);
8639 SET_DECL_VALUE_EXPR (r, ve);
8642 else if (DECL_SELF_REFERENCE_P (t))
8643 SET_DECL_SELF_REFERENCE_P (r);
8644 TREE_TYPE (r) = type;
8645 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8646 DECL_CONTEXT (r) = ctx;
8647 /* Clear out the mangled name and RTL for the instantiation. */
8648 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8649 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8650 SET_DECL_RTL (r, NULL_RTX);
8651 /* The initializer must not be expanded until it is required;
8652 see [temp.inst]. */
8653 DECL_INITIAL (r) = NULL_TREE;
8654 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8655 SET_DECL_RTL (r, NULL_RTX);
8656 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8657 if (TREE_CODE (r) == VAR_DECL)
8659 /* Possibly limit visibility based on template args. */
8660 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8661 if (DECL_VISIBILITY_SPECIFIED (t))
8663 DECL_VISIBILITY_SPECIFIED (r) = 0;
8664 DECL_ATTRIBUTES (r)
8665 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8667 determine_visibility (r);
8669 /* Preserve a typedef that names a type. */
8670 else if (TREE_CODE (r) == TYPE_DECL
8671 && DECL_ORIGINAL_TYPE (t)
8672 && type != error_mark_node)
8674 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
8675 args, complain, in_decl);
8676 TREE_TYPE (r) = type = build_variant_type_copy (type);
8677 TYPE_NAME (type) = r;
8680 if (!local_p)
8682 /* A static data member declaration is always marked
8683 external when it is declared in-class, even if an
8684 initializer is present. We mimic the non-template
8685 processing here. */
8686 DECL_EXTERNAL (r) = 1;
8688 register_specialization (r, gen_tmpl, argvec, false);
8689 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8690 SET_DECL_IMPLICIT_INSTANTIATION (r);
8692 else
8693 register_local_specialization (r, t);
8695 TREE_CHAIN (r) = NULL_TREE;
8697 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
8698 (int) ATTR_FLAG_TYPE_IN_PLACE,
8699 args, complain, in_decl);
8700 layout_decl (r, 0);
8702 break;
8704 default:
8705 gcc_unreachable ();
8708 /* Restore the file and line information. */
8709 input_location = saved_loc;
8711 return r;
8714 /* Substitute into the ARG_TYPES of a function type. */
8716 static tree
8717 tsubst_arg_types (tree arg_types,
8718 tree args,
8719 tsubst_flags_t complain,
8720 tree in_decl)
8722 tree remaining_arg_types;
8723 tree type = NULL_TREE;
8724 int i = 1;
8725 tree expanded_args = NULL_TREE;
8726 tree default_arg;
8728 if (!arg_types || arg_types == void_list_node)
8729 return arg_types;
8731 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8732 args, complain, in_decl);
8733 if (remaining_arg_types == error_mark_node)
8734 return error_mark_node;
8736 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8738 /* For a pack expansion, perform substitution on the
8739 entire expression. Later on, we'll handle the arguments
8740 one-by-one. */
8741 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8742 args, complain, in_decl);
8744 if (TREE_CODE (expanded_args) == TREE_VEC)
8745 /* So that we'll spin through the parameters, one by one. */
8746 i = TREE_VEC_LENGTH (expanded_args);
8747 else
8749 /* We only partially substituted into the parameter
8750 pack. Our type is TYPE_PACK_EXPANSION. */
8751 type = expanded_args;
8752 expanded_args = NULL_TREE;
8756 while (i > 0) {
8757 --i;
8759 if (expanded_args)
8760 type = TREE_VEC_ELT (expanded_args, i);
8761 else if (!type)
8762 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8764 if (type == error_mark_node)
8765 return error_mark_node;
8766 if (VOID_TYPE_P (type))
8768 if (complain & tf_error)
8770 error ("invalid parameter type %qT", type);
8771 if (in_decl)
8772 error ("in declaration %q+D", in_decl);
8774 return error_mark_node;
8777 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8778 top-level qualifiers as required. */
8779 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8781 /* We do not substitute into default arguments here. The standard
8782 mandates that they be instantiated only when needed, which is
8783 done in build_over_call. */
8784 default_arg = TREE_PURPOSE (arg_types);
8786 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8788 /* We've instantiated a template before its default arguments
8789 have been parsed. This can happen for a nested template
8790 class, and is not an error unless we require the default
8791 argument in a call of this function. */
8792 remaining_arg_types =
8793 tree_cons (default_arg, type, remaining_arg_types);
8794 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8795 remaining_arg_types);
8797 else
8798 remaining_arg_types =
8799 hash_tree_cons (default_arg, type, remaining_arg_types);
8802 return remaining_arg_types;
8805 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8806 *not* handle the exception-specification for FNTYPE, because the
8807 initial substitution of explicitly provided template parameters
8808 during argument deduction forbids substitution into the
8809 exception-specification:
8811 [temp.deduct]
8813 All references in the function type of the function template to the
8814 corresponding template parameters are replaced by the specified tem-
8815 plate argument values. If a substitution in a template parameter or
8816 in the function type of the function template results in an invalid
8817 type, type deduction fails. [Note: The equivalent substitution in
8818 exception specifications is done only when the function is instanti-
8819 ated, at which point a program is ill-formed if the substitution
8820 results in an invalid type.] */
8822 static tree
8823 tsubst_function_type (tree t,
8824 tree args,
8825 tsubst_flags_t complain,
8826 tree in_decl)
8828 tree return_type;
8829 tree arg_types;
8830 tree fntype;
8832 /* The TYPE_CONTEXT is not used for function/method types. */
8833 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8835 /* Substitute the return type. */
8836 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8837 if (return_type == error_mark_node)
8838 return error_mark_node;
8839 /* The standard does not presently indicate that creation of a
8840 function type with an invalid return type is a deduction failure.
8841 However, that is clearly analogous to creating an array of "void"
8842 or a reference to a reference. This is core issue #486. */
8843 if (TREE_CODE (return_type) == ARRAY_TYPE
8844 || TREE_CODE (return_type) == FUNCTION_TYPE)
8846 if (complain & tf_error)
8848 if (TREE_CODE (return_type) == ARRAY_TYPE)
8849 error ("function returning an array");
8850 else
8851 error ("function returning a function");
8853 return error_mark_node;
8856 /* Substitute the argument types. */
8857 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8858 complain, in_decl);
8859 if (arg_types == error_mark_node)
8860 return error_mark_node;
8862 /* Construct a new type node and return it. */
8863 if (TREE_CODE (t) == FUNCTION_TYPE)
8864 fntype = build_function_type (return_type, arg_types);
8865 else
8867 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8868 if (! MAYBE_CLASS_TYPE_P (r))
8870 /* [temp.deduct]
8872 Type deduction may fail for any of the following
8873 reasons:
8875 -- Attempting to create "pointer to member of T" when T
8876 is not a class type. */
8877 if (complain & tf_error)
8878 error ("creating pointer to member function of non-class type %qT",
8880 return error_mark_node;
8883 fntype = build_method_type_directly (r, return_type,
8884 TREE_CHAIN (arg_types));
8886 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8887 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8889 return fntype;
8892 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8893 ARGS into that specification, and return the substituted
8894 specification. If there is no specification, return NULL_TREE. */
8896 static tree
8897 tsubst_exception_specification (tree fntype,
8898 tree args,
8899 tsubst_flags_t complain,
8900 tree in_decl)
8902 tree specs;
8903 tree new_specs;
8905 specs = TYPE_RAISES_EXCEPTIONS (fntype);
8906 new_specs = NULL_TREE;
8907 if (specs)
8909 if (! TREE_VALUE (specs))
8910 new_specs = specs;
8911 else
8912 while (specs)
8914 tree spec;
8915 int i, len = 1;
8916 tree expanded_specs = NULL_TREE;
8918 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
8920 /* Expand the pack expansion type. */
8921 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
8922 args, complain,
8923 in_decl);
8925 if (expanded_specs == error_mark_node)
8926 return error_mark_node;
8927 else if (TREE_CODE (expanded_specs) == TREE_VEC)
8928 len = TREE_VEC_LENGTH (expanded_specs);
8929 else
8931 /* We're substituting into a member template, so
8932 we got a TYPE_PACK_EXPANSION back. Add that
8933 expansion and move on. */
8934 gcc_assert (TREE_CODE (expanded_specs)
8935 == TYPE_PACK_EXPANSION);
8936 new_specs = add_exception_specifier (new_specs,
8937 expanded_specs,
8938 complain);
8939 specs = TREE_CHAIN (specs);
8940 continue;
8944 for (i = 0; i < len; ++i)
8946 if (expanded_specs)
8947 spec = TREE_VEC_ELT (expanded_specs, i);
8948 else
8949 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
8950 if (spec == error_mark_node)
8951 return spec;
8952 new_specs = add_exception_specifier (new_specs, spec,
8953 complain);
8956 specs = TREE_CHAIN (specs);
8959 return new_specs;
8962 /* Take the tree structure T and replace template parameters used
8963 therein with the argument vector ARGS. IN_DECL is an associated
8964 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8965 Issue error and warning messages under control of COMPLAIN. Note
8966 that we must be relatively non-tolerant of extensions here, in
8967 order to preserve conformance; if we allow substitutions that
8968 should not be allowed, we may allow argument deductions that should
8969 not succeed, and therefore report ambiguous overload situations
8970 where there are none. In theory, we could allow the substitution,
8971 but indicate that it should have failed, and allow our caller to
8972 make sure that the right thing happens, but we don't try to do this
8973 yet.
8975 This function is used for dealing with types, decls and the like;
8976 for expressions, use tsubst_expr or tsubst_copy. */
8978 static tree
8979 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8981 tree type, r;
8983 if (t == NULL_TREE || t == error_mark_node
8984 || t == integer_type_node
8985 || t == void_type_node
8986 || t == char_type_node
8987 || t == unknown_type_node
8988 || TREE_CODE (t) == NAMESPACE_DECL)
8989 return t;
8991 if (DECL_P (t))
8992 return tsubst_decl (t, args, complain);
8994 if (args == NULL_TREE)
8995 return t;
8997 if (TREE_CODE (t) == IDENTIFIER_NODE)
8998 type = IDENTIFIER_TYPE_VALUE (t);
8999 else
9000 type = TREE_TYPE (t);
9002 gcc_assert (type != unknown_type_node);
9004 /* Reuse typedefs. We need to do this to handle dependent attributes,
9005 such as attribute aligned. */
9006 if (TYPE_P (t)
9007 && TYPE_NAME (t)
9008 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9010 tree decl = TYPE_NAME (t);
9012 if (DECL_CLASS_SCOPE_P (decl)
9013 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9014 && uses_template_parms (DECL_CONTEXT (decl)))
9016 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9017 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9018 r = retrieve_specialization (tmpl, gen_args, false);
9020 else if (DECL_FUNCTION_SCOPE_P (decl)
9021 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9022 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9023 r = retrieve_local_specialization (decl);
9024 else
9025 /* The typedef is from a non-template context. */
9026 return t;
9028 if (r)
9030 r = TREE_TYPE (r);
9031 r = cp_build_qualified_type_real
9032 (r, cp_type_quals (t) | cp_type_quals (r),
9033 complain | tf_ignore_bad_quals);
9034 return r;
9036 /* Else we must be instantiating the typedef, so fall through. */
9039 if (type
9040 && TREE_CODE (t) != TYPENAME_TYPE
9041 && TREE_CODE (t) != IDENTIFIER_NODE
9042 && TREE_CODE (t) != FUNCTION_TYPE
9043 && TREE_CODE (t) != METHOD_TYPE)
9044 type = tsubst (type, args, complain, in_decl);
9045 if (type == error_mark_node)
9046 return error_mark_node;
9048 switch (TREE_CODE (t))
9050 case RECORD_TYPE:
9051 case UNION_TYPE:
9052 case ENUMERAL_TYPE:
9053 return tsubst_aggr_type (t, args, complain, in_decl,
9054 /*entering_scope=*/0);
9056 case ERROR_MARK:
9057 case IDENTIFIER_NODE:
9058 case VOID_TYPE:
9059 case REAL_TYPE:
9060 case COMPLEX_TYPE:
9061 case VECTOR_TYPE:
9062 case BOOLEAN_TYPE:
9063 case INTEGER_CST:
9064 case REAL_CST:
9065 case STRING_CST:
9066 return t;
9068 case INTEGER_TYPE:
9069 if (t == integer_type_node)
9070 return t;
9072 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9073 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9074 return t;
9077 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9079 max = tsubst_expr (omax, args, complain, in_decl,
9080 /*integral_constant_expression_p=*/false);
9081 max = fold_decl_constant_value (max);
9083 if (TREE_CODE (max) != INTEGER_CST
9084 && !at_function_scope_p ()
9085 && !value_dependent_expression_p (max))
9087 if (complain & tf_error)
9088 error ("array bound is not an integer constant");
9089 return error_mark_node;
9092 /* [temp.deduct]
9094 Type deduction may fail for any of the following
9095 reasons:
9097 Attempting to create an array with a size that is
9098 zero or negative. */
9099 if (integer_zerop (max) && !(complain & tf_error))
9100 /* We must fail if performing argument deduction (as
9101 indicated by the state of complain), so that
9102 another substitution can be found. */
9103 return error_mark_node;
9104 else if (TREE_CODE (max) == INTEGER_CST
9105 && INT_CST_LT (max, integer_zero_node))
9107 if (complain & tf_error)
9108 error ("creating array with negative size (%qE)", max);
9110 return error_mark_node;
9113 return compute_array_index_type (NULL_TREE, max);
9116 case TEMPLATE_TYPE_PARM:
9117 case TEMPLATE_TEMPLATE_PARM:
9118 case BOUND_TEMPLATE_TEMPLATE_PARM:
9119 case TEMPLATE_PARM_INDEX:
9121 int idx;
9122 int level;
9123 int levels;
9124 tree arg = NULL_TREE;
9126 r = NULL_TREE;
9128 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9129 template_parm_level_and_index (t, &level, &idx);
9131 levels = TMPL_ARGS_DEPTH (args);
9132 if (level <= levels)
9134 arg = TMPL_ARG (args, level, idx);
9136 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9137 /* See through ARGUMENT_PACK_SELECT arguments. */
9138 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9141 if (arg == error_mark_node)
9142 return error_mark_node;
9143 else if (arg != NULL_TREE)
9145 if (ARGUMENT_PACK_P (arg))
9146 /* If ARG is an argument pack, we don't actually want to
9147 perform a substitution here, because substitutions
9148 for argument packs are only done
9149 element-by-element. We can get to this point when
9150 substituting the type of a non-type template
9151 parameter pack, when that type actually contains
9152 template parameter packs from an outer template, e.g.,
9154 template<typename... Types> struct A {
9155 template<Types... Values> struct B { };
9156 }; */
9157 return t;
9159 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9161 int quals;
9162 gcc_assert (TYPE_P (arg));
9164 /* cv-quals from the template are discarded when
9165 substituting in a function or reference type. */
9166 if (TREE_CODE (arg) == FUNCTION_TYPE
9167 || TREE_CODE (arg) == METHOD_TYPE
9168 || TREE_CODE (arg) == REFERENCE_TYPE)
9169 quals = cp_type_quals (arg);
9170 else
9171 quals = cp_type_quals (arg) | cp_type_quals (t);
9173 return cp_build_qualified_type_real
9174 (arg, quals, complain | tf_ignore_bad_quals);
9176 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9178 /* We are processing a type constructed from a
9179 template template parameter. */
9180 tree argvec = tsubst (TYPE_TI_ARGS (t),
9181 args, complain, in_decl);
9182 if (argvec == error_mark_node)
9183 return error_mark_node;
9185 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9186 are resolving nested-types in the signature of a
9187 member function templates. Otherwise ARG is a
9188 TEMPLATE_DECL and is the real template to be
9189 instantiated. */
9190 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9191 arg = TYPE_NAME (arg);
9193 r = lookup_template_class (arg,
9194 argvec, in_decl,
9195 DECL_CONTEXT (arg),
9196 /*entering_scope=*/0,
9197 complain);
9198 return cp_build_qualified_type_real
9199 (r, TYPE_QUALS (t), complain);
9201 else
9202 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9203 return arg;
9206 if (level == 1)
9207 /* This can happen during the attempted tsubst'ing in
9208 unify. This means that we don't yet have any information
9209 about the template parameter in question. */
9210 return t;
9212 /* If we get here, we must have been looking at a parm for a
9213 more deeply nested template. Make a new version of this
9214 template parameter, but with a lower level. */
9215 switch (TREE_CODE (t))
9217 case TEMPLATE_TYPE_PARM:
9218 case TEMPLATE_TEMPLATE_PARM:
9219 case BOUND_TEMPLATE_TEMPLATE_PARM:
9220 if (cp_type_quals (t))
9222 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9223 r = cp_build_qualified_type_real
9224 (r, cp_type_quals (t),
9225 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9226 ? tf_ignore_bad_quals : 0));
9228 else
9230 r = copy_type (t);
9231 TEMPLATE_TYPE_PARM_INDEX (r)
9232 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9233 r, levels, args, complain);
9234 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9235 TYPE_MAIN_VARIANT (r) = r;
9236 TYPE_POINTER_TO (r) = NULL_TREE;
9237 TYPE_REFERENCE_TO (r) = NULL_TREE;
9239 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9240 /* We have reduced the level of the template
9241 template parameter, but not the levels of its
9242 template parameters, so canonical_type_parameter
9243 will not be able to find the canonical template
9244 template parameter for this level. Thus, we
9245 require structural equality checking to compare
9246 TEMPLATE_TEMPLATE_PARMs. */
9247 SET_TYPE_STRUCTURAL_EQUALITY (r);
9248 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9249 SET_TYPE_STRUCTURAL_EQUALITY (r);
9250 else
9251 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9253 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9255 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9256 complain, in_decl);
9257 if (argvec == error_mark_node)
9258 return error_mark_node;
9260 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9261 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9264 break;
9266 case TEMPLATE_PARM_INDEX:
9267 r = reduce_template_parm_level (t, type, levels, args, complain);
9268 break;
9270 default:
9271 gcc_unreachable ();
9274 return r;
9277 case TREE_LIST:
9279 tree purpose, value, chain;
9281 if (t == void_list_node)
9282 return t;
9284 purpose = TREE_PURPOSE (t);
9285 if (purpose)
9287 purpose = tsubst (purpose, args, complain, in_decl);
9288 if (purpose == error_mark_node)
9289 return error_mark_node;
9291 value = TREE_VALUE (t);
9292 if (value)
9294 value = tsubst (value, args, complain, in_decl);
9295 if (value == error_mark_node)
9296 return error_mark_node;
9298 chain = TREE_CHAIN (t);
9299 if (chain && chain != void_type_node)
9301 chain = tsubst (chain, args, complain, in_decl);
9302 if (chain == error_mark_node)
9303 return error_mark_node;
9305 if (purpose == TREE_PURPOSE (t)
9306 && value == TREE_VALUE (t)
9307 && chain == TREE_CHAIN (t))
9308 return t;
9309 return hash_tree_cons (purpose, value, chain);
9312 case TREE_BINFO:
9313 /* We should never be tsubsting a binfo. */
9314 gcc_unreachable ();
9316 case TREE_VEC:
9317 /* A vector of template arguments. */
9318 gcc_assert (!type);
9319 return tsubst_template_args (t, args, complain, in_decl);
9321 case POINTER_TYPE:
9322 case REFERENCE_TYPE:
9324 enum tree_code code;
9326 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9327 return t;
9329 code = TREE_CODE (t);
9332 /* [temp.deduct]
9334 Type deduction may fail for any of the following
9335 reasons:
9337 -- Attempting to create a pointer to reference type.
9338 -- Attempting to create a reference to a reference type or
9339 a reference to void.
9341 Core issue 106 says that creating a reference to a reference
9342 during instantiation is no longer a cause for failure. We
9343 only enforce this check in strict C++98 mode. */
9344 if ((TREE_CODE (type) == REFERENCE_TYPE
9345 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9346 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9348 static location_t last_loc;
9350 /* We keep track of the last time we issued this error
9351 message to avoid spewing a ton of messages during a
9352 single bad template instantiation. */
9353 if (complain & tf_error
9354 && last_loc != input_location)
9356 if (TREE_CODE (type) == VOID_TYPE)
9357 error ("forming reference to void");
9358 else
9359 error ("forming %s to reference type %qT",
9360 (code == POINTER_TYPE) ? "pointer" : "reference",
9361 type);
9362 last_loc = input_location;
9365 return error_mark_node;
9367 else if (code == POINTER_TYPE)
9369 r = build_pointer_type (type);
9370 if (TREE_CODE (type) == METHOD_TYPE)
9371 r = build_ptrmemfunc_type (r);
9373 else if (TREE_CODE (type) == REFERENCE_TYPE)
9374 /* In C++0x, during template argument substitution, when there is an
9375 attempt to create a reference to a reference type, reference
9376 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9378 "If a template-argument for a template-parameter T names a type
9379 that is a reference to a type A, an attempt to create the type
9380 'lvalue reference to cv T' creates the type 'lvalue reference to
9381 A,' while an attempt to create the type type rvalue reference to
9382 cv T' creates the type T"
9384 r = cp_build_reference_type
9385 (TREE_TYPE (type),
9386 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9387 else
9388 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9389 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
9391 if (r != error_mark_node)
9392 /* Will this ever be needed for TYPE_..._TO values? */
9393 layout_type (r);
9395 return r;
9397 case OFFSET_TYPE:
9399 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9400 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
9402 /* [temp.deduct]
9404 Type deduction may fail for any of the following
9405 reasons:
9407 -- Attempting to create "pointer to member of T" when T
9408 is not a class type. */
9409 if (complain & tf_error)
9410 error ("creating pointer to member of non-class type %qT", r);
9411 return error_mark_node;
9413 if (TREE_CODE (type) == REFERENCE_TYPE)
9415 if (complain & tf_error)
9416 error ("creating pointer to member reference type %qT", type);
9417 return error_mark_node;
9419 if (TREE_CODE (type) == VOID_TYPE)
9421 if (complain & tf_error)
9422 error ("creating pointer to member of type void");
9423 return error_mark_node;
9425 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9426 if (TREE_CODE (type) == FUNCTION_TYPE)
9428 /* The type of the implicit object parameter gets its
9429 cv-qualifiers from the FUNCTION_TYPE. */
9430 tree method_type;
9431 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9432 cp_type_quals (type));
9433 tree memptr;
9434 method_type = build_method_type_directly (this_type,
9435 TREE_TYPE (type),
9436 TYPE_ARG_TYPES (type));
9437 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9438 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9439 complain);
9441 else
9442 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9443 TYPE_QUALS (t),
9444 complain);
9446 case FUNCTION_TYPE:
9447 case METHOD_TYPE:
9449 tree fntype;
9450 tree specs;
9451 fntype = tsubst_function_type (t, args, complain, in_decl);
9452 if (fntype == error_mark_node)
9453 return error_mark_node;
9455 /* Substitute the exception specification. */
9456 specs = tsubst_exception_specification (t, args, complain,
9457 in_decl);
9458 if (specs == error_mark_node)
9459 return error_mark_node;
9460 if (specs)
9461 fntype = build_exception_variant (fntype, specs);
9462 return fntype;
9464 case ARRAY_TYPE:
9466 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9467 if (domain == error_mark_node)
9468 return error_mark_node;
9470 /* As an optimization, we avoid regenerating the array type if
9471 it will obviously be the same as T. */
9472 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9473 return t;
9475 /* These checks should match the ones in grokdeclarator.
9477 [temp.deduct]
9479 The deduction may fail for any of the following reasons:
9481 -- Attempting to create an array with an element type that
9482 is void, a function type, or a reference type, or [DR337]
9483 an abstract class type. */
9484 if (TREE_CODE (type) == VOID_TYPE
9485 || TREE_CODE (type) == FUNCTION_TYPE
9486 || TREE_CODE (type) == REFERENCE_TYPE)
9488 if (complain & tf_error)
9489 error ("creating array of %qT", type);
9490 return error_mark_node;
9492 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
9494 if (complain & tf_error)
9495 error ("creating array of %qT, which is an abstract class type",
9496 type);
9497 return error_mark_node;
9500 r = build_cplus_array_type (type, domain);
9502 if (TYPE_USER_ALIGN (t))
9504 TYPE_ALIGN (r) = TYPE_ALIGN (t);
9505 TYPE_USER_ALIGN (r) = 1;
9508 return r;
9511 case PLUS_EXPR:
9512 case MINUS_EXPR:
9514 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9515 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9517 if (e1 == error_mark_node || e2 == error_mark_node)
9518 return error_mark_node;
9520 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
9523 case NEGATE_EXPR:
9524 case NOP_EXPR:
9526 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9527 if (e == error_mark_node)
9528 return error_mark_node;
9530 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
9533 case TYPENAME_TYPE:
9535 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9536 in_decl, /*entering_scope=*/1);
9537 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
9538 complain, in_decl);
9540 if (ctx == error_mark_node || f == error_mark_node)
9541 return error_mark_node;
9543 if (!MAYBE_CLASS_TYPE_P (ctx))
9545 if (complain & tf_error)
9546 error ("%qT is not a class, struct, or union type", ctx);
9547 return error_mark_node;
9549 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9551 /* Normally, make_typename_type does not require that the CTX
9552 have complete type in order to allow things like:
9554 template <class T> struct S { typename S<T>::X Y; };
9556 But, such constructs have already been resolved by this
9557 point, so here CTX really should have complete type, unless
9558 it's a partial instantiation. */
9559 ctx = complete_type (ctx);
9560 if (!COMPLETE_TYPE_P (ctx))
9562 if (complain & tf_error)
9563 cxx_incomplete_type_error (NULL_TREE, ctx);
9564 return error_mark_node;
9568 f = make_typename_type (ctx, f, typename_type,
9569 (complain & tf_error) | tf_keep_type_decl);
9570 if (f == error_mark_node)
9571 return f;
9572 if (TREE_CODE (f) == TYPE_DECL)
9574 complain |= tf_ignore_bad_quals;
9575 f = TREE_TYPE (f);
9578 if (TREE_CODE (f) != TYPENAME_TYPE)
9580 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9581 error ("%qT resolves to %qT, which is not an enumeration type",
9582 t, f);
9583 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9584 error ("%qT resolves to %qT, which is is not a class type",
9585 t, f);
9588 return cp_build_qualified_type_real
9589 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9592 case UNBOUND_CLASS_TEMPLATE:
9594 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9595 in_decl, /*entering_scope=*/1);
9596 tree name = TYPE_IDENTIFIER (t);
9597 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9599 if (ctx == error_mark_node || name == error_mark_node)
9600 return error_mark_node;
9602 if (parm_list)
9603 parm_list = tsubst_template_parms (parm_list, args, complain);
9604 return make_unbound_class_template (ctx, name, parm_list, complain);
9607 case INDIRECT_REF:
9608 case ADDR_EXPR:
9609 case CALL_EXPR:
9610 gcc_unreachable ();
9612 case ARRAY_REF:
9614 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9615 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9616 /*integral_constant_expression_p=*/false);
9617 if (e1 == error_mark_node || e2 == error_mark_node)
9618 return error_mark_node;
9620 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9623 case SCOPE_REF:
9625 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9626 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9627 if (e1 == error_mark_node || e2 == error_mark_node)
9628 return error_mark_node;
9630 return build_qualified_name (/*type=*/NULL_TREE,
9631 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9634 case TYPEOF_TYPE:
9636 tree type;
9638 type = finish_typeof (tsubst_expr
9639 (TYPEOF_TYPE_EXPR (t), args,
9640 complain, in_decl,
9641 /*integral_constant_expression_p=*/false));
9642 return cp_build_qualified_type_real (type,
9643 cp_type_quals (t)
9644 | cp_type_quals (type),
9645 complain);
9648 case DECLTYPE_TYPE:
9650 tree type;
9652 ++skip_evaluation;
9654 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
9655 complain, in_decl,
9656 /*integral_constant_expression_p=*/false);
9658 --skip_evaluation;
9660 type =
9661 finish_decltype_type (type,
9662 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9663 return cp_build_qualified_type_real (type,
9664 cp_type_quals (t)
9665 | cp_type_quals (type),
9666 complain);
9669 case TYPE_ARGUMENT_PACK:
9670 case NONTYPE_ARGUMENT_PACK:
9672 tree r = make_node (TREE_CODE (t));
9673 tree packed_out =
9674 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9675 args,
9676 complain,
9677 in_decl);
9678 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9680 /* For template nontype argument packs, also substitute into
9681 the type. */
9682 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9683 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9685 return r;
9687 break;
9689 default:
9690 sorry ("use of %qs in template",
9691 tree_code_name [(int) TREE_CODE (t)]);
9692 return error_mark_node;
9696 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9697 type of the expression on the left-hand side of the "." or "->"
9698 operator. */
9700 static tree
9701 tsubst_baselink (tree baselink, tree object_type,
9702 tree args, tsubst_flags_t complain, tree in_decl)
9704 tree name;
9705 tree qualifying_scope;
9706 tree fns;
9707 tree optype;
9708 tree template_args = 0;
9709 bool template_id_p = false;
9711 /* A baselink indicates a function from a base class. Both the
9712 BASELINK_ACCESS_BINFO and the base class referenced may
9713 indicate bases of the template class, rather than the
9714 instantiated class. In addition, lookups that were not
9715 ambiguous before may be ambiguous now. Therefore, we perform
9716 the lookup again. */
9717 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9718 qualifying_scope = tsubst (qualifying_scope, args,
9719 complain, in_decl);
9720 fns = BASELINK_FUNCTIONS (baselink);
9721 optype = BASELINK_OPTYPE (baselink);
9722 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9724 template_id_p = true;
9725 template_args = TREE_OPERAND (fns, 1);
9726 fns = TREE_OPERAND (fns, 0);
9727 if (template_args)
9728 template_args = tsubst_template_args (template_args, args,
9729 complain, in_decl);
9731 name = DECL_NAME (get_first_fn (fns));
9732 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9734 /* If lookup found a single function, mark it as used at this
9735 point. (If it lookup found multiple functions the one selected
9736 later by overload resolution will be marked as used at that
9737 point.) */
9738 if (BASELINK_P (baselink))
9739 fns = BASELINK_FUNCTIONS (baselink);
9740 if (!template_id_p && !really_overloaded_fn (fns))
9741 mark_used (OVL_CURRENT (fns));
9743 /* Add back the template arguments, if present. */
9744 if (BASELINK_P (baselink) && template_id_p)
9745 BASELINK_FUNCTIONS (baselink)
9746 = build_nt (TEMPLATE_ID_EXPR,
9747 BASELINK_FUNCTIONS (baselink),
9748 template_args);
9749 /* Update the conversion operator type. */
9750 BASELINK_OPTYPE (baselink)
9751 = tsubst (optype, args, complain, in_decl);
9753 if (!object_type)
9754 object_type = current_class_type;
9755 return adjust_result_of_qualified_name_lookup (baselink,
9756 qualifying_scope,
9757 object_type);
9760 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9761 true if the qualified-id will be a postfix-expression in-and-of
9762 itself; false if more of the postfix-expression follows the
9763 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9764 of "&". */
9766 static tree
9767 tsubst_qualified_id (tree qualified_id, tree args,
9768 tsubst_flags_t complain, tree in_decl,
9769 bool done, bool address_p)
9771 tree expr;
9772 tree scope;
9773 tree name;
9774 bool is_template;
9775 tree template_args;
9777 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9779 /* Figure out what name to look up. */
9780 name = TREE_OPERAND (qualified_id, 1);
9781 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9783 is_template = true;
9784 template_args = TREE_OPERAND (name, 1);
9785 if (template_args)
9786 template_args = tsubst_template_args (template_args, args,
9787 complain, in_decl);
9788 name = TREE_OPERAND (name, 0);
9790 else
9792 is_template = false;
9793 template_args = NULL_TREE;
9796 /* Substitute into the qualifying scope. When there are no ARGS, we
9797 are just trying to simplify a non-dependent expression. In that
9798 case the qualifying scope may be dependent, and, in any case,
9799 substituting will not help. */
9800 scope = TREE_OPERAND (qualified_id, 0);
9801 if (args)
9803 scope = tsubst (scope, args, complain, in_decl);
9804 expr = tsubst_copy (name, args, complain, in_decl);
9806 else
9807 expr = name;
9809 if (dependent_type_p (scope))
9810 return build_qualified_name (/*type=*/NULL_TREE,
9811 scope, expr,
9812 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9814 if (!BASELINK_P (name) && !DECL_P (expr))
9816 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9817 /* If this were actually a destructor call, it would have been
9818 parsed as such by the parser. */
9819 expr = error_mark_node;
9820 else
9821 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9822 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9823 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9825 if (complain & tf_error)
9827 error ("dependent-name %qE is parsed as a non-type, but "
9828 "instantiation yields a type", qualified_id);
9829 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
9831 return error_mark_node;
9835 if (DECL_P (expr))
9837 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9838 scope);
9839 /* Remember that there was a reference to this entity. */
9840 mark_used (expr);
9843 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9845 if (complain & tf_error)
9846 qualified_name_lookup_error (scope,
9847 TREE_OPERAND (qualified_id, 1),
9848 expr, input_location);
9849 return error_mark_node;
9852 if (is_template)
9853 expr = lookup_template_function (expr, template_args);
9855 if (expr == error_mark_node && complain & tf_error)
9856 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9857 expr, input_location);
9858 else if (TYPE_P (scope))
9860 expr = (adjust_result_of_qualified_name_lookup
9861 (expr, scope, current_class_type));
9862 expr = (finish_qualified_id_expr
9863 (scope, expr, done, address_p,
9864 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9865 /*template_arg_p=*/false));
9868 /* Expressions do not generally have reference type. */
9869 if (TREE_CODE (expr) != SCOPE_REF
9870 /* However, if we're about to form a pointer-to-member, we just
9871 want the referenced member referenced. */
9872 && TREE_CODE (expr) != OFFSET_REF)
9873 expr = convert_from_reference (expr);
9875 return expr;
9878 /* Like tsubst, but deals with expressions. This function just replaces
9879 template parms; to finish processing the resultant expression, use
9880 tsubst_expr. */
9882 static tree
9883 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9885 enum tree_code code;
9886 tree r;
9888 if (t == NULL_TREE || t == error_mark_node)
9889 return t;
9891 code = TREE_CODE (t);
9893 switch (code)
9895 case PARM_DECL:
9896 r = retrieve_local_specialization (t);
9898 if (r == NULL)
9900 /* This can happen for a parameter name used later in a function
9901 declaration (such as in a late-specified return type).
9902 Replace it with an arbitrary expression with the same type
9903 (*(T*)0). This should only occur in an unevaluated context
9904 (i.e. decltype). */
9905 gcc_assert (skip_evaluation);
9906 r = non_reference (TREE_TYPE (t));
9907 r = tsubst (r, args, complain, in_decl);
9908 r = build_pointer_type (r);
9909 r = build_c_cast (r, null_node);
9910 return cp_build_indirect_ref (r, NULL, tf_warning_or_error);
9913 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
9914 r = ARGUMENT_PACK_SELECT_ARG (r);
9915 mark_used (r);
9916 return r;
9918 case CONST_DECL:
9920 tree enum_type;
9921 tree v;
9923 if (DECL_TEMPLATE_PARM_P (t))
9924 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
9925 /* There is no need to substitute into namespace-scope
9926 enumerators. */
9927 if (DECL_NAMESPACE_SCOPE_P (t))
9928 return t;
9929 /* If ARGS is NULL, then T is known to be non-dependent. */
9930 if (args == NULL_TREE)
9931 return integral_constant_value (t);
9933 /* Unfortunately, we cannot just call lookup_name here.
9934 Consider:
9936 template <int I> int f() {
9937 enum E { a = I };
9938 struct S { void g() { E e = a; } };
9941 When we instantiate f<7>::S::g(), say, lookup_name is not
9942 clever enough to find f<7>::a. */
9943 enum_type
9944 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
9945 /*entering_scope=*/0);
9947 for (v = TYPE_VALUES (enum_type);
9948 v != NULL_TREE;
9949 v = TREE_CHAIN (v))
9950 if (TREE_PURPOSE (v) == DECL_NAME (t))
9951 return TREE_VALUE (v);
9953 /* We didn't find the name. That should never happen; if
9954 name-lookup found it during preliminary parsing, we
9955 should find it again here during instantiation. */
9956 gcc_unreachable ();
9958 return t;
9960 case FIELD_DECL:
9961 if (DECL_CONTEXT (t))
9963 tree ctx;
9965 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
9966 /*entering_scope=*/1);
9967 if (ctx != DECL_CONTEXT (t))
9969 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
9970 if (!r)
9972 if (complain & tf_error)
9973 error ("using invalid field %qD", t);
9974 return error_mark_node;
9976 return r;
9980 return t;
9982 case VAR_DECL:
9983 case FUNCTION_DECL:
9984 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9985 || local_variable_p (t))
9986 t = tsubst (t, args, complain, in_decl);
9987 mark_used (t);
9988 return t;
9990 case BASELINK:
9991 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
9993 case TEMPLATE_DECL:
9994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9995 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
9996 args, complain, in_decl);
9997 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
9998 return tsubst (t, args, complain, in_decl);
9999 else if (DECL_CLASS_SCOPE_P (t)
10000 && uses_template_parms (DECL_CONTEXT (t)))
10002 /* Template template argument like the following example need
10003 special treatment:
10005 template <template <class> class TT> struct C {};
10006 template <class T> struct D {
10007 template <class U> struct E {};
10008 C<E> c; // #1
10010 D<int> d; // #2
10012 We are processing the template argument `E' in #1 for
10013 the template instantiation #2. Originally, `E' is a
10014 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10015 have to substitute this with one having context `D<int>'. */
10017 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10018 return lookup_field (context, DECL_NAME(t), 0, false);
10020 else
10021 /* Ordinary template template argument. */
10022 return t;
10024 case CAST_EXPR:
10025 case REINTERPRET_CAST_EXPR:
10026 case CONST_CAST_EXPR:
10027 case STATIC_CAST_EXPR:
10028 case DYNAMIC_CAST_EXPR:
10029 case NOP_EXPR:
10030 return build1
10031 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10032 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10034 case SIZEOF_EXPR:
10035 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10037 /* We only want to compute the number of arguments. */
10038 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10039 complain, in_decl);
10040 int len = 0;
10042 if (TREE_CODE (expanded) == TREE_VEC)
10043 len = TREE_VEC_LENGTH (expanded);
10045 if (expanded == error_mark_node)
10046 return error_mark_node;
10047 else if (PACK_EXPANSION_P (expanded)
10048 || (TREE_CODE (expanded) == TREE_VEC
10049 && len > 0
10050 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10052 if (TREE_CODE (expanded) == TREE_VEC)
10053 expanded = TREE_VEC_ELT (expanded, len - 1);
10055 if (TYPE_P (expanded))
10056 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10057 complain & tf_error);
10058 else
10059 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10060 complain & tf_error);
10062 else
10063 return build_int_cst (size_type_node, len);
10065 /* Fall through */
10067 case INDIRECT_REF:
10068 case NEGATE_EXPR:
10069 case TRUTH_NOT_EXPR:
10070 case BIT_NOT_EXPR:
10071 case ADDR_EXPR:
10072 case UNARY_PLUS_EXPR: /* Unary + */
10073 case ALIGNOF_EXPR:
10074 case ARROW_EXPR:
10075 case THROW_EXPR:
10076 case TYPEID_EXPR:
10077 case REALPART_EXPR:
10078 case IMAGPART_EXPR:
10079 return build1
10080 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10081 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10083 case COMPONENT_REF:
10085 tree object;
10086 tree name;
10088 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10089 name = TREE_OPERAND (t, 1);
10090 if (TREE_CODE (name) == BIT_NOT_EXPR)
10092 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10093 complain, in_decl);
10094 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10096 else if (TREE_CODE (name) == SCOPE_REF
10097 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10099 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10100 complain, in_decl);
10101 name = TREE_OPERAND (name, 1);
10102 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10103 complain, in_decl);
10104 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10105 name = build_qualified_name (/*type=*/NULL_TREE,
10106 base, name,
10107 /*template_p=*/false);
10109 else if (TREE_CODE (name) == BASELINK)
10110 name = tsubst_baselink (name,
10111 non_reference (TREE_TYPE (object)),
10112 args, complain,
10113 in_decl);
10114 else
10115 name = tsubst_copy (name, args, complain, in_decl);
10116 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10119 case PLUS_EXPR:
10120 case MINUS_EXPR:
10121 case MULT_EXPR:
10122 case TRUNC_DIV_EXPR:
10123 case CEIL_DIV_EXPR:
10124 case FLOOR_DIV_EXPR:
10125 case ROUND_DIV_EXPR:
10126 case EXACT_DIV_EXPR:
10127 case BIT_AND_EXPR:
10128 case BIT_IOR_EXPR:
10129 case BIT_XOR_EXPR:
10130 case TRUNC_MOD_EXPR:
10131 case FLOOR_MOD_EXPR:
10132 case TRUTH_ANDIF_EXPR:
10133 case TRUTH_ORIF_EXPR:
10134 case TRUTH_AND_EXPR:
10135 case TRUTH_OR_EXPR:
10136 case RSHIFT_EXPR:
10137 case LSHIFT_EXPR:
10138 case RROTATE_EXPR:
10139 case LROTATE_EXPR:
10140 case EQ_EXPR:
10141 case NE_EXPR:
10142 case MAX_EXPR:
10143 case MIN_EXPR:
10144 case LE_EXPR:
10145 case GE_EXPR:
10146 case LT_EXPR:
10147 case GT_EXPR:
10148 case COMPOUND_EXPR:
10149 case DOTSTAR_EXPR:
10150 case MEMBER_REF:
10151 case PREDECREMENT_EXPR:
10152 case PREINCREMENT_EXPR:
10153 case POSTDECREMENT_EXPR:
10154 case POSTINCREMENT_EXPR:
10155 return build_nt
10156 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10157 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10159 case SCOPE_REF:
10160 return build_qualified_name (/*type=*/NULL_TREE,
10161 tsubst_copy (TREE_OPERAND (t, 0),
10162 args, complain, in_decl),
10163 tsubst_copy (TREE_OPERAND (t, 1),
10164 args, complain, in_decl),
10165 QUALIFIED_NAME_IS_TEMPLATE (t));
10167 case ARRAY_REF:
10168 return build_nt
10169 (ARRAY_REF,
10170 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10171 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10172 NULL_TREE, NULL_TREE);
10174 case CALL_EXPR:
10176 int n = VL_EXP_OPERAND_LENGTH (t);
10177 tree result = build_vl_exp (CALL_EXPR, n);
10178 int i;
10179 for (i = 0; i < n; i++)
10180 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10181 complain, in_decl);
10182 return result;
10185 case COND_EXPR:
10186 case MODOP_EXPR:
10187 case PSEUDO_DTOR_EXPR:
10189 r = build_nt
10190 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10191 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10192 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10193 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10194 return r;
10197 case NEW_EXPR:
10199 r = build_nt
10200 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10201 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10202 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10203 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10204 return r;
10207 case DELETE_EXPR:
10209 r = build_nt
10210 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10211 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10212 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10213 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10214 return r;
10217 case TEMPLATE_ID_EXPR:
10219 /* Substituted template arguments */
10220 tree fn = TREE_OPERAND (t, 0);
10221 tree targs = TREE_OPERAND (t, 1);
10223 fn = tsubst_copy (fn, args, complain, in_decl);
10224 if (targs)
10225 targs = tsubst_template_args (targs, args, complain, in_decl);
10227 return lookup_template_function (fn, targs);
10230 case TREE_LIST:
10232 tree purpose, value, chain;
10234 if (t == void_list_node)
10235 return t;
10237 purpose = TREE_PURPOSE (t);
10238 if (purpose)
10239 purpose = tsubst_copy (purpose, args, complain, in_decl);
10240 value = TREE_VALUE (t);
10241 if (value)
10242 value = tsubst_copy (value, args, complain, in_decl);
10243 chain = TREE_CHAIN (t);
10244 if (chain && chain != void_type_node)
10245 chain = tsubst_copy (chain, args, complain, in_decl);
10246 if (purpose == TREE_PURPOSE (t)
10247 && value == TREE_VALUE (t)
10248 && chain == TREE_CHAIN (t))
10249 return t;
10250 return tree_cons (purpose, value, chain);
10253 case RECORD_TYPE:
10254 case UNION_TYPE:
10255 case ENUMERAL_TYPE:
10256 case INTEGER_TYPE:
10257 case TEMPLATE_TYPE_PARM:
10258 case TEMPLATE_TEMPLATE_PARM:
10259 case BOUND_TEMPLATE_TEMPLATE_PARM:
10260 case TEMPLATE_PARM_INDEX:
10261 case POINTER_TYPE:
10262 case REFERENCE_TYPE:
10263 case OFFSET_TYPE:
10264 case FUNCTION_TYPE:
10265 case METHOD_TYPE:
10266 case ARRAY_TYPE:
10267 case TYPENAME_TYPE:
10268 case UNBOUND_CLASS_TEMPLATE:
10269 case TYPEOF_TYPE:
10270 case DECLTYPE_TYPE:
10271 case TYPE_DECL:
10272 return tsubst (t, args, complain, in_decl);
10274 case IDENTIFIER_NODE:
10275 if (IDENTIFIER_TYPENAME_P (t))
10277 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10278 return mangle_conv_op_name_for_type (new_type);
10280 else
10281 return t;
10283 case CONSTRUCTOR:
10284 /* This is handled by tsubst_copy_and_build. */
10285 gcc_unreachable ();
10287 case VA_ARG_EXPR:
10288 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10289 in_decl),
10290 tsubst (TREE_TYPE (t), args, complain, in_decl));
10292 case CLEANUP_POINT_EXPR:
10293 /* We shouldn't have built any of these during initial template
10294 generation. Instead, they should be built during instantiation
10295 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10296 gcc_unreachable ();
10298 case OFFSET_REF:
10299 mark_used (TREE_OPERAND (t, 1));
10300 return t;
10302 case EXPR_PACK_EXPANSION:
10303 error ("invalid use of pack expansion expression");
10304 return error_mark_node;
10306 case NONTYPE_ARGUMENT_PACK:
10307 error ("use %<...%> to expand argument pack");
10308 return error_mark_node;
10310 default:
10311 return t;
10315 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10317 static tree
10318 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10319 tree in_decl)
10321 tree new_clauses = NULL, nc, oc;
10323 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10325 nc = copy_node (oc);
10326 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10327 new_clauses = nc;
10329 switch (OMP_CLAUSE_CODE (nc))
10331 case OMP_CLAUSE_LASTPRIVATE:
10332 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10334 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10335 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10336 in_decl, /*integral_constant_expression_p=*/false);
10337 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10338 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10340 /* FALLTHRU */
10341 case OMP_CLAUSE_PRIVATE:
10342 case OMP_CLAUSE_SHARED:
10343 case OMP_CLAUSE_FIRSTPRIVATE:
10344 case OMP_CLAUSE_REDUCTION:
10345 case OMP_CLAUSE_COPYIN:
10346 case OMP_CLAUSE_COPYPRIVATE:
10347 case OMP_CLAUSE_IF:
10348 case OMP_CLAUSE_NUM_THREADS:
10349 case OMP_CLAUSE_SCHEDULE:
10350 case OMP_CLAUSE_COLLAPSE:
10351 OMP_CLAUSE_OPERAND (nc, 0)
10352 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10353 in_decl, /*integral_constant_expression_p=*/false);
10354 break;
10355 case OMP_CLAUSE_NOWAIT:
10356 case OMP_CLAUSE_ORDERED:
10357 case OMP_CLAUSE_DEFAULT:
10358 case OMP_CLAUSE_UNTIED:
10359 break;
10360 default:
10361 gcc_unreachable ();
10365 return finish_omp_clauses (nreverse (new_clauses));
10368 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10370 static tree
10371 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10372 tree in_decl)
10374 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10376 tree purpose, value, chain;
10378 if (t == NULL)
10379 return t;
10381 if (TREE_CODE (t) != TREE_LIST)
10382 return tsubst_copy_and_build (t, args, complain, in_decl,
10383 /*function_p=*/false,
10384 /*integral_constant_expression_p=*/false);
10386 if (t == void_list_node)
10387 return t;
10389 purpose = TREE_PURPOSE (t);
10390 if (purpose)
10391 purpose = RECUR (purpose);
10392 value = TREE_VALUE (t);
10393 if (value)
10394 value = RECUR (value);
10395 chain = TREE_CHAIN (t);
10396 if (chain && chain != void_type_node)
10397 chain = RECUR (chain);
10398 return tree_cons (purpose, value, chain);
10399 #undef RECUR
10402 /* Substitute one OMP_FOR iterator. */
10404 static void
10405 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10406 tree condv, tree incrv, tree *clauses,
10407 tree args, tsubst_flags_t complain, tree in_decl,
10408 bool integral_constant_expression_p)
10410 #define RECUR(NODE) \
10411 tsubst_expr ((NODE), args, complain, in_decl, \
10412 integral_constant_expression_p)
10413 tree decl, init, cond, incr, auto_node;
10415 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10416 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10417 decl = RECUR (TREE_OPERAND (init, 0));
10418 init = TREE_OPERAND (init, 1);
10419 auto_node = type_uses_auto (TREE_TYPE (decl));
10420 if (auto_node && init)
10422 tree init_expr = init;
10423 if (TREE_CODE (init_expr) == DECL_EXPR)
10424 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
10425 init_expr = RECUR (init_expr);
10426 TREE_TYPE (decl)
10427 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
10429 gcc_assert (!type_dependent_expression_p (decl));
10431 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10433 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10434 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10435 if (TREE_CODE (incr) == MODIFY_EXPR)
10436 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10437 RECUR (TREE_OPERAND (incr, 1)),
10438 complain);
10439 else
10440 incr = RECUR (incr);
10441 TREE_VEC_ELT (declv, i) = decl;
10442 TREE_VEC_ELT (initv, i) = init;
10443 TREE_VEC_ELT (condv, i) = cond;
10444 TREE_VEC_ELT (incrv, i) = incr;
10445 return;
10448 if (init && TREE_CODE (init) != DECL_EXPR)
10450 tree c;
10451 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10453 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10454 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10455 && OMP_CLAUSE_DECL (c) == decl)
10456 break;
10457 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10458 && OMP_CLAUSE_DECL (c) == decl)
10459 error ("iteration variable %qD should not be firstprivate", decl);
10460 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10461 && OMP_CLAUSE_DECL (c) == decl)
10462 error ("iteration variable %qD should not be reduction", decl);
10464 if (c == NULL)
10466 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
10467 OMP_CLAUSE_DECL (c) = decl;
10468 c = finish_omp_clauses (c);
10469 if (c)
10471 OMP_CLAUSE_CHAIN (c) = *clauses;
10472 *clauses = c;
10476 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
10477 if (COMPARISON_CLASS_P (cond))
10478 cond = build2 (TREE_CODE (cond), boolean_type_node,
10479 RECUR (TREE_OPERAND (cond, 0)),
10480 RECUR (TREE_OPERAND (cond, 1)));
10481 else
10482 cond = RECUR (cond);
10483 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10484 switch (TREE_CODE (incr))
10486 case PREINCREMENT_EXPR:
10487 case PREDECREMENT_EXPR:
10488 case POSTINCREMENT_EXPR:
10489 case POSTDECREMENT_EXPR:
10490 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
10491 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
10492 break;
10493 case MODIFY_EXPR:
10494 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10495 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10497 tree rhs = TREE_OPERAND (incr, 1);
10498 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10499 RECUR (TREE_OPERAND (incr, 0)),
10500 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10501 RECUR (TREE_OPERAND (rhs, 0)),
10502 RECUR (TREE_OPERAND (rhs, 1))));
10504 else
10505 incr = RECUR (incr);
10506 break;
10507 case MODOP_EXPR:
10508 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10509 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10511 tree lhs = RECUR (TREE_OPERAND (incr, 0));
10512 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
10513 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
10514 TREE_TYPE (decl), lhs,
10515 RECUR (TREE_OPERAND (incr, 2))));
10517 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
10518 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
10519 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
10521 tree rhs = TREE_OPERAND (incr, 2);
10522 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10523 RECUR (TREE_OPERAND (incr, 0)),
10524 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10525 RECUR (TREE_OPERAND (rhs, 0)),
10526 RECUR (TREE_OPERAND (rhs, 1))));
10528 else
10529 incr = RECUR (incr);
10530 break;
10531 default:
10532 incr = RECUR (incr);
10533 break;
10536 TREE_VEC_ELT (declv, i) = decl;
10537 TREE_VEC_ELT (initv, i) = init;
10538 TREE_VEC_ELT (condv, i) = cond;
10539 TREE_VEC_ELT (incrv, i) = incr;
10540 #undef RECUR
10543 /* Like tsubst_copy for expressions, etc. but also does semantic
10544 processing. */
10546 static tree
10547 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
10548 bool integral_constant_expression_p)
10550 #define RECUR(NODE) \
10551 tsubst_expr ((NODE), args, complain, in_decl, \
10552 integral_constant_expression_p)
10554 tree stmt, tmp;
10556 if (t == NULL_TREE || t == error_mark_node)
10557 return t;
10559 if (EXPR_HAS_LOCATION (t))
10560 input_location = EXPR_LOCATION (t);
10561 if (STATEMENT_CODE_P (TREE_CODE (t)))
10562 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
10564 switch (TREE_CODE (t))
10566 case STATEMENT_LIST:
10568 tree_stmt_iterator i;
10569 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
10570 RECUR (tsi_stmt (i));
10571 break;
10574 case CTOR_INITIALIZER:
10575 finish_mem_initializers (tsubst_initializer_list
10576 (TREE_OPERAND (t, 0), args));
10577 break;
10579 case RETURN_EXPR:
10580 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
10581 break;
10583 case EXPR_STMT:
10584 tmp = RECUR (EXPR_STMT_EXPR (t));
10585 if (EXPR_STMT_STMT_EXPR_RESULT (t))
10586 finish_stmt_expr_expr (tmp, cur_stmt_expr);
10587 else
10588 finish_expr_stmt (tmp);
10589 break;
10591 case USING_STMT:
10592 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
10593 break;
10595 case DECL_EXPR:
10597 tree decl;
10598 tree init;
10600 decl = DECL_EXPR_DECL (t);
10601 if (TREE_CODE (decl) == LABEL_DECL)
10602 finish_label_decl (DECL_NAME (decl));
10603 else if (TREE_CODE (decl) == USING_DECL)
10605 tree scope = USING_DECL_SCOPE (decl);
10606 tree name = DECL_NAME (decl);
10607 tree decl;
10609 scope = RECUR (scope);
10610 decl = lookup_qualified_name (scope, name,
10611 /*is_type_p=*/false,
10612 /*complain=*/false);
10613 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
10614 qualified_name_lookup_error (scope, name, decl, input_location);
10615 else
10616 do_local_using_decl (decl, scope, name);
10618 else
10620 init = DECL_INITIAL (decl);
10621 decl = tsubst (decl, args, complain, in_decl);
10622 if (decl != error_mark_node)
10624 /* By marking the declaration as instantiated, we avoid
10625 trying to instantiate it. Since instantiate_decl can't
10626 handle local variables, and since we've already done
10627 all that needs to be done, that's the right thing to
10628 do. */
10629 if (TREE_CODE (decl) == VAR_DECL)
10630 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10631 if (TREE_CODE (decl) == VAR_DECL
10632 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
10633 /* Anonymous aggregates are a special case. */
10634 finish_anon_union (decl);
10635 else
10637 maybe_push_decl (decl);
10638 if (TREE_CODE (decl) == VAR_DECL
10639 && DECL_PRETTY_FUNCTION_P (decl))
10641 /* For __PRETTY_FUNCTION__ we have to adjust the
10642 initializer. */
10643 const char *const name
10644 = cxx_printable_name (current_function_decl, 2);
10645 init = cp_fname_init (name, &TREE_TYPE (decl));
10647 else
10649 tree t = RECUR (init);
10651 if (init && !t)
10652 /* If we had an initializer but it
10653 instantiated to nothing,
10654 value-initialize the object. This will
10655 only occur when the initializer was a
10656 pack expansion where the parameter packs
10657 used in that expansion were of length
10658 zero. */
10659 init = build_value_init (TREE_TYPE (decl));
10660 else
10661 init = t;
10664 finish_decl (decl, init, NULL_TREE);
10669 /* A DECL_EXPR can also be used as an expression, in the condition
10670 clause of an if/for/while construct. */
10671 return decl;
10674 case FOR_STMT:
10675 stmt = begin_for_stmt ();
10676 RECUR (FOR_INIT_STMT (t));
10677 finish_for_init_stmt (stmt);
10678 tmp = RECUR (FOR_COND (t));
10679 finish_for_cond (tmp, stmt);
10680 tmp = RECUR (FOR_EXPR (t));
10681 finish_for_expr (tmp, stmt);
10682 RECUR (FOR_BODY (t));
10683 finish_for_stmt (stmt);
10684 break;
10686 case WHILE_STMT:
10687 stmt = begin_while_stmt ();
10688 tmp = RECUR (WHILE_COND (t));
10689 finish_while_stmt_cond (tmp, stmt);
10690 RECUR (WHILE_BODY (t));
10691 finish_while_stmt (stmt);
10692 break;
10694 case DO_STMT:
10695 stmt = begin_do_stmt ();
10696 RECUR (DO_BODY (t));
10697 finish_do_body (stmt);
10698 tmp = RECUR (DO_COND (t));
10699 finish_do_stmt (tmp, stmt);
10700 break;
10702 case IF_STMT:
10703 stmt = begin_if_stmt ();
10704 tmp = RECUR (IF_COND (t));
10705 finish_if_stmt_cond (tmp, stmt);
10706 RECUR (THEN_CLAUSE (t));
10707 finish_then_clause (stmt);
10709 if (ELSE_CLAUSE (t))
10711 begin_else_clause (stmt);
10712 RECUR (ELSE_CLAUSE (t));
10713 finish_else_clause (stmt);
10716 finish_if_stmt (stmt);
10717 break;
10719 case BIND_EXPR:
10720 if (BIND_EXPR_BODY_BLOCK (t))
10721 stmt = begin_function_body ();
10722 else
10723 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
10724 ? BCS_TRY_BLOCK : 0);
10726 RECUR (BIND_EXPR_BODY (t));
10728 if (BIND_EXPR_BODY_BLOCK (t))
10729 finish_function_body (stmt);
10730 else
10731 finish_compound_stmt (stmt);
10732 break;
10734 case BREAK_STMT:
10735 finish_break_stmt ();
10736 break;
10738 case CONTINUE_STMT:
10739 finish_continue_stmt ();
10740 break;
10742 case SWITCH_STMT:
10743 stmt = begin_switch_stmt ();
10744 tmp = RECUR (SWITCH_STMT_COND (t));
10745 finish_switch_cond (tmp, stmt);
10746 RECUR (SWITCH_STMT_BODY (t));
10747 finish_switch_stmt (stmt);
10748 break;
10750 case CASE_LABEL_EXPR:
10751 finish_case_label (RECUR (CASE_LOW (t)),
10752 RECUR (CASE_HIGH (t)));
10753 break;
10755 case LABEL_EXPR:
10756 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
10757 break;
10759 case GOTO_EXPR:
10760 tmp = GOTO_DESTINATION (t);
10761 if (TREE_CODE (tmp) != LABEL_DECL)
10762 /* Computed goto's must be tsubst'd into. On the other hand,
10763 non-computed gotos must not be; the identifier in question
10764 will have no binding. */
10765 tmp = RECUR (tmp);
10766 else
10767 tmp = DECL_NAME (tmp);
10768 finish_goto_stmt (tmp);
10769 break;
10771 case ASM_EXPR:
10772 tmp = finish_asm_stmt
10773 (ASM_VOLATILE_P (t),
10774 RECUR (ASM_STRING (t)),
10775 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10776 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10777 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10779 tree asm_expr = tmp;
10780 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10781 asm_expr = TREE_OPERAND (asm_expr, 0);
10782 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10784 break;
10786 case TRY_BLOCK:
10787 if (CLEANUP_P (t))
10789 stmt = begin_try_block ();
10790 RECUR (TRY_STMTS (t));
10791 finish_cleanup_try_block (stmt);
10792 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10794 else
10796 tree compound_stmt = NULL_TREE;
10798 if (FN_TRY_BLOCK_P (t))
10799 stmt = begin_function_try_block (&compound_stmt);
10800 else
10801 stmt = begin_try_block ();
10803 RECUR (TRY_STMTS (t));
10805 if (FN_TRY_BLOCK_P (t))
10806 finish_function_try_block (stmt);
10807 else
10808 finish_try_block (stmt);
10810 RECUR (TRY_HANDLERS (t));
10811 if (FN_TRY_BLOCK_P (t))
10812 finish_function_handler_sequence (stmt, compound_stmt);
10813 else
10814 finish_handler_sequence (stmt);
10816 break;
10818 case HANDLER:
10820 tree decl = HANDLER_PARMS (t);
10822 if (decl)
10824 decl = tsubst (decl, args, complain, in_decl);
10825 /* Prevent instantiate_decl from trying to instantiate
10826 this variable. We've already done all that needs to be
10827 done. */
10828 if (decl != error_mark_node)
10829 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10831 stmt = begin_handler ();
10832 finish_handler_parms (decl, stmt);
10833 RECUR (HANDLER_BODY (t));
10834 finish_handler (stmt);
10836 break;
10838 case TAG_DEFN:
10839 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
10840 break;
10842 case STATIC_ASSERT:
10844 tree condition =
10845 tsubst_expr (STATIC_ASSERT_CONDITION (t),
10846 args,
10847 complain, in_decl,
10848 /*integral_constant_expression_p=*/true);
10849 finish_static_assert (condition,
10850 STATIC_ASSERT_MESSAGE (t),
10851 STATIC_ASSERT_SOURCE_LOCATION (t),
10852 /*member_p=*/false);
10854 break;
10856 case OMP_PARALLEL:
10857 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
10858 args, complain, in_decl);
10859 stmt = begin_omp_parallel ();
10860 RECUR (OMP_PARALLEL_BODY (t));
10861 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
10862 = OMP_PARALLEL_COMBINED (t);
10863 break;
10865 case OMP_TASK:
10866 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
10867 args, complain, in_decl);
10868 stmt = begin_omp_task ();
10869 RECUR (OMP_TASK_BODY (t));
10870 finish_omp_task (tmp, stmt);
10871 break;
10873 case OMP_FOR:
10875 tree clauses, body, pre_body;
10876 tree declv, initv, condv, incrv;
10877 int i;
10879 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
10880 args, complain, in_decl);
10881 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
10882 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
10883 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
10884 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
10886 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
10887 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
10888 &clauses, args, complain, in_decl,
10889 integral_constant_expression_p);
10891 stmt = begin_omp_structured_block ();
10893 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
10894 if (TREE_VEC_ELT (initv, i) == NULL
10895 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
10896 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
10897 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
10899 tree init = RECUR (TREE_VEC_ELT (initv, i));
10900 gcc_assert (init == TREE_VEC_ELT (declv, i));
10901 TREE_VEC_ELT (initv, i) = NULL_TREE;
10903 else
10905 tree decl_expr = TREE_VEC_ELT (initv, i);
10906 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
10907 gcc_assert (init != NULL);
10908 TREE_VEC_ELT (initv, i) = RECUR (init);
10909 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
10910 RECUR (decl_expr);
10911 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
10914 pre_body = push_stmt_list ();
10915 RECUR (OMP_FOR_PRE_BODY (t));
10916 pre_body = pop_stmt_list (pre_body);
10918 body = push_stmt_list ();
10919 RECUR (OMP_FOR_BODY (t));
10920 body = pop_stmt_list (body);
10922 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
10923 body, pre_body, clauses);
10925 add_stmt (finish_omp_structured_block (stmt));
10927 break;
10929 case OMP_SECTIONS:
10930 case OMP_SINGLE:
10931 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
10932 stmt = push_stmt_list ();
10933 RECUR (OMP_BODY (t));
10934 stmt = pop_stmt_list (stmt);
10936 t = copy_node (t);
10937 OMP_BODY (t) = stmt;
10938 OMP_CLAUSES (t) = tmp;
10939 add_stmt (t);
10940 break;
10942 case OMP_SECTION:
10943 case OMP_CRITICAL:
10944 case OMP_MASTER:
10945 case OMP_ORDERED:
10946 stmt = push_stmt_list ();
10947 RECUR (OMP_BODY (t));
10948 stmt = pop_stmt_list (stmt);
10950 t = copy_node (t);
10951 OMP_BODY (t) = stmt;
10952 add_stmt (t);
10953 break;
10955 case OMP_ATOMIC:
10956 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
10958 tree op1 = TREE_OPERAND (t, 1);
10959 tree lhs = RECUR (TREE_OPERAND (op1, 0));
10960 tree rhs = RECUR (TREE_OPERAND (op1, 1));
10961 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
10963 break;
10965 case EXPR_PACK_EXPANSION:
10966 error ("invalid use of pack expansion expression");
10967 return error_mark_node;
10969 case NONTYPE_ARGUMENT_PACK:
10970 error ("use %<...%> to expand argument pack");
10971 return error_mark_node;
10973 default:
10974 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
10976 return tsubst_copy_and_build (t, args, complain, in_decl,
10977 /*function_p=*/false,
10978 integral_constant_expression_p);
10981 return NULL_TREE;
10982 #undef RECUR
10985 /* T is a postfix-expression that is not being used in a function
10986 call. Return the substituted version of T. */
10988 static tree
10989 tsubst_non_call_postfix_expression (tree t, tree args,
10990 tsubst_flags_t complain,
10991 tree in_decl)
10993 if (TREE_CODE (t) == SCOPE_REF)
10994 t = tsubst_qualified_id (t, args, complain, in_decl,
10995 /*done=*/false, /*address_p=*/false);
10996 else
10997 t = tsubst_copy_and_build (t, args, complain, in_decl,
10998 /*function_p=*/false,
10999 /*integral_constant_expression_p=*/false);
11001 return t;
11004 /* Like tsubst but deals with expressions and performs semantic
11005 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11007 tree
11008 tsubst_copy_and_build (tree t,
11009 tree args,
11010 tsubst_flags_t complain,
11011 tree in_decl,
11012 bool function_p,
11013 bool integral_constant_expression_p)
11015 #define RECUR(NODE) \
11016 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11017 /*function_p=*/false, \
11018 integral_constant_expression_p)
11020 tree op1;
11022 if (t == NULL_TREE || t == error_mark_node)
11023 return t;
11025 switch (TREE_CODE (t))
11027 case USING_DECL:
11028 t = DECL_NAME (t);
11029 /* Fall through. */
11030 case IDENTIFIER_NODE:
11032 tree decl;
11033 cp_id_kind idk;
11034 bool non_integral_constant_expression_p;
11035 const char *error_msg;
11037 if (IDENTIFIER_TYPENAME_P (t))
11039 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11040 t = mangle_conv_op_name_for_type (new_type);
11043 /* Look up the name. */
11044 decl = lookup_name (t);
11046 /* By convention, expressions use ERROR_MARK_NODE to indicate
11047 failure, not NULL_TREE. */
11048 if (decl == NULL_TREE)
11049 decl = error_mark_node;
11051 decl = finish_id_expression (t, decl, NULL_TREE,
11052 &idk,
11053 integral_constant_expression_p,
11054 /*allow_non_integral_constant_expression_p=*/false,
11055 &non_integral_constant_expression_p,
11056 /*template_p=*/false,
11057 /*done=*/true,
11058 /*address_p=*/false,
11059 /*template_arg_p=*/false,
11060 &error_msg,
11061 input_location);
11062 if (error_msg)
11063 error (error_msg);
11064 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11065 decl = unqualified_name_lookup_error (decl);
11066 return decl;
11069 case TEMPLATE_ID_EXPR:
11071 tree object;
11072 tree templ = RECUR (TREE_OPERAND (t, 0));
11073 tree targs = TREE_OPERAND (t, 1);
11075 if (targs)
11076 targs = tsubst_template_args (targs, args, complain, in_decl);
11078 if (TREE_CODE (templ) == COMPONENT_REF)
11080 object = TREE_OPERAND (templ, 0);
11081 templ = TREE_OPERAND (templ, 1);
11083 else
11084 object = NULL_TREE;
11085 templ = lookup_template_function (templ, targs);
11087 if (object)
11088 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11089 object, templ, NULL_TREE);
11090 else
11091 return baselink_for_fns (templ);
11094 case INDIRECT_REF:
11096 tree r = RECUR (TREE_OPERAND (t, 0));
11098 if (REFERENCE_REF_P (t))
11100 /* A type conversion to reference type will be enclosed in
11101 such an indirect ref, but the substitution of the cast
11102 will have also added such an indirect ref. */
11103 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11104 r = convert_from_reference (r);
11106 else
11107 r = build_x_indirect_ref (r, "unary *", complain);
11108 return r;
11111 case NOP_EXPR:
11112 return build_nop
11113 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11114 RECUR (TREE_OPERAND (t, 0)));
11116 case CAST_EXPR:
11117 case REINTERPRET_CAST_EXPR:
11118 case CONST_CAST_EXPR:
11119 case DYNAMIC_CAST_EXPR:
11120 case STATIC_CAST_EXPR:
11122 tree type;
11123 tree op;
11125 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11126 if (integral_constant_expression_p
11127 && !cast_valid_in_integral_constant_expression_p (type))
11129 if (complain & tf_error)
11130 error ("a cast to a type other than an integral or "
11131 "enumeration type cannot appear in a constant-expression");
11132 return error_mark_node;
11135 op = RECUR (TREE_OPERAND (t, 0));
11137 switch (TREE_CODE (t))
11139 case CAST_EXPR:
11140 return build_functional_cast (type, op, complain);
11141 case REINTERPRET_CAST_EXPR:
11142 return build_reinterpret_cast (type, op, complain);
11143 case CONST_CAST_EXPR:
11144 return build_const_cast (type, op, complain);
11145 case DYNAMIC_CAST_EXPR:
11146 return build_dynamic_cast (type, op, complain);
11147 case STATIC_CAST_EXPR:
11148 return build_static_cast (type, op, complain);
11149 default:
11150 gcc_unreachable ();
11154 case POSTDECREMENT_EXPR:
11155 case POSTINCREMENT_EXPR:
11156 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11157 args, complain, in_decl);
11158 return build_x_unary_op (TREE_CODE (t), op1, complain);
11160 case PREDECREMENT_EXPR:
11161 case PREINCREMENT_EXPR:
11162 case NEGATE_EXPR:
11163 case BIT_NOT_EXPR:
11164 case ABS_EXPR:
11165 case TRUTH_NOT_EXPR:
11166 case UNARY_PLUS_EXPR: /* Unary + */
11167 case REALPART_EXPR:
11168 case IMAGPART_EXPR:
11169 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11170 complain);
11172 case ADDR_EXPR:
11173 op1 = TREE_OPERAND (t, 0);
11174 if (TREE_CODE (op1) == SCOPE_REF)
11175 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11176 /*done=*/true, /*address_p=*/true);
11177 else
11178 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11179 in_decl);
11180 if (TREE_CODE (op1) == LABEL_DECL)
11181 return finish_label_address_expr (DECL_NAME (op1),
11182 EXPR_LOCATION (op1));
11183 return build_x_unary_op (ADDR_EXPR, op1, complain);
11185 case PLUS_EXPR:
11186 case MINUS_EXPR:
11187 case MULT_EXPR:
11188 case TRUNC_DIV_EXPR:
11189 case CEIL_DIV_EXPR:
11190 case FLOOR_DIV_EXPR:
11191 case ROUND_DIV_EXPR:
11192 case EXACT_DIV_EXPR:
11193 case BIT_AND_EXPR:
11194 case BIT_IOR_EXPR:
11195 case BIT_XOR_EXPR:
11196 case TRUNC_MOD_EXPR:
11197 case FLOOR_MOD_EXPR:
11198 case TRUTH_ANDIF_EXPR:
11199 case TRUTH_ORIF_EXPR:
11200 case TRUTH_AND_EXPR:
11201 case TRUTH_OR_EXPR:
11202 case RSHIFT_EXPR:
11203 case LSHIFT_EXPR:
11204 case RROTATE_EXPR:
11205 case LROTATE_EXPR:
11206 case EQ_EXPR:
11207 case NE_EXPR:
11208 case MAX_EXPR:
11209 case MIN_EXPR:
11210 case LE_EXPR:
11211 case GE_EXPR:
11212 case LT_EXPR:
11213 case GT_EXPR:
11214 case MEMBER_REF:
11215 case DOTSTAR_EXPR:
11216 return build_x_binary_op
11217 (TREE_CODE (t),
11218 RECUR (TREE_OPERAND (t, 0)),
11219 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11220 ? ERROR_MARK
11221 : TREE_CODE (TREE_OPERAND (t, 0))),
11222 RECUR (TREE_OPERAND (t, 1)),
11223 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11224 ? ERROR_MARK
11225 : TREE_CODE (TREE_OPERAND (t, 1))),
11226 /*overloaded_p=*/NULL,
11227 complain);
11229 case SCOPE_REF:
11230 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11231 /*address_p=*/false);
11232 case ARRAY_REF:
11233 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11234 args, complain, in_decl);
11235 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11237 case SIZEOF_EXPR:
11238 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11239 return tsubst_copy (t, args, complain, in_decl);
11240 /* Fall through */
11242 case ALIGNOF_EXPR:
11243 op1 = TREE_OPERAND (t, 0);
11244 if (!args)
11246 /* When there are no ARGS, we are trying to evaluate a
11247 non-dependent expression from the parser. Trying to do
11248 the substitutions may not work. */
11249 if (!TYPE_P (op1))
11250 op1 = TREE_TYPE (op1);
11252 else
11254 ++skip_evaluation;
11255 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11256 /*function_p=*/false,
11257 /*integral_constant_expression_p=*/false);
11258 --skip_evaluation;
11260 if (TYPE_P (op1))
11261 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11262 complain & tf_error);
11263 else
11264 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11265 complain & tf_error);
11267 case MODOP_EXPR:
11269 tree r = build_x_modify_expr
11270 (RECUR (TREE_OPERAND (t, 0)),
11271 TREE_CODE (TREE_OPERAND (t, 1)),
11272 RECUR (TREE_OPERAND (t, 2)),
11273 complain);
11274 /* TREE_NO_WARNING must be set if either the expression was
11275 parenthesized or it uses an operator such as >>= rather
11276 than plain assignment. In the former case, it was already
11277 set and must be copied. In the latter case,
11278 build_x_modify_expr sets it and it must not be reset
11279 here. */
11280 if (TREE_NO_WARNING (t))
11281 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11282 return r;
11285 case ARROW_EXPR:
11286 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11287 args, complain, in_decl);
11288 /* Remember that there was a reference to this entity. */
11289 if (DECL_P (op1))
11290 mark_used (op1);
11291 return build_x_arrow (op1);
11293 case NEW_EXPR:
11295 tree init = RECUR (TREE_OPERAND (t, 3));
11297 if (TREE_OPERAND (t, 3) && !init)
11298 /* If there was an initializer in the original tree, but
11299 it instantiated to an empty list, then we should pass on
11300 VOID_ZERO_NODE to tell build_new that it was an empty
11301 initializer () rather than no initializer. This can only
11302 happen when the initializer is a pack expansion whose
11303 parameter packs are of length zero. */
11304 init = void_zero_node;
11306 return build_new
11307 (RECUR (TREE_OPERAND (t, 0)),
11308 RECUR (TREE_OPERAND (t, 1)),
11309 RECUR (TREE_OPERAND (t, 2)),
11310 init,
11311 NEW_EXPR_USE_GLOBAL (t),
11312 complain);
11315 case DELETE_EXPR:
11316 return delete_sanity
11317 (RECUR (TREE_OPERAND (t, 0)),
11318 RECUR (TREE_OPERAND (t, 1)),
11319 DELETE_EXPR_USE_VEC (t),
11320 DELETE_EXPR_USE_GLOBAL (t));
11322 case COMPOUND_EXPR:
11323 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
11324 RECUR (TREE_OPERAND (t, 1)),
11325 complain);
11327 case CALL_EXPR:
11329 tree function;
11330 tree call_args;
11331 bool qualified_p;
11332 bool koenig_p;
11334 function = CALL_EXPR_FN (t);
11335 /* When we parsed the expression, we determined whether or
11336 not Koenig lookup should be performed. */
11337 koenig_p = KOENIG_LOOKUP_P (t);
11338 if (TREE_CODE (function) == SCOPE_REF)
11340 qualified_p = true;
11341 function = tsubst_qualified_id (function, args, complain, in_decl,
11342 /*done=*/false,
11343 /*address_p=*/false);
11345 else
11347 if (TREE_CODE (function) == COMPONENT_REF)
11349 tree op = TREE_OPERAND (function, 1);
11351 qualified_p = (TREE_CODE (op) == SCOPE_REF
11352 || (BASELINK_P (op)
11353 && BASELINK_QUALIFIED_P (op)));
11355 else
11356 qualified_p = false;
11358 function = tsubst_copy_and_build (function, args, complain,
11359 in_decl,
11360 !qualified_p,
11361 integral_constant_expression_p);
11363 if (BASELINK_P (function))
11364 qualified_p = true;
11367 /* FIXME: Rewrite this so as not to construct an arglist. */
11368 call_args = RECUR (CALL_EXPR_ARGS (t));
11370 /* We do not perform argument-dependent lookup if normal
11371 lookup finds a non-function, in accordance with the
11372 expected resolution of DR 218. */
11373 if (koenig_p
11374 && ((is_overloaded_fn (function)
11375 /* If lookup found a member function, the Koenig lookup is
11376 not appropriate, even if an unqualified-name was used
11377 to denote the function. */
11378 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
11379 || TREE_CODE (function) == IDENTIFIER_NODE))
11380 function = perform_koenig_lookup (function, call_args);
11382 if (TREE_CODE (function) == IDENTIFIER_NODE)
11384 unqualified_name_lookup_error (function);
11385 return error_mark_node;
11388 /* Remember that there was a reference to this entity. */
11389 if (DECL_P (function))
11390 mark_used (function);
11392 if (TREE_CODE (function) == OFFSET_REF)
11393 return build_offset_ref_call_from_tree (function, call_args);
11394 if (TREE_CODE (function) == COMPONENT_REF)
11396 if (!BASELINK_P (TREE_OPERAND (function, 1)))
11397 return finish_call_expr (function, call_args,
11398 /*disallow_virtual=*/false,
11399 /*koenig_p=*/false,
11400 complain);
11401 else
11402 return (build_new_method_call
11403 (TREE_OPERAND (function, 0),
11404 TREE_OPERAND (function, 1),
11405 call_args, NULL_TREE,
11406 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
11407 /*fn_p=*/NULL,
11408 complain));
11410 /* Pass -1 for koenig_p so that build_new_function_call will
11411 allow hidden friends found by arg-dependent lookup at template
11412 parsing time. */
11413 return finish_call_expr (function, call_args,
11414 /*disallow_virtual=*/qualified_p,
11415 /*koenig_p*/-1,
11416 complain);
11419 case COND_EXPR:
11420 return build_x_conditional_expr
11421 (RECUR (TREE_OPERAND (t, 0)),
11422 RECUR (TREE_OPERAND (t, 1)),
11423 RECUR (TREE_OPERAND (t, 2)),
11424 complain);
11426 case PSEUDO_DTOR_EXPR:
11427 return finish_pseudo_destructor_expr
11428 (RECUR (TREE_OPERAND (t, 0)),
11429 RECUR (TREE_OPERAND (t, 1)),
11430 RECUR (TREE_OPERAND (t, 2)));
11432 case TREE_LIST:
11434 tree purpose, value, chain;
11436 if (t == void_list_node)
11437 return t;
11439 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
11440 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
11442 /* We have pack expansions, so expand those and
11443 create a new list out of it. */
11444 tree purposevec = NULL_TREE;
11445 tree valuevec = NULL_TREE;
11446 tree chain;
11447 int i, len = -1;
11449 /* Expand the argument expressions. */
11450 if (TREE_PURPOSE (t))
11451 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
11452 complain, in_decl);
11453 if (TREE_VALUE (t))
11454 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
11455 complain, in_decl);
11457 /* Build the rest of the list. */
11458 chain = TREE_CHAIN (t);
11459 if (chain && chain != void_type_node)
11460 chain = RECUR (chain);
11462 /* Determine the number of arguments. */
11463 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
11465 len = TREE_VEC_LENGTH (purposevec);
11466 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
11468 else if (TREE_CODE (valuevec) == TREE_VEC)
11469 len = TREE_VEC_LENGTH (valuevec);
11470 else
11472 /* Since we only performed a partial substitution into
11473 the argument pack, we only return a single list
11474 node. */
11475 if (purposevec == TREE_PURPOSE (t)
11476 && valuevec == TREE_VALUE (t)
11477 && chain == TREE_CHAIN (t))
11478 return t;
11480 return tree_cons (purposevec, valuevec, chain);
11483 /* Convert the argument vectors into a TREE_LIST */
11484 i = len;
11485 while (i > 0)
11487 /* Grab the Ith values. */
11488 i--;
11489 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
11490 : NULL_TREE;
11491 value
11492 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
11493 : NULL_TREE;
11495 /* Build the list (backwards). */
11496 chain = tree_cons (purpose, value, chain);
11499 return chain;
11502 purpose = TREE_PURPOSE (t);
11503 if (purpose)
11504 purpose = RECUR (purpose);
11505 value = TREE_VALUE (t);
11506 if (value)
11507 value = RECUR (value);
11508 chain = TREE_CHAIN (t);
11509 if (chain && chain != void_type_node)
11510 chain = RECUR (chain);
11511 if (purpose == TREE_PURPOSE (t)
11512 && value == TREE_VALUE (t)
11513 && chain == TREE_CHAIN (t))
11514 return t;
11515 return tree_cons (purpose, value, chain);
11518 case COMPONENT_REF:
11520 tree object;
11521 tree object_type;
11522 tree member;
11524 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11525 args, complain, in_decl);
11526 /* Remember that there was a reference to this entity. */
11527 if (DECL_P (object))
11528 mark_used (object);
11529 object_type = TREE_TYPE (object);
11531 member = TREE_OPERAND (t, 1);
11532 if (BASELINK_P (member))
11533 member = tsubst_baselink (member,
11534 non_reference (TREE_TYPE (object)),
11535 args, complain, in_decl);
11536 else
11537 member = tsubst_copy (member, args, complain, in_decl);
11538 if (member == error_mark_node)
11539 return error_mark_node;
11541 if (object_type && !CLASS_TYPE_P (object_type))
11543 if (SCALAR_TYPE_P (object_type))
11545 tree s = NULL_TREE;
11546 tree dtor = member;
11548 if (TREE_CODE (dtor) == SCOPE_REF)
11550 s = TREE_OPERAND (dtor, 0);
11551 dtor = TREE_OPERAND (dtor, 1);
11553 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
11555 dtor = TREE_OPERAND (dtor, 0);
11556 if (TYPE_P (dtor))
11557 return finish_pseudo_destructor_expr (object, s, dtor);
11561 else if (TREE_CODE (member) == SCOPE_REF
11562 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
11564 tree tmpl;
11565 tree args;
11567 /* Lookup the template functions now that we know what the
11568 scope is. */
11569 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
11570 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
11571 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
11572 /*is_type_p=*/false,
11573 /*complain=*/false);
11574 if (BASELINK_P (member))
11576 BASELINK_FUNCTIONS (member)
11577 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
11578 args);
11579 member = (adjust_result_of_qualified_name_lookup
11580 (member, BINFO_TYPE (BASELINK_BINFO (member)),
11581 object_type));
11583 else
11585 qualified_name_lookup_error (object_type, tmpl, member,
11586 input_location);
11587 return error_mark_node;
11590 else if (TREE_CODE (member) == SCOPE_REF
11591 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
11592 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
11594 if (complain & tf_error)
11596 if (TYPE_P (TREE_OPERAND (member, 0)))
11597 error ("%qT is not a class or namespace",
11598 TREE_OPERAND (member, 0));
11599 else
11600 error ("%qD is not a class or namespace",
11601 TREE_OPERAND (member, 0));
11603 return error_mark_node;
11605 else if (TREE_CODE (member) == FIELD_DECL)
11606 return finish_non_static_data_member (member, object, NULL_TREE);
11608 return finish_class_member_access_expr (object, member,
11609 /*template_p=*/false,
11610 complain);
11613 case THROW_EXPR:
11614 return build_throw
11615 (RECUR (TREE_OPERAND (t, 0)));
11617 case CONSTRUCTOR:
11619 VEC(constructor_elt,gc) *n;
11620 constructor_elt *ce;
11621 unsigned HOST_WIDE_INT idx;
11622 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11623 bool process_index_p;
11624 int newlen;
11625 bool need_copy_p = false;
11626 tree r;
11628 if (type == error_mark_node)
11629 return error_mark_node;
11631 /* digest_init will do the wrong thing if we let it. */
11632 if (type && TYPE_PTRMEMFUNC_P (type))
11633 return t;
11635 /* We do not want to process the index of aggregate
11636 initializers as they are identifier nodes which will be
11637 looked up by digest_init. */
11638 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
11640 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
11641 newlen = VEC_length (constructor_elt, n);
11642 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
11644 if (ce->index && process_index_p)
11645 ce->index = RECUR (ce->index);
11647 if (PACK_EXPANSION_P (ce->value))
11649 /* Substitute into the pack expansion. */
11650 ce->value = tsubst_pack_expansion (ce->value, args, complain,
11651 in_decl);
11653 if (ce->value == error_mark_node)
11655 else if (TREE_VEC_LENGTH (ce->value) == 1)
11656 /* Just move the argument into place. */
11657 ce->value = TREE_VEC_ELT (ce->value, 0);
11658 else
11660 /* Update the length of the final CONSTRUCTOR
11661 arguments vector, and note that we will need to
11662 copy.*/
11663 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
11664 need_copy_p = true;
11667 else
11668 ce->value = RECUR (ce->value);
11671 if (need_copy_p)
11673 VEC(constructor_elt,gc) *old_n = n;
11675 n = VEC_alloc (constructor_elt, gc, newlen);
11676 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
11677 idx++)
11679 if (TREE_CODE (ce->value) == TREE_VEC)
11681 int i, len = TREE_VEC_LENGTH (ce->value);
11682 for (i = 0; i < len; ++i)
11683 CONSTRUCTOR_APPEND_ELT (n, 0,
11684 TREE_VEC_ELT (ce->value, i));
11686 else
11687 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
11691 r = build_constructor (init_list_type_node, n);
11692 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
11694 if (TREE_HAS_CONSTRUCTOR (t))
11695 return finish_compound_literal (type, r);
11697 return r;
11700 case TYPEID_EXPR:
11702 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
11703 if (TYPE_P (operand_0))
11704 return get_typeid (operand_0);
11705 return build_typeid (operand_0);
11708 case VAR_DECL:
11709 if (!args)
11710 return t;
11711 /* Fall through */
11713 case PARM_DECL:
11715 tree r = tsubst_copy (t, args, complain, in_decl);
11717 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
11718 /* If the original type was a reference, we'll be wrapped in
11719 the appropriate INDIRECT_REF. */
11720 r = convert_from_reference (r);
11721 return r;
11724 case VA_ARG_EXPR:
11725 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
11726 tsubst_copy (TREE_TYPE (t), args, complain,
11727 in_decl));
11729 case OFFSETOF_EXPR:
11730 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
11732 case TRAIT_EXPR:
11734 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
11735 complain, in_decl);
11737 tree type2 = TRAIT_EXPR_TYPE2 (t);
11738 if (type2)
11739 type2 = tsubst_copy (type2, args, complain, in_decl);
11741 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
11744 case STMT_EXPR:
11746 tree old_stmt_expr = cur_stmt_expr;
11747 tree stmt_expr = begin_stmt_expr ();
11749 cur_stmt_expr = stmt_expr;
11750 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
11751 integral_constant_expression_p);
11752 stmt_expr = finish_stmt_expr (stmt_expr, false);
11753 cur_stmt_expr = old_stmt_expr;
11755 return stmt_expr;
11758 case CONST_DECL:
11759 t = tsubst_copy (t, args, complain, in_decl);
11760 /* As in finish_id_expression, we resolve enumeration constants
11761 to their underlying values. */
11762 if (TREE_CODE (t) == CONST_DECL)
11764 used_types_insert (TREE_TYPE (t));
11765 return DECL_INITIAL (t);
11767 return t;
11769 default:
11770 /* Handle Objective-C++ constructs, if appropriate. */
11772 tree subst
11773 = objcp_tsubst_copy_and_build (t, args, complain,
11774 in_decl, /*function_p=*/false);
11775 if (subst)
11776 return subst;
11778 return tsubst_copy (t, args, complain, in_decl);
11781 #undef RECUR
11784 /* Verify that the instantiated ARGS are valid. For type arguments,
11785 make sure that the type's linkage is ok. For non-type arguments,
11786 make sure they are constants if they are integral or enumerations.
11787 Emit an error under control of COMPLAIN, and return TRUE on error. */
11789 static bool
11790 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
11792 if (ARGUMENT_PACK_P (t))
11794 tree vec = ARGUMENT_PACK_ARGS (t);
11795 int len = TREE_VEC_LENGTH (vec);
11796 bool result = false;
11797 int i;
11799 for (i = 0; i < len; ++i)
11800 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
11801 result = true;
11802 return result;
11804 else if (TYPE_P (t))
11806 /* [basic.link]: A name with no linkage (notably, the name
11807 of a class or enumeration declared in a local scope)
11808 shall not be used to declare an entity with linkage.
11809 This implies that names with no linkage cannot be used as
11810 template arguments. */
11811 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
11813 if (nt)
11815 /* DR 488 makes use of a type with no linkage cause
11816 type deduction to fail. */
11817 if (complain & tf_error)
11819 if (TYPE_ANONYMOUS_P (nt))
11820 error ("%qT is/uses anonymous type", t);
11821 else
11822 error ("template argument for %qD uses local type %qT",
11823 tmpl, t);
11825 return true;
11827 /* In order to avoid all sorts of complications, we do not
11828 allow variably-modified types as template arguments. */
11829 else if (variably_modified_type_p (t, NULL_TREE))
11831 if (complain & tf_error)
11832 error ("%qT is a variably modified type", t);
11833 return true;
11836 /* A non-type argument of integral or enumerated type must be a
11837 constant. */
11838 else if (TREE_TYPE (t)
11839 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
11840 && !TREE_CONSTANT (t))
11842 if (complain & tf_error)
11843 error ("integral expression %qE is not constant", t);
11844 return true;
11846 return false;
11849 static bool
11850 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
11852 int ix, len = DECL_NTPARMS (tmpl);
11853 bool result = false;
11855 for (ix = 0; ix != len; ix++)
11857 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
11858 result = true;
11860 if (result && (complain & tf_error))
11861 error (" trying to instantiate %qD", tmpl);
11862 return result;
11865 /* Instantiate the indicated variable or function template TMPL with
11866 the template arguments in TARG_PTR. */
11868 tree
11869 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
11871 tree fndecl;
11872 tree gen_tmpl;
11873 tree spec;
11874 HOST_WIDE_INT saved_processing_template_decl;
11876 if (tmpl == error_mark_node)
11877 return error_mark_node;
11879 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
11881 /* If this function is a clone, handle it specially. */
11882 if (DECL_CLONED_FUNCTION_P (tmpl))
11884 tree spec;
11885 tree clone;
11887 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
11888 complain);
11889 if (spec == error_mark_node)
11890 return error_mark_node;
11892 /* Look for the clone. */
11893 FOR_EACH_CLONE (clone, spec)
11894 if (DECL_NAME (clone) == DECL_NAME (tmpl))
11895 return clone;
11896 /* We should always have found the clone by now. */
11897 gcc_unreachable ();
11898 return NULL_TREE;
11901 /* Check to see if we already have this specialization. */
11902 spec = retrieve_specialization (tmpl, targ_ptr,
11903 /*class_specializations_p=*/false);
11904 if (spec != NULL_TREE)
11905 return spec;
11907 gen_tmpl = most_general_template (tmpl);
11908 if (tmpl != gen_tmpl)
11910 /* The TMPL is a partial instantiation. To get a full set of
11911 arguments we must add the arguments used to perform the
11912 partial instantiation. */
11913 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
11914 targ_ptr);
11916 /* Check to see if we already have this specialization. */
11917 spec = retrieve_specialization (gen_tmpl, targ_ptr,
11918 /*class_specializations_p=*/false);
11919 if (spec != NULL_TREE)
11920 return spec;
11923 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
11924 complain))
11925 return error_mark_node;
11927 /* We are building a FUNCTION_DECL, during which the access of its
11928 parameters and return types have to be checked. However this
11929 FUNCTION_DECL which is the desired context for access checking
11930 is not built yet. We solve this chicken-and-egg problem by
11931 deferring all checks until we have the FUNCTION_DECL. */
11932 push_deferring_access_checks (dk_deferred);
11934 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11935 (because, for example, we have encountered a non-dependent
11936 function call in the body of a template function and must now
11937 determine which of several overloaded functions will be called),
11938 within the instantiation itself we are not processing a
11939 template. */
11940 saved_processing_template_decl = processing_template_decl;
11941 processing_template_decl = 0;
11942 /* Substitute template parameters to obtain the specialization. */
11943 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
11944 targ_ptr, complain, gen_tmpl);
11945 processing_template_decl = saved_processing_template_decl;
11946 if (fndecl == error_mark_node)
11947 return error_mark_node;
11949 /* Now we know the specialization, compute access previously
11950 deferred. */
11951 push_access_scope (fndecl);
11952 perform_deferred_access_checks ();
11953 pop_access_scope (fndecl);
11954 pop_deferring_access_checks ();
11956 /* The DECL_TI_TEMPLATE should always be the immediate parent
11957 template, not the most general template. */
11958 DECL_TI_TEMPLATE (fndecl) = tmpl;
11960 /* If we've just instantiated the main entry point for a function,
11961 instantiate all the alternate entry points as well. We do this
11962 by cloning the instantiation of the main entry point, not by
11963 instantiating the template clones. */
11964 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
11965 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
11967 return fndecl;
11970 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11971 arguments that are being used when calling it. TARGS is a vector
11972 into which the deduced template arguments are placed.
11974 Return zero for success, 2 for an incomplete match that doesn't resolve
11975 all the types, and 1 for complete failure. An error message will be
11976 printed only for an incomplete match.
11978 If FN is a conversion operator, or we are trying to produce a specific
11979 specialization, RETURN_TYPE is the return type desired.
11981 The EXPLICIT_TARGS are explicit template arguments provided via a
11982 template-id.
11984 The parameter STRICT is one of:
11986 DEDUCE_CALL:
11987 We are deducing arguments for a function call, as in
11988 [temp.deduct.call].
11990 DEDUCE_CONV:
11991 We are deducing arguments for a conversion function, as in
11992 [temp.deduct.conv].
11994 DEDUCE_EXACT:
11995 We are deducing arguments when doing an explicit instantiation
11996 as in [temp.explicit], when determining an explicit specialization
11997 as in [temp.expl.spec], or when taking the address of a function
11998 template, as in [temp.deduct.funcaddr]. */
12001 fn_type_unification (tree fn,
12002 tree explicit_targs,
12003 tree targs,
12004 tree args,
12005 tree return_type,
12006 unification_kind_t strict,
12007 int flags)
12009 tree parms;
12010 tree fntype;
12011 int result;
12012 bool incomplete_argument_packs_p = false;
12014 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12016 fntype = TREE_TYPE (fn);
12017 if (explicit_targs)
12019 /* [temp.deduct]
12021 The specified template arguments must match the template
12022 parameters in kind (i.e., type, nontype, template), and there
12023 must not be more arguments than there are parameters;
12024 otherwise type deduction fails.
12026 Nontype arguments must match the types of the corresponding
12027 nontype template parameters, or must be convertible to the
12028 types of the corresponding nontype parameters as specified in
12029 _temp.arg.nontype_, otherwise type deduction fails.
12031 All references in the function type of the function template
12032 to the corresponding template parameters are replaced by the
12033 specified template argument values. If a substitution in a
12034 template parameter or in the function type of the function
12035 template results in an invalid type, type deduction fails. */
12036 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12037 int i, len = TREE_VEC_LENGTH (tparms);
12038 tree converted_args;
12039 bool incomplete = false;
12041 if (explicit_targs == error_mark_node)
12042 return 1;
12044 converted_args
12045 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12046 /*require_all_args=*/false,
12047 /*use_default_args=*/false));
12048 if (converted_args == error_mark_node)
12049 return 1;
12051 /* Substitute the explicit args into the function type. This is
12052 necessary so that, for instance, explicitly declared function
12053 arguments can match null pointed constants. If we were given
12054 an incomplete set of explicit args, we must not do semantic
12055 processing during substitution as we could create partial
12056 instantiations. */
12057 for (i = 0; i < len; i++)
12059 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12060 bool parameter_pack = false;
12062 /* Dig out the actual parm. */
12063 if (TREE_CODE (parm) == TYPE_DECL
12064 || TREE_CODE (parm) == TEMPLATE_DECL)
12066 parm = TREE_TYPE (parm);
12067 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12069 else if (TREE_CODE (parm) == PARM_DECL)
12071 parm = DECL_INITIAL (parm);
12072 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12075 if (parameter_pack)
12077 int level, idx;
12078 tree targ;
12079 template_parm_level_and_index (parm, &level, &idx);
12081 /* Mark the argument pack as "incomplete". We could
12082 still deduce more arguments during unification. */
12083 targ = TMPL_ARG (converted_args, level, idx);
12084 if (targ)
12086 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12087 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12088 = ARGUMENT_PACK_ARGS (targ);
12091 /* We have some incomplete argument packs. */
12092 incomplete_argument_packs_p = true;
12096 if (incomplete_argument_packs_p)
12097 /* Any substitution is guaranteed to be incomplete if there
12098 are incomplete argument packs, because we can still deduce
12099 more arguments. */
12100 incomplete = 1;
12101 else
12102 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12104 processing_template_decl += incomplete;
12105 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12106 processing_template_decl -= incomplete;
12108 if (fntype == error_mark_node)
12109 return 1;
12111 /* Place the explicitly specified arguments in TARGS. */
12112 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12113 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12116 /* Never do unification on the 'this' parameter. */
12117 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12119 if (return_type)
12121 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12122 args = tree_cons (NULL_TREE, return_type, args);
12125 /* We allow incomplete unification without an error message here
12126 because the standard doesn't seem to explicitly prohibit it. Our
12127 callers must be ready to deal with unification failures in any
12128 event. */
12129 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12130 targs, parms, args, /*subr=*/0,
12131 strict, flags);
12133 if (result == 0 && incomplete_argument_packs_p)
12135 int i, len = NUM_TMPL_ARGS (targs);
12137 /* Clear the "incomplete" flags on all argument packs. */
12138 for (i = 0; i < len; i++)
12140 tree arg = TREE_VEC_ELT (targs, i);
12141 if (ARGUMENT_PACK_P (arg))
12143 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12144 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12149 /* Now that we have bindings for all of the template arguments,
12150 ensure that the arguments deduced for the template template
12151 parameters have compatible template parameter lists. We cannot
12152 check this property before we have deduced all template
12153 arguments, because the template parameter types of a template
12154 template parameter might depend on prior template parameters
12155 deduced after the template template parameter. The following
12156 ill-formed example illustrates this issue:
12158 template<typename T, template<T> class C> void f(C<5>, T);
12160 template<int N> struct X {};
12162 void g() {
12163 f(X<5>(), 5l); // error: template argument deduction fails
12166 The template parameter list of 'C' depends on the template type
12167 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12168 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12169 time that we deduce 'C'. */
12170 if (result == 0
12171 && !template_template_parm_bindings_ok_p
12172 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12173 return 1;
12175 if (result == 0)
12176 /* All is well so far. Now, check:
12178 [temp.deduct]
12180 When all template arguments have been deduced, all uses of
12181 template parameters in nondeduced contexts are replaced with
12182 the corresponding deduced argument values. If the
12183 substitution results in an invalid type, as described above,
12184 type deduction fails. */
12185 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
12186 == error_mark_node)
12187 return 1;
12189 return result;
12192 /* Adjust types before performing type deduction, as described in
12193 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12194 sections are symmetric. PARM is the type of a function parameter
12195 or the return type of the conversion function. ARG is the type of
12196 the argument passed to the call, or the type of the value
12197 initialized with the result of the conversion function.
12198 ARG_EXPR is the original argument expression, which may be null. */
12200 static int
12201 maybe_adjust_types_for_deduction (unification_kind_t strict,
12202 tree* parm,
12203 tree* arg,
12204 tree arg_expr)
12206 int result = 0;
12208 switch (strict)
12210 case DEDUCE_CALL:
12211 break;
12213 case DEDUCE_CONV:
12215 /* Swap PARM and ARG throughout the remainder of this
12216 function; the handling is precisely symmetric since PARM
12217 will initialize ARG rather than vice versa. */
12218 tree* temp = parm;
12219 parm = arg;
12220 arg = temp;
12221 break;
12224 case DEDUCE_EXACT:
12225 /* There is nothing to do in this case. */
12226 return 0;
12228 default:
12229 gcc_unreachable ();
12232 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12234 /* [temp.deduct.call]
12236 If P is not a reference type:
12238 --If A is an array type, the pointer type produced by the
12239 array-to-pointer standard conversion (_conv.array_) is
12240 used in place of A for type deduction; otherwise,
12242 --If A is a function type, the pointer type produced by
12243 the function-to-pointer standard conversion
12244 (_conv.func_) is used in place of A for type deduction;
12245 otherwise,
12247 --If A is a cv-qualified type, the top level
12248 cv-qualifiers of A's type are ignored for type
12249 deduction. */
12250 if (TREE_CODE (*arg) == ARRAY_TYPE)
12251 *arg = build_pointer_type (TREE_TYPE (*arg));
12252 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
12253 *arg = build_pointer_type (*arg);
12254 else
12255 *arg = TYPE_MAIN_VARIANT (*arg);
12258 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12259 of the form T&&, where T is a template parameter, and the argument
12260 is an lvalue, T is deduced as A& */
12261 if (TREE_CODE (*parm) == REFERENCE_TYPE
12262 && TYPE_REF_IS_RVALUE (*parm)
12263 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12264 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12265 && arg_expr && real_lvalue_p (arg_expr))
12266 *arg = build_reference_type (*arg);
12268 /* [temp.deduct.call]
12270 If P is a cv-qualified type, the top level cv-qualifiers
12271 of P's type are ignored for type deduction. If P is a
12272 reference type, the type referred to by P is used for
12273 type deduction. */
12274 *parm = TYPE_MAIN_VARIANT (*parm);
12275 if (TREE_CODE (*parm) == REFERENCE_TYPE)
12277 *parm = TREE_TYPE (*parm);
12278 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12281 /* DR 322. For conversion deduction, remove a reference type on parm
12282 too (which has been swapped into ARG). */
12283 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12284 *arg = TREE_TYPE (*arg);
12286 return result;
12289 /* Most parms like fn_type_unification.
12291 If SUBR is 1, we're being called recursively (to unify the
12292 arguments of a function or method parameter of a function
12293 template). */
12295 static int
12296 type_unification_real (tree tparms,
12297 tree targs,
12298 tree xparms,
12299 tree xargs,
12300 int subr,
12301 unification_kind_t strict,
12302 int flags)
12304 tree parm, arg, arg_expr;
12305 int i;
12306 int ntparms = TREE_VEC_LENGTH (tparms);
12307 int sub_strict;
12308 int saw_undeduced = 0;
12309 tree parms, args;
12311 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12312 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
12313 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
12314 gcc_assert (ntparms > 0);
12316 switch (strict)
12318 case DEDUCE_CALL:
12319 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
12320 | UNIFY_ALLOW_DERIVED);
12321 break;
12323 case DEDUCE_CONV:
12324 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12325 break;
12327 case DEDUCE_EXACT:
12328 sub_strict = UNIFY_ALLOW_NONE;
12329 break;
12331 default:
12332 gcc_unreachable ();
12335 again:
12336 parms = xparms;
12337 args = xargs;
12339 while (parms && parms != void_list_node
12340 && args && args != void_list_node)
12342 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12343 break;
12345 parm = TREE_VALUE (parms);
12346 parms = TREE_CHAIN (parms);
12347 arg = TREE_VALUE (args);
12348 args = TREE_CHAIN (args);
12349 arg_expr = NULL;
12351 if (arg == error_mark_node)
12352 return 1;
12353 if (arg == unknown_type_node)
12354 /* We can't deduce anything from this, but we might get all the
12355 template args from other function args. */
12356 continue;
12358 /* Conversions will be performed on a function argument that
12359 corresponds with a function parameter that contains only
12360 non-deducible template parameters and explicitly specified
12361 template parameters. */
12362 if (!uses_template_parms (parm))
12364 tree type;
12366 if (!TYPE_P (arg))
12367 type = TREE_TYPE (arg);
12368 else
12369 type = arg;
12371 if (same_type_p (parm, type))
12372 continue;
12373 if (strict != DEDUCE_EXACT
12374 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
12375 flags))
12376 continue;
12378 return 1;
12381 if (!TYPE_P (arg))
12383 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12384 if (type_unknown_p (arg))
12386 /* [temp.deduct.type]
12388 A template-argument can be deduced from a pointer to
12389 function or pointer to member function argument if
12390 the set of overloaded functions does not contain
12391 function templates and at most one of a set of
12392 overloaded functions provides a unique match. */
12393 if (resolve_overloaded_unification
12394 (tparms, targs, parm, arg, strict, sub_strict))
12395 continue;
12397 return 1;
12399 arg_expr = arg;
12400 arg = unlowered_expr_type (arg);
12401 if (arg == error_mark_node)
12402 return 1;
12406 int arg_strict = sub_strict;
12408 if (!subr)
12409 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
12410 arg_expr);
12412 if (arg == init_list_type_node && arg_expr)
12413 arg = arg_expr;
12414 if (unify (tparms, targs, parm, arg, arg_strict))
12415 return 1;
12420 if (parms
12421 && parms != void_list_node
12422 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12424 /* Unify the remaining arguments with the pack expansion type. */
12425 tree argvec;
12426 tree parmvec = make_tree_vec (1);
12427 int len = 0;
12428 tree t;
12430 /* Count the number of arguments that remain. */
12431 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
12432 len++;
12434 /* Allocate a TREE_VEC and copy in all of the arguments */
12435 argvec = make_tree_vec (len);
12436 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
12438 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
12439 ++i;
12442 /* Copy the parameter into parmvec. */
12443 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
12444 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
12445 /*call_args_p=*/true, /*subr=*/subr))
12446 return 1;
12448 /* Advance to the end of the list of parameters. */
12449 parms = TREE_CHAIN (parms);
12452 /* Fail if we've reached the end of the parm list, and more args
12453 are present, and the parm list isn't variadic. */
12454 if (args && args != void_list_node && parms == void_list_node)
12455 return 1;
12456 /* Fail if parms are left and they don't have default values. */
12457 if (parms && parms != void_list_node
12458 && TREE_PURPOSE (parms) == NULL_TREE)
12459 return 1;
12461 if (!subr)
12462 for (i = 0; i < ntparms; i++)
12463 if (!TREE_VEC_ELT (targs, i))
12465 tree tparm;
12467 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
12468 continue;
12470 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12472 /* If this is an undeduced nontype parameter that depends on
12473 a type parameter, try another pass; its type may have been
12474 deduced from a later argument than the one from which
12475 this parameter can be deduced. */
12476 if (TREE_CODE (tparm) == PARM_DECL
12477 && uses_template_parms (TREE_TYPE (tparm))
12478 && !saw_undeduced++)
12479 goto again;
12481 /* Core issue #226 (C++0x) [temp.deduct]:
12483 If a template argument has not been deduced, its
12484 default template argument, if any, is used.
12486 When we are in C++98 mode, TREE_PURPOSE will either
12487 be NULL_TREE or ERROR_MARK_NODE, so we do not need
12488 to explicitly check cxx_dialect here. */
12489 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
12491 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
12492 targs, tf_none, NULL_TREE);
12493 if (arg == error_mark_node)
12494 return 1;
12495 else
12497 TREE_VEC_ELT (targs, i) = arg;
12498 continue;
12502 /* If the type parameter is a parameter pack, then it will
12503 be deduced to an empty parameter pack. */
12504 if (template_parameter_pack_p (tparm))
12506 tree arg;
12508 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
12510 arg = make_node (NONTYPE_ARGUMENT_PACK);
12511 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
12512 TREE_CONSTANT (arg) = 1;
12514 else
12515 arg = make_node (TYPE_ARGUMENT_PACK);
12517 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
12519 TREE_VEC_ELT (targs, i) = arg;
12520 continue;
12523 return 2;
12526 return 0;
12529 /* Subroutine of type_unification_real. Args are like the variables
12530 at the call site. ARG is an overloaded function (or template-id);
12531 we try deducing template args from each of the overloads, and if
12532 only one succeeds, we go with that. Modifies TARGS and returns
12533 true on success. */
12535 static bool
12536 resolve_overloaded_unification (tree tparms,
12537 tree targs,
12538 tree parm,
12539 tree arg,
12540 unification_kind_t strict,
12541 int sub_strict)
12543 tree tempargs = copy_node (targs);
12544 int good = 0;
12545 bool addr_p;
12547 if (TREE_CODE (arg) == ADDR_EXPR)
12549 arg = TREE_OPERAND (arg, 0);
12550 addr_p = true;
12552 else
12553 addr_p = false;
12555 if (TREE_CODE (arg) == COMPONENT_REF)
12556 /* Handle `&x' where `x' is some static or non-static member
12557 function name. */
12558 arg = TREE_OPERAND (arg, 1);
12560 if (TREE_CODE (arg) == OFFSET_REF)
12561 arg = TREE_OPERAND (arg, 1);
12563 /* Strip baselink information. */
12564 if (BASELINK_P (arg))
12565 arg = BASELINK_FUNCTIONS (arg);
12567 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
12569 /* If we got some explicit template args, we need to plug them into
12570 the affected templates before we try to unify, in case the
12571 explicit args will completely resolve the templates in question. */
12573 tree expl_subargs = TREE_OPERAND (arg, 1);
12574 arg = TREE_OPERAND (arg, 0);
12576 for (; arg; arg = OVL_NEXT (arg))
12578 tree fn = OVL_CURRENT (arg);
12579 tree subargs, elem;
12581 if (TREE_CODE (fn) != TEMPLATE_DECL)
12582 continue;
12584 ++processing_template_decl;
12585 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
12586 expl_subargs, /*check_ret=*/false);
12587 if (subargs)
12589 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
12590 good += try_one_overload (tparms, targs, tempargs, parm,
12591 elem, strict, sub_strict, addr_p);
12593 --processing_template_decl;
12596 else if (TREE_CODE (arg) != OVERLOAD
12597 && TREE_CODE (arg) != FUNCTION_DECL)
12598 /* If ARG is, for example, "(0, &f)" then its type will be unknown
12599 -- but the deduction does not succeed because the expression is
12600 not just the function on its own. */
12601 return false;
12602 else
12603 for (; arg; arg = OVL_NEXT (arg))
12604 good += try_one_overload (tparms, targs, tempargs, parm,
12605 TREE_TYPE (OVL_CURRENT (arg)),
12606 strict, sub_strict, addr_p);
12608 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12609 to function or pointer to member function argument if the set of
12610 overloaded functions does not contain function templates and at most
12611 one of a set of overloaded functions provides a unique match.
12613 So if we found multiple possibilities, we return success but don't
12614 deduce anything. */
12616 if (good == 1)
12618 int i = TREE_VEC_LENGTH (targs);
12619 for (; i--; )
12620 if (TREE_VEC_ELT (tempargs, i))
12621 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
12623 if (good)
12624 return true;
12626 return false;
12629 /* Subroutine of resolve_overloaded_unification; does deduction for a single
12630 overload. Fills TARGS with any deduced arguments, or error_mark_node if
12631 different overloads deduce different arguments for a given parm.
12632 ADDR_P is true if the expression for which deduction is being
12633 performed was of the form "& fn" rather than simply "fn".
12635 Returns 1 on success. */
12637 static int
12638 try_one_overload (tree tparms,
12639 tree orig_targs,
12640 tree targs,
12641 tree parm,
12642 tree arg,
12643 unification_kind_t strict,
12644 int sub_strict,
12645 bool addr_p)
12647 int nargs;
12648 tree tempargs;
12649 int i;
12651 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12652 to function or pointer to member function argument if the set of
12653 overloaded functions does not contain function templates and at most
12654 one of a set of overloaded functions provides a unique match.
12656 So if this is a template, just return success. */
12658 if (uses_template_parms (arg))
12659 return 1;
12661 if (TREE_CODE (arg) == METHOD_TYPE)
12662 arg = build_ptrmemfunc_type (build_pointer_type (arg));
12663 else if (addr_p)
12664 arg = build_pointer_type (arg);
12666 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12668 /* We don't copy orig_targs for this because if we have already deduced
12669 some template args from previous args, unify would complain when we
12670 try to deduce a template parameter for the same argument, even though
12671 there isn't really a conflict. */
12672 nargs = TREE_VEC_LENGTH (targs);
12673 tempargs = make_tree_vec (nargs);
12675 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
12676 return 0;
12678 /* First make sure we didn't deduce anything that conflicts with
12679 explicitly specified args. */
12680 for (i = nargs; i--; )
12682 tree elt = TREE_VEC_ELT (tempargs, i);
12683 tree oldelt = TREE_VEC_ELT (orig_targs, i);
12685 if (!elt)
12686 /*NOP*/;
12687 else if (uses_template_parms (elt))
12688 /* Since we're unifying against ourselves, we will fill in
12689 template args used in the function parm list with our own
12690 template parms. Discard them. */
12691 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
12692 else if (oldelt && !template_args_equal (oldelt, elt))
12693 return 0;
12696 for (i = nargs; i--; )
12698 tree elt = TREE_VEC_ELT (tempargs, i);
12700 if (elt)
12701 TREE_VEC_ELT (targs, i) = elt;
12704 return 1;
12707 /* PARM is a template class (perhaps with unbound template
12708 parameters). ARG is a fully instantiated type. If ARG can be
12709 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
12710 TARGS are as for unify. */
12712 static tree
12713 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
12715 tree copy_of_targs;
12717 if (!CLASSTYPE_TEMPLATE_INFO (arg)
12718 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
12719 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
12720 return NULL_TREE;
12722 /* We need to make a new template argument vector for the call to
12723 unify. If we used TARGS, we'd clutter it up with the result of
12724 the attempted unification, even if this class didn't work out.
12725 We also don't want to commit ourselves to all the unifications
12726 we've already done, since unification is supposed to be done on
12727 an argument-by-argument basis. In other words, consider the
12728 following pathological case:
12730 template <int I, int J, int K>
12731 struct S {};
12733 template <int I, int J>
12734 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12736 template <int I, int J, int K>
12737 void f(S<I, J, K>, S<I, I, I>);
12739 void g() {
12740 S<0, 0, 0> s0;
12741 S<0, 1, 2> s2;
12743 f(s0, s2);
12746 Now, by the time we consider the unification involving `s2', we
12747 already know that we must have `f<0, 0, 0>'. But, even though
12748 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12749 because there are two ways to unify base classes of S<0, 1, 2>
12750 with S<I, I, I>. If we kept the already deduced knowledge, we
12751 would reject the possibility I=1. */
12752 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
12754 /* If unification failed, we're done. */
12755 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
12756 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
12757 return NULL_TREE;
12759 return arg;
12762 /* Given a template type PARM and a class type ARG, find the unique
12763 base type in ARG that is an instance of PARM. We do not examine
12764 ARG itself; only its base-classes. If there is not exactly one
12765 appropriate base class, return NULL_TREE. PARM may be the type of
12766 a partial specialization, as well as a plain template type. Used
12767 by unify. */
12769 static tree
12770 get_template_base (tree tparms, tree targs, tree parm, tree arg)
12772 tree rval = NULL_TREE;
12773 tree binfo;
12775 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
12777 binfo = TYPE_BINFO (complete_type (arg));
12778 if (!binfo)
12779 /* The type could not be completed. */
12780 return NULL_TREE;
12782 /* Walk in inheritance graph order. The search order is not
12783 important, and this avoids multiple walks of virtual bases. */
12784 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
12786 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
12788 if (r)
12790 /* If there is more than one satisfactory baseclass, then:
12792 [temp.deduct.call]
12794 If they yield more than one possible deduced A, the type
12795 deduction fails.
12797 applies. */
12798 if (rval && !same_type_p (r, rval))
12799 return NULL_TREE;
12801 rval = r;
12805 return rval;
12808 /* Returns the level of DECL, which declares a template parameter. */
12810 static int
12811 template_decl_level (tree decl)
12813 switch (TREE_CODE (decl))
12815 case TYPE_DECL:
12816 case TEMPLATE_DECL:
12817 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
12819 case PARM_DECL:
12820 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
12822 default:
12823 gcc_unreachable ();
12825 return 0;
12828 /* Decide whether ARG can be unified with PARM, considering only the
12829 cv-qualifiers of each type, given STRICT as documented for unify.
12830 Returns nonzero iff the unification is OK on that basis. */
12832 static int
12833 check_cv_quals_for_unify (int strict, tree arg, tree parm)
12835 int arg_quals = cp_type_quals (arg);
12836 int parm_quals = cp_type_quals (parm);
12838 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12839 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12841 /* Although a CVR qualifier is ignored when being applied to a
12842 substituted template parameter ([8.3.2]/1 for example), that
12843 does not apply during deduction [14.8.2.4]/1, (even though
12844 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12845 this). Except when we're allowing additional CV qualifiers
12846 at the outer level [14.8.2.1]/3,1st bullet. */
12847 if ((TREE_CODE (arg) == REFERENCE_TYPE
12848 || TREE_CODE (arg) == FUNCTION_TYPE
12849 || TREE_CODE (arg) == METHOD_TYPE)
12850 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
12851 return 0;
12853 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
12854 && (parm_quals & TYPE_QUAL_RESTRICT))
12855 return 0;
12858 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12859 && (arg_quals & parm_quals) != parm_quals)
12860 return 0;
12862 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
12863 && (parm_quals & arg_quals) != arg_quals)
12864 return 0;
12866 return 1;
12869 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12870 void
12871 template_parm_level_and_index (tree parm, int* level, int* index)
12873 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12874 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12875 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12877 *index = TEMPLATE_TYPE_IDX (parm);
12878 *level = TEMPLATE_TYPE_LEVEL (parm);
12880 else
12882 *index = TEMPLATE_PARM_IDX (parm);
12883 *level = TEMPLATE_PARM_LEVEL (parm);
12887 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12888 expansion at the end of PACKED_PARMS. Returns 0 if the type
12889 deduction succeeds, 1 otherwise. STRICT is the same as in
12890 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12891 call argument list. We'll need to adjust the arguments to make them
12892 types. SUBR tells us if this is from a recursive call to
12893 type_unification_real. */
12895 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
12896 tree packed_args, int strict, bool call_args_p,
12897 bool subr)
12899 tree parm
12900 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
12901 tree pattern = PACK_EXPANSION_PATTERN (parm);
12902 tree pack, packs = NULL_TREE;
12903 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
12904 int len = TREE_VEC_LENGTH (packed_args);
12906 /* Determine the parameter packs we will be deducing from the
12907 pattern, and record their current deductions. */
12908 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
12909 pack; pack = TREE_CHAIN (pack))
12911 tree parm_pack = TREE_VALUE (pack);
12912 int idx, level;
12914 /* Determine the index and level of this parameter pack. */
12915 template_parm_level_and_index (parm_pack, &level, &idx);
12917 /* Keep track of the parameter packs and their corresponding
12918 argument packs. */
12919 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
12920 TREE_TYPE (packs) = make_tree_vec (len - start);
12923 /* Loop through all of the arguments that have not yet been
12924 unified and unify each with the pattern. */
12925 for (i = start; i < len; i++)
12927 tree parm = pattern;
12929 /* For each parameter pack, clear out the deduced value so that
12930 we can deduce it again. */
12931 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12933 int idx, level;
12934 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12936 TMPL_ARG (targs, level, idx) = NULL_TREE;
12939 /* Unify the pattern with the current argument. */
12941 tree arg = TREE_VEC_ELT (packed_args, i);
12942 tree arg_expr = NULL_TREE;
12943 int arg_strict = strict;
12944 bool skip_arg_p = false;
12946 if (call_args_p)
12948 int sub_strict;
12950 /* This mirrors what we do in type_unification_real. */
12951 switch (strict)
12953 case DEDUCE_CALL:
12954 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
12955 | UNIFY_ALLOW_MORE_CV_QUAL
12956 | UNIFY_ALLOW_DERIVED);
12957 break;
12959 case DEDUCE_CONV:
12960 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12961 break;
12963 case DEDUCE_EXACT:
12964 sub_strict = UNIFY_ALLOW_NONE;
12965 break;
12967 default:
12968 gcc_unreachable ();
12971 if (!TYPE_P (arg))
12973 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12974 if (type_unknown_p (arg))
12976 /* [temp.deduct.type] A template-argument can be
12977 deduced from a pointer to function or pointer
12978 to member function argument if the set of
12979 overloaded functions does not contain function
12980 templates and at most one of a set of
12981 overloaded functions provides a unique
12982 match. */
12984 if (resolve_overloaded_unification
12985 (tparms, targs, parm, arg, strict, sub_strict)
12986 != 0)
12987 return 1;
12988 skip_arg_p = true;
12991 if (!skip_arg_p)
12993 arg_expr = arg;
12994 arg = unlowered_expr_type (arg);
12995 if (arg == error_mark_node)
12996 return 1;
13000 arg_strict = sub_strict;
13002 if (!subr)
13003 arg_strict |=
13004 maybe_adjust_types_for_deduction (strict, &parm, &arg,
13005 arg_expr);
13008 if (!skip_arg_p)
13010 if (unify (tparms, targs, parm, arg, arg_strict))
13011 return 1;
13015 /* For each parameter pack, collect the deduced value. */
13016 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13018 int idx, level;
13019 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13021 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13022 TMPL_ARG (targs, level, idx);
13026 /* Verify that the results of unification with the parameter packs
13027 produce results consistent with what we've seen before, and make
13028 the deduced argument packs available. */
13029 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13031 tree old_pack = TREE_VALUE (pack);
13032 tree new_args = TREE_TYPE (pack);
13033 int i, len = TREE_VEC_LENGTH (new_args);
13034 bool nondeduced_p = false;
13036 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13037 actually deduce anything. */
13038 for (i = 0; i < len && !nondeduced_p; ++i)
13039 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13040 nondeduced_p = true;
13041 if (nondeduced_p)
13042 continue;
13044 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13046 /* Prepend the explicit arguments onto NEW_ARGS. */
13047 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13048 tree old_args = new_args;
13049 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13050 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13052 /* Copy the explicit arguments. */
13053 new_args = make_tree_vec (len);
13054 for (i = 0; i < explicit_len; i++)
13055 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13057 /* Copy the deduced arguments. */
13058 for (; i < len; i++)
13059 TREE_VEC_ELT (new_args, i) =
13060 TREE_VEC_ELT (old_args, i - explicit_len);
13063 if (!old_pack)
13065 tree result;
13066 int idx, level;
13068 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13070 /* Build the deduced *_ARGUMENT_PACK. */
13071 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13073 result = make_node (NONTYPE_ARGUMENT_PACK);
13074 TREE_TYPE (result) =
13075 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13076 TREE_CONSTANT (result) = 1;
13078 else
13079 result = make_node (TYPE_ARGUMENT_PACK);
13081 SET_ARGUMENT_PACK_ARGS (result, new_args);
13083 /* Note the deduced argument packs for this parameter
13084 pack. */
13085 TMPL_ARG (targs, level, idx) = result;
13087 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13088 && (ARGUMENT_PACK_ARGS (old_pack)
13089 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13091 /* We only had the explicitly-provided arguments before, but
13092 now we have a complete set of arguments. */
13093 int idx, level;
13094 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13095 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13097 /* Keep the original deduced argument pack. */
13098 TMPL_ARG (targs, level, idx) = old_pack;
13100 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13101 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13102 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13104 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13105 new_args))
13106 /* Inconsistent unification of this parameter pack. */
13107 return 1;
13108 else
13110 int idx, level;
13112 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13114 /* Keep the original deduced argument pack. */
13115 TMPL_ARG (targs, level, idx) = old_pack;
13119 return 0;
13122 /* Deduce the value of template parameters. TPARMS is the (innermost)
13123 set of template parameters to a template. TARGS is the bindings
13124 for those template parameters, as determined thus far; TARGS may
13125 include template arguments for outer levels of template parameters
13126 as well. PARM is a parameter to a template function, or a
13127 subcomponent of that parameter; ARG is the corresponding argument.
13128 This function attempts to match PARM with ARG in a manner
13129 consistent with the existing assignments in TARGS. If more values
13130 are deduced, then TARGS is updated.
13132 Returns 0 if the type deduction succeeds, 1 otherwise. The
13133 parameter STRICT is a bitwise or of the following flags:
13135 UNIFY_ALLOW_NONE:
13136 Require an exact match between PARM and ARG.
13137 UNIFY_ALLOW_MORE_CV_QUAL:
13138 Allow the deduced ARG to be more cv-qualified (by qualification
13139 conversion) than ARG.
13140 UNIFY_ALLOW_LESS_CV_QUAL:
13141 Allow the deduced ARG to be less cv-qualified than ARG.
13142 UNIFY_ALLOW_DERIVED:
13143 Allow the deduced ARG to be a template base class of ARG,
13144 or a pointer to a template base class of the type pointed to by
13145 ARG.
13146 UNIFY_ALLOW_INTEGER:
13147 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13148 case for more information.
13149 UNIFY_ALLOW_OUTER_LEVEL:
13150 This is the outermost level of a deduction. Used to determine validity
13151 of qualification conversions. A valid qualification conversion must
13152 have const qualified pointers leading up to the inner type which
13153 requires additional CV quals, except at the outer level, where const
13154 is not required [conv.qual]. It would be normal to set this flag in
13155 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13156 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13157 This is the outermost level of a deduction, and PARM can be more CV
13158 qualified at this point.
13159 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13160 This is the outermost level of a deduction, and PARM can be less CV
13161 qualified at this point. */
13163 static int
13164 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
13166 int idx;
13167 tree targ;
13168 tree tparm;
13169 int strict_in = strict;
13171 /* I don't think this will do the right thing with respect to types.
13172 But the only case I've seen it in so far has been array bounds, where
13173 signedness is the only information lost, and I think that will be
13174 okay. */
13175 while (TREE_CODE (parm) == NOP_EXPR)
13176 parm = TREE_OPERAND (parm, 0);
13178 if (arg == error_mark_node)
13179 return 1;
13180 if (arg == unknown_type_node
13181 || arg == init_list_type_node)
13182 /* We can't deduce anything from this, but we might get all the
13183 template args from other function args. */
13184 return 0;
13186 /* If PARM uses template parameters, then we can't bail out here,
13187 even if ARG == PARM, since we won't record unifications for the
13188 template parameters. We might need them if we're trying to
13189 figure out which of two things is more specialized. */
13190 if (arg == parm && !uses_template_parms (parm))
13191 return 0;
13193 /* Handle init lists early, so the rest of the function can assume
13194 we're dealing with a type. */
13195 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13197 tree elt, elttype;
13198 unsigned i;
13200 if (!is_std_init_list (parm))
13201 /* We can only deduce from an initializer list argument if the
13202 parameter is std::initializer_list; otherwise this is a
13203 non-deduced context. */
13204 return 0;
13206 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13208 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13210 int elt_strict = strict;
13211 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
13213 tree type = TREE_TYPE (elt);
13214 /* It should only be possible to get here for a call. */
13215 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13216 elt_strict |= maybe_adjust_types_for_deduction
13217 (DEDUCE_CALL, &elttype, &type, elt);
13218 elt = type;
13221 if (unify (tparms, targs, elttype, elt, elt_strict))
13222 return 1;
13224 return 0;
13227 /* Immediately reject some pairs that won't unify because of
13228 cv-qualification mismatches. */
13229 if (TREE_CODE (arg) == TREE_CODE (parm)
13230 && TYPE_P (arg)
13231 /* It is the elements of the array which hold the cv quals of an array
13232 type, and the elements might be template type parms. We'll check
13233 when we recurse. */
13234 && TREE_CODE (arg) != ARRAY_TYPE
13235 /* We check the cv-qualifiers when unifying with template type
13236 parameters below. We want to allow ARG `const T' to unify with
13237 PARM `T' for example, when computing which of two templates
13238 is more specialized, for example. */
13239 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
13240 && !check_cv_quals_for_unify (strict_in, arg, parm))
13241 return 1;
13243 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
13244 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
13245 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13246 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13247 strict &= ~UNIFY_ALLOW_DERIVED;
13248 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13249 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
13251 switch (TREE_CODE (parm))
13253 case TYPENAME_TYPE:
13254 case SCOPE_REF:
13255 case UNBOUND_CLASS_TEMPLATE:
13256 /* In a type which contains a nested-name-specifier, template
13257 argument values cannot be deduced for template parameters used
13258 within the nested-name-specifier. */
13259 return 0;
13261 case TEMPLATE_TYPE_PARM:
13262 case TEMPLATE_TEMPLATE_PARM:
13263 case BOUND_TEMPLATE_TEMPLATE_PARM:
13264 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13265 if (tparm == error_mark_node)
13266 return 1;
13268 if (TEMPLATE_TYPE_LEVEL (parm)
13269 != template_decl_level (tparm))
13270 /* The PARM is not one we're trying to unify. Just check
13271 to see if it matches ARG. */
13272 return (TREE_CODE (arg) == TREE_CODE (parm)
13273 && same_type_p (parm, arg)) ? 0 : 1;
13274 idx = TEMPLATE_TYPE_IDX (parm);
13275 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13276 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
13278 /* Check for mixed types and values. */
13279 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13280 && TREE_CODE (tparm) != TYPE_DECL)
13281 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13282 && TREE_CODE (tparm) != TEMPLATE_DECL))
13283 return 1;
13285 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13287 /* ARG must be constructed from a template class or a template
13288 template parameter. */
13289 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
13290 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
13291 return 1;
13294 tree parmvec = TYPE_TI_ARGS (parm);
13295 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
13296 tree parm_parms
13297 = DECL_INNERMOST_TEMPLATE_PARMS
13298 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
13299 int i, len;
13300 int parm_variadic_p = 0;
13302 /* The resolution to DR150 makes clear that default
13303 arguments for an N-argument may not be used to bind T
13304 to a template template parameter with fewer than N
13305 parameters. It is not safe to permit the binding of
13306 default arguments as an extension, as that may change
13307 the meaning of a conforming program. Consider:
13309 struct Dense { static const unsigned int dim = 1; };
13311 template <template <typename> class View,
13312 typename Block>
13313 void operator+(float, View<Block> const&);
13315 template <typename Block,
13316 unsigned int Dim = Block::dim>
13317 struct Lvalue_proxy { operator float() const; };
13319 void
13320 test_1d (void) {
13321 Lvalue_proxy<Dense> p;
13322 float b;
13323 b + p;
13326 Here, if Lvalue_proxy is permitted to bind to View, then
13327 the global operator+ will be used; if they are not, the
13328 Lvalue_proxy will be converted to float. */
13329 if (coerce_template_parms (parm_parms,
13330 argvec,
13331 TYPE_TI_TEMPLATE (parm),
13332 tf_none,
13333 /*require_all_args=*/true,
13334 /*use_default_args=*/false)
13335 == error_mark_node)
13336 return 1;
13338 /* Deduce arguments T, i from TT<T> or TT<i>.
13339 We check each element of PARMVEC and ARGVEC individually
13340 rather than the whole TREE_VEC since they can have
13341 different number of elements. */
13343 parmvec = expand_template_argument_pack (parmvec);
13344 argvec = expand_template_argument_pack (argvec);
13346 len = TREE_VEC_LENGTH (parmvec);
13348 /* Check if the parameters end in a pack, making them
13349 variadic. */
13350 if (len > 0
13351 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
13352 parm_variadic_p = 1;
13354 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
13355 return 1;
13357 for (i = 0; i < len - parm_variadic_p; ++i)
13359 if (unify (tparms, targs,
13360 TREE_VEC_ELT (parmvec, i),
13361 TREE_VEC_ELT (argvec, i),
13362 UNIFY_ALLOW_NONE))
13363 return 1;
13366 if (parm_variadic_p
13367 && unify_pack_expansion (tparms, targs,
13368 parmvec, argvec,
13369 UNIFY_ALLOW_NONE,
13370 /*call_args_p=*/false,
13371 /*subr=*/false))
13372 return 1;
13374 arg = TYPE_TI_TEMPLATE (arg);
13376 /* Fall through to deduce template name. */
13379 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13380 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13382 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
13384 /* Simple cases: Value already set, does match or doesn't. */
13385 if (targ != NULL_TREE && template_args_equal (targ, arg))
13386 return 0;
13387 else if (targ)
13388 return 1;
13390 else
13392 /* If PARM is `const T' and ARG is only `int', we don't have
13393 a match unless we are allowing additional qualification.
13394 If ARG is `const int' and PARM is just `T' that's OK;
13395 that binds `const int' to `T'. */
13396 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
13397 arg, parm))
13398 return 1;
13400 /* Consider the case where ARG is `const volatile int' and
13401 PARM is `const T'. Then, T should be `volatile int'. */
13402 arg = cp_build_qualified_type_real
13403 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
13404 if (arg == error_mark_node)
13405 return 1;
13407 /* Simple cases: Value already set, does match or doesn't. */
13408 if (targ != NULL_TREE && same_type_p (targ, arg))
13409 return 0;
13410 else if (targ)
13411 return 1;
13413 /* Make sure that ARG is not a variable-sized array. (Note
13414 that were talking about variable-sized arrays (like
13415 `int[n]'), rather than arrays of unknown size (like
13416 `int[]').) We'll get very confused by such a type since
13417 the bound of the array will not be computable in an
13418 instantiation. Besides, such types are not allowed in
13419 ISO C++, so we can do as we please here. */
13420 if (variably_modified_type_p (arg, NULL_TREE))
13421 return 1;
13424 /* If ARG is a parameter pack or an expansion, we cannot unify
13425 against it unless PARM is also a parameter pack. */
13426 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13427 && !template_parameter_pack_p (parm))
13428 return 1;
13430 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13431 return 0;
13433 case TEMPLATE_PARM_INDEX:
13434 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13435 if (tparm == error_mark_node)
13436 return 1;
13438 if (TEMPLATE_PARM_LEVEL (parm)
13439 != template_decl_level (tparm))
13440 /* The PARM is not one we're trying to unify. Just check
13441 to see if it matches ARG. */
13442 return !(TREE_CODE (arg) == TREE_CODE (parm)
13443 && cp_tree_equal (parm, arg));
13445 idx = TEMPLATE_PARM_IDX (parm);
13446 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13448 if (targ)
13449 return !cp_tree_equal (targ, arg);
13451 /* [temp.deduct.type] If, in the declaration of a function template
13452 with a non-type template-parameter, the non-type
13453 template-parameter is used in an expression in the function
13454 parameter-list and, if the corresponding template-argument is
13455 deduced, the template-argument type shall match the type of the
13456 template-parameter exactly, except that a template-argument
13457 deduced from an array bound may be of any integral type.
13458 The non-type parameter might use already deduced type parameters. */
13459 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
13460 if (!TREE_TYPE (arg))
13461 /* Template-parameter dependent expression. Just accept it for now.
13462 It will later be processed in convert_template_argument. */
13464 else if (same_type_p (TREE_TYPE (arg), tparm))
13465 /* OK */;
13466 else if ((strict & UNIFY_ALLOW_INTEGER)
13467 && (TREE_CODE (tparm) == INTEGER_TYPE
13468 || TREE_CODE (tparm) == BOOLEAN_TYPE))
13469 /* Convert the ARG to the type of PARM; the deduced non-type
13470 template argument must exactly match the types of the
13471 corresponding parameter. */
13472 arg = fold (build_nop (tparm, arg));
13473 else if (uses_template_parms (tparm))
13474 /* We haven't deduced the type of this parameter yet. Try again
13475 later. */
13476 return 0;
13477 else
13478 return 1;
13480 /* If ARG is a parameter pack or an expansion, we cannot unify
13481 against it unless PARM is also a parameter pack. */
13482 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13483 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
13484 return 1;
13486 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13487 return 0;
13489 case PTRMEM_CST:
13491 /* A pointer-to-member constant can be unified only with
13492 another constant. */
13493 if (TREE_CODE (arg) != PTRMEM_CST)
13494 return 1;
13496 /* Just unify the class member. It would be useless (and possibly
13497 wrong, depending on the strict flags) to unify also
13498 PTRMEM_CST_CLASS, because we want to be sure that both parm and
13499 arg refer to the same variable, even if through different
13500 classes. For instance:
13502 struct A { int x; };
13503 struct B : A { };
13505 Unification of &A::x and &B::x must succeed. */
13506 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
13507 PTRMEM_CST_MEMBER (arg), strict);
13510 case POINTER_TYPE:
13512 if (TREE_CODE (arg) != POINTER_TYPE)
13513 return 1;
13515 /* [temp.deduct.call]
13517 A can be another pointer or pointer to member type that can
13518 be converted to the deduced A via a qualification
13519 conversion (_conv.qual_).
13521 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
13522 This will allow for additional cv-qualification of the
13523 pointed-to types if appropriate. */
13525 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
13526 /* The derived-to-base conversion only persists through one
13527 level of pointers. */
13528 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
13530 return unify (tparms, targs, TREE_TYPE (parm),
13531 TREE_TYPE (arg), strict);
13534 case REFERENCE_TYPE:
13535 if (TREE_CODE (arg) != REFERENCE_TYPE)
13536 return 1;
13537 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13538 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13540 case ARRAY_TYPE:
13541 if (TREE_CODE (arg) != ARRAY_TYPE)
13542 return 1;
13543 if ((TYPE_DOMAIN (parm) == NULL_TREE)
13544 != (TYPE_DOMAIN (arg) == NULL_TREE))
13545 return 1;
13546 if (TYPE_DOMAIN (parm) != NULL_TREE)
13548 tree parm_max;
13549 tree arg_max;
13550 bool parm_cst;
13551 bool arg_cst;
13553 /* Our representation of array types uses "N - 1" as the
13554 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
13555 not an integer constant. We cannot unify arbitrarily
13556 complex expressions, so we eliminate the MINUS_EXPRs
13557 here. */
13558 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
13559 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
13560 if (!parm_cst)
13562 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
13563 parm_max = TREE_OPERAND (parm_max, 0);
13565 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
13566 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
13567 if (!arg_cst)
13569 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
13570 trying to unify the type of a variable with the type
13571 of a template parameter. For example:
13573 template <unsigned int N>
13574 void f (char (&) [N]);
13575 int g();
13576 void h(int i) {
13577 char a[g(i)];
13578 f(a);
13581 Here, the type of the ARG will be "int [g(i)]", and
13582 may be a SAVE_EXPR, etc. */
13583 if (TREE_CODE (arg_max) != MINUS_EXPR)
13584 return 1;
13585 arg_max = TREE_OPERAND (arg_max, 0);
13588 /* If only one of the bounds used a MINUS_EXPR, compensate
13589 by adding one to the other bound. */
13590 if (parm_cst && !arg_cst)
13591 parm_max = fold_build2 (PLUS_EXPR,
13592 integer_type_node,
13593 parm_max,
13594 integer_one_node);
13595 else if (arg_cst && !parm_cst)
13596 arg_max = fold_build2 (PLUS_EXPR,
13597 integer_type_node,
13598 arg_max,
13599 integer_one_node);
13601 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
13602 return 1;
13604 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13605 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13607 case REAL_TYPE:
13608 case COMPLEX_TYPE:
13609 case VECTOR_TYPE:
13610 case INTEGER_TYPE:
13611 case BOOLEAN_TYPE:
13612 case ENUMERAL_TYPE:
13613 case VOID_TYPE:
13614 if (TREE_CODE (arg) != TREE_CODE (parm))
13615 return 1;
13617 /* We have already checked cv-qualification at the top of the
13618 function. */
13619 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
13620 return 1;
13622 /* As far as unification is concerned, this wins. Later checks
13623 will invalidate it if necessary. */
13624 return 0;
13626 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
13627 /* Type INTEGER_CST can come from ordinary constant template args. */
13628 case INTEGER_CST:
13629 while (TREE_CODE (arg) == NOP_EXPR)
13630 arg = TREE_OPERAND (arg, 0);
13632 if (TREE_CODE (arg) != INTEGER_CST)
13633 return 1;
13634 return !tree_int_cst_equal (parm, arg);
13636 case TREE_VEC:
13638 int i;
13639 if (TREE_CODE (arg) != TREE_VEC)
13640 return 1;
13641 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
13642 return 1;
13643 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
13644 if (unify (tparms, targs,
13645 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
13646 UNIFY_ALLOW_NONE))
13647 return 1;
13648 return 0;
13651 case RECORD_TYPE:
13652 case UNION_TYPE:
13653 if (TREE_CODE (arg) != TREE_CODE (parm))
13654 return 1;
13656 if (TYPE_PTRMEMFUNC_P (parm))
13658 if (!TYPE_PTRMEMFUNC_P (arg))
13659 return 1;
13661 return unify (tparms, targs,
13662 TYPE_PTRMEMFUNC_FN_TYPE (parm),
13663 TYPE_PTRMEMFUNC_FN_TYPE (arg),
13664 strict);
13667 if (CLASSTYPE_TEMPLATE_INFO (parm))
13669 tree t = NULL_TREE;
13671 if (strict_in & UNIFY_ALLOW_DERIVED)
13673 /* First, we try to unify the PARM and ARG directly. */
13674 t = try_class_unification (tparms, targs,
13675 parm, arg);
13677 if (!t)
13679 /* Fallback to the special case allowed in
13680 [temp.deduct.call]:
13682 If P is a class, and P has the form
13683 template-id, then A can be a derived class of
13684 the deduced A. Likewise, if P is a pointer to
13685 a class of the form template-id, A can be a
13686 pointer to a derived class pointed to by the
13687 deduced A. */
13688 t = get_template_base (tparms, targs, parm, arg);
13690 if (!t)
13691 return 1;
13694 else if (CLASSTYPE_TEMPLATE_INFO (arg)
13695 && (CLASSTYPE_TI_TEMPLATE (parm)
13696 == CLASSTYPE_TI_TEMPLATE (arg)))
13697 /* Perhaps PARM is something like S<U> and ARG is S<int>.
13698 Then, we should unify `int' and `U'. */
13699 t = arg;
13700 else
13701 /* There's no chance of unification succeeding. */
13702 return 1;
13704 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
13705 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
13707 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
13708 return 1;
13709 return 0;
13711 case METHOD_TYPE:
13712 case FUNCTION_TYPE:
13713 if (TREE_CODE (arg) != TREE_CODE (parm))
13714 return 1;
13716 /* CV qualifications for methods can never be deduced, they must
13717 match exactly. We need to check them explicitly here,
13718 because type_unification_real treats them as any other
13719 cv-qualified parameter. */
13720 if (TREE_CODE (parm) == METHOD_TYPE
13721 && (!check_cv_quals_for_unify
13722 (UNIFY_ALLOW_NONE,
13723 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
13724 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
13725 return 1;
13727 if (unify (tparms, targs, TREE_TYPE (parm),
13728 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
13729 return 1;
13730 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
13731 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
13732 LOOKUP_NORMAL);
13734 case OFFSET_TYPE:
13735 /* Unify a pointer to member with a pointer to member function, which
13736 deduces the type of the member as a function type. */
13737 if (TYPE_PTRMEMFUNC_P (arg))
13739 tree method_type;
13740 tree fntype;
13741 cp_cv_quals cv_quals;
13743 /* Check top-level cv qualifiers */
13744 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
13745 return 1;
13747 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
13748 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
13749 return 1;
13751 /* Determine the type of the function we are unifying against. */
13752 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
13753 fntype =
13754 build_function_type (TREE_TYPE (method_type),
13755 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
13757 /* Extract the cv-qualifiers of the member function from the
13758 implicit object parameter and place them on the function
13759 type to be restored later. */
13760 cv_quals =
13761 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
13762 fntype = build_qualified_type (fntype, cv_quals);
13763 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
13766 if (TREE_CODE (arg) != OFFSET_TYPE)
13767 return 1;
13768 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
13769 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
13770 return 1;
13771 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13772 strict);
13774 case CONST_DECL:
13775 if (DECL_TEMPLATE_PARM_P (parm))
13776 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
13777 if (arg != integral_constant_value (parm))
13778 return 1;
13779 return 0;
13781 case FIELD_DECL:
13782 case TEMPLATE_DECL:
13783 /* Matched cases are handled by the ARG == PARM test above. */
13784 return 1;
13786 case TYPE_ARGUMENT_PACK:
13787 case NONTYPE_ARGUMENT_PACK:
13789 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
13790 tree packed_args = ARGUMENT_PACK_ARGS (arg);
13791 int i, len = TREE_VEC_LENGTH (packed_parms);
13792 int argslen = TREE_VEC_LENGTH (packed_args);
13793 int parm_variadic_p = 0;
13795 for (i = 0; i < len; ++i)
13797 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
13799 if (i == len - 1)
13800 /* We can unify against something with a trailing
13801 parameter pack. */
13802 parm_variadic_p = 1;
13803 else
13804 /* Since there is something following the pack
13805 expansion, we cannot unify this template argument
13806 list. */
13807 return 0;
13812 /* If we don't have enough arguments to satisfy the parameters
13813 (not counting the pack expression at the end), or we have
13814 too many arguments for a parameter list that doesn't end in
13815 a pack expression, we can't unify. */
13816 if (argslen < (len - parm_variadic_p)
13817 || (argslen > len && !parm_variadic_p))
13818 return 1;
13820 /* Unify all of the parameters that precede the (optional)
13821 pack expression. */
13822 for (i = 0; i < len - parm_variadic_p; ++i)
13824 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
13825 TREE_VEC_ELT (packed_args, i), strict))
13826 return 1;
13829 if (parm_variadic_p)
13830 return unify_pack_expansion (tparms, targs,
13831 packed_parms, packed_args,
13832 strict, /*call_args_p=*/false,
13833 /*subr=*/false);
13834 return 0;
13837 break;
13839 case TYPEOF_TYPE:
13840 case DECLTYPE_TYPE:
13841 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
13842 nodes. */
13843 return 0;
13845 case ERROR_MARK:
13846 /* Unification fails if we hit an error node. */
13847 return 1;
13849 default:
13850 gcc_assert (EXPR_P (parm));
13852 /* We must be looking at an expression. This can happen with
13853 something like:
13855 template <int I>
13856 void foo(S<I>, S<I + 2>);
13858 This is a "nondeduced context":
13860 [deduct.type]
13862 The nondeduced contexts are:
13864 --A type that is a template-id in which one or more of
13865 the template-arguments is an expression that references
13866 a template-parameter.
13868 In these cases, we assume deduction succeeded, but don't
13869 actually infer any unifications. */
13871 if (!uses_template_parms (parm)
13872 && !template_args_equal (parm, arg))
13873 return 1;
13874 else
13875 return 0;
13879 /* Note that DECL can be defined in this translation unit, if
13880 required. */
13882 static void
13883 mark_definable (tree decl)
13885 tree clone;
13886 DECL_NOT_REALLY_EXTERN (decl) = 1;
13887 FOR_EACH_CLONE (clone, decl)
13888 DECL_NOT_REALLY_EXTERN (clone) = 1;
13891 /* Called if RESULT is explicitly instantiated, or is a member of an
13892 explicitly instantiated class. */
13894 void
13895 mark_decl_instantiated (tree result, int extern_p)
13897 SET_DECL_EXPLICIT_INSTANTIATION (result);
13899 /* If this entity has already been written out, it's too late to
13900 make any modifications. */
13901 if (TREE_ASM_WRITTEN (result))
13902 return;
13904 if (TREE_CODE (result) != FUNCTION_DECL)
13905 /* The TREE_PUBLIC flag for function declarations will have been
13906 set correctly by tsubst. */
13907 TREE_PUBLIC (result) = 1;
13909 /* This might have been set by an earlier implicit instantiation. */
13910 DECL_COMDAT (result) = 0;
13912 if (extern_p)
13913 DECL_NOT_REALLY_EXTERN (result) = 0;
13914 else
13916 mark_definable (result);
13917 /* Always make artificials weak. */
13918 if (DECL_ARTIFICIAL (result) && flag_weak)
13919 comdat_linkage (result);
13920 /* For WIN32 we also want to put explicit instantiations in
13921 linkonce sections. */
13922 else if (TREE_PUBLIC (result))
13923 maybe_make_one_only (result);
13926 /* If EXTERN_P, then this function will not be emitted -- unless
13927 followed by an explicit instantiation, at which point its linkage
13928 will be adjusted. If !EXTERN_P, then this function will be
13929 emitted here. In neither circumstance do we want
13930 import_export_decl to adjust the linkage. */
13931 DECL_INTERFACE_KNOWN (result) = 1;
13934 /* Given two function templates PAT1 and PAT2, return:
13936 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
13937 -1 if PAT2 is more specialized than PAT1.
13938 0 if neither is more specialized.
13940 LEN indicates the number of parameters we should consider
13941 (defaulted parameters should not be considered).
13943 The 1998 std underspecified function template partial ordering, and
13944 DR214 addresses the issue. We take pairs of arguments, one from
13945 each of the templates, and deduce them against each other. One of
13946 the templates will be more specialized if all the *other*
13947 template's arguments deduce against its arguments and at least one
13948 of its arguments *does* *not* deduce against the other template's
13949 corresponding argument. Deduction is done as for class templates.
13950 The arguments used in deduction have reference and top level cv
13951 qualifiers removed. Iff both arguments were originally reference
13952 types *and* deduction succeeds in both directions, the template
13953 with the more cv-qualified argument wins for that pairing (if
13954 neither is more cv-qualified, they both are equal). Unlike regular
13955 deduction, after all the arguments have been deduced in this way,
13956 we do *not* verify the deduced template argument values can be
13957 substituted into non-deduced contexts, nor do we have to verify
13958 that all template arguments have been deduced. */
13961 more_specialized_fn (tree pat1, tree pat2, int len)
13963 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
13964 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
13965 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
13966 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
13967 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
13968 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
13969 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
13970 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
13971 int better1 = 0;
13972 int better2 = 0;
13974 /* Remove the this parameter from non-static member functions. If
13975 one is a non-static member function and the other is not a static
13976 member function, remove the first parameter from that function
13977 also. This situation occurs for operator functions where we
13978 locate both a member function (with this pointer) and non-member
13979 operator (with explicit first operand). */
13980 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
13982 len--; /* LEN is the number of significant arguments for DECL1 */
13983 args1 = TREE_CHAIN (args1);
13984 if (!DECL_STATIC_FUNCTION_P (decl2))
13985 args2 = TREE_CHAIN (args2);
13987 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
13989 args2 = TREE_CHAIN (args2);
13990 if (!DECL_STATIC_FUNCTION_P (decl1))
13992 len--;
13993 args1 = TREE_CHAIN (args1);
13997 /* If only one is a conversion operator, they are unordered. */
13998 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
13999 return 0;
14001 /* Consider the return type for a conversion function */
14002 if (DECL_CONV_FN_P (decl1))
14004 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14005 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14006 len++;
14009 processing_template_decl++;
14011 while (len--
14012 /* Stop when an ellipsis is seen. */
14013 && args1 != NULL_TREE && args2 != NULL_TREE)
14015 tree arg1 = TREE_VALUE (args1);
14016 tree arg2 = TREE_VALUE (args2);
14017 int deduce1, deduce2;
14018 int quals1 = -1;
14019 int quals2 = -1;
14021 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14022 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14024 /* When both arguments are pack expansions, we need only
14025 unify the patterns themselves. */
14026 arg1 = PACK_EXPANSION_PATTERN (arg1);
14027 arg2 = PACK_EXPANSION_PATTERN (arg2);
14029 /* This is the last comparison we need to do. */
14030 len = 0;
14033 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14035 arg1 = TREE_TYPE (arg1);
14036 quals1 = cp_type_quals (arg1);
14039 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14041 arg2 = TREE_TYPE (arg2);
14042 quals2 = cp_type_quals (arg2);
14045 if ((quals1 < 0) != (quals2 < 0))
14047 /* Only of the args is a reference, see if we should apply
14048 array/function pointer decay to it. This is not part of
14049 DR214, but is, IMHO, consistent with the deduction rules
14050 for the function call itself, and with our earlier
14051 implementation of the underspecified partial ordering
14052 rules. (nathan). */
14053 if (quals1 >= 0)
14055 switch (TREE_CODE (arg1))
14057 case ARRAY_TYPE:
14058 arg1 = TREE_TYPE (arg1);
14059 /* FALLTHROUGH. */
14060 case FUNCTION_TYPE:
14061 arg1 = build_pointer_type (arg1);
14062 break;
14064 default:
14065 break;
14068 else
14070 switch (TREE_CODE (arg2))
14072 case ARRAY_TYPE:
14073 arg2 = TREE_TYPE (arg2);
14074 /* FALLTHROUGH. */
14075 case FUNCTION_TYPE:
14076 arg2 = build_pointer_type (arg2);
14077 break;
14079 default:
14080 break;
14085 arg1 = TYPE_MAIN_VARIANT (arg1);
14086 arg2 = TYPE_MAIN_VARIANT (arg2);
14088 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14090 int i, len2 = list_length (args2);
14091 tree parmvec = make_tree_vec (1);
14092 tree argvec = make_tree_vec (len2);
14093 tree ta = args2;
14095 /* Setup the parameter vector, which contains only ARG1. */
14096 TREE_VEC_ELT (parmvec, 0) = arg1;
14098 /* Setup the argument vector, which contains the remaining
14099 arguments. */
14100 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14101 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14103 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14104 argvec, UNIFY_ALLOW_NONE,
14105 /*call_args_p=*/false,
14106 /*subr=*/0);
14108 /* We cannot deduce in the other direction, because ARG1 is
14109 a pack expansion but ARG2 is not. */
14110 deduce2 = 0;
14112 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14114 int i, len1 = list_length (args1);
14115 tree parmvec = make_tree_vec (1);
14116 tree argvec = make_tree_vec (len1);
14117 tree ta = args1;
14119 /* Setup the parameter vector, which contains only ARG1. */
14120 TREE_VEC_ELT (parmvec, 0) = arg2;
14122 /* Setup the argument vector, which contains the remaining
14123 arguments. */
14124 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14125 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14127 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14128 argvec, UNIFY_ALLOW_NONE,
14129 /*call_args_p=*/false,
14130 /*subr=*/0);
14132 /* We cannot deduce in the other direction, because ARG2 is
14133 a pack expansion but ARG1 is not.*/
14134 deduce1 = 0;
14137 else
14139 /* The normal case, where neither argument is a pack
14140 expansion. */
14141 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14142 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14145 if (!deduce1)
14146 better2 = -1;
14147 if (!deduce2)
14148 better1 = -1;
14149 if (better1 < 0 && better2 < 0)
14150 /* We've failed to deduce something in either direction.
14151 These must be unordered. */
14152 break;
14154 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14156 /* Deduces in both directions, see if quals can
14157 disambiguate. Pretend the worse one failed to deduce. */
14158 if ((quals1 & quals2) == quals2)
14159 deduce1 = 0;
14160 if ((quals1 & quals2) == quals1)
14161 deduce2 = 0;
14163 if (deduce1 && !deduce2 && !better2)
14164 better2 = 1;
14165 if (deduce2 && !deduce1 && !better1)
14166 better1 = 1;
14168 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14169 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14170 /* We have already processed all of the arguments in our
14171 handing of the pack expansion type. */
14172 len = 0;
14174 args1 = TREE_CHAIN (args1);
14175 args2 = TREE_CHAIN (args2);
14178 processing_template_decl--;
14180 /* All things being equal, if the next argument is a pack expansion
14181 for one function but not for the other, prefer the
14182 non-variadic function. */
14183 if ((better1 > 0) - (better2 > 0) == 0
14184 && args1 && TREE_VALUE (args1)
14185 && args2 && TREE_VALUE (args2))
14187 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14188 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14189 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14190 return 1;
14193 return (better1 > 0) - (better2 > 0);
14196 /* Determine which of two partial specializations is more specialized.
14198 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14199 to the first partial specialization. The TREE_VALUE is the
14200 innermost set of template parameters for the partial
14201 specialization. PAT2 is similar, but for the second template.
14203 Return 1 if the first partial specialization is more specialized;
14204 -1 if the second is more specialized; 0 if neither is more
14205 specialized.
14207 See [temp.class.order] for information about determining which of
14208 two templates is more specialized. */
14210 static int
14211 more_specialized_class (tree pat1, tree pat2)
14213 tree targs;
14214 tree tmpl1, tmpl2;
14215 int winner = 0;
14216 bool any_deductions = false;
14218 tmpl1 = TREE_TYPE (pat1);
14219 tmpl2 = TREE_TYPE (pat2);
14221 /* Just like what happens for functions, if we are ordering between
14222 different class template specializations, we may encounter dependent
14223 types in the arguments, and we need our dependency check functions
14224 to behave correctly. */
14225 ++processing_template_decl;
14226 targs = get_class_bindings (TREE_VALUE (pat1),
14227 CLASSTYPE_TI_ARGS (tmpl1),
14228 CLASSTYPE_TI_ARGS (tmpl2));
14229 if (targs)
14231 --winner;
14232 any_deductions = true;
14235 targs = get_class_bindings (TREE_VALUE (pat2),
14236 CLASSTYPE_TI_ARGS (tmpl2),
14237 CLASSTYPE_TI_ARGS (tmpl1));
14238 if (targs)
14240 ++winner;
14241 any_deductions = true;
14243 --processing_template_decl;
14245 /* In the case of a tie where at least one of the class templates
14246 has a parameter pack at the end, the template with the most
14247 non-packed parameters wins. */
14248 if (winner == 0
14249 && any_deductions
14250 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14251 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14253 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14254 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14255 int len1 = TREE_VEC_LENGTH (args1);
14256 int len2 = TREE_VEC_LENGTH (args2);
14258 /* We don't count the pack expansion at the end. */
14259 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14260 --len1;
14261 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14262 --len2;
14264 if (len1 > len2)
14265 return 1;
14266 else if (len1 < len2)
14267 return -1;
14270 return winner;
14273 /* Return the template arguments that will produce the function signature
14274 DECL from the function template FN, with the explicit template
14275 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
14276 also match. Return NULL_TREE if no satisfactory arguments could be
14277 found. */
14279 static tree
14280 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
14282 int ntparms = DECL_NTPARMS (fn);
14283 tree targs = make_tree_vec (ntparms);
14284 tree decl_type;
14285 tree decl_arg_types;
14287 /* Substitute the explicit template arguments into the type of DECL.
14288 The call to fn_type_unification will handle substitution into the
14289 FN. */
14290 decl_type = TREE_TYPE (decl);
14291 if (explicit_args && uses_template_parms (decl_type))
14293 tree tmpl;
14294 tree converted_args;
14296 if (DECL_TEMPLATE_INFO (decl))
14297 tmpl = DECL_TI_TEMPLATE (decl);
14298 else
14299 /* We can get here for some invalid specializations. */
14300 return NULL_TREE;
14302 converted_args
14303 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
14304 explicit_args, NULL_TREE,
14305 tf_none,
14306 /*require_all_args=*/false,
14307 /*use_default_args=*/false);
14308 if (converted_args == error_mark_node)
14309 return NULL_TREE;
14311 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
14312 if (decl_type == error_mark_node)
14313 return NULL_TREE;
14316 /* Never do unification on the 'this' parameter. */
14317 decl_arg_types = skip_artificial_parms_for (decl,
14318 TYPE_ARG_TYPES (decl_type));
14320 if (fn_type_unification (fn, explicit_args, targs,
14321 decl_arg_types,
14322 (check_rettype || DECL_CONV_FN_P (fn)
14323 ? TREE_TYPE (decl_type) : NULL_TREE),
14324 DEDUCE_EXACT, LOOKUP_NORMAL))
14325 return NULL_TREE;
14327 return targs;
14330 /* Return the innermost template arguments that, when applied to a
14331 template specialization whose innermost template parameters are
14332 TPARMS, and whose specialization arguments are PARMS, yield the
14333 ARGS.
14335 For example, suppose we have:
14337 template <class T, class U> struct S {};
14338 template <class T> struct S<T*, int> {};
14340 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
14341 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
14342 int}. The resulting vector will be {double}, indicating that `T'
14343 is bound to `double'. */
14345 static tree
14346 get_class_bindings (tree tparms, tree spec_args, tree args)
14348 int i, ntparms = TREE_VEC_LENGTH (tparms);
14349 tree deduced_args;
14350 tree innermost_deduced_args;
14352 innermost_deduced_args = make_tree_vec (ntparms);
14353 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14355 deduced_args = copy_node (args);
14356 SET_TMPL_ARGS_LEVEL (deduced_args,
14357 TMPL_ARGS_DEPTH (deduced_args),
14358 innermost_deduced_args);
14360 else
14361 deduced_args = innermost_deduced_args;
14363 if (unify (tparms, deduced_args,
14364 INNERMOST_TEMPLATE_ARGS (spec_args),
14365 INNERMOST_TEMPLATE_ARGS (args),
14366 UNIFY_ALLOW_NONE))
14367 return NULL_TREE;
14369 for (i = 0; i < ntparms; ++i)
14370 if (! TREE_VEC_ELT (innermost_deduced_args, i))
14371 return NULL_TREE;
14373 /* Verify that nondeduced template arguments agree with the type
14374 obtained from argument deduction.
14376 For example:
14378 struct A { typedef int X; };
14379 template <class T, class U> struct C {};
14380 template <class T> struct C<T, typename T::X> {};
14382 Then with the instantiation `C<A, int>', we can deduce that
14383 `T' is `A' but unify () does not check whether `typename T::X'
14384 is `int'. */
14385 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
14386 if (spec_args == error_mark_node
14387 /* We only need to check the innermost arguments; the other
14388 arguments will always agree. */
14389 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
14390 INNERMOST_TEMPLATE_ARGS (args)))
14391 return NULL_TREE;
14393 /* Now that we have bindings for all of the template arguments,
14394 ensure that the arguments deduced for the template template
14395 parameters have compatible template parameter lists. See the use
14396 of template_template_parm_bindings_ok_p in fn_type_unification
14397 for more information. */
14398 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
14399 return NULL_TREE;
14401 return deduced_args;
14404 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
14405 Return the TREE_LIST node with the most specialized template, if
14406 any. If there is no most specialized template, the error_mark_node
14407 is returned.
14409 Note that this function does not look at, or modify, the
14410 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
14411 returned is one of the elements of INSTANTIATIONS, callers may
14412 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
14413 and retrieve it from the value returned. */
14415 tree
14416 most_specialized_instantiation (tree templates)
14418 tree fn, champ;
14420 ++processing_template_decl;
14422 champ = templates;
14423 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
14425 int fate = 0;
14427 if (get_bindings (TREE_VALUE (champ),
14428 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14429 NULL_TREE, /*check_ret=*/false))
14430 fate--;
14432 if (get_bindings (TREE_VALUE (fn),
14433 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14434 NULL_TREE, /*check_ret=*/false))
14435 fate++;
14437 if (fate == -1)
14438 champ = fn;
14439 else if (!fate)
14441 /* Equally specialized, move to next function. If there
14442 is no next function, nothing's most specialized. */
14443 fn = TREE_CHAIN (fn);
14444 champ = fn;
14445 if (!fn)
14446 break;
14450 if (champ)
14451 /* Now verify that champ is better than everything earlier in the
14452 instantiation list. */
14453 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
14454 if (get_bindings (TREE_VALUE (champ),
14455 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14456 NULL_TREE, /*check_ret=*/false)
14457 || !get_bindings (TREE_VALUE (fn),
14458 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14459 NULL_TREE, /*check_ret=*/false))
14461 champ = NULL_TREE;
14462 break;
14465 processing_template_decl--;
14467 if (!champ)
14468 return error_mark_node;
14470 return champ;
14473 /* If DECL is a specialization of some template, return the most
14474 general such template. Otherwise, returns NULL_TREE.
14476 For example, given:
14478 template <class T> struct S { template <class U> void f(U); };
14480 if TMPL is `template <class U> void S<int>::f(U)' this will return
14481 the full template. This function will not trace past partial
14482 specializations, however. For example, given in addition:
14484 template <class T> struct S<T*> { template <class U> void f(U); };
14486 if TMPL is `template <class U> void S<int*>::f(U)' this will return
14487 `template <class T> template <class U> S<T*>::f(U)'. */
14489 tree
14490 most_general_template (tree decl)
14492 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
14493 an immediate specialization. */
14494 if (TREE_CODE (decl) == FUNCTION_DECL)
14496 if (DECL_TEMPLATE_INFO (decl)) {
14497 decl = DECL_TI_TEMPLATE (decl);
14499 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
14500 template friend. */
14501 if (TREE_CODE (decl) != TEMPLATE_DECL)
14502 return NULL_TREE;
14503 } else
14504 return NULL_TREE;
14507 /* Look for more and more general templates. */
14508 while (DECL_TEMPLATE_INFO (decl))
14510 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
14511 (See cp-tree.h for details.) */
14512 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
14513 break;
14515 if (CLASS_TYPE_P (TREE_TYPE (decl))
14516 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
14517 break;
14519 /* Stop if we run into an explicitly specialized class template. */
14520 if (!DECL_NAMESPACE_SCOPE_P (decl)
14521 && DECL_CONTEXT (decl)
14522 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
14523 break;
14525 decl = DECL_TI_TEMPLATE (decl);
14528 return decl;
14531 /* Return the most specialized of the class template partial
14532 specializations of TMPL which can produce TYPE, a specialization of
14533 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
14534 a _TYPE node corresponding to the partial specialization, while the
14535 TREE_PURPOSE is the set of template arguments that must be
14536 substituted into the TREE_TYPE in order to generate TYPE.
14538 If the choice of partial specialization is ambiguous, a diagnostic
14539 is issued, and the error_mark_node is returned. If there are no
14540 partial specializations of TMPL matching TYPE, then NULL_TREE is
14541 returned. */
14543 static tree
14544 most_specialized_class (tree type, tree tmpl)
14546 tree list = NULL_TREE;
14547 tree t;
14548 tree champ;
14549 int fate;
14550 bool ambiguous_p;
14551 tree args;
14552 tree outer_args = NULL_TREE;
14554 tmpl = most_general_template (tmpl);
14555 args = CLASSTYPE_TI_ARGS (type);
14557 /* For determining which partial specialization to use, only the
14558 innermost args are interesting. */
14559 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14561 outer_args = strip_innermost_template_args (args, 1);
14562 args = INNERMOST_TEMPLATE_ARGS (args);
14565 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
14567 tree partial_spec_args;
14568 tree spec_args;
14569 tree parms = TREE_VALUE (t);
14571 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
14572 if (outer_args)
14574 int i;
14576 ++processing_template_decl;
14578 /* Discard the outer levels of args, and then substitute in the
14579 template args from the enclosing class. */
14580 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
14581 partial_spec_args = tsubst_template_args
14582 (partial_spec_args, outer_args, tf_none, NULL_TREE);
14584 /* PARMS already refers to just the innermost parms, but the
14585 template parms in partial_spec_args had their levels lowered
14586 by tsubst, so we need to do the same for the parm list. We
14587 can't just tsubst the TREE_VEC itself, as tsubst wants to
14588 treat a TREE_VEC as an argument vector. */
14589 parms = copy_node (parms);
14590 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
14591 TREE_VEC_ELT (parms, i) =
14592 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
14594 --processing_template_decl;
14596 spec_args = get_class_bindings (parms,
14597 partial_spec_args,
14598 args);
14599 if (spec_args)
14601 if (outer_args)
14602 spec_args = add_to_template_args (outer_args, spec_args);
14603 list = tree_cons (spec_args, TREE_VALUE (t), list);
14604 TREE_TYPE (list) = TREE_TYPE (t);
14608 if (! list)
14609 return NULL_TREE;
14611 ambiguous_p = false;
14612 t = list;
14613 champ = t;
14614 t = TREE_CHAIN (t);
14615 for (; t; t = TREE_CHAIN (t))
14617 fate = more_specialized_class (champ, t);
14618 if (fate == 1)
14620 else
14622 if (fate == 0)
14624 t = TREE_CHAIN (t);
14625 if (! t)
14627 ambiguous_p = true;
14628 break;
14631 champ = t;
14635 if (!ambiguous_p)
14636 for (t = list; t && t != champ; t = TREE_CHAIN (t))
14638 fate = more_specialized_class (champ, t);
14639 if (fate != 1)
14641 ambiguous_p = true;
14642 break;
14646 if (ambiguous_p)
14648 const char *str = "candidates are:";
14649 error ("ambiguous class template instantiation for %q#T", type);
14650 for (t = list; t; t = TREE_CHAIN (t))
14652 error ("%s %+#T", str, TREE_TYPE (t));
14653 str = " ";
14655 return error_mark_node;
14658 return champ;
14661 /* Explicitly instantiate DECL. */
14663 void
14664 do_decl_instantiation (tree decl, tree storage)
14666 tree result = NULL_TREE;
14667 int extern_p = 0;
14669 if (!decl || decl == error_mark_node)
14670 /* An error occurred, for which grokdeclarator has already issued
14671 an appropriate message. */
14672 return;
14673 else if (! DECL_LANG_SPECIFIC (decl))
14675 error ("explicit instantiation of non-template %q#D", decl);
14676 return;
14678 else if (TREE_CODE (decl) == VAR_DECL)
14680 /* There is an asymmetry here in the way VAR_DECLs and
14681 FUNCTION_DECLs are handled by grokdeclarator. In the case of
14682 the latter, the DECL we get back will be marked as a
14683 template instantiation, and the appropriate
14684 DECL_TEMPLATE_INFO will be set up. This does not happen for
14685 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
14686 should handle VAR_DECLs as it currently handles
14687 FUNCTION_DECLs. */
14688 if (!DECL_CLASS_SCOPE_P (decl))
14690 error ("%qD is not a static data member of a class template", decl);
14691 return;
14693 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
14694 if (!result || TREE_CODE (result) != VAR_DECL)
14696 error ("no matching template for %qD found", decl);
14697 return;
14699 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
14701 error ("type %qT for explicit instantiation %qD does not match "
14702 "declared type %qT", TREE_TYPE (result), decl,
14703 TREE_TYPE (decl));
14704 return;
14707 else if (TREE_CODE (decl) != FUNCTION_DECL)
14709 error ("explicit instantiation of %q#D", decl);
14710 return;
14712 else
14713 result = decl;
14715 /* Check for various error cases. Note that if the explicit
14716 instantiation is valid the RESULT will currently be marked as an
14717 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
14718 until we get here. */
14720 if (DECL_TEMPLATE_SPECIALIZATION (result))
14722 /* DR 259 [temp.spec].
14724 Both an explicit instantiation and a declaration of an explicit
14725 specialization shall not appear in a program unless the explicit
14726 instantiation follows a declaration of the explicit specialization.
14728 For a given set of template parameters, if an explicit
14729 instantiation of a template appears after a declaration of an
14730 explicit specialization for that template, the explicit
14731 instantiation has no effect. */
14732 return;
14734 else if (DECL_EXPLICIT_INSTANTIATION (result))
14736 /* [temp.spec]
14738 No program shall explicitly instantiate any template more
14739 than once.
14741 We check DECL_NOT_REALLY_EXTERN so as not to complain when
14742 the first instantiation was `extern' and the second is not,
14743 and EXTERN_P for the opposite case. */
14744 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
14745 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
14746 /* If an "extern" explicit instantiation follows an ordinary
14747 explicit instantiation, the template is instantiated. */
14748 if (extern_p)
14749 return;
14751 else if (!DECL_IMPLICIT_INSTANTIATION (result))
14753 error ("no matching template for %qD found", result);
14754 return;
14756 else if (!DECL_TEMPLATE_INFO (result))
14758 permerror (input_location, "explicit instantiation of non-template %q#D", result);
14759 return;
14762 if (storage == NULL_TREE)
14764 else if (storage == ridpointers[(int) RID_EXTERN])
14766 if (!in_system_header && (cxx_dialect == cxx98))
14767 pedwarn (input_location, OPT_pedantic,
14768 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
14769 "instantiations");
14770 extern_p = 1;
14772 else
14773 error ("storage class %qD applied to template instantiation", storage);
14775 check_explicit_instantiation_namespace (result);
14776 mark_decl_instantiated (result, extern_p);
14777 if (! extern_p)
14778 instantiate_decl (result, /*defer_ok=*/1,
14779 /*expl_inst_class_mem_p=*/false);
14782 static void
14783 mark_class_instantiated (tree t, int extern_p)
14785 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
14786 SET_CLASSTYPE_INTERFACE_KNOWN (t);
14787 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
14788 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
14789 if (! extern_p)
14791 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
14792 rest_of_type_compilation (t, 1);
14796 /* Called from do_type_instantiation through binding_table_foreach to
14797 do recursive instantiation for the type bound in ENTRY. */
14798 static void
14799 bt_instantiate_type_proc (binding_entry entry, void *data)
14801 tree storage = *(tree *) data;
14803 if (MAYBE_CLASS_TYPE_P (entry->type)
14804 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
14805 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
14808 /* Called from do_type_instantiation to instantiate a member
14809 (a member function or a static member variable) of an
14810 explicitly instantiated class template. */
14811 static void
14812 instantiate_class_member (tree decl, int extern_p)
14814 mark_decl_instantiated (decl, extern_p);
14815 if (! extern_p)
14816 instantiate_decl (decl, /*defer_ok=*/1,
14817 /*expl_inst_class_mem_p=*/true);
14820 /* Perform an explicit instantiation of template class T. STORAGE, if
14821 non-null, is the RID for extern, inline or static. COMPLAIN is
14822 nonzero if this is called from the parser, zero if called recursively,
14823 since the standard is unclear (as detailed below). */
14825 void
14826 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
14828 int extern_p = 0;
14829 int nomem_p = 0;
14830 int static_p = 0;
14831 int previous_instantiation_extern_p = 0;
14833 if (TREE_CODE (t) == TYPE_DECL)
14834 t = TREE_TYPE (t);
14836 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
14838 error ("explicit instantiation of non-template type %qT", t);
14839 return;
14842 complete_type (t);
14844 if (!COMPLETE_TYPE_P (t))
14846 if (complain & tf_error)
14847 error ("explicit instantiation of %q#T before definition of template",
14849 return;
14852 if (storage != NULL_TREE)
14854 if (!in_system_header)
14856 if (storage == ridpointers[(int) RID_EXTERN])
14858 if (cxx_dialect == cxx98)
14859 pedwarn (input_location, OPT_pedantic,
14860 "ISO C++ 1998 forbids the use of %<extern%> on "
14861 "explicit instantiations");
14863 else
14864 pedwarn (input_location, OPT_pedantic,
14865 "ISO C++ forbids the use of %qE"
14866 " on explicit instantiations", storage);
14869 if (storage == ridpointers[(int) RID_INLINE])
14870 nomem_p = 1;
14871 else if (storage == ridpointers[(int) RID_EXTERN])
14872 extern_p = 1;
14873 else if (storage == ridpointers[(int) RID_STATIC])
14874 static_p = 1;
14875 else
14877 error ("storage class %qD applied to template instantiation",
14878 storage);
14879 extern_p = 0;
14883 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
14885 /* DR 259 [temp.spec].
14887 Both an explicit instantiation and a declaration of an explicit
14888 specialization shall not appear in a program unless the explicit
14889 instantiation follows a declaration of the explicit specialization.
14891 For a given set of template parameters, if an explicit
14892 instantiation of a template appears after a declaration of an
14893 explicit specialization for that template, the explicit
14894 instantiation has no effect. */
14895 return;
14897 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
14899 /* [temp.spec]
14901 No program shall explicitly instantiate any template more
14902 than once.
14904 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
14905 instantiation was `extern'. If EXTERN_P then the second is.
14906 These cases are OK. */
14907 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
14909 if (!previous_instantiation_extern_p && !extern_p
14910 && (complain & tf_error))
14911 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
14913 /* If we've already instantiated the template, just return now. */
14914 if (!CLASSTYPE_INTERFACE_ONLY (t))
14915 return;
14918 check_explicit_instantiation_namespace (TYPE_NAME (t));
14919 mark_class_instantiated (t, extern_p);
14921 if (nomem_p)
14922 return;
14925 tree tmp;
14927 /* In contrast to implicit instantiation, where only the
14928 declarations, and not the definitions, of members are
14929 instantiated, we have here:
14931 [temp.explicit]
14933 The explicit instantiation of a class template specialization
14934 implies the instantiation of all of its members not
14935 previously explicitly specialized in the translation unit
14936 containing the explicit instantiation.
14938 Of course, we can't instantiate member template classes, since
14939 we don't have any arguments for them. Note that the standard
14940 is unclear on whether the instantiation of the members are
14941 *explicit* instantiations or not. However, the most natural
14942 interpretation is that it should be an explicit instantiation. */
14944 if (! static_p)
14945 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
14946 if (TREE_CODE (tmp) == FUNCTION_DECL
14947 && DECL_TEMPLATE_INSTANTIATION (tmp))
14948 instantiate_class_member (tmp, extern_p);
14950 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
14951 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
14952 instantiate_class_member (tmp, extern_p);
14954 if (CLASSTYPE_NESTED_UTDS (t))
14955 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
14956 bt_instantiate_type_proc, &storage);
14960 /* Given a function DECL, which is a specialization of TMPL, modify
14961 DECL to be a re-instantiation of TMPL with the same template
14962 arguments. TMPL should be the template into which tsubst'ing
14963 should occur for DECL, not the most general template.
14965 One reason for doing this is a scenario like this:
14967 template <class T>
14968 void f(const T&, int i);
14970 void g() { f(3, 7); }
14972 template <class T>
14973 void f(const T& t, const int i) { }
14975 Note that when the template is first instantiated, with
14976 instantiate_template, the resulting DECL will have no name for the
14977 first parameter, and the wrong type for the second. So, when we go
14978 to instantiate the DECL, we regenerate it. */
14980 static void
14981 regenerate_decl_from_template (tree decl, tree tmpl)
14983 /* The arguments used to instantiate DECL, from the most general
14984 template. */
14985 tree args;
14986 tree code_pattern;
14988 args = DECL_TI_ARGS (decl);
14989 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
14991 /* Make sure that we can see identifiers, and compute access
14992 correctly. */
14993 push_access_scope (decl);
14995 if (TREE_CODE (decl) == FUNCTION_DECL)
14997 tree decl_parm;
14998 tree pattern_parm;
14999 tree specs;
15000 int args_depth;
15001 int parms_depth;
15003 args_depth = TMPL_ARGS_DEPTH (args);
15004 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
15005 if (args_depth > parms_depth)
15006 args = get_innermost_template_args (args, parms_depth);
15008 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15009 args, tf_error, NULL_TREE);
15010 if (specs)
15011 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15012 specs);
15014 /* Merge parameter declarations. */
15015 decl_parm = skip_artificial_parms_for (decl,
15016 DECL_ARGUMENTS (decl));
15017 pattern_parm
15018 = skip_artificial_parms_for (code_pattern,
15019 DECL_ARGUMENTS (code_pattern));
15020 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
15022 tree parm_type;
15023 tree attributes;
15025 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15026 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15027 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15028 NULL_TREE);
15029 parm_type = type_decays_to (parm_type);
15030 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15031 TREE_TYPE (decl_parm) = parm_type;
15032 attributes = DECL_ATTRIBUTES (pattern_parm);
15033 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15035 DECL_ATTRIBUTES (decl_parm) = attributes;
15036 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15038 decl_parm = TREE_CHAIN (decl_parm);
15039 pattern_parm = TREE_CHAIN (pattern_parm);
15041 /* Merge any parameters that match with the function parameter
15042 pack. */
15043 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15045 int i, len;
15046 tree expanded_types;
15047 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15048 the parameters in this function parameter pack. */
15049 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15050 args, tf_error, NULL_TREE);
15051 len = TREE_VEC_LENGTH (expanded_types);
15052 for (i = 0; i < len; i++)
15054 tree parm_type;
15055 tree attributes;
15057 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15058 /* Rename the parameter to include the index. */
15059 DECL_NAME (decl_parm) =
15060 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15061 parm_type = TREE_VEC_ELT (expanded_types, i);
15062 parm_type = type_decays_to (parm_type);
15063 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15064 TREE_TYPE (decl_parm) = parm_type;
15065 attributes = DECL_ATTRIBUTES (pattern_parm);
15066 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15068 DECL_ATTRIBUTES (decl_parm) = attributes;
15069 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15071 decl_parm = TREE_CHAIN (decl_parm);
15074 /* Merge additional specifiers from the CODE_PATTERN. */
15075 if (DECL_DECLARED_INLINE_P (code_pattern)
15076 && !DECL_DECLARED_INLINE_P (decl))
15077 DECL_DECLARED_INLINE_P (decl) = 1;
15079 else if (TREE_CODE (decl) == VAR_DECL)
15080 DECL_INITIAL (decl) =
15081 tsubst_expr (DECL_INITIAL (code_pattern), args,
15082 tf_error, DECL_TI_TEMPLATE (decl),
15083 /*integral_constant_expression_p=*/false);
15084 else
15085 gcc_unreachable ();
15087 pop_access_scope (decl);
15090 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15091 substituted to get DECL. */
15093 tree
15094 template_for_substitution (tree decl)
15096 tree tmpl = DECL_TI_TEMPLATE (decl);
15098 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15099 for the instantiation. This is not always the most general
15100 template. Consider, for example:
15102 template <class T>
15103 struct S { template <class U> void f();
15104 template <> void f<int>(); };
15106 and an instantiation of S<double>::f<int>. We want TD to be the
15107 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15108 while (/* An instantiation cannot have a definition, so we need a
15109 more general template. */
15110 DECL_TEMPLATE_INSTANTIATION (tmpl)
15111 /* We must also deal with friend templates. Given:
15113 template <class T> struct S {
15114 template <class U> friend void f() {};
15117 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15118 so far as the language is concerned, but that's still
15119 where we get the pattern for the instantiation from. On
15120 other hand, if the definition comes outside the class, say:
15122 template <class T> struct S {
15123 template <class U> friend void f();
15125 template <class U> friend void f() {}
15127 we don't need to look any further. That's what the check for
15128 DECL_INITIAL is for. */
15129 || (TREE_CODE (decl) == FUNCTION_DECL
15130 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15131 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15133 /* The present template, TD, should not be a definition. If it
15134 were a definition, we should be using it! Note that we
15135 cannot restructure the loop to just keep going until we find
15136 a template with a definition, since that might go too far if
15137 a specialization was declared, but not defined. */
15138 gcc_assert (TREE_CODE (decl) != VAR_DECL
15139 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
15141 /* Fetch the more general template. */
15142 tmpl = DECL_TI_TEMPLATE (tmpl);
15145 return tmpl;
15148 /* Produce the definition of D, a _DECL generated from a template. If
15149 DEFER_OK is nonzero, then we don't have to actually do the
15150 instantiation now; we just have to do it sometime. Normally it is
15151 an error if this is an explicit instantiation but D is undefined.
15152 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15153 explicitly instantiated class template. */
15155 tree
15156 instantiate_decl (tree d, int defer_ok,
15157 bool expl_inst_class_mem_p)
15159 tree tmpl = DECL_TI_TEMPLATE (d);
15160 tree gen_args;
15161 tree args;
15162 tree td;
15163 tree code_pattern;
15164 tree spec;
15165 tree gen_tmpl;
15166 bool pattern_defined;
15167 int need_push;
15168 location_t saved_loc = input_location;
15169 bool external_p;
15171 /* This function should only be used to instantiate templates for
15172 functions and static member variables. */
15173 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15174 || TREE_CODE (d) == VAR_DECL);
15176 /* Variables are never deferred; if instantiation is required, they
15177 are instantiated right away. That allows for better code in the
15178 case that an expression refers to the value of the variable --
15179 if the variable has a constant value the referring expression can
15180 take advantage of that fact. */
15181 if (TREE_CODE (d) == VAR_DECL)
15182 defer_ok = 0;
15184 /* Don't instantiate cloned functions. Instead, instantiate the
15185 functions they cloned. */
15186 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15187 d = DECL_CLONED_FUNCTION (d);
15189 if (DECL_TEMPLATE_INSTANTIATED (d))
15190 /* D has already been instantiated. It might seem reasonable to
15191 check whether or not D is an explicit instantiation, and, if so,
15192 stop here. But when an explicit instantiation is deferred
15193 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
15194 is set, even though we still need to do the instantiation. */
15195 return d;
15197 /* If we already have a specialization of this declaration, then
15198 there's no reason to instantiate it. Note that
15199 retrieve_specialization gives us both instantiations and
15200 specializations, so we must explicitly check
15201 DECL_TEMPLATE_SPECIALIZATION. */
15202 gen_tmpl = most_general_template (tmpl);
15203 gen_args = DECL_TI_ARGS (d);
15204 spec = retrieve_specialization (gen_tmpl, gen_args,
15205 /*class_specializations_p=*/false);
15206 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
15207 return spec;
15209 /* This needs to happen before any tsubsting. */
15210 if (! push_tinst_level (d))
15211 return d;
15213 timevar_push (TV_PARSE);
15215 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15216 for the instantiation. */
15217 td = template_for_substitution (d);
15218 code_pattern = DECL_TEMPLATE_RESULT (td);
15220 /* We should never be trying to instantiate a member of a class
15221 template or partial specialization. */
15222 gcc_assert (d != code_pattern);
15224 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15225 || DECL_TEMPLATE_SPECIALIZATION (td))
15226 /* In the case of a friend template whose definition is provided
15227 outside the class, we may have too many arguments. Drop the
15228 ones we don't need. The same is true for specializations. */
15229 args = get_innermost_template_args
15230 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
15231 else
15232 args = gen_args;
15234 if (TREE_CODE (d) == FUNCTION_DECL)
15235 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
15236 else
15237 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
15239 /* We may be in the middle of deferred access check. Disable it now. */
15240 push_deferring_access_checks (dk_no_deferred);
15242 /* Unless an explicit instantiation directive has already determined
15243 the linkage of D, remember that a definition is available for
15244 this entity. */
15245 if (pattern_defined
15246 && !DECL_INTERFACE_KNOWN (d)
15247 && !DECL_NOT_REALLY_EXTERN (d))
15248 mark_definable (d);
15250 input_location = DECL_SOURCE_LOCATION (d);
15252 /* If D is a member of an explicitly instantiated class template,
15253 and no definition is available, treat it like an implicit
15254 instantiation. */
15255 if (!pattern_defined && expl_inst_class_mem_p
15256 && DECL_EXPLICIT_INSTANTIATION (d))
15258 DECL_NOT_REALLY_EXTERN (d) = 0;
15259 DECL_INTERFACE_KNOWN (d) = 0;
15260 SET_DECL_IMPLICIT_INSTANTIATION (d);
15263 if (!defer_ok)
15265 /* Recheck the substitutions to obtain any warning messages
15266 about ignoring cv qualifiers. */
15267 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
15268 tree type = TREE_TYPE (gen);
15270 /* Make sure that we can see identifiers, and compute access
15271 correctly. D is already the target FUNCTION_DECL with the
15272 right context. */
15273 push_access_scope (d);
15275 if (TREE_CODE (gen) == FUNCTION_DECL)
15277 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
15278 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
15280 /* Don't simply tsubst the function type, as that will give
15281 duplicate warnings about poor parameter qualifications.
15282 The function arguments are the same as the decl_arguments
15283 without the top level cv qualifiers. */
15284 type = TREE_TYPE (type);
15286 tsubst (type, gen_args, tf_warning_or_error, d);
15288 pop_access_scope (d);
15291 /* Check to see whether we know that this template will be
15292 instantiated in some other file, as with "extern template"
15293 extension. */
15294 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15295 /* In general, we do not instantiate such templates... */
15296 if (external_p
15297 /* ... but we instantiate inline functions so that we can inline
15298 them. An explicit instantiation declaration prohibits implicit
15299 instantiation of non-inline functions. With high levels of
15300 optimization, we would normally inline non-inline functions
15301 -- but we're not allowed to do that for "extern template" functions.
15302 Therefore, we check DECL_DECLARED_INLINE_P, rather than
15303 possibly_inlined_p. And ... */
15304 && ! (TREE_CODE (d) == FUNCTION_DECL
15305 && DECL_DECLARED_INLINE_P (d))
15306 /* ... we instantiate static data members whose values are
15307 needed in integral constant expressions. */
15308 && ! (TREE_CODE (d) == VAR_DECL
15309 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
15310 goto out;
15311 /* Defer all other templates, unless we have been explicitly
15312 forbidden from doing so. */
15313 if (/* If there is no definition, we cannot instantiate the
15314 template. */
15315 ! pattern_defined
15316 /* If it's OK to postpone instantiation, do so. */
15317 || defer_ok
15318 /* If this is a static data member that will be defined
15319 elsewhere, we don't want to instantiate the entire data
15320 member, but we do want to instantiate the initializer so that
15321 we can substitute that elsewhere. */
15322 || (external_p && TREE_CODE (d) == VAR_DECL))
15324 /* The definition of the static data member is now required so
15325 we must substitute the initializer. */
15326 if (TREE_CODE (d) == VAR_DECL
15327 && !DECL_INITIAL (d)
15328 && DECL_INITIAL (code_pattern))
15330 tree ns;
15331 tree init;
15333 ns = decl_namespace_context (d);
15334 push_nested_namespace (ns);
15335 push_nested_class (DECL_CONTEXT (d));
15336 init = tsubst_expr (DECL_INITIAL (code_pattern),
15337 args,
15338 tf_warning_or_error, NULL_TREE,
15339 /*integral_constant_expression_p=*/false);
15340 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
15341 /*asmspec_tree=*/NULL_TREE,
15342 LOOKUP_ONLYCONVERTING);
15343 pop_nested_class ();
15344 pop_nested_namespace (ns);
15347 /* We restore the source position here because it's used by
15348 add_pending_template. */
15349 input_location = saved_loc;
15351 if (at_eof && !pattern_defined
15352 && DECL_EXPLICIT_INSTANTIATION (d)
15353 && DECL_NOT_REALLY_EXTERN (d))
15354 /* [temp.explicit]
15356 The definition of a non-exported function template, a
15357 non-exported member function template, or a non-exported
15358 member function or static data member of a class template
15359 shall be present in every translation unit in which it is
15360 explicitly instantiated. */
15361 permerror (input_location, "explicit instantiation of %qD "
15362 "but no definition available", d);
15364 /* ??? Historically, we have instantiated inline functions, even
15365 when marked as "extern template". */
15366 if (!(external_p && TREE_CODE (d) == VAR_DECL))
15367 add_pending_template (d);
15368 goto out;
15370 /* Tell the repository that D is available in this translation unit
15371 -- and see if it is supposed to be instantiated here. */
15372 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
15374 /* In a PCH file, despite the fact that the repository hasn't
15375 requested instantiation in the PCH it is still possible that
15376 an instantiation will be required in a file that includes the
15377 PCH. */
15378 if (pch_file)
15379 add_pending_template (d);
15380 /* Instantiate inline functions so that the inliner can do its
15381 job, even though we'll not be emitting a copy of this
15382 function. */
15383 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
15384 goto out;
15387 need_push = !cfun || !global_bindings_p ();
15388 if (need_push)
15389 push_to_top_level ();
15391 /* Mark D as instantiated so that recursive calls to
15392 instantiate_decl do not try to instantiate it again. */
15393 DECL_TEMPLATE_INSTANTIATED (d) = 1;
15395 /* Regenerate the declaration in case the template has been modified
15396 by a subsequent redeclaration. */
15397 regenerate_decl_from_template (d, td);
15399 /* We already set the file and line above. Reset them now in case
15400 they changed as a result of calling regenerate_decl_from_template. */
15401 input_location = DECL_SOURCE_LOCATION (d);
15403 if (TREE_CODE (d) == VAR_DECL)
15405 tree init;
15407 /* Clear out DECL_RTL; whatever was there before may not be right
15408 since we've reset the type of the declaration. */
15409 SET_DECL_RTL (d, NULL_RTX);
15410 DECL_IN_AGGR_P (d) = 0;
15412 /* The initializer is placed in DECL_INITIAL by
15413 regenerate_decl_from_template. Pull it out so that
15414 finish_decl can process it. */
15415 init = DECL_INITIAL (d);
15416 DECL_INITIAL (d) = NULL_TREE;
15417 DECL_INITIALIZED_P (d) = 0;
15419 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
15420 initializer. That function will defer actual emission until
15421 we have a chance to determine linkage. */
15422 DECL_EXTERNAL (d) = 0;
15424 /* Enter the scope of D so that access-checking works correctly. */
15425 push_nested_class (DECL_CONTEXT (d));
15426 finish_decl (d, init, NULL_TREE);
15427 pop_nested_class ();
15429 else if (TREE_CODE (d) == FUNCTION_DECL)
15431 htab_t saved_local_specializations;
15432 tree subst_decl;
15433 tree tmpl_parm;
15434 tree spec_parm;
15436 /* Save away the current list, in case we are instantiating one
15437 template from within the body of another. */
15438 saved_local_specializations = local_specializations;
15440 /* Set up the list of local specializations. */
15441 local_specializations = htab_create (37,
15442 hash_local_specialization,
15443 eq_local_specializations,
15444 NULL);
15446 /* Set up context. */
15447 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
15449 /* Create substitution entries for the parameters. */
15450 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
15451 tmpl_parm = DECL_ARGUMENTS (subst_decl);
15452 spec_parm = DECL_ARGUMENTS (d);
15453 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
15455 register_local_specialization (spec_parm, tmpl_parm);
15456 spec_parm = skip_artificial_parms_for (d, spec_parm);
15457 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
15459 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15461 register_local_specialization (spec_parm, tmpl_parm);
15462 tmpl_parm = TREE_CHAIN (tmpl_parm);
15463 spec_parm = TREE_CHAIN (spec_parm);
15465 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15467 /* Collect all of the extra "packed" parameters into an
15468 argument pack. */
15469 tree parmvec;
15470 tree parmtypevec;
15471 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
15472 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
15473 int i, len = 0;
15474 tree t;
15476 /* Count how many parameters remain. */
15477 for (t = spec_parm; t; t = TREE_CHAIN (t))
15478 len++;
15480 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
15481 parmvec = make_tree_vec (len);
15482 parmtypevec = make_tree_vec (len);
15483 for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
15485 TREE_VEC_ELT (parmvec, i) = spec_parm;
15486 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
15489 /* Build the argument packs. */
15490 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
15491 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
15492 TREE_TYPE (argpack) = argtypepack;
15494 /* Register the (value) argument pack as a specialization of
15495 TMPL_PARM, then move on. */
15496 register_local_specialization (argpack, tmpl_parm);
15497 tmpl_parm = TREE_CHAIN (tmpl_parm);
15499 gcc_assert (!spec_parm);
15501 /* Substitute into the body of the function. */
15502 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
15503 tf_warning_or_error, tmpl,
15504 /*integral_constant_expression_p=*/false);
15506 /* Set the current input_location to the end of the function
15507 so that finish_function knows where we are. */
15508 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
15510 /* We don't need the local specializations any more. */
15511 htab_delete (local_specializations);
15512 local_specializations = saved_local_specializations;
15514 /* Finish the function. */
15515 d = finish_function (0);
15516 expand_or_defer_fn (d);
15519 /* We're not deferring instantiation any more. */
15520 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
15522 if (need_push)
15523 pop_from_top_level ();
15525 out:
15526 input_location = saved_loc;
15527 pop_deferring_access_checks ();
15528 pop_tinst_level ();
15530 timevar_pop (TV_PARSE);
15532 return d;
15535 /* Run through the list of templates that we wish we could
15536 instantiate, and instantiate any we can. RETRIES is the
15537 number of times we retry pending template instantiation. */
15539 void
15540 instantiate_pending_templates (int retries)
15542 int reconsider;
15543 location_t saved_loc = input_location;
15545 /* Instantiating templates may trigger vtable generation. This in turn
15546 may require further template instantiations. We place a limit here
15547 to avoid infinite loop. */
15548 if (pending_templates && retries >= max_tinst_depth)
15550 tree decl = pending_templates->tinst->decl;
15552 error ("template instantiation depth exceeds maximum of %d"
15553 " instantiating %q+D, possibly from virtual table generation"
15554 " (use -ftemplate-depth-NN to increase the maximum)",
15555 max_tinst_depth, decl);
15556 if (TREE_CODE (decl) == FUNCTION_DECL)
15557 /* Pretend that we defined it. */
15558 DECL_INITIAL (decl) = error_mark_node;
15559 return;
15564 struct pending_template **t = &pending_templates;
15565 struct pending_template *last = NULL;
15566 reconsider = 0;
15567 while (*t)
15569 tree instantiation = reopen_tinst_level ((*t)->tinst);
15570 bool complete = false;
15572 if (TYPE_P (instantiation))
15574 tree fn;
15576 if (!COMPLETE_TYPE_P (instantiation))
15578 instantiate_class_template (instantiation);
15579 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
15580 for (fn = TYPE_METHODS (instantiation);
15582 fn = TREE_CHAIN (fn))
15583 if (! DECL_ARTIFICIAL (fn))
15584 instantiate_decl (fn,
15585 /*defer_ok=*/0,
15586 /*expl_inst_class_mem_p=*/false);
15587 if (COMPLETE_TYPE_P (instantiation))
15588 reconsider = 1;
15591 complete = COMPLETE_TYPE_P (instantiation);
15593 else
15595 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
15596 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
15598 instantiation
15599 = instantiate_decl (instantiation,
15600 /*defer_ok=*/0,
15601 /*expl_inst_class_mem_p=*/false);
15602 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
15603 reconsider = 1;
15606 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
15607 || DECL_TEMPLATE_INSTANTIATED (instantiation));
15610 if (complete)
15611 /* If INSTANTIATION has been instantiated, then we don't
15612 need to consider it again in the future. */
15613 *t = (*t)->next;
15614 else
15616 last = *t;
15617 t = &(*t)->next;
15619 tinst_depth = 0;
15620 current_tinst_level = NULL;
15622 last_pending_template = last;
15624 while (reconsider);
15626 input_location = saved_loc;
15629 /* Substitute ARGVEC into T, which is a list of initializers for
15630 either base class or a non-static data member. The TREE_PURPOSEs
15631 are DECLs, and the TREE_VALUEs are the initializer values. Used by
15632 instantiate_decl. */
15634 static tree
15635 tsubst_initializer_list (tree t, tree argvec)
15637 tree inits = NULL_TREE;
15639 for (; t; t = TREE_CHAIN (t))
15641 tree decl;
15642 tree init;
15643 tree expanded_bases = NULL_TREE;
15644 tree expanded_arguments = NULL_TREE;
15645 int i, len = 1;
15647 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
15649 tree expr;
15650 tree arg;
15652 /* Expand the base class expansion type into separate base
15653 classes. */
15654 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
15655 tf_warning_or_error,
15656 NULL_TREE);
15657 if (expanded_bases == error_mark_node)
15658 continue;
15660 /* We'll be building separate TREE_LISTs of arguments for
15661 each base. */
15662 len = TREE_VEC_LENGTH (expanded_bases);
15663 expanded_arguments = make_tree_vec (len);
15664 for (i = 0; i < len; i++)
15665 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
15667 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
15668 expand each argument in the TREE_VALUE of t. */
15669 expr = make_node (EXPR_PACK_EXPANSION);
15670 PACK_EXPANSION_PARAMETER_PACKS (expr) =
15671 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
15673 if (TREE_VALUE (t) == void_type_node)
15674 /* VOID_TYPE_NODE is used to indicate
15675 value-initialization. */
15677 for (i = 0; i < len; i++)
15678 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
15680 else
15682 /* Substitute parameter packs into each argument in the
15683 TREE_LIST. */
15684 in_base_initializer = 1;
15685 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
15687 tree expanded_exprs;
15689 /* Expand the argument. */
15690 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
15691 expanded_exprs
15692 = tsubst_pack_expansion (expr, argvec,
15693 tf_warning_or_error,
15694 NULL_TREE);
15695 if (expanded_exprs == error_mark_node)
15696 continue;
15698 /* Prepend each of the expanded expressions to the
15699 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
15700 for (i = 0; i < len; i++)
15702 TREE_VEC_ELT (expanded_arguments, i) =
15703 tree_cons (NULL_TREE,
15704 TREE_VEC_ELT (expanded_exprs, i),
15705 TREE_VEC_ELT (expanded_arguments, i));
15708 in_base_initializer = 0;
15710 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
15711 since we built them backwards. */
15712 for (i = 0; i < len; i++)
15714 TREE_VEC_ELT (expanded_arguments, i) =
15715 nreverse (TREE_VEC_ELT (expanded_arguments, i));
15720 for (i = 0; i < len; ++i)
15722 if (expanded_bases)
15724 decl = TREE_VEC_ELT (expanded_bases, i);
15725 decl = expand_member_init (decl);
15726 init = TREE_VEC_ELT (expanded_arguments, i);
15728 else
15730 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
15731 tf_warning_or_error, NULL_TREE);
15733 decl = expand_member_init (decl);
15734 if (decl && !DECL_P (decl))
15735 in_base_initializer = 1;
15737 init = tsubst_expr (TREE_VALUE (t), argvec,
15738 tf_warning_or_error, NULL_TREE,
15739 /*integral_constant_expression_p=*/false);
15740 in_base_initializer = 0;
15743 if (decl)
15745 init = build_tree_list (decl, init);
15746 TREE_CHAIN (init) = inits;
15747 inits = init;
15751 return inits;
15754 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
15756 static void
15757 set_current_access_from_decl (tree decl)
15759 if (TREE_PRIVATE (decl))
15760 current_access_specifier = access_private_node;
15761 else if (TREE_PROTECTED (decl))
15762 current_access_specifier = access_protected_node;
15763 else
15764 current_access_specifier = access_public_node;
15767 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
15768 is the instantiation (which should have been created with
15769 start_enum) and ARGS are the template arguments to use. */
15771 static void
15772 tsubst_enum (tree tag, tree newtag, tree args)
15774 tree e;
15776 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
15778 tree value;
15779 tree decl;
15781 decl = TREE_VALUE (e);
15782 /* Note that in a template enum, the TREE_VALUE is the
15783 CONST_DECL, not the corresponding INTEGER_CST. */
15784 value = tsubst_expr (DECL_INITIAL (decl),
15785 args, tf_warning_or_error, NULL_TREE,
15786 /*integral_constant_expression_p=*/true);
15788 /* Give this enumeration constant the correct access. */
15789 set_current_access_from_decl (decl);
15791 /* Actually build the enumerator itself. */
15792 build_enumerator (DECL_NAME (decl), value, newtag);
15795 finish_enum (newtag);
15796 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
15797 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
15800 /* DECL is a FUNCTION_DECL that is a template specialization. Return
15801 its type -- but without substituting the innermost set of template
15802 arguments. So, innermost set of template parameters will appear in
15803 the type. */
15805 tree
15806 get_mostly_instantiated_function_type (tree decl)
15808 tree fn_type;
15809 tree tmpl;
15810 tree targs;
15811 tree tparms;
15812 int parm_depth;
15814 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15815 targs = DECL_TI_ARGS (decl);
15816 tparms = DECL_TEMPLATE_PARMS (tmpl);
15817 parm_depth = TMPL_PARMS_DEPTH (tparms);
15819 /* There should be as many levels of arguments as there are levels
15820 of parameters. */
15821 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
15823 fn_type = TREE_TYPE (tmpl);
15825 if (parm_depth == 1)
15826 /* No substitution is necessary. */
15828 else
15830 int i, save_access_control;
15831 tree partial_args;
15833 /* Replace the innermost level of the TARGS with NULL_TREEs to
15834 let tsubst know not to substitute for those parameters. */
15835 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
15836 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
15837 SET_TMPL_ARGS_LEVEL (partial_args, i,
15838 TMPL_ARGS_LEVEL (targs, i));
15839 SET_TMPL_ARGS_LEVEL (partial_args,
15840 TMPL_ARGS_DEPTH (targs),
15841 make_tree_vec (DECL_NTPARMS (tmpl)));
15843 /* Disable access control as this function is used only during
15844 name-mangling. */
15845 save_access_control = flag_access_control;
15846 flag_access_control = 0;
15848 ++processing_template_decl;
15849 /* Now, do the (partial) substitution to figure out the
15850 appropriate function type. */
15851 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
15852 --processing_template_decl;
15854 /* Substitute into the template parameters to obtain the real
15855 innermost set of parameters. This step is important if the
15856 innermost set of template parameters contains value
15857 parameters whose types depend on outer template parameters. */
15858 TREE_VEC_LENGTH (partial_args)--;
15859 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
15861 flag_access_control = save_access_control;
15864 return fn_type;
15867 /* Return truthvalue if we're processing a template different from
15868 the last one involved in diagnostics. */
15870 problematic_instantiation_changed (void)
15872 return last_template_error_tick != tinst_level_tick;
15875 /* Remember current template involved in diagnostics. */
15876 void
15877 record_last_problematic_instantiation (void)
15879 last_template_error_tick = tinst_level_tick;
15882 struct tinst_level *
15883 current_instantiation (void)
15885 return current_tinst_level;
15888 /* [temp.param] Check that template non-type parm TYPE is of an allowable
15889 type. Return zero for ok, nonzero for disallowed. Issue error and
15890 warning messages under control of COMPLAIN. */
15892 static int
15893 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
15895 if (INTEGRAL_TYPE_P (type))
15896 return 0;
15897 else if (POINTER_TYPE_P (type))
15898 return 0;
15899 else if (TYPE_PTR_TO_MEMBER_P (type))
15900 return 0;
15901 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15902 return 0;
15903 else if (TREE_CODE (type) == TYPENAME_TYPE)
15904 return 0;
15906 if (complain & tf_error)
15907 error ("%q#T is not a valid type for a template constant parameter", type);
15908 return 1;
15911 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
15912 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
15914 static bool
15915 dependent_type_p_r (tree type)
15917 tree scope;
15919 /* [temp.dep.type]
15921 A type is dependent if it is:
15923 -- a template parameter. Template template parameters are types
15924 for us (since TYPE_P holds true for them) so we handle
15925 them here. */
15926 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15927 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
15928 return true;
15929 /* -- a qualified-id with a nested-name-specifier which contains a
15930 class-name that names a dependent type or whose unqualified-id
15931 names a dependent type. */
15932 if (TREE_CODE (type) == TYPENAME_TYPE)
15933 return true;
15934 /* -- a cv-qualified type where the cv-unqualified type is
15935 dependent. */
15936 type = TYPE_MAIN_VARIANT (type);
15937 /* -- a compound type constructed from any dependent type. */
15938 if (TYPE_PTR_TO_MEMBER_P (type))
15939 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
15940 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
15941 (type)));
15942 else if (TREE_CODE (type) == POINTER_TYPE
15943 || TREE_CODE (type) == REFERENCE_TYPE)
15944 return dependent_type_p (TREE_TYPE (type));
15945 else if (TREE_CODE (type) == FUNCTION_TYPE
15946 || TREE_CODE (type) == METHOD_TYPE)
15948 tree arg_type;
15950 if (dependent_type_p (TREE_TYPE (type)))
15951 return true;
15952 for (arg_type = TYPE_ARG_TYPES (type);
15953 arg_type;
15954 arg_type = TREE_CHAIN (arg_type))
15955 if (dependent_type_p (TREE_VALUE (arg_type)))
15956 return true;
15957 return false;
15959 /* -- an array type constructed from any dependent type or whose
15960 size is specified by a constant expression that is
15961 value-dependent. */
15962 if (TREE_CODE (type) == ARRAY_TYPE)
15964 if (TYPE_DOMAIN (type)
15965 && dependent_type_p (TYPE_DOMAIN (type)))
15966 return true;
15967 return dependent_type_p (TREE_TYPE (type));
15969 else if (TREE_CODE (type) == INTEGER_TYPE
15970 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
15972 /* If this is the TYPE_DOMAIN of an array type, consider it
15973 dependent. */
15974 return (value_dependent_expression_p (TYPE_MAX_VALUE (type))
15975 || type_dependent_expression_p (TYPE_MAX_VALUE (type)));
15978 /* -- a template-id in which either the template name is a template
15979 parameter ... */
15980 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15981 return true;
15982 /* ... or any of the template arguments is a dependent type or
15983 an expression that is type-dependent or value-dependent. */
15984 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
15985 && (any_dependent_template_arguments_p
15986 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
15987 return true;
15989 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
15990 argument of the `typeof' expression is not type-dependent, then
15991 it should already been have resolved. */
15992 if (TREE_CODE (type) == TYPEOF_TYPE
15993 || TREE_CODE (type) == DECLTYPE_TYPE)
15994 return true;
15996 /* A template argument pack is dependent if any of its packed
15997 arguments are. */
15998 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16000 tree args = ARGUMENT_PACK_ARGS (type);
16001 int i, len = TREE_VEC_LENGTH (args);
16002 for (i = 0; i < len; ++i)
16003 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16004 return true;
16007 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16008 be template parameters. */
16009 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16010 return true;
16012 /* The standard does not specifically mention types that are local
16013 to template functions or local classes, but they should be
16014 considered dependent too. For example:
16016 template <int I> void f() {
16017 enum E { a = I };
16018 S<sizeof (E)> s;
16021 The size of `E' cannot be known until the value of `I' has been
16022 determined. Therefore, `E' must be considered dependent. */
16023 scope = TYPE_CONTEXT (type);
16024 if (scope && TYPE_P (scope))
16025 return dependent_type_p (scope);
16026 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16027 return type_dependent_expression_p (scope);
16029 /* Other types are non-dependent. */
16030 return false;
16033 /* Returns TRUE if TYPE is dependent, in the sense of
16034 [temp.dep.type]. */
16036 bool
16037 dependent_type_p (tree type)
16039 /* If there are no template parameters in scope, then there can't be
16040 any dependent types. */
16041 if (!processing_template_decl)
16043 /* If we are not processing a template, then nobody should be
16044 providing us with a dependent type. */
16045 gcc_assert (type);
16046 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
16047 return false;
16050 /* If the type is NULL, we have not computed a type for the entity
16051 in question; in that case, the type is dependent. */
16052 if (!type)
16053 return true;
16055 /* Erroneous types can be considered non-dependent. */
16056 if (type == error_mark_node)
16057 return false;
16059 /* If we have not already computed the appropriate value for TYPE,
16060 do so now. */
16061 if (!TYPE_DEPENDENT_P_VALID (type))
16063 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16064 TYPE_DEPENDENT_P_VALID (type) = 1;
16067 return TYPE_DEPENDENT_P (type);
16070 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16072 static bool
16073 dependent_scope_ref_p (tree expression, bool criterion (tree))
16075 tree scope;
16076 tree name;
16078 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
16080 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16081 return true;
16083 scope = TREE_OPERAND (expression, 0);
16084 name = TREE_OPERAND (expression, 1);
16086 /* [temp.dep.expr]
16088 An id-expression is type-dependent if it contains a
16089 nested-name-specifier that contains a class-name that names a
16090 dependent type. */
16091 /* The suggested resolution to Core Issue 2 implies that if the
16092 qualifying type is the current class, then we must peek
16093 inside it. */
16094 if (DECL_P (name)
16095 && currently_open_class (scope)
16096 && !criterion (name))
16097 return false;
16098 if (dependent_type_p (scope))
16099 return true;
16101 return false;
16104 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16105 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16106 expression. */
16108 bool
16109 value_dependent_expression_p (tree expression)
16111 if (!processing_template_decl)
16112 return false;
16114 /* A name declared with a dependent type. */
16115 if (DECL_P (expression) && type_dependent_expression_p (expression))
16116 return true;
16118 switch (TREE_CODE (expression))
16120 case IDENTIFIER_NODE:
16121 /* A name that has not been looked up -- must be dependent. */
16122 return true;
16124 case TEMPLATE_PARM_INDEX:
16125 /* A non-type template parm. */
16126 return true;
16128 case CONST_DECL:
16129 /* A non-type template parm. */
16130 if (DECL_TEMPLATE_PARM_P (expression))
16131 return true;
16132 return value_dependent_expression_p (DECL_INITIAL (expression));
16134 case VAR_DECL:
16135 /* A constant with integral or enumeration type and is initialized
16136 with an expression that is value-dependent. */
16137 if (DECL_INITIAL (expression)
16138 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16139 && value_dependent_expression_p (DECL_INITIAL (expression)))
16140 return true;
16141 return false;
16143 case DYNAMIC_CAST_EXPR:
16144 case STATIC_CAST_EXPR:
16145 case CONST_CAST_EXPR:
16146 case REINTERPRET_CAST_EXPR:
16147 case CAST_EXPR:
16148 /* These expressions are value-dependent if the type to which
16149 the cast occurs is dependent or the expression being casted
16150 is value-dependent. */
16152 tree type = TREE_TYPE (expression);
16154 if (dependent_type_p (type))
16155 return true;
16157 /* A functional cast has a list of operands. */
16158 expression = TREE_OPERAND (expression, 0);
16159 if (!expression)
16161 /* If there are no operands, it must be an expression such
16162 as "int()". This should not happen for aggregate types
16163 because it would form non-constant expressions. */
16164 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
16166 return false;
16169 if (TREE_CODE (expression) == TREE_LIST)
16170 return any_value_dependent_elements_p (expression);
16172 return value_dependent_expression_p (expression);
16175 case SIZEOF_EXPR:
16176 case ALIGNOF_EXPR:
16177 /* A `sizeof' expression is value-dependent if the operand is
16178 type-dependent or is a pack expansion. */
16179 expression = TREE_OPERAND (expression, 0);
16180 if (PACK_EXPANSION_P (expression))
16181 return true;
16182 else if (TYPE_P (expression))
16183 return dependent_type_p (expression);
16184 return type_dependent_expression_p (expression);
16186 case SCOPE_REF:
16187 return dependent_scope_ref_p (expression, value_dependent_expression_p);
16189 case COMPONENT_REF:
16190 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16191 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16193 case CALL_EXPR:
16194 /* A CALL_EXPR may appear in a constant expression if it is a
16195 call to a builtin function, e.g., __builtin_constant_p. All
16196 such calls are value-dependent. */
16197 return true;
16199 case NONTYPE_ARGUMENT_PACK:
16200 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16201 is value-dependent. */
16203 tree values = ARGUMENT_PACK_ARGS (expression);
16204 int i, len = TREE_VEC_LENGTH (values);
16206 for (i = 0; i < len; ++i)
16207 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16208 return true;
16210 return false;
16213 case TRAIT_EXPR:
16215 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16216 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16217 || (type2 ? dependent_type_p (type2) : false));
16220 case MODOP_EXPR:
16221 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16222 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16224 default:
16225 /* A constant expression is value-dependent if any subexpression is
16226 value-dependent. */
16227 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16229 case tcc_reference:
16230 case tcc_unary:
16231 return (value_dependent_expression_p
16232 (TREE_OPERAND (expression, 0)));
16234 case tcc_comparison:
16235 case tcc_binary:
16236 return ((value_dependent_expression_p
16237 (TREE_OPERAND (expression, 0)))
16238 || (value_dependent_expression_p
16239 (TREE_OPERAND (expression, 1))));
16241 case tcc_expression:
16242 case tcc_vl_exp:
16244 int i;
16245 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
16246 /* In some cases, some of the operands may be missing.
16247 (For example, in the case of PREDECREMENT_EXPR, the
16248 amount to increment by may be missing.) That doesn't
16249 make the expression dependent. */
16250 if (TREE_OPERAND (expression, i)
16251 && (value_dependent_expression_p
16252 (TREE_OPERAND (expression, i))))
16253 return true;
16254 return false;
16257 default:
16258 break;
16262 /* The expression is not value-dependent. */
16263 return false;
16266 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16267 [temp.dep.expr]. */
16269 bool
16270 type_dependent_expression_p (tree expression)
16272 if (!processing_template_decl)
16273 return false;
16275 if (expression == error_mark_node)
16276 return false;
16278 /* An unresolved name is always dependent. */
16279 if (TREE_CODE (expression) == IDENTIFIER_NODE
16280 || TREE_CODE (expression) == USING_DECL)
16281 return true;
16283 /* Some expression forms are never type-dependent. */
16284 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
16285 || TREE_CODE (expression) == SIZEOF_EXPR
16286 || TREE_CODE (expression) == ALIGNOF_EXPR
16287 || TREE_CODE (expression) == TRAIT_EXPR
16288 || TREE_CODE (expression) == TYPEID_EXPR
16289 || TREE_CODE (expression) == DELETE_EXPR
16290 || TREE_CODE (expression) == VEC_DELETE_EXPR
16291 || TREE_CODE (expression) == THROW_EXPR)
16292 return false;
16294 /* The types of these expressions depends only on the type to which
16295 the cast occurs. */
16296 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
16297 || TREE_CODE (expression) == STATIC_CAST_EXPR
16298 || TREE_CODE (expression) == CONST_CAST_EXPR
16299 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
16300 || TREE_CODE (expression) == CAST_EXPR)
16301 return dependent_type_p (TREE_TYPE (expression));
16303 /* The types of these expressions depends only on the type created
16304 by the expression. */
16305 if (TREE_CODE (expression) == NEW_EXPR
16306 || TREE_CODE (expression) == VEC_NEW_EXPR)
16308 /* For NEW_EXPR tree nodes created inside a template, either
16309 the object type itself or a TREE_LIST may appear as the
16310 operand 1. */
16311 tree type = TREE_OPERAND (expression, 1);
16312 if (TREE_CODE (type) == TREE_LIST)
16313 /* This is an array type. We need to check array dimensions
16314 as well. */
16315 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
16316 || value_dependent_expression_p
16317 (TREE_OPERAND (TREE_VALUE (type), 1));
16318 else
16319 return dependent_type_p (type);
16322 if (TREE_CODE (expression) == SCOPE_REF
16323 && dependent_scope_ref_p (expression,
16324 type_dependent_expression_p))
16325 return true;
16327 if (TREE_CODE (expression) == FUNCTION_DECL
16328 && DECL_LANG_SPECIFIC (expression)
16329 && DECL_TEMPLATE_INFO (expression)
16330 && (any_dependent_template_arguments_p
16331 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
16332 return true;
16334 if (TREE_CODE (expression) == TEMPLATE_DECL
16335 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
16336 return false;
16338 if (TREE_CODE (expression) == STMT_EXPR)
16339 expression = stmt_expr_value_expr (expression);
16341 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
16343 tree elt;
16344 unsigned i;
16346 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
16348 if (type_dependent_expression_p (elt))
16349 return true;
16351 return false;
16354 if (TREE_TYPE (expression) == unknown_type_node)
16356 if (TREE_CODE (expression) == ADDR_EXPR)
16357 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
16358 if (TREE_CODE (expression) == COMPONENT_REF
16359 || TREE_CODE (expression) == OFFSET_REF)
16361 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
16362 return true;
16363 expression = TREE_OPERAND (expression, 1);
16364 if (TREE_CODE (expression) == IDENTIFIER_NODE)
16365 return false;
16367 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
16368 if (TREE_CODE (expression) == SCOPE_REF)
16369 return false;
16371 if (TREE_CODE (expression) == BASELINK)
16372 expression = BASELINK_FUNCTIONS (expression);
16374 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
16376 if (any_dependent_template_arguments_p
16377 (TREE_OPERAND (expression, 1)))
16378 return true;
16379 expression = TREE_OPERAND (expression, 0);
16381 gcc_assert (TREE_CODE (expression) == OVERLOAD
16382 || TREE_CODE (expression) == FUNCTION_DECL);
16384 while (expression)
16386 if (type_dependent_expression_p (OVL_CURRENT (expression)))
16387 return true;
16388 expression = OVL_NEXT (expression);
16390 return false;
16393 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
16395 return (dependent_type_p (TREE_TYPE (expression)));
16398 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
16399 contains a type-dependent expression. */
16401 bool
16402 any_type_dependent_arguments_p (const_tree args)
16404 while (args)
16406 tree arg = TREE_VALUE (args);
16408 if (type_dependent_expression_p (arg))
16409 return true;
16410 args = TREE_CHAIN (args);
16412 return false;
16415 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
16416 expressions) contains any value-dependent expressions. */
16418 bool
16419 any_value_dependent_elements_p (const_tree list)
16421 for (; list; list = TREE_CHAIN (list))
16422 if (value_dependent_expression_p (TREE_VALUE (list)))
16423 return true;
16425 return false;
16428 /* Returns TRUE if the ARG (a template argument) is dependent. */
16430 bool
16431 dependent_template_arg_p (tree arg)
16433 if (!processing_template_decl)
16434 return false;
16436 if (TREE_CODE (arg) == TEMPLATE_DECL
16437 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16438 return dependent_template_p (arg);
16439 else if (ARGUMENT_PACK_P (arg))
16441 tree args = ARGUMENT_PACK_ARGS (arg);
16442 int i, len = TREE_VEC_LENGTH (args);
16443 for (i = 0; i < len; ++i)
16445 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16446 return true;
16449 return false;
16451 else if (TYPE_P (arg))
16452 return dependent_type_p (arg);
16453 else
16454 return (type_dependent_expression_p (arg)
16455 || value_dependent_expression_p (arg));
16458 /* Returns true if ARGS (a collection of template arguments) contains
16459 any types that require structural equality testing. */
16461 bool
16462 any_template_arguments_need_structural_equality_p (tree args)
16464 int i;
16465 int j;
16467 if (!args)
16468 return false;
16469 if (args == error_mark_node)
16470 return true;
16472 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16474 tree level = TMPL_ARGS_LEVEL (args, i + 1);
16475 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16477 tree arg = TREE_VEC_ELT (level, j);
16478 tree packed_args = NULL_TREE;
16479 int k, len = 1;
16481 if (ARGUMENT_PACK_P (arg))
16483 /* Look inside the argument pack. */
16484 packed_args = ARGUMENT_PACK_ARGS (arg);
16485 len = TREE_VEC_LENGTH (packed_args);
16488 for (k = 0; k < len; ++k)
16490 if (packed_args)
16491 arg = TREE_VEC_ELT (packed_args, k);
16493 if (error_operand_p (arg))
16494 return true;
16495 else if (TREE_CODE (arg) == TEMPLATE_DECL
16496 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16497 continue;
16498 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
16499 return true;
16500 else if (!TYPE_P (arg) && TREE_TYPE (arg)
16501 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
16502 return true;
16507 return false;
16510 /* Returns true if ARGS (a collection of template arguments) contains
16511 any dependent arguments. */
16513 bool
16514 any_dependent_template_arguments_p (const_tree args)
16516 int i;
16517 int j;
16519 if (!args)
16520 return false;
16521 if (args == error_mark_node)
16522 return true;
16524 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16526 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
16527 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16528 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
16529 return true;
16532 return false;
16535 /* Returns TRUE if the template TMPL is dependent. */
16537 bool
16538 dependent_template_p (tree tmpl)
16540 if (TREE_CODE (tmpl) == OVERLOAD)
16542 while (tmpl)
16544 if (dependent_template_p (OVL_FUNCTION (tmpl)))
16545 return true;
16546 tmpl = OVL_CHAIN (tmpl);
16548 return false;
16551 /* Template template parameters are dependent. */
16552 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
16553 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16554 return true;
16555 /* So are names that have not been looked up. */
16556 if (TREE_CODE (tmpl) == SCOPE_REF
16557 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
16558 return true;
16559 /* So are member templates of dependent classes. */
16560 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
16561 return dependent_type_p (DECL_CONTEXT (tmpl));
16562 return false;
16565 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
16567 bool
16568 dependent_template_id_p (tree tmpl, tree args)
16570 return (dependent_template_p (tmpl)
16571 || any_dependent_template_arguments_p (args));
16574 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
16575 is dependent. */
16577 bool
16578 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
16580 int i;
16582 if (!processing_template_decl)
16583 return false;
16585 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
16587 tree decl = TREE_VEC_ELT (declv, i);
16588 tree init = TREE_VEC_ELT (initv, i);
16589 tree cond = TREE_VEC_ELT (condv, i);
16590 tree incr = TREE_VEC_ELT (incrv, i);
16592 if (type_dependent_expression_p (decl))
16593 return true;
16595 if (init && type_dependent_expression_p (init))
16596 return true;
16598 if (type_dependent_expression_p (cond))
16599 return true;
16601 if (COMPARISON_CLASS_P (cond)
16602 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
16603 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
16604 return true;
16606 if (TREE_CODE (incr) == MODOP_EXPR)
16608 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
16609 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
16610 return true;
16612 else if (type_dependent_expression_p (incr))
16613 return true;
16614 else if (TREE_CODE (incr) == MODIFY_EXPR)
16616 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
16617 return true;
16618 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
16620 tree t = TREE_OPERAND (incr, 1);
16621 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
16622 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
16623 return true;
16628 return false;
16631 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
16632 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
16633 no such TYPE can be found. Note that this function peers inside
16634 uninstantiated templates and therefore should be used only in
16635 extremely limited situations. ONLY_CURRENT_P restricts this
16636 peering to the currently open classes hierarchy (which is required
16637 when comparing types). */
16639 tree
16640 resolve_typename_type (tree type, bool only_current_p)
16642 tree scope;
16643 tree name;
16644 tree decl;
16645 int quals;
16646 tree pushed_scope;
16647 tree result;
16649 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
16651 scope = TYPE_CONTEXT (type);
16652 name = TYPE_IDENTIFIER (type);
16654 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
16655 it first before we can figure out what NAME refers to. */
16656 if (TREE_CODE (scope) == TYPENAME_TYPE)
16657 scope = resolve_typename_type (scope, only_current_p);
16658 /* If we don't know what SCOPE refers to, then we cannot resolve the
16659 TYPENAME_TYPE. */
16660 if (TREE_CODE (scope) == TYPENAME_TYPE)
16661 return type;
16662 /* If the SCOPE is a template type parameter, we have no way of
16663 resolving the name. */
16664 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
16665 return type;
16666 /* If the SCOPE is not the current instantiation, there's no reason
16667 to look inside it. */
16668 if (only_current_p && !currently_open_class (scope))
16669 return type;
16670 /* If SCOPE isn't the template itself, it will not have a valid
16671 TYPE_FIELDS list. */
16672 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
16673 /* scope is either the template itself or a compatible instantiation
16674 like X<T>, so look up the name in the original template. */
16675 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
16676 else
16677 /* scope is a partial instantiation, so we can't do the lookup or we
16678 will lose the template arguments. */
16679 return type;
16680 /* Enter the SCOPE so that name lookup will be resolved as if we
16681 were in the class definition. In particular, SCOPE will no
16682 longer be considered a dependent type. */
16683 pushed_scope = push_scope (scope);
16684 /* Look up the declaration. */
16685 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
16687 result = NULL_TREE;
16689 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
16690 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
16691 if (!decl)
16692 /*nop*/;
16693 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
16694 && TREE_CODE (decl) == TYPE_DECL)
16696 result = TREE_TYPE (decl);
16697 if (result == error_mark_node)
16698 result = NULL_TREE;
16700 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
16701 && DECL_CLASS_TEMPLATE_P (decl))
16703 tree tmpl;
16704 tree args;
16705 /* Obtain the template and the arguments. */
16706 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
16707 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
16708 /* Instantiate the template. */
16709 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
16710 /*entering_scope=*/0,
16711 tf_error | tf_user);
16712 if (result == error_mark_node)
16713 result = NULL_TREE;
16716 /* Leave the SCOPE. */
16717 if (pushed_scope)
16718 pop_scope (pushed_scope);
16720 /* If we failed to resolve it, return the original typename. */
16721 if (!result)
16722 return type;
16724 /* If lookup found a typename type, resolve that too. */
16725 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
16727 /* Ill-formed programs can cause infinite recursion here, so we
16728 must catch that. */
16729 TYPENAME_IS_RESOLVING_P (type) = 1;
16730 result = resolve_typename_type (result, only_current_p);
16731 TYPENAME_IS_RESOLVING_P (type) = 0;
16734 /* Qualify the resulting type. */
16735 quals = cp_type_quals (type);
16736 if (quals)
16737 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
16739 return result;
16742 /* EXPR is an expression which is not type-dependent. Return a proxy
16743 for EXPR that can be used to compute the types of larger
16744 expressions containing EXPR. */
16746 tree
16747 build_non_dependent_expr (tree expr)
16749 tree inner_expr;
16751 /* Preserve null pointer constants so that the type of things like
16752 "p == 0" where "p" is a pointer can be determined. */
16753 if (null_ptr_cst_p (expr))
16754 return expr;
16755 /* Preserve OVERLOADs; the functions must be available to resolve
16756 types. */
16757 inner_expr = expr;
16758 if (TREE_CODE (inner_expr) == STMT_EXPR)
16759 inner_expr = stmt_expr_value_expr (inner_expr);
16760 if (TREE_CODE (inner_expr) == ADDR_EXPR)
16761 inner_expr = TREE_OPERAND (inner_expr, 0);
16762 if (TREE_CODE (inner_expr) == COMPONENT_REF)
16763 inner_expr = TREE_OPERAND (inner_expr, 1);
16764 if (is_overloaded_fn (inner_expr)
16765 || TREE_CODE (inner_expr) == OFFSET_REF)
16766 return expr;
16767 /* There is no need to return a proxy for a variable. */
16768 if (TREE_CODE (expr) == VAR_DECL)
16769 return expr;
16770 /* Preserve string constants; conversions from string constants to
16771 "char *" are allowed, even though normally a "const char *"
16772 cannot be used to initialize a "char *". */
16773 if (TREE_CODE (expr) == STRING_CST)
16774 return expr;
16775 /* Preserve arithmetic constants, as an optimization -- there is no
16776 reason to create a new node. */
16777 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
16778 return expr;
16779 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
16780 There is at least one place where we want to know that a
16781 particular expression is a throw-expression: when checking a ?:
16782 expression, there are special rules if the second or third
16783 argument is a throw-expression. */
16784 if (TREE_CODE (expr) == THROW_EXPR)
16785 return expr;
16787 if (TREE_CODE (expr) == COND_EXPR)
16788 return build3 (COND_EXPR,
16789 TREE_TYPE (expr),
16790 TREE_OPERAND (expr, 0),
16791 (TREE_OPERAND (expr, 1)
16792 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
16793 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
16794 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
16795 if (TREE_CODE (expr) == COMPOUND_EXPR
16796 && !COMPOUND_EXPR_OVERLOADED (expr))
16797 return build2 (COMPOUND_EXPR,
16798 TREE_TYPE (expr),
16799 TREE_OPERAND (expr, 0),
16800 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
16802 /* If the type is unknown, it can't really be non-dependent */
16803 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
16805 /* Otherwise, build a NON_DEPENDENT_EXPR.
16807 REFERENCE_TYPEs are not stripped for expressions in templates
16808 because doing so would play havoc with mangling. Consider, for
16809 example:
16811 template <typename T> void f<T& g>() { g(); }
16813 In the body of "f", the expression for "g" will have
16814 REFERENCE_TYPE, even though the standard says that it should
16815 not. The reason is that we must preserve the syntactic form of
16816 the expression so that mangling (say) "f<g>" inside the body of
16817 "f" works out correctly. Therefore, the REFERENCE_TYPE is
16818 stripped here. */
16819 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
16822 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
16823 Return a new TREE_LIST with the various arguments replaced with
16824 equivalent non-dependent expressions. */
16826 tree
16827 build_non_dependent_args (tree args)
16829 tree a;
16830 tree new_args;
16832 new_args = NULL_TREE;
16833 for (a = args; a; a = TREE_CHAIN (a))
16834 new_args = tree_cons (NULL_TREE,
16835 build_non_dependent_expr (TREE_VALUE (a)),
16836 new_args);
16837 return nreverse (new_args);
16840 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
16841 with a level one deeper than the actual template parms. */
16843 tree
16844 make_auto (void)
16846 tree au;
16848 /* ??? Is it worth caching this for multiple autos at the same level? */
16849 au = cxx_make_type (TEMPLATE_TYPE_PARM);
16850 TYPE_NAME (au) = build_decl (TYPE_DECL, get_identifier ("auto"), au);
16851 TYPE_STUB_DECL (au) = TYPE_NAME (au);
16852 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
16853 (0, processing_template_decl + 1, processing_template_decl + 1,
16854 TYPE_NAME (au), NULL_TREE);
16855 TYPE_CANONICAL (au) = canonical_type_parameter (au);
16856 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
16857 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
16859 return au;
16862 /* Replace auto in TYPE with std::initializer_list<auto>. */
16864 static tree
16865 listify_autos (tree type, tree auto_node)
16867 tree std_init_list = namespace_binding
16868 (get_identifier ("initializer_list"), std_node);
16869 tree argvec;
16870 tree init_auto;
16871 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
16873 error ("deducing auto from brace-enclosed initializer list requires "
16874 "#include <initializer_list>");
16875 return error_mark_node;
16877 argvec = make_tree_vec (1);
16878 TREE_VEC_ELT (argvec, 0) = auto_node;
16879 init_auto = lookup_template_class (std_init_list, argvec, NULL_TREE,
16880 NULL_TREE, 0, tf_warning_or_error);
16882 TREE_VEC_ELT (argvec, 0) = init_auto;
16883 if (processing_template_decl)
16884 argvec = add_to_template_args (current_template_args (), argvec);
16885 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
16888 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
16889 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
16891 tree
16892 do_auto_deduction (tree type, tree init, tree auto_node)
16894 tree parms, args, tparms, targs;
16895 int val;
16897 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
16898 with either a new invented type template parameter U or, if the
16899 initializer is a braced-init-list (8.5.4), with
16900 std::initializer_list<U>. */
16901 if (BRACE_ENCLOSED_INITIALIZER_P (init))
16902 type = listify_autos (type, auto_node);
16904 parms = build_tree_list (NULL_TREE, type);
16905 args = build_tree_list (NULL_TREE, init);
16906 tparms = make_tree_vec (1);
16907 targs = make_tree_vec (1);
16908 TREE_VEC_ELT (tparms, 0)
16909 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
16910 val = type_unification_real (tparms, targs, parms, args, 0,
16911 DEDUCE_CALL, LOOKUP_NORMAL);
16912 if (val > 0)
16914 error ("unable to deduce %qT from %qE", type, init);
16915 return error_mark_node;
16918 if (processing_template_decl)
16919 targs = add_to_template_args (current_template_args (), targs);
16920 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
16923 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
16924 result. */
16926 tree
16927 splice_late_return_type (tree type, tree late_return_type)
16929 tree argvec;
16931 if (late_return_type == NULL_TREE)
16932 return type;
16933 argvec = make_tree_vec (1);
16934 TREE_VEC_ELT (argvec, 0) = late_return_type;
16935 if (processing_template_decl)
16936 argvec = add_to_template_args (current_template_args (), argvec);
16937 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
16940 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
16942 bool
16943 is_auto (const_tree type)
16945 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16946 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
16947 return true;
16948 else
16949 return false;
16952 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
16953 appear as a type-specifier for the declaration in question, we don't
16954 have to look through the whole type. */
16956 tree
16957 type_uses_auto (tree type)
16959 enum tree_code code;
16960 if (is_auto (type))
16961 return type;
16963 code = TREE_CODE (type);
16965 if (code == POINTER_TYPE || code == REFERENCE_TYPE
16966 || code == OFFSET_TYPE || code == FUNCTION_TYPE
16967 || code == METHOD_TYPE || code == ARRAY_TYPE)
16968 return type_uses_auto (TREE_TYPE (type));
16970 if (TYPE_PTRMEMFUNC_P (type))
16971 return type_uses_auto (TREE_TYPE (TREE_TYPE
16972 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
16974 return NULL_TREE;
16977 #include "gt-cp-pt.h"