tree.h (enum tree_code_class): Add tcc_vl_exp.
[official-gcc.git] / gcc / cp / pt.c
blob31ad46c505282007a539c6876ee38d77e6022fda
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 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
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 The TREE_PURPOSE of each entry is either a DECL (for a function or
58 static data member), or a TYPE (for a class) indicating what we are
59 hoping to instantiate. The TREE_VALUE is not used. */
60 static GTY(()) tree pending_templates;
61 static GTY(()) tree last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) tree current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static htab_t local_specializations;
83 /* Contains canonical template parameter types. The vector is indexed by
84 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
85 TREE_LIST, whose TREE_VALUEs contain the canonical template
86 parameters of various types and levels. */
87 static GTY(()) VEC(tree,gc) *canonical_template_parms;
89 #define UNIFY_ALLOW_NONE 0
90 #define UNIFY_ALLOW_MORE_CV_QUAL 1
91 #define UNIFY_ALLOW_LESS_CV_QUAL 2
92 #define UNIFY_ALLOW_DERIVED 4
93 #define UNIFY_ALLOW_INTEGER 8
94 #define UNIFY_ALLOW_OUTER_LEVEL 16
95 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
96 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
98 static void push_access_scope (tree);
99 static void pop_access_scope (tree);
100 static int resolve_overloaded_unification (tree, tree, tree, tree,
101 unification_kind_t, int);
102 static int try_one_overload (tree, tree, tree, tree, tree,
103 unification_kind_t, int, bool);
104 static int unify (tree, tree, tree, tree, int);
105 static void add_pending_template (tree);
106 static int push_tinst_level (tree);
107 static void pop_tinst_level (void);
108 static void reopen_tinst_level (tree);
109 static tree classtype_mangled_name (tree);
110 static char* mangle_class_name_for_template (const char *, tree, tree);
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 static int type_unification_real (tree, tree, tree, tree,
121 int, unification_kind_t, int);
122 static void note_template_header (int);
123 static tree convert_nontype_argument_function (tree, tree);
124 static tree convert_nontype_argument (tree, tree);
125 static tree convert_template_argument (tree, tree, tree,
126 tsubst_flags_t, int, tree);
127 static int for_each_template_parm (tree, tree_fn_t, void*,
128 struct pointer_set_t*);
129 static tree build_template_parm_index (int, int, int, tree, tree);
130 static int inline_needs_template_parms (tree);
131 static void push_inline_template_parms_recursive (tree, int);
132 static tree retrieve_local_specialization (tree);
133 static void register_local_specialization (tree, tree);
134 static tree reduce_template_parm_level (tree, tree, int);
135 static int mark_template_parm (tree, void *);
136 static int template_parm_this_level_p (tree, void *);
137 static tree tsubst_friend_function (tree, tree);
138 static tree tsubst_friend_class (tree, tree);
139 static int can_complete_type_without_circularity (tree);
140 static tree get_bindings (tree, tree, tree, bool);
141 static int template_decl_level (tree);
142 static int check_cv_quals_for_unify (int, tree, tree);
143 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
144 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
145 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
146 static void regenerate_decl_from_template (tree, tree);
147 static tree most_specialized_class (tree, tree);
148 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
149 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
150 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
151 static bool check_specialization_scope (void);
152 static tree process_partial_specialization (tree);
153 static void set_current_access_from_decl (tree);
154 static void check_default_tmpl_args (tree, tree, int, int);
155 static tree get_template_base (tree, tree, tree, tree);
156 static tree try_class_unification (tree, tree, tree, tree);
157 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
158 tree, tree);
159 static int template_args_equal (tree, tree);
160 static void tsubst_default_arguments (tree);
161 static tree for_each_template_parm_r (tree *, int *, void *);
162 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
163 static void copy_default_args_to_explicit_spec (tree);
164 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
165 static int eq_local_specializations (const void *, const void *);
166 static bool any_template_arguments_need_structural_equality_p (tree);
167 static bool dependent_type_p_r (tree);
168 static tree tsubst (tree, tree, tsubst_flags_t, tree);
169 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
170 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
172 /* Make the current scope suitable for access checking when we are
173 processing T. T can be FUNCTION_DECL for instantiated function
174 template, or VAR_DECL for static member variable (need by
175 instantiate_decl). */
177 static void
178 push_access_scope (tree t)
180 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
181 || TREE_CODE (t) == VAR_DECL);
183 if (DECL_FRIEND_CONTEXT (t))
184 push_nested_class (DECL_FRIEND_CONTEXT (t));
185 else if (DECL_CLASS_SCOPE_P (t))
186 push_nested_class (DECL_CONTEXT (t));
187 else
188 push_to_top_level ();
190 if (TREE_CODE (t) == FUNCTION_DECL)
192 saved_access_scope = tree_cons
193 (NULL_TREE, current_function_decl, saved_access_scope);
194 current_function_decl = t;
198 /* Restore the scope set up by push_access_scope. T is the node we
199 are processing. */
201 static void
202 pop_access_scope (tree t)
204 if (TREE_CODE (t) == FUNCTION_DECL)
206 current_function_decl = TREE_VALUE (saved_access_scope);
207 saved_access_scope = TREE_CHAIN (saved_access_scope);
210 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
211 pop_nested_class ();
212 else
213 pop_from_top_level ();
216 /* Do any processing required when DECL (a member template
217 declaration) is finished. Returns the TEMPLATE_DECL corresponding
218 to DECL, unless it is a specialization, in which case the DECL
219 itself is returned. */
221 tree
222 finish_member_template_decl (tree decl)
224 if (decl == error_mark_node)
225 return error_mark_node;
227 gcc_assert (DECL_P (decl));
229 if (TREE_CODE (decl) == TYPE_DECL)
231 tree type;
233 type = TREE_TYPE (decl);
234 if (IS_AGGR_TYPE (type)
235 && CLASSTYPE_TEMPLATE_INFO (type)
236 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
238 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
239 check_member_template (tmpl);
240 return tmpl;
242 return NULL_TREE;
244 else if (TREE_CODE (decl) == FIELD_DECL)
245 error ("data member %qD cannot be a member template", decl);
246 else if (DECL_TEMPLATE_INFO (decl))
248 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
250 check_member_template (DECL_TI_TEMPLATE (decl));
251 return DECL_TI_TEMPLATE (decl);
253 else
254 return decl;
256 else
257 error ("invalid member template declaration %qD", decl);
259 return error_mark_node;
262 /* Returns the template nesting level of the indicated class TYPE.
264 For example, in:
265 template <class T>
266 struct A
268 template <class U>
269 struct B {};
272 A<T>::B<U> has depth two, while A<T> has depth one.
273 Both A<T>::B<int> and A<int>::B<U> have depth one, if
274 they are instantiations, not specializations.
276 This function is guaranteed to return 0 if passed NULL_TREE so
277 that, for example, `template_class_depth (current_class_type)' is
278 always safe. */
281 template_class_depth (tree type)
283 int depth;
285 for (depth = 0;
286 type && TREE_CODE (type) != NAMESPACE_DECL;
287 type = (TREE_CODE (type) == FUNCTION_DECL)
288 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
290 if (TREE_CODE (type) != FUNCTION_DECL)
292 if (CLASSTYPE_TEMPLATE_INFO (type)
293 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
294 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
295 ++depth;
297 else
299 if (DECL_TEMPLATE_INFO (type)
300 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
301 && uses_template_parms (DECL_TI_ARGS (type)))
302 ++depth;
306 return depth;
309 /* Returns 1 if processing DECL as part of do_pending_inlines
310 needs us to push template parms. */
312 static int
313 inline_needs_template_parms (tree decl)
315 if (! DECL_TEMPLATE_INFO (decl))
316 return 0;
318 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
319 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
322 /* Subroutine of maybe_begin_member_template_processing.
323 Push the template parms in PARMS, starting from LEVELS steps into the
324 chain, and ending at the beginning, since template parms are listed
325 innermost first. */
327 static void
328 push_inline_template_parms_recursive (tree parmlist, int levels)
330 tree parms = TREE_VALUE (parmlist);
331 int i;
333 if (levels > 1)
334 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
336 ++processing_template_decl;
337 current_template_parms
338 = tree_cons (size_int (processing_template_decl),
339 parms, current_template_parms);
340 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
342 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
343 NULL);
344 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
346 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
348 if (parm == error_mark_node)
349 continue;
351 gcc_assert (DECL_P (parm));
353 switch (TREE_CODE (parm))
355 case TYPE_DECL:
356 case TEMPLATE_DECL:
357 pushdecl (parm);
358 break;
360 case PARM_DECL:
362 /* Make a CONST_DECL as is done in process_template_parm.
363 It is ugly that we recreate this here; the original
364 version built in process_template_parm is no longer
365 available. */
366 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
367 TREE_TYPE (parm));
368 DECL_ARTIFICIAL (decl) = 1;
369 TREE_CONSTANT (decl) = 1;
370 TREE_INVARIANT (decl) = 1;
371 TREE_READONLY (decl) = 1;
372 DECL_INITIAL (decl) = DECL_INITIAL (parm);
373 SET_DECL_TEMPLATE_PARM_P (decl);
374 pushdecl (decl);
376 break;
378 default:
379 gcc_unreachable ();
384 /* Restore the template parameter context for a member template or
385 a friend template defined in a class definition. */
387 void
388 maybe_begin_member_template_processing (tree decl)
390 tree parms;
391 int levels = 0;
393 if (inline_needs_template_parms (decl))
395 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
396 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
398 if (DECL_TEMPLATE_SPECIALIZATION (decl))
400 --levels;
401 parms = TREE_CHAIN (parms);
404 push_inline_template_parms_recursive (parms, levels);
407 /* Remember how many levels of template parameters we pushed so that
408 we can pop them later. */
409 VEC_safe_push (int, heap, inline_parm_levels, levels);
412 /* Undo the effects of maybe_begin_member_template_processing. */
414 void
415 maybe_end_member_template_processing (void)
417 int i;
418 int last;
420 if (VEC_length (int, inline_parm_levels) == 0)
421 return;
423 last = VEC_pop (int, inline_parm_levels);
424 for (i = 0; i < last; ++i)
426 --processing_template_decl;
427 current_template_parms = TREE_CHAIN (current_template_parms);
428 poplevel (0, 0, 0);
432 /* Return a new template argument vector which contains all of ARGS,
433 but has as its innermost set of arguments the EXTRA_ARGS. */
435 static tree
436 add_to_template_args (tree args, tree extra_args)
438 tree new_args;
439 int extra_depth;
440 int i;
441 int j;
443 extra_depth = TMPL_ARGS_DEPTH (extra_args);
444 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
446 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
447 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
449 for (j = 1; j <= extra_depth; ++j, ++i)
450 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
452 return new_args;
455 /* Like add_to_template_args, but only the outermost ARGS are added to
456 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
457 (EXTRA_ARGS) levels are added. This function is used to combine
458 the template arguments from a partial instantiation with the
459 template arguments used to attain the full instantiation from the
460 partial instantiation. */
462 static tree
463 add_outermost_template_args (tree args, tree extra_args)
465 tree new_args;
467 /* If there are more levels of EXTRA_ARGS than there are ARGS,
468 something very fishy is going on. */
469 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
471 /* If *all* the new arguments will be the EXTRA_ARGS, just return
472 them. */
473 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
474 return extra_args;
476 /* For the moment, we make ARGS look like it contains fewer levels. */
477 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
479 new_args = add_to_template_args (args, extra_args);
481 /* Now, we restore ARGS to its full dimensions. */
482 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
484 return new_args;
487 /* Return the N levels of innermost template arguments from the ARGS. */
489 tree
490 get_innermost_template_args (tree args, int n)
492 tree new_args;
493 int extra_levels;
494 int i;
496 gcc_assert (n >= 0);
498 /* If N is 1, just return the innermost set of template arguments. */
499 if (n == 1)
500 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
502 /* If we're not removing anything, just return the arguments we were
503 given. */
504 extra_levels = TMPL_ARGS_DEPTH (args) - n;
505 gcc_assert (extra_levels >= 0);
506 if (extra_levels == 0)
507 return args;
509 /* Make a new set of arguments, not containing the outer arguments. */
510 new_args = make_tree_vec (n);
511 for (i = 1; i <= n; ++i)
512 SET_TMPL_ARGS_LEVEL (new_args, i,
513 TMPL_ARGS_LEVEL (args, i + extra_levels));
515 return new_args;
518 /* We've got a template header coming up; push to a new level for storing
519 the parms. */
521 void
522 begin_template_parm_list (void)
524 /* We use a non-tag-transparent scope here, which causes pushtag to
525 put tags in this scope, rather than in the enclosing class or
526 namespace scope. This is the right thing, since we want
527 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
528 global template class, push_template_decl handles putting the
529 TEMPLATE_DECL into top-level scope. For a nested template class,
530 e.g.:
532 template <class T> struct S1 {
533 template <class T> struct S2 {};
536 pushtag contains special code to call pushdecl_with_scope on the
537 TEMPLATE_DECL for S2. */
538 begin_scope (sk_template_parms, NULL);
539 ++processing_template_decl;
540 ++processing_template_parmlist;
541 note_template_header (0);
544 /* This routine is called when a specialization is declared. If it is
545 invalid to declare a specialization here, an error is reported and
546 false is returned, otherwise this routine will return true. */
548 static bool
549 check_specialization_scope (void)
551 tree scope = current_scope ();
553 /* [temp.expl.spec]
555 An explicit specialization shall be declared in the namespace of
556 which the template is a member, or, for member templates, in the
557 namespace of which the enclosing class or enclosing class
558 template is a member. An explicit specialization of a member
559 function, member class or static data member of a class template
560 shall be declared in the namespace of which the class template
561 is a member. */
562 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
564 error ("explicit specialization in non-namespace scope %qD", scope);
565 return false;
568 /* [temp.expl.spec]
570 In an explicit specialization declaration for a member of a class
571 template or a member template that appears in namespace scope,
572 the member template and some of its enclosing class templates may
573 remain unspecialized, except that the declaration shall not
574 explicitly specialize a class member template if its enclosing
575 class templates are not explicitly specialized as well. */
576 if (current_template_parms)
578 error ("enclosing class templates are not explicitly specialized");
579 return false;
582 return true;
585 /* We've just seen template <>. */
587 bool
588 begin_specialization (void)
590 begin_scope (sk_template_spec, NULL);
591 note_template_header (1);
592 return check_specialization_scope ();
595 /* Called at then end of processing a declaration preceded by
596 template<>. */
598 void
599 end_specialization (void)
601 finish_scope ();
602 reset_specialization ();
605 /* Any template <>'s that we have seen thus far are not referring to a
606 function specialization. */
608 void
609 reset_specialization (void)
611 processing_specialization = 0;
612 template_header_count = 0;
615 /* We've just seen a template header. If SPECIALIZATION is nonzero,
616 it was of the form template <>. */
618 static void
619 note_template_header (int specialization)
621 processing_specialization = specialization;
622 template_header_count++;
625 /* We're beginning an explicit instantiation. */
627 void
628 begin_explicit_instantiation (void)
630 gcc_assert (!processing_explicit_instantiation);
631 processing_explicit_instantiation = true;
635 void
636 end_explicit_instantiation (void)
638 gcc_assert (processing_explicit_instantiation);
639 processing_explicit_instantiation = false;
642 /* An explicit specialization or partial specialization TMPL is being
643 declared. Check that the namespace in which the specialization is
644 occurring is permissible. Returns false iff it is invalid to
645 specialize TMPL in the current namespace. */
647 static bool
648 check_specialization_namespace (tree tmpl)
650 tree tpl_ns = decl_namespace_context (tmpl);
652 /* [tmpl.expl.spec]
654 An explicit specialization shall be declared in the namespace of
655 which the template is a member, or, for member templates, in the
656 namespace of which the enclosing class or enclosing class
657 template is a member. An explicit specialization of a member
658 function, member class or static data member of a class template
659 shall be declared in the namespace of which the class template is
660 a member. */
661 if (is_associated_namespace (current_namespace, tpl_ns))
662 /* Same or super-using namespace. */
663 return true;
664 else
666 pedwarn ("specialization of %qD in different namespace", tmpl);
667 pedwarn (" from definition of %q+#D", tmpl);
668 return false;
672 /* SPEC is an explicit instantiation. Check that it is valid to
673 perform this explicit instantiation in the current namespace. */
675 static void
676 check_explicit_instantiation_namespace (tree spec)
678 tree ns;
680 /* DR 275: An explicit instantiation shall appear in an enclosing
681 namespace of its template. */
682 ns = decl_namespace_context (spec);
683 if (!is_ancestor (current_namespace, ns))
684 pedwarn ("explicit instantiation of %qD in namespace %qD "
685 "(which does not enclose namespace %qD)",
686 spec, current_namespace, ns);
689 /* The TYPE is being declared. If it is a template type, that means it
690 is a partial specialization. Do appropriate error-checking. */
692 tree
693 maybe_process_partial_specialization (tree type)
695 tree context;
697 if (type == error_mark_node)
698 return error_mark_node;
700 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
702 error ("name of class shadows template template parameter %qD",
703 TYPE_NAME (type));
704 return error_mark_node;
707 context = TYPE_CONTEXT (type);
709 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
711 /* This is for ordinary explicit specialization and partial
712 specialization of a template class such as:
714 template <> class C<int>;
718 template <class T> class C<T*>;
720 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
722 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
723 && !COMPLETE_TYPE_P (type))
725 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
726 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
727 if (processing_template_decl)
728 push_template_decl (TYPE_MAIN_DECL (type));
730 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
731 error ("specialization of %qT after instantiation", type);
733 else if (CLASS_TYPE_P (type)
734 && !CLASSTYPE_USE_TEMPLATE (type)
735 && CLASSTYPE_TEMPLATE_INFO (type)
736 && context && CLASS_TYPE_P (context)
737 && CLASSTYPE_TEMPLATE_INFO (context))
739 /* This is for an explicit specialization of member class
740 template according to [temp.expl.spec/18]:
742 template <> template <class U> class C<int>::D;
744 The context `C<int>' must be an implicit instantiation.
745 Otherwise this is just a member class template declared
746 earlier like:
748 template <> class C<int> { template <class U> class D; };
749 template <> template <class U> class C<int>::D;
751 In the first case, `C<int>::D' is a specialization of `C<T>::D'
752 while in the second case, `C<int>::D' is a primary template
753 and `C<T>::D' may not exist. */
755 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
756 && !COMPLETE_TYPE_P (type))
758 tree t;
760 if (current_namespace
761 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
763 pedwarn ("specializing %q#T in different namespace", type);
764 pedwarn (" from definition of %q+#D",
765 CLASSTYPE_TI_TEMPLATE (type));
768 /* Check for invalid specialization after instantiation:
770 template <> template <> class C<int>::D<int>;
771 template <> template <class U> class C<int>::D; */
773 for (t = DECL_TEMPLATE_INSTANTIATIONS
774 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
775 t; t = TREE_CHAIN (t))
776 if (TREE_VALUE (t) != type
777 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
778 error ("specialization %qT after instantiation %qT",
779 type, TREE_VALUE (t));
781 /* Mark TYPE as a specialization. And as a result, we only
782 have one level of template argument for the innermost
783 class template. */
784 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
785 CLASSTYPE_TI_ARGS (type)
786 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
789 else if (processing_specialization)
791 error ("explicit specialization of non-template %qT", type);
792 return error_mark_node;
795 return type;
798 /* Returns nonzero if we can optimize the retrieval of specializations
799 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
800 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
802 static inline bool
803 optimize_specialization_lookup_p (tree tmpl)
805 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
806 && DECL_CLASS_SCOPE_P (tmpl)
807 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
808 parameter. */
809 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
810 /* The optimized lookup depends on the fact that the
811 template arguments for the member function template apply
812 purely to the containing class, which is not true if the
813 containing class is an explicit or partial
814 specialization. */
815 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
816 && !DECL_MEMBER_TEMPLATE_P (tmpl)
817 && !DECL_CONV_FN_P (tmpl)
818 /* It is possible to have a template that is not a member
819 template and is not a member of a template class:
821 template <typename T>
822 struct S { friend A::f(); };
824 Here, the friend function is a template, but the context does
825 not have template information. The optimized lookup relies
826 on having ARGS be the template arguments for both the class
827 and the function template. */
828 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
831 /* Retrieve the specialization (in the sense of [temp.spec] - a
832 specialization is either an instantiation or an explicit
833 specialization) of TMPL for the given template ARGS. If there is
834 no such specialization, return NULL_TREE. The ARGS are a vector of
835 arguments, or a vector of vectors of arguments, in the case of
836 templates with more than one level of parameters.
838 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
839 then we search for a partial specialization matching ARGS. This
840 parameter is ignored if TMPL is not a class template. */
842 static tree
843 retrieve_specialization (tree tmpl, tree args,
844 bool class_specializations_p)
846 if (args == error_mark_node)
847 return NULL_TREE;
849 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
851 /* There should be as many levels of arguments as there are
852 levels of parameters. */
853 gcc_assert (TMPL_ARGS_DEPTH (args)
854 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
856 if (optimize_specialization_lookup_p (tmpl))
858 tree class_template;
859 tree class_specialization;
860 VEC(tree,gc) *methods;
861 tree fns;
862 int idx;
864 /* The template arguments actually apply to the containing
865 class. Find the class specialization with those
866 arguments. */
867 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
868 class_specialization
869 = retrieve_specialization (class_template, args,
870 /*class_specializations_p=*/false);
871 if (!class_specialization)
872 return NULL_TREE;
873 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
874 for the specialization. */
875 idx = class_method_index_for_fn (class_specialization, tmpl);
876 if (idx == -1)
877 return NULL_TREE;
878 /* Iterate through the methods with the indicated name, looking
879 for the one that has an instance of TMPL. */
880 methods = CLASSTYPE_METHOD_VEC (class_specialization);
881 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
883 tree fn = OVL_CURRENT (fns);
884 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
885 return fn;
887 return NULL_TREE;
889 else
891 tree *sp;
892 tree *head;
894 /* Class templates store their instantiations on the
895 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
896 DECL_TEMPLATE_SPECIALIZATIONS list. */
897 if (!class_specializations_p
898 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
899 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
900 else
901 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
902 head = sp;
903 /* Iterate through the list until we find a matching template. */
904 while (*sp != NULL_TREE)
906 tree spec = *sp;
908 if (comp_template_args (TREE_PURPOSE (spec), args))
910 /* Use the move-to-front heuristic to speed up future
911 searches. */
912 if (spec != *head)
914 *sp = TREE_CHAIN (*sp);
915 TREE_CHAIN (spec) = *head;
916 *head = spec;
918 return TREE_VALUE (spec);
920 sp = &TREE_CHAIN (spec);
924 return NULL_TREE;
927 /* Like retrieve_specialization, but for local declarations. */
929 static tree
930 retrieve_local_specialization (tree tmpl)
932 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
933 htab_hash_pointer (tmpl));
934 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
937 /* Returns nonzero iff DECL is a specialization of TMPL. */
940 is_specialization_of (tree decl, tree tmpl)
942 tree t;
944 if (TREE_CODE (decl) == FUNCTION_DECL)
946 for (t = decl;
947 t != NULL_TREE;
948 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
949 if (t == tmpl)
950 return 1;
952 else
954 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
956 for (t = TREE_TYPE (decl);
957 t != NULL_TREE;
958 t = CLASSTYPE_USE_TEMPLATE (t)
959 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
960 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
961 return 1;
964 return 0;
967 /* Returns nonzero iff DECL is a specialization of friend declaration
968 FRIEND according to [temp.friend]. */
970 bool
971 is_specialization_of_friend (tree decl, tree friend)
973 bool need_template = true;
974 int template_depth;
976 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
977 || TREE_CODE (decl) == TYPE_DECL);
979 /* For [temp.friend/6] when FRIEND is an ordinary member function
980 of a template class, we want to check if DECL is a specialization
981 if this. */
982 if (TREE_CODE (friend) == FUNCTION_DECL
983 && DECL_TEMPLATE_INFO (friend)
984 && !DECL_USE_TEMPLATE (friend))
986 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
987 friend = DECL_TI_TEMPLATE (friend);
988 need_template = false;
990 else if (TREE_CODE (friend) == TEMPLATE_DECL
991 && !PRIMARY_TEMPLATE_P (friend))
992 need_template = false;
994 /* There is nothing to do if this is not a template friend. */
995 if (TREE_CODE (friend) != TEMPLATE_DECL)
996 return false;
998 if (is_specialization_of (decl, friend))
999 return true;
1001 /* [temp.friend/6]
1002 A member of a class template may be declared to be a friend of a
1003 non-template class. In this case, the corresponding member of
1004 every specialization of the class template is a friend of the
1005 class granting friendship.
1007 For example, given a template friend declaration
1009 template <class T> friend void A<T>::f();
1011 the member function below is considered a friend
1013 template <> struct A<int> {
1014 void f();
1017 For this type of template friend, TEMPLATE_DEPTH below will be
1018 nonzero. To determine if DECL is a friend of FRIEND, we first
1019 check if the enclosing class is a specialization of another. */
1021 template_depth = template_class_depth (DECL_CONTEXT (friend));
1022 if (template_depth
1023 && DECL_CLASS_SCOPE_P (decl)
1024 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1025 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1027 /* Next, we check the members themselves. In order to handle
1028 a few tricky cases, such as when FRIEND's are
1030 template <class T> friend void A<T>::g(T t);
1031 template <class T> template <T t> friend void A<T>::h();
1033 and DECL's are
1035 void A<int>::g(int);
1036 template <int> void A<int>::h();
1038 we need to figure out ARGS, the template arguments from
1039 the context of DECL. This is required for template substitution
1040 of `T' in the function parameter of `g' and template parameter
1041 of `h' in the above examples. Here ARGS corresponds to `int'. */
1043 tree context = DECL_CONTEXT (decl);
1044 tree args = NULL_TREE;
1045 int current_depth = 0;
1047 while (current_depth < template_depth)
1049 if (CLASSTYPE_TEMPLATE_INFO (context))
1051 if (current_depth == 0)
1052 args = TYPE_TI_ARGS (context);
1053 else
1054 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1055 current_depth++;
1057 context = TYPE_CONTEXT (context);
1060 if (TREE_CODE (decl) == FUNCTION_DECL)
1062 bool is_template;
1063 tree friend_type;
1064 tree decl_type;
1065 tree friend_args_type;
1066 tree decl_args_type;
1068 /* Make sure that both DECL and FRIEND are templates or
1069 non-templates. */
1070 is_template = DECL_TEMPLATE_INFO (decl)
1071 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1072 if (need_template ^ is_template)
1073 return false;
1074 else if (is_template)
1076 /* If both are templates, check template parameter list. */
1077 tree friend_parms
1078 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1079 args, tf_none);
1080 if (!comp_template_parms
1081 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1082 friend_parms))
1083 return false;
1085 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1087 else
1088 decl_type = TREE_TYPE (decl);
1090 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1091 tf_none, NULL_TREE);
1092 if (friend_type == error_mark_node)
1093 return false;
1095 /* Check if return types match. */
1096 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1097 return false;
1099 /* Check if function parameter types match, ignoring the
1100 `this' parameter. */
1101 friend_args_type = TYPE_ARG_TYPES (friend_type);
1102 decl_args_type = TYPE_ARG_TYPES (decl_type);
1103 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1104 friend_args_type = TREE_CHAIN (friend_args_type);
1105 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1106 decl_args_type = TREE_CHAIN (decl_args_type);
1108 return compparms (decl_args_type, friend_args_type);
1110 else
1112 /* DECL is a TYPE_DECL */
1113 bool is_template;
1114 tree decl_type = TREE_TYPE (decl);
1116 /* Make sure that both DECL and FRIEND are templates or
1117 non-templates. */
1118 is_template
1119 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1120 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1122 if (need_template ^ is_template)
1123 return false;
1124 else if (is_template)
1126 tree friend_parms;
1127 /* If both are templates, check the name of the two
1128 TEMPLATE_DECL's first because is_friend didn't. */
1129 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1130 != DECL_NAME (friend))
1131 return false;
1133 /* Now check template parameter list. */
1134 friend_parms
1135 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1136 args, tf_none);
1137 return comp_template_parms
1138 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1139 friend_parms);
1141 else
1142 return (DECL_NAME (decl)
1143 == DECL_NAME (friend));
1146 return false;
1149 /* Register the specialization SPEC as a specialization of TMPL with
1150 the indicated ARGS. IS_FRIEND indicates whether the specialization
1151 is actually just a friend declaration. Returns SPEC, or an
1152 equivalent prior declaration, if available. */
1154 static tree
1155 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1157 tree fn;
1159 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1161 if (TREE_CODE (spec) == FUNCTION_DECL
1162 && uses_template_parms (DECL_TI_ARGS (spec)))
1163 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1164 register it; we want the corresponding TEMPLATE_DECL instead.
1165 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1166 the more obvious `uses_template_parms (spec)' to avoid problems
1167 with default function arguments. In particular, given
1168 something like this:
1170 template <class T> void f(T t1, T t = T())
1172 the default argument expression is not substituted for in an
1173 instantiation unless and until it is actually needed. */
1174 return spec;
1176 fn = retrieve_specialization (tmpl, args,
1177 /*class_specializations_p=*/false);
1178 /* We can sometimes try to re-register a specialization that we've
1179 already got. In particular, regenerate_decl_from_template calls
1180 duplicate_decls which will update the specialization list. But,
1181 we'll still get called again here anyhow. It's more convenient
1182 to simply allow this than to try to prevent it. */
1183 if (fn == spec)
1184 return spec;
1185 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1187 if (DECL_TEMPLATE_INSTANTIATION (fn))
1189 if (TREE_USED (fn)
1190 || DECL_EXPLICIT_INSTANTIATION (fn))
1192 error ("specialization of %qD after instantiation",
1193 fn);
1194 return error_mark_node;
1196 else
1198 tree clone;
1199 /* This situation should occur only if the first
1200 specialization is an implicit instantiation, the
1201 second is an explicit specialization, and the
1202 implicit instantiation has not yet been used. That
1203 situation can occur if we have implicitly
1204 instantiated a member function and then specialized
1205 it later.
1207 We can also wind up here if a friend declaration that
1208 looked like an instantiation turns out to be a
1209 specialization:
1211 template <class T> void foo(T);
1212 class S { friend void foo<>(int) };
1213 template <> void foo(int);
1215 We transform the existing DECL in place so that any
1216 pointers to it become pointers to the updated
1217 declaration.
1219 If there was a definition for the template, but not
1220 for the specialization, we want this to look as if
1221 there were no definition, and vice versa. */
1222 DECL_INITIAL (fn) = NULL_TREE;
1223 duplicate_decls (spec, fn, is_friend);
1224 /* The call to duplicate_decls will have applied
1225 [temp.expl.spec]:
1227 An explicit specialization of a function template
1228 is inline only if it is explicitly declared to be,
1229 and independently of whether its function template
1232 to the primary function; now copy the inline bits to
1233 the various clones. */
1234 FOR_EACH_CLONE (clone, fn)
1236 DECL_DECLARED_INLINE_P (clone)
1237 = DECL_DECLARED_INLINE_P (fn);
1238 DECL_INLINE (clone)
1239 = DECL_INLINE (fn);
1241 check_specialization_namespace (fn);
1243 return fn;
1246 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1248 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1249 /* Dup decl failed, but this is a new definition. Set the
1250 line number so any errors match this new
1251 definition. */
1252 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1254 return fn;
1258 /* A specialization must be declared in the same namespace as the
1259 template it is specializing. */
1260 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1261 && !check_specialization_namespace (tmpl))
1262 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1264 if (!optimize_specialization_lookup_p (tmpl))
1265 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1266 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1268 return spec;
1271 /* Unregister the specialization SPEC as a specialization of TMPL.
1272 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1273 if the SPEC was listed as a specialization of TMPL. */
1275 bool
1276 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1278 tree* s;
1280 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1281 *s != NULL_TREE;
1282 s = &TREE_CHAIN (*s))
1283 if (TREE_VALUE (*s) == spec)
1285 if (!new_spec)
1286 *s = TREE_CHAIN (*s);
1287 else
1288 TREE_VALUE (*s) = new_spec;
1289 return 1;
1292 return 0;
1295 /* Compare an entry in the local specializations hash table P1 (which
1296 is really a pointer to a TREE_LIST) with P2 (which is really a
1297 DECL). */
1299 static int
1300 eq_local_specializations (const void *p1, const void *p2)
1302 return TREE_VALUE ((tree) p1) == (tree) p2;
1305 /* Hash P1, an entry in the local specializations table. */
1307 static hashval_t
1308 hash_local_specialization (const void* p1)
1310 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1313 /* Like register_specialization, but for local declarations. We are
1314 registering SPEC, an instantiation of TMPL. */
1316 static void
1317 register_local_specialization (tree spec, tree tmpl)
1319 void **slot;
1321 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1322 htab_hash_pointer (tmpl), INSERT);
1323 *slot = build_tree_list (spec, tmpl);
1326 /* TYPE is a class type. Returns true if TYPE is an explicitly
1327 specialized class. */
1329 bool
1330 explicit_class_specialization_p (tree type)
1332 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1333 return false;
1334 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1337 /* Print the list of candidate FNS in an error message. */
1339 void
1340 print_candidates (tree fns)
1342 tree fn;
1344 const char *str = "candidates are:";
1346 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1348 tree f;
1350 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1351 error ("%s %+#D", str, OVL_CURRENT (f));
1352 str = " ";
1356 /* Returns the template (one of the functions given by TEMPLATE_ID)
1357 which can be specialized to match the indicated DECL with the
1358 explicit template args given in TEMPLATE_ID. The DECL may be
1359 NULL_TREE if none is available. In that case, the functions in
1360 TEMPLATE_ID are non-members.
1362 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1363 specialization of a member template.
1365 The TEMPLATE_COUNT is the number of references to qualifying
1366 template classes that appeared in the name of the function. See
1367 check_explicit_specialization for a more accurate description.
1369 TSK indicates what kind of template declaration (if any) is being
1370 declared. TSK_TEMPLATE indicates that the declaration given by
1371 DECL, though a FUNCTION_DECL, has template parameters, and is
1372 therefore a template function.
1374 The template args (those explicitly specified and those deduced)
1375 are output in a newly created vector *TARGS_OUT.
1377 If it is impossible to determine the result, an error message is
1378 issued. The error_mark_node is returned to indicate failure. */
1380 static tree
1381 determine_specialization (tree template_id,
1382 tree decl,
1383 tree* targs_out,
1384 int need_member_template,
1385 int template_count,
1386 tmpl_spec_kind tsk)
1388 tree fns;
1389 tree targs;
1390 tree explicit_targs;
1391 tree candidates = NULL_TREE;
1392 /* A TREE_LIST of templates of which DECL may be a specialization.
1393 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1394 corresponding TREE_PURPOSE is the set of template arguments that,
1395 when used to instantiate the template, would produce a function
1396 with the signature of DECL. */
1397 tree templates = NULL_TREE;
1398 int header_count;
1399 struct cp_binding_level *b;
1401 *targs_out = NULL_TREE;
1403 if (template_id == error_mark_node || decl == error_mark_node)
1404 return error_mark_node;
1406 fns = TREE_OPERAND (template_id, 0);
1407 explicit_targs = TREE_OPERAND (template_id, 1);
1409 if (fns == error_mark_node)
1410 return error_mark_node;
1412 /* Check for baselinks. */
1413 if (BASELINK_P (fns))
1414 fns = BASELINK_FUNCTIONS (fns);
1416 if (!is_overloaded_fn (fns))
1418 error ("%qD is not a function template", fns);
1419 return error_mark_node;
1422 /* Count the number of template headers specified for this
1423 specialization. */
1424 header_count = 0;
1425 for (b = current_binding_level;
1426 b->kind == sk_template_parms;
1427 b = b->level_chain)
1428 ++header_count;
1430 for (; fns; fns = OVL_NEXT (fns))
1432 tree fn = OVL_CURRENT (fns);
1434 if (TREE_CODE (fn) == TEMPLATE_DECL)
1436 tree decl_arg_types;
1437 tree fn_arg_types;
1439 /* In case of explicit specialization, we need to check if
1440 the number of template headers appearing in the specialization
1441 is correct. This is usually done in check_explicit_specialization,
1442 but the check done there cannot be exhaustive when specializing
1443 member functions. Consider the following code:
1445 template <> void A<int>::f(int);
1446 template <> template <> void A<int>::f(int);
1448 Assuming that A<int> is not itself an explicit specialization
1449 already, the first line specializes "f" which is a non-template
1450 member function, whilst the second line specializes "f" which
1451 is a template member function. So both lines are syntactically
1452 correct, and check_explicit_specialization does not reject
1453 them.
1455 Here, we can do better, as we are matching the specialization
1456 against the declarations. We count the number of template
1457 headers, and we check if they match TEMPLATE_COUNT + 1
1458 (TEMPLATE_COUNT is the number of qualifying template classes,
1459 plus there must be another header for the member template
1460 itself).
1462 Notice that if header_count is zero, this is not a
1463 specialization but rather a template instantiation, so there
1464 is no check we can perform here. */
1465 if (header_count && header_count != template_count + 1)
1466 continue;
1468 /* Check that the number of template arguments at the
1469 innermost level for DECL is the same as for FN. */
1470 if (current_binding_level->kind == sk_template_parms
1471 && !current_binding_level->explicit_spec_p
1472 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1473 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1474 (current_template_parms))))
1475 continue;
1477 /* DECL might be a specialization of FN. */
1478 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1479 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1481 /* For a non-static member function, we need to make sure
1482 that the const qualification is the same. Since
1483 get_bindings does not try to merge the "this" parameter,
1484 we must do the comparison explicitly. */
1485 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1486 && !same_type_p (TREE_VALUE (fn_arg_types),
1487 TREE_VALUE (decl_arg_types)))
1488 continue;
1490 /* Skip the "this" parameter and, for constructors of
1491 classes with virtual bases, the VTT parameter. A
1492 full specialization of a constructor will have a VTT
1493 parameter, but a template never will. */
1494 decl_arg_types
1495 = skip_artificial_parms_for (decl, decl_arg_types);
1496 fn_arg_types
1497 = skip_artificial_parms_for (fn, fn_arg_types);
1499 /* Check that the number of function parameters matches.
1500 For example,
1501 template <class T> void f(int i = 0);
1502 template <> void f<int>();
1503 The specialization f<int> is invalid but is not caught
1504 by get_bindings below. */
1505 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1506 continue;
1508 /* Function templates cannot be specializations; there are
1509 no partial specializations of functions. Therefore, if
1510 the type of DECL does not match FN, there is no
1511 match. */
1512 if (tsk == tsk_template)
1514 if (compparms (fn_arg_types, decl_arg_types))
1515 candidates = tree_cons (NULL_TREE, fn, candidates);
1516 continue;
1519 /* See whether this function might be a specialization of this
1520 template. */
1521 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1523 if (!targs)
1524 /* We cannot deduce template arguments that when used to
1525 specialize TMPL will produce DECL. */
1526 continue;
1528 /* Save this template, and the arguments deduced. */
1529 templates = tree_cons (targs, fn, templates);
1531 else if (need_member_template)
1532 /* FN is an ordinary member function, and we need a
1533 specialization of a member template. */
1535 else if (TREE_CODE (fn) != FUNCTION_DECL)
1536 /* We can get IDENTIFIER_NODEs here in certain erroneous
1537 cases. */
1539 else if (!DECL_FUNCTION_MEMBER_P (fn))
1540 /* This is just an ordinary non-member function. Nothing can
1541 be a specialization of that. */
1543 else if (DECL_ARTIFICIAL (fn))
1544 /* Cannot specialize functions that are created implicitly. */
1546 else
1548 tree decl_arg_types;
1550 /* This is an ordinary member function. However, since
1551 we're here, we can assume it's enclosing class is a
1552 template class. For example,
1554 template <typename T> struct S { void f(); };
1555 template <> void S<int>::f() {}
1557 Here, S<int>::f is a non-template, but S<int> is a
1558 template class. If FN has the same type as DECL, we
1559 might be in business. */
1561 if (!DECL_TEMPLATE_INFO (fn))
1562 /* Its enclosing class is an explicit specialization
1563 of a template class. This is not a candidate. */
1564 continue;
1566 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1567 TREE_TYPE (TREE_TYPE (fn))))
1568 /* The return types differ. */
1569 continue;
1571 /* Adjust the type of DECL in case FN is a static member. */
1572 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1573 if (DECL_STATIC_FUNCTION_P (fn)
1574 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1575 decl_arg_types = TREE_CHAIN (decl_arg_types);
1577 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1578 decl_arg_types))
1579 /* They match! */
1580 candidates = tree_cons (NULL_TREE, fn, candidates);
1584 if (templates && TREE_CHAIN (templates))
1586 /* We have:
1588 [temp.expl.spec]
1590 It is possible for a specialization with a given function
1591 signature to be instantiated from more than one function
1592 template. In such cases, explicit specification of the
1593 template arguments must be used to uniquely identify the
1594 function template specialization being specialized.
1596 Note that here, there's no suggestion that we're supposed to
1597 determine which of the candidate templates is most
1598 specialized. However, we, also have:
1600 [temp.func.order]
1602 Partial ordering of overloaded function template
1603 declarations is used in the following contexts to select
1604 the function template to which a function template
1605 specialization refers:
1607 -- when an explicit specialization refers to a function
1608 template.
1610 So, we do use the partial ordering rules, at least for now.
1611 This extension can only serve to make invalid programs valid,
1612 so it's safe. And, there is strong anecdotal evidence that
1613 the committee intended the partial ordering rules to apply;
1614 the EDG front end has that behavior, and John Spicer claims
1615 that the committee simply forgot to delete the wording in
1616 [temp.expl.spec]. */
1617 tree tmpl = most_specialized_instantiation (templates);
1618 if (tmpl != error_mark_node)
1620 templates = tmpl;
1621 TREE_CHAIN (templates) = NULL_TREE;
1625 if (templates == NULL_TREE && candidates == NULL_TREE)
1627 error ("template-id %qD for %q+D does not match any template "
1628 "declaration", template_id, decl);
1629 return error_mark_node;
1631 else if ((templates && TREE_CHAIN (templates))
1632 || (candidates && TREE_CHAIN (candidates))
1633 || (templates && candidates))
1635 error ("ambiguous template specialization %qD for %q+D",
1636 template_id, decl);
1637 chainon (candidates, templates);
1638 print_candidates (candidates);
1639 return error_mark_node;
1642 /* We have one, and exactly one, match. */
1643 if (candidates)
1645 tree fn = TREE_VALUE (candidates);
1646 /* DECL is a re-declaration of a template function. */
1647 if (TREE_CODE (fn) == TEMPLATE_DECL)
1648 return fn;
1649 /* It was a specialization of an ordinary member function in a
1650 template class. */
1651 *targs_out = copy_node (DECL_TI_ARGS (fn));
1652 return DECL_TI_TEMPLATE (fn);
1655 /* It was a specialization of a template. */
1656 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1657 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1659 *targs_out = copy_node (targs);
1660 SET_TMPL_ARGS_LEVEL (*targs_out,
1661 TMPL_ARGS_DEPTH (*targs_out),
1662 TREE_PURPOSE (templates));
1664 else
1665 *targs_out = TREE_PURPOSE (templates);
1666 return TREE_VALUE (templates);
1669 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1670 but with the default argument values filled in from those in the
1671 TMPL_TYPES. */
1673 static tree
1674 copy_default_args_to_explicit_spec_1 (tree spec_types,
1675 tree tmpl_types)
1677 tree new_spec_types;
1679 if (!spec_types)
1680 return NULL_TREE;
1682 if (spec_types == void_list_node)
1683 return void_list_node;
1685 /* Substitute into the rest of the list. */
1686 new_spec_types =
1687 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1688 TREE_CHAIN (tmpl_types));
1690 /* Add the default argument for this parameter. */
1691 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1692 TREE_VALUE (spec_types),
1693 new_spec_types);
1696 /* DECL is an explicit specialization. Replicate default arguments
1697 from the template it specializes. (That way, code like:
1699 template <class T> void f(T = 3);
1700 template <> void f(double);
1701 void g () { f (); }
1703 works, as required.) An alternative approach would be to look up
1704 the correct default arguments at the call-site, but this approach
1705 is consistent with how implicit instantiations are handled. */
1707 static void
1708 copy_default_args_to_explicit_spec (tree decl)
1710 tree tmpl;
1711 tree spec_types;
1712 tree tmpl_types;
1713 tree new_spec_types;
1714 tree old_type;
1715 tree new_type;
1716 tree t;
1717 tree object_type = NULL_TREE;
1718 tree in_charge = NULL_TREE;
1719 tree vtt = NULL_TREE;
1721 /* See if there's anything we need to do. */
1722 tmpl = DECL_TI_TEMPLATE (decl);
1723 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1724 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1725 if (TREE_PURPOSE (t))
1726 break;
1727 if (!t)
1728 return;
1730 old_type = TREE_TYPE (decl);
1731 spec_types = TYPE_ARG_TYPES (old_type);
1733 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1735 /* Remove the this pointer, but remember the object's type for
1736 CV quals. */
1737 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1738 spec_types = TREE_CHAIN (spec_types);
1739 tmpl_types = TREE_CHAIN (tmpl_types);
1741 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1743 /* DECL may contain more parameters than TMPL due to the extra
1744 in-charge parameter in constructors and destructors. */
1745 in_charge = spec_types;
1746 spec_types = TREE_CHAIN (spec_types);
1748 if (DECL_HAS_VTT_PARM_P (decl))
1750 vtt = spec_types;
1751 spec_types = TREE_CHAIN (spec_types);
1755 /* Compute the merged default arguments. */
1756 new_spec_types =
1757 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1759 /* Compute the new FUNCTION_TYPE. */
1760 if (object_type)
1762 if (vtt)
1763 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1764 TREE_VALUE (vtt),
1765 new_spec_types);
1767 if (in_charge)
1768 /* Put the in-charge parameter back. */
1769 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1770 TREE_VALUE (in_charge),
1771 new_spec_types);
1773 new_type = build_method_type_directly (object_type,
1774 TREE_TYPE (old_type),
1775 new_spec_types);
1777 else
1778 new_type = build_function_type (TREE_TYPE (old_type),
1779 new_spec_types);
1780 new_type = cp_build_type_attribute_variant (new_type,
1781 TYPE_ATTRIBUTES (old_type));
1782 new_type = build_exception_variant (new_type,
1783 TYPE_RAISES_EXCEPTIONS (old_type));
1784 TREE_TYPE (decl) = new_type;
1787 /* Check to see if the function just declared, as indicated in
1788 DECLARATOR, and in DECL, is a specialization of a function
1789 template. We may also discover that the declaration is an explicit
1790 instantiation at this point.
1792 Returns DECL, or an equivalent declaration that should be used
1793 instead if all goes well. Issues an error message if something is
1794 amiss. Returns error_mark_node if the error is not easily
1795 recoverable.
1797 FLAGS is a bitmask consisting of the following flags:
1799 2: The function has a definition.
1800 4: The function is a friend.
1802 The TEMPLATE_COUNT is the number of references to qualifying
1803 template classes that appeared in the name of the function. For
1804 example, in
1806 template <class T> struct S { void f(); };
1807 void S<int>::f();
1809 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1810 classes are not counted in the TEMPLATE_COUNT, so that in
1812 template <class T> struct S {};
1813 template <> struct S<int> { void f(); }
1814 template <> void S<int>::f();
1816 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1817 invalid; there should be no template <>.)
1819 If the function is a specialization, it is marked as such via
1820 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1821 is set up correctly, and it is added to the list of specializations
1822 for that template. */
1824 tree
1825 check_explicit_specialization (tree declarator,
1826 tree decl,
1827 int template_count,
1828 int flags)
1830 int have_def = flags & 2;
1831 int is_friend = flags & 4;
1832 int specialization = 0;
1833 int explicit_instantiation = 0;
1834 int member_specialization = 0;
1835 tree ctype = DECL_CLASS_CONTEXT (decl);
1836 tree dname = DECL_NAME (decl);
1837 tmpl_spec_kind tsk;
1839 if (is_friend)
1841 if (!processing_specialization)
1842 tsk = tsk_none;
1843 else
1844 tsk = tsk_excessive_parms;
1846 else
1847 tsk = current_tmpl_spec_kind (template_count);
1849 switch (tsk)
1851 case tsk_none:
1852 if (processing_specialization)
1854 specialization = 1;
1855 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1857 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1859 if (is_friend)
1860 /* This could be something like:
1862 template <class T> void f(T);
1863 class S { friend void f<>(int); } */
1864 specialization = 1;
1865 else
1867 /* This case handles bogus declarations like template <>
1868 template <class T> void f<int>(); */
1870 error ("template-id %qD in declaration of primary template",
1871 declarator);
1872 return decl;
1875 break;
1877 case tsk_invalid_member_spec:
1878 /* The error has already been reported in
1879 check_specialization_scope. */
1880 return error_mark_node;
1882 case tsk_invalid_expl_inst:
1883 error ("template parameter list used in explicit instantiation");
1885 /* Fall through. */
1887 case tsk_expl_inst:
1888 if (have_def)
1889 error ("definition provided for explicit instantiation");
1891 explicit_instantiation = 1;
1892 break;
1894 case tsk_excessive_parms:
1895 case tsk_insufficient_parms:
1896 if (tsk == tsk_excessive_parms)
1897 error ("too many template parameter lists in declaration of %qD",
1898 decl);
1899 else if (template_header_count)
1900 error("too few template parameter lists in declaration of %qD", decl);
1901 else
1902 error("explicit specialization of %qD must be introduced by "
1903 "%<template <>%>", decl);
1905 /* Fall through. */
1906 case tsk_expl_spec:
1907 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1908 if (ctype)
1909 member_specialization = 1;
1910 else
1911 specialization = 1;
1912 break;
1914 case tsk_template:
1915 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1917 /* This case handles bogus declarations like template <>
1918 template <class T> void f<int>(); */
1920 if (uses_template_parms (declarator))
1921 error ("function template partial specialization %qD "
1922 "is not allowed", declarator);
1923 else
1924 error ("template-id %qD in declaration of primary template",
1925 declarator);
1926 return decl;
1929 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1930 /* This is a specialization of a member template, without
1931 specialization the containing class. Something like:
1933 template <class T> struct S {
1934 template <class U> void f (U);
1936 template <> template <class U> void S<int>::f(U) {}
1938 That's a specialization -- but of the entire template. */
1939 specialization = 1;
1940 break;
1942 default:
1943 gcc_unreachable ();
1946 if (specialization || member_specialization)
1948 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1949 for (; t; t = TREE_CHAIN (t))
1950 if (TREE_PURPOSE (t))
1952 pedwarn
1953 ("default argument specified in explicit specialization");
1954 break;
1958 if (specialization || member_specialization || explicit_instantiation)
1960 tree tmpl = NULL_TREE;
1961 tree targs = NULL_TREE;
1963 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1964 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1966 tree fns;
1968 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1969 if (ctype)
1970 fns = dname;
1971 else
1973 /* If there is no class context, the explicit instantiation
1974 must be at namespace scope. */
1975 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1977 /* Find the namespace binding, using the declaration
1978 context. */
1979 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1980 false, true);
1981 if (!fns || !is_overloaded_fn (fns))
1983 error ("%qD is not a template function", dname);
1984 fns = error_mark_node;
1986 else
1988 tree fn = OVL_CURRENT (fns);
1989 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1990 CP_DECL_CONTEXT (fn)))
1991 error ("%qD is not declared in %qD",
1992 decl, current_namespace);
1996 declarator = lookup_template_function (fns, NULL_TREE);
1999 if (declarator == error_mark_node)
2000 return error_mark_node;
2002 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2004 if (!explicit_instantiation)
2005 /* A specialization in class scope. This is invalid,
2006 but the error will already have been flagged by
2007 check_specialization_scope. */
2008 return error_mark_node;
2009 else
2011 /* It's not valid to write an explicit instantiation in
2012 class scope, e.g.:
2014 class C { template void f(); }
2016 This case is caught by the parser. However, on
2017 something like:
2019 template class C { void f(); };
2021 (which is invalid) we can get here. The error will be
2022 issued later. */
2026 return decl;
2028 else if (ctype != NULL_TREE
2029 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2030 IDENTIFIER_NODE))
2032 /* Find the list of functions in ctype that have the same
2033 name as the declared function. */
2034 tree name = TREE_OPERAND (declarator, 0);
2035 tree fns = NULL_TREE;
2036 int idx;
2038 if (constructor_name_p (name, ctype))
2040 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2042 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2043 : !CLASSTYPE_DESTRUCTORS (ctype))
2045 /* From [temp.expl.spec]:
2047 If such an explicit specialization for the member
2048 of a class template names an implicitly-declared
2049 special member function (clause _special_), the
2050 program is ill-formed.
2052 Similar language is found in [temp.explicit]. */
2053 error ("specialization of implicitly-declared special member function");
2054 return error_mark_node;
2057 name = is_constructor ? ctor_identifier : dtor_identifier;
2060 if (!DECL_CONV_FN_P (decl))
2062 idx = lookup_fnfields_1 (ctype, name);
2063 if (idx >= 0)
2064 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2066 else
2068 VEC(tree,gc) *methods;
2069 tree ovl;
2071 /* For a type-conversion operator, we cannot do a
2072 name-based lookup. We might be looking for `operator
2073 int' which will be a specialization of `operator T'.
2074 So, we find *all* the conversion operators, and then
2075 select from them. */
2076 fns = NULL_TREE;
2078 methods = CLASSTYPE_METHOD_VEC (ctype);
2079 if (methods)
2080 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2081 VEC_iterate (tree, methods, idx, ovl);
2082 ++idx)
2084 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2085 /* There are no more conversion functions. */
2086 break;
2088 /* Glue all these conversion functions together
2089 with those we already have. */
2090 for (; ovl; ovl = OVL_NEXT (ovl))
2091 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2095 if (fns == NULL_TREE)
2097 error ("no member function %qD declared in %qT", name, ctype);
2098 return error_mark_node;
2100 else
2101 TREE_OPERAND (declarator, 0) = fns;
2104 /* Figure out what exactly is being specialized at this point.
2105 Note that for an explicit instantiation, even one for a
2106 member function, we cannot tell apriori whether the
2107 instantiation is for a member template, or just a member
2108 function of a template class. Even if a member template is
2109 being instantiated, the member template arguments may be
2110 elided if they can be deduced from the rest of the
2111 declaration. */
2112 tmpl = determine_specialization (declarator, decl,
2113 &targs,
2114 member_specialization,
2115 template_count,
2116 tsk);
2118 if (!tmpl || tmpl == error_mark_node)
2119 /* We couldn't figure out what this declaration was
2120 specializing. */
2121 return error_mark_node;
2122 else
2124 tree gen_tmpl = most_general_template (tmpl);
2126 if (explicit_instantiation)
2128 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2129 is done by do_decl_instantiation later. */
2131 int arg_depth = TMPL_ARGS_DEPTH (targs);
2132 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2134 if (arg_depth > parm_depth)
2136 /* If TMPL is not the most general template (for
2137 example, if TMPL is a friend template that is
2138 injected into namespace scope), then there will
2139 be too many levels of TARGS. Remove some of them
2140 here. */
2141 int i;
2142 tree new_targs;
2144 new_targs = make_tree_vec (parm_depth);
2145 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2146 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2147 = TREE_VEC_ELT (targs, i);
2148 targs = new_targs;
2151 return instantiate_template (tmpl, targs, tf_error);
2154 /* If we thought that the DECL was a member function, but it
2155 turns out to be specializing a static member function,
2156 make DECL a static member function as well. */
2157 if (DECL_STATIC_FUNCTION_P (tmpl)
2158 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2159 revert_static_member_fn (decl);
2161 /* If this is a specialization of a member template of a
2162 template class, we want to return the TEMPLATE_DECL, not
2163 the specialization of it. */
2164 if (tsk == tsk_template)
2166 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2167 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2168 if (have_def)
2170 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2171 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2172 = DECL_SOURCE_LOCATION (decl);
2173 /* We want to use the argument list specified in the
2174 definition, not in the original declaration. */
2175 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2176 = DECL_ARGUMENTS (decl);
2178 return tmpl;
2181 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2182 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2184 /* Inherit default function arguments from the template
2185 DECL is specializing. */
2186 copy_default_args_to_explicit_spec (decl);
2188 /* This specialization has the same protection as the
2189 template it specializes. */
2190 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2191 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2193 /* If DECL is a friend declaration, declared using an
2194 unqualified name, the namespace associated with DECL may
2195 have been set incorrectly. For example, in:
2197 template <typename T> void f(T);
2198 namespace N {
2199 struct S { friend void f<int>(int); }
2202 we will have set the DECL_CONTEXT for the friend
2203 declaration to N, rather than to the global namespace. */
2204 if (DECL_NAMESPACE_SCOPE_P (decl))
2205 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2207 if (is_friend && !have_def)
2208 /* This is not really a declaration of a specialization.
2209 It's just the name of an instantiation. But, it's not
2210 a request for an instantiation, either. */
2211 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2212 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2213 /* This is indeed a specialization. In case of constructors
2214 and destructors, we need in-charge and not-in-charge
2215 versions in V3 ABI. */
2216 clone_function_decl (decl, /*update_method_vec_p=*/0);
2218 /* Register this specialization so that we can find it
2219 again. */
2220 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2224 return decl;
2227 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2228 parameters. These are represented in the same format used for
2229 DECL_TEMPLATE_PARMS. */
2232 comp_template_parms (tree parms1, tree parms2)
2234 tree p1;
2235 tree p2;
2237 if (parms1 == parms2)
2238 return 1;
2240 for (p1 = parms1, p2 = parms2;
2241 p1 != NULL_TREE && p2 != NULL_TREE;
2242 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2244 tree t1 = TREE_VALUE (p1);
2245 tree t2 = TREE_VALUE (p2);
2246 int i;
2248 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2249 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2251 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2252 return 0;
2254 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2256 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2257 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2259 /* If either of the template parameters are invalid, assume
2260 they match for the sake of error recovery. */
2261 if (parm1 == error_mark_node || parm2 == error_mark_node)
2262 return 1;
2264 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2265 return 0;
2267 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2268 continue;
2269 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2270 return 0;
2274 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2275 /* One set of parameters has more parameters lists than the
2276 other. */
2277 return 0;
2279 return 1;
2282 /* Complain if DECL shadows a template parameter.
2284 [temp.local]: A template-parameter shall not be redeclared within its
2285 scope (including nested scopes). */
2287 void
2288 check_template_shadow (tree decl)
2290 tree olddecl;
2292 /* If we're not in a template, we can't possibly shadow a template
2293 parameter. */
2294 if (!current_template_parms)
2295 return;
2297 /* Figure out what we're shadowing. */
2298 if (TREE_CODE (decl) == OVERLOAD)
2299 decl = OVL_CURRENT (decl);
2300 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2302 /* If there's no previous binding for this name, we're not shadowing
2303 anything, let alone a template parameter. */
2304 if (!olddecl)
2305 return;
2307 /* If we're not shadowing a template parameter, we're done. Note
2308 that OLDDECL might be an OVERLOAD (or perhaps even an
2309 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2310 node. */
2311 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2312 return;
2314 /* We check for decl != olddecl to avoid bogus errors for using a
2315 name inside a class. We check TPFI to avoid duplicate errors for
2316 inline member templates. */
2317 if (decl == olddecl
2318 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2319 return;
2321 error ("declaration of %q+#D", decl);
2322 error (" shadows template parm %q+#D", olddecl);
2325 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2326 ORIG_LEVEL, DECL, and TYPE. */
2328 static tree
2329 build_template_parm_index (int index,
2330 int level,
2331 int orig_level,
2332 tree decl,
2333 tree type)
2335 tree t = make_node (TEMPLATE_PARM_INDEX);
2336 TEMPLATE_PARM_IDX (t) = index;
2337 TEMPLATE_PARM_LEVEL (t) = level;
2338 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2339 TEMPLATE_PARM_DECL (t) = decl;
2340 TREE_TYPE (t) = type;
2341 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2342 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2343 TREE_READONLY (t) = TREE_READONLY (decl);
2345 return t;
2348 /* Find the canonical type parameter for the given template type
2349 parameter. Returns the canonical type parameter, which may be TYPE
2350 if no such parameter existed. */
2351 static tree
2352 canonical_type_parameter (tree type)
2354 tree list;
2355 int idx = TEMPLATE_TYPE_IDX (type);
2356 if (!canonical_template_parms)
2357 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2359 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2360 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2362 list = VEC_index (tree, canonical_template_parms, idx);
2363 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2364 list = TREE_CHAIN (list);
2366 if (list)
2367 return TREE_VALUE (list);
2368 else
2370 VEC_replace(tree, canonical_template_parms, idx,
2371 tree_cons (NULL_TREE, type,
2372 VEC_index (tree, canonical_template_parms, idx)));
2373 return type;
2377 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2378 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2379 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2380 new one is created. */
2382 static tree
2383 reduce_template_parm_level (tree index, tree type, int levels)
2385 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2386 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2387 != TEMPLATE_PARM_LEVEL (index) - levels))
2389 tree orig_decl = TEMPLATE_PARM_DECL (index);
2390 tree decl, t;
2392 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2393 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2394 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2395 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2396 DECL_ARTIFICIAL (decl) = 1;
2397 SET_DECL_TEMPLATE_PARM_P (decl);
2399 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2400 TEMPLATE_PARM_LEVEL (index) - levels,
2401 TEMPLATE_PARM_ORIG_LEVEL (index),
2402 decl, type);
2403 TEMPLATE_PARM_DESCENDANTS (index) = t;
2405 /* Template template parameters need this. */
2406 if (TREE_CODE (decl) != CONST_DECL)
2407 DECL_TEMPLATE_PARMS (decl)
2408 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2411 return TEMPLATE_PARM_DESCENDANTS (index);
2414 /* Process information from new template parameter PARM and append it to the
2415 LIST being built. This new parameter is a non-type parameter iff
2416 IS_NON_TYPE is true. */
2418 tree
2419 process_template_parm (tree list, tree parm, bool is_non_type)
2421 tree decl = 0;
2422 tree defval;
2423 tree err_parm_list;
2424 int idx = 0;
2426 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2427 defval = TREE_PURPOSE (parm);
2429 if (list)
2431 tree p = tree_last (list);
2433 if (p && TREE_VALUE (p) != error_mark_node)
2435 p = TREE_VALUE (p);
2436 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2437 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2438 else
2439 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2442 ++idx;
2444 else
2445 idx = 0;
2447 if (is_non_type)
2449 parm = TREE_VALUE (parm);
2451 SET_DECL_TEMPLATE_PARM_P (parm);
2453 if (TREE_TYPE (parm) == error_mark_node)
2455 err_parm_list = build_tree_list (defval, parm);
2456 TREE_VALUE (err_parm_list) = error_mark_node;
2457 return chainon (list, err_parm_list);
2459 else
2461 /* [temp.param]
2463 The top-level cv-qualifiers on the template-parameter are
2464 ignored when determining its type. */
2465 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2466 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2468 err_parm_list = build_tree_list (defval, parm);
2469 TREE_VALUE (err_parm_list) = error_mark_node;
2470 return chainon (list, err_parm_list);
2474 /* A template parameter is not modifiable. */
2475 TREE_CONSTANT (parm) = 1;
2476 TREE_INVARIANT (parm) = 1;
2477 TREE_READONLY (parm) = 1;
2478 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2479 TREE_CONSTANT (decl) = 1;
2480 TREE_INVARIANT (decl) = 1;
2481 TREE_READONLY (decl) = 1;
2482 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2483 = build_template_parm_index (idx, processing_template_decl,
2484 processing_template_decl,
2485 decl, TREE_TYPE (parm));
2487 else
2489 tree t;
2490 parm = TREE_VALUE (TREE_VALUE (parm));
2492 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2494 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2495 /* This is for distinguishing between real templates and template
2496 template parameters */
2497 TREE_TYPE (parm) = t;
2498 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2499 decl = parm;
2501 else
2503 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2504 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2505 decl = build_decl (TYPE_DECL, parm, t);
2508 TYPE_NAME (t) = decl;
2509 TYPE_STUB_DECL (t) = decl;
2510 parm = decl;
2511 TEMPLATE_TYPE_PARM_INDEX (t)
2512 = build_template_parm_index (idx, processing_template_decl,
2513 processing_template_decl,
2514 decl, TREE_TYPE (parm));
2515 TYPE_CANONICAL (t) = canonical_type_parameter (t);
2517 DECL_ARTIFICIAL (decl) = 1;
2518 SET_DECL_TEMPLATE_PARM_P (decl);
2519 pushdecl (decl);
2520 parm = build_tree_list (defval, parm);
2521 return chainon (list, parm);
2524 /* The end of a template parameter list has been reached. Process the
2525 tree list into a parameter vector, converting each parameter into a more
2526 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2527 as PARM_DECLs. */
2529 tree
2530 end_template_parm_list (tree parms)
2532 int nparms;
2533 tree parm, next;
2534 tree saved_parmlist = make_tree_vec (list_length (parms));
2536 current_template_parms
2537 = tree_cons (size_int (processing_template_decl),
2538 saved_parmlist, current_template_parms);
2540 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2542 next = TREE_CHAIN (parm);
2543 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2544 TREE_CHAIN (parm) = NULL_TREE;
2547 --processing_template_parmlist;
2549 return saved_parmlist;
2552 /* end_template_decl is called after a template declaration is seen. */
2554 void
2555 end_template_decl (void)
2557 reset_specialization ();
2559 if (! processing_template_decl)
2560 return;
2562 /* This matches the pushlevel in begin_template_parm_list. */
2563 finish_scope ();
2565 --processing_template_decl;
2566 current_template_parms = TREE_CHAIN (current_template_parms);
2569 /* Given a template argument vector containing the template PARMS.
2570 The innermost PARMS are given first. */
2572 static tree
2573 current_template_args (void)
2575 tree header;
2576 tree args = NULL_TREE;
2577 int length = TMPL_PARMS_DEPTH (current_template_parms);
2578 int l = length;
2580 /* If there is only one level of template parameters, we do not
2581 create a TREE_VEC of TREE_VECs. Instead, we return a single
2582 TREE_VEC containing the arguments. */
2583 if (length > 1)
2584 args = make_tree_vec (length);
2586 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2588 tree a = copy_node (TREE_VALUE (header));
2589 int i;
2591 TREE_TYPE (a) = NULL_TREE;
2592 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2594 tree t = TREE_VEC_ELT (a, i);
2596 /* T will be a list if we are called from within a
2597 begin/end_template_parm_list pair, but a vector directly
2598 if within a begin/end_member_template_processing pair. */
2599 if (TREE_CODE (t) == TREE_LIST)
2601 t = TREE_VALUE (t);
2603 if (t != error_mark_node)
2605 if (TREE_CODE (t) == TYPE_DECL
2606 || TREE_CODE (t) == TEMPLATE_DECL)
2607 t = TREE_TYPE (t);
2608 else
2609 t = DECL_INITIAL (t);
2612 TREE_VEC_ELT (a, i) = t;
2616 if (length > 1)
2617 TREE_VEC_ELT (args, --l) = a;
2618 else
2619 args = a;
2622 return args;
2625 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2626 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2627 a member template. Used by push_template_decl below. */
2629 static tree
2630 build_template_decl (tree decl, tree parms, bool member_template_p)
2632 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2633 DECL_TEMPLATE_PARMS (tmpl) = parms;
2634 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2635 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2636 if (DECL_LANG_SPECIFIC (decl))
2638 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2639 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2640 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2641 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2642 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2643 if (DECL_OVERLOADED_OPERATOR_P (decl))
2644 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2645 DECL_OVERLOADED_OPERATOR_P (decl));
2648 return tmpl;
2651 struct template_parm_data
2653 /* The level of the template parameters we are currently
2654 processing. */
2655 int level;
2657 /* The index of the specialization argument we are currently
2658 processing. */
2659 int current_arg;
2661 /* An array whose size is the number of template parameters. The
2662 elements are nonzero if the parameter has been used in any one
2663 of the arguments processed so far. */
2664 int* parms;
2666 /* An array whose size is the number of template arguments. The
2667 elements are nonzero if the argument makes use of template
2668 parameters of this level. */
2669 int* arg_uses_template_parms;
2672 /* Subroutine of push_template_decl used to see if each template
2673 parameter in a partial specialization is used in the explicit
2674 argument list. If T is of the LEVEL given in DATA (which is
2675 treated as a template_parm_data*), then DATA->PARMS is marked
2676 appropriately. */
2678 static int
2679 mark_template_parm (tree t, void* data)
2681 int level;
2682 int idx;
2683 struct template_parm_data* tpd = (struct template_parm_data*) data;
2685 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2687 level = TEMPLATE_PARM_LEVEL (t);
2688 idx = TEMPLATE_PARM_IDX (t);
2690 else
2692 level = TEMPLATE_TYPE_LEVEL (t);
2693 idx = TEMPLATE_TYPE_IDX (t);
2696 if (level == tpd->level)
2698 tpd->parms[idx] = 1;
2699 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2702 /* Return zero so that for_each_template_parm will continue the
2703 traversal of the tree; we want to mark *every* template parm. */
2704 return 0;
2707 /* Process the partial specialization DECL. */
2709 static tree
2710 process_partial_specialization (tree decl)
2712 tree type = TREE_TYPE (decl);
2713 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2714 tree specargs = CLASSTYPE_TI_ARGS (type);
2715 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2716 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2717 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2718 int nargs = TREE_VEC_LENGTH (inner_args);
2719 int ntparms = TREE_VEC_LENGTH (inner_parms);
2720 int i;
2721 int did_error_intro = 0;
2722 struct template_parm_data tpd;
2723 struct template_parm_data tpd2;
2725 /* We check that each of the template parameters given in the
2726 partial specialization is used in the argument list to the
2727 specialization. For example:
2729 template <class T> struct S;
2730 template <class T> struct S<T*>;
2732 The second declaration is OK because `T*' uses the template
2733 parameter T, whereas
2735 template <class T> struct S<int>;
2737 is no good. Even trickier is:
2739 template <class T>
2740 struct S1
2742 template <class U>
2743 struct S2;
2744 template <class U>
2745 struct S2<T>;
2748 The S2<T> declaration is actually invalid; it is a
2749 full-specialization. Of course,
2751 template <class U>
2752 struct S2<T (*)(U)>;
2754 or some such would have been OK. */
2755 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2756 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2757 memset (tpd.parms, 0, sizeof (int) * ntparms);
2759 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2760 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2761 for (i = 0; i < nargs; ++i)
2763 tpd.current_arg = i;
2764 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2765 &mark_template_parm,
2766 &tpd,
2767 NULL);
2769 for (i = 0; i < ntparms; ++i)
2770 if (tpd.parms[i] == 0)
2772 /* One of the template parms was not used in the
2773 specialization. */
2774 if (!did_error_intro)
2776 error ("template parameters not used in partial specialization:");
2777 did_error_intro = 1;
2780 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2783 /* [temp.class.spec]
2785 The argument list of the specialization shall not be identical to
2786 the implicit argument list of the primary template. */
2787 if (comp_template_args
2788 (inner_args,
2789 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2790 (maintmpl)))))
2791 error ("partial specialization %qT does not specialize any template arguments", type);
2793 /* [temp.class.spec]
2795 A partially specialized non-type argument expression shall not
2796 involve template parameters of the partial specialization except
2797 when the argument expression is a simple identifier.
2799 The type of a template parameter corresponding to a specialized
2800 non-type argument shall not be dependent on a parameter of the
2801 specialization. */
2802 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2803 tpd2.parms = 0;
2804 for (i = 0; i < nargs; ++i)
2806 tree arg = TREE_VEC_ELT (inner_args, i);
2807 if (/* These first two lines are the `non-type' bit. */
2808 !TYPE_P (arg)
2809 && TREE_CODE (arg) != TEMPLATE_DECL
2810 /* This next line is the `argument expression is not just a
2811 simple identifier' condition and also the `specialized
2812 non-type argument' bit. */
2813 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2815 if (tpd.arg_uses_template_parms[i])
2816 error ("template argument %qE involves template parameter(s)", arg);
2817 else
2819 /* Look at the corresponding template parameter,
2820 marking which template parameters its type depends
2821 upon. */
2822 tree type =
2823 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2824 i)));
2826 if (!tpd2.parms)
2828 /* We haven't yet initialized TPD2. Do so now. */
2829 tpd2.arg_uses_template_parms
2830 = (int *) alloca (sizeof (int) * nargs);
2831 /* The number of parameters here is the number in the
2832 main template, which, as checked in the assertion
2833 above, is NARGS. */
2834 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2835 tpd2.level =
2836 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2839 /* Mark the template parameters. But this time, we're
2840 looking for the template parameters of the main
2841 template, not in the specialization. */
2842 tpd2.current_arg = i;
2843 tpd2.arg_uses_template_parms[i] = 0;
2844 memset (tpd2.parms, 0, sizeof (int) * nargs);
2845 for_each_template_parm (type,
2846 &mark_template_parm,
2847 &tpd2,
2848 NULL);
2850 if (tpd2.arg_uses_template_parms [i])
2852 /* The type depended on some template parameters.
2853 If they are fully specialized in the
2854 specialization, that's OK. */
2855 int j;
2856 for (j = 0; j < nargs; ++j)
2857 if (tpd2.parms[j] != 0
2858 && tpd.arg_uses_template_parms [j])
2860 error ("type %qT of template argument %qE depends "
2861 "on template parameter(s)",
2862 type,
2863 arg);
2864 break;
2871 if (retrieve_specialization (maintmpl, specargs,
2872 /*class_specializations_p=*/true))
2873 /* We've already got this specialization. */
2874 return decl;
2876 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2877 = tree_cons (specargs, inner_parms,
2878 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2879 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2880 return decl;
2883 /* Check that a template declaration's use of default arguments is not
2884 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2885 nonzero if DECL is the thing declared by a primary template.
2886 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2888 static void
2889 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2891 const char *msg;
2892 int last_level_to_check;
2893 tree parm_level;
2895 /* [temp.param]
2897 A default template-argument shall not be specified in a
2898 function template declaration or a function template definition, nor
2899 in the template-parameter-list of the definition of a member of a
2900 class template. */
2902 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2903 /* You can't have a function template declaration in a local
2904 scope, nor you can you define a member of a class template in a
2905 local scope. */
2906 return;
2908 if (current_class_type
2909 && !TYPE_BEING_DEFINED (current_class_type)
2910 && DECL_LANG_SPECIFIC (decl)
2911 /* If this is either a friend defined in the scope of the class
2912 or a member function. */
2913 && (DECL_FUNCTION_MEMBER_P (decl)
2914 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2915 : DECL_FRIEND_CONTEXT (decl)
2916 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2917 : false)
2918 /* And, if it was a member function, it really was defined in
2919 the scope of the class. */
2920 && (!DECL_FUNCTION_MEMBER_P (decl)
2921 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2922 /* We already checked these parameters when the template was
2923 declared, so there's no need to do it again now. This function
2924 was defined in class scope, but we're processing it's body now
2925 that the class is complete. */
2926 return;
2928 /* [temp.param]
2930 If a template-parameter has a default template-argument, all
2931 subsequent template-parameters shall have a default
2932 template-argument supplied. */
2933 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2935 tree inner_parms = TREE_VALUE (parm_level);
2936 int ntparms = TREE_VEC_LENGTH (inner_parms);
2937 int seen_def_arg_p = 0;
2938 int i;
2940 for (i = 0; i < ntparms; ++i)
2942 tree parm = TREE_VEC_ELT (inner_parms, i);
2944 if (parm == error_mark_node)
2945 continue;
2947 if (TREE_PURPOSE (parm))
2948 seen_def_arg_p = 1;
2949 else if (seen_def_arg_p)
2951 error ("no default argument for %qD", TREE_VALUE (parm));
2952 /* For better subsequent error-recovery, we indicate that
2953 there should have been a default argument. */
2954 TREE_PURPOSE (parm) = error_mark_node;
2959 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2960 /* For an ordinary class template, default template arguments are
2961 allowed at the innermost level, e.g.:
2962 template <class T = int>
2963 struct S {};
2964 but, in a partial specialization, they're not allowed even
2965 there, as we have in [temp.class.spec]:
2967 The template parameter list of a specialization shall not
2968 contain default template argument values.
2970 So, for a partial specialization, or for a function template,
2971 we look at all of them. */
2973 else
2974 /* But, for a primary class template that is not a partial
2975 specialization we look at all template parameters except the
2976 innermost ones. */
2977 parms = TREE_CHAIN (parms);
2979 /* Figure out what error message to issue. */
2980 if (TREE_CODE (decl) == FUNCTION_DECL)
2981 msg = "default template arguments may not be used in function templates";
2982 else if (is_partial)
2983 msg = "default template arguments may not be used in partial specializations";
2984 else
2985 msg = "default argument for template parameter for class enclosing %qD";
2987 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2988 /* If we're inside a class definition, there's no need to
2989 examine the parameters to the class itself. On the one
2990 hand, they will be checked when the class is defined, and,
2991 on the other, default arguments are valid in things like:
2992 template <class T = double>
2993 struct S { template <class U> void f(U); };
2994 Here the default argument for `S' has no bearing on the
2995 declaration of `f'. */
2996 last_level_to_check = template_class_depth (current_class_type) + 1;
2997 else
2998 /* Check everything. */
2999 last_level_to_check = 0;
3001 for (parm_level = parms;
3002 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3003 parm_level = TREE_CHAIN (parm_level))
3005 tree inner_parms = TREE_VALUE (parm_level);
3006 int i;
3007 int ntparms;
3009 ntparms = TREE_VEC_LENGTH (inner_parms);
3010 for (i = 0; i < ntparms; ++i)
3012 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3013 continue;
3015 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3017 if (msg)
3019 error (msg, decl);
3020 msg = 0;
3023 /* Clear out the default argument so that we are not
3024 confused later. */
3025 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3029 /* At this point, if we're still interested in issuing messages,
3030 they must apply to classes surrounding the object declared. */
3031 if (msg)
3032 msg = "default argument for template parameter for class enclosing %qD";
3036 /* Worker for push_template_decl_real, called via
3037 for_each_template_parm. DATA is really an int, indicating the
3038 level of the parameters we are interested in. If T is a template
3039 parameter of that level, return nonzero. */
3041 static int
3042 template_parm_this_level_p (tree t, void* data)
3044 int this_level = *(int *)data;
3045 int level;
3047 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3048 level = TEMPLATE_PARM_LEVEL (t);
3049 else
3050 level = TEMPLATE_TYPE_LEVEL (t);
3051 return level == this_level;
3054 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3055 parameters given by current_template_args, or reuses a
3056 previously existing one, if appropriate. Returns the DECL, or an
3057 equivalent one, if it is replaced via a call to duplicate_decls.
3059 If IS_FRIEND is true, DECL is a friend declaration. */
3061 tree
3062 push_template_decl_real (tree decl, bool is_friend)
3064 tree tmpl;
3065 tree args;
3066 tree info;
3067 tree ctx;
3068 int primary;
3069 int is_partial;
3070 int new_template_p = 0;
3071 /* True if the template is a member template, in the sense of
3072 [temp.mem]. */
3073 bool member_template_p = false;
3075 if (decl == error_mark_node)
3076 return decl;
3078 /* See if this is a partial specialization. */
3079 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3080 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3081 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3083 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3084 is_friend = true;
3086 if (is_friend)
3087 /* For a friend, we want the context of the friend function, not
3088 the type of which it is a friend. */
3089 ctx = DECL_CONTEXT (decl);
3090 else if (CP_DECL_CONTEXT (decl)
3091 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3092 /* In the case of a virtual function, we want the class in which
3093 it is defined. */
3094 ctx = CP_DECL_CONTEXT (decl);
3095 else
3096 /* Otherwise, if we're currently defining some class, the DECL
3097 is assumed to be a member of the class. */
3098 ctx = current_scope ();
3100 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3101 ctx = NULL_TREE;
3103 if (!DECL_CONTEXT (decl))
3104 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3106 /* See if this is a primary template. */
3107 if (is_friend && ctx)
3108 /* A friend template that specifies a class context, i.e.
3109 template <typename T> friend void A<T>::f();
3110 is not primary. */
3111 primary = 0;
3112 else
3113 primary = template_parm_scope_p ();
3115 if (primary)
3117 if (DECL_CLASS_SCOPE_P (decl))
3118 member_template_p = true;
3119 if (TREE_CODE (decl) == TYPE_DECL
3120 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3121 error ("template class without a name");
3122 else if (TREE_CODE (decl) == FUNCTION_DECL)
3124 if (DECL_DESTRUCTOR_P (decl))
3126 /* [temp.mem]
3128 A destructor shall not be a member template. */
3129 error ("destructor %qD declared as member template", decl);
3130 return error_mark_node;
3132 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3133 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3134 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3135 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3136 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3137 == void_list_node)))
3139 /* [basic.stc.dynamic.allocation]
3141 An allocation function can be a function
3142 template. ... Template allocation functions shall
3143 have two or more parameters. */
3144 error ("invalid template declaration of %qD", decl);
3145 return error_mark_node;
3148 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3149 && CLASS_TYPE_P (TREE_TYPE (decl)))
3150 /* OK */;
3151 else
3153 error ("template declaration of %q#D", decl);
3154 return error_mark_node;
3158 /* Check to see that the rules regarding the use of default
3159 arguments are not being violated. */
3160 check_default_tmpl_args (decl, current_template_parms,
3161 primary, is_partial);
3163 if (is_partial)
3164 return process_partial_specialization (decl);
3166 args = current_template_args ();
3168 if (!ctx
3169 || TREE_CODE (ctx) == FUNCTION_DECL
3170 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3171 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3173 if (DECL_LANG_SPECIFIC (decl)
3174 && DECL_TEMPLATE_INFO (decl)
3175 && DECL_TI_TEMPLATE (decl))
3176 tmpl = DECL_TI_TEMPLATE (decl);
3177 /* If DECL is a TYPE_DECL for a class-template, then there won't
3178 be DECL_LANG_SPECIFIC. The information equivalent to
3179 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3180 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3181 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3182 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3184 /* Since a template declaration already existed for this
3185 class-type, we must be redeclaring it here. Make sure
3186 that the redeclaration is valid. */
3187 redeclare_class_template (TREE_TYPE (decl),
3188 current_template_parms);
3189 /* We don't need to create a new TEMPLATE_DECL; just use the
3190 one we already had. */
3191 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3193 else
3195 tmpl = build_template_decl (decl, current_template_parms,
3196 member_template_p);
3197 new_template_p = 1;
3199 if (DECL_LANG_SPECIFIC (decl)
3200 && DECL_TEMPLATE_SPECIALIZATION (decl))
3202 /* A specialization of a member template of a template
3203 class. */
3204 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3205 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3206 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3210 else
3212 tree a, t, current, parms;
3213 int i;
3215 if (TREE_CODE (decl) == TYPE_DECL)
3217 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3218 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3219 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3220 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3221 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3222 else
3224 error ("%qD does not declare a template type", decl);
3225 return decl;
3228 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3230 error ("template definition of non-template %q#D", decl);
3231 return decl;
3233 else
3234 tmpl = DECL_TI_TEMPLATE (decl);
3236 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3237 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3238 && DECL_TEMPLATE_SPECIALIZATION (decl)
3239 && DECL_MEMBER_TEMPLATE_P (tmpl))
3241 tree new_tmpl;
3243 /* The declaration is a specialization of a member
3244 template, declared outside the class. Therefore, the
3245 innermost template arguments will be NULL, so we
3246 replace them with the arguments determined by the
3247 earlier call to check_explicit_specialization. */
3248 args = DECL_TI_ARGS (decl);
3250 new_tmpl
3251 = build_template_decl (decl, current_template_parms,
3252 member_template_p);
3253 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3254 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3255 DECL_TI_TEMPLATE (decl) = new_tmpl;
3256 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3257 DECL_TEMPLATE_INFO (new_tmpl)
3258 = tree_cons (tmpl, args, NULL_TREE);
3260 register_specialization (new_tmpl,
3261 most_general_template (tmpl),
3262 args,
3263 is_friend);
3264 return decl;
3267 /* Make sure the template headers we got make sense. */
3269 parms = DECL_TEMPLATE_PARMS (tmpl);
3270 i = TMPL_PARMS_DEPTH (parms);
3271 if (TMPL_ARGS_DEPTH (args) != i)
3273 error ("expected %d levels of template parms for %q#D, got %d",
3274 i, decl, TMPL_ARGS_DEPTH (args));
3276 else
3277 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3279 a = TMPL_ARGS_LEVEL (args, i);
3280 t = INNERMOST_TEMPLATE_PARMS (parms);
3282 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3284 if (current == decl)
3285 error ("got %d template parameters for %q#D",
3286 TREE_VEC_LENGTH (a), decl);
3287 else
3288 error ("got %d template parameters for %q#T",
3289 TREE_VEC_LENGTH (a), current);
3290 error (" but %d required", TREE_VEC_LENGTH (t));
3291 return error_mark_node;
3294 /* Perhaps we should also check that the parms are used in the
3295 appropriate qualifying scopes in the declarator? */
3297 if (current == decl)
3298 current = ctx;
3299 else
3300 current = TYPE_CONTEXT (current);
3304 DECL_TEMPLATE_RESULT (tmpl) = decl;
3305 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3307 /* Push template declarations for global functions and types. Note
3308 that we do not try to push a global template friend declared in a
3309 template class; such a thing may well depend on the template
3310 parameters of the class. */
3311 if (new_template_p && !ctx
3312 && !(is_friend && template_class_depth (current_class_type) > 0))
3314 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3315 if (tmpl == error_mark_node)
3316 return error_mark_node;
3318 /* Hide template friend classes that haven't been declared yet. */
3319 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3321 DECL_ANTICIPATED (tmpl) = 1;
3322 DECL_FRIEND_P (tmpl) = 1;
3326 if (primary)
3328 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3329 if (DECL_CONV_FN_P (tmpl))
3331 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3333 /* It is a conversion operator. See if the type converted to
3334 depends on innermost template operands. */
3336 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3337 depth))
3338 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3342 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3343 back to its most general template. If TMPL is a specialization,
3344 ARGS may only have the innermost set of arguments. Add the missing
3345 argument levels if necessary. */
3346 if (DECL_TEMPLATE_INFO (tmpl))
3347 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3349 info = tree_cons (tmpl, args, NULL_TREE);
3351 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3353 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3354 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3355 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3356 /* Don't change the name if we've already set it up. */
3357 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3358 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3360 else if (DECL_LANG_SPECIFIC (decl))
3361 DECL_TEMPLATE_INFO (decl) = info;
3363 return DECL_TEMPLATE_RESULT (tmpl);
3366 tree
3367 push_template_decl (tree decl)
3369 return push_template_decl_real (decl, false);
3372 /* Called when a class template TYPE is redeclared with the indicated
3373 template PARMS, e.g.:
3375 template <class T> struct S;
3376 template <class T> struct S {}; */
3378 bool
3379 redeclare_class_template (tree type, tree parms)
3381 tree tmpl;
3382 tree tmpl_parms;
3383 int i;
3385 if (!TYPE_TEMPLATE_INFO (type))
3387 error ("%qT is not a template type", type);
3388 return false;
3391 tmpl = TYPE_TI_TEMPLATE (type);
3392 if (!PRIMARY_TEMPLATE_P (tmpl))
3393 /* The type is nested in some template class. Nothing to worry
3394 about here; there are no new template parameters for the nested
3395 type. */
3396 return true;
3398 if (!parms)
3400 error ("template specifiers not specified in declaration of %qD",
3401 tmpl);
3402 return false;
3405 parms = INNERMOST_TEMPLATE_PARMS (parms);
3406 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3408 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3410 error ("previous declaration %q+D", tmpl);
3411 error ("used %d template parameter(s) instead of %d",
3412 TREE_VEC_LENGTH (tmpl_parms),
3413 TREE_VEC_LENGTH (parms));
3414 return false;
3417 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3419 tree tmpl_parm;
3420 tree parm;
3421 tree tmpl_default;
3422 tree parm_default;
3424 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3425 || TREE_VEC_ELT (parms, i) == error_mark_node)
3426 continue;
3428 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3429 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3430 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3431 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3433 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3434 TEMPLATE_DECL. */
3435 if (tmpl_parm != error_mark_node
3436 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3437 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3438 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
3440 error ("template parameter %q+#D", tmpl_parm);
3441 error ("redeclared here as %q#D", parm);
3442 return false;
3445 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3447 /* We have in [temp.param]:
3449 A template-parameter may not be given default arguments
3450 by two different declarations in the same scope. */
3451 error ("redefinition of default argument for %q#D", parm);
3452 error ("%J original definition appeared here", tmpl_parm);
3453 return false;
3456 if (parm_default != NULL_TREE)
3457 /* Update the previous template parameters (which are the ones
3458 that will really count) with the new default value. */
3459 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3460 else if (tmpl_default != NULL_TREE)
3461 /* Update the new parameters, too; they'll be used as the
3462 parameters for any members. */
3463 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3466 return true;
3469 /* Simplify EXPR if it is a non-dependent expression. Returns the
3470 (possibly simplified) expression. */
3472 tree
3473 fold_non_dependent_expr (tree expr)
3475 if (expr == NULL_TREE)
3476 return NULL_TREE;
3478 /* If we're in a template, but EXPR isn't value dependent, simplify
3479 it. We're supposed to treat:
3481 template <typename T> void f(T[1 + 1]);
3482 template <typename T> void f(T[2]);
3484 as two declarations of the same function, for example. */
3485 if (processing_template_decl
3486 && !type_dependent_expression_p (expr)
3487 && !value_dependent_expression_p (expr))
3489 HOST_WIDE_INT saved_processing_template_decl;
3491 saved_processing_template_decl = processing_template_decl;
3492 processing_template_decl = 0;
3493 expr = tsubst_copy_and_build (expr,
3494 /*args=*/NULL_TREE,
3495 tf_error,
3496 /*in_decl=*/NULL_TREE,
3497 /*function_p=*/false,
3498 /*integral_constant_expression_p=*/true);
3499 processing_template_decl = saved_processing_template_decl;
3501 return expr;
3504 /* EXPR is an expression which is used in a constant-expression context.
3505 For instance, it could be a VAR_DECL with a constant initializer.
3506 Extract the innest constant expression.
3508 This is basically a more powerful version of
3509 integral_constant_value, which can be used also in templates where
3510 initializers can maintain a syntactic rather than semantic form
3511 (even if they are non-dependent, for access-checking purposes). */
3513 static tree
3514 fold_decl_constant_value (tree expr)
3516 tree const_expr = expr;
3519 expr = fold_non_dependent_expr (const_expr);
3520 const_expr = integral_constant_value (expr);
3522 while (expr != const_expr);
3524 return expr;
3527 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3528 must be a function or a pointer-to-function type, as specified
3529 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3530 and check that the resulting function has external linkage. */
3532 static tree
3533 convert_nontype_argument_function (tree type, tree expr)
3535 tree fns = expr;
3536 tree fn, fn_no_ptr;
3538 fn = instantiate_type (type, fns, tf_none);
3539 if (fn == error_mark_node)
3540 return error_mark_node;
3542 fn_no_ptr = fn;
3543 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3544 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3545 if (TREE_CODE (fn_no_ptr) == BASELINK)
3546 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3548 /* [temp.arg.nontype]/1
3550 A template-argument for a non-type, non-template template-parameter
3551 shall be one of:
3552 [...]
3553 -- the address of an object or function with external linkage. */
3554 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3556 error ("%qE is not a valid template argument for type %qT "
3557 "because function %qD has not external linkage",
3558 expr, type, fn_no_ptr);
3559 return NULL_TREE;
3562 return fn;
3565 /* Attempt to convert the non-type template parameter EXPR to the
3566 indicated TYPE. If the conversion is successful, return the
3567 converted value. If the conversion is unsuccessful, return
3568 NULL_TREE if we issued an error message, or error_mark_node if we
3569 did not. We issue error messages for out-and-out bad template
3570 parameters, but not simply because the conversion failed, since we
3571 might be just trying to do argument deduction. Both TYPE and EXPR
3572 must be non-dependent.
3574 The conversion follows the special rules described in
3575 [temp.arg.nontype], and it is much more strict than an implicit
3576 conversion.
3578 This function is called twice for each template argument (see
3579 lookup_template_class for a more accurate description of this
3580 problem). This means that we need to handle expressions which
3581 are not valid in a C++ source, but can be created from the
3582 first call (for instance, casts to perform conversions). These
3583 hacks can go away after we fix the double coercion problem. */
3585 static tree
3586 convert_nontype_argument (tree type, tree expr)
3588 tree expr_type;
3590 /* Detect immediately string literals as invalid non-type argument.
3591 This special-case is not needed for correctness (we would easily
3592 catch this later), but only to provide better diagnostic for this
3593 common user mistake. As suggested by DR 100, we do not mention
3594 linkage issues in the diagnostic as this is not the point. */
3595 if (TREE_CODE (expr) == STRING_CST)
3597 error ("%qE is not a valid template argument for type %qT "
3598 "because string literals can never be used in this context",
3599 expr, type);
3600 return NULL_TREE;
3603 /* If we are in a template, EXPR may be non-dependent, but still
3604 have a syntactic, rather than semantic, form. For example, EXPR
3605 might be a SCOPE_REF, rather than the VAR_DECL to which the
3606 SCOPE_REF refers. Preserving the qualifying scope is necessary
3607 so that access checking can be performed when the template is
3608 instantiated -- but here we need the resolved form so that we can
3609 convert the argument. */
3610 expr = fold_non_dependent_expr (expr);
3611 if (error_operand_p (expr))
3612 return error_mark_node;
3613 expr_type = TREE_TYPE (expr);
3615 /* HACK: Due to double coercion, we can get a
3616 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3617 which is the tree that we built on the first call (see
3618 below when coercing to reference to object or to reference to
3619 function). We just strip everything and get to the arg.
3620 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3621 for examples. */
3622 if (TREE_CODE (expr) == NOP_EXPR)
3624 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3626 /* ??? Maybe we could use convert_from_reference here, but we
3627 would need to relax its constraints because the NOP_EXPR
3628 could actually change the type to something more cv-qualified,
3629 and this is not folded by convert_from_reference. */
3630 tree addr = TREE_OPERAND (expr, 0);
3631 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3632 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3633 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3634 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3635 (TREE_TYPE (expr_type),
3636 TREE_TYPE (TREE_TYPE (addr))));
3638 expr = TREE_OPERAND (addr, 0);
3639 expr_type = TREE_TYPE (expr);
3642 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3643 parameter is a pointer to object, through decay and
3644 qualification conversion. Let's strip everything. */
3645 else if (TYPE_PTROBV_P (type))
3647 STRIP_NOPS (expr);
3648 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3649 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3650 /* Skip the ADDR_EXPR only if it is part of the decay for
3651 an array. Otherwise, it is part of the original argument
3652 in the source code. */
3653 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3654 expr = TREE_OPERAND (expr, 0);
3655 expr_type = TREE_TYPE (expr);
3659 /* [temp.arg.nontype]/5, bullet 1
3661 For a non-type template-parameter of integral or enumeration type,
3662 integral promotions (_conv.prom_) and integral conversions
3663 (_conv.integral_) are applied. */
3664 if (INTEGRAL_TYPE_P (type))
3666 if (!INTEGRAL_TYPE_P (expr_type))
3667 return error_mark_node;
3669 expr = fold_decl_constant_value (expr);
3670 /* Notice that there are constant expressions like '4 % 0' which
3671 do not fold into integer constants. */
3672 if (TREE_CODE (expr) != INTEGER_CST)
3674 error ("%qE is not a valid template argument for type %qT "
3675 "because it is a non-constant expression", expr, type);
3676 return NULL_TREE;
3679 /* At this point, an implicit conversion does what we want,
3680 because we already know that the expression is of integral
3681 type. */
3682 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3683 if (expr == error_mark_node)
3684 return error_mark_node;
3686 /* Conversion was allowed: fold it to a bare integer constant. */
3687 expr = fold (expr);
3689 /* [temp.arg.nontype]/5, bullet 2
3691 For a non-type template-parameter of type pointer to object,
3692 qualification conversions (_conv.qual_) and the array-to-pointer
3693 conversion (_conv.array_) are applied. */
3694 else if (TYPE_PTROBV_P (type))
3696 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3698 A template-argument for a non-type, non-template template-parameter
3699 shall be one of: [...]
3701 -- the name of a non-type template-parameter;
3702 -- the address of an object or function with external linkage, [...]
3703 expressed as "& id-expression" where the & is optional if the name
3704 refers to a function or array, or if the corresponding
3705 template-parameter is a reference.
3707 Here, we do not care about functions, as they are invalid anyway
3708 for a parameter of type pointer-to-object. */
3710 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
3711 /* Non-type template parameters are OK. */
3713 else if (TREE_CODE (expr) != ADDR_EXPR
3714 && TREE_CODE (expr_type) != ARRAY_TYPE)
3716 if (TREE_CODE (expr) == VAR_DECL)
3718 error ("%qD is not a valid template argument "
3719 "because %qD is a variable, not the address of "
3720 "a variable",
3721 expr, expr);
3722 return NULL_TREE;
3724 /* Other values, like integer constants, might be valid
3725 non-type arguments of some other type. */
3726 return error_mark_node;
3728 else
3730 tree decl;
3732 decl = ((TREE_CODE (expr) == ADDR_EXPR)
3733 ? TREE_OPERAND (expr, 0) : expr);
3734 if (TREE_CODE (decl) != VAR_DECL)
3736 error ("%qE is not a valid template argument of type %qT "
3737 "because %qE is not a variable",
3738 expr, type, decl);
3739 return NULL_TREE;
3741 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
3743 error ("%qE is not a valid template argument of type %qT "
3744 "because %qD does not have external linkage",
3745 expr, type, decl);
3746 return NULL_TREE;
3750 expr = decay_conversion (expr);
3751 if (expr == error_mark_node)
3752 return error_mark_node;
3754 expr = perform_qualification_conversions (type, expr);
3755 if (expr == error_mark_node)
3756 return error_mark_node;
3758 /* [temp.arg.nontype]/5, bullet 3
3760 For a non-type template-parameter of type reference to object, no
3761 conversions apply. The type referred to by the reference may be more
3762 cv-qualified than the (otherwise identical) type of the
3763 template-argument. The template-parameter is bound directly to the
3764 template-argument, which must be an lvalue. */
3765 else if (TYPE_REF_OBJ_P (type))
3767 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3768 expr_type))
3769 return error_mark_node;
3771 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3773 error ("%qE is not a valid template argument for type %qT "
3774 "because of conflicts in cv-qualification", expr, type);
3775 return NULL_TREE;
3778 if (!real_lvalue_p (expr))
3780 error ("%qE is not a valid template argument for type %qT "
3781 "because it is not an lvalue", expr, type);
3782 return NULL_TREE;
3785 /* [temp.arg.nontype]/1
3787 A template-argument for a non-type, non-template template-parameter
3788 shall be one of: [...]
3790 -- the address of an object or function with external linkage. */
3791 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3793 error ("%qE is not a valid template argument for type %qT "
3794 "because object %qD has not external linkage",
3795 expr, type, expr);
3796 return NULL_TREE;
3799 expr = build_nop (type, build_address (expr));
3801 /* [temp.arg.nontype]/5, bullet 4
3803 For a non-type template-parameter of type pointer to function, only
3804 the function-to-pointer conversion (_conv.func_) is applied. If the
3805 template-argument represents a set of overloaded functions (or a
3806 pointer to such), the matching function is selected from the set
3807 (_over.over_). */
3808 else if (TYPE_PTRFN_P (type))
3810 /* If the argument is a template-id, we might not have enough
3811 context information to decay the pointer. */
3812 if (!type_unknown_p (expr_type))
3814 expr = decay_conversion (expr);
3815 if (expr == error_mark_node)
3816 return error_mark_node;
3819 expr = convert_nontype_argument_function (type, expr);
3820 if (!expr || expr == error_mark_node)
3821 return expr;
3823 /* [temp.arg.nontype]/5, bullet 5
3825 For a non-type template-parameter of type reference to function, no
3826 conversions apply. If the template-argument represents a set of
3827 overloaded functions, the matching function is selected from the set
3828 (_over.over_). */
3829 else if (TYPE_REFFN_P (type))
3831 if (TREE_CODE (expr) == ADDR_EXPR)
3833 error ("%qE is not a valid template argument for type %qT "
3834 "because it is a pointer", expr, type);
3835 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3836 return NULL_TREE;
3839 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3840 if (!expr || expr == error_mark_node)
3841 return expr;
3843 expr = build_nop (type, build_address (expr));
3845 /* [temp.arg.nontype]/5, bullet 6
3847 For a non-type template-parameter of type pointer to member function,
3848 no conversions apply. If the template-argument represents a set of
3849 overloaded member functions, the matching member function is selected
3850 from the set (_over.over_). */
3851 else if (TYPE_PTRMEMFUNC_P (type))
3853 expr = instantiate_type (type, expr, tf_none);
3854 if (expr == error_mark_node)
3855 return error_mark_node;
3857 /* There is no way to disable standard conversions in
3858 resolve_address_of_overloaded_function (called by
3859 instantiate_type). It is possible that the call succeeded by
3860 converting &B::I to &D::I (where B is a base of D), so we need
3861 to reject this conversion here.
3863 Actually, even if there was a way to disable standard conversions,
3864 it would still be better to reject them here so that we can
3865 provide a superior diagnostic. */
3866 if (!same_type_p (TREE_TYPE (expr), type))
3868 /* Make sure we are just one standard conversion off. */
3869 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3870 error ("%qE is not a valid template argument for type %qT "
3871 "because it is of type %qT", expr, type,
3872 TREE_TYPE (expr));
3873 inform ("standard conversions are not allowed in this context");
3874 return NULL_TREE;
3877 /* [temp.arg.nontype]/5, bullet 7
3879 For a non-type template-parameter of type pointer to data member,
3880 qualification conversions (_conv.qual_) are applied. */
3881 else if (TYPE_PTRMEM_P (type))
3883 expr = perform_qualification_conversions (type, expr);
3884 if (expr == error_mark_node)
3885 return expr;
3887 /* A template non-type parameter must be one of the above. */
3888 else
3889 gcc_unreachable ();
3891 /* Sanity check: did we actually convert the argument to the
3892 right type? */
3893 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3894 return expr;
3898 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3899 template template parameters. Both PARM_PARMS and ARG_PARMS are
3900 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3901 or PARM_DECL.
3903 Consider the example:
3904 template <class T> class A;
3905 template<template <class U> class TT> class B;
3907 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3908 the parameters to A, and OUTER_ARGS contains A. */
3910 static int
3911 coerce_template_template_parms (tree parm_parms,
3912 tree arg_parms,
3913 tsubst_flags_t complain,
3914 tree in_decl,
3915 tree outer_args)
3917 int nparms, nargs, i;
3918 tree parm, arg;
3920 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3921 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3923 nparms = TREE_VEC_LENGTH (parm_parms);
3924 nargs = TREE_VEC_LENGTH (arg_parms);
3926 if (nargs != nparms)
3927 return 0;
3929 for (i = 0; i < nparms; ++i)
3931 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3932 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
3933 continue;
3935 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3936 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3938 if (arg == NULL_TREE || arg == error_mark_node
3939 || parm == NULL_TREE || parm == error_mark_node)
3940 return 0;
3942 if (TREE_CODE (arg) != TREE_CODE (parm))
3943 return 0;
3945 switch (TREE_CODE (parm))
3947 case TYPE_DECL:
3948 break;
3950 case TEMPLATE_DECL:
3951 /* We encounter instantiations of templates like
3952 template <template <template <class> class> class TT>
3953 class C; */
3955 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3956 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3958 if (!coerce_template_template_parms
3959 (parmparm, argparm, complain, in_decl, outer_args))
3960 return 0;
3962 break;
3964 case PARM_DECL:
3965 /* The tsubst call is used to handle cases such as
3967 template <int> class C {};
3968 template <class T, template <T> class TT> class D {};
3969 D<int, C> d;
3971 i.e. the parameter list of TT depends on earlier parameters. */
3972 if (!dependent_type_p (TREE_TYPE (arg))
3973 && !same_type_p
3974 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3975 TREE_TYPE (arg)))
3976 return 0;
3977 break;
3979 default:
3980 gcc_unreachable ();
3983 return 1;
3986 /* Convert the indicated template ARG as necessary to match the
3987 indicated template PARM. Returns the converted ARG, or
3988 error_mark_node if the conversion was unsuccessful. Error and
3989 warning messages are issued under control of COMPLAIN. This
3990 conversion is for the Ith parameter in the parameter list. ARGS is
3991 the full set of template arguments deduced so far. */
3993 static tree
3994 convert_template_argument (tree parm,
3995 tree arg,
3996 tree args,
3997 tsubst_flags_t complain,
3998 int i,
3999 tree in_decl)
4001 tree val;
4002 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4004 if (TREE_CODE (arg) == TREE_LIST
4005 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4007 /* The template argument was the name of some
4008 member function. That's usually
4009 invalid, but static members are OK. In any
4010 case, grab the underlying fields/functions
4011 and issue an error later if required. */
4012 arg = TREE_VALUE (arg);
4013 TREE_TYPE (arg) = unknown_type_node;
4016 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4017 requires_type = (TREE_CODE (parm) == TYPE_DECL
4018 || requires_tmpl_type);
4020 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
4021 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
4022 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4023 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
4025 if (is_tmpl_type
4026 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4027 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4028 arg = TYPE_STUB_DECL (arg);
4030 is_type = TYPE_P (arg) || is_tmpl_type;
4032 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4033 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4035 pedwarn ("to refer to a type member of a template parameter, "
4036 "use %<typename %E%>", arg);
4038 arg = make_typename_type (TREE_OPERAND (arg, 0),
4039 TREE_OPERAND (arg, 1),
4040 typename_type,
4041 complain & tf_error);
4042 is_type = 1;
4044 if (is_type != requires_type)
4046 if (in_decl)
4048 if (complain & tf_error)
4050 error ("type/value mismatch at argument %d in template "
4051 "parameter list for %qD",
4052 i + 1, in_decl);
4053 if (is_type)
4054 error (" expected a constant of type %qT, got %qT",
4055 TREE_TYPE (parm),
4056 (is_tmpl_type ? DECL_NAME (arg) : arg));
4057 else if (requires_tmpl_type)
4058 error (" expected a class template, got %qE", arg);
4059 else
4060 error (" expected a type, got %qE", arg);
4063 return error_mark_node;
4065 if (is_tmpl_type ^ requires_tmpl_type)
4067 if (in_decl && (complain & tf_error))
4069 error ("type/value mismatch at argument %d in template "
4070 "parameter list for %qD",
4071 i + 1, in_decl);
4072 if (is_tmpl_type)
4073 error (" expected a type, got %qT", DECL_NAME (arg));
4074 else
4075 error (" expected a class template, got %qT", arg);
4077 return error_mark_node;
4080 if (is_type)
4082 if (requires_tmpl_type)
4084 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4085 /* The number of argument required is not known yet.
4086 Just accept it for now. */
4087 val = TREE_TYPE (arg);
4088 else
4090 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4091 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4093 if (coerce_template_template_parms (parmparm, argparm,
4094 complain, in_decl,
4095 args))
4097 val = arg;
4099 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4100 TEMPLATE_DECL. */
4101 if (val != error_mark_node
4102 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4103 val = TREE_TYPE (val);
4105 else
4107 if (in_decl && (complain & tf_error))
4109 error ("type/value mismatch at argument %d in "
4110 "template parameter list for %qD",
4111 i + 1, in_decl);
4112 error (" expected a template of type %qD, got %qD",
4113 parm, arg);
4116 val = error_mark_node;
4120 else
4121 val = arg;
4122 /* We only form one instance of each template specialization.
4123 Therefore, if we use a non-canonical variant (i.e., a
4124 typedef), any future messages referring to the type will use
4125 the typedef, which is confusing if those future uses do not
4126 themselves also use the typedef. */
4127 if (TYPE_P (val))
4128 val = canonical_type_variant (val);
4130 else
4132 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4134 if (invalid_nontype_parm_type_p (t, complain))
4135 return error_mark_node;
4137 if (!uses_template_parms (arg) && !uses_template_parms (t))
4138 /* We used to call digest_init here. However, digest_init
4139 will report errors, which we don't want when complain
4140 is zero. More importantly, digest_init will try too
4141 hard to convert things: for example, `0' should not be
4142 converted to pointer type at this point according to
4143 the standard. Accepting this is not merely an
4144 extension, since deciding whether or not these
4145 conversions can occur is part of determining which
4146 function template to call, or whether a given explicit
4147 argument specification is valid. */
4148 val = convert_nontype_argument (t, arg);
4149 else
4150 val = arg;
4152 if (val == NULL_TREE)
4153 val = error_mark_node;
4154 else if (val == error_mark_node && (complain & tf_error))
4155 error ("could not convert template argument %qE to %qT", arg, t);
4158 return val;
4161 /* Convert all template arguments to their appropriate types, and
4162 return a vector containing the innermost resulting template
4163 arguments. If any error occurs, return error_mark_node. Error and
4164 warning messages are issued under control of COMPLAIN.
4166 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4167 for arguments not specified in ARGS. Otherwise, if
4168 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4169 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4170 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4171 ARGS. */
4173 static tree
4174 coerce_template_parms (tree parms,
4175 tree args,
4176 tree in_decl,
4177 tsubst_flags_t complain,
4178 bool require_all_args,
4179 bool use_default_args)
4181 int nparms, nargs, i, lost = 0;
4182 tree inner_args;
4183 tree new_args;
4184 tree new_inner_args;
4185 bool saved_skip_evaluation;
4187 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4188 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4189 nparms = TREE_VEC_LENGTH (parms);
4191 if (nargs > nparms
4192 || (nargs < nparms
4193 && require_all_args
4194 && (!use_default_args
4195 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4196 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
4198 if (complain & tf_error)
4200 error ("wrong number of template arguments (%d, should be %d)",
4201 nargs, nparms);
4203 if (in_decl)
4204 error ("provided for %q+D", in_decl);
4207 return error_mark_node;
4210 /* We need to evaluate the template arguments, even though this
4211 template-id may be nested within a "sizeof". */
4212 saved_skip_evaluation = skip_evaluation;
4213 skip_evaluation = false;
4214 new_inner_args = make_tree_vec (nparms);
4215 new_args = add_outermost_template_args (args, new_inner_args);
4216 for (i = 0; i < nparms; i++)
4218 tree arg;
4219 tree parm;
4221 /* Get the Ith template parameter. */
4222 parm = TREE_VEC_ELT (parms, i);
4224 if (parm == error_mark_node)
4226 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
4227 continue;
4230 /* Calculate the Ith argument. */
4231 if (i < nargs)
4232 arg = TREE_VEC_ELT (inner_args, i);
4233 else if (require_all_args)
4234 /* There must be a default arg in this case. */
4235 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4236 complain, in_decl);
4237 else
4238 break;
4240 gcc_assert (arg);
4241 if (arg == error_mark_node)
4243 if (complain & tf_error)
4244 error ("template argument %d is invalid", i + 1);
4246 else
4247 arg = convert_template_argument (TREE_VALUE (parm),
4248 arg, new_args, complain, i,
4249 in_decl);
4251 if (arg == error_mark_node)
4252 lost++;
4253 TREE_VEC_ELT (new_inner_args, i) = arg;
4255 skip_evaluation = saved_skip_evaluation;
4257 if (lost)
4258 return error_mark_node;
4260 return new_inner_args;
4263 /* Returns 1 if template args OT and NT are equivalent. */
4265 static int
4266 template_args_equal (tree ot, tree nt)
4268 if (nt == ot)
4269 return 1;
4271 if (TREE_CODE (nt) == TREE_VEC)
4272 /* For member templates */
4273 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4274 else if (TYPE_P (nt))
4275 return TYPE_P (ot) && same_type_p (ot, nt);
4276 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4277 return 0;
4278 else
4279 return cp_tree_equal (ot, nt);
4282 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4283 of template arguments. Returns 0 otherwise. */
4286 comp_template_args (tree oldargs, tree newargs)
4288 int i;
4290 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4291 return 0;
4293 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4295 tree nt = TREE_VEC_ELT (newargs, i);
4296 tree ot = TREE_VEC_ELT (oldargs, i);
4298 if (! template_args_equal (ot, nt))
4299 return 0;
4301 return 1;
4304 /* Given class template name and parameter list, produce a user-friendly name
4305 for the instantiation. */
4307 static char *
4308 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4310 static struct obstack scratch_obstack;
4311 static char *scratch_firstobj;
4312 int i, nparms;
4314 if (!scratch_firstobj)
4315 gcc_obstack_init (&scratch_obstack);
4316 else
4317 obstack_free (&scratch_obstack, scratch_firstobj);
4318 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4320 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4321 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4323 cat (name);
4324 ccat ('<');
4325 nparms = TREE_VEC_LENGTH (parms);
4326 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4327 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4328 for (i = 0; i < nparms; i++)
4330 tree parm;
4331 tree arg;
4333 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4334 arg = TREE_VEC_ELT (arglist, i);
4336 if (parm == error_mark_node)
4337 continue;
4339 if (i)
4340 ccat (',');
4342 if (TREE_CODE (parm) == TYPE_DECL)
4344 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4345 continue;
4347 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4349 if (TREE_CODE (arg) == TEMPLATE_DECL)
4351 /* Already substituted with real template. Just output
4352 the template name here */
4353 tree context = DECL_CONTEXT (arg);
4354 if (context)
4356 /* The template may be defined in a namespace, or
4357 may be a member template. */
4358 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4359 || CLASS_TYPE_P (context));
4360 cat (decl_as_string (DECL_CONTEXT (arg),
4361 TFF_PLAIN_IDENTIFIER));
4362 cat ("::");
4364 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4366 else
4367 /* Output the parameter declaration. */
4368 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4369 continue;
4371 else
4372 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4374 /* No need to check arglist against parmlist here; we did that
4375 in coerce_template_parms, called from lookup_template_class. */
4376 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4379 char *bufp = obstack_next_free (&scratch_obstack);
4380 int offset = 0;
4381 while (bufp[offset - 1] == ' ')
4382 offset--;
4383 obstack_blank_fast (&scratch_obstack, offset);
4385 /* B<C<char> >, not B<C<char>> */
4386 if (bufp[offset - 1] == '>')
4387 ccat (' ');
4389 ccat ('>');
4390 ccat ('\0');
4391 return (char *) obstack_base (&scratch_obstack);
4394 static tree
4395 classtype_mangled_name (tree t)
4397 if (CLASSTYPE_TEMPLATE_INFO (t)
4398 /* Specializations have already had their names set up in
4399 lookup_template_class. */
4400 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4402 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4404 /* For non-primary templates, the template parameters are
4405 implicit from their surrounding context. */
4406 if (PRIMARY_TEMPLATE_P (tmpl))
4408 tree name = DECL_NAME (tmpl);
4409 char *mangled_name = mangle_class_name_for_template
4410 (IDENTIFIER_POINTER (name),
4411 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4412 CLASSTYPE_TI_ARGS (t));
4413 tree id = get_identifier (mangled_name);
4414 IDENTIFIER_TEMPLATE (id) = name;
4415 return id;
4419 return TYPE_IDENTIFIER (t);
4422 static void
4423 add_pending_template (tree d)
4425 tree ti = (TYPE_P (d)
4426 ? CLASSTYPE_TEMPLATE_INFO (d)
4427 : DECL_TEMPLATE_INFO (d));
4428 tree pt;
4429 int level;
4431 if (TI_PENDING_TEMPLATE_FLAG (ti))
4432 return;
4434 /* We are called both from instantiate_decl, where we've already had a
4435 tinst_level pushed, and instantiate_template, where we haven't.
4436 Compensate. */
4437 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4439 if (level)
4440 push_tinst_level (d);
4442 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4443 if (last_pending_template)
4444 TREE_CHAIN (last_pending_template) = pt;
4445 else
4446 pending_templates = pt;
4448 last_pending_template = pt;
4450 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4452 if (level)
4453 pop_tinst_level ();
4457 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4458 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4459 documentation for TEMPLATE_ID_EXPR. */
4461 tree
4462 lookup_template_function (tree fns, tree arglist)
4464 tree type;
4466 if (fns == error_mark_node || arglist == error_mark_node)
4467 return error_mark_node;
4469 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4470 gcc_assert (fns && (is_overloaded_fn (fns)
4471 || TREE_CODE (fns) == IDENTIFIER_NODE));
4473 if (BASELINK_P (fns))
4475 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4476 unknown_type_node,
4477 BASELINK_FUNCTIONS (fns),
4478 arglist);
4479 return fns;
4482 type = TREE_TYPE (fns);
4483 if (TREE_CODE (fns) == OVERLOAD || !type)
4484 type = unknown_type_node;
4486 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4489 /* Within the scope of a template class S<T>, the name S gets bound
4490 (in build_self_reference) to a TYPE_DECL for the class, not a
4491 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4492 or one of its enclosing classes, and that type is a template,
4493 return the associated TEMPLATE_DECL. Otherwise, the original
4494 DECL is returned. */
4496 tree
4497 maybe_get_template_decl_from_type_decl (tree decl)
4499 return (decl != NULL_TREE
4500 && TREE_CODE (decl) == TYPE_DECL
4501 && DECL_ARTIFICIAL (decl)
4502 && CLASS_TYPE_P (TREE_TYPE (decl))
4503 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4504 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4507 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4508 parameters, find the desired type.
4510 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4512 IN_DECL, if non-NULL, is the template declaration we are trying to
4513 instantiate.
4515 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4516 the class we are looking up.
4518 Issue error and warning messages under control of COMPLAIN.
4520 If the template class is really a local class in a template
4521 function, then the FUNCTION_CONTEXT is the function in which it is
4522 being instantiated.
4524 ??? Note that this function is currently called *twice* for each
4525 template-id: the first time from the parser, while creating the
4526 incomplete type (finish_template_type), and the second type during the
4527 real instantiation (instantiate_template_class). This is surely something
4528 that we want to avoid. It also causes some problems with argument
4529 coercion (see convert_nontype_argument for more information on this). */
4531 tree
4532 lookup_template_class (tree d1,
4533 tree arglist,
4534 tree in_decl,
4535 tree context,
4536 int entering_scope,
4537 tsubst_flags_t complain)
4539 tree template = NULL_TREE, parmlist;
4540 tree t;
4542 timevar_push (TV_NAME_LOOKUP);
4544 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4546 tree value = innermost_non_namespace_value (d1);
4547 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4548 template = value;
4549 else
4551 if (context)
4552 push_decl_namespace (context);
4553 template = lookup_name (d1);
4554 template = maybe_get_template_decl_from_type_decl (template);
4555 if (context)
4556 pop_decl_namespace ();
4558 if (template)
4559 context = DECL_CONTEXT (template);
4561 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4563 tree type = TREE_TYPE (d1);
4565 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4566 an implicit typename for the second A. Deal with it. */
4567 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4568 type = TREE_TYPE (type);
4570 if (CLASSTYPE_TEMPLATE_INFO (type))
4572 template = CLASSTYPE_TI_TEMPLATE (type);
4573 d1 = DECL_NAME (template);
4576 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4577 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4579 template = TYPE_TI_TEMPLATE (d1);
4580 d1 = DECL_NAME (template);
4582 else if (TREE_CODE (d1) == TEMPLATE_DECL
4583 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4585 template = d1;
4586 d1 = DECL_NAME (template);
4587 context = DECL_CONTEXT (template);
4590 /* Issue an error message if we didn't find a template. */
4591 if (! template)
4593 if (complain & tf_error)
4594 error ("%qT is not a template", d1);
4595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4598 if (TREE_CODE (template) != TEMPLATE_DECL
4599 /* Make sure it's a user visible template, if it was named by
4600 the user. */
4601 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4602 && !PRIMARY_TEMPLATE_P (template)))
4604 if (complain & tf_error)
4606 error ("non-template type %qT used as a template", d1);
4607 if (in_decl)
4608 error ("for template declaration %q+D", in_decl);
4610 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4613 complain &= ~tf_user;
4615 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4617 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4618 template arguments */
4620 tree parm;
4621 tree arglist2;
4623 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4625 /* Consider an example where a template template parameter declared as
4627 template <class T, class U = std::allocator<T> > class TT
4629 The template parameter level of T and U are one level larger than
4630 of TT. To proper process the default argument of U, say when an
4631 instantiation `TT<int>' is seen, we need to build the full
4632 arguments containing {int} as the innermost level. Outer levels,
4633 available when not appearing as default template argument, can be
4634 obtained from `current_template_args ()'.
4636 Suppose that TT is later substituted with std::vector. The above
4637 instantiation is `TT<int, std::allocator<T> >' with TT at
4638 level 1, and T at level 2, while the template arguments at level 1
4639 becomes {std::vector} and the inner level 2 is {int}. */
4641 if (current_template_parms)
4642 arglist = add_to_template_args (current_template_args (), arglist);
4644 arglist2 = coerce_template_parms (parmlist, arglist, template,
4645 complain,
4646 /*require_all_args=*/true,
4647 /*use_default_args=*/true);
4648 if (arglist2 == error_mark_node
4649 || (!uses_template_parms (arglist2)
4650 && check_instantiated_args (template, arglist2, complain)))
4651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4653 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4654 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4656 else
4658 tree template_type = TREE_TYPE (template);
4659 tree gen_tmpl;
4660 tree type_decl;
4661 tree found = NULL_TREE;
4662 int arg_depth;
4663 int parm_depth;
4664 int is_partial_instantiation;
4666 gen_tmpl = most_general_template (template);
4667 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4668 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4669 arg_depth = TMPL_ARGS_DEPTH (arglist);
4671 if (arg_depth == 1 && parm_depth > 1)
4673 /* We've been given an incomplete set of template arguments.
4674 For example, given:
4676 template <class T> struct S1 {
4677 template <class U> struct S2 {};
4678 template <class U> struct S2<U*> {};
4681 we will be called with an ARGLIST of `U*', but the
4682 TEMPLATE will be `template <class T> template
4683 <class U> struct S1<T>::S2'. We must fill in the missing
4684 arguments. */
4685 arglist
4686 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4687 arglist);
4688 arg_depth = TMPL_ARGS_DEPTH (arglist);
4691 /* Now we should have enough arguments. */
4692 gcc_assert (parm_depth == arg_depth);
4694 /* From here on, we're only interested in the most general
4695 template. */
4696 template = gen_tmpl;
4698 /* Calculate the BOUND_ARGS. These will be the args that are
4699 actually tsubst'd into the definition to create the
4700 instantiation. */
4701 if (parm_depth > 1)
4703 /* We have multiple levels of arguments to coerce, at once. */
4704 int i;
4705 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4707 tree bound_args = make_tree_vec (parm_depth);
4709 for (i = saved_depth,
4710 t = DECL_TEMPLATE_PARMS (template);
4711 i > 0 && t != NULL_TREE;
4712 --i, t = TREE_CHAIN (t))
4714 tree a = coerce_template_parms (TREE_VALUE (t),
4715 arglist, template,
4716 complain,
4717 /*require_all_args=*/true,
4718 /*use_default_args=*/true);
4720 /* Don't process further if one of the levels fails. */
4721 if (a == error_mark_node)
4723 /* Restore the ARGLIST to its full size. */
4724 TREE_VEC_LENGTH (arglist) = saved_depth;
4725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4728 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4730 /* We temporarily reduce the length of the ARGLIST so
4731 that coerce_template_parms will see only the arguments
4732 corresponding to the template parameters it is
4733 examining. */
4734 TREE_VEC_LENGTH (arglist)--;
4737 /* Restore the ARGLIST to its full size. */
4738 TREE_VEC_LENGTH (arglist) = saved_depth;
4740 arglist = bound_args;
4742 else
4743 arglist
4744 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4745 INNERMOST_TEMPLATE_ARGS (arglist),
4746 template,
4747 complain,
4748 /*require_all_args=*/true,
4749 /*use_default_args=*/true);
4751 if (arglist == error_mark_node)
4752 /* We were unable to bind the arguments. */
4753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4755 /* In the scope of a template class, explicit references to the
4756 template class refer to the type of the template, not any
4757 instantiation of it. For example, in:
4759 template <class T> class C { void f(C<T>); }
4761 the `C<T>' is just the same as `C'. Outside of the
4762 class, however, such a reference is an instantiation. */
4763 if (comp_template_args (TYPE_TI_ARGS (template_type),
4764 arglist))
4766 found = template_type;
4768 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4770 tree ctx;
4772 for (ctx = current_class_type;
4773 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4774 ctx = (TYPE_P (ctx)
4775 ? TYPE_CONTEXT (ctx)
4776 : DECL_CONTEXT (ctx)))
4777 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4778 goto found_ctx;
4780 /* We're not in the scope of the class, so the
4781 TEMPLATE_TYPE is not the type we want after all. */
4782 found = NULL_TREE;
4783 found_ctx:;
4786 if (found)
4787 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4789 /* If we already have this specialization, return it. */
4790 found = retrieve_specialization (template, arglist,
4791 /*class_specializations_p=*/false);
4792 if (found)
4793 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4795 /* This type is a "partial instantiation" if any of the template
4796 arguments still involve template parameters. Note that we set
4797 IS_PARTIAL_INSTANTIATION for partial specializations as
4798 well. */
4799 is_partial_instantiation = uses_template_parms (arglist);
4801 /* If the deduced arguments are invalid, then the binding
4802 failed. */
4803 if (!is_partial_instantiation
4804 && check_instantiated_args (template,
4805 INNERMOST_TEMPLATE_ARGS (arglist),
4806 complain))
4807 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4809 if (!is_partial_instantiation
4810 && !PRIMARY_TEMPLATE_P (template)
4811 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4813 found = xref_tag_from_type (TREE_TYPE (template),
4814 DECL_NAME (template),
4815 /*tag_scope=*/ts_global);
4816 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4819 context = tsubst (DECL_CONTEXT (template), arglist,
4820 complain, in_decl);
4821 if (!context)
4822 context = global_namespace;
4824 /* Create the type. */
4825 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4827 if (!is_partial_instantiation)
4829 set_current_access_from_decl (TYPE_NAME (template_type));
4830 t = start_enum (TYPE_IDENTIFIER (template_type));
4832 else
4833 /* We don't want to call start_enum for this type, since
4834 the values for the enumeration constants may involve
4835 template parameters. And, no one should be interested
4836 in the enumeration constants for such a type. */
4837 t = make_node (ENUMERAL_TYPE);
4839 else
4841 t = make_aggr_type (TREE_CODE (template_type));
4842 CLASSTYPE_DECLARED_CLASS (t)
4843 = CLASSTYPE_DECLARED_CLASS (template_type);
4844 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4845 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4847 /* A local class. Make sure the decl gets registered properly. */
4848 if (context == current_function_decl)
4849 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4851 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
4852 /* This instantiation is another name for the primary
4853 template type. Set the TYPE_CANONICAL field
4854 appropriately. */
4855 TYPE_CANONICAL (t) = template_type;
4856 else if (any_template_arguments_need_structural_equality_p (arglist))
4857 /* Some of the template arguments require structural
4858 equality testing, so this template class requires
4859 structural equality testing. */
4860 SET_TYPE_STRUCTURAL_EQUALITY (t);
4863 /* If we called start_enum or pushtag above, this information
4864 will already be set up. */
4865 if (!TYPE_NAME (t))
4867 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4869 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4870 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4871 TYPE_STUB_DECL (t) = type_decl;
4872 DECL_SOURCE_LOCATION (type_decl)
4873 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4875 else
4876 type_decl = TYPE_NAME (t);
4878 TREE_PRIVATE (type_decl)
4879 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4880 TREE_PROTECTED (type_decl)
4881 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4882 DECL_IN_SYSTEM_HEADER (type_decl)
4883 = DECL_IN_SYSTEM_HEADER (template);
4884 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4886 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4887 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4890 /* Set up the template information. We have to figure out which
4891 template is the immediate parent if this is a full
4892 instantiation. */
4893 if (parm_depth == 1 || is_partial_instantiation
4894 || !PRIMARY_TEMPLATE_P (template))
4895 /* This case is easy; there are no member templates involved. */
4896 found = template;
4897 else
4899 /* This is a full instantiation of a member template. Look
4900 for a partial instantiation of which this is an instance. */
4902 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4903 found; found = TREE_CHAIN (found))
4905 int success;
4906 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4908 /* We only want partial instantiations, here, not
4909 specializations or full instantiations. */
4910 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4911 || !uses_template_parms (TREE_VALUE (found)))
4912 continue;
4914 /* Temporarily reduce by one the number of levels in the
4915 ARGLIST and in FOUND so as to avoid comparing the
4916 last set of arguments. */
4917 TREE_VEC_LENGTH (arglist)--;
4918 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4920 /* See if the arguments match. If they do, then TMPL is
4921 the partial instantiation we want. */
4922 success = comp_template_args (TREE_PURPOSE (found), arglist);
4924 /* Restore the argument vectors to their full size. */
4925 TREE_VEC_LENGTH (arglist)++;
4926 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4928 if (success)
4930 found = tmpl;
4931 break;
4935 if (!found)
4937 /* There was no partial instantiation. This happens
4938 where C<T> is a member template of A<T> and it's used
4939 in something like
4941 template <typename T> struct B { A<T>::C<int> m; };
4942 B<float>;
4944 Create the partial instantiation.
4946 TREE_VEC_LENGTH (arglist)--;
4947 found = tsubst (template, arglist, complain, NULL_TREE);
4948 TREE_VEC_LENGTH (arglist)++;
4952 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4953 DECL_TEMPLATE_INSTANTIATIONS (template)
4954 = tree_cons (arglist, t,
4955 DECL_TEMPLATE_INSTANTIATIONS (template));
4957 if (TREE_CODE (t) == ENUMERAL_TYPE
4958 && !is_partial_instantiation)
4959 /* Now that the type has been registered on the instantiations
4960 list, we set up the enumerators. Because the enumeration
4961 constants may involve the enumeration type itself, we make
4962 sure to register the type first, and then create the
4963 constants. That way, doing tsubst_expr for the enumeration
4964 constants won't result in recursive calls here; we'll find
4965 the instantiation and exit above. */
4966 tsubst_enum (template_type, t, arglist);
4968 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4969 is set up. */
4970 if (TREE_CODE (t) != ENUMERAL_TYPE)
4971 DECL_NAME (type_decl) = classtype_mangled_name (t);
4972 if (is_partial_instantiation)
4973 /* If the type makes use of template parameters, the
4974 code that generates debugging information will crash. */
4975 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4977 /* Possibly limit visibility based on template args. */
4978 TREE_PUBLIC (type_decl) = 1;
4979 determine_visibility (type_decl);
4981 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4983 timevar_pop (TV_NAME_LOOKUP);
4986 struct pair_fn_data
4988 tree_fn_t fn;
4989 void *data;
4990 struct pointer_set_t *visited;
4993 /* Called from for_each_template_parm via walk_tree. */
4995 static tree
4996 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4998 tree t = *tp;
4999 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
5000 tree_fn_t fn = pfd->fn;
5001 void *data = pfd->data;
5003 if (TYPE_P (t)
5004 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
5005 return error_mark_node;
5007 switch (TREE_CODE (t))
5009 case RECORD_TYPE:
5010 if (TYPE_PTRMEMFUNC_P (t))
5011 break;
5012 /* Fall through. */
5014 case UNION_TYPE:
5015 case ENUMERAL_TYPE:
5016 if (!TYPE_TEMPLATE_INFO (t))
5017 *walk_subtrees = 0;
5018 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
5019 fn, data, pfd->visited))
5020 return error_mark_node;
5021 break;
5023 case INTEGER_TYPE:
5024 if (for_each_template_parm (TYPE_MIN_VALUE (t),
5025 fn, data, pfd->visited)
5026 || for_each_template_parm (TYPE_MAX_VALUE (t),
5027 fn, data, pfd->visited))
5028 return error_mark_node;
5029 break;
5031 case METHOD_TYPE:
5032 /* Since we're not going to walk subtrees, we have to do this
5033 explicitly here. */
5034 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
5035 pfd->visited))
5036 return error_mark_node;
5037 /* Fall through. */
5039 case FUNCTION_TYPE:
5040 /* Check the return type. */
5041 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5042 return error_mark_node;
5044 /* Check the parameter types. Since default arguments are not
5045 instantiated until they are needed, the TYPE_ARG_TYPES may
5046 contain expressions that involve template parameters. But,
5047 no-one should be looking at them yet. And, once they're
5048 instantiated, they don't contain template parameters, so
5049 there's no point in looking at them then, either. */
5051 tree parm;
5053 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5054 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5055 pfd->visited))
5056 return error_mark_node;
5058 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5059 want walk_tree walking into them itself. */
5060 *walk_subtrees = 0;
5062 break;
5064 case TYPEOF_TYPE:
5065 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5066 pfd->visited))
5067 return error_mark_node;
5068 break;
5070 case FUNCTION_DECL:
5071 case VAR_DECL:
5072 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5073 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5074 pfd->visited))
5075 return error_mark_node;
5076 /* Fall through. */
5078 case PARM_DECL:
5079 case CONST_DECL:
5080 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5081 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5082 pfd->visited))
5083 return error_mark_node;
5084 if (DECL_CONTEXT (t)
5085 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5086 pfd->visited))
5087 return error_mark_node;
5088 break;
5090 case BOUND_TEMPLATE_TEMPLATE_PARM:
5091 /* Record template parameters such as `T' inside `TT<T>'. */
5092 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5093 return error_mark_node;
5094 /* Fall through. */
5096 case TEMPLATE_TEMPLATE_PARM:
5097 case TEMPLATE_TYPE_PARM:
5098 case TEMPLATE_PARM_INDEX:
5099 if (fn && (*fn)(t, data))
5100 return error_mark_node;
5101 else if (!fn)
5102 return error_mark_node;
5103 break;
5105 case TEMPLATE_DECL:
5106 /* A template template parameter is encountered. */
5107 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5108 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5109 return error_mark_node;
5111 /* Already substituted template template parameter */
5112 *walk_subtrees = 0;
5113 break;
5115 case TYPENAME_TYPE:
5116 if (!fn
5117 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5118 data, pfd->visited))
5119 return error_mark_node;
5120 break;
5122 case CONSTRUCTOR:
5123 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5124 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5125 (TREE_TYPE (t)), fn, data,
5126 pfd->visited))
5127 return error_mark_node;
5128 break;
5130 case INDIRECT_REF:
5131 case COMPONENT_REF:
5132 /* If there's no type, then this thing must be some expression
5133 involving template parameters. */
5134 if (!fn && !TREE_TYPE (t))
5135 return error_mark_node;
5136 break;
5138 case MODOP_EXPR:
5139 case CAST_EXPR:
5140 case REINTERPRET_CAST_EXPR:
5141 case CONST_CAST_EXPR:
5142 case STATIC_CAST_EXPR:
5143 case DYNAMIC_CAST_EXPR:
5144 case ARROW_EXPR:
5145 case DOTSTAR_EXPR:
5146 case TYPEID_EXPR:
5147 case PSEUDO_DTOR_EXPR:
5148 if (!fn)
5149 return error_mark_node;
5150 break;
5152 case BASELINK:
5153 /* If we do not handle this case specially, we end up walking
5154 the BINFO hierarchy, which is circular, and therefore
5155 confuses walk_tree. */
5156 *walk_subtrees = 0;
5157 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5158 pfd->visited))
5159 return error_mark_node;
5160 break;
5162 default:
5163 break;
5166 /* We didn't find any template parameters we liked. */
5167 return NULL_TREE;
5170 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5171 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5172 call FN with the parameter and the DATA.
5173 If FN returns nonzero, the iteration is terminated, and
5174 for_each_template_parm returns 1. Otherwise, the iteration
5175 continues. If FN never returns a nonzero value, the value
5176 returned by for_each_template_parm is 0. If FN is NULL, it is
5177 considered to be the function which always returns 1. */
5179 static int
5180 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5181 struct pointer_set_t *visited)
5183 struct pair_fn_data pfd;
5184 int result;
5186 /* Set up. */
5187 pfd.fn = fn;
5188 pfd.data = data;
5190 /* Walk the tree. (Conceptually, we would like to walk without
5191 duplicates, but for_each_template_parm_r recursively calls
5192 for_each_template_parm, so we would need to reorganize a fair
5193 bit to use walk_tree_without_duplicates, so we keep our own
5194 visited list.) */
5195 if (visited)
5196 pfd.visited = visited;
5197 else
5198 pfd.visited = pointer_set_create ();
5199 result = walk_tree (&t,
5200 for_each_template_parm_r,
5201 &pfd,
5202 pfd.visited) != NULL_TREE;
5204 /* Clean up. */
5205 if (!visited)
5207 pointer_set_destroy (pfd.visited);
5208 pfd.visited = 0;
5211 return result;
5214 /* Returns true if T depends on any template parameter. */
5217 uses_template_parms (tree t)
5219 bool dependent_p;
5220 int saved_processing_template_decl;
5222 saved_processing_template_decl = processing_template_decl;
5223 if (!saved_processing_template_decl)
5224 processing_template_decl = 1;
5225 if (TYPE_P (t))
5226 dependent_p = dependent_type_p (t);
5227 else if (TREE_CODE (t) == TREE_VEC)
5228 dependent_p = any_dependent_template_arguments_p (t);
5229 else if (TREE_CODE (t) == TREE_LIST)
5230 dependent_p = (uses_template_parms (TREE_VALUE (t))
5231 || uses_template_parms (TREE_CHAIN (t)));
5232 else if (TREE_CODE (t) == TYPE_DECL)
5233 dependent_p = dependent_type_p (TREE_TYPE (t));
5234 else if (DECL_P (t)
5235 || EXPR_P (t)
5236 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5237 || TREE_CODE (t) == OVERLOAD
5238 || TREE_CODE (t) == BASELINK
5239 || TREE_CODE (t) == IDENTIFIER_NODE
5240 || CONSTANT_CLASS_P (t))
5241 dependent_p = (type_dependent_expression_p (t)
5242 || value_dependent_expression_p (t));
5243 else
5245 gcc_assert (t == error_mark_node);
5246 dependent_p = false;
5249 processing_template_decl = saved_processing_template_decl;
5251 return dependent_p;
5254 /* Returns true if T depends on any template parameter with level LEVEL. */
5257 uses_template_parms_level (tree t, int level)
5259 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5262 static int tinst_depth;
5263 extern int max_tinst_depth;
5264 #ifdef GATHER_STATISTICS
5265 int depth_reached;
5266 #endif
5267 static int tinst_level_tick;
5268 static int last_template_error_tick;
5270 /* We're starting to instantiate D; record the template instantiation context
5271 for diagnostics and to restore it later. */
5273 static int
5274 push_tinst_level (tree d)
5276 tree new;
5278 if (tinst_depth >= max_tinst_depth)
5280 /* If the instantiation in question still has unbound template parms,
5281 we don't really care if we can't instantiate it, so just return.
5282 This happens with base instantiation for implicit `typename'. */
5283 if (uses_template_parms (d))
5284 return 0;
5286 last_template_error_tick = tinst_level_tick;
5287 error ("template instantiation depth exceeds maximum of %d (use "
5288 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5289 max_tinst_depth, d);
5291 print_instantiation_context ();
5293 return 0;
5296 new = make_node (TINST_LEVEL);
5297 TINST_DECL (new) = d;
5298 TINST_LOCATION (new) = input_location;
5299 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5300 TREE_CHAIN (new) = current_tinst_level;
5301 current_tinst_level = new;
5303 ++tinst_depth;
5304 #ifdef GATHER_STATISTICS
5305 if (tinst_depth > depth_reached)
5306 depth_reached = tinst_depth;
5307 #endif
5309 ++tinst_level_tick;
5310 return 1;
5313 /* We're done instantiating this template; return to the instantiation
5314 context. */
5316 static void
5317 pop_tinst_level (void)
5319 tree old = current_tinst_level;
5321 /* Restore the filename and line number stashed away when we started
5322 this instantiation. */
5323 input_location = TINST_LOCATION (old);
5324 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5325 current_tinst_level = TREE_CHAIN (old);
5326 --tinst_depth;
5327 ++tinst_level_tick;
5330 /* We're instantiating a deferred template; restore the template
5331 instantiation context in which the instantiation was requested, which
5332 is one step out from LEVEL. */
5334 static void
5335 reopen_tinst_level (tree level)
5337 tree t;
5339 tinst_depth = 0;
5340 for (t = level; t; t = TREE_CHAIN (t))
5341 ++tinst_depth;
5343 current_tinst_level = level;
5344 pop_tinst_level ();
5347 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5348 vector of template arguments, as for tsubst.
5350 Returns an appropriate tsubst'd friend declaration. */
5352 static tree
5353 tsubst_friend_function (tree decl, tree args)
5355 tree new_friend;
5357 if (TREE_CODE (decl) == FUNCTION_DECL
5358 && DECL_TEMPLATE_INSTANTIATION (decl)
5359 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5360 /* This was a friend declared with an explicit template
5361 argument list, e.g.:
5363 friend void f<>(T);
5365 to indicate that f was a template instantiation, not a new
5366 function declaration. Now, we have to figure out what
5367 instantiation of what template. */
5369 tree template_id, arglist, fns;
5370 tree new_args;
5371 tree tmpl;
5372 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5374 /* Friend functions are looked up in the containing namespace scope.
5375 We must enter that scope, to avoid finding member functions of the
5376 current cless with same name. */
5377 push_nested_namespace (ns);
5378 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5379 tf_warning_or_error, NULL_TREE,
5380 /*integral_constant_expression_p=*/false);
5381 pop_nested_namespace (ns);
5382 arglist = tsubst (DECL_TI_ARGS (decl), args,
5383 tf_warning_or_error, NULL_TREE);
5384 template_id = lookup_template_function (fns, arglist);
5386 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5387 tmpl = determine_specialization (template_id, new_friend,
5388 &new_args,
5389 /*need_member_template=*/0,
5390 TREE_VEC_LENGTH (args),
5391 tsk_none);
5392 return instantiate_template (tmpl, new_args, tf_error);
5395 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5397 /* The NEW_FRIEND will look like an instantiation, to the
5398 compiler, but is not an instantiation from the point of view of
5399 the language. For example, we might have had:
5401 template <class T> struct S {
5402 template <class U> friend void f(T, U);
5405 Then, in S<int>, template <class U> void f(int, U) is not an
5406 instantiation of anything. */
5407 if (new_friend == error_mark_node)
5408 return error_mark_node;
5410 DECL_USE_TEMPLATE (new_friend) = 0;
5411 if (TREE_CODE (decl) == TEMPLATE_DECL)
5413 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5414 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5415 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5418 /* The mangled name for the NEW_FRIEND is incorrect. The function
5419 is not a template instantiation and should not be mangled like
5420 one. Therefore, we forget the mangling here; we'll recompute it
5421 later if we need it. */
5422 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5424 SET_DECL_RTL (new_friend, NULL_RTX);
5425 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5428 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5430 tree old_decl;
5431 tree new_friend_template_info;
5432 tree new_friend_result_template_info;
5433 tree ns;
5434 int new_friend_is_defn;
5436 /* We must save some information from NEW_FRIEND before calling
5437 duplicate decls since that function will free NEW_FRIEND if
5438 possible. */
5439 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5440 new_friend_is_defn =
5441 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5442 (template_for_substitution (new_friend)))
5443 != NULL_TREE);
5444 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5446 /* This declaration is a `primary' template. */
5447 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5449 new_friend_result_template_info
5450 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5452 else
5453 new_friend_result_template_info = NULL_TREE;
5455 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5456 if (new_friend_is_defn)
5457 DECL_INITIAL (new_friend) = error_mark_node;
5459 /* Inside pushdecl_namespace_level, we will push into the
5460 current namespace. However, the friend function should go
5461 into the namespace of the template. */
5462 ns = decl_namespace_context (new_friend);
5463 push_nested_namespace (ns);
5464 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5465 pop_nested_namespace (ns);
5467 if (old_decl == error_mark_node)
5468 return error_mark_node;
5470 if (old_decl != new_friend)
5472 /* This new friend declaration matched an existing
5473 declaration. For example, given:
5475 template <class T> void f(T);
5476 template <class U> class C {
5477 template <class T> friend void f(T) {}
5480 the friend declaration actually provides the definition
5481 of `f', once C has been instantiated for some type. So,
5482 old_decl will be the out-of-class template declaration,
5483 while new_friend is the in-class definition.
5485 But, if `f' was called before this point, the
5486 instantiation of `f' will have DECL_TI_ARGS corresponding
5487 to `T' but not to `U', references to which might appear
5488 in the definition of `f'. Previously, the most general
5489 template for an instantiation of `f' was the out-of-class
5490 version; now it is the in-class version. Therefore, we
5491 run through all specialization of `f', adding to their
5492 DECL_TI_ARGS appropriately. In particular, they need a
5493 new set of outer arguments, corresponding to the
5494 arguments for this class instantiation.
5496 The same situation can arise with something like this:
5498 friend void f(int);
5499 template <class T> class C {
5500 friend void f(T) {}
5503 when `C<int>' is instantiated. Now, `f(int)' is defined
5504 in the class. */
5506 if (!new_friend_is_defn)
5507 /* On the other hand, if the in-class declaration does
5508 *not* provide a definition, then we don't want to alter
5509 existing definitions. We can just leave everything
5510 alone. */
5512 else
5514 /* Overwrite whatever template info was there before, if
5515 any, with the new template information pertaining to
5516 the declaration. */
5517 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5519 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5520 reregister_specialization (new_friend,
5521 most_general_template (old_decl),
5522 old_decl);
5523 else
5525 tree t;
5526 tree new_friend_args;
5528 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5529 = new_friend_result_template_info;
5531 new_friend_args = TI_ARGS (new_friend_template_info);
5532 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5533 t != NULL_TREE;
5534 t = TREE_CHAIN (t))
5536 tree spec = TREE_VALUE (t);
5538 DECL_TI_ARGS (spec)
5539 = add_outermost_template_args (new_friend_args,
5540 DECL_TI_ARGS (spec));
5543 /* Now, since specializations are always supposed to
5544 hang off of the most general template, we must move
5545 them. */
5546 t = most_general_template (old_decl);
5547 if (t != old_decl)
5549 DECL_TEMPLATE_SPECIALIZATIONS (t)
5550 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5551 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5552 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5557 /* The information from NEW_FRIEND has been merged into OLD_DECL
5558 by duplicate_decls. */
5559 new_friend = old_decl;
5562 else
5564 tree context = DECL_CONTEXT (new_friend);
5565 bool dependent_p;
5567 /* In the code
5568 template <class T> class C {
5569 template <class U> friend void C1<U>::f (); // case 1
5570 friend void C2<T>::f (); // case 2
5572 we only need to make sure CONTEXT is a complete type for
5573 case 2. To distinguish between the two cases, we note that
5574 CONTEXT of case 1 remains dependent type after tsubst while
5575 this isn't true for case 2. */
5576 ++processing_template_decl;
5577 dependent_p = dependent_type_p (context);
5578 --processing_template_decl;
5580 if (!dependent_p
5581 && !complete_type_or_else (context, NULL_TREE))
5582 return error_mark_node;
5584 if (COMPLETE_TYPE_P (context))
5586 /* Check to see that the declaration is really present, and,
5587 possibly obtain an improved declaration. */
5588 tree fn = check_classfn (context,
5589 new_friend, NULL_TREE);
5591 if (fn)
5592 new_friend = fn;
5596 return new_friend;
5599 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5600 template arguments, as for tsubst.
5602 Returns an appropriate tsubst'd friend type or error_mark_node on
5603 failure. */
5605 static tree
5606 tsubst_friend_class (tree friend_tmpl, tree args)
5608 tree friend_type;
5609 tree tmpl;
5610 tree context;
5612 context = DECL_CONTEXT (friend_tmpl);
5614 if (context)
5616 if (TREE_CODE (context) == NAMESPACE_DECL)
5617 push_nested_namespace (context);
5618 else
5619 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5622 /* Look for a class template declaration. We look for hidden names
5623 because two friend declarations of the same template are the
5624 same. For example, in:
5626 struct A {
5627 template <typename> friend class F;
5629 template <typename> struct B {
5630 template <typename> friend class F;
5633 both F templates are the same. */
5634 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5635 /*block_p=*/true, 0,
5636 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
5638 /* But, if we don't find one, it might be because we're in a
5639 situation like this:
5641 template <class T>
5642 struct S {
5643 template <class U>
5644 friend struct S;
5647 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5648 for `S<int>', not the TEMPLATE_DECL. */
5649 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5651 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5652 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5655 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5657 /* The friend template has already been declared. Just
5658 check to see that the declarations match, and install any new
5659 default parameters. We must tsubst the default parameters,
5660 of course. We only need the innermost template parameters
5661 because that is all that redeclare_class_template will look
5662 at. */
5663 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5664 > TMPL_ARGS_DEPTH (args))
5666 tree parms;
5667 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5668 args, tf_warning_or_error);
5669 redeclare_class_template (TREE_TYPE (tmpl), parms);
5672 friend_type = TREE_TYPE (tmpl);
5674 else
5676 /* The friend template has not already been declared. In this
5677 case, the instantiation of the template class will cause the
5678 injection of this template into the global scope. */
5679 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5680 if (tmpl == error_mark_node)
5681 return error_mark_node;
5683 /* The new TMPL is not an instantiation of anything, so we
5684 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5685 the new type because that is supposed to be the corresponding
5686 template decl, i.e., TMPL. */
5687 DECL_USE_TEMPLATE (tmpl) = 0;
5688 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5689 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5690 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5691 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5693 /* Inject this template into the global scope. */
5694 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5697 if (context)
5699 if (TREE_CODE (context) == NAMESPACE_DECL)
5700 pop_nested_namespace (context);
5701 else
5702 pop_nested_class ();
5705 return friend_type;
5708 /* Returns zero if TYPE cannot be completed later due to circularity.
5709 Otherwise returns one. */
5711 static int
5712 can_complete_type_without_circularity (tree type)
5714 if (type == NULL_TREE || type == error_mark_node)
5715 return 0;
5716 else if (COMPLETE_TYPE_P (type))
5717 return 1;
5718 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5719 return can_complete_type_without_circularity (TREE_TYPE (type));
5720 else if (CLASS_TYPE_P (type)
5721 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5722 return 0;
5723 else
5724 return 1;
5727 tree
5728 instantiate_class_template (tree type)
5730 tree template, args, pattern, t, member;
5731 tree typedecl;
5732 tree pbinfo;
5733 tree base_list;
5735 if (type == error_mark_node)
5736 return error_mark_node;
5738 if (TYPE_BEING_DEFINED (type)
5739 || COMPLETE_TYPE_P (type)
5740 || dependent_type_p (type))
5741 return type;
5743 /* Figure out which template is being instantiated. */
5744 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5745 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5747 /* Determine what specialization of the original template to
5748 instantiate. */
5749 t = most_specialized_class (type, template);
5750 if (t == error_mark_node)
5752 TYPE_BEING_DEFINED (type) = 1;
5753 return error_mark_node;
5755 else if (t)
5757 /* This TYPE is actually an instantiation of a partial
5758 specialization. We replace the innermost set of ARGS with
5759 the arguments appropriate for substitution. For example,
5760 given:
5762 template <class T> struct S {};
5763 template <class T> struct S<T*> {};
5765 and supposing that we are instantiating S<int*>, ARGS will
5766 presently be {int*} -- but we need {int}. */
5767 pattern = TREE_TYPE (t);
5768 args = TREE_PURPOSE (t);
5770 else
5772 pattern = TREE_TYPE (template);
5773 args = CLASSTYPE_TI_ARGS (type);
5776 /* If the template we're instantiating is incomplete, then clearly
5777 there's nothing we can do. */
5778 if (!COMPLETE_TYPE_P (pattern))
5779 return type;
5781 /* If we've recursively instantiated too many templates, stop. */
5782 if (! push_tinst_level (type))
5783 return type;
5785 /* Now we're really doing the instantiation. Mark the type as in
5786 the process of being defined. */
5787 TYPE_BEING_DEFINED (type) = 1;
5789 /* We may be in the middle of deferred access check. Disable
5790 it now. */
5791 push_deferring_access_checks (dk_no_deferred);
5793 push_to_top_level ();
5795 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5797 /* Set the input location to the template definition. This is needed
5798 if tsubsting causes an error. */
5799 typedecl = TYPE_MAIN_DECL (type);
5800 input_location = DECL_SOURCE_LOCATION (typedecl);
5801 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5803 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5804 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5805 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5806 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5807 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5808 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5809 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5810 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5811 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5812 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5813 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5814 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5815 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5816 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5817 if (ANON_AGGR_TYPE_P (pattern))
5818 SET_ANON_AGGR_TYPE_P (type);
5819 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5821 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5822 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5825 pbinfo = TYPE_BINFO (pattern);
5827 /* We should never instantiate a nested class before its enclosing
5828 class; we need to look up the nested class by name before we can
5829 instantiate it, and that lookup should instantiate the enclosing
5830 class. */
5831 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5832 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5833 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5835 base_list = NULL_TREE;
5836 if (BINFO_N_BASE_BINFOS (pbinfo))
5838 tree pbase_binfo;
5839 tree context = TYPE_CONTEXT (type);
5840 tree pushed_scope;
5841 int i;
5843 /* We must enter the scope containing the type, as that is where
5844 the accessibility of types named in dependent bases are
5845 looked up from. */
5846 pushed_scope = push_scope (context ? context : global_namespace);
5848 /* Substitute into each of the bases to determine the actual
5849 basetypes. */
5850 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5852 tree base;
5853 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5855 /* Substitute to figure out the base class. */
5856 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5857 if (base == error_mark_node)
5858 continue;
5860 base_list = tree_cons (access, base, base_list);
5861 if (BINFO_VIRTUAL_P (pbase_binfo))
5862 TREE_TYPE (base_list) = integer_type_node;
5865 /* The list is now in reverse order; correct that. */
5866 base_list = nreverse (base_list);
5868 if (pushed_scope)
5869 pop_scope (pushed_scope);
5871 /* Now call xref_basetypes to set up all the base-class
5872 information. */
5873 xref_basetypes (type, base_list);
5876 /* Now that our base classes are set up, enter the scope of the
5877 class, so that name lookups into base classes, etc. will work
5878 correctly. This is precisely analogous to what we do in
5879 begin_class_definition when defining an ordinary non-template
5880 class. */
5881 pushclass (type);
5883 /* Now members are processed in the order of declaration. */
5884 for (member = CLASSTYPE_DECL_LIST (pattern);
5885 member; member = TREE_CHAIN (member))
5887 tree t = TREE_VALUE (member);
5889 if (TREE_PURPOSE (member))
5891 if (TYPE_P (t))
5893 /* Build new CLASSTYPE_NESTED_UTDS. */
5895 tree newtag;
5896 bool class_template_p;
5898 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5899 && TYPE_LANG_SPECIFIC (t)
5900 && CLASSTYPE_IS_TEMPLATE (t));
5901 /* If the member is a class template, then -- even after
5902 substitution -- there may be dependent types in the
5903 template argument list for the class. We increment
5904 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5905 that function will assume that no types are dependent
5906 when outside of a template. */
5907 if (class_template_p)
5908 ++processing_template_decl;
5909 newtag = tsubst (t, args, tf_error, NULL_TREE);
5910 if (class_template_p)
5911 --processing_template_decl;
5912 if (newtag == error_mark_node)
5913 continue;
5915 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5917 tree name = TYPE_IDENTIFIER (t);
5919 if (class_template_p)
5920 /* Unfortunately, lookup_template_class sets
5921 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5922 instantiation (i.e., for the type of a member
5923 template class nested within a template class.)
5924 This behavior is required for
5925 maybe_process_partial_specialization to work
5926 correctly, but is not accurate in this case;
5927 the TAG is not an instantiation of anything.
5928 (The corresponding TEMPLATE_DECL is an
5929 instantiation, but the TYPE is not.) */
5930 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5932 /* Now, we call pushtag to put this NEWTAG into the scope of
5933 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5934 pushtag calling push_template_decl. We don't have to do
5935 this for enums because it will already have been done in
5936 tsubst_enum. */
5937 if (name)
5938 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5939 pushtag (name, newtag, /*tag_scope=*/ts_current);
5942 else if (TREE_CODE (t) == FUNCTION_DECL
5943 || DECL_FUNCTION_TEMPLATE_P (t))
5945 /* Build new TYPE_METHODS. */
5946 tree r;
5948 if (TREE_CODE (t) == TEMPLATE_DECL)
5949 ++processing_template_decl;
5950 r = tsubst (t, args, tf_error, NULL_TREE);
5951 if (TREE_CODE (t) == TEMPLATE_DECL)
5952 --processing_template_decl;
5953 set_current_access_from_decl (r);
5954 finish_member_declaration (r);
5956 else
5958 /* Build new TYPE_FIELDS. */
5959 if (TREE_CODE (t) == STATIC_ASSERT)
5961 tree condition =
5962 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
5963 tf_warning_or_error, NULL_TREE,
5964 /*integral_constant_expression_p=*/true);
5965 finish_static_assert (condition,
5966 STATIC_ASSERT_MESSAGE (t),
5967 STATIC_ASSERT_SOURCE_LOCATION (t),
5968 /*member_p=*/true);
5970 else if (TREE_CODE (t) != CONST_DECL)
5972 tree r;
5974 /* The the file and line for this declaration, to
5975 assist in error message reporting. Since we
5976 called push_tinst_level above, we don't need to
5977 restore these. */
5978 input_location = DECL_SOURCE_LOCATION (t);
5980 if (TREE_CODE (t) == TEMPLATE_DECL)
5981 ++processing_template_decl;
5982 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5983 if (TREE_CODE (t) == TEMPLATE_DECL)
5984 --processing_template_decl;
5985 if (TREE_CODE (r) == VAR_DECL)
5987 /* In [temp.inst]:
5989 [t]he initialization (and any associated
5990 side-effects) of a static data member does
5991 not occur unless the static data member is
5992 itself used in a way that requires the
5993 definition of the static data member to
5994 exist.
5996 Therefore, we do not substitute into the
5997 initialized for the static data member here. */
5998 finish_static_data_member_decl
6000 /*init=*/NULL_TREE,
6001 /*init_const_expr_p=*/false,
6002 /*asmspec_tree=*/NULL_TREE,
6003 /*flags=*/0);
6004 if (DECL_INITIALIZED_IN_CLASS_P (r))
6005 check_static_variable_definition (r, TREE_TYPE (r));
6007 else if (TREE_CODE (r) == FIELD_DECL)
6009 /* Determine whether R has a valid type and can be
6010 completed later. If R is invalid, then it is
6011 replaced by error_mark_node so that it will not be
6012 added to TYPE_FIELDS. */
6013 tree rtype = TREE_TYPE (r);
6014 if (can_complete_type_without_circularity (rtype))
6015 complete_type (rtype);
6017 if (!COMPLETE_TYPE_P (rtype))
6019 cxx_incomplete_type_error (r, rtype);
6020 r = error_mark_node;
6024 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6025 such a thing will already have been added to the field
6026 list by tsubst_enum in finish_member_declaration in the
6027 CLASSTYPE_NESTED_UTDS case above. */
6028 if (!(TREE_CODE (r) == TYPE_DECL
6029 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
6030 && DECL_ARTIFICIAL (r)))
6032 set_current_access_from_decl (r);
6033 finish_member_declaration (r);
6038 else
6040 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
6042 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6044 tree friend_type = t;
6045 bool adjust_processing_template_decl = false;
6047 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6049 /* template <class T> friend class C; */
6050 friend_type = tsubst_friend_class (friend_type, args);
6051 adjust_processing_template_decl = true;
6053 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6055 /* template <class T> friend class C::D; */
6056 friend_type = tsubst (friend_type, args,
6057 tf_warning_or_error, NULL_TREE);
6058 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6059 friend_type = TREE_TYPE (friend_type);
6060 adjust_processing_template_decl = true;
6062 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6064 /* This could be either
6066 friend class T::C;
6068 when dependent_type_p is false or
6070 template <class U> friend class T::C;
6072 otherwise. */
6073 friend_type = tsubst (friend_type, args,
6074 tf_warning_or_error, NULL_TREE);
6075 /* Bump processing_template_decl for correct
6076 dependent_type_p calculation. */
6077 ++processing_template_decl;
6078 if (dependent_type_p (friend_type))
6079 adjust_processing_template_decl = true;
6080 --processing_template_decl;
6082 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6083 && hidden_name_p (TYPE_NAME (friend_type)))
6085 /* friend class C;
6087 where C hasn't been declared yet. Let's lookup name
6088 from namespace scope directly, bypassing any name that
6089 come from dependent base class. */
6090 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6092 /* The call to xref_tag_from_type does injection for friend
6093 classes. */
6094 push_nested_namespace (ns);
6095 friend_type =
6096 xref_tag_from_type (friend_type, NULL_TREE,
6097 /*tag_scope=*/ts_current);
6098 pop_nested_namespace (ns);
6100 else if (uses_template_parms (friend_type))
6101 /* friend class C<T>; */
6102 friend_type = tsubst (friend_type, args,
6103 tf_warning_or_error, NULL_TREE);
6104 /* Otherwise it's
6106 friend class C;
6108 where C is already declared or
6110 friend class C<int>;
6112 We don't have to do anything in these cases. */
6114 if (adjust_processing_template_decl)
6115 /* Trick make_friend_class into realizing that the friend
6116 we're adding is a template, not an ordinary class. It's
6117 important that we use make_friend_class since it will
6118 perform some error-checking and output cross-reference
6119 information. */
6120 ++processing_template_decl;
6122 if (friend_type != error_mark_node)
6123 make_friend_class (type, friend_type, /*complain=*/false);
6125 if (adjust_processing_template_decl)
6126 --processing_template_decl;
6128 else
6130 /* Build new DECL_FRIENDLIST. */
6131 tree r;
6133 /* The the file and line for this declaration, to
6134 assist in error message reporting. Since we
6135 called push_tinst_level above, we don't need to
6136 restore these. */
6137 input_location = DECL_SOURCE_LOCATION (t);
6139 if (TREE_CODE (t) == TEMPLATE_DECL)
6141 ++processing_template_decl;
6142 push_deferring_access_checks (dk_no_check);
6145 r = tsubst_friend_function (t, args);
6146 add_friend (type, r, /*complain=*/false);
6147 if (TREE_CODE (t) == TEMPLATE_DECL)
6149 pop_deferring_access_checks ();
6150 --processing_template_decl;
6156 /* Set the file and line number information to whatever is given for
6157 the class itself. This puts error messages involving generated
6158 implicit functions at a predictable point, and the same point
6159 that would be used for non-template classes. */
6160 input_location = DECL_SOURCE_LOCATION (typedecl);
6162 unreverse_member_declarations (type);
6163 finish_struct_1 (type);
6164 TYPE_BEING_DEFINED (type) = 0;
6166 /* Now that the class is complete, instantiate default arguments for
6167 any member functions. We don't do this earlier because the
6168 default arguments may reference members of the class. */
6169 if (!PRIMARY_TEMPLATE_P (template))
6170 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6171 if (TREE_CODE (t) == FUNCTION_DECL
6172 /* Implicitly generated member functions will not have template
6173 information; they are not instantiations, but instead are
6174 created "fresh" for each instantiation. */
6175 && DECL_TEMPLATE_INFO (t))
6176 tsubst_default_arguments (t);
6178 popclass ();
6179 pop_from_top_level ();
6180 pop_deferring_access_checks ();
6181 pop_tinst_level ();
6183 /* The vtable for a template class can be emitted in any translation
6184 unit in which the class is instantiated. When there is no key
6185 method, however, finish_struct_1 will already have added TYPE to
6186 the keyed_classes list. */
6187 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6188 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6190 return type;
6193 static tree
6194 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6196 tree r;
6198 if (!t)
6199 r = t;
6200 else if (TYPE_P (t))
6201 r = tsubst (t, args, complain, in_decl);
6202 else
6204 r = tsubst_expr (t, args, complain, in_decl,
6205 /*integral_constant_expression_p=*/true);
6206 r = fold_non_dependent_expr (r);
6208 return r;
6211 /* Substitute ARGS into the vector or list of template arguments T. */
6213 static tree
6214 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6216 int len = TREE_VEC_LENGTH (t);
6217 int need_new = 0, i;
6218 tree *elts = (tree *) alloca (len * sizeof (tree));
6220 for (i = 0; i < len; i++)
6222 tree orig_arg = TREE_VEC_ELT (t, i);
6223 tree new_arg;
6225 if (TREE_CODE (orig_arg) == TREE_VEC)
6226 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
6227 else
6228 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
6230 if (new_arg == error_mark_node)
6231 return error_mark_node;
6233 elts[i] = new_arg;
6234 if (new_arg != orig_arg)
6235 need_new = 1;
6238 if (!need_new)
6239 return t;
6241 t = make_tree_vec (len);
6242 for (i = 0; i < len; i++)
6243 TREE_VEC_ELT (t, i) = elts[i];
6245 return t;
6248 /* Return the result of substituting ARGS into the template parameters
6249 given by PARMS. If there are m levels of ARGS and m + n levels of
6250 PARMS, then the result will contain n levels of PARMS. For
6251 example, if PARMS is `template <class T> template <class U>
6252 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6253 result will be `template <int*, double, class V>'. */
6255 static tree
6256 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6258 tree r = NULL_TREE;
6259 tree* new_parms;
6261 /* When substituting into a template, we must set
6262 PROCESSING_TEMPLATE_DECL as the template parameters may be
6263 dependent if they are based on one-another, and the dependency
6264 predicates are short-circuit outside of templates. */
6265 ++processing_template_decl;
6267 for (new_parms = &r;
6268 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6269 new_parms = &(TREE_CHAIN (*new_parms)),
6270 parms = TREE_CHAIN (parms))
6272 tree new_vec =
6273 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6274 int i;
6276 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6278 tree tuple;
6279 tree default_value;
6280 tree parm_decl;
6282 if (parms == error_mark_node)
6283 continue;
6285 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6287 if (tuple == error_mark_node)
6288 continue;
6290 default_value = TREE_PURPOSE (tuple);
6291 parm_decl = TREE_VALUE (tuple);
6293 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6294 if (TREE_CODE (parm_decl) == PARM_DECL
6295 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6296 parm_decl = error_mark_node;
6297 default_value = tsubst_template_arg (default_value, args,
6298 complain, NULL_TREE);
6300 tuple = build_tree_list (default_value, parm_decl);
6301 TREE_VEC_ELT (new_vec, i) = tuple;
6304 *new_parms =
6305 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6306 - TMPL_ARGS_DEPTH (args)),
6307 new_vec, NULL_TREE);
6310 --processing_template_decl;
6312 return r;
6315 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6316 type T. If T is not an aggregate or enumeration type, it is
6317 handled as if by tsubst. IN_DECL is as for tsubst. If
6318 ENTERING_SCOPE is nonzero, T is the context for a template which
6319 we are presently tsubst'ing. Return the substituted value. */
6321 static tree
6322 tsubst_aggr_type (tree t,
6323 tree args,
6324 tsubst_flags_t complain,
6325 tree in_decl,
6326 int entering_scope)
6328 if (t == NULL_TREE)
6329 return NULL_TREE;
6331 switch (TREE_CODE (t))
6333 case RECORD_TYPE:
6334 if (TYPE_PTRMEMFUNC_P (t))
6335 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6337 /* Else fall through. */
6338 case ENUMERAL_TYPE:
6339 case UNION_TYPE:
6340 if (TYPE_TEMPLATE_INFO (t))
6342 tree argvec;
6343 tree context;
6344 tree r;
6345 bool saved_skip_evaluation;
6347 /* In "sizeof(X<I>)" we need to evaluate "I". */
6348 saved_skip_evaluation = skip_evaluation;
6349 skip_evaluation = false;
6351 /* First, determine the context for the type we are looking
6352 up. */
6353 context = TYPE_CONTEXT (t);
6354 if (context)
6355 context = tsubst_aggr_type (context, args, complain,
6356 in_decl, /*entering_scope=*/1);
6358 /* Then, figure out what arguments are appropriate for the
6359 type we are trying to find. For example, given:
6361 template <class T> struct S;
6362 template <class T, class U> void f(T, U) { S<U> su; }
6364 and supposing that we are instantiating f<int, double>,
6365 then our ARGS will be {int, double}, but, when looking up
6366 S we only want {double}. */
6367 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6368 complain, in_decl);
6369 if (argvec == error_mark_node)
6370 r = error_mark_node;
6371 else
6373 r = lookup_template_class (t, argvec, in_decl, context,
6374 entering_scope, complain);
6375 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6378 skip_evaluation = saved_skip_evaluation;
6380 return r;
6382 else
6383 /* This is not a template type, so there's nothing to do. */
6384 return t;
6386 default:
6387 return tsubst (t, args, complain, in_decl);
6391 /* Substitute into the default argument ARG (a default argument for
6392 FN), which has the indicated TYPE. */
6394 tree
6395 tsubst_default_argument (tree fn, tree type, tree arg)
6397 tree saved_class_ptr = NULL_TREE;
6398 tree saved_class_ref = NULL_TREE;
6400 /* This default argument came from a template. Instantiate the
6401 default argument here, not in tsubst. In the case of
6402 something like:
6404 template <class T>
6405 struct S {
6406 static T t();
6407 void f(T = t());
6410 we must be careful to do name lookup in the scope of S<T>,
6411 rather than in the current class. */
6412 push_access_scope (fn);
6413 /* The "this" pointer is not valid in a default argument. */
6414 if (cfun)
6416 saved_class_ptr = current_class_ptr;
6417 cp_function_chain->x_current_class_ptr = NULL_TREE;
6418 saved_class_ref = current_class_ref;
6419 cp_function_chain->x_current_class_ref = NULL_TREE;
6422 push_deferring_access_checks(dk_no_deferred);
6423 /* The default argument expression may cause implicitly defined
6424 member functions to be synthesized, which will result in garbage
6425 collection. We must treat this situation as if we were within
6426 the body of function so as to avoid collecting live data on the
6427 stack. */
6428 ++function_depth;
6429 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6430 tf_warning_or_error, NULL_TREE,
6431 /*integral_constant_expression_p=*/false);
6432 --function_depth;
6433 pop_deferring_access_checks();
6435 /* Restore the "this" pointer. */
6436 if (cfun)
6438 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6439 cp_function_chain->x_current_class_ref = saved_class_ref;
6442 pop_access_scope (fn);
6444 /* Make sure the default argument is reasonable. */
6445 arg = check_default_argument (type, arg);
6447 return arg;
6450 /* Substitute into all the default arguments for FN. */
6452 static void
6453 tsubst_default_arguments (tree fn)
6455 tree arg;
6456 tree tmpl_args;
6458 tmpl_args = DECL_TI_ARGS (fn);
6460 /* If this function is not yet instantiated, we certainly don't need
6461 its default arguments. */
6462 if (uses_template_parms (tmpl_args))
6463 return;
6465 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6466 arg;
6467 arg = TREE_CHAIN (arg))
6468 if (TREE_PURPOSE (arg))
6469 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6470 TREE_VALUE (arg),
6471 TREE_PURPOSE (arg));
6474 /* Substitute the ARGS into the T, which is a _DECL. Return the
6475 result of the substitution. Issue error and warning messages under
6476 control of COMPLAIN. */
6478 static tree
6479 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6481 location_t saved_loc;
6482 tree r = NULL_TREE;
6483 tree in_decl = t;
6485 /* Set the filename and linenumber to improve error-reporting. */
6486 saved_loc = input_location;
6487 input_location = DECL_SOURCE_LOCATION (t);
6489 switch (TREE_CODE (t))
6491 case TEMPLATE_DECL:
6493 /* We can get here when processing a member function template,
6494 member class template, and template template parameter of
6495 a template class. */
6496 tree decl = DECL_TEMPLATE_RESULT (t);
6497 tree spec;
6498 tree tmpl_args;
6499 tree full_args;
6501 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6503 /* Template template parameter is treated here. */
6504 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6505 if (new_type == error_mark_node)
6506 return error_mark_node;
6508 r = copy_decl (t);
6509 TREE_CHAIN (r) = NULL_TREE;
6510 TREE_TYPE (r) = new_type;
6511 DECL_TEMPLATE_RESULT (r)
6512 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6513 DECL_TEMPLATE_PARMS (r)
6514 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6515 complain);
6516 TYPE_NAME (new_type) = r;
6517 break;
6520 /* We might already have an instance of this template.
6521 The ARGS are for the surrounding class type, so the
6522 full args contain the tsubst'd args for the context,
6523 plus the innermost args from the template decl. */
6524 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6525 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6526 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6527 /* Because this is a template, the arguments will still be
6528 dependent, even after substitution. If
6529 PROCESSING_TEMPLATE_DECL is not set, the dependency
6530 predicates will short-circuit. */
6531 ++processing_template_decl;
6532 full_args = tsubst_template_args (tmpl_args, args,
6533 complain, in_decl);
6534 --processing_template_decl;
6535 if (full_args == error_mark_node)
6536 return error_mark_node;
6538 /* tsubst_template_args doesn't copy the vector if
6539 nothing changed. But, *something* should have
6540 changed. */
6541 gcc_assert (full_args != tmpl_args);
6543 spec = retrieve_specialization (t, full_args,
6544 /*class_specializations_p=*/true);
6545 if (spec != NULL_TREE)
6547 r = spec;
6548 break;
6551 /* Make a new template decl. It will be similar to the
6552 original, but will record the current template arguments.
6553 We also create a new function declaration, which is just
6554 like the old one, but points to this new template, rather
6555 than the old one. */
6556 r = copy_decl (t);
6557 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6558 TREE_CHAIN (r) = NULL_TREE;
6560 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6562 if (TREE_CODE (decl) == TYPE_DECL)
6564 tree new_type;
6565 ++processing_template_decl;
6566 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6567 --processing_template_decl;
6568 if (new_type == error_mark_node)
6569 return error_mark_node;
6571 TREE_TYPE (r) = new_type;
6572 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6573 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6574 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6575 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6577 else
6579 tree new_decl;
6580 ++processing_template_decl;
6581 new_decl = tsubst (decl, args, complain, in_decl);
6582 --processing_template_decl;
6583 if (new_decl == error_mark_node)
6584 return error_mark_node;
6586 DECL_TEMPLATE_RESULT (r) = new_decl;
6587 DECL_TI_TEMPLATE (new_decl) = r;
6588 TREE_TYPE (r) = TREE_TYPE (new_decl);
6589 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6590 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6593 SET_DECL_IMPLICIT_INSTANTIATION (r);
6594 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6595 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6597 /* The template parameters for this new template are all the
6598 template parameters for the old template, except the
6599 outermost level of parameters. */
6600 DECL_TEMPLATE_PARMS (r)
6601 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6602 complain);
6604 if (PRIMARY_TEMPLATE_P (t))
6605 DECL_PRIMARY_TEMPLATE (r) = r;
6607 if (TREE_CODE (decl) != TYPE_DECL)
6608 /* Record this non-type partial instantiation. */
6609 register_specialization (r, t,
6610 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6611 false);
6613 break;
6615 case FUNCTION_DECL:
6617 tree ctx;
6618 tree argvec = NULL_TREE;
6619 tree *friends;
6620 tree gen_tmpl;
6621 tree type;
6622 int member;
6623 int args_depth;
6624 int parms_depth;
6626 /* Nobody should be tsubst'ing into non-template functions. */
6627 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6629 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6631 tree spec;
6632 bool dependent_p;
6634 /* If T is not dependent, just return it. We have to
6635 increment PROCESSING_TEMPLATE_DECL because
6636 value_dependent_expression_p assumes that nothing is
6637 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6638 ++processing_template_decl;
6639 dependent_p = value_dependent_expression_p (t);
6640 --processing_template_decl;
6641 if (!dependent_p)
6642 return t;
6644 /* Calculate the most general template of which R is a
6645 specialization, and the complete set of arguments used to
6646 specialize R. */
6647 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6648 argvec = tsubst_template_args (DECL_TI_ARGS
6649 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6650 args, complain, in_decl);
6652 /* Check to see if we already have this specialization. */
6653 spec = retrieve_specialization (gen_tmpl, argvec,
6654 /*class_specializations_p=*/false);
6656 if (spec)
6658 r = spec;
6659 break;
6662 /* We can see more levels of arguments than parameters if
6663 there was a specialization of a member template, like
6664 this:
6666 template <class T> struct S { template <class U> void f(); }
6667 template <> template <class U> void S<int>::f(U);
6669 Here, we'll be substituting into the specialization,
6670 because that's where we can find the code we actually
6671 want to generate, but we'll have enough arguments for
6672 the most general template.
6674 We also deal with the peculiar case:
6676 template <class T> struct S {
6677 template <class U> friend void f();
6679 template <class U> void f() {}
6680 template S<int>;
6681 template void f<double>();
6683 Here, the ARGS for the instantiation of will be {int,
6684 double}. But, we only need as many ARGS as there are
6685 levels of template parameters in CODE_PATTERN. We are
6686 careful not to get fooled into reducing the ARGS in
6687 situations like:
6689 template <class T> struct S { template <class U> void f(U); }
6690 template <class T> template <> void S<T>::f(int) {}
6692 which we can spot because the pattern will be a
6693 specialization in this case. */
6694 args_depth = TMPL_ARGS_DEPTH (args);
6695 parms_depth =
6696 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6697 if (args_depth > parms_depth
6698 && !DECL_TEMPLATE_SPECIALIZATION (t))
6699 args = get_innermost_template_args (args, parms_depth);
6701 else
6703 /* This special case arises when we have something like this:
6705 template <class T> struct S {
6706 friend void f<int>(int, double);
6709 Here, the DECL_TI_TEMPLATE for the friend declaration
6710 will be an IDENTIFIER_NODE. We are being called from
6711 tsubst_friend_function, and we want only to create a
6712 new decl (R) with appropriate types so that we can call
6713 determine_specialization. */
6714 gen_tmpl = NULL_TREE;
6717 if (DECL_CLASS_SCOPE_P (t))
6719 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6720 member = 2;
6721 else
6722 member = 1;
6723 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6724 complain, t, /*entering_scope=*/1);
6726 else
6728 member = 0;
6729 ctx = DECL_CONTEXT (t);
6731 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6732 if (type == error_mark_node)
6733 return error_mark_node;
6735 /* We do NOT check for matching decls pushed separately at this
6736 point, as they may not represent instantiations of this
6737 template, and in any case are considered separate under the
6738 discrete model. */
6739 r = copy_decl (t);
6740 DECL_USE_TEMPLATE (r) = 0;
6741 TREE_TYPE (r) = type;
6742 /* Clear out the mangled name and RTL for the instantiation. */
6743 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6744 SET_DECL_RTL (r, NULL_RTX);
6745 DECL_INITIAL (r) = NULL_TREE;
6746 DECL_CONTEXT (r) = ctx;
6748 if (member && DECL_CONV_FN_P (r))
6749 /* Type-conversion operator. Reconstruct the name, in
6750 case it's the name of one of the template's parameters. */
6751 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6753 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6754 complain, t);
6755 DECL_RESULT (r) = NULL_TREE;
6757 TREE_STATIC (r) = 0;
6758 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6759 DECL_EXTERNAL (r) = 1;
6760 /* If this is an instantiation of a function with internal
6761 linkage, we already know what object file linkage will be
6762 assigned to the instantiation. */
6763 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6764 DECL_DEFER_OUTPUT (r) = 0;
6765 TREE_CHAIN (r) = NULL_TREE;
6766 DECL_PENDING_INLINE_INFO (r) = 0;
6767 DECL_PENDING_INLINE_P (r) = 0;
6768 DECL_SAVED_TREE (r) = NULL_TREE;
6769 TREE_USED (r) = 0;
6770 if (DECL_CLONED_FUNCTION (r))
6772 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6773 args, complain, t);
6774 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6775 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6778 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6779 this in the special friend case mentioned above where
6780 GEN_TMPL is NULL. */
6781 if (gen_tmpl)
6783 DECL_TEMPLATE_INFO (r)
6784 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6785 SET_DECL_IMPLICIT_INSTANTIATION (r);
6786 register_specialization (r, gen_tmpl, argvec, false);
6788 /* We're not supposed to instantiate default arguments
6789 until they are called, for a template. But, for a
6790 declaration like:
6792 template <class T> void f ()
6793 { extern void g(int i = T()); }
6795 we should do the substitution when the template is
6796 instantiated. We handle the member function case in
6797 instantiate_class_template since the default arguments
6798 might refer to other members of the class. */
6799 if (!member
6800 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6801 && !uses_template_parms (argvec))
6802 tsubst_default_arguments (r);
6804 else
6805 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6807 /* Copy the list of befriending classes. */
6808 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6809 *friends;
6810 friends = &TREE_CHAIN (*friends))
6812 *friends = copy_node (*friends);
6813 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6814 args, complain,
6815 in_decl);
6818 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6820 maybe_retrofit_in_chrg (r);
6821 if (DECL_CONSTRUCTOR_P (r))
6822 grok_ctor_properties (ctx, r);
6823 /* If this is an instantiation of a member template, clone it.
6824 If it isn't, that'll be handled by
6825 clone_constructors_and_destructors. */
6826 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6827 clone_function_decl (r, /*update_method_vec_p=*/0);
6829 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6830 && !grok_op_properties (r, (complain & tf_error) != 0))
6831 return error_mark_node;
6833 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6834 SET_DECL_FRIEND_CONTEXT (r,
6835 tsubst (DECL_FRIEND_CONTEXT (t),
6836 args, complain, in_decl));
6838 /* Possibly limit visibility based on template args. */
6839 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
6840 if (DECL_VISIBILITY_SPECIFIED (t))
6842 DECL_VISIBILITY_SPECIFIED (r) = 0;
6843 DECL_ATTRIBUTES (r)
6844 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6846 determine_visibility (r);
6848 break;
6850 case PARM_DECL:
6852 tree type;
6854 r = copy_node (t);
6855 if (DECL_TEMPLATE_PARM_P (t))
6856 SET_DECL_TEMPLATE_PARM_P (r);
6858 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6859 type = type_decays_to (type);
6860 TREE_TYPE (r) = type;
6861 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6863 if (DECL_INITIAL (r))
6865 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6866 DECL_INITIAL (r) = TREE_TYPE (r);
6867 else
6868 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6869 complain, in_decl);
6872 DECL_CONTEXT (r) = NULL_TREE;
6874 if (!DECL_TEMPLATE_PARM_P (r))
6875 DECL_ARG_TYPE (r) = type_passed_as (type);
6876 if (TREE_CHAIN (t))
6877 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6878 complain, TREE_CHAIN (t));
6880 break;
6882 case FIELD_DECL:
6884 tree type;
6886 r = copy_decl (t);
6887 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6888 if (type == error_mark_node)
6889 return error_mark_node;
6890 TREE_TYPE (r) = type;
6891 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6893 /* DECL_INITIAL gives the number of bits in a bit-field. */
6894 DECL_INITIAL (r)
6895 = tsubst_expr (DECL_INITIAL (t), args,
6896 complain, in_decl,
6897 /*integral_constant_expression_p=*/true);
6898 /* We don't have to set DECL_CONTEXT here; it is set by
6899 finish_member_declaration. */
6900 TREE_CHAIN (r) = NULL_TREE;
6901 if (VOID_TYPE_P (type))
6902 error ("instantiation of %q+D as type %qT", r, type);
6904 break;
6906 case USING_DECL:
6907 /* We reach here only for member using decls. */
6908 if (DECL_DEPENDENT_P (t))
6910 r = do_class_using_decl
6911 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6912 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6913 if (!r)
6914 r = error_mark_node;
6916 else
6918 r = copy_node (t);
6919 TREE_CHAIN (r) = NULL_TREE;
6921 break;
6923 case TYPE_DECL:
6924 case VAR_DECL:
6926 tree argvec = NULL_TREE;
6927 tree gen_tmpl = NULL_TREE;
6928 tree spec;
6929 tree tmpl = NULL_TREE;
6930 tree ctx;
6931 tree type = NULL_TREE;
6932 bool local_p;
6934 if (TREE_CODE (t) == TYPE_DECL)
6936 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6937 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6938 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6940 /* If this is the canonical decl, we don't have to
6941 mess with instantiations, and often we can't (for
6942 typename, template type parms and such). Note that
6943 TYPE_NAME is not correct for the above test if
6944 we've copied the type for a typedef. */
6945 r = TYPE_NAME (type);
6946 break;
6950 /* Check to see if we already have the specialization we
6951 need. */
6952 spec = NULL_TREE;
6953 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6955 /* T is a static data member or namespace-scope entity.
6956 We have to substitute into namespace-scope variables
6957 (even though such entities are never templates) because
6958 of cases like:
6960 template <class T> void f() { extern T t; }
6962 where the entity referenced is not known until
6963 instantiation time. */
6964 local_p = false;
6965 ctx = DECL_CONTEXT (t);
6966 if (DECL_CLASS_SCOPE_P (t))
6968 ctx = tsubst_aggr_type (ctx, args,
6969 complain,
6970 in_decl, /*entering_scope=*/1);
6971 /* If CTX is unchanged, then T is in fact the
6972 specialization we want. That situation occurs when
6973 referencing a static data member within in its own
6974 class. We can use pointer equality, rather than
6975 same_type_p, because DECL_CONTEXT is always
6976 canonical. */
6977 if (ctx == DECL_CONTEXT (t))
6978 spec = t;
6981 if (!spec)
6983 tmpl = DECL_TI_TEMPLATE (t);
6984 gen_tmpl = most_general_template (tmpl);
6985 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6986 spec = (retrieve_specialization
6987 (gen_tmpl, argvec,
6988 /*class_specializations_p=*/false));
6991 else
6993 /* A local variable. */
6994 local_p = true;
6995 /* Subsequent calls to pushdecl will fill this in. */
6996 ctx = NULL_TREE;
6997 spec = retrieve_local_specialization (t);
6999 /* If we already have the specialization we need, there is
7000 nothing more to do. */
7001 if (spec)
7003 r = spec;
7004 break;
7007 /* Create a new node for the specialization we need. */
7008 r = copy_decl (t);
7009 if (TREE_CODE (r) == VAR_DECL)
7011 /* Even if the original location is out of scope, the
7012 newly substituted one is not. */
7013 DECL_DEAD_FOR_LOCAL (r) = 0;
7014 DECL_INITIALIZED_P (r) = 0;
7015 DECL_TEMPLATE_INSTANTIATED (r) = 0;
7016 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7017 if (type == error_mark_node)
7018 return error_mark_node;
7019 if (TREE_CODE (type) == FUNCTION_TYPE)
7021 /* It may seem that this case cannot occur, since:
7023 typedef void f();
7024 void g() { f x; }
7026 declares a function, not a variable. However:
7028 typedef void f();
7029 template <typename T> void g() { T t; }
7030 template void g<f>();
7032 is an attempt to declare a variable with function
7033 type. */
7034 error ("variable %qD has function type",
7035 /* R is not yet sufficiently initialized, so we
7036 just use its name. */
7037 DECL_NAME (r));
7038 return error_mark_node;
7040 type = complete_type (type);
7041 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
7042 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
7043 type = check_var_type (DECL_NAME (r), type);
7045 if (DECL_HAS_VALUE_EXPR_P (t))
7047 tree ve = DECL_VALUE_EXPR (t);
7048 ve = tsubst_expr (ve, args, complain, in_decl,
7049 /*constant_expression_p=*/false);
7050 SET_DECL_VALUE_EXPR (r, ve);
7053 else if (DECL_SELF_REFERENCE_P (t))
7054 SET_DECL_SELF_REFERENCE_P (r);
7055 TREE_TYPE (r) = type;
7056 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7057 DECL_CONTEXT (r) = ctx;
7058 /* Clear out the mangled name and RTL for the instantiation. */
7059 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7060 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7061 SET_DECL_RTL (r, NULL_RTX);
7062 /* The initializer must not be expanded until it is required;
7063 see [temp.inst]. */
7064 DECL_INITIAL (r) = NULL_TREE;
7065 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7066 SET_DECL_RTL (r, NULL_RTX);
7067 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
7068 if (TREE_CODE (r) == VAR_DECL)
7070 /* Possibly limit visibility based on template args. */
7071 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7072 if (DECL_VISIBILITY_SPECIFIED (t))
7074 DECL_VISIBILITY_SPECIFIED (r) = 0;
7075 DECL_ATTRIBUTES (r)
7076 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7078 determine_visibility (r);
7081 if (!local_p)
7083 /* A static data member declaration is always marked
7084 external when it is declared in-class, even if an
7085 initializer is present. We mimic the non-template
7086 processing here. */
7087 DECL_EXTERNAL (r) = 1;
7089 register_specialization (r, gen_tmpl, argvec, false);
7090 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
7091 SET_DECL_IMPLICIT_INSTANTIATION (r);
7093 else
7094 register_local_specialization (r, t);
7096 TREE_CHAIN (r) = NULL_TREE;
7097 layout_decl (r, 0);
7099 break;
7101 default:
7102 gcc_unreachable ();
7105 /* Restore the file and line information. */
7106 input_location = saved_loc;
7108 return r;
7111 /* Substitute into the ARG_TYPES of a function type. */
7113 static tree
7114 tsubst_arg_types (tree arg_types,
7115 tree args,
7116 tsubst_flags_t complain,
7117 tree in_decl)
7119 tree remaining_arg_types;
7120 tree type;
7121 tree default_arg;
7122 tree result = NULL_TREE;
7124 if (!arg_types || arg_types == void_list_node)
7125 return arg_types;
7127 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
7128 args, complain, in_decl);
7129 if (remaining_arg_types == error_mark_node)
7130 return error_mark_node;
7132 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
7133 if (type == error_mark_node)
7134 return error_mark_node;
7135 if (VOID_TYPE_P (type))
7137 if (complain & tf_error)
7139 error ("invalid parameter type %qT", type);
7140 if (in_decl)
7141 error ("in declaration %q+D", in_decl);
7143 return error_mark_node;
7146 /* Do array-to-pointer, function-to-pointer conversion, and ignore
7147 top-level qualifiers as required. */
7148 type = TYPE_MAIN_VARIANT (type_decays_to (type));
7150 /* We do not substitute into default arguments here. The standard
7151 mandates that they be instantiated only when needed, which is
7152 done in build_over_call. */
7153 default_arg = TREE_PURPOSE (arg_types);
7155 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
7157 /* We've instantiated a template before its default arguments
7158 have been parsed. This can happen for a nested template
7159 class, and is not an error unless we require the default
7160 argument in a call of this function. */
7161 result = tree_cons (default_arg, type, remaining_arg_types);
7162 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
7164 else
7165 result = hash_tree_cons (default_arg, type, remaining_arg_types);
7167 return result;
7170 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7171 *not* handle the exception-specification for FNTYPE, because the
7172 initial substitution of explicitly provided template parameters
7173 during argument deduction forbids substitution into the
7174 exception-specification:
7176 [temp.deduct]
7178 All references in the function type of the function template to the
7179 corresponding template parameters are replaced by the specified tem-
7180 plate argument values. If a substitution in a template parameter or
7181 in the function type of the function template results in an invalid
7182 type, type deduction fails. [Note: The equivalent substitution in
7183 exception specifications is done only when the function is instanti-
7184 ated, at which point a program is ill-formed if the substitution
7185 results in an invalid type.] */
7187 static tree
7188 tsubst_function_type (tree t,
7189 tree args,
7190 tsubst_flags_t complain,
7191 tree in_decl)
7193 tree return_type;
7194 tree arg_types;
7195 tree fntype;
7197 /* The TYPE_CONTEXT is not used for function/method types. */
7198 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
7200 /* Substitute the return type. */
7201 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7202 if (return_type == error_mark_node)
7203 return error_mark_node;
7204 /* The standard does not presently indicate that creation of a
7205 function type with an invalid return type is a deduction failure.
7206 However, that is clearly analogous to creating an array of "void"
7207 or a reference to a reference. This is core issue #486. */
7208 if (TREE_CODE (return_type) == ARRAY_TYPE
7209 || TREE_CODE (return_type) == FUNCTION_TYPE)
7211 if (complain & tf_error)
7213 if (TREE_CODE (return_type) == ARRAY_TYPE)
7214 error ("function returning an array");
7215 else
7216 error ("function returning a function");
7218 return error_mark_node;
7221 /* Substitute the argument types. */
7222 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
7223 complain, in_decl);
7224 if (arg_types == error_mark_node)
7225 return error_mark_node;
7227 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
7228 && in_decl != NULL_TREE
7229 && !TREE_NO_WARNING (in_decl)
7230 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
7231 warning (OPT_Wreturn_type,
7232 "type qualifiers ignored on function return type");
7234 /* Construct a new type node and return it. */
7235 if (TREE_CODE (t) == FUNCTION_TYPE)
7236 fntype = build_function_type (return_type, arg_types);
7237 else
7239 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7240 if (! IS_AGGR_TYPE (r))
7242 /* [temp.deduct]
7244 Type deduction may fail for any of the following
7245 reasons:
7247 -- Attempting to create "pointer to member of T" when T
7248 is not a class type. */
7249 if (complain & tf_error)
7250 error ("creating pointer to member function of non-class type %qT",
7252 return error_mark_node;
7255 fntype = build_method_type_directly (r, return_type,
7256 TREE_CHAIN (arg_types));
7258 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
7259 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
7261 return fntype;
7264 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7265 ARGS into that specification, and return the substituted
7266 specification. If there is no specification, return NULL_TREE. */
7268 static tree
7269 tsubst_exception_specification (tree fntype,
7270 tree args,
7271 tsubst_flags_t complain,
7272 tree in_decl)
7274 tree specs;
7275 tree new_specs;
7277 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7278 new_specs = NULL_TREE;
7279 if (specs)
7281 if (! TREE_VALUE (specs))
7282 new_specs = specs;
7283 else
7284 while (specs)
7286 tree spec;
7287 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7288 if (spec == error_mark_node)
7289 return spec;
7290 new_specs = add_exception_specifier (new_specs, spec, complain);
7291 specs = TREE_CHAIN (specs);
7294 return new_specs;
7297 /* Take the tree structure T and replace template parameters used
7298 therein with the argument vector ARGS. IN_DECL is an associated
7299 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
7300 Issue error and warning messages under control of COMPLAIN. Note
7301 that we must be relatively non-tolerant of extensions here, in
7302 order to preserve conformance; if we allow substitutions that
7303 should not be allowed, we may allow argument deductions that should
7304 not succeed, and therefore report ambiguous overload situations
7305 where there are none. In theory, we could allow the substitution,
7306 but indicate that it should have failed, and allow our caller to
7307 make sure that the right thing happens, but we don't try to do this
7308 yet.
7310 This function is used for dealing with types, decls and the like;
7311 for expressions, use tsubst_expr or tsubst_copy. */
7313 static tree
7314 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7316 tree type, r;
7318 if (t == NULL_TREE || t == error_mark_node
7319 || t == integer_type_node
7320 || t == void_type_node
7321 || t == char_type_node
7322 || t == unknown_type_node
7323 || TREE_CODE (t) == NAMESPACE_DECL)
7324 return t;
7326 if (DECL_P (t))
7327 return tsubst_decl (t, args, complain);
7329 if (TREE_CODE (t) == IDENTIFIER_NODE)
7330 type = IDENTIFIER_TYPE_VALUE (t);
7331 else
7332 type = TREE_TYPE (t);
7334 gcc_assert (type != unknown_type_node);
7336 if (type
7337 && TREE_CODE (t) != TYPENAME_TYPE
7338 && TREE_CODE (t) != IDENTIFIER_NODE
7339 && TREE_CODE (t) != FUNCTION_TYPE
7340 && TREE_CODE (t) != METHOD_TYPE)
7341 type = tsubst (type, args, complain, in_decl);
7342 if (type == error_mark_node)
7343 return error_mark_node;
7345 switch (TREE_CODE (t))
7347 case RECORD_TYPE:
7348 case UNION_TYPE:
7349 case ENUMERAL_TYPE:
7350 return tsubst_aggr_type (t, args, complain, in_decl,
7351 /*entering_scope=*/0);
7353 case ERROR_MARK:
7354 case IDENTIFIER_NODE:
7355 case VOID_TYPE:
7356 case REAL_TYPE:
7357 case COMPLEX_TYPE:
7358 case VECTOR_TYPE:
7359 case BOOLEAN_TYPE:
7360 case INTEGER_CST:
7361 case REAL_CST:
7362 case STRING_CST:
7363 return t;
7365 case INTEGER_TYPE:
7366 if (t == integer_type_node)
7367 return t;
7369 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7370 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7371 return t;
7374 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7376 max = tsubst_expr (omax, args, complain, in_decl,
7377 /*integral_constant_expression_p=*/false);
7378 max = fold_decl_constant_value (max);
7380 if (TREE_CODE (max) != INTEGER_CST
7381 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7382 && !at_function_scope_p ())
7384 if (complain & tf_error)
7385 error ("array bound is not an integer constant");
7386 return error_mark_node;
7389 /* [temp.deduct]
7391 Type deduction may fail for any of the following
7392 reasons:
7394 Attempting to create an array with a size that is
7395 zero or negative. */
7396 if (integer_zerop (max) && !(complain & tf_error))
7397 /* We must fail if performing argument deduction (as
7398 indicated by the state of complain), so that
7399 another substitution can be found. */
7400 return error_mark_node;
7401 else if (TREE_CODE (max) == INTEGER_CST
7402 && INT_CST_LT (max, integer_zero_node))
7404 if (complain & tf_error)
7405 error ("creating array with negative size (%qE)", max);
7407 return error_mark_node;
7410 return compute_array_index_type (NULL_TREE, max);
7413 case TEMPLATE_TYPE_PARM:
7414 case TEMPLATE_TEMPLATE_PARM:
7415 case BOUND_TEMPLATE_TEMPLATE_PARM:
7416 case TEMPLATE_PARM_INDEX:
7418 int idx;
7419 int level;
7420 int levels;
7421 tree arg = NULL_TREE;
7423 r = NULL_TREE;
7425 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7426 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7427 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7428 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7430 idx = TEMPLATE_TYPE_IDX (t);
7431 level = TEMPLATE_TYPE_LEVEL (t);
7433 else
7435 idx = TEMPLATE_PARM_IDX (t);
7436 level = TEMPLATE_PARM_LEVEL (t);
7439 levels = TMPL_ARGS_DEPTH (args);
7440 if (level <= levels)
7441 arg = TMPL_ARG (args, level, idx);
7443 if (arg == error_mark_node)
7444 return error_mark_node;
7445 else if (arg != NULL_TREE)
7447 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7449 int quals;
7450 gcc_assert (TYPE_P (arg));
7452 /* cv-quals from the template are discarded when
7453 substituting in a function or reference type. */
7454 if (TREE_CODE (arg) == FUNCTION_TYPE
7455 || TREE_CODE (arg) == METHOD_TYPE
7456 || TREE_CODE (arg) == REFERENCE_TYPE)
7457 quals = cp_type_quals (arg);
7458 else
7459 quals = cp_type_quals (arg) | cp_type_quals (t);
7461 return cp_build_qualified_type_real
7462 (arg, quals, complain | tf_ignore_bad_quals);
7464 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7466 /* We are processing a type constructed from a
7467 template template parameter. */
7468 tree argvec = tsubst (TYPE_TI_ARGS (t),
7469 args, complain, in_decl);
7470 if (argvec == error_mark_node)
7471 return error_mark_node;
7473 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7474 are resolving nested-types in the signature of a
7475 member function templates. Otherwise ARG is a
7476 TEMPLATE_DECL and is the real template to be
7477 instantiated. */
7478 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7479 arg = TYPE_NAME (arg);
7481 r = lookup_template_class (arg,
7482 argvec, in_decl,
7483 DECL_CONTEXT (arg),
7484 /*entering_scope=*/0,
7485 complain);
7486 return cp_build_qualified_type_real
7487 (r, TYPE_QUALS (t), complain);
7489 else
7490 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7491 return arg;
7494 if (level == 1)
7495 /* This can happen during the attempted tsubst'ing in
7496 unify. This means that we don't yet have any information
7497 about the template parameter in question. */
7498 return t;
7500 /* If we get here, we must have been looking at a parm for a
7501 more deeply nested template. Make a new version of this
7502 template parameter, but with a lower level. */
7503 switch (TREE_CODE (t))
7505 case TEMPLATE_TYPE_PARM:
7506 case TEMPLATE_TEMPLATE_PARM:
7507 case BOUND_TEMPLATE_TEMPLATE_PARM:
7508 if (cp_type_quals (t))
7510 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7511 r = cp_build_qualified_type_real
7512 (r, cp_type_quals (t),
7513 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7514 ? tf_ignore_bad_quals : 0));
7516 else
7518 r = copy_type (t);
7519 TEMPLATE_TYPE_PARM_INDEX (r)
7520 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7521 r, levels);
7522 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7523 TYPE_MAIN_VARIANT (r) = r;
7524 TYPE_POINTER_TO (r) = NULL_TREE;
7525 TYPE_REFERENCE_TO (r) = NULL_TREE;
7527 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
7528 /* We have reduced the level of the template
7529 template parameter, but not the levels of its
7530 template parameters, so canonical_type_parameter
7531 will not be able to find the canonical template
7532 template parameter for this level. Thus, we
7533 require structural equality checking to compare
7534 TEMPLATE_TEMPLATE_PARMs. */
7535 SET_TYPE_STRUCTURAL_EQUALITY (r);
7536 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
7537 SET_TYPE_STRUCTURAL_EQUALITY (r);
7538 else
7539 TYPE_CANONICAL (r) = canonical_type_parameter (r);
7541 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7543 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7544 complain, in_decl);
7545 if (argvec == error_mark_node)
7546 return error_mark_node;
7548 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7549 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7552 break;
7554 case TEMPLATE_PARM_INDEX:
7555 r = reduce_template_parm_level (t, type, levels);
7556 break;
7558 default:
7559 gcc_unreachable ();
7562 return r;
7565 case TREE_LIST:
7567 tree purpose, value, chain;
7569 if (t == void_list_node)
7570 return t;
7572 purpose = TREE_PURPOSE (t);
7573 if (purpose)
7575 purpose = tsubst (purpose, args, complain, in_decl);
7576 if (purpose == error_mark_node)
7577 return error_mark_node;
7579 value = TREE_VALUE (t);
7580 if (value)
7582 value = tsubst (value, args, complain, in_decl);
7583 if (value == error_mark_node)
7584 return error_mark_node;
7586 chain = TREE_CHAIN (t);
7587 if (chain && chain != void_type_node)
7589 chain = tsubst (chain, args, complain, in_decl);
7590 if (chain == error_mark_node)
7591 return error_mark_node;
7593 if (purpose == TREE_PURPOSE (t)
7594 && value == TREE_VALUE (t)
7595 && chain == TREE_CHAIN (t))
7596 return t;
7597 return hash_tree_cons (purpose, value, chain);
7600 case TREE_BINFO:
7601 /* We should never be tsubsting a binfo. */
7602 gcc_unreachable ();
7604 case TREE_VEC:
7605 /* A vector of template arguments. */
7606 gcc_assert (!type);
7607 return tsubst_template_args (t, args, complain, in_decl);
7609 case POINTER_TYPE:
7610 case REFERENCE_TYPE:
7612 enum tree_code code;
7614 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7615 return t;
7617 code = TREE_CODE (t);
7620 /* [temp.deduct]
7622 Type deduction may fail for any of the following
7623 reasons:
7625 -- Attempting to create a pointer to reference type.
7626 -- Attempting to create a reference to a reference type or
7627 a reference to void. */
7628 if (TREE_CODE (type) == REFERENCE_TYPE
7629 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7631 static location_t last_loc;
7633 /* We keep track of the last time we issued this error
7634 message to avoid spewing a ton of messages during a
7635 single bad template instantiation. */
7636 if (complain & tf_error
7637 #ifdef USE_MAPPED_LOCATION
7638 && last_loc != input_location
7639 #else
7640 && (last_loc.line != input_line
7641 || last_loc.file != input_filename)
7642 #endif
7645 if (TREE_CODE (type) == VOID_TYPE)
7646 error ("forming reference to void");
7647 else
7648 error ("forming %s to reference type %qT",
7649 (code == POINTER_TYPE) ? "pointer" : "reference",
7650 type);
7651 last_loc = input_location;
7654 return error_mark_node;
7656 else if (code == POINTER_TYPE)
7658 r = build_pointer_type (type);
7659 if (TREE_CODE (type) == METHOD_TYPE)
7660 r = build_ptrmemfunc_type (r);
7662 else
7663 r = build_reference_type (type);
7664 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7666 if (r != error_mark_node)
7667 /* Will this ever be needed for TYPE_..._TO values? */
7668 layout_type (r);
7670 return r;
7672 case OFFSET_TYPE:
7674 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7675 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7677 /* [temp.deduct]
7679 Type deduction may fail for any of the following
7680 reasons:
7682 -- Attempting to create "pointer to member of T" when T
7683 is not a class type. */
7684 if (complain & tf_error)
7685 error ("creating pointer to member of non-class type %qT", r);
7686 return error_mark_node;
7688 if (TREE_CODE (type) == REFERENCE_TYPE)
7690 if (complain & tf_error)
7691 error ("creating pointer to member reference type %qT", type);
7692 return error_mark_node;
7694 if (TREE_CODE (type) == VOID_TYPE)
7696 if (complain & tf_error)
7697 error ("creating pointer to member of type void");
7698 return error_mark_node;
7700 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7701 if (TREE_CODE (type) == FUNCTION_TYPE)
7703 /* The type of the implicit object parameter gets its
7704 cv-qualifiers from the FUNCTION_TYPE. */
7705 tree method_type;
7706 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7707 cp_type_quals (type));
7708 tree memptr;
7709 method_type = build_method_type_directly (this_type,
7710 TREE_TYPE (type),
7711 TYPE_ARG_TYPES (type));
7712 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7713 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7714 complain);
7716 else
7717 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7718 TYPE_QUALS (t),
7719 complain);
7721 case FUNCTION_TYPE:
7722 case METHOD_TYPE:
7724 tree fntype;
7725 tree specs;
7726 fntype = tsubst_function_type (t, args, complain, in_decl);
7727 if (fntype == error_mark_node)
7728 return error_mark_node;
7730 /* Substitute the exception specification. */
7731 specs = tsubst_exception_specification (t, args, complain,
7732 in_decl);
7733 if (specs == error_mark_node)
7734 return error_mark_node;
7735 if (specs)
7736 fntype = build_exception_variant (fntype, specs);
7737 return fntype;
7739 case ARRAY_TYPE:
7741 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7742 if (domain == error_mark_node)
7743 return error_mark_node;
7745 /* As an optimization, we avoid regenerating the array type if
7746 it will obviously be the same as T. */
7747 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7748 return t;
7750 /* These checks should match the ones in grokdeclarator.
7752 [temp.deduct]
7754 The deduction may fail for any of the following reasons:
7756 -- Attempting to create an array with an element type that
7757 is void, a function type, or a reference type, or [DR337]
7758 an abstract class type. */
7759 if (TREE_CODE (type) == VOID_TYPE
7760 || TREE_CODE (type) == FUNCTION_TYPE
7761 || TREE_CODE (type) == REFERENCE_TYPE)
7763 if (complain & tf_error)
7764 error ("creating array of %qT", type);
7765 return error_mark_node;
7767 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7769 if (complain & tf_error)
7770 error ("creating array of %qT, which is an abstract class type",
7771 type);
7772 return error_mark_node;
7775 r = build_cplus_array_type (type, domain);
7776 return r;
7779 case PLUS_EXPR:
7780 case MINUS_EXPR:
7782 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7783 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7785 if (e1 == error_mark_node || e2 == error_mark_node)
7786 return error_mark_node;
7788 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7791 case NEGATE_EXPR:
7792 case NOP_EXPR:
7794 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7795 if (e == error_mark_node)
7796 return error_mark_node;
7798 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7801 case TYPENAME_TYPE:
7803 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7804 in_decl, /*entering_scope=*/1);
7805 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7806 complain, in_decl);
7808 if (ctx == error_mark_node || f == error_mark_node)
7809 return error_mark_node;
7811 if (!IS_AGGR_TYPE (ctx))
7813 if (complain & tf_error)
7814 error ("%qT is not a class, struct, or union type", ctx);
7815 return error_mark_node;
7817 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7819 /* Normally, make_typename_type does not require that the CTX
7820 have complete type in order to allow things like:
7822 template <class T> struct S { typename S<T>::X Y; };
7824 But, such constructs have already been resolved by this
7825 point, so here CTX really should have complete type, unless
7826 it's a partial instantiation. */
7827 ctx = complete_type (ctx);
7828 if (!COMPLETE_TYPE_P (ctx))
7830 if (complain & tf_error)
7831 cxx_incomplete_type_error (NULL_TREE, ctx);
7832 return error_mark_node;
7836 f = make_typename_type (ctx, f, typename_type,
7837 (complain & tf_error) | tf_keep_type_decl);
7838 if (f == error_mark_node)
7839 return f;
7840 if (TREE_CODE (f) == TYPE_DECL)
7842 complain |= tf_ignore_bad_quals;
7843 f = TREE_TYPE (f);
7846 if (TREE_CODE (f) != TYPENAME_TYPE)
7848 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7849 error ("%qT resolves to %qT, which is not an enumeration type",
7850 t, f);
7851 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7852 error ("%qT resolves to %qT, which is is not a class type",
7853 t, f);
7856 return cp_build_qualified_type_real
7857 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7860 case UNBOUND_CLASS_TEMPLATE:
7862 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7863 in_decl, /*entering_scope=*/1);
7864 tree name = TYPE_IDENTIFIER (t);
7865 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7867 if (ctx == error_mark_node || name == error_mark_node)
7868 return error_mark_node;
7870 if (parm_list)
7871 parm_list = tsubst_template_parms (parm_list, args, complain);
7872 return make_unbound_class_template (ctx, name, parm_list, complain);
7875 case INDIRECT_REF:
7876 case ADDR_EXPR:
7877 case CALL_EXPR:
7878 gcc_unreachable ();
7880 case ARRAY_REF:
7882 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7883 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7884 /*integral_constant_expression_p=*/false);
7885 if (e1 == error_mark_node || e2 == error_mark_node)
7886 return error_mark_node;
7888 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7891 case SCOPE_REF:
7893 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7894 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7895 if (e1 == error_mark_node || e2 == error_mark_node)
7896 return error_mark_node;
7898 return build_qualified_name (/*type=*/NULL_TREE,
7899 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7902 case TYPEOF_TYPE:
7904 tree type;
7906 type = finish_typeof (tsubst_expr
7907 (TYPEOF_TYPE_EXPR (t), args,
7908 complain, in_decl,
7909 /*integral_constant_expression_p=*/false));
7910 return cp_build_qualified_type_real (type,
7911 cp_type_quals (t)
7912 | cp_type_quals (type),
7913 complain);
7916 default:
7917 sorry ("use of %qs in template",
7918 tree_code_name [(int) TREE_CODE (t)]);
7919 return error_mark_node;
7923 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7924 type of the expression on the left-hand side of the "." or "->"
7925 operator. */
7927 static tree
7928 tsubst_baselink (tree baselink, tree object_type,
7929 tree args, tsubst_flags_t complain, tree in_decl)
7931 tree name;
7932 tree qualifying_scope;
7933 tree fns;
7934 tree optype;
7935 tree template_args = 0;
7936 bool template_id_p = false;
7938 /* A baselink indicates a function from a base class. Both the
7939 BASELINK_ACCESS_BINFO and the base class referenced may
7940 indicate bases of the template class, rather than the
7941 instantiated class. In addition, lookups that were not
7942 ambiguous before may be ambiguous now. Therefore, we perform
7943 the lookup again. */
7944 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7945 qualifying_scope = tsubst (qualifying_scope, args,
7946 complain, in_decl);
7947 fns = BASELINK_FUNCTIONS (baselink);
7948 optype = BASELINK_OPTYPE (baselink);
7949 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7951 template_id_p = true;
7952 template_args = TREE_OPERAND (fns, 1);
7953 fns = TREE_OPERAND (fns, 0);
7954 if (template_args)
7955 template_args = tsubst_template_args (template_args, args,
7956 complain, in_decl);
7958 name = DECL_NAME (get_first_fn (fns));
7959 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7961 /* If lookup found a single function, mark it as used at this
7962 point. (If it lookup found multiple functions the one selected
7963 later by overload resolution will be marked as used at that
7964 point.) */
7965 if (BASELINK_P (baselink))
7966 fns = BASELINK_FUNCTIONS (baselink);
7967 if (!template_id_p && !really_overloaded_fn (fns))
7968 mark_used (OVL_CURRENT (fns));
7970 /* Add back the template arguments, if present. */
7971 if (BASELINK_P (baselink) && template_id_p)
7972 BASELINK_FUNCTIONS (baselink)
7973 = build_nt (TEMPLATE_ID_EXPR,
7974 BASELINK_FUNCTIONS (baselink),
7975 template_args);
7976 /* Update the conversion operator type. */
7977 BASELINK_OPTYPE (baselink)
7978 = tsubst (optype, args, complain, in_decl);
7980 if (!object_type)
7981 object_type = current_class_type;
7982 return adjust_result_of_qualified_name_lookup (baselink,
7983 qualifying_scope,
7984 object_type);
7987 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7988 true if the qualified-id will be a postfix-expression in-and-of
7989 itself; false if more of the postfix-expression follows the
7990 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7991 of "&". */
7993 static tree
7994 tsubst_qualified_id (tree qualified_id, tree args,
7995 tsubst_flags_t complain, tree in_decl,
7996 bool done, bool address_p)
7998 tree expr;
7999 tree scope;
8000 tree name;
8001 bool is_template;
8002 tree template_args;
8004 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
8006 /* Figure out what name to look up. */
8007 name = TREE_OPERAND (qualified_id, 1);
8008 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8010 is_template = true;
8011 template_args = TREE_OPERAND (name, 1);
8012 if (template_args)
8013 template_args = tsubst_template_args (template_args, args,
8014 complain, in_decl);
8015 name = TREE_OPERAND (name, 0);
8017 else
8019 is_template = false;
8020 template_args = NULL_TREE;
8023 /* Substitute into the qualifying scope. When there are no ARGS, we
8024 are just trying to simplify a non-dependent expression. In that
8025 case the qualifying scope may be dependent, and, in any case,
8026 substituting will not help. */
8027 scope = TREE_OPERAND (qualified_id, 0);
8028 if (args)
8030 scope = tsubst (scope, args, complain, in_decl);
8031 expr = tsubst_copy (name, args, complain, in_decl);
8033 else
8034 expr = name;
8036 if (dependent_type_p (scope))
8037 return build_qualified_name (/*type=*/NULL_TREE,
8038 scope, expr,
8039 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
8041 if (!BASELINK_P (name) && !DECL_P (expr))
8043 if (TREE_CODE (expr) == BIT_NOT_EXPR)
8044 /* If this were actually a destructor call, it would have been
8045 parsed as such by the parser. */
8046 expr = error_mark_node;
8047 else
8048 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
8049 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
8050 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
8052 if (complain & tf_error)
8054 error ("dependent-name %qE is parsed as a non-type, but "
8055 "instantiation yields a type", qualified_id);
8056 inform ("say %<typename %E%> if a type is meant", qualified_id);
8058 return error_mark_node;
8062 if (DECL_P (expr))
8064 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
8065 scope);
8066 /* Remember that there was a reference to this entity. */
8067 mark_used (expr);
8070 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
8072 if (complain & tf_error)
8073 qualified_name_lookup_error (scope,
8074 TREE_OPERAND (qualified_id, 1),
8075 expr);
8076 return error_mark_node;
8079 if (is_template)
8080 expr = lookup_template_function (expr, template_args);
8082 if (expr == error_mark_node && complain & tf_error)
8083 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
8084 expr);
8085 else if (TYPE_P (scope))
8087 expr = (adjust_result_of_qualified_name_lookup
8088 (expr, scope, current_class_type));
8089 expr = (finish_qualified_id_expr
8090 (scope, expr, done, address_p,
8091 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
8092 /*template_arg_p=*/false));
8095 /* Expressions do not generally have reference type. */
8096 if (TREE_CODE (expr) != SCOPE_REF
8097 /* However, if we're about to form a pointer-to-member, we just
8098 want the referenced member referenced. */
8099 && TREE_CODE (expr) != OFFSET_REF)
8100 expr = convert_from_reference (expr);
8102 return expr;
8105 /* Like tsubst, but deals with expressions. This function just replaces
8106 template parms; to finish processing the resultant expression, use
8107 tsubst_expr. */
8109 static tree
8110 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8112 enum tree_code code;
8113 tree r;
8115 if (t == NULL_TREE || t == error_mark_node)
8116 return t;
8118 code = TREE_CODE (t);
8120 switch (code)
8122 case PARM_DECL:
8123 r = retrieve_local_specialization (t);
8124 gcc_assert (r != NULL);
8125 mark_used (r);
8126 return r;
8128 case CONST_DECL:
8130 tree enum_type;
8131 tree v;
8133 if (DECL_TEMPLATE_PARM_P (t))
8134 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
8135 /* There is no need to substitute into namespace-scope
8136 enumerators. */
8137 if (DECL_NAMESPACE_SCOPE_P (t))
8138 return t;
8139 /* If ARGS is NULL, then T is known to be non-dependent. */
8140 if (args == NULL_TREE)
8141 return integral_constant_value (t);
8143 /* Unfortunately, we cannot just call lookup_name here.
8144 Consider:
8146 template <int I> int f() {
8147 enum E { a = I };
8148 struct S { void g() { E e = a; } };
8151 When we instantiate f<7>::S::g(), say, lookup_name is not
8152 clever enough to find f<7>::a. */
8153 enum_type
8154 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
8155 /*entering_scope=*/0);
8157 for (v = TYPE_VALUES (enum_type);
8158 v != NULL_TREE;
8159 v = TREE_CHAIN (v))
8160 if (TREE_PURPOSE (v) == DECL_NAME (t))
8161 return TREE_VALUE (v);
8163 /* We didn't find the name. That should never happen; if
8164 name-lookup found it during preliminary parsing, we
8165 should find it again here during instantiation. */
8166 gcc_unreachable ();
8168 return t;
8170 case FIELD_DECL:
8171 if (DECL_CONTEXT (t))
8173 tree ctx;
8175 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
8176 /*entering_scope=*/1);
8177 if (ctx != DECL_CONTEXT (t))
8179 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
8180 if (!r)
8182 if (complain & tf_error)
8183 error ("using invalid field %qD", t);
8184 return error_mark_node;
8186 return r;
8190 return t;
8192 case VAR_DECL:
8193 case FUNCTION_DECL:
8194 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8195 || local_variable_p (t))
8196 t = tsubst (t, args, complain, in_decl);
8197 mark_used (t);
8198 return t;
8200 case BASELINK:
8201 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
8203 case TEMPLATE_DECL:
8204 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8205 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
8206 args, complain, in_decl);
8207 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
8208 return tsubst (t, args, complain, in_decl);
8209 else if (DECL_CLASS_SCOPE_P (t)
8210 && uses_template_parms (DECL_CONTEXT (t)))
8212 /* Template template argument like the following example need
8213 special treatment:
8215 template <template <class> class TT> struct C {};
8216 template <class T> struct D {
8217 template <class U> struct E {};
8218 C<E> c; // #1
8220 D<int> d; // #2
8222 We are processing the template argument `E' in #1 for
8223 the template instantiation #2. Originally, `E' is a
8224 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8225 have to substitute this with one having context `D<int>'. */
8227 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8228 return lookup_field (context, DECL_NAME(t), 0, false);
8230 else
8231 /* Ordinary template template argument. */
8232 return t;
8234 case CAST_EXPR:
8235 case REINTERPRET_CAST_EXPR:
8236 case CONST_CAST_EXPR:
8237 case STATIC_CAST_EXPR:
8238 case DYNAMIC_CAST_EXPR:
8239 case NOP_EXPR:
8240 return build1
8241 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8242 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8244 case INDIRECT_REF:
8245 case NEGATE_EXPR:
8246 case TRUTH_NOT_EXPR:
8247 case BIT_NOT_EXPR:
8248 case ADDR_EXPR:
8249 case UNARY_PLUS_EXPR: /* Unary + */
8250 case SIZEOF_EXPR:
8251 case ALIGNOF_EXPR:
8252 case ARROW_EXPR:
8253 case THROW_EXPR:
8254 case TYPEID_EXPR:
8255 case REALPART_EXPR:
8256 case IMAGPART_EXPR:
8257 return build1
8258 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8259 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8261 case COMPONENT_REF:
8263 tree object;
8264 tree name;
8266 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8267 name = TREE_OPERAND (t, 1);
8268 if (TREE_CODE (name) == BIT_NOT_EXPR)
8270 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8271 complain, in_decl);
8272 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8274 else if (TREE_CODE (name) == SCOPE_REF
8275 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8277 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8278 complain, in_decl);
8279 name = TREE_OPERAND (name, 1);
8280 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8281 complain, in_decl);
8282 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8283 name = build_qualified_name (/*type=*/NULL_TREE,
8284 base, name,
8285 /*template_p=*/false);
8287 else if (TREE_CODE (name) == BASELINK)
8288 name = tsubst_baselink (name,
8289 non_reference (TREE_TYPE (object)),
8290 args, complain,
8291 in_decl);
8292 else
8293 name = tsubst_copy (name, args, complain, in_decl);
8294 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
8297 case PLUS_EXPR:
8298 case MINUS_EXPR:
8299 case MULT_EXPR:
8300 case TRUNC_DIV_EXPR:
8301 case CEIL_DIV_EXPR:
8302 case FLOOR_DIV_EXPR:
8303 case ROUND_DIV_EXPR:
8304 case EXACT_DIV_EXPR:
8305 case BIT_AND_EXPR:
8306 case BIT_IOR_EXPR:
8307 case BIT_XOR_EXPR:
8308 case TRUNC_MOD_EXPR:
8309 case FLOOR_MOD_EXPR:
8310 case TRUTH_ANDIF_EXPR:
8311 case TRUTH_ORIF_EXPR:
8312 case TRUTH_AND_EXPR:
8313 case TRUTH_OR_EXPR:
8314 case RSHIFT_EXPR:
8315 case LSHIFT_EXPR:
8316 case RROTATE_EXPR:
8317 case LROTATE_EXPR:
8318 case EQ_EXPR:
8319 case NE_EXPR:
8320 case MAX_EXPR:
8321 case MIN_EXPR:
8322 case LE_EXPR:
8323 case GE_EXPR:
8324 case LT_EXPR:
8325 case GT_EXPR:
8326 case COMPOUND_EXPR:
8327 case DOTSTAR_EXPR:
8328 case MEMBER_REF:
8329 case PREDECREMENT_EXPR:
8330 case PREINCREMENT_EXPR:
8331 case POSTDECREMENT_EXPR:
8332 case POSTINCREMENT_EXPR:
8333 return build_nt
8334 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8335 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8337 case SCOPE_REF:
8338 return build_qualified_name (/*type=*/NULL_TREE,
8339 tsubst_copy (TREE_OPERAND (t, 0),
8340 args, complain, in_decl),
8341 tsubst_copy (TREE_OPERAND (t, 1),
8342 args, complain, in_decl),
8343 QUALIFIED_NAME_IS_TEMPLATE (t));
8345 case ARRAY_REF:
8346 return build_nt
8347 (ARRAY_REF,
8348 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8349 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8350 NULL_TREE, NULL_TREE);
8352 case CALL_EXPR:
8354 int n = VL_EXP_OPERAND_LENGTH (t);
8355 tree result = build_vl_exp (CALL_EXPR, n);
8356 int i;
8357 for (i = 0; i < n; i++)
8358 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
8359 complain, in_decl);
8360 return result;
8363 case COND_EXPR:
8364 case MODOP_EXPR:
8365 case PSEUDO_DTOR_EXPR:
8367 r = build_nt
8368 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8369 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8370 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8371 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8372 return r;
8375 case NEW_EXPR:
8377 r = build_nt
8378 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8379 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8380 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8381 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8382 return r;
8385 case DELETE_EXPR:
8387 r = build_nt
8388 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8389 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8390 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8391 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8392 return r;
8395 case TEMPLATE_ID_EXPR:
8397 /* Substituted template arguments */
8398 tree fn = TREE_OPERAND (t, 0);
8399 tree targs = TREE_OPERAND (t, 1);
8401 fn = tsubst_copy (fn, args, complain, in_decl);
8402 if (targs)
8403 targs = tsubst_template_args (targs, args, complain, in_decl);
8405 return lookup_template_function (fn, targs);
8408 case TREE_LIST:
8410 tree purpose, value, chain;
8412 if (t == void_list_node)
8413 return t;
8415 purpose = TREE_PURPOSE (t);
8416 if (purpose)
8417 purpose = tsubst_copy (purpose, args, complain, in_decl);
8418 value = TREE_VALUE (t);
8419 if (value)
8420 value = tsubst_copy (value, args, complain, in_decl);
8421 chain = TREE_CHAIN (t);
8422 if (chain && chain != void_type_node)
8423 chain = tsubst_copy (chain, args, complain, in_decl);
8424 if (purpose == TREE_PURPOSE (t)
8425 && value == TREE_VALUE (t)
8426 && chain == TREE_CHAIN (t))
8427 return t;
8428 return tree_cons (purpose, value, chain);
8431 case RECORD_TYPE:
8432 case UNION_TYPE:
8433 case ENUMERAL_TYPE:
8434 case INTEGER_TYPE:
8435 case TEMPLATE_TYPE_PARM:
8436 case TEMPLATE_TEMPLATE_PARM:
8437 case BOUND_TEMPLATE_TEMPLATE_PARM:
8438 case TEMPLATE_PARM_INDEX:
8439 case POINTER_TYPE:
8440 case REFERENCE_TYPE:
8441 case OFFSET_TYPE:
8442 case FUNCTION_TYPE:
8443 case METHOD_TYPE:
8444 case ARRAY_TYPE:
8445 case TYPENAME_TYPE:
8446 case UNBOUND_CLASS_TEMPLATE:
8447 case TYPEOF_TYPE:
8448 case TYPE_DECL:
8449 return tsubst (t, args, complain, in_decl);
8451 case IDENTIFIER_NODE:
8452 if (IDENTIFIER_TYPENAME_P (t))
8454 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8455 return mangle_conv_op_name_for_type (new_type);
8457 else
8458 return t;
8460 case CONSTRUCTOR:
8461 /* This is handled by tsubst_copy_and_build. */
8462 gcc_unreachable ();
8464 case VA_ARG_EXPR:
8465 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8466 in_decl),
8467 tsubst (TREE_TYPE (t), args, complain, in_decl));
8469 case CLEANUP_POINT_EXPR:
8470 /* We shouldn't have built any of these during initial template
8471 generation. Instead, they should be built during instantiation
8472 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8473 gcc_unreachable ();
8475 case OFFSET_REF:
8476 mark_used (TREE_OPERAND (t, 1));
8477 return t;
8479 default:
8480 return t;
8484 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8486 static tree
8487 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8488 tree in_decl)
8490 tree new_clauses = NULL, nc, oc;
8492 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8494 nc = copy_node (oc);
8495 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8496 new_clauses = nc;
8498 switch (OMP_CLAUSE_CODE (nc))
8500 case OMP_CLAUSE_PRIVATE:
8501 case OMP_CLAUSE_SHARED:
8502 case OMP_CLAUSE_FIRSTPRIVATE:
8503 case OMP_CLAUSE_LASTPRIVATE:
8504 case OMP_CLAUSE_REDUCTION:
8505 case OMP_CLAUSE_COPYIN:
8506 case OMP_CLAUSE_COPYPRIVATE:
8507 case OMP_CLAUSE_IF:
8508 case OMP_CLAUSE_NUM_THREADS:
8509 case OMP_CLAUSE_SCHEDULE:
8510 OMP_CLAUSE_OPERAND (nc, 0)
8511 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8512 in_decl, /*integral_constant_expression_p=*/false);
8513 break;
8514 case OMP_CLAUSE_NOWAIT:
8515 case OMP_CLAUSE_ORDERED:
8516 case OMP_CLAUSE_DEFAULT:
8517 break;
8518 default:
8519 gcc_unreachable ();
8523 return finish_omp_clauses (nreverse (new_clauses));
8526 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8528 static tree
8529 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8530 tree in_decl)
8532 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8534 tree purpose, value, chain;
8536 if (t == NULL)
8537 return t;
8539 if (TREE_CODE (t) != TREE_LIST)
8540 return tsubst_copy_and_build (t, args, complain, in_decl,
8541 /*function_p=*/false,
8542 /*integral_constant_expression_p=*/false);
8544 if (t == void_list_node)
8545 return t;
8547 purpose = TREE_PURPOSE (t);
8548 if (purpose)
8549 purpose = RECUR (purpose);
8550 value = TREE_VALUE (t);
8551 if (value)
8552 value = RECUR (value);
8553 chain = TREE_CHAIN (t);
8554 if (chain && chain != void_type_node)
8555 chain = RECUR (chain);
8556 return tree_cons (purpose, value, chain);
8557 #undef RECUR
8560 /* Like tsubst_copy for expressions, etc. but also does semantic
8561 processing. */
8563 static tree
8564 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8565 bool integral_constant_expression_p)
8567 #define RECUR(NODE) \
8568 tsubst_expr ((NODE), args, complain, in_decl, \
8569 integral_constant_expression_p)
8571 tree stmt, tmp;
8573 if (t == NULL_TREE || t == error_mark_node)
8574 return t;
8576 if (EXPR_HAS_LOCATION (t))
8577 input_location = EXPR_LOCATION (t);
8578 if (STATEMENT_CODE_P (TREE_CODE (t)))
8579 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8581 switch (TREE_CODE (t))
8583 case STATEMENT_LIST:
8585 tree_stmt_iterator i;
8586 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8587 RECUR (tsi_stmt (i));
8588 break;
8591 case CTOR_INITIALIZER:
8592 finish_mem_initializers (tsubst_initializer_list
8593 (TREE_OPERAND (t, 0), args));
8594 break;
8596 case RETURN_EXPR:
8597 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
8598 break;
8600 case EXPR_STMT:
8601 tmp = RECUR (EXPR_STMT_EXPR (t));
8602 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8603 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8604 else
8605 finish_expr_stmt (tmp);
8606 break;
8608 case USING_STMT:
8609 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
8610 break;
8612 case DECL_EXPR:
8614 tree decl;
8615 tree init;
8617 decl = DECL_EXPR_DECL (t);
8618 if (TREE_CODE (decl) == LABEL_DECL)
8619 finish_label_decl (DECL_NAME (decl));
8620 else if (TREE_CODE (decl) == USING_DECL)
8622 tree scope = USING_DECL_SCOPE (decl);
8623 tree name = DECL_NAME (decl);
8624 tree decl;
8626 scope = RECUR (scope);
8627 decl = lookup_qualified_name (scope, name,
8628 /*is_type_p=*/false,
8629 /*complain=*/false);
8630 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8631 qualified_name_lookup_error (scope, name, decl);
8632 else
8633 do_local_using_decl (decl, scope, name);
8635 else
8637 init = DECL_INITIAL (decl);
8638 decl = tsubst (decl, args, complain, in_decl);
8639 if (decl != error_mark_node)
8641 /* By marking the declaration as instantiated, we avoid
8642 trying to instantiate it. Since instantiate_decl can't
8643 handle local variables, and since we've already done
8644 all that needs to be done, that's the right thing to
8645 do. */
8646 if (TREE_CODE (decl) == VAR_DECL)
8647 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8648 if (TREE_CODE (decl) == VAR_DECL
8649 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8650 /* Anonymous aggregates are a special case. */
8651 finish_anon_union (decl);
8652 else
8654 maybe_push_decl (decl);
8655 if (TREE_CODE (decl) == VAR_DECL
8656 && DECL_PRETTY_FUNCTION_P (decl))
8658 /* For __PRETTY_FUNCTION__ we have to adjust the
8659 initializer. */
8660 const char *const name
8661 = cxx_printable_name (current_function_decl, 2);
8662 init = cp_fname_init (name, &TREE_TYPE (decl));
8664 else
8665 init = RECUR (init);
8666 finish_decl (decl, init, NULL_TREE);
8671 /* A DECL_EXPR can also be used as an expression, in the condition
8672 clause of an if/for/while construct. */
8673 return decl;
8676 case FOR_STMT:
8677 stmt = begin_for_stmt ();
8678 RECUR (FOR_INIT_STMT (t));
8679 finish_for_init_stmt (stmt);
8680 tmp = RECUR (FOR_COND (t));
8681 finish_for_cond (tmp, stmt);
8682 tmp = RECUR (FOR_EXPR (t));
8683 finish_for_expr (tmp, stmt);
8684 RECUR (FOR_BODY (t));
8685 finish_for_stmt (stmt);
8686 break;
8688 case WHILE_STMT:
8689 stmt = begin_while_stmt ();
8690 tmp = RECUR (WHILE_COND (t));
8691 finish_while_stmt_cond (tmp, stmt);
8692 RECUR (WHILE_BODY (t));
8693 finish_while_stmt (stmt);
8694 break;
8696 case DO_STMT:
8697 stmt = begin_do_stmt ();
8698 RECUR (DO_BODY (t));
8699 finish_do_body (stmt);
8700 tmp = RECUR (DO_COND (t));
8701 finish_do_stmt (tmp, stmt);
8702 break;
8704 case IF_STMT:
8705 stmt = begin_if_stmt ();
8706 tmp = RECUR (IF_COND (t));
8707 finish_if_stmt_cond (tmp, stmt);
8708 RECUR (THEN_CLAUSE (t));
8709 finish_then_clause (stmt);
8711 if (ELSE_CLAUSE (t))
8713 begin_else_clause (stmt);
8714 RECUR (ELSE_CLAUSE (t));
8715 finish_else_clause (stmt);
8718 finish_if_stmt (stmt);
8719 break;
8721 case BIND_EXPR:
8722 if (BIND_EXPR_BODY_BLOCK (t))
8723 stmt = begin_function_body ();
8724 else
8725 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8726 ? BCS_TRY_BLOCK : 0);
8728 RECUR (BIND_EXPR_BODY (t));
8730 if (BIND_EXPR_BODY_BLOCK (t))
8731 finish_function_body (stmt);
8732 else
8733 finish_compound_stmt (stmt);
8734 break;
8736 case BREAK_STMT:
8737 finish_break_stmt ();
8738 break;
8740 case CONTINUE_STMT:
8741 finish_continue_stmt ();
8742 break;
8744 case SWITCH_STMT:
8745 stmt = begin_switch_stmt ();
8746 tmp = RECUR (SWITCH_STMT_COND (t));
8747 finish_switch_cond (tmp, stmt);
8748 RECUR (SWITCH_STMT_BODY (t));
8749 finish_switch_stmt (stmt);
8750 break;
8752 case CASE_LABEL_EXPR:
8753 finish_case_label (RECUR (CASE_LOW (t)),
8754 RECUR (CASE_HIGH (t)));
8755 break;
8757 case LABEL_EXPR:
8758 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8759 break;
8761 case GOTO_EXPR:
8762 tmp = GOTO_DESTINATION (t);
8763 if (TREE_CODE (tmp) != LABEL_DECL)
8764 /* Computed goto's must be tsubst'd into. On the other hand,
8765 non-computed gotos must not be; the identifier in question
8766 will have no binding. */
8767 tmp = RECUR (tmp);
8768 else
8769 tmp = DECL_NAME (tmp);
8770 finish_goto_stmt (tmp);
8771 break;
8773 case ASM_EXPR:
8774 tmp = finish_asm_stmt
8775 (ASM_VOLATILE_P (t),
8776 RECUR (ASM_STRING (t)),
8777 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8778 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8779 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8781 tree asm_expr = tmp;
8782 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8783 asm_expr = TREE_OPERAND (asm_expr, 0);
8784 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8786 break;
8788 case TRY_BLOCK:
8789 if (CLEANUP_P (t))
8791 stmt = begin_try_block ();
8792 RECUR (TRY_STMTS (t));
8793 finish_cleanup_try_block (stmt);
8794 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
8796 else
8798 tree compound_stmt = NULL_TREE;
8800 if (FN_TRY_BLOCK_P (t))
8801 stmt = begin_function_try_block (&compound_stmt);
8802 else
8803 stmt = begin_try_block ();
8805 RECUR (TRY_STMTS (t));
8807 if (FN_TRY_BLOCK_P (t))
8808 finish_function_try_block (stmt);
8809 else
8810 finish_try_block (stmt);
8812 RECUR (TRY_HANDLERS (t));
8813 if (FN_TRY_BLOCK_P (t))
8814 finish_function_handler_sequence (stmt, compound_stmt);
8815 else
8816 finish_handler_sequence (stmt);
8818 break;
8820 case HANDLER:
8822 tree decl = HANDLER_PARMS (t);
8824 if (decl)
8826 decl = tsubst (decl, args, complain, in_decl);
8827 /* Prevent instantiate_decl from trying to instantiate
8828 this variable. We've already done all that needs to be
8829 done. */
8830 if (decl != error_mark_node)
8831 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8833 stmt = begin_handler ();
8834 finish_handler_parms (decl, stmt);
8835 RECUR (HANDLER_BODY (t));
8836 finish_handler (stmt);
8838 break;
8840 case TAG_DEFN:
8841 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8842 break;
8844 case STATIC_ASSERT:
8846 tree condition =
8847 tsubst_expr (STATIC_ASSERT_CONDITION (t),
8848 args,
8849 complain, in_decl,
8850 /*integral_constant_expression_p=*/true);
8851 finish_static_assert (condition,
8852 STATIC_ASSERT_MESSAGE (t),
8853 STATIC_ASSERT_SOURCE_LOCATION (t),
8854 /*member_p=*/false);
8856 break;
8858 case OMP_PARALLEL:
8859 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8860 args, complain, in_decl);
8861 stmt = begin_omp_parallel ();
8862 RECUR (OMP_PARALLEL_BODY (t));
8863 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8864 = OMP_PARALLEL_COMBINED (t);
8865 break;
8867 case OMP_FOR:
8869 tree clauses, decl, init, cond, incr, body, pre_body;
8871 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8872 args, complain, in_decl);
8873 init = OMP_FOR_INIT (t);
8874 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8875 decl = RECUR (TREE_OPERAND (init, 0));
8876 init = RECUR (TREE_OPERAND (init, 1));
8877 cond = RECUR (OMP_FOR_COND (t));
8878 incr = RECUR (OMP_FOR_INCR (t));
8880 stmt = begin_omp_structured_block ();
8882 pre_body = push_stmt_list ();
8883 RECUR (OMP_FOR_PRE_BODY (t));
8884 pre_body = pop_stmt_list (pre_body);
8886 body = push_stmt_list ();
8887 RECUR (OMP_FOR_BODY (t));
8888 body = pop_stmt_list (body);
8890 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8891 pre_body);
8892 if (t)
8893 OMP_FOR_CLAUSES (t) = clauses;
8895 add_stmt (finish_omp_structured_block (stmt));
8897 break;
8899 case OMP_SECTIONS:
8900 case OMP_SINGLE:
8901 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8902 stmt = push_stmt_list ();
8903 RECUR (OMP_BODY (t));
8904 stmt = pop_stmt_list (stmt);
8906 t = copy_node (t);
8907 OMP_BODY (t) = stmt;
8908 OMP_CLAUSES (t) = tmp;
8909 add_stmt (t);
8910 break;
8912 case OMP_SECTION:
8913 case OMP_CRITICAL:
8914 case OMP_MASTER:
8915 case OMP_ORDERED:
8916 stmt = push_stmt_list ();
8917 RECUR (OMP_BODY (t));
8918 stmt = pop_stmt_list (stmt);
8920 t = copy_node (t);
8921 OMP_BODY (t) = stmt;
8922 add_stmt (t);
8923 break;
8925 case OMP_ATOMIC:
8926 if (OMP_ATOMIC_DEPENDENT_P (t))
8928 tree op1 = TREE_OPERAND (t, 1);
8929 tree lhs = RECUR (TREE_OPERAND (op1, 0));
8930 tree rhs = RECUR (TREE_OPERAND (op1, 1));
8931 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
8933 break;
8935 default:
8936 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8938 return tsubst_copy_and_build (t, args, complain, in_decl,
8939 /*function_p=*/false,
8940 integral_constant_expression_p);
8943 return NULL_TREE;
8944 #undef RECUR
8947 /* T is a postfix-expression that is not being used in a function
8948 call. Return the substituted version of T. */
8950 static tree
8951 tsubst_non_call_postfix_expression (tree t, tree args,
8952 tsubst_flags_t complain,
8953 tree in_decl)
8955 if (TREE_CODE (t) == SCOPE_REF)
8956 t = tsubst_qualified_id (t, args, complain, in_decl,
8957 /*done=*/false, /*address_p=*/false);
8958 else
8959 t = tsubst_copy_and_build (t, args, complain, in_decl,
8960 /*function_p=*/false,
8961 /*integral_constant_expression_p=*/false);
8963 return t;
8966 /* Like tsubst but deals with expressions and performs semantic
8967 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8969 tree
8970 tsubst_copy_and_build (tree t,
8971 tree args,
8972 tsubst_flags_t complain,
8973 tree in_decl,
8974 bool function_p,
8975 bool integral_constant_expression_p)
8977 #define RECUR(NODE) \
8978 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8979 /*function_p=*/false, \
8980 integral_constant_expression_p)
8982 tree op1;
8984 if (t == NULL_TREE || t == error_mark_node)
8985 return t;
8987 switch (TREE_CODE (t))
8989 case USING_DECL:
8990 t = DECL_NAME (t);
8991 /* Fall through. */
8992 case IDENTIFIER_NODE:
8994 tree decl;
8995 cp_id_kind idk;
8996 bool non_integral_constant_expression_p;
8997 const char *error_msg;
8999 if (IDENTIFIER_TYPENAME_P (t))
9001 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9002 t = mangle_conv_op_name_for_type (new_type);
9005 /* Look up the name. */
9006 decl = lookup_name (t);
9008 /* By convention, expressions use ERROR_MARK_NODE to indicate
9009 failure, not NULL_TREE. */
9010 if (decl == NULL_TREE)
9011 decl = error_mark_node;
9013 decl = finish_id_expression (t, decl, NULL_TREE,
9014 &idk,
9015 integral_constant_expression_p,
9016 /*allow_non_integral_constant_expression_p=*/false,
9017 &non_integral_constant_expression_p,
9018 /*template_p=*/false,
9019 /*done=*/true,
9020 /*address_p=*/false,
9021 /*template_arg_p=*/false,
9022 &error_msg);
9023 if (error_msg)
9024 error (error_msg);
9025 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
9026 decl = unqualified_name_lookup_error (decl);
9027 return decl;
9030 case TEMPLATE_ID_EXPR:
9032 tree object;
9033 tree template = RECUR (TREE_OPERAND (t, 0));
9034 tree targs = TREE_OPERAND (t, 1);
9036 if (targs)
9037 targs = tsubst_template_args (targs, args, complain, in_decl);
9039 if (TREE_CODE (template) == COMPONENT_REF)
9041 object = TREE_OPERAND (template, 0);
9042 template = TREE_OPERAND (template, 1);
9044 else
9045 object = NULL_TREE;
9046 template = lookup_template_function (template, targs);
9048 if (object)
9049 return build3 (COMPONENT_REF, TREE_TYPE (template),
9050 object, template, NULL_TREE);
9051 else
9052 return baselink_for_fns (template);
9055 case INDIRECT_REF:
9057 tree r = RECUR (TREE_OPERAND (t, 0));
9059 if (REFERENCE_REF_P (t))
9061 /* A type conversion to reference type will be enclosed in
9062 such an indirect ref, but the substitution of the cast
9063 will have also added such an indirect ref. */
9064 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
9065 r = convert_from_reference (r);
9067 else
9068 r = build_x_indirect_ref (r, "unary *");
9069 return r;
9072 case NOP_EXPR:
9073 return build_nop
9074 (tsubst (TREE_TYPE (t), args, complain, in_decl),
9075 RECUR (TREE_OPERAND (t, 0)));
9077 case CAST_EXPR:
9078 case REINTERPRET_CAST_EXPR:
9079 case CONST_CAST_EXPR:
9080 case DYNAMIC_CAST_EXPR:
9081 case STATIC_CAST_EXPR:
9083 tree type;
9084 tree op;
9086 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9087 if (integral_constant_expression_p
9088 && !cast_valid_in_integral_constant_expression_p (type))
9090 error ("a cast to a type other than an integral or "
9091 "enumeration type cannot appear in a constant-expression");
9092 return error_mark_node;
9095 op = RECUR (TREE_OPERAND (t, 0));
9097 switch (TREE_CODE (t))
9099 case CAST_EXPR:
9100 return build_functional_cast (type, op);
9101 case REINTERPRET_CAST_EXPR:
9102 return build_reinterpret_cast (type, op);
9103 case CONST_CAST_EXPR:
9104 return build_const_cast (type, op);
9105 case DYNAMIC_CAST_EXPR:
9106 return build_dynamic_cast (type, op);
9107 case STATIC_CAST_EXPR:
9108 return build_static_cast (type, op);
9109 default:
9110 gcc_unreachable ();
9114 case POSTDECREMENT_EXPR:
9115 case POSTINCREMENT_EXPR:
9116 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9117 args, complain, in_decl);
9118 return build_x_unary_op (TREE_CODE (t), op1);
9120 case PREDECREMENT_EXPR:
9121 case PREINCREMENT_EXPR:
9122 case NEGATE_EXPR:
9123 case BIT_NOT_EXPR:
9124 case ABS_EXPR:
9125 case TRUTH_NOT_EXPR:
9126 case UNARY_PLUS_EXPR: /* Unary + */
9127 case REALPART_EXPR:
9128 case IMAGPART_EXPR:
9129 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
9131 case ADDR_EXPR:
9132 op1 = TREE_OPERAND (t, 0);
9133 if (TREE_CODE (op1) == SCOPE_REF)
9134 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
9135 /*done=*/true, /*address_p=*/true);
9136 else
9137 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
9138 in_decl);
9139 if (TREE_CODE (op1) == LABEL_DECL)
9140 return finish_label_address_expr (DECL_NAME (op1));
9141 return build_x_unary_op (ADDR_EXPR, op1);
9143 case PLUS_EXPR:
9144 case MINUS_EXPR:
9145 case MULT_EXPR:
9146 case TRUNC_DIV_EXPR:
9147 case CEIL_DIV_EXPR:
9148 case FLOOR_DIV_EXPR:
9149 case ROUND_DIV_EXPR:
9150 case EXACT_DIV_EXPR:
9151 case BIT_AND_EXPR:
9152 case BIT_IOR_EXPR:
9153 case BIT_XOR_EXPR:
9154 case TRUNC_MOD_EXPR:
9155 case FLOOR_MOD_EXPR:
9156 case TRUTH_ANDIF_EXPR:
9157 case TRUTH_ORIF_EXPR:
9158 case TRUTH_AND_EXPR:
9159 case TRUTH_OR_EXPR:
9160 case RSHIFT_EXPR:
9161 case LSHIFT_EXPR:
9162 case RROTATE_EXPR:
9163 case LROTATE_EXPR:
9164 case EQ_EXPR:
9165 case NE_EXPR:
9166 case MAX_EXPR:
9167 case MIN_EXPR:
9168 case LE_EXPR:
9169 case GE_EXPR:
9170 case LT_EXPR:
9171 case GT_EXPR:
9172 case MEMBER_REF:
9173 case DOTSTAR_EXPR:
9174 return build_x_binary_op
9175 (TREE_CODE (t),
9176 RECUR (TREE_OPERAND (t, 0)),
9177 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
9178 ? ERROR_MARK
9179 : TREE_CODE (TREE_OPERAND (t, 0))),
9180 RECUR (TREE_OPERAND (t, 1)),
9181 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
9182 ? ERROR_MARK
9183 : TREE_CODE (TREE_OPERAND (t, 1))),
9184 /*overloaded_p=*/NULL);
9186 case SCOPE_REF:
9187 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
9188 /*address_p=*/false);
9189 case ARRAY_REF:
9190 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9191 args, complain, in_decl);
9192 return build_x_binary_op (ARRAY_REF, op1,
9193 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
9194 ? ERROR_MARK
9195 : TREE_CODE (TREE_OPERAND (t, 0))),
9196 RECUR (TREE_OPERAND (t, 1)),
9197 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
9198 ? ERROR_MARK
9199 : TREE_CODE (TREE_OPERAND (t, 1))),
9200 /*overloaded_p=*/NULL);
9202 case SIZEOF_EXPR:
9203 case ALIGNOF_EXPR:
9204 op1 = TREE_OPERAND (t, 0);
9205 if (!args)
9207 /* When there are no ARGS, we are trying to evaluate a
9208 non-dependent expression from the parser. Trying to do
9209 the substitutions may not work. */
9210 if (!TYPE_P (op1))
9211 op1 = TREE_TYPE (op1);
9213 else
9215 ++skip_evaluation;
9216 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9217 /*function_p=*/false,
9218 /*integral_constant_expression_p=*/false);
9219 --skip_evaluation;
9221 if (TYPE_P (op1))
9222 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
9223 else
9224 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
9226 case MODOP_EXPR:
9228 tree r = build_x_modify_expr
9229 (RECUR (TREE_OPERAND (t, 0)),
9230 TREE_CODE (TREE_OPERAND (t, 1)),
9231 RECUR (TREE_OPERAND (t, 2)));
9232 /* TREE_NO_WARNING must be set if either the expression was
9233 parenthesized or it uses an operator such as >>= rather
9234 than plain assignment. In the former case, it was already
9235 set and must be copied. In the latter case,
9236 build_x_modify_expr sets it and it must not be reset
9237 here. */
9238 if (TREE_NO_WARNING (t))
9239 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9240 return r;
9243 case ARROW_EXPR:
9244 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9245 args, complain, in_decl);
9246 /* Remember that there was a reference to this entity. */
9247 if (DECL_P (op1))
9248 mark_used (op1);
9249 return build_x_arrow (op1);
9251 case NEW_EXPR:
9252 return build_new
9253 (RECUR (TREE_OPERAND (t, 0)),
9254 RECUR (TREE_OPERAND (t, 1)),
9255 RECUR (TREE_OPERAND (t, 2)),
9256 RECUR (TREE_OPERAND (t, 3)),
9257 NEW_EXPR_USE_GLOBAL (t));
9259 case DELETE_EXPR:
9260 return delete_sanity
9261 (RECUR (TREE_OPERAND (t, 0)),
9262 RECUR (TREE_OPERAND (t, 1)),
9263 DELETE_EXPR_USE_VEC (t),
9264 DELETE_EXPR_USE_GLOBAL (t));
9266 case COMPOUND_EXPR:
9267 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9268 RECUR (TREE_OPERAND (t, 1)));
9270 case CALL_EXPR:
9272 tree function;
9273 tree call_args;
9274 bool qualified_p;
9275 bool koenig_p;
9277 function = CALL_EXPR_FN (t);
9278 /* When we parsed the expression, we determined whether or
9279 not Koenig lookup should be performed. */
9280 koenig_p = KOENIG_LOOKUP_P (t);
9281 if (TREE_CODE (function) == SCOPE_REF)
9283 qualified_p = true;
9284 function = tsubst_qualified_id (function, args, complain, in_decl,
9285 /*done=*/false,
9286 /*address_p=*/false);
9288 else
9290 if (TREE_CODE (function) == COMPONENT_REF)
9292 tree op = TREE_OPERAND (function, 1);
9294 qualified_p = (TREE_CODE (op) == SCOPE_REF
9295 || (BASELINK_P (op)
9296 && BASELINK_QUALIFIED_P (op)));
9298 else
9299 qualified_p = false;
9301 function = tsubst_copy_and_build (function, args, complain,
9302 in_decl,
9303 !qualified_p,
9304 integral_constant_expression_p);
9306 if (BASELINK_P (function))
9307 qualified_p = true;
9310 /* FIXME: Rewrite this so as not to construct an arglist. */
9311 call_args = RECUR (CALL_EXPR_ARGS (t));
9313 /* We do not perform argument-dependent lookup if normal
9314 lookup finds a non-function, in accordance with the
9315 expected resolution of DR 218. */
9316 if (koenig_p
9317 && ((is_overloaded_fn (function)
9318 /* If lookup found a member function, the Koenig lookup is
9319 not appropriate, even if an unqualified-name was used
9320 to denote the function. */
9321 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
9322 || TREE_CODE (function) == IDENTIFIER_NODE))
9323 function = perform_koenig_lookup (function, call_args);
9325 if (TREE_CODE (function) == IDENTIFIER_NODE)
9327 unqualified_name_lookup_error (function);
9328 return error_mark_node;
9331 /* Remember that there was a reference to this entity. */
9332 if (DECL_P (function))
9333 mark_used (function);
9335 if (TREE_CODE (function) == OFFSET_REF)
9336 return build_offset_ref_call_from_tree (function, call_args);
9337 if (TREE_CODE (function) == COMPONENT_REF)
9339 if (!BASELINK_P (TREE_OPERAND (function, 1)))
9340 return finish_call_expr (function, call_args,
9341 /*disallow_virtual=*/false,
9342 /*koenig_p=*/false);
9343 else
9344 return (build_new_method_call
9345 (TREE_OPERAND (function, 0),
9346 TREE_OPERAND (function, 1),
9347 call_args, NULL_TREE,
9348 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9349 /*fn_p=*/NULL));
9351 return finish_call_expr (function, call_args,
9352 /*disallow_virtual=*/qualified_p,
9353 koenig_p);
9356 case COND_EXPR:
9357 return build_x_conditional_expr
9358 (RECUR (TREE_OPERAND (t, 0)),
9359 RECUR (TREE_OPERAND (t, 1)),
9360 RECUR (TREE_OPERAND (t, 2)));
9362 case PSEUDO_DTOR_EXPR:
9363 return finish_pseudo_destructor_expr
9364 (RECUR (TREE_OPERAND (t, 0)),
9365 RECUR (TREE_OPERAND (t, 1)),
9366 RECUR (TREE_OPERAND (t, 2)));
9368 case TREE_LIST:
9370 tree purpose, value, chain;
9372 if (t == void_list_node)
9373 return t;
9375 purpose = TREE_PURPOSE (t);
9376 if (purpose)
9377 purpose = RECUR (purpose);
9378 value = TREE_VALUE (t);
9379 if (value)
9380 value = RECUR (value);
9381 chain = TREE_CHAIN (t);
9382 if (chain && chain != void_type_node)
9383 chain = RECUR (chain);
9384 if (purpose == TREE_PURPOSE (t)
9385 && value == TREE_VALUE (t)
9386 && chain == TREE_CHAIN (t))
9387 return t;
9388 return tree_cons (purpose, value, chain);
9391 case COMPONENT_REF:
9393 tree object;
9394 tree object_type;
9395 tree member;
9397 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9398 args, complain, in_decl);
9399 /* Remember that there was a reference to this entity. */
9400 if (DECL_P (object))
9401 mark_used (object);
9402 object_type = TREE_TYPE (object);
9404 member = TREE_OPERAND (t, 1);
9405 if (BASELINK_P (member))
9406 member = tsubst_baselink (member,
9407 non_reference (TREE_TYPE (object)),
9408 args, complain, in_decl);
9409 else
9410 member = tsubst_copy (member, args, complain, in_decl);
9411 if (member == error_mark_node)
9412 return error_mark_node;
9414 if (object_type && !CLASS_TYPE_P (object_type))
9416 if (TREE_CODE (member) == BIT_NOT_EXPR)
9417 return finish_pseudo_destructor_expr (object,
9418 NULL_TREE,
9419 object_type);
9420 else if (TREE_CODE (member) == SCOPE_REF
9421 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
9422 return finish_pseudo_destructor_expr (object,
9423 object,
9424 object_type);
9426 else if (TREE_CODE (member) == SCOPE_REF
9427 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9429 tree tmpl;
9430 tree args;
9432 /* Lookup the template functions now that we know what the
9433 scope is. */
9434 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9435 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
9436 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
9437 /*is_type_p=*/false,
9438 /*complain=*/false);
9439 if (BASELINK_P (member))
9441 BASELINK_FUNCTIONS (member)
9442 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9443 args);
9444 member = (adjust_result_of_qualified_name_lookup
9445 (member, BINFO_TYPE (BASELINK_BINFO (member)),
9446 object_type));
9448 else
9450 qualified_name_lookup_error (object_type, tmpl, member);
9451 return error_mark_node;
9454 else if (TREE_CODE (member) == SCOPE_REF
9455 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9456 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9458 if (complain & tf_error)
9460 if (TYPE_P (TREE_OPERAND (member, 0)))
9461 error ("%qT is not a class or namespace",
9462 TREE_OPERAND (member, 0));
9463 else
9464 error ("%qD is not a class or namespace",
9465 TREE_OPERAND (member, 0));
9467 return error_mark_node;
9469 else if (TREE_CODE (member) == FIELD_DECL)
9470 return finish_non_static_data_member (member, object, NULL_TREE);
9472 return finish_class_member_access_expr (object, member,
9473 /*template_p=*/false);
9476 case THROW_EXPR:
9477 return build_throw
9478 (RECUR (TREE_OPERAND (t, 0)));
9480 case CONSTRUCTOR:
9482 VEC(constructor_elt,gc) *n;
9483 constructor_elt *ce;
9484 unsigned HOST_WIDE_INT idx;
9485 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9486 bool process_index_p;
9488 if (type == error_mark_node)
9489 return error_mark_node;
9491 /* digest_init will do the wrong thing if we let it. */
9492 if (type && TYPE_PTRMEMFUNC_P (type))
9493 return t;
9495 /* We do not want to process the index of aggregate
9496 initializers as they are identifier nodes which will be
9497 looked up by digest_init. */
9498 process_index_p = !(type && IS_AGGR_TYPE (type));
9500 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9501 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9503 if (ce->index && process_index_p)
9504 ce->index = RECUR (ce->index);
9505 ce->value = RECUR (ce->value);
9508 if (TREE_HAS_CONSTRUCTOR (t))
9509 return finish_compound_literal (type, n);
9511 return build_constructor (NULL_TREE, n);
9514 case TYPEID_EXPR:
9516 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9517 if (TYPE_P (operand_0))
9518 return get_typeid (operand_0);
9519 return build_typeid (operand_0);
9522 case VAR_DECL:
9523 if (!args)
9524 return t;
9525 /* Fall through */
9527 case PARM_DECL:
9529 tree r = tsubst_copy (t, args, complain, in_decl);
9531 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9532 /* If the original type was a reference, we'll be wrapped in
9533 the appropriate INDIRECT_REF. */
9534 r = convert_from_reference (r);
9535 return r;
9538 case VA_ARG_EXPR:
9539 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9540 tsubst_copy (TREE_TYPE (t), args, complain,
9541 in_decl));
9543 case OFFSETOF_EXPR:
9544 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
9546 case STMT_EXPR:
9548 tree old_stmt_expr = cur_stmt_expr;
9549 tree stmt_expr = begin_stmt_expr ();
9551 cur_stmt_expr = stmt_expr;
9552 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9553 integral_constant_expression_p);
9554 stmt_expr = finish_stmt_expr (stmt_expr, false);
9555 cur_stmt_expr = old_stmt_expr;
9557 return stmt_expr;
9560 case CONST_DECL:
9561 t = tsubst_copy (t, args, complain, in_decl);
9562 /* As in finish_id_expression, we resolve enumeration constants
9563 to their underlying values. */
9564 if (TREE_CODE (t) == CONST_DECL)
9566 used_types_insert (TREE_TYPE (t));
9567 return DECL_INITIAL (t);
9569 return t;
9571 default:
9572 /* Handle Objective-C++ constructs, if appropriate. */
9574 tree subst
9575 = objcp_tsubst_copy_and_build (t, args, complain,
9576 in_decl, /*function_p=*/false);
9577 if (subst)
9578 return subst;
9580 return tsubst_copy (t, args, complain, in_decl);
9583 #undef RECUR
9586 /* Verify that the instantiated ARGS are valid. For type arguments,
9587 make sure that the type's linkage is ok. For non-type arguments,
9588 make sure they are constants if they are integral or enumerations.
9589 Emit an error under control of COMPLAIN, and return TRUE on error. */
9591 static bool
9592 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9594 int ix, len = DECL_NTPARMS (tmpl);
9595 bool result = false;
9597 for (ix = 0; ix != len; ix++)
9599 tree t = TREE_VEC_ELT (args, ix);
9601 if (TYPE_P (t))
9603 /* [basic.link]: A name with no linkage (notably, the name
9604 of a class or enumeration declared in a local scope)
9605 shall not be used to declare an entity with linkage.
9606 This implies that names with no linkage cannot be used as
9607 template arguments. */
9608 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9610 if (nt)
9612 /* DR 488 makes use of a type with no linkage cause
9613 type deduction to fail. */
9614 if (complain & tf_error)
9616 if (TYPE_ANONYMOUS_P (nt))
9617 error ("%qT is/uses anonymous type", t);
9618 else
9619 error ("template argument for %qD uses local type %qT",
9620 tmpl, t);
9622 result = true;
9624 /* In order to avoid all sorts of complications, we do not
9625 allow variably-modified types as template arguments. */
9626 else if (variably_modified_type_p (t, NULL_TREE))
9628 if (complain & tf_error)
9629 error ("%qT is a variably modified type", t);
9630 result = true;
9633 /* A non-type argument of integral or enumerated type must be a
9634 constant. */
9635 else if (TREE_TYPE (t)
9636 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9637 && !TREE_CONSTANT (t))
9639 if (complain & tf_error)
9640 error ("integral expression %qE is not constant", t);
9641 result = true;
9644 if (result && (complain & tf_error))
9645 error (" trying to instantiate %qD", tmpl);
9646 return result;
9649 /* Instantiate the indicated variable or function template TMPL with
9650 the template arguments in TARG_PTR. */
9652 tree
9653 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9655 tree fndecl;
9656 tree gen_tmpl;
9657 tree spec;
9658 HOST_WIDE_INT saved_processing_template_decl;
9660 if (tmpl == error_mark_node)
9661 return error_mark_node;
9663 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9665 /* If this function is a clone, handle it specially. */
9666 if (DECL_CLONED_FUNCTION_P (tmpl))
9668 tree spec;
9669 tree clone;
9671 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9672 complain);
9673 if (spec == error_mark_node)
9674 return error_mark_node;
9676 /* Look for the clone. */
9677 FOR_EACH_CLONE (clone, spec)
9678 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9679 return clone;
9680 /* We should always have found the clone by now. */
9681 gcc_unreachable ();
9682 return NULL_TREE;
9685 /* Check to see if we already have this specialization. */
9686 spec = retrieve_specialization (tmpl, targ_ptr,
9687 /*class_specializations_p=*/false);
9688 if (spec != NULL_TREE)
9689 return spec;
9691 gen_tmpl = most_general_template (tmpl);
9692 if (tmpl != gen_tmpl)
9694 /* The TMPL is a partial instantiation. To get a full set of
9695 arguments we must add the arguments used to perform the
9696 partial instantiation. */
9697 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9698 targ_ptr);
9700 /* Check to see if we already have this specialization. */
9701 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9702 /*class_specializations_p=*/false);
9703 if (spec != NULL_TREE)
9704 return spec;
9707 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9708 complain))
9709 return error_mark_node;
9711 /* We are building a FUNCTION_DECL, during which the access of its
9712 parameters and return types have to be checked. However this
9713 FUNCTION_DECL which is the desired context for access checking
9714 is not built yet. We solve this chicken-and-egg problem by
9715 deferring all checks until we have the FUNCTION_DECL. */
9716 push_deferring_access_checks (dk_deferred);
9718 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9719 (because, for example, we have encountered a non-dependent
9720 function call in the body of a template function and must now
9721 determine which of several overloaded functions will be called),
9722 within the instantiation itself we are not processing a
9723 template. */
9724 saved_processing_template_decl = processing_template_decl;
9725 processing_template_decl = 0;
9726 /* Substitute template parameters to obtain the specialization. */
9727 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9728 targ_ptr, complain, gen_tmpl);
9729 processing_template_decl = saved_processing_template_decl;
9730 if (fndecl == error_mark_node)
9731 return error_mark_node;
9733 /* Now we know the specialization, compute access previously
9734 deferred. */
9735 push_access_scope (fndecl);
9736 perform_deferred_access_checks ();
9737 pop_access_scope (fndecl);
9738 pop_deferring_access_checks ();
9740 /* The DECL_TI_TEMPLATE should always be the immediate parent
9741 template, not the most general template. */
9742 DECL_TI_TEMPLATE (fndecl) = tmpl;
9744 /* If we've just instantiated the main entry point for a function,
9745 instantiate all the alternate entry points as well. We do this
9746 by cloning the instantiation of the main entry point, not by
9747 instantiating the template clones. */
9748 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9749 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9751 return fndecl;
9754 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9755 arguments that are being used when calling it. TARGS is a vector
9756 into which the deduced template arguments are placed.
9758 Return zero for success, 2 for an incomplete match that doesn't resolve
9759 all the types, and 1 for complete failure. An error message will be
9760 printed only for an incomplete match.
9762 If FN is a conversion operator, or we are trying to produce a specific
9763 specialization, RETURN_TYPE is the return type desired.
9765 The EXPLICIT_TARGS are explicit template arguments provided via a
9766 template-id.
9768 The parameter STRICT is one of:
9770 DEDUCE_CALL:
9771 We are deducing arguments for a function call, as in
9772 [temp.deduct.call].
9774 DEDUCE_CONV:
9775 We are deducing arguments for a conversion function, as in
9776 [temp.deduct.conv].
9778 DEDUCE_EXACT:
9779 We are deducing arguments when doing an explicit instantiation
9780 as in [temp.explicit], when determining an explicit specialization
9781 as in [temp.expl.spec], or when taking the address of a function
9782 template, as in [temp.deduct.funcaddr]. */
9785 fn_type_unification (tree fn,
9786 tree explicit_targs,
9787 tree targs,
9788 tree args,
9789 tree return_type,
9790 unification_kind_t strict,
9791 int flags)
9793 tree parms;
9794 tree fntype;
9795 int result;
9797 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9799 fntype = TREE_TYPE (fn);
9800 if (explicit_targs)
9802 /* [temp.deduct]
9804 The specified template arguments must match the template
9805 parameters in kind (i.e., type, nontype, template), and there
9806 must not be more arguments than there are parameters;
9807 otherwise type deduction fails.
9809 Nontype arguments must match the types of the corresponding
9810 nontype template parameters, or must be convertible to the
9811 types of the corresponding nontype parameters as specified in
9812 _temp.arg.nontype_, otherwise type deduction fails.
9814 All references in the function type of the function template
9815 to the corresponding template parameters are replaced by the
9816 specified template argument values. If a substitution in a
9817 template parameter or in the function type of the function
9818 template results in an invalid type, type deduction fails. */
9819 int i;
9820 tree converted_args;
9821 bool incomplete;
9823 if (explicit_targs == error_mark_node)
9824 return 1;
9826 converted_args
9827 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9828 explicit_targs, NULL_TREE, tf_none,
9829 /*require_all_args=*/false,
9830 /*use_default_args=*/false));
9831 if (converted_args == error_mark_node)
9832 return 1;
9834 /* Substitute the explicit args into the function type. This is
9835 necessary so that, for instance, explicitly declared function
9836 arguments can match null pointed constants. If we were given
9837 an incomplete set of explicit args, we must not do semantic
9838 processing during substitution as we could create partial
9839 instantiations. */
9840 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9841 processing_template_decl += incomplete;
9842 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9843 processing_template_decl -= incomplete;
9845 if (fntype == error_mark_node)
9846 return 1;
9848 /* Place the explicitly specified arguments in TARGS. */
9849 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9850 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9853 /* Never do unification on the 'this' parameter. */
9854 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
9856 if (return_type)
9858 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9859 args = tree_cons (NULL_TREE, return_type, args);
9862 /* We allow incomplete unification without an error message here
9863 because the standard doesn't seem to explicitly prohibit it. Our
9864 callers must be ready to deal with unification failures in any
9865 event. */
9866 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9867 targs, parms, args, /*subr=*/0,
9868 strict, flags);
9870 if (result == 0)
9871 /* All is well so far. Now, check:
9873 [temp.deduct]
9875 When all template arguments have been deduced, all uses of
9876 template parameters in nondeduced contexts are replaced with
9877 the corresponding deduced argument values. If the
9878 substitution results in an invalid type, as described above,
9879 type deduction fails. */
9880 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9881 == error_mark_node)
9882 return 1;
9884 return result;
9887 /* Adjust types before performing type deduction, as described in
9888 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9889 sections are symmetric. PARM is the type of a function parameter
9890 or the return type of the conversion function. ARG is the type of
9891 the argument passed to the call, or the type of the value
9892 initialized with the result of the conversion function. */
9894 static int
9895 maybe_adjust_types_for_deduction (unification_kind_t strict,
9896 tree* parm,
9897 tree* arg)
9899 int result = 0;
9901 switch (strict)
9903 case DEDUCE_CALL:
9904 break;
9906 case DEDUCE_CONV:
9908 /* Swap PARM and ARG throughout the remainder of this
9909 function; the handling is precisely symmetric since PARM
9910 will initialize ARG rather than vice versa. */
9911 tree* temp = parm;
9912 parm = arg;
9913 arg = temp;
9914 break;
9917 case DEDUCE_EXACT:
9918 /* There is nothing to do in this case. */
9919 return 0;
9921 default:
9922 gcc_unreachable ();
9925 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9927 /* [temp.deduct.call]
9929 If P is not a reference type:
9931 --If A is an array type, the pointer type produced by the
9932 array-to-pointer standard conversion (_conv.array_) is
9933 used in place of A for type deduction; otherwise,
9935 --If A is a function type, the pointer type produced by
9936 the function-to-pointer standard conversion
9937 (_conv.func_) is used in place of A for type deduction;
9938 otherwise,
9940 --If A is a cv-qualified type, the top level
9941 cv-qualifiers of A's type are ignored for type
9942 deduction. */
9943 if (TREE_CODE (*arg) == ARRAY_TYPE)
9944 *arg = build_pointer_type (TREE_TYPE (*arg));
9945 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9946 *arg = build_pointer_type (*arg);
9947 else
9948 *arg = TYPE_MAIN_VARIANT (*arg);
9951 /* [temp.deduct.call]
9953 If P is a cv-qualified type, the top level cv-qualifiers
9954 of P's type are ignored for type deduction. If P is a
9955 reference type, the type referred to by P is used for
9956 type deduction. */
9957 *parm = TYPE_MAIN_VARIANT (*parm);
9958 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9960 *parm = TREE_TYPE (*parm);
9961 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9964 /* DR 322. For conversion deduction, remove a reference type on parm
9965 too (which has been swapped into ARG). */
9966 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9967 *arg = TREE_TYPE (*arg);
9969 return result;
9972 /* Most parms like fn_type_unification.
9974 If SUBR is 1, we're being called recursively (to unify the
9975 arguments of a function or method parameter of a function
9976 template). */
9978 static int
9979 type_unification_real (tree tparms,
9980 tree targs,
9981 tree xparms,
9982 tree xargs,
9983 int subr,
9984 unification_kind_t strict,
9985 int flags)
9987 tree parm, arg;
9988 int i;
9989 int ntparms = TREE_VEC_LENGTH (tparms);
9990 int sub_strict;
9991 int saw_undeduced = 0;
9992 tree parms, args;
9994 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9995 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9996 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9997 gcc_assert (ntparms > 0);
9999 switch (strict)
10001 case DEDUCE_CALL:
10002 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
10003 | UNIFY_ALLOW_DERIVED);
10004 break;
10006 case DEDUCE_CONV:
10007 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
10008 break;
10010 case DEDUCE_EXACT:
10011 sub_strict = UNIFY_ALLOW_NONE;
10012 break;
10014 default:
10015 gcc_unreachable ();
10018 again:
10019 parms = xparms;
10020 args = xargs;
10022 while (parms && parms != void_list_node
10023 && args && args != void_list_node)
10025 parm = TREE_VALUE (parms);
10026 parms = TREE_CHAIN (parms);
10027 arg = TREE_VALUE (args);
10028 args = TREE_CHAIN (args);
10030 if (arg == error_mark_node)
10031 return 1;
10032 if (arg == unknown_type_node)
10033 /* We can't deduce anything from this, but we might get all the
10034 template args from other function args. */
10035 continue;
10037 /* Conversions will be performed on a function argument that
10038 corresponds with a function parameter that contains only
10039 non-deducible template parameters and explicitly specified
10040 template parameters. */
10041 if (!uses_template_parms (parm))
10043 tree type;
10045 if (!TYPE_P (arg))
10046 type = TREE_TYPE (arg);
10047 else
10048 type = arg;
10050 if (same_type_p (parm, type))
10051 continue;
10052 if (strict != DEDUCE_EXACT
10053 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
10054 flags))
10055 continue;
10057 return 1;
10060 if (!TYPE_P (arg))
10062 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
10063 if (type_unknown_p (arg))
10065 /* [temp.deduct.type] A template-argument can be deduced from
10066 a pointer to function or pointer to member function
10067 argument if the set of overloaded functions does not
10068 contain function templates and at most one of a set of
10069 overloaded functions provides a unique match. */
10071 if (resolve_overloaded_unification
10072 (tparms, targs, parm, arg, strict, sub_strict)
10073 != 0)
10074 return 1;
10075 continue;
10077 arg = TREE_TYPE (arg);
10078 if (arg == error_mark_node)
10079 return 1;
10083 int arg_strict = sub_strict;
10085 if (!subr)
10086 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
10088 if (unify (tparms, targs, parm, arg, arg_strict))
10089 return 1;
10093 /* Fail if we've reached the end of the parm list, and more args
10094 are present, and the parm list isn't variadic. */
10095 if (args && args != void_list_node && parms == void_list_node)
10096 return 1;
10097 /* Fail if parms are left and they don't have default values. */
10098 if (parms && parms != void_list_node
10099 && TREE_PURPOSE (parms) == NULL_TREE)
10100 return 1;
10102 if (!subr)
10103 for (i = 0; i < ntparms; i++)
10104 if (!TREE_VEC_ELT (targs, i))
10106 tree tparm;
10108 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
10109 continue;
10111 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
10113 /* If this is an undeduced nontype parameter that depends on
10114 a type parameter, try another pass; its type may have been
10115 deduced from a later argument than the one from which
10116 this parameter can be deduced. */
10117 if (TREE_CODE (tparm) == PARM_DECL
10118 && uses_template_parms (TREE_TYPE (tparm))
10119 && !saw_undeduced++)
10120 goto again;
10122 return 2;
10125 return 0;
10128 /* Subroutine of type_unification_real. Args are like the variables at the
10129 call site. ARG is an overloaded function (or template-id); we try
10130 deducing template args from each of the overloads, and if only one
10131 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
10133 static int
10134 resolve_overloaded_unification (tree tparms,
10135 tree targs,
10136 tree parm,
10137 tree arg,
10138 unification_kind_t strict,
10139 int sub_strict)
10141 tree tempargs = copy_node (targs);
10142 int good = 0;
10143 bool addr_p;
10145 if (TREE_CODE (arg) == ADDR_EXPR)
10147 arg = TREE_OPERAND (arg, 0);
10148 addr_p = true;
10150 else
10151 addr_p = false;
10153 if (TREE_CODE (arg) == COMPONENT_REF)
10154 /* Handle `&x' where `x' is some static or non-static member
10155 function name. */
10156 arg = TREE_OPERAND (arg, 1);
10158 if (TREE_CODE (arg) == OFFSET_REF)
10159 arg = TREE_OPERAND (arg, 1);
10161 /* Strip baselink information. */
10162 if (BASELINK_P (arg))
10163 arg = BASELINK_FUNCTIONS (arg);
10165 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
10167 /* If we got some explicit template args, we need to plug them into
10168 the affected templates before we try to unify, in case the
10169 explicit args will completely resolve the templates in question. */
10171 tree expl_subargs = TREE_OPERAND (arg, 1);
10172 arg = TREE_OPERAND (arg, 0);
10174 for (; arg; arg = OVL_NEXT (arg))
10176 tree fn = OVL_CURRENT (arg);
10177 tree subargs, elem;
10179 if (TREE_CODE (fn) != TEMPLATE_DECL)
10180 continue;
10182 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
10183 expl_subargs, /*check_ret=*/false);
10184 if (subargs)
10186 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
10187 good += try_one_overload (tparms, targs, tempargs, parm,
10188 elem, strict, sub_strict, addr_p);
10192 else
10194 gcc_assert (TREE_CODE (arg) == OVERLOAD
10195 || TREE_CODE (arg) == FUNCTION_DECL);
10197 for (; arg; arg = OVL_NEXT (arg))
10198 good += try_one_overload (tparms, targs, tempargs, parm,
10199 TREE_TYPE (OVL_CURRENT (arg)),
10200 strict, sub_strict, addr_p);
10203 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10204 to function or pointer to member function argument if the set of
10205 overloaded functions does not contain function templates and at most
10206 one of a set of overloaded functions provides a unique match.
10208 So if we found multiple possibilities, we return success but don't
10209 deduce anything. */
10211 if (good == 1)
10213 int i = TREE_VEC_LENGTH (targs);
10214 for (; i--; )
10215 if (TREE_VEC_ELT (tempargs, i))
10216 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
10218 if (good)
10219 return 0;
10221 return 1;
10224 /* Subroutine of resolve_overloaded_unification; does deduction for a single
10225 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10226 different overloads deduce different arguments for a given parm.
10227 ADDR_P is true if the expression for which deduction is being
10228 performed was of the form "& fn" rather than simply "fn".
10230 Returns 1 on success. */
10232 static int
10233 try_one_overload (tree tparms,
10234 tree orig_targs,
10235 tree targs,
10236 tree parm,
10237 tree arg,
10238 unification_kind_t strict,
10239 int sub_strict,
10240 bool addr_p)
10242 int nargs;
10243 tree tempargs;
10244 int i;
10246 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10247 to function or pointer to member function argument if the set of
10248 overloaded functions does not contain function templates and at most
10249 one of a set of overloaded functions provides a unique match.
10251 So if this is a template, just return success. */
10253 if (uses_template_parms (arg))
10254 return 1;
10256 if (TREE_CODE (arg) == METHOD_TYPE)
10257 arg = build_ptrmemfunc_type (build_pointer_type (arg));
10258 else if (addr_p)
10259 arg = build_pointer_type (arg);
10261 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
10263 /* We don't copy orig_targs for this because if we have already deduced
10264 some template args from previous args, unify would complain when we
10265 try to deduce a template parameter for the same argument, even though
10266 there isn't really a conflict. */
10267 nargs = TREE_VEC_LENGTH (targs);
10268 tempargs = make_tree_vec (nargs);
10270 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
10271 return 0;
10273 /* First make sure we didn't deduce anything that conflicts with
10274 explicitly specified args. */
10275 for (i = nargs; i--; )
10277 tree elt = TREE_VEC_ELT (tempargs, i);
10278 tree oldelt = TREE_VEC_ELT (orig_targs, i);
10280 if (!elt)
10281 /*NOP*/;
10282 else if (uses_template_parms (elt))
10283 /* Since we're unifying against ourselves, we will fill in
10284 template args used in the function parm list with our own
10285 template parms. Discard them. */
10286 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10287 else if (oldelt && !template_args_equal (oldelt, elt))
10288 return 0;
10291 for (i = nargs; i--; )
10293 tree elt = TREE_VEC_ELT (tempargs, i);
10295 if (elt)
10296 TREE_VEC_ELT (targs, i) = elt;
10299 return 1;
10302 /* PARM is a template class (perhaps with unbound template
10303 parameters). ARG is a fully instantiated type. If ARG can be
10304 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10305 TARGS are as for unify. */
10307 static tree
10308 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
10310 tree copy_of_targs;
10312 if (!CLASSTYPE_TEMPLATE_INFO (arg)
10313 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
10314 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
10315 return NULL_TREE;
10317 /* We need to make a new template argument vector for the call to
10318 unify. If we used TARGS, we'd clutter it up with the result of
10319 the attempted unification, even if this class didn't work out.
10320 We also don't want to commit ourselves to all the unifications
10321 we've already done, since unification is supposed to be done on
10322 an argument-by-argument basis. In other words, consider the
10323 following pathological case:
10325 template <int I, int J, int K>
10326 struct S {};
10328 template <int I, int J>
10329 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
10331 template <int I, int J, int K>
10332 void f(S<I, J, K>, S<I, I, I>);
10334 void g() {
10335 S<0, 0, 0> s0;
10336 S<0, 1, 2> s2;
10338 f(s0, s2);
10341 Now, by the time we consider the unification involving `s2', we
10342 already know that we must have `f<0, 0, 0>'. But, even though
10343 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
10344 because there are two ways to unify base classes of S<0, 1, 2>
10345 with S<I, I, I>. If we kept the already deduced knowledge, we
10346 would reject the possibility I=1. */
10347 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
10349 /* If unification failed, we're done. */
10350 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
10351 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
10352 return NULL_TREE;
10354 return arg;
10357 /* Given a template type PARM and a class type ARG, find the unique
10358 base type in ARG that is an instance of PARM. We do not examine
10359 ARG itself; only its base-classes. If there is not exactly one
10360 appropriate base class, return NULL_TREE. PARM may be the type of
10361 a partial specialization, as well as a plain template type. Used
10362 by unify. */
10364 static tree
10365 get_template_base (tree tparms, tree targs, tree parm, tree arg)
10367 tree rval = NULL_TREE;
10368 tree binfo;
10370 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
10372 binfo = TYPE_BINFO (complete_type (arg));
10373 if (!binfo)
10374 /* The type could not be completed. */
10375 return NULL_TREE;
10377 /* Walk in inheritance graph order. The search order is not
10378 important, and this avoids multiple walks of virtual bases. */
10379 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
10381 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
10383 if (r)
10385 /* If there is more than one satisfactory baseclass, then:
10387 [temp.deduct.call]
10389 If they yield more than one possible deduced A, the type
10390 deduction fails.
10392 applies. */
10393 if (rval && !same_type_p (r, rval))
10394 return NULL_TREE;
10396 rval = r;
10400 return rval;
10403 /* Returns the level of DECL, which declares a template parameter. */
10405 static int
10406 template_decl_level (tree decl)
10408 switch (TREE_CODE (decl))
10410 case TYPE_DECL:
10411 case TEMPLATE_DECL:
10412 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10414 case PARM_DECL:
10415 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10417 default:
10418 gcc_unreachable ();
10420 return 0;
10423 /* Decide whether ARG can be unified with PARM, considering only the
10424 cv-qualifiers of each type, given STRICT as documented for unify.
10425 Returns nonzero iff the unification is OK on that basis. */
10427 static int
10428 check_cv_quals_for_unify (int strict, tree arg, tree parm)
10430 int arg_quals = cp_type_quals (arg);
10431 int parm_quals = cp_type_quals (parm);
10433 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10434 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10436 /* Although a CVR qualifier is ignored when being applied to a
10437 substituted template parameter ([8.3.2]/1 for example), that
10438 does not apply during deduction [14.8.2.4]/1, (even though
10439 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10440 this). Except when we're allowing additional CV qualifiers
10441 at the outer level [14.8.2.1]/3,1st bullet. */
10442 if ((TREE_CODE (arg) == REFERENCE_TYPE
10443 || TREE_CODE (arg) == FUNCTION_TYPE
10444 || TREE_CODE (arg) == METHOD_TYPE)
10445 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10446 return 0;
10448 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10449 && (parm_quals & TYPE_QUAL_RESTRICT))
10450 return 0;
10453 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10454 && (arg_quals & parm_quals) != parm_quals)
10455 return 0;
10457 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
10458 && (parm_quals & arg_quals) != arg_quals)
10459 return 0;
10461 return 1;
10464 /* Deduce the value of template parameters. TPARMS is the (innermost)
10465 set of template parameters to a template. TARGS is the bindings
10466 for those template parameters, as determined thus far; TARGS may
10467 include template arguments for outer levels of template parameters
10468 as well. PARM is a parameter to a template function, or a
10469 subcomponent of that parameter; ARG is the corresponding argument.
10470 This function attempts to match PARM with ARG in a manner
10471 consistent with the existing assignments in TARGS. If more values
10472 are deduced, then TARGS is updated.
10474 Returns 0 if the type deduction succeeds, 1 otherwise. The
10475 parameter STRICT is a bitwise or of the following flags:
10477 UNIFY_ALLOW_NONE:
10478 Require an exact match between PARM and ARG.
10479 UNIFY_ALLOW_MORE_CV_QUAL:
10480 Allow the deduced ARG to be more cv-qualified (by qualification
10481 conversion) than ARG.
10482 UNIFY_ALLOW_LESS_CV_QUAL:
10483 Allow the deduced ARG to be less cv-qualified than ARG.
10484 UNIFY_ALLOW_DERIVED:
10485 Allow the deduced ARG to be a template base class of ARG,
10486 or a pointer to a template base class of the type pointed to by
10487 ARG.
10488 UNIFY_ALLOW_INTEGER:
10489 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10490 case for more information.
10491 UNIFY_ALLOW_OUTER_LEVEL:
10492 This is the outermost level of a deduction. Used to determine validity
10493 of qualification conversions. A valid qualification conversion must
10494 have const qualified pointers leading up to the inner type which
10495 requires additional CV quals, except at the outer level, where const
10496 is not required [conv.qual]. It would be normal to set this flag in
10497 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10498 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10499 This is the outermost level of a deduction, and PARM can be more CV
10500 qualified at this point.
10501 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10502 This is the outermost level of a deduction, and PARM can be less CV
10503 qualified at this point. */
10505 static int
10506 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10508 int idx;
10509 tree targ;
10510 tree tparm;
10511 int strict_in = strict;
10513 /* I don't think this will do the right thing with respect to types.
10514 But the only case I've seen it in so far has been array bounds, where
10515 signedness is the only information lost, and I think that will be
10516 okay. */
10517 while (TREE_CODE (parm) == NOP_EXPR)
10518 parm = TREE_OPERAND (parm, 0);
10520 if (arg == error_mark_node)
10521 return 1;
10522 if (arg == unknown_type_node)
10523 /* We can't deduce anything from this, but we might get all the
10524 template args from other function args. */
10525 return 0;
10527 /* If PARM uses template parameters, then we can't bail out here,
10528 even if ARG == PARM, since we won't record unifications for the
10529 template parameters. We might need them if we're trying to
10530 figure out which of two things is more specialized. */
10531 if (arg == parm && !uses_template_parms (parm))
10532 return 0;
10534 /* Immediately reject some pairs that won't unify because of
10535 cv-qualification mismatches. */
10536 if (TREE_CODE (arg) == TREE_CODE (parm)
10537 && TYPE_P (arg)
10538 /* It is the elements of the array which hold the cv quals of an array
10539 type, and the elements might be template type parms. We'll check
10540 when we recurse. */
10541 && TREE_CODE (arg) != ARRAY_TYPE
10542 /* We check the cv-qualifiers when unifying with template type
10543 parameters below. We want to allow ARG `const T' to unify with
10544 PARM `T' for example, when computing which of two templates
10545 is more specialized, for example. */
10546 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10547 && !check_cv_quals_for_unify (strict_in, arg, parm))
10548 return 1;
10550 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10551 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10552 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10553 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10554 strict &= ~UNIFY_ALLOW_DERIVED;
10555 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10556 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10558 switch (TREE_CODE (parm))
10560 case TYPENAME_TYPE:
10561 case SCOPE_REF:
10562 case UNBOUND_CLASS_TEMPLATE:
10563 /* In a type which contains a nested-name-specifier, template
10564 argument values cannot be deduced for template parameters used
10565 within the nested-name-specifier. */
10566 return 0;
10568 case TEMPLATE_TYPE_PARM:
10569 case TEMPLATE_TEMPLATE_PARM:
10570 case BOUND_TEMPLATE_TEMPLATE_PARM:
10571 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10573 if (TEMPLATE_TYPE_LEVEL (parm)
10574 != template_decl_level (tparm))
10575 /* The PARM is not one we're trying to unify. Just check
10576 to see if it matches ARG. */
10577 return (TREE_CODE (arg) == TREE_CODE (parm)
10578 && same_type_p (parm, arg)) ? 0 : 1;
10579 idx = TEMPLATE_TYPE_IDX (parm);
10580 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10581 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10583 /* Check for mixed types and values. */
10584 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10585 && TREE_CODE (tparm) != TYPE_DECL)
10586 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10587 && TREE_CODE (tparm) != TEMPLATE_DECL))
10588 return 1;
10590 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10592 /* ARG must be constructed from a template class or a template
10593 template parameter. */
10594 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10595 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
10596 return 1;
10599 tree parmvec = TYPE_TI_ARGS (parm);
10600 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10601 tree argtmplvec
10602 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10603 int i;
10605 /* The resolution to DR150 makes clear that default
10606 arguments for an N-argument may not be used to bind T
10607 to a template template parameter with fewer than N
10608 parameters. It is not safe to permit the binding of
10609 default arguments as an extension, as that may change
10610 the meaning of a conforming program. Consider:
10612 struct Dense { static const unsigned int dim = 1; };
10614 template <template <typename> class View,
10615 typename Block>
10616 void operator+(float, View<Block> const&);
10618 template <typename Block,
10619 unsigned int Dim = Block::dim>
10620 struct Lvalue_proxy { operator float() const; };
10622 void
10623 test_1d (void) {
10624 Lvalue_proxy<Dense> p;
10625 float b;
10626 b + p;
10629 Here, if Lvalue_proxy is permitted to bind to View, then
10630 the global operator+ will be used; if they are not, the
10631 Lvalue_proxy will be converted to float. */
10632 if (coerce_template_parms (argtmplvec, parmvec,
10633 TYPE_TI_TEMPLATE (parm),
10634 tf_none,
10635 /*require_all_args=*/true,
10636 /*use_default_args=*/false)
10637 == error_mark_node)
10638 return 1;
10640 /* Deduce arguments T, i from TT<T> or TT<i>.
10641 We check each element of PARMVEC and ARGVEC individually
10642 rather than the whole TREE_VEC since they can have
10643 different number of elements. */
10645 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10647 if (unify (tparms, targs,
10648 TREE_VEC_ELT (parmvec, i),
10649 TREE_VEC_ELT (argvec, i),
10650 UNIFY_ALLOW_NONE))
10651 return 1;
10654 arg = TYPE_TI_TEMPLATE (arg);
10656 /* Fall through to deduce template name. */
10659 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10660 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10662 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10664 /* Simple cases: Value already set, does match or doesn't. */
10665 if (targ != NULL_TREE && template_args_equal (targ, arg))
10666 return 0;
10667 else if (targ)
10668 return 1;
10670 else
10672 /* If PARM is `const T' and ARG is only `int', we don't have
10673 a match unless we are allowing additional qualification.
10674 If ARG is `const int' and PARM is just `T' that's OK;
10675 that binds `const int' to `T'. */
10676 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10677 arg, parm))
10678 return 1;
10680 /* Consider the case where ARG is `const volatile int' and
10681 PARM is `const T'. Then, T should be `volatile int'. */
10682 arg = cp_build_qualified_type_real
10683 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10684 if (arg == error_mark_node)
10685 return 1;
10687 /* Simple cases: Value already set, does match or doesn't. */
10688 if (targ != NULL_TREE && same_type_p (targ, arg))
10689 return 0;
10690 else if (targ)
10691 return 1;
10693 /* Make sure that ARG is not a variable-sized array. (Note
10694 that were talking about variable-sized arrays (like
10695 `int[n]'), rather than arrays of unknown size (like
10696 `int[]').) We'll get very confused by such a type since
10697 the bound of the array will not be computable in an
10698 instantiation. Besides, such types are not allowed in
10699 ISO C++, so we can do as we please here. */
10700 if (variably_modified_type_p (arg, NULL_TREE))
10701 return 1;
10704 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10705 return 0;
10707 case TEMPLATE_PARM_INDEX:
10708 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10709 if (tparm == error_mark_node)
10710 return 1;
10712 if (TEMPLATE_PARM_LEVEL (parm)
10713 != template_decl_level (tparm))
10714 /* The PARM is not one we're trying to unify. Just check
10715 to see if it matches ARG. */
10716 return !(TREE_CODE (arg) == TREE_CODE (parm)
10717 && cp_tree_equal (parm, arg));
10719 idx = TEMPLATE_PARM_IDX (parm);
10720 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10722 if (targ)
10723 return !cp_tree_equal (targ, arg);
10725 /* [temp.deduct.type] If, in the declaration of a function template
10726 with a non-type template-parameter, the non-type
10727 template-parameter is used in an expression in the function
10728 parameter-list and, if the corresponding template-argument is
10729 deduced, the template-argument type shall match the type of the
10730 template-parameter exactly, except that a template-argument
10731 deduced from an array bound may be of any integral type.
10732 The non-type parameter might use already deduced type parameters. */
10733 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10734 if (!TREE_TYPE (arg))
10735 /* Template-parameter dependent expression. Just accept it for now.
10736 It will later be processed in convert_template_argument. */
10738 else if (same_type_p (TREE_TYPE (arg), tparm))
10739 /* OK */;
10740 else if ((strict & UNIFY_ALLOW_INTEGER)
10741 && (TREE_CODE (tparm) == INTEGER_TYPE
10742 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10743 /* Convert the ARG to the type of PARM; the deduced non-type
10744 template argument must exactly match the types of the
10745 corresponding parameter. */
10746 arg = fold (build_nop (TREE_TYPE (parm), arg));
10747 else if (uses_template_parms (tparm))
10748 /* We haven't deduced the type of this parameter yet. Try again
10749 later. */
10750 return 0;
10751 else
10752 return 1;
10754 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10755 return 0;
10757 case PTRMEM_CST:
10759 /* A pointer-to-member constant can be unified only with
10760 another constant. */
10761 if (TREE_CODE (arg) != PTRMEM_CST)
10762 return 1;
10764 /* Just unify the class member. It would be useless (and possibly
10765 wrong, depending on the strict flags) to unify also
10766 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10767 arg refer to the same variable, even if through different
10768 classes. For instance:
10770 struct A { int x; };
10771 struct B : A { };
10773 Unification of &A::x and &B::x must succeed. */
10774 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10775 PTRMEM_CST_MEMBER (arg), strict);
10778 case POINTER_TYPE:
10780 if (TREE_CODE (arg) != POINTER_TYPE)
10781 return 1;
10783 /* [temp.deduct.call]
10785 A can be another pointer or pointer to member type that can
10786 be converted to the deduced A via a qualification
10787 conversion (_conv.qual_).
10789 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10790 This will allow for additional cv-qualification of the
10791 pointed-to types if appropriate. */
10793 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10794 /* The derived-to-base conversion only persists through one
10795 level of pointers. */
10796 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10798 return unify (tparms, targs, TREE_TYPE (parm),
10799 TREE_TYPE (arg), strict);
10802 case REFERENCE_TYPE:
10803 if (TREE_CODE (arg) != REFERENCE_TYPE)
10804 return 1;
10805 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10806 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10808 case ARRAY_TYPE:
10809 if (TREE_CODE (arg) != ARRAY_TYPE)
10810 return 1;
10811 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10812 != (TYPE_DOMAIN (arg) == NULL_TREE))
10813 return 1;
10814 if (TYPE_DOMAIN (parm) != NULL_TREE)
10816 tree parm_max;
10817 tree arg_max;
10818 bool parm_cst;
10819 bool arg_cst;
10821 /* Our representation of array types uses "N - 1" as the
10822 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10823 not an integer constant. We cannot unify arbitrarily
10824 complex expressions, so we eliminate the MINUS_EXPRs
10825 here. */
10826 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10827 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10828 if (!parm_cst)
10830 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
10831 parm_max = TREE_OPERAND (parm_max, 0);
10833 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10834 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10835 if (!arg_cst)
10837 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10838 trying to unify the type of a variable with the type
10839 of a template parameter. For example:
10841 template <unsigned int N>
10842 void f (char (&) [N]);
10843 int g();
10844 void h(int i) {
10845 char a[g(i)];
10846 f(a);
10849 Here, the type of the ARG will be "int [g(i)]", and
10850 may be a SAVE_EXPR, etc. */
10851 if (TREE_CODE (arg_max) != MINUS_EXPR)
10852 return 1;
10853 arg_max = TREE_OPERAND (arg_max, 0);
10856 /* If only one of the bounds used a MINUS_EXPR, compensate
10857 by adding one to the other bound. */
10858 if (parm_cst && !arg_cst)
10859 parm_max = fold_build2 (PLUS_EXPR,
10860 integer_type_node,
10861 parm_max,
10862 integer_one_node);
10863 else if (arg_cst && !parm_cst)
10864 arg_max = fold_build2 (PLUS_EXPR,
10865 integer_type_node,
10866 arg_max,
10867 integer_one_node);
10869 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10870 return 1;
10872 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10873 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10875 case REAL_TYPE:
10876 case COMPLEX_TYPE:
10877 case VECTOR_TYPE:
10878 case INTEGER_TYPE:
10879 case BOOLEAN_TYPE:
10880 case ENUMERAL_TYPE:
10881 case VOID_TYPE:
10882 if (TREE_CODE (arg) != TREE_CODE (parm))
10883 return 1;
10885 /* We have already checked cv-qualification at the top of the
10886 function. */
10887 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10888 return 1;
10890 /* As far as unification is concerned, this wins. Later checks
10891 will invalidate it if necessary. */
10892 return 0;
10894 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10895 /* Type INTEGER_CST can come from ordinary constant template args. */
10896 case INTEGER_CST:
10897 while (TREE_CODE (arg) == NOP_EXPR)
10898 arg = TREE_OPERAND (arg, 0);
10900 if (TREE_CODE (arg) != INTEGER_CST)
10901 return 1;
10902 return !tree_int_cst_equal (parm, arg);
10904 case TREE_VEC:
10906 int i;
10907 if (TREE_CODE (arg) != TREE_VEC)
10908 return 1;
10909 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10910 return 1;
10911 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10912 if (unify (tparms, targs,
10913 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10914 UNIFY_ALLOW_NONE))
10915 return 1;
10916 return 0;
10919 case RECORD_TYPE:
10920 case UNION_TYPE:
10921 if (TREE_CODE (arg) != TREE_CODE (parm))
10922 return 1;
10924 if (TYPE_PTRMEMFUNC_P (parm))
10926 if (!TYPE_PTRMEMFUNC_P (arg))
10927 return 1;
10929 return unify (tparms, targs,
10930 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10931 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10932 strict);
10935 if (CLASSTYPE_TEMPLATE_INFO (parm))
10937 tree t = NULL_TREE;
10939 if (strict_in & UNIFY_ALLOW_DERIVED)
10941 /* First, we try to unify the PARM and ARG directly. */
10942 t = try_class_unification (tparms, targs,
10943 parm, arg);
10945 if (!t)
10947 /* Fallback to the special case allowed in
10948 [temp.deduct.call]:
10950 If P is a class, and P has the form
10951 template-id, then A can be a derived class of
10952 the deduced A. Likewise, if P is a pointer to
10953 a class of the form template-id, A can be a
10954 pointer to a derived class pointed to by the
10955 deduced A. */
10956 t = get_template_base (tparms, targs, parm, arg);
10958 if (!t)
10959 return 1;
10962 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10963 && (CLASSTYPE_TI_TEMPLATE (parm)
10964 == CLASSTYPE_TI_TEMPLATE (arg)))
10965 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10966 Then, we should unify `int' and `U'. */
10967 t = arg;
10968 else
10969 /* There's no chance of unification succeeding. */
10970 return 1;
10972 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10973 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10975 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10976 return 1;
10977 return 0;
10979 case METHOD_TYPE:
10980 case FUNCTION_TYPE:
10981 if (TREE_CODE (arg) != TREE_CODE (parm))
10982 return 1;
10984 /* CV qualifications for methods can never be deduced, they must
10985 match exactly. We need to check them explicitly here,
10986 because type_unification_real treats them as any other
10987 cvqualified parameter. */
10988 if (TREE_CODE (parm) == METHOD_TYPE
10989 && (!check_cv_quals_for_unify
10990 (UNIFY_ALLOW_NONE,
10991 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10992 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10993 return 1;
10995 if (unify (tparms, targs, TREE_TYPE (parm),
10996 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10997 return 1;
10998 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10999 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
11000 LOOKUP_NORMAL);
11002 case OFFSET_TYPE:
11003 /* Unify a pointer to member with a pointer to member function, which
11004 deduces the type of the member as a function type. */
11005 if (TYPE_PTRMEMFUNC_P (arg))
11007 tree method_type;
11008 tree fntype;
11009 cp_cv_quals cv_quals;
11011 /* Check top-level cv qualifiers */
11012 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
11013 return 1;
11015 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
11016 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
11017 return 1;
11019 /* Determine the type of the function we are unifying against. */
11020 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
11021 fntype =
11022 build_function_type (TREE_TYPE (method_type),
11023 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
11025 /* Extract the cv-qualifiers of the member function from the
11026 implicit object parameter and place them on the function
11027 type to be restored later. */
11028 cv_quals =
11029 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
11030 fntype = build_qualified_type (fntype, cv_quals);
11031 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
11034 if (TREE_CODE (arg) != OFFSET_TYPE)
11035 return 1;
11036 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
11037 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
11038 return 1;
11039 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
11040 strict);
11042 case CONST_DECL:
11043 if (DECL_TEMPLATE_PARM_P (parm))
11044 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
11045 if (arg != integral_constant_value (parm))
11046 return 1;
11047 return 0;
11049 case FIELD_DECL:
11050 case TEMPLATE_DECL:
11051 /* Matched cases are handled by the ARG == PARM test above. */
11052 return 1;
11054 default:
11055 gcc_assert (EXPR_P (parm));
11057 /* We must be looking at an expression. This can happen with
11058 something like:
11060 template <int I>
11061 void foo(S<I>, S<I + 2>);
11063 This is a "nondeduced context":
11065 [deduct.type]
11067 The nondeduced contexts are:
11069 --A type that is a template-id in which one or more of
11070 the template-arguments is an expression that references
11071 a template-parameter.
11073 In these cases, we assume deduction succeeded, but don't
11074 actually infer any unifications. */
11076 if (!uses_template_parms (parm)
11077 && !template_args_equal (parm, arg))
11078 return 1;
11079 else
11080 return 0;
11084 /* Note that DECL can be defined in this translation unit, if
11085 required. */
11087 static void
11088 mark_definable (tree decl)
11090 tree clone;
11091 DECL_NOT_REALLY_EXTERN (decl) = 1;
11092 FOR_EACH_CLONE (clone, decl)
11093 DECL_NOT_REALLY_EXTERN (clone) = 1;
11096 /* Called if RESULT is explicitly instantiated, or is a member of an
11097 explicitly instantiated class. */
11099 void
11100 mark_decl_instantiated (tree result, int extern_p)
11102 SET_DECL_EXPLICIT_INSTANTIATION (result);
11104 /* If this entity has already been written out, it's too late to
11105 make any modifications. */
11106 if (TREE_ASM_WRITTEN (result))
11107 return;
11109 if (TREE_CODE (result) != FUNCTION_DECL)
11110 /* The TREE_PUBLIC flag for function declarations will have been
11111 set correctly by tsubst. */
11112 TREE_PUBLIC (result) = 1;
11114 /* This might have been set by an earlier implicit instantiation. */
11115 DECL_COMDAT (result) = 0;
11117 if (extern_p)
11118 DECL_NOT_REALLY_EXTERN (result) = 0;
11119 else
11121 mark_definable (result);
11122 /* Always make artificials weak. */
11123 if (DECL_ARTIFICIAL (result) && flag_weak)
11124 comdat_linkage (result);
11125 /* For WIN32 we also want to put explicit instantiations in
11126 linkonce sections. */
11127 else if (TREE_PUBLIC (result))
11128 maybe_make_one_only (result);
11131 /* If EXTERN_P, then this function will not be emitted -- unless
11132 followed by an explicit instantiation, at which point its linkage
11133 will be adjusted. If !EXTERN_P, then this function will be
11134 emitted here. In neither circumstance do we want
11135 import_export_decl to adjust the linkage. */
11136 DECL_INTERFACE_KNOWN (result) = 1;
11139 /* Given two function templates PAT1 and PAT2, return:
11141 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
11142 -1 if PAT2 is more specialized than PAT1.
11143 0 if neither is more specialized.
11145 LEN indicates the number of parameters we should consider
11146 (defaulted parameters should not be considered).
11148 The 1998 std underspecified function template partial ordering, and
11149 DR214 addresses the issue. We take pairs of arguments, one from
11150 each of the templates, and deduce them against each other. One of
11151 the templates will be more specialized if all the *other*
11152 template's arguments deduce against its arguments and at least one
11153 of its arguments *does* *not* deduce against the other template's
11154 corresponding argument. Deduction is done as for class templates.
11155 The arguments used in deduction have reference and top level cv
11156 qualifiers removed. Iff both arguments were originally reference
11157 types *and* deduction succeeds in both directions, the template
11158 with the more cv-qualified argument wins for that pairing (if
11159 neither is more cv-qualified, they both are equal). Unlike regular
11160 deduction, after all the arguments have been deduced in this way,
11161 we do *not* verify the deduced template argument values can be
11162 substituted into non-deduced contexts, nor do we have to verify
11163 that all template arguments have been deduced. */
11166 more_specialized_fn (tree pat1, tree pat2, int len)
11168 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
11169 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
11170 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
11171 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
11172 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
11173 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
11174 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
11175 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
11176 int better1 = 0;
11177 int better2 = 0;
11179 /* Remove the this parameter from non-static member functions. If
11180 one is a non-static member function and the other is not a static
11181 member function, remove the first parameter from that function
11182 also. This situation occurs for operator functions where we
11183 locate both a member function (with this pointer) and non-member
11184 operator (with explicit first operand). */
11185 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
11187 len--; /* LEN is the number of significant arguments for DECL1 */
11188 args1 = TREE_CHAIN (args1);
11189 if (!DECL_STATIC_FUNCTION_P (decl2))
11190 args2 = TREE_CHAIN (args2);
11192 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
11194 args2 = TREE_CHAIN (args2);
11195 if (!DECL_STATIC_FUNCTION_P (decl1))
11197 len--;
11198 args1 = TREE_CHAIN (args1);
11202 /* If only one is a conversion operator, they are unordered. */
11203 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
11204 return 0;
11206 /* Consider the return type for a conversion function */
11207 if (DECL_CONV_FN_P (decl1))
11209 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
11210 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
11211 len++;
11214 processing_template_decl++;
11216 while (len--)
11218 tree arg1 = TREE_VALUE (args1);
11219 tree arg2 = TREE_VALUE (args2);
11220 int deduce1, deduce2;
11221 int quals1 = -1;
11222 int quals2 = -1;
11224 if (TREE_CODE (arg1) == REFERENCE_TYPE)
11226 arg1 = TREE_TYPE (arg1);
11227 quals1 = cp_type_quals (arg1);
11230 if (TREE_CODE (arg2) == REFERENCE_TYPE)
11232 arg2 = TREE_TYPE (arg2);
11233 quals2 = cp_type_quals (arg2);
11236 if ((quals1 < 0) != (quals2 < 0))
11238 /* Only of the args is a reference, see if we should apply
11239 array/function pointer decay to it. This is not part of
11240 DR214, but is, IMHO, consistent with the deduction rules
11241 for the function call itself, and with our earlier
11242 implementation of the underspecified partial ordering
11243 rules. (nathan). */
11244 if (quals1 >= 0)
11246 switch (TREE_CODE (arg1))
11248 case ARRAY_TYPE:
11249 arg1 = TREE_TYPE (arg1);
11250 /* FALLTHROUGH. */
11251 case FUNCTION_TYPE:
11252 arg1 = build_pointer_type (arg1);
11253 break;
11255 default:
11256 break;
11259 else
11261 switch (TREE_CODE (arg2))
11263 case ARRAY_TYPE:
11264 arg2 = TREE_TYPE (arg2);
11265 /* FALLTHROUGH. */
11266 case FUNCTION_TYPE:
11267 arg2 = build_pointer_type (arg2);
11268 break;
11270 default:
11271 break;
11276 arg1 = TYPE_MAIN_VARIANT (arg1);
11277 arg2 = TYPE_MAIN_VARIANT (arg2);
11279 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11280 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11282 if (!deduce1)
11283 better2 = -1;
11284 if (!deduce2)
11285 better1 = -1;
11286 if (better1 < 0 && better2 < 0)
11287 /* We've failed to deduce something in either direction.
11288 These must be unordered. */
11289 break;
11291 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11293 /* Deduces in both directions, see if quals can
11294 disambiguate. Pretend the worse one failed to deduce. */
11295 if ((quals1 & quals2) == quals2)
11296 deduce1 = 0;
11297 if ((quals1 & quals2) == quals1)
11298 deduce2 = 0;
11300 if (deduce1 && !deduce2 && !better2)
11301 better2 = 1;
11302 if (deduce2 && !deduce1 && !better1)
11303 better1 = 1;
11305 args1 = TREE_CHAIN (args1);
11306 args2 = TREE_CHAIN (args2);
11309 processing_template_decl--;
11311 return (better1 > 0) - (better2 > 0);
11314 /* Determine which of two partial specializations is more specialized.
11316 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11317 to the first partial specialization. The TREE_VALUE is the
11318 innermost set of template parameters for the partial
11319 specialization. PAT2 is similar, but for the second template.
11321 Return 1 if the first partial specialization is more specialized;
11322 -1 if the second is more specialized; 0 if neither is more
11323 specialized.
11325 See [temp.class.order] for information about determining which of
11326 two templates is more specialized. */
11328 static int
11329 more_specialized_class (tree pat1, tree pat2)
11331 tree targs;
11332 tree tmpl1, tmpl2;
11333 int winner = 0;
11335 tmpl1 = TREE_TYPE (pat1);
11336 tmpl2 = TREE_TYPE (pat2);
11338 /* Just like what happens for functions, if we are ordering between
11339 different class template specializations, we may encounter dependent
11340 types in the arguments, and we need our dependency check functions
11341 to behave correctly. */
11342 ++processing_template_decl;
11343 targs = get_class_bindings (TREE_VALUE (pat1),
11344 CLASSTYPE_TI_ARGS (tmpl1),
11345 CLASSTYPE_TI_ARGS (tmpl2));
11346 if (targs)
11347 --winner;
11349 targs = get_class_bindings (TREE_VALUE (pat2),
11350 CLASSTYPE_TI_ARGS (tmpl2),
11351 CLASSTYPE_TI_ARGS (tmpl1));
11352 if (targs)
11353 ++winner;
11354 --processing_template_decl;
11356 return winner;
11359 /* Return the template arguments that will produce the function signature
11360 DECL from the function template FN, with the explicit template
11361 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
11362 also match. Return NULL_TREE if no satisfactory arguments could be
11363 found. */
11365 static tree
11366 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
11368 int ntparms = DECL_NTPARMS (fn);
11369 tree targs = make_tree_vec (ntparms);
11370 tree decl_type;
11371 tree decl_arg_types;
11373 /* Substitute the explicit template arguments into the type of DECL.
11374 The call to fn_type_unification will handle substitution into the
11375 FN. */
11376 decl_type = TREE_TYPE (decl);
11377 if (explicit_args && uses_template_parms (decl_type))
11379 tree tmpl;
11380 tree converted_args;
11382 if (DECL_TEMPLATE_INFO (decl))
11383 tmpl = DECL_TI_TEMPLATE (decl);
11384 else
11385 /* We can get here for some invalid specializations. */
11386 return NULL_TREE;
11388 converted_args
11389 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11390 explicit_args, NULL_TREE,
11391 tf_none,
11392 /*require_all_args=*/false,
11393 /*use_default_args=*/false);
11394 if (converted_args == error_mark_node)
11395 return NULL_TREE;
11397 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
11398 if (decl_type == error_mark_node)
11399 return NULL_TREE;
11402 /* Never do unification on the 'this' parameter. */
11403 decl_arg_types = skip_artificial_parms_for (decl,
11404 TYPE_ARG_TYPES (decl_type));
11406 if (fn_type_unification (fn, explicit_args, targs,
11407 decl_arg_types,
11408 (check_rettype || DECL_CONV_FN_P (fn)
11409 ? TREE_TYPE (decl_type) : NULL_TREE),
11410 DEDUCE_EXACT, LOOKUP_NORMAL))
11411 return NULL_TREE;
11413 return targs;
11416 /* Return the innermost template arguments that, when applied to a
11417 template specialization whose innermost template parameters are
11418 TPARMS, and whose specialization arguments are PARMS, yield the
11419 ARGS.
11421 For example, suppose we have:
11423 template <class T, class U> struct S {};
11424 template <class T> struct S<T*, int> {};
11426 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
11427 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
11428 int}. The resulting vector will be {double}, indicating that `T'
11429 is bound to `double'. */
11431 static tree
11432 get_class_bindings (tree tparms, tree spec_args, tree args)
11434 int i, ntparms = TREE_VEC_LENGTH (tparms);
11435 tree deduced_args;
11436 tree innermost_deduced_args;
11438 innermost_deduced_args = make_tree_vec (ntparms);
11439 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11441 deduced_args = copy_node (args);
11442 SET_TMPL_ARGS_LEVEL (deduced_args,
11443 TMPL_ARGS_DEPTH (deduced_args),
11444 innermost_deduced_args);
11446 else
11447 deduced_args = innermost_deduced_args;
11449 if (unify (tparms, deduced_args,
11450 INNERMOST_TEMPLATE_ARGS (spec_args),
11451 INNERMOST_TEMPLATE_ARGS (args),
11452 UNIFY_ALLOW_NONE))
11453 return NULL_TREE;
11455 for (i = 0; i < ntparms; ++i)
11456 if (! TREE_VEC_ELT (innermost_deduced_args, i))
11457 return NULL_TREE;
11459 /* Verify that nondeduced template arguments agree with the type
11460 obtained from argument deduction.
11462 For example:
11464 struct A { typedef int X; };
11465 template <class T, class U> struct C {};
11466 template <class T> struct C<T, typename T::X> {};
11468 Then with the instantiation `C<A, int>', we can deduce that
11469 `T' is `A' but unify () does not check whether `typename T::X'
11470 is `int'. */
11471 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11472 if (spec_args == error_mark_node
11473 /* We only need to check the innermost arguments; the other
11474 arguments will always agree. */
11475 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11476 INNERMOST_TEMPLATE_ARGS (args)))
11477 return NULL_TREE;
11479 return deduced_args;
11482 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11483 Return the TREE_LIST node with the most specialized template, if
11484 any. If there is no most specialized template, the error_mark_node
11485 is returned.
11487 Note that this function does not look at, or modify, the
11488 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11489 returned is one of the elements of INSTANTIATIONS, callers may
11490 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11491 and retrieve it from the value returned. */
11493 tree
11494 most_specialized_instantiation (tree templates)
11496 tree fn, champ;
11498 ++processing_template_decl;
11500 champ = templates;
11501 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
11503 int fate = 0;
11505 if (get_bindings (TREE_VALUE (champ),
11506 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11507 NULL_TREE, /*check_ret=*/false))
11508 fate--;
11510 if (get_bindings (TREE_VALUE (fn),
11511 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11512 NULL_TREE, /*check_ret=*/false))
11513 fate++;
11515 if (fate == -1)
11516 champ = fn;
11517 else if (!fate)
11519 /* Equally specialized, move to next function. If there
11520 is no next function, nothing's most specialized. */
11521 fn = TREE_CHAIN (fn);
11522 champ = fn;
11523 if (!fn)
11524 break;
11528 if (champ)
11529 /* Now verify that champ is better than everything earlier in the
11530 instantiation list. */
11531 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11532 if (get_bindings (TREE_VALUE (champ),
11533 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11534 NULL_TREE, /*check_ret=*/false)
11535 || !get_bindings (TREE_VALUE (fn),
11536 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11537 NULL_TREE, /*check_ret=*/false))
11539 champ = NULL_TREE;
11540 break;
11543 processing_template_decl--;
11545 if (!champ)
11546 return error_mark_node;
11548 return champ;
11551 /* If DECL is a specialization of some template, return the most
11552 general such template. Otherwise, returns NULL_TREE.
11554 For example, given:
11556 template <class T> struct S { template <class U> void f(U); };
11558 if TMPL is `template <class U> void S<int>::f(U)' this will return
11559 the full template. This function will not trace past partial
11560 specializations, however. For example, given in addition:
11562 template <class T> struct S<T*> { template <class U> void f(U); };
11564 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11565 `template <class T> template <class U> S<T*>::f(U)'. */
11567 tree
11568 most_general_template (tree decl)
11570 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11571 an immediate specialization. */
11572 if (TREE_CODE (decl) == FUNCTION_DECL)
11574 if (DECL_TEMPLATE_INFO (decl)) {
11575 decl = DECL_TI_TEMPLATE (decl);
11577 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11578 template friend. */
11579 if (TREE_CODE (decl) != TEMPLATE_DECL)
11580 return NULL_TREE;
11581 } else
11582 return NULL_TREE;
11585 /* Look for more and more general templates. */
11586 while (DECL_TEMPLATE_INFO (decl))
11588 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11589 (See cp-tree.h for details.) */
11590 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11591 break;
11593 if (CLASS_TYPE_P (TREE_TYPE (decl))
11594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11595 break;
11597 /* Stop if we run into an explicitly specialized class template. */
11598 if (!DECL_NAMESPACE_SCOPE_P (decl)
11599 && DECL_CONTEXT (decl)
11600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11601 break;
11603 decl = DECL_TI_TEMPLATE (decl);
11606 return decl;
11609 /* Return the most specialized of the class template partial
11610 specializations of TMPL which can produce TYPE, a specialization of
11611 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11612 a _TYPE node corresponding to the partial specialization, while the
11613 TREE_PURPOSE is the set of template arguments that must be
11614 substituted into the TREE_TYPE in order to generate TYPE.
11616 If the choice of partial specialization is ambiguous, a diagnostic
11617 is issued, and the error_mark_node is returned. If there are no
11618 partial specializations of TMPL matching TYPE, then NULL_TREE is
11619 returned. */
11621 static tree
11622 most_specialized_class (tree type, tree tmpl)
11624 tree list = NULL_TREE;
11625 tree t;
11626 tree champ;
11627 int fate;
11628 bool ambiguous_p;
11629 tree args;
11631 tmpl = most_general_template (tmpl);
11632 args = CLASSTYPE_TI_ARGS (type);
11633 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11635 tree partial_spec_args;
11636 tree spec_args;
11638 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11639 spec_args = get_class_bindings (TREE_VALUE (t),
11640 partial_spec_args,
11641 args);
11642 if (spec_args)
11644 list = tree_cons (spec_args, TREE_VALUE (t), list);
11645 TREE_TYPE (list) = TREE_TYPE (t);
11649 if (! list)
11650 return NULL_TREE;
11652 ambiguous_p = false;
11653 t = list;
11654 champ = t;
11655 t = TREE_CHAIN (t);
11656 for (; t; t = TREE_CHAIN (t))
11658 fate = more_specialized_class (champ, t);
11659 if (fate == 1)
11661 else
11663 if (fate == 0)
11665 t = TREE_CHAIN (t);
11666 if (! t)
11668 ambiguous_p = true;
11669 break;
11672 champ = t;
11676 if (!ambiguous_p)
11677 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11679 fate = more_specialized_class (champ, t);
11680 if (fate != 1)
11682 ambiguous_p = true;
11683 break;
11687 if (ambiguous_p)
11689 const char *str = "candidates are:";
11690 error ("ambiguous class template instantiation for %q#T", type);
11691 for (t = list; t; t = TREE_CHAIN (t))
11693 error ("%s %+#T", str, TREE_TYPE (t));
11694 str = " ";
11696 return error_mark_node;
11699 return champ;
11702 /* Explicitly instantiate DECL. */
11704 void
11705 do_decl_instantiation (tree decl, tree storage)
11707 tree result = NULL_TREE;
11708 int extern_p = 0;
11710 if (!decl || decl == error_mark_node)
11711 /* An error occurred, for which grokdeclarator has already issued
11712 an appropriate message. */
11713 return;
11714 else if (! DECL_LANG_SPECIFIC (decl))
11716 error ("explicit instantiation of non-template %q#D", decl);
11717 return;
11719 else if (TREE_CODE (decl) == VAR_DECL)
11721 /* There is an asymmetry here in the way VAR_DECLs and
11722 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11723 the latter, the DECL we get back will be marked as a
11724 template instantiation, and the appropriate
11725 DECL_TEMPLATE_INFO will be set up. This does not happen for
11726 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11727 should handle VAR_DECLs as it currently handles
11728 FUNCTION_DECLs. */
11729 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11730 if (!result || TREE_CODE (result) != VAR_DECL)
11732 error ("no matching template for %qD found", decl);
11733 return;
11735 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
11737 error ("type %qT for explicit instantiation %qD does not match "
11738 "declared type %qT", TREE_TYPE (result), decl,
11739 TREE_TYPE (decl));
11740 return;
11743 else if (TREE_CODE (decl) != FUNCTION_DECL)
11745 error ("explicit instantiation of %q#D", decl);
11746 return;
11748 else
11749 result = decl;
11751 /* Check for various error cases. Note that if the explicit
11752 instantiation is valid the RESULT will currently be marked as an
11753 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11754 until we get here. */
11756 if (DECL_TEMPLATE_SPECIALIZATION (result))
11758 /* DR 259 [temp.spec].
11760 Both an explicit instantiation and a declaration of an explicit
11761 specialization shall not appear in a program unless the explicit
11762 instantiation follows a declaration of the explicit specialization.
11764 For a given set of template parameters, if an explicit
11765 instantiation of a template appears after a declaration of an
11766 explicit specialization for that template, the explicit
11767 instantiation has no effect. */
11768 return;
11770 else if (DECL_EXPLICIT_INSTANTIATION (result))
11772 /* [temp.spec]
11774 No program shall explicitly instantiate any template more
11775 than once.
11777 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11778 the first instantiation was `extern' and the second is not,
11779 and EXTERN_P for the opposite case. */
11780 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11781 pedwarn ("duplicate explicit instantiation of %q#D", result);
11782 /* If an "extern" explicit instantiation follows an ordinary
11783 explicit instantiation, the template is instantiated. */
11784 if (extern_p)
11785 return;
11787 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11789 error ("no matching template for %qD found", result);
11790 return;
11792 else if (!DECL_TEMPLATE_INFO (result))
11794 pedwarn ("explicit instantiation of non-template %q#D", result);
11795 return;
11798 if (storage == NULL_TREE)
11800 else if (storage == ridpointers[(int) RID_EXTERN])
11802 if (pedantic && !in_system_header)
11803 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11804 "instantiations");
11805 extern_p = 1;
11807 else
11808 error ("storage class %qD applied to template instantiation", storage);
11810 check_explicit_instantiation_namespace (result);
11811 mark_decl_instantiated (result, extern_p);
11812 if (! extern_p)
11813 instantiate_decl (result, /*defer_ok=*/1,
11814 /*expl_inst_class_mem_p=*/false);
11817 static void
11818 mark_class_instantiated (tree t, int extern_p)
11820 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11821 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11822 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11823 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11824 if (! extern_p)
11826 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11827 rest_of_type_compilation (t, 1);
11831 /* Called from do_type_instantiation through binding_table_foreach to
11832 do recursive instantiation for the type bound in ENTRY. */
11833 static void
11834 bt_instantiate_type_proc (binding_entry entry, void *data)
11836 tree storage = *(tree *) data;
11838 if (IS_AGGR_TYPE (entry->type)
11839 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11840 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11843 /* Called from do_type_instantiation to instantiate a member
11844 (a member function or a static member variable) of an
11845 explicitly instantiated class template. */
11846 static void
11847 instantiate_class_member (tree decl, int extern_p)
11849 mark_decl_instantiated (decl, extern_p);
11850 if (! extern_p)
11851 instantiate_decl (decl, /*defer_ok=*/1,
11852 /*expl_inst_class_mem_p=*/true);
11855 /* Perform an explicit instantiation of template class T. STORAGE, if
11856 non-null, is the RID for extern, inline or static. COMPLAIN is
11857 nonzero if this is called from the parser, zero if called recursively,
11858 since the standard is unclear (as detailed below). */
11860 void
11861 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11863 int extern_p = 0;
11864 int nomem_p = 0;
11865 int static_p = 0;
11866 int previous_instantiation_extern_p = 0;
11868 if (TREE_CODE (t) == TYPE_DECL)
11869 t = TREE_TYPE (t);
11871 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11873 error ("explicit instantiation of non-template type %qT", t);
11874 return;
11877 complete_type (t);
11879 if (!COMPLETE_TYPE_P (t))
11881 if (complain & tf_error)
11882 error ("explicit instantiation of %q#T before definition of template",
11884 return;
11887 if (storage != NULL_TREE)
11889 if (pedantic && !in_system_header)
11890 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11891 storage);
11893 if (storage == ridpointers[(int) RID_INLINE])
11894 nomem_p = 1;
11895 else if (storage == ridpointers[(int) RID_EXTERN])
11896 extern_p = 1;
11897 else if (storage == ridpointers[(int) RID_STATIC])
11898 static_p = 1;
11899 else
11901 error ("storage class %qD applied to template instantiation",
11902 storage);
11903 extern_p = 0;
11907 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11909 /* DR 259 [temp.spec].
11911 Both an explicit instantiation and a declaration of an explicit
11912 specialization shall not appear in a program unless the explicit
11913 instantiation follows a declaration of the explicit specialization.
11915 For a given set of template parameters, if an explicit
11916 instantiation of a template appears after a declaration of an
11917 explicit specialization for that template, the explicit
11918 instantiation has no effect. */
11919 return;
11921 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11923 /* [temp.spec]
11925 No program shall explicitly instantiate any template more
11926 than once.
11928 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11929 instantiation was `extern'. If EXTERN_P then the second is.
11930 These cases are OK. */
11931 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11933 if (!previous_instantiation_extern_p && !extern_p
11934 && (complain & tf_error))
11935 pedwarn ("duplicate explicit instantiation of %q#T", t);
11937 /* If we've already instantiated the template, just return now. */
11938 if (!CLASSTYPE_INTERFACE_ONLY (t))
11939 return;
11942 check_explicit_instantiation_namespace (TYPE_NAME (t));
11943 mark_class_instantiated (t, extern_p);
11945 if (nomem_p)
11946 return;
11949 tree tmp;
11951 /* In contrast to implicit instantiation, where only the
11952 declarations, and not the definitions, of members are
11953 instantiated, we have here:
11955 [temp.explicit]
11957 The explicit instantiation of a class template specialization
11958 implies the instantiation of all of its members not
11959 previously explicitly specialized in the translation unit
11960 containing the explicit instantiation.
11962 Of course, we can't instantiate member template classes, since
11963 we don't have any arguments for them. Note that the standard
11964 is unclear on whether the instantiation of the members are
11965 *explicit* instantiations or not. However, the most natural
11966 interpretation is that it should be an explicit instantiation. */
11968 if (! static_p)
11969 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11970 if (TREE_CODE (tmp) == FUNCTION_DECL
11971 && DECL_TEMPLATE_INSTANTIATION (tmp))
11972 instantiate_class_member (tmp, extern_p);
11974 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11975 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11976 instantiate_class_member (tmp, extern_p);
11978 if (CLASSTYPE_NESTED_UTDS (t))
11979 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11980 bt_instantiate_type_proc, &storage);
11984 /* Given a function DECL, which is a specialization of TMPL, modify
11985 DECL to be a re-instantiation of TMPL with the same template
11986 arguments. TMPL should be the template into which tsubst'ing
11987 should occur for DECL, not the most general template.
11989 One reason for doing this is a scenario like this:
11991 template <class T>
11992 void f(const T&, int i);
11994 void g() { f(3, 7); }
11996 template <class T>
11997 void f(const T& t, const int i) { }
11999 Note that when the template is first instantiated, with
12000 instantiate_template, the resulting DECL will have no name for the
12001 first parameter, and the wrong type for the second. So, when we go
12002 to instantiate the DECL, we regenerate it. */
12004 static void
12005 regenerate_decl_from_template (tree decl, tree tmpl)
12007 /* The arguments used to instantiate DECL, from the most general
12008 template. */
12009 tree args;
12010 tree code_pattern;
12012 args = DECL_TI_ARGS (decl);
12013 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
12015 /* Make sure that we can see identifiers, and compute access
12016 correctly. */
12017 push_access_scope (decl);
12019 if (TREE_CODE (decl) == FUNCTION_DECL)
12021 tree decl_parm;
12022 tree pattern_parm;
12023 tree specs;
12024 int args_depth;
12025 int parms_depth;
12027 args_depth = TMPL_ARGS_DEPTH (args);
12028 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
12029 if (args_depth > parms_depth)
12030 args = get_innermost_template_args (args, parms_depth);
12032 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
12033 args, tf_error, NULL_TREE);
12034 if (specs)
12035 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
12036 specs);
12038 /* Merge parameter declarations. */
12039 decl_parm = skip_artificial_parms_for (decl,
12040 DECL_ARGUMENTS (decl));
12041 pattern_parm
12042 = skip_artificial_parms_for (code_pattern,
12043 DECL_ARGUMENTS (code_pattern));
12044 while (decl_parm)
12046 tree parm_type;
12047 tree attributes;
12049 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
12050 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
12051 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
12052 NULL_TREE);
12053 parm_type = type_decays_to (parm_type);
12054 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
12055 TREE_TYPE (decl_parm) = parm_type;
12056 attributes = DECL_ATTRIBUTES (pattern_parm);
12057 if (DECL_ATTRIBUTES (decl_parm) != attributes)
12059 DECL_ATTRIBUTES (decl_parm) = attributes;
12060 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
12062 decl_parm = TREE_CHAIN (decl_parm);
12063 pattern_parm = TREE_CHAIN (pattern_parm);
12066 /* Merge additional specifiers from the CODE_PATTERN. */
12067 if (DECL_DECLARED_INLINE_P (code_pattern)
12068 && !DECL_DECLARED_INLINE_P (decl))
12069 DECL_DECLARED_INLINE_P (decl) = 1;
12070 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
12071 DECL_INLINE (decl) = 1;
12073 else if (TREE_CODE (decl) == VAR_DECL)
12074 DECL_INITIAL (decl) =
12075 tsubst_expr (DECL_INITIAL (code_pattern), args,
12076 tf_error, DECL_TI_TEMPLATE (decl),
12077 /*integral_constant_expression_p=*/false);
12078 else
12079 gcc_unreachable ();
12081 pop_access_scope (decl);
12084 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
12085 substituted to get DECL. */
12087 tree
12088 template_for_substitution (tree decl)
12090 tree tmpl = DECL_TI_TEMPLATE (decl);
12092 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
12093 for the instantiation. This is not always the most general
12094 template. Consider, for example:
12096 template <class T>
12097 struct S { template <class U> void f();
12098 template <> void f<int>(); };
12100 and an instantiation of S<double>::f<int>. We want TD to be the
12101 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
12102 while (/* An instantiation cannot have a definition, so we need a
12103 more general template. */
12104 DECL_TEMPLATE_INSTANTIATION (tmpl)
12105 /* We must also deal with friend templates. Given:
12107 template <class T> struct S {
12108 template <class U> friend void f() {};
12111 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
12112 so far as the language is concerned, but that's still
12113 where we get the pattern for the instantiation from. On
12114 other hand, if the definition comes outside the class, say:
12116 template <class T> struct S {
12117 template <class U> friend void f();
12119 template <class U> friend void f() {}
12121 we don't need to look any further. That's what the check for
12122 DECL_INITIAL is for. */
12123 || (TREE_CODE (decl) == FUNCTION_DECL
12124 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
12125 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
12127 /* The present template, TD, should not be a definition. If it
12128 were a definition, we should be using it! Note that we
12129 cannot restructure the loop to just keep going until we find
12130 a template with a definition, since that might go too far if
12131 a specialization was declared, but not defined. */
12132 gcc_assert (TREE_CODE (decl) != VAR_DECL
12133 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
12135 /* Fetch the more general template. */
12136 tmpl = DECL_TI_TEMPLATE (tmpl);
12139 return tmpl;
12142 /* Produce the definition of D, a _DECL generated from a template. If
12143 DEFER_OK is nonzero, then we don't have to actually do the
12144 instantiation now; we just have to do it sometime. Normally it is
12145 an error if this is an explicit instantiation but D is undefined.
12146 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
12147 explicitly instantiated class template. */
12149 tree
12150 instantiate_decl (tree d, int defer_ok,
12151 bool expl_inst_class_mem_p)
12153 tree tmpl = DECL_TI_TEMPLATE (d);
12154 tree gen_args;
12155 tree args;
12156 tree td;
12157 tree code_pattern;
12158 tree spec;
12159 tree gen_tmpl;
12160 bool pattern_defined;
12161 int need_push;
12162 location_t saved_loc = input_location;
12163 bool external_p;
12165 /* This function should only be used to instantiate templates for
12166 functions and static member variables. */
12167 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
12168 || TREE_CODE (d) == VAR_DECL);
12170 /* Variables are never deferred; if instantiation is required, they
12171 are instantiated right away. That allows for better code in the
12172 case that an expression refers to the value of the variable --
12173 if the variable has a constant value the referring expression can
12174 take advantage of that fact. */
12175 if (TREE_CODE (d) == VAR_DECL)
12176 defer_ok = 0;
12178 /* Don't instantiate cloned functions. Instead, instantiate the
12179 functions they cloned. */
12180 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
12181 d = DECL_CLONED_FUNCTION (d);
12183 if (DECL_TEMPLATE_INSTANTIATED (d))
12184 /* D has already been instantiated. It might seem reasonable to
12185 check whether or not D is an explicit instantiation, and, if so,
12186 stop here. But when an explicit instantiation is deferred
12187 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
12188 is set, even though we still need to do the instantiation. */
12189 return d;
12191 /* If we already have a specialization of this declaration, then
12192 there's no reason to instantiate it. Note that
12193 retrieve_specialization gives us both instantiations and
12194 specializations, so we must explicitly check
12195 DECL_TEMPLATE_SPECIALIZATION. */
12196 gen_tmpl = most_general_template (tmpl);
12197 gen_args = DECL_TI_ARGS (d);
12198 spec = retrieve_specialization (gen_tmpl, gen_args,
12199 /*class_specializations_p=*/false);
12200 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
12201 return spec;
12203 /* This needs to happen before any tsubsting. */
12204 if (! push_tinst_level (d))
12205 return d;
12207 timevar_push (TV_PARSE);
12209 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
12210 for the instantiation. */
12211 td = template_for_substitution (d);
12212 code_pattern = DECL_TEMPLATE_RESULT (td);
12214 /* We should never be trying to instantiate a member of a class
12215 template or partial specialization. */
12216 gcc_assert (d != code_pattern);
12218 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
12219 || DECL_TEMPLATE_SPECIALIZATION (td))
12220 /* In the case of a friend template whose definition is provided
12221 outside the class, we may have too many arguments. Drop the
12222 ones we don't need. The same is true for specializations. */
12223 args = get_innermost_template_args
12224 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
12225 else
12226 args = gen_args;
12228 if (TREE_CODE (d) == FUNCTION_DECL)
12229 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
12230 else
12231 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
12233 /* We may be in the middle of deferred access check. Disable it now. */
12234 push_deferring_access_checks (dk_no_deferred);
12236 /* Unless an explicit instantiation directive has already determined
12237 the linkage of D, remember that a definition is available for
12238 this entity. */
12239 if (pattern_defined
12240 && !DECL_INTERFACE_KNOWN (d)
12241 && !DECL_NOT_REALLY_EXTERN (d))
12242 mark_definable (d);
12244 input_location = DECL_SOURCE_LOCATION (d);
12246 /* If D is a member of an explicitly instantiated class template,
12247 and no definition is available, treat it like an implicit
12248 instantiation. */
12249 if (!pattern_defined && expl_inst_class_mem_p
12250 && DECL_EXPLICIT_INSTANTIATION (d))
12252 DECL_NOT_REALLY_EXTERN (d) = 0;
12253 DECL_INTERFACE_KNOWN (d) = 0;
12254 SET_DECL_IMPLICIT_INSTANTIATION (d);
12257 if (!defer_ok)
12259 /* Recheck the substitutions to obtain any warning messages
12260 about ignoring cv qualifiers. */
12261 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
12262 tree type = TREE_TYPE (gen);
12264 /* Make sure that we can see identifiers, and compute access
12265 correctly. D is already the target FUNCTION_DECL with the
12266 right context. */
12267 push_access_scope (d);
12269 if (TREE_CODE (gen) == FUNCTION_DECL)
12271 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
12272 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
12273 tf_warning_or_error, d);
12274 /* Don't simply tsubst the function type, as that will give
12275 duplicate warnings about poor parameter qualifications.
12276 The function arguments are the same as the decl_arguments
12277 without the top level cv qualifiers. */
12278 type = TREE_TYPE (type);
12280 tsubst (type, gen_args, tf_warning_or_error, d);
12282 pop_access_scope (d);
12285 /* Check to see whether we know that this template will be
12286 instantiated in some other file, as with "extern template"
12287 extension. */
12288 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12289 /* In general, we do not instantiate such templates... */
12290 if (external_p
12291 /* ... but we instantiate inline functions so that we can inline
12292 them and ... */
12293 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12294 /* ... we instantiate static data members whose values are
12295 needed in integral constant expressions. */
12296 && ! (TREE_CODE (d) == VAR_DECL
12297 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
12298 goto out;
12299 /* Defer all other templates, unless we have been explicitly
12300 forbidden from doing so. */
12301 if (/* If there is no definition, we cannot instantiate the
12302 template. */
12303 ! pattern_defined
12304 /* If it's OK to postpone instantiation, do so. */
12305 || defer_ok
12306 /* If this is a static data member that will be defined
12307 elsewhere, we don't want to instantiate the entire data
12308 member, but we do want to instantiate the initializer so that
12309 we can substitute that elsewhere. */
12310 || (external_p && TREE_CODE (d) == VAR_DECL))
12312 /* The definition of the static data member is now required so
12313 we must substitute the initializer. */
12314 if (TREE_CODE (d) == VAR_DECL
12315 && !DECL_INITIAL (d)
12316 && DECL_INITIAL (code_pattern))
12318 tree ns;
12319 tree init;
12321 ns = decl_namespace_context (d);
12322 push_nested_namespace (ns);
12323 push_nested_class (DECL_CONTEXT (d));
12324 init = tsubst_expr (DECL_INITIAL (code_pattern),
12325 args,
12326 tf_warning_or_error, NULL_TREE,
12327 /*integral_constant_expression_p=*/false);
12328 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12329 /*asmspec_tree=*/NULL_TREE,
12330 LOOKUP_ONLYCONVERTING);
12331 pop_nested_class ();
12332 pop_nested_namespace (ns);
12335 /* We restore the source position here because it's used by
12336 add_pending_template. */
12337 input_location = saved_loc;
12339 if (at_eof && !pattern_defined
12340 && DECL_EXPLICIT_INSTANTIATION (d))
12341 /* [temp.explicit]
12343 The definition of a non-exported function template, a
12344 non-exported member function template, or a non-exported
12345 member function or static data member of a class template
12346 shall be present in every translation unit in which it is
12347 explicitly instantiated. */
12348 pedwarn
12349 ("explicit instantiation of %qD but no definition available", d);
12351 /* ??? Historically, we have instantiated inline functions, even
12352 when marked as "extern template". */
12353 if (!(external_p && TREE_CODE (d) == VAR_DECL))
12354 add_pending_template (d);
12355 goto out;
12357 /* Tell the repository that D is available in this translation unit
12358 -- and see if it is supposed to be instantiated here. */
12359 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12361 /* In a PCH file, despite the fact that the repository hasn't
12362 requested instantiation in the PCH it is still possible that
12363 an instantiation will be required in a file that includes the
12364 PCH. */
12365 if (pch_file)
12366 add_pending_template (d);
12367 /* Instantiate inline functions so that the inliner can do its
12368 job, even though we'll not be emitting a copy of this
12369 function. */
12370 if (!(TREE_CODE (d) == FUNCTION_DECL
12371 && flag_inline_trees
12372 && DECL_DECLARED_INLINE_P (d)))
12373 goto out;
12376 need_push = !cfun || !global_bindings_p ();
12377 if (need_push)
12378 push_to_top_level ();
12380 /* Mark D as instantiated so that recursive calls to
12381 instantiate_decl do not try to instantiate it again. */
12382 DECL_TEMPLATE_INSTANTIATED (d) = 1;
12384 /* Regenerate the declaration in case the template has been modified
12385 by a subsequent redeclaration. */
12386 regenerate_decl_from_template (d, td);
12388 /* We already set the file and line above. Reset them now in case
12389 they changed as a result of calling regenerate_decl_from_template. */
12390 input_location = DECL_SOURCE_LOCATION (d);
12392 if (TREE_CODE (d) == VAR_DECL)
12394 tree init;
12396 /* Clear out DECL_RTL; whatever was there before may not be right
12397 since we've reset the type of the declaration. */
12398 SET_DECL_RTL (d, NULL_RTX);
12399 DECL_IN_AGGR_P (d) = 0;
12401 /* The initializer is placed in DECL_INITIAL by
12402 regenerate_decl_from_template. Pull it out so that
12403 finish_decl can process it. */
12404 init = DECL_INITIAL (d);
12405 DECL_INITIAL (d) = NULL_TREE;
12406 DECL_INITIALIZED_P (d) = 0;
12408 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12409 initializer. That function will defer actual emission until
12410 we have a chance to determine linkage. */
12411 DECL_EXTERNAL (d) = 0;
12413 /* Enter the scope of D so that access-checking works correctly. */
12414 push_nested_class (DECL_CONTEXT (d));
12415 finish_decl (d, init, NULL_TREE);
12416 pop_nested_class ();
12418 else if (TREE_CODE (d) == FUNCTION_DECL)
12420 htab_t saved_local_specializations;
12421 tree subst_decl;
12422 tree tmpl_parm;
12423 tree spec_parm;
12425 /* Save away the current list, in case we are instantiating one
12426 template from within the body of another. */
12427 saved_local_specializations = local_specializations;
12429 /* Set up the list of local specializations. */
12430 local_specializations = htab_create (37,
12431 hash_local_specialization,
12432 eq_local_specializations,
12433 NULL);
12435 /* Set up context. */
12436 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
12438 /* Create substitution entries for the parameters. */
12439 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12440 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12441 spec_parm = DECL_ARGUMENTS (d);
12442 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12444 register_local_specialization (spec_parm, tmpl_parm);
12445 spec_parm = skip_artificial_parms_for (d, spec_parm);
12446 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12448 while (tmpl_parm)
12450 register_local_specialization (spec_parm, tmpl_parm);
12451 tmpl_parm = TREE_CHAIN (tmpl_parm);
12452 spec_parm = TREE_CHAIN (spec_parm);
12454 gcc_assert (!spec_parm);
12456 /* Substitute into the body of the function. */
12457 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
12458 tf_warning_or_error, tmpl,
12459 /*integral_constant_expression_p=*/false);
12461 /* We don't need the local specializations any more. */
12462 htab_delete (local_specializations);
12463 local_specializations = saved_local_specializations;
12465 /* Finish the function. */
12466 d = finish_function (0);
12467 expand_or_defer_fn (d);
12470 /* We're not deferring instantiation any more. */
12471 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12473 if (need_push)
12474 pop_from_top_level ();
12476 out:
12477 input_location = saved_loc;
12478 pop_deferring_access_checks ();
12479 pop_tinst_level ();
12481 timevar_pop (TV_PARSE);
12483 return d;
12486 /* Run through the list of templates that we wish we could
12487 instantiate, and instantiate any we can. RETRIES is the
12488 number of times we retry pending template instantiation. */
12490 void
12491 instantiate_pending_templates (int retries)
12493 tree *t;
12494 tree last = NULL_TREE;
12495 int reconsider;
12496 location_t saved_loc = input_location;
12497 int saved_in_system_header = in_system_header;
12499 /* Instantiating templates may trigger vtable generation. This in turn
12500 may require further template instantiations. We place a limit here
12501 to avoid infinite loop. */
12502 if (pending_templates && retries >= max_tinst_depth)
12504 tree decl = TREE_VALUE (pending_templates);
12506 error ("template instantiation depth exceeds maximum of %d"
12507 " instantiating %q+D, possibly from virtual table generation"
12508 " (use -ftemplate-depth-NN to increase the maximum)",
12509 max_tinst_depth, decl);
12510 if (TREE_CODE (decl) == FUNCTION_DECL)
12511 /* Pretend that we defined it. */
12512 DECL_INITIAL (decl) = error_mark_node;
12513 return;
12518 reconsider = 0;
12520 t = &pending_templates;
12521 while (*t)
12523 tree instantiation = TREE_VALUE (*t);
12525 reopen_tinst_level (TREE_PURPOSE (*t));
12527 if (TYPE_P (instantiation))
12529 tree fn;
12531 if (!COMPLETE_TYPE_P (instantiation))
12533 instantiate_class_template (instantiation);
12534 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12535 for (fn = TYPE_METHODS (instantiation);
12537 fn = TREE_CHAIN (fn))
12538 if (! DECL_ARTIFICIAL (fn))
12539 instantiate_decl (fn,
12540 /*defer_ok=*/0,
12541 /*expl_inst_class_mem_p=*/false);
12542 if (COMPLETE_TYPE_P (instantiation))
12543 reconsider = 1;
12546 if (COMPLETE_TYPE_P (instantiation))
12547 /* If INSTANTIATION has been instantiated, then we don't
12548 need to consider it again in the future. */
12549 *t = TREE_CHAIN (*t);
12550 else
12552 last = *t;
12553 t = &TREE_CHAIN (*t);
12556 else
12558 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12559 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12561 instantiation
12562 = instantiate_decl (instantiation,
12563 /*defer_ok=*/0,
12564 /*expl_inst_class_mem_p=*/false);
12565 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12566 reconsider = 1;
12569 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12570 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12571 /* If INSTANTIATION has been instantiated, then we don't
12572 need to consider it again in the future. */
12573 *t = TREE_CHAIN (*t);
12574 else
12576 last = *t;
12577 t = &TREE_CHAIN (*t);
12580 tinst_depth = 0;
12581 current_tinst_level = NULL_TREE;
12583 last_pending_template = last;
12585 while (reconsider);
12587 input_location = saved_loc;
12588 in_system_header = saved_in_system_header;
12591 /* Substitute ARGVEC into T, which is a list of initializers for
12592 either base class or a non-static data member. The TREE_PURPOSEs
12593 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12594 instantiate_decl. */
12596 static tree
12597 tsubst_initializer_list (tree t, tree argvec)
12599 tree inits = NULL_TREE;
12601 for (; t; t = TREE_CHAIN (t))
12603 tree decl;
12604 tree init;
12606 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
12607 NULL_TREE);
12608 decl = expand_member_init (decl);
12609 if (decl && !DECL_P (decl))
12610 in_base_initializer = 1;
12612 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12613 NULL_TREE,
12614 /*integral_constant_expression_p=*/false);
12615 in_base_initializer = 0;
12617 if (decl)
12619 init = build_tree_list (decl, init);
12620 TREE_CHAIN (init) = inits;
12621 inits = init;
12624 return inits;
12627 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12629 static void
12630 set_current_access_from_decl (tree decl)
12632 if (TREE_PRIVATE (decl))
12633 current_access_specifier = access_private_node;
12634 else if (TREE_PROTECTED (decl))
12635 current_access_specifier = access_protected_node;
12636 else
12637 current_access_specifier = access_public_node;
12640 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12641 is the instantiation (which should have been created with
12642 start_enum) and ARGS are the template arguments to use. */
12644 static void
12645 tsubst_enum (tree tag, tree newtag, tree args)
12647 tree e;
12649 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12651 tree value;
12652 tree decl;
12654 decl = TREE_VALUE (e);
12655 /* Note that in a template enum, the TREE_VALUE is the
12656 CONST_DECL, not the corresponding INTEGER_CST. */
12657 value = tsubst_expr (DECL_INITIAL (decl),
12658 args, tf_warning_or_error, NULL_TREE,
12659 /*integral_constant_expression_p=*/true);
12661 /* Give this enumeration constant the correct access. */
12662 set_current_access_from_decl (decl);
12664 /* Actually build the enumerator itself. */
12665 build_enumerator (DECL_NAME (decl), value, newtag);
12668 finish_enum (newtag);
12669 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12670 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12673 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12674 its type -- but without substituting the innermost set of template
12675 arguments. So, innermost set of template parameters will appear in
12676 the type. */
12678 tree
12679 get_mostly_instantiated_function_type (tree decl)
12681 tree fn_type;
12682 tree tmpl;
12683 tree targs;
12684 tree tparms;
12685 int parm_depth;
12687 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12688 targs = DECL_TI_ARGS (decl);
12689 tparms = DECL_TEMPLATE_PARMS (tmpl);
12690 parm_depth = TMPL_PARMS_DEPTH (tparms);
12692 /* There should be as many levels of arguments as there are levels
12693 of parameters. */
12694 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12696 fn_type = TREE_TYPE (tmpl);
12698 if (parm_depth == 1)
12699 /* No substitution is necessary. */
12701 else
12703 int i, save_access_control;
12704 tree partial_args;
12706 /* Replace the innermost level of the TARGS with NULL_TREEs to
12707 let tsubst know not to substitute for those parameters. */
12708 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12709 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12710 SET_TMPL_ARGS_LEVEL (partial_args, i,
12711 TMPL_ARGS_LEVEL (targs, i));
12712 SET_TMPL_ARGS_LEVEL (partial_args,
12713 TMPL_ARGS_DEPTH (targs),
12714 make_tree_vec (DECL_NTPARMS (tmpl)));
12716 /* Disable access control as this function is used only during
12717 name-mangling. */
12718 save_access_control = flag_access_control;
12719 flag_access_control = 0;
12721 ++processing_template_decl;
12722 /* Now, do the (partial) substitution to figure out the
12723 appropriate function type. */
12724 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12725 --processing_template_decl;
12727 /* Substitute into the template parameters to obtain the real
12728 innermost set of parameters. This step is important if the
12729 innermost set of template parameters contains value
12730 parameters whose types depend on outer template parameters. */
12731 TREE_VEC_LENGTH (partial_args)--;
12732 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12734 flag_access_control = save_access_control;
12737 return fn_type;
12740 /* Return truthvalue if we're processing a template different from
12741 the last one involved in diagnostics. */
12743 problematic_instantiation_changed (void)
12745 return last_template_error_tick != tinst_level_tick;
12748 /* Remember current template involved in diagnostics. */
12749 void
12750 record_last_problematic_instantiation (void)
12752 last_template_error_tick = tinst_level_tick;
12755 tree
12756 current_instantiation (void)
12758 return current_tinst_level;
12761 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12762 type. Return zero for ok, nonzero for disallowed. Issue error and
12763 warning messages under control of COMPLAIN. */
12765 static int
12766 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12768 if (INTEGRAL_TYPE_P (type))
12769 return 0;
12770 else if (POINTER_TYPE_P (type))
12771 return 0;
12772 else if (TYPE_PTR_TO_MEMBER_P (type))
12773 return 0;
12774 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12775 return 0;
12776 else if (TREE_CODE (type) == TYPENAME_TYPE)
12777 return 0;
12779 if (complain & tf_error)
12780 error ("%q#T is not a valid type for a template constant parameter", type);
12781 return 1;
12784 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12785 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12787 static bool
12788 dependent_type_p_r (tree type)
12790 tree scope;
12792 /* [temp.dep.type]
12794 A type is dependent if it is:
12796 -- a template parameter. Template template parameters are types
12797 for us (since TYPE_P holds true for them) so we handle
12798 them here. */
12799 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12800 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12801 return true;
12802 /* -- a qualified-id with a nested-name-specifier which contains a
12803 class-name that names a dependent type or whose unqualified-id
12804 names a dependent type. */
12805 if (TREE_CODE (type) == TYPENAME_TYPE)
12806 return true;
12807 /* -- a cv-qualified type where the cv-unqualified type is
12808 dependent. */
12809 type = TYPE_MAIN_VARIANT (type);
12810 /* -- a compound type constructed from any dependent type. */
12811 if (TYPE_PTR_TO_MEMBER_P (type))
12812 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12813 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12814 (type)));
12815 else if (TREE_CODE (type) == POINTER_TYPE
12816 || TREE_CODE (type) == REFERENCE_TYPE)
12817 return dependent_type_p (TREE_TYPE (type));
12818 else if (TREE_CODE (type) == FUNCTION_TYPE
12819 || TREE_CODE (type) == METHOD_TYPE)
12821 tree arg_type;
12823 if (dependent_type_p (TREE_TYPE (type)))
12824 return true;
12825 for (arg_type = TYPE_ARG_TYPES (type);
12826 arg_type;
12827 arg_type = TREE_CHAIN (arg_type))
12828 if (dependent_type_p (TREE_VALUE (arg_type)))
12829 return true;
12830 return false;
12832 /* -- an array type constructed from any dependent type or whose
12833 size is specified by a constant expression that is
12834 value-dependent. */
12835 if (TREE_CODE (type) == ARRAY_TYPE)
12837 if (TYPE_DOMAIN (type)
12838 && ((value_dependent_expression_p
12839 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12840 || (type_dependent_expression_p
12841 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12842 return true;
12843 return dependent_type_p (TREE_TYPE (type));
12846 /* -- a template-id in which either the template name is a template
12847 parameter ... */
12848 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12849 return true;
12850 /* ... or any of the template arguments is a dependent type or
12851 an expression that is type-dependent or value-dependent. */
12852 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12853 && (any_dependent_template_arguments_p
12854 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12855 return true;
12857 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12858 expression is not type-dependent, then it should already been
12859 have resolved. */
12860 if (TREE_CODE (type) == TYPEOF_TYPE)
12861 return true;
12863 /* The standard does not specifically mention types that are local
12864 to template functions or local classes, but they should be
12865 considered dependent too. For example:
12867 template <int I> void f() {
12868 enum E { a = I };
12869 S<sizeof (E)> s;
12872 The size of `E' cannot be known until the value of `I' has been
12873 determined. Therefore, `E' must be considered dependent. */
12874 scope = TYPE_CONTEXT (type);
12875 if (scope && TYPE_P (scope))
12876 return dependent_type_p (scope);
12877 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12878 return type_dependent_expression_p (scope);
12880 /* Other types are non-dependent. */
12881 return false;
12884 /* Returns TRUE if TYPE is dependent, in the sense of
12885 [temp.dep.type]. */
12887 bool
12888 dependent_type_p (tree type)
12890 /* If there are no template parameters in scope, then there can't be
12891 any dependent types. */
12892 if (!processing_template_decl)
12894 /* If we are not processing a template, then nobody should be
12895 providing us with a dependent type. */
12896 gcc_assert (type);
12897 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12898 return false;
12901 /* If the type is NULL, we have not computed a type for the entity
12902 in question; in that case, the type is dependent. */
12903 if (!type)
12904 return true;
12906 /* Erroneous types can be considered non-dependent. */
12907 if (type == error_mark_node)
12908 return false;
12910 /* If we have not already computed the appropriate value for TYPE,
12911 do so now. */
12912 if (!TYPE_DEPENDENT_P_VALID (type))
12914 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12915 TYPE_DEPENDENT_P_VALID (type) = 1;
12918 return TYPE_DEPENDENT_P (type);
12921 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12923 static bool
12924 dependent_scope_ref_p (tree expression, bool criterion (tree))
12926 tree scope;
12927 tree name;
12929 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12931 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12932 return true;
12934 scope = TREE_OPERAND (expression, 0);
12935 name = TREE_OPERAND (expression, 1);
12937 /* [temp.dep.expr]
12939 An id-expression is type-dependent if it contains a
12940 nested-name-specifier that contains a class-name that names a
12941 dependent type. */
12942 /* The suggested resolution to Core Issue 2 implies that if the
12943 qualifying type is the current class, then we must peek
12944 inside it. */
12945 if (DECL_P (name)
12946 && currently_open_class (scope)
12947 && !criterion (name))
12948 return false;
12949 if (dependent_type_p (scope))
12950 return true;
12952 return false;
12955 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12956 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12957 expression. */
12959 bool
12960 value_dependent_expression_p (tree expression)
12962 if (!processing_template_decl)
12963 return false;
12965 /* A name declared with a dependent type. */
12966 if (DECL_P (expression) && type_dependent_expression_p (expression))
12967 return true;
12969 switch (TREE_CODE (expression))
12971 case IDENTIFIER_NODE:
12972 /* A name that has not been looked up -- must be dependent. */
12973 return true;
12975 case TEMPLATE_PARM_INDEX:
12976 /* A non-type template parm. */
12977 return true;
12979 case CONST_DECL:
12980 /* A non-type template parm. */
12981 if (DECL_TEMPLATE_PARM_P (expression))
12982 return true;
12983 return false;
12985 case VAR_DECL:
12986 /* A constant with integral or enumeration type and is initialized
12987 with an expression that is value-dependent. */
12988 if (DECL_INITIAL (expression)
12989 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12990 && value_dependent_expression_p (DECL_INITIAL (expression)))
12991 return true;
12992 return false;
12994 case DYNAMIC_CAST_EXPR:
12995 case STATIC_CAST_EXPR:
12996 case CONST_CAST_EXPR:
12997 case REINTERPRET_CAST_EXPR:
12998 case CAST_EXPR:
12999 /* These expressions are value-dependent if the type to which
13000 the cast occurs is dependent or the expression being casted
13001 is value-dependent. */
13003 tree type = TREE_TYPE (expression);
13005 if (dependent_type_p (type))
13006 return true;
13008 /* A functional cast has a list of operands. */
13009 expression = TREE_OPERAND (expression, 0);
13010 if (!expression)
13012 /* If there are no operands, it must be an expression such
13013 as "int()". This should not happen for aggregate types
13014 because it would form non-constant expressions. */
13015 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
13017 return false;
13020 if (TREE_CODE (expression) == TREE_LIST)
13022 for (; expression; expression = TREE_CHAIN (expression))
13023 if (value_dependent_expression_p (TREE_VALUE (expression)))
13024 return true;
13025 return false;
13028 return value_dependent_expression_p (expression);
13031 case SIZEOF_EXPR:
13032 case ALIGNOF_EXPR:
13033 /* A `sizeof' expression is value-dependent if the operand is
13034 type-dependent. */
13035 expression = TREE_OPERAND (expression, 0);
13036 if (TYPE_P (expression))
13037 return dependent_type_p (expression);
13038 return type_dependent_expression_p (expression);
13040 case SCOPE_REF:
13041 return dependent_scope_ref_p (expression, value_dependent_expression_p);
13043 case COMPONENT_REF:
13044 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
13045 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
13047 case CALL_EXPR:
13048 /* A CALL_EXPR may appear in a constant expression if it is a
13049 call to a builtin function, e.g., __builtin_constant_p. All
13050 such calls are value-dependent. */
13051 return true;
13053 default:
13054 /* A constant expression is value-dependent if any subexpression is
13055 value-dependent. */
13056 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
13058 case tcc_reference:
13059 case tcc_unary:
13060 return (value_dependent_expression_p
13061 (TREE_OPERAND (expression, 0)));
13063 case tcc_comparison:
13064 case tcc_binary:
13065 return ((value_dependent_expression_p
13066 (TREE_OPERAND (expression, 0)))
13067 || (value_dependent_expression_p
13068 (TREE_OPERAND (expression, 1))));
13070 case tcc_expression:
13071 case tcc_vl_exp:
13073 int i;
13074 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
13075 /* In some cases, some of the operands may be missing.
13076 (For example, in the case of PREDECREMENT_EXPR, the
13077 amount to increment by may be missing.) That doesn't
13078 make the expression dependent. */
13079 if (TREE_OPERAND (expression, i)
13080 && (value_dependent_expression_p
13081 (TREE_OPERAND (expression, i))))
13082 return true;
13083 return false;
13086 default:
13087 break;
13091 /* The expression is not value-dependent. */
13092 return false;
13095 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
13096 [temp.dep.expr]. */
13098 bool
13099 type_dependent_expression_p (tree expression)
13101 if (!processing_template_decl)
13102 return false;
13104 if (expression == error_mark_node)
13105 return false;
13107 /* An unresolved name is always dependent. */
13108 if (TREE_CODE (expression) == IDENTIFIER_NODE
13109 || TREE_CODE (expression) == USING_DECL)
13110 return true;
13112 /* Some expression forms are never type-dependent. */
13113 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
13114 || TREE_CODE (expression) == SIZEOF_EXPR
13115 || TREE_CODE (expression) == ALIGNOF_EXPR
13116 || TREE_CODE (expression) == TYPEID_EXPR
13117 || TREE_CODE (expression) == DELETE_EXPR
13118 || TREE_CODE (expression) == VEC_DELETE_EXPR
13119 || TREE_CODE (expression) == THROW_EXPR)
13120 return false;
13122 /* The types of these expressions depends only on the type to which
13123 the cast occurs. */
13124 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
13125 || TREE_CODE (expression) == STATIC_CAST_EXPR
13126 || TREE_CODE (expression) == CONST_CAST_EXPR
13127 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
13128 || TREE_CODE (expression) == CAST_EXPR)
13129 return dependent_type_p (TREE_TYPE (expression));
13131 /* The types of these expressions depends only on the type created
13132 by the expression. */
13133 if (TREE_CODE (expression) == NEW_EXPR
13134 || TREE_CODE (expression) == VEC_NEW_EXPR)
13136 /* For NEW_EXPR tree nodes created inside a template, either
13137 the object type itself or a TREE_LIST may appear as the
13138 operand 1. */
13139 tree type = TREE_OPERAND (expression, 1);
13140 if (TREE_CODE (type) == TREE_LIST)
13141 /* This is an array type. We need to check array dimensions
13142 as well. */
13143 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
13144 || value_dependent_expression_p
13145 (TREE_OPERAND (TREE_VALUE (type), 1));
13146 else
13147 return dependent_type_p (type);
13150 if (TREE_CODE (expression) == SCOPE_REF
13151 && dependent_scope_ref_p (expression,
13152 type_dependent_expression_p))
13153 return true;
13155 if (TREE_CODE (expression) == FUNCTION_DECL
13156 && DECL_LANG_SPECIFIC (expression)
13157 && DECL_TEMPLATE_INFO (expression)
13158 && (any_dependent_template_arguments_p
13159 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
13160 return true;
13162 if (TREE_CODE (expression) == TEMPLATE_DECL
13163 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
13164 return false;
13166 if (TREE_TYPE (expression) == unknown_type_node)
13168 if (TREE_CODE (expression) == ADDR_EXPR)
13169 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
13170 if (TREE_CODE (expression) == COMPONENT_REF
13171 || TREE_CODE (expression) == OFFSET_REF)
13173 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
13174 return true;
13175 expression = TREE_OPERAND (expression, 1);
13176 if (TREE_CODE (expression) == IDENTIFIER_NODE)
13177 return false;
13179 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
13180 if (TREE_CODE (expression) == SCOPE_REF)
13181 return false;
13183 if (TREE_CODE (expression) == BASELINK)
13184 expression = BASELINK_FUNCTIONS (expression);
13186 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
13188 if (any_dependent_template_arguments_p
13189 (TREE_OPERAND (expression, 1)))
13190 return true;
13191 expression = TREE_OPERAND (expression, 0);
13193 gcc_assert (TREE_CODE (expression) == OVERLOAD
13194 || TREE_CODE (expression) == FUNCTION_DECL);
13196 while (expression)
13198 if (type_dependent_expression_p (OVL_CURRENT (expression)))
13199 return true;
13200 expression = OVL_NEXT (expression);
13202 return false;
13205 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
13207 return (dependent_type_p (TREE_TYPE (expression)));
13210 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13211 contains a type-dependent expression. */
13213 bool
13214 any_type_dependent_arguments_p (tree args)
13216 while (args)
13218 tree arg = TREE_VALUE (args);
13220 if (type_dependent_expression_p (arg))
13221 return true;
13222 args = TREE_CHAIN (args);
13224 return false;
13227 /* Returns TRUE if the ARG (a template argument) is dependent. */
13229 static bool
13230 dependent_template_arg_p (tree arg)
13232 if (!processing_template_decl)
13233 return false;
13235 if (TREE_CODE (arg) == TEMPLATE_DECL
13236 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13237 return dependent_template_p (arg);
13238 else if (TYPE_P (arg))
13239 return dependent_type_p (arg);
13240 else
13241 return (type_dependent_expression_p (arg)
13242 || value_dependent_expression_p (arg));
13245 /* Returns true if ARGS (a collection of template arguments) contains
13246 any types that require structural equality testing. */
13248 bool
13249 any_template_arguments_need_structural_equality_p (tree args)
13251 int i;
13252 int j;
13254 if (!args)
13255 return false;
13256 if (args == error_mark_node)
13257 return true;
13259 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13261 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13262 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13264 tree arg = TREE_VEC_ELT (level, j);
13265 if (TREE_CODE (arg) == TEMPLATE_DECL
13266 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13267 continue;
13268 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
13269 return true;
13270 else if (!TYPE_P (arg) && TREE_TYPE (arg)
13271 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
13272 return true;
13276 return false;
13279 /* Returns true if ARGS (a collection of template arguments) contains
13280 any dependent arguments. */
13282 bool
13283 any_dependent_template_arguments_p (tree args)
13285 int i;
13286 int j;
13288 if (!args)
13289 return false;
13290 if (args == error_mark_node)
13291 return true;
13293 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13295 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13296 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13297 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13298 return true;
13301 return false;
13304 /* Returns TRUE if the template TMPL is dependent. */
13306 bool
13307 dependent_template_p (tree tmpl)
13309 if (TREE_CODE (tmpl) == OVERLOAD)
13311 while (tmpl)
13313 if (dependent_template_p (OVL_FUNCTION (tmpl)))
13314 return true;
13315 tmpl = OVL_CHAIN (tmpl);
13317 return false;
13320 /* Template template parameters are dependent. */
13321 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13322 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13323 return true;
13324 /* So are names that have not been looked up. */
13325 if (TREE_CODE (tmpl) == SCOPE_REF
13326 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
13327 return true;
13328 /* So are member templates of dependent classes. */
13329 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13330 return dependent_type_p (DECL_CONTEXT (tmpl));
13331 return false;
13334 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13336 bool
13337 dependent_template_id_p (tree tmpl, tree args)
13339 return (dependent_template_p (tmpl)
13340 || any_dependent_template_arguments_p (args));
13343 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13344 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13345 can be found. Note that this function peers inside uninstantiated
13346 templates and therefore should be used only in extremely limited
13347 situations. ONLY_CURRENT_P restricts this peering to the currently
13348 open classes hierarchy (which is required when comparing types). */
13350 tree
13351 resolve_typename_type (tree type, bool only_current_p)
13353 tree scope;
13354 tree name;
13355 tree decl;
13356 int quals;
13357 tree pushed_scope;
13359 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
13361 scope = TYPE_CONTEXT (type);
13362 name = TYPE_IDENTIFIER (type);
13364 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13365 it first before we can figure out what NAME refers to. */
13366 if (TREE_CODE (scope) == TYPENAME_TYPE)
13367 scope = resolve_typename_type (scope, only_current_p);
13368 /* If we don't know what SCOPE refers to, then we cannot resolve the
13369 TYPENAME_TYPE. */
13370 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13371 return error_mark_node;
13372 /* If the SCOPE is a template type parameter, we have no way of
13373 resolving the name. */
13374 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13375 return type;
13376 /* If the SCOPE is not the current instantiation, there's no reason
13377 to look inside it. */
13378 if (only_current_p && !currently_open_class (scope))
13379 return error_mark_node;
13380 /* If SCOPE is a partial instantiation, it will not have a valid
13381 TYPE_FIELDS list, so use the original template. */
13382 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
13383 /* Enter the SCOPE so that name lookup will be resolved as if we
13384 were in the class definition. In particular, SCOPE will no
13385 longer be considered a dependent type. */
13386 pushed_scope = push_scope (scope);
13387 /* Look up the declaration. */
13388 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
13389 /* Obtain the set of qualifiers applied to the TYPE. */
13390 quals = cp_type_quals (type);
13391 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13392 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13393 if (!decl)
13394 type = error_mark_node;
13395 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13396 && TREE_CODE (decl) == TYPE_DECL)
13397 type = TREE_TYPE (decl);
13398 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13399 && DECL_CLASS_TEMPLATE_P (decl))
13401 tree tmpl;
13402 tree args;
13403 /* Obtain the template and the arguments. */
13404 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13405 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13406 /* Instantiate the template. */
13407 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
13408 /*entering_scope=*/0, tf_error | tf_user);
13410 else
13411 type = error_mark_node;
13412 /* Qualify the resulting type. */
13413 if (type != error_mark_node && quals)
13414 type = cp_build_qualified_type (type, quals);
13415 /* Leave the SCOPE. */
13416 if (pushed_scope)
13417 pop_scope (pushed_scope);
13419 return type;
13422 /* EXPR is an expression which is not type-dependent. Return a proxy
13423 for EXPR that can be used to compute the types of larger
13424 expressions containing EXPR. */
13426 tree
13427 build_non_dependent_expr (tree expr)
13429 tree inner_expr;
13431 /* Preserve null pointer constants so that the type of things like
13432 "p == 0" where "p" is a pointer can be determined. */
13433 if (null_ptr_cst_p (expr))
13434 return expr;
13435 /* Preserve OVERLOADs; the functions must be available to resolve
13436 types. */
13437 inner_expr = expr;
13438 if (TREE_CODE (inner_expr) == ADDR_EXPR)
13439 inner_expr = TREE_OPERAND (inner_expr, 0);
13440 if (TREE_CODE (inner_expr) == COMPONENT_REF)
13441 inner_expr = TREE_OPERAND (inner_expr, 1);
13442 if (is_overloaded_fn (inner_expr)
13443 || TREE_CODE (inner_expr) == OFFSET_REF)
13444 return expr;
13445 /* There is no need to return a proxy for a variable. */
13446 if (TREE_CODE (expr) == VAR_DECL)
13447 return expr;
13448 /* Preserve string constants; conversions from string constants to
13449 "char *" are allowed, even though normally a "const char *"
13450 cannot be used to initialize a "char *". */
13451 if (TREE_CODE (expr) == STRING_CST)
13452 return expr;
13453 /* Preserve arithmetic constants, as an optimization -- there is no
13454 reason to create a new node. */
13455 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13456 return expr;
13457 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13458 There is at least one place where we want to know that a
13459 particular expression is a throw-expression: when checking a ?:
13460 expression, there are special rules if the second or third
13461 argument is a throw-expression. */
13462 if (TREE_CODE (expr) == THROW_EXPR)
13463 return expr;
13465 if (TREE_CODE (expr) == COND_EXPR)
13466 return build3 (COND_EXPR,
13467 TREE_TYPE (expr),
13468 TREE_OPERAND (expr, 0),
13469 (TREE_OPERAND (expr, 1)
13470 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13471 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13472 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
13473 if (TREE_CODE (expr) == COMPOUND_EXPR
13474 && !COMPOUND_EXPR_OVERLOADED (expr))
13475 return build2 (COMPOUND_EXPR,
13476 TREE_TYPE (expr),
13477 TREE_OPERAND (expr, 0),
13478 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
13480 /* If the type is unknown, it can't really be non-dependent */
13481 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
13483 /* Otherwise, build a NON_DEPENDENT_EXPR.
13485 REFERENCE_TYPEs are not stripped for expressions in templates
13486 because doing so would play havoc with mangling. Consider, for
13487 example:
13489 template <typename T> void f<T& g>() { g(); }
13491 In the body of "f", the expression for "g" will have
13492 REFERENCE_TYPE, even though the standard says that it should
13493 not. The reason is that we must preserve the syntactic form of
13494 the expression so that mangling (say) "f<g>" inside the body of
13495 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13496 stripped here. */
13497 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
13500 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
13501 Return a new TREE_LIST with the various arguments replaced with
13502 equivalent non-dependent expressions. */
13504 tree
13505 build_non_dependent_args (tree args)
13507 tree a;
13508 tree new_args;
13510 new_args = NULL_TREE;
13511 for (a = args; a; a = TREE_CHAIN (a))
13512 new_args = tree_cons (NULL_TREE,
13513 build_non_dependent_expr (TREE_VALUE (a)),
13514 new_args);
13515 return nreverse (new_args);
13518 #include "gt-cp-pt.h"