Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / cp / pt.c
blob10517f17b78f96ea7fe4a5a57dea391d0e78e9ae
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"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work.
56 The TREE_PURPOSE of each entry is either a DECL (for a function or
57 static data member), or a TYPE (for a class) indicating what we are
58 hoping to instantiate. The TREE_VALUE is not used. */
59 static GTY(()) tree pending_templates;
60 static GTY(()) tree last_pending_template;
62 int processing_template_parmlist;
63 static int template_header_count;
65 static GTY(()) tree saved_trees;
66 static GTY(()) varray_type inline_parm_levels;
67 static size_t inline_parm_levels_used;
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 #define UNIFY_ALLOW_NONE 0
84 #define UNIFY_ALLOW_MORE_CV_QUAL 1
85 #define UNIFY_ALLOW_LESS_CV_QUAL 2
86 #define UNIFY_ALLOW_DERIVED 4
87 #define UNIFY_ALLOW_INTEGER 8
88 #define UNIFY_ALLOW_OUTER_LEVEL 16
89 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
90 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
92 static void push_access_scope (tree);
93 static void pop_access_scope (tree);
94 static int resolve_overloaded_unification (tree, tree, tree, tree,
95 unification_kind_t, int);
96 static int try_one_overload (tree, tree, tree, tree, tree,
97 unification_kind_t, int, bool);
98 static int unify (tree, tree, tree, tree, int);
99 static void add_pending_template (tree);
100 static void reopen_tinst_level (tree);
101 static tree classtype_mangled_name (tree);
102 static char* mangle_class_name_for_template (const char *, tree, tree);
103 static tree tsubst_initializer_list (tree, tree);
104 static tree get_class_bindings (tree, tree, tree);
105 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
106 static void tsubst_enum (tree, tree, tree);
107 static tree add_to_template_args (tree, tree);
108 static tree add_outermost_template_args (tree, tree);
109 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
110 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
111 static int type_unification_real (tree, tree, tree, tree,
112 int, unification_kind_t, int);
113 static void note_template_header (int);
114 static tree convert_nontype_argument_function (tree, tree);
115 static tree convert_nontype_argument (tree, tree);
116 static tree convert_template_argument (tree, tree, tree,
117 tsubst_flags_t, int, tree);
118 static int for_each_template_parm (tree, tree_fn_t, void*,
119 struct pointer_set_t*);
120 static tree build_template_parm_index (int, int, int, tree, tree);
121 static int inline_needs_template_parms (tree);
122 static void push_inline_template_parms_recursive (tree, int);
123 static tree retrieve_local_specialization (tree);
124 static void register_local_specialization (tree, tree);
125 static tree reduce_template_parm_level (tree, tree, int);
126 static int mark_template_parm (tree, void *);
127 static int template_parm_this_level_p (tree, void *);
128 static tree tsubst_friend_function (tree, tree);
129 static tree tsubst_friend_class (tree, tree);
130 static int can_complete_type_without_circularity (tree);
131 static tree get_bindings (tree, tree, tree, bool);
132 static int template_decl_level (tree);
133 static int check_cv_quals_for_unify (int, tree, tree);
134 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
135 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
136 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
137 static void regenerate_decl_from_template (tree, tree);
138 static tree most_specialized (tree, tree, tree);
139 static tree most_specialized_class (tree, tree);
140 static int template_class_depth_real (tree, int);
141 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
142 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
143 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
144 static void check_specialization_scope (void);
145 static tree process_partial_specialization (tree);
146 static void set_current_access_from_decl (tree);
147 static void check_default_tmpl_args (tree, tree, int, int);
148 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
149 static tree get_template_base (tree, tree, tree, tree);
150 static tree try_class_unification (tree, tree, tree, tree);
151 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152 tree, tree);
153 static int template_args_equal (tree, tree);
154 static void tsubst_default_arguments (tree);
155 static tree for_each_template_parm_r (tree *, int *, void *);
156 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
157 static void copy_default_args_to_explicit_spec (tree);
158 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
159 static int eq_local_specializations (const void *, const void *);
160 static bool dependent_type_p_r (tree);
161 static tree tsubst (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
165 /* Make the current scope suitable for access checking when we are
166 processing T. T can be FUNCTION_DECL for instantiated function
167 template, or VAR_DECL for static member variable (need by
168 instantiate_decl). */
170 static void
171 push_access_scope (tree t)
173 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
174 || TREE_CODE (t) == VAR_DECL);
176 if (DECL_FRIEND_CONTEXT (t))
177 push_nested_class (DECL_FRIEND_CONTEXT (t));
178 else if (DECL_CLASS_SCOPE_P (t))
179 push_nested_class (DECL_CONTEXT (t));
180 else
181 push_to_top_level ();
183 if (TREE_CODE (t) == FUNCTION_DECL)
185 saved_access_scope = tree_cons
186 (NULL_TREE, current_function_decl, saved_access_scope);
187 current_function_decl = t;
191 /* Restore the scope set up by push_access_scope. T is the node we
192 are processing. */
194 static void
195 pop_access_scope (tree t)
197 if (TREE_CODE (t) == FUNCTION_DECL)
199 current_function_decl = TREE_VALUE (saved_access_scope);
200 saved_access_scope = TREE_CHAIN (saved_access_scope);
203 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
204 pop_nested_class ();
205 else
206 pop_from_top_level ();
209 /* Do any processing required when DECL (a member template
210 declaration) is finished. Returns the TEMPLATE_DECL corresponding
211 to DECL, unless it is a specialization, in which case the DECL
212 itself is returned. */
214 tree
215 finish_member_template_decl (tree decl)
217 if (decl == error_mark_node)
218 return error_mark_node;
220 gcc_assert (DECL_P (decl));
222 if (TREE_CODE (decl) == TYPE_DECL)
224 tree type;
226 type = TREE_TYPE (decl);
227 if (IS_AGGR_TYPE (type)
228 && CLASSTYPE_TEMPLATE_INFO (type)
229 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
231 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
232 check_member_template (tmpl);
233 return tmpl;
235 return NULL_TREE;
237 else if (TREE_CODE (decl) == FIELD_DECL)
238 error ("data member %qD cannot be a member template", decl);
239 else if (DECL_TEMPLATE_INFO (decl))
241 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
243 check_member_template (DECL_TI_TEMPLATE (decl));
244 return DECL_TI_TEMPLATE (decl);
246 else
247 return decl;
249 else
250 error ("invalid member template declaration %qD", decl);
252 return error_mark_node;
255 /* Returns the template nesting level of the indicated class TYPE.
257 For example, in:
258 template <class T>
259 struct A
261 template <class U>
262 struct B {};
265 A<T>::B<U> has depth two, while A<T> has depth one.
266 Both A<T>::B<int> and A<int>::B<U> have depth one, if
267 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
268 specializations.
270 This function is guaranteed to return 0 if passed NULL_TREE so
271 that, for example, `template_class_depth (current_class_type)' is
272 always safe. */
274 static int
275 template_class_depth_real (tree type, int count_specializations)
277 int depth;
279 for (depth = 0;
280 type && TREE_CODE (type) != NAMESPACE_DECL;
281 type = (TREE_CODE (type) == FUNCTION_DECL)
282 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
284 if (TREE_CODE (type) != FUNCTION_DECL)
286 if (CLASSTYPE_TEMPLATE_INFO (type)
287 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
288 && ((count_specializations
289 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
290 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
291 ++depth;
293 else
295 if (DECL_TEMPLATE_INFO (type)
296 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
297 && ((count_specializations
298 && DECL_TEMPLATE_SPECIALIZATION (type))
299 || uses_template_parms (DECL_TI_ARGS (type))))
300 ++depth;
304 return depth;
307 /* Returns the template nesting level of the indicated class TYPE.
308 Like template_class_depth_real, but instantiations do not count in
309 the depth. */
312 template_class_depth (tree type)
314 return template_class_depth_real (type, /*count_specializations=*/0);
317 /* Returns 1 if processing DECL as part of do_pending_inlines
318 needs us to push template parms. */
320 static int
321 inline_needs_template_parms (tree decl)
323 if (! DECL_TEMPLATE_INFO (decl))
324 return 0;
326 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
327 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
330 /* Subroutine of maybe_begin_member_template_processing.
331 Push the template parms in PARMS, starting from LEVELS steps into the
332 chain, and ending at the beginning, since template parms are listed
333 innermost first. */
335 static void
336 push_inline_template_parms_recursive (tree parmlist, int levels)
338 tree parms = TREE_VALUE (parmlist);
339 int i;
341 if (levels > 1)
342 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
344 ++processing_template_decl;
345 current_template_parms
346 = tree_cons (size_int (processing_template_decl),
347 parms, current_template_parms);
348 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
350 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
351 NULL);
352 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
354 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
355 gcc_assert (DECL_P (parm));
357 switch (TREE_CODE (parm))
359 case TYPE_DECL:
360 case TEMPLATE_DECL:
361 pushdecl (parm);
362 break;
364 case PARM_DECL:
366 /* Make a CONST_DECL as is done in process_template_parm.
367 It is ugly that we recreate this here; the original
368 version built in process_template_parm is no longer
369 available. */
370 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
371 TREE_TYPE (parm));
372 DECL_ARTIFICIAL (decl) = 1;
373 TREE_CONSTANT (decl) = 1;
374 TREE_INVARIANT (decl) = 1;
375 TREE_READONLY (decl) = 1;
376 DECL_INITIAL (decl) = DECL_INITIAL (parm);
377 SET_DECL_TEMPLATE_PARM_P (decl);
378 pushdecl (decl);
380 break;
382 default:
383 gcc_unreachable ();
388 /* Restore the template parameter context for a member template or
389 a friend template defined in a class definition. */
391 void
392 maybe_begin_member_template_processing (tree decl)
394 tree parms;
395 int levels = 0;
397 if (inline_needs_template_parms (decl))
399 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
400 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
402 if (DECL_TEMPLATE_SPECIALIZATION (decl))
404 --levels;
405 parms = TREE_CHAIN (parms);
408 push_inline_template_parms_recursive (parms, levels);
411 /* Remember how many levels of template parameters we pushed so that
412 we can pop them later. */
413 if (!inline_parm_levels)
414 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
415 if (inline_parm_levels_used == inline_parm_levels->num_elements)
416 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
417 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
418 ++inline_parm_levels_used;
421 /* Undo the effects of maybe_begin_member_template_processing. */
423 void
424 maybe_end_member_template_processing (void)
426 int i;
428 if (!inline_parm_levels_used)
429 return;
431 --inline_parm_levels_used;
432 for (i = 0;
433 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
434 ++i)
436 --processing_template_decl;
437 current_template_parms = TREE_CHAIN (current_template_parms);
438 poplevel (0, 0, 0);
442 /* Return a new template argument vector which contains all of ARGS,
443 but has as its innermost set of arguments the EXTRA_ARGS. */
445 static tree
446 add_to_template_args (tree args, tree extra_args)
448 tree new_args;
449 int extra_depth;
450 int i;
451 int j;
453 extra_depth = TMPL_ARGS_DEPTH (extra_args);
454 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
456 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
457 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
459 for (j = 1; j <= extra_depth; ++j, ++i)
460 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
462 return new_args;
465 /* Like add_to_template_args, but only the outermost ARGS are added to
466 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
467 (EXTRA_ARGS) levels are added. This function is used to combine
468 the template arguments from a partial instantiation with the
469 template arguments used to attain the full instantiation from the
470 partial instantiation. */
472 static tree
473 add_outermost_template_args (tree args, tree extra_args)
475 tree new_args;
477 /* If there are more levels of EXTRA_ARGS than there are ARGS,
478 something very fishy is going on. */
479 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
481 /* If *all* the new arguments will be the EXTRA_ARGS, just return
482 them. */
483 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
484 return extra_args;
486 /* For the moment, we make ARGS look like it contains fewer levels. */
487 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
489 new_args = add_to_template_args (args, extra_args);
491 /* Now, we restore ARGS to its full dimensions. */
492 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
494 return new_args;
497 /* Return the N levels of innermost template arguments from the ARGS. */
499 tree
500 get_innermost_template_args (tree args, int n)
502 tree new_args;
503 int extra_levels;
504 int i;
506 gcc_assert (n >= 0);
508 /* If N is 1, just return the innermost set of template arguments. */
509 if (n == 1)
510 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
512 /* If we're not removing anything, just return the arguments we were
513 given. */
514 extra_levels = TMPL_ARGS_DEPTH (args) - n;
515 gcc_assert (extra_levels >= 0);
516 if (extra_levels == 0)
517 return args;
519 /* Make a new set of arguments, not containing the outer arguments. */
520 new_args = make_tree_vec (n);
521 for (i = 1; i <= n; ++i)
522 SET_TMPL_ARGS_LEVEL (new_args, i,
523 TMPL_ARGS_LEVEL (args, i + extra_levels));
525 return new_args;
528 /* We've got a template header coming up; push to a new level for storing
529 the parms. */
531 void
532 begin_template_parm_list (void)
534 /* We use a non-tag-transparent scope here, which causes pushtag to
535 put tags in this scope, rather than in the enclosing class or
536 namespace scope. This is the right thing, since we want
537 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
538 global template class, push_template_decl handles putting the
539 TEMPLATE_DECL into top-level scope. For a nested template class,
540 e.g.:
542 template <class T> struct S1 {
543 template <class T> struct S2 {};
546 pushtag contains special code to call pushdecl_with_scope on the
547 TEMPLATE_DECL for S2. */
548 begin_scope (sk_template_parms, NULL);
549 ++processing_template_decl;
550 ++processing_template_parmlist;
551 note_template_header (0);
554 /* This routine is called when a specialization is declared. If it is
555 invalid to declare a specialization here, an error is reported. */
557 static void
558 check_specialization_scope (void)
560 tree scope = current_scope ();
562 /* [temp.expl.spec]
564 An explicit specialization shall be declared in the namespace of
565 which the template is a member, or, for member templates, in the
566 namespace of which the enclosing class or enclosing class
567 template is a member. An explicit specialization of a member
568 function, member class or static data member of a class template
569 shall be declared in the namespace of which the class template
570 is a member. */
571 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
572 error ("explicit specialization in non-namespace scope %qD", scope);
574 /* [temp.expl.spec]
576 In an explicit specialization declaration for a member of a class
577 template or a member template that appears in namespace scope,
578 the member template and some of its enclosing class templates may
579 remain unspecialized, except that the declaration shall not
580 explicitly specialize a class member template if its enclosing
581 class templates are not explicitly specialized as well. */
582 if (current_template_parms)
583 error ("enclosing class templates are not explicitly specialized");
586 /* We've just seen template <>. */
588 void
589 begin_specialization (void)
591 begin_scope (sk_template_spec, NULL);
592 note_template_header (1);
593 check_specialization_scope ();
596 /* Called at then end of processing a declaration preceded by
597 template<>. */
599 void
600 end_specialization (void)
602 finish_scope ();
603 reset_specialization ();
606 /* Any template <>'s that we have seen thus far are not referring to a
607 function specialization. */
609 void
610 reset_specialization (void)
612 processing_specialization = 0;
613 template_header_count = 0;
616 /* We've just seen a template header. If SPECIALIZATION is nonzero,
617 it was of the form template <>. */
619 static void
620 note_template_header (int specialization)
622 processing_specialization = specialization;
623 template_header_count++;
626 /* We're beginning an explicit instantiation. */
628 void
629 begin_explicit_instantiation (void)
631 gcc_assert (!processing_explicit_instantiation);
632 processing_explicit_instantiation = true;
636 void
637 end_explicit_instantiation (void)
639 gcc_assert (processing_explicit_instantiation);
640 processing_explicit_instantiation = false;
643 /* An explicit specialization or partial specialization TMPL is being
644 declared. Check that the namespace in which the specialization is
645 occurring is permissible. Returns false iff it is invalid to
646 specialize TMPL in the current namespace. */
648 static bool
649 check_specialization_namespace (tree tmpl)
651 tree tpl_ns = decl_namespace_context (tmpl);
653 /* [tmpl.expl.spec]
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template is
661 a member. */
662 if (is_associated_namespace (current_namespace, tpl_ns))
663 /* Same or super-using namespace. */
664 return true;
665 else
667 pedwarn ("specialization of %qD in different namespace", tmpl);
668 pedwarn (" from definition of %q+#D", tmpl);
669 return false;
673 /* SPEC is an explicit instantiation. Check that it is valid to
674 perform this explicit instantiation in the current namespace. */
676 static void
677 check_explicit_instantiation_namespace (tree spec)
679 tree ns;
681 /* DR 275: An explicit instantiation shall appear in an enclosing
682 namespace of its template. */
683 ns = decl_namespace_context (spec);
684 if (!is_ancestor (current_namespace, ns))
685 pedwarn ("explicit instantiation of %qD in namespace %qD "
686 "(which does not enclose namespace %qD)",
687 spec, current_namespace, ns);
690 /* The TYPE is being declared. If it is a template type, that means it
691 is a partial specialization. Do appropriate error-checking. */
693 tree
694 maybe_process_partial_specialization (tree type)
696 tree context;
698 if (type == error_mark_node)
699 return error_mark_node;
701 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
703 error ("name of class shadows template template parameter %qD",
704 TYPE_NAME (type));
705 return error_mark_node;
708 context = TYPE_CONTEXT (type);
710 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
712 /* This is for ordinary explicit specialization and partial
713 specialization of a template class such as:
715 template <> class C<int>;
719 template <class T> class C<T*>;
721 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
723 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
724 && !COMPLETE_TYPE_P (type))
726 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
727 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
728 if (processing_template_decl)
729 push_template_decl (TYPE_MAIN_DECL (type));
731 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
732 error ("specialization of %qT after instantiation", type);
734 else if (CLASS_TYPE_P (type)
735 && !CLASSTYPE_USE_TEMPLATE (type)
736 && CLASSTYPE_TEMPLATE_INFO (type)
737 && context && CLASS_TYPE_P (context)
738 && CLASSTYPE_TEMPLATE_INFO (context))
740 /* This is for an explicit specialization of member class
741 template according to [temp.expl.spec/18]:
743 template <> template <class U> class C<int>::D;
745 The context `C<int>' must be an implicit instantiation.
746 Otherwise this is just a member class template declared
747 earlier like:
749 template <> class C<int> { template <class U> class D; };
750 template <> template <class U> class C<int>::D;
752 In the first case, `C<int>::D' is a specialization of `C<T>::D'
753 while in the second case, `C<int>::D' is a primary template
754 and `C<T>::D' may not exist. */
756 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
757 && !COMPLETE_TYPE_P (type))
759 tree t;
761 if (current_namespace
762 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
764 pedwarn ("specializing %q#T in different namespace", type);
765 pedwarn (" from definition of %q+#D",
766 CLASSTYPE_TI_TEMPLATE (type));
769 /* Check for invalid specialization after instantiation:
771 template <> template <> class C<int>::D<int>;
772 template <> template <class U> class C<int>::D; */
774 for (t = DECL_TEMPLATE_INSTANTIATIONS
775 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
776 t; t = TREE_CHAIN (t))
777 if (TREE_VALUE (t) != type
778 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
779 error ("specialization %qT after instantiation %qT",
780 type, TREE_VALUE (t));
782 /* Mark TYPE as a specialization. And as a result, we only
783 have one level of template argument for the innermost
784 class template. */
785 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
786 CLASSTYPE_TI_ARGS (type)
787 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
790 else if (processing_specialization)
792 error ("explicit specialization of non-template %qT", type);
793 return error_mark_node;
796 return type;
799 /* Returns nonzero if we can optimize the retrieval of specializations
800 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
801 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
803 static inline bool
804 optimize_specialization_lookup_p (tree tmpl)
806 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
807 && DECL_CLASS_SCOPE_P (tmpl)
808 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
809 parameter. */
810 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
811 /* The optimized lookup depends on the fact that the
812 template arguments for the member function template apply
813 purely to the containing class, which is not true if the
814 containing class is an explicit or partial
815 specialization. */
816 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
817 && !DECL_MEMBER_TEMPLATE_P (tmpl)
818 && !DECL_CONV_FN_P (tmpl)
819 /* It is possible to have a template that is not a member
820 template and is not a member of a template class:
822 template <typename T>
823 struct S { friend A::f(); };
825 Here, the friend function is a template, but the context does
826 not have template information. The optimized lookup relies
827 on having ARGS be the template arguments for both the class
828 and the function template. */
829 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
832 /* Retrieve the specialization (in the sense of [temp.spec] - a
833 specialization is either an instantiation or an explicit
834 specialization) of TMPL for the given template ARGS. If there is
835 no such specialization, return NULL_TREE. The ARGS are a vector of
836 arguments, or a vector of vectors of arguments, in the case of
837 templates with more than one level of parameters.
839 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
840 then we search for a partial specialization matching ARGS. This
841 parameter is ignored if TMPL is not a class template. */
843 static tree
844 retrieve_specialization (tree tmpl, tree args,
845 bool class_specializations_p)
847 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
849 /* There should be as many levels of arguments as there are
850 levels of parameters. */
851 gcc_assert (TMPL_ARGS_DEPTH (args)
852 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
854 if (optimize_specialization_lookup_p (tmpl))
856 tree class_template;
857 tree class_specialization;
858 VEC(tree,gc) *methods;
859 tree fns;
860 int idx;
862 /* The template arguments actually apply to the containing
863 class. Find the class specialization with those
864 arguments. */
865 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
866 class_specialization
867 = retrieve_specialization (class_template, args,
868 /*class_specializations_p=*/false);
869 if (!class_specialization)
870 return NULL_TREE;
871 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
872 for the specialization. */
873 idx = class_method_index_for_fn (class_specialization, tmpl);
874 if (idx == -1)
875 return NULL_TREE;
876 /* Iterate through the methods with the indicated name, looking
877 for the one that has an instance of TMPL. */
878 methods = CLASSTYPE_METHOD_VEC (class_specialization);
879 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
881 tree fn = OVL_CURRENT (fns);
882 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
883 return fn;
885 return NULL_TREE;
887 else
889 tree *sp;
890 tree *head;
892 /* Class templates store their instantiations on the
893 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
894 DECL_TEMPLATE_SPECIALIZATIONS list. */
895 if (!class_specializations_p
896 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
897 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
898 else
899 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
900 head = sp;
901 /* Iterate through the list until we find a matching template. */
902 while (*sp != NULL_TREE)
904 tree spec = *sp;
906 if (comp_template_args (TREE_PURPOSE (spec), args))
908 /* Use the move-to-front heuristic to speed up future
909 searches. */
910 if (spec != *head)
912 *sp = TREE_CHAIN (*sp);
913 TREE_CHAIN (spec) = *head;
914 *head = spec;
916 return TREE_VALUE (spec);
918 sp = &TREE_CHAIN (spec);
922 return NULL_TREE;
925 /* Like retrieve_specialization, but for local declarations. */
927 static tree
928 retrieve_local_specialization (tree tmpl)
930 tree spec = htab_find_with_hash (local_specializations, tmpl,
931 htab_hash_pointer (tmpl));
932 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
935 /* Returns nonzero iff DECL is a specialization of TMPL. */
938 is_specialization_of (tree decl, tree tmpl)
940 tree t;
942 if (TREE_CODE (decl) == FUNCTION_DECL)
944 for (t = decl;
945 t != NULL_TREE;
946 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
947 if (t == tmpl)
948 return 1;
950 else
952 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
954 for (t = TREE_TYPE (decl);
955 t != NULL_TREE;
956 t = CLASSTYPE_USE_TEMPLATE (t)
957 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
958 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
959 return 1;
962 return 0;
965 /* Returns nonzero iff DECL is a specialization of friend declaration
966 FRIEND according to [temp.friend]. */
968 bool
969 is_specialization_of_friend (tree decl, tree friend)
971 bool need_template = true;
972 int template_depth;
974 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
975 || TREE_CODE (decl) == TYPE_DECL);
977 /* For [temp.friend/6] when FRIEND is an ordinary member function
978 of a template class, we want to check if DECL is a specialization
979 if this. */
980 if (TREE_CODE (friend) == FUNCTION_DECL
981 && DECL_TEMPLATE_INFO (friend)
982 && !DECL_USE_TEMPLATE (friend))
984 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
985 friend = DECL_TI_TEMPLATE (friend);
986 need_template = false;
988 else if (TREE_CODE (friend) == TEMPLATE_DECL
989 && !PRIMARY_TEMPLATE_P (friend))
990 need_template = false;
992 /* There is nothing to do if this is not a template friend. */
993 if (TREE_CODE (friend) != TEMPLATE_DECL)
994 return false;
996 if (is_specialization_of (decl, friend))
997 return true;
999 /* [temp.friend/6]
1000 A member of a class template may be declared to be a friend of a
1001 non-template class. In this case, the corresponding member of
1002 every specialization of the class template is a friend of the
1003 class granting friendship.
1005 For example, given a template friend declaration
1007 template <class T> friend void A<T>::f();
1009 the member function below is considered a friend
1011 template <> struct A<int> {
1012 void f();
1015 For this type of template friend, TEMPLATE_DEPTH below will be
1016 nonzero. To determine if DECL is a friend of FRIEND, we first
1017 check if the enclosing class is a specialization of another. */
1019 template_depth = template_class_depth (DECL_CONTEXT (friend));
1020 if (template_depth
1021 && DECL_CLASS_SCOPE_P (decl)
1022 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1023 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1025 /* Next, we check the members themselves. In order to handle
1026 a few tricky cases, such as when FRIEND's are
1028 template <class T> friend void A<T>::g(T t);
1029 template <class T> template <T t> friend void A<T>::h();
1031 and DECL's are
1033 void A<int>::g(int);
1034 template <int> void A<int>::h();
1036 we need to figure out ARGS, the template arguments from
1037 the context of DECL. This is required for template substitution
1038 of `T' in the function parameter of `g' and template parameter
1039 of `h' in the above examples. Here ARGS corresponds to `int'. */
1041 tree context = DECL_CONTEXT (decl);
1042 tree args = NULL_TREE;
1043 int current_depth = 0;
1045 while (current_depth < template_depth)
1047 if (CLASSTYPE_TEMPLATE_INFO (context))
1049 if (current_depth == 0)
1050 args = TYPE_TI_ARGS (context);
1051 else
1052 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1053 current_depth++;
1055 context = TYPE_CONTEXT (context);
1058 if (TREE_CODE (decl) == FUNCTION_DECL)
1060 bool is_template;
1061 tree friend_type;
1062 tree decl_type;
1063 tree friend_args_type;
1064 tree decl_args_type;
1066 /* Make sure that both DECL and FRIEND are templates or
1067 non-templates. */
1068 is_template = DECL_TEMPLATE_INFO (decl)
1069 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1070 if (need_template ^ is_template)
1071 return false;
1072 else if (is_template)
1074 /* If both are templates, check template parameter list. */
1075 tree friend_parms
1076 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1077 args, tf_none);
1078 if (!comp_template_parms
1079 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1080 friend_parms))
1081 return false;
1083 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1085 else
1086 decl_type = TREE_TYPE (decl);
1088 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1089 tf_none, NULL_TREE);
1090 if (friend_type == error_mark_node)
1091 return false;
1093 /* Check if return types match. */
1094 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1095 return false;
1097 /* Check if function parameter types match, ignoring the
1098 `this' parameter. */
1099 friend_args_type = TYPE_ARG_TYPES (friend_type);
1100 decl_args_type = TYPE_ARG_TYPES (decl_type);
1101 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1102 friend_args_type = TREE_CHAIN (friend_args_type);
1103 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1104 decl_args_type = TREE_CHAIN (decl_args_type);
1106 return compparms (decl_args_type, friend_args_type);
1108 else
1110 /* DECL is a TYPE_DECL */
1111 bool is_template;
1112 tree decl_type = TREE_TYPE (decl);
1114 /* Make sure that both DECL and FRIEND are templates or
1115 non-templates. */
1116 is_template
1117 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1118 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1120 if (need_template ^ is_template)
1121 return false;
1122 else if (is_template)
1124 tree friend_parms;
1125 /* If both are templates, check the name of the two
1126 TEMPLATE_DECL's first because is_friend didn't. */
1127 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1128 != DECL_NAME (friend))
1129 return false;
1131 /* Now check template parameter list. */
1132 friend_parms
1133 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1134 args, tf_none);
1135 return comp_template_parms
1136 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1137 friend_parms);
1139 else
1140 return (DECL_NAME (decl)
1141 == DECL_NAME (friend));
1144 return false;
1147 /* Register the specialization SPEC as a specialization of TMPL with
1148 the indicated ARGS. IS_FRIEND indicates whether the specialization
1149 is actually just a friend declaration. Returns SPEC, or an
1150 equivalent prior declaration, if available. */
1152 static tree
1153 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1155 tree fn;
1157 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1159 if (TREE_CODE (spec) == FUNCTION_DECL
1160 && uses_template_parms (DECL_TI_ARGS (spec)))
1161 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1162 register it; we want the corresponding TEMPLATE_DECL instead.
1163 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1164 the more obvious `uses_template_parms (spec)' to avoid problems
1165 with default function arguments. In particular, given
1166 something like this:
1168 template <class T> void f(T t1, T t = T())
1170 the default argument expression is not substituted for in an
1171 instantiation unless and until it is actually needed. */
1172 return spec;
1174 fn = retrieve_specialization (tmpl, args,
1175 /*class_specializations_p=*/false);
1176 /* We can sometimes try to re-register a specialization that we've
1177 already got. In particular, regenerate_decl_from_template calls
1178 duplicate_decls which will update the specialization list. But,
1179 we'll still get called again here anyhow. It's more convenient
1180 to simply allow this than to try to prevent it. */
1181 if (fn == spec)
1182 return spec;
1183 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1185 if (DECL_TEMPLATE_INSTANTIATION (fn))
1187 if (TREE_USED (fn)
1188 || DECL_EXPLICIT_INSTANTIATION (fn))
1190 error ("specialization of %qD after instantiation",
1191 fn);
1192 return error_mark_node;
1194 else
1196 tree clone;
1197 /* This situation should occur only if the first
1198 specialization is an implicit instantiation, the
1199 second is an explicit specialization, and the
1200 implicit instantiation has not yet been used. That
1201 situation can occur if we have implicitly
1202 instantiated a member function and then specialized
1203 it later.
1205 We can also wind up here if a friend declaration that
1206 looked like an instantiation turns out to be a
1207 specialization:
1209 template <class T> void foo(T);
1210 class S { friend void foo<>(int) };
1211 template <> void foo(int);
1213 We transform the existing DECL in place so that any
1214 pointers to it become pointers to the updated
1215 declaration.
1217 If there was a definition for the template, but not
1218 for the specialization, we want this to look as if
1219 there were no definition, and vice versa. */
1220 DECL_INITIAL (fn) = NULL_TREE;
1221 duplicate_decls (spec, fn, is_friend);
1222 /* The call to duplicate_decls will have applied
1223 [temp.expl.spec]:
1225 An explicit specialization of a function template
1226 is inline only if it is explicitly declared to be,
1227 and independently of whether its function tempalte
1230 to the primary function; now copy the inline bits to
1231 the various clones. */
1232 FOR_EACH_CLONE (clone, fn)
1234 DECL_DECLARED_INLINE_P (clone)
1235 = DECL_DECLARED_INLINE_P (fn);
1236 DECL_INLINE (clone)
1237 = DECL_INLINE (fn);
1239 check_specialization_namespace (fn);
1241 return fn;
1244 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1246 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1247 /* Dup decl failed, but this is a new definition. Set the
1248 line number so any errors match this new
1249 definition. */
1250 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1252 return fn;
1256 /* A specialization must be declared in the same namespace as the
1257 template it is specializing. */
1258 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1259 && !check_specialization_namespace (tmpl))
1260 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1262 if (!optimize_specialization_lookup_p (tmpl))
1263 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1264 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1266 return spec;
1269 /* Unregister the specialization SPEC as a specialization of TMPL.
1270 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1271 if the SPEC was listed as a specialization of TMPL. */
1273 bool
1274 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1276 tree* s;
1278 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1279 *s != NULL_TREE;
1280 s = &TREE_CHAIN (*s))
1281 if (TREE_VALUE (*s) == spec)
1283 if (!new_spec)
1284 *s = TREE_CHAIN (*s);
1285 else
1286 TREE_VALUE (*s) = new_spec;
1287 return 1;
1290 return 0;
1293 /* Compare an entry in the local specializations hash table P1 (which
1294 is really a pointer to a TREE_LIST) with P2 (which is really a
1295 DECL). */
1297 static int
1298 eq_local_specializations (const void *p1, const void *p2)
1300 return TREE_VALUE ((tree) p1) == (tree) p2;
1303 /* Hash P1, an entry in the local specializations table. */
1305 static hashval_t
1306 hash_local_specialization (const void* p1)
1308 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1311 /* Like register_specialization, but for local declarations. We are
1312 registering SPEC, an instantiation of TMPL. */
1314 static void
1315 register_local_specialization (tree spec, tree tmpl)
1317 void **slot;
1319 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1320 htab_hash_pointer (tmpl), INSERT);
1321 *slot = build_tree_list (spec, tmpl);
1324 /* TYPE is a class type. Returns true if TYPE is an explicitly
1325 specialized class. */
1327 bool
1328 explicit_class_specialization_p (tree type)
1330 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1331 return false;
1332 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1335 /* Print the list of candidate FNS in an error message. */
1337 void
1338 print_candidates (tree fns)
1340 tree fn;
1342 const char *str = "candidates are:";
1344 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1346 tree f;
1348 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1349 error ("%s %+#D", str, OVL_CURRENT (f));
1350 str = " ";
1354 /* Returns the template (one of the functions given by TEMPLATE_ID)
1355 which can be specialized to match the indicated DECL with the
1356 explicit template args given in TEMPLATE_ID. The DECL may be
1357 NULL_TREE if none is available. In that case, the functions in
1358 TEMPLATE_ID are non-members.
1360 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1361 specialization of a member template.
1363 The TEMPLATE_COUNT is the number of references to qualifying
1364 template classes that appeared in the name of the function. See
1365 check_explicit_specialization for a more accurate description.
1367 TSK indicates what kind of template declaration (if any) is being
1368 declared. TSK_TEMPLATE indicates that the declaration given by
1369 DECL, though a FUNCTION_DECL, has template parameters, and is
1370 therefore a template function.
1372 The template args (those explicitly specified and those deduced)
1373 are output in a newly created vector *TARGS_OUT.
1375 If it is impossible to determine the result, an error message is
1376 issued. The error_mark_node is returned to indicate failure. */
1378 static tree
1379 determine_specialization (tree template_id,
1380 tree decl,
1381 tree* targs_out,
1382 int need_member_template,
1383 int template_count,
1384 tmpl_spec_kind tsk)
1386 tree fns;
1387 tree targs;
1388 tree explicit_targs;
1389 tree candidates = NULL_TREE;
1390 tree templates = NULL_TREE;
1391 int header_count;
1392 struct cp_binding_level *b;
1394 *targs_out = NULL_TREE;
1396 if (template_id == error_mark_node)
1397 return error_mark_node;
1399 fns = TREE_OPERAND (template_id, 0);
1400 explicit_targs = TREE_OPERAND (template_id, 1);
1402 if (fns == error_mark_node)
1403 return error_mark_node;
1405 /* Check for baselinks. */
1406 if (BASELINK_P (fns))
1407 fns = BASELINK_FUNCTIONS (fns);
1409 if (!is_overloaded_fn (fns))
1411 error ("%qD is not a function template", fns);
1412 return error_mark_node;
1415 /* Count the number of template headers specified for this
1416 specialization. */
1417 header_count = 0;
1418 for (b = current_binding_level;
1419 b->kind == sk_template_parms;
1420 b = b->level_chain)
1421 ++header_count;
1423 for (; fns; fns = OVL_NEXT (fns))
1425 tree fn = OVL_CURRENT (fns);
1427 if (TREE_CODE (fn) == TEMPLATE_DECL)
1429 tree decl_arg_types;
1430 tree fn_arg_types;
1432 /* DECL might be a specialization of FN. */
1434 /* Adjust the type of DECL in case FN is a static member. */
1435 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1436 if (DECL_STATIC_FUNCTION_P (fn)
1437 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1438 decl_arg_types = TREE_CHAIN (decl_arg_types);
1440 /* Check that the number of function parameters matches.
1441 For example,
1442 template <class T> void f(int i = 0);
1443 template <> void f<int>();
1444 The specialization f<int> is invalid but is not caught
1445 by get_bindings below. */
1447 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1448 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1449 continue;
1451 /* For a non-static member function, we need to make sure that
1452 the const qualification is the same. This can be done by
1453 checking the 'this' in the argument list. */
1454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1455 && !same_type_p (TREE_VALUE (fn_arg_types),
1456 TREE_VALUE (decl_arg_types)))
1457 continue;
1459 /* In case of explicit specialization, we need to check if
1460 the number of template headers appearing in the specialization
1461 is correct. This is usually done in check_explicit_specialization,
1462 but the check done there cannot be exhaustive when specializing
1463 member functions. Consider the following code:
1465 template <> void A<int>::f(int);
1466 template <> template <> void A<int>::f(int);
1468 Assuming that A<int> is not itself an explicit specialization
1469 already, the first line specializes "f" which is a non-template
1470 member function, whilst the second line specializes "f" which
1471 is a template member function. So both lines are syntactically
1472 correct, and check_explicit_specialization does not reject
1473 them.
1475 Here, we can do better, as we are matching the specialization
1476 against the declarations. We count the number of template
1477 headers, and we check if they match TEMPLATE_COUNT + 1
1478 (TEMPLATE_COUNT is the number of qualifying template classes,
1479 plus there must be another header for the member template
1480 itself).
1482 Notice that if header_count is zero, this is not a
1483 specialization but rather a template instantiation, so there
1484 is no check we can perform here. */
1485 if (header_count && header_count != template_count + 1)
1486 continue;
1488 /* Check that the number of template arguments at the
1489 innermost level for DECL is the same as for FN. */
1490 if (current_binding_level->kind == sk_template_parms
1491 && !current_binding_level->explicit_spec_p
1492 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1493 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1494 (current_template_parms))))
1495 continue;
1497 /* Function templates cannot be specializations; there are
1498 no partial specializations of functions. Therefore, if
1499 the type of DECL does not match FN, there is no
1500 match. */
1501 if (tsk == tsk_template)
1503 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1504 decl_arg_types))
1505 candidates = tree_cons (NULL_TREE, fn, candidates);
1506 continue;
1509 /* See whether this function might be a specialization of this
1510 template. */
1511 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1513 if (!targs)
1514 /* We cannot deduce template arguments that when used to
1515 specialize TMPL will produce DECL. */
1516 continue;
1518 /* Save this template, and the arguments deduced. */
1519 templates = tree_cons (targs, fn, templates);
1521 else if (need_member_template)
1522 /* FN is an ordinary member function, and we need a
1523 specialization of a member template. */
1525 else if (TREE_CODE (fn) != FUNCTION_DECL)
1526 /* We can get IDENTIFIER_NODEs here in certain erroneous
1527 cases. */
1529 else if (!DECL_FUNCTION_MEMBER_P (fn))
1530 /* This is just an ordinary non-member function. Nothing can
1531 be a specialization of that. */
1533 else if (DECL_ARTIFICIAL (fn))
1534 /* Cannot specialize functions that are created implicitly. */
1536 else
1538 tree decl_arg_types;
1540 /* This is an ordinary member function. However, since
1541 we're here, we can assume it's enclosing class is a
1542 template class. For example,
1544 template <typename T> struct S { void f(); };
1545 template <> void S<int>::f() {}
1547 Here, S<int>::f is a non-template, but S<int> is a
1548 template class. If FN has the same type as DECL, we
1549 might be in business. */
1551 if (!DECL_TEMPLATE_INFO (fn))
1552 /* Its enclosing class is an explicit specialization
1553 of a template class. This is not a candidate. */
1554 continue;
1556 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1557 TREE_TYPE (TREE_TYPE (fn))))
1558 /* The return types differ. */
1559 continue;
1561 /* Adjust the type of DECL in case FN is a static member. */
1562 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1563 if (DECL_STATIC_FUNCTION_P (fn)
1564 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1565 decl_arg_types = TREE_CHAIN (decl_arg_types);
1567 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1568 decl_arg_types))
1569 /* They match! */
1570 candidates = tree_cons (NULL_TREE, fn, candidates);
1574 if (templates && TREE_CHAIN (templates))
1576 /* We have:
1578 [temp.expl.spec]
1580 It is possible for a specialization with a given function
1581 signature to be instantiated from more than one function
1582 template. In such cases, explicit specification of the
1583 template arguments must be used to uniquely identify the
1584 function template specialization being specialized.
1586 Note that here, there's no suggestion that we're supposed to
1587 determine which of the candidate templates is most
1588 specialized. However, we, also have:
1590 [temp.func.order]
1592 Partial ordering of overloaded function template
1593 declarations is used in the following contexts to select
1594 the function template to which a function template
1595 specialization refers:
1597 -- when an explicit specialization refers to a function
1598 template.
1600 So, we do use the partial ordering rules, at least for now.
1601 This extension can only serve to make invalid programs valid,
1602 so it's safe. And, there is strong anecdotal evidence that
1603 the committee intended the partial ordering rules to apply;
1604 the EDG front-end has that behavior, and John Spicer claims
1605 that the committee simply forgot to delete the wording in
1606 [temp.expl.spec]. */
1607 tree tmpl = most_specialized (templates, decl, explicit_targs);
1608 if (tmpl && tmpl != error_mark_node)
1610 targs = get_bindings (tmpl, decl, explicit_targs, /*check_ret=*/true);
1611 templates = tree_cons (targs, tmpl, NULL_TREE);
1615 if (templates == NULL_TREE && candidates == NULL_TREE)
1617 error ("template-id %qD for %q+D does not match any template "
1618 "declaration", template_id, decl);
1619 return error_mark_node;
1621 else if ((templates && TREE_CHAIN (templates))
1622 || (candidates && TREE_CHAIN (candidates))
1623 || (templates && candidates))
1625 error ("ambiguous template specialization %qD for %q+D",
1626 template_id, decl);
1627 chainon (candidates, templates);
1628 print_candidates (candidates);
1629 return error_mark_node;
1632 /* We have one, and exactly one, match. */
1633 if (candidates)
1635 tree fn = TREE_VALUE (candidates);
1636 /* DECL is a re-declaration of a template function. */
1637 if (TREE_CODE (fn) == TEMPLATE_DECL)
1638 return fn;
1639 /* It was a specialization of an ordinary member function in a
1640 template class. */
1641 *targs_out = copy_node (DECL_TI_ARGS (fn));
1642 return DECL_TI_TEMPLATE (fn);
1645 /* It was a specialization of a template. */
1646 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1647 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1649 *targs_out = copy_node (targs);
1650 SET_TMPL_ARGS_LEVEL (*targs_out,
1651 TMPL_ARGS_DEPTH (*targs_out),
1652 TREE_PURPOSE (templates));
1654 else
1655 *targs_out = TREE_PURPOSE (templates);
1656 return TREE_VALUE (templates);
1659 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1660 but with the default argument values filled in from those in the
1661 TMPL_TYPES. */
1663 static tree
1664 copy_default_args_to_explicit_spec_1 (tree spec_types,
1665 tree tmpl_types)
1667 tree new_spec_types;
1669 if (!spec_types)
1670 return NULL_TREE;
1672 if (spec_types == void_list_node)
1673 return void_list_node;
1675 /* Substitute into the rest of the list. */
1676 new_spec_types =
1677 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1678 TREE_CHAIN (tmpl_types));
1680 /* Add the default argument for this parameter. */
1681 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1682 TREE_VALUE (spec_types),
1683 new_spec_types);
1686 /* DECL is an explicit specialization. Replicate default arguments
1687 from the template it specializes. (That way, code like:
1689 template <class T> void f(T = 3);
1690 template <> void f(double);
1691 void g () { f (); }
1693 works, as required.) An alternative approach would be to look up
1694 the correct default arguments at the call-site, but this approach
1695 is consistent with how implicit instantiations are handled. */
1697 static void
1698 copy_default_args_to_explicit_spec (tree decl)
1700 tree tmpl;
1701 tree spec_types;
1702 tree tmpl_types;
1703 tree new_spec_types;
1704 tree old_type;
1705 tree new_type;
1706 tree t;
1707 tree object_type = NULL_TREE;
1708 tree in_charge = NULL_TREE;
1709 tree vtt = NULL_TREE;
1711 /* See if there's anything we need to do. */
1712 tmpl = DECL_TI_TEMPLATE (decl);
1713 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1714 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1715 if (TREE_PURPOSE (t))
1716 break;
1717 if (!t)
1718 return;
1720 old_type = TREE_TYPE (decl);
1721 spec_types = TYPE_ARG_TYPES (old_type);
1723 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1725 /* Remove the this pointer, but remember the object's type for
1726 CV quals. */
1727 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1728 spec_types = TREE_CHAIN (spec_types);
1729 tmpl_types = TREE_CHAIN (tmpl_types);
1731 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1733 /* DECL may contain more parameters than TMPL due to the extra
1734 in-charge parameter in constructors and destructors. */
1735 in_charge = spec_types;
1736 spec_types = TREE_CHAIN (spec_types);
1738 if (DECL_HAS_VTT_PARM_P (decl))
1740 vtt = spec_types;
1741 spec_types = TREE_CHAIN (spec_types);
1745 /* Compute the merged default arguments. */
1746 new_spec_types =
1747 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1749 /* Compute the new FUNCTION_TYPE. */
1750 if (object_type)
1752 if (vtt)
1753 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1754 TREE_VALUE (vtt),
1755 new_spec_types);
1757 if (in_charge)
1758 /* Put the in-charge parameter back. */
1759 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1760 TREE_VALUE (in_charge),
1761 new_spec_types);
1763 new_type = build_method_type_directly (object_type,
1764 TREE_TYPE (old_type),
1765 new_spec_types);
1767 else
1768 new_type = build_function_type (TREE_TYPE (old_type),
1769 new_spec_types);
1770 new_type = cp_build_type_attribute_variant (new_type,
1771 TYPE_ATTRIBUTES (old_type));
1772 new_type = build_exception_variant (new_type,
1773 TYPE_RAISES_EXCEPTIONS (old_type));
1774 TREE_TYPE (decl) = new_type;
1777 /* Check to see if the function just declared, as indicated in
1778 DECLARATOR, and in DECL, is a specialization of a function
1779 template. We may also discover that the declaration is an explicit
1780 instantiation at this point.
1782 Returns DECL, or an equivalent declaration that should be used
1783 instead if all goes well. Issues an error message if something is
1784 amiss. Returns error_mark_node if the error is not easily
1785 recoverable.
1787 FLAGS is a bitmask consisting of the following flags:
1789 2: The function has a definition.
1790 4: The function is a friend.
1792 The TEMPLATE_COUNT is the number of references to qualifying
1793 template classes that appeared in the name of the function. For
1794 example, in
1796 template <class T> struct S { void f(); };
1797 void S<int>::f();
1799 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1800 classes are not counted in the TEMPLATE_COUNT, so that in
1802 template <class T> struct S {};
1803 template <> struct S<int> { void f(); }
1804 template <> void S<int>::f();
1806 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1807 invalid; there should be no template <>.)
1809 If the function is a specialization, it is marked as such via
1810 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1811 is set up correctly, and it is added to the list of specializations
1812 for that template. */
1814 tree
1815 check_explicit_specialization (tree declarator,
1816 tree decl,
1817 int template_count,
1818 int flags)
1820 int have_def = flags & 2;
1821 int is_friend = flags & 4;
1822 int specialization = 0;
1823 int explicit_instantiation = 0;
1824 int member_specialization = 0;
1825 tree ctype = DECL_CLASS_CONTEXT (decl);
1826 tree dname = DECL_NAME (decl);
1827 tmpl_spec_kind tsk;
1829 if (is_friend)
1831 if (!processing_specialization)
1832 tsk = tsk_none;
1833 else
1834 tsk = tsk_excessive_parms;
1836 else
1837 tsk = current_tmpl_spec_kind (template_count);
1839 switch (tsk)
1841 case tsk_none:
1842 if (processing_specialization)
1844 specialization = 1;
1845 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1847 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1849 if (is_friend)
1850 /* This could be something like:
1852 template <class T> void f(T);
1853 class S { friend void f<>(int); } */
1854 specialization = 1;
1855 else
1857 /* This case handles bogus declarations like template <>
1858 template <class T> void f<int>(); */
1860 error ("template-id %qD in declaration of primary template",
1861 declarator);
1862 return decl;
1865 break;
1867 case tsk_invalid_member_spec:
1868 /* The error has already been reported in
1869 check_specialization_scope. */
1870 return error_mark_node;
1872 case tsk_invalid_expl_inst:
1873 error ("template parameter list used in explicit instantiation");
1875 /* Fall through. */
1877 case tsk_expl_inst:
1878 if (have_def)
1879 error ("definition provided for explicit instantiation");
1881 explicit_instantiation = 1;
1882 break;
1884 case tsk_excessive_parms:
1885 case tsk_insufficient_parms:
1886 if (tsk == tsk_excessive_parms)
1887 error ("too many template parameter lists in declaration of %qD",
1888 decl);
1889 else if (template_header_count)
1890 error("too few template parameter lists in declaration of %qD", decl);
1891 else
1892 error("explicit specialization of %qD must be introduced by "
1893 "%<template <>%>", decl);
1895 /* Fall through. */
1896 case tsk_expl_spec:
1897 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1898 if (ctype)
1899 member_specialization = 1;
1900 else
1901 specialization = 1;
1902 break;
1904 case tsk_template:
1905 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1907 /* This case handles bogus declarations like template <>
1908 template <class T> void f<int>(); */
1910 if (uses_template_parms (declarator))
1911 error ("function template partial specialization %qD "
1912 "is not allowed", declarator);
1913 else
1914 error ("template-id %qD in declaration of primary template",
1915 declarator);
1916 return decl;
1919 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1920 /* This is a specialization of a member template, without
1921 specialization the containing class. Something like:
1923 template <class T> struct S {
1924 template <class U> void f (U);
1926 template <> template <class U> void S<int>::f(U) {}
1928 That's a specialization -- but of the entire template. */
1929 specialization = 1;
1930 break;
1932 default:
1933 gcc_unreachable ();
1936 if (specialization || member_specialization)
1938 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1939 for (; t; t = TREE_CHAIN (t))
1940 if (TREE_PURPOSE (t))
1942 pedwarn
1943 ("default argument specified in explicit specialization");
1944 break;
1948 if (specialization || member_specialization || explicit_instantiation)
1950 tree tmpl = NULL_TREE;
1951 tree targs = NULL_TREE;
1953 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1954 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1956 tree fns;
1958 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1959 if (ctype)
1960 fns = dname;
1961 else
1963 /* If there is no class context, the explicit instantiation
1964 must be at namespace scope. */
1965 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1967 /* Find the namespace binding, using the declaration
1968 context. */
1969 fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
1970 if (!fns || !is_overloaded_fn (fns))
1972 error ("%qD is not a template function", dname);
1973 fns = error_mark_node;
1977 declarator = lookup_template_function (fns, NULL_TREE);
1980 if (declarator == error_mark_node)
1981 return error_mark_node;
1983 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1985 if (!explicit_instantiation)
1986 /* A specialization in class scope. This is invalid,
1987 but the error will already have been flagged by
1988 check_specialization_scope. */
1989 return error_mark_node;
1990 else
1992 /* It's not valid to write an explicit instantiation in
1993 class scope, e.g.:
1995 class C { template void f(); }
1997 This case is caught by the parser. However, on
1998 something like:
2000 template class C { void f(); };
2002 (which is invalid) we can get here. The error will be
2003 issued later. */
2007 return decl;
2009 else if (ctype != NULL_TREE
2010 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2011 IDENTIFIER_NODE))
2013 /* Find the list of functions in ctype that have the same
2014 name as the declared function. */
2015 tree name = TREE_OPERAND (declarator, 0);
2016 tree fns = NULL_TREE;
2017 int idx;
2019 if (constructor_name_p (name, ctype))
2021 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2023 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2024 : !CLASSTYPE_DESTRUCTORS (ctype))
2026 /* From [temp.expl.spec]:
2028 If such an explicit specialization for the member
2029 of a class template names an implicitly-declared
2030 special member function (clause _special_), the
2031 program is ill-formed.
2033 Similar language is found in [temp.explicit]. */
2034 error ("specialization of implicitly-declared special member function");
2035 return error_mark_node;
2038 name = is_constructor ? ctor_identifier : dtor_identifier;
2041 if (!DECL_CONV_FN_P (decl))
2043 idx = lookup_fnfields_1 (ctype, name);
2044 if (idx >= 0)
2045 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2047 else
2049 VEC(tree,gc) *methods;
2050 tree ovl;
2052 /* For a type-conversion operator, we cannot do a
2053 name-based lookup. We might be looking for `operator
2054 int' which will be a specialization of `operator T'.
2055 So, we find *all* the conversion operators, and then
2056 select from them. */
2057 fns = NULL_TREE;
2059 methods = CLASSTYPE_METHOD_VEC (ctype);
2060 if (methods)
2061 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2062 VEC_iterate (tree, methods, idx, ovl);
2063 ++idx)
2065 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2066 /* There are no more conversion functions. */
2067 break;
2069 /* Glue all these conversion functions together
2070 with those we already have. */
2071 for (; ovl; ovl = OVL_NEXT (ovl))
2072 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2076 if (fns == NULL_TREE)
2078 error ("no member function %qD declared in %qT", name, ctype);
2079 return error_mark_node;
2081 else
2082 TREE_OPERAND (declarator, 0) = fns;
2085 /* Figure out what exactly is being specialized at this point.
2086 Note that for an explicit instantiation, even one for a
2087 member function, we cannot tell apriori whether the
2088 instantiation is for a member template, or just a member
2089 function of a template class. Even if a member template is
2090 being instantiated, the member template arguments may be
2091 elided if they can be deduced from the rest of the
2092 declaration. */
2093 tmpl = determine_specialization (declarator, decl,
2094 &targs,
2095 member_specialization,
2096 template_count,
2097 tsk);
2099 if (!tmpl || tmpl == error_mark_node)
2100 /* We couldn't figure out what this declaration was
2101 specializing. */
2102 return error_mark_node;
2103 else
2105 tree gen_tmpl = most_general_template (tmpl);
2107 if (explicit_instantiation)
2109 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2110 is done by do_decl_instantiation later. */
2112 int arg_depth = TMPL_ARGS_DEPTH (targs);
2113 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2115 if (arg_depth > parm_depth)
2117 /* If TMPL is not the most general template (for
2118 example, if TMPL is a friend template that is
2119 injected into namespace scope), then there will
2120 be too many levels of TARGS. Remove some of them
2121 here. */
2122 int i;
2123 tree new_targs;
2125 new_targs = make_tree_vec (parm_depth);
2126 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2127 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2128 = TREE_VEC_ELT (targs, i);
2129 targs = new_targs;
2132 return instantiate_template (tmpl, targs, tf_error);
2135 /* If we thought that the DECL was a member function, but it
2136 turns out to be specializing a static member function,
2137 make DECL a static member function as well. */
2138 if (DECL_STATIC_FUNCTION_P (tmpl)
2139 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2140 revert_static_member_fn (decl);
2142 /* If this is a specialization of a member template of a
2143 template class, we want to return the TEMPLATE_DECL, not
2144 the specialization of it. */
2145 if (tsk == tsk_template)
2147 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2148 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2149 if (have_def)
2151 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2152 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2153 = DECL_SOURCE_LOCATION (decl);
2154 /* We want to use the argument list specified in the
2155 definition, not in the original declaration. */
2156 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2157 = DECL_ARGUMENTS (decl);
2159 return tmpl;
2162 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2163 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2165 /* Inherit default function arguments from the template
2166 DECL is specializing. */
2167 copy_default_args_to_explicit_spec (decl);
2169 /* This specialization has the same protection as the
2170 template it specializes. */
2171 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2172 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2173 /* The specialization has the same visibility as the
2174 template it specializes. */
2175 if (DECL_VISIBILITY_SPECIFIED (gen_tmpl))
2177 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2178 DECL_VISIBILITY (decl) = DECL_VISIBILITY (gen_tmpl);
2180 /* If DECL is a friend declaration, declared using an
2181 unqualified name, the namespace associated with DECL may
2182 have been set incorrectly. For example, in:
2184 template <typename T> void f(T);
2185 namespace N {
2186 struct S { friend void f<int>(int); }
2189 we will have set the DECL_CONTEXT for the friend
2190 declaration to N, rather than to the global namespace. */
2191 if (DECL_NAMESPACE_SCOPE_P (decl))
2192 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2194 if (is_friend && !have_def)
2195 /* This is not really a declaration of a specialization.
2196 It's just the name of an instantiation. But, it's not
2197 a request for an instantiation, either. */
2198 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2199 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2200 /* This is indeed a specialization. In case of constructors
2201 and destructors, we need in-charge and not-in-charge
2202 versions in V3 ABI. */
2203 clone_function_decl (decl, /*update_method_vec_p=*/0);
2205 /* Register this specialization so that we can find it
2206 again. */
2207 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2211 return decl;
2214 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2215 parameters. These are represented in the same format used for
2216 DECL_TEMPLATE_PARMS. */
2219 comp_template_parms (tree parms1, tree parms2)
2221 tree p1;
2222 tree p2;
2224 if (parms1 == parms2)
2225 return 1;
2227 for (p1 = parms1, p2 = parms2;
2228 p1 != NULL_TREE && p2 != NULL_TREE;
2229 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2231 tree t1 = TREE_VALUE (p1);
2232 tree t2 = TREE_VALUE (p2);
2233 int i;
2235 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2236 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2238 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2239 return 0;
2241 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2243 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2244 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2246 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2247 return 0;
2249 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2250 continue;
2251 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2252 return 0;
2256 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2257 /* One set of parameters has more parameters lists than the
2258 other. */
2259 return 0;
2261 return 1;
2264 /* Complain if DECL shadows a template parameter.
2266 [temp.local]: A template-parameter shall not be redeclared within its
2267 scope (including nested scopes). */
2269 void
2270 check_template_shadow (tree decl)
2272 tree olddecl;
2274 /* If we're not in a template, we can't possibly shadow a template
2275 parameter. */
2276 if (!current_template_parms)
2277 return;
2279 /* Figure out what we're shadowing. */
2280 if (TREE_CODE (decl) == OVERLOAD)
2281 decl = OVL_CURRENT (decl);
2282 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2284 /* If there's no previous binding for this name, we're not shadowing
2285 anything, let alone a template parameter. */
2286 if (!olddecl)
2287 return;
2289 /* If we're not shadowing a template parameter, we're done. Note
2290 that OLDDECL might be an OVERLOAD (or perhaps even an
2291 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2292 node. */
2293 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2294 return;
2296 /* We check for decl != olddecl to avoid bogus errors for using a
2297 name inside a class. We check TPFI to avoid duplicate errors for
2298 inline member templates. */
2299 if (decl == olddecl
2300 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2301 return;
2303 error ("declaration of %q+#D", decl);
2304 error (" shadows template parm %q+#D", olddecl);
2307 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2308 ORIG_LEVEL, DECL, and TYPE. */
2310 static tree
2311 build_template_parm_index (int index,
2312 int level,
2313 int orig_level,
2314 tree decl,
2315 tree type)
2317 tree t = make_node (TEMPLATE_PARM_INDEX);
2318 TEMPLATE_PARM_IDX (t) = index;
2319 TEMPLATE_PARM_LEVEL (t) = level;
2320 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2321 TEMPLATE_PARM_DECL (t) = decl;
2322 TREE_TYPE (t) = type;
2323 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2324 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2325 TREE_READONLY (t) = TREE_READONLY (decl);
2327 return t;
2330 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2331 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2332 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2333 new one is created. */
2335 static tree
2336 reduce_template_parm_level (tree index, tree type, int levels)
2338 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2339 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2340 != TEMPLATE_PARM_LEVEL (index) - levels))
2342 tree orig_decl = TEMPLATE_PARM_DECL (index);
2343 tree decl, t;
2345 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2346 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2347 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2348 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2349 DECL_ARTIFICIAL (decl) = 1;
2350 SET_DECL_TEMPLATE_PARM_P (decl);
2352 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2353 TEMPLATE_PARM_LEVEL (index) - levels,
2354 TEMPLATE_PARM_ORIG_LEVEL (index),
2355 decl, type);
2356 TEMPLATE_PARM_DESCENDANTS (index) = t;
2358 /* Template template parameters need this. */
2359 if (TREE_CODE (decl) != CONST_DECL)
2360 DECL_TEMPLATE_PARMS (decl)
2361 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2364 return TEMPLATE_PARM_DESCENDANTS (index);
2367 /* Process information from new template parameter NEXT and append it to the
2368 LIST being built. This new parameter is a non-type parameter iff
2369 IS_NON_TYPE is true. */
2371 tree
2372 process_template_parm (tree list, tree next, bool is_non_type)
2374 tree parm;
2375 tree decl = 0;
2376 tree defval;
2377 int idx;
2379 parm = next;
2380 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2381 defval = TREE_PURPOSE (parm);
2383 if (list)
2385 tree p = TREE_VALUE (tree_last (list));
2387 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2388 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2389 else
2390 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2391 ++idx;
2393 else
2394 idx = 0;
2396 if (is_non_type)
2398 parm = TREE_VALUE (parm);
2400 SET_DECL_TEMPLATE_PARM_P (parm);
2402 /* [temp.param]
2404 The top-level cv-qualifiers on the template-parameter are
2405 ignored when determining its type. */
2406 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2408 /* A template parameter is not modifiable. */
2409 TREE_CONSTANT (parm) = 1;
2410 TREE_INVARIANT (parm) = 1;
2411 TREE_READONLY (parm) = 1;
2412 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2413 TREE_TYPE (parm) = void_type_node;
2414 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2415 TREE_CONSTANT (decl) = 1;
2416 TREE_INVARIANT (decl) = 1;
2417 TREE_READONLY (decl) = 1;
2418 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2419 = build_template_parm_index (idx, processing_template_decl,
2420 processing_template_decl,
2421 decl, TREE_TYPE (parm));
2423 else
2425 tree t;
2426 parm = TREE_VALUE (TREE_VALUE (parm));
2428 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2430 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2431 /* This is for distinguishing between real templates and template
2432 template parameters */
2433 TREE_TYPE (parm) = t;
2434 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2435 decl = parm;
2437 else
2439 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2440 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2441 decl = build_decl (TYPE_DECL, parm, t);
2444 TYPE_NAME (t) = decl;
2445 TYPE_STUB_DECL (t) = decl;
2446 parm = decl;
2447 TEMPLATE_TYPE_PARM_INDEX (t)
2448 = build_template_parm_index (idx, processing_template_decl,
2449 processing_template_decl,
2450 decl, TREE_TYPE (parm));
2452 DECL_ARTIFICIAL (decl) = 1;
2453 SET_DECL_TEMPLATE_PARM_P (decl);
2454 pushdecl (decl);
2455 parm = build_tree_list (defval, parm);
2456 return chainon (list, parm);
2459 /* The end of a template parameter list has been reached. Process the
2460 tree list into a parameter vector, converting each parameter into a more
2461 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2462 as PARM_DECLs. */
2464 tree
2465 end_template_parm_list (tree parms)
2467 int nparms;
2468 tree parm, next;
2469 tree saved_parmlist = make_tree_vec (list_length (parms));
2471 current_template_parms
2472 = tree_cons (size_int (processing_template_decl),
2473 saved_parmlist, current_template_parms);
2475 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2477 next = TREE_CHAIN (parm);
2478 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2479 TREE_CHAIN (parm) = NULL_TREE;
2482 --processing_template_parmlist;
2484 return saved_parmlist;
2487 /* end_template_decl is called after a template declaration is seen. */
2489 void
2490 end_template_decl (void)
2492 reset_specialization ();
2494 if (! processing_template_decl)
2495 return;
2497 /* This matches the pushlevel in begin_template_parm_list. */
2498 finish_scope ();
2500 --processing_template_decl;
2501 current_template_parms = TREE_CHAIN (current_template_parms);
2504 /* Given a template argument vector containing the template PARMS.
2505 The innermost PARMS are given first. */
2507 tree
2508 current_template_args (void)
2510 tree header;
2511 tree args = NULL_TREE;
2512 int length = TMPL_PARMS_DEPTH (current_template_parms);
2513 int l = length;
2515 /* If there is only one level of template parameters, we do not
2516 create a TREE_VEC of TREE_VECs. Instead, we return a single
2517 TREE_VEC containing the arguments. */
2518 if (length > 1)
2519 args = make_tree_vec (length);
2521 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2523 tree a = copy_node (TREE_VALUE (header));
2524 int i;
2526 TREE_TYPE (a) = NULL_TREE;
2527 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2529 tree t = TREE_VEC_ELT (a, i);
2531 /* T will be a list if we are called from within a
2532 begin/end_template_parm_list pair, but a vector directly
2533 if within a begin/end_member_template_processing pair. */
2534 if (TREE_CODE (t) == TREE_LIST)
2536 t = TREE_VALUE (t);
2538 if (TREE_CODE (t) == TYPE_DECL
2539 || TREE_CODE (t) == TEMPLATE_DECL)
2540 t = TREE_TYPE (t);
2541 else
2542 t = DECL_INITIAL (t);
2543 TREE_VEC_ELT (a, i) = t;
2547 if (length > 1)
2548 TREE_VEC_ELT (args, --l) = a;
2549 else
2550 args = a;
2553 return args;
2556 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2557 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2558 a member template. Used by push_template_decl below. */
2560 static tree
2561 build_template_decl (tree decl, tree parms, bool member_template_p)
2563 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2564 DECL_TEMPLATE_PARMS (tmpl) = parms;
2565 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2566 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2567 if (DECL_LANG_SPECIFIC (decl))
2569 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2570 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2571 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2572 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2573 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2574 if (DECL_OVERLOADED_OPERATOR_P (decl))
2575 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2576 DECL_OVERLOADED_OPERATOR_P (decl));
2579 return tmpl;
2582 struct template_parm_data
2584 /* The level of the template parameters we are currently
2585 processing. */
2586 int level;
2588 /* The index of the specialization argument we are currently
2589 processing. */
2590 int current_arg;
2592 /* An array whose size is the number of template parameters. The
2593 elements are nonzero if the parameter has been used in any one
2594 of the arguments processed so far. */
2595 int* parms;
2597 /* An array whose size is the number of template arguments. The
2598 elements are nonzero if the argument makes use of template
2599 parameters of this level. */
2600 int* arg_uses_template_parms;
2603 /* Subroutine of push_template_decl used to see if each template
2604 parameter in a partial specialization is used in the explicit
2605 argument list. If T is of the LEVEL given in DATA (which is
2606 treated as a template_parm_data*), then DATA->PARMS is marked
2607 appropriately. */
2609 static int
2610 mark_template_parm (tree t, void* data)
2612 int level;
2613 int idx;
2614 struct template_parm_data* tpd = (struct template_parm_data*) data;
2616 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2618 level = TEMPLATE_PARM_LEVEL (t);
2619 idx = TEMPLATE_PARM_IDX (t);
2621 else
2623 level = TEMPLATE_TYPE_LEVEL (t);
2624 idx = TEMPLATE_TYPE_IDX (t);
2627 if (level == tpd->level)
2629 tpd->parms[idx] = 1;
2630 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2633 /* Return zero so that for_each_template_parm will continue the
2634 traversal of the tree; we want to mark *every* template parm. */
2635 return 0;
2638 /* Process the partial specialization DECL. */
2640 static tree
2641 process_partial_specialization (tree decl)
2643 tree type = TREE_TYPE (decl);
2644 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2645 tree specargs = CLASSTYPE_TI_ARGS (type);
2646 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2647 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2648 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2649 int nargs = TREE_VEC_LENGTH (inner_args);
2650 int ntparms = TREE_VEC_LENGTH (inner_parms);
2651 int i;
2652 int did_error_intro = 0;
2653 struct template_parm_data tpd;
2654 struct template_parm_data tpd2;
2656 /* We check that each of the template parameters given in the
2657 partial specialization is used in the argument list to the
2658 specialization. For example:
2660 template <class T> struct S;
2661 template <class T> struct S<T*>;
2663 The second declaration is OK because `T*' uses the template
2664 parameter T, whereas
2666 template <class T> struct S<int>;
2668 is no good. Even trickier is:
2670 template <class T>
2671 struct S1
2673 template <class U>
2674 struct S2;
2675 template <class U>
2676 struct S2<T>;
2679 The S2<T> declaration is actually invalid; it is a
2680 full-specialization. Of course,
2682 template <class U>
2683 struct S2<T (*)(U)>;
2685 or some such would have been OK. */
2686 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2687 tpd.parms = alloca (sizeof (int) * ntparms);
2688 memset (tpd.parms, 0, sizeof (int) * ntparms);
2690 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2691 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2692 for (i = 0; i < nargs; ++i)
2694 tpd.current_arg = i;
2695 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2696 &mark_template_parm,
2697 &tpd,
2698 NULL);
2700 for (i = 0; i < ntparms; ++i)
2701 if (tpd.parms[i] == 0)
2703 /* One of the template parms was not used in the
2704 specialization. */
2705 if (!did_error_intro)
2707 error ("template parameters not used in partial specialization:");
2708 did_error_intro = 1;
2711 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2714 /* [temp.class.spec]
2716 The argument list of the specialization shall not be identical to
2717 the implicit argument list of the primary template. */
2718 if (comp_template_args
2719 (inner_args,
2720 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2721 (maintmpl)))))
2722 error ("partial specialization %qT does not specialize any template arguments", type);
2724 /* [temp.class.spec]
2726 A partially specialized non-type argument expression shall not
2727 involve template parameters of the partial specialization except
2728 when the argument expression is a simple identifier.
2730 The type of a template parameter corresponding to a specialized
2731 non-type argument shall not be dependent on a parameter of the
2732 specialization. */
2733 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2734 tpd2.parms = 0;
2735 for (i = 0; i < nargs; ++i)
2737 tree arg = TREE_VEC_ELT (inner_args, i);
2738 if (/* These first two lines are the `non-type' bit. */
2739 !TYPE_P (arg)
2740 && TREE_CODE (arg) != TEMPLATE_DECL
2741 /* This next line is the `argument expression is not just a
2742 simple identifier' condition and also the `specialized
2743 non-type argument' bit. */
2744 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2746 if (tpd.arg_uses_template_parms[i])
2747 error ("template argument %qE involves template parameter(s)", arg);
2748 else
2750 /* Look at the corresponding template parameter,
2751 marking which template parameters its type depends
2752 upon. */
2753 tree type =
2754 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2755 i)));
2757 if (!tpd2.parms)
2759 /* We haven't yet initialized TPD2. Do so now. */
2760 tpd2.arg_uses_template_parms
2761 = alloca (sizeof (int) * nargs);
2762 /* The number of parameters here is the number in the
2763 main template, which, as checked in the assertion
2764 above, is NARGS. */
2765 tpd2.parms = alloca (sizeof (int) * nargs);
2766 tpd2.level =
2767 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2770 /* Mark the template parameters. But this time, we're
2771 looking for the template parameters of the main
2772 template, not in the specialization. */
2773 tpd2.current_arg = i;
2774 tpd2.arg_uses_template_parms[i] = 0;
2775 memset (tpd2.parms, 0, sizeof (int) * nargs);
2776 for_each_template_parm (type,
2777 &mark_template_parm,
2778 &tpd2,
2779 NULL);
2781 if (tpd2.arg_uses_template_parms [i])
2783 /* The type depended on some template parameters.
2784 If they are fully specialized in the
2785 specialization, that's OK. */
2786 int j;
2787 for (j = 0; j < nargs; ++j)
2788 if (tpd2.parms[j] != 0
2789 && tpd.arg_uses_template_parms [j])
2791 error ("type %qT of template argument %qE depends "
2792 "on template parameter(s)",
2793 type,
2794 arg);
2795 break;
2802 if (retrieve_specialization (maintmpl, specargs,
2803 /*class_specializations_p=*/true))
2804 /* We've already got this specialization. */
2805 return decl;
2807 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2808 = tree_cons (specargs, inner_parms,
2809 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2810 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2811 return decl;
2814 /* Check that a template declaration's use of default arguments is not
2815 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2816 nonzero if DECL is the thing declared by a primary template.
2817 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2819 static void
2820 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2822 const char *msg;
2823 int last_level_to_check;
2824 tree parm_level;
2826 /* [temp.param]
2828 A default template-argument shall not be specified in a
2829 function template declaration or a function template definition, nor
2830 in the template-parameter-list of the definition of a member of a
2831 class template. */
2833 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2834 /* You can't have a function template declaration in a local
2835 scope, nor you can you define a member of a class template in a
2836 local scope. */
2837 return;
2839 if (current_class_type
2840 && !TYPE_BEING_DEFINED (current_class_type)
2841 && DECL_LANG_SPECIFIC (decl)
2842 /* If this is either a friend defined in the scope of the class
2843 or a member function. */
2844 && (DECL_FUNCTION_MEMBER_P (decl)
2845 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2846 : DECL_FRIEND_CONTEXT (decl)
2847 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2848 : false)
2849 /* And, if it was a member function, it really was defined in
2850 the scope of the class. */
2851 && (!DECL_FUNCTION_MEMBER_P (decl)
2852 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2853 /* We already checked these parameters when the template was
2854 declared, so there's no need to do it again now. This function
2855 was defined in class scope, but we're processing it's body now
2856 that the class is complete. */
2857 return;
2859 /* [temp.param]
2861 If a template-parameter has a default template-argument, all
2862 subsequent template-parameters shall have a default
2863 template-argument supplied. */
2864 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2866 tree inner_parms = TREE_VALUE (parm_level);
2867 int ntparms = TREE_VEC_LENGTH (inner_parms);
2868 int seen_def_arg_p = 0;
2869 int i;
2871 for (i = 0; i < ntparms; ++i)
2873 tree parm = TREE_VEC_ELT (inner_parms, i);
2875 if (TREE_PURPOSE (parm))
2876 seen_def_arg_p = 1;
2877 else if (seen_def_arg_p)
2879 error ("no default argument for %qD", TREE_VALUE (parm));
2880 /* For better subsequent error-recovery, we indicate that
2881 there should have been a default argument. */
2882 TREE_PURPOSE (parm) = error_mark_node;
2887 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2888 /* For an ordinary class template, default template arguments are
2889 allowed at the innermost level, e.g.:
2890 template <class T = int>
2891 struct S {};
2892 but, in a partial specialization, they're not allowed even
2893 there, as we have in [temp.class.spec]:
2895 The template parameter list of a specialization shall not
2896 contain default template argument values.
2898 So, for a partial specialization, or for a function template,
2899 we look at all of them. */
2901 else
2902 /* But, for a primary class template that is not a partial
2903 specialization we look at all template parameters except the
2904 innermost ones. */
2905 parms = TREE_CHAIN (parms);
2907 /* Figure out what error message to issue. */
2908 if (TREE_CODE (decl) == FUNCTION_DECL)
2909 msg = "default template arguments may not be used in function templates";
2910 else if (is_partial)
2911 msg = "default template arguments may not be used in partial specializations";
2912 else
2913 msg = "default argument for template parameter for class enclosing %qD";
2915 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2916 /* If we're inside a class definition, there's no need to
2917 examine the parameters to the class itself. On the one
2918 hand, they will be checked when the class is defined, and,
2919 on the other, default arguments are valid in things like:
2920 template <class T = double>
2921 struct S { template <class U> void f(U); };
2922 Here the default argument for `S' has no bearing on the
2923 declaration of `f'. */
2924 last_level_to_check = template_class_depth (current_class_type) + 1;
2925 else
2926 /* Check everything. */
2927 last_level_to_check = 0;
2929 for (parm_level = parms;
2930 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2931 parm_level = TREE_CHAIN (parm_level))
2933 tree inner_parms = TREE_VALUE (parm_level);
2934 int i;
2935 int ntparms;
2937 ntparms = TREE_VEC_LENGTH (inner_parms);
2938 for (i = 0; i < ntparms; ++i)
2940 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2942 if (msg)
2944 error (msg, decl);
2945 msg = 0;
2948 /* Clear out the default argument so that we are not
2949 confused later. */
2950 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2954 /* At this point, if we're still interested in issuing messages,
2955 they must apply to classes surrounding the object declared. */
2956 if (msg)
2957 msg = "default argument for template parameter for class enclosing %qD";
2961 /* Worker for push_template_decl_real, called via
2962 for_each_template_parm. DATA is really an int, indicating the
2963 level of the parameters we are interested in. If T is a template
2964 parameter of that level, return nonzero. */
2966 static int
2967 template_parm_this_level_p (tree t, void* data)
2969 int this_level = *(int *)data;
2970 int level;
2972 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2973 level = TEMPLATE_PARM_LEVEL (t);
2974 else
2975 level = TEMPLATE_TYPE_LEVEL (t);
2976 return level == this_level;
2979 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2980 parameters given by current_template_args, or reuses a
2981 previously existing one, if appropriate. Returns the DECL, or an
2982 equivalent one, if it is replaced via a call to duplicate_decls.
2984 If IS_FRIEND is true, DECL is a friend declaration. */
2986 tree
2987 push_template_decl_real (tree decl, bool is_friend)
2989 tree tmpl;
2990 tree args;
2991 tree info;
2992 tree ctx;
2993 int primary;
2994 int is_partial;
2995 int new_template_p = 0;
2996 /* True if the template is a member template, in the sense of
2997 [temp.mem]. */
2998 bool member_template_p = false;
3000 if (decl == error_mark_node)
3001 return decl;
3003 /* See if this is a partial specialization. */
3004 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3005 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3006 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3008 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3009 is_friend = true;
3011 if (is_friend)
3012 /* For a friend, we want the context of the friend function, not
3013 the type of which it is a friend. */
3014 ctx = DECL_CONTEXT (decl);
3015 else if (CP_DECL_CONTEXT (decl)
3016 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3017 /* In the case of a virtual function, we want the class in which
3018 it is defined. */
3019 ctx = CP_DECL_CONTEXT (decl);
3020 else
3021 /* Otherwise, if we're currently defining some class, the DECL
3022 is assumed to be a member of the class. */
3023 ctx = current_scope ();
3025 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3026 ctx = NULL_TREE;
3028 if (!DECL_CONTEXT (decl))
3029 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3031 /* See if this is a primary template. */
3032 if (is_friend && ctx)
3033 /* A friend template that specifies a class context, i.e.
3034 template <typename T> friend void A<T>::f();
3035 is not primary. */
3036 primary = 0;
3037 else
3038 primary = template_parm_scope_p ();
3040 if (primary)
3042 if (DECL_CLASS_SCOPE_P (decl))
3043 member_template_p = true;
3044 if (TREE_CODE (decl) == TYPE_DECL
3045 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3046 error ("template class without a name");
3047 else if (TREE_CODE (decl) == FUNCTION_DECL)
3049 if (DECL_DESTRUCTOR_P (decl))
3051 /* [temp.mem]
3053 A destructor shall not be a member template. */
3054 error ("destructor %qD declared as member template", decl);
3055 return error_mark_node;
3057 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3058 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3059 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3060 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3061 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3062 == void_list_node)))
3064 /* [basic.stc.dynamic.allocation]
3066 An allocation function can be a function
3067 template. ... Template allocation functions shall
3068 have two or more parameters. */
3069 error ("invalid template declaration of %qD", decl);
3070 return error_mark_node;
3073 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3074 && CLASS_TYPE_P (TREE_TYPE (decl)))
3075 /* OK */;
3076 else
3078 error ("template declaration of %q#D", decl);
3079 return error_mark_node;
3083 /* Check to see that the rules regarding the use of default
3084 arguments are not being violated. */
3085 check_default_tmpl_args (decl, current_template_parms,
3086 primary, is_partial);
3088 if (is_partial)
3089 return process_partial_specialization (decl);
3091 args = current_template_args ();
3093 if (!ctx
3094 || TREE_CODE (ctx) == FUNCTION_DECL
3095 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3096 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3098 if (DECL_LANG_SPECIFIC (decl)
3099 && DECL_TEMPLATE_INFO (decl)
3100 && DECL_TI_TEMPLATE (decl))
3101 tmpl = DECL_TI_TEMPLATE (decl);
3102 /* If DECL is a TYPE_DECL for a class-template, then there won't
3103 be DECL_LANG_SPECIFIC. The information equivalent to
3104 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3105 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3106 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3107 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3109 /* Since a template declaration already existed for this
3110 class-type, we must be redeclaring it here. Make sure
3111 that the redeclaration is valid. */
3112 redeclare_class_template (TREE_TYPE (decl),
3113 current_template_parms);
3114 /* We don't need to create a new TEMPLATE_DECL; just use the
3115 one we already had. */
3116 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3118 else
3120 tmpl = build_template_decl (decl, current_template_parms,
3121 member_template_p);
3122 new_template_p = 1;
3124 if (DECL_LANG_SPECIFIC (decl)
3125 && DECL_TEMPLATE_SPECIALIZATION (decl))
3127 /* A specialization of a member template of a template
3128 class. */
3129 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3130 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3131 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3135 else
3137 tree a, t, current, parms;
3138 int i;
3140 if (TREE_CODE (decl) == TYPE_DECL)
3142 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3143 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3144 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3145 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3146 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3147 else
3149 error ("%qD does not declare a template type", decl);
3150 return decl;
3153 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3155 error ("template definition of non-template %q#D", decl);
3156 return decl;
3158 else
3159 tmpl = DECL_TI_TEMPLATE (decl);
3161 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3162 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3163 && DECL_TEMPLATE_SPECIALIZATION (decl)
3164 && DECL_MEMBER_TEMPLATE_P (tmpl))
3166 tree new_tmpl;
3168 /* The declaration is a specialization of a member
3169 template, declared outside the class. Therefore, the
3170 innermost template arguments will be NULL, so we
3171 replace them with the arguments determined by the
3172 earlier call to check_explicit_specialization. */
3173 args = DECL_TI_ARGS (decl);
3175 new_tmpl
3176 = build_template_decl (decl, current_template_parms,
3177 member_template_p);
3178 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3179 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3180 DECL_TI_TEMPLATE (decl) = new_tmpl;
3181 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3182 DECL_TEMPLATE_INFO (new_tmpl)
3183 = tree_cons (tmpl, args, NULL_TREE);
3185 register_specialization (new_tmpl,
3186 most_general_template (tmpl),
3187 args,
3188 is_friend);
3189 return decl;
3192 /* Make sure the template headers we got make sense. */
3194 parms = DECL_TEMPLATE_PARMS (tmpl);
3195 i = TMPL_PARMS_DEPTH (parms);
3196 if (TMPL_ARGS_DEPTH (args) != i)
3198 error ("expected %d levels of template parms for %q#D, got %d",
3199 i, decl, TMPL_ARGS_DEPTH (args));
3201 else
3202 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3204 a = TMPL_ARGS_LEVEL (args, i);
3205 t = INNERMOST_TEMPLATE_PARMS (parms);
3207 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3209 if (current == decl)
3210 error ("got %d template parameters for %q#D",
3211 TREE_VEC_LENGTH (a), decl);
3212 else
3213 error ("got %d template parameters for %q#T",
3214 TREE_VEC_LENGTH (a), current);
3215 error (" but %d required", TREE_VEC_LENGTH (t));
3216 return error_mark_node;
3219 /* Perhaps we should also check that the parms are used in the
3220 appropriate qualifying scopes in the declarator? */
3222 if (current == decl)
3223 current = ctx;
3224 else
3225 current = TYPE_CONTEXT (current);
3229 DECL_TEMPLATE_RESULT (tmpl) = decl;
3230 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3232 /* Push template declarations for global functions and types. Note
3233 that we do not try to push a global template friend declared in a
3234 template class; such a thing may well depend on the template
3235 parameters of the class. */
3236 if (new_template_p && !ctx
3237 && !(is_friend && template_class_depth (current_class_type) > 0))
3239 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3240 if (tmpl == error_mark_node)
3241 return error_mark_node;
3243 /* Hide template friend classes that haven't been declared yet. */
3244 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3246 DECL_ANTICIPATED (tmpl) = 1;
3247 DECL_FRIEND_P (tmpl) = 1;
3251 if (primary)
3253 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3254 if (DECL_CONV_FN_P (tmpl))
3256 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3258 /* It is a conversion operator. See if the type converted to
3259 depends on innermost template operands. */
3261 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3262 depth))
3263 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3267 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3268 back to its most general template. If TMPL is a specialization,
3269 ARGS may only have the innermost set of arguments. Add the missing
3270 argument levels if necessary. */
3271 if (DECL_TEMPLATE_INFO (tmpl))
3272 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3274 info = tree_cons (tmpl, args, NULL_TREE);
3276 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3278 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3279 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3280 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3281 /* Don't change the name if we've already set it up. */
3282 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3283 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3285 else if (DECL_LANG_SPECIFIC (decl))
3286 DECL_TEMPLATE_INFO (decl) = info;
3288 return DECL_TEMPLATE_RESULT (tmpl);
3291 tree
3292 push_template_decl (tree decl)
3294 return push_template_decl_real (decl, false);
3297 /* Called when a class template TYPE is redeclared with the indicated
3298 template PARMS, e.g.:
3300 template <class T> struct S;
3301 template <class T> struct S {}; */
3303 void
3304 redeclare_class_template (tree type, tree parms)
3306 tree tmpl;
3307 tree tmpl_parms;
3308 int i;
3310 if (!TYPE_TEMPLATE_INFO (type))
3312 error ("%qT is not a template type", type);
3313 return;
3316 tmpl = TYPE_TI_TEMPLATE (type);
3317 if (!PRIMARY_TEMPLATE_P (tmpl))
3318 /* The type is nested in some template class. Nothing to worry
3319 about here; there are no new template parameters for the nested
3320 type. */
3321 return;
3323 if (!parms)
3325 error ("template specifiers not specified in declaration of %qD",
3326 tmpl);
3327 return;
3330 parms = INNERMOST_TEMPLATE_PARMS (parms);
3331 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3333 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3335 error ("previous declaration %q+D", tmpl);
3336 error ("used %d template parameter(s) instead of %d",
3337 TREE_VEC_LENGTH (tmpl_parms),
3338 TREE_VEC_LENGTH (parms));
3339 return;
3342 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3344 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3345 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3346 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3347 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3349 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3350 TEMPLATE_DECL. */
3351 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3352 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3353 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3355 error ("template parameter %q+#D", tmpl_parm);
3356 error ("redeclared here as %q#D", parm);
3357 return;
3360 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3362 /* We have in [temp.param]:
3364 A template-parameter may not be given default arguments
3365 by two different declarations in the same scope. */
3366 error ("redefinition of default argument for %q#D", parm);
3367 error ("%J original definition appeared here", tmpl_parm);
3368 return;
3371 if (parm_default != NULL_TREE)
3372 /* Update the previous template parameters (which are the ones
3373 that will really count) with the new default value. */
3374 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3375 else if (tmpl_default != NULL_TREE)
3376 /* Update the new parameters, too; they'll be used as the
3377 parameters for any members. */
3378 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3382 /* Simplify EXPR if it is a non-dependent expression. Returns the
3383 (possibly simplified) expression. */
3385 tree
3386 fold_non_dependent_expr (tree expr)
3388 if (expr == NULL_TREE)
3389 return NULL_TREE;
3391 /* If we're in a template, but EXPR isn't value dependent, simplify
3392 it. We're supposed to treat:
3394 template <typename T> void f(T[1 + 1]);
3395 template <typename T> void f(T[2]);
3397 as two declarations of the same function, for example. */
3398 if (processing_template_decl
3399 && !type_dependent_expression_p (expr)
3400 && !value_dependent_expression_p (expr))
3402 HOST_WIDE_INT saved_processing_template_decl;
3404 saved_processing_template_decl = processing_template_decl;
3405 processing_template_decl = 0;
3406 expr = tsubst_copy_and_build (expr,
3407 /*args=*/NULL_TREE,
3408 tf_error,
3409 /*in_decl=*/NULL_TREE,
3410 /*function_p=*/false);
3411 processing_template_decl = saved_processing_template_decl;
3413 return expr;
3416 /* EXPR is an expression which is used in a constant-expression context.
3417 For instance, it could be a VAR_DECL with a constant initializer.
3418 Extract the innest constant expression.
3420 This is basically a more powerful version of
3421 integral_constant_value, which can be used also in templates where
3422 initializers can maintain a syntactic rather than semantic form
3423 (even if they are non-dependent, for access-checking purposes). */
3425 tree
3426 fold_decl_constant_value (tree expr)
3428 tree const_expr = expr;
3431 expr = fold_non_dependent_expr (const_expr);
3432 const_expr = integral_constant_value (expr);
3434 while (expr != const_expr);
3436 return expr;
3439 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3440 must be a function or a pointer-to-function type, as specified
3441 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3442 and check that the resulting function has external linkage. */
3444 static tree
3445 convert_nontype_argument_function (tree type, tree expr)
3447 tree fns = expr;
3448 tree fn, fn_no_ptr;
3450 fn = instantiate_type (type, fns, tf_none);
3451 if (fn == error_mark_node)
3452 return error_mark_node;
3454 fn_no_ptr = fn;
3455 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3456 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3458 /* [temp.arg.nontype]/1
3460 A template-argument for a non-type, non-template template-parameter
3461 shall be one of:
3462 [...]
3463 -- the address of an object or function with external linkage. */
3464 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3466 error ("%qE is not a valid template argument for type %qT "
3467 "because function %qD has not external linkage",
3468 expr, type, fn_no_ptr);
3469 return NULL_TREE;
3472 return fn;
3475 /* Attempt to convert the non-type template parameter EXPR to the
3476 indicated TYPE. If the conversion is successful, return the
3477 converted value. If the conversion is unsuccessful, return
3478 NULL_TREE if we issued an error message, or error_mark_node if we
3479 did not. We issue error messages for out-and-out bad template
3480 parameters, but not simply because the conversion failed, since we
3481 might be just trying to do argument deduction. Both TYPE and EXPR
3482 must be non-dependent.
3484 The conversion follows the special rules described in
3485 [temp.arg.nontype], and it is much more strict than an implicit
3486 conversion.
3488 This function is called twice for each template argument (see
3489 lookup_template_class for a more accurate description of this
3490 problem). This means that we need to handle expressions which
3491 are not valid in a C++ source, but can be created from the
3492 first call (for instance, casts to perform conversions). These
3493 hacks can go away after we fix the double coercion problem. */
3495 static tree
3496 convert_nontype_argument (tree type, tree expr)
3498 tree expr_type;
3500 /* Detect immediately string literals as invalid non-type argument.
3501 This special-case is not needed for correctness (we would easily
3502 catch this later), but only to provide better diagnostic for this
3503 common user mistake. As suggested by DR 100, we do not mention
3504 linkage issues in the diagnostic as this is not the point. */
3505 if (TREE_CODE (expr) == STRING_CST)
3507 error ("%qE is not a valid template argument for type %qT "
3508 "because string literals can never be used in this context",
3509 expr, type);
3510 return NULL_TREE;
3513 /* If we are in a template, EXPR may be non-dependent, but still
3514 have a syntactic, rather than semantic, form. For example, EXPR
3515 might be a SCOPE_REF, rather than the VAR_DECL to which the
3516 SCOPE_REF refers. Preserving the qualifying scope is necessary
3517 so that access checking can be performed when the template is
3518 instantiated -- but here we need the resolved form so that we can
3519 convert the argument. */
3520 expr = fold_non_dependent_expr (expr);
3521 if (error_operand_p (expr))
3522 return error_mark_node;
3523 expr_type = TREE_TYPE (expr);
3525 /* HACK: Due to double coercion, we can get a
3526 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3527 which is the tree that we built on the first call (see
3528 below when coercing to reference to object or to reference to
3529 function). We just strip everything and get to the arg.
3530 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3531 for examples. */
3532 if (TREE_CODE (expr) == NOP_EXPR)
3534 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3536 /* ??? Maybe we could use convert_from_reference here, but we
3537 would need to relax its constraints because the NOP_EXPR
3538 could actually change the type to something more cv-qualified,
3539 and this is not folded by convert_from_reference. */
3540 tree addr = TREE_OPERAND (expr, 0);
3541 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3542 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3543 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3544 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3545 (TREE_TYPE (expr_type),
3546 TREE_TYPE (TREE_TYPE (addr))));
3548 expr = TREE_OPERAND (addr, 0);
3549 expr_type = TREE_TYPE (expr);
3552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3553 parameter is a pointer to object, through decay and
3554 qualification conversion. Let's strip everything. */
3555 else if (TYPE_PTROBV_P (type))
3557 STRIP_NOPS (expr);
3558 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3559 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3560 /* Skip the ADDR_EXPR only if it is part of the decay for
3561 an array. Otherwise, it is part of the original argument
3562 in the source code. */
3563 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3564 expr = TREE_OPERAND (expr, 0);
3565 expr_type = TREE_TYPE (expr);
3569 /* [temp.arg.nontype]/5, bullet 1
3571 For a non-type template-parameter of integral or enumeration type,
3572 integral promotions (_conv.prom_) and integral conversions
3573 (_conv.integral_) are applied. */
3574 if (INTEGRAL_TYPE_P (type))
3576 if (!INTEGRAL_TYPE_P (expr_type))
3577 return error_mark_node;
3579 expr = fold_decl_constant_value (expr);
3580 /* Notice that there are constant expressions like '4 % 0' which
3581 do not fold into integer constants. */
3582 if (TREE_CODE (expr) != INTEGER_CST)
3584 error ("%qE is not a valid template argument for type %qT "
3585 "because it is a non-constant expression", expr, type);
3586 return NULL_TREE;
3589 /* At this point, an implicit conversion does what we want,
3590 because we already know that the expression is of integral
3591 type. */
3592 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3593 if (expr == error_mark_node)
3594 return error_mark_node;
3596 /* Conversion was allowed: fold it to a bare integer constant. */
3597 expr = fold (expr);
3599 /* [temp.arg.nontype]/5, bullet 2
3601 For a non-type template-parameter of type pointer to object,
3602 qualification conversions (_conv.qual_) and the array-to-pointer
3603 conversion (_conv.array_) are applied. */
3604 else if (TYPE_PTROBV_P (type))
3606 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3608 A template-argument for a non-type, non-template template-parameter
3609 shall be one of: [...]
3611 -- the name of a non-type template-parameter;
3612 -- the address of an object or function with external linkage, [...]
3613 expressed as "& id-expression" where the & is optional if the name
3614 refers to a function or array, or if the corresponding
3615 template-parameter is a reference.
3617 Here, we do not care about functions, as they are invalid anyway
3618 for a parameter of type pointer-to-object. */
3619 bool constant_address_p =
3620 (TREE_CODE (expr) == ADDR_EXPR
3621 || TREE_CODE (expr_type) == ARRAY_TYPE
3622 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3624 expr = decay_conversion (expr);
3625 if (expr == error_mark_node)
3626 return error_mark_node;
3628 expr = perform_qualification_conversions (type, expr);
3629 if (expr == error_mark_node)
3630 return error_mark_node;
3632 if (!constant_address_p)
3634 error ("%qE is not a valid template argument for type %qT "
3635 "because it is not a constant pointer", expr, type);
3636 return NULL_TREE;
3639 /* [temp.arg.nontype]/5, bullet 3
3641 For a non-type template-parameter of type reference to object, no
3642 conversions apply. The type referred to by the reference may be more
3643 cv-qualified than the (otherwise identical) type of the
3644 template-argument. The template-parameter is bound directly to the
3645 template-argument, which must be an lvalue. */
3646 else if (TYPE_REF_OBJ_P (type))
3648 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3649 expr_type))
3650 return error_mark_node;
3652 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3654 error ("%qE is not a valid template argument for type %qT "
3655 "because of conflicts in cv-qualification", expr, type);
3656 return NULL_TREE;
3659 if (!real_lvalue_p (expr))
3661 error ("%qE is not a valid template argument for type %qT "
3662 "because it is not a lvalue", expr, type);
3663 return NULL_TREE;
3666 /* [temp.arg.nontype]/1
3668 A template-argument for a non-type, non-template template-parameter
3669 shall be one of: [...]
3671 -- the address of an object or function with external linkage. */
3672 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3674 error ("%qE is not a valid template argument for type %qT "
3675 "because object %qD has not external linkage",
3676 expr, type, expr);
3677 return NULL_TREE;
3680 expr = build_nop (type, build_address (expr));
3682 /* [temp.arg.nontype]/5, bullet 4
3684 For a non-type template-parameter of type pointer to function, only
3685 the function-to-pointer conversion (_conv.func_) is applied. If the
3686 template-argument represents a set of overloaded functions (or a
3687 pointer to such), the matching function is selected from the set
3688 (_over.over_). */
3689 else if (TYPE_PTRFN_P (type))
3691 /* If the argument is a template-id, we might not have enough
3692 context information to decay the pointer. */
3693 if (!type_unknown_p (expr_type))
3695 expr = decay_conversion (expr);
3696 if (expr == error_mark_node)
3697 return error_mark_node;
3700 expr = convert_nontype_argument_function (type, expr);
3701 if (!expr || expr == error_mark_node)
3702 return expr;
3704 /* [temp.arg.nontype]/5, bullet 5
3706 For a non-type template-parameter of type reference to function, no
3707 conversions apply. If the template-argument represents a set of
3708 overloaded functions, the matching function is selected from the set
3709 (_over.over_). */
3710 else if (TYPE_REFFN_P (type))
3712 if (TREE_CODE (expr) == ADDR_EXPR)
3714 error ("%qE is not a valid template argument for type %qT "
3715 "because it is a pointer", expr, type);
3716 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3717 return NULL_TREE;
3720 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3721 if (!expr || expr == error_mark_node)
3722 return expr;
3724 expr = build_nop (type, build_address (expr));
3726 /* [temp.arg.nontype]/5, bullet 6
3728 For a non-type template-parameter of type pointer to member function,
3729 no conversions apply. If the template-argument represents a set of
3730 overloaded member functions, the matching member function is selected
3731 from the set (_over.over_). */
3732 else if (TYPE_PTRMEMFUNC_P (type))
3734 expr = instantiate_type (type, expr, tf_none);
3735 if (expr == error_mark_node)
3736 return error_mark_node;
3738 /* There is no way to disable standard conversions in
3739 resolve_address_of_overloaded_function (called by
3740 instantiate_type). It is possible that the call succeeded by
3741 converting &B::I to &D::I (where B is a base of D), so we need
3742 to reject this conversion here.
3744 Actually, even if there was a way to disable standard conversions,
3745 it would still be better to reject them here so that we can
3746 provide a superior diagnostic. */
3747 if (!same_type_p (TREE_TYPE (expr), type))
3749 /* Make sure we are just one standard conversion off. */
3750 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3751 error ("%qE is not a valid template argument for type %qT "
3752 "because it is of type %qT", expr, type,
3753 TREE_TYPE (expr));
3754 inform ("standard conversions are not allowed in this context");
3755 return NULL_TREE;
3758 /* [temp.arg.nontype]/5, bullet 7
3760 For a non-type template-parameter of type pointer to data member,
3761 qualification conversions (_conv.qual_) are applied. */
3762 else if (TYPE_PTRMEM_P (type))
3764 expr = perform_qualification_conversions (type, expr);
3765 if (expr == error_mark_node)
3766 return expr;
3768 /* A template non-type parameter must be one of the above. */
3769 else
3770 gcc_unreachable ();
3772 /* Sanity check: did we actually convert the argument to the
3773 right type? */
3774 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3775 return expr;
3779 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3780 template template parameters. Both PARM_PARMS and ARG_PARMS are
3781 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3782 or PARM_DECL.
3784 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3785 the case, then extra parameters must have default arguments.
3787 Consider the example:
3788 template <class T, class Allocator = allocator> class vector;
3789 template<template <class U> class TT> class C;
3791 C<vector> is a valid instantiation. PARM_PARMS for the above code
3792 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3793 T and Allocator) and OUTER_ARGS contains the argument that is used to
3794 substitute the TT parameter. */
3796 static int
3797 coerce_template_template_parms (tree parm_parms,
3798 tree arg_parms,
3799 tsubst_flags_t complain,
3800 tree in_decl,
3801 tree outer_args)
3803 int nparms, nargs, i;
3804 tree parm, arg;
3806 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3807 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3809 nparms = TREE_VEC_LENGTH (parm_parms);
3810 nargs = TREE_VEC_LENGTH (arg_parms);
3812 /* The rule here is opposite of coerce_template_parms. */
3813 if (nargs < nparms
3814 || (nargs > nparms
3815 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3816 return 0;
3818 for (i = 0; i < nparms; ++i)
3820 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3821 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3823 if (arg == NULL_TREE || arg == error_mark_node
3824 || parm == NULL_TREE || parm == error_mark_node)
3825 return 0;
3827 if (TREE_CODE (arg) != TREE_CODE (parm))
3828 return 0;
3830 switch (TREE_CODE (parm))
3832 case TYPE_DECL:
3833 break;
3835 case TEMPLATE_DECL:
3836 /* We encounter instantiations of templates like
3837 template <template <template <class> class> class TT>
3838 class C; */
3840 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3841 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3843 if (!coerce_template_template_parms
3844 (parmparm, argparm, complain, in_decl, outer_args))
3845 return 0;
3847 break;
3849 case PARM_DECL:
3850 /* The tsubst call is used to handle cases such as
3852 template <int> class C {};
3853 template <class T, template <T> class TT> class D {};
3854 D<int, C> d;
3856 i.e. the parameter list of TT depends on earlier parameters. */
3857 if (!dependent_type_p (TREE_TYPE (arg))
3858 && !same_type_p
3859 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3860 TREE_TYPE (arg)))
3861 return 0;
3862 break;
3864 default:
3865 gcc_unreachable ();
3868 return 1;
3871 /* Convert the indicated template ARG as necessary to match the
3872 indicated template PARM. Returns the converted ARG, or
3873 error_mark_node if the conversion was unsuccessful. Error and
3874 warning messages are issued under control of COMPLAIN. This
3875 conversion is for the Ith parameter in the parameter list. ARGS is
3876 the full set of template arguments deduced so far. */
3878 static tree
3879 convert_template_argument (tree parm,
3880 tree arg,
3881 tree args,
3882 tsubst_flags_t complain,
3883 int i,
3884 tree in_decl)
3886 tree val;
3887 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3889 if (TREE_CODE (arg) == TREE_LIST
3890 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3892 /* The template argument was the name of some
3893 member function. That's usually
3894 invalid, but static members are OK. In any
3895 case, grab the underlying fields/functions
3896 and issue an error later if required. */
3897 arg = TREE_VALUE (arg);
3898 TREE_TYPE (arg) = unknown_type_node;
3901 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3902 requires_type = (TREE_CODE (parm) == TYPE_DECL
3903 || requires_tmpl_type);
3905 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3906 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3907 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3908 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3910 if (is_tmpl_type
3911 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3912 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3913 arg = TYPE_STUB_DECL (arg);
3915 is_type = TYPE_P (arg) || is_tmpl_type;
3917 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3918 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3920 pedwarn ("to refer to a type member of a template parameter, "
3921 "use %<typename %E%>", arg);
3923 arg = make_typename_type (TREE_OPERAND (arg, 0),
3924 TREE_OPERAND (arg, 1),
3925 typename_type,
3926 complain & tf_error);
3927 is_type = 1;
3929 if (is_type != requires_type)
3931 if (in_decl)
3933 if (complain & tf_error)
3935 error ("type/value mismatch at argument %d in template "
3936 "parameter list for %qD",
3937 i + 1, in_decl);
3938 if (is_type)
3939 error (" expected a constant of type %qT, got %qT",
3940 TREE_TYPE (parm),
3941 (is_tmpl_type ? DECL_NAME (arg) : arg));
3942 else if (requires_tmpl_type)
3943 error (" expected a class template, got %qE", arg);
3944 else
3945 error (" expected a type, got %qE", arg);
3948 return error_mark_node;
3950 if (is_tmpl_type ^ requires_tmpl_type)
3952 if (in_decl && (complain & tf_error))
3954 error ("type/value mismatch at argument %d in template "
3955 "parameter list for %qD",
3956 i + 1, in_decl);
3957 if (is_tmpl_type)
3958 error (" expected a type, got %qT", DECL_NAME (arg));
3959 else
3960 error (" expected a class template, got %qT", arg);
3962 return error_mark_node;
3965 if (is_type)
3967 if (requires_tmpl_type)
3969 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3970 /* The number of argument required is not known yet.
3971 Just accept it for now. */
3972 val = TREE_TYPE (arg);
3973 else
3975 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3976 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3978 if (coerce_template_template_parms (parmparm, argparm,
3979 complain, in_decl,
3980 args))
3982 val = arg;
3984 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3985 TEMPLATE_DECL. */
3986 if (val != error_mark_node
3987 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3988 val = TREE_TYPE (val);
3990 else
3992 if (in_decl && (complain & tf_error))
3994 error ("type/value mismatch at argument %d in "
3995 "template parameter list for %qD",
3996 i + 1, in_decl);
3997 error (" expected a template of type %qD, got %qD",
3998 parm, arg);
4001 val = error_mark_node;
4005 else
4006 val = arg;
4007 /* We only form one instance of each template specialization.
4008 Therefore, if we use a non-canonical variant (i.e., a
4009 typedef), any future messages referring to the type will use
4010 the typedef, which is confusing if those future uses do not
4011 themselves also use the typedef. */
4012 if (TYPE_P (val))
4013 val = canonical_type_variant (val);
4015 else
4017 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4019 if (invalid_nontype_parm_type_p (t, complain))
4020 return error_mark_node;
4022 if (!uses_template_parms (arg) && !uses_template_parms (t))
4023 /* We used to call digest_init here. However, digest_init
4024 will report errors, which we don't want when complain
4025 is zero. More importantly, digest_init will try too
4026 hard to convert things: for example, `0' should not be
4027 converted to pointer type at this point according to
4028 the standard. Accepting this is not merely an
4029 extension, since deciding whether or not these
4030 conversions can occur is part of determining which
4031 function template to call, or whether a given explicit
4032 argument specification is valid. */
4033 val = convert_nontype_argument (t, arg);
4034 else
4035 val = arg;
4037 if (val == NULL_TREE)
4038 val = error_mark_node;
4039 else if (val == error_mark_node && (complain & tf_error))
4040 error ("could not convert template argument %qE to %qT", arg, t);
4043 return val;
4046 /* Convert all template arguments to their appropriate types, and
4047 return a vector containing the innermost resulting template
4048 arguments. If any error occurs, return error_mark_node. Error and
4049 warning messages are issued under control of COMPLAIN.
4051 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
4052 provided in ARGLIST, or else trailing parameters must have default
4053 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
4054 deduction for any unspecified trailing arguments. */
4056 static tree
4057 coerce_template_parms (tree parms,
4058 tree args,
4059 tree in_decl,
4060 tsubst_flags_t complain,
4061 int require_all_arguments)
4063 int nparms, nargs, i, lost = 0;
4064 tree inner_args;
4065 tree new_args;
4066 tree new_inner_args;
4067 bool saved_skip_evaluation;
4069 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4070 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4071 nparms = TREE_VEC_LENGTH (parms);
4073 if (nargs > nparms
4074 || (nargs < nparms
4075 && require_all_arguments
4076 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
4078 if (complain & tf_error)
4080 error ("wrong number of template arguments (%d, should be %d)",
4081 nargs, nparms);
4083 if (in_decl)
4084 error ("provided for %q+D", in_decl);
4087 return error_mark_node;
4090 /* We need to evaluate the template arguments, even though this
4091 template-id may be nested within a "sizeof". */
4092 saved_skip_evaluation = skip_evaluation;
4093 skip_evaluation = false;
4094 new_inner_args = make_tree_vec (nparms);
4095 new_args = add_outermost_template_args (args, new_inner_args);
4096 for (i = 0; i < nparms; i++)
4098 tree arg;
4099 tree parm;
4101 /* Get the Ith template parameter. */
4102 parm = TREE_VEC_ELT (parms, i);
4104 /* Calculate the Ith argument. */
4105 if (i < nargs)
4106 arg = TREE_VEC_ELT (inner_args, i);
4107 else if (require_all_arguments)
4108 /* There must be a default arg in this case. */
4109 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4110 complain, in_decl);
4111 else
4112 break;
4114 gcc_assert (arg);
4115 if (arg == error_mark_node)
4117 if (complain & tf_error)
4118 error ("template argument %d is invalid", i + 1);
4120 else
4121 arg = convert_template_argument (TREE_VALUE (parm),
4122 arg, new_args, complain, i,
4123 in_decl);
4125 if (arg == error_mark_node)
4126 lost++;
4127 TREE_VEC_ELT (new_inner_args, i) = arg;
4129 skip_evaluation = saved_skip_evaluation;
4131 if (lost)
4132 return error_mark_node;
4134 return new_inner_args;
4137 /* Returns 1 if template args OT and NT are equivalent. */
4139 static int
4140 template_args_equal (tree ot, tree nt)
4142 if (nt == ot)
4143 return 1;
4145 if (TREE_CODE (nt) == TREE_VEC)
4146 /* For member templates */
4147 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4148 else if (TYPE_P (nt))
4149 return TYPE_P (ot) && same_type_p (ot, nt);
4150 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4151 return 0;
4152 else
4153 return cp_tree_equal (ot, nt);
4156 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4157 of template arguments. Returns 0 otherwise. */
4160 comp_template_args (tree oldargs, tree newargs)
4162 int i;
4164 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4165 return 0;
4167 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4169 tree nt = TREE_VEC_ELT (newargs, i);
4170 tree ot = TREE_VEC_ELT (oldargs, i);
4172 if (! template_args_equal (ot, nt))
4173 return 0;
4175 return 1;
4178 /* Given class template name and parameter list, produce a user-friendly name
4179 for the instantiation. */
4181 static char *
4182 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4184 static struct obstack scratch_obstack;
4185 static char *scratch_firstobj;
4186 int i, nparms;
4188 if (!scratch_firstobj)
4189 gcc_obstack_init (&scratch_obstack);
4190 else
4191 obstack_free (&scratch_obstack, scratch_firstobj);
4192 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
4194 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4195 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4197 cat (name);
4198 ccat ('<');
4199 nparms = TREE_VEC_LENGTH (parms);
4200 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4201 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4202 for (i = 0; i < nparms; i++)
4204 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4205 tree arg = TREE_VEC_ELT (arglist, i);
4207 if (i)
4208 ccat (',');
4210 if (TREE_CODE (parm) == TYPE_DECL)
4212 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4213 continue;
4215 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4217 if (TREE_CODE (arg) == TEMPLATE_DECL)
4219 /* Already substituted with real template. Just output
4220 the template name here */
4221 tree context = DECL_CONTEXT (arg);
4222 if (context)
4224 /* The template may be defined in a namespace, or
4225 may be a member template. */
4226 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4227 || CLASS_TYPE_P (context));
4228 cat (decl_as_string (DECL_CONTEXT (arg),
4229 TFF_PLAIN_IDENTIFIER));
4230 cat ("::");
4232 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4234 else
4235 /* Output the parameter declaration. */
4236 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4237 continue;
4239 else
4240 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4242 /* No need to check arglist against parmlist here; we did that
4243 in coerce_template_parms, called from lookup_template_class. */
4244 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4247 char *bufp = obstack_next_free (&scratch_obstack);
4248 int offset = 0;
4249 while (bufp[offset - 1] == ' ')
4250 offset--;
4251 obstack_blank_fast (&scratch_obstack, offset);
4253 /* B<C<char> >, not B<C<char>> */
4254 if (bufp[offset - 1] == '>')
4255 ccat (' ');
4257 ccat ('>');
4258 ccat ('\0');
4259 return (char *) obstack_base (&scratch_obstack);
4262 static tree
4263 classtype_mangled_name (tree t)
4265 if (CLASSTYPE_TEMPLATE_INFO (t)
4266 /* Specializations have already had their names set up in
4267 lookup_template_class. */
4268 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4270 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4272 /* For non-primary templates, the template parameters are
4273 implicit from their surrounding context. */
4274 if (PRIMARY_TEMPLATE_P (tmpl))
4276 tree name = DECL_NAME (tmpl);
4277 char *mangled_name = mangle_class_name_for_template
4278 (IDENTIFIER_POINTER (name),
4279 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4280 CLASSTYPE_TI_ARGS (t));
4281 tree id = get_identifier (mangled_name);
4282 IDENTIFIER_TEMPLATE (id) = name;
4283 return id;
4287 return TYPE_IDENTIFIER (t);
4290 static void
4291 add_pending_template (tree d)
4293 tree ti = (TYPE_P (d)
4294 ? CLASSTYPE_TEMPLATE_INFO (d)
4295 : DECL_TEMPLATE_INFO (d));
4296 tree pt;
4297 int level;
4299 if (TI_PENDING_TEMPLATE_FLAG (ti))
4300 return;
4302 /* We are called both from instantiate_decl, where we've already had a
4303 tinst_level pushed, and instantiate_template, where we haven't.
4304 Compensate. */
4305 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4307 if (level)
4308 push_tinst_level (d);
4310 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4311 if (last_pending_template)
4312 TREE_CHAIN (last_pending_template) = pt;
4313 else
4314 pending_templates = pt;
4316 last_pending_template = pt;
4318 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4320 if (level)
4321 pop_tinst_level ();
4325 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4326 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4327 documentation for TEMPLATE_ID_EXPR. */
4329 tree
4330 lookup_template_function (tree fns, tree arglist)
4332 tree type;
4334 if (fns == error_mark_node || arglist == error_mark_node)
4335 return error_mark_node;
4337 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4338 gcc_assert (fns && (is_overloaded_fn (fns)
4339 || TREE_CODE (fns) == IDENTIFIER_NODE));
4341 if (BASELINK_P (fns))
4343 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4344 unknown_type_node,
4345 BASELINK_FUNCTIONS (fns),
4346 arglist);
4347 return fns;
4350 type = TREE_TYPE (fns);
4351 if (TREE_CODE (fns) == OVERLOAD || !type)
4352 type = unknown_type_node;
4354 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4357 /* Within the scope of a template class S<T>, the name S gets bound
4358 (in build_self_reference) to a TYPE_DECL for the class, not a
4359 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4360 or one of its enclosing classes, and that type is a template,
4361 return the associated TEMPLATE_DECL. Otherwise, the original
4362 DECL is returned. */
4364 tree
4365 maybe_get_template_decl_from_type_decl (tree decl)
4367 return (decl != NULL_TREE
4368 && TREE_CODE (decl) == TYPE_DECL
4369 && DECL_ARTIFICIAL (decl)
4370 && CLASS_TYPE_P (TREE_TYPE (decl))
4371 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4372 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4375 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4376 parameters, find the desired type.
4378 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4380 IN_DECL, if non-NULL, is the template declaration we are trying to
4381 instantiate.
4383 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4384 the class we are looking up.
4386 Issue error and warning messages under control of COMPLAIN.
4388 If the template class is really a local class in a template
4389 function, then the FUNCTION_CONTEXT is the function in which it is
4390 being instantiated.
4392 ??? Note that this function is currently called *twice* for each
4393 template-id: the first time from the parser, while creating the
4394 incomplete type (finish_template_type), and the second type during the
4395 real instantiation (instantiate_template_class). This is surely something
4396 that we want to avoid. It also causes some problems with argument
4397 coercion (see convert_nontype_argument for more information on this). */
4399 tree
4400 lookup_template_class (tree d1,
4401 tree arglist,
4402 tree in_decl,
4403 tree context,
4404 int entering_scope,
4405 tsubst_flags_t complain)
4407 tree template = NULL_TREE, parmlist;
4408 tree t;
4410 timevar_push (TV_NAME_LOOKUP);
4412 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4414 tree value = innermost_non_namespace_value (d1);
4415 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4416 template = value;
4417 else
4419 if (context)
4420 push_decl_namespace (context);
4421 template = lookup_name (d1, /*prefer_type=*/0);
4422 template = maybe_get_template_decl_from_type_decl (template);
4423 if (context)
4424 pop_decl_namespace ();
4426 if (template)
4427 context = DECL_CONTEXT (template);
4429 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4431 tree type = TREE_TYPE (d1);
4433 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4434 an implicit typename for the second A. Deal with it. */
4435 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4436 type = TREE_TYPE (type);
4438 if (CLASSTYPE_TEMPLATE_INFO (type))
4440 template = CLASSTYPE_TI_TEMPLATE (type);
4441 d1 = DECL_NAME (template);
4444 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4445 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4447 template = TYPE_TI_TEMPLATE (d1);
4448 d1 = DECL_NAME (template);
4450 else if (TREE_CODE (d1) == TEMPLATE_DECL
4451 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4453 template = d1;
4454 d1 = DECL_NAME (template);
4455 context = DECL_CONTEXT (template);
4458 /* Issue an error message if we didn't find a template. */
4459 if (! template)
4461 if (complain & tf_error)
4462 error ("%qT is not a template", d1);
4463 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4466 if (TREE_CODE (template) != TEMPLATE_DECL
4467 /* Make sure it's a user visible template, if it was named by
4468 the user. */
4469 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4470 && !PRIMARY_TEMPLATE_P (template)))
4472 if (complain & tf_error)
4474 error ("non-template type %qT used as a template", d1);
4475 if (in_decl)
4476 error ("for template declaration %q+D", in_decl);
4478 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4481 complain &= ~tf_user;
4483 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4485 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4486 template arguments */
4488 tree parm;
4489 tree arglist2;
4491 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4493 /* Consider an example where a template template parameter declared as
4495 template <class T, class U = std::allocator<T> > class TT
4497 The template parameter level of T and U are one level larger than
4498 of TT. To proper process the default argument of U, say when an
4499 instantiation `TT<int>' is seen, we need to build the full
4500 arguments containing {int} as the innermost level. Outer levels,
4501 available when not appearing as default template argument, can be
4502 obtained from `current_template_args ()'.
4504 Suppose that TT is later substituted with std::vector. The above
4505 instantiation is `TT<int, std::allocator<T> >' with TT at
4506 level 1, and T at level 2, while the template arguments at level 1
4507 becomes {std::vector} and the inner level 2 is {int}. */
4509 if (current_template_parms)
4510 arglist = add_to_template_args (current_template_args (), arglist);
4512 arglist2 = coerce_template_parms (parmlist, arglist, template,
4513 complain, /*require_all_args=*/1);
4514 if (arglist2 == error_mark_node
4515 || (!uses_template_parms (arglist2)
4516 && check_instantiated_args (template, arglist2, complain)))
4517 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4519 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4520 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4522 else
4524 tree template_type = TREE_TYPE (template);
4525 tree gen_tmpl;
4526 tree type_decl;
4527 tree found = NULL_TREE;
4528 int arg_depth;
4529 int parm_depth;
4530 int is_partial_instantiation;
4532 gen_tmpl = most_general_template (template);
4533 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4534 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4535 arg_depth = TMPL_ARGS_DEPTH (arglist);
4537 if (arg_depth == 1 && parm_depth > 1)
4539 /* We've been given an incomplete set of template arguments.
4540 For example, given:
4542 template <class T> struct S1 {
4543 template <class U> struct S2 {};
4544 template <class U> struct S2<U*> {};
4547 we will be called with an ARGLIST of `U*', but the
4548 TEMPLATE will be `template <class T> template
4549 <class U> struct S1<T>::S2'. We must fill in the missing
4550 arguments. */
4551 arglist
4552 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4553 arglist);
4554 arg_depth = TMPL_ARGS_DEPTH (arglist);
4557 /* Now we should have enough arguments. */
4558 gcc_assert (parm_depth == arg_depth);
4560 /* From here on, we're only interested in the most general
4561 template. */
4562 template = gen_tmpl;
4564 /* Calculate the BOUND_ARGS. These will be the args that are
4565 actually tsubst'd into the definition to create the
4566 instantiation. */
4567 if (parm_depth > 1)
4569 /* We have multiple levels of arguments to coerce, at once. */
4570 int i;
4571 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4573 tree bound_args = make_tree_vec (parm_depth);
4575 for (i = saved_depth,
4576 t = DECL_TEMPLATE_PARMS (template);
4577 i > 0 && t != NULL_TREE;
4578 --i, t = TREE_CHAIN (t))
4580 tree a = coerce_template_parms (TREE_VALUE (t),
4581 arglist, template,
4582 complain, /*require_all_args=*/1);
4584 /* Don't process further if one of the levels fails. */
4585 if (a == error_mark_node)
4587 /* Restore the ARGLIST to its full size. */
4588 TREE_VEC_LENGTH (arglist) = saved_depth;
4589 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4592 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4594 /* We temporarily reduce the length of the ARGLIST so
4595 that coerce_template_parms will see only the arguments
4596 corresponding to the template parameters it is
4597 examining. */
4598 TREE_VEC_LENGTH (arglist)--;
4601 /* Restore the ARGLIST to its full size. */
4602 TREE_VEC_LENGTH (arglist) = saved_depth;
4604 arglist = bound_args;
4606 else
4607 arglist
4608 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4609 INNERMOST_TEMPLATE_ARGS (arglist),
4610 template,
4611 complain, /*require_all_args=*/1);
4613 if (arglist == error_mark_node)
4614 /* We were unable to bind the arguments. */
4615 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4617 /* In the scope of a template class, explicit references to the
4618 template class refer to the type of the template, not any
4619 instantiation of it. For example, in:
4621 template <class T> class C { void f(C<T>); }
4623 the `C<T>' is just the same as `C'. Outside of the
4624 class, however, such a reference is an instantiation. */
4625 if (comp_template_args (TYPE_TI_ARGS (template_type),
4626 arglist))
4628 found = template_type;
4630 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4632 tree ctx;
4634 for (ctx = current_class_type;
4635 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4636 ctx = (TYPE_P (ctx)
4637 ? TYPE_CONTEXT (ctx)
4638 : DECL_CONTEXT (ctx)))
4639 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4640 goto found_ctx;
4642 /* We're not in the scope of the class, so the
4643 TEMPLATE_TYPE is not the type we want after all. */
4644 found = NULL_TREE;
4645 found_ctx:;
4648 if (found)
4649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4651 /* If we already have this specialization, return it. */
4652 found = retrieve_specialization (template, arglist,
4653 /*class_specializations_p=*/false);
4654 if (found)
4655 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4657 /* This type is a "partial instantiation" if any of the template
4658 arguments still involve template parameters. Note that we set
4659 IS_PARTIAL_INSTANTIATION for partial specializations as
4660 well. */
4661 is_partial_instantiation = uses_template_parms (arglist);
4663 /* If the deduced arguments are invalid, then the binding
4664 failed. */
4665 if (!is_partial_instantiation
4666 && check_instantiated_args (template,
4667 INNERMOST_TEMPLATE_ARGS (arglist),
4668 complain))
4669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4671 if (!is_partial_instantiation
4672 && !PRIMARY_TEMPLATE_P (template)
4673 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4675 found = xref_tag_from_type (TREE_TYPE (template),
4676 DECL_NAME (template),
4677 /*tag_scope=*/ts_global);
4678 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4681 context = tsubst (DECL_CONTEXT (template), arglist,
4682 complain, in_decl);
4683 if (!context)
4684 context = global_namespace;
4686 /* Create the type. */
4687 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4689 if (!is_partial_instantiation)
4691 set_current_access_from_decl (TYPE_NAME (template_type));
4692 t = start_enum (TYPE_IDENTIFIER (template_type));
4694 else
4695 /* We don't want to call start_enum for this type, since
4696 the values for the enumeration constants may involve
4697 template parameters. And, no one should be interested
4698 in the enumeration constants for such a type. */
4699 t = make_node (ENUMERAL_TYPE);
4701 else
4703 t = make_aggr_type (TREE_CODE (template_type));
4704 CLASSTYPE_DECLARED_CLASS (t)
4705 = CLASSTYPE_DECLARED_CLASS (template_type);
4706 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4707 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4709 /* A local class. Make sure the decl gets registered properly. */
4710 if (context == current_function_decl)
4711 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4714 /* If we called start_enum or pushtag above, this information
4715 will already be set up. */
4716 if (!TYPE_NAME (t))
4718 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4720 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4721 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4722 TYPE_STUB_DECL (t) = type_decl;
4723 DECL_SOURCE_LOCATION (type_decl)
4724 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4726 else
4727 type_decl = TYPE_NAME (t);
4729 TREE_PRIVATE (type_decl)
4730 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4731 TREE_PROTECTED (type_decl)
4732 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4733 DECL_IN_SYSTEM_HEADER (type_decl)
4734 = DECL_IN_SYSTEM_HEADER (template);
4735 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4737 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4738 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4741 /* Set up the template information. We have to figure out which
4742 template is the immediate parent if this is a full
4743 instantiation. */
4744 if (parm_depth == 1 || is_partial_instantiation
4745 || !PRIMARY_TEMPLATE_P (template))
4746 /* This case is easy; there are no member templates involved. */
4747 found = template;
4748 else
4750 /* This is a full instantiation of a member template. Look
4751 for a partial instantiation of which this is an instance. */
4753 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4754 found; found = TREE_CHAIN (found))
4756 int success;
4757 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4759 /* We only want partial instantiations, here, not
4760 specializations or full instantiations. */
4761 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4762 || !uses_template_parms (TREE_VALUE (found)))
4763 continue;
4765 /* Temporarily reduce by one the number of levels in the
4766 ARGLIST and in FOUND so as to avoid comparing the
4767 last set of arguments. */
4768 TREE_VEC_LENGTH (arglist)--;
4769 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4771 /* See if the arguments match. If they do, then TMPL is
4772 the partial instantiation we want. */
4773 success = comp_template_args (TREE_PURPOSE (found), arglist);
4775 /* Restore the argument vectors to their full size. */
4776 TREE_VEC_LENGTH (arglist)++;
4777 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4779 if (success)
4781 found = tmpl;
4782 break;
4786 if (!found)
4788 /* There was no partial instantiation. This happens
4789 where C<T> is a member template of A<T> and it's used
4790 in something like
4792 template <typename T> struct B { A<T>::C<int> m; };
4793 B<float>;
4795 Create the partial instantiation.
4797 TREE_VEC_LENGTH (arglist)--;
4798 found = tsubst (template, arglist, complain, NULL_TREE);
4799 TREE_VEC_LENGTH (arglist)++;
4803 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4804 DECL_TEMPLATE_INSTANTIATIONS (template)
4805 = tree_cons (arglist, t,
4806 DECL_TEMPLATE_INSTANTIATIONS (template));
4808 if (TREE_CODE (t) == ENUMERAL_TYPE
4809 && !is_partial_instantiation)
4810 /* Now that the type has been registered on the instantiations
4811 list, we set up the enumerators. Because the enumeration
4812 constants may involve the enumeration type itself, we make
4813 sure to register the type first, and then create the
4814 constants. That way, doing tsubst_expr for the enumeration
4815 constants won't result in recursive calls here; we'll find
4816 the instantiation and exit above. */
4817 tsubst_enum (template_type, t, arglist);
4819 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4820 is set up. */
4821 if (TREE_CODE (t) != ENUMERAL_TYPE)
4822 DECL_NAME (type_decl) = classtype_mangled_name (t);
4823 if (is_partial_instantiation)
4824 /* If the type makes use of template parameters, the
4825 code that generates debugging information will crash. */
4826 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4828 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4830 timevar_pop (TV_NAME_LOOKUP);
4833 struct pair_fn_data
4835 tree_fn_t fn;
4836 void *data;
4837 struct pointer_set_t *visited;
4840 /* Called from for_each_template_parm via walk_tree. */
4842 static tree
4843 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4845 tree t = *tp;
4846 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4847 tree_fn_t fn = pfd->fn;
4848 void *data = pfd->data;
4850 if (TYPE_P (t)
4851 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4852 return error_mark_node;
4854 switch (TREE_CODE (t))
4856 case RECORD_TYPE:
4857 if (TYPE_PTRMEMFUNC_P (t))
4858 break;
4859 /* Fall through. */
4861 case UNION_TYPE:
4862 case ENUMERAL_TYPE:
4863 if (!TYPE_TEMPLATE_INFO (t))
4864 *walk_subtrees = 0;
4865 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4866 fn, data, pfd->visited))
4867 return error_mark_node;
4868 break;
4870 case METHOD_TYPE:
4871 /* Since we're not going to walk subtrees, we have to do this
4872 explicitly here. */
4873 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4874 pfd->visited))
4875 return error_mark_node;
4876 /* Fall through. */
4878 case FUNCTION_TYPE:
4879 /* Check the return type. */
4880 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4881 return error_mark_node;
4883 /* Check the parameter types. Since default arguments are not
4884 instantiated until they are needed, the TYPE_ARG_TYPES may
4885 contain expressions that involve template parameters. But,
4886 no-one should be looking at them yet. And, once they're
4887 instantiated, they don't contain template parameters, so
4888 there's no point in looking at them then, either. */
4890 tree parm;
4892 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4893 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4894 pfd->visited))
4895 return error_mark_node;
4897 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4898 want walk_tree walking into them itself. */
4899 *walk_subtrees = 0;
4901 break;
4903 case TYPEOF_TYPE:
4904 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4905 pfd->visited))
4906 return error_mark_node;
4907 break;
4909 case FUNCTION_DECL:
4910 case VAR_DECL:
4911 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4912 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4913 pfd->visited))
4914 return error_mark_node;
4915 /* Fall through. */
4917 case PARM_DECL:
4918 case CONST_DECL:
4919 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4920 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4921 pfd->visited))
4922 return error_mark_node;
4923 if (DECL_CONTEXT (t)
4924 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4925 pfd->visited))
4926 return error_mark_node;
4927 break;
4929 case BOUND_TEMPLATE_TEMPLATE_PARM:
4930 /* Record template parameters such as `T' inside `TT<T>'. */
4931 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4932 return error_mark_node;
4933 /* Fall through. */
4935 case TEMPLATE_TEMPLATE_PARM:
4936 case TEMPLATE_TYPE_PARM:
4937 case TEMPLATE_PARM_INDEX:
4938 if (fn && (*fn)(t, data))
4939 return error_mark_node;
4940 else if (!fn)
4941 return error_mark_node;
4942 break;
4944 case TEMPLATE_DECL:
4945 /* A template template parameter is encountered. */
4946 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4947 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4948 return error_mark_node;
4950 /* Already substituted template template parameter */
4951 *walk_subtrees = 0;
4952 break;
4954 case TYPENAME_TYPE:
4955 if (!fn
4956 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4957 data, pfd->visited))
4958 return error_mark_node;
4959 break;
4961 case CONSTRUCTOR:
4962 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4963 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4964 (TREE_TYPE (t)), fn, data,
4965 pfd->visited))
4966 return error_mark_node;
4967 break;
4969 case INDIRECT_REF:
4970 case COMPONENT_REF:
4971 /* If there's no type, then this thing must be some expression
4972 involving template parameters. */
4973 if (!fn && !TREE_TYPE (t))
4974 return error_mark_node;
4975 break;
4977 case MODOP_EXPR:
4978 case CAST_EXPR:
4979 case REINTERPRET_CAST_EXPR:
4980 case CONST_CAST_EXPR:
4981 case STATIC_CAST_EXPR:
4982 case DYNAMIC_CAST_EXPR:
4983 case ARROW_EXPR:
4984 case DOTSTAR_EXPR:
4985 case TYPEID_EXPR:
4986 case PSEUDO_DTOR_EXPR:
4987 if (!fn)
4988 return error_mark_node;
4989 break;
4991 case BASELINK:
4992 /* If we do not handle this case specially, we end up walking
4993 the BINFO hierarchy, which is circular, and therefore
4994 confuses walk_tree. */
4995 *walk_subtrees = 0;
4996 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4997 pfd->visited))
4998 return error_mark_node;
4999 break;
5001 default:
5002 break;
5005 /* We didn't find any template parameters we liked. */
5006 return NULL_TREE;
5009 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5010 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5011 call FN with the parameter and the DATA.
5012 If FN returns nonzero, the iteration is terminated, and
5013 for_each_template_parm returns 1. Otherwise, the iteration
5014 continues. If FN never returns a nonzero value, the value
5015 returned by for_each_template_parm is 0. If FN is NULL, it is
5016 considered to be the function which always returns 1. */
5018 static int
5019 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5020 struct pointer_set_t *visited)
5022 struct pair_fn_data pfd;
5023 int result;
5025 /* Set up. */
5026 pfd.fn = fn;
5027 pfd.data = data;
5029 /* Walk the tree. (Conceptually, we would like to walk without
5030 duplicates, but for_each_template_parm_r recursively calls
5031 for_each_template_parm, so we would need to reorganize a fair
5032 bit to use walk_tree_without_duplicates, so we keep our own
5033 visited list.) */
5034 if (visited)
5035 pfd.visited = visited;
5036 else
5037 pfd.visited = pointer_set_create ();
5038 result = walk_tree (&t,
5039 for_each_template_parm_r,
5040 &pfd,
5041 pfd.visited) != NULL_TREE;
5043 /* Clean up. */
5044 if (!visited)
5046 pointer_set_destroy (pfd.visited);
5047 pfd.visited = 0;
5050 return result;
5053 /* Returns true if T depends on any template parameter. */
5056 uses_template_parms (tree t)
5058 bool dependent_p;
5059 int saved_processing_template_decl;
5061 saved_processing_template_decl = processing_template_decl;
5062 if (!saved_processing_template_decl)
5063 processing_template_decl = 1;
5064 if (TYPE_P (t))
5065 dependent_p = dependent_type_p (t);
5066 else if (TREE_CODE (t) == TREE_VEC)
5067 dependent_p = any_dependent_template_arguments_p (t);
5068 else if (TREE_CODE (t) == TREE_LIST)
5069 dependent_p = (uses_template_parms (TREE_VALUE (t))
5070 || uses_template_parms (TREE_CHAIN (t)));
5071 else if (TREE_CODE (t) == TYPE_DECL)
5072 dependent_p = dependent_type_p (TREE_TYPE (t));
5073 else if (DECL_P (t)
5074 || EXPR_P (t)
5075 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5076 || TREE_CODE (t) == OVERLOAD
5077 || TREE_CODE (t) == BASELINK
5078 || TREE_CODE (t) == IDENTIFIER_NODE
5079 || CONSTANT_CLASS_P (t))
5080 dependent_p = (type_dependent_expression_p (t)
5081 || value_dependent_expression_p (t));
5082 else
5084 gcc_assert (t == error_mark_node);
5085 dependent_p = false;
5088 processing_template_decl = saved_processing_template_decl;
5090 return dependent_p;
5093 /* Returns true if T depends on any template parameter with level LEVEL. */
5096 uses_template_parms_level (tree t, int level)
5098 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5101 static int tinst_depth;
5102 extern int max_tinst_depth;
5103 #ifdef GATHER_STATISTICS
5104 int depth_reached;
5105 #endif
5106 static int tinst_level_tick;
5107 static int last_template_error_tick;
5109 /* We're starting to instantiate D; record the template instantiation context
5110 for diagnostics and to restore it later. */
5113 push_tinst_level (tree d)
5115 tree new;
5117 if (tinst_depth >= max_tinst_depth)
5119 /* If the instantiation in question still has unbound template parms,
5120 we don't really care if we can't instantiate it, so just return.
5121 This happens with base instantiation for implicit `typename'. */
5122 if (uses_template_parms (d))
5123 return 0;
5125 last_template_error_tick = tinst_level_tick;
5126 error ("template instantiation depth exceeds maximum of %d (use "
5127 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5128 max_tinst_depth, d);
5130 print_instantiation_context ();
5132 return 0;
5135 new = make_node (TINST_LEVEL);
5136 TINST_DECL (new) = d;
5137 TINST_LOCATION (new) = input_location;
5138 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5139 TREE_CHAIN (new) = current_tinst_level;
5140 current_tinst_level = new;
5142 ++tinst_depth;
5143 #ifdef GATHER_STATISTICS
5144 if (tinst_depth > depth_reached)
5145 depth_reached = tinst_depth;
5146 #endif
5148 ++tinst_level_tick;
5149 return 1;
5152 /* We're done instantiating this template; return to the instantiation
5153 context. */
5155 void
5156 pop_tinst_level (void)
5158 tree old = current_tinst_level;
5160 /* Restore the filename and line number stashed away when we started
5161 this instantiation. */
5162 input_location = TINST_LOCATION (old);
5163 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5164 current_tinst_level = TREE_CHAIN (old);
5165 --tinst_depth;
5166 ++tinst_level_tick;
5169 /* We're instantiating a deferred template; restore the template
5170 instantiation context in which the instantiation was requested, which
5171 is one step out from LEVEL. */
5173 static void
5174 reopen_tinst_level (tree level)
5176 tree t;
5178 tinst_depth = 0;
5179 for (t = level; t; t = TREE_CHAIN (t))
5180 ++tinst_depth;
5182 current_tinst_level = level;
5183 pop_tinst_level ();
5186 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5187 vector of template arguments, as for tsubst.
5189 Returns an appropriate tsubst'd friend declaration. */
5191 static tree
5192 tsubst_friend_function (tree decl, tree args)
5194 tree new_friend;
5196 if (TREE_CODE (decl) == FUNCTION_DECL
5197 && DECL_TEMPLATE_INSTANTIATION (decl)
5198 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5199 /* This was a friend declared with an explicit template
5200 argument list, e.g.:
5202 friend void f<>(T);
5204 to indicate that f was a template instantiation, not a new
5205 function declaration. Now, we have to figure out what
5206 instantiation of what template. */
5208 tree template_id, arglist, fns;
5209 tree new_args;
5210 tree tmpl;
5211 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5213 /* Friend functions are looked up in the containing namespace scope.
5214 We must enter that scope, to avoid finding member functions of the
5215 current cless with same name. */
5216 push_nested_namespace (ns);
5217 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5218 tf_error | tf_warning, NULL_TREE);
5219 pop_nested_namespace (ns);
5220 arglist = tsubst (DECL_TI_ARGS (decl), args,
5221 tf_error | tf_warning, NULL_TREE);
5222 template_id = lookup_template_function (fns, arglist);
5224 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5225 tmpl = determine_specialization (template_id, new_friend,
5226 &new_args,
5227 /*need_member_template=*/0,
5228 TREE_VEC_LENGTH (args),
5229 tsk_none);
5230 return instantiate_template (tmpl, new_args, tf_error);
5233 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5235 /* The NEW_FRIEND will look like an instantiation, to the
5236 compiler, but is not an instantiation from the point of view of
5237 the language. For example, we might have had:
5239 template <class T> struct S {
5240 template <class U> friend void f(T, U);
5243 Then, in S<int>, template <class U> void f(int, U) is not an
5244 instantiation of anything. */
5245 if (new_friend == error_mark_node)
5246 return error_mark_node;
5248 DECL_USE_TEMPLATE (new_friend) = 0;
5249 if (TREE_CODE (decl) == TEMPLATE_DECL)
5251 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5252 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5253 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5256 /* The mangled name for the NEW_FRIEND is incorrect. The function
5257 is not a template instantiation and should not be mangled like
5258 one. Therefore, we forget the mangling here; we'll recompute it
5259 later if we need it. */
5260 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5262 SET_DECL_RTL (new_friend, NULL_RTX);
5263 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5266 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5268 tree old_decl;
5269 tree new_friend_template_info;
5270 tree new_friend_result_template_info;
5271 tree ns;
5272 int new_friend_is_defn;
5274 /* We must save some information from NEW_FRIEND before calling
5275 duplicate decls since that function will free NEW_FRIEND if
5276 possible. */
5277 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5278 new_friend_is_defn =
5279 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5280 (template_for_substitution (new_friend)))
5281 != NULL_TREE);
5282 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5284 /* This declaration is a `primary' template. */
5285 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5287 new_friend_result_template_info
5288 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5290 else
5291 new_friend_result_template_info = NULL_TREE;
5293 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5294 if (new_friend_is_defn)
5295 DECL_INITIAL (new_friend) = error_mark_node;
5297 /* Inside pushdecl_namespace_level, we will push into the
5298 current namespace. However, the friend function should go
5299 into the namespace of the template. */
5300 ns = decl_namespace_context (new_friend);
5301 push_nested_namespace (ns);
5302 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5303 pop_nested_namespace (ns);
5305 if (old_decl != new_friend)
5307 /* This new friend declaration matched an existing
5308 declaration. For example, given:
5310 template <class T> void f(T);
5311 template <class U> class C {
5312 template <class T> friend void f(T) {}
5315 the friend declaration actually provides the definition
5316 of `f', once C has been instantiated for some type. So,
5317 old_decl will be the out-of-class template declaration,
5318 while new_friend is the in-class definition.
5320 But, if `f' was called before this point, the
5321 instantiation of `f' will have DECL_TI_ARGS corresponding
5322 to `T' but not to `U', references to which might appear
5323 in the definition of `f'. Previously, the most general
5324 template for an instantiation of `f' was the out-of-class
5325 version; now it is the in-class version. Therefore, we
5326 run through all specialization of `f', adding to their
5327 DECL_TI_ARGS appropriately. In particular, they need a
5328 new set of outer arguments, corresponding to the
5329 arguments for this class instantiation.
5331 The same situation can arise with something like this:
5333 friend void f(int);
5334 template <class T> class C {
5335 friend void f(T) {}
5338 when `C<int>' is instantiated. Now, `f(int)' is defined
5339 in the class. */
5341 if (!new_friend_is_defn)
5342 /* On the other hand, if the in-class declaration does
5343 *not* provide a definition, then we don't want to alter
5344 existing definitions. We can just leave everything
5345 alone. */
5347 else
5349 /* Overwrite whatever template info was there before, if
5350 any, with the new template information pertaining to
5351 the declaration. */
5352 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5354 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5355 reregister_specialization (new_friend,
5356 most_general_template (old_decl),
5357 old_decl);
5358 else
5360 tree t;
5361 tree new_friend_args;
5363 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5364 = new_friend_result_template_info;
5366 new_friend_args = TI_ARGS (new_friend_template_info);
5367 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5368 t != NULL_TREE;
5369 t = TREE_CHAIN (t))
5371 tree spec = TREE_VALUE (t);
5373 DECL_TI_ARGS (spec)
5374 = add_outermost_template_args (new_friend_args,
5375 DECL_TI_ARGS (spec));
5378 /* Now, since specializations are always supposed to
5379 hang off of the most general template, we must move
5380 them. */
5381 t = most_general_template (old_decl);
5382 if (t != old_decl)
5384 DECL_TEMPLATE_SPECIALIZATIONS (t)
5385 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5386 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5387 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5392 /* The information from NEW_FRIEND has been merged into OLD_DECL
5393 by duplicate_decls. */
5394 new_friend = old_decl;
5397 else
5399 tree context = DECL_CONTEXT (new_friend);
5400 bool dependent_p;
5402 /* In the code
5403 template <class T> class C {
5404 template <class U> friend void C1<U>::f (); // case 1
5405 friend void C2<T>::f (); // case 2
5407 we only need to make sure CONTEXT is a complete type for
5408 case 2. To distinguish between the two cases, we note that
5409 CONTEXT of case 1 remains dependent type after tsubst while
5410 this isn't true for case 2. */
5411 ++processing_template_decl;
5412 dependent_p = dependent_type_p (context);
5413 --processing_template_decl;
5415 if (!dependent_p
5416 && !complete_type_or_else (context, NULL_TREE))
5417 return error_mark_node;
5419 if (COMPLETE_TYPE_P (context))
5421 /* Check to see that the declaration is really present, and,
5422 possibly obtain an improved declaration. */
5423 tree fn = check_classfn (context,
5424 new_friend, NULL_TREE);
5426 if (fn)
5427 new_friend = fn;
5431 return new_friend;
5434 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5435 template arguments, as for tsubst.
5437 Returns an appropriate tsubst'd friend type or error_mark_node on
5438 failure. */
5440 static tree
5441 tsubst_friend_class (tree friend_tmpl, tree args)
5443 tree friend_type;
5444 tree tmpl;
5445 tree context;
5447 context = DECL_CONTEXT (friend_tmpl);
5449 if (context)
5451 if (TREE_CODE (context) == NAMESPACE_DECL)
5452 push_nested_namespace (context);
5453 else
5454 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5457 /* Look for a class template declaration. We look for hidden names
5458 because two friend declarations of the same template are the
5459 same. For example, in:
5461 struct A {
5462 template <typename> friend class F;
5464 template <typename> struct B {
5465 template <typename> friend class F;
5468 both F templates are the same. */
5469 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5470 /*block_p=*/true, 0,
5471 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
5473 /* But, if we don't find one, it might be because we're in a
5474 situation like this:
5476 template <class T>
5477 struct S {
5478 template <class U>
5479 friend struct S;
5482 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5483 for `S<int>', not the TEMPLATE_DECL. */
5484 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5486 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5487 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5490 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5492 /* The friend template has already been declared. Just
5493 check to see that the declarations match, and install any new
5494 default parameters. We must tsubst the default parameters,
5495 of course. We only need the innermost template parameters
5496 because that is all that redeclare_class_template will look
5497 at. */
5498 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5499 > TMPL_ARGS_DEPTH (args))
5501 tree parms;
5502 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5503 args, tf_error | tf_warning);
5504 redeclare_class_template (TREE_TYPE (tmpl), parms);
5507 friend_type = TREE_TYPE (tmpl);
5509 else
5511 /* The friend template has not already been declared. In this
5512 case, the instantiation of the template class will cause the
5513 injection of this template into the global scope. */
5514 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5516 /* The new TMPL is not an instantiation of anything, so we
5517 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5518 the new type because that is supposed to be the corresponding
5519 template decl, i.e., TMPL. */
5520 DECL_USE_TEMPLATE (tmpl) = 0;
5521 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5522 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5523 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5524 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5526 /* Inject this template into the global scope. */
5527 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5530 if (context)
5532 if (TREE_CODE (context) == NAMESPACE_DECL)
5533 pop_nested_namespace (context);
5534 else
5535 pop_nested_class ();
5538 return friend_type;
5541 /* Returns zero if TYPE cannot be completed later due to circularity.
5542 Otherwise returns one. */
5544 static int
5545 can_complete_type_without_circularity (tree type)
5547 if (type == NULL_TREE || type == error_mark_node)
5548 return 0;
5549 else if (COMPLETE_TYPE_P (type))
5550 return 1;
5551 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5552 return can_complete_type_without_circularity (TREE_TYPE (type));
5553 else if (CLASS_TYPE_P (type)
5554 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5555 return 0;
5556 else
5557 return 1;
5560 tree
5561 instantiate_class_template (tree type)
5563 tree template, args, pattern, t, member;
5564 tree typedecl;
5565 tree pbinfo;
5566 tree base_list;
5568 if (type == error_mark_node)
5569 return error_mark_node;
5571 if (TYPE_BEING_DEFINED (type)
5572 || COMPLETE_TYPE_P (type)
5573 || dependent_type_p (type))
5574 return type;
5576 /* Figure out which template is being instantiated. */
5577 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5578 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5580 /* Determine what specialization of the original template to
5581 instantiate. */
5582 t = most_specialized_class (type, template);
5583 if (t == error_mark_node)
5585 TYPE_BEING_DEFINED (type) = 1;
5586 return error_mark_node;
5588 else if (t)
5590 /* This TYPE is actually an instantiation of a partial
5591 specialization. We replace the innermost set of ARGS with
5592 the arguments appropriate for substitution. For example,
5593 given:
5595 template <class T> struct S {};
5596 template <class T> struct S<T*> {};
5598 and supposing that we are instantiating S<int*>, ARGS will
5599 presently be {int*} -- but we need {int}. */
5600 pattern = TREE_TYPE (t);
5601 args = TREE_PURPOSE (t);
5603 else
5605 pattern = TREE_TYPE (template);
5606 args = CLASSTYPE_TI_ARGS (type);
5609 /* If the template we're instantiating is incomplete, then clearly
5610 there's nothing we can do. */
5611 if (!COMPLETE_TYPE_P (pattern))
5612 return type;
5614 /* If we've recursively instantiated too many templates, stop. */
5615 if (! push_tinst_level (type))
5616 return type;
5618 /* Now we're really doing the instantiation. Mark the type as in
5619 the process of being defined. */
5620 TYPE_BEING_DEFINED (type) = 1;
5622 /* We may be in the middle of deferred access check. Disable
5623 it now. */
5624 push_deferring_access_checks (dk_no_deferred);
5626 push_to_top_level ();
5628 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5630 /* Set the input location to the template definition. This is needed
5631 if tsubsting causes an error. */
5632 typedecl = TYPE_MAIN_DECL (type);
5633 input_location = DECL_SOURCE_LOCATION (typedecl);
5634 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5636 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5637 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5638 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5639 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5640 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5641 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5642 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5643 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5644 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5645 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5646 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5647 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5648 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5649 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5650 if (ANON_AGGR_TYPE_P (pattern))
5651 SET_ANON_AGGR_TYPE_P (type);
5652 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5654 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5655 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5658 pbinfo = TYPE_BINFO (pattern);
5660 /* We should never instantiate a nested class before its enclosing
5661 class; we need to look up the nested class by name before we can
5662 instantiate it, and that lookup should instantiate the enclosing
5663 class. */
5664 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5665 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5666 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5668 base_list = NULL_TREE;
5669 if (BINFO_N_BASE_BINFOS (pbinfo))
5671 tree pbase_binfo;
5672 tree context = TYPE_CONTEXT (type);
5673 tree pushed_scope;
5674 int i;
5676 /* We must enter the scope containing the type, as that is where
5677 the accessibility of types named in dependent bases are
5678 looked up from. */
5679 pushed_scope = push_scope (context ? context : global_namespace);
5681 /* Substitute into each of the bases to determine the actual
5682 basetypes. */
5683 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5685 tree base;
5686 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5688 /* Substitute to figure out the base class. */
5689 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5690 if (base == error_mark_node)
5691 continue;
5693 base_list = tree_cons (access, base, base_list);
5694 if (BINFO_VIRTUAL_P (pbase_binfo))
5695 TREE_TYPE (base_list) = integer_type_node;
5698 /* The list is now in reverse order; correct that. */
5699 base_list = nreverse (base_list);
5701 if (pushed_scope)
5702 pop_scope (pushed_scope);
5704 /* Now call xref_basetypes to set up all the base-class
5705 information. */
5706 xref_basetypes (type, base_list);
5709 /* Now that our base classes are set up, enter the scope of the
5710 class, so that name lookups into base classes, etc. will work
5711 correctly. This is precisely analogous to what we do in
5712 begin_class_definition when defining an ordinary non-template
5713 class. */
5714 pushclass (type);
5716 /* Now members are processed in the order of declaration. */
5717 for (member = CLASSTYPE_DECL_LIST (pattern);
5718 member; member = TREE_CHAIN (member))
5720 tree t = TREE_VALUE (member);
5722 if (TREE_PURPOSE (member))
5724 if (TYPE_P (t))
5726 /* Build new CLASSTYPE_NESTED_UTDS. */
5728 tree tag = t;
5729 tree name = TYPE_IDENTIFIER (tag);
5730 tree newtag;
5731 bool class_template_p;
5733 class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE
5734 && TYPE_LANG_SPECIFIC (tag)
5735 && CLASSTYPE_IS_TEMPLATE (tag));
5736 /* If the member is a class template, then -- even after
5737 substitution -- there may be dependent types in the
5738 template argument list for the class. We increment
5739 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5740 that function will assume that no types are dependent
5741 when outside of a template. */
5742 if (class_template_p)
5743 ++processing_template_decl;
5744 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5745 if (class_template_p)
5746 --processing_template_decl;
5747 if (newtag == error_mark_node)
5748 continue;
5750 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5752 if (class_template_p)
5753 /* Unfortunately, lookup_template_class sets
5754 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5755 instantiation (i.e., for the type of a member
5756 template class nested within a template class.)
5757 This behavior is required for
5758 maybe_process_partial_specialization to work
5759 correctly, but is not accurate in this case;
5760 the TAG is not an instantiation of anything.
5761 (The corresponding TEMPLATE_DECL is an
5762 instantiation, but the TYPE is not.) */
5763 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5765 /* Now, we call pushtag to put this NEWTAG into the scope of
5766 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5767 pushtag calling push_template_decl. We don't have to do
5768 this for enums because it will already have been done in
5769 tsubst_enum. */
5770 if (name)
5771 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5772 pushtag (name, newtag, /*tag_scope=*/ts_current);
5775 else if (TREE_CODE (t) == FUNCTION_DECL
5776 || DECL_FUNCTION_TEMPLATE_P (t))
5778 /* Build new TYPE_METHODS. */
5779 tree r;
5781 if (TREE_CODE (t) == TEMPLATE_DECL)
5782 ++processing_template_decl;
5783 r = tsubst (t, args, tf_error, NULL_TREE);
5784 if (TREE_CODE (t) == TEMPLATE_DECL)
5785 --processing_template_decl;
5786 set_current_access_from_decl (r);
5787 grok_special_member_properties (r);
5788 finish_member_declaration (r);
5790 else
5792 /* Build new TYPE_FIELDS. */
5794 if (TREE_CODE (t) != CONST_DECL)
5796 tree r;
5798 /* The the file and line for this declaration, to
5799 assist in error message reporting. Since we
5800 called push_tinst_level above, we don't need to
5801 restore these. */
5802 input_location = DECL_SOURCE_LOCATION (t);
5804 if (TREE_CODE (t) == TEMPLATE_DECL)
5805 ++processing_template_decl;
5806 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5807 if (TREE_CODE (t) == TEMPLATE_DECL)
5808 --processing_template_decl;
5809 if (TREE_CODE (r) == VAR_DECL)
5811 /* In [temp.inst]:
5813 [t]he initialization (and any associated
5814 side-effects) of a static data member does
5815 not occur unless the static data member is
5816 itself used in a way that requires the
5817 definition of the static data member to
5818 exist.
5820 Therefore, we do not substitute into the
5821 initialized for the static data member here. */
5822 finish_static_data_member_decl
5823 (r,
5824 /*init=*/NULL_TREE,
5825 /*init_const_expr_p=*/false,
5826 /*asmspec_tree=*/NULL_TREE,
5827 /*flags=*/0);
5828 if (DECL_INITIALIZED_IN_CLASS_P (r))
5829 check_static_variable_definition (r, TREE_TYPE (r));
5831 else if (TREE_CODE (r) == FIELD_DECL)
5833 /* Determine whether R has a valid type and can be
5834 completed later. If R is invalid, then it is
5835 replaced by error_mark_node so that it will not be
5836 added to TYPE_FIELDS. */
5837 tree rtype = TREE_TYPE (r);
5838 if (can_complete_type_without_circularity (rtype))
5839 complete_type (rtype);
5841 if (!COMPLETE_TYPE_P (rtype))
5843 cxx_incomplete_type_error (r, rtype);
5844 r = error_mark_node;
5848 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5849 such a thing will already have been added to the field
5850 list by tsubst_enum in finish_member_declaration in the
5851 CLASSTYPE_NESTED_UTDS case above. */
5852 if (!(TREE_CODE (r) == TYPE_DECL
5853 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5854 && DECL_ARTIFICIAL (r)))
5856 set_current_access_from_decl (r);
5857 finish_member_declaration (r);
5862 else
5864 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5866 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5868 tree friend_type = t;
5869 bool adjust_processing_template_decl = false;
5871 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5873 /* template <class T> friend class C; */
5874 friend_type = tsubst_friend_class (friend_type, args);
5875 adjust_processing_template_decl = true;
5877 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5879 /* template <class T> friend class C::D; */
5880 friend_type = tsubst (friend_type, args,
5881 tf_error | tf_warning, NULL_TREE);
5882 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5883 friend_type = TREE_TYPE (friend_type);
5884 adjust_processing_template_decl = true;
5886 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5888 /* This could be either
5890 friend class T::C;
5892 when dependent_type_p is false or
5894 template <class U> friend class T::C;
5896 otherwise. */
5897 friend_type = tsubst (friend_type, args,
5898 tf_error | tf_warning, NULL_TREE);
5899 /* Bump processing_template_decl for correct
5900 dependent_type_p calculation. */
5901 ++processing_template_decl;
5902 if (dependent_type_p (friend_type))
5903 adjust_processing_template_decl = true;
5904 --processing_template_decl;
5906 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5907 && hidden_name_p (TYPE_NAME (friend_type)))
5909 /* friend class C;
5911 where C hasn't been declared yet. Let's lookup name
5912 from namespace scope directly, bypassing any name that
5913 come from dependent base class. */
5914 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5916 /* The call to xref_tag_from_type does injection for friend
5917 classes. */
5918 push_nested_namespace (ns);
5919 friend_type =
5920 xref_tag_from_type (friend_type, NULL_TREE,
5921 /*tag_scope=*/ts_current);
5922 pop_nested_namespace (ns);
5924 else if (uses_template_parms (friend_type))
5925 /* friend class C<T>; */
5926 friend_type = tsubst (friend_type, args,
5927 tf_error | tf_warning, NULL_TREE);
5928 /* Otherwise it's
5930 friend class C;
5932 where C is already declared or
5934 friend class C<int>;
5936 We don't have to do anything in these cases. */
5938 if (adjust_processing_template_decl)
5939 /* Trick make_friend_class into realizing that the friend
5940 we're adding is a template, not an ordinary class. It's
5941 important that we use make_friend_class since it will
5942 perform some error-checking and output cross-reference
5943 information. */
5944 ++processing_template_decl;
5946 if (friend_type != error_mark_node)
5947 make_friend_class (type, friend_type, /*complain=*/false);
5949 if (adjust_processing_template_decl)
5950 --processing_template_decl;
5952 else
5954 /* Build new DECL_FRIENDLIST. */
5955 tree r;
5957 /* The the file and line for this declaration, to
5958 assist in error message reporting. Since we
5959 called push_tinst_level above, we don't need to
5960 restore these. */
5961 input_location = DECL_SOURCE_LOCATION (t);
5963 if (TREE_CODE (t) == TEMPLATE_DECL)
5965 ++processing_template_decl;
5966 push_deferring_access_checks (dk_no_check);
5969 r = tsubst_friend_function (t, args);
5970 add_friend (type, r, /*complain=*/false);
5971 if (TREE_CODE (t) == TEMPLATE_DECL)
5973 pop_deferring_access_checks ();
5974 --processing_template_decl;
5980 /* Set the file and line number information to whatever is given for
5981 the class itself. This puts error messages involving generated
5982 implicit functions at a predictable point, and the same point
5983 that would be used for non-template classes. */
5984 input_location = DECL_SOURCE_LOCATION (typedecl);
5986 unreverse_member_declarations (type);
5987 finish_struct_1 (type);
5988 TYPE_BEING_DEFINED (type) = 0;
5990 /* Now that the class is complete, instantiate default arguments for
5991 any member functions. We don't do this earlier because the
5992 default arguments may reference members of the class. */
5993 if (!PRIMARY_TEMPLATE_P (template))
5994 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5995 if (TREE_CODE (t) == FUNCTION_DECL
5996 /* Implicitly generated member functions will not have template
5997 information; they are not instantiations, but instead are
5998 created "fresh" for each instantiation. */
5999 && DECL_TEMPLATE_INFO (t))
6000 tsubst_default_arguments (t);
6002 popclass ();
6003 pop_from_top_level ();
6004 pop_deferring_access_checks ();
6005 pop_tinst_level ();
6007 /* The vtable for a template class can be emitted in any translation
6008 unit in which the class is instantiated. When there is no key
6009 method, however, finish_struct_1 will already have added TYPE to
6010 the keyed_classes list. */
6011 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6012 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6014 return type;
6017 static tree
6018 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6020 tree r;
6022 if (!t)
6023 r = t;
6024 else if (TYPE_P (t))
6025 r = tsubst (t, args, complain, in_decl);
6026 else
6028 r = tsubst_expr (t, args, complain, in_decl);
6029 r = fold_non_dependent_expr (r);
6031 return r;
6034 /* Substitute ARGS into the vector or list of template arguments T. */
6036 static tree
6037 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6039 int len = TREE_VEC_LENGTH (t);
6040 int need_new = 0, i;
6041 tree *elts = alloca (len * sizeof (tree));
6043 for (i = 0; i < len; i++)
6045 tree orig_arg = TREE_VEC_ELT (t, i);
6046 tree new_arg;
6048 if (TREE_CODE (orig_arg) == TREE_VEC)
6049 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
6050 else
6051 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
6053 if (new_arg == error_mark_node)
6054 return error_mark_node;
6056 elts[i] = new_arg;
6057 if (new_arg != orig_arg)
6058 need_new = 1;
6061 if (!need_new)
6062 return t;
6064 t = make_tree_vec (len);
6065 for (i = 0; i < len; i++)
6066 TREE_VEC_ELT (t, i) = elts[i];
6068 return t;
6071 /* Return the result of substituting ARGS into the template parameters
6072 given by PARMS. If there are m levels of ARGS and m + n levels of
6073 PARMS, then the result will contain n levels of PARMS. For
6074 example, if PARMS is `template <class T> template <class U>
6075 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6076 result will be `template <int*, double, class V>'. */
6078 static tree
6079 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6081 tree r = NULL_TREE;
6082 tree* new_parms;
6084 /* When substituting into a template, we must set
6085 PROCESSING_TEMPLATE_DECL as the template parameters may be
6086 dependent if they are based on one-another, and the dependency
6087 predicates are short-circuit outside of templates. */
6088 ++processing_template_decl;
6090 for (new_parms = &r;
6091 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6092 new_parms = &(TREE_CHAIN (*new_parms)),
6093 parms = TREE_CHAIN (parms))
6095 tree new_vec =
6096 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6097 int i;
6099 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6101 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6102 tree default_value = TREE_PURPOSE (tuple);
6103 tree parm_decl = TREE_VALUE (tuple);
6105 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6106 if (TREE_CODE (parm_decl) == PARM_DECL
6107 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6108 parm_decl = error_mark_node;
6109 default_value = tsubst_template_arg (default_value, args,
6110 complain, NULL_TREE);
6112 tuple = build_tree_list (default_value, parm_decl);
6113 TREE_VEC_ELT (new_vec, i) = tuple;
6116 *new_parms =
6117 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6118 - TMPL_ARGS_DEPTH (args)),
6119 new_vec, NULL_TREE);
6122 --processing_template_decl;
6124 return r;
6127 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6128 type T. If T is not an aggregate or enumeration type, it is
6129 handled as if by tsubst. IN_DECL is as for tsubst. If
6130 ENTERING_SCOPE is nonzero, T is the context for a template which
6131 we are presently tsubst'ing. Return the substituted value. */
6133 static tree
6134 tsubst_aggr_type (tree t,
6135 tree args,
6136 tsubst_flags_t complain,
6137 tree in_decl,
6138 int entering_scope)
6140 if (t == NULL_TREE)
6141 return NULL_TREE;
6143 switch (TREE_CODE (t))
6145 case RECORD_TYPE:
6146 if (TYPE_PTRMEMFUNC_P (t))
6147 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6149 /* Else fall through. */
6150 case ENUMERAL_TYPE:
6151 case UNION_TYPE:
6152 if (TYPE_TEMPLATE_INFO (t))
6154 tree argvec;
6155 tree context;
6156 tree r;
6157 bool saved_skip_evaluation;
6159 /* In "sizeof(X<I>)" we need to evaluate "I". */
6160 saved_skip_evaluation = skip_evaluation;
6161 skip_evaluation = false;
6163 /* First, determine the context for the type we are looking
6164 up. */
6165 context = TYPE_CONTEXT (t);
6166 if (context)
6168 context = tsubst_aggr_type (context, args, complain,
6169 in_decl, /*entering_scope=*/1);
6170 /* If context is a nested class inside a class template,
6171 it may still need to be instantiated (c++/33959). */
6172 if (TYPE_P (context))
6173 complete_type (context);
6176 /* Then, figure out what arguments are appropriate for the
6177 type we are trying to find. For example, given:
6179 template <class T> struct S;
6180 template <class T, class U> void f(T, U) { S<U> su; }
6182 and supposing that we are instantiating f<int, double>,
6183 then our ARGS will be {int, double}, but, when looking up
6184 S we only want {double}. */
6185 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6186 complain, in_decl);
6187 if (argvec == error_mark_node)
6188 r = error_mark_node;
6189 else
6191 r = lookup_template_class (t, argvec, in_decl, context,
6192 entering_scope, complain);
6193 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6196 skip_evaluation = saved_skip_evaluation;
6198 return r;
6200 else
6201 /* This is not a template type, so there's nothing to do. */
6202 return t;
6204 default:
6205 return tsubst (t, args, complain, in_decl);
6209 /* Substitute into the default argument ARG (a default argument for
6210 FN), which has the indicated TYPE. */
6212 tree
6213 tsubst_default_argument (tree fn, tree type, tree arg)
6215 tree saved_class_ptr = NULL_TREE;
6216 tree saved_class_ref = NULL_TREE;
6218 /* This default argument came from a template. Instantiate the
6219 default argument here, not in tsubst. In the case of
6220 something like:
6222 template <class T>
6223 struct S {
6224 static T t();
6225 void f(T = t());
6228 we must be careful to do name lookup in the scope of S<T>,
6229 rather than in the current class. */
6230 push_access_scope (fn);
6231 /* The "this" pointer is not valid in a default argument. */
6232 if (cfun)
6234 saved_class_ptr = current_class_ptr;
6235 cp_function_chain->x_current_class_ptr = NULL_TREE;
6236 saved_class_ref = current_class_ref;
6237 cp_function_chain->x_current_class_ref = NULL_TREE;
6240 push_deferring_access_checks(dk_no_deferred);
6241 /* The default argument expression may cause implicitly defined
6242 member functions to be synthesized, which will result in garbage
6243 collection. We must treat this situation as if we were within
6244 the body of function so as to avoid collecting live data on the
6245 stack. */
6246 ++function_depth;
6247 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6248 tf_error | tf_warning, NULL_TREE);
6249 --function_depth;
6250 pop_deferring_access_checks();
6252 /* Restore the "this" pointer. */
6253 if (cfun)
6255 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6256 cp_function_chain->x_current_class_ref = saved_class_ref;
6259 pop_access_scope (fn);
6261 /* Make sure the default argument is reasonable. */
6262 arg = check_default_argument (type, arg);
6264 return arg;
6267 /* Substitute into all the default arguments for FN. */
6269 static void
6270 tsubst_default_arguments (tree fn)
6272 tree arg;
6273 tree tmpl_args;
6275 tmpl_args = DECL_TI_ARGS (fn);
6277 /* If this function is not yet instantiated, we certainly don't need
6278 its default arguments. */
6279 if (uses_template_parms (tmpl_args))
6280 return;
6282 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6283 arg;
6284 arg = TREE_CHAIN (arg))
6285 if (TREE_PURPOSE (arg))
6286 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6287 TREE_VALUE (arg),
6288 TREE_PURPOSE (arg));
6291 /* Substitute the ARGS into the T, which is a _DECL. Return the
6292 result of the substitution. Issue error and warning messages under
6293 control of COMPLAIN. */
6295 static tree
6296 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6298 location_t saved_loc;
6299 tree r = NULL_TREE;
6300 tree in_decl = t;
6302 /* Set the filename and linenumber to improve error-reporting. */
6303 saved_loc = input_location;
6304 input_location = DECL_SOURCE_LOCATION (t);
6306 switch (TREE_CODE (t))
6308 case TEMPLATE_DECL:
6310 /* We can get here when processing a member function template,
6311 member class template, and template template parameter of
6312 a template class. */
6313 tree decl = DECL_TEMPLATE_RESULT (t);
6314 tree spec;
6315 tree tmpl_args;
6316 tree full_args;
6318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6320 /* Template template parameter is treated here. */
6321 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6322 if (new_type == error_mark_node)
6323 return error_mark_node;
6325 r = copy_decl (t);
6326 TREE_CHAIN (r) = NULL_TREE;
6327 TREE_TYPE (r) = new_type;
6328 DECL_TEMPLATE_RESULT (r)
6329 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6330 DECL_TEMPLATE_PARMS (r)
6331 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6332 complain);
6333 TYPE_NAME (new_type) = r;
6334 break;
6337 /* We might already have an instance of this template.
6338 The ARGS are for the surrounding class type, so the
6339 full args contain the tsubst'd args for the context,
6340 plus the innermost args from the template decl. */
6341 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6342 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6343 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6344 /* Because this is a template, the arguments will still be
6345 dependent, even after substitution. If
6346 PROCESSING_TEMPLATE_DECL is not set, the dependency
6347 predicates will short-circuit. */
6348 ++processing_template_decl;
6349 full_args = tsubst_template_args (tmpl_args, args,
6350 complain, in_decl);
6351 --processing_template_decl;
6352 if (full_args == error_mark_node)
6353 return error_mark_node;
6355 /* tsubst_template_args doesn't copy the vector if
6356 nothing changed. But, *something* should have
6357 changed. */
6358 gcc_assert (full_args != tmpl_args);
6360 spec = retrieve_specialization (t, full_args,
6361 /*class_specializations_p=*/true);
6362 if (spec != NULL_TREE)
6364 r = spec;
6365 break;
6368 /* Make a new template decl. It will be similar to the
6369 original, but will record the current template arguments.
6370 We also create a new function declaration, which is just
6371 like the old one, but points to this new template, rather
6372 than the old one. */
6373 r = copy_decl (t);
6374 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6375 TREE_CHAIN (r) = NULL_TREE;
6377 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6379 if (TREE_CODE (decl) == TYPE_DECL)
6381 tree new_type;
6382 ++processing_template_decl;
6383 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6384 --processing_template_decl;
6385 if (new_type == error_mark_node)
6386 return error_mark_node;
6388 TREE_TYPE (r) = new_type;
6389 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6390 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6391 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6392 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6394 else
6396 tree new_decl;
6397 ++processing_template_decl;
6398 new_decl = tsubst (decl, args, complain, in_decl);
6399 --processing_template_decl;
6400 if (new_decl == error_mark_node)
6401 return error_mark_node;
6403 DECL_TEMPLATE_RESULT (r) = new_decl;
6404 DECL_TI_TEMPLATE (new_decl) = r;
6405 TREE_TYPE (r) = TREE_TYPE (new_decl);
6406 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6407 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6410 SET_DECL_IMPLICIT_INSTANTIATION (r);
6411 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6412 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6414 /* The template parameters for this new template are all the
6415 template parameters for the old template, except the
6416 outermost level of parameters. */
6417 DECL_TEMPLATE_PARMS (r)
6418 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6419 complain);
6421 if (PRIMARY_TEMPLATE_P (t))
6422 DECL_PRIMARY_TEMPLATE (r) = r;
6424 if (TREE_CODE (decl) != TYPE_DECL)
6425 /* Record this non-type partial instantiation. */
6426 register_specialization (r, t,
6427 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6428 false);
6430 break;
6432 case FUNCTION_DECL:
6434 tree ctx;
6435 tree argvec = NULL_TREE;
6436 tree *friends;
6437 tree gen_tmpl;
6438 tree type;
6439 int member;
6440 int args_depth;
6441 int parms_depth;
6443 /* Nobody should be tsubst'ing into non-template functions. */
6444 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6446 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6448 tree spec;
6449 bool dependent_p;
6451 /* If T is not dependent, just return it. We have to
6452 increment PROCESSING_TEMPLATE_DECL because
6453 value_dependent_expression_p assumes that nothing is
6454 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6455 ++processing_template_decl;
6456 dependent_p = value_dependent_expression_p (t);
6457 --processing_template_decl;
6458 if (!dependent_p)
6459 return t;
6461 /* Calculate the most general template of which R is a
6462 specialization, and the complete set of arguments used to
6463 specialize R. */
6464 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6465 argvec = tsubst_template_args (DECL_TI_ARGS
6466 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6467 args, complain, in_decl);
6469 /* Check to see if we already have this specialization. */
6470 spec = retrieve_specialization (gen_tmpl, argvec,
6471 /*class_specializations_p=*/false);
6473 if (spec)
6475 r = spec;
6476 break;
6479 /* We can see more levels of arguments than parameters if
6480 there was a specialization of a member template, like
6481 this:
6483 template <class T> struct S { template <class U> void f(); }
6484 template <> template <class U> void S<int>::f(U);
6486 Here, we'll be substituting into the specialization,
6487 because that's where we can find the code we actually
6488 want to generate, but we'll have enough arguments for
6489 the most general template.
6491 We also deal with the peculiar case:
6493 template <class T> struct S {
6494 template <class U> friend void f();
6496 template <class U> void f() {}
6497 template S<int>;
6498 template void f<double>();
6500 Here, the ARGS for the instantiation of will be {int,
6501 double}. But, we only need as many ARGS as there are
6502 levels of template parameters in CODE_PATTERN. We are
6503 careful not to get fooled into reducing the ARGS in
6504 situations like:
6506 template <class T> struct S { template <class U> void f(U); }
6507 template <class T> template <> void S<T>::f(int) {}
6509 which we can spot because the pattern will be a
6510 specialization in this case. */
6511 args_depth = TMPL_ARGS_DEPTH (args);
6512 parms_depth =
6513 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6514 if (args_depth > parms_depth
6515 && !DECL_TEMPLATE_SPECIALIZATION (t))
6516 args = get_innermost_template_args (args, parms_depth);
6518 else
6520 /* This special case arises when we have something like this:
6522 template <class T> struct S {
6523 friend void f<int>(int, double);
6526 Here, the DECL_TI_TEMPLATE for the friend declaration
6527 will be an IDENTIFIER_NODE. We are being called from
6528 tsubst_friend_function, and we want only to create a
6529 new decl (R) with appropriate types so that we can call
6530 determine_specialization. */
6531 gen_tmpl = NULL_TREE;
6534 if (DECL_CLASS_SCOPE_P (t))
6536 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6537 member = 2;
6538 else
6539 member = 1;
6540 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6541 complain, t, /*entering_scope=*/1);
6543 else
6545 member = 0;
6546 ctx = DECL_CONTEXT (t);
6548 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6549 if (type == error_mark_node)
6550 return error_mark_node;
6552 /* We do NOT check for matching decls pushed separately at this
6553 point, as they may not represent instantiations of this
6554 template, and in any case are considered separate under the
6555 discrete model. */
6556 r = copy_decl (t);
6557 DECL_USE_TEMPLATE (r) = 0;
6558 TREE_TYPE (r) = type;
6559 /* Clear out the mangled name and RTL for the instantiation. */
6560 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6561 SET_DECL_RTL (r, NULL_RTX);
6562 DECL_INITIAL (r) = NULL_TREE;
6563 DECL_CONTEXT (r) = ctx;
6565 if (member && DECL_CONV_FN_P (r))
6566 /* Type-conversion operator. Reconstruct the name, in
6567 case it's the name of one of the template's parameters. */
6568 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6570 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6571 complain, t);
6572 DECL_RESULT (r) = NULL_TREE;
6574 TREE_STATIC (r) = 0;
6575 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6576 DECL_EXTERNAL (r) = 1;
6577 /* If this is an instantiation of a function with internal
6578 linkage, we already know what object file linkage will be
6579 assigned to the instantiation. */
6580 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6581 DECL_DEFER_OUTPUT (r) = 0;
6582 TREE_CHAIN (r) = NULL_TREE;
6583 DECL_PENDING_INLINE_INFO (r) = 0;
6584 DECL_PENDING_INLINE_P (r) = 0;
6585 DECL_SAVED_TREE (r) = NULL_TREE;
6586 TREE_USED (r) = 0;
6587 if (DECL_CLONED_FUNCTION (r))
6589 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6590 args, complain, t);
6591 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6592 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6595 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6596 this in the special friend case mentioned above where
6597 GEN_TMPL is NULL. */
6598 if (gen_tmpl)
6600 DECL_TEMPLATE_INFO (r)
6601 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6602 SET_DECL_IMPLICIT_INSTANTIATION (r);
6603 register_specialization (r, gen_tmpl, argvec, false);
6605 /* We're not supposed to instantiate default arguments
6606 until they are called, for a template. But, for a
6607 declaration like:
6609 template <class T> void f ()
6610 { extern void g(int i = T()); }
6612 we should do the substitution when the template is
6613 instantiated. We handle the member function case in
6614 instantiate_class_template since the default arguments
6615 might refer to other members of the class. */
6616 if (!member
6617 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6618 && !uses_template_parms (argvec))
6619 tsubst_default_arguments (r);
6621 else
6622 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6624 /* Copy the list of befriending classes. */
6625 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6626 *friends;
6627 friends = &TREE_CHAIN (*friends))
6629 *friends = copy_node (*friends);
6630 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6631 args, complain,
6632 in_decl);
6635 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6637 maybe_retrofit_in_chrg (r);
6638 if (DECL_CONSTRUCTOR_P (r))
6639 grok_ctor_properties (ctx, r);
6640 /* If this is an instantiation of a member template, clone it.
6641 If it isn't, that'll be handled by
6642 clone_constructors_and_destructors. */
6643 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6644 clone_function_decl (r, /*update_method_vec_p=*/0);
6646 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6647 grok_op_properties (r, (complain & tf_error) != 0);
6649 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6650 SET_DECL_FRIEND_CONTEXT (r,
6651 tsubst (DECL_FRIEND_CONTEXT (t),
6652 args, complain, in_decl));
6654 break;
6656 case PARM_DECL:
6658 tree type;
6660 r = copy_node (t);
6661 if (DECL_TEMPLATE_PARM_P (t))
6662 SET_DECL_TEMPLATE_PARM_P (r);
6664 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6665 type = type_decays_to (type);
6666 TREE_TYPE (r) = type;
6667 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6669 if (DECL_INITIAL (r))
6671 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6672 DECL_INITIAL (r) = TREE_TYPE (r);
6673 else
6674 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6675 complain, in_decl);
6678 DECL_CONTEXT (r) = NULL_TREE;
6680 if (!DECL_TEMPLATE_PARM_P (r))
6681 DECL_ARG_TYPE (r) = type_passed_as (type);
6682 if (TREE_CHAIN (t))
6683 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6684 complain, TREE_CHAIN (t));
6686 break;
6688 case FIELD_DECL:
6690 tree type;
6692 r = copy_decl (t);
6693 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6694 if (type == error_mark_node)
6695 return error_mark_node;
6696 TREE_TYPE (r) = type;
6697 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6699 /* We don't have to set DECL_CONTEXT here; it is set by
6700 finish_member_declaration. */
6701 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6702 complain, in_decl);
6703 TREE_CHAIN (r) = NULL_TREE;
6704 if (VOID_TYPE_P (type))
6705 error ("instantiation of %q+D as type %qT", r, type);
6707 break;
6709 case USING_DECL:
6710 /* We reach here only for member using decls. */
6711 if (DECL_DEPENDENT_P (t))
6713 r = do_class_using_decl
6714 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6715 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6716 if (!r)
6717 r = error_mark_node;
6719 else
6721 r = copy_node (t);
6722 TREE_CHAIN (r) = NULL_TREE;
6724 break;
6726 case TYPE_DECL:
6727 case VAR_DECL:
6729 tree argvec = NULL_TREE;
6730 tree gen_tmpl = NULL_TREE;
6731 tree spec;
6732 tree tmpl = NULL_TREE;
6733 tree ctx;
6734 tree type = NULL_TREE;
6735 bool local_p;
6737 if (TREE_CODE (t) == TYPE_DECL)
6739 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6740 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6741 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6743 /* If this is the canonical decl, we don't have to
6744 mess with instantiations, and often we can't (for
6745 typename, template type parms and such). Note that
6746 TYPE_NAME is not correct for the above test if
6747 we've copied the type for a typedef. */
6748 r = TYPE_NAME (type);
6749 break;
6753 /* Check to see if we already have the specialization we
6754 need. */
6755 spec = NULL_TREE;
6756 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6758 /* T is a static data member or namespace-scope entity.
6759 We have to substitute into namespace-scope variables
6760 (even though such entities are never templates) because
6761 of cases like:
6763 template <class T> void f() { extern T t; }
6765 where the entity referenced is not known until
6766 instantiation time. */
6767 local_p = false;
6768 ctx = DECL_CONTEXT (t);
6769 if (DECL_CLASS_SCOPE_P (t))
6771 ctx = tsubst_aggr_type (ctx, args,
6772 complain,
6773 in_decl, /*entering_scope=*/1);
6774 /* If CTX is unchanged, then T is in fact the
6775 specialization we want. That situation occurs when
6776 referencing a static data member within in its own
6777 class. We can use pointer equality, rather than
6778 same_type_p, because DECL_CONTEXT is always
6779 canonical. */
6780 if (ctx == DECL_CONTEXT (t))
6781 spec = t;
6784 if (!spec)
6786 tmpl = DECL_TI_TEMPLATE (t);
6787 gen_tmpl = most_general_template (tmpl);
6788 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6789 spec = (retrieve_specialization
6790 (gen_tmpl, argvec,
6791 /*class_specializations_p=*/false));
6794 else
6796 /* A local variable. */
6797 local_p = true;
6798 /* Subsequent calls to pushdecl will fill this in. */
6799 ctx = NULL_TREE;
6800 spec = retrieve_local_specialization (t);
6802 /* If we already have the specialization we need, there is
6803 nothing more to do. */
6804 if (spec)
6806 r = spec;
6807 break;
6810 /* Create a new node for the specialization we need. */
6811 r = copy_decl (t);
6812 if (TREE_CODE (r) == VAR_DECL)
6814 /* Even if the original location is out of scope, the
6815 newly substituted one is not. */
6816 DECL_DEAD_FOR_LOCAL (r) = 0;
6817 DECL_INITIALIZED_P (r) = 0;
6818 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6819 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6820 if (type == error_mark_node)
6821 return error_mark_node;
6822 if (TREE_CODE (type) == FUNCTION_TYPE)
6824 /* It may seem that this case cannot occur, since:
6826 typedef void f();
6827 void g() { f x; }
6829 declares a function, not a variable. However:
6831 typedef void f();
6832 template <typename T> void g() { T t; }
6833 template void g<f>();
6835 is an attempt to declare a variable with function
6836 type. */
6837 error ("variable %qD has function type",
6838 /* R is not yet sufficiently initialized, so we
6839 just use its name. */
6840 DECL_NAME (r));
6841 return error_mark_node;
6843 type = complete_type (type);
6844 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6845 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6846 type = check_var_type (DECL_NAME (r), type);
6848 if (DECL_HAS_VALUE_EXPR_P (t))
6850 tree ve = DECL_VALUE_EXPR (t);
6851 ve = tsubst_expr (ve, args, complain, in_decl);
6852 SET_DECL_VALUE_EXPR (r, ve);
6855 else if (DECL_SELF_REFERENCE_P (t))
6856 SET_DECL_SELF_REFERENCE_P (r);
6857 TREE_TYPE (r) = type;
6858 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6859 DECL_CONTEXT (r) = ctx;
6860 /* Clear out the mangled name and RTL for the instantiation. */
6861 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6862 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6863 SET_DECL_RTL (r, NULL_RTX);
6864 /* The initializer must not be expanded until it is required;
6865 see [temp.inst]. */
6866 DECL_INITIAL (r) = NULL_TREE;
6867 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6868 SET_DECL_RTL (r, NULL_RTX);
6869 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6871 if (!local_p)
6873 /* A static data member declaration is always marked
6874 external when it is declared in-class, even if an
6875 initializer is present. We mimic the non-template
6876 processing here. */
6877 DECL_EXTERNAL (r) = 1;
6879 register_specialization (r, gen_tmpl, argvec, false);
6880 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6881 SET_DECL_IMPLICIT_INSTANTIATION (r);
6883 else
6884 register_local_specialization (r, t);
6886 TREE_CHAIN (r) = NULL_TREE;
6887 layout_decl (r, 0);
6889 break;
6891 default:
6892 gcc_unreachable ();
6895 /* Restore the file and line information. */
6896 input_location = saved_loc;
6898 return r;
6901 /* Substitute into the ARG_TYPES of a function type. */
6903 static tree
6904 tsubst_arg_types (tree arg_types,
6905 tree args,
6906 tsubst_flags_t complain,
6907 tree in_decl)
6909 tree remaining_arg_types;
6910 tree type;
6911 tree default_arg;
6912 tree result = NULL_TREE;
6914 if (!arg_types || arg_types == void_list_node)
6915 return arg_types;
6917 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6918 args, complain, in_decl);
6919 if (remaining_arg_types == error_mark_node)
6920 return error_mark_node;
6922 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6923 if (type == error_mark_node)
6924 return error_mark_node;
6925 if (VOID_TYPE_P (type))
6927 if (complain & tf_error)
6929 error ("invalid parameter type %qT", type);
6930 if (in_decl)
6931 error ("in declaration %q+D", in_decl);
6933 return error_mark_node;
6936 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6937 top-level qualifiers as required. */
6938 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6940 /* We do not substitute into default arguments here. The standard
6941 mandates that they be instantiated only when needed, which is
6942 done in build_over_call. */
6943 default_arg = TREE_PURPOSE (arg_types);
6945 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
6947 /* We've instantiated a template before its default arguments
6948 have been parsed. This can happen for a nested template
6949 class, and is not an error unless we require the default
6950 argument in a call of this function. */
6951 result = tree_cons (default_arg, type, remaining_arg_types);
6952 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
6954 else
6955 result = hash_tree_cons (default_arg, type, remaining_arg_types);
6957 return result;
6960 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6961 *not* handle the exception-specification for FNTYPE, because the
6962 initial substitution of explicitly provided template parameters
6963 during argument deduction forbids substitution into the
6964 exception-specification:
6966 [temp.deduct]
6968 All references in the function type of the function template to the
6969 corresponding template parameters are replaced by the specified tem-
6970 plate argument values. If a substitution in a template parameter or
6971 in the function type of the function template results in an invalid
6972 type, type deduction fails. [Note: The equivalent substitution in
6973 exception specifications is done only when the function is instanti-
6974 ated, at which point a program is ill-formed if the substitution
6975 results in an invalid type.] */
6977 static tree
6978 tsubst_function_type (tree t,
6979 tree args,
6980 tsubst_flags_t complain,
6981 tree in_decl)
6983 tree return_type;
6984 tree arg_types;
6985 tree fntype;
6987 /* The TYPE_CONTEXT is not used for function/method types. */
6988 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6990 /* Substitute the return type. */
6991 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6992 if (return_type == error_mark_node)
6993 return error_mark_node;
6994 /* The standard does not presently indicate that creation of a
6995 function type with an invalid return type is a deduction failure.
6996 However, that is clearly analogous to creating an array of "void"
6997 or a reference to a reference. This is core issue #486. */
6998 if (TREE_CODE (return_type) == ARRAY_TYPE
6999 || TREE_CODE (return_type) == FUNCTION_TYPE)
7001 if (complain & tf_error)
7003 if (TREE_CODE (return_type) == ARRAY_TYPE)
7004 error ("function returning an array");
7005 else
7006 error ("function returning a function");
7008 return error_mark_node;
7011 /* Substitute the argument types. */
7012 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
7013 complain, in_decl);
7014 if (arg_types == error_mark_node)
7015 return error_mark_node;
7017 /* Construct a new type node and return it. */
7018 if (TREE_CODE (t) == FUNCTION_TYPE)
7019 fntype = build_function_type (return_type, arg_types);
7020 else
7022 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7023 if (! IS_AGGR_TYPE (r))
7025 /* [temp.deduct]
7027 Type deduction may fail for any of the following
7028 reasons:
7030 -- Attempting to create "pointer to member of T" when T
7031 is not a class type. */
7032 if (complain & tf_error)
7033 error ("creating pointer to member function of non-class type %qT",
7035 return error_mark_node;
7038 fntype = build_method_type_directly (r, return_type,
7039 TREE_CHAIN (arg_types));
7041 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
7042 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
7044 return fntype;
7047 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7048 ARGS into that specification, and return the substituted
7049 specification. If there is no specification, return NULL_TREE. */
7051 static tree
7052 tsubst_exception_specification (tree fntype,
7053 tree args,
7054 tsubst_flags_t complain,
7055 tree in_decl)
7057 tree specs;
7058 tree new_specs;
7060 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7061 new_specs = NULL_TREE;
7062 if (specs)
7064 if (! TREE_VALUE (specs))
7065 new_specs = specs;
7066 else
7067 while (specs)
7069 tree spec;
7070 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7071 if (spec == error_mark_node)
7072 return spec;
7073 new_specs = add_exception_specifier (new_specs, spec, complain);
7074 specs = TREE_CHAIN (specs);
7077 return new_specs;
7080 /* Substitute into the PARMS of a call-declarator. */
7082 static tree
7083 tsubst_call_declarator_parms (tree parms,
7084 tree args,
7085 tsubst_flags_t complain,
7086 tree in_decl)
7088 tree new_parms;
7089 tree type;
7090 tree defarg;
7092 if (!parms || parms == void_list_node)
7093 return parms;
7095 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
7096 args, complain, in_decl);
7098 /* Figure out the type of this parameter. */
7099 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
7101 /* Figure out the default argument as well. Note that we use
7102 tsubst_expr since the default argument is really an expression. */
7103 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
7105 /* Chain this parameter on to the front of those we have already
7106 processed. We don't use hash_tree_cons because that function
7107 doesn't check TREE_PARMLIST. */
7108 new_parms = tree_cons (defarg, type, new_parms);
7110 return new_parms;
7113 /* Take the tree structure T and replace template parameters used
7114 therein with the argument vector ARGS. IN_DECL is an associated
7115 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
7116 Issue error and warning messages under control of COMPLAIN. Note
7117 that we must be relatively non-tolerant of extensions here, in
7118 order to preserve conformance; if we allow substitutions that
7119 should not be allowed, we may allow argument deductions that should
7120 not succeed, and therefore report ambiguous overload situations
7121 where there are none. In theory, we could allow the substitution,
7122 but indicate that it should have failed, and allow our caller to
7123 make sure that the right thing happens, but we don't try to do this
7124 yet.
7126 This function is used for dealing with types, decls and the like;
7127 for expressions, use tsubst_expr or tsubst_copy. */
7129 static tree
7130 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7132 tree type, r;
7134 if (t == NULL_TREE || t == error_mark_node
7135 || t == integer_type_node
7136 || t == void_type_node
7137 || t == char_type_node
7138 || t == unknown_type_node
7139 || TREE_CODE (t) == NAMESPACE_DECL)
7140 return t;
7142 if (DECL_P (t))
7143 return tsubst_decl (t, args, complain);
7145 if (TREE_CODE (t) == IDENTIFIER_NODE)
7146 type = IDENTIFIER_TYPE_VALUE (t);
7147 else
7148 type = TREE_TYPE (t);
7150 gcc_assert (type != unknown_type_node);
7152 if (type
7153 && TREE_CODE (t) != TYPENAME_TYPE
7154 && TREE_CODE (t) != IDENTIFIER_NODE
7155 && TREE_CODE (t) != FUNCTION_TYPE
7156 && TREE_CODE (t) != METHOD_TYPE)
7157 type = tsubst (type, args, complain, in_decl);
7158 if (type == error_mark_node)
7159 return error_mark_node;
7161 switch (TREE_CODE (t))
7163 case RECORD_TYPE:
7164 case UNION_TYPE:
7165 case ENUMERAL_TYPE:
7166 return tsubst_aggr_type (t, args, complain, in_decl,
7167 /*entering_scope=*/0);
7169 case ERROR_MARK:
7170 case IDENTIFIER_NODE:
7171 case VOID_TYPE:
7172 case REAL_TYPE:
7173 case COMPLEX_TYPE:
7174 case VECTOR_TYPE:
7175 case BOOLEAN_TYPE:
7176 case INTEGER_CST:
7177 case REAL_CST:
7178 case STRING_CST:
7179 return t;
7181 case INTEGER_TYPE:
7182 if (t == integer_type_node)
7183 return t;
7185 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7186 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7187 return t;
7190 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7192 /* The array dimension behaves like a non-type template arg,
7193 in that we want to fold it as much as possible. */
7194 max = tsubst_template_arg (omax, args, complain, in_decl);
7195 max = fold_decl_constant_value (max);
7197 if (TREE_CODE (max) != INTEGER_CST
7198 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7199 && !at_function_scope_p ())
7201 if (complain & tf_error)
7202 error ("array bound is not an integer constant");
7203 return error_mark_node;
7206 /* [temp.deduct]
7208 Type deduction may fail for any of the following
7209 reasons:
7211 Attempting to create an array with a size that is
7212 zero or negative. */
7213 if (integer_zerop (max) && !(complain & tf_error))
7214 /* We must fail if performing argument deduction (as
7215 indicated by the state of complain), so that
7216 another substitution can be found. */
7217 return error_mark_node;
7218 else if (TREE_CODE (max) == INTEGER_CST
7219 && INT_CST_LT (max, integer_zero_node))
7221 if (complain & tf_error)
7222 error ("creating array with negative size (%qE)", max);
7224 return error_mark_node;
7227 return compute_array_index_type (NULL_TREE, max);
7230 case TEMPLATE_TYPE_PARM:
7231 case TEMPLATE_TEMPLATE_PARM:
7232 case BOUND_TEMPLATE_TEMPLATE_PARM:
7233 case TEMPLATE_PARM_INDEX:
7235 int idx;
7236 int level;
7237 int levels;
7238 tree arg = NULL_TREE;
7240 r = NULL_TREE;
7242 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7243 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7244 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7245 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7247 idx = TEMPLATE_TYPE_IDX (t);
7248 level = TEMPLATE_TYPE_LEVEL (t);
7250 else
7252 idx = TEMPLATE_PARM_IDX (t);
7253 level = TEMPLATE_PARM_LEVEL (t);
7256 levels = TMPL_ARGS_DEPTH (args);
7257 if (level <= levels)
7258 arg = TMPL_ARG (args, level, idx);
7260 if (arg == error_mark_node)
7261 return error_mark_node;
7262 else if (arg != NULL_TREE)
7264 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7266 int quals;
7267 gcc_assert (TYPE_P (arg));
7269 /* cv-quals from the template are discarded when
7270 substituting in a function or reference type. */
7271 if (TREE_CODE (arg) == FUNCTION_TYPE
7272 || TREE_CODE (arg) == METHOD_TYPE
7273 || TREE_CODE (arg) == REFERENCE_TYPE)
7274 quals = cp_type_quals (arg);
7275 else
7276 quals = cp_type_quals (arg) | cp_type_quals (t);
7278 return cp_build_qualified_type_real
7279 (arg, quals, complain | tf_ignore_bad_quals);
7281 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7283 /* We are processing a type constructed from a
7284 template template parameter. */
7285 tree argvec = tsubst (TYPE_TI_ARGS (t),
7286 args, complain, in_decl);
7287 if (argvec == error_mark_node)
7288 return error_mark_node;
7290 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7291 are resolving nested-types in the signature of a
7292 member function templates. Otherwise ARG is a
7293 TEMPLATE_DECL and is the real template to be
7294 instantiated. */
7295 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7296 arg = TYPE_NAME (arg);
7298 r = lookup_template_class (arg,
7299 argvec, in_decl,
7300 DECL_CONTEXT (arg),
7301 /*entering_scope=*/0,
7302 complain);
7303 return cp_build_qualified_type_real
7304 (r, TYPE_QUALS (t), complain);
7306 else
7307 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7308 return arg;
7311 if (level == 1)
7312 /* This can happen during the attempted tsubst'ing in
7313 unify. This means that we don't yet have any information
7314 about the template parameter in question. */
7315 return t;
7317 /* If we get here, we must have been looking at a parm for a
7318 more deeply nested template. Make a new version of this
7319 template parameter, but with a lower level. */
7320 switch (TREE_CODE (t))
7322 case TEMPLATE_TYPE_PARM:
7323 case TEMPLATE_TEMPLATE_PARM:
7324 case BOUND_TEMPLATE_TEMPLATE_PARM:
7325 if (cp_type_quals (t))
7327 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7328 r = cp_build_qualified_type_real
7329 (r, cp_type_quals (t),
7330 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7331 ? tf_ignore_bad_quals : 0));
7333 else
7335 r = copy_type (t);
7336 TEMPLATE_TYPE_PARM_INDEX (r)
7337 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7338 r, levels);
7339 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7340 TYPE_MAIN_VARIANT (r) = r;
7341 TYPE_POINTER_TO (r) = NULL_TREE;
7342 TYPE_REFERENCE_TO (r) = NULL_TREE;
7344 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7346 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7347 complain, in_decl);
7348 if (argvec == error_mark_node)
7349 return error_mark_node;
7351 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7352 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7355 break;
7357 case TEMPLATE_PARM_INDEX:
7358 r = reduce_template_parm_level (t, type, levels);
7359 break;
7361 default:
7362 gcc_unreachable ();
7365 return r;
7368 case TREE_LIST:
7370 tree purpose, value, chain;
7372 if (t == void_list_node)
7373 return t;
7375 purpose = TREE_PURPOSE (t);
7376 if (purpose)
7378 purpose = tsubst (purpose, args, complain, in_decl);
7379 if (purpose == error_mark_node)
7380 return error_mark_node;
7382 value = TREE_VALUE (t);
7383 if (value)
7385 value = tsubst (value, args, complain, in_decl);
7386 if (value == error_mark_node)
7387 return error_mark_node;
7389 chain = TREE_CHAIN (t);
7390 if (chain && chain != void_type_node)
7392 chain = tsubst (chain, args, complain, in_decl);
7393 if (chain == error_mark_node)
7394 return error_mark_node;
7396 if (purpose == TREE_PURPOSE (t)
7397 && value == TREE_VALUE (t)
7398 && chain == TREE_CHAIN (t))
7399 return t;
7400 return hash_tree_cons (purpose, value, chain);
7403 case TREE_BINFO:
7404 /* We should never be tsubsting a binfo. */
7405 gcc_unreachable ();
7407 case TREE_VEC:
7408 /* A vector of template arguments. */
7409 gcc_assert (!type);
7410 return tsubst_template_args (t, args, complain, in_decl);
7412 case POINTER_TYPE:
7413 case REFERENCE_TYPE:
7415 enum tree_code code;
7417 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7418 return t;
7420 code = TREE_CODE (t);
7423 /* [temp.deduct]
7425 Type deduction may fail for any of the following
7426 reasons:
7428 -- Attempting to create a pointer to reference type.
7429 -- Attempting to create a reference to a reference type or
7430 a reference to void. */
7431 if (TREE_CODE (type) == REFERENCE_TYPE
7432 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7434 static location_t last_loc;
7436 /* We keep track of the last time we issued this error
7437 message to avoid spewing a ton of messages during a
7438 single bad template instantiation. */
7439 if (complain & tf_error
7440 #ifdef USE_MAPPED_LOCATION
7441 && last_loc != input_location
7442 #else
7443 && (last_loc.line != input_line
7444 || last_loc.file != input_filename)
7445 #endif
7448 if (TREE_CODE (type) == VOID_TYPE)
7449 error ("forming reference to void");
7450 else
7451 error ("forming %s to reference type %qT",
7452 (code == POINTER_TYPE) ? "pointer" : "reference",
7453 type);
7454 last_loc = input_location;
7457 return error_mark_node;
7459 else if (code == POINTER_TYPE)
7461 r = build_pointer_type (type);
7462 if (TREE_CODE (type) == METHOD_TYPE)
7463 r = build_ptrmemfunc_type (r);
7465 else
7466 r = build_reference_type (type);
7467 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7469 if (r != error_mark_node)
7470 /* Will this ever be needed for TYPE_..._TO values? */
7471 layout_type (r);
7473 return r;
7475 case OFFSET_TYPE:
7477 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7478 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7480 /* [temp.deduct]
7482 Type deduction may fail for any of the following
7483 reasons:
7485 -- Attempting to create "pointer to member of T" when T
7486 is not a class type. */
7487 if (complain & tf_error)
7488 error ("creating pointer to member of non-class type %qT", r);
7489 return error_mark_node;
7491 if (TREE_CODE (type) == REFERENCE_TYPE)
7493 if (complain & tf_error)
7494 error ("creating pointer to member reference type %qT", type);
7495 return error_mark_node;
7497 if (TREE_CODE (type) == VOID_TYPE)
7499 if (complain & tf_error)
7500 error ("creating pointer to member of type void");
7501 return error_mark_node;
7503 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7504 if (TREE_CODE (type) == FUNCTION_TYPE)
7506 /* The type of the implicit object parameter gets its
7507 cv-qualifiers from the FUNCTION_TYPE. */
7508 tree method_type;
7509 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7510 cp_type_quals (type));
7511 tree memptr;
7512 method_type = build_method_type_directly (this_type,
7513 TREE_TYPE (type),
7514 TYPE_ARG_TYPES (type));
7515 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7516 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7517 complain);
7519 else
7520 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7521 TYPE_QUALS (t),
7522 complain);
7524 case FUNCTION_TYPE:
7525 case METHOD_TYPE:
7527 tree fntype;
7528 tree specs;
7529 fntype = tsubst_function_type (t, args, complain, in_decl);
7530 if (fntype == error_mark_node)
7531 return error_mark_node;
7533 /* Substitute the exception specification. */
7534 specs = tsubst_exception_specification (t, args, complain,
7535 in_decl);
7536 if (specs == error_mark_node)
7537 return error_mark_node;
7538 if (specs)
7539 fntype = build_exception_variant (fntype, specs);
7540 return fntype;
7542 case ARRAY_TYPE:
7544 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7545 if (domain == error_mark_node)
7546 return error_mark_node;
7548 /* As an optimization, we avoid regenerating the array type if
7549 it will obviously be the same as T. */
7550 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7551 return t;
7553 /* These checks should match the ones in grokdeclarator.
7555 [temp.deduct]
7557 The deduction may fail for any of the following reasons:
7559 -- Attempting to create an array with an element type that
7560 is void, a function type, or a reference type, or [DR337]
7561 an abstract class type. */
7562 if (TREE_CODE (type) == VOID_TYPE
7563 || TREE_CODE (type) == FUNCTION_TYPE
7564 || TREE_CODE (type) == REFERENCE_TYPE)
7566 if (complain & tf_error)
7567 error ("creating array of %qT", type);
7568 return error_mark_node;
7570 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7572 if (complain & tf_error)
7573 error ("creating array of %qT, which is an abstract class type",
7574 type);
7575 return error_mark_node;
7578 r = build_cplus_array_type (type, domain);
7579 return r;
7582 case PLUS_EXPR:
7583 case MINUS_EXPR:
7585 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7586 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7588 if (e1 == error_mark_node || e2 == error_mark_node)
7589 return error_mark_node;
7591 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7594 case NEGATE_EXPR:
7595 case NOP_EXPR:
7597 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7598 if (e == error_mark_node)
7599 return error_mark_node;
7601 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7604 case TYPENAME_TYPE:
7606 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7607 in_decl, /*entering_scope=*/1);
7608 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7609 complain, in_decl);
7611 if (ctx == error_mark_node || f == error_mark_node)
7612 return error_mark_node;
7614 if (!IS_AGGR_TYPE (ctx))
7616 if (complain & tf_error)
7617 error ("%qT is not a class, struct, or union type", ctx);
7618 return error_mark_node;
7620 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7622 /* Normally, make_typename_type does not require that the CTX
7623 have complete type in order to allow things like:
7625 template <class T> struct S { typename S<T>::X Y; };
7627 But, such constructs have already been resolved by this
7628 point, so here CTX really should have complete type, unless
7629 it's a partial instantiation. */
7630 ctx = complete_type (ctx);
7631 if (!COMPLETE_TYPE_P (ctx))
7633 if (complain & tf_error)
7634 cxx_incomplete_type_error (NULL_TREE, ctx);
7635 return error_mark_node;
7639 f = make_typename_type (ctx, f, typename_type,
7640 (complain & tf_error) | tf_keep_type_decl);
7641 if (f == error_mark_node)
7642 return f;
7643 if (TREE_CODE (f) == TYPE_DECL)
7645 complain |= tf_ignore_bad_quals;
7646 f = TREE_TYPE (f);
7649 if (TREE_CODE (f) != TYPENAME_TYPE)
7651 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7652 error ("%qT resolves to %qT, which is not an enumeration type",
7653 t, f);
7654 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7655 error ("%qT resolves to %qT, which is is not a class type",
7656 t, f);
7659 return cp_build_qualified_type_real
7660 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7663 case UNBOUND_CLASS_TEMPLATE:
7665 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7666 in_decl, /*entering_scope=*/1);
7667 tree name = TYPE_IDENTIFIER (t);
7668 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7670 if (ctx == error_mark_node || name == error_mark_node)
7671 return error_mark_node;
7673 if (parm_list)
7674 parm_list = tsubst_template_parms (parm_list, args, complain);
7675 return make_unbound_class_template (ctx, name, parm_list, complain);
7678 case INDIRECT_REF:
7679 case ADDR_EXPR:
7680 case CALL_EXPR:
7681 gcc_unreachable ();
7683 case ARRAY_REF:
7685 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7686 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7687 if (e1 == error_mark_node || e2 == error_mark_node)
7688 return error_mark_node;
7690 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7693 case SCOPE_REF:
7695 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7696 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7697 if (e1 == error_mark_node || e2 == error_mark_node)
7698 return error_mark_node;
7700 return build_qualified_name (/*type=*/NULL_TREE,
7701 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7704 case TYPEOF_TYPE:
7706 tree type;
7708 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7709 complain, in_decl));
7710 return cp_build_qualified_type_real (type,
7711 cp_type_quals (t)
7712 | cp_type_quals (type),
7713 complain);
7716 default:
7717 sorry ("use of %qs in template",
7718 tree_code_name [(int) TREE_CODE (t)]);
7719 return error_mark_node;
7723 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7724 type of the expression on the left-hand side of the "." or "->"
7725 operator. */
7727 static tree
7728 tsubst_baselink (tree baselink, tree object_type,
7729 tree args, tsubst_flags_t complain, tree in_decl)
7731 tree name;
7732 tree qualifying_scope;
7733 tree fns;
7734 tree optype;
7735 tree template_args = 0;
7736 bool template_id_p = false;
7738 /* A baselink indicates a function from a base class. The
7739 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7740 non-dependent types; otherwise, the lookup could not have
7741 succeeded. However, they may indicate bases of the template
7742 class, rather than the instantiated class.
7744 In addition, lookups that were not ambiguous before may be
7745 ambiguous now. Therefore, we perform the lookup again. */
7746 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7747 fns = BASELINK_FUNCTIONS (baselink);
7748 optype = BASELINK_OPTYPE (baselink);
7749 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7751 template_id_p = true;
7752 template_args = TREE_OPERAND (fns, 1);
7753 fns = TREE_OPERAND (fns, 0);
7754 if (template_args)
7755 template_args = tsubst_template_args (template_args, args,
7756 complain, in_decl);
7758 name = DECL_NAME (get_first_fn (fns));
7759 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7761 /* If lookup found a single function, mark it as used at this
7762 point. (If it lookup found multiple functions the one selected
7763 later by overload resolution will be marked as used at that
7764 point.) */
7765 if (BASELINK_P (baselink))
7766 fns = BASELINK_FUNCTIONS (baselink);
7767 if (!template_id_p && !really_overloaded_fn (fns))
7768 mark_used (OVL_CURRENT (fns));
7770 /* Add back the template arguments, if present. */
7771 if (BASELINK_P (baselink) && template_id_p)
7772 BASELINK_FUNCTIONS (baselink)
7773 = build_nt (TEMPLATE_ID_EXPR,
7774 BASELINK_FUNCTIONS (baselink),
7775 template_args);
7776 /* Update the conversion operator type. */
7777 BASELINK_OPTYPE (baselink)
7778 = tsubst (optype, args, complain, in_decl);
7780 if (!object_type)
7781 object_type = current_class_type;
7782 return adjust_result_of_qualified_name_lookup (baselink,
7783 qualifying_scope,
7784 object_type);
7787 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7788 true if the qualified-id will be a postfix-expression in-and-of
7789 itself; false if more of the postfix-expression follows the
7790 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7791 of "&". */
7793 static tree
7794 tsubst_qualified_id (tree qualified_id, tree args,
7795 tsubst_flags_t complain, tree in_decl,
7796 bool done, bool address_p)
7798 tree expr;
7799 tree scope;
7800 tree name;
7801 bool is_template;
7802 tree template_args;
7804 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7806 /* Figure out what name to look up. */
7807 name = TREE_OPERAND (qualified_id, 1);
7808 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7810 is_template = true;
7811 template_args = TREE_OPERAND (name, 1);
7812 if (template_args)
7813 template_args = tsubst_template_args (template_args, args,
7814 complain, in_decl);
7815 name = TREE_OPERAND (name, 0);
7817 else
7819 is_template = false;
7820 template_args = NULL_TREE;
7823 /* Substitute into the qualifying scope. When there are no ARGS, we
7824 are just trying to simplify a non-dependent expression. In that
7825 case the qualifying scope may be dependent, and, in any case,
7826 substituting will not help. */
7827 scope = TREE_OPERAND (qualified_id, 0);
7828 if (args)
7830 scope = tsubst (scope, args, complain, in_decl);
7831 expr = tsubst_copy (name, args, complain, in_decl);
7833 else
7834 expr = name;
7836 if (dependent_type_p (scope))
7837 return build_qualified_name (/*type=*/NULL_TREE,
7838 scope, expr,
7839 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7841 if (!BASELINK_P (name) && !DECL_P (expr))
7843 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7844 /* If this were actually a destructor call, it would have been
7845 parsed as such by the parser. */
7846 expr = error_mark_node;
7847 else
7848 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7849 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7850 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7852 if (complain & tf_error)
7854 error ("dependent-name %qE is parsed as a non-type, but "
7855 "instantiation yields a type", qualified_id);
7856 inform ("say %<typename %E%> if a type is meant", qualified_id);
7858 return error_mark_node;
7862 if (DECL_P (expr))
7864 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7865 scope);
7866 /* Remember that there was a reference to this entity. */
7867 mark_used (expr);
7870 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7872 if (complain & tf_error)
7873 qualified_name_lookup_error (scope,
7874 TREE_OPERAND (qualified_id, 1),
7875 expr);
7876 return error_mark_node;
7879 if (is_template)
7880 expr = lookup_template_function (expr, template_args);
7882 if (expr == error_mark_node && complain & tf_error)
7883 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7884 expr);
7885 else if (TYPE_P (scope))
7887 expr = (adjust_result_of_qualified_name_lookup
7888 (expr, scope, current_class_type));
7889 expr = (finish_qualified_id_expr
7890 (scope, expr, done, address_p,
7891 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7892 /*template_arg_p=*/false));
7895 /* Expressions do not generally have reference type. */
7896 if (TREE_CODE (expr) != SCOPE_REF
7897 /* However, if we're about to form a pointer-to-member, we just
7898 want the referenced member referenced. */
7899 && TREE_CODE (expr) != OFFSET_REF)
7900 expr = convert_from_reference (expr);
7902 return expr;
7905 /* Like tsubst, but deals with expressions. This function just replaces
7906 template parms; to finish processing the resultant expression, use
7907 tsubst_expr. */
7909 static tree
7910 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7912 enum tree_code code;
7913 tree r;
7915 if (t == NULL_TREE || t == error_mark_node)
7916 return t;
7918 code = TREE_CODE (t);
7920 switch (code)
7922 case PARM_DECL:
7923 r = retrieve_local_specialization (t);
7924 gcc_assert (r != NULL);
7925 mark_used (r);
7926 return r;
7928 case CONST_DECL:
7930 tree enum_type;
7931 tree v;
7933 if (DECL_TEMPLATE_PARM_P (t))
7934 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7935 /* There is no need to substitute into namespace-scope
7936 enumerators. */
7937 if (DECL_NAMESPACE_SCOPE_P (t))
7938 return t;
7939 /* If ARGS is NULL, then T is known to be non-dependent. */
7940 if (args == NULL_TREE)
7941 return integral_constant_value (t);
7943 /* Unfortunately, we cannot just call lookup_name here.
7944 Consider:
7946 template <int I> int f() {
7947 enum E { a = I };
7948 struct S { void g() { E e = a; } };
7951 When we instantiate f<7>::S::g(), say, lookup_name is not
7952 clever enough to find f<7>::a. */
7953 enum_type
7954 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7955 /*entering_scope=*/0);
7957 for (v = TYPE_VALUES (enum_type);
7958 v != NULL_TREE;
7959 v = TREE_CHAIN (v))
7960 if (TREE_PURPOSE (v) == DECL_NAME (t))
7961 return TREE_VALUE (v);
7963 /* We didn't find the name. That should never happen; if
7964 name-lookup found it during preliminary parsing, we
7965 should find it again here during instantiation. */
7966 gcc_unreachable ();
7968 return t;
7970 case FIELD_DECL:
7971 if (DECL_CONTEXT (t))
7973 tree ctx;
7975 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7976 /*entering_scope=*/1);
7977 if (ctx != DECL_CONTEXT (t))
7979 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7980 if (!r)
7982 if (complain & tf_error)
7983 error ("using invalid field %qD", t);
7984 return error_mark_node;
7986 return r;
7990 return t;
7992 case VAR_DECL:
7993 case FUNCTION_DECL:
7994 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7995 || local_variable_p (t))
7996 t = tsubst (t, args, complain, in_decl);
7997 mark_used (t);
7998 return t;
8000 case BASELINK:
8001 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
8003 case TEMPLATE_DECL:
8004 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8005 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
8006 args, complain, in_decl);
8007 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
8008 return tsubst (t, args, complain, in_decl);
8009 else if (DECL_CLASS_SCOPE_P (t)
8010 && uses_template_parms (DECL_CONTEXT (t)))
8012 /* Template template argument like the following example need
8013 special treatment:
8015 template <template <class> class TT> struct C {};
8016 template <class T> struct D {
8017 template <class U> struct E {};
8018 C<E> c; // #1
8020 D<int> d; // #2
8022 We are processing the template argument `E' in #1 for
8023 the template instantiation #2. Originally, `E' is a
8024 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8025 have to substitute this with one having context `D<int>'. */
8027 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8028 return lookup_field (context, DECL_NAME(t), 0, false);
8030 else
8031 /* Ordinary template template argument. */
8032 return t;
8034 case CAST_EXPR:
8035 case REINTERPRET_CAST_EXPR:
8036 case CONST_CAST_EXPR:
8037 case STATIC_CAST_EXPR:
8038 case DYNAMIC_CAST_EXPR:
8039 case NOP_EXPR:
8040 return build1
8041 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8042 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8044 case INDIRECT_REF:
8045 case NEGATE_EXPR:
8046 case TRUTH_NOT_EXPR:
8047 case BIT_NOT_EXPR:
8048 case ADDR_EXPR:
8049 case UNARY_PLUS_EXPR: /* Unary + */
8050 case SIZEOF_EXPR:
8051 case ALIGNOF_EXPR:
8052 case ARROW_EXPR:
8053 case THROW_EXPR:
8054 case TYPEID_EXPR:
8055 case REALPART_EXPR:
8056 case IMAGPART_EXPR:
8057 return build1
8058 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8059 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8061 case COMPONENT_REF:
8063 tree object;
8064 tree name;
8066 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8067 name = TREE_OPERAND (t, 1);
8068 if (TREE_CODE (name) == BIT_NOT_EXPR)
8070 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8071 complain, in_decl);
8072 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8074 else if (TREE_CODE (name) == SCOPE_REF
8075 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8077 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8078 complain, in_decl);
8079 name = TREE_OPERAND (name, 1);
8080 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8081 complain, in_decl);
8082 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8083 name = build_qualified_name (/*type=*/NULL_TREE,
8084 base, name,
8085 /*template_p=*/false);
8087 else if (TREE_CODE (name) == BASELINK)
8088 name = tsubst_baselink (name,
8089 non_reference (TREE_TYPE (object)),
8090 args, complain,
8091 in_decl);
8092 else
8093 name = tsubst_copy (name, args, complain, in_decl);
8094 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
8097 case PLUS_EXPR:
8098 case MINUS_EXPR:
8099 case MULT_EXPR:
8100 case TRUNC_DIV_EXPR:
8101 case CEIL_DIV_EXPR:
8102 case FLOOR_DIV_EXPR:
8103 case ROUND_DIV_EXPR:
8104 case EXACT_DIV_EXPR:
8105 case BIT_AND_EXPR:
8106 case BIT_IOR_EXPR:
8107 case BIT_XOR_EXPR:
8108 case TRUNC_MOD_EXPR:
8109 case FLOOR_MOD_EXPR:
8110 case TRUTH_ANDIF_EXPR:
8111 case TRUTH_ORIF_EXPR:
8112 case TRUTH_AND_EXPR:
8113 case TRUTH_OR_EXPR:
8114 case RSHIFT_EXPR:
8115 case LSHIFT_EXPR:
8116 case RROTATE_EXPR:
8117 case LROTATE_EXPR:
8118 case EQ_EXPR:
8119 case NE_EXPR:
8120 case MAX_EXPR:
8121 case MIN_EXPR:
8122 case LE_EXPR:
8123 case GE_EXPR:
8124 case LT_EXPR:
8125 case GT_EXPR:
8126 case COMPOUND_EXPR:
8127 case DOTSTAR_EXPR:
8128 case MEMBER_REF:
8129 case PREDECREMENT_EXPR:
8130 case PREINCREMENT_EXPR:
8131 case POSTDECREMENT_EXPR:
8132 case POSTINCREMENT_EXPR:
8133 return build_nt
8134 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8135 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8137 case SCOPE_REF:
8138 return build_qualified_name (/*type=*/NULL_TREE,
8139 tsubst_copy (TREE_OPERAND (t, 0),
8140 args, complain, in_decl),
8141 tsubst_copy (TREE_OPERAND (t, 1),
8142 args, complain, in_decl),
8143 QUALIFIED_NAME_IS_TEMPLATE (t));
8145 case ARRAY_REF:
8146 return build_nt
8147 (ARRAY_REF,
8148 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8149 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8150 NULL_TREE, NULL_TREE);
8152 case CALL_EXPR:
8153 return build_nt (code,
8154 tsubst_copy (TREE_OPERAND (t, 0), args,
8155 complain, in_decl),
8156 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8157 in_decl),
8158 NULL_TREE);
8160 case COND_EXPR:
8161 case MODOP_EXPR:
8162 case PSEUDO_DTOR_EXPR:
8164 r = build_nt
8165 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8166 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8167 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8168 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8169 return r;
8172 case NEW_EXPR:
8174 r = build_nt
8175 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8176 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8177 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8178 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8179 return r;
8182 case DELETE_EXPR:
8184 r = build_nt
8185 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8186 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8187 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8188 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8189 return r;
8192 case TEMPLATE_ID_EXPR:
8194 /* Substituted template arguments */
8195 tree fn = TREE_OPERAND (t, 0);
8196 tree targs = TREE_OPERAND (t, 1);
8198 fn = tsubst_copy (fn, args, complain, in_decl);
8199 if (targs)
8200 targs = tsubst_template_args (targs, args, complain, in_decl);
8202 return lookup_template_function (fn, targs);
8205 case TREE_LIST:
8207 tree purpose, value, chain;
8209 if (t == void_list_node)
8210 return t;
8212 purpose = TREE_PURPOSE (t);
8213 if (purpose)
8214 purpose = tsubst_copy (purpose, args, complain, in_decl);
8215 value = TREE_VALUE (t);
8216 if (value)
8217 value = tsubst_copy (value, args, complain, in_decl);
8218 chain = TREE_CHAIN (t);
8219 if (chain && chain != void_type_node)
8220 chain = tsubst_copy (chain, args, complain, in_decl);
8221 if (purpose == TREE_PURPOSE (t)
8222 && value == TREE_VALUE (t)
8223 && chain == TREE_CHAIN (t))
8224 return t;
8225 return tree_cons (purpose, value, chain);
8228 case RECORD_TYPE:
8229 case UNION_TYPE:
8230 case ENUMERAL_TYPE:
8231 case INTEGER_TYPE:
8232 case TEMPLATE_TYPE_PARM:
8233 case TEMPLATE_TEMPLATE_PARM:
8234 case BOUND_TEMPLATE_TEMPLATE_PARM:
8235 case TEMPLATE_PARM_INDEX:
8236 case POINTER_TYPE:
8237 case REFERENCE_TYPE:
8238 case OFFSET_TYPE:
8239 case FUNCTION_TYPE:
8240 case METHOD_TYPE:
8241 case ARRAY_TYPE:
8242 case TYPENAME_TYPE:
8243 case UNBOUND_CLASS_TEMPLATE:
8244 case TYPEOF_TYPE:
8245 case TYPE_DECL:
8246 return tsubst (t, args, complain, in_decl);
8248 case IDENTIFIER_NODE:
8249 if (IDENTIFIER_TYPENAME_P (t))
8251 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8252 return mangle_conv_op_name_for_type (new_type);
8254 else
8255 return t;
8257 case CONSTRUCTOR:
8258 /* This is handled by tsubst_copy_and_build. */
8259 gcc_unreachable ();
8261 case VA_ARG_EXPR:
8262 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8263 in_decl),
8264 tsubst (TREE_TYPE (t), args, complain, in_decl));
8266 case CLEANUP_POINT_EXPR:
8267 /* We shouldn't have built any of these during initial template
8268 generation. Instead, they should be built during instantiation
8269 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8270 gcc_unreachable ();
8272 case OFFSET_REF:
8273 mark_used (TREE_OPERAND (t, 1));
8274 return t;
8276 default:
8277 return t;
8281 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8283 static tree
8284 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8285 tree in_decl)
8287 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8289 tree purpose, value, chain;
8291 if (t == NULL)
8292 return t;
8294 if (TREE_CODE (t) != TREE_LIST)
8295 return tsubst_copy_and_build (t, args, complain, in_decl,
8296 /*function_p=*/false);
8298 if (t == void_list_node)
8299 return t;
8301 purpose = TREE_PURPOSE (t);
8302 if (purpose)
8303 purpose = RECUR (purpose);
8304 value = TREE_VALUE (t);
8305 if (value)
8306 value = RECUR (value);
8307 chain = TREE_CHAIN (t);
8308 if (chain && chain != void_type_node)
8309 chain = RECUR (chain);
8310 return tree_cons (purpose, value, chain);
8311 #undef RECUR
8314 /* Like tsubst_copy for expressions, etc. but also does semantic
8315 processing. */
8317 static tree
8318 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8320 tree stmt, tmp;
8322 if (t == NULL_TREE || t == error_mark_node)
8323 return t;
8325 if (EXPR_HAS_LOCATION (t))
8326 input_location = EXPR_LOCATION (t);
8327 if (STATEMENT_CODE_P (TREE_CODE (t)))
8328 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8330 switch (TREE_CODE (t))
8332 case STATEMENT_LIST:
8334 tree_stmt_iterator i;
8335 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8336 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
8337 break;
8340 case CTOR_INITIALIZER:
8341 finish_mem_initializers (tsubst_initializer_list
8342 (TREE_OPERAND (t, 0), args));
8343 break;
8345 case RETURN_EXPR:
8346 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
8347 args, complain, in_decl));
8348 break;
8350 case EXPR_STMT:
8351 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
8352 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8353 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8354 else
8355 finish_expr_stmt (tmp);
8356 break;
8358 case USING_STMT:
8359 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
8360 args, complain, in_decl));
8361 break;
8363 case DECL_EXPR:
8365 tree decl;
8366 tree init;
8368 decl = DECL_EXPR_DECL (t);
8369 if (TREE_CODE (decl) == LABEL_DECL)
8370 finish_label_decl (DECL_NAME (decl));
8371 else if (TREE_CODE (decl) == USING_DECL)
8373 tree scope = USING_DECL_SCOPE (decl);
8374 tree name = DECL_NAME (decl);
8375 tree decl;
8377 scope = tsubst_expr (scope, args, complain, in_decl);
8378 decl = lookup_qualified_name (scope, name,
8379 /*is_type_p=*/false,
8380 /*complain=*/false);
8381 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8382 qualified_name_lookup_error (scope, name, decl);
8383 else
8384 do_local_using_decl (decl, scope, name);
8386 else
8388 init = DECL_INITIAL (decl);
8389 decl = tsubst (decl, args, complain, in_decl);
8390 if (decl != error_mark_node)
8392 if (init)
8393 DECL_INITIAL (decl) = error_mark_node;
8394 /* By marking the declaration as instantiated, we avoid
8395 trying to instantiate it. Since instantiate_decl can't
8396 handle local variables, and since we've already done
8397 all that needs to be done, that's the right thing to
8398 do. */
8399 if (TREE_CODE (decl) == VAR_DECL)
8400 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8401 if (TREE_CODE (decl) == VAR_DECL
8402 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8403 /* Anonymous aggregates are a special case. */
8404 finish_anon_union (decl);
8405 else
8407 maybe_push_decl (decl);
8408 if (TREE_CODE (decl) == VAR_DECL
8409 && DECL_PRETTY_FUNCTION_P (decl))
8411 /* For __PRETTY_FUNCTION__ we have to adjust the
8412 initializer. */
8413 const char *const name
8414 = cxx_printable_name (current_function_decl, 2);
8415 init = cp_fname_init (name, &TREE_TYPE (decl));
8417 else
8418 init = tsubst_expr (init, args, complain, in_decl);
8419 finish_decl (decl, init, NULL_TREE);
8424 /* A DECL_EXPR can also be used as an expression, in the condition
8425 clause of an if/for/while construct. */
8426 return decl;
8429 case FOR_STMT:
8430 stmt = begin_for_stmt ();
8431 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
8432 finish_for_init_stmt (stmt);
8433 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
8434 finish_for_cond (tmp, stmt);
8435 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
8436 finish_for_expr (tmp, stmt);
8437 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
8438 finish_for_stmt (stmt);
8439 break;
8441 case WHILE_STMT:
8442 stmt = begin_while_stmt ();
8443 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
8444 finish_while_stmt_cond (tmp, stmt);
8445 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
8446 finish_while_stmt (stmt);
8447 break;
8449 case DO_STMT:
8450 stmt = begin_do_stmt ();
8451 tsubst_expr (DO_BODY (t), args, complain, in_decl);
8452 finish_do_body (stmt);
8453 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
8454 finish_do_stmt (tmp, stmt);
8455 break;
8457 case IF_STMT:
8458 stmt = begin_if_stmt ();
8459 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
8460 finish_if_stmt_cond (tmp, stmt);
8461 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
8462 finish_then_clause (stmt);
8464 if (ELSE_CLAUSE (t))
8466 begin_else_clause (stmt);
8467 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
8468 finish_else_clause (stmt);
8471 finish_if_stmt (stmt);
8472 break;
8474 case BIND_EXPR:
8475 if (BIND_EXPR_BODY_BLOCK (t))
8476 stmt = begin_function_body ();
8477 else
8478 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8479 ? BCS_TRY_BLOCK : 0);
8481 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
8483 if (BIND_EXPR_BODY_BLOCK (t))
8484 finish_function_body (stmt);
8485 else
8486 finish_compound_stmt (stmt);
8487 break;
8489 case BREAK_STMT:
8490 finish_break_stmt ();
8491 break;
8493 case CONTINUE_STMT:
8494 finish_continue_stmt ();
8495 break;
8497 case SWITCH_STMT:
8498 stmt = begin_switch_stmt ();
8499 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
8500 finish_switch_cond (tmp, stmt);
8501 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
8502 finish_switch_stmt (stmt);
8503 break;
8505 case CASE_LABEL_EXPR:
8506 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
8507 tsubst_expr (CASE_HIGH (t), args, complain,
8508 in_decl));
8509 break;
8511 case LABEL_EXPR:
8512 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8513 break;
8515 case GOTO_EXPR:
8516 tmp = GOTO_DESTINATION (t);
8517 if (TREE_CODE (tmp) != LABEL_DECL)
8518 /* Computed goto's must be tsubst'd into. On the other hand,
8519 non-computed gotos must not be; the identifier in question
8520 will have no binding. */
8521 tmp = tsubst_expr (tmp, args, complain, in_decl);
8522 else
8523 tmp = DECL_NAME (tmp);
8524 finish_goto_stmt (tmp);
8525 break;
8527 case ASM_EXPR:
8528 tmp = finish_asm_stmt
8529 (ASM_VOLATILE_P (t),
8530 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8531 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8532 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8533 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8535 tree asm_expr = tmp;
8536 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8537 asm_expr = TREE_OPERAND (asm_expr, 0);
8538 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8540 break;
8542 case TRY_BLOCK:
8543 if (CLEANUP_P (t))
8545 stmt = begin_try_block ();
8546 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8547 finish_cleanup_try_block (stmt);
8548 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8549 complain, in_decl),
8550 stmt);
8552 else
8554 tree compound_stmt = NULL_TREE;
8556 if (FN_TRY_BLOCK_P (t))
8557 stmt = begin_function_try_block (&compound_stmt);
8558 else
8559 stmt = begin_try_block ();
8561 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8563 if (FN_TRY_BLOCK_P (t))
8564 finish_function_try_block (stmt);
8565 else
8566 finish_try_block (stmt);
8568 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8569 if (FN_TRY_BLOCK_P (t))
8570 finish_function_handler_sequence (stmt, compound_stmt);
8571 else
8572 finish_handler_sequence (stmt);
8574 break;
8576 case HANDLER:
8578 tree decl;
8580 stmt = begin_handler ();
8581 if (HANDLER_PARMS (t))
8583 decl = HANDLER_PARMS (t);
8584 decl = tsubst (decl, args, complain, in_decl);
8585 /* Prevent instantiate_decl from trying to instantiate
8586 this variable. We've already done all that needs to be
8587 done. */
8588 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8590 else
8591 decl = NULL_TREE;
8592 finish_handler_parms (decl, stmt);
8593 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8594 finish_handler (stmt);
8596 break;
8598 case TAG_DEFN:
8599 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8600 break;
8602 default:
8603 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8605 return tsubst_copy_and_build (t, args, complain, in_decl,
8606 /*function_p=*/false);
8609 return NULL_TREE;
8612 /* T is a postfix-expression that is not being used in a function
8613 call. Return the substituted version of T. */
8615 static tree
8616 tsubst_non_call_postfix_expression (tree t, tree args,
8617 tsubst_flags_t complain,
8618 tree in_decl)
8620 if (TREE_CODE (t) == SCOPE_REF)
8621 t = tsubst_qualified_id (t, args, complain, in_decl,
8622 /*done=*/false, /*address_p=*/false);
8623 else
8624 t = tsubst_copy_and_build (t, args, complain, in_decl,
8625 /*function_p=*/false);
8627 return t;
8630 /* Like tsubst but deals with expressions and performs semantic
8631 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8633 tree
8634 tsubst_copy_and_build (tree t,
8635 tree args,
8636 tsubst_flags_t complain,
8637 tree in_decl,
8638 bool function_p)
8640 #define RECUR(NODE) \
8641 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8643 tree op1;
8645 if (t == NULL_TREE || t == error_mark_node)
8646 return t;
8648 switch (TREE_CODE (t))
8650 case USING_DECL:
8651 t = DECL_NAME (t);
8652 /* Fall through. */
8653 case IDENTIFIER_NODE:
8655 tree decl;
8656 cp_id_kind idk;
8657 bool non_integral_constant_expression_p;
8658 const char *error_msg;
8660 if (IDENTIFIER_TYPENAME_P (t))
8662 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8663 t = mangle_conv_op_name_for_type (new_type);
8666 /* Look up the name. */
8667 decl = lookup_name (t, 0);
8669 /* By convention, expressions use ERROR_MARK_NODE to indicate
8670 failure, not NULL_TREE. */
8671 if (decl == NULL_TREE)
8672 decl = error_mark_node;
8674 decl = finish_id_expression (t, decl, NULL_TREE,
8675 &idk,
8676 /*integral_constant_expression_p=*/false,
8677 /*allow_non_integral_constant_expression_p=*/false,
8678 &non_integral_constant_expression_p,
8679 /*template_p=*/false,
8680 /*done=*/true,
8681 /*address_p=*/false,
8682 /*template_arg_p=*/false,
8683 &error_msg);
8684 if (error_msg)
8685 error (error_msg);
8686 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8687 decl = unqualified_name_lookup_error (decl);
8688 return decl;
8691 case TEMPLATE_ID_EXPR:
8693 tree object;
8694 tree template = RECUR (TREE_OPERAND (t, 0));
8695 tree targs = TREE_OPERAND (t, 1);
8697 if (targs)
8698 targs = tsubst_template_args (targs, args, complain, in_decl);
8700 if (TREE_CODE (template) == COMPONENT_REF)
8702 object = TREE_OPERAND (template, 0);
8703 template = TREE_OPERAND (template, 1);
8705 else
8706 object = NULL_TREE;
8707 template = lookup_template_function (template, targs);
8709 if (object)
8710 return build3 (COMPONENT_REF, TREE_TYPE (template),
8711 object, template, NULL_TREE);
8712 else
8713 return template;
8716 case INDIRECT_REF:
8718 tree r = RECUR (TREE_OPERAND (t, 0));
8720 if (REFERENCE_REF_P (t))
8722 /* A type conversion to reference type will be enclosed in
8723 such an indirect ref, but the substitution of the cast
8724 will have also added such an indirect ref. */
8725 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8726 r = convert_from_reference (r);
8728 else
8729 r = build_x_indirect_ref (r, "unary *");
8730 return r;
8733 case NOP_EXPR:
8734 return build_nop
8735 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8736 RECUR (TREE_OPERAND (t, 0)));
8738 case CAST_EXPR:
8739 return build_functional_cast
8740 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8741 RECUR (TREE_OPERAND (t, 0)));
8743 case REINTERPRET_CAST_EXPR:
8744 return build_reinterpret_cast
8745 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8746 RECUR (TREE_OPERAND (t, 0)));
8748 case CONST_CAST_EXPR:
8749 return build_const_cast
8750 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8751 RECUR (TREE_OPERAND (t, 0)));
8753 case DYNAMIC_CAST_EXPR:
8754 return build_dynamic_cast
8755 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8756 RECUR (TREE_OPERAND (t, 0)));
8758 case STATIC_CAST_EXPR:
8759 return build_static_cast
8760 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8761 RECUR (TREE_OPERAND (t, 0)));
8763 case POSTDECREMENT_EXPR:
8764 case POSTINCREMENT_EXPR:
8765 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8766 args, complain, in_decl);
8767 return build_x_unary_op (TREE_CODE (t), op1);
8769 case PREDECREMENT_EXPR:
8770 case PREINCREMENT_EXPR:
8771 case NEGATE_EXPR:
8772 case BIT_NOT_EXPR:
8773 case ABS_EXPR:
8774 case TRUTH_NOT_EXPR:
8775 case UNARY_PLUS_EXPR: /* Unary + */
8776 case REALPART_EXPR:
8777 case IMAGPART_EXPR:
8778 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8780 case ADDR_EXPR:
8781 op1 = TREE_OPERAND (t, 0);
8782 if (TREE_CODE (op1) == SCOPE_REF)
8783 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8784 /*done=*/true, /*address_p=*/true);
8785 else
8786 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8787 in_decl);
8788 if (TREE_CODE (op1) == LABEL_DECL)
8789 return finish_label_address_expr (DECL_NAME (op1));
8790 return build_x_unary_op (ADDR_EXPR, op1);
8792 case PLUS_EXPR:
8793 case MINUS_EXPR:
8794 case MULT_EXPR:
8795 case TRUNC_DIV_EXPR:
8796 case CEIL_DIV_EXPR:
8797 case FLOOR_DIV_EXPR:
8798 case ROUND_DIV_EXPR:
8799 case EXACT_DIV_EXPR:
8800 case BIT_AND_EXPR:
8801 case BIT_IOR_EXPR:
8802 case BIT_XOR_EXPR:
8803 case TRUNC_MOD_EXPR:
8804 case FLOOR_MOD_EXPR:
8805 case TRUTH_ANDIF_EXPR:
8806 case TRUTH_ORIF_EXPR:
8807 case TRUTH_AND_EXPR:
8808 case TRUTH_OR_EXPR:
8809 case RSHIFT_EXPR:
8810 case LSHIFT_EXPR:
8811 case RROTATE_EXPR:
8812 case LROTATE_EXPR:
8813 case EQ_EXPR:
8814 case NE_EXPR:
8815 case MAX_EXPR:
8816 case MIN_EXPR:
8817 case LE_EXPR:
8818 case GE_EXPR:
8819 case LT_EXPR:
8820 case GT_EXPR:
8821 case MEMBER_REF:
8822 case DOTSTAR_EXPR:
8823 return build_x_binary_op
8824 (TREE_CODE (t),
8825 RECUR (TREE_OPERAND (t, 0)),
8826 RECUR (TREE_OPERAND (t, 1)),
8827 /*overloaded_p=*/NULL);
8829 case SCOPE_REF:
8830 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8831 /*address_p=*/false);
8832 case ARRAY_REF:
8833 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8834 args, complain, in_decl);
8835 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8836 /*overloaded_p=*/NULL);
8838 case SIZEOF_EXPR:
8839 case ALIGNOF_EXPR:
8840 op1 = TREE_OPERAND (t, 0);
8841 if (!args)
8843 /* When there are no ARGS, we are trying to evaluate a
8844 non-dependent expression from the parser. Trying to do
8845 the substitutions may not work. */
8846 if (!TYPE_P (op1))
8847 op1 = TREE_TYPE (op1);
8849 else
8851 ++skip_evaluation;
8852 op1 = RECUR (op1);
8853 --skip_evaluation;
8855 if (TYPE_P (op1))
8856 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8857 else
8858 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8860 case MODOP_EXPR:
8862 tree r = build_x_modify_expr
8863 (RECUR (TREE_OPERAND (t, 0)),
8864 TREE_CODE (TREE_OPERAND (t, 1)),
8865 RECUR (TREE_OPERAND (t, 2)));
8866 /* TREE_NO_WARNING must be set if either the expression was
8867 parenthesized or it uses an operator such as >>= rather
8868 than plain assignment. In the former case, it was already
8869 set and must be copied. In the latter case,
8870 build_x_modify_expr sets it and it must not be reset
8871 here. */
8872 if (TREE_NO_WARNING (t))
8873 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8874 return r;
8877 case ARROW_EXPR:
8878 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8879 args, complain, in_decl);
8880 /* Remember that there was a reference to this entity. */
8881 if (DECL_P (op1))
8882 mark_used (op1);
8883 return build_x_arrow (op1);
8885 case NEW_EXPR:
8886 return build_new
8887 (RECUR (TREE_OPERAND (t, 0)),
8888 RECUR (TREE_OPERAND (t, 1)),
8889 RECUR (TREE_OPERAND (t, 2)),
8890 RECUR (TREE_OPERAND (t, 3)),
8891 NEW_EXPR_USE_GLOBAL (t));
8893 case DELETE_EXPR:
8894 return delete_sanity
8895 (RECUR (TREE_OPERAND (t, 0)),
8896 RECUR (TREE_OPERAND (t, 1)),
8897 DELETE_EXPR_USE_VEC (t),
8898 DELETE_EXPR_USE_GLOBAL (t));
8900 case COMPOUND_EXPR:
8901 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8902 RECUR (TREE_OPERAND (t, 1)));
8904 case CALL_EXPR:
8906 tree function;
8907 tree call_args;
8908 bool qualified_p;
8909 bool koenig_p;
8911 function = TREE_OPERAND (t, 0);
8912 /* When we parsed the expression, we determined whether or
8913 not Koenig lookup should be performed. */
8914 koenig_p = KOENIG_LOOKUP_P (t);
8915 if (TREE_CODE (function) == SCOPE_REF)
8917 qualified_p = true;
8918 function = tsubst_qualified_id (function, args, complain, in_decl,
8919 /*done=*/false,
8920 /*address_p=*/false);
8922 else
8924 if (TREE_CODE (function) == COMPONENT_REF)
8926 tree op = TREE_OPERAND (function, 1);
8928 qualified_p = (TREE_CODE (op) == SCOPE_REF
8929 || (BASELINK_P (op)
8930 && BASELINK_QUALIFIED_P (op)));
8932 else
8933 qualified_p = false;
8935 function = tsubst_copy_and_build (function, args, complain,
8936 in_decl,
8937 !qualified_p);
8938 if (BASELINK_P (function))
8939 qualified_p = true;
8942 call_args = RECUR (TREE_OPERAND (t, 1));
8944 /* We do not perform argument-dependent lookup if normal
8945 lookup finds a non-function, in accordance with the
8946 expected resolution of DR 218. */
8947 if (koenig_p
8948 && ((is_overloaded_fn (function)
8949 /* If lookup found a member function, the Koenig lookup is
8950 not appropriate, even if an unqualified-name was used
8951 to denote the function. */
8952 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
8953 || TREE_CODE (function) == IDENTIFIER_NODE))
8954 function = perform_koenig_lookup (function, call_args);
8956 if (TREE_CODE (function) == IDENTIFIER_NODE)
8958 unqualified_name_lookup_error (function);
8959 return error_mark_node;
8962 /* Remember that there was a reference to this entity. */
8963 if (DECL_P (function))
8964 mark_used (function);
8966 if (TREE_CODE (function) == OFFSET_REF)
8967 return build_offset_ref_call_from_tree (function, call_args);
8968 if (TREE_CODE (function) == COMPONENT_REF)
8970 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8971 return finish_call_expr (function, call_args,
8972 /*disallow_virtual=*/false,
8973 /*koenig_p=*/false);
8974 else
8975 return (build_new_method_call
8976 (TREE_OPERAND (function, 0),
8977 TREE_OPERAND (function, 1),
8978 call_args, NULL_TREE,
8979 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8981 return finish_call_expr (function, call_args,
8982 /*disallow_virtual=*/qualified_p,
8983 koenig_p);
8986 case COND_EXPR:
8987 return build_x_conditional_expr
8988 (RECUR (TREE_OPERAND (t, 0)),
8989 RECUR (TREE_OPERAND (t, 1)),
8990 RECUR (TREE_OPERAND (t, 2)));
8992 case PSEUDO_DTOR_EXPR:
8993 return finish_pseudo_destructor_expr
8994 (RECUR (TREE_OPERAND (t, 0)),
8995 RECUR (TREE_OPERAND (t, 1)),
8996 RECUR (TREE_OPERAND (t, 2)));
8998 case TREE_LIST:
9000 tree purpose, value, chain;
9002 if (t == void_list_node)
9003 return t;
9005 purpose = TREE_PURPOSE (t);
9006 if (purpose)
9007 purpose = RECUR (purpose);
9008 value = TREE_VALUE (t);
9009 if (value)
9010 value = RECUR (value);
9011 chain = TREE_CHAIN (t);
9012 if (chain && chain != void_type_node)
9013 chain = RECUR (chain);
9014 if (purpose == TREE_PURPOSE (t)
9015 && value == TREE_VALUE (t)
9016 && chain == TREE_CHAIN (t))
9017 return t;
9018 return tree_cons (purpose, value, chain);
9021 case COMPONENT_REF:
9023 tree object;
9024 tree object_type;
9025 tree member;
9027 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9028 args, complain, in_decl);
9029 /* Remember that there was a reference to this entity. */
9030 if (DECL_P (object))
9031 mark_used (object);
9032 object_type = TREE_TYPE (object);
9034 member = TREE_OPERAND (t, 1);
9035 if (BASELINK_P (member))
9036 member = tsubst_baselink (member,
9037 non_reference (TREE_TYPE (object)),
9038 args, complain, in_decl);
9039 else
9040 member = tsubst_copy (member, args, complain, in_decl);
9041 if (member == error_mark_node)
9042 return error_mark_node;
9044 if (object_type && !CLASS_TYPE_P (object_type))
9046 if (TREE_CODE (member) == BIT_NOT_EXPR)
9047 return finish_pseudo_destructor_expr (object,
9048 NULL_TREE,
9049 object_type);
9050 else if (TREE_CODE (member) == SCOPE_REF
9051 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
9052 return finish_pseudo_destructor_expr (object,
9053 object,
9054 object_type);
9056 else if (TREE_CODE (member) == SCOPE_REF
9057 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9059 tree tmpl;
9060 tree args;
9062 /* Lookup the template functions now that we know what the
9063 scope is. */
9064 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9065 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
9066 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
9067 /*is_type_p=*/false,
9068 /*complain=*/false);
9069 if (BASELINK_P (member))
9071 BASELINK_FUNCTIONS (member)
9072 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9073 args);
9074 member = (adjust_result_of_qualified_name_lookup
9075 (member, BINFO_TYPE (BASELINK_BINFO (member)),
9076 object_type));
9078 else
9080 qualified_name_lookup_error (object_type, tmpl, member);
9081 return error_mark_node;
9084 else if (TREE_CODE (member) == SCOPE_REF
9085 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9086 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9088 if (complain & tf_error)
9090 if (TYPE_P (TREE_OPERAND (member, 0)))
9091 error ("%qT is not a class or namespace",
9092 TREE_OPERAND (member, 0));
9093 else
9094 error ("%qD is not a class or namespace",
9095 TREE_OPERAND (member, 0));
9097 return error_mark_node;
9099 else if (TREE_CODE (member) == FIELD_DECL)
9100 return finish_non_static_data_member (member, object, NULL_TREE);
9102 return finish_class_member_access_expr (object, member,
9103 /*template_p=*/false);
9106 case THROW_EXPR:
9107 return build_throw
9108 (RECUR (TREE_OPERAND (t, 0)));
9110 case CONSTRUCTOR:
9112 VEC(constructor_elt,gc) *n;
9113 constructor_elt *ce;
9114 unsigned HOST_WIDE_INT idx;
9115 tree r;
9116 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9117 bool process_index_p;
9119 /* digest_init will do the wrong thing if we let it. */
9120 if (type && TYPE_PTRMEMFUNC_P (type))
9121 return t;
9123 /* We do not want to process the index of aggregate
9124 initializers as they are identifier nodes which will be
9125 looked up by digest_init. */
9126 process_index_p = !(type && IS_AGGR_TYPE (type));
9128 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9129 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9131 if (ce->index && process_index_p)
9132 ce->index = RECUR (ce->index);
9133 ce->value = RECUR (ce->value);
9136 r = build_constructor (NULL_TREE, n);
9137 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
9139 if (type)
9140 return digest_init (type, r);
9141 return r;
9144 case TYPEID_EXPR:
9146 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9147 if (TYPE_P (operand_0))
9148 return get_typeid (operand_0);
9149 return build_typeid (operand_0);
9152 case VAR_DECL:
9153 if (!args)
9154 return t;
9155 /* Fall through */
9157 case PARM_DECL:
9159 tree r = tsubst_copy (t, args, complain, in_decl);
9161 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9162 /* If the original type was a reference, we'll be wrapped in
9163 the appropriate INDIRECT_REF. */
9164 r = convert_from_reference (r);
9165 return r;
9168 case VA_ARG_EXPR:
9169 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9170 tsubst_copy (TREE_TYPE (t), args, complain,
9171 in_decl));
9173 case OFFSETOF_EXPR:
9174 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
9176 case STMT_EXPR:
9178 tree old_stmt_expr = cur_stmt_expr;
9179 tree stmt_expr = begin_stmt_expr ();
9181 cur_stmt_expr = stmt_expr;
9182 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
9183 stmt_expr = finish_stmt_expr (stmt_expr, false);
9184 cur_stmt_expr = old_stmt_expr;
9186 return stmt_expr;
9189 case CONST_DECL:
9190 t = tsubst_copy (t, args, complain, in_decl);
9191 /* As in finish_id_expression, we resolve enumeration constants
9192 to their underlying values. */
9193 if (TREE_CODE (t) == CONST_DECL)
9194 return DECL_INITIAL (t);
9195 return t;
9197 default:
9198 /* Handle Objective-C++ constructs, if appropriate. */
9200 tree subst
9201 = objcp_tsubst_copy_and_build (t, args, complain,
9202 in_decl, /*function_p=*/false);
9203 if (subst)
9204 return subst;
9206 return tsubst_copy (t, args, complain, in_decl);
9209 #undef RECUR
9212 /* Verify that the instantiated ARGS are valid. For type arguments,
9213 make sure that the type's linkage is ok. For non-type arguments,
9214 make sure they are constants if they are integral or enumerations.
9215 Emit an error under control of COMPLAIN, and return TRUE on error. */
9217 static bool
9218 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9220 int ix, len = DECL_NTPARMS (tmpl);
9221 bool result = false;
9223 for (ix = 0; ix != len; ix++)
9225 tree t = TREE_VEC_ELT (args, ix);
9227 if (TYPE_P (t))
9229 /* [basic.link]: A name with no linkage (notably, the name
9230 of a class or enumeration declared in a local scope)
9231 shall not be used to declare an entity with linkage.
9232 This implies that names with no linkage cannot be used as
9233 template arguments. */
9234 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9236 if (nt)
9238 /* DR 488 makes use of a type with no linkage cause
9239 type deduction to fail. */
9240 if (complain & tf_error)
9242 if (TYPE_ANONYMOUS_P (nt))
9243 error ("%qT is/uses anonymous type", t);
9244 else
9245 error ("%qT uses local type %qT", t, nt);
9247 result = true;
9249 /* In order to avoid all sorts of complications, we do not
9250 allow variably-modified types as template arguments. */
9251 else if (variably_modified_type_p (t, NULL_TREE))
9253 if (complain & tf_error)
9254 error ("%qT is a variably modified type", t);
9255 result = true;
9258 /* A non-type argument of integral or enumerated type must be a
9259 constant. */
9260 else if (TREE_TYPE (t)
9261 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9262 && !TREE_CONSTANT (t))
9264 if (complain & tf_error)
9265 error ("integral expression %qE is not constant", t);
9266 result = true;
9269 if (result && (complain & tf_error))
9270 error (" trying to instantiate %qD", tmpl);
9271 return result;
9274 /* Instantiate the indicated variable or function template TMPL with
9275 the template arguments in TARG_PTR. */
9277 tree
9278 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9280 tree fndecl;
9281 tree gen_tmpl;
9282 tree spec;
9284 if (tmpl == error_mark_node)
9285 return error_mark_node;
9287 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9289 /* If this function is a clone, handle it specially. */
9290 if (DECL_CLONED_FUNCTION_P (tmpl))
9292 tree spec;
9293 tree clone;
9295 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9296 complain);
9297 if (spec == error_mark_node)
9298 return error_mark_node;
9300 /* Look for the clone. */
9301 FOR_EACH_CLONE (clone, spec)
9302 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9303 return clone;
9304 /* We should always have found the clone by now. */
9305 gcc_unreachable ();
9306 return NULL_TREE;
9309 /* Check to see if we already have this specialization. */
9310 spec = retrieve_specialization (tmpl, targ_ptr,
9311 /*class_specializations_p=*/false);
9312 if (spec != NULL_TREE)
9313 return spec;
9315 gen_tmpl = most_general_template (tmpl);
9316 if (tmpl != gen_tmpl)
9318 /* The TMPL is a partial instantiation. To get a full set of
9319 arguments we must add the arguments used to perform the
9320 partial instantiation. */
9321 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9322 targ_ptr);
9324 /* Check to see if we already have this specialization. */
9325 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9326 /*class_specializations_p=*/false);
9327 if (spec != NULL_TREE)
9328 return spec;
9331 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9332 complain))
9333 return error_mark_node;
9335 /* We are building a FUNCTION_DECL, during which the access of its
9336 parameters and return types have to be checked. However this
9337 FUNCTION_DECL which is the desired context for access checking
9338 is not built yet. We solve this chicken-and-egg problem by
9339 deferring all checks until we have the FUNCTION_DECL. */
9340 push_deferring_access_checks (dk_deferred);
9342 /* Substitute template parameters. */
9343 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9344 targ_ptr, complain, gen_tmpl);
9345 if (fndecl == error_mark_node)
9346 return error_mark_node;
9348 /* Now we know the specialization, compute access previously
9349 deferred. */
9350 push_access_scope (fndecl);
9351 perform_deferred_access_checks ();
9352 pop_access_scope (fndecl);
9353 pop_deferring_access_checks ();
9355 /* The DECL_TI_TEMPLATE should always be the immediate parent
9356 template, not the most general template. */
9357 DECL_TI_TEMPLATE (fndecl) = tmpl;
9359 /* If we've just instantiated the main entry point for a function,
9360 instantiate all the alternate entry points as well. We do this
9361 by cloning the instantiation of the main entry point, not by
9362 instantiating the template clones. */
9363 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9364 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9366 return fndecl;
9369 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9370 arguments that are being used when calling it. TARGS is a vector
9371 into which the deduced template arguments are placed.
9373 Return zero for success, 2 for an incomplete match that doesn't resolve
9374 all the types, and 1 for complete failure. An error message will be
9375 printed only for an incomplete match.
9377 If FN is a conversion operator, or we are trying to produce a specific
9378 specialization, RETURN_TYPE is the return type desired.
9380 The EXPLICIT_TARGS are explicit template arguments provided via a
9381 template-id.
9383 The parameter STRICT is one of:
9385 DEDUCE_CALL:
9386 We are deducing arguments for a function call, as in
9387 [temp.deduct.call].
9389 DEDUCE_CONV:
9390 We are deducing arguments for a conversion function, as in
9391 [temp.deduct.conv].
9393 DEDUCE_EXACT:
9394 We are deducing arguments when doing an explicit instantiation
9395 as in [temp.explicit], when determining an explicit specialization
9396 as in [temp.expl.spec], or when taking the address of a function
9397 template, as in [temp.deduct.funcaddr]. */
9400 fn_type_unification (tree fn,
9401 tree explicit_targs,
9402 tree targs,
9403 tree args,
9404 tree return_type,
9405 unification_kind_t strict,
9406 int flags)
9408 tree parms;
9409 tree fntype;
9410 int result;
9412 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9414 fntype = TREE_TYPE (fn);
9415 if (explicit_targs)
9417 /* [temp.deduct]
9419 The specified template arguments must match the template
9420 parameters in kind (i.e., type, nontype, template), and there
9421 must not be more arguments than there are parameters;
9422 otherwise type deduction fails.
9424 Nontype arguments must match the types of the corresponding
9425 nontype template parameters, or must be convertible to the
9426 types of the corresponding nontype parameters as specified in
9427 _temp.arg.nontype_, otherwise type deduction fails.
9429 All references in the function type of the function template
9430 to the corresponding template parameters are replaced by the
9431 specified template argument values. If a substitution in a
9432 template parameter or in the function type of the function
9433 template results in an invalid type, type deduction fails. */
9434 int i;
9435 tree converted_args;
9436 bool incomplete;
9438 if (explicit_targs == error_mark_node)
9439 return 1;
9441 converted_args
9442 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9443 explicit_targs, NULL_TREE, tf_none,
9444 /*require_all_arguments=*/0));
9445 if (converted_args == error_mark_node)
9446 return 1;
9448 /* Substitute the explicit args into the function type. This is
9449 necessary so that, for instance, explicitly declared function
9450 arguments can match null pointed constants. If we were given
9451 an incomplete set of explicit args, we must not do semantic
9452 processing during substitution as we could create partial
9453 instantiations. */
9454 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9455 processing_template_decl += incomplete;
9456 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9457 processing_template_decl -= incomplete;
9459 if (fntype == error_mark_node)
9460 return 1;
9462 /* Place the explicitly specified arguments in TARGS. */
9463 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9464 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9467 parms = TYPE_ARG_TYPES (fntype);
9468 /* Never do unification on the 'this' parameter. */
9469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9470 parms = TREE_CHAIN (parms);
9472 if (return_type)
9474 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9475 args = tree_cons (NULL_TREE, return_type, args);
9478 /* We allow incomplete unification without an error message here
9479 because the standard doesn't seem to explicitly prohibit it. Our
9480 callers must be ready to deal with unification failures in any
9481 event. */
9482 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9483 targs, parms, args, /*subr=*/0,
9484 strict, flags);
9486 if (result == 0)
9487 /* All is well so far. Now, check:
9489 [temp.deduct]
9491 When all template arguments have been deduced, all uses of
9492 template parameters in nondeduced contexts are replaced with
9493 the corresponding deduced argument values. If the
9494 substitution results in an invalid type, as described above,
9495 type deduction fails. */
9496 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9497 == error_mark_node)
9498 return 1;
9500 return result;
9503 /* Adjust types before performing type deduction, as described in
9504 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9505 sections are symmetric. PARM is the type of a function parameter
9506 or the return type of the conversion function. ARG is the type of
9507 the argument passed to the call, or the type of the value
9508 initialized with the result of the conversion function. */
9510 static int
9511 maybe_adjust_types_for_deduction (unification_kind_t strict,
9512 tree* parm,
9513 tree* arg)
9515 int result = 0;
9517 switch (strict)
9519 case DEDUCE_CALL:
9520 break;
9522 case DEDUCE_CONV:
9524 /* Swap PARM and ARG throughout the remainder of this
9525 function; the handling is precisely symmetric since PARM
9526 will initialize ARG rather than vice versa. */
9527 tree* temp = parm;
9528 parm = arg;
9529 arg = temp;
9530 break;
9533 case DEDUCE_EXACT:
9534 /* There is nothing to do in this case. */
9535 return 0;
9537 default:
9538 gcc_unreachable ();
9541 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9543 /* [temp.deduct.call]
9545 If P is not a reference type:
9547 --If A is an array type, the pointer type produced by the
9548 array-to-pointer standard conversion (_conv.array_) is
9549 used in place of A for type deduction; otherwise,
9551 --If A is a function type, the pointer type produced by
9552 the function-to-pointer standard conversion
9553 (_conv.func_) is used in place of A for type deduction;
9554 otherwise,
9556 --If A is a cv-qualified type, the top level
9557 cv-qualifiers of A's type are ignored for type
9558 deduction. */
9559 if (TREE_CODE (*arg) == ARRAY_TYPE)
9560 *arg = build_pointer_type (TREE_TYPE (*arg));
9561 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9562 *arg = build_pointer_type (*arg);
9563 else
9564 *arg = TYPE_MAIN_VARIANT (*arg);
9567 /* [temp.deduct.call]
9569 If P is a cv-qualified type, the top level cv-qualifiers
9570 of P's type are ignored for type deduction. If P is a
9571 reference type, the type referred to by P is used for
9572 type deduction. */
9573 *parm = TYPE_MAIN_VARIANT (*parm);
9574 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9576 *parm = TREE_TYPE (*parm);
9577 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9580 /* DR 322. For conversion deduction, remove a reference type on parm
9581 too (which has been swapped into ARG). */
9582 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9583 *arg = TREE_TYPE (*arg);
9585 return result;
9588 /* Most parms like fn_type_unification.
9590 If SUBR is 1, we're being called recursively (to unify the
9591 arguments of a function or method parameter of a function
9592 template). */
9594 static int
9595 type_unification_real (tree tparms,
9596 tree targs,
9597 tree xparms,
9598 tree xargs,
9599 int subr,
9600 unification_kind_t strict,
9601 int flags)
9603 tree parm, arg;
9604 int i;
9605 int ntparms = TREE_VEC_LENGTH (tparms);
9606 int sub_strict;
9607 int saw_undeduced = 0;
9608 tree parms, args;
9610 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9611 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9612 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9613 gcc_assert (ntparms > 0);
9615 switch (strict)
9617 case DEDUCE_CALL:
9618 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9619 | UNIFY_ALLOW_DERIVED);
9620 break;
9622 case DEDUCE_CONV:
9623 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9624 break;
9626 case DEDUCE_EXACT:
9627 sub_strict = UNIFY_ALLOW_NONE;
9628 break;
9630 default:
9631 gcc_unreachable ();
9634 again:
9635 parms = xparms;
9636 args = xargs;
9638 while (parms && parms != void_list_node
9639 && args && args != void_list_node)
9641 parm = TREE_VALUE (parms);
9642 parms = TREE_CHAIN (parms);
9643 arg = TREE_VALUE (args);
9644 args = TREE_CHAIN (args);
9646 if (arg == error_mark_node)
9647 return 1;
9648 if (arg == unknown_type_node)
9649 /* We can't deduce anything from this, but we might get all the
9650 template args from other function args. */
9651 continue;
9653 /* Conversions will be performed on a function argument that
9654 corresponds with a function parameter that contains only
9655 non-deducible template parameters and explicitly specified
9656 template parameters. */
9657 if (!uses_template_parms (parm))
9659 tree type;
9661 if (!TYPE_P (arg))
9662 type = TREE_TYPE (arg);
9663 else
9664 type = arg;
9666 if (same_type_p (parm, type))
9667 continue;
9668 if (strict != DEDUCE_EXACT
9669 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9670 flags))
9671 continue;
9673 return 1;
9676 if (!TYPE_P (arg))
9678 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9679 if (type_unknown_p (arg))
9681 /* [temp.deduct.type] A template-argument can be deduced from
9682 a pointer to function or pointer to member function
9683 argument if the set of overloaded functions does not
9684 contain function templates and at most one of a set of
9685 overloaded functions provides a unique match. */
9687 if (resolve_overloaded_unification
9688 (tparms, targs, parm, arg, strict, sub_strict)
9689 != 0)
9690 return 1;
9691 continue;
9693 arg = TREE_TYPE (arg);
9694 if (arg == error_mark_node)
9695 return 1;
9699 int arg_strict = sub_strict;
9701 if (!subr)
9702 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9704 if (unify (tparms, targs, parm, arg, arg_strict))
9705 return 1;
9709 /* Fail if we've reached the end of the parm list, and more args
9710 are present, and the parm list isn't variadic. */
9711 if (args && args != void_list_node && parms == void_list_node)
9712 return 1;
9713 /* Fail if parms are left and they don't have default values. */
9714 if (parms && parms != void_list_node
9715 && TREE_PURPOSE (parms) == NULL_TREE)
9716 return 1;
9718 if (!subr)
9719 for (i = 0; i < ntparms; i++)
9720 if (!TREE_VEC_ELT (targs, i))
9722 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9724 /* If this is an undeduced nontype parameter that depends on
9725 a type parameter, try another pass; its type may have been
9726 deduced from a later argument than the one from which
9727 this parameter can be deduced. */
9728 if (TREE_CODE (tparm) == PARM_DECL
9729 && uses_template_parms (TREE_TYPE (tparm))
9730 && !saw_undeduced++)
9731 goto again;
9733 return 2;
9736 return 0;
9739 /* Subroutine of type_unification_real. Args are like the variables at the
9740 call site. ARG is an overloaded function (or template-id); we try
9741 deducing template args from each of the overloads, and if only one
9742 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9744 static int
9745 resolve_overloaded_unification (tree tparms,
9746 tree targs,
9747 tree parm,
9748 tree arg,
9749 unification_kind_t strict,
9750 int sub_strict)
9752 tree tempargs = copy_node (targs);
9753 int good = 0;
9754 bool addr_p;
9756 if (TREE_CODE (arg) == ADDR_EXPR)
9758 arg = TREE_OPERAND (arg, 0);
9759 addr_p = true;
9761 else
9762 addr_p = false;
9764 if (TREE_CODE (arg) == COMPONENT_REF)
9765 /* Handle `&x' where `x' is some static or non-static member
9766 function name. */
9767 arg = TREE_OPERAND (arg, 1);
9769 if (TREE_CODE (arg) == OFFSET_REF)
9770 arg = TREE_OPERAND (arg, 1);
9772 /* Strip baselink information. */
9773 if (BASELINK_P (arg))
9774 arg = BASELINK_FUNCTIONS (arg);
9776 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9778 /* If we got some explicit template args, we need to plug them into
9779 the affected templates before we try to unify, in case the
9780 explicit args will completely resolve the templates in question. */
9782 tree expl_subargs = TREE_OPERAND (arg, 1);
9783 arg = TREE_OPERAND (arg, 0);
9785 for (; arg; arg = OVL_NEXT (arg))
9787 tree fn = OVL_CURRENT (arg);
9788 tree subargs, elem;
9790 if (TREE_CODE (fn) != TEMPLATE_DECL)
9791 continue;
9793 ++processing_template_decl;
9794 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9795 expl_subargs, /*check_ret=*/false);
9796 if (subargs)
9798 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9799 good += try_one_overload (tparms, targs, tempargs, parm,
9800 elem, strict, sub_strict, addr_p);
9802 --processing_template_decl;
9805 else
9807 gcc_assert (TREE_CODE (arg) == OVERLOAD
9808 || TREE_CODE (arg) == FUNCTION_DECL);
9810 for (; arg; arg = OVL_NEXT (arg))
9811 good += try_one_overload (tparms, targs, tempargs, parm,
9812 TREE_TYPE (OVL_CURRENT (arg)),
9813 strict, sub_strict, addr_p);
9816 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9817 to function or pointer to member function argument if the set of
9818 overloaded functions does not contain function templates and at most
9819 one of a set of overloaded functions provides a unique match.
9821 So if we found multiple possibilities, we return success but don't
9822 deduce anything. */
9824 if (good == 1)
9826 int i = TREE_VEC_LENGTH (targs);
9827 for (; i--; )
9828 if (TREE_VEC_ELT (tempargs, i))
9829 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9831 if (good)
9832 return 0;
9834 return 1;
9837 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9838 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9839 different overloads deduce different arguments for a given parm.
9840 ADDR_P is true if the expression for which deduction is being
9841 performed was of the form "& fn" rather than simply "fn".
9843 Returns 1 on success. */
9845 static int
9846 try_one_overload (tree tparms,
9847 tree orig_targs,
9848 tree targs,
9849 tree parm,
9850 tree arg,
9851 unification_kind_t strict,
9852 int sub_strict,
9853 bool addr_p)
9855 int nargs;
9856 tree tempargs;
9857 int i;
9859 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9860 to function or pointer to member function argument if the set of
9861 overloaded functions does not contain function templates and at most
9862 one of a set of overloaded functions provides a unique match.
9864 So if this is a template, just return success. */
9866 if (uses_template_parms (arg))
9867 return 1;
9869 if (TREE_CODE (arg) == METHOD_TYPE)
9870 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9871 else if (addr_p)
9872 arg = build_pointer_type (arg);
9874 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9876 /* We don't copy orig_targs for this because if we have already deduced
9877 some template args from previous args, unify would complain when we
9878 try to deduce a template parameter for the same argument, even though
9879 there isn't really a conflict. */
9880 nargs = TREE_VEC_LENGTH (targs);
9881 tempargs = make_tree_vec (nargs);
9883 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9884 return 0;
9886 /* First make sure we didn't deduce anything that conflicts with
9887 explicitly specified args. */
9888 for (i = nargs; i--; )
9890 tree elt = TREE_VEC_ELT (tempargs, i);
9891 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9893 if (!elt)
9894 /*NOP*/;
9895 else if (uses_template_parms (elt))
9896 /* Since we're unifying against ourselves, we will fill in
9897 template args used in the function parm list with our own
9898 template parms. Discard them. */
9899 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9900 else if (oldelt && !template_args_equal (oldelt, elt))
9901 return 0;
9904 for (i = nargs; i--; )
9906 tree elt = TREE_VEC_ELT (tempargs, i);
9908 if (elt)
9909 TREE_VEC_ELT (targs, i) = elt;
9912 return 1;
9915 /* PARM is a template class (perhaps with unbound template
9916 parameters). ARG is a fully instantiated type. If ARG can be
9917 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9918 TARGS are as for unify. */
9920 static tree
9921 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9923 tree copy_of_targs;
9925 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9926 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9927 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9928 return NULL_TREE;
9930 /* We need to make a new template argument vector for the call to
9931 unify. If we used TARGS, we'd clutter it up with the result of
9932 the attempted unification, even if this class didn't work out.
9933 We also don't want to commit ourselves to all the unifications
9934 we've already done, since unification is supposed to be done on
9935 an argument-by-argument basis. In other words, consider the
9936 following pathological case:
9938 template <int I, int J, int K>
9939 struct S {};
9941 template <int I, int J>
9942 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9944 template <int I, int J, int K>
9945 void f(S<I, J, K>, S<I, I, I>);
9947 void g() {
9948 S<0, 0, 0> s0;
9949 S<0, 1, 2> s2;
9951 f(s0, s2);
9954 Now, by the time we consider the unification involving `s2', we
9955 already know that we must have `f<0, 0, 0>'. But, even though
9956 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9957 because there are two ways to unify base classes of S<0, 1, 2>
9958 with S<I, I, I>. If we kept the already deduced knowledge, we
9959 would reject the possibility I=1. */
9960 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9962 /* If unification failed, we're done. */
9963 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9964 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9965 return NULL_TREE;
9967 return arg;
9970 /* Given a template type PARM and a class type ARG, find the unique
9971 base type in ARG that is an instance of PARM. We do not examine
9972 ARG itself; only its base-classes. If there is not exactly one
9973 appropriate base class, return NULL_TREE. PARM may be the type of
9974 a partial specialization, as well as a plain template type. Used
9975 by unify. */
9977 static tree
9978 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9980 tree rval = NULL_TREE;
9981 tree binfo;
9983 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9985 binfo = TYPE_BINFO (complete_type (arg));
9986 if (!binfo)
9987 /* The type could not be completed. */
9988 return NULL_TREE;
9990 /* Walk in inheritance graph order. The search order is not
9991 important, and this avoids multiple walks of virtual bases. */
9992 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9994 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9996 if (r)
9998 /* If there is more than one satisfactory baseclass, then:
10000 [temp.deduct.call]
10002 If they yield more than one possible deduced A, the type
10003 deduction fails.
10005 applies. */
10006 if (rval && !same_type_p (r, rval))
10007 return NULL_TREE;
10009 rval = r;
10013 return rval;
10016 /* Returns the level of DECL, which declares a template parameter. */
10018 static int
10019 template_decl_level (tree decl)
10021 switch (TREE_CODE (decl))
10023 case TYPE_DECL:
10024 case TEMPLATE_DECL:
10025 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10027 case PARM_DECL:
10028 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10030 default:
10031 gcc_unreachable ();
10033 return 0;
10036 /* Decide whether ARG can be unified with PARM, considering only the
10037 cv-qualifiers of each type, given STRICT as documented for unify.
10038 Returns nonzero iff the unification is OK on that basis. */
10040 static int
10041 check_cv_quals_for_unify (int strict, tree arg, tree parm)
10043 int arg_quals = cp_type_quals (arg);
10044 int parm_quals = cp_type_quals (parm);
10046 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10047 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10049 /* Although a CVR qualifier is ignored when being applied to a
10050 substituted template parameter ([8.3.2]/1 for example), that
10051 does not apply during deduction [14.8.2.4]/1, (even though
10052 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10053 this). Except when we're allowing additional CV qualifiers
10054 at the outer level [14.8.2.1]/3,1st bullet. */
10055 if ((TREE_CODE (arg) == REFERENCE_TYPE
10056 || TREE_CODE (arg) == FUNCTION_TYPE
10057 || TREE_CODE (arg) == METHOD_TYPE)
10058 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10059 return 0;
10061 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10062 && (parm_quals & TYPE_QUAL_RESTRICT))
10063 return 0;
10066 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10067 && (arg_quals & parm_quals) != parm_quals)
10068 return 0;
10070 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
10071 && (parm_quals & arg_quals) != arg_quals)
10072 return 0;
10074 return 1;
10077 /* Deduce the value of template parameters. TPARMS is the (innermost)
10078 set of template parameters to a template. TARGS is the bindings
10079 for those template parameters, as determined thus far; TARGS may
10080 include template arguments for outer levels of template parameters
10081 as well. PARM is a parameter to a template function, or a
10082 subcomponent of that parameter; ARG is the corresponding argument.
10083 This function attempts to match PARM with ARG in a manner
10084 consistent with the existing assignments in TARGS. If more values
10085 are deduced, then TARGS is updated.
10087 Returns 0 if the type deduction succeeds, 1 otherwise. The
10088 parameter STRICT is a bitwise or of the following flags:
10090 UNIFY_ALLOW_NONE:
10091 Require an exact match between PARM and ARG.
10092 UNIFY_ALLOW_MORE_CV_QUAL:
10093 Allow the deduced ARG to be more cv-qualified (by qualification
10094 conversion) than ARG.
10095 UNIFY_ALLOW_LESS_CV_QUAL:
10096 Allow the deduced ARG to be less cv-qualified than ARG.
10097 UNIFY_ALLOW_DERIVED:
10098 Allow the deduced ARG to be a template base class of ARG,
10099 or a pointer to a template base class of the type pointed to by
10100 ARG.
10101 UNIFY_ALLOW_INTEGER:
10102 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10103 case for more information.
10104 UNIFY_ALLOW_OUTER_LEVEL:
10105 This is the outermost level of a deduction. Used to determine validity
10106 of qualification conversions. A valid qualification conversion must
10107 have const qualified pointers leading up to the inner type which
10108 requires additional CV quals, except at the outer level, where const
10109 is not required [conv.qual]. It would be normal to set this flag in
10110 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10111 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10112 This is the outermost level of a deduction, and PARM can be more CV
10113 qualified at this point.
10114 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10115 This is the outermost level of a deduction, and PARM can be less CV
10116 qualified at this point. */
10118 static int
10119 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10121 int idx;
10122 tree targ;
10123 tree tparm;
10124 int strict_in = strict;
10126 /* I don't think this will do the right thing with respect to types.
10127 But the only case I've seen it in so far has been array bounds, where
10128 signedness is the only information lost, and I think that will be
10129 okay. */
10130 while (TREE_CODE (parm) == NOP_EXPR)
10131 parm = TREE_OPERAND (parm, 0);
10133 if (arg == error_mark_node)
10134 return 1;
10135 if (arg == unknown_type_node)
10136 /* We can't deduce anything from this, but we might get all the
10137 template args from other function args. */
10138 return 0;
10140 /* If PARM uses template parameters, then we can't bail out here,
10141 even if ARG == PARM, since we won't record unifications for the
10142 template parameters. We might need them if we're trying to
10143 figure out which of two things is more specialized. */
10144 if (arg == parm && !uses_template_parms (parm))
10145 return 0;
10147 /* Immediately reject some pairs that won't unify because of
10148 cv-qualification mismatches. */
10149 if (TREE_CODE (arg) == TREE_CODE (parm)
10150 && TYPE_P (arg)
10151 /* It is the elements of the array which hold the cv quals of an array
10152 type, and the elements might be template type parms. We'll check
10153 when we recurse. */
10154 && TREE_CODE (arg) != ARRAY_TYPE
10155 /* We check the cv-qualifiers when unifying with template type
10156 parameters below. We want to allow ARG `const T' to unify with
10157 PARM `T' for example, when computing which of two templates
10158 is more specialized, for example. */
10159 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10160 && !check_cv_quals_for_unify (strict_in, arg, parm))
10161 return 1;
10163 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10164 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10165 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10166 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10167 strict &= ~UNIFY_ALLOW_DERIVED;
10168 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10169 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10171 switch (TREE_CODE (parm))
10173 case TYPENAME_TYPE:
10174 case SCOPE_REF:
10175 case UNBOUND_CLASS_TEMPLATE:
10176 /* In a type which contains a nested-name-specifier, template
10177 argument values cannot be deduced for template parameters used
10178 within the nested-name-specifier. */
10179 return 0;
10181 case TEMPLATE_TYPE_PARM:
10182 case TEMPLATE_TEMPLATE_PARM:
10183 case BOUND_TEMPLATE_TEMPLATE_PARM:
10184 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10186 if (TEMPLATE_TYPE_LEVEL (parm)
10187 != template_decl_level (tparm))
10188 /* The PARM is not one we're trying to unify. Just check
10189 to see if it matches ARG. */
10190 return (TREE_CODE (arg) == TREE_CODE (parm)
10191 && same_type_p (parm, arg)) ? 0 : 1;
10192 idx = TEMPLATE_TYPE_IDX (parm);
10193 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10194 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10196 /* Check for mixed types and values. */
10197 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10198 && TREE_CODE (tparm) != TYPE_DECL)
10199 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10200 && TREE_CODE (tparm) != TEMPLATE_DECL))
10201 return 1;
10203 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10205 /* ARG must be constructed from a template class or a template
10206 template parameter. */
10207 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10208 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
10209 return 1;
10212 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
10213 tree parmvec = TYPE_TI_ARGS (parm);
10214 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10215 tree argtmplvec
10216 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10217 int i;
10219 /* The parameter and argument roles have to be switched here
10220 in order to handle default arguments properly. For example,
10221 template<template <class> class TT> void f(TT<int>)
10222 should be able to accept vector<int> which comes from
10223 template <class T, class Allocator = allocator>
10224 class vector. */
10226 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
10227 == error_mark_node)
10228 return 1;
10230 /* Deduce arguments T, i from TT<T> or TT<i>.
10231 We check each element of PARMVEC and ARGVEC individually
10232 rather than the whole TREE_VEC since they can have
10233 different number of elements. */
10235 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10237 if (unify (tparms, targs,
10238 TREE_VEC_ELT (parmvec, i),
10239 TREE_VEC_ELT (argvec, i),
10240 UNIFY_ALLOW_NONE))
10241 return 1;
10244 arg = TYPE_TI_TEMPLATE (arg);
10246 /* Fall through to deduce template name. */
10249 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10250 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10252 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10254 /* Simple cases: Value already set, does match or doesn't. */
10255 if (targ != NULL_TREE && template_args_equal (targ, arg))
10256 return 0;
10257 else if (targ)
10258 return 1;
10260 else
10262 /* If PARM is `const T' and ARG is only `int', we don't have
10263 a match unless we are allowing additional qualification.
10264 If ARG is `const int' and PARM is just `T' that's OK;
10265 that binds `const int' to `T'. */
10266 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10267 arg, parm))
10268 return 1;
10270 /* Consider the case where ARG is `const volatile int' and
10271 PARM is `const T'. Then, T should be `volatile int'. */
10272 arg = cp_build_qualified_type_real
10273 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10274 if (arg == error_mark_node)
10275 return 1;
10277 /* Simple cases: Value already set, does match or doesn't. */
10278 if (targ != NULL_TREE && same_type_p (targ, arg))
10279 return 0;
10280 else if (targ)
10281 return 1;
10283 /* Make sure that ARG is not a variable-sized array. (Note
10284 that were talking about variable-sized arrays (like
10285 `int[n]'), rather than arrays of unknown size (like
10286 `int[]').) We'll get very confused by such a type since
10287 the bound of the array will not be computable in an
10288 instantiation. Besides, such types are not allowed in
10289 ISO C++, so we can do as we please here. */
10290 if (variably_modified_type_p (arg, NULL_TREE))
10291 return 1;
10294 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10295 return 0;
10297 case TEMPLATE_PARM_INDEX:
10298 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10299 if (tparm == error_mark_node)
10300 return 1;
10302 if (TEMPLATE_PARM_LEVEL (parm)
10303 != template_decl_level (tparm))
10304 /* The PARM is not one we're trying to unify. Just check
10305 to see if it matches ARG. */
10306 return !(TREE_CODE (arg) == TREE_CODE (parm)
10307 && cp_tree_equal (parm, arg));
10309 idx = TEMPLATE_PARM_IDX (parm);
10310 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10312 if (targ)
10313 return !cp_tree_equal (targ, arg);
10315 /* [temp.deduct.type] If, in the declaration of a function template
10316 with a non-type template-parameter, the non-type
10317 template-parameter is used in an expression in the function
10318 parameter-list and, if the corresponding template-argument is
10319 deduced, the template-argument type shall match the type of the
10320 template-parameter exactly, except that a template-argument
10321 deduced from an array bound may be of any integral type.
10322 The non-type parameter might use already deduced type parameters. */
10323 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10324 if (!TREE_TYPE (arg))
10325 /* Template-parameter dependent expression. Just accept it for now.
10326 It will later be processed in convert_template_argument. */
10328 else if (same_type_p (TREE_TYPE (arg), tparm))
10329 /* OK */;
10330 else if ((strict & UNIFY_ALLOW_INTEGER)
10331 && (TREE_CODE (tparm) == INTEGER_TYPE
10332 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10333 /* Convert the ARG to the type of PARM; the deduced non-type
10334 template argument must exactly match the types of the
10335 corresponding parameter. */
10336 arg = fold (build_nop (TREE_TYPE (parm), arg));
10337 else if (uses_template_parms (tparm))
10338 /* We haven't deduced the type of this parameter yet. Try again
10339 later. */
10340 return 0;
10341 else
10342 return 1;
10344 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10345 return 0;
10347 case PTRMEM_CST:
10349 /* A pointer-to-member constant can be unified only with
10350 another constant. */
10351 if (TREE_CODE (arg) != PTRMEM_CST)
10352 return 1;
10354 /* Just unify the class member. It would be useless (and possibly
10355 wrong, depending on the strict flags) to unify also
10356 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10357 arg refer to the same variable, even if through different
10358 classes. For instance:
10360 struct A { int x; };
10361 struct B : A { };
10363 Unification of &A::x and &B::x must succeed. */
10364 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10365 PTRMEM_CST_MEMBER (arg), strict);
10368 case POINTER_TYPE:
10370 if (TREE_CODE (arg) != POINTER_TYPE)
10371 return 1;
10373 /* [temp.deduct.call]
10375 A can be another pointer or pointer to member type that can
10376 be converted to the deduced A via a qualification
10377 conversion (_conv.qual_).
10379 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10380 This will allow for additional cv-qualification of the
10381 pointed-to types if appropriate. */
10383 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10384 /* The derived-to-base conversion only persists through one
10385 level of pointers. */
10386 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10388 return unify (tparms, targs, TREE_TYPE (parm),
10389 TREE_TYPE (arg), strict);
10392 case REFERENCE_TYPE:
10393 if (TREE_CODE (arg) != REFERENCE_TYPE)
10394 return 1;
10395 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10396 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10398 case ARRAY_TYPE:
10399 if (TREE_CODE (arg) != ARRAY_TYPE)
10400 return 1;
10401 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10402 != (TYPE_DOMAIN (arg) == NULL_TREE))
10403 return 1;
10404 if (TYPE_DOMAIN (parm) != NULL_TREE)
10406 tree parm_max;
10407 tree arg_max;
10408 bool parm_cst;
10409 bool arg_cst;
10411 /* Our representation of array types uses "N - 1" as the
10412 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10413 not an integer constant. We cannot unify arbitrarily
10414 complex expressions, so we eliminate the MINUS_EXPRs
10415 here. */
10416 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10417 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10418 if (!parm_cst)
10420 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
10421 parm_max = TREE_OPERAND (parm_max, 0);
10423 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10424 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10425 if (!arg_cst)
10427 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10428 trying to unify the type of a variable with the type
10429 of a template parameter. For example:
10431 template <unsigned int N>
10432 void f (char (&) [N]);
10433 int g();
10434 void h(int i) {
10435 char a[g(i)];
10436 f(a);
10439 Here, the type of the ARG will be "int [g(i)]", and
10440 may be a SAVE_EXPR, etc. */
10441 if (TREE_CODE (arg_max) != MINUS_EXPR)
10442 return 1;
10443 arg_max = TREE_OPERAND (arg_max, 0);
10446 /* If only one of the bounds used a MINUS_EXPR, compensate
10447 by adding one to the other bound. */
10448 if (parm_cst && !arg_cst)
10449 parm_max = fold_build2 (PLUS_EXPR,
10450 integer_type_node,
10451 parm_max,
10452 integer_one_node);
10453 else if (arg_cst && !parm_cst)
10454 arg_max = fold_build2 (PLUS_EXPR,
10455 integer_type_node,
10456 arg_max,
10457 integer_one_node);
10459 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10460 return 1;
10462 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10463 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10465 case REAL_TYPE:
10466 case COMPLEX_TYPE:
10467 case VECTOR_TYPE:
10468 case INTEGER_TYPE:
10469 case BOOLEAN_TYPE:
10470 case ENUMERAL_TYPE:
10471 case VOID_TYPE:
10472 if (TREE_CODE (arg) != TREE_CODE (parm))
10473 return 1;
10475 /* We have already checked cv-qualification at the top of the
10476 function. */
10477 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10478 return 1;
10480 /* As far as unification is concerned, this wins. Later checks
10481 will invalidate it if necessary. */
10482 return 0;
10484 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10485 /* Type INTEGER_CST can come from ordinary constant template args. */
10486 case INTEGER_CST:
10487 while (TREE_CODE (arg) == NOP_EXPR)
10488 arg = TREE_OPERAND (arg, 0);
10490 if (TREE_CODE (arg) != INTEGER_CST)
10491 return 1;
10492 return !tree_int_cst_equal (parm, arg);
10494 case TREE_VEC:
10496 int i;
10497 if (TREE_CODE (arg) != TREE_VEC)
10498 return 1;
10499 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10500 return 1;
10501 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10502 if (unify (tparms, targs,
10503 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10504 UNIFY_ALLOW_NONE))
10505 return 1;
10506 return 0;
10509 case RECORD_TYPE:
10510 case UNION_TYPE:
10511 if (TREE_CODE (arg) != TREE_CODE (parm))
10512 return 1;
10514 if (TYPE_PTRMEMFUNC_P (parm))
10516 if (!TYPE_PTRMEMFUNC_P (arg))
10517 return 1;
10519 return unify (tparms, targs,
10520 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10521 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10522 strict);
10525 if (CLASSTYPE_TEMPLATE_INFO (parm))
10527 tree t = NULL_TREE;
10529 if (strict_in & UNIFY_ALLOW_DERIVED)
10531 /* First, we try to unify the PARM and ARG directly. */
10532 t = try_class_unification (tparms, targs,
10533 parm, arg);
10535 if (!t)
10537 /* Fallback to the special case allowed in
10538 [temp.deduct.call]:
10540 If P is a class, and P has the form
10541 template-id, then A can be a derived class of
10542 the deduced A. Likewise, if P is a pointer to
10543 a class of the form template-id, A can be a
10544 pointer to a derived class pointed to by the
10545 deduced A. */
10546 t = get_template_base (tparms, targs, parm, arg);
10548 if (!t)
10549 return 1;
10552 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10553 && (CLASSTYPE_TI_TEMPLATE (parm)
10554 == CLASSTYPE_TI_TEMPLATE (arg)))
10555 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10556 Then, we should unify `int' and `U'. */
10557 t = arg;
10558 else
10559 /* There's no chance of unification succeeding. */
10560 return 1;
10562 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10563 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10565 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10566 return 1;
10567 return 0;
10569 case METHOD_TYPE:
10570 case FUNCTION_TYPE:
10571 if (TREE_CODE (arg) != TREE_CODE (parm))
10572 return 1;
10574 /* CV qualifications for methods can never be deduced, they must
10575 match exactly. We need to check them explicitly here,
10576 because type_unification_real treats them as any other
10577 cvqualified parameter. */
10578 if (TREE_CODE (parm) == METHOD_TYPE
10579 && (!check_cv_quals_for_unify
10580 (UNIFY_ALLOW_NONE,
10581 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10582 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10583 return 1;
10585 if (unify (tparms, targs, TREE_TYPE (parm),
10586 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10587 return 1;
10588 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10589 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10590 LOOKUP_NORMAL);
10592 case OFFSET_TYPE:
10593 /* Unify a pointer to member with a pointer to member function, which
10594 deduces the type of the member as a function type. */
10595 if (TYPE_PTRMEMFUNC_P (arg))
10597 tree method_type;
10598 tree fntype;
10599 cp_cv_quals cv_quals;
10601 /* Check top-level cv qualifiers */
10602 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10603 return 1;
10605 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10606 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10607 return 1;
10609 /* Determine the type of the function we are unifying against. */
10610 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10611 fntype =
10612 build_function_type (TREE_TYPE (method_type),
10613 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10615 /* Extract the cv-qualifiers of the member function from the
10616 implicit object parameter and place them on the function
10617 type to be restored later. */
10618 cv_quals =
10619 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10620 fntype = build_qualified_type (fntype, cv_quals);
10621 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10624 if (TREE_CODE (arg) != OFFSET_TYPE)
10625 return 1;
10626 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10627 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10628 return 1;
10629 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10630 strict);
10632 case CONST_DECL:
10633 if (DECL_TEMPLATE_PARM_P (parm))
10634 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10635 if (arg != integral_constant_value (parm))
10636 return 1;
10637 return 0;
10639 case FIELD_DECL:
10640 case TEMPLATE_DECL:
10641 /* Matched cases are handled by the ARG == PARM test above. */
10642 return 1;
10644 default:
10645 gcc_assert (EXPR_P (parm));
10647 /* We must be looking at an expression. This can happen with
10648 something like:
10650 template <int I>
10651 void foo(S<I>, S<I + 2>);
10653 This is a "nondeduced context":
10655 [deduct.type]
10657 The nondeduced contexts are:
10659 --A type that is a template-id in which one or more of
10660 the template-arguments is an expression that references
10661 a template-parameter.
10663 In these cases, we assume deduction succeeded, but don't
10664 actually infer any unifications. */
10666 if (!uses_template_parms (parm)
10667 && !template_args_equal (parm, arg))
10668 return 1;
10669 else
10670 return 0;
10674 /* Note that DECL can be defined in this translation unit, if
10675 required. */
10677 static void
10678 mark_definable (tree decl)
10680 tree clone;
10681 DECL_NOT_REALLY_EXTERN (decl) = 1;
10682 FOR_EACH_CLONE (clone, decl)
10683 DECL_NOT_REALLY_EXTERN (clone) = 1;
10686 /* Called if RESULT is explicitly instantiated, or is a member of an
10687 explicitly instantiated class. */
10689 void
10690 mark_decl_instantiated (tree result, int extern_p)
10692 SET_DECL_EXPLICIT_INSTANTIATION (result);
10694 /* If this entity has already been written out, it's too late to
10695 make any modifications. */
10696 if (TREE_ASM_WRITTEN (result))
10697 return;
10699 if (TREE_CODE (result) != FUNCTION_DECL)
10700 /* The TREE_PUBLIC flag for function declarations will have been
10701 set correctly by tsubst. */
10702 TREE_PUBLIC (result) = 1;
10704 /* This might have been set by an earlier implicit instantiation. */
10705 DECL_COMDAT (result) = 0;
10707 if (extern_p)
10708 DECL_NOT_REALLY_EXTERN (result) = 0;
10709 else
10711 mark_definable (result);
10712 /* Always make artificials weak. */
10713 if (DECL_ARTIFICIAL (result) && flag_weak)
10714 comdat_linkage (result);
10715 /* For WIN32 we also want to put explicit instantiations in
10716 linkonce sections. */
10717 else if (TREE_PUBLIC (result))
10718 maybe_make_one_only (result);
10721 /* If EXTERN_P, then this function will not be emitted -- unless
10722 followed by an explicit instantiation, at which point its linkage
10723 will be adjusted. If !EXTERN_P, then this function will be
10724 emitted here. In neither circumstance do we want
10725 import_export_decl to adjust the linkage. */
10726 DECL_INTERFACE_KNOWN (result) = 1;
10729 /* Given two function templates PAT1 and PAT2, return:
10731 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10732 -1 if PAT2 is more specialized than PAT1.
10733 0 if neither is more specialized.
10735 LEN indicates the number of parameters we should consider
10736 (defaulted parameters should not be considered).
10738 The 1998 std underspecified function template partial ordering, and
10739 DR214 addresses the issue. We take pairs of arguments, one from
10740 each of the templates, and deduce them against each other. One of
10741 the templates will be more specialized if all the *other*
10742 template's arguments deduce against its arguments and at least one
10743 of its arguments *does* *not* deduce against the other template's
10744 corresponding argument. Deduction is done as for class templates.
10745 The arguments used in deduction have reference and top level cv
10746 qualifiers removed. Iff both arguments were originally reference
10747 types *and* deduction succeeds in both directions, the template
10748 with the more cv-qualified argument wins for that pairing (if
10749 neither is more cv-qualified, they both are equal). Unlike regular
10750 deduction, after all the arguments have been deduced in this way,
10751 we do *not* verify the deduced template argument values can be
10752 substituted into non-deduced contexts, nor do we have to verify
10753 that all template arguments have been deduced. */
10756 more_specialized_fn (tree pat1, tree pat2, int len)
10758 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10759 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10760 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10761 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10762 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10763 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10764 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10765 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10766 int better1 = 0;
10767 int better2 = 0;
10769 /* Remove the this parameter from non-static member functions. If
10770 one is a non-static member function and the other is not a static
10771 member function, remove the first parameter from that function
10772 also. This situation occurs for operator functions where we
10773 locate both a member function (with this pointer) and non-member
10774 operator (with explicit first operand). */
10775 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10777 len--; /* LEN is the number of significant arguments for DECL1 */
10778 args1 = TREE_CHAIN (args1);
10779 if (!DECL_STATIC_FUNCTION_P (decl2))
10780 args2 = TREE_CHAIN (args2);
10782 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10784 args2 = TREE_CHAIN (args2);
10785 if (!DECL_STATIC_FUNCTION_P (decl1))
10787 len--;
10788 args1 = TREE_CHAIN (args1);
10792 /* If only one is a conversion operator, they are unordered. */
10793 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10794 return 0;
10796 /* Consider the return type for a conversion function */
10797 if (DECL_CONV_FN_P (decl1))
10799 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10800 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10801 len++;
10804 processing_template_decl++;
10806 while (len--)
10808 tree arg1 = TREE_VALUE (args1);
10809 tree arg2 = TREE_VALUE (args2);
10810 int deduce1, deduce2;
10811 int quals1 = -1;
10812 int quals2 = -1;
10814 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10816 arg1 = TREE_TYPE (arg1);
10817 quals1 = cp_type_quals (arg1);
10820 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10822 arg2 = TREE_TYPE (arg2);
10823 quals2 = cp_type_quals (arg2);
10826 if ((quals1 < 0) != (quals2 < 0))
10828 /* Only of the args is a reference, see if we should apply
10829 array/function pointer decay to it. This is not part of
10830 DR214, but is, IMHO, consistent with the deduction rules
10831 for the function call itself, and with our earlier
10832 implementation of the underspecified partial ordering
10833 rules. (nathan). */
10834 if (quals1 >= 0)
10836 switch (TREE_CODE (arg1))
10838 case ARRAY_TYPE:
10839 arg1 = TREE_TYPE (arg1);
10840 /* FALLTHROUGH. */
10841 case FUNCTION_TYPE:
10842 arg1 = build_pointer_type (arg1);
10843 break;
10845 default:
10846 break;
10849 else
10851 switch (TREE_CODE (arg2))
10853 case ARRAY_TYPE:
10854 arg2 = TREE_TYPE (arg2);
10855 /* FALLTHROUGH. */
10856 case FUNCTION_TYPE:
10857 arg2 = build_pointer_type (arg2);
10858 break;
10860 default:
10861 break;
10866 arg1 = TYPE_MAIN_VARIANT (arg1);
10867 arg2 = TYPE_MAIN_VARIANT (arg2);
10869 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10870 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10872 if (!deduce1)
10873 better2 = -1;
10874 if (!deduce2)
10875 better1 = -1;
10876 if (better1 < 0 && better2 < 0)
10877 /* We've failed to deduce something in either direction.
10878 These must be unordered. */
10879 break;
10881 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10883 /* Deduces in both directions, see if quals can
10884 disambiguate. Pretend the worse one failed to deduce. */
10885 if ((quals1 & quals2) == quals2)
10886 deduce1 = 0;
10887 if ((quals1 & quals2) == quals1)
10888 deduce2 = 0;
10890 if (deduce1 && !deduce2 && !better2)
10891 better2 = 1;
10892 if (deduce2 && !deduce1 && !better1)
10893 better1 = 1;
10895 args1 = TREE_CHAIN (args1);
10896 args2 = TREE_CHAIN (args2);
10899 processing_template_decl--;
10901 return (better1 > 0) - (better2 > 0);
10904 /* Determine which of two partial specializations is more specialized.
10906 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
10907 to the first partial specialization. The TREE_VALUE is the
10908 innermost set of template parameters for the partial
10909 specialization. PAT2 is similar, but for the second template.
10911 Return 1 if the first partial specialization is more specialized;
10912 -1 if the second is more specialized; 0 if neither is more
10913 specialized.
10915 See [temp.class.order] for information about determining which of
10916 two templates is more specialized. */
10919 more_specialized_class (tree pat1, tree pat2)
10921 tree targs;
10922 tree tmpl1, tmpl2;
10923 int winner = 0;
10925 tmpl1 = TREE_TYPE (pat1);
10926 tmpl2 = TREE_TYPE (pat2);
10928 /* Just like what happens for functions, if we are ordering between
10929 different class template specializations, we may encounter dependent
10930 types in the arguments, and we need our dependency check functions
10931 to behave correctly. */
10932 ++processing_template_decl;
10933 targs = get_class_bindings (TREE_VALUE (pat1),
10934 CLASSTYPE_TI_ARGS (tmpl1),
10935 CLASSTYPE_TI_ARGS (tmpl2));
10936 if (targs)
10937 --winner;
10939 targs = get_class_bindings (TREE_VALUE (pat2),
10940 CLASSTYPE_TI_ARGS (tmpl2),
10941 CLASSTYPE_TI_ARGS (tmpl1));
10942 if (targs)
10943 ++winner;
10944 --processing_template_decl;
10946 return winner;
10949 /* Return the template arguments that will produce the function signature
10950 DECL from the function template FN, with the explicit template
10951 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
10952 also match. Return NULL_TREE if no satisfactory arguments could be
10953 found. */
10955 static tree
10956 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
10958 int ntparms = DECL_NTPARMS (fn);
10959 tree targs = make_tree_vec (ntparms);
10960 tree decl_type;
10961 tree decl_arg_types;
10963 /* Substitute the explicit template arguments into the type of DECL.
10964 The call to fn_type_unification will handle substitution into the
10965 FN. */
10966 decl_type = TREE_TYPE (decl);
10967 if (explicit_args && uses_template_parms (decl_type))
10969 tree tmpl;
10970 tree converted_args;
10972 if (DECL_TEMPLATE_INFO (decl))
10973 tmpl = DECL_TI_TEMPLATE (decl);
10974 else
10975 /* We can get here for some invalid specializations. */
10976 return NULL_TREE;
10978 converted_args
10979 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10980 explicit_args, NULL_TREE,
10981 tf_none, /*require_all_arguments=*/0));
10982 if (converted_args == error_mark_node)
10983 return NULL_TREE;
10985 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10986 if (decl_type == error_mark_node)
10987 return NULL_TREE;
10990 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10991 /* Never do unification on the 'this' parameter. */
10992 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10993 decl_arg_types = TREE_CHAIN (decl_arg_types);
10995 if (fn_type_unification (fn, explicit_args, targs,
10996 decl_arg_types,
10997 (check_rettype || DECL_CONV_FN_P (fn)
10998 ? TREE_TYPE (decl_type) : NULL_TREE),
10999 DEDUCE_EXACT, LOOKUP_NORMAL))
11000 return NULL_TREE;
11002 return targs;
11005 /* Return the innermost template arguments that, when applied to a
11006 template specialization whose innermost template parameters are
11007 TPARMS, and whose specialization arguments are PARMS, yield the
11008 ARGS.
11010 For example, suppose we have:
11012 template <class T, class U> struct S {};
11013 template <class T> struct S<T*, int> {};
11015 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
11016 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
11017 int}. The resulting vector will be {double}, indicating that `T'
11018 is bound to `double'. */
11020 static tree
11021 get_class_bindings (tree tparms, tree spec_args, tree args)
11023 int i, ntparms = TREE_VEC_LENGTH (tparms);
11024 tree deduced_args;
11025 tree innermost_deduced_args;
11027 innermost_deduced_args = make_tree_vec (ntparms);
11028 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11030 deduced_args = copy_node (args);
11031 SET_TMPL_ARGS_LEVEL (deduced_args,
11032 TMPL_ARGS_DEPTH (deduced_args),
11033 innermost_deduced_args);
11035 else
11036 deduced_args = innermost_deduced_args;
11038 if (unify (tparms, deduced_args,
11039 INNERMOST_TEMPLATE_ARGS (spec_args),
11040 INNERMOST_TEMPLATE_ARGS (args),
11041 UNIFY_ALLOW_NONE))
11042 return NULL_TREE;
11044 for (i = 0; i < ntparms; ++i)
11045 if (! TREE_VEC_ELT (innermost_deduced_args, i))
11046 return NULL_TREE;
11048 /* Verify that nondeduced template arguments agree with the type
11049 obtained from argument deduction.
11051 For example:
11053 struct A { typedef int X; };
11054 template <class T, class U> struct C {};
11055 template <class T> struct C<T, typename T::X> {};
11057 Then with the instantiation `C<A, int>', we can deduce that
11058 `T' is `A' but unify () does not check whether `typename T::X'
11059 is `int'. */
11060 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11061 if (spec_args == error_mark_node
11062 /* We only need to check the innermost arguments; the other
11063 arguments will always agree. */
11064 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11065 INNERMOST_TEMPLATE_ARGS (args)))
11066 return NULL_TREE;
11068 return deduced_args;
11071 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
11072 Pick the most specialized template, and return the corresponding
11073 instantiation, or if there is no corresponding instantiation, the
11074 template itself. If there is no most specialized template,
11075 error_mark_node is returned. If there are no templates at all,
11076 NULL_TREE is returned. */
11078 tree
11079 most_specialized_instantiation (tree instantiations)
11081 tree fn, champ;
11083 if (!instantiations)
11084 return NULL_TREE;
11086 ++processing_template_decl;
11088 champ = instantiations;
11089 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
11091 int fate = 0;
11093 if (get_bindings (TREE_VALUE (champ),
11094 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11095 NULL_TREE, /*check_ret=*/false))
11096 fate--;
11098 if (get_bindings (TREE_VALUE (fn),
11099 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11100 NULL_TREE, /*check_ret=*/false))
11101 fate++;
11103 if (fate == -1)
11104 champ = fn;
11105 else if (!fate)
11107 /* Equally specialized, move to next function. If there
11108 is no next function, nothing's most specialized. */
11109 fn = TREE_CHAIN (fn);
11110 champ = fn;
11111 if (!fn)
11112 break;
11116 if (champ)
11117 /* Now verify that champ is better than everything earlier in the
11118 instantiation list. */
11119 for (fn = instantiations; fn != champ; fn = TREE_CHAIN (fn))
11120 if (get_bindings (TREE_VALUE (champ),
11121 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11122 NULL_TREE, /*check_ret=*/false)
11123 || !get_bindings (TREE_VALUE (fn),
11124 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11125 NULL_TREE, /*check_ret=*/false))
11127 champ = NULL_TREE;
11128 break;
11131 processing_template_decl--;
11133 if (!champ)
11134 return error_mark_node;
11136 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
11139 /* Return the most specialized of the list of templates in FNS that can
11140 produce an instantiation matching DECL, given the explicit template
11141 arguments EXPLICIT_ARGS. */
11143 static tree
11144 most_specialized (tree fns, tree decl, tree explicit_args)
11146 tree candidates = NULL_TREE;
11147 tree fn, args;
11149 for (fn = fns; fn; fn = TREE_CHAIN (fn))
11151 tree candidate = TREE_VALUE (fn);
11153 args = get_bindings (candidate, decl, explicit_args, /*check_ret=*/true);
11154 if (args)
11155 candidates = tree_cons (NULL_TREE, candidate, candidates);
11158 return most_specialized_instantiation (candidates);
11161 /* If DECL is a specialization of some template, return the most
11162 general such template. Otherwise, returns NULL_TREE.
11164 For example, given:
11166 template <class T> struct S { template <class U> void f(U); };
11168 if TMPL is `template <class U> void S<int>::f(U)' this will return
11169 the full template. This function will not trace past partial
11170 specializations, however. For example, given in addition:
11172 template <class T> struct S<T*> { template <class U> void f(U); };
11174 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11175 `template <class T> template <class U> S<T*>::f(U)'. */
11177 tree
11178 most_general_template (tree decl)
11180 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11181 an immediate specialization. */
11182 if (TREE_CODE (decl) == FUNCTION_DECL)
11184 if (DECL_TEMPLATE_INFO (decl)) {
11185 decl = DECL_TI_TEMPLATE (decl);
11187 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11188 template friend. */
11189 if (TREE_CODE (decl) != TEMPLATE_DECL)
11190 return NULL_TREE;
11191 } else
11192 return NULL_TREE;
11195 /* Look for more and more general templates. */
11196 while (DECL_TEMPLATE_INFO (decl))
11198 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11199 (See cp-tree.h for details.) */
11200 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11201 break;
11203 if (CLASS_TYPE_P (TREE_TYPE (decl))
11204 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11205 break;
11207 /* Stop if we run into an explicitly specialized class template. */
11208 if (!DECL_NAMESPACE_SCOPE_P (decl)
11209 && DECL_CONTEXT (decl)
11210 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11211 break;
11213 decl = DECL_TI_TEMPLATE (decl);
11216 return decl;
11219 /* Return the most specialized of the class template partial
11220 specializations of TMPL which can produce TYPE, a specialization of
11221 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11222 a _TYPE node corresponding to the partial specialization, while the
11223 TREE_PURPOSE is the set of template arguments that must be
11224 substituted into the TREE_TYPE in order to generate TYPE.
11226 If the choice of partial specialization is ambiguous, a diagnostic
11227 is issued, and the error_mark_node is returned. If there are no
11228 partial specializations of TMPL matching TYPE, then NULL_TREE is
11229 returned. */
11231 static tree
11232 most_specialized_class (tree type, tree tmpl)
11234 tree list = NULL_TREE;
11235 tree t;
11236 tree champ;
11237 int fate;
11238 bool ambiguous_p;
11239 tree args;
11241 tmpl = most_general_template (tmpl);
11242 args = CLASSTYPE_TI_ARGS (type);
11243 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11245 tree partial_spec_args;
11246 tree spec_args;
11248 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11249 spec_args = get_class_bindings (TREE_VALUE (t),
11250 partial_spec_args,
11251 args);
11252 if (spec_args)
11254 list = tree_cons (spec_args, TREE_VALUE (t), list);
11255 TREE_TYPE (list) = TREE_TYPE (t);
11259 if (! list)
11260 return NULL_TREE;
11262 ambiguous_p = false;
11263 t = list;
11264 champ = t;
11265 t = TREE_CHAIN (t);
11266 for (; t; t = TREE_CHAIN (t))
11268 fate = more_specialized_class (champ, t);
11269 if (fate == 1)
11271 else
11273 if (fate == 0)
11275 t = TREE_CHAIN (t);
11276 if (! t)
11278 ambiguous_p = true;
11279 break;
11282 champ = t;
11286 if (!ambiguous_p)
11287 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11289 fate = more_specialized_class (champ, t);
11290 if (fate != 1)
11292 ambiguous_p = true;
11293 break;
11297 if (ambiguous_p)
11299 const char *str = "candidates are:";
11300 error ("ambiguous class template instantiation for %q#T", type);
11301 for (t = list; t; t = TREE_CHAIN (t))
11303 error ("%s %+#T", str, TREE_TYPE (t));
11304 str = " ";
11306 return error_mark_node;
11309 return champ;
11312 /* Explicitly instantiate DECL. */
11314 void
11315 do_decl_instantiation (tree decl, tree storage)
11317 tree result = NULL_TREE;
11318 int extern_p = 0;
11320 if (!decl || decl == error_mark_node)
11321 /* An error occurred, for which grokdeclarator has already issued
11322 an appropriate message. */
11323 return;
11324 else if (! DECL_LANG_SPECIFIC (decl))
11326 error ("explicit instantiation of non-template %q#D", decl);
11327 return;
11329 else if (TREE_CODE (decl) == VAR_DECL)
11331 /* There is an asymmetry here in the way VAR_DECLs and
11332 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11333 the latter, the DECL we get back will be marked as a
11334 template instantiation, and the appropriate
11335 DECL_TEMPLATE_INFO will be set up. This does not happen for
11336 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11337 should handle VAR_DECLs as it currently handles
11338 FUNCTION_DECLs. */
11339 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11340 if (!result || TREE_CODE (result) != VAR_DECL)
11342 error ("no matching template for %qD found", decl);
11343 return;
11346 else if (TREE_CODE (decl) != FUNCTION_DECL)
11348 error ("explicit instantiation of %q#D", decl);
11349 return;
11351 else
11352 result = decl;
11354 /* Check for various error cases. Note that if the explicit
11355 instantiation is valid the RESULT will currently be marked as an
11356 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11357 until we get here. */
11359 if (DECL_TEMPLATE_SPECIALIZATION (result))
11361 /* DR 259 [temp.spec].
11363 Both an explicit instantiation and a declaration of an explicit
11364 specialization shall not appear in a program unless the explicit
11365 instantiation follows a declaration of the explicit specialization.
11367 For a given set of template parameters, if an explicit
11368 instantiation of a template appears after a declaration of an
11369 explicit specialization for that template, the explicit
11370 instantiation has no effect. */
11371 return;
11373 else if (DECL_EXPLICIT_INSTANTIATION (result))
11375 /* [temp.spec]
11377 No program shall explicitly instantiate any template more
11378 than once.
11380 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11381 the first instantiation was `extern' and the second is not,
11382 and EXTERN_P for the opposite case. */
11383 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11384 pedwarn ("duplicate explicit instantiation of %q#D", result);
11385 /* If an "extern" explicit instantiation follows an ordinary
11386 explicit instantiation, the template is instantiated. */
11387 if (extern_p)
11388 return;
11390 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11392 error ("no matching template for %qD found", result);
11393 return;
11395 else if (!DECL_TEMPLATE_INFO (result))
11397 pedwarn ("explicit instantiation of non-template %q#D", result);
11398 return;
11401 if (storage == NULL_TREE)
11403 else if (storage == ridpointers[(int) RID_EXTERN])
11405 if (pedantic && !in_system_header)
11406 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11407 "instantiations");
11408 extern_p = 1;
11410 else
11411 error ("storage class %qD applied to template instantiation", storage);
11413 check_explicit_instantiation_namespace (result);
11414 mark_decl_instantiated (result, extern_p);
11415 if (! extern_p)
11416 instantiate_decl (result, /*defer_ok=*/1,
11417 /*expl_inst_class_mem_p=*/false);
11420 void
11421 mark_class_instantiated (tree t, int extern_p)
11423 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11424 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11425 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11426 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11427 if (! extern_p)
11429 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11430 rest_of_type_compilation (t, 1);
11434 /* Called from do_type_instantiation through binding_table_foreach to
11435 do recursive instantiation for the type bound in ENTRY. */
11436 static void
11437 bt_instantiate_type_proc (binding_entry entry, void *data)
11439 tree storage = *(tree *) data;
11441 if (IS_AGGR_TYPE (entry->type)
11442 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11443 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11446 /* Called from do_type_instantiation to instantiate a member
11447 (a member function or a static member variable) of an
11448 explicitly instantiated class template. */
11449 static void
11450 instantiate_class_member (tree decl, int extern_p)
11452 mark_decl_instantiated (decl, extern_p);
11453 if (! extern_p)
11454 instantiate_decl (decl, /*defer_ok=*/1,
11455 /*expl_inst_class_mem_p=*/true);
11458 /* Perform an explicit instantiation of template class T. STORAGE, if
11459 non-null, is the RID for extern, inline or static. COMPLAIN is
11460 nonzero if this is called from the parser, zero if called recursively,
11461 since the standard is unclear (as detailed below). */
11463 void
11464 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11466 int extern_p = 0;
11467 int nomem_p = 0;
11468 int static_p = 0;
11469 int previous_instantiation_extern_p = 0;
11471 if (TREE_CODE (t) == TYPE_DECL)
11472 t = TREE_TYPE (t);
11474 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11476 error ("explicit instantiation of non-template type %qT", t);
11477 return;
11480 complete_type (t);
11482 if (!COMPLETE_TYPE_P (t))
11484 if (complain & tf_error)
11485 error ("explicit instantiation of %q#T before definition of template",
11487 return;
11490 if (storage != NULL_TREE)
11492 if (pedantic && !in_system_header)
11493 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11494 storage);
11496 if (storage == ridpointers[(int) RID_INLINE])
11497 nomem_p = 1;
11498 else if (storage == ridpointers[(int) RID_EXTERN])
11499 extern_p = 1;
11500 else if (storage == ridpointers[(int) RID_STATIC])
11501 static_p = 1;
11502 else
11504 error ("storage class %qD applied to template instantiation",
11505 storage);
11506 extern_p = 0;
11510 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11512 /* DR 259 [temp.spec].
11514 Both an explicit instantiation and a declaration of an explicit
11515 specialization shall not appear in a program unless the explicit
11516 instantiation follows a declaration of the explicit specialization.
11518 For a given set of template parameters, if an explicit
11519 instantiation of a template appears after a declaration of an
11520 explicit specialization for that template, the explicit
11521 instantiation has no effect. */
11522 return;
11524 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11526 /* [temp.spec]
11528 No program shall explicitly instantiate any template more
11529 than once.
11531 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11532 instantiation was `extern'. If EXTERN_P then the second is.
11533 These cases are OK. */
11534 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11536 if (!previous_instantiation_extern_p && !extern_p
11537 && (complain & tf_error))
11538 pedwarn ("duplicate explicit instantiation of %q#T", t);
11540 /* If we've already instantiated the template, just return now. */
11541 if (!CLASSTYPE_INTERFACE_ONLY (t))
11542 return;
11545 check_explicit_instantiation_namespace (TYPE_NAME (t));
11546 mark_class_instantiated (t, extern_p);
11548 if (nomem_p)
11549 return;
11552 tree tmp;
11554 /* In contrast to implicit instantiation, where only the
11555 declarations, and not the definitions, of members are
11556 instantiated, we have here:
11558 [temp.explicit]
11560 The explicit instantiation of a class template specialization
11561 implies the instantiation of all of its members not
11562 previously explicitly specialized in the translation unit
11563 containing the explicit instantiation.
11565 Of course, we can't instantiate member template classes, since
11566 we don't have any arguments for them. Note that the standard
11567 is unclear on whether the instantiation of the members are
11568 *explicit* instantiations or not. However, the most natural
11569 interpretation is that it should be an explicit instantiation. */
11571 if (! static_p)
11572 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11573 if (TREE_CODE (tmp) == FUNCTION_DECL
11574 && DECL_TEMPLATE_INSTANTIATION (tmp))
11575 instantiate_class_member (tmp, extern_p);
11577 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11578 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11579 instantiate_class_member (tmp, extern_p);
11581 if (CLASSTYPE_NESTED_UTDS (t))
11582 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11583 bt_instantiate_type_proc, &storage);
11587 /* Given a function DECL, which is a specialization of TMPL, modify
11588 DECL to be a re-instantiation of TMPL with the same template
11589 arguments. TMPL should be the template into which tsubst'ing
11590 should occur for DECL, not the most general template.
11592 One reason for doing this is a scenario like this:
11594 template <class T>
11595 void f(const T&, int i);
11597 void g() { f(3, 7); }
11599 template <class T>
11600 void f(const T& t, const int i) { }
11602 Note that when the template is first instantiated, with
11603 instantiate_template, the resulting DECL will have no name for the
11604 first parameter, and the wrong type for the second. So, when we go
11605 to instantiate the DECL, we regenerate it. */
11607 static void
11608 regenerate_decl_from_template (tree decl, tree tmpl)
11610 /* The arguments used to instantiate DECL, from the most general
11611 template. */
11612 tree args;
11613 tree code_pattern;
11615 args = DECL_TI_ARGS (decl);
11616 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11618 /* Make sure that we can see identifiers, and compute access
11619 correctly. */
11620 push_access_scope (decl);
11622 if (TREE_CODE (decl) == FUNCTION_DECL)
11624 tree decl_parm;
11625 tree pattern_parm;
11626 tree specs;
11627 int args_depth;
11628 int parms_depth;
11630 args_depth = TMPL_ARGS_DEPTH (args);
11631 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11632 if (args_depth > parms_depth)
11633 args = get_innermost_template_args (args, parms_depth);
11635 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11636 args, tf_error, NULL_TREE);
11637 if (specs)
11638 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11639 specs);
11641 /* Merge parameter declarations. */
11642 decl_parm = skip_artificial_parms_for (decl,
11643 DECL_ARGUMENTS (decl));
11644 pattern_parm
11645 = skip_artificial_parms_for (code_pattern,
11646 DECL_ARGUMENTS (code_pattern));
11647 while (decl_parm)
11649 tree parm_type;
11650 tree attributes;
11652 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11653 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11654 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11655 NULL_TREE);
11656 parm_type = type_decays_to (parm_type);
11657 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11658 TREE_TYPE (decl_parm) = parm_type;
11659 attributes = DECL_ATTRIBUTES (pattern_parm);
11660 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11662 DECL_ATTRIBUTES (decl_parm) = attributes;
11663 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11665 decl_parm = TREE_CHAIN (decl_parm);
11666 pattern_parm = TREE_CHAIN (pattern_parm);
11669 /* Merge additional specifiers from the CODE_PATTERN. */
11670 if (DECL_DECLARED_INLINE_P (code_pattern)
11671 && !DECL_DECLARED_INLINE_P (decl))
11672 DECL_DECLARED_INLINE_P (decl) = 1;
11673 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11674 DECL_INLINE (decl) = 1;
11676 else if (TREE_CODE (decl) == VAR_DECL)
11677 DECL_INITIAL (decl) =
11678 tsubst_expr (DECL_INITIAL (code_pattern), args,
11679 tf_error, DECL_TI_TEMPLATE (decl));
11680 else
11681 gcc_unreachable ();
11683 pop_access_scope (decl);
11686 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11687 substituted to get DECL. */
11689 tree
11690 template_for_substitution (tree decl)
11692 tree tmpl = DECL_TI_TEMPLATE (decl);
11694 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11695 for the instantiation. This is not always the most general
11696 template. Consider, for example:
11698 template <class T>
11699 struct S { template <class U> void f();
11700 template <> void f<int>(); };
11702 and an instantiation of S<double>::f<int>. We want TD to be the
11703 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11704 while (/* An instantiation cannot have a definition, so we need a
11705 more general template. */
11706 DECL_TEMPLATE_INSTANTIATION (tmpl)
11707 /* We must also deal with friend templates. Given:
11709 template <class T> struct S {
11710 template <class U> friend void f() {};
11713 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11714 so far as the language is concerned, but that's still
11715 where we get the pattern for the instantiation from. On
11716 other hand, if the definition comes outside the class, say:
11718 template <class T> struct S {
11719 template <class U> friend void f();
11721 template <class U> friend void f() {}
11723 we don't need to look any further. That's what the check for
11724 DECL_INITIAL is for. */
11725 || (TREE_CODE (decl) == FUNCTION_DECL
11726 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11727 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11729 /* The present template, TD, should not be a definition. If it
11730 were a definition, we should be using it! Note that we
11731 cannot restructure the loop to just keep going until we find
11732 a template with a definition, since that might go too far if
11733 a specialization was declared, but not defined. */
11734 gcc_assert (TREE_CODE (decl) != VAR_DECL
11735 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11737 /* Fetch the more general template. */
11738 tmpl = DECL_TI_TEMPLATE (tmpl);
11741 return tmpl;
11744 /* Produce the definition of D, a _DECL generated from a template. If
11745 DEFER_OK is nonzero, then we don't have to actually do the
11746 instantiation now; we just have to do it sometime. Normally it is
11747 an error if this is an explicit instantiation but D is undefined.
11748 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11749 explicitly instantiated class template. */
11751 tree
11752 instantiate_decl (tree d, int defer_ok,
11753 bool expl_inst_class_mem_p)
11755 tree tmpl = DECL_TI_TEMPLATE (d);
11756 tree gen_args;
11757 tree args;
11758 tree td;
11759 tree code_pattern;
11760 tree spec;
11761 tree gen_tmpl;
11762 bool pattern_defined;
11763 int need_push;
11764 location_t saved_loc = input_location;
11765 bool external_p;
11767 /* This function should only be used to instantiate templates for
11768 functions and static member variables. */
11769 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11770 || TREE_CODE (d) == VAR_DECL);
11772 /* Variables are never deferred; if instantiation is required, they
11773 are instantiated right away. That allows for better code in the
11774 case that an expression refers to the value of the variable --
11775 if the variable has a constant value the referring expression can
11776 take advantage of that fact. */
11777 if (TREE_CODE (d) == VAR_DECL)
11778 defer_ok = 0;
11780 /* Don't instantiate cloned functions. Instead, instantiate the
11781 functions they cloned. */
11782 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11783 d = DECL_CLONED_FUNCTION (d);
11785 if (DECL_TEMPLATE_INSTANTIATED (d))
11786 /* D has already been instantiated. It might seem reasonable to
11787 check whether or not D is an explicit instantiation, and, if so,
11788 stop here. But when an explicit instantiation is deferred
11789 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11790 is set, even though we still need to do the instantiation. */
11791 return d;
11793 /* If we already have a specialization of this declaration, then
11794 there's no reason to instantiate it. Note that
11795 retrieve_specialization gives us both instantiations and
11796 specializations, so we must explicitly check
11797 DECL_TEMPLATE_SPECIALIZATION. */
11798 gen_tmpl = most_general_template (tmpl);
11799 gen_args = DECL_TI_ARGS (d);
11800 spec = retrieve_specialization (gen_tmpl, gen_args,
11801 /*class_specializations_p=*/false);
11802 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11803 return spec;
11805 /* This needs to happen before any tsubsting. */
11806 if (! push_tinst_level (d))
11807 return d;
11809 timevar_push (TV_PARSE);
11811 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11812 for the instantiation. */
11813 td = template_for_substitution (d);
11814 code_pattern = DECL_TEMPLATE_RESULT (td);
11816 /* We should never be trying to instantiate a member of a class
11817 template or partial specialization. */
11818 gcc_assert (d != code_pattern);
11820 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11821 || DECL_TEMPLATE_SPECIALIZATION (td))
11822 /* In the case of a friend template whose definition is provided
11823 outside the class, we may have too many arguments. Drop the
11824 ones we don't need. The same is true for specializations. */
11825 args = get_innermost_template_args
11826 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11827 else
11828 args = gen_args;
11830 if (TREE_CODE (d) == FUNCTION_DECL)
11831 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11832 else
11833 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11835 /* We may be in the middle of deferred access check. Disable it now. */
11836 push_deferring_access_checks (dk_no_deferred);
11838 /* Unless an explicit instantiation directive has already determined
11839 the linkage of D, remember that a definition is available for
11840 this entity. */
11841 if (pattern_defined
11842 && !DECL_INTERFACE_KNOWN (d)
11843 && !DECL_NOT_REALLY_EXTERN (d))
11844 mark_definable (d);
11846 input_location = DECL_SOURCE_LOCATION (d);
11848 /* If D is a member of an explicitly instantiated class template,
11849 and no definition is available, treat it like an implicit
11850 instantiation. */
11851 if (!pattern_defined && expl_inst_class_mem_p
11852 && DECL_EXPLICIT_INSTANTIATION (d))
11854 DECL_NOT_REALLY_EXTERN (d) = 0;
11855 DECL_INTERFACE_KNOWN (d) = 0;
11856 SET_DECL_IMPLICIT_INSTANTIATION (d);
11859 if (!defer_ok)
11861 /* Recheck the substitutions to obtain any warning messages
11862 about ignoring cv qualifiers. */
11863 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11864 tree type = TREE_TYPE (gen);
11866 /* Make sure that we can see identifiers, and compute access
11867 correctly. D is already the target FUNCTION_DECL with the
11868 right context. */
11869 push_access_scope (d);
11871 if (TREE_CODE (gen) == FUNCTION_DECL)
11873 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
11874 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11875 tf_error | tf_warning, d);
11876 /* Don't simply tsubst the function type, as that will give
11877 duplicate warnings about poor parameter qualifications.
11878 The function arguments are the same as the decl_arguments
11879 without the top level cv qualifiers. */
11880 type = TREE_TYPE (type);
11882 tsubst (type, gen_args, tf_error | tf_warning, d);
11884 pop_access_scope (d);
11887 /* Check to see whether we know that this template will be
11888 instantiated in some other file, as with "extern template"
11889 extension. */
11890 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
11891 /* In general, we do not instantiate such templates... */
11892 if (external_p
11893 /* ... but we instantiate inline functions so that we can inline
11894 them and ... */
11895 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
11896 /* ... we instantiate static data members whose values are
11897 needed in integral constant expressions. */
11898 && ! (TREE_CODE (d) == VAR_DECL
11899 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
11900 goto out;
11901 /* Defer all other templates, unless we have been explicitly
11902 forbidden from doing so. */
11903 if (/* If there is no definition, we cannot instantiate the
11904 template. */
11905 ! pattern_defined
11906 /* If it's OK to postpone instantiation, do so. */
11907 || defer_ok
11908 /* If this is a static data member that will be defined
11909 elsewhere, we don't want to instantiate the entire data
11910 member, but we do want to instantiate the initializer so that
11911 we can substitute that elsewhere. */
11912 || (external_p && TREE_CODE (d) == VAR_DECL))
11914 /* The definition of the static data member is now required so
11915 we must substitute the initializer. */
11916 if (TREE_CODE (d) == VAR_DECL
11917 && !DECL_INITIAL (d)
11918 && DECL_INITIAL (code_pattern))
11920 tree ns;
11921 tree init;
11923 ns = decl_namespace_context (d);
11924 push_nested_namespace (ns);
11925 push_nested_class (DECL_CONTEXT (d));
11926 init = tsubst_expr (DECL_INITIAL (code_pattern),
11927 args,
11928 tf_error | tf_warning, NULL_TREE);
11929 DECL_INITIAL (d) = init;
11930 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
11931 /*asmspec_tree=*/NULL_TREE,
11932 LOOKUP_ONLYCONVERTING);
11933 pop_nested_class ();
11934 pop_nested_namespace (ns);
11937 /* We restore the source position here because it's used by
11938 add_pending_template. */
11939 input_location = saved_loc;
11941 if (at_eof && !pattern_defined
11942 && DECL_EXPLICIT_INSTANTIATION (d))
11943 /* [temp.explicit]
11945 The definition of a non-exported function template, a
11946 non-exported member function template, or a non-exported
11947 member function or static data member of a class template
11948 shall be present in every translation unit in which it is
11949 explicitly instantiated. */
11950 pedwarn
11951 ("explicit instantiation of %qD but no definition available", d);
11953 /* ??? Historically, we have instantiated inline functions, even
11954 when marked as "extern template". */
11955 if (!(external_p && TREE_CODE (d) == VAR_DECL))
11956 add_pending_template (d);
11957 goto out;
11959 /* Tell the repository that D is available in this translation unit
11960 -- and see if it is supposed to be instantiated here. */
11961 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11963 /* In a PCH file, despite the fact that the repository hasn't
11964 requested instantiation in the PCH it is still possible that
11965 an instantiation will be required in a file that includes the
11966 PCH. */
11967 if (pch_file)
11968 add_pending_template (d);
11969 /* Instantiate inline functions so that the inliner can do its
11970 job, even though we'll not be emitting a copy of this
11971 function. */
11972 if (!(TREE_CODE (d) == FUNCTION_DECL
11973 && flag_inline_trees
11974 && DECL_DECLARED_INLINE_P (d)))
11975 goto out;
11978 need_push = !cfun || !global_bindings_p ();
11979 if (need_push)
11980 push_to_top_level ();
11982 /* Mark D as instantiated so that recursive calls to
11983 instantiate_decl do not try to instantiate it again. */
11984 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11986 /* Regenerate the declaration in case the template has been modified
11987 by a subsequent redeclaration. */
11988 regenerate_decl_from_template (d, td);
11990 /* We already set the file and line above. Reset them now in case
11991 they changed as a result of calling regenerate_decl_from_template. */
11992 input_location = DECL_SOURCE_LOCATION (d);
11994 if (TREE_CODE (d) == VAR_DECL)
11996 /* Clear out DECL_RTL; whatever was there before may not be right
11997 since we've reset the type of the declaration. */
11998 SET_DECL_RTL (d, NULL_RTX);
11999 DECL_IN_AGGR_P (d) = 0;
12001 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12002 initializer. That function will defer actual emission until
12003 we have a chance to determine linkage. */
12004 DECL_EXTERNAL (d) = 0;
12006 /* Enter the scope of D so that access-checking works correctly. */
12007 push_nested_class (DECL_CONTEXT (d));
12008 finish_decl (d, DECL_INITIAL (d), NULL_TREE);
12009 pop_nested_class ();
12011 else if (TREE_CODE (d) == FUNCTION_DECL)
12013 htab_t saved_local_specializations;
12014 tree subst_decl;
12015 tree tmpl_parm;
12016 tree spec_parm;
12018 /* Save away the current list, in case we are instantiating one
12019 template from within the body of another. */
12020 saved_local_specializations = local_specializations;
12022 /* Set up the list of local specializations. */
12023 local_specializations = htab_create (37,
12024 hash_local_specialization,
12025 eq_local_specializations,
12026 NULL);
12028 /* Set up context. */
12029 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
12031 /* Create substitution entries for the parameters. */
12032 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12033 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12034 spec_parm = DECL_ARGUMENTS (d);
12035 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12037 register_local_specialization (spec_parm, tmpl_parm);
12038 spec_parm = skip_artificial_parms_for (d, spec_parm);
12039 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12041 while (tmpl_parm)
12043 register_local_specialization (spec_parm, tmpl_parm);
12044 tmpl_parm = TREE_CHAIN (tmpl_parm);
12045 spec_parm = TREE_CHAIN (spec_parm);
12047 gcc_assert (!spec_parm);
12049 /* Substitute into the body of the function. */
12050 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
12051 tf_error | tf_warning, tmpl);
12053 /* We don't need the local specializations any more. */
12054 htab_delete (local_specializations);
12055 local_specializations = saved_local_specializations;
12057 /* Finish the function. */
12058 d = finish_function (0);
12059 expand_or_defer_fn (d);
12062 /* We're not deferring instantiation any more. */
12063 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12065 if (need_push)
12066 pop_from_top_level ();
12068 out:
12069 input_location = saved_loc;
12070 pop_deferring_access_checks ();
12071 pop_tinst_level ();
12073 timevar_pop (TV_PARSE);
12075 return d;
12078 /* Run through the list of templates that we wish we could
12079 instantiate, and instantiate any we can. RETRIES is the
12080 number of times we retry pending template instantiation. */
12082 void
12083 instantiate_pending_templates (int retries)
12085 tree *t;
12086 tree last = NULL_TREE;
12087 int reconsider;
12088 location_t saved_loc = input_location;
12089 int saved_in_system_header = in_system_header;
12091 /* Instantiating templates may trigger vtable generation. This in turn
12092 may require further template instantiations. We place a limit here
12093 to avoid infinite loop. */
12094 if (pending_templates && retries >= max_tinst_depth)
12096 tree decl = TREE_VALUE (pending_templates);
12098 error ("template instantiation depth exceeds maximum of %d"
12099 " instantiating %q+D, possibly from virtual table generation"
12100 " (use -ftemplate-depth-NN to increase the maximum)",
12101 max_tinst_depth, decl);
12102 if (TREE_CODE (decl) == FUNCTION_DECL)
12103 /* Pretend that we defined it. */
12104 DECL_INITIAL (decl) = error_mark_node;
12105 return;
12110 reconsider = 0;
12112 t = &pending_templates;
12113 while (*t)
12115 tree instantiation = TREE_VALUE (*t);
12117 reopen_tinst_level (TREE_PURPOSE (*t));
12119 if (TYPE_P (instantiation))
12121 tree fn;
12123 if (!COMPLETE_TYPE_P (instantiation))
12125 instantiate_class_template (instantiation);
12126 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12127 for (fn = TYPE_METHODS (instantiation);
12129 fn = TREE_CHAIN (fn))
12130 if (! DECL_ARTIFICIAL (fn))
12131 instantiate_decl (fn,
12132 /*defer_ok=*/0,
12133 /*expl_inst_class_mem_p=*/false);
12134 if (COMPLETE_TYPE_P (instantiation))
12135 reconsider = 1;
12138 if (COMPLETE_TYPE_P (instantiation))
12139 /* If INSTANTIATION has been instantiated, then we don't
12140 need to consider it again in the future. */
12141 *t = TREE_CHAIN (*t);
12142 else
12144 last = *t;
12145 t = &TREE_CHAIN (*t);
12148 else
12150 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12151 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12153 instantiation
12154 = instantiate_decl (instantiation,
12155 /*defer_ok=*/0,
12156 /*expl_inst_class_mem_p=*/false);
12157 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12158 reconsider = 1;
12161 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12162 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12163 /* If INSTANTIATION has been instantiated, then we don't
12164 need to consider it again in the future. */
12165 *t = TREE_CHAIN (*t);
12166 else
12168 last = *t;
12169 t = &TREE_CHAIN (*t);
12172 tinst_depth = 0;
12173 current_tinst_level = NULL_TREE;
12175 last_pending_template = last;
12177 while (reconsider);
12179 input_location = saved_loc;
12180 in_system_header = saved_in_system_header;
12183 /* Substitute ARGVEC into T, which is a list of initializers for
12184 either base class or a non-static data member. The TREE_PURPOSEs
12185 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12186 instantiate_decl. */
12188 static tree
12189 tsubst_initializer_list (tree t, tree argvec)
12191 tree inits = NULL_TREE;
12193 for (; t; t = TREE_CHAIN (t))
12195 tree decl;
12196 tree init;
12198 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
12199 NULL_TREE);
12200 decl = expand_member_init (decl);
12201 if (decl && !DECL_P (decl))
12202 in_base_initializer = 1;
12204 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
12205 NULL_TREE);
12206 in_base_initializer = 0;
12208 if (decl)
12210 init = build_tree_list (decl, init);
12211 TREE_CHAIN (init) = inits;
12212 inits = init;
12215 return inits;
12218 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12220 static void
12221 set_current_access_from_decl (tree decl)
12223 if (TREE_PRIVATE (decl))
12224 current_access_specifier = access_private_node;
12225 else if (TREE_PROTECTED (decl))
12226 current_access_specifier = access_protected_node;
12227 else
12228 current_access_specifier = access_public_node;
12231 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12232 is the instantiation (which should have been created with
12233 start_enum) and ARGS are the template arguments to use. */
12235 static void
12236 tsubst_enum (tree tag, tree newtag, tree args)
12238 tree e;
12240 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12242 tree value;
12243 tree decl;
12245 decl = TREE_VALUE (e);
12246 /* Note that in a template enum, the TREE_VALUE is the
12247 CONST_DECL, not the corresponding INTEGER_CST. */
12248 value = tsubst_expr (DECL_INITIAL (decl),
12249 args, tf_error | tf_warning,
12250 NULL_TREE);
12252 /* Give this enumeration constant the correct access. */
12253 set_current_access_from_decl (decl);
12255 /* Actually build the enumerator itself. */
12256 build_enumerator (DECL_NAME (decl), value, newtag);
12259 finish_enum (newtag);
12260 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12261 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12264 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12265 its type -- but without substituting the innermost set of template
12266 arguments. So, innermost set of template parameters will appear in
12267 the type. */
12269 tree
12270 get_mostly_instantiated_function_type (tree decl)
12272 tree fn_type;
12273 tree tmpl;
12274 tree targs;
12275 tree tparms;
12276 int parm_depth;
12278 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12279 targs = DECL_TI_ARGS (decl);
12280 tparms = DECL_TEMPLATE_PARMS (tmpl);
12281 parm_depth = TMPL_PARMS_DEPTH (tparms);
12283 /* There should be as many levels of arguments as there are levels
12284 of parameters. */
12285 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12287 fn_type = TREE_TYPE (tmpl);
12289 if (parm_depth == 1)
12290 /* No substitution is necessary. */
12292 else
12294 int i, save_access_control;
12295 tree partial_args;
12297 /* Replace the innermost level of the TARGS with NULL_TREEs to
12298 let tsubst know not to substitute for those parameters. */
12299 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12300 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12301 SET_TMPL_ARGS_LEVEL (partial_args, i,
12302 TMPL_ARGS_LEVEL (targs, i));
12303 SET_TMPL_ARGS_LEVEL (partial_args,
12304 TMPL_ARGS_DEPTH (targs),
12305 make_tree_vec (DECL_NTPARMS (tmpl)));
12307 /* Disable access control as this function is used only during
12308 name-mangling. */
12309 save_access_control = flag_access_control;
12310 flag_access_control = 0;
12312 ++processing_template_decl;
12313 /* Now, do the (partial) substitution to figure out the
12314 appropriate function type. */
12315 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12316 --processing_template_decl;
12318 /* Substitute into the template parameters to obtain the real
12319 innermost set of parameters. This step is important if the
12320 innermost set of template parameters contains value
12321 parameters whose types depend on outer template parameters. */
12322 TREE_VEC_LENGTH (partial_args)--;
12323 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12325 flag_access_control = save_access_control;
12328 return fn_type;
12331 /* Return truthvalue if we're processing a template different from
12332 the last one involved in diagnostics. */
12334 problematic_instantiation_changed (void)
12336 return last_template_error_tick != tinst_level_tick;
12339 /* Remember current template involved in diagnostics. */
12340 void
12341 record_last_problematic_instantiation (void)
12343 last_template_error_tick = tinst_level_tick;
12346 tree
12347 current_instantiation (void)
12349 return current_tinst_level;
12352 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12353 type. Return zero for ok, nonzero for disallowed. Issue error and
12354 warning messages under control of COMPLAIN. */
12356 static int
12357 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12359 if (INTEGRAL_TYPE_P (type))
12360 return 0;
12361 else if (POINTER_TYPE_P (type))
12362 return 0;
12363 else if (TYPE_PTR_TO_MEMBER_P (type))
12364 return 0;
12365 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12366 return 0;
12367 else if (TREE_CODE (type) == TYPENAME_TYPE)
12368 return 0;
12370 if (complain & tf_error)
12371 error ("%q#T is not a valid type for a template constant parameter", type);
12372 return 1;
12375 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12376 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12378 static bool
12379 dependent_type_p_r (tree type)
12381 tree scope;
12383 /* [temp.dep.type]
12385 A type is dependent if it is:
12387 -- a template parameter. Template template parameters are types
12388 for us (since TYPE_P holds true for them) so we handle
12389 them here. */
12390 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12391 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12392 return true;
12393 /* -- a qualified-id with a nested-name-specifier which contains a
12394 class-name that names a dependent type or whose unqualified-id
12395 names a dependent type. */
12396 if (TREE_CODE (type) == TYPENAME_TYPE)
12397 return true;
12398 /* -- a cv-qualified type where the cv-unqualified type is
12399 dependent. */
12400 type = TYPE_MAIN_VARIANT (type);
12401 /* -- a compound type constructed from any dependent type. */
12402 if (TYPE_PTR_TO_MEMBER_P (type))
12403 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12404 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12405 (type)));
12406 else if (TREE_CODE (type) == POINTER_TYPE
12407 || TREE_CODE (type) == REFERENCE_TYPE)
12408 return dependent_type_p (TREE_TYPE (type));
12409 else if (TREE_CODE (type) == FUNCTION_TYPE
12410 || TREE_CODE (type) == METHOD_TYPE)
12412 tree arg_type;
12414 if (dependent_type_p (TREE_TYPE (type)))
12415 return true;
12416 for (arg_type = TYPE_ARG_TYPES (type);
12417 arg_type;
12418 arg_type = TREE_CHAIN (arg_type))
12419 if (dependent_type_p (TREE_VALUE (arg_type)))
12420 return true;
12421 return false;
12423 /* -- an array type constructed from any dependent type or whose
12424 size is specified by a constant expression that is
12425 value-dependent. */
12426 if (TREE_CODE (type) == ARRAY_TYPE)
12428 if (TYPE_DOMAIN (type)
12429 && ((value_dependent_expression_p
12430 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12431 || (type_dependent_expression_p
12432 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12433 return true;
12434 return dependent_type_p (TREE_TYPE (type));
12437 /* -- a template-id in which either the template name is a template
12438 parameter ... */
12439 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12440 return true;
12441 /* ... or any of the template arguments is a dependent type or
12442 an expression that is type-dependent or value-dependent. */
12443 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12444 && (any_dependent_template_arguments_p
12445 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12446 return true;
12448 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12449 expression is not type-dependent, then it should already been
12450 have resolved. */
12451 if (TREE_CODE (type) == TYPEOF_TYPE)
12452 return true;
12454 /* The standard does not specifically mention types that are local
12455 to template functions or local classes, but they should be
12456 considered dependent too. For example:
12458 template <int I> void f() {
12459 enum E { a = I };
12460 S<sizeof (E)> s;
12463 The size of `E' cannot be known until the value of `I' has been
12464 determined. Therefore, `E' must be considered dependent. */
12465 scope = TYPE_CONTEXT (type);
12466 if (scope && TYPE_P (scope))
12467 return dependent_type_p (scope);
12468 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12469 return type_dependent_expression_p (scope);
12471 /* Other types are non-dependent. */
12472 return false;
12475 /* Returns TRUE if TYPE is dependent, in the sense of
12476 [temp.dep.type]. */
12478 bool
12479 dependent_type_p (tree type)
12481 /* If there are no template parameters in scope, then there can't be
12482 any dependent types. */
12483 if (!processing_template_decl)
12484 return false;
12486 /* If the type is NULL, we have not computed a type for the entity
12487 in question; in that case, the type is dependent. */
12488 if (!type)
12489 return true;
12491 /* Erroneous types can be considered non-dependent. */
12492 if (type == error_mark_node)
12493 return false;
12495 /* If we have not already computed the appropriate value for TYPE,
12496 do so now. */
12497 if (!TYPE_DEPENDENT_P_VALID (type))
12499 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12500 TYPE_DEPENDENT_P_VALID (type) = 1;
12503 return TYPE_DEPENDENT_P (type);
12506 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12508 static bool
12509 dependent_scope_ref_p (tree expression, bool criterion (tree))
12511 tree scope;
12512 tree name;
12514 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12516 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12517 return true;
12519 scope = TREE_OPERAND (expression, 0);
12520 name = TREE_OPERAND (expression, 1);
12522 /* [temp.dep.expr]
12524 An id-expression is type-dependent if it contains a
12525 nested-name-specifier that contains a class-name that names a
12526 dependent type. */
12527 /* The suggested resolution to Core Issue 2 implies that if the
12528 qualifying type is the current class, then we must peek
12529 inside it. */
12530 if (DECL_P (name)
12531 && currently_open_class (scope)
12532 && !criterion (name))
12533 return false;
12534 if (dependent_type_p (scope))
12535 return true;
12537 return false;
12540 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12541 [temp.dep.constexpr] */
12543 bool
12544 value_dependent_expression_p (tree expression)
12546 if (!processing_template_decl)
12547 return false;
12549 /* A name declared with a dependent type. */
12550 if (DECL_P (expression) && type_dependent_expression_p (expression))
12551 return true;
12553 switch (TREE_CODE (expression))
12555 case IDENTIFIER_NODE:
12556 /* A name that has not been looked up -- must be dependent. */
12557 return true;
12559 case TEMPLATE_PARM_INDEX:
12560 /* A non-type template parm. */
12561 return true;
12563 case CONST_DECL:
12564 /* A non-type template parm. */
12565 if (DECL_TEMPLATE_PARM_P (expression))
12566 return true;
12567 return value_dependent_expression_p (DECL_INITIAL (expression));
12569 case VAR_DECL:
12570 /* A constant with integral or enumeration type and is initialized
12571 with an expression that is value-dependent. */
12572 if (DECL_INITIAL (expression)
12573 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12574 && value_dependent_expression_p (DECL_INITIAL (expression)))
12575 return true;
12576 return false;
12578 case DYNAMIC_CAST_EXPR:
12579 case STATIC_CAST_EXPR:
12580 case CONST_CAST_EXPR:
12581 case REINTERPRET_CAST_EXPR:
12582 case CAST_EXPR:
12583 /* These expressions are value-dependent if the type to which
12584 the cast occurs is dependent or the expression being casted
12585 is value-dependent. */
12587 tree type = TREE_TYPE (expression);
12589 if (dependent_type_p (type))
12590 return true;
12592 /* A functional cast has a list of operands. */
12593 expression = TREE_OPERAND (expression, 0);
12594 if (!expression)
12596 /* If there are no operands, it must be an expression such
12597 as "int()". This should not happen for aggregate types
12598 because it would form non-constant expressions. */
12599 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12601 return false;
12604 if (TREE_CODE (expression) == TREE_LIST)
12606 for (; expression; expression = TREE_CHAIN (expression))
12607 if (value_dependent_expression_p (TREE_VALUE (expression)))
12608 return true;
12609 return false;
12612 return value_dependent_expression_p (expression);
12615 case SIZEOF_EXPR:
12616 case ALIGNOF_EXPR:
12617 /* A `sizeof' expression is value-dependent if the operand is
12618 type-dependent. */
12619 expression = TREE_OPERAND (expression, 0);
12620 if (TYPE_P (expression))
12621 return dependent_type_p (expression);
12622 return type_dependent_expression_p (expression);
12624 case SCOPE_REF:
12625 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12627 case COMPONENT_REF:
12628 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12629 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12631 case CALL_EXPR:
12632 /* A CALL_EXPR may appear in a constant expression if it is a
12633 call to a builtin function, e.g., __builtin_constant_p. All
12634 such calls are value-dependent. */
12635 return true;
12637 case MODOP_EXPR:
12638 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
12639 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
12641 default:
12642 /* A constant expression is value-dependent if any subexpression is
12643 value-dependent. */
12644 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12646 case tcc_reference:
12647 case tcc_unary:
12648 return (value_dependent_expression_p
12649 (TREE_OPERAND (expression, 0)));
12651 case tcc_comparison:
12652 case tcc_binary:
12653 return ((value_dependent_expression_p
12654 (TREE_OPERAND (expression, 0)))
12655 || (value_dependent_expression_p
12656 (TREE_OPERAND (expression, 1))));
12658 case tcc_expression:
12660 int i;
12661 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12662 /* In some cases, some of the operands may be missing.
12663 (For example, in the case of PREDECREMENT_EXPR, the
12664 amount to increment by may be missing.) That doesn't
12665 make the expression dependent. */
12666 if (TREE_OPERAND (expression, i)
12667 && (value_dependent_expression_p
12668 (TREE_OPERAND (expression, i))))
12669 return true;
12670 return false;
12673 default:
12674 break;
12678 /* The expression is not value-dependent. */
12679 return false;
12682 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12683 [temp.dep.expr]. */
12685 bool
12686 type_dependent_expression_p (tree expression)
12688 if (!processing_template_decl)
12689 return false;
12691 if (expression == error_mark_node)
12692 return false;
12694 /* An unresolved name is always dependent. */
12695 if (TREE_CODE (expression) == IDENTIFIER_NODE
12696 || TREE_CODE (expression) == USING_DECL)
12697 return true;
12699 /* Some expression forms are never type-dependent. */
12700 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12701 || TREE_CODE (expression) == SIZEOF_EXPR
12702 || TREE_CODE (expression) == ALIGNOF_EXPR
12703 || TREE_CODE (expression) == TYPEID_EXPR
12704 || TREE_CODE (expression) == DELETE_EXPR
12705 || TREE_CODE (expression) == VEC_DELETE_EXPR
12706 || TREE_CODE (expression) == THROW_EXPR)
12707 return false;
12709 /* The types of these expressions depends only on the type to which
12710 the cast occurs. */
12711 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12712 || TREE_CODE (expression) == STATIC_CAST_EXPR
12713 || TREE_CODE (expression) == CONST_CAST_EXPR
12714 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12715 || TREE_CODE (expression) == CAST_EXPR)
12716 return dependent_type_p (TREE_TYPE (expression));
12718 /* The types of these expressions depends only on the type created
12719 by the expression. */
12720 if (TREE_CODE (expression) == NEW_EXPR
12721 || TREE_CODE (expression) == VEC_NEW_EXPR)
12723 /* For NEW_EXPR tree nodes created inside a template, either
12724 the object type itself or a TREE_LIST may appear as the
12725 operand 1. */
12726 tree type = TREE_OPERAND (expression, 1);
12727 if (TREE_CODE (type) == TREE_LIST)
12728 /* This is an array type. We need to check array dimensions
12729 as well. */
12730 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12731 || value_dependent_expression_p
12732 (TREE_OPERAND (TREE_VALUE (type), 1));
12733 else
12734 return dependent_type_p (type);
12737 if (TREE_CODE (expression) == SCOPE_REF
12738 && dependent_scope_ref_p (expression,
12739 type_dependent_expression_p))
12740 return true;
12742 if (TREE_CODE (expression) == FUNCTION_DECL
12743 && DECL_LANG_SPECIFIC (expression)
12744 && DECL_TEMPLATE_INFO (expression)
12745 && (any_dependent_template_arguments_p
12746 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12747 return true;
12749 if (TREE_CODE (expression) == TEMPLATE_DECL
12750 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12751 return false;
12753 if (TREE_TYPE (expression) == unknown_type_node)
12755 if (TREE_CODE (expression) == ADDR_EXPR)
12756 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12757 if (TREE_CODE (expression) == COMPONENT_REF
12758 || TREE_CODE (expression) == OFFSET_REF)
12760 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12761 return true;
12762 expression = TREE_OPERAND (expression, 1);
12763 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12764 return false;
12766 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12767 if (TREE_CODE (expression) == SCOPE_REF)
12768 return false;
12770 if (TREE_CODE (expression) == BASELINK)
12771 expression = BASELINK_FUNCTIONS (expression);
12773 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12775 if (any_dependent_template_arguments_p
12776 (TREE_OPERAND (expression, 1)))
12777 return true;
12778 expression = TREE_OPERAND (expression, 0);
12780 gcc_assert (TREE_CODE (expression) == OVERLOAD
12781 || TREE_CODE (expression) == FUNCTION_DECL);
12783 while (expression)
12785 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12786 return true;
12787 expression = OVL_NEXT (expression);
12789 return false;
12792 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
12794 return (dependent_type_p (TREE_TYPE (expression)));
12797 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12798 contains a type-dependent expression. */
12800 bool
12801 any_type_dependent_arguments_p (tree args)
12803 while (args)
12805 tree arg = TREE_VALUE (args);
12807 if (type_dependent_expression_p (arg))
12808 return true;
12809 args = TREE_CHAIN (args);
12811 return false;
12814 /* Returns TRUE if the ARG (a template argument) is dependent. */
12816 static bool
12817 dependent_template_arg_p (tree arg)
12819 if (!processing_template_decl)
12820 return false;
12822 if (TREE_CODE (arg) == TEMPLATE_DECL
12823 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12824 return dependent_template_p (arg);
12825 else if (TYPE_P (arg))
12826 return dependent_type_p (arg);
12827 else
12828 return (type_dependent_expression_p (arg)
12829 || value_dependent_expression_p (arg));
12832 /* Returns true if ARGS (a collection of template arguments) contains
12833 any dependent arguments. */
12835 bool
12836 any_dependent_template_arguments_p (tree args)
12838 int i;
12839 int j;
12841 if (!args)
12842 return false;
12843 if (args == error_mark_node)
12844 return true;
12846 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12848 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12849 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12850 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12851 return true;
12854 return false;
12857 /* Returns TRUE if the template TMPL is dependent. */
12859 bool
12860 dependent_template_p (tree tmpl)
12862 if (TREE_CODE (tmpl) == OVERLOAD)
12864 while (tmpl)
12866 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12867 return true;
12868 tmpl = OVL_CHAIN (tmpl);
12870 return false;
12873 /* Template template parameters are dependent. */
12874 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12875 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12876 return true;
12877 /* So are names that have not been looked up. */
12878 if (TREE_CODE (tmpl) == SCOPE_REF
12879 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12880 return true;
12881 /* So are member templates of dependent classes. */
12882 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12883 return dependent_type_p (DECL_CONTEXT (tmpl));
12884 return false;
12887 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12889 bool
12890 dependent_template_id_p (tree tmpl, tree args)
12892 return (dependent_template_p (tmpl)
12893 || any_dependent_template_arguments_p (args));
12896 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12897 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12898 can be found. Note that this function peers inside uninstantiated
12899 templates and therefore should be used only in extremely limited
12900 situations. ONLY_CURRENT_P restricts this peering to the currently
12901 open classes hierarchy (which is required when comparing types). */
12903 tree
12904 resolve_typename_type (tree type, bool only_current_p)
12906 tree scope;
12907 tree name;
12908 tree decl;
12909 int quals;
12910 tree pushed_scope;
12912 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12914 scope = TYPE_CONTEXT (type);
12915 name = TYPE_IDENTIFIER (type);
12917 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12918 it first before we can figure out what NAME refers to. */
12919 if (TREE_CODE (scope) == TYPENAME_TYPE)
12920 scope = resolve_typename_type (scope, only_current_p);
12921 /* If we don't know what SCOPE refers to, then we cannot resolve the
12922 TYPENAME_TYPE. */
12923 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12924 return error_mark_node;
12925 /* If the SCOPE is a template type parameter, we have no way of
12926 resolving the name. */
12927 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12928 return type;
12929 /* If the SCOPE is not the current instantiation, there's no reason
12930 to look inside it. */
12931 if (only_current_p && !currently_open_class (scope))
12932 return error_mark_node;
12933 /* If SCOPE is a partial instantiation, it will not have a valid
12934 TYPE_FIELDS list, so use the original template. */
12935 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12936 /* Enter the SCOPE so that name lookup will be resolved as if we
12937 were in the class definition. In particular, SCOPE will no
12938 longer be considered a dependent type. */
12939 pushed_scope = push_scope (scope);
12940 /* Look up the declaration. */
12941 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12942 /* Obtain the set of qualifiers applied to the TYPE. */
12943 quals = cp_type_quals (type);
12944 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12945 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12946 if (!decl)
12947 type = error_mark_node;
12948 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12949 && TREE_CODE (decl) == TYPE_DECL)
12950 type = TREE_TYPE (decl);
12951 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12952 && DECL_CLASS_TEMPLATE_P (decl))
12954 tree tmpl;
12955 tree args;
12956 /* Obtain the template and the arguments. */
12957 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12958 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12959 /* Instantiate the template. */
12960 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12961 /*entering_scope=*/0, tf_error | tf_user);
12963 else
12964 type = error_mark_node;
12965 /* Qualify the resulting type. */
12966 if (type != error_mark_node && quals)
12967 type = cp_build_qualified_type (type, quals);
12968 /* Leave the SCOPE. */
12969 if (pushed_scope)
12970 pop_scope (pushed_scope);
12972 return type;
12975 /* EXPR is an expression which is not type-dependent. Return a proxy
12976 for EXPR that can be used to compute the types of larger
12977 expressions containing EXPR. */
12979 tree
12980 build_non_dependent_expr (tree expr)
12982 tree inner_expr;
12984 /* Preserve null pointer constants so that the type of things like
12985 "p == 0" where "p" is a pointer can be determined. */
12986 if (null_ptr_cst_p (expr))
12987 return expr;
12988 /* Preserve OVERLOADs; the functions must be available to resolve
12989 types. */
12990 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12991 TREE_OPERAND (expr, 0) :
12992 TREE_CODE (expr) == COMPONENT_REF ?
12993 TREE_OPERAND (expr, 1) : expr);
12994 if (is_overloaded_fn (inner_expr)
12995 || TREE_CODE (inner_expr) == OFFSET_REF)
12996 return expr;
12997 /* There is no need to return a proxy for a variable. */
12998 if (TREE_CODE (expr) == VAR_DECL)
12999 return expr;
13000 /* Preserve string constants; conversions from string constants to
13001 "char *" are allowed, even though normally a "const char *"
13002 cannot be used to initialize a "char *". */
13003 if (TREE_CODE (expr) == STRING_CST)
13004 return expr;
13005 /* Preserve arithmetic constants, as an optimization -- there is no
13006 reason to create a new node. */
13007 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13008 return expr;
13009 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13010 There is at least one place where we want to know that a
13011 particular expression is a throw-expression: when checking a ?:
13012 expression, there are special rules if the second or third
13013 argument is a throw-expression. */
13014 if (TREE_CODE (expr) == THROW_EXPR)
13015 return expr;
13017 if (TREE_CODE (expr) == COND_EXPR)
13018 return build3 (COND_EXPR,
13019 TREE_TYPE (expr),
13020 TREE_OPERAND (expr, 0),
13021 (TREE_OPERAND (expr, 1)
13022 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13023 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13024 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
13025 if (TREE_CODE (expr) == COMPOUND_EXPR
13026 && !COMPOUND_EXPR_OVERLOADED (expr))
13027 return build2 (COMPOUND_EXPR,
13028 TREE_TYPE (expr),
13029 TREE_OPERAND (expr, 0),
13030 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
13032 /* If the type is unknown, it can't really be non-dependent */
13033 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
13035 /* Otherwise, build a NON_DEPENDENT_EXPR.
13037 REFERENCE_TYPEs are not stripped for expressions in templates
13038 because doing so would play havoc with mangling. Consider, for
13039 example:
13041 template <typename T> void f<T& g>() { g(); }
13043 In the body of "f", the expression for "g" will have
13044 REFERENCE_TYPE, even though the standard says that it should
13045 not. The reason is that we must preserve the syntactic form of
13046 the expression so that mangling (say) "f<g>" inside the body of
13047 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13048 stripped here. */
13049 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
13052 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
13053 Return a new TREE_LIST with the various arguments replaced with
13054 equivalent non-dependent expressions. */
13056 tree
13057 build_non_dependent_args (tree args)
13059 tree a;
13060 tree new_args;
13062 new_args = NULL_TREE;
13063 for (a = args; a; a = TREE_CHAIN (a))
13064 new_args = tree_cons (NULL_TREE,
13065 build_non_dependent_expr (TREE_VALUE (a)),
13066 new_args);
13067 return nreverse (new_args);
13070 #include "gt-cp-pt.h"