Merge from mainline
[official-gcc.git] / gcc / cp / pt.c
blobc113f41b285e14bb48cced7dbbc6f7fc28cafd03
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 int push_tinst_level (tree);
101 static void pop_tinst_level (void);
102 static void reopen_tinst_level (tree);
103 static tree classtype_mangled_name (tree);
104 static char* mangle_class_name_for_template (const char *, tree, tree);
105 static tree tsubst_initializer_list (tree, tree);
106 static tree get_class_bindings (tree, tree, tree);
107 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
108 bool, bool);
109 static void tsubst_enum (tree, tree, tree);
110 static tree add_to_template_args (tree, tree);
111 static tree add_outermost_template_args (tree, tree);
112 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
113 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
114 static int type_unification_real (tree, tree, tree, tree,
115 int, unification_kind_t, int);
116 static void note_template_header (int);
117 static tree convert_nontype_argument_function (tree, tree);
118 static tree convert_nontype_argument (tree, tree);
119 static tree convert_template_argument (tree, tree, tree,
120 tsubst_flags_t, int, tree);
121 static int for_each_template_parm (tree, tree_fn_t, void*,
122 struct pointer_set_t*);
123 static tree build_template_parm_index (int, int, int, tree, tree);
124 static int inline_needs_template_parms (tree);
125 static void push_inline_template_parms_recursive (tree, int);
126 static tree retrieve_local_specialization (tree);
127 static void register_local_specialization (tree, tree);
128 static tree reduce_template_parm_level (tree, tree, int);
129 static int mark_template_parm (tree, void *);
130 static int template_parm_this_level_p (tree, void *);
131 static tree tsubst_friend_function (tree, tree);
132 static tree tsubst_friend_class (tree, tree);
133 static int can_complete_type_without_circularity (tree);
134 static tree get_bindings (tree, tree, tree, bool);
135 static int template_decl_level (tree);
136 static int check_cv_quals_for_unify (int, tree, tree);
137 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
139 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140 static void regenerate_decl_from_template (tree, tree);
141 static tree most_specialized_class (tree, tree);
142 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
145 static void check_specialization_scope (void);
146 static tree process_partial_specialization (tree);
147 static void set_current_access_from_decl (tree);
148 static void check_default_tmpl_args (tree, tree, int, int);
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 tree determine_specialization (tree, tree, tree *, int, int);
154 static int template_args_equal (tree, tree);
155 static void tsubst_default_arguments (tree);
156 static tree for_each_template_parm_r (tree *, int *, void *);
157 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
158 static void copy_default_args_to_explicit_spec (tree);
159 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
160 static int eq_local_specializations (const void *, const void *);
161 static bool dependent_type_p_r (tree);
162 static tree tsubst (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
166 /* Make the current scope suitable for access checking when we are
167 processing T. T can be FUNCTION_DECL for instantiated function
168 template, or VAR_DECL for static member variable (need by
169 instantiate_decl). */
171 static void
172 push_access_scope (tree t)
174 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
175 || TREE_CODE (t) == VAR_DECL);
177 if (DECL_FRIEND_CONTEXT (t))
178 push_nested_class (DECL_FRIEND_CONTEXT (t));
179 else if (DECL_CLASS_SCOPE_P (t))
180 push_nested_class (DECL_CONTEXT (t));
181 else
182 push_to_top_level ();
184 if (TREE_CODE (t) == FUNCTION_DECL)
186 saved_access_scope = tree_cons
187 (NULL_TREE, current_function_decl, saved_access_scope);
188 current_function_decl = t;
192 /* Restore the scope set up by push_access_scope. T is the node we
193 are processing. */
195 static void
196 pop_access_scope (tree t)
198 if (TREE_CODE (t) == FUNCTION_DECL)
200 current_function_decl = TREE_VALUE (saved_access_scope);
201 saved_access_scope = TREE_CHAIN (saved_access_scope);
204 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
205 pop_nested_class ();
206 else
207 pop_from_top_level ();
210 /* Do any processing required when DECL (a member template
211 declaration) is finished. Returns the TEMPLATE_DECL corresponding
212 to DECL, unless it is a specialization, in which case the DECL
213 itself is returned. */
215 tree
216 finish_member_template_decl (tree decl)
218 if (decl == error_mark_node)
219 return error_mark_node;
221 gcc_assert (DECL_P (decl));
223 if (TREE_CODE (decl) == TYPE_DECL)
225 tree type;
227 type = TREE_TYPE (decl);
228 if (IS_AGGR_TYPE (type)
229 && CLASSTYPE_TEMPLATE_INFO (type)
230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
232 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
233 check_member_template (tmpl);
234 return tmpl;
236 return NULL_TREE;
238 else if (TREE_CODE (decl) == FIELD_DECL)
239 error ("data member %qD cannot be a member template", decl);
240 else if (DECL_TEMPLATE_INFO (decl))
242 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
244 check_member_template (DECL_TI_TEMPLATE (decl));
245 return DECL_TI_TEMPLATE (decl);
247 else
248 return decl;
250 else
251 error ("invalid member template declaration %qD", decl);
253 return error_mark_node;
256 /* Returns the template nesting level of the indicated class TYPE.
258 For example, in:
259 template <class T>
260 struct A
262 template <class U>
263 struct B {};
266 A<T>::B<U> has depth two, while A<T> has depth one.
267 Both A<T>::B<int> and A<int>::B<U> have depth one, if
268 they are instantiations, not 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. */
275 template_class_depth (tree type)
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 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
289 ++depth;
291 else
293 if (DECL_TEMPLATE_INFO (type)
294 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
295 && uses_template_parms (DECL_TI_ARGS (type)))
296 ++depth;
300 return depth;
303 /* Returns 1 if processing DECL as part of do_pending_inlines
304 needs us to push template parms. */
306 static int
307 inline_needs_template_parms (tree decl)
309 if (! DECL_TEMPLATE_INFO (decl))
310 return 0;
312 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
313 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
316 /* Subroutine of maybe_begin_member_template_processing.
317 Push the template parms in PARMS, starting from LEVELS steps into the
318 chain, and ending at the beginning, since template parms are listed
319 innermost first. */
321 static void
322 push_inline_template_parms_recursive (tree parmlist, int levels)
324 tree parms = TREE_VALUE (parmlist);
325 int i;
327 if (levels > 1)
328 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
330 ++processing_template_decl;
331 current_template_parms
332 = tree_cons (size_int (processing_template_decl),
333 parms, current_template_parms);
334 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
336 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
337 NULL);
338 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
340 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
341 gcc_assert (DECL_P (parm));
343 switch (TREE_CODE (parm))
345 case TYPE_DECL:
346 case TEMPLATE_DECL:
347 pushdecl (parm);
348 break;
350 case PARM_DECL:
352 /* Make a CONST_DECL as is done in process_template_parm.
353 It is ugly that we recreate this here; the original
354 version built in process_template_parm is no longer
355 available. */
356 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
357 TREE_TYPE (parm));
358 DECL_ARTIFICIAL (decl) = 1;
359 TREE_CONSTANT (decl) = 1;
360 TREE_INVARIANT (decl) = 1;
361 TREE_READONLY (decl) = 1;
362 DECL_INITIAL (decl) = DECL_INITIAL (parm);
363 SET_DECL_TEMPLATE_PARM_P (decl);
364 pushdecl (decl);
366 break;
368 default:
369 gcc_unreachable ();
374 /* Restore the template parameter context for a member template or
375 a friend template defined in a class definition. */
377 void
378 maybe_begin_member_template_processing (tree decl)
380 tree parms;
381 int levels = 0;
383 if (inline_needs_template_parms (decl))
385 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
386 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
388 if (DECL_TEMPLATE_SPECIALIZATION (decl))
390 --levels;
391 parms = TREE_CHAIN (parms);
394 push_inline_template_parms_recursive (parms, levels);
397 /* Remember how many levels of template parameters we pushed so that
398 we can pop them later. */
399 if (!inline_parm_levels)
400 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
401 if (inline_parm_levels_used == inline_parm_levels->num_elements)
402 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
403 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
404 ++inline_parm_levels_used;
407 /* Undo the effects of maybe_begin_member_template_processing. */
409 void
410 maybe_end_member_template_processing (void)
412 int i;
414 if (!inline_parm_levels_used)
415 return;
417 --inline_parm_levels_used;
418 for (i = 0;
419 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
420 ++i)
422 --processing_template_decl;
423 current_template_parms = TREE_CHAIN (current_template_parms);
424 poplevel (0, 0, 0);
428 /* Return a new template argument vector which contains all of ARGS,
429 but has as its innermost set of arguments the EXTRA_ARGS. */
431 static tree
432 add_to_template_args (tree args, tree extra_args)
434 tree new_args;
435 int extra_depth;
436 int i;
437 int j;
439 extra_depth = TMPL_ARGS_DEPTH (extra_args);
440 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
442 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
445 for (j = 1; j <= extra_depth; ++j, ++i)
446 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
448 return new_args;
451 /* Like add_to_template_args, but only the outermost ARGS are added to
452 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
453 (EXTRA_ARGS) levels are added. This function is used to combine
454 the template arguments from a partial instantiation with the
455 template arguments used to attain the full instantiation from the
456 partial instantiation. */
458 static tree
459 add_outermost_template_args (tree args, tree extra_args)
461 tree new_args;
463 /* If there are more levels of EXTRA_ARGS than there are ARGS,
464 something very fishy is going on. */
465 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
467 /* If *all* the new arguments will be the EXTRA_ARGS, just return
468 them. */
469 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
470 return extra_args;
472 /* For the moment, we make ARGS look like it contains fewer levels. */
473 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
475 new_args = add_to_template_args (args, extra_args);
477 /* Now, we restore ARGS to its full dimensions. */
478 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
480 return new_args;
483 /* Return the N levels of innermost template arguments from the ARGS. */
485 tree
486 get_innermost_template_args (tree args, int n)
488 tree new_args;
489 int extra_levels;
490 int i;
492 gcc_assert (n >= 0);
494 /* If N is 1, just return the innermost set of template arguments. */
495 if (n == 1)
496 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
498 /* If we're not removing anything, just return the arguments we were
499 given. */
500 extra_levels = TMPL_ARGS_DEPTH (args) - n;
501 gcc_assert (extra_levels >= 0);
502 if (extra_levels == 0)
503 return args;
505 /* Make a new set of arguments, not containing the outer arguments. */
506 new_args = make_tree_vec (n);
507 for (i = 1; i <= n; ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i,
509 TMPL_ARGS_LEVEL (args, i + extra_levels));
511 return new_args;
514 /* We've got a template header coming up; push to a new level for storing
515 the parms. */
517 void
518 begin_template_parm_list (void)
520 /* We use a non-tag-transparent scope here, which causes pushtag to
521 put tags in this scope, rather than in the enclosing class or
522 namespace scope. This is the right thing, since we want
523 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
524 global template class, push_template_decl handles putting the
525 TEMPLATE_DECL into top-level scope. For a nested template class,
526 e.g.:
528 template <class T> struct S1 {
529 template <class T> struct S2 {};
532 pushtag contains special code to call pushdecl_with_scope on the
533 TEMPLATE_DECL for S2. */
534 begin_scope (sk_template_parms, NULL);
535 ++processing_template_decl;
536 ++processing_template_parmlist;
537 note_template_header (0);
540 /* This routine is called when a specialization is declared. If it is
541 invalid to declare a specialization here, an error is reported. */
543 static void
544 check_specialization_scope (void)
546 tree scope = current_scope ();
548 /* [temp.expl.spec]
550 An explicit specialization shall be declared in the namespace of
551 which the template is a member, or, for member templates, in the
552 namespace of which the enclosing class or enclosing class
553 template is a member. An explicit specialization of a member
554 function, member class or static data member of a class template
555 shall be declared in the namespace of which the class template
556 is a member. */
557 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
558 error ("explicit specialization in non-namespace scope %qD", scope);
560 /* [temp.expl.spec]
562 In an explicit specialization declaration for a member of a class
563 template or a member template that appears in namespace scope,
564 the member template and some of its enclosing class templates may
565 remain unspecialized, except that the declaration shall not
566 explicitly specialize a class member template if its enclosing
567 class templates are not explicitly specialized as well. */
568 if (current_template_parms)
569 error ("enclosing class templates are not explicitly specialized");
572 /* We've just seen template <>. */
574 void
575 begin_specialization (void)
577 begin_scope (sk_template_spec, NULL);
578 note_template_header (1);
579 check_specialization_scope ();
582 /* Called at then end of processing a declaration preceded by
583 template<>. */
585 void
586 end_specialization (void)
588 finish_scope ();
589 reset_specialization ();
592 /* Any template <>'s that we have seen thus far are not referring to a
593 function specialization. */
595 void
596 reset_specialization (void)
598 processing_specialization = 0;
599 template_header_count = 0;
602 /* We've just seen a template header. If SPECIALIZATION is nonzero,
603 it was of the form template <>. */
605 static void
606 note_template_header (int specialization)
608 processing_specialization = specialization;
609 template_header_count++;
612 /* We're beginning an explicit instantiation. */
614 void
615 begin_explicit_instantiation (void)
617 gcc_assert (!processing_explicit_instantiation);
618 processing_explicit_instantiation = true;
622 void
623 end_explicit_instantiation (void)
625 gcc_assert (processing_explicit_instantiation);
626 processing_explicit_instantiation = false;
629 /* An explicit specialization or partial specialization TMPL is being
630 declared. Check that the namespace in which the specialization is
631 occurring is permissible. Returns false iff it is invalid to
632 specialize TMPL in the current namespace. */
634 static bool
635 check_specialization_namespace (tree tmpl)
637 tree tpl_ns = decl_namespace_context (tmpl);
639 /* [tmpl.expl.spec]
641 An explicit specialization shall be declared in the namespace of
642 which the template is a member, or, for member templates, in the
643 namespace of which the enclosing class or enclosing class
644 template is a member. An explicit specialization of a member
645 function, member class or static data member of a class template
646 shall be declared in the namespace of which the class template is
647 a member. */
648 if (is_associated_namespace (current_namespace, tpl_ns))
649 /* Same or super-using namespace. */
650 return true;
651 else
653 pedwarn ("specialization of %qD in different namespace", tmpl);
654 pedwarn (" from definition of %q+#D", tmpl);
655 return false;
659 /* SPEC is an explicit instantiation. Check that it is valid to
660 perform this explicit instantiation in the current namespace. */
662 static void
663 check_explicit_instantiation_namespace (tree spec)
665 tree ns;
667 /* DR 275: An explicit instantiation shall appear in an enclosing
668 namespace of its template. */
669 ns = decl_namespace_context (spec);
670 if (!is_ancestor (current_namespace, ns))
671 pedwarn ("explicit instantiation of %qD in namespace %qD "
672 "(which does not enclose namespace %qD)",
673 spec, current_namespace, ns);
676 /* The TYPE is being declared. If it is a template type, that means it
677 is a partial specialization. Do appropriate error-checking. */
679 void
680 maybe_process_partial_specialization (tree type)
682 tree context;
684 if (type == error_mark_node)
685 return;
687 context = TYPE_CONTEXT (type);
689 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
691 /* This is for ordinary explicit specialization and partial
692 specialization of a template class such as:
694 template <> class C<int>;
698 template <class T> class C<T*>;
700 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
702 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
703 && !COMPLETE_TYPE_P (type))
705 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
706 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
707 if (processing_template_decl)
708 push_template_decl (TYPE_MAIN_DECL (type));
710 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
711 error ("specialization of %qT after instantiation", type);
713 else if (CLASS_TYPE_P (type)
714 && !CLASSTYPE_USE_TEMPLATE (type)
715 && CLASSTYPE_TEMPLATE_INFO (type)
716 && context && CLASS_TYPE_P (context)
717 && CLASSTYPE_TEMPLATE_INFO (context))
719 /* This is for an explicit specialization of member class
720 template according to [temp.expl.spec/18]:
722 template <> template <class U> class C<int>::D;
724 The context `C<int>' must be an implicit instantiation.
725 Otherwise this is just a member class template declared
726 earlier like:
728 template <> class C<int> { template <class U> class D; };
729 template <> template <class U> class C<int>::D;
731 In the first case, `C<int>::D' is a specialization of `C<T>::D'
732 while in the second case, `C<int>::D' is a primary template
733 and `C<T>::D' may not exist. */
735 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
736 && !COMPLETE_TYPE_P (type))
738 tree t;
740 if (current_namespace
741 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
743 pedwarn ("specializing %q#T in different namespace", type);
744 pedwarn (" from definition of %q+#D",
745 CLASSTYPE_TI_TEMPLATE (type));
748 /* Check for invalid specialization after instantiation:
750 template <> template <> class C<int>::D<int>;
751 template <> template <class U> class C<int>::D; */
753 for (t = DECL_TEMPLATE_INSTANTIATIONS
754 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
755 t; t = TREE_CHAIN (t))
756 if (TREE_VALUE (t) != type
757 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
758 error ("specialization %qT after instantiation %qT",
759 type, TREE_VALUE (t));
761 /* Mark TYPE as a specialization. And as a result, we only
762 have one level of template argument for the innermost
763 class template. */
764 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
765 CLASSTYPE_TI_ARGS (type)
766 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
769 else if (processing_specialization)
770 error ("explicit specialization of non-template %qT", type);
773 /* Returns nonzero if we can optimize the retrieval of specializations
774 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
775 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
777 static inline bool
778 optimize_specialization_lookup_p (tree tmpl)
780 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
781 && DECL_CLASS_SCOPE_P (tmpl)
782 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
783 parameter. */
784 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
785 /* The optimized lookup depends on the fact that the
786 template arguments for the member function template apply
787 purely to the containing class, which is not true if the
788 containing class is an explicit or partial
789 specialization. */
790 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
791 && !DECL_MEMBER_TEMPLATE_P (tmpl)
792 && !DECL_CONV_FN_P (tmpl)
793 /* It is possible to have a template that is not a member
794 template and is not a member of a template class:
796 template <typename T>
797 struct S { friend A::f(); };
799 Here, the friend function is a template, but the context does
800 not have template information. The optimized lookup relies
801 on having ARGS be the template arguments for both the class
802 and the function template. */
803 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
806 /* Retrieve the specialization (in the sense of [temp.spec] - a
807 specialization is either an instantiation or an explicit
808 specialization) of TMPL for the given template ARGS. If there is
809 no such specialization, return NULL_TREE. The ARGS are a vector of
810 arguments, or a vector of vectors of arguments, in the case of
811 templates with more than one level of parameters.
813 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
814 then we search for a partial specialization matching ARGS. This
815 parameter is ignored if TMPL is not a class template. */
817 static tree
818 retrieve_specialization (tree tmpl, tree args,
819 bool class_specializations_p)
821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
823 /* There should be as many levels of arguments as there are
824 levels of parameters. */
825 gcc_assert (TMPL_ARGS_DEPTH (args)
826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
828 if (optimize_specialization_lookup_p (tmpl))
830 tree class_template;
831 tree class_specialization;
832 VEC(tree,gc) *methods;
833 tree fns;
834 int idx;
836 /* The template arguments actually apply to the containing
837 class. Find the class specialization with those
838 arguments. */
839 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
840 class_specialization
841 = retrieve_specialization (class_template, args,
842 /*class_specializations_p=*/false);
843 if (!class_specialization)
844 return NULL_TREE;
845 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
846 for the specialization. */
847 idx = class_method_index_for_fn (class_specialization, tmpl);
848 if (idx == -1)
849 return NULL_TREE;
850 /* Iterate through the methods with the indicated name, looking
851 for the one that has an instance of TMPL. */
852 methods = CLASSTYPE_METHOD_VEC (class_specialization);
853 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
855 tree fn = OVL_CURRENT (fns);
856 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
857 return fn;
859 return NULL_TREE;
861 else
863 tree *sp;
864 tree *head;
866 /* Class templates store their instantiations on the
867 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
868 DECL_TEMPLATE_SPECIALIZATIONS list. */
869 if (!class_specializations_p
870 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
871 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
872 else
873 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
874 head = sp;
875 /* Iterate through the list until we find a matching template. */
876 while (*sp != NULL_TREE)
878 tree spec = *sp;
880 if (comp_template_args (TREE_PURPOSE (spec), args))
882 /* Use the move-to-front heuristic to speed up future
883 searches. */
884 if (spec != *head)
886 *sp = TREE_CHAIN (*sp);
887 TREE_CHAIN (spec) = *head;
888 *head = spec;
890 return TREE_VALUE (spec);
892 sp = &TREE_CHAIN (spec);
896 return NULL_TREE;
899 /* Like retrieve_specialization, but for local declarations. */
901 static tree
902 retrieve_local_specialization (tree tmpl)
904 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
905 htab_hash_pointer (tmpl));
906 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
909 /* Returns nonzero iff DECL is a specialization of TMPL. */
912 is_specialization_of (tree decl, tree tmpl)
914 tree t;
916 if (TREE_CODE (decl) == FUNCTION_DECL)
918 for (t = decl;
919 t != NULL_TREE;
920 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
921 if (t == tmpl)
922 return 1;
924 else
926 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
928 for (t = TREE_TYPE (decl);
929 t != NULL_TREE;
930 t = CLASSTYPE_USE_TEMPLATE (t)
931 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
932 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
933 return 1;
936 return 0;
939 /* Returns nonzero iff DECL is a specialization of friend declaration
940 FRIEND according to [temp.friend]. */
942 bool
943 is_specialization_of_friend (tree decl, tree friend)
945 bool need_template = true;
946 int template_depth;
948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
949 || TREE_CODE (decl) == TYPE_DECL);
951 /* For [temp.friend/6] when FRIEND is an ordinary member function
952 of a template class, we want to check if DECL is a specialization
953 if this. */
954 if (TREE_CODE (friend) == FUNCTION_DECL
955 && DECL_TEMPLATE_INFO (friend)
956 && !DECL_USE_TEMPLATE (friend))
958 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
959 friend = DECL_TI_TEMPLATE (friend);
960 need_template = false;
962 else if (TREE_CODE (friend) == TEMPLATE_DECL
963 && !PRIMARY_TEMPLATE_P (friend))
964 need_template = false;
966 /* There is nothing to do if this is not a template friend. */
967 if (TREE_CODE (friend) != TEMPLATE_DECL)
968 return false;
970 if (is_specialization_of (decl, friend))
971 return true;
973 /* [temp.friend/6]
974 A member of a class template may be declared to be a friend of a
975 non-template class. In this case, the corresponding member of
976 every specialization of the class template is a friend of the
977 class granting friendship.
979 For example, given a template friend declaration
981 template <class T> friend void A<T>::f();
983 the member function below is considered a friend
985 template <> struct A<int> {
986 void f();
989 For this type of template friend, TEMPLATE_DEPTH below will be
990 nonzero. To determine if DECL is a friend of FRIEND, we first
991 check if the enclosing class is a specialization of another. */
993 template_depth = template_class_depth (DECL_CONTEXT (friend));
994 if (template_depth
995 && DECL_CLASS_SCOPE_P (decl)
996 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
997 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
999 /* Next, we check the members themselves. In order to handle
1000 a few tricky cases, such as when FRIEND's are
1002 template <class T> friend void A<T>::g(T t);
1003 template <class T> template <T t> friend void A<T>::h();
1005 and DECL's are
1007 void A<int>::g(int);
1008 template <int> void A<int>::h();
1010 we need to figure out ARGS, the template arguments from
1011 the context of DECL. This is required for template substitution
1012 of `T' in the function parameter of `g' and template parameter
1013 of `h' in the above examples. Here ARGS corresponds to `int'. */
1015 tree context = DECL_CONTEXT (decl);
1016 tree args = NULL_TREE;
1017 int current_depth = 0;
1019 while (current_depth < template_depth)
1021 if (CLASSTYPE_TEMPLATE_INFO (context))
1023 if (current_depth == 0)
1024 args = TYPE_TI_ARGS (context);
1025 else
1026 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1027 current_depth++;
1029 context = TYPE_CONTEXT (context);
1032 if (TREE_CODE (decl) == FUNCTION_DECL)
1034 bool is_template;
1035 tree friend_type;
1036 tree decl_type;
1037 tree friend_args_type;
1038 tree decl_args_type;
1040 /* Make sure that both DECL and FRIEND are templates or
1041 non-templates. */
1042 is_template = DECL_TEMPLATE_INFO (decl)
1043 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1044 if (need_template ^ is_template)
1045 return false;
1046 else if (is_template)
1048 /* If both are templates, check template parameter list. */
1049 tree friend_parms
1050 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1051 args, tf_none);
1052 if (!comp_template_parms
1053 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1054 friend_parms))
1055 return false;
1057 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1059 else
1060 decl_type = TREE_TYPE (decl);
1062 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1063 tf_none, NULL_TREE);
1064 if (friend_type == error_mark_node)
1065 return false;
1067 /* Check if return types match. */
1068 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1069 return false;
1071 /* Check if function parameter types match, ignoring the
1072 `this' parameter. */
1073 friend_args_type = TYPE_ARG_TYPES (friend_type);
1074 decl_args_type = TYPE_ARG_TYPES (decl_type);
1075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1076 friend_args_type = TREE_CHAIN (friend_args_type);
1077 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1078 decl_args_type = TREE_CHAIN (decl_args_type);
1080 return compparms (decl_args_type, friend_args_type);
1082 else
1084 /* DECL is a TYPE_DECL */
1085 bool is_template;
1086 tree decl_type = TREE_TYPE (decl);
1088 /* Make sure that both DECL and FRIEND are templates or
1089 non-templates. */
1090 is_template
1091 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1094 if (need_template ^ is_template)
1095 return false;
1096 else if (is_template)
1098 tree friend_parms;
1099 /* If both are templates, check the name of the two
1100 TEMPLATE_DECL's first because is_friend didn't. */
1101 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1102 != DECL_NAME (friend))
1103 return false;
1105 /* Now check template parameter list. */
1106 friend_parms
1107 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1108 args, tf_none);
1109 return comp_template_parms
1110 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1111 friend_parms);
1113 else
1114 return (DECL_NAME (decl)
1115 == DECL_NAME (friend));
1118 return false;
1121 /* Register the specialization SPEC as a specialization of TMPL with
1122 the indicated ARGS. IS_FRIEND indicates whether the specialization
1123 is actually just a friend declaration. Returns SPEC, or an
1124 equivalent prior declaration, if available. */
1126 static tree
1127 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1129 tree fn;
1131 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1133 if (TREE_CODE (spec) == FUNCTION_DECL
1134 && uses_template_parms (DECL_TI_ARGS (spec)))
1135 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1136 register it; we want the corresponding TEMPLATE_DECL instead.
1137 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1138 the more obvious `uses_template_parms (spec)' to avoid problems
1139 with default function arguments. In particular, given
1140 something like this:
1142 template <class T> void f(T t1, T t = T())
1144 the default argument expression is not substituted for in an
1145 instantiation unless and until it is actually needed. */
1146 return spec;
1148 fn = retrieve_specialization (tmpl, args,
1149 /*class_specializations_p=*/false);
1150 /* We can sometimes try to re-register a specialization that we've
1151 already got. In particular, regenerate_decl_from_template calls
1152 duplicate_decls which will update the specialization list. But,
1153 we'll still get called again here anyhow. It's more convenient
1154 to simply allow this than to try to prevent it. */
1155 if (fn == spec)
1156 return spec;
1157 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1159 if (DECL_TEMPLATE_INSTANTIATION (fn))
1161 if (TREE_USED (fn)
1162 || DECL_EXPLICIT_INSTANTIATION (fn))
1164 error ("specialization of %qD after instantiation",
1165 fn);
1166 return spec;
1168 else
1170 tree clone;
1171 /* This situation should occur only if the first
1172 specialization is an implicit instantiation, the
1173 second is an explicit specialization, and the
1174 implicit instantiation has not yet been used. That
1175 situation can occur if we have implicitly
1176 instantiated a member function and then specialized
1177 it later.
1179 We can also wind up here if a friend declaration that
1180 looked like an instantiation turns out to be a
1181 specialization:
1183 template <class T> void foo(T);
1184 class S { friend void foo<>(int) };
1185 template <> void foo(int);
1187 We transform the existing DECL in place so that any
1188 pointers to it become pointers to the updated
1189 declaration.
1191 If there was a definition for the template, but not
1192 for the specialization, we want this to look as if
1193 there were no definition, and vice versa. */
1194 DECL_INITIAL (fn) = NULL_TREE;
1195 duplicate_decls (spec, fn, is_friend);
1196 /* The call to duplicate_decls will have applied
1197 [temp.expl.spec]:
1199 An explicit specialization of a function template
1200 is inline only if it is explicitly declared to be,
1201 and independently of whether its function template
1204 to the primary function; now copy the inline bits to
1205 the various clones. */
1206 FOR_EACH_CLONE (clone, fn)
1208 DECL_DECLARED_INLINE_P (clone)
1209 = DECL_DECLARED_INLINE_P (fn);
1210 DECL_INLINE (clone)
1211 = DECL_INLINE (fn);
1213 check_specialization_namespace (fn);
1215 return fn;
1218 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1220 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1221 /* Dup decl failed, but this is a new definition. Set the
1222 line number so any errors match this new
1223 definition. */
1224 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1226 return fn;
1230 /* A specialization must be declared in the same namespace as the
1231 template it is specializing. */
1232 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1233 && !check_specialization_namespace (tmpl))
1234 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1236 if (!optimize_specialization_lookup_p (tmpl))
1237 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1238 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1240 return spec;
1243 /* Unregister the specialization SPEC as a specialization of TMPL.
1244 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1245 if the SPEC was listed as a specialization of TMPL. */
1247 bool
1248 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1250 tree* s;
1252 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1253 *s != NULL_TREE;
1254 s = &TREE_CHAIN (*s))
1255 if (TREE_VALUE (*s) == spec)
1257 if (!new_spec)
1258 *s = TREE_CHAIN (*s);
1259 else
1260 TREE_VALUE (*s) = new_spec;
1261 return 1;
1264 return 0;
1267 /* Compare an entry in the local specializations hash table P1 (which
1268 is really a pointer to a TREE_LIST) with P2 (which is really a
1269 DECL). */
1271 static int
1272 eq_local_specializations (const void *p1, const void *p2)
1274 return TREE_VALUE ((tree) p1) == (tree) p2;
1277 /* Hash P1, an entry in the local specializations table. */
1279 static hashval_t
1280 hash_local_specialization (const void* p1)
1282 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1285 /* Like register_specialization, but for local declarations. We are
1286 registering SPEC, an instantiation of TMPL. */
1288 static void
1289 register_local_specialization (tree spec, tree tmpl)
1291 void **slot;
1293 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1294 htab_hash_pointer (tmpl), INSERT);
1295 *slot = build_tree_list (spec, tmpl);
1298 /* Print the list of candidate FNS in an error message. */
1300 void
1301 print_candidates (tree fns)
1303 tree fn;
1305 const char *str = "candidates are:";
1307 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1309 tree f;
1311 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1312 error ("%s %+#D", str, OVL_CURRENT (f));
1313 str = " ";
1317 /* Returns the template (one of the functions given by TEMPLATE_ID)
1318 which can be specialized to match the indicated DECL with the
1319 explicit template args given in TEMPLATE_ID. The DECL may be
1320 NULL_TREE if none is available. In that case, the functions in
1321 TEMPLATE_ID are non-members.
1323 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1324 specialization of a member template.
1326 The TEMPLATE_COUNT is the number of references to qualifying
1327 template classes that appeared in the name of the function. See
1328 check_explicit_specialization for a more accurate description.
1330 The template args (those explicitly specified and those deduced)
1331 are output in a newly created vector *TARGS_OUT.
1333 If it is impossible to determine the result, an error message is
1334 issued. The error_mark_node is returned to indicate failure. */
1336 static tree
1337 determine_specialization (tree template_id,
1338 tree decl,
1339 tree* targs_out,
1340 int need_member_template,
1341 int template_count)
1343 tree fns;
1344 tree targs;
1345 tree explicit_targs;
1346 tree candidates = NULL_TREE;
1347 /* A TREE_LIST of templates of which DECL may be a specialization.
1348 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1349 corresponding TREE_PURPOSE is the set of template arguments that,
1350 when used to instantiate the template, would produce a function
1351 with the signature of DECL. */
1352 tree templates = NULL_TREE;
1353 int header_count;
1354 struct cp_binding_level *b;
1356 *targs_out = NULL_TREE;
1358 if (template_id == error_mark_node)
1359 return error_mark_node;
1361 fns = TREE_OPERAND (template_id, 0);
1362 explicit_targs = TREE_OPERAND (template_id, 1);
1364 if (fns == error_mark_node)
1365 return error_mark_node;
1367 /* Check for baselinks. */
1368 if (BASELINK_P (fns))
1369 fns = BASELINK_FUNCTIONS (fns);
1371 if (!is_overloaded_fn (fns))
1373 error ("%qD is not a function template", fns);
1374 return error_mark_node;
1377 /* Count the number of template headers specified for this
1378 specialization. */
1379 header_count = 0;
1380 for (b = current_binding_level;
1381 b->kind == sk_template_parms;
1382 b = b->level_chain)
1383 ++header_count;
1385 for (; fns; fns = OVL_NEXT (fns))
1387 tree fn = OVL_CURRENT (fns);
1389 if (TREE_CODE (fn) == TEMPLATE_DECL)
1391 tree decl_arg_types;
1392 tree fn_arg_types;
1394 /* DECL might be a specialization of FN. */
1396 /* Adjust the type of DECL in case FN is a static member. */
1397 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1398 if (DECL_STATIC_FUNCTION_P (fn)
1399 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1400 decl_arg_types = TREE_CHAIN (decl_arg_types);
1402 /* Check that the number of function parameters matches.
1403 For example,
1404 template <class T> void f(int i = 0);
1405 template <> void f<int>();
1406 The specialization f<int> is invalid but is not caught
1407 by get_bindings below. */
1409 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1410 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1411 continue;
1413 /* For a non-static member function, we need to make sure that
1414 the const qualification is the same. This can be done by
1415 checking the 'this' in the argument list. */
1416 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1417 && !same_type_p (TREE_VALUE (fn_arg_types),
1418 TREE_VALUE (decl_arg_types)))
1419 continue;
1421 /* In case of explicit specialization, we need to check if
1422 the number of template headers appearing in the specialization
1423 is correct. This is usually done in check_explicit_specialization,
1424 but the check done there cannot be exhaustive when specializing
1425 member functions. Consider the following code:
1427 template <> void A<int>::f(int);
1428 template <> template <> void A<int>::f(int);
1430 Assuming that A<int> is not itself an explicit specialization
1431 already, the first line specializes "f" which is a non-template
1432 member function, whilst the second line specializes "f" which
1433 is a template member function. So both lines are syntactically
1434 correct, and check_explicit_specialization does not reject
1435 them.
1437 Here, we can do better, as we are matching the specialization
1438 against the declarations. We count the number of template
1439 headers, and we check if they match TEMPLATE_COUNT + 1
1440 (TEMPLATE_COUNT is the number of qualifying template classes,
1441 plus there must be another header for the member template
1442 itself).
1444 Notice that if header_count is zero, this is not a
1445 specialization but rather a template instantiation, so there
1446 is no check we can perform here. */
1447 if (header_count && header_count != template_count + 1)
1448 continue;
1450 /* Check that the number of template arguments at the
1451 innermost level for DECL is the same as for FN. */
1452 if (current_binding_level->kind == sk_template_parms
1453 && !current_binding_level->explicit_spec_p
1454 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1455 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1456 (current_template_parms))))
1457 continue;
1459 /* See whether this function might be a specialization of this
1460 template. */
1461 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1463 if (!targs)
1464 /* We cannot deduce template arguments that when used to
1465 specialize TMPL will produce DECL. */
1466 continue;
1468 /* Save this template, and the arguments deduced. */
1469 templates = tree_cons (targs, fn, templates);
1471 else if (need_member_template)
1472 /* FN is an ordinary member function, and we need a
1473 specialization of a member template. */
1475 else if (TREE_CODE (fn) != FUNCTION_DECL)
1476 /* We can get IDENTIFIER_NODEs here in certain erroneous
1477 cases. */
1479 else if (!DECL_FUNCTION_MEMBER_P (fn))
1480 /* This is just an ordinary non-member function. Nothing can
1481 be a specialization of that. */
1483 else if (DECL_ARTIFICIAL (fn))
1484 /* Cannot specialize functions that are created implicitly. */
1486 else
1488 tree decl_arg_types;
1490 /* This is an ordinary member function. However, since
1491 we're here, we can assume it's enclosing class is a
1492 template class. For example,
1494 template <typename T> struct S { void f(); };
1495 template <> void S<int>::f() {}
1497 Here, S<int>::f is a non-template, but S<int> is a
1498 template class. If FN has the same type as DECL, we
1499 might be in business. */
1501 if (!DECL_TEMPLATE_INFO (fn))
1502 /* Its enclosing class is an explicit specialization
1503 of a template class. This is not a candidate. */
1504 continue;
1506 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1507 TREE_TYPE (TREE_TYPE (fn))))
1508 /* The return types differ. */
1509 continue;
1511 /* Adjust the type of DECL in case FN is a static member. */
1512 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1513 if (DECL_STATIC_FUNCTION_P (fn)
1514 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1515 decl_arg_types = TREE_CHAIN (decl_arg_types);
1517 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1518 decl_arg_types))
1519 /* They match! */
1520 candidates = tree_cons (NULL_TREE, fn, candidates);
1524 if (templates && TREE_CHAIN (templates))
1526 /* We have:
1528 [temp.expl.spec]
1530 It is possible for a specialization with a given function
1531 signature to be instantiated from more than one function
1532 template. In such cases, explicit specification of the
1533 template arguments must be used to uniquely identify the
1534 function template specialization being specialized.
1536 Note that here, there's no suggestion that we're supposed to
1537 determine which of the candidate templates is most
1538 specialized. However, we, also have:
1540 [temp.func.order]
1542 Partial ordering of overloaded function template
1543 declarations is used in the following contexts to select
1544 the function template to which a function template
1545 specialization refers:
1547 -- when an explicit specialization refers to a function
1548 template.
1550 So, we do use the partial ordering rules, at least for now.
1551 This extension can only serve to make invalid programs valid,
1552 so it's safe. And, there is strong anecdotal evidence that
1553 the committee intended the partial ordering rules to apply;
1554 the EDG front-end has that behavior, and John Spicer claims
1555 that the committee simply forgot to delete the wording in
1556 [temp.expl.spec]. */
1557 tree tmpl = most_specialized_instantiation (templates);
1558 if (tmpl != error_mark_node)
1560 templates = tmpl;
1561 TREE_CHAIN (templates) = NULL_TREE;
1565 if (templates == NULL_TREE && candidates == NULL_TREE)
1567 error ("template-id %qD for %q+D does not match any template "
1568 "declaration", template_id, decl);
1569 return error_mark_node;
1571 else if ((templates && TREE_CHAIN (templates))
1572 || (candidates && TREE_CHAIN (candidates))
1573 || (templates && candidates))
1575 error ("ambiguous template specialization %qD for %q+D",
1576 template_id, decl);
1577 chainon (candidates, templates);
1578 print_candidates (candidates);
1579 return error_mark_node;
1582 /* We have one, and exactly one, match. */
1583 if (candidates)
1585 /* It was a specialization of an ordinary member function in a
1586 template class. */
1587 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1588 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1591 /* It was a specialization of a template. */
1592 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1593 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1595 *targs_out = copy_node (targs);
1596 SET_TMPL_ARGS_LEVEL (*targs_out,
1597 TMPL_ARGS_DEPTH (*targs_out),
1598 TREE_PURPOSE (templates));
1600 else
1601 *targs_out = TREE_PURPOSE (templates);
1602 return TREE_VALUE (templates);
1605 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1606 but with the default argument values filled in from those in the
1607 TMPL_TYPES. */
1609 static tree
1610 copy_default_args_to_explicit_spec_1 (tree spec_types,
1611 tree tmpl_types)
1613 tree new_spec_types;
1615 if (!spec_types)
1616 return NULL_TREE;
1618 if (spec_types == void_list_node)
1619 return void_list_node;
1621 /* Substitute into the rest of the list. */
1622 new_spec_types =
1623 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1624 TREE_CHAIN (tmpl_types));
1626 /* Add the default argument for this parameter. */
1627 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1628 TREE_VALUE (spec_types),
1629 new_spec_types);
1632 /* DECL is an explicit specialization. Replicate default arguments
1633 from the template it specializes. (That way, code like:
1635 template <class T> void f(T = 3);
1636 template <> void f(double);
1637 void g () { f (); }
1639 works, as required.) An alternative approach would be to look up
1640 the correct default arguments at the call-site, but this approach
1641 is consistent with how implicit instantiations are handled. */
1643 static void
1644 copy_default_args_to_explicit_spec (tree decl)
1646 tree tmpl;
1647 tree spec_types;
1648 tree tmpl_types;
1649 tree new_spec_types;
1650 tree old_type;
1651 tree new_type;
1652 tree t;
1653 tree object_type = NULL_TREE;
1654 tree in_charge = NULL_TREE;
1655 tree vtt = NULL_TREE;
1657 /* See if there's anything we need to do. */
1658 tmpl = DECL_TI_TEMPLATE (decl);
1659 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1660 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1661 if (TREE_PURPOSE (t))
1662 break;
1663 if (!t)
1664 return;
1666 old_type = TREE_TYPE (decl);
1667 spec_types = TYPE_ARG_TYPES (old_type);
1669 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1671 /* Remove the this pointer, but remember the object's type for
1672 CV quals. */
1673 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1674 spec_types = TREE_CHAIN (spec_types);
1675 tmpl_types = TREE_CHAIN (tmpl_types);
1677 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1679 /* DECL may contain more parameters than TMPL due to the extra
1680 in-charge parameter in constructors and destructors. */
1681 in_charge = spec_types;
1682 spec_types = TREE_CHAIN (spec_types);
1684 if (DECL_HAS_VTT_PARM_P (decl))
1686 vtt = spec_types;
1687 spec_types = TREE_CHAIN (spec_types);
1691 /* Compute the merged default arguments. */
1692 new_spec_types =
1693 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1695 /* Compute the new FUNCTION_TYPE. */
1696 if (object_type)
1698 if (vtt)
1699 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1700 TREE_VALUE (vtt),
1701 new_spec_types);
1703 if (in_charge)
1704 /* Put the in-charge parameter back. */
1705 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1706 TREE_VALUE (in_charge),
1707 new_spec_types);
1709 new_type = build_method_type_directly (object_type,
1710 TREE_TYPE (old_type),
1711 new_spec_types);
1713 else
1714 new_type = build_function_type (TREE_TYPE (old_type),
1715 new_spec_types);
1716 new_type = cp_build_type_attribute_variant (new_type,
1717 TYPE_ATTRIBUTES (old_type));
1718 new_type = build_exception_variant (new_type,
1719 TYPE_RAISES_EXCEPTIONS (old_type));
1720 TREE_TYPE (decl) = new_type;
1723 /* Check to see if the function just declared, as indicated in
1724 DECLARATOR, and in DECL, is a specialization of a function
1725 template. We may also discover that the declaration is an explicit
1726 instantiation at this point.
1728 Returns DECL, or an equivalent declaration that should be used
1729 instead if all goes well. Issues an error message if something is
1730 amiss. Returns error_mark_node if the error is not easily
1731 recoverable.
1733 FLAGS is a bitmask consisting of the following flags:
1735 2: The function has a definition.
1736 4: The function is a friend.
1738 The TEMPLATE_COUNT is the number of references to qualifying
1739 template classes that appeared in the name of the function. For
1740 example, in
1742 template <class T> struct S { void f(); };
1743 void S<int>::f();
1745 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1746 classes are not counted in the TEMPLATE_COUNT, so that in
1748 template <class T> struct S {};
1749 template <> struct S<int> { void f(); }
1750 template <> void S<int>::f();
1752 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1753 invalid; there should be no template <>.)
1755 If the function is a specialization, it is marked as such via
1756 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1757 is set up correctly, and it is added to the list of specializations
1758 for that template. */
1760 tree
1761 check_explicit_specialization (tree declarator,
1762 tree decl,
1763 int template_count,
1764 int flags)
1766 int have_def = flags & 2;
1767 int is_friend = flags & 4;
1768 int specialization = 0;
1769 int explicit_instantiation = 0;
1770 int member_specialization = 0;
1771 tree ctype = DECL_CLASS_CONTEXT (decl);
1772 tree dname = DECL_NAME (decl);
1773 tmpl_spec_kind tsk;
1775 if (is_friend)
1777 if (!processing_specialization)
1778 tsk = tsk_none;
1779 else
1780 tsk = tsk_excessive_parms;
1782 else
1783 tsk = current_tmpl_spec_kind (template_count);
1785 switch (tsk)
1787 case tsk_none:
1788 if (processing_specialization)
1790 specialization = 1;
1791 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1793 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1795 if (is_friend)
1796 /* This could be something like:
1798 template <class T> void f(T);
1799 class S { friend void f<>(int); } */
1800 specialization = 1;
1801 else
1803 /* This case handles bogus declarations like template <>
1804 template <class T> void f<int>(); */
1806 error ("template-id %qD in declaration of primary template",
1807 declarator);
1808 return decl;
1811 break;
1813 case tsk_invalid_member_spec:
1814 /* The error has already been reported in
1815 check_specialization_scope. */
1816 return error_mark_node;
1818 case tsk_invalid_expl_inst:
1819 error ("template parameter list used in explicit instantiation");
1821 /* Fall through. */
1823 case tsk_expl_inst:
1824 if (have_def)
1825 error ("definition provided for explicit instantiation");
1827 explicit_instantiation = 1;
1828 break;
1830 case tsk_excessive_parms:
1831 case tsk_insufficient_parms:
1832 if (tsk == tsk_excessive_parms)
1833 error ("too many template parameter lists in declaration of %qD",
1834 decl);
1835 else if (template_header_count)
1836 error("too few template parameter lists in declaration of %qD", decl);
1837 else
1838 error("explicit specialization of %qD must be introduced by "
1839 "%<template <>%>", decl);
1841 /* Fall through. */
1842 case tsk_expl_spec:
1843 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1844 if (ctype)
1845 member_specialization = 1;
1846 else
1847 specialization = 1;
1848 break;
1850 case tsk_template:
1851 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1853 /* This case handles bogus declarations like template <>
1854 template <class T> void f<int>(); */
1856 if (uses_template_parms (declarator))
1857 error ("function template partial specialization %qD "
1858 "is not allowed", declarator);
1859 else
1860 error ("template-id %qD in declaration of primary template",
1861 declarator);
1862 return decl;
1865 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1866 /* This is a specialization of a member template, without
1867 specialization the containing class. Something like:
1869 template <class T> struct S {
1870 template <class U> void f (U);
1872 template <> template <class U> void S<int>::f(U) {}
1874 That's a specialization -- but of the entire template. */
1875 specialization = 1;
1876 break;
1878 default:
1879 gcc_unreachable ();
1882 if (specialization || member_specialization)
1884 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1885 for (; t; t = TREE_CHAIN (t))
1886 if (TREE_PURPOSE (t))
1888 pedwarn
1889 ("default argument specified in explicit specialization");
1890 break;
1894 if (specialization || member_specialization || explicit_instantiation)
1896 tree tmpl = NULL_TREE;
1897 tree targs = NULL_TREE;
1899 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1900 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1902 tree fns;
1904 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1905 if (ctype)
1906 fns = dname;
1907 else
1909 /* If there is no class context, the explicit instantiation
1910 must be at namespace scope. */
1911 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1913 /* Find the namespace binding, using the declaration
1914 context. */
1915 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1916 false, true);
1917 if (!fns || !is_overloaded_fn (fns))
1919 error ("%qD is not a template function", dname);
1920 fns = error_mark_node;
1922 else
1924 tree fn = OVL_CURRENT (fns);
1925 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1926 CP_DECL_CONTEXT (fn)))
1927 error ("%qD is not declared in %qD",
1928 decl, current_namespace);
1932 declarator = lookup_template_function (fns, NULL_TREE);
1935 if (declarator == error_mark_node)
1936 return error_mark_node;
1938 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1940 if (!explicit_instantiation)
1941 /* A specialization in class scope. This is invalid,
1942 but the error will already have been flagged by
1943 check_specialization_scope. */
1944 return error_mark_node;
1945 else
1947 /* It's not valid to write an explicit instantiation in
1948 class scope, e.g.:
1950 class C { template void f(); }
1952 This case is caught by the parser. However, on
1953 something like:
1955 template class C { void f(); };
1957 (which is invalid) we can get here. The error will be
1958 issued later. */
1962 return decl;
1964 else if (ctype != NULL_TREE
1965 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1966 IDENTIFIER_NODE))
1968 /* Find the list of functions in ctype that have the same
1969 name as the declared function. */
1970 tree name = TREE_OPERAND (declarator, 0);
1971 tree fns = NULL_TREE;
1972 int idx;
1974 if (constructor_name_p (name, ctype))
1976 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1978 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1979 : !CLASSTYPE_DESTRUCTORS (ctype))
1981 /* From [temp.expl.spec]:
1983 If such an explicit specialization for the member
1984 of a class template names an implicitly-declared
1985 special member function (clause _special_), the
1986 program is ill-formed.
1988 Similar language is found in [temp.explicit]. */
1989 error ("specialization of implicitly-declared special member function");
1990 return error_mark_node;
1993 name = is_constructor ? ctor_identifier : dtor_identifier;
1996 if (!DECL_CONV_FN_P (decl))
1998 idx = lookup_fnfields_1 (ctype, name);
1999 if (idx >= 0)
2000 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2002 else
2004 VEC(tree,gc) *methods;
2005 tree ovl;
2007 /* For a type-conversion operator, we cannot do a
2008 name-based lookup. We might be looking for `operator
2009 int' which will be a specialization of `operator T'.
2010 So, we find *all* the conversion operators, and then
2011 select from them. */
2012 fns = NULL_TREE;
2014 methods = CLASSTYPE_METHOD_VEC (ctype);
2015 if (methods)
2016 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2017 VEC_iterate (tree, methods, idx, ovl);
2018 ++idx)
2020 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2021 /* There are no more conversion functions. */
2022 break;
2024 /* Glue all these conversion functions together
2025 with those we already have. */
2026 for (; ovl; ovl = OVL_NEXT (ovl))
2027 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2031 if (fns == NULL_TREE)
2033 error ("no member function %qD declared in %qT", name, ctype);
2034 return error_mark_node;
2036 else
2037 TREE_OPERAND (declarator, 0) = fns;
2040 /* Figure out what exactly is being specialized at this point.
2041 Note that for an explicit instantiation, even one for a
2042 member function, we cannot tell apriori whether the
2043 instantiation is for a member template, or just a member
2044 function of a template class. Even if a member template is
2045 being instantiated, the member template arguments may be
2046 elided if they can be deduced from the rest of the
2047 declaration. */
2048 tmpl = determine_specialization (declarator, decl,
2049 &targs,
2050 member_specialization,
2051 template_count);
2053 if (!tmpl || tmpl == error_mark_node)
2054 /* We couldn't figure out what this declaration was
2055 specializing. */
2056 return error_mark_node;
2057 else
2059 tree gen_tmpl = most_general_template (tmpl);
2061 if (explicit_instantiation)
2063 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2064 is done by do_decl_instantiation later. */
2066 int arg_depth = TMPL_ARGS_DEPTH (targs);
2067 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2069 if (arg_depth > parm_depth)
2071 /* If TMPL is not the most general template (for
2072 example, if TMPL is a friend template that is
2073 injected into namespace scope), then there will
2074 be too many levels of TARGS. Remove some of them
2075 here. */
2076 int i;
2077 tree new_targs;
2079 new_targs = make_tree_vec (parm_depth);
2080 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2081 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2082 = TREE_VEC_ELT (targs, i);
2083 targs = new_targs;
2086 return instantiate_template (tmpl, targs, tf_error);
2089 /* If we thought that the DECL was a member function, but it
2090 turns out to be specializing a static member function,
2091 make DECL a static member function as well. */
2092 if (DECL_STATIC_FUNCTION_P (tmpl)
2093 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2094 revert_static_member_fn (decl);
2096 /* If this is a specialization of a member template of a
2097 template class. In we want to return the TEMPLATE_DECL,
2098 not the specialization of it. */
2099 if (tsk == tsk_template)
2101 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2102 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2103 if (have_def)
2105 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2106 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2107 = DECL_SOURCE_LOCATION (decl);
2108 /* We want to use the argument list specified in the
2109 definition, not in the original declaration. */
2110 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2111 = DECL_ARGUMENTS (decl);
2113 return tmpl;
2116 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2117 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2119 /* Inherit default function arguments from the template
2120 DECL is specializing. */
2121 copy_default_args_to_explicit_spec (decl);
2123 /* This specialization has the same protection as the
2124 template it specializes. */
2125 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2126 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2127 /* The specialization has the same visibility as the
2128 template it specializes. */
2129 if (DECL_VISIBILITY_SPECIFIED (gen_tmpl))
2131 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2132 DECL_VISIBILITY (decl) = DECL_VISIBILITY (gen_tmpl);
2134 /* If DECL is a friend declaration, declared using an
2135 unqualified name, the namespace associated with DECL may
2136 have been set incorrectly. For example, in:
2138 template <typename T> void f(T);
2139 namespace N {
2140 struct S { friend void f<int>(int); }
2143 we will have set the DECL_CONTEXT for the friend
2144 declaration to N, rather than to the global namespace. */
2145 if (DECL_NAMESPACE_SCOPE_P (decl))
2146 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2148 if (is_friend && !have_def)
2149 /* This is not really a declaration of a specialization.
2150 It's just the name of an instantiation. But, it's not
2151 a request for an instantiation, either. */
2152 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2153 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2154 /* This is indeed a specialization. In case of constructors
2155 and destructors, we need in-charge and not-in-charge
2156 versions in V3 ABI. */
2157 clone_function_decl (decl, /*update_method_vec_p=*/0);
2159 /* Register this specialization so that we can find it
2160 again. */
2161 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2165 return decl;
2168 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2169 parameters. These are represented in the same format used for
2170 DECL_TEMPLATE_PARMS. */
2173 comp_template_parms (tree parms1, tree parms2)
2175 tree p1;
2176 tree p2;
2178 if (parms1 == parms2)
2179 return 1;
2181 for (p1 = parms1, p2 = parms2;
2182 p1 != NULL_TREE && p2 != NULL_TREE;
2183 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2185 tree t1 = TREE_VALUE (p1);
2186 tree t2 = TREE_VALUE (p2);
2187 int i;
2189 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2190 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2192 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2193 return 0;
2195 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2197 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2198 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2200 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2201 return 0;
2203 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2204 continue;
2205 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2206 return 0;
2210 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2211 /* One set of parameters has more parameters lists than the
2212 other. */
2213 return 0;
2215 return 1;
2218 /* Complain if DECL shadows a template parameter.
2220 [temp.local]: A template-parameter shall not be redeclared within its
2221 scope (including nested scopes). */
2223 void
2224 check_template_shadow (tree decl)
2226 tree olddecl;
2228 /* If we're not in a template, we can't possibly shadow a template
2229 parameter. */
2230 if (!current_template_parms)
2231 return;
2233 /* Figure out what we're shadowing. */
2234 if (TREE_CODE (decl) == OVERLOAD)
2235 decl = OVL_CURRENT (decl);
2236 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2238 /* If there's no previous binding for this name, we're not shadowing
2239 anything, let alone a template parameter. */
2240 if (!olddecl)
2241 return;
2243 /* If we're not shadowing a template parameter, we're done. Note
2244 that OLDDECL might be an OVERLOAD (or perhaps even an
2245 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2246 node. */
2247 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2248 return;
2250 /* We check for decl != olddecl to avoid bogus errors for using a
2251 name inside a class. We check TPFI to avoid duplicate errors for
2252 inline member templates. */
2253 if (decl == olddecl
2254 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2255 return;
2257 error ("declaration of %q+#D", decl);
2258 error (" shadows template parm %q+#D", olddecl);
2261 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2262 ORIG_LEVEL, DECL, and TYPE. */
2264 static tree
2265 build_template_parm_index (int index,
2266 int level,
2267 int orig_level,
2268 tree decl,
2269 tree type)
2271 tree t = make_node (TEMPLATE_PARM_INDEX);
2272 TEMPLATE_PARM_IDX (t) = index;
2273 TEMPLATE_PARM_LEVEL (t) = level;
2274 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2275 TEMPLATE_PARM_DECL (t) = decl;
2276 TREE_TYPE (t) = type;
2277 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2278 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2279 TREE_READONLY (t) = TREE_READONLY (decl);
2281 return t;
2284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2287 new one is created. */
2289 static tree
2290 reduce_template_parm_level (tree index, tree type, int levels)
2292 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2293 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2294 != TEMPLATE_PARM_LEVEL (index) - levels))
2296 tree orig_decl = TEMPLATE_PARM_DECL (index);
2297 tree decl, t;
2299 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2300 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2301 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2302 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2303 DECL_ARTIFICIAL (decl) = 1;
2304 SET_DECL_TEMPLATE_PARM_P (decl);
2306 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2307 TEMPLATE_PARM_LEVEL (index) - levels,
2308 TEMPLATE_PARM_ORIG_LEVEL (index),
2309 decl, type);
2310 TEMPLATE_PARM_DESCENDANTS (index) = t;
2312 /* Template template parameters need this. */
2313 if (TREE_CODE (decl) != CONST_DECL)
2314 DECL_TEMPLATE_PARMS (decl)
2315 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2318 return TEMPLATE_PARM_DESCENDANTS (index);
2321 /* Process information from new template parameter NEXT and append it to the
2322 LIST being built. This new parameter is a non-type parameter iff
2323 IS_NON_TYPE is true. */
2325 tree
2326 process_template_parm (tree list, tree next, bool is_non_type)
2328 tree parm;
2329 tree decl = 0;
2330 tree defval;
2331 int idx;
2333 parm = next;
2334 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2335 defval = TREE_PURPOSE (parm);
2337 if (list)
2339 tree p = TREE_VALUE (tree_last (list));
2341 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2342 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2343 else
2344 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2345 ++idx;
2347 else
2348 idx = 0;
2350 if (is_non_type)
2352 parm = TREE_VALUE (parm);
2354 SET_DECL_TEMPLATE_PARM_P (parm);
2356 /* [temp.param]
2358 The top-level cv-qualifiers on the template-parameter are
2359 ignored when determining its type. */
2360 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2362 /* A template parameter is not modifiable. */
2363 TREE_CONSTANT (parm) = 1;
2364 TREE_INVARIANT (parm) = 1;
2365 TREE_READONLY (parm) = 1;
2366 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2367 TREE_TYPE (parm) = void_type_node;
2368 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2369 TREE_CONSTANT (decl) = 1;
2370 TREE_INVARIANT (decl) = 1;
2371 TREE_READONLY (decl) = 1;
2372 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2373 = build_template_parm_index (idx, processing_template_decl,
2374 processing_template_decl,
2375 decl, TREE_TYPE (parm));
2377 else
2379 tree t;
2380 parm = TREE_VALUE (TREE_VALUE (parm));
2382 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2384 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2385 /* This is for distinguishing between real templates and template
2386 template parameters */
2387 TREE_TYPE (parm) = t;
2388 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2389 decl = parm;
2391 else
2393 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2394 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2395 decl = build_decl (TYPE_DECL, parm, t);
2398 TYPE_NAME (t) = decl;
2399 TYPE_STUB_DECL (t) = decl;
2400 parm = decl;
2401 TEMPLATE_TYPE_PARM_INDEX (t)
2402 = build_template_parm_index (idx, processing_template_decl,
2403 processing_template_decl,
2404 decl, TREE_TYPE (parm));
2406 DECL_ARTIFICIAL (decl) = 1;
2407 SET_DECL_TEMPLATE_PARM_P (decl);
2408 pushdecl (decl);
2409 parm = build_tree_list (defval, parm);
2410 return chainon (list, parm);
2413 /* The end of a template parameter list has been reached. Process the
2414 tree list into a parameter vector, converting each parameter into a more
2415 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2416 as PARM_DECLs. */
2418 tree
2419 end_template_parm_list (tree parms)
2421 int nparms;
2422 tree parm, next;
2423 tree saved_parmlist = make_tree_vec (list_length (parms));
2425 current_template_parms
2426 = tree_cons (size_int (processing_template_decl),
2427 saved_parmlist, current_template_parms);
2429 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2431 next = TREE_CHAIN (parm);
2432 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2433 TREE_CHAIN (parm) = NULL_TREE;
2436 --processing_template_parmlist;
2438 return saved_parmlist;
2441 /* end_template_decl is called after a template declaration is seen. */
2443 void
2444 end_template_decl (void)
2446 reset_specialization ();
2448 if (! processing_template_decl)
2449 return;
2451 /* This matches the pushlevel in begin_template_parm_list. */
2452 finish_scope ();
2454 --processing_template_decl;
2455 current_template_parms = TREE_CHAIN (current_template_parms);
2458 /* Given a template argument vector containing the template PARMS.
2459 The innermost PARMS are given first. */
2461 static tree
2462 current_template_args (void)
2464 tree header;
2465 tree args = NULL_TREE;
2466 int length = TMPL_PARMS_DEPTH (current_template_parms);
2467 int l = length;
2469 /* If there is only one level of template parameters, we do not
2470 create a TREE_VEC of TREE_VECs. Instead, we return a single
2471 TREE_VEC containing the arguments. */
2472 if (length > 1)
2473 args = make_tree_vec (length);
2475 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2477 tree a = copy_node (TREE_VALUE (header));
2478 int i;
2480 TREE_TYPE (a) = NULL_TREE;
2481 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2483 tree t = TREE_VEC_ELT (a, i);
2485 /* T will be a list if we are called from within a
2486 begin/end_template_parm_list pair, but a vector directly
2487 if within a begin/end_member_template_processing pair. */
2488 if (TREE_CODE (t) == TREE_LIST)
2490 t = TREE_VALUE (t);
2492 if (TREE_CODE (t) == TYPE_DECL
2493 || TREE_CODE (t) == TEMPLATE_DECL)
2494 t = TREE_TYPE (t);
2495 else
2496 t = DECL_INITIAL (t);
2497 TREE_VEC_ELT (a, i) = t;
2501 if (length > 1)
2502 TREE_VEC_ELT (args, --l) = a;
2503 else
2504 args = a;
2507 return args;
2510 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2511 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2512 a member template. Used by push_template_decl below. */
2514 static tree
2515 build_template_decl (tree decl, tree parms, bool member_template_p)
2517 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2518 DECL_TEMPLATE_PARMS (tmpl) = parms;
2519 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2520 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2521 if (DECL_LANG_SPECIFIC (decl))
2523 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2524 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2525 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2526 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2527 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2528 if (DECL_OVERLOADED_OPERATOR_P (decl))
2529 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2530 DECL_OVERLOADED_OPERATOR_P (decl));
2533 return tmpl;
2536 struct template_parm_data
2538 /* The level of the template parameters we are currently
2539 processing. */
2540 int level;
2542 /* The index of the specialization argument we are currently
2543 processing. */
2544 int current_arg;
2546 /* An array whose size is the number of template parameters. The
2547 elements are nonzero if the parameter has been used in any one
2548 of the arguments processed so far. */
2549 int* parms;
2551 /* An array whose size is the number of template arguments. The
2552 elements are nonzero if the argument makes use of template
2553 parameters of this level. */
2554 int* arg_uses_template_parms;
2557 /* Subroutine of push_template_decl used to see if each template
2558 parameter in a partial specialization is used in the explicit
2559 argument list. If T is of the LEVEL given in DATA (which is
2560 treated as a template_parm_data*), then DATA->PARMS is marked
2561 appropriately. */
2563 static int
2564 mark_template_parm (tree t, void* data)
2566 int level;
2567 int idx;
2568 struct template_parm_data* tpd = (struct template_parm_data*) data;
2570 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2572 level = TEMPLATE_PARM_LEVEL (t);
2573 idx = TEMPLATE_PARM_IDX (t);
2575 else
2577 level = TEMPLATE_TYPE_LEVEL (t);
2578 idx = TEMPLATE_TYPE_IDX (t);
2581 if (level == tpd->level)
2583 tpd->parms[idx] = 1;
2584 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2587 /* Return zero so that for_each_template_parm will continue the
2588 traversal of the tree; we want to mark *every* template parm. */
2589 return 0;
2592 /* Process the partial specialization DECL. */
2594 static tree
2595 process_partial_specialization (tree decl)
2597 tree type = TREE_TYPE (decl);
2598 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2599 tree specargs = CLASSTYPE_TI_ARGS (type);
2600 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2601 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2602 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2603 int nargs = TREE_VEC_LENGTH (inner_args);
2604 int ntparms = TREE_VEC_LENGTH (inner_parms);
2605 int i;
2606 int did_error_intro = 0;
2607 struct template_parm_data tpd;
2608 struct template_parm_data tpd2;
2610 /* We check that each of the template parameters given in the
2611 partial specialization is used in the argument list to the
2612 specialization. For example:
2614 template <class T> struct S;
2615 template <class T> struct S<T*>;
2617 The second declaration is OK because `T*' uses the template
2618 parameter T, whereas
2620 template <class T> struct S<int>;
2622 is no good. Even trickier is:
2624 template <class T>
2625 struct S1
2627 template <class U>
2628 struct S2;
2629 template <class U>
2630 struct S2<T>;
2633 The S2<T> declaration is actually invalid; it is a
2634 full-specialization. Of course,
2636 template <class U>
2637 struct S2<T (*)(U)>;
2639 or some such would have been OK. */
2640 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2641 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2642 memset (tpd.parms, 0, sizeof (int) * ntparms);
2644 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2645 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2646 for (i = 0; i < nargs; ++i)
2648 tpd.current_arg = i;
2649 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2650 &mark_template_parm,
2651 &tpd,
2652 NULL);
2654 for (i = 0; i < ntparms; ++i)
2655 if (tpd.parms[i] == 0)
2657 /* One of the template parms was not used in the
2658 specialization. */
2659 if (!did_error_intro)
2661 error ("template parameters not used in partial specialization:");
2662 did_error_intro = 1;
2665 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2668 /* [temp.class.spec]
2670 The argument list of the specialization shall not be identical to
2671 the implicit argument list of the primary template. */
2672 if (comp_template_args
2673 (inner_args,
2674 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2675 (maintmpl)))))
2676 error ("partial specialization %qT does not specialize any template arguments", type);
2678 /* [temp.class.spec]
2680 A partially specialized non-type argument expression shall not
2681 involve template parameters of the partial specialization except
2682 when the argument expression is a simple identifier.
2684 The type of a template parameter corresponding to a specialized
2685 non-type argument shall not be dependent on a parameter of the
2686 specialization. */
2687 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2688 tpd2.parms = 0;
2689 for (i = 0; i < nargs; ++i)
2691 tree arg = TREE_VEC_ELT (inner_args, i);
2692 if (/* These first two lines are the `non-type' bit. */
2693 !TYPE_P (arg)
2694 && TREE_CODE (arg) != TEMPLATE_DECL
2695 /* This next line is the `argument expression is not just a
2696 simple identifier' condition and also the `specialized
2697 non-type argument' bit. */
2698 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2700 if (tpd.arg_uses_template_parms[i])
2701 error ("template argument %qE involves template parameter(s)", arg);
2702 else
2704 /* Look at the corresponding template parameter,
2705 marking which template parameters its type depends
2706 upon. */
2707 tree type =
2708 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2709 i)));
2711 if (!tpd2.parms)
2713 /* We haven't yet initialized TPD2. Do so now. */
2714 tpd2.arg_uses_template_parms
2715 = (int *) alloca (sizeof (int) * nargs);
2716 /* The number of parameters here is the number in the
2717 main template, which, as checked in the assertion
2718 above, is NARGS. */
2719 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2720 tpd2.level =
2721 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2724 /* Mark the template parameters. But this time, we're
2725 looking for the template parameters of the main
2726 template, not in the specialization. */
2727 tpd2.current_arg = i;
2728 tpd2.arg_uses_template_parms[i] = 0;
2729 memset (tpd2.parms, 0, sizeof (int) * nargs);
2730 for_each_template_parm (type,
2731 &mark_template_parm,
2732 &tpd2,
2733 NULL);
2735 if (tpd2.arg_uses_template_parms [i])
2737 /* The type depended on some template parameters.
2738 If they are fully specialized in the
2739 specialization, that's OK. */
2740 int j;
2741 for (j = 0; j < nargs; ++j)
2742 if (tpd2.parms[j] != 0
2743 && tpd.arg_uses_template_parms [j])
2745 error ("type %qT of template argument %qE depends "
2746 "on template parameter(s)",
2747 type,
2748 arg);
2749 break;
2756 if (retrieve_specialization (maintmpl, specargs,
2757 /*class_specializations_p=*/true))
2758 /* We've already got this specialization. */
2759 return decl;
2761 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2762 = tree_cons (specargs, inner_parms,
2763 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2764 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2765 return decl;
2768 /* Check that a template declaration's use of default arguments is not
2769 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2770 nonzero if DECL is the thing declared by a primary template.
2771 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2773 static void
2774 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2776 const char *msg;
2777 int last_level_to_check;
2778 tree parm_level;
2780 /* [temp.param]
2782 A default template-argument shall not be specified in a
2783 function template declaration or a function template definition, nor
2784 in the template-parameter-list of the definition of a member of a
2785 class template. */
2787 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2788 /* You can't have a function template declaration in a local
2789 scope, nor you can you define a member of a class template in a
2790 local scope. */
2791 return;
2793 if (current_class_type
2794 && !TYPE_BEING_DEFINED (current_class_type)
2795 && DECL_LANG_SPECIFIC (decl)
2796 /* If this is either a friend defined in the scope of the class
2797 or a member function. */
2798 && (DECL_FUNCTION_MEMBER_P (decl)
2799 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2800 : DECL_FRIEND_CONTEXT (decl)
2801 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2802 : false)
2803 /* And, if it was a member function, it really was defined in
2804 the scope of the class. */
2805 && (!DECL_FUNCTION_MEMBER_P (decl)
2806 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2807 /* We already checked these parameters when the template was
2808 declared, so there's no need to do it again now. This function
2809 was defined in class scope, but we're processing it's body now
2810 that the class is complete. */
2811 return;
2813 /* [temp.param]
2815 If a template-parameter has a default template-argument, all
2816 subsequent template-parameters shall have a default
2817 template-argument supplied. */
2818 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2820 tree inner_parms = TREE_VALUE (parm_level);
2821 int ntparms = TREE_VEC_LENGTH (inner_parms);
2822 int seen_def_arg_p = 0;
2823 int i;
2825 for (i = 0; i < ntparms; ++i)
2827 tree parm = TREE_VEC_ELT (inner_parms, i);
2828 if (TREE_PURPOSE (parm))
2829 seen_def_arg_p = 1;
2830 else if (seen_def_arg_p)
2832 error ("no default argument for %qD", TREE_VALUE (parm));
2833 /* For better subsequent error-recovery, we indicate that
2834 there should have been a default argument. */
2835 TREE_PURPOSE (parm) = error_mark_node;
2840 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2841 /* For an ordinary class template, default template arguments are
2842 allowed at the innermost level, e.g.:
2843 template <class T = int>
2844 struct S {};
2845 but, in a partial specialization, they're not allowed even
2846 there, as we have in [temp.class.spec]:
2848 The template parameter list of a specialization shall not
2849 contain default template argument values.
2851 So, for a partial specialization, or for a function template,
2852 we look at all of them. */
2854 else
2855 /* But, for a primary class template that is not a partial
2856 specialization we look at all template parameters except the
2857 innermost ones. */
2858 parms = TREE_CHAIN (parms);
2860 /* Figure out what error message to issue. */
2861 if (TREE_CODE (decl) == FUNCTION_DECL)
2862 msg = "default template arguments may not be used in function templates";
2863 else if (is_partial)
2864 msg = "default template arguments may not be used in partial specializations";
2865 else
2866 msg = "default argument for template parameter for class enclosing %qD";
2868 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2869 /* If we're inside a class definition, there's no need to
2870 examine the parameters to the class itself. On the one
2871 hand, they will be checked when the class is defined, and,
2872 on the other, default arguments are valid in things like:
2873 template <class T = double>
2874 struct S { template <class U> void f(U); };
2875 Here the default argument for `S' has no bearing on the
2876 declaration of `f'. */
2877 last_level_to_check = template_class_depth (current_class_type) + 1;
2878 else
2879 /* Check everything. */
2880 last_level_to_check = 0;
2882 for (parm_level = parms;
2883 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2884 parm_level = TREE_CHAIN (parm_level))
2886 tree inner_parms = TREE_VALUE (parm_level);
2887 int i;
2888 int ntparms;
2890 ntparms = TREE_VEC_LENGTH (inner_parms);
2891 for (i = 0; i < ntparms; ++i)
2892 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2894 if (msg)
2896 error (msg, decl);
2897 msg = 0;
2900 /* Clear out the default argument so that we are not
2901 confused later. */
2902 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2905 /* At this point, if we're still interested in issuing messages,
2906 they must apply to classes surrounding the object declared. */
2907 if (msg)
2908 msg = "default argument for template parameter for class enclosing %qD";
2912 /* Worker for push_template_decl_real, called via
2913 for_each_template_parm. DATA is really an int, indicating the
2914 level of the parameters we are interested in. If T is a template
2915 parameter of that level, return nonzero. */
2917 static int
2918 template_parm_this_level_p (tree t, void* data)
2920 int this_level = *(int *)data;
2921 int level;
2923 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2924 level = TEMPLATE_PARM_LEVEL (t);
2925 else
2926 level = TEMPLATE_TYPE_LEVEL (t);
2927 return level == this_level;
2930 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2931 parameters given by current_template_args, or reuses a
2932 previously existing one, if appropriate. Returns the DECL, or an
2933 equivalent one, if it is replaced via a call to duplicate_decls.
2935 If IS_FRIEND is true, DECL is a friend declaration. */
2937 tree
2938 push_template_decl_real (tree decl, bool is_friend)
2940 tree tmpl;
2941 tree args;
2942 tree info;
2943 tree ctx;
2944 int primary;
2945 int is_partial;
2946 int new_template_p = 0;
2947 /* True if the template is a member template, in the sense of
2948 [temp.mem]. */
2949 bool member_template_p = false;
2951 if (decl == error_mark_node)
2952 return decl;
2954 /* See if this is a partial specialization. */
2955 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2956 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2957 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2959 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
2960 is_friend = true;
2962 if (is_friend)
2963 /* For a friend, we want the context of the friend function, not
2964 the type of which it is a friend. */
2965 ctx = DECL_CONTEXT (decl);
2966 else if (CP_DECL_CONTEXT (decl)
2967 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2968 /* In the case of a virtual function, we want the class in which
2969 it is defined. */
2970 ctx = CP_DECL_CONTEXT (decl);
2971 else
2972 /* Otherwise, if we're currently defining some class, the DECL
2973 is assumed to be a member of the class. */
2974 ctx = current_scope ();
2976 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2977 ctx = NULL_TREE;
2979 if (!DECL_CONTEXT (decl))
2980 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2982 /* See if this is a primary template. */
2983 primary = template_parm_scope_p ();
2985 if (primary)
2987 if (DECL_CLASS_SCOPE_P (decl))
2988 member_template_p = true;
2989 if (TREE_CODE (decl) == TYPE_DECL
2990 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2991 error ("template class without a name");
2992 else if (TREE_CODE (decl) == FUNCTION_DECL)
2994 if (DECL_DESTRUCTOR_P (decl))
2996 /* [temp.mem]
2998 A destructor shall not be a member template. */
2999 error ("destructor %qD declared as member template", decl);
3000 return error_mark_node;
3002 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3003 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3004 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3005 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3006 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3007 == void_list_node)))
3009 /* [basic.stc.dynamic.allocation]
3011 An allocation function can be a function
3012 template. ... Template allocation functions shall
3013 have two or more parameters. */
3014 error ("invalid template declaration of %qD", decl);
3015 return decl;
3018 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3019 && CLASS_TYPE_P (TREE_TYPE (decl)))
3020 /* OK */;
3021 else
3023 error ("template declaration of %q#D", decl);
3024 return error_mark_node;
3028 /* Check to see that the rules regarding the use of default
3029 arguments are not being violated. */
3030 check_default_tmpl_args (decl, current_template_parms,
3031 primary, is_partial);
3033 if (is_partial)
3034 return process_partial_specialization (decl);
3036 args = current_template_args ();
3038 if (!ctx
3039 || TREE_CODE (ctx) == FUNCTION_DECL
3040 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3041 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3043 if (DECL_LANG_SPECIFIC (decl)
3044 && DECL_TEMPLATE_INFO (decl)
3045 && DECL_TI_TEMPLATE (decl))
3046 tmpl = DECL_TI_TEMPLATE (decl);
3047 /* If DECL is a TYPE_DECL for a class-template, then there won't
3048 be DECL_LANG_SPECIFIC. The information equivalent to
3049 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3050 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3051 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3052 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3054 /* Since a template declaration already existed for this
3055 class-type, we must be redeclaring it here. Make sure
3056 that the redeclaration is valid. */
3057 redeclare_class_template (TREE_TYPE (decl),
3058 current_template_parms);
3059 /* We don't need to create a new TEMPLATE_DECL; just use the
3060 one we already had. */
3061 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3063 else
3065 tmpl = build_template_decl (decl, current_template_parms,
3066 member_template_p);
3067 new_template_p = 1;
3069 if (DECL_LANG_SPECIFIC (decl)
3070 && DECL_TEMPLATE_SPECIALIZATION (decl))
3072 /* A specialization of a member template of a template
3073 class. */
3074 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3075 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3076 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3080 else
3082 tree a, t, current, parms;
3083 int i;
3085 if (TREE_CODE (decl) == TYPE_DECL)
3087 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3088 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3089 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3090 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3091 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3092 else
3094 error ("%qD does not declare a template type", decl);
3095 return decl;
3098 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3100 error ("template definition of non-template %q#D", decl);
3101 return decl;
3103 else
3104 tmpl = DECL_TI_TEMPLATE (decl);
3106 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3107 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3108 && DECL_TEMPLATE_SPECIALIZATION (decl)
3109 && DECL_MEMBER_TEMPLATE_P (tmpl))
3111 tree new_tmpl;
3113 /* The declaration is a specialization of a member
3114 template, declared outside the class. Therefore, the
3115 innermost template arguments will be NULL, so we
3116 replace them with the arguments determined by the
3117 earlier call to check_explicit_specialization. */
3118 args = DECL_TI_ARGS (decl);
3120 new_tmpl
3121 = build_template_decl (decl, current_template_parms,
3122 member_template_p);
3123 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3124 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3125 DECL_TI_TEMPLATE (decl) = new_tmpl;
3126 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3127 DECL_TEMPLATE_INFO (new_tmpl)
3128 = tree_cons (tmpl, args, NULL_TREE);
3130 register_specialization (new_tmpl,
3131 most_general_template (tmpl),
3132 args,
3133 is_friend);
3134 return decl;
3137 /* Make sure the template headers we got make sense. */
3139 parms = DECL_TEMPLATE_PARMS (tmpl);
3140 i = TMPL_PARMS_DEPTH (parms);
3141 if (TMPL_ARGS_DEPTH (args) != i)
3143 error ("expected %d levels of template parms for %q#D, got %d",
3144 i, decl, TMPL_ARGS_DEPTH (args));
3146 else
3147 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3149 a = TMPL_ARGS_LEVEL (args, i);
3150 t = INNERMOST_TEMPLATE_PARMS (parms);
3152 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3154 if (current == decl)
3155 error ("got %d template parameters for %q#D",
3156 TREE_VEC_LENGTH (a), decl);
3157 else
3158 error ("got %d template parameters for %q#T",
3159 TREE_VEC_LENGTH (a), current);
3160 error (" but %d required", TREE_VEC_LENGTH (t));
3161 return error_mark_node;
3164 /* Perhaps we should also check that the parms are used in the
3165 appropriate qualifying scopes in the declarator? */
3167 if (current == decl)
3168 current = ctx;
3169 else
3170 current = TYPE_CONTEXT (current);
3174 DECL_TEMPLATE_RESULT (tmpl) = decl;
3175 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3177 /* Push template declarations for global functions and types. Note
3178 that we do not try to push a global template friend declared in a
3179 template class; such a thing may well depend on the template
3180 parameters of the class. */
3181 if (new_template_p && !ctx
3182 && !(is_friend && template_class_depth (current_class_type) > 0))
3184 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3185 if (tmpl == error_mark_node)
3186 return error_mark_node;
3188 /* Hide template friend classes that haven't been declared yet. */
3189 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3191 DECL_ANTICIPATED (tmpl) = 1;
3192 DECL_FRIEND_P (tmpl) = 1;
3196 if (primary)
3198 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3199 if (DECL_CONV_FN_P (tmpl))
3201 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3203 /* It is a conversion operator. See if the type converted to
3204 depends on innermost template operands. */
3206 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3207 depth))
3208 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3212 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3213 back to its most general template. If TMPL is a specialization,
3214 ARGS may only have the innermost set of arguments. Add the missing
3215 argument levels if necessary. */
3216 if (DECL_TEMPLATE_INFO (tmpl))
3217 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3219 info = tree_cons (tmpl, args, NULL_TREE);
3221 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3223 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3224 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3225 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3226 /* Don't change the name if we've already set it up. */
3227 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3228 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3230 else if (DECL_LANG_SPECIFIC (decl))
3231 DECL_TEMPLATE_INFO (decl) = info;
3233 return DECL_TEMPLATE_RESULT (tmpl);
3236 tree
3237 push_template_decl (tree decl)
3239 return push_template_decl_real (decl, false);
3242 /* Called when a class template TYPE is redeclared with the indicated
3243 template PARMS, e.g.:
3245 template <class T> struct S;
3246 template <class T> struct S {}; */
3248 void
3249 redeclare_class_template (tree type, tree parms)
3251 tree tmpl;
3252 tree tmpl_parms;
3253 int i;
3255 if (!TYPE_TEMPLATE_INFO (type))
3257 error ("%qT is not a template type", type);
3258 return;
3261 tmpl = TYPE_TI_TEMPLATE (type);
3262 if (!PRIMARY_TEMPLATE_P (tmpl))
3263 /* The type is nested in some template class. Nothing to worry
3264 about here; there are no new template parameters for the nested
3265 type. */
3266 return;
3268 if (!parms)
3270 error ("template specifiers not specified in declaration of %qD",
3271 tmpl);
3272 return;
3275 parms = INNERMOST_TEMPLATE_PARMS (parms);
3276 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3278 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3280 error ("previous declaration %q+D", tmpl);
3281 error ("used %d template parameter(s) instead of %d",
3282 TREE_VEC_LENGTH (tmpl_parms),
3283 TREE_VEC_LENGTH (parms));
3284 return;
3287 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3289 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3290 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3291 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3292 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3294 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3295 TEMPLATE_DECL. */
3296 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3297 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3298 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3300 error ("template parameter %q+#D", tmpl_parm);
3301 error ("redeclared here as %q#D", parm);
3302 return;
3305 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3307 /* We have in [temp.param]:
3309 A template-parameter may not be given default arguments
3310 by two different declarations in the same scope. */
3311 error ("redefinition of default argument for %q#D", parm);
3312 error ("%J original definition appeared here", tmpl_parm);
3313 return;
3316 if (parm_default != NULL_TREE)
3317 /* Update the previous template parameters (which are the ones
3318 that will really count) with the new default value. */
3319 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3320 else if (tmpl_default != NULL_TREE)
3321 /* Update the new parameters, too; they'll be used as the
3322 parameters for any members. */
3323 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3327 /* Simplify EXPR if it is a non-dependent expression. Returns the
3328 (possibly simplified) expression. */
3330 tree
3331 fold_non_dependent_expr (tree expr)
3333 /* If we're in a template, but EXPR isn't value dependent, simplify
3334 it. We're supposed to treat:
3336 template <typename T> void f(T[1 + 1]);
3337 template <typename T> void f(T[2]);
3339 as two declarations of the same function, for example. */
3340 if (processing_template_decl
3341 && !type_dependent_expression_p (expr)
3342 && !value_dependent_expression_p (expr))
3344 HOST_WIDE_INT saved_processing_template_decl;
3346 saved_processing_template_decl = processing_template_decl;
3347 processing_template_decl = 0;
3348 expr = tsubst_copy_and_build (expr,
3349 /*args=*/NULL_TREE,
3350 tf_error,
3351 /*in_decl=*/NULL_TREE,
3352 /*function_p=*/false);
3353 processing_template_decl = saved_processing_template_decl;
3355 return expr;
3358 /* EXPR is an expression which is used in a constant-expression context.
3359 For instance, it could be a VAR_DECL with a constant initializer.
3360 Extract the innest constant expression.
3362 This is basically a more powerful version of
3363 integral_constant_value, which can be used also in templates where
3364 initializers can maintain a syntactic rather than semantic form
3365 (even if they are non-dependent, for access-checking purposes). */
3367 static tree
3368 fold_decl_constant_value (tree expr)
3370 tree const_expr = expr;
3373 expr = fold_non_dependent_expr (const_expr);
3374 const_expr = integral_constant_value (expr);
3376 while (expr != const_expr);
3378 return expr;
3381 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3382 must be a function or a pointer-to-function type, as specified
3383 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3384 and check that the resulting function has external linkage. */
3386 static tree
3387 convert_nontype_argument_function (tree type, tree expr)
3389 tree fns = expr;
3390 tree fn, fn_no_ptr;
3392 fn = instantiate_type (type, fns, tf_none);
3393 if (fn == error_mark_node)
3394 return error_mark_node;
3396 fn_no_ptr = fn;
3397 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3398 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3400 /* [temp.arg.nontype]/1
3402 A template-argument for a non-type, non-template template-parameter
3403 shall be one of:
3404 [...]
3405 -- the address of an object or function with external linkage. */
3406 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3408 error ("%qE is not a valid template argument for type %qT "
3409 "because function %qD has not external linkage",
3410 expr, type, fn_no_ptr);
3411 return NULL_TREE;
3414 return fn;
3417 /* Attempt to convert the non-type template parameter EXPR to the
3418 indicated TYPE. If the conversion is successful, return the
3419 converted value. If the conversion is unsuccessful, return
3420 NULL_TREE if we issued an error message, or error_mark_node if we
3421 did not. We issue error messages for out-and-out bad template
3422 parameters, but not simply because the conversion failed, since we
3423 might be just trying to do argument deduction. Both TYPE and EXPR
3424 must be non-dependent.
3426 The conversion follows the special rules described in
3427 [temp.arg.nontype], and it is much more strict than an implicit
3428 conversion.
3430 This function is called twice for each template argument (see
3431 lookup_template_class for a more accurate description of this
3432 problem). This means that we need to handle expressions which
3433 are not valid in a C++ source, but can be created from the
3434 first call (for instance, casts to perform conversions). These
3435 hacks can go away after we fix the double coercion problem. */
3437 static tree
3438 convert_nontype_argument (tree type, tree expr)
3440 tree expr_type;
3442 /* Detect immediately string literals as invalid non-type argument.
3443 This special-case is not needed for correctness (we would easily
3444 catch this later), but only to provide better diagnostic for this
3445 common user mistake. As suggested by DR 100, we do not mention
3446 linkage issues in the diagnostic as this is not the point. */
3447 if (TREE_CODE (expr) == STRING_CST)
3449 error ("%qE is not a valid template argument for type %qT "
3450 "because string literals can never be used in this context",
3451 expr, type);
3452 return NULL_TREE;
3455 /* If we are in a template, EXPR may be non-dependent, but still
3456 have a syntactic, rather than semantic, form. For example, EXPR
3457 might be a SCOPE_REF, rather than the VAR_DECL to which the
3458 SCOPE_REF refers. Preserving the qualifying scope is necessary
3459 so that access checking can be performed when the template is
3460 instantiated -- but here we need the resolved form so that we can
3461 convert the argument. */
3462 expr = fold_non_dependent_expr (expr);
3463 expr_type = TREE_TYPE (expr);
3465 /* HACK: Due to double coercion, we can get a
3466 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3467 which is the tree that we built on the first call (see
3468 below when coercing to reference to object or to reference to
3469 function). We just strip everything and get to the arg.
3470 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3471 for examples. */
3472 if (TREE_CODE (expr) == NOP_EXPR)
3474 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3476 /* ??? Maybe we could use convert_from_reference here, but we
3477 would need to relax its constraints because the NOP_EXPR
3478 could actually change the type to something more cv-qualified,
3479 and this is not folded by convert_from_reference. */
3480 tree addr = TREE_OPERAND (expr, 0);
3481 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3482 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3483 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3484 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3485 (TREE_TYPE (expr_type),
3486 TREE_TYPE (TREE_TYPE (addr))));
3488 expr = TREE_OPERAND (addr, 0);
3489 expr_type = TREE_TYPE (expr);
3492 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3493 parameter is a pointer to object, through decay and
3494 qualification conversion. Let's strip everything. */
3495 else if (TYPE_PTROBV_P (type))
3497 STRIP_NOPS (expr);
3498 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3499 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3500 /* Skip the ADDR_EXPR only if it is part of the decay for
3501 an array. Otherwise, it is part of the original argument
3502 in the source code. */
3503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3504 expr = TREE_OPERAND (expr, 0);
3505 expr_type = TREE_TYPE (expr);
3509 /* [temp.arg.nontype]/5, bullet 1
3511 For a non-type template-parameter of integral or enumeration type,
3512 integral promotions (_conv.prom_) and integral conversions
3513 (_conv.integral_) are applied. */
3514 if (INTEGRAL_TYPE_P (type))
3516 if (!INTEGRAL_TYPE_P (expr_type))
3517 return error_mark_node;
3519 expr = fold_decl_constant_value (expr);
3520 /* Notice that there are constant expressions like '4 % 0' which
3521 do not fold into integer constants. */
3522 if (TREE_CODE (expr) != INTEGER_CST)
3524 error ("%qE is not a valid template argument for type %qT "
3525 "because it is a non-constant expression", expr, type);
3526 return NULL_TREE;
3529 /* At this point, an implicit conversion does what we want,
3530 because we already know that the expression is of integral
3531 type. */
3532 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3533 if (expr == error_mark_node)
3534 return error_mark_node;
3536 /* Conversion was allowed: fold it to a bare integer constant. */
3537 expr = fold (expr);
3539 /* [temp.arg.nontype]/5, bullet 2
3541 For a non-type template-parameter of type pointer to object,
3542 qualification conversions (_conv.qual_) and the array-to-pointer
3543 conversion (_conv.array_) are applied. */
3544 else if (TYPE_PTROBV_P (type))
3546 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3548 A template-argument for a non-type, non-template template-parameter
3549 shall be one of: [...]
3551 -- the name of a non-type template-parameter;
3552 -- the address of an object or function with external linkage, [...]
3553 expressed as "& id-expression" where the & is optional if the name
3554 refers to a function or array, or if the corresponding
3555 template-parameter is a reference.
3557 Here, we do not care about functions, as they are invalid anyway
3558 for a parameter of type pointer-to-object. */
3559 bool constant_address_p =
3560 (TREE_CODE (expr) == ADDR_EXPR
3561 || TREE_CODE (expr_type) == ARRAY_TYPE
3562 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3564 expr = decay_conversion (expr);
3565 if (expr == error_mark_node)
3566 return error_mark_node;
3568 expr = perform_qualification_conversions (type, expr);
3569 if (expr == error_mark_node)
3570 return error_mark_node;
3572 if (!constant_address_p)
3574 error ("%qE is not a valid template argument for type %qT "
3575 "because it is not a constant pointer", expr, type);
3576 return NULL_TREE;
3579 /* [temp.arg.nontype]/5, bullet 3
3581 For a non-type template-parameter of type reference to object, no
3582 conversions apply. The type referred to by the reference may be more
3583 cv-qualified than the (otherwise identical) type of the
3584 template-argument. The template-parameter is bound directly to the
3585 template-argument, which must be an lvalue. */
3586 else if (TYPE_REF_OBJ_P (type))
3588 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3589 expr_type))
3590 return error_mark_node;
3592 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3594 error ("%qE is not a valid template argument for type %qT "
3595 "because of conflicts in cv-qualification", expr, type);
3596 return NULL_TREE;
3599 if (!real_lvalue_p (expr))
3601 error ("%qE is not a valid template argument for type %qT "
3602 "because it is not a lvalue", expr, type);
3603 return NULL_TREE;
3606 /* [temp.arg.nontype]/1
3608 A template-argument for a non-type, non-template template-parameter
3609 shall be one of: [...]
3611 -- the address of an object or function with external linkage. */
3612 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3614 error ("%qE is not a valid template argument for type %qT "
3615 "because object %qD has not external linkage",
3616 expr, type, expr);
3617 return NULL_TREE;
3620 expr = build_nop (type, build_address (expr));
3622 /* [temp.arg.nontype]/5, bullet 4
3624 For a non-type template-parameter of type pointer to function, only
3625 the function-to-pointer conversion (_conv.func_) is applied. If the
3626 template-argument represents a set of overloaded functions (or a
3627 pointer to such), the matching function is selected from the set
3628 (_over.over_). */
3629 else if (TYPE_PTRFN_P (type))
3631 /* If the argument is a template-id, we might not have enough
3632 context information to decay the pointer. */
3633 if (!type_unknown_p (expr_type))
3635 expr = decay_conversion (expr);
3636 if (expr == error_mark_node)
3637 return error_mark_node;
3640 expr = convert_nontype_argument_function (type, expr);
3641 if (!expr || expr == error_mark_node)
3642 return expr;
3644 /* [temp.arg.nontype]/5, bullet 5
3646 For a non-type template-parameter of type reference to function, no
3647 conversions apply. If the template-argument represents a set of
3648 overloaded functions, the matching function is selected from the set
3649 (_over.over_). */
3650 else if (TYPE_REFFN_P (type))
3652 if (TREE_CODE (expr) == ADDR_EXPR)
3654 error ("%qE is not a valid template argument for type %qT "
3655 "because it is a pointer", expr, type);
3656 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3657 return NULL_TREE;
3660 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3661 if (!expr || expr == error_mark_node)
3662 return expr;
3664 expr = build_nop (type, build_address (expr));
3666 /* [temp.arg.nontype]/5, bullet 6
3668 For a non-type template-parameter of type pointer to member function,
3669 no conversions apply. If the template-argument represents a set of
3670 overloaded member functions, the matching member function is selected
3671 from the set (_over.over_). */
3672 else if (TYPE_PTRMEMFUNC_P (type))
3674 expr = instantiate_type (type, expr, tf_none);
3675 if (expr == error_mark_node)
3676 return error_mark_node;
3678 /* There is no way to disable standard conversions in
3679 resolve_address_of_overloaded_function (called by
3680 instantiate_type). It is possible that the call succeeded by
3681 converting &B::I to &D::I (where B is a base of D), so we need
3682 to reject this conversion here.
3684 Actually, even if there was a way to disable standard conversions,
3685 it would still be better to reject them here so that we can
3686 provide a superior diagnostic. */
3687 if (!same_type_p (TREE_TYPE (expr), type))
3689 /* Make sure we are just one standard conversion off. */
3690 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3691 error ("%qE is not a valid template argument for type %qT "
3692 "because it is of type %qT", expr, type,
3693 TREE_TYPE (expr));
3694 inform ("standard conversions are not allowed in this context");
3695 return NULL_TREE;
3698 /* [temp.arg.nontype]/5, bullet 7
3700 For a non-type template-parameter of type pointer to data member,
3701 qualification conversions (_conv.qual_) are applied. */
3702 else if (TYPE_PTRMEM_P (type))
3704 expr = perform_qualification_conversions (type, expr);
3705 if (expr == error_mark_node)
3706 return expr;
3708 /* A template non-type parameter must be one of the above. */
3709 else
3710 gcc_unreachable ();
3712 /* Sanity check: did we actually convert the argument to the
3713 right type? */
3714 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3715 return expr;
3719 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3720 template template parameters. Both PARM_PARMS and ARG_PARMS are
3721 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3722 or PARM_DECL.
3724 Consider the example:
3725 template <class T> class A;
3726 template<template <class U> class TT> class B;
3728 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3729 the parameters to A, and OUTER_ARGS contains A. */
3731 static int
3732 coerce_template_template_parms (tree parm_parms,
3733 tree arg_parms,
3734 tsubst_flags_t complain,
3735 tree in_decl,
3736 tree outer_args)
3738 int nparms, nargs, i;
3739 tree parm, arg;
3741 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3742 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3744 nparms = TREE_VEC_LENGTH (parm_parms);
3745 nargs = TREE_VEC_LENGTH (arg_parms);
3747 if (nargs != nparms)
3748 return 0;
3750 for (i = 0; i < nparms; ++i)
3752 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3753 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3755 if (arg == NULL_TREE || arg == error_mark_node
3756 || parm == NULL_TREE || parm == error_mark_node)
3757 return 0;
3759 if (TREE_CODE (arg) != TREE_CODE (parm))
3760 return 0;
3762 switch (TREE_CODE (parm))
3764 case TYPE_DECL:
3765 break;
3767 case TEMPLATE_DECL:
3768 /* We encounter instantiations of templates like
3769 template <template <template <class> class> class TT>
3770 class C; */
3772 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3773 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3775 if (!coerce_template_template_parms
3776 (parmparm, argparm, complain, in_decl, outer_args))
3777 return 0;
3779 break;
3781 case PARM_DECL:
3782 /* The tsubst call is used to handle cases such as
3784 template <int> class C {};
3785 template <class T, template <T> class TT> class D {};
3786 D<int, C> d;
3788 i.e. the parameter list of TT depends on earlier parameters. */
3789 if (!dependent_type_p (TREE_TYPE (arg))
3790 && !same_type_p
3791 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3792 TREE_TYPE (arg)))
3793 return 0;
3794 break;
3796 default:
3797 gcc_unreachable ();
3800 return 1;
3803 /* Convert the indicated template ARG as necessary to match the
3804 indicated template PARM. Returns the converted ARG, or
3805 error_mark_node if the conversion was unsuccessful. Error and
3806 warning messages are issued under control of COMPLAIN. This
3807 conversion is for the Ith parameter in the parameter list. ARGS is
3808 the full set of template arguments deduced so far. */
3810 static tree
3811 convert_template_argument (tree parm,
3812 tree arg,
3813 tree args,
3814 tsubst_flags_t complain,
3815 int i,
3816 tree in_decl)
3818 tree val;
3819 tree inner_args;
3820 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3822 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3824 if (TREE_CODE (arg) == TREE_LIST
3825 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3827 /* The template argument was the name of some
3828 member function. That's usually
3829 invalid, but static members are OK. In any
3830 case, grab the underlying fields/functions
3831 and issue an error later if required. */
3832 arg = TREE_VALUE (arg);
3833 TREE_TYPE (arg) = unknown_type_node;
3836 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3837 requires_type = (TREE_CODE (parm) == TYPE_DECL
3838 || requires_tmpl_type);
3840 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3841 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3842 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3843 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3845 if (is_tmpl_type
3846 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3847 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3848 arg = TYPE_STUB_DECL (arg);
3850 is_type = TYPE_P (arg) || is_tmpl_type;
3852 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3853 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3855 pedwarn ("to refer to a type member of a template parameter, "
3856 "use %<typename %E%>", arg);
3858 arg = make_typename_type (TREE_OPERAND (arg, 0),
3859 TREE_OPERAND (arg, 1),
3860 typename_type,
3861 complain & tf_error);
3862 is_type = 1;
3864 if (is_type != requires_type)
3866 if (in_decl)
3868 if (complain & tf_error)
3870 error ("type/value mismatch at argument %d in template "
3871 "parameter list for %qD",
3872 i + 1, in_decl);
3873 if (is_type)
3874 error (" expected a constant of type %qT, got %qT",
3875 TREE_TYPE (parm),
3876 (is_tmpl_type ? DECL_NAME (arg) : arg));
3877 else if (requires_tmpl_type)
3878 error (" expected a class template, got %qE", arg);
3879 else
3880 error (" expected a type, got %qE", arg);
3883 return error_mark_node;
3885 if (is_tmpl_type ^ requires_tmpl_type)
3887 if (in_decl && (complain & tf_error))
3889 error ("type/value mismatch at argument %d in template "
3890 "parameter list for %qD",
3891 i + 1, in_decl);
3892 if (is_tmpl_type)
3893 error (" expected a type, got %qT", DECL_NAME (arg));
3894 else
3895 error (" expected a class template, got %qT", arg);
3897 return error_mark_node;
3900 if (is_type)
3902 if (requires_tmpl_type)
3904 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3905 /* The number of argument required is not known yet.
3906 Just accept it for now. */
3907 val = TREE_TYPE (arg);
3908 else
3910 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3911 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3913 if (coerce_template_template_parms (parmparm, argparm,
3914 complain, in_decl,
3915 inner_args))
3917 val = arg;
3919 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3920 TEMPLATE_DECL. */
3921 if (val != error_mark_node
3922 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3923 val = TREE_TYPE (val);
3925 else
3927 if (in_decl && (complain & tf_error))
3929 error ("type/value mismatch at argument %d in "
3930 "template parameter list for %qD",
3931 i + 1, in_decl);
3932 error (" expected a template of type %qD, got %qD",
3933 parm, arg);
3936 val = error_mark_node;
3940 else
3941 val = arg;
3942 /* We only form one instance of each template specialization.
3943 Therefore, if we use a non-canonical variant (i.e., a
3944 typedef), any future messages referring to the type will use
3945 the typedef, which is confusing if those future uses do not
3946 themselves also use the typedef. */
3947 if (TYPE_P (val))
3948 val = canonical_type_variant (val);
3950 else
3952 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3954 if (invalid_nontype_parm_type_p (t, complain))
3955 return error_mark_node;
3957 if (!uses_template_parms (arg) && !uses_template_parms (t))
3958 /* We used to call digest_init here. However, digest_init
3959 will report errors, which we don't want when complain
3960 is zero. More importantly, digest_init will try too
3961 hard to convert things: for example, `0' should not be
3962 converted to pointer type at this point according to
3963 the standard. Accepting this is not merely an
3964 extension, since deciding whether or not these
3965 conversions can occur is part of determining which
3966 function template to call, or whether a given explicit
3967 argument specification is valid. */
3968 val = convert_nontype_argument (t, arg);
3969 else
3970 val = arg;
3972 if (val == NULL_TREE)
3973 val = error_mark_node;
3974 else if (val == error_mark_node && (complain & tf_error))
3975 error ("could not convert template argument %qE to %qT", arg, t);
3978 return val;
3981 /* Convert all template arguments to their appropriate types, and
3982 return a vector containing the innermost resulting template
3983 arguments. If any error occurs, return error_mark_node. Error and
3984 warning messages are issued under control of COMPLAIN.
3986 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
3987 for arugments not specified in ARGS. Otherwise, if
3988 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
3989 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
3990 USE_DEFAULT_ARGS is false, then all arguments must be specified in
3991 ARGS. */
3993 static tree
3994 coerce_template_parms (tree parms,
3995 tree args,
3996 tree in_decl,
3997 tsubst_flags_t complain,
3998 bool require_all_args,
3999 bool use_default_args)
4001 int nparms, nargs, i, lost = 0;
4002 tree inner_args;
4003 tree new_args;
4004 tree new_inner_args;
4006 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4007 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4008 nparms = TREE_VEC_LENGTH (parms);
4010 if (nargs > nparms
4011 || (nargs < nparms
4012 && require_all_args
4013 && (!use_default_args
4014 || !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
4016 if (complain & tf_error)
4018 error ("wrong number of template arguments (%d, should be %d)",
4019 nargs, nparms);
4021 if (in_decl)
4022 error ("provided for %q+D", in_decl);
4025 return error_mark_node;
4028 new_inner_args = make_tree_vec (nparms);
4029 new_args = add_outermost_template_args (args, new_inner_args);
4030 for (i = 0; i < nparms; i++)
4032 tree arg;
4033 tree parm;
4035 /* Get the Ith template parameter. */
4036 parm = TREE_VEC_ELT (parms, i);
4038 /* Calculate the Ith argument. */
4039 if (i < nargs)
4040 arg = TREE_VEC_ELT (inner_args, i);
4041 else if (require_all_args)
4042 /* There must be a default arg in this case. */
4043 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4044 complain, in_decl);
4045 else
4046 break;
4048 gcc_assert (arg);
4049 if (arg == error_mark_node)
4051 if (complain & tf_error)
4052 error ("template argument %d is invalid", i + 1);
4054 else
4055 arg = convert_template_argument (TREE_VALUE (parm),
4056 arg, new_args, complain, i,
4057 in_decl);
4059 if (arg == error_mark_node)
4060 lost++;
4061 TREE_VEC_ELT (new_inner_args, i) = arg;
4064 if (lost)
4065 return error_mark_node;
4067 return new_inner_args;
4070 /* Returns 1 if template args OT and NT are equivalent. */
4072 static int
4073 template_args_equal (tree ot, tree nt)
4075 if (nt == ot)
4076 return 1;
4078 if (TREE_CODE (nt) == TREE_VEC)
4079 /* For member templates */
4080 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4081 else if (TYPE_P (nt))
4082 return TYPE_P (ot) && same_type_p (ot, nt);
4083 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4084 return 0;
4085 else
4086 return cp_tree_equal (ot, nt);
4089 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4090 of template arguments. Returns 0 otherwise. */
4093 comp_template_args (tree oldargs, tree newargs)
4095 int i;
4097 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4098 return 0;
4100 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4102 tree nt = TREE_VEC_ELT (newargs, i);
4103 tree ot = TREE_VEC_ELT (oldargs, i);
4105 if (! template_args_equal (ot, nt))
4106 return 0;
4108 return 1;
4111 /* Given class template name and parameter list, produce a user-friendly name
4112 for the instantiation. */
4114 static char *
4115 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4117 static struct obstack scratch_obstack;
4118 static char *scratch_firstobj;
4119 int i, nparms;
4121 if (!scratch_firstobj)
4122 gcc_obstack_init (&scratch_obstack);
4123 else
4124 obstack_free (&scratch_obstack, scratch_firstobj);
4125 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4127 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4128 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4130 cat (name);
4131 ccat ('<');
4132 nparms = TREE_VEC_LENGTH (parms);
4133 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4134 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4135 for (i = 0; i < nparms; i++)
4137 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4138 tree arg = TREE_VEC_ELT (arglist, i);
4140 if (i)
4141 ccat (',');
4143 if (TREE_CODE (parm) == TYPE_DECL)
4145 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4146 continue;
4148 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4150 if (TREE_CODE (arg) == TEMPLATE_DECL)
4152 /* Already substituted with real template. Just output
4153 the template name here */
4154 tree context = DECL_CONTEXT (arg);
4155 if (context)
4157 /* The template may be defined in a namespace, or
4158 may be a member template. */
4159 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4160 || CLASS_TYPE_P (context));
4161 cat (decl_as_string (DECL_CONTEXT (arg),
4162 TFF_PLAIN_IDENTIFIER));
4163 cat ("::");
4165 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4167 else
4168 /* Output the parameter declaration. */
4169 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4170 continue;
4172 else
4173 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4175 /* No need to check arglist against parmlist here; we did that
4176 in coerce_template_parms, called from lookup_template_class. */
4177 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4180 char *bufp = obstack_next_free (&scratch_obstack);
4181 int offset = 0;
4182 while (bufp[offset - 1] == ' ')
4183 offset--;
4184 obstack_blank_fast (&scratch_obstack, offset);
4186 /* B<C<char> >, not B<C<char>> */
4187 if (bufp[offset - 1] == '>')
4188 ccat (' ');
4190 ccat ('>');
4191 ccat ('\0');
4192 return (char *) obstack_base (&scratch_obstack);
4195 static tree
4196 classtype_mangled_name (tree t)
4198 if (CLASSTYPE_TEMPLATE_INFO (t)
4199 /* Specializations have already had their names set up in
4200 lookup_template_class. */
4201 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4203 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4205 /* For non-primary templates, the template parameters are
4206 implicit from their surrounding context. */
4207 if (PRIMARY_TEMPLATE_P (tmpl))
4209 tree name = DECL_NAME (tmpl);
4210 char *mangled_name = mangle_class_name_for_template
4211 (IDENTIFIER_POINTER (name),
4212 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4213 CLASSTYPE_TI_ARGS (t));
4214 tree id = get_identifier (mangled_name);
4215 IDENTIFIER_TEMPLATE (id) = name;
4216 return id;
4220 return TYPE_IDENTIFIER (t);
4223 static void
4224 add_pending_template (tree d)
4226 tree ti = (TYPE_P (d)
4227 ? CLASSTYPE_TEMPLATE_INFO (d)
4228 : DECL_TEMPLATE_INFO (d));
4229 tree pt;
4230 int level;
4232 if (TI_PENDING_TEMPLATE_FLAG (ti))
4233 return;
4235 /* We are called both from instantiate_decl, where we've already had a
4236 tinst_level pushed, and instantiate_template, where we haven't.
4237 Compensate. */
4238 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4240 if (level)
4241 push_tinst_level (d);
4243 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4244 if (last_pending_template)
4245 TREE_CHAIN (last_pending_template) = pt;
4246 else
4247 pending_templates = pt;
4249 last_pending_template = pt;
4251 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4253 if (level)
4254 pop_tinst_level ();
4258 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4259 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4260 documentation for TEMPLATE_ID_EXPR. */
4262 tree
4263 lookup_template_function (tree fns, tree arglist)
4265 tree type;
4267 if (fns == error_mark_node || arglist == error_mark_node)
4268 return error_mark_node;
4270 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4271 gcc_assert (fns && (is_overloaded_fn (fns)
4272 || TREE_CODE (fns) == IDENTIFIER_NODE));
4274 if (BASELINK_P (fns))
4276 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4277 unknown_type_node,
4278 BASELINK_FUNCTIONS (fns),
4279 arglist);
4280 return fns;
4283 type = TREE_TYPE (fns);
4284 if (TREE_CODE (fns) == OVERLOAD || !type)
4285 type = unknown_type_node;
4287 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4290 /* Within the scope of a template class S<T>, the name S gets bound
4291 (in build_self_reference) to a TYPE_DECL for the class, not a
4292 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4293 or one of its enclosing classes, and that type is a template,
4294 return the associated TEMPLATE_DECL. Otherwise, the original
4295 DECL is returned. */
4297 tree
4298 maybe_get_template_decl_from_type_decl (tree decl)
4300 return (decl != NULL_TREE
4301 && TREE_CODE (decl) == TYPE_DECL
4302 && DECL_ARTIFICIAL (decl)
4303 && CLASS_TYPE_P (TREE_TYPE (decl))
4304 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4305 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4308 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4309 parameters, find the desired type.
4311 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4313 IN_DECL, if non-NULL, is the template declaration we are trying to
4314 instantiate.
4316 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4317 the class we are looking up.
4319 Issue error and warning messages under control of COMPLAIN.
4321 If the template class is really a local class in a template
4322 function, then the FUNCTION_CONTEXT is the function in which it is
4323 being instantiated.
4325 ??? Note that this function is currently called *twice* for each
4326 template-id: the first time from the parser, while creating the
4327 incomplete type (finish_template_type), and the second type during the
4328 real instantiation (instantiate_template_class). This is surely something
4329 that we want to avoid. It also causes some problems with argument
4330 coercion (see convert_nontype_argument for more information on this). */
4332 tree
4333 lookup_template_class (tree d1,
4334 tree arglist,
4335 tree in_decl,
4336 tree context,
4337 int entering_scope,
4338 tsubst_flags_t complain)
4340 tree template = NULL_TREE, parmlist;
4341 tree t;
4343 timevar_push (TV_NAME_LOOKUP);
4345 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4347 tree value = innermost_non_namespace_value (d1);
4348 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4349 template = value;
4350 else
4352 if (context)
4353 push_decl_namespace (context);
4354 template = lookup_name (d1);
4355 template = maybe_get_template_decl_from_type_decl (template);
4356 if (context)
4357 pop_decl_namespace ();
4359 if (template)
4360 context = DECL_CONTEXT (template);
4362 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4364 tree type = TREE_TYPE (d1);
4366 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4367 an implicit typename for the second A. Deal with it. */
4368 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4369 type = TREE_TYPE (type);
4371 if (CLASSTYPE_TEMPLATE_INFO (type))
4373 template = CLASSTYPE_TI_TEMPLATE (type);
4374 d1 = DECL_NAME (template);
4377 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4378 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4380 template = TYPE_TI_TEMPLATE (d1);
4381 d1 = DECL_NAME (template);
4383 else if (TREE_CODE (d1) == TEMPLATE_DECL
4384 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4386 template = d1;
4387 d1 = DECL_NAME (template);
4388 context = DECL_CONTEXT (template);
4391 /* Issue an error message if we didn't find a template. */
4392 if (! template)
4394 if (complain & tf_error)
4395 error ("%qT is not a template", d1);
4396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4399 if (TREE_CODE (template) != TEMPLATE_DECL
4400 /* Make sure it's a user visible template, if it was named by
4401 the user. */
4402 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4403 && !PRIMARY_TEMPLATE_P (template)))
4405 if (complain & tf_error)
4407 error ("non-template type %qT used as a template", d1);
4408 if (in_decl)
4409 error ("for template declaration %q+D", in_decl);
4411 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4414 complain &= ~tf_user;
4416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4418 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4419 template arguments */
4421 tree parm;
4422 tree arglist2;
4424 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4426 /* Consider an example where a template template parameter declared as
4428 template <class T, class U = std::allocator<T> > class TT
4430 The template parameter level of T and U are one level larger than
4431 of TT. To proper process the default argument of U, say when an
4432 instantiation `TT<int>' is seen, we need to build the full
4433 arguments containing {int} as the innermost level. Outer levels,
4434 available when not appearing as default template argument, can be
4435 obtained from `current_template_args ()'.
4437 Suppose that TT is later substituted with std::vector. The above
4438 instantiation is `TT<int, std::allocator<T> >' with TT at
4439 level 1, and T at level 2, while the template arguments at level 1
4440 becomes {std::vector} and the inner level 2 is {int}. */
4442 if (current_template_parms)
4443 arglist = add_to_template_args (current_template_args (), arglist);
4445 arglist2 = coerce_template_parms (parmlist, arglist, template,
4446 complain,
4447 /*require_all_args=*/true,
4448 /*use_default_args=*/true);
4449 if (arglist2 == error_mark_node
4450 || (!uses_template_parms (arglist2)
4451 && check_instantiated_args (template, arglist2, complain)))
4452 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4454 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4455 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4457 else
4459 tree template_type = TREE_TYPE (template);
4460 tree gen_tmpl;
4461 tree type_decl;
4462 tree found = NULL_TREE;
4463 int arg_depth;
4464 int parm_depth;
4465 int is_partial_instantiation;
4467 gen_tmpl = most_general_template (template);
4468 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4469 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4470 arg_depth = TMPL_ARGS_DEPTH (arglist);
4472 if (arg_depth == 1 && parm_depth > 1)
4474 /* We've been given an incomplete set of template arguments.
4475 For example, given:
4477 template <class T> struct S1 {
4478 template <class U> struct S2 {};
4479 template <class U> struct S2<U*> {};
4482 we will be called with an ARGLIST of `U*', but the
4483 TEMPLATE will be `template <class T> template
4484 <class U> struct S1<T>::S2'. We must fill in the missing
4485 arguments. */
4486 arglist
4487 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4488 arglist);
4489 arg_depth = TMPL_ARGS_DEPTH (arglist);
4492 /* Now we should have enough arguments. */
4493 gcc_assert (parm_depth == arg_depth);
4495 /* From here on, we're only interested in the most general
4496 template. */
4497 template = gen_tmpl;
4499 /* Calculate the BOUND_ARGS. These will be the args that are
4500 actually tsubst'd into the definition to create the
4501 instantiation. */
4502 if (parm_depth > 1)
4504 /* We have multiple levels of arguments to coerce, at once. */
4505 int i;
4506 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4508 tree bound_args = make_tree_vec (parm_depth);
4510 for (i = saved_depth,
4511 t = DECL_TEMPLATE_PARMS (template);
4512 i > 0 && t != NULL_TREE;
4513 --i, t = TREE_CHAIN (t))
4515 tree a = coerce_template_parms (TREE_VALUE (t),
4516 arglist, template,
4517 complain,
4518 /*require_all_args=*/true,
4519 /*use_default_args=*/true);
4521 /* Don't process further if one of the levels fails. */
4522 if (a == error_mark_node)
4524 /* Restore the ARGLIST to its full size. */
4525 TREE_VEC_LENGTH (arglist) = saved_depth;
4526 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4529 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4531 /* We temporarily reduce the length of the ARGLIST so
4532 that coerce_template_parms will see only the arguments
4533 corresponding to the template parameters it is
4534 examining. */
4535 TREE_VEC_LENGTH (arglist)--;
4538 /* Restore the ARGLIST to its full size. */
4539 TREE_VEC_LENGTH (arglist) = saved_depth;
4541 arglist = bound_args;
4543 else
4544 arglist
4545 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4546 INNERMOST_TEMPLATE_ARGS (arglist),
4547 template,
4548 complain,
4549 /*require_all_args=*/true,
4550 /*use_default_args=*/true);
4552 if (arglist == error_mark_node)
4553 /* We were unable to bind the arguments. */
4554 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4556 /* In the scope of a template class, explicit references to the
4557 template class refer to the type of the template, not any
4558 instantiation of it. For example, in:
4560 template <class T> class C { void f(C<T>); }
4562 the `C<T>' is just the same as `C'. Outside of the
4563 class, however, such a reference is an instantiation. */
4564 if (comp_template_args (TYPE_TI_ARGS (template_type),
4565 arglist))
4567 found = template_type;
4569 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4571 tree ctx;
4573 for (ctx = current_class_type;
4574 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4575 ctx = (TYPE_P (ctx)
4576 ? TYPE_CONTEXT (ctx)
4577 : DECL_CONTEXT (ctx)))
4578 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4579 goto found_ctx;
4581 /* We're not in the scope of the class, so the
4582 TEMPLATE_TYPE is not the type we want after all. */
4583 found = NULL_TREE;
4584 found_ctx:;
4587 if (found)
4588 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4590 /* If we already have this specialization, return it. */
4591 found = retrieve_specialization (template, arglist,
4592 /*class_specializations_p=*/false);
4593 if (found)
4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4596 /* This type is a "partial instantiation" if any of the template
4597 arguments still involve template parameters. Note that we set
4598 IS_PARTIAL_INSTANTIATION for partial specializations as
4599 well. */
4600 is_partial_instantiation = uses_template_parms (arglist);
4602 /* If the deduced arguments are invalid, then the binding
4603 failed. */
4604 if (!is_partial_instantiation
4605 && check_instantiated_args (template,
4606 INNERMOST_TEMPLATE_ARGS (arglist),
4607 complain))
4608 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4610 if (!is_partial_instantiation
4611 && !PRIMARY_TEMPLATE_P (template)
4612 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4614 found = xref_tag_from_type (TREE_TYPE (template),
4615 DECL_NAME (template),
4616 /*tag_scope=*/ts_global);
4617 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4620 context = tsubst (DECL_CONTEXT (template), arglist,
4621 complain, in_decl);
4622 if (!context)
4623 context = global_namespace;
4625 /* Create the type. */
4626 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4628 if (!is_partial_instantiation)
4630 set_current_access_from_decl (TYPE_NAME (template_type));
4631 t = start_enum (TYPE_IDENTIFIER (template_type));
4633 else
4634 /* We don't want to call start_enum for this type, since
4635 the values for the enumeration constants may involve
4636 template parameters. And, no one should be interested
4637 in the enumeration constants for such a type. */
4638 t = make_node (ENUMERAL_TYPE);
4640 else
4642 t = make_aggr_type (TREE_CODE (template_type));
4643 CLASSTYPE_DECLARED_CLASS (t)
4644 = CLASSTYPE_DECLARED_CLASS (template_type);
4645 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4646 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4648 /* A local class. Make sure the decl gets registered properly. */
4649 if (context == current_function_decl)
4650 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4653 /* If we called start_enum or pushtag above, this information
4654 will already be set up. */
4655 if (!TYPE_NAME (t))
4657 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4659 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4660 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4661 TYPE_STUB_DECL (t) = type_decl;
4662 DECL_SOURCE_LOCATION (type_decl)
4663 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4665 else
4666 type_decl = TYPE_NAME (t);
4668 TREE_PRIVATE (type_decl)
4669 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4670 TREE_PROTECTED (type_decl)
4671 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4672 DECL_IN_SYSTEM_HEADER (type_decl)
4673 = DECL_IN_SYSTEM_HEADER (template);
4674 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4676 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4677 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4680 /* Set up the template information. We have to figure out which
4681 template is the immediate parent if this is a full
4682 instantiation. */
4683 if (parm_depth == 1 || is_partial_instantiation
4684 || !PRIMARY_TEMPLATE_P (template))
4685 /* This case is easy; there are no member templates involved. */
4686 found = template;
4687 else
4689 /* This is a full instantiation of a member template. Look
4690 for a partial instantiation of which this is an instance. */
4692 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4693 found; found = TREE_CHAIN (found))
4695 int success;
4696 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4698 /* We only want partial instantiations, here, not
4699 specializations or full instantiations. */
4700 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4701 || !uses_template_parms (TREE_VALUE (found)))
4702 continue;
4704 /* Temporarily reduce by one the number of levels in the
4705 ARGLIST and in FOUND so as to avoid comparing the
4706 last set of arguments. */
4707 TREE_VEC_LENGTH (arglist)--;
4708 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4710 /* See if the arguments match. If they do, then TMPL is
4711 the partial instantiation we want. */
4712 success = comp_template_args (TREE_PURPOSE (found), arglist);
4714 /* Restore the argument vectors to their full size. */
4715 TREE_VEC_LENGTH (arglist)++;
4716 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4718 if (success)
4720 found = tmpl;
4721 break;
4725 if (!found)
4727 /* There was no partial instantiation. This happens
4728 where C<T> is a member template of A<T> and it's used
4729 in something like
4731 template <typename T> struct B { A<T>::C<int> m; };
4732 B<float>;
4734 Create the partial instantiation.
4736 TREE_VEC_LENGTH (arglist)--;
4737 found = tsubst (template, arglist, complain, NULL_TREE);
4738 TREE_VEC_LENGTH (arglist)++;
4742 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4743 DECL_TEMPLATE_INSTANTIATIONS (template)
4744 = tree_cons (arglist, t,
4745 DECL_TEMPLATE_INSTANTIATIONS (template));
4747 if (TREE_CODE (t) == ENUMERAL_TYPE
4748 && !is_partial_instantiation)
4749 /* Now that the type has been registered on the instantiations
4750 list, we set up the enumerators. Because the enumeration
4751 constants may involve the enumeration type itself, we make
4752 sure to register the type first, and then create the
4753 constants. That way, doing tsubst_expr for the enumeration
4754 constants won't result in recursive calls here; we'll find
4755 the instantiation and exit above. */
4756 tsubst_enum (template_type, t, arglist);
4758 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4759 is set up. */
4760 if (TREE_CODE (t) != ENUMERAL_TYPE)
4761 DECL_NAME (type_decl) = classtype_mangled_name (t);
4762 if (is_partial_instantiation)
4763 /* If the type makes use of template parameters, the
4764 code that generates debugging information will crash. */
4765 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4769 timevar_pop (TV_NAME_LOOKUP);
4772 struct pair_fn_data
4774 tree_fn_t fn;
4775 void *data;
4776 struct pointer_set_t *visited;
4779 /* Called from for_each_template_parm via walk_tree. */
4781 static tree
4782 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4784 tree t = *tp;
4785 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4786 tree_fn_t fn = pfd->fn;
4787 void *data = pfd->data;
4789 if (TYPE_P (t)
4790 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4791 return error_mark_node;
4793 switch (TREE_CODE (t))
4795 case RECORD_TYPE:
4796 if (TYPE_PTRMEMFUNC_P (t))
4797 break;
4798 /* Fall through. */
4800 case UNION_TYPE:
4801 case ENUMERAL_TYPE:
4802 if (!TYPE_TEMPLATE_INFO (t))
4803 *walk_subtrees = 0;
4804 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4805 fn, data, pfd->visited))
4806 return error_mark_node;
4807 break;
4809 case METHOD_TYPE:
4810 /* Since we're not going to walk subtrees, we have to do this
4811 explicitly here. */
4812 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4813 pfd->visited))
4814 return error_mark_node;
4815 /* Fall through. */
4817 case FUNCTION_TYPE:
4818 /* Check the return type. */
4819 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4820 return error_mark_node;
4822 /* Check the parameter types. Since default arguments are not
4823 instantiated until they are needed, the TYPE_ARG_TYPES may
4824 contain expressions that involve template parameters. But,
4825 no-one should be looking at them yet. And, once they're
4826 instantiated, they don't contain template parameters, so
4827 there's no point in looking at them then, either. */
4829 tree parm;
4831 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4832 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4833 pfd->visited))
4834 return error_mark_node;
4836 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4837 want walk_tree walking into them itself. */
4838 *walk_subtrees = 0;
4840 break;
4842 case TYPEOF_TYPE:
4843 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4844 pfd->visited))
4845 return error_mark_node;
4846 break;
4848 case FUNCTION_DECL:
4849 case VAR_DECL:
4850 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4851 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4852 pfd->visited))
4853 return error_mark_node;
4854 /* Fall through. */
4856 case PARM_DECL:
4857 case CONST_DECL:
4858 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4859 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4860 pfd->visited))
4861 return error_mark_node;
4862 if (DECL_CONTEXT (t)
4863 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4864 pfd->visited))
4865 return error_mark_node;
4866 break;
4868 case BOUND_TEMPLATE_TEMPLATE_PARM:
4869 /* Record template parameters such as `T' inside `TT<T>'. */
4870 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4871 return error_mark_node;
4872 /* Fall through. */
4874 case TEMPLATE_TEMPLATE_PARM:
4875 case TEMPLATE_TYPE_PARM:
4876 case TEMPLATE_PARM_INDEX:
4877 if (fn && (*fn)(t, data))
4878 return error_mark_node;
4879 else if (!fn)
4880 return error_mark_node;
4881 break;
4883 case TEMPLATE_DECL:
4884 /* A template template parameter is encountered. */
4885 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4886 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4887 return error_mark_node;
4889 /* Already substituted template template parameter */
4890 *walk_subtrees = 0;
4891 break;
4893 case TYPENAME_TYPE:
4894 if (!fn
4895 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4896 data, pfd->visited))
4897 return error_mark_node;
4898 break;
4900 case CONSTRUCTOR:
4901 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4902 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4903 (TREE_TYPE (t)), fn, data,
4904 pfd->visited))
4905 return error_mark_node;
4906 break;
4908 case INDIRECT_REF:
4909 case COMPONENT_REF:
4910 /* If there's no type, then this thing must be some expression
4911 involving template parameters. */
4912 if (!fn && !TREE_TYPE (t))
4913 return error_mark_node;
4914 break;
4916 case MODOP_EXPR:
4917 case CAST_EXPR:
4918 case REINTERPRET_CAST_EXPR:
4919 case CONST_CAST_EXPR:
4920 case STATIC_CAST_EXPR:
4921 case DYNAMIC_CAST_EXPR:
4922 case ARROW_EXPR:
4923 case DOTSTAR_EXPR:
4924 case TYPEID_EXPR:
4925 case PSEUDO_DTOR_EXPR:
4926 if (!fn)
4927 return error_mark_node;
4928 break;
4930 case BASELINK:
4931 /* If we do not handle this case specially, we end up walking
4932 the BINFO hierarchy, which is circular, and therefore
4933 confuses walk_tree. */
4934 *walk_subtrees = 0;
4935 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4936 pfd->visited))
4937 return error_mark_node;
4938 break;
4940 default:
4941 break;
4944 /* We didn't find any template parameters we liked. */
4945 return NULL_TREE;
4948 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4949 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4950 call FN with the parameter and the DATA.
4951 If FN returns nonzero, the iteration is terminated, and
4952 for_each_template_parm returns 1. Otherwise, the iteration
4953 continues. If FN never returns a nonzero value, the value
4954 returned by for_each_template_parm is 0. If FN is NULL, it is
4955 considered to be the function which always returns 1. */
4957 static int
4958 for_each_template_parm (tree t, tree_fn_t fn, void* data,
4959 struct pointer_set_t *visited)
4961 struct pair_fn_data pfd;
4962 int result;
4964 /* Set up. */
4965 pfd.fn = fn;
4966 pfd.data = data;
4968 /* Walk the tree. (Conceptually, we would like to walk without
4969 duplicates, but for_each_template_parm_r recursively calls
4970 for_each_template_parm, so we would need to reorganize a fair
4971 bit to use walk_tree_without_duplicates, so we keep our own
4972 visited list.) */
4973 if (visited)
4974 pfd.visited = visited;
4975 else
4976 pfd.visited = pointer_set_create ();
4977 result = walk_tree (&t,
4978 for_each_template_parm_r,
4979 &pfd,
4980 pfd.visited) != NULL_TREE;
4982 /* Clean up. */
4983 if (!visited)
4985 pointer_set_destroy (pfd.visited);
4986 pfd.visited = 0;
4989 return result;
4992 /* Returns true if T depends on any template parameter. */
4995 uses_template_parms (tree t)
4997 bool dependent_p;
4998 int saved_processing_template_decl;
5000 saved_processing_template_decl = processing_template_decl;
5001 if (!saved_processing_template_decl)
5002 processing_template_decl = 1;
5003 if (TYPE_P (t))
5004 dependent_p = dependent_type_p (t);
5005 else if (TREE_CODE (t) == TREE_VEC)
5006 dependent_p = any_dependent_template_arguments_p (t);
5007 else if (TREE_CODE (t) == TREE_LIST)
5008 dependent_p = (uses_template_parms (TREE_VALUE (t))
5009 || uses_template_parms (TREE_CHAIN (t)));
5010 else if (TREE_CODE (t) == TYPE_DECL)
5011 dependent_p = dependent_type_p (TREE_TYPE (t));
5012 else if (DECL_P (t)
5013 || EXPR_P (t)
5014 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5015 || TREE_CODE (t) == OVERLOAD
5016 || TREE_CODE (t) == BASELINK
5017 || TREE_CODE (t) == IDENTIFIER_NODE
5018 || CONSTANT_CLASS_P (t))
5019 dependent_p = (type_dependent_expression_p (t)
5020 || value_dependent_expression_p (t));
5021 else
5023 gcc_assert (t == error_mark_node);
5024 dependent_p = false;
5027 processing_template_decl = saved_processing_template_decl;
5029 return dependent_p;
5032 /* Returns true if T depends on any template parameter with level LEVEL. */
5035 uses_template_parms_level (tree t, int level)
5037 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5040 static int tinst_depth;
5041 extern int max_tinst_depth;
5042 #ifdef GATHER_STATISTICS
5043 int depth_reached;
5044 #endif
5045 static int tinst_level_tick;
5046 static int last_template_error_tick;
5048 /* We're starting to instantiate D; record the template instantiation context
5049 for diagnostics and to restore it later. */
5051 static int
5052 push_tinst_level (tree d)
5054 tree new;
5056 if (tinst_depth >= max_tinst_depth)
5058 /* If the instantiation in question still has unbound template parms,
5059 we don't really care if we can't instantiate it, so just return.
5060 This happens with base instantiation for implicit `typename'. */
5061 if (uses_template_parms (d))
5062 return 0;
5064 last_template_error_tick = tinst_level_tick;
5065 error ("template instantiation depth exceeds maximum of %d (use "
5066 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5067 max_tinst_depth, d);
5069 print_instantiation_context ();
5071 return 0;
5074 new = make_node (TINST_LEVEL);
5075 TINST_DECL (new) = d;
5076 TINST_LOCATION (new) = input_location;
5077 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5078 TREE_CHAIN (new) = current_tinst_level;
5079 current_tinst_level = new;
5081 ++tinst_depth;
5082 #ifdef GATHER_STATISTICS
5083 if (tinst_depth > depth_reached)
5084 depth_reached = tinst_depth;
5085 #endif
5087 ++tinst_level_tick;
5088 return 1;
5091 /* We're done instantiating this template; return to the instantiation
5092 context. */
5094 static void
5095 pop_tinst_level (void)
5097 tree old = current_tinst_level;
5099 /* Restore the filename and line number stashed away when we started
5100 this instantiation. */
5101 input_location = TINST_LOCATION (old);
5102 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5103 current_tinst_level = TREE_CHAIN (old);
5104 --tinst_depth;
5105 ++tinst_level_tick;
5108 /* We're instantiating a deferred template; restore the template
5109 instantiation context in which the instantiation was requested, which
5110 is one step out from LEVEL. */
5112 static void
5113 reopen_tinst_level (tree level)
5115 tree t;
5117 tinst_depth = 0;
5118 for (t = level; t; t = TREE_CHAIN (t))
5119 ++tinst_depth;
5121 current_tinst_level = level;
5122 pop_tinst_level ();
5125 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5126 vector of template arguments, as for tsubst.
5128 Returns an appropriate tsubst'd friend declaration. */
5130 static tree
5131 tsubst_friend_function (tree decl, tree args)
5133 tree new_friend;
5135 if (TREE_CODE (decl) == FUNCTION_DECL
5136 && DECL_TEMPLATE_INSTANTIATION (decl)
5137 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5138 /* This was a friend declared with an explicit template
5139 argument list, e.g.:
5141 friend void f<>(T);
5143 to indicate that f was a template instantiation, not a new
5144 function declaration. Now, we have to figure out what
5145 instantiation of what template. */
5147 tree template_id, arglist, fns;
5148 tree new_args;
5149 tree tmpl;
5150 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5152 /* Friend functions are looked up in the containing namespace scope.
5153 We must enter that scope, to avoid finding member functions of the
5154 current cless with same name. */
5155 push_nested_namespace (ns);
5156 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5157 tf_warning_or_error, NULL_TREE);
5158 pop_nested_namespace (ns);
5159 arglist = tsubst (DECL_TI_ARGS (decl), args,
5160 tf_warning_or_error, NULL_TREE);
5161 template_id = lookup_template_function (fns, arglist);
5163 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5164 tmpl = determine_specialization (template_id, new_friend,
5165 &new_args,
5166 /*need_member_template=*/0,
5167 TREE_VEC_LENGTH (args));
5168 return instantiate_template (tmpl, new_args, tf_error);
5171 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5173 /* The NEW_FRIEND will look like an instantiation, to the
5174 compiler, but is not an instantiation from the point of view of
5175 the language. For example, we might have had:
5177 template <class T> struct S {
5178 template <class U> friend void f(T, U);
5181 Then, in S<int>, template <class U> void f(int, U) is not an
5182 instantiation of anything. */
5183 if (new_friend == error_mark_node)
5184 return error_mark_node;
5186 DECL_USE_TEMPLATE (new_friend) = 0;
5187 if (TREE_CODE (decl) == TEMPLATE_DECL)
5189 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5190 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5191 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5194 /* The mangled name for the NEW_FRIEND is incorrect. The function
5195 is not a template instantiation and should not be mangled like
5196 one. Therefore, we forget the mangling here; we'll recompute it
5197 later if we need it. */
5198 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5200 SET_DECL_RTL (new_friend, NULL_RTX);
5201 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5204 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5206 tree old_decl;
5207 tree new_friend_template_info;
5208 tree new_friend_result_template_info;
5209 tree ns;
5210 int new_friend_is_defn;
5212 /* We must save some information from NEW_FRIEND before calling
5213 duplicate decls since that function will free NEW_FRIEND if
5214 possible. */
5215 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5216 new_friend_is_defn =
5217 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5218 (template_for_substitution (new_friend)))
5219 != NULL_TREE);
5220 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5222 /* This declaration is a `primary' template. */
5223 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5225 new_friend_result_template_info
5226 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5228 else
5229 new_friend_result_template_info = NULL_TREE;
5231 /* Inside pushdecl_namespace_level, we will push into the
5232 current namespace. However, the friend function should go
5233 into the namespace of the template. */
5234 ns = decl_namespace_context (new_friend);
5235 push_nested_namespace (ns);
5236 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5237 pop_nested_namespace (ns);
5239 if (old_decl != new_friend)
5241 /* This new friend declaration matched an existing
5242 declaration. For example, given:
5244 template <class T> void f(T);
5245 template <class U> class C {
5246 template <class T> friend void f(T) {}
5249 the friend declaration actually provides the definition
5250 of `f', once C has been instantiated for some type. So,
5251 old_decl will be the out-of-class template declaration,
5252 while new_friend is the in-class definition.
5254 But, if `f' was called before this point, the
5255 instantiation of `f' will have DECL_TI_ARGS corresponding
5256 to `T' but not to `U', references to which might appear
5257 in the definition of `f'. Previously, the most general
5258 template for an instantiation of `f' was the out-of-class
5259 version; now it is the in-class version. Therefore, we
5260 run through all specialization of `f', adding to their
5261 DECL_TI_ARGS appropriately. In particular, they need a
5262 new set of outer arguments, corresponding to the
5263 arguments for this class instantiation.
5265 The same situation can arise with something like this:
5267 friend void f(int);
5268 template <class T> class C {
5269 friend void f(T) {}
5272 when `C<int>' is instantiated. Now, `f(int)' is defined
5273 in the class. */
5275 if (!new_friend_is_defn)
5276 /* On the other hand, if the in-class declaration does
5277 *not* provide a definition, then we don't want to alter
5278 existing definitions. We can just leave everything
5279 alone. */
5281 else
5283 /* Overwrite whatever template info was there before, if
5284 any, with the new template information pertaining to
5285 the declaration. */
5286 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5288 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5289 reregister_specialization (new_friend,
5290 most_general_template (old_decl),
5291 old_decl);
5292 else
5294 tree t;
5295 tree new_friend_args;
5297 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5298 = new_friend_result_template_info;
5300 new_friend_args = TI_ARGS (new_friend_template_info);
5301 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5302 t != NULL_TREE;
5303 t = TREE_CHAIN (t))
5305 tree spec = TREE_VALUE (t);
5307 DECL_TI_ARGS (spec)
5308 = add_outermost_template_args (new_friend_args,
5309 DECL_TI_ARGS (spec));
5312 /* Now, since specializations are always supposed to
5313 hang off of the most general template, we must move
5314 them. */
5315 t = most_general_template (old_decl);
5316 if (t != old_decl)
5318 DECL_TEMPLATE_SPECIALIZATIONS (t)
5319 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5320 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5321 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5326 /* The information from NEW_FRIEND has been merged into OLD_DECL
5327 by duplicate_decls. */
5328 new_friend = old_decl;
5331 else
5333 tree context = DECL_CONTEXT (new_friend);
5334 bool dependent_p;
5336 /* In the code
5337 template <class T> class C {
5338 template <class U> friend void C1<U>::f (); // case 1
5339 friend void C2<T>::f (); // case 2
5341 we only need to make sure CONTEXT is a complete type for
5342 case 2. To distinguish between the two cases, we note that
5343 CONTEXT of case 1 remains dependent type after tsubst while
5344 this isn't true for case 2. */
5345 ++processing_template_decl;
5346 dependent_p = dependent_type_p (context);
5347 --processing_template_decl;
5349 if (!dependent_p
5350 && !complete_type_or_else (context, NULL_TREE))
5351 return error_mark_node;
5353 if (COMPLETE_TYPE_P (context))
5355 /* Check to see that the declaration is really present, and,
5356 possibly obtain an improved declaration. */
5357 tree fn = check_classfn (context,
5358 new_friend, NULL_TREE);
5360 if (fn)
5361 new_friend = fn;
5365 return new_friend;
5368 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5369 template arguments, as for tsubst.
5371 Returns an appropriate tsubst'd friend type or error_mark_node on
5372 failure. */
5374 static tree
5375 tsubst_friend_class (tree friend_tmpl, tree args)
5377 tree friend_type;
5378 tree tmpl;
5379 tree context;
5381 context = DECL_CONTEXT (friend_tmpl);
5383 if (context)
5385 if (TREE_CODE (context) == NAMESPACE_DECL)
5386 push_nested_namespace (context);
5387 else
5388 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5391 /* First, we look for a class template. */
5392 tmpl = lookup_name (DECL_NAME (friend_tmpl));
5394 /* But, if we don't find one, it might be because we're in a
5395 situation like this:
5397 template <class T>
5398 struct S {
5399 template <class U>
5400 friend struct S;
5403 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5404 for `S<int>', not the TEMPLATE_DECL. */
5405 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5407 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5408 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5411 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5413 /* The friend template has already been declared. Just
5414 check to see that the declarations match, and install any new
5415 default parameters. We must tsubst the default parameters,
5416 of course. We only need the innermost template parameters
5417 because that is all that redeclare_class_template will look
5418 at. */
5419 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5420 > TMPL_ARGS_DEPTH (args))
5422 tree parms;
5423 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5424 args, tf_warning_or_error);
5425 redeclare_class_template (TREE_TYPE (tmpl), parms);
5428 friend_type = TREE_TYPE (tmpl);
5430 else
5432 /* The friend template has not already been declared. In this
5433 case, the instantiation of the template class will cause the
5434 injection of this template into the global scope. */
5435 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5437 /* The new TMPL is not an instantiation of anything, so we
5438 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5439 the new type because that is supposed to be the corresponding
5440 template decl, i.e., TMPL. */
5441 DECL_USE_TEMPLATE (tmpl) = 0;
5442 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5443 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5444 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5445 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5447 /* Inject this template into the global scope. */
5448 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5451 if (context)
5453 if (TREE_CODE (context) == NAMESPACE_DECL)
5454 pop_nested_namespace (context);
5455 else
5456 pop_nested_class ();
5459 return friend_type;
5462 /* Returns zero if TYPE cannot be completed later due to circularity.
5463 Otherwise returns one. */
5465 static int
5466 can_complete_type_without_circularity (tree type)
5468 if (type == NULL_TREE || type == error_mark_node)
5469 return 0;
5470 else if (COMPLETE_TYPE_P (type))
5471 return 1;
5472 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5473 return can_complete_type_without_circularity (TREE_TYPE (type));
5474 else if (CLASS_TYPE_P (type)
5475 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5476 return 0;
5477 else
5478 return 1;
5481 tree
5482 instantiate_class_template (tree type)
5484 tree template, args, pattern, t, member;
5485 tree typedecl;
5486 tree pbinfo;
5487 tree base_list;
5489 if (type == error_mark_node)
5490 return error_mark_node;
5492 if (TYPE_BEING_DEFINED (type)
5493 || COMPLETE_TYPE_P (type)
5494 || dependent_type_p (type))
5495 return type;
5497 /* Figure out which template is being instantiated. */
5498 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5499 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5501 /* Determine what specialization of the original template to
5502 instantiate. */
5503 t = most_specialized_class (type, template);
5504 if (t == error_mark_node)
5506 TYPE_BEING_DEFINED (type) = 1;
5507 return error_mark_node;
5509 else if (t)
5511 /* This TYPE is actually an instantiation of a partial
5512 specialization. We replace the innermost set of ARGS with
5513 the arguments appropriate for substitution. For example,
5514 given:
5516 template <class T> struct S {};
5517 template <class T> struct S<T*> {};
5519 and supposing that we are instantiating S<int*>, ARGS will
5520 presently be {int*} -- but we need {int}. */
5521 pattern = TREE_TYPE (t);
5522 args = TREE_PURPOSE (t);
5524 else
5526 pattern = TREE_TYPE (template);
5527 args = CLASSTYPE_TI_ARGS (type);
5530 /* If the template we're instantiating is incomplete, then clearly
5531 there's nothing we can do. */
5532 if (!COMPLETE_TYPE_P (pattern))
5533 return type;
5535 /* If we've recursively instantiated too many templates, stop. */
5536 if (! push_tinst_level (type))
5537 return type;
5539 /* Now we're really doing the instantiation. Mark the type as in
5540 the process of being defined. */
5541 TYPE_BEING_DEFINED (type) = 1;
5543 /* We may be in the middle of deferred access check. Disable
5544 it now. */
5545 push_deferring_access_checks (dk_no_deferred);
5547 push_to_top_level ();
5549 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5551 /* Set the input location to the template definition. This is needed
5552 if tsubsting causes an error. */
5553 typedecl = TYPE_MAIN_DECL (type);
5554 input_location = DECL_SOURCE_LOCATION (typedecl);
5555 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5557 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5558 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5559 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5560 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5561 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5562 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5563 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5564 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5565 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5566 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5567 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5568 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5569 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5570 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5571 if (ANON_AGGR_TYPE_P (pattern))
5572 SET_ANON_AGGR_TYPE_P (type);
5573 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5575 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5576 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5579 pbinfo = TYPE_BINFO (pattern);
5581 /* We should never instantiate a nested class before its enclosing
5582 class; we need to look up the nested class by name before we can
5583 instantiate it, and that lookup should instantiate the enclosing
5584 class. */
5585 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5586 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5587 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5589 base_list = NULL_TREE;
5590 if (BINFO_N_BASE_BINFOS (pbinfo))
5592 tree pbase_binfo;
5593 tree context = TYPE_CONTEXT (type);
5594 tree pushed_scope;
5595 int i;
5597 /* We must enter the scope containing the type, as that is where
5598 the accessibility of types named in dependent bases are
5599 looked up from. */
5600 pushed_scope = push_scope (context ? context : global_namespace);
5602 /* Substitute into each of the bases to determine the actual
5603 basetypes. */
5604 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5606 tree base;
5607 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5609 /* Substitute to figure out the base class. */
5610 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5611 if (base == error_mark_node)
5612 continue;
5614 base_list = tree_cons (access, base, base_list);
5615 if (BINFO_VIRTUAL_P (pbase_binfo))
5616 TREE_TYPE (base_list) = integer_type_node;
5619 /* The list is now in reverse order; correct that. */
5620 base_list = nreverse (base_list);
5622 if (pushed_scope)
5623 pop_scope (pushed_scope);
5625 /* Now call xref_basetypes to set up all the base-class
5626 information. */
5627 xref_basetypes (type, base_list);
5630 /* Now that our base classes are set up, enter the scope of the
5631 class, so that name lookups into base classes, etc. will work
5632 correctly. This is precisely analogous to what we do in
5633 begin_class_definition when defining an ordinary non-template
5634 class. */
5635 pushclass (type);
5637 /* Now members are processed in the order of declaration. */
5638 for (member = CLASSTYPE_DECL_LIST (pattern);
5639 member; member = TREE_CHAIN (member))
5641 tree t = TREE_VALUE (member);
5643 if (TREE_PURPOSE (member))
5645 if (TYPE_P (t))
5647 /* Build new CLASSTYPE_NESTED_UTDS. */
5649 tree newtag;
5650 bool class_template_p;
5652 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5653 && TYPE_LANG_SPECIFIC (t)
5654 && CLASSTYPE_IS_TEMPLATE (t));
5655 /* If the member is a class template, then -- even after
5656 substitution -- there may be dependent types in the
5657 template argument list for the class. We increment
5658 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5659 that function will assume that no types are dependent
5660 when outside of a template. */
5661 if (class_template_p)
5662 ++processing_template_decl;
5663 newtag = tsubst (t, args, tf_error, NULL_TREE);
5664 if (class_template_p)
5665 --processing_template_decl;
5666 if (newtag == error_mark_node)
5667 continue;
5669 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5671 tree name = TYPE_IDENTIFIER (t);
5673 if (class_template_p)
5674 /* Unfortunately, lookup_template_class sets
5675 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5676 instantiation (i.e., for the type of a member
5677 template class nested within a template class.)
5678 This behavior is required for
5679 maybe_process_partial_specialization to work
5680 correctly, but is not accurate in this case;
5681 the TAG is not an instantiation of anything.
5682 (The corresponding TEMPLATE_DECL is an
5683 instantiation, but the TYPE is not.) */
5684 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5686 /* Now, we call pushtag to put this NEWTAG into the scope of
5687 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5688 pushtag calling push_template_decl. We don't have to do
5689 this for enums because it will already have been done in
5690 tsubst_enum. */
5691 if (name)
5692 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5693 pushtag (name, newtag, /*tag_scope=*/ts_current);
5696 else if (TREE_CODE (t) == FUNCTION_DECL
5697 || DECL_FUNCTION_TEMPLATE_P (t))
5699 /* Build new TYPE_METHODS. */
5700 tree r;
5702 if (TREE_CODE (t) == TEMPLATE_DECL)
5703 ++processing_template_decl;
5704 r = tsubst (t, args, tf_error, NULL_TREE);
5705 if (TREE_CODE (t) == TEMPLATE_DECL)
5706 --processing_template_decl;
5707 set_current_access_from_decl (r);
5708 grok_special_member_properties (r);
5709 finish_member_declaration (r);
5711 else
5713 /* Build new TYPE_FIELDS. */
5715 if (TREE_CODE (t) != CONST_DECL)
5717 tree r;
5719 /* The the file and line for this declaration, to
5720 assist in error message reporting. Since we
5721 called push_tinst_level above, we don't need to
5722 restore these. */
5723 input_location = DECL_SOURCE_LOCATION (t);
5725 if (TREE_CODE (t) == TEMPLATE_DECL)
5726 ++processing_template_decl;
5727 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5728 if (TREE_CODE (t) == TEMPLATE_DECL)
5729 --processing_template_decl;
5730 if (TREE_CODE (r) == VAR_DECL)
5732 /* In [temp.inst]:
5734 [t]he initialization (and any associated
5735 side-effects) of a static data member does
5736 not occur unless the static data member is
5737 itself used in a way that requires the
5738 definition of the static data member to
5739 exist.
5741 Therefore, we do not substitute into the
5742 initialized for the static data member here. */
5743 finish_static_data_member_decl
5744 (r,
5745 /*init=*/NULL_TREE,
5746 /*asmspec_tree=*/NULL_TREE,
5747 /*flags=*/0);
5748 if (DECL_INITIALIZED_IN_CLASS_P (r))
5749 check_static_variable_definition (r, TREE_TYPE (r));
5751 else if (TREE_CODE (r) == FIELD_DECL)
5753 /* Determine whether R has a valid type and can be
5754 completed later. If R is invalid, then it is
5755 replaced by error_mark_node so that it will not be
5756 added to TYPE_FIELDS. */
5757 tree rtype = TREE_TYPE (r);
5758 if (can_complete_type_without_circularity (rtype))
5759 complete_type (rtype);
5761 if (!COMPLETE_TYPE_P (rtype))
5763 cxx_incomplete_type_error (r, rtype);
5764 r = error_mark_node;
5768 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5769 such a thing will already have been added to the field
5770 list by tsubst_enum in finish_member_declaration in the
5771 CLASSTYPE_NESTED_UTDS case above. */
5772 if (!(TREE_CODE (r) == TYPE_DECL
5773 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5774 && DECL_ARTIFICIAL (r)))
5776 set_current_access_from_decl (r);
5777 finish_member_declaration (r);
5782 else
5784 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5786 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5788 tree friend_type = t;
5789 bool adjust_processing_template_decl = false;
5791 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5793 /* template <class T> friend class C; */
5794 friend_type = tsubst_friend_class (friend_type, args);
5795 adjust_processing_template_decl = true;
5797 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5799 /* template <class T> friend class C::D; */
5800 friend_type = tsubst (friend_type, args,
5801 tf_warning_or_error, NULL_TREE);
5802 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5803 friend_type = TREE_TYPE (friend_type);
5804 adjust_processing_template_decl = true;
5806 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5808 /* This could be either
5810 friend class T::C;
5812 when dependent_type_p is false or
5814 template <class U> friend class T::C;
5816 otherwise. */
5817 friend_type = tsubst (friend_type, args,
5818 tf_warning_or_error, NULL_TREE);
5819 /* Bump processing_template_decl for correct
5820 dependent_type_p calculation. */
5821 ++processing_template_decl;
5822 if (dependent_type_p (friend_type))
5823 adjust_processing_template_decl = true;
5824 --processing_template_decl;
5826 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5827 && hidden_name_p (TYPE_NAME (friend_type)))
5829 /* friend class C;
5831 where C hasn't been declared yet. Let's lookup name
5832 from namespace scope directly, bypassing any name that
5833 come from dependent base class. */
5834 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5836 /* The call to xref_tag_from_type does injection for friend
5837 classes. */
5838 push_nested_namespace (ns);
5839 friend_type =
5840 xref_tag_from_type (friend_type, NULL_TREE,
5841 /*tag_scope=*/ts_current);
5842 pop_nested_namespace (ns);
5844 else if (uses_template_parms (friend_type))
5845 /* friend class C<T>; */
5846 friend_type = tsubst (friend_type, args,
5847 tf_warning_or_error, NULL_TREE);
5848 /* Otherwise it's
5850 friend class C;
5852 where C is already declared or
5854 friend class C<int>;
5856 We don't have to do anything in these cases. */
5858 if (adjust_processing_template_decl)
5859 /* Trick make_friend_class into realizing that the friend
5860 we're adding is a template, not an ordinary class. It's
5861 important that we use make_friend_class since it will
5862 perform some error-checking and output cross-reference
5863 information. */
5864 ++processing_template_decl;
5866 if (friend_type != error_mark_node)
5867 make_friend_class (type, friend_type, /*complain=*/false);
5869 if (adjust_processing_template_decl)
5870 --processing_template_decl;
5872 else
5874 /* Build new DECL_FRIENDLIST. */
5875 tree r;
5877 /* The the file and line for this declaration, to
5878 assist in error message reporting. Since we
5879 called push_tinst_level above, we don't need to
5880 restore these. */
5881 input_location = DECL_SOURCE_LOCATION (t);
5883 if (TREE_CODE (t) == TEMPLATE_DECL)
5885 ++processing_template_decl;
5886 push_deferring_access_checks (dk_no_check);
5889 r = tsubst_friend_function (t, args);
5890 add_friend (type, r, /*complain=*/false);
5891 if (TREE_CODE (t) == TEMPLATE_DECL)
5893 pop_deferring_access_checks ();
5894 --processing_template_decl;
5900 /* Set the file and line number information to whatever is given for
5901 the class itself. This puts error messages involving generated
5902 implicit functions at a predictable point, and the same point
5903 that would be used for non-template classes. */
5904 input_location = DECL_SOURCE_LOCATION (typedecl);
5906 unreverse_member_declarations (type);
5907 finish_struct_1 (type);
5908 TYPE_BEING_DEFINED (type) = 0;
5910 /* Now that the class is complete, instantiate default arguments for
5911 any member functions. We don't do this earlier because the
5912 default arguments may reference members of the class. */
5913 if (!PRIMARY_TEMPLATE_P (template))
5914 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5915 if (TREE_CODE (t) == FUNCTION_DECL
5916 /* Implicitly generated member functions will not have template
5917 information; they are not instantiations, but instead are
5918 created "fresh" for each instantiation. */
5919 && DECL_TEMPLATE_INFO (t))
5920 tsubst_default_arguments (t);
5922 popclass ();
5923 pop_from_top_level ();
5924 pop_deferring_access_checks ();
5925 pop_tinst_level ();
5927 /* The vtable for a template class can be emitted in any translation
5928 unit in which the class is instantiated. When there is no key
5929 method, however, finish_struct_1 will already have added TYPE to
5930 the keyed_classes list. */
5931 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5932 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5934 return type;
5937 static tree
5938 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5940 tree r;
5942 if (!t)
5943 r = t;
5944 else if (TYPE_P (t))
5945 r = tsubst (t, args, complain, in_decl);
5946 else
5948 r = tsubst_expr (t, args, complain, in_decl);
5949 r = fold_non_dependent_expr (r);
5951 return r;
5954 /* Substitute ARGS into the vector or list of template arguments T. */
5956 static tree
5957 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5959 int len = TREE_VEC_LENGTH (t);
5960 int need_new = 0, i;
5961 tree *elts = (tree *) alloca (len * sizeof (tree));
5963 for (i = 0; i < len; i++)
5965 tree orig_arg = TREE_VEC_ELT (t, i);
5966 tree new_arg;
5968 if (TREE_CODE (orig_arg) == TREE_VEC)
5969 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5970 else
5971 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5973 if (new_arg == error_mark_node)
5974 return error_mark_node;
5976 elts[i] = new_arg;
5977 if (new_arg != orig_arg)
5978 need_new = 1;
5981 if (!need_new)
5982 return t;
5984 t = make_tree_vec (len);
5985 for (i = 0; i < len; i++)
5986 TREE_VEC_ELT (t, i) = elts[i];
5988 return t;
5991 /* Return the result of substituting ARGS into the template parameters
5992 given by PARMS. If there are m levels of ARGS and m + n levels of
5993 PARMS, then the result will contain n levels of PARMS. For
5994 example, if PARMS is `template <class T> template <class U>
5995 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5996 result will be `template <int*, double, class V>'. */
5998 static tree
5999 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6001 tree r = NULL_TREE;
6002 tree* new_parms;
6004 /* When substituting into a template, we must set
6005 PROCESSING_TEMPLATE_DECL as the template parameters may be
6006 dependent if they are based on one-another, and the dependency
6007 predicates are short-circuit outside of templates. */
6008 ++processing_template_decl;
6010 for (new_parms = &r;
6011 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6012 new_parms = &(TREE_CHAIN (*new_parms)),
6013 parms = TREE_CHAIN (parms))
6015 tree new_vec =
6016 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6017 int i;
6019 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6021 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6022 tree default_value = TREE_PURPOSE (tuple);
6023 tree parm_decl = TREE_VALUE (tuple);
6025 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6026 if (TREE_CODE (parm_decl) == PARM_DECL
6027 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6028 parm_decl = error_mark_node;
6029 default_value = tsubst_template_arg (default_value, args,
6030 complain, NULL_TREE);
6032 tuple = build_tree_list (default_value, parm_decl);
6033 TREE_VEC_ELT (new_vec, i) = tuple;
6036 *new_parms =
6037 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6038 - TMPL_ARGS_DEPTH (args)),
6039 new_vec, NULL_TREE);
6042 --processing_template_decl;
6044 return r;
6047 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6048 type T. If T is not an aggregate or enumeration type, it is
6049 handled as if by tsubst. IN_DECL is as for tsubst. If
6050 ENTERING_SCOPE is nonzero, T is the context for a template which
6051 we are presently tsubst'ing. Return the substituted value. */
6053 static tree
6054 tsubst_aggr_type (tree t,
6055 tree args,
6056 tsubst_flags_t complain,
6057 tree in_decl,
6058 int entering_scope)
6060 if (t == NULL_TREE)
6061 return NULL_TREE;
6063 switch (TREE_CODE (t))
6065 case RECORD_TYPE:
6066 if (TYPE_PTRMEMFUNC_P (t))
6067 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6069 /* Else fall through. */
6070 case ENUMERAL_TYPE:
6071 case UNION_TYPE:
6072 if (TYPE_TEMPLATE_INFO (t))
6074 tree argvec;
6075 tree context;
6076 tree r;
6077 bool saved_skip_evaluation;
6079 /* In "sizeof(X<I>)" we need to evaluate "I". */
6080 saved_skip_evaluation = skip_evaluation;
6081 skip_evaluation = false;
6083 /* First, determine the context for the type we are looking
6084 up. */
6085 context = TYPE_CONTEXT (t);
6086 if (context)
6087 context = tsubst_aggr_type (context, args, complain,
6088 in_decl, /*entering_scope=*/1);
6090 /* Then, figure out what arguments are appropriate for the
6091 type we are trying to find. For example, given:
6093 template <class T> struct S;
6094 template <class T, class U> void f(T, U) { S<U> su; }
6096 and supposing that we are instantiating f<int, double>,
6097 then our ARGS will be {int, double}, but, when looking up
6098 S we only want {double}. */
6099 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6100 complain, in_decl);
6101 if (argvec == error_mark_node)
6102 r = error_mark_node;
6103 else
6105 r = lookup_template_class (t, argvec, in_decl, context,
6106 entering_scope, complain);
6107 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6110 skip_evaluation = saved_skip_evaluation;
6112 return r;
6114 else
6115 /* This is not a template type, so there's nothing to do. */
6116 return t;
6118 default:
6119 return tsubst (t, args, complain, in_decl);
6123 /* Substitute into the default argument ARG (a default argument for
6124 FN), which has the indicated TYPE. */
6126 tree
6127 tsubst_default_argument (tree fn, tree type, tree arg)
6129 tree saved_class_ptr = NULL_TREE;
6130 tree saved_class_ref = NULL_TREE;
6132 /* This default argument came from a template. Instantiate the
6133 default argument here, not in tsubst. In the case of
6134 something like:
6136 template <class T>
6137 struct S {
6138 static T t();
6139 void f(T = t());
6142 we must be careful to do name lookup in the scope of S<T>,
6143 rather than in the current class. */
6144 push_access_scope (fn);
6145 /* The "this" pointer is not valid in a default argument. */
6146 if (cfun)
6148 saved_class_ptr = current_class_ptr;
6149 cp_function_chain->x_current_class_ptr = NULL_TREE;
6150 saved_class_ref = current_class_ref;
6151 cp_function_chain->x_current_class_ref = NULL_TREE;
6154 push_deferring_access_checks(dk_no_deferred);
6155 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6156 tf_warning_or_error, NULL_TREE);
6157 pop_deferring_access_checks();
6159 /* Restore the "this" pointer. */
6160 if (cfun)
6162 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6163 cp_function_chain->x_current_class_ref = saved_class_ref;
6166 pop_access_scope (fn);
6168 /* Make sure the default argument is reasonable. */
6169 arg = check_default_argument (type, arg);
6171 return arg;
6174 /* Substitute into all the default arguments for FN. */
6176 static void
6177 tsubst_default_arguments (tree fn)
6179 tree arg;
6180 tree tmpl_args;
6182 tmpl_args = DECL_TI_ARGS (fn);
6184 /* If this function is not yet instantiated, we certainly don't need
6185 its default arguments. */
6186 if (uses_template_parms (tmpl_args))
6187 return;
6189 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6190 arg;
6191 arg = TREE_CHAIN (arg))
6192 if (TREE_PURPOSE (arg))
6193 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6194 TREE_VALUE (arg),
6195 TREE_PURPOSE (arg));
6198 /* Substitute the ARGS into the T, which is a _DECL. Return the
6199 result of the substitution. Issue error and warning messages under
6200 control of COMPLAIN. */
6202 static tree
6203 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6205 location_t saved_loc;
6206 tree r = NULL_TREE;
6207 tree in_decl = t;
6209 /* Set the filename and linenumber to improve error-reporting. */
6210 saved_loc = input_location;
6211 input_location = DECL_SOURCE_LOCATION (t);
6213 switch (TREE_CODE (t))
6215 case TEMPLATE_DECL:
6217 /* We can get here when processing a member function template,
6218 member class template, and template template parameter of
6219 a template class. */
6220 tree decl = DECL_TEMPLATE_RESULT (t);
6221 tree spec;
6222 tree tmpl_args;
6223 tree full_args;
6225 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6227 /* Template template parameter is treated here. */
6228 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6229 if (new_type == error_mark_node)
6230 return error_mark_node;
6232 r = copy_decl (t);
6233 TREE_CHAIN (r) = NULL_TREE;
6234 TREE_TYPE (r) = new_type;
6235 DECL_TEMPLATE_RESULT (r)
6236 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6237 DECL_TEMPLATE_PARMS (r)
6238 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6239 complain);
6240 TYPE_NAME (new_type) = r;
6241 break;
6244 /* We might already have an instance of this template.
6245 The ARGS are for the surrounding class type, so the
6246 full args contain the tsubst'd args for the context,
6247 plus the innermost args from the template decl. */
6248 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6249 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6250 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6251 /* Because this is a template, the arguments will still be
6252 dependent, even after substitution. If
6253 PROCESSING_TEMPLATE_DECL is not set, the dependency
6254 predicates will short-circuit. */
6255 ++processing_template_decl;
6256 full_args = tsubst_template_args (tmpl_args, args,
6257 complain, in_decl);
6258 --processing_template_decl;
6259 if (full_args == error_mark_node)
6260 return error_mark_node;
6262 /* tsubst_template_args doesn't copy the vector if
6263 nothing changed. But, *something* should have
6264 changed. */
6265 gcc_assert (full_args != tmpl_args);
6267 spec = retrieve_specialization (t, full_args,
6268 /*class_specializations_p=*/true);
6269 if (spec != NULL_TREE)
6271 r = spec;
6272 break;
6275 /* Make a new template decl. It will be similar to the
6276 original, but will record the current template arguments.
6277 We also create a new function declaration, which is just
6278 like the old one, but points to this new template, rather
6279 than the old one. */
6280 r = copy_decl (t);
6281 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6282 TREE_CHAIN (r) = NULL_TREE;
6284 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6286 if (TREE_CODE (decl) == TYPE_DECL)
6288 tree new_type;
6289 ++processing_template_decl;
6290 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6291 --processing_template_decl;
6292 if (new_type == error_mark_node)
6293 return error_mark_node;
6295 TREE_TYPE (r) = new_type;
6296 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6297 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6298 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6299 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6301 else
6303 tree new_decl;
6304 ++processing_template_decl;
6305 new_decl = tsubst (decl, args, complain, in_decl);
6306 --processing_template_decl;
6307 if (new_decl == error_mark_node)
6308 return error_mark_node;
6310 DECL_TEMPLATE_RESULT (r) = new_decl;
6311 DECL_TI_TEMPLATE (new_decl) = r;
6312 TREE_TYPE (r) = TREE_TYPE (new_decl);
6313 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6314 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6317 SET_DECL_IMPLICIT_INSTANTIATION (r);
6318 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6319 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6321 /* The template parameters for this new template are all the
6322 template parameters for the old template, except the
6323 outermost level of parameters. */
6324 DECL_TEMPLATE_PARMS (r)
6325 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6326 complain);
6328 if (PRIMARY_TEMPLATE_P (t))
6329 DECL_PRIMARY_TEMPLATE (r) = r;
6331 if (TREE_CODE (decl) != TYPE_DECL)
6332 /* Record this non-type partial instantiation. */
6333 register_specialization (r, t,
6334 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6335 false);
6337 break;
6339 case FUNCTION_DECL:
6341 tree ctx;
6342 tree argvec = NULL_TREE;
6343 tree *friends;
6344 tree gen_tmpl;
6345 tree type;
6346 int member;
6347 int args_depth;
6348 int parms_depth;
6350 /* Nobody should be tsubst'ing into non-template functions. */
6351 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6353 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6355 tree spec;
6356 bool dependent_p;
6358 /* If T is not dependent, just return it. We have to
6359 increment PROCESSING_TEMPLATE_DECL because
6360 value_dependent_expression_p assumes that nothing is
6361 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6362 ++processing_template_decl;
6363 dependent_p = value_dependent_expression_p (t);
6364 --processing_template_decl;
6365 if (!dependent_p)
6366 return t;
6368 /* Calculate the most general template of which R is a
6369 specialization, and the complete set of arguments used to
6370 specialize R. */
6371 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6372 argvec = tsubst_template_args (DECL_TI_ARGS
6373 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6374 args, complain, in_decl);
6376 /* Check to see if we already have this specialization. */
6377 spec = retrieve_specialization (gen_tmpl, argvec,
6378 /*class_specializations_p=*/false);
6380 if (spec)
6382 r = spec;
6383 break;
6386 /* We can see more levels of arguments than parameters if
6387 there was a specialization of a member template, like
6388 this:
6390 template <class T> struct S { template <class U> void f(); }
6391 template <> template <class U> void S<int>::f(U);
6393 Here, we'll be substituting into the specialization,
6394 because that's where we can find the code we actually
6395 want to generate, but we'll have enough arguments for
6396 the most general template.
6398 We also deal with the peculiar case:
6400 template <class T> struct S {
6401 template <class U> friend void f();
6403 template <class U> void f() {}
6404 template S<int>;
6405 template void f<double>();
6407 Here, the ARGS for the instantiation of will be {int,
6408 double}. But, we only need as many ARGS as there are
6409 levels of template parameters in CODE_PATTERN. We are
6410 careful not to get fooled into reducing the ARGS in
6411 situations like:
6413 template <class T> struct S { template <class U> void f(U); }
6414 template <class T> template <> void S<T>::f(int) {}
6416 which we can spot because the pattern will be a
6417 specialization in this case. */
6418 args_depth = TMPL_ARGS_DEPTH (args);
6419 parms_depth =
6420 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6421 if (args_depth > parms_depth
6422 && !DECL_TEMPLATE_SPECIALIZATION (t))
6423 args = get_innermost_template_args (args, parms_depth);
6425 else
6427 /* This special case arises when we have something like this:
6429 template <class T> struct S {
6430 friend void f<int>(int, double);
6433 Here, the DECL_TI_TEMPLATE for the friend declaration
6434 will be an IDENTIFIER_NODE. We are being called from
6435 tsubst_friend_function, and we want only to create a
6436 new decl (R) with appropriate types so that we can call
6437 determine_specialization. */
6438 gen_tmpl = NULL_TREE;
6441 if (DECL_CLASS_SCOPE_P (t))
6443 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6444 member = 2;
6445 else
6446 member = 1;
6447 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6448 complain, t, /*entering_scope=*/1);
6450 else
6452 member = 0;
6453 ctx = DECL_CONTEXT (t);
6455 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6456 if (type == error_mark_node)
6457 return error_mark_node;
6459 /* We do NOT check for matching decls pushed separately at this
6460 point, as they may not represent instantiations of this
6461 template, and in any case are considered separate under the
6462 discrete model. */
6463 r = copy_decl (t);
6464 DECL_USE_TEMPLATE (r) = 0;
6465 TREE_TYPE (r) = type;
6466 /* Clear out the mangled name and RTL for the instantiation. */
6467 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6468 SET_DECL_RTL (r, NULL_RTX);
6469 DECL_INITIAL (r) = NULL_TREE;
6470 DECL_CONTEXT (r) = ctx;
6472 if (member && DECL_CONV_FN_P (r))
6473 /* Type-conversion operator. Reconstruct the name, in
6474 case it's the name of one of the template's parameters. */
6475 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6477 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6478 complain, t);
6479 DECL_RESULT (r) = NULL_TREE;
6481 TREE_STATIC (r) = 0;
6482 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6483 DECL_EXTERNAL (r) = 1;
6484 /* If this is an instantiation of a function with internal
6485 linkage, we already know what object file linkage will be
6486 assigned to the instantiation. */
6487 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6488 DECL_DEFER_OUTPUT (r) = 0;
6489 TREE_CHAIN (r) = NULL_TREE;
6490 DECL_PENDING_INLINE_INFO (r) = 0;
6491 DECL_PENDING_INLINE_P (r) = 0;
6492 DECL_SAVED_TREE (r) = NULL_TREE;
6493 TREE_USED (r) = 0;
6494 if (DECL_CLONED_FUNCTION (r))
6496 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6497 args, complain, t);
6498 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6499 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6502 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6503 this in the special friend case mentioned above where
6504 GEN_TMPL is NULL. */
6505 if (gen_tmpl)
6507 DECL_TEMPLATE_INFO (r)
6508 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6509 SET_DECL_IMPLICIT_INSTANTIATION (r);
6510 register_specialization (r, gen_tmpl, argvec, false);
6512 /* We're not supposed to instantiate default arguments
6513 until they are called, for a template. But, for a
6514 declaration like:
6516 template <class T> void f ()
6517 { extern void g(int i = T()); }
6519 we should do the substitution when the template is
6520 instantiated. We handle the member function case in
6521 instantiate_class_template since the default arguments
6522 might refer to other members of the class. */
6523 if (!member
6524 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6525 && !uses_template_parms (argvec))
6526 tsubst_default_arguments (r);
6528 else
6529 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6531 /* Copy the list of befriending classes. */
6532 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6533 *friends;
6534 friends = &TREE_CHAIN (*friends))
6536 *friends = copy_node (*friends);
6537 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6538 args, complain,
6539 in_decl);
6542 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6544 maybe_retrofit_in_chrg (r);
6545 if (DECL_CONSTRUCTOR_P (r))
6546 grok_ctor_properties (ctx, r);
6547 /* If this is an instantiation of a member template, clone it.
6548 If it isn't, that'll be handled by
6549 clone_constructors_and_destructors. */
6550 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6551 clone_function_decl (r, /*update_method_vec_p=*/0);
6553 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6554 grok_op_properties (r, (complain & tf_error) != 0);
6556 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6557 SET_DECL_FRIEND_CONTEXT (r,
6558 tsubst (DECL_FRIEND_CONTEXT (t),
6559 args, complain, in_decl));
6561 break;
6563 case PARM_DECL:
6565 tree type;
6567 r = copy_node (t);
6568 if (DECL_TEMPLATE_PARM_P (t))
6569 SET_DECL_TEMPLATE_PARM_P (r);
6571 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6572 type = type_decays_to (type);
6573 TREE_TYPE (r) = type;
6574 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6576 if (DECL_INITIAL (r))
6578 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6579 DECL_INITIAL (r) = TREE_TYPE (r);
6580 else
6581 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6582 complain, in_decl);
6585 DECL_CONTEXT (r) = NULL_TREE;
6587 if (!DECL_TEMPLATE_PARM_P (r))
6588 DECL_ARG_TYPE (r) = type_passed_as (type);
6589 if (TREE_CHAIN (t))
6590 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6591 complain, TREE_CHAIN (t));
6593 break;
6595 case FIELD_DECL:
6597 tree type;
6599 r = copy_decl (t);
6600 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6601 if (type == error_mark_node)
6602 return error_mark_node;
6603 TREE_TYPE (r) = type;
6604 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6606 /* We don't have to set DECL_CONTEXT here; it is set by
6607 finish_member_declaration. */
6608 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6609 complain, in_decl);
6610 TREE_CHAIN (r) = NULL_TREE;
6611 if (VOID_TYPE_P (type))
6612 error ("instantiation of %q+D as type %qT", r, type);
6614 break;
6616 case USING_DECL:
6617 /* We reach here only for member using decls. */
6618 if (DECL_DEPENDENT_P (t))
6620 r = do_class_using_decl
6621 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6622 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6623 if (!r)
6624 r = error_mark_node;
6626 else
6628 r = copy_node (t);
6629 TREE_CHAIN (r) = NULL_TREE;
6631 break;
6633 case TYPE_DECL:
6634 case VAR_DECL:
6636 tree argvec = NULL_TREE;
6637 tree gen_tmpl = NULL_TREE;
6638 tree spec;
6639 tree tmpl = NULL_TREE;
6640 tree ctx;
6641 tree type = NULL_TREE;
6642 int local_p;
6644 if (TREE_CODE (t) == TYPE_DECL)
6646 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6647 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6648 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6650 /* If this is the canonical decl, we don't have to
6651 mess with instantiations, and often we can't (for
6652 typename, template type parms and such). Note that
6653 TYPE_NAME is not correct for the above test if
6654 we've copied the type for a typedef. */
6655 r = TYPE_NAME (type);
6656 break;
6660 /* Assume this is a non-local variable. */
6661 local_p = 0;
6663 if (TYPE_P (CP_DECL_CONTEXT (t)))
6664 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6665 complain,
6666 in_decl, /*entering_scope=*/1);
6667 else if (DECL_NAMESPACE_SCOPE_P (t))
6668 ctx = DECL_CONTEXT (t);
6669 else
6671 /* Subsequent calls to pushdecl will fill this in. */
6672 ctx = NULL_TREE;
6673 local_p = 1;
6676 /* Check to see if we already have this specialization. */
6677 if (!local_p)
6679 tmpl = DECL_TI_TEMPLATE (t);
6680 gen_tmpl = most_general_template (tmpl);
6681 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6682 spec = retrieve_specialization (gen_tmpl, argvec,
6683 /*class_specializations_p=*/false);
6685 else
6686 spec = retrieve_local_specialization (t);
6688 if (spec)
6690 r = spec;
6691 break;
6694 r = copy_decl (t);
6695 if (TREE_CODE (r) == VAR_DECL)
6697 /* Even if the original location is out of scope, the
6698 newly substituted one is not. */
6699 DECL_DEAD_FOR_LOCAL (r) = 0;
6700 DECL_INITIALIZED_P (r) = 0;
6701 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6702 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6703 if (type == error_mark_node)
6704 return error_mark_node;
6705 type = complete_type (type);
6706 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6707 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6708 type = check_var_type (DECL_NAME (r), type);
6710 if (DECL_HAS_VALUE_EXPR_P (t))
6712 tree ve = DECL_VALUE_EXPR (t);
6713 ve = tsubst_expr (ve, args, complain, in_decl);
6714 SET_DECL_VALUE_EXPR (r, ve);
6717 else if (DECL_SELF_REFERENCE_P (t))
6718 SET_DECL_SELF_REFERENCE_P (r);
6719 TREE_TYPE (r) = type;
6720 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6721 DECL_CONTEXT (r) = ctx;
6722 /* Clear out the mangled name and RTL for the instantiation. */
6723 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6724 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6725 SET_DECL_RTL (r, NULL_RTX);
6726 /* The initializer must not be expanded until it is required;
6727 see [temp.inst]. */
6728 DECL_INITIAL (r) = NULL_TREE;
6729 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6730 SET_DECL_RTL (r, NULL_RTX);
6731 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6733 if (!local_p)
6735 /* A static data member declaration is always marked
6736 external when it is declared in-class, even if an
6737 initializer is present. We mimic the non-template
6738 processing here. */
6739 DECL_EXTERNAL (r) = 1;
6741 register_specialization (r, gen_tmpl, argvec, false);
6742 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6743 SET_DECL_IMPLICIT_INSTANTIATION (r);
6745 else
6746 register_local_specialization (r, t);
6748 TREE_CHAIN (r) = NULL_TREE;
6749 layout_decl (r, 0);
6751 break;
6753 default:
6754 gcc_unreachable ();
6757 /* Restore the file and line information. */
6758 input_location = saved_loc;
6760 return r;
6763 /* Substitute into the ARG_TYPES of a function type. */
6765 static tree
6766 tsubst_arg_types (tree arg_types,
6767 tree args,
6768 tsubst_flags_t complain,
6769 tree in_decl)
6771 tree remaining_arg_types;
6772 tree type;
6773 tree default_arg;
6774 tree result = NULL_TREE;
6776 if (!arg_types || arg_types == void_list_node)
6777 return arg_types;
6779 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6780 args, complain, in_decl);
6781 if (remaining_arg_types == error_mark_node)
6782 return error_mark_node;
6784 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6785 if (type == error_mark_node)
6786 return error_mark_node;
6787 if (VOID_TYPE_P (type))
6789 if (complain & tf_error)
6791 error ("invalid parameter type %qT", type);
6792 if (in_decl)
6793 error ("in declaration %q+D", in_decl);
6795 return error_mark_node;
6798 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6799 top-level qualifiers as required. */
6800 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6802 /* We do not substitute into default arguments here. The standard
6803 mandates that they be instantiated only when needed, which is
6804 done in build_over_call. */
6805 default_arg = TREE_PURPOSE (arg_types);
6807 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
6809 /* We've instantiated a template before its default arguments
6810 have been parsed. This can happen for a nested template
6811 class, and is not an error unless we require the default
6812 argument in a call of this function. */
6813 result = tree_cons (default_arg, type, remaining_arg_types);
6814 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
6816 else
6817 result = hash_tree_cons (default_arg, type, remaining_arg_types);
6819 return result;
6822 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6823 *not* handle the exception-specification for FNTYPE, because the
6824 initial substitution of explicitly provided template parameters
6825 during argument deduction forbids substitution into the
6826 exception-specification:
6828 [temp.deduct]
6830 All references in the function type of the function template to the
6831 corresponding template parameters are replaced by the specified tem-
6832 plate argument values. If a substitution in a template parameter or
6833 in the function type of the function template results in an invalid
6834 type, type deduction fails. [Note: The equivalent substitution in
6835 exception specifications is done only when the function is instanti-
6836 ated, at which point a program is ill-formed if the substitution
6837 results in an invalid type.] */
6839 static tree
6840 tsubst_function_type (tree t,
6841 tree args,
6842 tsubst_flags_t complain,
6843 tree in_decl)
6845 tree return_type;
6846 tree arg_types;
6847 tree fntype;
6849 /* The TYPE_CONTEXT is not used for function/method types. */
6850 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6852 /* Substitute the return type. */
6853 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6854 if (return_type == error_mark_node)
6855 return error_mark_node;
6856 /* The standard does not presently indicate that creation of a
6857 function type with an invalid return type is a deduction failure.
6858 However, that is clearly analogous to creating an array of "void"
6859 or a reference to a reference. This is core issue #486. */
6860 if (TREE_CODE (return_type) == ARRAY_TYPE
6861 || TREE_CODE (return_type) == FUNCTION_TYPE)
6863 if (complain & tf_error)
6865 if (TREE_CODE (return_type) == ARRAY_TYPE)
6866 error ("function returning an array");
6867 else
6868 error ("function returning a function");
6870 return error_mark_node;
6873 /* Substitute the argument types. */
6874 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6875 complain, in_decl);
6876 if (arg_types == error_mark_node)
6877 return error_mark_node;
6879 /* Construct a new type node and return it. */
6880 if (TREE_CODE (t) == FUNCTION_TYPE)
6881 fntype = build_function_type (return_type, arg_types);
6882 else
6884 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6885 if (! IS_AGGR_TYPE (r))
6887 /* [temp.deduct]
6889 Type deduction may fail for any of the following
6890 reasons:
6892 -- Attempting to create "pointer to member of T" when T
6893 is not a class type. */
6894 if (complain & tf_error)
6895 error ("creating pointer to member function of non-class type %qT",
6897 return error_mark_node;
6900 fntype = build_method_type_directly (r, return_type,
6901 TREE_CHAIN (arg_types));
6903 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6904 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6906 return fntype;
6909 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
6910 ARGS into that specification, and return the substituted
6911 specification. If there is no specification, return NULL_TREE. */
6913 static tree
6914 tsubst_exception_specification (tree fntype,
6915 tree args,
6916 tsubst_flags_t complain,
6917 tree in_decl)
6919 tree specs;
6920 tree new_specs;
6922 specs = TYPE_RAISES_EXCEPTIONS (fntype);
6923 new_specs = NULL_TREE;
6924 if (specs)
6926 if (! TREE_VALUE (specs))
6927 new_specs = specs;
6928 else
6929 while (specs)
6931 tree spec;
6932 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
6933 if (spec == error_mark_node)
6934 return spec;
6935 new_specs = add_exception_specifier (new_specs, spec, complain);
6936 specs = TREE_CHAIN (specs);
6939 return new_specs;
6942 /* Take the tree structure T and replace template parameters used
6943 therein with the argument vector ARGS. IN_DECL is an associated
6944 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6945 Issue error and warning messages under control of COMPLAIN. Note
6946 that we must be relatively non-tolerant of extensions here, in
6947 order to preserve conformance; if we allow substitutions that
6948 should not be allowed, we may allow argument deductions that should
6949 not succeed, and therefore report ambiguous overload situations
6950 where there are none. In theory, we could allow the substitution,
6951 but indicate that it should have failed, and allow our caller to
6952 make sure that the right thing happens, but we don't try to do this
6953 yet.
6955 This function is used for dealing with types, decls and the like;
6956 for expressions, use tsubst_expr or tsubst_copy. */
6958 static tree
6959 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6961 tree type, r;
6963 if (t == NULL_TREE || t == error_mark_node
6964 || t == integer_type_node
6965 || t == void_type_node
6966 || t == char_type_node
6967 || t == unknown_type_node
6968 || TREE_CODE (t) == NAMESPACE_DECL)
6969 return t;
6971 if (DECL_P (t))
6972 return tsubst_decl (t, args, complain);
6974 if (TREE_CODE (t) == IDENTIFIER_NODE)
6975 type = IDENTIFIER_TYPE_VALUE (t);
6976 else
6977 type = TREE_TYPE (t);
6979 gcc_assert (type != unknown_type_node);
6981 if (type
6982 && TREE_CODE (t) != TYPENAME_TYPE
6983 && TREE_CODE (t) != IDENTIFIER_NODE
6984 && TREE_CODE (t) != FUNCTION_TYPE
6985 && TREE_CODE (t) != METHOD_TYPE)
6986 type = tsubst (type, args, complain, in_decl);
6987 if (type == error_mark_node)
6988 return error_mark_node;
6990 switch (TREE_CODE (t))
6992 case RECORD_TYPE:
6993 case UNION_TYPE:
6994 case ENUMERAL_TYPE:
6995 return tsubst_aggr_type (t, args, complain, in_decl,
6996 /*entering_scope=*/0);
6998 case ERROR_MARK:
6999 case IDENTIFIER_NODE:
7000 case VOID_TYPE:
7001 case REAL_TYPE:
7002 case COMPLEX_TYPE:
7003 case VECTOR_TYPE:
7004 case BOOLEAN_TYPE:
7005 case INTEGER_CST:
7006 case REAL_CST:
7007 case STRING_CST:
7008 return t;
7010 case INTEGER_TYPE:
7011 if (t == integer_type_node)
7012 return t;
7014 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7015 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7016 return t;
7019 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7021 /* The array dimension behaves like a non-type template arg,
7022 in that we want to fold it as much as possible. */
7023 max = tsubst_template_arg (omax, args, complain, in_decl);
7024 max = fold_decl_constant_value (max);
7026 /* [temp.deduct]
7028 Type deduction may fail for any of the following
7029 reasons:
7031 Attempting to create an array with a size that is
7032 zero or negative. */
7033 if (integer_zerop (max) && !(complain & tf_error))
7034 /* We must fail if performing argument deduction (as
7035 indicated by the state of complain), so that
7036 another substitution can be found. */
7037 return error_mark_node;
7039 else if (TREE_CODE (max) == INTEGER_CST
7040 && INT_CST_LT (max, integer_zero_node))
7042 if (complain & tf_error)
7043 error ("creating array with negative size (%qE)", max);
7045 return error_mark_node;
7048 return compute_array_index_type (NULL_TREE, max);
7051 case TEMPLATE_TYPE_PARM:
7052 case TEMPLATE_TEMPLATE_PARM:
7053 case BOUND_TEMPLATE_TEMPLATE_PARM:
7054 case TEMPLATE_PARM_INDEX:
7056 int idx;
7057 int level;
7058 int levels;
7059 tree arg = NULL_TREE;
7061 r = NULL_TREE;
7063 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7064 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7065 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7066 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7068 idx = TEMPLATE_TYPE_IDX (t);
7069 level = TEMPLATE_TYPE_LEVEL (t);
7071 else
7073 idx = TEMPLATE_PARM_IDX (t);
7074 level = TEMPLATE_PARM_LEVEL (t);
7077 levels = TMPL_ARGS_DEPTH (args);
7078 if (level <= levels)
7079 arg = TMPL_ARG (args, level, idx);
7081 if (arg == error_mark_node)
7082 return error_mark_node;
7083 else if (arg != NULL_TREE)
7085 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7087 gcc_assert (TYPE_P (arg));
7088 return cp_build_qualified_type_real
7089 (arg, cp_type_quals (arg) | cp_type_quals (t),
7090 complain | tf_ignore_bad_quals);
7092 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7094 /* We are processing a type constructed from a
7095 template template parameter. */
7096 tree argvec = tsubst (TYPE_TI_ARGS (t),
7097 args, complain, in_decl);
7098 if (argvec == error_mark_node)
7099 return error_mark_node;
7101 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7102 are resolving nested-types in the signature of a
7103 member function templates. Otherwise ARG is a
7104 TEMPLATE_DECL and is the real template to be
7105 instantiated. */
7106 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7107 arg = TYPE_NAME (arg);
7109 r = lookup_template_class (arg,
7110 argvec, in_decl,
7111 DECL_CONTEXT (arg),
7112 /*entering_scope=*/0,
7113 complain);
7114 return cp_build_qualified_type_real
7115 (r, TYPE_QUALS (t), complain);
7117 else
7118 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7119 return arg;
7122 if (level == 1)
7123 /* This can happen during the attempted tsubst'ing in
7124 unify. This means that we don't yet have any information
7125 about the template parameter in question. */
7126 return t;
7128 /* If we get here, we must have been looking at a parm for a
7129 more deeply nested template. Make a new version of this
7130 template parameter, but with a lower level. */
7131 switch (TREE_CODE (t))
7133 case TEMPLATE_TYPE_PARM:
7134 case TEMPLATE_TEMPLATE_PARM:
7135 case BOUND_TEMPLATE_TEMPLATE_PARM:
7136 if (cp_type_quals (t))
7138 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7139 r = cp_build_qualified_type_real
7140 (r, cp_type_quals (t),
7141 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7142 ? tf_ignore_bad_quals : 0));
7144 else
7146 r = copy_type (t);
7147 TEMPLATE_TYPE_PARM_INDEX (r)
7148 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7149 r, levels);
7150 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7151 TYPE_MAIN_VARIANT (r) = r;
7152 TYPE_POINTER_TO (r) = NULL_TREE;
7153 TYPE_REFERENCE_TO (r) = NULL_TREE;
7155 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7157 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7158 complain, in_decl);
7159 if (argvec == error_mark_node)
7160 return error_mark_node;
7162 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7163 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7166 break;
7168 case TEMPLATE_PARM_INDEX:
7169 r = reduce_template_parm_level (t, type, levels);
7170 break;
7172 default:
7173 gcc_unreachable ();
7176 return r;
7179 case TREE_LIST:
7181 tree purpose, value, chain;
7183 if (t == void_list_node)
7184 return t;
7186 purpose = TREE_PURPOSE (t);
7187 if (purpose)
7189 purpose = tsubst (purpose, args, complain, in_decl);
7190 if (purpose == error_mark_node)
7191 return error_mark_node;
7193 value = TREE_VALUE (t);
7194 if (value)
7196 value = tsubst (value, args, complain, in_decl);
7197 if (value == error_mark_node)
7198 return error_mark_node;
7200 chain = TREE_CHAIN (t);
7201 if (chain && chain != void_type_node)
7203 chain = tsubst (chain, args, complain, in_decl);
7204 if (chain == error_mark_node)
7205 return error_mark_node;
7207 if (purpose == TREE_PURPOSE (t)
7208 && value == TREE_VALUE (t)
7209 && chain == TREE_CHAIN (t))
7210 return t;
7211 return hash_tree_cons (purpose, value, chain);
7214 case TREE_BINFO:
7215 /* We should never be tsubsting a binfo. */
7216 gcc_unreachable ();
7218 case TREE_VEC:
7219 /* A vector of template arguments. */
7220 gcc_assert (!type);
7221 return tsubst_template_args (t, args, complain, in_decl);
7223 case POINTER_TYPE:
7224 case REFERENCE_TYPE:
7226 enum tree_code code;
7228 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7229 return t;
7231 code = TREE_CODE (t);
7234 /* [temp.deduct]
7236 Type deduction may fail for any of the following
7237 reasons:
7239 -- Attempting to create a pointer to reference type.
7240 -- Attempting to create a reference to a reference type or
7241 a reference to void. */
7242 if (TREE_CODE (type) == REFERENCE_TYPE
7243 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7245 static location_t last_loc;
7247 /* We keep track of the last time we issued this error
7248 message to avoid spewing a ton of messages during a
7249 single bad template instantiation. */
7250 if (complain & tf_error
7251 #ifdef USE_MAPPED_LOCATION
7252 && last_loc != input_location
7253 #else
7254 && (last_loc.line != input_line
7255 || last_loc.file != input_filename)
7256 #endif
7259 if (TREE_CODE (type) == VOID_TYPE)
7260 error ("forming reference to void");
7261 else
7262 error ("forming %s to reference type %qT",
7263 (code == POINTER_TYPE) ? "pointer" : "reference",
7264 type);
7265 last_loc = input_location;
7268 return error_mark_node;
7270 else if (code == POINTER_TYPE)
7272 r = build_pointer_type (type);
7273 if (TREE_CODE (type) == METHOD_TYPE)
7274 r = build_ptrmemfunc_type (r);
7276 else
7277 r = build_reference_type (type);
7278 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7280 if (r != error_mark_node)
7281 /* Will this ever be needed for TYPE_..._TO values? */
7282 layout_type (r);
7284 return r;
7286 case OFFSET_TYPE:
7288 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7289 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7291 /* [temp.deduct]
7293 Type deduction may fail for any of the following
7294 reasons:
7296 -- Attempting to create "pointer to member of T" when T
7297 is not a class type. */
7298 if (complain & tf_error)
7299 error ("creating pointer to member of non-class type %qT", r);
7300 return error_mark_node;
7302 if (TREE_CODE (type) == REFERENCE_TYPE)
7304 if (complain & tf_error)
7305 error ("creating pointer to member reference type %qT", type);
7306 return error_mark_node;
7308 if (TREE_CODE (type) == VOID_TYPE)
7310 if (complain & tf_error)
7311 error ("creating pointer to member of type void");
7312 return error_mark_node;
7314 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7315 if (TREE_CODE (type) == FUNCTION_TYPE)
7317 /* The type of the implicit object parameter gets its
7318 cv-qualifiers from the FUNCTION_TYPE. */
7319 tree method_type;
7320 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7321 cp_type_quals (type));
7322 tree memptr;
7323 method_type = build_method_type_directly (this_type,
7324 TREE_TYPE (type),
7325 TYPE_ARG_TYPES (type));
7326 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7327 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7328 complain);
7330 else
7331 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7332 TYPE_QUALS (t),
7333 complain);
7335 case FUNCTION_TYPE:
7336 case METHOD_TYPE:
7338 tree fntype;
7339 tree specs;
7340 fntype = tsubst_function_type (t, args, complain, in_decl);
7341 if (fntype == error_mark_node)
7342 return error_mark_node;
7344 /* Substitute the exception specification. */
7345 specs = tsubst_exception_specification (t, args, complain,
7346 in_decl);
7347 if (specs == error_mark_node)
7348 return error_mark_node;
7349 if (specs)
7350 fntype = build_exception_variant (fntype, specs);
7351 return fntype;
7353 case ARRAY_TYPE:
7355 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7356 if (domain == error_mark_node)
7357 return error_mark_node;
7359 /* As an optimization, we avoid regenerating the array type if
7360 it will obviously be the same as T. */
7361 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7362 return t;
7364 /* These checks should match the ones in grokdeclarator.
7366 [temp.deduct]
7368 The deduction may fail for any of the following reasons:
7370 -- Attempting to create an array with an element type that
7371 is void, a function type, or a reference type, or [DR337]
7372 an abstract class type. */
7373 if (TREE_CODE (type) == VOID_TYPE
7374 || TREE_CODE (type) == FUNCTION_TYPE
7375 || TREE_CODE (type) == REFERENCE_TYPE)
7377 if (complain & tf_error)
7378 error ("creating array of %qT", type);
7379 return error_mark_node;
7381 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7383 if (complain & tf_error)
7384 error ("creating array of %qT, which is an abstract class type",
7385 type);
7386 return error_mark_node;
7389 r = build_cplus_array_type (type, domain);
7390 return r;
7393 case PLUS_EXPR:
7394 case MINUS_EXPR:
7396 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7397 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7399 if (e1 == error_mark_node || e2 == error_mark_node)
7400 return error_mark_node;
7402 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7405 case NEGATE_EXPR:
7406 case NOP_EXPR:
7408 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7409 if (e == error_mark_node)
7410 return error_mark_node;
7412 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7415 case TYPENAME_TYPE:
7417 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7418 in_decl, /*entering_scope=*/1);
7419 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7420 complain, in_decl);
7422 if (ctx == error_mark_node || f == error_mark_node)
7423 return error_mark_node;
7425 if (!IS_AGGR_TYPE (ctx))
7427 if (complain & tf_error)
7428 error ("%qT is not a class, struct, or union type", ctx);
7429 return error_mark_node;
7431 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7433 /* Normally, make_typename_type does not require that the CTX
7434 have complete type in order to allow things like:
7436 template <class T> struct S { typename S<T>::X Y; };
7438 But, such constructs have already been resolved by this
7439 point, so here CTX really should have complete type, unless
7440 it's a partial instantiation. */
7441 ctx = complete_type (ctx);
7442 if (!COMPLETE_TYPE_P (ctx))
7444 if (complain & tf_error)
7445 cxx_incomplete_type_error (NULL_TREE, ctx);
7446 return error_mark_node;
7450 f = make_typename_type (ctx, f, typename_type,
7451 (complain & tf_error) | tf_keep_type_decl);
7452 if (f == error_mark_node)
7453 return f;
7454 if (TREE_CODE (f) == TYPE_DECL)
7456 complain |= tf_ignore_bad_quals;
7457 f = TREE_TYPE (f);
7460 if (TREE_CODE (f) != TYPENAME_TYPE)
7462 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7463 error ("%qT resolves to %qT, which is not an enumeration type",
7464 t, f);
7465 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7466 error ("%qT resolves to %qT, which is is not a class type",
7467 t, f);
7470 return cp_build_qualified_type_real
7471 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7474 case UNBOUND_CLASS_TEMPLATE:
7476 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7477 in_decl, /*entering_scope=*/1);
7478 tree name = TYPE_IDENTIFIER (t);
7479 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7481 if (ctx == error_mark_node || name == error_mark_node)
7482 return error_mark_node;
7484 if (parm_list)
7485 parm_list = tsubst_template_parms (parm_list, args, complain);
7486 return make_unbound_class_template (ctx, name, parm_list, complain);
7489 case INDIRECT_REF:
7490 case ADDR_EXPR:
7491 case CALL_EXPR:
7492 gcc_unreachable ();
7494 case ARRAY_REF:
7496 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7497 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7498 if (e1 == error_mark_node || e2 == error_mark_node)
7499 return error_mark_node;
7501 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7504 case SCOPE_REF:
7506 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7507 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7508 if (e1 == error_mark_node || e2 == error_mark_node)
7509 return error_mark_node;
7511 return build_qualified_name (/*type=*/NULL_TREE,
7512 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7515 case TYPEOF_TYPE:
7517 tree type;
7519 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7520 complain, in_decl));
7521 return cp_build_qualified_type_real (type,
7522 cp_type_quals (t)
7523 | cp_type_quals (type),
7524 complain);
7527 default:
7528 sorry ("use of %qs in template",
7529 tree_code_name [(int) TREE_CODE (t)]);
7530 return error_mark_node;
7534 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7535 type of the expression on the left-hand side of the "." or "->"
7536 operator. */
7538 static tree
7539 tsubst_baselink (tree baselink, tree object_type,
7540 tree args, tsubst_flags_t complain, tree in_decl)
7542 tree name;
7543 tree qualifying_scope;
7544 tree fns;
7545 tree template_args = 0;
7546 bool template_id_p = false;
7548 /* A baselink indicates a function from a base class. The
7549 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7550 non-dependent types; otherwise, the lookup could not have
7551 succeeded. However, they may indicate bases of the template
7552 class, rather than the instantiated class.
7554 In addition, lookups that were not ambiguous before may be
7555 ambiguous now. Therefore, we perform the lookup again. */
7556 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7557 fns = BASELINK_FUNCTIONS (baselink);
7558 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7560 template_id_p = true;
7561 template_args = TREE_OPERAND (fns, 1);
7562 fns = TREE_OPERAND (fns, 0);
7563 if (template_args)
7564 template_args = tsubst_template_args (template_args, args,
7565 complain, in_decl);
7567 name = DECL_NAME (get_first_fn (fns));
7568 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7570 /* If lookup found a single function, mark it as used at this
7571 point. (If it lookup found multiple functions the one selected
7572 later by overload resolution will be marked as used at that
7573 point.) */
7574 if (BASELINK_P (baselink))
7575 fns = BASELINK_FUNCTIONS (baselink);
7576 if (!template_id_p && !really_overloaded_fn (fns))
7577 mark_used (OVL_CURRENT (fns));
7579 /* Add back the template arguments, if present. */
7580 if (BASELINK_P (baselink) && template_id_p)
7581 BASELINK_FUNCTIONS (baselink)
7582 = build_nt (TEMPLATE_ID_EXPR,
7583 BASELINK_FUNCTIONS (baselink),
7584 template_args);
7586 if (!object_type)
7587 object_type = current_class_type;
7588 return adjust_result_of_qualified_name_lookup (baselink,
7589 qualifying_scope,
7590 object_type);
7593 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7594 true if the qualified-id will be a postfix-expression in-and-of
7595 itself; false if more of the postfix-expression follows the
7596 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7597 of "&". */
7599 static tree
7600 tsubst_qualified_id (tree qualified_id, tree args,
7601 tsubst_flags_t complain, tree in_decl,
7602 bool done, bool address_p)
7604 tree expr;
7605 tree scope;
7606 tree name;
7607 bool is_template;
7608 tree template_args;
7610 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7612 /* Figure out what name to look up. */
7613 name = TREE_OPERAND (qualified_id, 1);
7614 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7616 is_template = true;
7617 template_args = TREE_OPERAND (name, 1);
7618 if (template_args)
7619 template_args = tsubst_template_args (template_args, args,
7620 complain, in_decl);
7621 name = TREE_OPERAND (name, 0);
7623 else
7625 is_template = false;
7626 template_args = NULL_TREE;
7629 /* Substitute into the qualifying scope. When there are no ARGS, we
7630 are just trying to simplify a non-dependent expression. In that
7631 case the qualifying scope may be dependent, and, in any case,
7632 substituting will not help. */
7633 scope = TREE_OPERAND (qualified_id, 0);
7634 if (args)
7636 scope = tsubst (scope, args, complain, in_decl);
7637 expr = tsubst_copy (name, args, complain, in_decl);
7639 else
7640 expr = name;
7642 if (dependent_type_p (scope))
7643 return build_qualified_name (/*type=*/NULL_TREE,
7644 scope, expr,
7645 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7647 if (!BASELINK_P (name) && !DECL_P (expr))
7649 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7650 /* If this were actually a destructor call, it would have been
7651 parsed as such by the parser. */
7652 expr = error_mark_node;
7653 else
7654 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7655 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7656 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7658 if (complain & tf_error)
7660 error ("dependent-name %qE is parsed as a non-type, but "
7661 "instantiation yields a type", qualified_id);
7662 inform ("say %<typename %E%> if a type is meant", qualified_id);
7664 return error_mark_node;
7668 if (DECL_P (expr))
7670 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7671 scope);
7672 /* Remember that there was a reference to this entity. */
7673 mark_used (expr);
7676 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7678 if (complain & tf_error)
7679 qualified_name_lookup_error (scope,
7680 TREE_OPERAND (qualified_id, 1),
7681 expr);
7682 return error_mark_node;
7685 if (is_template)
7686 expr = lookup_template_function (expr, template_args);
7688 if (expr == error_mark_node && complain & tf_error)
7689 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7690 expr);
7691 else if (TYPE_P (scope))
7693 expr = (adjust_result_of_qualified_name_lookup
7694 (expr, scope, current_class_type));
7695 expr = (finish_qualified_id_expr
7696 (scope, expr, done, address_p,
7697 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7698 /*template_arg_p=*/false));
7701 if (TREE_CODE (expr) != SCOPE_REF)
7702 expr = convert_from_reference (expr);
7704 return expr;
7707 /* Like tsubst, but deals with expressions. This function just replaces
7708 template parms; to finish processing the resultant expression, use
7709 tsubst_expr. */
7711 static tree
7712 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7714 enum tree_code code;
7715 tree r;
7717 if (t == NULL_TREE || t == error_mark_node)
7718 return t;
7720 code = TREE_CODE (t);
7722 switch (code)
7724 case PARM_DECL:
7725 r = retrieve_local_specialization (t);
7726 gcc_assert (r != NULL);
7727 mark_used (r);
7728 return r;
7730 case CONST_DECL:
7732 tree enum_type;
7733 tree v;
7735 if (DECL_TEMPLATE_PARM_P (t))
7736 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7737 /* There is no need to substitute into namespace-scope
7738 enumerators. */
7739 if (DECL_NAMESPACE_SCOPE_P (t))
7740 return t;
7741 /* If ARGS is NULL, then T is known to be non-dependent. */
7742 if (args == NULL_TREE)
7743 return integral_constant_value (t);
7745 /* Unfortunately, we cannot just call lookup_name here.
7746 Consider:
7748 template <int I> int f() {
7749 enum E { a = I };
7750 struct S { void g() { E e = a; } };
7753 When we instantiate f<7>::S::g(), say, lookup_name is not
7754 clever enough to find f<7>::a. */
7755 enum_type
7756 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7757 /*entering_scope=*/0);
7759 for (v = TYPE_VALUES (enum_type);
7760 v != NULL_TREE;
7761 v = TREE_CHAIN (v))
7762 if (TREE_PURPOSE (v) == DECL_NAME (t))
7763 return TREE_VALUE (v);
7765 /* We didn't find the name. That should never happen; if
7766 name-lookup found it during preliminary parsing, we
7767 should find it again here during instantiation. */
7768 gcc_unreachable ();
7770 return t;
7772 case FIELD_DECL:
7773 if (DECL_CONTEXT (t))
7775 tree ctx;
7777 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7778 /*entering_scope=*/1);
7779 if (ctx != DECL_CONTEXT (t))
7781 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7782 if (!r)
7784 if (complain & tf_error)
7785 error ("using invalid field %qD", t);
7786 return error_mark_node;
7788 return r;
7792 return t;
7794 case VAR_DECL:
7795 case FUNCTION_DECL:
7796 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7797 || local_variable_p (t))
7798 t = tsubst (t, args, complain, in_decl);
7799 mark_used (t);
7800 return t;
7802 case BASELINK:
7803 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7805 case TEMPLATE_DECL:
7806 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7807 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7808 args, complain, in_decl);
7809 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
7810 return tsubst (t, args, complain, in_decl);
7811 else if (DECL_CLASS_SCOPE_P (t)
7812 && uses_template_parms (DECL_CONTEXT (t)))
7814 /* Template template argument like the following example need
7815 special treatment:
7817 template <template <class> class TT> struct C {};
7818 template <class T> struct D {
7819 template <class U> struct E {};
7820 C<E> c; // #1
7822 D<int> d; // #2
7824 We are processing the template argument `E' in #1 for
7825 the template instantiation #2. Originally, `E' is a
7826 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7827 have to substitute this with one having context `D<int>'. */
7829 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7830 return lookup_field (context, DECL_NAME(t), 0, false);
7832 else
7833 /* Ordinary template template argument. */
7834 return t;
7836 case CAST_EXPR:
7837 case REINTERPRET_CAST_EXPR:
7838 case CONST_CAST_EXPR:
7839 case STATIC_CAST_EXPR:
7840 case DYNAMIC_CAST_EXPR:
7841 case NOP_EXPR:
7842 return build1
7843 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7844 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7846 case INDIRECT_REF:
7847 case NEGATE_EXPR:
7848 case TRUTH_NOT_EXPR:
7849 case BIT_NOT_EXPR:
7850 case ADDR_EXPR:
7851 case UNARY_PLUS_EXPR: /* Unary + */
7852 case SIZEOF_EXPR:
7853 case ALIGNOF_EXPR:
7854 case ARROW_EXPR:
7855 case THROW_EXPR:
7856 case TYPEID_EXPR:
7857 case REALPART_EXPR:
7858 case IMAGPART_EXPR:
7859 return build1
7860 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7861 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7863 case COMPONENT_REF:
7865 tree object;
7866 tree name;
7868 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7869 name = TREE_OPERAND (t, 1);
7870 if (TREE_CODE (name) == BIT_NOT_EXPR)
7872 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7873 complain, in_decl);
7874 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7876 else if (TREE_CODE (name) == SCOPE_REF
7877 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7879 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7880 complain, in_decl);
7881 name = TREE_OPERAND (name, 1);
7882 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7883 complain, in_decl);
7884 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7885 name = build_qualified_name (/*type=*/NULL_TREE,
7886 base, name,
7887 /*template_p=*/false);
7889 else if (TREE_CODE (name) == BASELINK)
7890 name = tsubst_baselink (name,
7891 non_reference (TREE_TYPE (object)),
7892 args, complain,
7893 in_decl);
7894 else
7895 name = tsubst_copy (name, args, complain, in_decl);
7896 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7899 case PLUS_EXPR:
7900 case MINUS_EXPR:
7901 case MULT_EXPR:
7902 case TRUNC_DIV_EXPR:
7903 case CEIL_DIV_EXPR:
7904 case FLOOR_DIV_EXPR:
7905 case ROUND_DIV_EXPR:
7906 case EXACT_DIV_EXPR:
7907 case BIT_AND_EXPR:
7908 case BIT_IOR_EXPR:
7909 case BIT_XOR_EXPR:
7910 case TRUNC_MOD_EXPR:
7911 case FLOOR_MOD_EXPR:
7912 case TRUTH_ANDIF_EXPR:
7913 case TRUTH_ORIF_EXPR:
7914 case TRUTH_AND_EXPR:
7915 case TRUTH_OR_EXPR:
7916 case RSHIFT_EXPR:
7917 case LSHIFT_EXPR:
7918 case RROTATE_EXPR:
7919 case LROTATE_EXPR:
7920 case EQ_EXPR:
7921 case NE_EXPR:
7922 case MAX_EXPR:
7923 case MIN_EXPR:
7924 case LE_EXPR:
7925 case GE_EXPR:
7926 case LT_EXPR:
7927 case GT_EXPR:
7928 case COMPOUND_EXPR:
7929 case DOTSTAR_EXPR:
7930 case MEMBER_REF:
7931 case PREDECREMENT_EXPR:
7932 case PREINCREMENT_EXPR:
7933 case POSTDECREMENT_EXPR:
7934 case POSTINCREMENT_EXPR:
7935 return build_nt
7936 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7937 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7939 case SCOPE_REF:
7940 return build_qualified_name (/*type=*/NULL_TREE,
7941 tsubst_copy (TREE_OPERAND (t, 0),
7942 args, complain, in_decl),
7943 tsubst_copy (TREE_OPERAND (t, 1),
7944 args, complain, in_decl),
7945 QUALIFIED_NAME_IS_TEMPLATE (t));
7947 case ARRAY_REF:
7948 return build_nt
7949 (ARRAY_REF,
7950 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7951 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7952 NULL_TREE, NULL_TREE);
7954 case CALL_EXPR:
7955 return build_nt (code,
7956 tsubst_copy (TREE_OPERAND (t, 0), args,
7957 complain, in_decl),
7958 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7959 in_decl),
7960 NULL_TREE);
7962 case COND_EXPR:
7963 case MODOP_EXPR:
7964 case PSEUDO_DTOR_EXPR:
7966 r = build_nt
7967 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7968 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7969 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7970 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7971 return r;
7974 case NEW_EXPR:
7976 r = build_nt
7977 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7978 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7979 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7980 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7981 return r;
7984 case DELETE_EXPR:
7986 r = build_nt
7987 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7988 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7989 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7990 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7991 return r;
7994 case TEMPLATE_ID_EXPR:
7996 /* Substituted template arguments */
7997 tree fn = TREE_OPERAND (t, 0);
7998 tree targs = TREE_OPERAND (t, 1);
8000 fn = tsubst_copy (fn, args, complain, in_decl);
8001 if (targs)
8002 targs = tsubst_template_args (targs, args, complain, in_decl);
8004 return lookup_template_function (fn, targs);
8007 case TREE_LIST:
8009 tree purpose, value, chain;
8011 if (t == void_list_node)
8012 return t;
8014 purpose = TREE_PURPOSE (t);
8015 if (purpose)
8016 purpose = tsubst_copy (purpose, args, complain, in_decl);
8017 value = TREE_VALUE (t);
8018 if (value)
8019 value = tsubst_copy (value, args, complain, in_decl);
8020 chain = TREE_CHAIN (t);
8021 if (chain && chain != void_type_node)
8022 chain = tsubst_copy (chain, args, complain, in_decl);
8023 if (purpose == TREE_PURPOSE (t)
8024 && value == TREE_VALUE (t)
8025 && chain == TREE_CHAIN (t))
8026 return t;
8027 return tree_cons (purpose, value, chain);
8030 case RECORD_TYPE:
8031 case UNION_TYPE:
8032 case ENUMERAL_TYPE:
8033 case INTEGER_TYPE:
8034 case TEMPLATE_TYPE_PARM:
8035 case TEMPLATE_TEMPLATE_PARM:
8036 case BOUND_TEMPLATE_TEMPLATE_PARM:
8037 case TEMPLATE_PARM_INDEX:
8038 case POINTER_TYPE:
8039 case REFERENCE_TYPE:
8040 case OFFSET_TYPE:
8041 case FUNCTION_TYPE:
8042 case METHOD_TYPE:
8043 case ARRAY_TYPE:
8044 case TYPENAME_TYPE:
8045 case UNBOUND_CLASS_TEMPLATE:
8046 case TYPEOF_TYPE:
8047 case TYPE_DECL:
8048 return tsubst (t, args, complain, in_decl);
8050 case IDENTIFIER_NODE:
8051 if (IDENTIFIER_TYPENAME_P (t))
8053 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8054 return mangle_conv_op_name_for_type (new_type);
8056 else
8057 return t;
8059 case CONSTRUCTOR:
8060 /* This is handled by tsubst_copy_and_build. */
8061 gcc_unreachable ();
8063 case VA_ARG_EXPR:
8064 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8065 in_decl),
8066 tsubst (TREE_TYPE (t), args, complain, in_decl));
8068 case CLEANUP_POINT_EXPR:
8069 /* We shouldn't have built any of these during initial template
8070 generation. Instead, they should be built during instantiation
8071 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8072 gcc_unreachable ();
8074 case OFFSET_REF:
8075 mark_used (TREE_OPERAND (t, 1));
8076 return t;
8078 default:
8079 return t;
8083 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8085 static tree
8086 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8087 tree in_decl)
8089 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8091 tree purpose, value, chain;
8093 if (t == NULL)
8094 return t;
8096 if (TREE_CODE (t) != TREE_LIST)
8097 return tsubst_copy_and_build (t, args, complain, in_decl,
8098 /*function_p=*/false);
8100 if (t == void_list_node)
8101 return t;
8103 purpose = TREE_PURPOSE (t);
8104 if (purpose)
8105 purpose = RECUR (purpose);
8106 value = TREE_VALUE (t);
8107 if (value)
8108 value = RECUR (value);
8109 chain = TREE_CHAIN (t);
8110 if (chain && chain != void_type_node)
8111 chain = RECUR (chain);
8112 return tree_cons (purpose, value, chain);
8113 #undef RECUR
8116 /* Like tsubst_copy for expressions, etc. but also does semantic
8117 processing. */
8119 static tree
8120 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8122 tree stmt, tmp;
8124 if (t == NULL_TREE || t == error_mark_node)
8125 return t;
8127 if (EXPR_HAS_LOCATION (t))
8128 input_location = EXPR_LOCATION (t);
8129 if (STATEMENT_CODE_P (TREE_CODE (t)))
8130 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8132 switch (TREE_CODE (t))
8134 case STATEMENT_LIST:
8136 tree_stmt_iterator i;
8137 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8138 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
8139 break;
8142 case CTOR_INITIALIZER:
8143 finish_mem_initializers (tsubst_initializer_list
8144 (TREE_OPERAND (t, 0), args));
8145 break;
8147 case RETURN_EXPR:
8148 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
8149 args, complain, in_decl));
8150 break;
8152 case EXPR_STMT:
8153 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
8154 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8155 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8156 else
8157 finish_expr_stmt (tmp);
8158 break;
8160 case USING_STMT:
8161 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
8162 args, complain, in_decl));
8163 break;
8165 case DECL_EXPR:
8167 tree decl;
8168 tree init;
8170 decl = DECL_EXPR_DECL (t);
8171 if (TREE_CODE (decl) == LABEL_DECL)
8172 finish_label_decl (DECL_NAME (decl));
8173 else if (TREE_CODE (decl) == USING_DECL)
8175 tree scope = USING_DECL_SCOPE (decl);
8176 tree name = DECL_NAME (decl);
8177 tree decl;
8179 scope = tsubst_expr (scope, args, complain, in_decl);
8180 decl = lookup_qualified_name (scope, name,
8181 /*is_type_p=*/false,
8182 /*complain=*/false);
8183 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8184 qualified_name_lookup_error (scope, name, decl);
8185 else
8186 do_local_using_decl (decl, scope, name);
8188 else
8190 init = DECL_INITIAL (decl);
8191 decl = tsubst (decl, args, complain, in_decl);
8192 if (decl != error_mark_node)
8194 if (init)
8195 DECL_INITIAL (decl) = error_mark_node;
8196 /* By marking the declaration as instantiated, we avoid
8197 trying to instantiate it. Since instantiate_decl can't
8198 handle local variables, and since we've already done
8199 all that needs to be done, that's the right thing to
8200 do. */
8201 if (TREE_CODE (decl) == VAR_DECL)
8202 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8203 if (TREE_CODE (decl) == VAR_DECL
8204 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8205 /* Anonymous aggregates are a special case. */
8206 finish_anon_union (decl);
8207 else
8209 maybe_push_decl (decl);
8210 if (TREE_CODE (decl) == VAR_DECL
8211 && DECL_PRETTY_FUNCTION_P (decl))
8213 /* For __PRETTY_FUNCTION__ we have to adjust the
8214 initializer. */
8215 const char *const name
8216 = cxx_printable_name (current_function_decl, 2);
8217 init = cp_fname_init (name, &TREE_TYPE (decl));
8219 else
8220 init = tsubst_expr (init, args, complain, in_decl);
8221 cp_finish_decl (decl, init, NULL_TREE, 0);
8226 /* A DECL_EXPR can also be used as an expression, in the condition
8227 clause of an if/for/while construct. */
8228 return decl;
8231 case FOR_STMT:
8232 stmt = begin_for_stmt ();
8233 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
8234 finish_for_init_stmt (stmt);
8235 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
8236 finish_for_cond (tmp, stmt);
8237 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
8238 finish_for_expr (tmp, stmt);
8239 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
8240 finish_for_stmt (stmt);
8241 break;
8243 case WHILE_STMT:
8244 stmt = begin_while_stmt ();
8245 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
8246 finish_while_stmt_cond (tmp, stmt);
8247 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
8248 finish_while_stmt (stmt);
8249 break;
8251 case DO_STMT:
8252 stmt = begin_do_stmt ();
8253 tsubst_expr (DO_BODY (t), args, complain, in_decl);
8254 finish_do_body (stmt);
8255 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
8256 finish_do_stmt (tmp, stmt);
8257 break;
8259 case IF_STMT:
8260 stmt = begin_if_stmt ();
8261 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
8262 finish_if_stmt_cond (tmp, stmt);
8263 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
8264 finish_then_clause (stmt);
8266 if (ELSE_CLAUSE (t))
8268 begin_else_clause (stmt);
8269 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
8270 finish_else_clause (stmt);
8273 finish_if_stmt (stmt);
8274 break;
8276 case BIND_EXPR:
8277 if (BIND_EXPR_BODY_BLOCK (t))
8278 stmt = begin_function_body ();
8279 else
8280 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8281 ? BCS_TRY_BLOCK : 0);
8283 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
8285 if (BIND_EXPR_BODY_BLOCK (t))
8286 finish_function_body (stmt);
8287 else
8288 finish_compound_stmt (stmt);
8289 break;
8291 case BREAK_STMT:
8292 finish_break_stmt ();
8293 break;
8295 case CONTINUE_STMT:
8296 finish_continue_stmt ();
8297 break;
8299 case SWITCH_STMT:
8300 stmt = begin_switch_stmt ();
8301 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
8302 finish_switch_cond (tmp, stmt);
8303 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
8304 finish_switch_stmt (stmt);
8305 break;
8307 case CASE_LABEL_EXPR:
8308 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
8309 tsubst_expr (CASE_HIGH (t), args, complain,
8310 in_decl));
8311 break;
8313 case LABEL_EXPR:
8314 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8315 break;
8317 case GOTO_EXPR:
8318 tmp = GOTO_DESTINATION (t);
8319 if (TREE_CODE (tmp) != LABEL_DECL)
8320 /* Computed goto's must be tsubst'd into. On the other hand,
8321 non-computed gotos must not be; the identifier in question
8322 will have no binding. */
8323 tmp = tsubst_expr (tmp, args, complain, in_decl);
8324 else
8325 tmp = DECL_NAME (tmp);
8326 finish_goto_stmt (tmp);
8327 break;
8329 case ASM_EXPR:
8330 tmp = finish_asm_stmt
8331 (ASM_VOLATILE_P (t),
8332 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8333 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8334 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8335 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8337 tree asm_expr = tmp;
8338 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8339 asm_expr = TREE_OPERAND (asm_expr, 0);
8340 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8342 break;
8344 case TRY_BLOCK:
8345 if (CLEANUP_P (t))
8347 stmt = begin_try_block ();
8348 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8349 finish_cleanup_try_block (stmt);
8350 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8351 complain, in_decl),
8352 stmt);
8354 else
8356 if (FN_TRY_BLOCK_P (t))
8357 stmt = begin_function_try_block ();
8358 else
8359 stmt = begin_try_block ();
8361 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8363 if (FN_TRY_BLOCK_P (t))
8364 finish_function_try_block (stmt);
8365 else
8366 finish_try_block (stmt);
8368 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8369 if (FN_TRY_BLOCK_P (t))
8370 finish_function_handler_sequence (stmt);
8371 else
8372 finish_handler_sequence (stmt);
8374 break;
8376 case HANDLER:
8378 tree decl;
8380 stmt = begin_handler ();
8381 if (HANDLER_PARMS (t))
8383 decl = HANDLER_PARMS (t);
8384 decl = tsubst (decl, args, complain, in_decl);
8385 /* Prevent instantiate_decl from trying to instantiate
8386 this variable. We've already done all that needs to be
8387 done. */
8388 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8390 else
8391 decl = NULL_TREE;
8392 finish_handler_parms (decl, stmt);
8393 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8394 finish_handler (stmt);
8396 break;
8398 case TAG_DEFN:
8399 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8400 break;
8402 default:
8403 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8405 return tsubst_copy_and_build (t, args, complain, in_decl,
8406 /*function_p=*/false);
8409 return NULL_TREE;
8412 /* T is a postfix-expression that is not being used in a function
8413 call. Return the substituted version of T. */
8415 static tree
8416 tsubst_non_call_postfix_expression (tree t, tree args,
8417 tsubst_flags_t complain,
8418 tree in_decl)
8420 if (TREE_CODE (t) == SCOPE_REF)
8421 t = tsubst_qualified_id (t, args, complain, in_decl,
8422 /*done=*/false, /*address_p=*/false);
8423 else
8424 t = tsubst_copy_and_build (t, args, complain, in_decl,
8425 /*function_p=*/false);
8427 return t;
8430 /* Like tsubst but deals with expressions and performs semantic
8431 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8433 tree
8434 tsubst_copy_and_build (tree t,
8435 tree args,
8436 tsubst_flags_t complain,
8437 tree in_decl,
8438 bool function_p)
8440 #define RECUR(NODE) \
8441 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8443 tree op1;
8445 if (t == NULL_TREE || t == error_mark_node)
8446 return t;
8448 switch (TREE_CODE (t))
8450 case USING_DECL:
8451 t = DECL_NAME (t);
8452 /* Fall through. */
8453 case IDENTIFIER_NODE:
8455 tree decl;
8456 cp_id_kind idk;
8457 bool non_integral_constant_expression_p;
8458 const char *error_msg;
8460 if (IDENTIFIER_TYPENAME_P (t))
8462 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8463 t = mangle_conv_op_name_for_type (new_type);
8466 /* Look up the name. */
8467 decl = lookup_name (t);
8469 /* By convention, expressions use ERROR_MARK_NODE to indicate
8470 failure, not NULL_TREE. */
8471 if (decl == NULL_TREE)
8472 decl = error_mark_node;
8474 decl = finish_id_expression (t, decl, NULL_TREE,
8475 &idk,
8476 /*integral_constant_expression_p=*/false,
8477 /*allow_non_integral_constant_expression_p=*/false,
8478 &non_integral_constant_expression_p,
8479 /*template_p=*/false,
8480 /*done=*/true,
8481 /*address_p=*/false,
8482 /*template_arg_p=*/false,
8483 &error_msg);
8484 if (error_msg)
8485 error (error_msg);
8486 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8487 decl = unqualified_name_lookup_error (decl);
8488 return decl;
8491 case TEMPLATE_ID_EXPR:
8493 tree object;
8494 tree template = RECUR (TREE_OPERAND (t, 0));
8495 tree targs = TREE_OPERAND (t, 1);
8497 if (targs)
8498 targs = tsubst_template_args (targs, args, complain, in_decl);
8500 if (TREE_CODE (template) == COMPONENT_REF)
8502 object = TREE_OPERAND (template, 0);
8503 template = TREE_OPERAND (template, 1);
8505 else
8506 object = NULL_TREE;
8507 template = lookup_template_function (template, targs);
8509 if (object)
8510 return build3 (COMPONENT_REF, TREE_TYPE (template),
8511 object, template, NULL_TREE);
8512 else
8513 return template;
8516 case INDIRECT_REF:
8518 tree r = RECUR (TREE_OPERAND (t, 0));
8520 if (REFERENCE_REF_P (t))
8522 /* A type conversion to reference type will be enclosed in
8523 such an indirect ref, but the substitution of the cast
8524 will have also added such an indirect ref. */
8525 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8526 r = convert_from_reference (r);
8528 else
8529 r = build_x_indirect_ref (r, "unary *");
8530 return r;
8533 case NOP_EXPR:
8534 return build_nop
8535 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8536 RECUR (TREE_OPERAND (t, 0)));
8538 case CAST_EXPR:
8539 return build_functional_cast
8540 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8541 RECUR (TREE_OPERAND (t, 0)));
8543 case REINTERPRET_CAST_EXPR:
8544 return build_reinterpret_cast
8545 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8546 RECUR (TREE_OPERAND (t, 0)));
8548 case CONST_CAST_EXPR:
8549 return build_const_cast
8550 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8551 RECUR (TREE_OPERAND (t, 0)));
8553 case DYNAMIC_CAST_EXPR:
8554 return build_dynamic_cast
8555 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8556 RECUR (TREE_OPERAND (t, 0)));
8558 case STATIC_CAST_EXPR:
8559 return build_static_cast
8560 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8561 RECUR (TREE_OPERAND (t, 0)));
8563 case POSTDECREMENT_EXPR:
8564 case POSTINCREMENT_EXPR:
8565 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8566 args, complain, in_decl);
8567 return build_x_unary_op (TREE_CODE (t), op1);
8569 case PREDECREMENT_EXPR:
8570 case PREINCREMENT_EXPR:
8571 case NEGATE_EXPR:
8572 case BIT_NOT_EXPR:
8573 case ABS_EXPR:
8574 case TRUTH_NOT_EXPR:
8575 case UNARY_PLUS_EXPR: /* Unary + */
8576 case REALPART_EXPR:
8577 case IMAGPART_EXPR:
8578 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8580 case ADDR_EXPR:
8581 op1 = TREE_OPERAND (t, 0);
8582 if (TREE_CODE (op1) == SCOPE_REF)
8583 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8584 /*done=*/true, /*address_p=*/true);
8585 else
8586 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8587 in_decl);
8588 if (TREE_CODE (op1) == LABEL_DECL)
8589 return finish_label_address_expr (DECL_NAME (op1));
8590 return build_x_unary_op (ADDR_EXPR, op1);
8592 case PLUS_EXPR:
8593 case MINUS_EXPR:
8594 case MULT_EXPR:
8595 case TRUNC_DIV_EXPR:
8596 case CEIL_DIV_EXPR:
8597 case FLOOR_DIV_EXPR:
8598 case ROUND_DIV_EXPR:
8599 case EXACT_DIV_EXPR:
8600 case BIT_AND_EXPR:
8601 case BIT_IOR_EXPR:
8602 case BIT_XOR_EXPR:
8603 case TRUNC_MOD_EXPR:
8604 case FLOOR_MOD_EXPR:
8605 case TRUTH_ANDIF_EXPR:
8606 case TRUTH_ORIF_EXPR:
8607 case TRUTH_AND_EXPR:
8608 case TRUTH_OR_EXPR:
8609 case RSHIFT_EXPR:
8610 case LSHIFT_EXPR:
8611 case RROTATE_EXPR:
8612 case LROTATE_EXPR:
8613 case EQ_EXPR:
8614 case NE_EXPR:
8615 case MAX_EXPR:
8616 case MIN_EXPR:
8617 case LE_EXPR:
8618 case GE_EXPR:
8619 case LT_EXPR:
8620 case GT_EXPR:
8621 case MEMBER_REF:
8622 case DOTSTAR_EXPR:
8623 return build_x_binary_op
8624 (TREE_CODE (t),
8625 RECUR (TREE_OPERAND (t, 0)),
8626 RECUR (TREE_OPERAND (t, 1)),
8627 /*overloaded_p=*/NULL);
8629 case SCOPE_REF:
8630 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8631 /*address_p=*/false);
8632 case ARRAY_REF:
8633 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8634 args, complain, in_decl);
8635 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8636 /*overloaded_p=*/NULL);
8638 case SIZEOF_EXPR:
8639 case ALIGNOF_EXPR:
8640 op1 = TREE_OPERAND (t, 0);
8641 if (!args)
8643 /* When there are no ARGS, we are trying to evaluate a
8644 non-dependent expression from the parser. Trying to do
8645 the substitutions may not work. */
8646 if (!TYPE_P (op1))
8647 op1 = TREE_TYPE (op1);
8649 else
8651 ++skip_evaluation;
8652 op1 = RECUR (op1);
8653 --skip_evaluation;
8655 if (TYPE_P (op1))
8656 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8657 else
8658 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8660 case MODOP_EXPR:
8662 tree r = build_x_modify_expr
8663 (RECUR (TREE_OPERAND (t, 0)),
8664 TREE_CODE (TREE_OPERAND (t, 1)),
8665 RECUR (TREE_OPERAND (t, 2)));
8666 /* TREE_NO_WARNING must be set if either the expression was
8667 parenthesized or it uses an operator such as >>= rather
8668 than plain assignment. In the former case, it was already
8669 set and must be copied. In the latter case,
8670 build_x_modify_expr sets it and it must not be reset
8671 here. */
8672 if (TREE_NO_WARNING (t))
8673 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8674 return r;
8677 case ARROW_EXPR:
8678 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8679 args, complain, in_decl);
8680 /* Remember that there was a reference to this entity. */
8681 if (DECL_P (op1))
8682 mark_used (op1);
8683 return build_x_arrow (op1);
8685 case NEW_EXPR:
8686 return build_new
8687 (RECUR (TREE_OPERAND (t, 0)),
8688 RECUR (TREE_OPERAND (t, 1)),
8689 RECUR (TREE_OPERAND (t, 2)),
8690 RECUR (TREE_OPERAND (t, 3)),
8691 NEW_EXPR_USE_GLOBAL (t));
8693 case DELETE_EXPR:
8694 return delete_sanity
8695 (RECUR (TREE_OPERAND (t, 0)),
8696 RECUR (TREE_OPERAND (t, 1)),
8697 DELETE_EXPR_USE_VEC (t),
8698 DELETE_EXPR_USE_GLOBAL (t));
8700 case COMPOUND_EXPR:
8701 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8702 RECUR (TREE_OPERAND (t, 1)));
8704 case CALL_EXPR:
8706 tree function;
8707 tree call_args;
8708 bool qualified_p;
8709 bool koenig_p;
8711 function = TREE_OPERAND (t, 0);
8712 /* When we parsed the expression, we determined whether or
8713 not Koenig lookup should be performed. */
8714 koenig_p = KOENIG_LOOKUP_P (t);
8715 if (TREE_CODE (function) == SCOPE_REF)
8717 qualified_p = true;
8718 function = tsubst_qualified_id (function, args, complain, in_decl,
8719 /*done=*/false,
8720 /*address_p=*/false);
8722 else
8724 if (TREE_CODE (function) == COMPONENT_REF)
8726 tree op = TREE_OPERAND (function, 1);
8728 qualified_p = (TREE_CODE (op) == SCOPE_REF
8729 || (BASELINK_P (op)
8730 && BASELINK_QUALIFIED_P (op)));
8732 else
8733 qualified_p = false;
8735 function = tsubst_copy_and_build (function, args, complain,
8736 in_decl,
8737 !qualified_p);
8738 if (BASELINK_P (function))
8739 qualified_p = true;
8742 call_args = RECUR (TREE_OPERAND (t, 1));
8744 /* We do not perform argument-dependent lookup if normal
8745 lookup finds a non-function, in accordance with the
8746 expected resolution of DR 218. */
8747 if (koenig_p
8748 && ((is_overloaded_fn (function)
8749 /* If lookup found a member function, the Koenig lookup is
8750 not appropriate, even if an unqualified-name was used
8751 to denote the function. */
8752 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
8753 || TREE_CODE (function) == IDENTIFIER_NODE))
8754 function = perform_koenig_lookup (function, call_args);
8756 if (TREE_CODE (function) == IDENTIFIER_NODE)
8758 unqualified_name_lookup_error (function);
8759 return error_mark_node;
8762 /* Remember that there was a reference to this entity. */
8763 if (DECL_P (function))
8764 mark_used (function);
8766 if (TREE_CODE (function) == OFFSET_REF)
8767 return build_offset_ref_call_from_tree (function, call_args);
8768 if (TREE_CODE (function) == COMPONENT_REF)
8770 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8771 return finish_call_expr (function, call_args,
8772 /*disallow_virtual=*/false,
8773 /*koenig_p=*/false);
8774 else
8775 return (build_new_method_call
8776 (TREE_OPERAND (function, 0),
8777 TREE_OPERAND (function, 1),
8778 call_args, NULL_TREE,
8779 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8781 return finish_call_expr (function, call_args,
8782 /*disallow_virtual=*/qualified_p,
8783 koenig_p);
8786 case COND_EXPR:
8787 return build_x_conditional_expr
8788 (RECUR (TREE_OPERAND (t, 0)),
8789 RECUR (TREE_OPERAND (t, 1)),
8790 RECUR (TREE_OPERAND (t, 2)));
8792 case PSEUDO_DTOR_EXPR:
8793 return finish_pseudo_destructor_expr
8794 (RECUR (TREE_OPERAND (t, 0)),
8795 RECUR (TREE_OPERAND (t, 1)),
8796 RECUR (TREE_OPERAND (t, 2)));
8798 case TREE_LIST:
8800 tree purpose, value, chain;
8802 if (t == void_list_node)
8803 return t;
8805 purpose = TREE_PURPOSE (t);
8806 if (purpose)
8807 purpose = RECUR (purpose);
8808 value = TREE_VALUE (t);
8809 if (value)
8810 value = RECUR (value);
8811 chain = TREE_CHAIN (t);
8812 if (chain && chain != void_type_node)
8813 chain = RECUR (chain);
8814 if (purpose == TREE_PURPOSE (t)
8815 && value == TREE_VALUE (t)
8816 && chain == TREE_CHAIN (t))
8817 return t;
8818 return tree_cons (purpose, value, chain);
8821 case COMPONENT_REF:
8823 tree object;
8824 tree object_type;
8825 tree member;
8827 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8828 args, complain, in_decl);
8829 /* Remember that there was a reference to this entity. */
8830 if (DECL_P (object))
8831 mark_used (object);
8832 object_type = TREE_TYPE (object);
8834 member = TREE_OPERAND (t, 1);
8835 if (BASELINK_P (member))
8836 member = tsubst_baselink (member,
8837 non_reference (TREE_TYPE (object)),
8838 args, complain, in_decl);
8839 else
8840 member = tsubst_copy (member, args, complain, in_decl);
8841 if (member == error_mark_node)
8842 return error_mark_node;
8844 if (object_type && !CLASS_TYPE_P (object_type))
8846 if (TREE_CODE (member) == BIT_NOT_EXPR)
8847 return finish_pseudo_destructor_expr (object,
8848 NULL_TREE,
8849 object_type);
8850 else if (TREE_CODE (member) == SCOPE_REF
8851 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8852 return finish_pseudo_destructor_expr (object,
8853 object,
8854 object_type);
8856 else if (TREE_CODE (member) == SCOPE_REF
8857 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8859 tree tmpl;
8860 tree args;
8862 /* Lookup the template functions now that we know what the
8863 scope is. */
8864 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8865 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8866 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8867 /*is_type_p=*/false,
8868 /*complain=*/false);
8869 if (BASELINK_P (member))
8871 BASELINK_FUNCTIONS (member)
8872 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8873 args);
8874 member = (adjust_result_of_qualified_name_lookup
8875 (member, BINFO_TYPE (BASELINK_BINFO (member)),
8876 object_type));
8878 else
8880 qualified_name_lookup_error (object_type, tmpl, member);
8881 return error_mark_node;
8884 else if (TREE_CODE (member) == SCOPE_REF
8885 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
8886 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
8888 if (complain & tf_error)
8890 if (TYPE_P (TREE_OPERAND (member, 0)))
8891 error ("%qT is not a class or namespace",
8892 TREE_OPERAND (member, 0));
8893 else
8894 error ("%qD is not a class or namespace",
8895 TREE_OPERAND (member, 0));
8897 return error_mark_node;
8899 else if (TREE_CODE (member) == FIELD_DECL)
8900 return finish_non_static_data_member (member, object, NULL_TREE);
8902 return finish_class_member_access_expr (object, member,
8903 /*template_p=*/false);
8906 case THROW_EXPR:
8907 return build_throw
8908 (RECUR (TREE_OPERAND (t, 0)));
8910 case CONSTRUCTOR:
8912 VEC(constructor_elt,gc) *n;
8913 constructor_elt *ce;
8914 unsigned HOST_WIDE_INT idx;
8915 tree r;
8916 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8917 bool process_index_p;
8919 /* digest_init will do the wrong thing if we let it. */
8920 if (type && TYPE_PTRMEMFUNC_P (type))
8921 return t;
8923 /* We do not want to process the index of aggregate
8924 initializers as they are identifier nodes which will be
8925 looked up by digest_init. */
8926 process_index_p = !(type && IS_AGGR_TYPE (type));
8928 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
8929 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
8931 if (ce->index && process_index_p)
8932 ce->index = RECUR (ce->index);
8933 ce->value = RECUR (ce->value);
8936 r = build_constructor (NULL_TREE, n);
8937 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8939 if (type)
8940 return digest_init (type, r);
8941 return r;
8944 case TYPEID_EXPR:
8946 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
8947 if (TYPE_P (operand_0))
8948 return get_typeid (operand_0);
8949 return build_typeid (operand_0);
8952 case VAR_DECL:
8953 if (!args)
8954 return t;
8955 /* Fall through */
8957 case PARM_DECL:
8959 tree r = tsubst_copy (t, args, complain, in_decl);
8961 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
8962 /* If the original type was a reference, we'll be wrapped in
8963 the appropriate INDIRECT_REF. */
8964 r = convert_from_reference (r);
8965 return r;
8968 case VA_ARG_EXPR:
8969 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
8970 tsubst_copy (TREE_TYPE (t), args, complain,
8971 in_decl));
8973 case OFFSETOF_EXPR:
8974 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
8976 case STMT_EXPR:
8978 tree old_stmt_expr = cur_stmt_expr;
8979 tree stmt_expr = begin_stmt_expr ();
8981 cur_stmt_expr = stmt_expr;
8982 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
8983 stmt_expr = finish_stmt_expr (stmt_expr, false);
8984 cur_stmt_expr = old_stmt_expr;
8986 return stmt_expr;
8989 case CONST_DECL:
8990 t = tsubst_copy (t, args, complain, in_decl);
8991 /* As in finish_id_expression, we resolve enumeration constants
8992 to their underlying values. */
8993 if (TREE_CODE (t) == CONST_DECL)
8994 return DECL_INITIAL (t);
8995 return t;
8997 default:
8998 /* Handle Objective-C++ constructs, if appropriate. */
9000 tree subst
9001 = objcp_tsubst_copy_and_build (t, args, complain,
9002 in_decl, /*function_p=*/false);
9003 if (subst)
9004 return subst;
9006 return tsubst_copy (t, args, complain, in_decl);
9009 #undef RECUR
9012 /* Verify that the instantiated ARGS are valid. For type arguments,
9013 make sure that the type's linkage is ok. For non-type arguments,
9014 make sure they are constants if they are integral or enumerations.
9015 Emit an error under control of COMPLAIN, and return TRUE on error. */
9017 static bool
9018 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9020 int ix, len = DECL_NTPARMS (tmpl);
9021 bool result = false;
9023 for (ix = 0; ix != len; ix++)
9025 tree t = TREE_VEC_ELT (args, ix);
9027 if (TYPE_P (t))
9029 /* [basic.link]: A name with no linkage (notably, the name
9030 of a class or enumeration declared in a local scope)
9031 shall not be used to declare an entity with linkage.
9032 This implies that names with no linkage cannot be used as
9033 template arguments. */
9034 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9036 if (nt)
9038 /* DR 488 makes use of a type with no linkage cause
9039 type deduction to fail. */
9040 if (complain & tf_error)
9042 if (TYPE_ANONYMOUS_P (nt))
9043 error ("%qT is/uses anonymous type", t);
9044 else
9045 error ("template argument for %qD uses local type %qT",
9046 tmpl, t);
9048 result = true;
9050 /* In order to avoid all sorts of complications, we do not
9051 allow variably-modified types as template arguments. */
9052 else if (variably_modified_type_p (t, NULL_TREE))
9054 if (complain & tf_error)
9055 error ("%qT is a variably modified type", t);
9056 result = true;
9059 /* A non-type argument of integral or enumerated type must be a
9060 constant. */
9061 else if (TREE_TYPE (t)
9062 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9063 && !TREE_CONSTANT (t))
9065 if (complain & tf_error)
9066 error ("integral expression %qE is not constant", t);
9067 result = true;
9070 if (result && (complain & tf_error))
9071 error (" trying to instantiate %qD", tmpl);
9072 return result;
9075 /* Instantiate the indicated variable or function template TMPL with
9076 the template arguments in TARG_PTR. */
9078 tree
9079 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9081 tree fndecl;
9082 tree gen_tmpl;
9083 tree spec;
9085 if (tmpl == error_mark_node)
9086 return error_mark_node;
9088 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9090 /* If this function is a clone, handle it specially. */
9091 if (DECL_CLONED_FUNCTION_P (tmpl))
9093 tree spec;
9094 tree clone;
9096 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9097 complain);
9098 if (spec == error_mark_node)
9099 return error_mark_node;
9101 /* Look for the clone. */
9102 FOR_EACH_CLONE (clone, spec)
9103 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9104 return clone;
9105 /* We should always have found the clone by now. */
9106 gcc_unreachable ();
9107 return NULL_TREE;
9110 /* Check to see if we already have this specialization. */
9111 spec = retrieve_specialization (tmpl, targ_ptr,
9112 /*class_specializations_p=*/false);
9113 if (spec != NULL_TREE)
9114 return spec;
9116 gen_tmpl = most_general_template (tmpl);
9117 if (tmpl != gen_tmpl)
9119 /* The TMPL is a partial instantiation. To get a full set of
9120 arguments we must add the arguments used to perform the
9121 partial instantiation. */
9122 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9123 targ_ptr);
9125 /* Check to see if we already have this specialization. */
9126 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9127 /*class_specializations_p=*/false);
9128 if (spec != NULL_TREE)
9129 return spec;
9132 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9133 complain))
9134 return error_mark_node;
9136 /* We are building a FUNCTION_DECL, during which the access of its
9137 parameters and return types have to be checked. However this
9138 FUNCTION_DECL which is the desired context for access checking
9139 is not built yet. We solve this chicken-and-egg problem by
9140 deferring all checks until we have the FUNCTION_DECL. */
9141 push_deferring_access_checks (dk_deferred);
9143 /* Substitute template parameters. */
9144 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9145 targ_ptr, complain, gen_tmpl);
9146 if (fndecl == error_mark_node)
9147 return error_mark_node;
9149 /* Now we know the specialization, compute access previously
9150 deferred. */
9151 push_access_scope (fndecl);
9152 perform_deferred_access_checks ();
9153 pop_access_scope (fndecl);
9154 pop_deferring_access_checks ();
9156 /* The DECL_TI_TEMPLATE should always be the immediate parent
9157 template, not the most general template. */
9158 DECL_TI_TEMPLATE (fndecl) = tmpl;
9160 /* If we've just instantiated the main entry point for a function,
9161 instantiate all the alternate entry points as well. We do this
9162 by cloning the instantiation of the main entry point, not by
9163 instantiating the template clones. */
9164 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9165 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9167 return fndecl;
9170 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9171 arguments that are being used when calling it. TARGS is a vector
9172 into which the deduced template arguments are placed.
9174 Return zero for success, 2 for an incomplete match that doesn't resolve
9175 all the types, and 1 for complete failure. An error message will be
9176 printed only for an incomplete match.
9178 If FN is a conversion operator, or we are trying to produce a specific
9179 specialization, RETURN_TYPE is the return type desired.
9181 The EXPLICIT_TARGS are explicit template arguments provided via a
9182 template-id.
9184 The parameter STRICT is one of:
9186 DEDUCE_CALL:
9187 We are deducing arguments for a function call, as in
9188 [temp.deduct.call].
9190 DEDUCE_CONV:
9191 We are deducing arguments for a conversion function, as in
9192 [temp.deduct.conv].
9194 DEDUCE_EXACT:
9195 We are deducing arguments when doing an explicit instantiation
9196 as in [temp.explicit], when determining an explicit specialization
9197 as in [temp.expl.spec], or when taking the address of a function
9198 template, as in [temp.deduct.funcaddr]. */
9201 fn_type_unification (tree fn,
9202 tree explicit_targs,
9203 tree targs,
9204 tree args,
9205 tree return_type,
9206 unification_kind_t strict,
9207 int flags)
9209 tree parms;
9210 tree fntype;
9211 int result;
9213 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9215 fntype = TREE_TYPE (fn);
9216 if (explicit_targs)
9218 /* [temp.deduct]
9220 The specified template arguments must match the template
9221 parameters in kind (i.e., type, nontype, template), and there
9222 must not be more arguments than there are parameters;
9223 otherwise type deduction fails.
9225 Nontype arguments must match the types of the corresponding
9226 nontype template parameters, or must be convertible to the
9227 types of the corresponding nontype parameters as specified in
9228 _temp.arg.nontype_, otherwise type deduction fails.
9230 All references in the function type of the function template
9231 to the corresponding template parameters are replaced by the
9232 specified template argument values. If a substitution in a
9233 template parameter or in the function type of the function
9234 template results in an invalid type, type deduction fails. */
9235 int i;
9236 tree converted_args;
9237 bool incomplete;
9239 if (explicit_targs == error_mark_node)
9240 return 1;
9242 converted_args
9243 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9244 explicit_targs, NULL_TREE, tf_none,
9245 /*require_all_args=*/false,
9246 /*use_default_args=*/false));
9247 if (converted_args == error_mark_node)
9248 return 1;
9250 /* Substitute the explicit args into the function type. This is
9251 necessary so that, for instance, explicitly declared function
9252 arguments can match null pointed constants. If we were given
9253 an incomplete set of explicit args, we must not do semantic
9254 processing during substitution as we could create partial
9255 instantiations. */
9256 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9257 processing_template_decl += incomplete;
9258 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9259 processing_template_decl -= incomplete;
9261 if (fntype == error_mark_node)
9262 return 1;
9264 /* Place the explicitly specified arguments in TARGS. */
9265 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9266 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9269 parms = TYPE_ARG_TYPES (fntype);
9270 /* Never do unification on the 'this' parameter. */
9271 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9272 parms = TREE_CHAIN (parms);
9274 if (return_type)
9276 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9277 args = tree_cons (NULL_TREE, return_type, args);
9280 /* We allow incomplete unification without an error message here
9281 because the standard doesn't seem to explicitly prohibit it. Our
9282 callers must be ready to deal with unification failures in any
9283 event. */
9284 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9285 targs, parms, args, /*subr=*/0,
9286 strict, flags);
9288 if (result == 0)
9289 /* All is well so far. Now, check:
9291 [temp.deduct]
9293 When all template arguments have been deduced, all uses of
9294 template parameters in nondeduced contexts are replaced with
9295 the corresponding deduced argument values. If the
9296 substitution results in an invalid type, as described above,
9297 type deduction fails. */
9298 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9299 == error_mark_node)
9300 return 1;
9302 return result;
9305 /* Adjust types before performing type deduction, as described in
9306 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9307 sections are symmetric. PARM is the type of a function parameter
9308 or the return type of the conversion function. ARG is the type of
9309 the argument passed to the call, or the type of the value
9310 initialized with the result of the conversion function. */
9312 static int
9313 maybe_adjust_types_for_deduction (unification_kind_t strict,
9314 tree* parm,
9315 tree* arg)
9317 int result = 0;
9319 switch (strict)
9321 case DEDUCE_CALL:
9322 break;
9324 case DEDUCE_CONV:
9326 /* Swap PARM and ARG throughout the remainder of this
9327 function; the handling is precisely symmetric since PARM
9328 will initialize ARG rather than vice versa. */
9329 tree* temp = parm;
9330 parm = arg;
9331 arg = temp;
9332 break;
9335 case DEDUCE_EXACT:
9336 /* There is nothing to do in this case. */
9337 return 0;
9339 default:
9340 gcc_unreachable ();
9343 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9345 /* [temp.deduct.call]
9347 If P is not a reference type:
9349 --If A is an array type, the pointer type produced by the
9350 array-to-pointer standard conversion (_conv.array_) is
9351 used in place of A for type deduction; otherwise,
9353 --If A is a function type, the pointer type produced by
9354 the function-to-pointer standard conversion
9355 (_conv.func_) is used in place of A for type deduction;
9356 otherwise,
9358 --If A is a cv-qualified type, the top level
9359 cv-qualifiers of A's type are ignored for type
9360 deduction. */
9361 if (TREE_CODE (*arg) == ARRAY_TYPE)
9362 *arg = build_pointer_type (TREE_TYPE (*arg));
9363 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9364 *arg = build_pointer_type (*arg);
9365 else
9366 *arg = TYPE_MAIN_VARIANT (*arg);
9369 /* [temp.deduct.call]
9371 If P is a cv-qualified type, the top level cv-qualifiers
9372 of P's type are ignored for type deduction. If P is a
9373 reference type, the type referred to by P is used for
9374 type deduction. */
9375 *parm = TYPE_MAIN_VARIANT (*parm);
9376 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9378 *parm = TREE_TYPE (*parm);
9379 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9382 /* DR 322. For conversion deduction, remove a reference type on parm
9383 too (which has been swapped into ARG). */
9384 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9385 *arg = TREE_TYPE (*arg);
9387 return result;
9390 /* Most parms like fn_type_unification.
9392 If SUBR is 1, we're being called recursively (to unify the
9393 arguments of a function or method parameter of a function
9394 template). */
9396 static int
9397 type_unification_real (tree tparms,
9398 tree targs,
9399 tree xparms,
9400 tree xargs,
9401 int subr,
9402 unification_kind_t strict,
9403 int flags)
9405 tree parm, arg;
9406 int i;
9407 int ntparms = TREE_VEC_LENGTH (tparms);
9408 int sub_strict;
9409 int saw_undeduced = 0;
9410 tree parms, args;
9412 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9413 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9414 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9415 gcc_assert (ntparms > 0);
9417 switch (strict)
9419 case DEDUCE_CALL:
9420 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9421 | UNIFY_ALLOW_DERIVED);
9422 break;
9424 case DEDUCE_CONV:
9425 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9426 break;
9428 case DEDUCE_EXACT:
9429 sub_strict = UNIFY_ALLOW_NONE;
9430 break;
9432 default:
9433 gcc_unreachable ();
9436 again:
9437 parms = xparms;
9438 args = xargs;
9440 while (parms && parms != void_list_node
9441 && args && args != void_list_node)
9443 parm = TREE_VALUE (parms);
9444 parms = TREE_CHAIN (parms);
9445 arg = TREE_VALUE (args);
9446 args = TREE_CHAIN (args);
9448 if (arg == error_mark_node)
9449 return 1;
9450 if (arg == unknown_type_node)
9451 /* We can't deduce anything from this, but we might get all the
9452 template args from other function args. */
9453 continue;
9455 /* Conversions will be performed on a function argument that
9456 corresponds with a function parameter that contains only
9457 non-deducible template parameters and explicitly specified
9458 template parameters. */
9459 if (!uses_template_parms (parm))
9461 tree type;
9463 if (!TYPE_P (arg))
9464 type = TREE_TYPE (arg);
9465 else
9466 type = arg;
9468 if (same_type_p (parm, type))
9469 continue;
9470 if (strict != DEDUCE_EXACT
9471 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9472 flags))
9473 continue;
9475 return 1;
9478 if (!TYPE_P (arg))
9480 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9481 if (type_unknown_p (arg))
9483 /* [temp.deduct.type] A template-argument can be deduced from
9484 a pointer to function or pointer to member function
9485 argument if the set of overloaded functions does not
9486 contain function templates and at most one of a set of
9487 overloaded functions provides a unique match. */
9489 if (resolve_overloaded_unification
9490 (tparms, targs, parm, arg, strict, sub_strict)
9491 != 0)
9492 return 1;
9493 continue;
9495 arg = TREE_TYPE (arg);
9496 if (arg == error_mark_node)
9497 return 1;
9501 int arg_strict = sub_strict;
9503 if (!subr)
9504 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9506 if (unify (tparms, targs, parm, arg, arg_strict))
9507 return 1;
9511 /* Fail if we've reached the end of the parm list, and more args
9512 are present, and the parm list isn't variadic. */
9513 if (args && args != void_list_node && parms == void_list_node)
9514 return 1;
9515 /* Fail if parms are left and they don't have default values. */
9516 if (parms && parms != void_list_node
9517 && TREE_PURPOSE (parms) == NULL_TREE)
9518 return 1;
9520 if (!subr)
9521 for (i = 0; i < ntparms; i++)
9522 if (!TREE_VEC_ELT (targs, i))
9524 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9526 /* If this is an undeduced nontype parameter that depends on
9527 a type parameter, try another pass; its type may have been
9528 deduced from a later argument than the one from which
9529 this parameter can be deduced. */
9530 if (TREE_CODE (tparm) == PARM_DECL
9531 && uses_template_parms (TREE_TYPE (tparm))
9532 && !saw_undeduced++)
9533 goto again;
9535 return 2;
9538 return 0;
9541 /* Subroutine of type_unification_real. Args are like the variables at the
9542 call site. ARG is an overloaded function (or template-id); we try
9543 deducing template args from each of the overloads, and if only one
9544 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9546 static int
9547 resolve_overloaded_unification (tree tparms,
9548 tree targs,
9549 tree parm,
9550 tree arg,
9551 unification_kind_t strict,
9552 int sub_strict)
9554 tree tempargs = copy_node (targs);
9555 int good = 0;
9556 bool addr_p;
9558 if (TREE_CODE (arg) == ADDR_EXPR)
9560 arg = TREE_OPERAND (arg, 0);
9561 addr_p = true;
9563 else
9564 addr_p = false;
9566 if (TREE_CODE (arg) == COMPONENT_REF)
9567 /* Handle `&x' where `x' is some static or non-static member
9568 function name. */
9569 arg = TREE_OPERAND (arg, 1);
9571 if (TREE_CODE (arg) == OFFSET_REF)
9572 arg = TREE_OPERAND (arg, 1);
9574 /* Strip baselink information. */
9575 if (BASELINK_P (arg))
9576 arg = BASELINK_FUNCTIONS (arg);
9578 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9580 /* If we got some explicit template args, we need to plug them into
9581 the affected templates before we try to unify, in case the
9582 explicit args will completely resolve the templates in question. */
9584 tree expl_subargs = TREE_OPERAND (arg, 1);
9585 arg = TREE_OPERAND (arg, 0);
9587 for (; arg; arg = OVL_NEXT (arg))
9589 tree fn = OVL_CURRENT (arg);
9590 tree subargs, elem;
9592 if (TREE_CODE (fn) != TEMPLATE_DECL)
9593 continue;
9595 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9596 expl_subargs, /*check_ret=*/false);
9597 if (subargs)
9599 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9600 good += try_one_overload (tparms, targs, tempargs, parm,
9601 elem, strict, sub_strict, addr_p);
9605 else
9607 gcc_assert (TREE_CODE (arg) == OVERLOAD
9608 || TREE_CODE (arg) == FUNCTION_DECL);
9610 for (; arg; arg = OVL_NEXT (arg))
9611 good += try_one_overload (tparms, targs, tempargs, parm,
9612 TREE_TYPE (OVL_CURRENT (arg)),
9613 strict, sub_strict, addr_p);
9616 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9617 to function or pointer to member function argument if the set of
9618 overloaded functions does not contain function templates and at most
9619 one of a set of overloaded functions provides a unique match.
9621 So if we found multiple possibilities, we return success but don't
9622 deduce anything. */
9624 if (good == 1)
9626 int i = TREE_VEC_LENGTH (targs);
9627 for (; i--; )
9628 if (TREE_VEC_ELT (tempargs, i))
9629 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9631 if (good)
9632 return 0;
9634 return 1;
9637 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9638 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9639 different overloads deduce different arguments for a given parm.
9640 ADDR_P is true if the expression for which deduction is being
9641 performed was of the form "& fn" rather than simply "fn".
9643 Returns 1 on success. */
9645 static int
9646 try_one_overload (tree tparms,
9647 tree orig_targs,
9648 tree targs,
9649 tree parm,
9650 tree arg,
9651 unification_kind_t strict,
9652 int sub_strict,
9653 bool addr_p)
9655 int nargs;
9656 tree tempargs;
9657 int i;
9659 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9660 to function or pointer to member function argument if the set of
9661 overloaded functions does not contain function templates and at most
9662 one of a set of overloaded functions provides a unique match.
9664 So if this is a template, just return success. */
9666 if (uses_template_parms (arg))
9667 return 1;
9669 if (TREE_CODE (arg) == METHOD_TYPE)
9670 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9671 else if (addr_p)
9672 arg = build_pointer_type (arg);
9674 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9676 /* We don't copy orig_targs for this because if we have already deduced
9677 some template args from previous args, unify would complain when we
9678 try to deduce a template parameter for the same argument, even though
9679 there isn't really a conflict. */
9680 nargs = TREE_VEC_LENGTH (targs);
9681 tempargs = make_tree_vec (nargs);
9683 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9684 return 0;
9686 /* First make sure we didn't deduce anything that conflicts with
9687 explicitly specified args. */
9688 for (i = nargs; i--; )
9690 tree elt = TREE_VEC_ELT (tempargs, i);
9691 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9693 if (!elt)
9694 /*NOP*/;
9695 else if (uses_template_parms (elt))
9696 /* Since we're unifying against ourselves, we will fill in
9697 template args used in the function parm list with our own
9698 template parms. Discard them. */
9699 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9700 else if (oldelt && !template_args_equal (oldelt, elt))
9701 return 0;
9704 for (i = nargs; i--; )
9706 tree elt = TREE_VEC_ELT (tempargs, i);
9708 if (elt)
9709 TREE_VEC_ELT (targs, i) = elt;
9712 return 1;
9715 /* PARM is a template class (perhaps with unbound template
9716 parameters). ARG is a fully instantiated type. If ARG can be
9717 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9718 TARGS are as for unify. */
9720 static tree
9721 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9723 tree copy_of_targs;
9725 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9726 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9727 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9728 return NULL_TREE;
9730 /* We need to make a new template argument vector for the call to
9731 unify. If we used TARGS, we'd clutter it up with the result of
9732 the attempted unification, even if this class didn't work out.
9733 We also don't want to commit ourselves to all the unifications
9734 we've already done, since unification is supposed to be done on
9735 an argument-by-argument basis. In other words, consider the
9736 following pathological case:
9738 template <int I, int J, int K>
9739 struct S {};
9741 template <int I, int J>
9742 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9744 template <int I, int J, int K>
9745 void f(S<I, J, K>, S<I, I, I>);
9747 void g() {
9748 S<0, 0, 0> s0;
9749 S<0, 1, 2> s2;
9751 f(s0, s2);
9754 Now, by the time we consider the unification involving `s2', we
9755 already know that we must have `f<0, 0, 0>'. But, even though
9756 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9757 because there are two ways to unify base classes of S<0, 1, 2>
9758 with S<I, I, I>. If we kept the already deduced knowledge, we
9759 would reject the possibility I=1. */
9760 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9762 /* If unification failed, we're done. */
9763 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9764 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9765 return NULL_TREE;
9767 return arg;
9770 /* Given a template type PARM and a class type ARG, find the unique
9771 base type in ARG that is an instance of PARM. We do not examine
9772 ARG itself; only its base-classes. If there is not exactly one
9773 appropriate base class, return NULL_TREE. PARM may be the type of
9774 a partial specialization, as well as a plain template type. Used
9775 by unify. */
9777 static tree
9778 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9780 tree rval = NULL_TREE;
9781 tree binfo;
9783 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9785 binfo = TYPE_BINFO (complete_type (arg));
9786 if (!binfo)
9787 /* The type could not be completed. */
9788 return NULL_TREE;
9790 /* Walk in inheritance graph order. The search order is not
9791 important, and this avoids multiple walks of virtual bases. */
9792 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9794 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9796 if (r)
9798 /* If there is more than one satisfactory baseclass, then:
9800 [temp.deduct.call]
9802 If they yield more than one possible deduced A, the type
9803 deduction fails.
9805 applies. */
9806 if (rval && !same_type_p (r, rval))
9807 return NULL_TREE;
9809 rval = r;
9813 return rval;
9816 /* Returns the level of DECL, which declares a template parameter. */
9818 static int
9819 template_decl_level (tree decl)
9821 switch (TREE_CODE (decl))
9823 case TYPE_DECL:
9824 case TEMPLATE_DECL:
9825 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9827 case PARM_DECL:
9828 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9830 default:
9831 gcc_unreachable ();
9833 return 0;
9836 /* Decide whether ARG can be unified with PARM, considering only the
9837 cv-qualifiers of each type, given STRICT as documented for unify.
9838 Returns nonzero iff the unification is OK on that basis. */
9840 static int
9841 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9843 int arg_quals = cp_type_quals (arg);
9844 int parm_quals = cp_type_quals (parm);
9846 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9847 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9849 /* Although a CVR qualifier is ignored when being applied to a
9850 substituted template parameter ([8.3.2]/1 for example), that
9851 does not apply during deduction [14.8.2.4]/1, (even though
9852 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9853 this). Except when we're allowing additional CV qualifiers
9854 at the outer level [14.8.2.1]/3,1st bullet. */
9855 if ((TREE_CODE (arg) == REFERENCE_TYPE
9856 || TREE_CODE (arg) == FUNCTION_TYPE
9857 || TREE_CODE (arg) == METHOD_TYPE)
9858 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9859 return 0;
9861 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9862 && (parm_quals & TYPE_QUAL_RESTRICT))
9863 return 0;
9866 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9867 && (arg_quals & parm_quals) != parm_quals)
9868 return 0;
9870 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9871 && (parm_quals & arg_quals) != arg_quals)
9872 return 0;
9874 return 1;
9877 /* Deduce the value of template parameters. TPARMS is the (innermost)
9878 set of template parameters to a template. TARGS is the bindings
9879 for those template parameters, as determined thus far; TARGS may
9880 include template arguments for outer levels of template parameters
9881 as well. PARM is a parameter to a template function, or a
9882 subcomponent of that parameter; ARG is the corresponding argument.
9883 This function attempts to match PARM with ARG in a manner
9884 consistent with the existing assignments in TARGS. If more values
9885 are deduced, then TARGS is updated.
9887 Returns 0 if the type deduction succeeds, 1 otherwise. The
9888 parameter STRICT is a bitwise or of the following flags:
9890 UNIFY_ALLOW_NONE:
9891 Require an exact match between PARM and ARG.
9892 UNIFY_ALLOW_MORE_CV_QUAL:
9893 Allow the deduced ARG to be more cv-qualified (by qualification
9894 conversion) than ARG.
9895 UNIFY_ALLOW_LESS_CV_QUAL:
9896 Allow the deduced ARG to be less cv-qualified than ARG.
9897 UNIFY_ALLOW_DERIVED:
9898 Allow the deduced ARG to be a template base class of ARG,
9899 or a pointer to a template base class of the type pointed to by
9900 ARG.
9901 UNIFY_ALLOW_INTEGER:
9902 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9903 case for more information.
9904 UNIFY_ALLOW_OUTER_LEVEL:
9905 This is the outermost level of a deduction. Used to determine validity
9906 of qualification conversions. A valid qualification conversion must
9907 have const qualified pointers leading up to the inner type which
9908 requires additional CV quals, except at the outer level, where const
9909 is not required [conv.qual]. It would be normal to set this flag in
9910 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9911 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9912 This is the outermost level of a deduction, and PARM can be more CV
9913 qualified at this point.
9914 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9915 This is the outermost level of a deduction, and PARM can be less CV
9916 qualified at this point. */
9918 static int
9919 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
9921 int idx;
9922 tree targ;
9923 tree tparm;
9924 int strict_in = strict;
9926 /* I don't think this will do the right thing with respect to types.
9927 But the only case I've seen it in so far has been array bounds, where
9928 signedness is the only information lost, and I think that will be
9929 okay. */
9930 while (TREE_CODE (parm) == NOP_EXPR)
9931 parm = TREE_OPERAND (parm, 0);
9933 if (arg == error_mark_node)
9934 return 1;
9935 if (arg == unknown_type_node)
9936 /* We can't deduce anything from this, but we might get all the
9937 template args from other function args. */
9938 return 0;
9940 /* If PARM uses template parameters, then we can't bail out here,
9941 even if ARG == PARM, since we won't record unifications for the
9942 template parameters. We might need them if we're trying to
9943 figure out which of two things is more specialized. */
9944 if (arg == parm && !uses_template_parms (parm))
9945 return 0;
9947 /* Immediately reject some pairs that won't unify because of
9948 cv-qualification mismatches. */
9949 if (TREE_CODE (arg) == TREE_CODE (parm)
9950 && TYPE_P (arg)
9951 /* It is the elements of the array which hold the cv quals of an array
9952 type, and the elements might be template type parms. We'll check
9953 when we recurse. */
9954 && TREE_CODE (arg) != ARRAY_TYPE
9955 /* We check the cv-qualifiers when unifying with template type
9956 parameters below. We want to allow ARG `const T' to unify with
9957 PARM `T' for example, when computing which of two templates
9958 is more specialized, for example. */
9959 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9960 && !check_cv_quals_for_unify (strict_in, arg, parm))
9961 return 1;
9963 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9964 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9965 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9966 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9967 strict &= ~UNIFY_ALLOW_DERIVED;
9968 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9969 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9971 switch (TREE_CODE (parm))
9973 case TYPENAME_TYPE:
9974 case SCOPE_REF:
9975 case UNBOUND_CLASS_TEMPLATE:
9976 /* In a type which contains a nested-name-specifier, template
9977 argument values cannot be deduced for template parameters used
9978 within the nested-name-specifier. */
9979 return 0;
9981 case TEMPLATE_TYPE_PARM:
9982 case TEMPLATE_TEMPLATE_PARM:
9983 case BOUND_TEMPLATE_TEMPLATE_PARM:
9984 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9986 if (TEMPLATE_TYPE_LEVEL (parm)
9987 != template_decl_level (tparm))
9988 /* The PARM is not one we're trying to unify. Just check
9989 to see if it matches ARG. */
9990 return (TREE_CODE (arg) == TREE_CODE (parm)
9991 && same_type_p (parm, arg)) ? 0 : 1;
9992 idx = TEMPLATE_TYPE_IDX (parm);
9993 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
9994 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9996 /* Check for mixed types and values. */
9997 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9998 && TREE_CODE (tparm) != TYPE_DECL)
9999 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10000 && TREE_CODE (tparm) != TEMPLATE_DECL))
10001 return 1;
10003 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10005 /* ARG must be constructed from a template class or a template
10006 template parameter. */
10007 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10008 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
10009 return 1;
10012 tree parmvec = TYPE_TI_ARGS (parm);
10013 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10014 tree argtmplvec
10015 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10016 int i;
10018 /* The resolution to DR150 makes clear that default
10019 arguments for an N-argument may not be used to bind T
10020 to a template template parameter with fewer than N
10021 parameters. It is not safe to permit the binding of
10022 default arguments as an extension, as that may change
10023 the meaning of a conforming program. Consider:
10025 struct Dense { static const unsigned int dim = 1; };
10027 template <template <typename> class View,
10028 typename Block>
10029 void operator+(float, View<Block> const&);
10031 template <typename Block,
10032 unsigned int Dim = Block::dim>
10033 struct Lvalue_proxy { operator float() const; };
10035 void
10036 test_1d (void) {
10037 Lvalue_proxy<Dense> p;
10038 float b;
10039 b + p;
10042 Here, if Lvalue_proxy is permitted to bind to View, then
10043 the global operator+ will be used; if they are not, the
10044 Lvalue_proxy will be converted to float. */
10045 if (coerce_template_parms (argtmplvec, parmvec,
10046 TYPE_TI_TEMPLATE (parm),
10047 tf_none,
10048 /*require_all_args=*/true,
10049 /*use_default_args=*/false)
10050 == error_mark_node)
10051 return 1;
10053 /* Deduce arguments T, i from TT<T> or TT<i>.
10054 We check each element of PARMVEC and ARGVEC individually
10055 rather than the whole TREE_VEC since they can have
10056 different number of elements. */
10058 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10060 if (unify (tparms, targs,
10061 TREE_VEC_ELT (parmvec, i),
10062 TREE_VEC_ELT (argvec, i),
10063 UNIFY_ALLOW_NONE))
10064 return 1;
10067 arg = TYPE_TI_TEMPLATE (arg);
10069 /* Fall through to deduce template name. */
10072 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10073 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10075 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10077 /* Simple cases: Value already set, does match or doesn't. */
10078 if (targ != NULL_TREE && template_args_equal (targ, arg))
10079 return 0;
10080 else if (targ)
10081 return 1;
10083 else
10085 /* If PARM is `const T' and ARG is only `int', we don't have
10086 a match unless we are allowing additional qualification.
10087 If ARG is `const int' and PARM is just `T' that's OK;
10088 that binds `const int' to `T'. */
10089 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10090 arg, parm))
10091 return 1;
10093 /* Consider the case where ARG is `const volatile int' and
10094 PARM is `const T'. Then, T should be `volatile int'. */
10095 arg = cp_build_qualified_type_real
10096 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10097 if (arg == error_mark_node)
10098 return 1;
10100 /* Simple cases: Value already set, does match or doesn't. */
10101 if (targ != NULL_TREE && same_type_p (targ, arg))
10102 return 0;
10103 else if (targ)
10104 return 1;
10106 /* Make sure that ARG is not a variable-sized array. (Note
10107 that were talking about variable-sized arrays (like
10108 `int[n]'), rather than arrays of unknown size (like
10109 `int[]').) We'll get very confused by such a type since
10110 the bound of the array will not be computable in an
10111 instantiation. Besides, such types are not allowed in
10112 ISO C++, so we can do as we please here. */
10113 if (variably_modified_type_p (arg, NULL_TREE))
10114 return 1;
10117 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10118 return 0;
10120 case TEMPLATE_PARM_INDEX:
10121 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10123 if (TEMPLATE_PARM_LEVEL (parm)
10124 != template_decl_level (tparm))
10125 /* The PARM is not one we're trying to unify. Just check
10126 to see if it matches ARG. */
10127 return !(TREE_CODE (arg) == TREE_CODE (parm)
10128 && cp_tree_equal (parm, arg));
10130 idx = TEMPLATE_PARM_IDX (parm);
10131 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10133 if (targ)
10134 return !cp_tree_equal (targ, arg);
10136 /* [temp.deduct.type] If, in the declaration of a function template
10137 with a non-type template-parameter, the non-type
10138 template-parameter is used in an expression in the function
10139 parameter-list and, if the corresponding template-argument is
10140 deduced, the template-argument type shall match the type of the
10141 template-parameter exactly, except that a template-argument
10142 deduced from an array bound may be of any integral type.
10143 The non-type parameter might use already deduced type parameters. */
10144 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10145 if (!TREE_TYPE (arg))
10146 /* Template-parameter dependent expression. Just accept it for now.
10147 It will later be processed in convert_template_argument. */
10149 else if (same_type_p (TREE_TYPE (arg), tparm))
10150 /* OK */;
10151 else if ((strict & UNIFY_ALLOW_INTEGER)
10152 && (TREE_CODE (tparm) == INTEGER_TYPE
10153 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10154 /* Convert the ARG to the type of PARM; the deduced non-type
10155 template argument must exactly match the types of the
10156 corresponding parameter. */
10157 arg = fold (build_nop (TREE_TYPE (parm), arg));
10158 else if (uses_template_parms (tparm))
10159 /* We haven't deduced the type of this parameter yet. Try again
10160 later. */
10161 return 0;
10162 else
10163 return 1;
10165 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10166 return 0;
10168 case PTRMEM_CST:
10170 /* A pointer-to-member constant can be unified only with
10171 another constant. */
10172 if (TREE_CODE (arg) != PTRMEM_CST)
10173 return 1;
10175 /* Just unify the class member. It would be useless (and possibly
10176 wrong, depending on the strict flags) to unify also
10177 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10178 arg refer to the same variable, even if through different
10179 classes. For instance:
10181 struct A { int x; };
10182 struct B : A { };
10184 Unification of &A::x and &B::x must succeed. */
10185 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10186 PTRMEM_CST_MEMBER (arg), strict);
10189 case POINTER_TYPE:
10191 if (TREE_CODE (arg) != POINTER_TYPE)
10192 return 1;
10194 /* [temp.deduct.call]
10196 A can be another pointer or pointer to member type that can
10197 be converted to the deduced A via a qualification
10198 conversion (_conv.qual_).
10200 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10201 This will allow for additional cv-qualification of the
10202 pointed-to types if appropriate. */
10204 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10205 /* The derived-to-base conversion only persists through one
10206 level of pointers. */
10207 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10209 return unify (tparms, targs, TREE_TYPE (parm),
10210 TREE_TYPE (arg), strict);
10213 case REFERENCE_TYPE:
10214 if (TREE_CODE (arg) != REFERENCE_TYPE)
10215 return 1;
10216 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10217 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10219 case ARRAY_TYPE:
10220 if (TREE_CODE (arg) != ARRAY_TYPE)
10221 return 1;
10222 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10223 != (TYPE_DOMAIN (arg) == NULL_TREE))
10224 return 1;
10225 if (TYPE_DOMAIN (parm) != NULL_TREE)
10227 tree parm_max;
10228 tree arg_max;
10230 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10231 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10233 /* Our representation of array types uses "N - 1" as the
10234 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10235 not an integer constant. */
10236 if (TREE_CODE (parm_max) == MINUS_EXPR)
10238 arg_max = fold_build2 (PLUS_EXPR,
10239 integer_type_node,
10240 arg_max,
10241 TREE_OPERAND (parm_max, 1));
10242 parm_max = TREE_OPERAND (parm_max, 0);
10245 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10246 return 1;
10248 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10249 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10251 case REAL_TYPE:
10252 case COMPLEX_TYPE:
10253 case VECTOR_TYPE:
10254 case INTEGER_TYPE:
10255 case BOOLEAN_TYPE:
10256 case ENUMERAL_TYPE:
10257 case VOID_TYPE:
10258 if (TREE_CODE (arg) != TREE_CODE (parm))
10259 return 1;
10261 /* We have already checked cv-qualification at the top of the
10262 function. */
10263 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10264 return 1;
10266 /* As far as unification is concerned, this wins. Later checks
10267 will invalidate it if necessary. */
10268 return 0;
10270 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10271 /* Type INTEGER_CST can come from ordinary constant template args. */
10272 case INTEGER_CST:
10273 while (TREE_CODE (arg) == NOP_EXPR)
10274 arg = TREE_OPERAND (arg, 0);
10276 if (TREE_CODE (arg) != INTEGER_CST)
10277 return 1;
10278 return !tree_int_cst_equal (parm, arg);
10280 case TREE_VEC:
10282 int i;
10283 if (TREE_CODE (arg) != TREE_VEC)
10284 return 1;
10285 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10286 return 1;
10287 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10288 if (unify (tparms, targs,
10289 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10290 UNIFY_ALLOW_NONE))
10291 return 1;
10292 return 0;
10295 case RECORD_TYPE:
10296 case UNION_TYPE:
10297 if (TREE_CODE (arg) != TREE_CODE (parm))
10298 return 1;
10300 if (TYPE_PTRMEMFUNC_P (parm))
10302 if (!TYPE_PTRMEMFUNC_P (arg))
10303 return 1;
10305 return unify (tparms, targs,
10306 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10307 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10308 strict);
10311 if (CLASSTYPE_TEMPLATE_INFO (parm))
10313 tree t = NULL_TREE;
10315 if (strict_in & UNIFY_ALLOW_DERIVED)
10317 /* First, we try to unify the PARM and ARG directly. */
10318 t = try_class_unification (tparms, targs,
10319 parm, arg);
10321 if (!t)
10323 /* Fallback to the special case allowed in
10324 [temp.deduct.call]:
10326 If P is a class, and P has the form
10327 template-id, then A can be a derived class of
10328 the deduced A. Likewise, if P is a pointer to
10329 a class of the form template-id, A can be a
10330 pointer to a derived class pointed to by the
10331 deduced A. */
10332 t = get_template_base (tparms, targs, parm, arg);
10334 if (!t)
10335 return 1;
10338 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10339 && (CLASSTYPE_TI_TEMPLATE (parm)
10340 == CLASSTYPE_TI_TEMPLATE (arg)))
10341 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10342 Then, we should unify `int' and `U'. */
10343 t = arg;
10344 else
10345 /* There's no chance of unification succeeding. */
10346 return 1;
10348 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10349 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10351 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10352 return 1;
10353 return 0;
10355 case METHOD_TYPE:
10356 case FUNCTION_TYPE:
10357 if (TREE_CODE (arg) != TREE_CODE (parm))
10358 return 1;
10360 /* CV qualifications for methods can never be deduced, they must
10361 match exactly. We need to check them explicitly here,
10362 because type_unification_real treats them as any other
10363 cvqualified parameter. */
10364 if (TREE_CODE (parm) == METHOD_TYPE
10365 && (!check_cv_quals_for_unify
10366 (UNIFY_ALLOW_NONE,
10367 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10368 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10369 return 1;
10371 if (unify (tparms, targs, TREE_TYPE (parm),
10372 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10373 return 1;
10374 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10375 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10376 LOOKUP_NORMAL);
10378 case OFFSET_TYPE:
10379 /* Unify a pointer to member with a pointer to member function, which
10380 deduces the type of the member as a function type. */
10381 if (TYPE_PTRMEMFUNC_P (arg))
10383 tree method_type;
10384 tree fntype;
10385 cp_cv_quals cv_quals;
10387 /* Check top-level cv qualifiers */
10388 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10389 return 1;
10391 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10392 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10393 return 1;
10395 /* Determine the type of the function we are unifying against. */
10396 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10397 fntype =
10398 build_function_type (TREE_TYPE (method_type),
10399 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10401 /* Extract the cv-qualifiers of the member function from the
10402 implicit object parameter and place them on the function
10403 type to be restored later. */
10404 cv_quals =
10405 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10406 fntype = build_qualified_type (fntype, cv_quals);
10407 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10410 if (TREE_CODE (arg) != OFFSET_TYPE)
10411 return 1;
10412 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10413 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10414 return 1;
10415 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10416 strict);
10418 case CONST_DECL:
10419 if (DECL_TEMPLATE_PARM_P (parm))
10420 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10421 if (arg != integral_constant_value (parm))
10422 return 1;
10423 return 0;
10425 case FIELD_DECL:
10426 case TEMPLATE_DECL:
10427 /* Matched cases are handled by the ARG == PARM test above. */
10428 return 1;
10430 default:
10431 gcc_assert (EXPR_P (parm));
10433 /* We must be looking at an expression. This can happen with
10434 something like:
10436 template <int I>
10437 void foo(S<I>, S<I + 2>);
10439 This is a "nondeduced context":
10441 [deduct.type]
10443 The nondeduced contexts are:
10445 --A type that is a template-id in which one or more of
10446 the template-arguments is an expression that references
10447 a template-parameter.
10449 In these cases, we assume deduction succeeded, but don't
10450 actually infer any unifications. */
10452 if (!uses_template_parms (parm)
10453 && !template_args_equal (parm, arg))
10454 return 1;
10455 else
10456 return 0;
10460 /* Note that DECL can be defined in this translation unit, if
10461 required. */
10463 static void
10464 mark_definable (tree decl)
10466 tree clone;
10467 DECL_NOT_REALLY_EXTERN (decl) = 1;
10468 FOR_EACH_CLONE (clone, decl)
10469 DECL_NOT_REALLY_EXTERN (clone) = 1;
10472 /* Called if RESULT is explicitly instantiated, or is a member of an
10473 explicitly instantiated class. */
10475 void
10476 mark_decl_instantiated (tree result, int extern_p)
10478 SET_DECL_EXPLICIT_INSTANTIATION (result);
10480 /* If this entity has already been written out, it's too late to
10481 make any modifications. */
10482 if (TREE_ASM_WRITTEN (result))
10483 return;
10485 if (TREE_CODE (result) != FUNCTION_DECL)
10486 /* The TREE_PUBLIC flag for function declarations will have been
10487 set correctly by tsubst. */
10488 TREE_PUBLIC (result) = 1;
10490 /* This might have been set by an earlier implicit instantiation. */
10491 DECL_COMDAT (result) = 0;
10493 if (extern_p)
10494 DECL_NOT_REALLY_EXTERN (result) = 0;
10495 else
10497 mark_definable (result);
10498 /* Always make artificials weak. */
10499 if (DECL_ARTIFICIAL (result) && flag_weak)
10500 comdat_linkage (result);
10501 /* For WIN32 we also want to put explicit instantiations in
10502 linkonce sections. */
10503 else if (TREE_PUBLIC (result))
10504 maybe_make_one_only (result);
10507 /* If EXTERN_P, then this function will not be emitted -- unless
10508 followed by an explicit instantiation, at which point its linkage
10509 will be adjusted. If !EXTERN_P, then this function will be
10510 emitted here. In neither circumstance do we want
10511 import_export_decl to adjust the linkage. */
10512 DECL_INTERFACE_KNOWN (result) = 1;
10515 /* Given two function templates PAT1 and PAT2, return:
10517 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10518 -1 if PAT2 is more specialized than PAT1.
10519 0 if neither is more specialized.
10521 LEN indicates the number of parameters we should consider
10522 (defaulted parameters should not be considered).
10524 The 1998 std underspecified function template partial ordering, and
10525 DR214 addresses the issue. We take pairs of arguments, one from
10526 each of the templates, and deduce them against each other. One of
10527 the templates will be more specialized if all the *other*
10528 template's arguments deduce against its arguments and at least one
10529 of its arguments *does* *not* deduce against the other template's
10530 corresponding argument. Deduction is done as for class templates.
10531 The arguments used in deduction have reference and top level cv
10532 qualifiers removed. Iff both arguments were originally reference
10533 types *and* deduction succeeds in both directions, the template
10534 with the more cv-qualified argument wins for that pairing (if
10535 neither is more cv-qualified, they both are equal). Unlike regular
10536 deduction, after all the arguments have been deduced in this way,
10537 we do *not* verify the deduced template argument values can be
10538 substituted into non-deduced contexts, nor do we have to verify
10539 that all template arguments have been deduced. */
10542 more_specialized_fn (tree pat1, tree pat2, int len)
10544 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10545 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10546 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10547 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10548 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10549 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10550 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10551 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10552 int better1 = 0;
10553 int better2 = 0;
10555 /* Remove the this parameter from non-static member functions. If
10556 one is a non-static member function and the other is not a static
10557 member function, remove the first parameter from that function
10558 also. This situation occurs for operator functions where we
10559 locate both a member function (with this pointer) and non-member
10560 operator (with explicit first operand). */
10561 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10563 len--; /* LEN is the number of significant arguments for DECL1 */
10564 args1 = TREE_CHAIN (args1);
10565 if (!DECL_STATIC_FUNCTION_P (decl2))
10566 args2 = TREE_CHAIN (args2);
10568 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10570 args2 = TREE_CHAIN (args2);
10571 if (!DECL_STATIC_FUNCTION_P (decl1))
10573 len--;
10574 args1 = TREE_CHAIN (args1);
10578 /* If only one is a conversion operator, they are unordered. */
10579 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10580 return 0;
10582 /* Consider the return type for a conversion function */
10583 if (DECL_CONV_FN_P (decl1))
10585 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10586 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10587 len++;
10590 processing_template_decl++;
10592 while (len--)
10594 tree arg1 = TREE_VALUE (args1);
10595 tree arg2 = TREE_VALUE (args2);
10596 int deduce1, deduce2;
10597 int quals1 = -1;
10598 int quals2 = -1;
10600 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10602 arg1 = TREE_TYPE (arg1);
10603 quals1 = cp_type_quals (arg1);
10606 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10608 arg2 = TREE_TYPE (arg2);
10609 quals2 = cp_type_quals (arg2);
10612 if ((quals1 < 0) != (quals2 < 0))
10614 /* Only of the args is a reference, see if we should apply
10615 array/function pointer decay to it. This is not part of
10616 DR214, but is, IMHO, consistent with the deduction rules
10617 for the function call itself, and with our earlier
10618 implementation of the underspecified partial ordering
10619 rules. (nathan). */
10620 if (quals1 >= 0)
10622 switch (TREE_CODE (arg1))
10624 case ARRAY_TYPE:
10625 arg1 = TREE_TYPE (arg1);
10626 /* FALLTHROUGH. */
10627 case FUNCTION_TYPE:
10628 arg1 = build_pointer_type (arg1);
10629 break;
10631 default:
10632 break;
10635 else
10637 switch (TREE_CODE (arg2))
10639 case ARRAY_TYPE:
10640 arg2 = TREE_TYPE (arg2);
10641 /* FALLTHROUGH. */
10642 case FUNCTION_TYPE:
10643 arg2 = build_pointer_type (arg2);
10644 break;
10646 default:
10647 break;
10652 arg1 = TYPE_MAIN_VARIANT (arg1);
10653 arg2 = TYPE_MAIN_VARIANT (arg2);
10655 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10656 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10658 if (!deduce1)
10659 better2 = -1;
10660 if (!deduce2)
10661 better1 = -1;
10662 if (better1 < 0 && better2 < 0)
10663 /* We've failed to deduce something in either direction.
10664 These must be unordered. */
10665 break;
10667 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10669 /* Deduces in both directions, see if quals can
10670 disambiguate. Pretend the worse one failed to deduce. */
10671 if ((quals1 & quals2) == quals2)
10672 deduce1 = 0;
10673 if ((quals1 & quals2) == quals1)
10674 deduce2 = 0;
10676 if (deduce1 && !deduce2 && !better2)
10677 better2 = 1;
10678 if (deduce2 && !deduce1 && !better1)
10679 better1 = 1;
10681 args1 = TREE_CHAIN (args1);
10682 args2 = TREE_CHAIN (args2);
10685 processing_template_decl--;
10687 return (better1 > 0) - (better2 > 0);
10690 /* Determine which of two partial specializations is more specialized.
10692 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
10693 to the first partial specialization. The TREE_VALUE is the
10694 innermost set of template parameters for the partial
10695 specialization. PAT2 is similar, but for the second template.
10697 Return 1 if the first partial specialization is more specialized;
10698 -1 if the second is more specialized; 0 if neither is more
10699 specialized.
10701 See [temp.class.order] for information about determining which of
10702 two templates is more specialized. */
10704 static int
10705 more_specialized_class (tree pat1, tree pat2)
10707 tree targs;
10708 tree tmpl1, tmpl2;
10709 int winner = 0;
10711 tmpl1 = TREE_TYPE (pat1);
10712 tmpl2 = TREE_TYPE (pat2);
10714 /* Just like what happens for functions, if we are ordering between
10715 different class template specializations, we may encounter dependent
10716 types in the arguments, and we need our dependency check functions
10717 to behave correctly. */
10718 ++processing_template_decl;
10719 targs = get_class_bindings (TREE_VALUE (pat1),
10720 CLASSTYPE_TI_ARGS (tmpl1),
10721 CLASSTYPE_TI_ARGS (tmpl2));
10722 if (targs)
10723 --winner;
10725 targs = get_class_bindings (TREE_VALUE (pat2),
10726 CLASSTYPE_TI_ARGS (tmpl2),
10727 CLASSTYPE_TI_ARGS (tmpl1));
10728 if (targs)
10729 ++winner;
10730 --processing_template_decl;
10732 return winner;
10735 /* Return the template arguments that will produce the function signature
10736 DECL from the function template FN, with the explicit template
10737 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
10738 also match. Return NULL_TREE if no satisfactory arguments could be
10739 found. */
10741 static tree
10742 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
10744 int ntparms = DECL_NTPARMS (fn);
10745 tree targs = make_tree_vec (ntparms);
10746 tree decl_type;
10747 tree decl_arg_types;
10749 /* Substitute the explicit template arguments into the type of DECL.
10750 The call to fn_type_unification will handle substitution into the
10751 FN. */
10752 decl_type = TREE_TYPE (decl);
10753 if (explicit_args && uses_template_parms (decl_type))
10755 tree tmpl;
10756 tree converted_args;
10758 if (DECL_TEMPLATE_INFO (decl))
10759 tmpl = DECL_TI_TEMPLATE (decl);
10760 else
10761 /* We can get here for some invalid specializations. */
10762 return NULL_TREE;
10764 converted_args
10765 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10766 explicit_args, NULL_TREE,
10767 tf_none,
10768 /*require_all_args=*/false,
10769 /*use_default_args=*/false);
10770 if (converted_args == error_mark_node)
10771 return NULL_TREE;
10773 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10774 if (decl_type == error_mark_node)
10775 return NULL_TREE;
10778 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10779 /* Never do unification on the 'this' parameter. */
10780 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10781 decl_arg_types = TREE_CHAIN (decl_arg_types);
10783 if (fn_type_unification (fn, explicit_args, targs,
10784 decl_arg_types,
10785 (check_rettype || DECL_CONV_FN_P (fn)
10786 ? TREE_TYPE (decl_type) : NULL_TREE),
10787 DEDUCE_EXACT, LOOKUP_NORMAL))
10788 return NULL_TREE;
10790 return targs;
10793 /* Return the innermost template arguments that, when applied to a
10794 template specialization whose innermost template parameters are
10795 TPARMS, and whose specialization arguments are PARMS, yield the
10796 ARGS.
10798 For example, suppose we have:
10800 template <class T, class U> struct S {};
10801 template <class T> struct S<T*, int> {};
10803 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10804 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
10805 int}. The resulting vector will be {double}, indicating that `T'
10806 is bound to `double'. */
10808 static tree
10809 get_class_bindings (tree tparms, tree spec_args, tree args)
10811 int i, ntparms = TREE_VEC_LENGTH (tparms);
10812 tree deduced_args;
10813 tree innermost_deduced_args;
10815 innermost_deduced_args = make_tree_vec (ntparms);
10816 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10818 deduced_args = copy_node (args);
10819 SET_TMPL_ARGS_LEVEL (deduced_args,
10820 TMPL_ARGS_DEPTH (deduced_args),
10821 innermost_deduced_args);
10823 else
10824 deduced_args = innermost_deduced_args;
10826 if (unify (tparms, deduced_args,
10827 INNERMOST_TEMPLATE_ARGS (spec_args),
10828 INNERMOST_TEMPLATE_ARGS (args),
10829 UNIFY_ALLOW_NONE))
10830 return NULL_TREE;
10832 for (i = 0; i < ntparms; ++i)
10833 if (! TREE_VEC_ELT (innermost_deduced_args, i))
10834 return NULL_TREE;
10836 /* Verify that nondeduced template arguments agree with the type
10837 obtained from argument deduction.
10839 For example:
10841 struct A { typedef int X; };
10842 template <class T, class U> struct C {};
10843 template <class T> struct C<T, typename T::X> {};
10845 Then with the instantiation `C<A, int>', we can deduce that
10846 `T' is `A' but unify () does not check whether `typename T::X'
10847 is `int'. */
10848 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
10849 if (spec_args == error_mark_node
10850 /* We only need to check the innermost arguments; the other
10851 arguments will always agree. */
10852 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
10853 INNERMOST_TEMPLATE_ARGS (args)))
10854 return NULL_TREE;
10856 return deduced_args;
10859 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
10860 Return the TREE_LIST node with the most specialized template, if
10861 any. If there is no most specialized template, the error_mark_node
10862 is returned.
10864 Note that this function does not look at, or modify, the
10865 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
10866 returned is one of the elements of INSTANTIATIONS, callers may
10867 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
10868 and retrieve it from the value returned. */
10870 tree
10871 most_specialized_instantiation (tree templates)
10873 tree fn, champ;
10875 ++processing_template_decl;
10877 champ = templates;
10878 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
10880 int fate = 0;
10882 if (get_bindings (TREE_VALUE (champ),
10883 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10884 NULL_TREE, /*check_ret=*/false))
10885 fate--;
10887 if (get_bindings (TREE_VALUE (fn),
10888 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10889 NULL_TREE, /*check_ret=*/false))
10890 fate++;
10892 if (fate == -1)
10893 champ = fn;
10894 else if (!fate)
10896 /* Equally specialized, move to next function. If there
10897 is no next function, nothing's most specialized. */
10898 fn = TREE_CHAIN (fn);
10899 champ = fn;
10900 if (!fn)
10901 break;
10905 if (champ)
10906 /* Now verify that champ is better than everything earlier in the
10907 instantiation list. */
10908 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
10909 if (get_bindings (TREE_VALUE (champ),
10910 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10911 NULL_TREE, /*check_ret=*/false)
10912 || !get_bindings (TREE_VALUE (fn),
10913 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10914 NULL_TREE, /*check_ret=*/false))
10916 champ = NULL_TREE;
10917 break;
10920 processing_template_decl--;
10922 if (!champ)
10923 return error_mark_node;
10925 return champ;
10928 /* If DECL is a specialization of some template, return the most
10929 general such template. Otherwise, returns NULL_TREE.
10931 For example, given:
10933 template <class T> struct S { template <class U> void f(U); };
10935 if TMPL is `template <class U> void S<int>::f(U)' this will return
10936 the full template. This function will not trace past partial
10937 specializations, however. For example, given in addition:
10939 template <class T> struct S<T*> { template <class U> void f(U); };
10941 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10942 `template <class T> template <class U> S<T*>::f(U)'. */
10944 tree
10945 most_general_template (tree decl)
10947 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10948 an immediate specialization. */
10949 if (TREE_CODE (decl) == FUNCTION_DECL)
10951 if (DECL_TEMPLATE_INFO (decl)) {
10952 decl = DECL_TI_TEMPLATE (decl);
10954 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10955 template friend. */
10956 if (TREE_CODE (decl) != TEMPLATE_DECL)
10957 return NULL_TREE;
10958 } else
10959 return NULL_TREE;
10962 /* Look for more and more general templates. */
10963 while (DECL_TEMPLATE_INFO (decl))
10965 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
10966 (See cp-tree.h for details.) */
10967 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10968 break;
10970 if (CLASS_TYPE_P (TREE_TYPE (decl))
10971 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10972 break;
10974 /* Stop if we run into an explicitly specialized class template. */
10975 if (!DECL_NAMESPACE_SCOPE_P (decl)
10976 && DECL_CONTEXT (decl)
10977 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10978 break;
10980 decl = DECL_TI_TEMPLATE (decl);
10983 return decl;
10986 /* Return the most specialized of the class template partial
10987 specializations of TMPL which can produce TYPE, a specialization of
10988 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
10989 a _TYPE node corresponding to the partial specialization, while the
10990 TREE_PURPOSE is the set of template arguments that must be
10991 substituted into the TREE_TYPE in order to generate TYPE.
10993 If the choice of partial specialization is ambiguous, a diagnostic
10994 is issued, and the error_mark_node is returned. If there are no
10995 partial specializations of TMPL matching TYPE, then NULL_TREE is
10996 returned. */
10998 static tree
10999 most_specialized_class (tree type, tree tmpl)
11001 tree list = NULL_TREE;
11002 tree t;
11003 tree champ;
11004 int fate;
11005 bool ambiguous_p;
11006 tree args;
11008 tmpl = most_general_template (tmpl);
11009 args = CLASSTYPE_TI_ARGS (type);
11010 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11012 tree partial_spec_args;
11013 tree spec_args;
11015 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11016 spec_args = get_class_bindings (TREE_VALUE (t),
11017 partial_spec_args,
11018 args);
11019 if (spec_args)
11021 list = tree_cons (spec_args, TREE_VALUE (t), list);
11022 TREE_TYPE (list) = TREE_TYPE (t);
11026 if (! list)
11027 return NULL_TREE;
11029 ambiguous_p = false;
11030 t = list;
11031 champ = t;
11032 t = TREE_CHAIN (t);
11033 for (; t; t = TREE_CHAIN (t))
11035 fate = more_specialized_class (champ, t);
11036 if (fate == 1)
11038 else
11040 if (fate == 0)
11042 t = TREE_CHAIN (t);
11043 if (! t)
11045 ambiguous_p = true;
11046 break;
11049 champ = t;
11053 if (!ambiguous_p)
11054 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11056 fate = more_specialized_class (champ, t);
11057 if (fate != 1)
11059 ambiguous_p = true;
11060 break;
11064 if (ambiguous_p)
11066 const char *str = "candidates are:";
11067 error ("ambiguous class template instantiation for %q#T", type);
11068 for (t = list; t; t = TREE_CHAIN (t))
11070 error ("%s %+#T", str, TREE_TYPE (t));
11071 str = " ";
11073 return error_mark_node;
11076 return champ;
11079 /* Explicitly instantiate DECL. */
11081 void
11082 do_decl_instantiation (tree decl, tree storage)
11084 tree result = NULL_TREE;
11085 int extern_p = 0;
11087 if (!decl)
11088 /* An error occurred, for which grokdeclarator has already issued
11089 an appropriate message. */
11090 return;
11091 else if (! DECL_LANG_SPECIFIC (decl))
11093 error ("explicit instantiation of non-template %q#D", decl);
11094 return;
11096 else if (TREE_CODE (decl) == VAR_DECL)
11098 /* There is an asymmetry here in the way VAR_DECLs and
11099 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11100 the latter, the DECL we get back will be marked as a
11101 template instantiation, and the appropriate
11102 DECL_TEMPLATE_INFO will be set up. This does not happen for
11103 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11104 should handle VAR_DECLs as it currently handles
11105 FUNCTION_DECLs. */
11106 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11107 if (!result || TREE_CODE (result) != VAR_DECL)
11109 error ("no matching template for %qD found", decl);
11110 return;
11113 else if (TREE_CODE (decl) != FUNCTION_DECL)
11115 error ("explicit instantiation of %q#D", decl);
11116 return;
11118 else
11119 result = decl;
11121 /* Check for various error cases. Note that if the explicit
11122 instantiation is valid the RESULT will currently be marked as an
11123 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11124 until we get here. */
11126 if (DECL_TEMPLATE_SPECIALIZATION (result))
11128 /* DR 259 [temp.spec].
11130 Both an explicit instantiation and a declaration of an explicit
11131 specialization shall not appear in a program unless the explicit
11132 instantiation follows a declaration of the explicit specialization.
11134 For a given set of template parameters, if an explicit
11135 instantiation of a template appears after a declaration of an
11136 explicit specialization for that template, the explicit
11137 instantiation has no effect. */
11138 return;
11140 else if (DECL_EXPLICIT_INSTANTIATION (result))
11142 /* [temp.spec]
11144 No program shall explicitly instantiate any template more
11145 than once.
11147 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11148 the first instantiation was `extern' and the second is not,
11149 and EXTERN_P for the opposite case. */
11150 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11151 pedwarn ("duplicate explicit instantiation of %q#D", result);
11152 /* If an "extern" explicit instantiation follows an ordinary
11153 explicit instantiation, the template is instantiated. */
11154 if (extern_p)
11155 return;
11157 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11159 error ("no matching template for %qD found", result);
11160 return;
11162 else if (!DECL_TEMPLATE_INFO (result))
11164 pedwarn ("explicit instantiation of non-template %q#D", result);
11165 return;
11168 if (storage == NULL_TREE)
11170 else if (storage == ridpointers[(int) RID_EXTERN])
11172 if (pedantic && !in_system_header)
11173 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11174 "instantiations");
11175 extern_p = 1;
11177 else
11178 error ("storage class %qD applied to template instantiation", storage);
11180 check_explicit_instantiation_namespace (result);
11181 mark_decl_instantiated (result, extern_p);
11182 if (! extern_p)
11183 instantiate_decl (result, /*defer_ok=*/1,
11184 /*expl_inst_class_mem_p=*/false);
11187 static void
11188 mark_class_instantiated (tree t, int extern_p)
11190 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11191 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11192 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11193 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11194 if (! extern_p)
11196 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11197 rest_of_type_compilation (t, 1);
11201 /* Called from do_type_instantiation through binding_table_foreach to
11202 do recursive instantiation for the type bound in ENTRY. */
11203 static void
11204 bt_instantiate_type_proc (binding_entry entry, void *data)
11206 tree storage = *(tree *) data;
11208 if (IS_AGGR_TYPE (entry->type)
11209 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11210 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11213 /* Called from do_type_instantiation to instantiate a member
11214 (a member function or a static member variable) of an
11215 explicitly instantiated class template. */
11216 static void
11217 instantiate_class_member (tree decl, int extern_p)
11219 mark_decl_instantiated (decl, extern_p);
11220 if (! extern_p)
11221 instantiate_decl (decl, /*defer_ok=*/1,
11222 /*expl_inst_class_mem_p=*/true);
11225 /* Perform an explicit instantiation of template class T. STORAGE, if
11226 non-null, is the RID for extern, inline or static. COMPLAIN is
11227 nonzero if this is called from the parser, zero if called recursively,
11228 since the standard is unclear (as detailed below). */
11230 void
11231 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11233 int extern_p = 0;
11234 int nomem_p = 0;
11235 int static_p = 0;
11236 int previous_instantiation_extern_p = 0;
11238 if (TREE_CODE (t) == TYPE_DECL)
11239 t = TREE_TYPE (t);
11241 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11243 error ("explicit instantiation of non-template type %qT", t);
11244 return;
11247 complete_type (t);
11249 if (!COMPLETE_TYPE_P (t))
11251 if (complain & tf_error)
11252 error ("explicit instantiation of %q#T before definition of template",
11254 return;
11257 if (storage != NULL_TREE)
11259 if (pedantic && !in_system_header)
11260 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11261 storage);
11263 if (storage == ridpointers[(int) RID_INLINE])
11264 nomem_p = 1;
11265 else if (storage == ridpointers[(int) RID_EXTERN])
11266 extern_p = 1;
11267 else if (storage == ridpointers[(int) RID_STATIC])
11268 static_p = 1;
11269 else
11271 error ("storage class %qD applied to template instantiation",
11272 storage);
11273 extern_p = 0;
11277 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11279 /* DR 259 [temp.spec].
11281 Both an explicit instantiation and a declaration of an explicit
11282 specialization shall not appear in a program unless the explicit
11283 instantiation follows a declaration of the explicit specialization.
11285 For a given set of template parameters, if an explicit
11286 instantiation of a template appears after a declaration of an
11287 explicit specialization for that template, the explicit
11288 instantiation has no effect. */
11289 return;
11291 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11293 /* [temp.spec]
11295 No program shall explicitly instantiate any template more
11296 than once.
11298 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11299 instantiation was `extern'. If EXTERN_P then the second is.
11300 These cases are OK. */
11301 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11303 if (!previous_instantiation_extern_p && !extern_p
11304 && (complain & tf_error))
11305 pedwarn ("duplicate explicit instantiation of %q#T", t);
11307 /* If we've already instantiated the template, just return now. */
11308 if (!CLASSTYPE_INTERFACE_ONLY (t))
11309 return;
11312 check_explicit_instantiation_namespace (TYPE_NAME (t));
11313 mark_class_instantiated (t, extern_p);
11315 if (nomem_p)
11316 return;
11319 tree tmp;
11321 /* In contrast to implicit instantiation, where only the
11322 declarations, and not the definitions, of members are
11323 instantiated, we have here:
11325 [temp.explicit]
11327 The explicit instantiation of a class template specialization
11328 implies the instantiation of all of its members not
11329 previously explicitly specialized in the translation unit
11330 containing the explicit instantiation.
11332 Of course, we can't instantiate member template classes, since
11333 we don't have any arguments for them. Note that the standard
11334 is unclear on whether the instantiation of the members are
11335 *explicit* instantiations or not. However, the most natural
11336 interpretation is that it should be an explicit instantiation. */
11338 if (! static_p)
11339 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11340 if (TREE_CODE (tmp) == FUNCTION_DECL
11341 && DECL_TEMPLATE_INSTANTIATION (tmp))
11342 instantiate_class_member (tmp, extern_p);
11344 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11345 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11346 instantiate_class_member (tmp, extern_p);
11348 if (CLASSTYPE_NESTED_UTDS (t))
11349 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11350 bt_instantiate_type_proc, &storage);
11354 /* Given a function DECL, which is a specialization of TMPL, modify
11355 DECL to be a re-instantiation of TMPL with the same template
11356 arguments. TMPL should be the template into which tsubst'ing
11357 should occur for DECL, not the most general template.
11359 One reason for doing this is a scenario like this:
11361 template <class T>
11362 void f(const T&, int i);
11364 void g() { f(3, 7); }
11366 template <class T>
11367 void f(const T& t, const int i) { }
11369 Note that when the template is first instantiated, with
11370 instantiate_template, the resulting DECL will have no name for the
11371 first parameter, and the wrong type for the second. So, when we go
11372 to instantiate the DECL, we regenerate it. */
11374 static void
11375 regenerate_decl_from_template (tree decl, tree tmpl)
11377 /* The arguments used to instantiate DECL, from the most general
11378 template. */
11379 tree args;
11380 tree code_pattern;
11382 args = DECL_TI_ARGS (decl);
11383 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11385 /* Make sure that we can see identifiers, and compute access
11386 correctly. */
11387 push_access_scope (decl);
11389 if (TREE_CODE (decl) == FUNCTION_DECL)
11391 tree decl_parm;
11392 tree pattern_parm;
11393 tree specs;
11394 int args_depth;
11395 int parms_depth;
11397 args_depth = TMPL_ARGS_DEPTH (args);
11398 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11399 if (args_depth > parms_depth)
11400 args = get_innermost_template_args (args, parms_depth);
11402 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11403 args, tf_error, NULL_TREE);
11404 if (specs)
11405 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11406 specs);
11408 /* Merge parameter declarations. */
11409 decl_parm = skip_artificial_parms_for (decl,
11410 DECL_ARGUMENTS (decl));
11411 pattern_parm
11412 = skip_artificial_parms_for (code_pattern,
11413 DECL_ARGUMENTS (code_pattern));
11414 while (decl_parm)
11416 tree parm_type;
11417 tree attributes;
11419 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11420 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11421 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11422 NULL_TREE);
11423 parm_type = type_decays_to (parm_type);
11424 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11425 TREE_TYPE (decl_parm) = parm_type;
11426 attributes = DECL_ATTRIBUTES (pattern_parm);
11427 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11429 DECL_ATTRIBUTES (decl_parm) = attributes;
11430 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11432 decl_parm = TREE_CHAIN (decl_parm);
11433 pattern_parm = TREE_CHAIN (pattern_parm);
11436 /* Merge additional specifiers from the CODE_PATTERN. */
11437 if (DECL_DECLARED_INLINE_P (code_pattern)
11438 && !DECL_DECLARED_INLINE_P (decl))
11439 DECL_DECLARED_INLINE_P (decl) = 1;
11440 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11441 DECL_INLINE (decl) = 1;
11443 else if (TREE_CODE (decl) == VAR_DECL)
11444 DECL_INITIAL (decl) =
11445 tsubst_expr (DECL_INITIAL (code_pattern), args,
11446 tf_error, DECL_TI_TEMPLATE (decl));
11447 else
11448 gcc_unreachable ();
11450 pop_access_scope (decl);
11453 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11454 substituted to get DECL. */
11456 tree
11457 template_for_substitution (tree decl)
11459 tree tmpl = DECL_TI_TEMPLATE (decl);
11461 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11462 for the instantiation. This is not always the most general
11463 template. Consider, for example:
11465 template <class T>
11466 struct S { template <class U> void f();
11467 template <> void f<int>(); };
11469 and an instantiation of S<double>::f<int>. We want TD to be the
11470 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11471 while (/* An instantiation cannot have a definition, so we need a
11472 more general template. */
11473 DECL_TEMPLATE_INSTANTIATION (tmpl)
11474 /* We must also deal with friend templates. Given:
11476 template <class T> struct S {
11477 template <class U> friend void f() {};
11480 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11481 so far as the language is concerned, but that's still
11482 where we get the pattern for the instantiation from. On
11483 other hand, if the definition comes outside the class, say:
11485 template <class T> struct S {
11486 template <class U> friend void f();
11488 template <class U> friend void f() {}
11490 we don't need to look any further. That's what the check for
11491 DECL_INITIAL is for. */
11492 || (TREE_CODE (decl) == FUNCTION_DECL
11493 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11494 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11496 /* The present template, TD, should not be a definition. If it
11497 were a definition, we should be using it! Note that we
11498 cannot restructure the loop to just keep going until we find
11499 a template with a definition, since that might go too far if
11500 a specialization was declared, but not defined. */
11501 gcc_assert (TREE_CODE (decl) != VAR_DECL
11502 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11504 /* Fetch the more general template. */
11505 tmpl = DECL_TI_TEMPLATE (tmpl);
11508 return tmpl;
11511 /* Produce the definition of D, a _DECL generated from a template. If
11512 DEFER_OK is nonzero, then we don't have to actually do the
11513 instantiation now; we just have to do it sometime. Normally it is
11514 an error if this is an explicit instantiation but D is undefined.
11515 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11516 explicitly instantiated class template. */
11518 tree
11519 instantiate_decl (tree d, int defer_ok,
11520 bool expl_inst_class_mem_p)
11522 tree tmpl = DECL_TI_TEMPLATE (d);
11523 tree gen_args;
11524 tree args;
11525 tree td;
11526 tree code_pattern;
11527 tree spec;
11528 tree gen_tmpl;
11529 bool pattern_defined;
11530 int need_push;
11531 location_t saved_loc = input_location;
11532 bool external_p;
11534 /* This function should only be used to instantiate templates for
11535 functions and static member variables. */
11536 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11537 || TREE_CODE (d) == VAR_DECL);
11539 /* Variables are never deferred; if instantiation is required, they
11540 are instantiated right away. That allows for better code in the
11541 case that an expression refers to the value of the variable --
11542 if the variable has a constant value the referring expression can
11543 take advantage of that fact. */
11544 if (TREE_CODE (d) == VAR_DECL)
11545 defer_ok = 0;
11547 /* Don't instantiate cloned functions. Instead, instantiate the
11548 functions they cloned. */
11549 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11550 d = DECL_CLONED_FUNCTION (d);
11552 if (DECL_TEMPLATE_INSTANTIATED (d))
11553 /* D has already been instantiated. It might seem reasonable to
11554 check whether or not D is an explicit instantiation, and, if so,
11555 stop here. But when an explicit instantiation is deferred
11556 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11557 is set, even though we still need to do the instantiation. */
11558 return d;
11560 /* If we already have a specialization of this declaration, then
11561 there's no reason to instantiate it. Note that
11562 retrieve_specialization gives us both instantiations and
11563 specializations, so we must explicitly check
11564 DECL_TEMPLATE_SPECIALIZATION. */
11565 gen_tmpl = most_general_template (tmpl);
11566 gen_args = DECL_TI_ARGS (d);
11567 spec = retrieve_specialization (gen_tmpl, gen_args,
11568 /*class_specializations_p=*/false);
11569 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11570 return spec;
11572 /* This needs to happen before any tsubsting. */
11573 if (! push_tinst_level (d))
11574 return d;
11576 timevar_push (TV_PARSE);
11578 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11579 for the instantiation. */
11580 td = template_for_substitution (d);
11581 code_pattern = DECL_TEMPLATE_RESULT (td);
11583 /* We should never be trying to instantiate a member of a class
11584 template or partial specialization. */
11585 gcc_assert (d != code_pattern);
11587 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11588 || DECL_TEMPLATE_SPECIALIZATION (td))
11589 /* In the case of a friend template whose definition is provided
11590 outside the class, we may have too many arguments. Drop the
11591 ones we don't need. The same is true for specializations. */
11592 args = get_innermost_template_args
11593 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11594 else
11595 args = gen_args;
11597 if (TREE_CODE (d) == FUNCTION_DECL)
11598 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11599 else
11600 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11602 /* We may be in the middle of deferred access check. Disable it now. */
11603 push_deferring_access_checks (dk_no_deferred);
11605 /* Unless an explicit instantiation directive has already determined
11606 the linkage of D, remember that a definition is available for
11607 this entity. */
11608 if (pattern_defined
11609 && !DECL_INTERFACE_KNOWN (d)
11610 && !DECL_NOT_REALLY_EXTERN (d))
11611 mark_definable (d);
11613 input_location = DECL_SOURCE_LOCATION (d);
11615 /* If D is a member of an explicitly instantiated class template,
11616 and no definition is available, treat it like an implicit
11617 instantiation. */
11618 if (!pattern_defined && expl_inst_class_mem_p
11619 && DECL_EXPLICIT_INSTANTIATION (d))
11621 DECL_NOT_REALLY_EXTERN (d) = 0;
11622 DECL_INTERFACE_KNOWN (d) = 0;
11623 SET_DECL_IMPLICIT_INSTANTIATION (d);
11626 if (!defer_ok)
11628 /* Recheck the substitutions to obtain any warning messages
11629 about ignoring cv qualifiers. */
11630 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11631 tree type = TREE_TYPE (gen);
11633 /* Make sure that we can see identifiers, and compute access
11634 correctly. D is already the target FUNCTION_DECL with the
11635 right context. */
11636 push_access_scope (d);
11638 if (TREE_CODE (gen) == FUNCTION_DECL)
11640 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
11641 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11642 tf_warning_or_error, d);
11643 /* Don't simply tsubst the function type, as that will give
11644 duplicate warnings about poor parameter qualifications.
11645 The function arguments are the same as the decl_arguments
11646 without the top level cv qualifiers. */
11647 type = TREE_TYPE (type);
11649 tsubst (type, gen_args, tf_warning_or_error, d);
11651 pop_access_scope (d);
11654 /* Check to see whether we know that this template will be
11655 instantiated in some other file, as with "extern template"
11656 extension. */
11657 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
11658 /* In general, we do not instantiate such templates... */
11659 if (external_p
11660 /* ... but we instantiate inline functions so that we can inline
11661 them and ... */
11662 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
11663 /* ... we instantiate static data members whose values are
11664 needed in integral constant expressions. */
11665 && ! (TREE_CODE (d) == VAR_DECL
11666 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
11667 goto out;
11668 /* Defer all other templates, unless we have been explicitly
11669 forbidden from doing so. */
11670 if (/* If there is no definition, we cannot instantiate the
11671 template. */
11672 ! pattern_defined
11673 /* If it's OK to postpone instantiation, do so. */
11674 || defer_ok
11675 /* If this is a static data member that will be defined
11676 elsewhere, we don't want to instantiate the entire data
11677 member, but we do want to instantiate the initializer so that
11678 we can substitute that elsewhere. */
11679 || (external_p && TREE_CODE (d) == VAR_DECL))
11681 /* The definition of the static data member is now required so
11682 we must substitute the initializer. */
11683 if (TREE_CODE (d) == VAR_DECL
11684 && !DECL_INITIAL (d)
11685 && DECL_INITIAL (code_pattern))
11687 tree ns;
11688 tree init;
11690 ns = decl_namespace_context (d);
11691 push_nested_namespace (ns);
11692 push_nested_class (DECL_CONTEXT (d));
11693 init = tsubst_expr (DECL_INITIAL (code_pattern),
11694 args,
11695 tf_warning_or_error, NULL_TREE);
11696 DECL_INITIAL (d) = init;
11697 cp_finish_decl (d, init, /*asmspec_tree=*/NULL_TREE,
11698 LOOKUP_ONLYCONVERTING);
11699 pop_nested_class ();
11700 pop_nested_namespace (ns);
11703 /* We restore the source position here because it's used by
11704 add_pending_template. */
11705 input_location = saved_loc;
11707 if (at_eof && !pattern_defined
11708 && DECL_EXPLICIT_INSTANTIATION (d))
11709 /* [temp.explicit]
11711 The definition of a non-exported function template, a
11712 non-exported member function template, or a non-exported
11713 member function or static data member of a class template
11714 shall be present in every translation unit in which it is
11715 explicitly instantiated. */
11716 pedwarn
11717 ("explicit instantiation of %qD but no definition available", d);
11719 /* ??? Historically, we have instantiated inline functions, even
11720 when marked as "extern template". */
11721 if (!(external_p && TREE_CODE (d) == VAR_DECL))
11722 add_pending_template (d);
11723 goto out;
11725 /* Tell the repository that D is available in this translation unit
11726 -- and see if it is supposed to be instantiated here. */
11727 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11729 /* In a PCH file, despite the fact that the repository hasn't
11730 requested instantiation in the PCH it is still possible that
11731 an instantiation will be required in a file that includes the
11732 PCH. */
11733 if (pch_file)
11734 add_pending_template (d);
11735 /* Instantiate inline functions so that the inliner can do its
11736 job, even though we'll not be emitting a copy of this
11737 function. */
11738 if (!(TREE_CODE (d) == FUNCTION_DECL
11739 && flag_inline_trees
11740 && DECL_DECLARED_INLINE_P (d)))
11741 goto out;
11744 need_push = !cfun || !global_bindings_p ();
11745 if (need_push)
11746 push_to_top_level ();
11748 /* Mark D as instantiated so that recursive calls to
11749 instantiate_decl do not try to instantiate it again. */
11750 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11752 /* Regenerate the declaration in case the template has been modified
11753 by a subsequent redeclaration. */
11754 regenerate_decl_from_template (d, td);
11756 /* We already set the file and line above. Reset them now in case
11757 they changed as a result of calling regenerate_decl_from_template. */
11758 input_location = DECL_SOURCE_LOCATION (d);
11760 if (TREE_CODE (d) == VAR_DECL)
11762 /* Clear out DECL_RTL; whatever was there before may not be right
11763 since we've reset the type of the declaration. */
11764 SET_DECL_RTL (d, NULL_RTX);
11765 DECL_IN_AGGR_P (d) = 0;
11767 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11768 initializer. That function will defer actual emission until
11769 we have a chance to determine linkage. */
11770 DECL_EXTERNAL (d) = 0;
11772 /* Enter the scope of D so that access-checking works correctly. */
11773 push_nested_class (DECL_CONTEXT (d));
11774 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
11775 pop_nested_class ();
11777 else if (TREE_CODE (d) == FUNCTION_DECL)
11779 htab_t saved_local_specializations;
11780 tree subst_decl;
11781 tree tmpl_parm;
11782 tree spec_parm;
11784 /* Save away the current list, in case we are instantiating one
11785 template from within the body of another. */
11786 saved_local_specializations = local_specializations;
11788 /* Set up the list of local specializations. */
11789 local_specializations = htab_create (37,
11790 hash_local_specialization,
11791 eq_local_specializations,
11792 NULL);
11794 /* Set up context. */
11795 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11797 /* Create substitution entries for the parameters. */
11798 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11799 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11800 spec_parm = DECL_ARGUMENTS (d);
11801 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11803 register_local_specialization (spec_parm, tmpl_parm);
11804 spec_parm = skip_artificial_parms_for (d, spec_parm);
11805 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11807 while (tmpl_parm)
11809 register_local_specialization (spec_parm, tmpl_parm);
11810 tmpl_parm = TREE_CHAIN (tmpl_parm);
11811 spec_parm = TREE_CHAIN (spec_parm);
11813 gcc_assert (!spec_parm);
11815 /* Substitute into the body of the function. */
11816 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11817 tf_warning_or_error, tmpl);
11819 /* We don't need the local specializations any more. */
11820 htab_delete (local_specializations);
11821 local_specializations = saved_local_specializations;
11823 /* Finish the function. */
11824 d = finish_function (0);
11825 expand_or_defer_fn (d);
11828 /* We're not deferring instantiation any more. */
11829 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11831 if (need_push)
11832 pop_from_top_level ();
11834 out:
11835 input_location = saved_loc;
11836 pop_deferring_access_checks ();
11837 pop_tinst_level ();
11839 timevar_pop (TV_PARSE);
11841 return d;
11844 /* Run through the list of templates that we wish we could
11845 instantiate, and instantiate any we can. RETRIES is the
11846 number of times we retry pending template instantiation. */
11848 void
11849 instantiate_pending_templates (int retries)
11851 tree *t;
11852 tree last = NULL_TREE;
11853 int reconsider;
11854 location_t saved_loc = input_location;
11855 int saved_in_system_header = in_system_header;
11857 /* Instantiating templates may trigger vtable generation. This in turn
11858 may require further template instantiations. We place a limit here
11859 to avoid infinite loop. */
11860 if (pending_templates && retries >= max_tinst_depth)
11862 tree decl = TREE_VALUE (pending_templates);
11864 error ("template instantiation depth exceeds maximum of %d"
11865 " instantiating %q+D, possibly from virtual table generation"
11866 " (use -ftemplate-depth-NN to increase the maximum)",
11867 max_tinst_depth, decl);
11868 if (TREE_CODE (decl) == FUNCTION_DECL)
11869 /* Pretend that we defined it. */
11870 DECL_INITIAL (decl) = error_mark_node;
11871 return;
11876 reconsider = 0;
11878 t = &pending_templates;
11879 while (*t)
11881 tree instantiation = TREE_VALUE (*t);
11883 reopen_tinst_level (TREE_PURPOSE (*t));
11885 if (TYPE_P (instantiation))
11887 tree fn;
11889 if (!COMPLETE_TYPE_P (instantiation))
11891 instantiate_class_template (instantiation);
11892 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11893 for (fn = TYPE_METHODS (instantiation);
11895 fn = TREE_CHAIN (fn))
11896 if (! DECL_ARTIFICIAL (fn))
11897 instantiate_decl (fn,
11898 /*defer_ok=*/0,
11899 /*expl_inst_class_mem_p=*/false);
11900 if (COMPLETE_TYPE_P (instantiation))
11901 reconsider = 1;
11904 if (COMPLETE_TYPE_P (instantiation))
11905 /* If INSTANTIATION has been instantiated, then we don't
11906 need to consider it again in the future. */
11907 *t = TREE_CHAIN (*t);
11908 else
11910 last = *t;
11911 t = &TREE_CHAIN (*t);
11914 else
11916 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11917 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11919 instantiation
11920 = instantiate_decl (instantiation,
11921 /*defer_ok=*/0,
11922 /*expl_inst_class_mem_p=*/false);
11923 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11924 reconsider = 1;
11927 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11928 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11929 /* If INSTANTIATION has been instantiated, then we don't
11930 need to consider it again in the future. */
11931 *t = TREE_CHAIN (*t);
11932 else
11934 last = *t;
11935 t = &TREE_CHAIN (*t);
11938 tinst_depth = 0;
11939 current_tinst_level = NULL_TREE;
11941 last_pending_template = last;
11943 while (reconsider);
11945 input_location = saved_loc;
11946 in_system_header = saved_in_system_header;
11949 /* Substitute ARGVEC into T, which is a list of initializers for
11950 either base class or a non-static data member. The TREE_PURPOSEs
11951 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11952 instantiate_decl. */
11954 static tree
11955 tsubst_initializer_list (tree t, tree argvec)
11957 tree inits = NULL_TREE;
11959 for (; t; t = TREE_CHAIN (t))
11961 tree decl;
11962 tree init;
11964 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
11965 NULL_TREE);
11966 decl = expand_member_init (decl);
11967 if (decl && !DECL_P (decl))
11968 in_base_initializer = 1;
11970 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
11971 NULL_TREE);
11972 in_base_initializer = 0;
11974 if (decl)
11976 init = build_tree_list (decl, init);
11977 TREE_CHAIN (init) = inits;
11978 inits = init;
11981 return inits;
11984 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11986 static void
11987 set_current_access_from_decl (tree decl)
11989 if (TREE_PRIVATE (decl))
11990 current_access_specifier = access_private_node;
11991 else if (TREE_PROTECTED (decl))
11992 current_access_specifier = access_protected_node;
11993 else
11994 current_access_specifier = access_public_node;
11997 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11998 is the instantiation (which should have been created with
11999 start_enum) and ARGS are the template arguments to use. */
12001 static void
12002 tsubst_enum (tree tag, tree newtag, tree args)
12004 tree e;
12006 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12008 tree value;
12009 tree decl;
12011 decl = TREE_VALUE (e);
12012 /* Note that in a template enum, the TREE_VALUE is the
12013 CONST_DECL, not the corresponding INTEGER_CST. */
12014 value = tsubst_expr (DECL_INITIAL (decl),
12015 args, tf_warning_or_error, NULL_TREE);
12017 /* Give this enumeration constant the correct access. */
12018 set_current_access_from_decl (decl);
12020 /* Actually build the enumerator itself. */
12021 build_enumerator (DECL_NAME (decl), value, newtag);
12024 finish_enum (newtag);
12025 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12026 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12029 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12030 its type -- but without substituting the innermost set of template
12031 arguments. So, innermost set of template parameters will appear in
12032 the type. */
12034 tree
12035 get_mostly_instantiated_function_type (tree decl)
12037 tree fn_type;
12038 tree tmpl;
12039 tree targs;
12040 tree tparms;
12041 int parm_depth;
12043 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12044 targs = DECL_TI_ARGS (decl);
12045 tparms = DECL_TEMPLATE_PARMS (tmpl);
12046 parm_depth = TMPL_PARMS_DEPTH (tparms);
12048 /* There should be as many levels of arguments as there are levels
12049 of parameters. */
12050 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12052 fn_type = TREE_TYPE (tmpl);
12054 if (parm_depth == 1)
12055 /* No substitution is necessary. */
12057 else
12059 int i, save_access_control;
12060 tree partial_args;
12062 /* Replace the innermost level of the TARGS with NULL_TREEs to
12063 let tsubst know not to substitute for those parameters. */
12064 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12065 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12066 SET_TMPL_ARGS_LEVEL (partial_args, i,
12067 TMPL_ARGS_LEVEL (targs, i));
12068 SET_TMPL_ARGS_LEVEL (partial_args,
12069 TMPL_ARGS_DEPTH (targs),
12070 make_tree_vec (DECL_NTPARMS (tmpl)));
12072 /* Disable access control as this function is used only during
12073 name-mangling. */
12074 save_access_control = flag_access_control;
12075 flag_access_control = 0;
12077 ++processing_template_decl;
12078 /* Now, do the (partial) substitution to figure out the
12079 appropriate function type. */
12080 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12081 --processing_template_decl;
12083 /* Substitute into the template parameters to obtain the real
12084 innermost set of parameters. This step is important if the
12085 innermost set of template parameters contains value
12086 parameters whose types depend on outer template parameters. */
12087 TREE_VEC_LENGTH (partial_args)--;
12088 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12090 flag_access_control = save_access_control;
12093 return fn_type;
12096 /* Return truthvalue if we're processing a template different from
12097 the last one involved in diagnostics. */
12099 problematic_instantiation_changed (void)
12101 return last_template_error_tick != tinst_level_tick;
12104 /* Remember current template involved in diagnostics. */
12105 void
12106 record_last_problematic_instantiation (void)
12108 last_template_error_tick = tinst_level_tick;
12111 tree
12112 current_instantiation (void)
12114 return current_tinst_level;
12117 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12118 type. Return zero for ok, nonzero for disallowed. Issue error and
12119 warning messages under control of COMPLAIN. */
12121 static int
12122 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12124 if (INTEGRAL_TYPE_P (type))
12125 return 0;
12126 else if (POINTER_TYPE_P (type))
12127 return 0;
12128 else if (TYPE_PTR_TO_MEMBER_P (type))
12129 return 0;
12130 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12131 return 0;
12132 else if (TREE_CODE (type) == TYPENAME_TYPE)
12133 return 0;
12135 if (complain & tf_error)
12136 error ("%q#T is not a valid type for a template constant parameter", type);
12137 return 1;
12140 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12141 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12143 static bool
12144 dependent_type_p_r (tree type)
12146 tree scope;
12148 /* [temp.dep.type]
12150 A type is dependent if it is:
12152 -- a template parameter. Template template parameters are types
12153 for us (since TYPE_P holds true for them) so we handle
12154 them here. */
12155 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12156 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12157 return true;
12158 /* -- a qualified-id with a nested-name-specifier which contains a
12159 class-name that names a dependent type or whose unqualified-id
12160 names a dependent type. */
12161 if (TREE_CODE (type) == TYPENAME_TYPE)
12162 return true;
12163 /* -- a cv-qualified type where the cv-unqualified type is
12164 dependent. */
12165 type = TYPE_MAIN_VARIANT (type);
12166 /* -- a compound type constructed from any dependent type. */
12167 if (TYPE_PTR_TO_MEMBER_P (type))
12168 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12169 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12170 (type)));
12171 else if (TREE_CODE (type) == POINTER_TYPE
12172 || TREE_CODE (type) == REFERENCE_TYPE)
12173 return dependent_type_p (TREE_TYPE (type));
12174 else if (TREE_CODE (type) == FUNCTION_TYPE
12175 || TREE_CODE (type) == METHOD_TYPE)
12177 tree arg_type;
12179 if (dependent_type_p (TREE_TYPE (type)))
12180 return true;
12181 for (arg_type = TYPE_ARG_TYPES (type);
12182 arg_type;
12183 arg_type = TREE_CHAIN (arg_type))
12184 if (dependent_type_p (TREE_VALUE (arg_type)))
12185 return true;
12186 return false;
12188 /* -- an array type constructed from any dependent type or whose
12189 size is specified by a constant expression that is
12190 value-dependent. */
12191 if (TREE_CODE (type) == ARRAY_TYPE)
12193 if (TYPE_DOMAIN (type)
12194 && ((value_dependent_expression_p
12195 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12196 || (type_dependent_expression_p
12197 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12198 return true;
12199 return dependent_type_p (TREE_TYPE (type));
12202 /* -- a template-id in which either the template name is a template
12203 parameter ... */
12204 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12205 return true;
12206 /* ... or any of the template arguments is a dependent type or
12207 an expression that is type-dependent or value-dependent. */
12208 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12209 && (any_dependent_template_arguments_p
12210 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12211 return true;
12213 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12214 expression is not type-dependent, then it should already been
12215 have resolved. */
12216 if (TREE_CODE (type) == TYPEOF_TYPE)
12217 return true;
12219 /* The standard does not specifically mention types that are local
12220 to template functions or local classes, but they should be
12221 considered dependent too. For example:
12223 template <int I> void f() {
12224 enum E { a = I };
12225 S<sizeof (E)> s;
12228 The size of `E' cannot be known until the value of `I' has been
12229 determined. Therefore, `E' must be considered dependent. */
12230 scope = TYPE_CONTEXT (type);
12231 if (scope && TYPE_P (scope))
12232 return dependent_type_p (scope);
12233 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12234 return type_dependent_expression_p (scope);
12236 /* Other types are non-dependent. */
12237 return false;
12240 /* Returns TRUE if TYPE is dependent, in the sense of
12241 [temp.dep.type]. */
12243 bool
12244 dependent_type_p (tree type)
12246 /* If there are no template parameters in scope, then there can't be
12247 any dependent types. */
12248 if (!processing_template_decl)
12250 /* If we are not processing a template, then nobody should be
12251 providing us with a dependent type. */
12252 gcc_assert (type);
12253 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12254 return false;
12257 /* If the type is NULL, we have not computed a type for the entity
12258 in question; in that case, the type is dependent. */
12259 if (!type)
12260 return true;
12262 /* Erroneous types can be considered non-dependent. */
12263 if (type == error_mark_node)
12264 return false;
12266 /* If we have not already computed the appropriate value for TYPE,
12267 do so now. */
12268 if (!TYPE_DEPENDENT_P_VALID (type))
12270 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12271 TYPE_DEPENDENT_P_VALID (type) = 1;
12274 return TYPE_DEPENDENT_P (type);
12277 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12279 static bool
12280 dependent_scope_ref_p (tree expression, bool criterion (tree))
12282 tree scope;
12283 tree name;
12285 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12287 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12288 return true;
12290 scope = TREE_OPERAND (expression, 0);
12291 name = TREE_OPERAND (expression, 1);
12293 /* [temp.dep.expr]
12295 An id-expression is type-dependent if it contains a
12296 nested-name-specifier that contains a class-name that names a
12297 dependent type. */
12298 /* The suggested resolution to Core Issue 2 implies that if the
12299 qualifying type is the current class, then we must peek
12300 inside it. */
12301 if (DECL_P (name)
12302 && currently_open_class (scope)
12303 && !criterion (name))
12304 return false;
12305 if (dependent_type_p (scope))
12306 return true;
12308 return false;
12311 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12312 [temp.dep.constexpr] */
12314 bool
12315 value_dependent_expression_p (tree expression)
12317 if (!processing_template_decl)
12318 return false;
12320 /* A name declared with a dependent type. */
12321 if (DECL_P (expression) && type_dependent_expression_p (expression))
12322 return true;
12324 switch (TREE_CODE (expression))
12326 case IDENTIFIER_NODE:
12327 /* A name that has not been looked up -- must be dependent. */
12328 return true;
12330 case TEMPLATE_PARM_INDEX:
12331 /* A non-type template parm. */
12332 return true;
12334 case CONST_DECL:
12335 /* A non-type template parm. */
12336 if (DECL_TEMPLATE_PARM_P (expression))
12337 return true;
12338 return false;
12340 case VAR_DECL:
12341 /* A constant with integral or enumeration type and is initialized
12342 with an expression that is value-dependent. */
12343 if (DECL_INITIAL (expression)
12344 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12345 && value_dependent_expression_p (DECL_INITIAL (expression)))
12346 return true;
12347 return false;
12349 case DYNAMIC_CAST_EXPR:
12350 case STATIC_CAST_EXPR:
12351 case CONST_CAST_EXPR:
12352 case REINTERPRET_CAST_EXPR:
12353 case CAST_EXPR:
12354 /* These expressions are value-dependent if the type to which
12355 the cast occurs is dependent or the expression being casted
12356 is value-dependent. */
12358 tree type = TREE_TYPE (expression);
12360 if (dependent_type_p (type))
12361 return true;
12363 /* A functional cast has a list of operands. */
12364 expression = TREE_OPERAND (expression, 0);
12365 if (!expression)
12367 /* If there are no operands, it must be an expression such
12368 as "int()". This should not happen for aggregate types
12369 because it would form non-constant expressions. */
12370 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12372 return false;
12375 if (TREE_CODE (expression) == TREE_LIST)
12377 for (; expression; expression = TREE_CHAIN (expression))
12378 if (value_dependent_expression_p (TREE_VALUE (expression)))
12379 return true;
12380 return false;
12383 return value_dependent_expression_p (expression);
12386 case SIZEOF_EXPR:
12387 case ALIGNOF_EXPR:
12388 /* A `sizeof' expression is value-dependent if the operand is
12389 type-dependent. */
12390 expression = TREE_OPERAND (expression, 0);
12391 if (TYPE_P (expression))
12392 return dependent_type_p (expression);
12393 return type_dependent_expression_p (expression);
12395 case SCOPE_REF:
12396 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12398 case COMPONENT_REF:
12399 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12400 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12402 case CALL_EXPR:
12403 /* A CALL_EXPR is value-dependent if any argument is
12404 value-dependent. Why do we have to handle CALL_EXPRs in this
12405 function at all? First, some function calls, those for which
12406 value_dependent_expression_p is true, man appear in constant
12407 expressions. Second, there appear to be bugs which result in
12408 other CALL_EXPRs reaching this point. */
12410 tree function = TREE_OPERAND (expression, 0);
12411 tree args = TREE_OPERAND (expression, 1);
12413 if (value_dependent_expression_p (function))
12414 return true;
12416 if (! args)
12417 return false;
12419 if (TREE_CODE (args) == TREE_LIST)
12421 for (; args; args = TREE_CHAIN (args))
12422 if (value_dependent_expression_p (TREE_VALUE (args)))
12423 return true;
12424 return false;
12427 return value_dependent_expression_p (args);
12430 default:
12431 /* A constant expression is value-dependent if any subexpression is
12432 value-dependent. */
12433 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12435 case tcc_reference:
12436 case tcc_unary:
12437 return (value_dependent_expression_p
12438 (TREE_OPERAND (expression, 0)));
12440 case tcc_comparison:
12441 case tcc_binary:
12442 return ((value_dependent_expression_p
12443 (TREE_OPERAND (expression, 0)))
12444 || (value_dependent_expression_p
12445 (TREE_OPERAND (expression, 1))));
12447 case tcc_expression:
12449 int i;
12450 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12451 /* In some cases, some of the operands may be missing.
12452 (For example, in the case of PREDECREMENT_EXPR, the
12453 amount to increment by may be missing.) That doesn't
12454 make the expression dependent. */
12455 if (TREE_OPERAND (expression, i)
12456 && (value_dependent_expression_p
12457 (TREE_OPERAND (expression, i))))
12458 return true;
12459 return false;
12462 default:
12463 break;
12467 /* The expression is not value-dependent. */
12468 return false;
12471 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12472 [temp.dep.expr]. */
12474 bool
12475 type_dependent_expression_p (tree expression)
12477 if (!processing_template_decl)
12478 return false;
12480 if (expression == error_mark_node)
12481 return false;
12483 /* An unresolved name is always dependent. */
12484 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12485 return true;
12487 /* Some expression forms are never type-dependent. */
12488 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12489 || TREE_CODE (expression) == SIZEOF_EXPR
12490 || TREE_CODE (expression) == ALIGNOF_EXPR
12491 || TREE_CODE (expression) == TYPEID_EXPR
12492 || TREE_CODE (expression) == DELETE_EXPR
12493 || TREE_CODE (expression) == VEC_DELETE_EXPR
12494 || TREE_CODE (expression) == THROW_EXPR)
12495 return false;
12497 /* The types of these expressions depends only on the type to which
12498 the cast occurs. */
12499 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12500 || TREE_CODE (expression) == STATIC_CAST_EXPR
12501 || TREE_CODE (expression) == CONST_CAST_EXPR
12502 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12503 || TREE_CODE (expression) == CAST_EXPR)
12504 return dependent_type_p (TREE_TYPE (expression));
12506 /* The types of these expressions depends only on the type created
12507 by the expression. */
12508 if (TREE_CODE (expression) == NEW_EXPR
12509 || TREE_CODE (expression) == VEC_NEW_EXPR)
12511 /* For NEW_EXPR tree nodes created inside a template, either
12512 the object type itself or a TREE_LIST may appear as the
12513 operand 1. */
12514 tree type = TREE_OPERAND (expression, 1);
12515 if (TREE_CODE (type) == TREE_LIST)
12516 /* This is an array type. We need to check array dimensions
12517 as well. */
12518 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12519 || value_dependent_expression_p
12520 (TREE_OPERAND (TREE_VALUE (type), 1));
12521 else
12522 return dependent_type_p (type);
12525 if (TREE_CODE (expression) == SCOPE_REF
12526 && dependent_scope_ref_p (expression,
12527 type_dependent_expression_p))
12528 return true;
12530 if (TREE_CODE (expression) == FUNCTION_DECL
12531 && DECL_LANG_SPECIFIC (expression)
12532 && DECL_TEMPLATE_INFO (expression)
12533 && (any_dependent_template_arguments_p
12534 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12535 return true;
12537 if (TREE_CODE (expression) == TEMPLATE_DECL
12538 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12539 return false;
12541 if (TREE_TYPE (expression) == unknown_type_node)
12543 if (TREE_CODE (expression) == ADDR_EXPR)
12544 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12545 if (TREE_CODE (expression) == COMPONENT_REF
12546 || TREE_CODE (expression) == OFFSET_REF)
12548 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12549 return true;
12550 expression = TREE_OPERAND (expression, 1);
12551 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12552 return false;
12554 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12555 if (TREE_CODE (expression) == SCOPE_REF)
12556 return false;
12558 if (TREE_CODE (expression) == BASELINK)
12559 expression = BASELINK_FUNCTIONS (expression);
12561 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12563 if (any_dependent_template_arguments_p
12564 (TREE_OPERAND (expression, 1)))
12565 return true;
12566 expression = TREE_OPERAND (expression, 0);
12568 gcc_assert (TREE_CODE (expression) == OVERLOAD
12569 || TREE_CODE (expression) == FUNCTION_DECL);
12571 while (expression)
12573 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12574 return true;
12575 expression = OVL_NEXT (expression);
12577 return false;
12580 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
12582 return (dependent_type_p (TREE_TYPE (expression)));
12585 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12586 contains a type-dependent expression. */
12588 bool
12589 any_type_dependent_arguments_p (tree args)
12591 while (args)
12593 tree arg = TREE_VALUE (args);
12595 if (type_dependent_expression_p (arg))
12596 return true;
12597 args = TREE_CHAIN (args);
12599 return false;
12602 /* Returns TRUE if the ARG (a template argument) is dependent. */
12604 static bool
12605 dependent_template_arg_p (tree arg)
12607 if (!processing_template_decl)
12608 return false;
12610 if (TREE_CODE (arg) == TEMPLATE_DECL
12611 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12612 return dependent_template_p (arg);
12613 else if (TYPE_P (arg))
12614 return dependent_type_p (arg);
12615 else
12616 return (type_dependent_expression_p (arg)
12617 || value_dependent_expression_p (arg));
12620 /* Returns true if ARGS (a collection of template arguments) contains
12621 any dependent arguments. */
12623 bool
12624 any_dependent_template_arguments_p (tree args)
12626 int i;
12627 int j;
12629 if (!args)
12630 return false;
12632 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12634 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12635 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12636 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12637 return true;
12640 return false;
12643 /* Returns TRUE if the template TMPL is dependent. */
12645 bool
12646 dependent_template_p (tree tmpl)
12648 if (TREE_CODE (tmpl) == OVERLOAD)
12650 while (tmpl)
12652 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12653 return true;
12654 tmpl = OVL_CHAIN (tmpl);
12656 return false;
12659 /* Template template parameters are dependent. */
12660 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12661 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12662 return true;
12663 /* So are names that have not been looked up. */
12664 if (TREE_CODE (tmpl) == SCOPE_REF
12665 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12666 return true;
12667 /* So are member templates of dependent classes. */
12668 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12669 return dependent_type_p (DECL_CONTEXT (tmpl));
12670 return false;
12673 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12675 bool
12676 dependent_template_id_p (tree tmpl, tree args)
12678 return (dependent_template_p (tmpl)
12679 || any_dependent_template_arguments_p (args));
12682 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12683 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12684 can be found. Note that this function peers inside uninstantiated
12685 templates and therefore should be used only in extremely limited
12686 situations. ONLY_CURRENT_P restricts this peering to the currently
12687 open classes hierarchy (which is required when comparing types). */
12689 tree
12690 resolve_typename_type (tree type, bool only_current_p)
12692 tree scope;
12693 tree name;
12694 tree decl;
12695 int quals;
12696 tree pushed_scope;
12698 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12700 scope = TYPE_CONTEXT (type);
12701 name = TYPE_IDENTIFIER (type);
12703 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12704 it first before we can figure out what NAME refers to. */
12705 if (TREE_CODE (scope) == TYPENAME_TYPE)
12706 scope = resolve_typename_type (scope, only_current_p);
12707 /* If we don't know what SCOPE refers to, then we cannot resolve the
12708 TYPENAME_TYPE. */
12709 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12710 return error_mark_node;
12711 /* If the SCOPE is a template type parameter, we have no way of
12712 resolving the name. */
12713 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12714 return type;
12715 /* If the SCOPE is not the current instantiation, there's no reason
12716 to look inside it. */
12717 if (only_current_p && !currently_open_class (scope))
12718 return error_mark_node;
12719 /* If SCOPE is a partial instantiation, it will not have a valid
12720 TYPE_FIELDS list, so use the original template. */
12721 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12722 /* Enter the SCOPE so that name lookup will be resolved as if we
12723 were in the class definition. In particular, SCOPE will no
12724 longer be considered a dependent type. */
12725 pushed_scope = push_scope (scope);
12726 /* Look up the declaration. */
12727 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12728 /* Obtain the set of qualifiers applied to the TYPE. */
12729 quals = cp_type_quals (type);
12730 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12731 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12732 if (!decl)
12733 type = error_mark_node;
12734 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12735 && TREE_CODE (decl) == TYPE_DECL)
12736 type = TREE_TYPE (decl);
12737 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12738 && DECL_CLASS_TEMPLATE_P (decl))
12740 tree tmpl;
12741 tree args;
12742 /* Obtain the template and the arguments. */
12743 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12744 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12745 /* Instantiate the template. */
12746 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12747 /*entering_scope=*/0, tf_error | tf_user);
12749 else
12750 type = error_mark_node;
12751 /* Qualify the resulting type. */
12752 if (type != error_mark_node && quals)
12753 type = cp_build_qualified_type (type, quals);
12754 /* Leave the SCOPE. */
12755 if (pushed_scope)
12756 pop_scope (pushed_scope);
12758 return type;
12761 /* EXPR is an expression which is not type-dependent. Return a proxy
12762 for EXPR that can be used to compute the types of larger
12763 expressions containing EXPR. */
12765 tree
12766 build_non_dependent_expr (tree expr)
12768 tree inner_expr;
12770 /* Preserve null pointer constants so that the type of things like
12771 "p == 0" where "p" is a pointer can be determined. */
12772 if (null_ptr_cst_p (expr))
12773 return expr;
12774 /* Preserve OVERLOADs; the functions must be available to resolve
12775 types. */
12776 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12777 TREE_OPERAND (expr, 0) :
12778 TREE_CODE (expr) == COMPONENT_REF ?
12779 TREE_OPERAND (expr, 1) : expr);
12780 if (is_overloaded_fn (inner_expr)
12781 || TREE_CODE (inner_expr) == OFFSET_REF)
12782 return expr;
12783 /* There is no need to return a proxy for a variable. */
12784 if (TREE_CODE (expr) == VAR_DECL)
12785 return expr;
12786 /* Preserve string constants; conversions from string constants to
12787 "char *" are allowed, even though normally a "const char *"
12788 cannot be used to initialize a "char *". */
12789 if (TREE_CODE (expr) == STRING_CST)
12790 return expr;
12791 /* Preserve arithmetic constants, as an optimization -- there is no
12792 reason to create a new node. */
12793 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12794 return expr;
12795 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12796 There is at least one place where we want to know that a
12797 particular expression is a throw-expression: when checking a ?:
12798 expression, there are special rules if the second or third
12799 argument is a throw-expression. */
12800 if (TREE_CODE (expr) == THROW_EXPR)
12801 return expr;
12803 if (TREE_CODE (expr) == COND_EXPR)
12804 return build3 (COND_EXPR,
12805 TREE_TYPE (expr),
12806 TREE_OPERAND (expr, 0),
12807 (TREE_OPERAND (expr, 1)
12808 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12809 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12810 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12811 if (TREE_CODE (expr) == COMPOUND_EXPR
12812 && !COMPOUND_EXPR_OVERLOADED (expr))
12813 return build2 (COMPOUND_EXPR,
12814 TREE_TYPE (expr),
12815 TREE_OPERAND (expr, 0),
12816 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12818 /* If the type is unknown, it can't really be non-dependent */
12819 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
12821 /* Otherwise, build a NON_DEPENDENT_EXPR.
12823 REFERENCE_TYPEs are not stripped for expressions in templates
12824 because doing so would play havoc with mangling. Consider, for
12825 example:
12827 template <typename T> void f<T& g>() { g(); }
12829 In the body of "f", the expression for "g" will have
12830 REFERENCE_TYPE, even though the standard says that it should
12831 not. The reason is that we must preserve the syntactic form of
12832 the expression so that mangling (say) "f<g>" inside the body of
12833 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12834 stripped here. */
12835 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12838 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12839 Return a new TREE_LIST with the various arguments replaced with
12840 equivalent non-dependent expressions. */
12842 tree
12843 build_non_dependent_args (tree args)
12845 tree a;
12846 tree new_args;
12848 new_args = NULL_TREE;
12849 for (a = args; a; a = TREE_CHAIN (a))
12850 new_args = tree_cons (NULL_TREE,
12851 build_non_dependent_expr (TREE_VALUE (a)),
12852 new_args);
12853 return nreverse (new_args);
12856 #include "gt-cp-pt.h"