(read_braced_string): Check for EOF. If encountered issue an error message.
[official-gcc.git] / gcc / cp / pt.c
blob7ce9031e03335d30011f92dfe3a30b7809f6b56a
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, 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 "flags.h"
36 #include "cp-tree.h"
37 #include "tree-inline.h"
38 #include "decl.h"
39 #include "lex.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "rtl.h"
44 #include "timevar.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work.
53 The TREE_PURPOSE of each entry is either a DECL (for a function or
54 static data member), or a TYPE (for a class) indicating what we are
55 hoping to instantiate. The TREE_VALUE is not used. */
56 static GTY(()) tree pending_templates;
57 static GTY(()) tree last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static GTY(()) varray_type inline_parm_levels;
64 static size_t inline_parm_levels_used;
66 static GTY(()) tree current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* A map from local variable declarations in the body of the template
71 presently being instantiated to the corresponding instantiated
72 local variables. */
73 static htab_t local_specializations;
75 #define UNIFY_ALLOW_NONE 0
76 #define UNIFY_ALLOW_MORE_CV_QUAL 1
77 #define UNIFY_ALLOW_LESS_CV_QUAL 2
78 #define UNIFY_ALLOW_DERIVED 4
79 #define UNIFY_ALLOW_INTEGER 8
80 #define UNIFY_ALLOW_OUTER_LEVEL 16
81 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
82 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
83 #define UNIFY_ALLOW_MAX_CORRECTION 128
85 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
86 virtual, or a base class of a virtual
87 base. */
88 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
89 type with the desired type. */
91 static void push_access_scope_real (tree, tree, tree);
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);
98 static int unify (tree, tree, tree, tree, int);
99 static void add_pending_template (tree);
100 static void reopen_tinst_level (tree);
101 static tree classtype_mangled_name (tree);
102 static char* mangle_class_name_for_template (const char *, tree, tree);
103 static tree tsubst_initializer_list (tree, tree);
104 static int list_eq (tree, tree);
105 static tree get_class_bindings (tree, tree, tree);
106 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
107 static void tsubst_enum (tree, tree, tree);
108 static tree add_to_template_args (tree, tree);
109 static tree add_outermost_template_args (tree, tree);
110 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
111 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
112 static int type_unification_real (tree, tree, tree, tree,
113 int, unification_kind_t, int, int);
114 static void note_template_header (int);
115 static tree maybe_fold_nontype_arg (tree);
116 static void maybe_fold_nontype_args (tree);
117 static tree convert_nontype_argument (tree, tree);
118 static tree convert_template_argument (tree, tree, tree,
119 tsubst_flags_t, int, tree);
120 static tree get_bindings_overload (tree, tree, tree);
121 static int for_each_template_parm (tree, tree_fn_t, void*, htab_t);
122 static tree build_template_parm_index (int, int, int, tree, tree);
123 static int inline_needs_template_parms (tree);
124 static void push_inline_template_parms_recursive (tree, int);
125 static tree retrieve_specialization (tree, tree);
126 static tree retrieve_local_specialization (tree);
127 static tree register_specialization (tree, tree, tree);
128 static void register_local_specialization (tree, tree);
129 static int unregister_specialization (tree, tree);
130 static tree reduce_template_parm_level (tree, tree, int);
131 static tree build_template_decl (tree, tree);
132 static int mark_template_parm (tree, void *);
133 static int template_parm_this_level_p (tree, void *);
134 static tree tsubst_friend_function (tree, tree);
135 static tree tsubst_friend_class (tree, tree);
136 static int can_complete_type_without_circularity (tree);
137 static tree get_bindings_real (tree, tree, tree, int, int, int);
138 static int template_decl_level (tree);
139 static int check_cv_quals_for_unify (int, tree, tree);
140 static tree tsubst_template_arg_vector (tree, tree, tsubst_flags_t);
141 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
142 static void regenerate_decl_from_template (tree, tree);
143 static tree most_specialized (tree, tree, tree);
144 static tree most_specialized_class (tree, tree);
145 static int template_class_depth_real (tree, int);
146 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
147 static tree tsubst_decl (tree, tree, tree, tsubst_flags_t);
148 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
149 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
150 static void check_specialization_scope (void);
151 static tree process_partial_specialization (tree);
152 static void set_current_access_from_decl (tree);
153 static void check_default_tmpl_args (tree, tree, int, int);
154 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
155 static tree get_template_base_recursive (tree, tree, tree, tree, tree, int);
156 static tree get_template_base (tree, tree, tree, tree);
157 static int verify_class_unification (tree, tree, tree);
158 static tree try_class_unification (tree, tree, tree, tree);
159 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
160 tree, tree);
161 static tree determine_specialization (tree, tree, tree *, int);
162 static int template_args_equal (tree, tree);
163 static void tsubst_default_arguments (tree);
164 static tree for_each_template_parm_r (tree *, int *, void *);
165 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
166 static void copy_default_args_to_explicit_spec (tree);
167 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
168 static int eq_local_specializations (const void *, const void *);
169 static bool dependent_type_p_r (tree);
170 static bool dependent_template_id_p (tree, tree);
171 static tree tsubst (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_copy_and_build (tree, tree, tsubst_flags_t, tree);
176 /* Make the current scope suitable for access checking when we are
177 processing T. T can be FUNCTION_DECL for instantiated function
178 template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for
179 static member variable (need by instantiate_decl). ARGS is the
180 template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE,
181 this is used instead of the context of T. */
183 void
184 push_access_scope_real (tree t, tree args, tree context)
186 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
188 /* When we are processing specialization `foo<Outer>' for code like
190 template <class U> typename U::Inner foo ();
191 class Outer {
192 struct Inner {};
193 friend Outer::Inner foo<Outer> ();
196 `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of
197 its specialization. We can get the FUNCTION_DECL with the right
198 information because this specialization has already been
199 registered by the friend declaration above. */
201 if (DECL_FUNCTION_TEMPLATE_P (t) && args)
203 tree full_args = tsubst_template_arg_vector
204 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none);
205 tree spec = NULL_TREE;
206 if (full_args != error_mark_node)
207 spec = retrieve_specialization (t, full_args);
208 if (spec)
209 t = spec;
213 if (!context)
214 context = DECL_CONTEXT (t);
215 if (context && TYPE_P (context))
216 push_nested_class (context);
217 else
218 push_to_top_level ();
220 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
222 saved_access_scope = tree_cons
223 (NULL_TREE, current_function_decl, saved_access_scope);
224 current_function_decl = t;
228 /* Like push_access_scope_real, but always uses DECL_CONTEXT. */
230 void
231 push_access_scope (tree t)
233 push_access_scope_real (t, NULL_TREE, NULL_TREE);
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
239 void
240 pop_access_scope (tree t)
242 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
248 if (DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
259 tree
260 finish_member_template_decl (tree decl)
262 if (decl == error_mark_node)
263 return error_mark_node;
265 my_friendly_assert (DECL_P (decl), 20020812);
267 if (TREE_CODE (decl) == TYPE_DECL)
269 tree type;
271 type = TREE_TYPE (decl);
272 if (IS_AGGR_TYPE (type)
273 && CLASSTYPE_TEMPLATE_INFO (type)
274 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
276 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
277 check_member_template (tmpl);
278 return tmpl;
280 return NULL_TREE;
282 else if (TREE_CODE (decl) == FIELD_DECL)
283 error ("data member `%D' cannot be a member template", decl);
284 else if (DECL_TEMPLATE_INFO (decl))
286 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
288 check_member_template (DECL_TI_TEMPLATE (decl));
289 return DECL_TI_TEMPLATE (decl);
291 else
292 return decl;
294 else
295 error ("invalid member template declaration `%D'", decl);
297 return error_mark_node;
300 /* Returns the template nesting level of the indicated class TYPE.
302 For example, in:
303 template <class T>
304 struct A
306 template <class U>
307 struct B {};
310 A<T>::B<U> has depth two, while A<T> has depth one.
311 Both A<T>::B<int> and A<int>::B<U> have depth one, if
312 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
313 specializations.
315 This function is guaranteed to return 0 if passed NULL_TREE so
316 that, for example, `template_class_depth (current_class_type)' is
317 always safe. */
319 static int
320 template_class_depth_real (tree type, int count_specializations)
322 int depth;
324 for (depth = 0;
325 type && TREE_CODE (type) != NAMESPACE_DECL;
326 type = (TREE_CODE (type) == FUNCTION_DECL)
327 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
329 if (TREE_CODE (type) != FUNCTION_DECL)
331 if (CLASSTYPE_TEMPLATE_INFO (type)
332 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
333 && ((count_specializations
334 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
335 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
336 ++depth;
338 else
340 if (DECL_TEMPLATE_INFO (type)
341 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
342 && ((count_specializations
343 && DECL_TEMPLATE_SPECIALIZATION (type))
344 || uses_template_parms (DECL_TI_ARGS (type))))
345 ++depth;
349 return depth;
352 /* Returns the template nesting level of the indicated class TYPE.
353 Like template_class_depth_real, but instantiations do not count in
354 the depth. */
356 int
357 template_class_depth (tree type)
359 return template_class_depth_real (type, /*count_specializations=*/0);
362 /* Returns 1 if processing DECL as part of do_pending_inlines
363 needs us to push template parms. */
365 static int
366 inline_needs_template_parms (tree decl)
368 if (! DECL_TEMPLATE_INFO (decl))
369 return 0;
371 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
372 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
375 /* Subroutine of maybe_begin_member_template_processing.
376 Push the template parms in PARMS, starting from LEVELS steps into the
377 chain, and ending at the beginning, since template parms are listed
378 innermost first. */
380 static void
381 push_inline_template_parms_recursive (tree parmlist, int levels)
383 tree parms = TREE_VALUE (parmlist);
384 int i;
386 if (levels > 1)
387 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
389 ++processing_template_decl;
390 current_template_parms
391 = tree_cons (size_int (processing_template_decl),
392 parms, current_template_parms);
393 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
395 pushlevel (0);
396 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
398 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
399 my_friendly_assert (DECL_P (parm), 0);
401 switch (TREE_CODE (parm))
403 case TYPE_DECL:
404 case TEMPLATE_DECL:
405 pushdecl (parm);
406 break;
408 case PARM_DECL:
410 /* Make a CONST_DECL as is done in process_template_parm.
411 It is ugly that we recreate this here; the original
412 version built in process_template_parm is no longer
413 available. */
414 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
415 TREE_TYPE (parm));
416 DECL_ARTIFICIAL (decl) = 1;
417 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
418 DECL_INITIAL (decl) = DECL_INITIAL (parm);
419 SET_DECL_TEMPLATE_PARM_P (decl);
420 pushdecl (decl);
422 break;
424 default:
425 abort ();
430 /* Restore the template parameter context for a member template or
431 a friend template defined in a class definition. */
433 void
434 maybe_begin_member_template_processing (tree decl)
436 tree parms;
437 int levels = 0;
439 if (inline_needs_template_parms (decl))
441 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
442 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
444 if (DECL_TEMPLATE_SPECIALIZATION (decl))
446 --levels;
447 parms = TREE_CHAIN (parms);
450 push_inline_template_parms_recursive (parms, levels);
453 /* Remember how many levels of template parameters we pushed so that
454 we can pop them later. */
455 if (!inline_parm_levels)
456 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
457 if (inline_parm_levels_used == inline_parm_levels->num_elements)
458 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
459 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
460 ++inline_parm_levels_used;
463 /* Undo the effects of begin_member_template_processing. */
465 void
466 maybe_end_member_template_processing (void)
468 int i;
470 if (!inline_parm_levels_used)
471 return;
473 --inline_parm_levels_used;
474 for (i = 0;
475 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
476 ++i)
478 --processing_template_decl;
479 current_template_parms = TREE_CHAIN (current_template_parms);
480 poplevel (0, 0, 0);
484 /* Returns nonzero iff T is a member template function. We must be
485 careful as in
487 template <class T> class C { void f(); }
489 Here, f is a template function, and a member, but not a member
490 template. This function does not concern itself with the origin of
491 T, only its present state. So if we have
493 template <class T> class C { template <class U> void f(U); }
495 then neither C<int>::f<char> nor C<T>::f<double> is considered
496 to be a member template. But, `template <class U> void
497 C<int>::f(U)' is considered a member template. */
500 is_member_template (tree t)
502 if (!DECL_FUNCTION_TEMPLATE_P (t))
503 /* Anything that isn't a function or a template function is
504 certainly not a member template. */
505 return 0;
507 /* A local class can't have member templates. */
508 if (decl_function_context (t))
509 return 0;
511 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
512 /* If there are more levels of template parameters than
513 there are template classes surrounding the declaration,
514 then we have a member template. */
515 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
516 template_class_depth (DECL_CONTEXT (t))));
519 #if 0 /* UNUSED */
520 /* Returns nonzero iff T is a member template class. See
521 is_member_template for a description of what precisely constitutes
522 a member template. */
525 is_member_template_class (tree t)
527 if (!DECL_CLASS_TEMPLATE_P (t))
528 /* Anything that isn't a class template, is certainly not a member
529 template. */
530 return 0;
532 if (!DECL_CLASS_SCOPE_P (t))
533 /* Anything whose context isn't a class type is surely not a
534 member template. */
535 return 0;
537 /* If there are more levels of template parameters than there are
538 template classes surrounding the declaration, then we have a
539 member template. */
540 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
541 template_class_depth (DECL_CONTEXT (t)));
543 #endif
545 /* Return a new template argument vector which contains all of ARGS,
546 but has as its innermost set of arguments the EXTRA_ARGS. */
548 static tree
549 add_to_template_args (tree args, tree extra_args)
551 tree new_args;
552 int extra_depth;
553 int i;
554 int j;
556 extra_depth = TMPL_ARGS_DEPTH (extra_args);
557 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
559 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
560 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
562 for (j = 1; j <= extra_depth; ++j, ++i)
563 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
565 return new_args;
568 /* Like add_to_template_args, but only the outermost ARGS are added to
569 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
570 (EXTRA_ARGS) levels are added. This function is used to combine
571 the template arguments from a partial instantiation with the
572 template arguments used to attain the full instantiation from the
573 partial instantiation. */
575 static tree
576 add_outermost_template_args (tree args, tree extra_args)
578 tree new_args;
580 /* If there are more levels of EXTRA_ARGS than there are ARGS,
581 something very fishy is going on. */
582 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
585 /* If *all* the new arguments will be the EXTRA_ARGS, just return
586 them. */
587 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
588 return extra_args;
590 /* For the moment, we make ARGS look like it contains fewer levels. */
591 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
593 new_args = add_to_template_args (args, extra_args);
595 /* Now, we restore ARGS to its full dimensions. */
596 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
598 return new_args;
601 /* Return the N levels of innermost template arguments from the ARGS. */
603 tree
604 get_innermost_template_args (tree args, int n)
606 tree new_args;
607 int extra_levels;
608 int i;
610 my_friendly_assert (n >= 0, 20000603);
612 /* If N is 1, just return the innermost set of template arguments. */
613 if (n == 1)
614 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
616 /* If we're not removing anything, just return the arguments we were
617 given. */
618 extra_levels = TMPL_ARGS_DEPTH (args) - n;
619 my_friendly_assert (extra_levels >= 0, 20000603);
620 if (extra_levels == 0)
621 return args;
623 /* Make a new set of arguments, not containing the outer arguments. */
624 new_args = make_tree_vec (n);
625 for (i = 1; i <= n; ++i)
626 SET_TMPL_ARGS_LEVEL (new_args, i,
627 TMPL_ARGS_LEVEL (args, i + extra_levels));
629 return new_args;
632 /* We've got a template header coming up; push to a new level for storing
633 the parms. */
635 void
636 begin_template_parm_list (void)
638 /* We use a non-tag-transparent scope here, which causes pushtag to
639 put tags in this scope, rather than in the enclosing class or
640 namespace scope. This is the right thing, since we want
641 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
642 global template class, push_template_decl handles putting the
643 TEMPLATE_DECL into top-level scope. For a nested template class,
644 e.g.:
646 template <class T> struct S1 {
647 template <class T> struct S2 {};
650 pushtag contains special code to call pushdecl_with_scope on the
651 TEMPLATE_DECL for S2. */
652 begin_scope (sk_template_parms);
653 ++processing_template_decl;
654 ++processing_template_parmlist;
655 note_template_header (0);
658 /* This routine is called when a specialization is declared. If it is
659 invalid to declare a specialization here, an error is reported. */
661 static void
662 check_specialization_scope (void)
664 tree scope = current_scope ();
666 /* [temp.expl.spec]
668 An explicit specialization shall be declared in the namespace of
669 which the template is a member, or, for member templates, in the
670 namespace of which the enclosing class or enclosing class
671 template is a member. An explicit specialization of a member
672 function, member class or static data member of a class template
673 shall be declared in the namespace of which the class template
674 is a member. */
675 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
676 error ("explicit specialization in non-namespace scope `%D'",
677 scope);
679 /* [temp.expl.spec]
681 In an explicit specialization declaration for a member of a class
682 template or a member template that appears in namespace scope,
683 the member template and some of its enclosing class templates may
684 remain unspecialized, except that the declaration shall not
685 explicitly specialize a class member template if its enclosing
686 class templates are not explicitly specialized as well. */
687 if (current_template_parms)
688 error ("enclosing class templates are not explicitly specialized");
691 /* We've just seen template <>. */
693 void
694 begin_specialization (void)
696 begin_scope (sk_template_spec);
697 note_template_header (1);
698 check_specialization_scope ();
701 /* Called at then end of processing a declaration preceded by
702 template<>. */
704 void
705 end_specialization (void)
707 finish_scope ();
708 reset_specialization ();
711 /* Any template <>'s that we have seen thus far are not referring to a
712 function specialization. */
714 void
715 reset_specialization (void)
717 processing_specialization = 0;
718 template_header_count = 0;
721 /* We've just seen a template header. If SPECIALIZATION is nonzero,
722 it was of the form template <>. */
724 static void
725 note_template_header (int specialization)
727 processing_specialization = specialization;
728 template_header_count++;
731 /* We're beginning an explicit instantiation. */
733 void
734 begin_explicit_instantiation (void)
736 my_friendly_assert (!processing_explicit_instantiation, 20020913);
737 processing_explicit_instantiation = true;
741 void
742 end_explicit_instantiation (void)
744 my_friendly_assert(processing_explicit_instantiation, 20020913);
745 processing_explicit_instantiation = false;
748 /* The TYPE is being declared. If it is a template type, that means it
749 is a partial specialization. Do appropriate error-checking. */
751 void
752 maybe_process_partial_specialization (tree type)
754 /* TYPE maybe an ERROR_MARK_NODE. */
755 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
757 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
759 /* This is for ordinary explicit specialization and partial
760 specialization of a template class such as:
762 template <> class C<int>;
766 template <class T> class C<T*>;
768 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
770 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
771 && !COMPLETE_TYPE_P (type))
773 if (current_namespace
774 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
776 pedwarn ("specializing `%#T' in different namespace", type);
777 cp_pedwarn_at (" from definition of `%#D'",
778 CLASSTYPE_TI_TEMPLATE (type));
780 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
781 if (processing_template_decl)
782 push_template_decl (TYPE_MAIN_DECL (type));
784 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
785 error ("specialization of `%T' after instantiation", type);
787 else if (CLASS_TYPE_P (type)
788 && !CLASSTYPE_USE_TEMPLATE (type)
789 && CLASSTYPE_TEMPLATE_INFO (type)
790 && context && CLASS_TYPE_P (context)
791 && CLASSTYPE_TEMPLATE_INFO (context))
793 /* This is for an explicit specialization of member class
794 template according to [temp.expl.spec/18]:
796 template <> template <class U> class C<int>::D;
798 The context `C<int>' must be an implicit instantiation.
799 Otherwise this is just a member class template declared
800 earlier like:
802 template <> class C<int> { template <class U> class D; };
803 template <> template <class U> class C<int>::D;
805 In the first case, `C<int>::D' is a specialization of `C<T>::D'
806 while in the second case, `C<int>::D' is a primary template
807 and `C<T>::D' may not exist. */
809 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
810 && !COMPLETE_TYPE_P (type))
812 tree t;
814 if (current_namespace
815 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
817 pedwarn ("specializing `%#T' in different namespace", type);
818 cp_pedwarn_at (" from definition of `%#D'",
819 CLASSTYPE_TI_TEMPLATE (type));
822 /* Check for invalid specialization after instantiation:
824 template <> template <> class C<int>::D<int>;
825 template <> template <class U> class C<int>::D; */
827 for (t = DECL_TEMPLATE_INSTANTIATIONS
828 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
829 t; t = TREE_CHAIN (t))
830 if (TREE_VALUE (t) != type
831 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
832 error ("specialization `%T' after instantiation `%T'",
833 type, TREE_VALUE (t));
835 /* Mark TYPE as a specialization. And as a result, we only
836 have one level of template argument for the innermost
837 class template. */
838 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
839 CLASSTYPE_TI_ARGS (type)
840 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
843 else if (processing_specialization)
844 error ("explicit specialization of non-template `%T'", type);
847 /* Retrieve the specialization (in the sense of [temp.spec] - a
848 specialization is either an instantiation or an explicit
849 specialization) of TMPL for the given template ARGS. If there is
850 no such specialization, return NULL_TREE. The ARGS are a vector of
851 arguments, or a vector of vectors of arguments, in the case of
852 templates with more than one level of parameters. */
854 static tree
855 retrieve_specialization (tree tmpl, tree args)
857 tree s;
859 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
861 /* There should be as many levels of arguments as there are
862 levels of parameters. */
863 my_friendly_assert (TMPL_ARGS_DEPTH (args)
864 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
867 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
868 s != NULL_TREE;
869 s = TREE_CHAIN (s))
870 if (comp_template_args (TREE_PURPOSE (s), args))
871 return TREE_VALUE (s);
873 return NULL_TREE;
876 /* Like retrieve_specialization, but for local declarations. */
878 static tree
879 retrieve_local_specialization (tree tmpl)
881 tree spec =
882 (tree) htab_find_with_hash (local_specializations, tmpl,
883 htab_hash_pointer (tmpl));
884 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
887 /* Returns nonzero iff DECL is a specialization of TMPL. */
890 is_specialization_of (tree decl, tree tmpl)
892 tree t;
894 if (TREE_CODE (decl) == FUNCTION_DECL)
896 for (t = decl;
897 t != NULL_TREE;
898 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
899 if (t == tmpl)
900 return 1;
902 else
904 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
906 for (t = TREE_TYPE (decl);
907 t != NULL_TREE;
908 t = CLASSTYPE_USE_TEMPLATE (t)
909 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
910 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
911 return 1;
914 return 0;
917 /* Register the specialization SPEC as a specialization of TMPL with
918 the indicated ARGS. Returns SPEC, or an equivalent prior
919 declaration, if available. */
921 static tree
922 register_specialization (tree spec, tree tmpl, tree args)
924 tree s;
926 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
928 if (TREE_CODE (spec) == FUNCTION_DECL
929 && uses_template_parms (DECL_TI_ARGS (spec)))
930 /* This is the FUNCTION_DECL for a partial instantiation. Don't
931 register it; we want the corresponding TEMPLATE_DECL instead.
932 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
933 the more obvious `uses_template_parms (spec)' to avoid problems
934 with default function arguments. In particular, given
935 something like this:
937 template <class T> void f(T t1, T t = T())
939 the default argument expression is not substituted for in an
940 instantiation unless and until it is actually needed. */
941 return spec;
943 /* There should be as many levels of arguments as there are
944 levels of parameters. */
945 my_friendly_assert (TMPL_ARGS_DEPTH (args)
946 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
949 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
950 s != NULL_TREE;
951 s = TREE_CHAIN (s))
953 tree fn = TREE_VALUE (s);
955 /* We can sometimes try to re-register a specialization that we've
956 already got. In particular, regenerate_decl_from_template
957 calls duplicate_decls which will update the specialization
958 list. But, we'll still get called again here anyhow. It's
959 more convenient to simply allow this than to try to prevent it. */
960 if (fn == spec)
961 return spec;
962 else if (comp_template_args (TREE_PURPOSE (s), args))
964 if (DECL_TEMPLATE_SPECIALIZATION (spec))
966 if (DECL_TEMPLATE_INSTANTIATION (fn))
968 if (TREE_USED (fn)
969 || DECL_EXPLICIT_INSTANTIATION (fn))
971 error ("specialization of %D after instantiation",
972 fn);
973 return spec;
975 else
977 /* This situation should occur only if the first
978 specialization is an implicit instantiation,
979 the second is an explicit specialization, and
980 the implicit instantiation has not yet been
981 used. That situation can occur if we have
982 implicitly instantiated a member function and
983 then specialized it later.
985 We can also wind up here if a friend
986 declaration that looked like an instantiation
987 turns out to be a specialization:
989 template <class T> void foo(T);
990 class S { friend void foo<>(int) };
991 template <> void foo(int);
993 We transform the existing DECL in place so that
994 any pointers to it become pointers to the
995 updated declaration.
997 If there was a definition for the template, but
998 not for the specialization, we want this to
999 look as if there is no definition, and vice
1000 versa. */
1001 DECL_INITIAL (fn) = NULL_TREE;
1002 duplicate_decls (spec, fn);
1004 return fn;
1007 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1009 if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
1010 /* Dup decl failed, but this is a new
1011 definition. Set the line number so any errors
1012 match this new definition. */
1013 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1015 return fn;
1021 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1022 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1024 return spec;
1027 /* Unregister the specialization SPEC as a specialization of TMPL.
1028 Returns nonzero if the SPEC was listed as a specialization of
1029 TMPL. */
1031 static int
1032 unregister_specialization (tree spec, tree tmpl)
1034 tree* s;
1036 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1037 *s != NULL_TREE;
1038 s = &TREE_CHAIN (*s))
1039 if (TREE_VALUE (*s) == spec)
1041 *s = TREE_CHAIN (*s);
1042 return 1;
1045 return 0;
1048 /* Compare an entry in the local specializations hash table P1 (which
1049 is really a pointer to a TREE_LIST) with P2 (which is really a
1050 DECL). */
1052 static int
1053 eq_local_specializations (const void *p1, const void *p2)
1055 return TREE_VALUE ((tree) p1) == (tree) p2;
1058 /* Hash P1, an entry in the local specializations table. */
1060 static hashval_t
1061 hash_local_specialization (const void* p1)
1063 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1066 /* Like register_specialization, but for local declarations. We are
1067 registering SPEC, an instantiation of TMPL. */
1069 static void
1070 register_local_specialization (tree spec, tree tmpl)
1072 void **slot;
1074 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1075 htab_hash_pointer (tmpl), INSERT);
1076 *slot = build_tree_list (spec, tmpl);
1079 /* Print the list of candidate FNS in an error message. */
1081 void
1082 print_candidates (tree fns)
1084 tree fn;
1086 const char *str = "candidates are:";
1088 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1090 tree f;
1092 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1093 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1094 str = " ";
1098 /* Returns the template (one of the functions given by TEMPLATE_ID)
1099 which can be specialized to match the indicated DECL with the
1100 explicit template args given in TEMPLATE_ID. The DECL may be
1101 NULL_TREE if none is available. In that case, the functions in
1102 TEMPLATE_ID are non-members.
1104 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1105 specialization of a member template.
1107 The template args (those explicitly specified and those deduced)
1108 are output in a newly created vector *TARGS_OUT.
1110 If it is impossible to determine the result, an error message is
1111 issued. The error_mark_node is returned to indicate failure. */
1113 static tree
1114 determine_specialization (tree template_id,
1115 tree decl,
1116 tree* targs_out,
1117 int need_member_template)
1119 tree fns;
1120 tree targs;
1121 tree explicit_targs;
1122 tree candidates = NULL_TREE;
1123 tree templates = NULL_TREE;
1125 *targs_out = NULL_TREE;
1127 if (template_id == error_mark_node)
1128 return error_mark_node;
1130 fns = TREE_OPERAND (template_id, 0);
1131 explicit_targs = TREE_OPERAND (template_id, 1);
1133 if (fns == error_mark_node)
1134 return error_mark_node;
1136 /* Check for baselinks. */
1137 if (BASELINK_P (fns))
1138 fns = BASELINK_FUNCTIONS (fns);
1140 if (!is_overloaded_fn (fns))
1142 error ("`%D' is not a function template", fns);
1143 return error_mark_node;
1146 for (; fns; fns = OVL_NEXT (fns))
1148 tree fn = OVL_CURRENT (fns);
1150 if (TREE_CODE (fn) == TEMPLATE_DECL)
1152 tree decl_arg_types;
1154 /* DECL might be a specialization of FN. */
1156 /* Adjust the type of DECL in case FN is a static member. */
1157 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1158 if (DECL_STATIC_FUNCTION_P (fn)
1159 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1160 decl_arg_types = TREE_CHAIN (decl_arg_types);
1162 /* Check that the number of function parameters matches.
1163 For example,
1164 template <class T> void f(int i = 0);
1165 template <> void f<int>();
1166 The specialization f<int> is invalid but is not caught
1167 by get_bindings below. */
1169 if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1170 != list_length (decl_arg_types))
1171 continue;
1173 /* See whether this function might be a specialization of this
1174 template. */
1175 targs = get_bindings (fn, decl, explicit_targs);
1177 if (!targs)
1178 /* We cannot deduce template arguments that when used to
1179 specialize TMPL will produce DECL. */
1180 continue;
1182 /* Save this template, and the arguments deduced. */
1183 templates = tree_cons (targs, fn, templates);
1185 else if (need_member_template)
1186 /* FN is an ordinary member function, and we need a
1187 specialization of a member template. */
1189 else if (TREE_CODE (fn) != FUNCTION_DECL)
1190 /* We can get IDENTIFIER_NODEs here in certain erroneous
1191 cases. */
1193 else if (!DECL_FUNCTION_MEMBER_P (fn))
1194 /* This is just an ordinary non-member function. Nothing can
1195 be a specialization of that. */
1197 else if (DECL_ARTIFICIAL (fn))
1198 /* Cannot specialize functions that are created implicitly. */
1200 else
1202 tree decl_arg_types;
1204 /* This is an ordinary member function. However, since
1205 we're here, we can assume it's enclosing class is a
1206 template class. For example,
1208 template <typename T> struct S { void f(); };
1209 template <> void S<int>::f() {}
1211 Here, S<int>::f is a non-template, but S<int> is a
1212 template class. If FN has the same type as DECL, we
1213 might be in business. */
1215 if (!DECL_TEMPLATE_INFO (fn))
1216 /* Its enclosing class is an explicit specialization
1217 of a template class. This is not a candidate. */
1218 continue;
1220 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1221 TREE_TYPE (TREE_TYPE (fn))))
1222 /* The return types differ. */
1223 continue;
1225 /* Adjust the type of DECL in case FN is a static member. */
1226 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1227 if (DECL_STATIC_FUNCTION_P (fn)
1228 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1229 decl_arg_types = TREE_CHAIN (decl_arg_types);
1231 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1232 decl_arg_types))
1233 /* They match! */
1234 candidates = tree_cons (NULL_TREE, fn, candidates);
1238 if (templates && TREE_CHAIN (templates))
1240 /* We have:
1242 [temp.expl.spec]
1244 It is possible for a specialization with a given function
1245 signature to be instantiated from more than one function
1246 template. In such cases, explicit specification of the
1247 template arguments must be used to uniquely identify the
1248 function template specialization being specialized.
1250 Note that here, there's no suggestion that we're supposed to
1251 determine which of the candidate templates is most
1252 specialized. However, we, also have:
1254 [temp.func.order]
1256 Partial ordering of overloaded function template
1257 declarations is used in the following contexts to select
1258 the function template to which a function template
1259 specialization refers:
1261 -- when an explicit specialization refers to a function
1262 template.
1264 So, we do use the partial ordering rules, at least for now.
1265 This extension can only serve to make invalid programs valid,
1266 so it's safe. And, there is strong anecdotal evidence that
1267 the committee intended the partial ordering rules to apply;
1268 the EDG front-end has that behavior, and John Spicer claims
1269 that the committee simply forgot to delete the wording in
1270 [temp.expl.spec]. */
1271 tree tmpl = most_specialized (templates, decl, explicit_targs);
1272 if (tmpl && tmpl != error_mark_node)
1274 targs = get_bindings (tmpl, decl, explicit_targs);
1275 templates = tree_cons (targs, tmpl, NULL_TREE);
1279 if (templates == NULL_TREE && candidates == NULL_TREE)
1281 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1282 template_id, decl);
1283 return error_mark_node;
1285 else if ((templates && TREE_CHAIN (templates))
1286 || (candidates && TREE_CHAIN (candidates))
1287 || (templates && candidates))
1289 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1290 template_id, decl);
1291 chainon (candidates, templates);
1292 print_candidates (candidates);
1293 return error_mark_node;
1296 /* We have one, and exactly one, match. */
1297 if (candidates)
1299 /* It was a specialization of an ordinary member function in a
1300 template class. */
1301 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1302 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1305 /* It was a specialization of a template. */
1306 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1307 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1309 *targs_out = copy_node (targs);
1310 SET_TMPL_ARGS_LEVEL (*targs_out,
1311 TMPL_ARGS_DEPTH (*targs_out),
1312 TREE_PURPOSE (templates));
1314 else
1315 *targs_out = TREE_PURPOSE (templates);
1316 return TREE_VALUE (templates);
1319 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1320 but with the default argument values filled in from those in the
1321 TMPL_TYPES. */
1323 static tree
1324 copy_default_args_to_explicit_spec_1 (tree spec_types,
1325 tree tmpl_types)
1327 tree new_spec_types;
1329 if (!spec_types)
1330 return NULL_TREE;
1332 if (spec_types == void_list_node)
1333 return void_list_node;
1335 /* Substitute into the rest of the list. */
1336 new_spec_types =
1337 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1338 TREE_CHAIN (tmpl_types));
1340 /* Add the default argument for this parameter. */
1341 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1342 TREE_VALUE (spec_types),
1343 new_spec_types);
1346 /* DECL is an explicit specialization. Replicate default arguments
1347 from the template it specializes. (That way, code like:
1349 template <class T> void f(T = 3);
1350 template <> void f(double);
1351 void g () { f (); }
1353 works, as required.) An alternative approach would be to look up
1354 the correct default arguments at the call-site, but this approach
1355 is consistent with how implicit instantiations are handled. */
1357 static void
1358 copy_default_args_to_explicit_spec (tree decl)
1360 tree tmpl;
1361 tree spec_types;
1362 tree tmpl_types;
1363 tree new_spec_types;
1364 tree old_type;
1365 tree new_type;
1366 tree t;
1367 tree object_type = NULL_TREE;
1368 tree in_charge = NULL_TREE;
1369 tree vtt = NULL_TREE;
1371 /* See if there's anything we need to do. */
1372 tmpl = DECL_TI_TEMPLATE (decl);
1373 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1374 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1375 if (TREE_PURPOSE (t))
1376 break;
1377 if (!t)
1378 return;
1380 old_type = TREE_TYPE (decl);
1381 spec_types = TYPE_ARG_TYPES (old_type);
1383 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1385 /* Remove the this pointer, but remember the object's type for
1386 CV quals. */
1387 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1388 spec_types = TREE_CHAIN (spec_types);
1389 tmpl_types = TREE_CHAIN (tmpl_types);
1391 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1393 /* DECL may contain more parameters than TMPL due to the extra
1394 in-charge parameter in constructors and destructors. */
1395 in_charge = spec_types;
1396 spec_types = TREE_CHAIN (spec_types);
1398 if (DECL_HAS_VTT_PARM_P (decl))
1400 vtt = spec_types;
1401 spec_types = TREE_CHAIN (spec_types);
1405 /* Compute the merged default arguments. */
1406 new_spec_types =
1407 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1409 /* Compute the new FUNCTION_TYPE. */
1410 if (object_type)
1412 if (vtt)
1413 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1414 TREE_VALUE (vtt),
1415 new_spec_types);
1417 if (in_charge)
1418 /* Put the in-charge parameter back. */
1419 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1420 TREE_VALUE (in_charge),
1421 new_spec_types);
1423 new_type = build_cplus_method_type (object_type,
1424 TREE_TYPE (old_type),
1425 new_spec_types);
1427 else
1428 new_type = build_function_type (TREE_TYPE (old_type),
1429 new_spec_types);
1430 new_type = build_type_attribute_variant (new_type,
1431 TYPE_ATTRIBUTES (old_type));
1432 new_type = build_exception_variant (new_type,
1433 TYPE_RAISES_EXCEPTIONS (old_type));
1434 TREE_TYPE (decl) = new_type;
1437 /* Check to see if the function just declared, as indicated in
1438 DECLARATOR, and in DECL, is a specialization of a function
1439 template. We may also discover that the declaration is an explicit
1440 instantiation at this point.
1442 Returns DECL, or an equivalent declaration that should be used
1443 instead if all goes well. Issues an error message if something is
1444 amiss. Returns error_mark_node if the error is not easily
1445 recoverable.
1447 FLAGS is a bitmask consisting of the following flags:
1449 2: The function has a definition.
1450 4: The function is a friend.
1452 The TEMPLATE_COUNT is the number of references to qualifying
1453 template classes that appeared in the name of the function. For
1454 example, in
1456 template <class T> struct S { void f(); };
1457 void S<int>::f();
1459 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1460 classes are not counted in the TEMPLATE_COUNT, so that in
1462 template <class T> struct S {};
1463 template <> struct S<int> { void f(); }
1464 template <> void S<int>::f();
1466 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1467 invalid; there should be no template <>.)
1469 If the function is a specialization, it is marked as such via
1470 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1471 is set up correctly, and it is added to the list of specializations
1472 for that template. */
1474 tree
1475 check_explicit_specialization (tree declarator,
1476 tree decl,
1477 int template_count,
1478 int flags)
1480 int have_def = flags & 2;
1481 int is_friend = flags & 4;
1482 int specialization = 0;
1483 int explicit_instantiation = 0;
1484 int member_specialization = 0;
1485 tree ctype = DECL_CLASS_CONTEXT (decl);
1486 tree dname = DECL_NAME (decl);
1487 tmpl_spec_kind tsk;
1489 tsk = current_tmpl_spec_kind (template_count);
1491 switch (tsk)
1493 case tsk_none:
1494 if (processing_specialization)
1496 specialization = 1;
1497 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1499 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1501 if (is_friend)
1502 /* This could be something like:
1504 template <class T> void f(T);
1505 class S { friend void f<>(int); } */
1506 specialization = 1;
1507 else
1509 /* This case handles bogus declarations like template <>
1510 template <class T> void f<int>(); */
1512 error ("template-id `%D' in declaration of primary template",
1513 declarator);
1514 return decl;
1517 break;
1519 case tsk_invalid_member_spec:
1520 /* The error has already been reported in
1521 check_specialization_scope. */
1522 return error_mark_node;
1524 case tsk_invalid_expl_inst:
1525 error ("template parameter list used in explicit instantiation");
1527 /* Fall through. */
1529 case tsk_expl_inst:
1530 if (have_def)
1531 error ("definition provided for explicit instantiation");
1533 explicit_instantiation = 1;
1534 break;
1536 case tsk_excessive_parms:
1537 error ("too many template parameter lists in declaration of `%D'",
1538 decl);
1539 return error_mark_node;
1541 /* Fall through. */
1542 case tsk_expl_spec:
1543 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1544 if (ctype)
1545 member_specialization = 1;
1546 else
1547 specialization = 1;
1548 break;
1550 case tsk_insufficient_parms:
1551 if (template_header_count)
1553 error("too few template parameter lists in declaration of `%D'",
1554 decl);
1555 return decl;
1557 else if (ctype != NULL_TREE
1558 && !TYPE_BEING_DEFINED (ctype)
1559 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1560 && !is_friend)
1562 /* For backwards compatibility, we accept:
1564 template <class T> struct S { void f(); };
1565 void S<int>::f() {} // Missing template <>
1567 That used to be valid C++. */
1568 if (pedantic)
1569 pedwarn
1570 ("explicit specialization not preceded by `template <>'");
1571 specialization = 1;
1572 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1574 break;
1576 case tsk_template:
1577 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1579 /* This case handles bogus declarations like template <>
1580 template <class T> void f<int>(); */
1582 if (uses_template_parms (declarator))
1583 error ("partial specialization `%D' of function template",
1584 declarator);
1585 else
1586 error ("template-id `%D' in declaration of primary template",
1587 declarator);
1588 return decl;
1591 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1592 /* This is a specialization of a member template, without
1593 specialization the containing class. Something like:
1595 template <class T> struct S {
1596 template <class U> void f (U);
1598 template <> template <class U> void S<int>::f(U) {}
1600 That's a specialization -- but of the entire template. */
1601 specialization = 1;
1602 break;
1604 default:
1605 abort ();
1608 if (specialization || member_specialization)
1610 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1611 for (; t; t = TREE_CHAIN (t))
1612 if (TREE_PURPOSE (t))
1614 pedwarn
1615 ("default argument specified in explicit specialization");
1616 break;
1618 if (current_lang_name == lang_name_c)
1619 error ("template specialization with C linkage");
1622 if (specialization || member_specialization || explicit_instantiation)
1624 tree tmpl = NULL_TREE;
1625 tree targs = NULL_TREE;
1627 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1628 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1630 tree fns;
1632 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1634 if (!ctype)
1635 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1636 else
1637 fns = dname;
1639 declarator =
1640 lookup_template_function (fns, NULL_TREE);
1643 if (declarator == error_mark_node)
1644 return error_mark_node;
1646 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1648 if (!explicit_instantiation)
1649 /* A specialization in class scope. This is invalid,
1650 but the error will already have been flagged by
1651 check_specialization_scope. */
1652 return error_mark_node;
1653 else
1655 /* It's not valid to write an explicit instantiation in
1656 class scope, e.g.:
1658 class C { template void f(); }
1660 This case is caught by the parser. However, on
1661 something like:
1663 template class C { void f(); };
1665 (which is invalid) we can get here. The error will be
1666 issued later. */
1670 return decl;
1672 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1674 /* A friend declaration. We can't do much, because we don't
1675 know what this resolves to, yet. */
1676 my_friendly_assert (is_friend != 0, 0);
1677 my_friendly_assert (!explicit_instantiation, 0);
1678 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1679 return decl;
1681 else if (ctype != NULL_TREE
1682 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1683 IDENTIFIER_NODE))
1685 /* Find the list of functions in ctype that have the same
1686 name as the declared function. */
1687 tree name = TREE_OPERAND (declarator, 0);
1688 tree fns = NULL_TREE;
1689 int idx;
1691 if (constructor_name_p (name, ctype))
1693 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1695 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1696 : !TYPE_HAS_DESTRUCTOR (ctype))
1698 /* From [temp.expl.spec]:
1700 If such an explicit specialization for the member
1701 of a class template names an implicitly-declared
1702 special member function (clause _special_), the
1703 program is ill-formed.
1705 Similar language is found in [temp.explicit]. */
1706 error ("specialization of implicitly-declared special member function");
1707 return error_mark_node;
1710 name = is_constructor ? ctor_identifier : dtor_identifier;
1713 if (!DECL_CONV_FN_P (decl))
1715 idx = lookup_fnfields_1 (ctype, name);
1716 if (idx >= 0)
1717 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1719 else
1721 tree methods;
1723 /* For a type-conversion operator, we cannot do a
1724 name-based lookup. We might be looking for `operator
1725 int' which will be a specialization of `operator T'.
1726 So, we find *all* the conversion operators, and then
1727 select from them. */
1728 fns = NULL_TREE;
1730 methods = CLASSTYPE_METHOD_VEC (ctype);
1731 if (methods)
1732 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1733 idx < TREE_VEC_LENGTH (methods); ++idx)
1735 tree ovl = TREE_VEC_ELT (methods, idx);
1737 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1738 /* There are no more conversion functions. */
1739 break;
1741 /* Glue all these conversion functions together
1742 with those we already have. */
1743 for (; ovl; ovl = OVL_NEXT (ovl))
1744 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1748 if (fns == NULL_TREE)
1750 error ("no member function `%D' declared in `%T'",
1751 name, ctype);
1752 return error_mark_node;
1754 else
1755 TREE_OPERAND (declarator, 0) = fns;
1758 /* Figure out what exactly is being specialized at this point.
1759 Note that for an explicit instantiation, even one for a
1760 member function, we cannot tell apriori whether the
1761 instantiation is for a member template, or just a member
1762 function of a template class. Even if a member template is
1763 being instantiated, the member template arguments may be
1764 elided if they can be deduced from the rest of the
1765 declaration. */
1766 tmpl = determine_specialization (declarator, decl,
1767 &targs,
1768 member_specialization);
1770 if (!tmpl || tmpl == error_mark_node)
1771 /* We couldn't figure out what this declaration was
1772 specializing. */
1773 return error_mark_node;
1774 else
1776 tree gen_tmpl = most_general_template (tmpl);
1778 if (explicit_instantiation)
1780 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1781 is done by do_decl_instantiation later. */
1783 int arg_depth = TMPL_ARGS_DEPTH (targs);
1784 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1786 if (arg_depth > parm_depth)
1788 /* If TMPL is not the most general template (for
1789 example, if TMPL is a friend template that is
1790 injected into namespace scope), then there will
1791 be too many levels of TARGS. Remove some of them
1792 here. */
1793 int i;
1794 tree new_targs;
1796 new_targs = make_tree_vec (parm_depth);
1797 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1798 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1799 = TREE_VEC_ELT (targs, i);
1800 targs = new_targs;
1803 return instantiate_template (tmpl, targs, tf_error);
1806 /* If we thought that the DECL was a member function, but it
1807 turns out to be specializing a static member function,
1808 make DECL a static member function as well. We also have
1809 to adjust last_function_parms to avoid confusing
1810 start_function later. */
1811 if (DECL_STATIC_FUNCTION_P (tmpl)
1812 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1814 revert_static_member_fn (decl);
1815 last_function_parms = TREE_CHAIN (last_function_parms);
1818 /* If this is a specialization of a member template of a
1819 template class. In we want to return the TEMPLATE_DECL,
1820 not the specialization of it. */
1821 if (tsk == tsk_template)
1823 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1824 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1825 if (have_def)
1827 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
1828 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
1829 = DECL_SOURCE_LOCATION (decl);
1831 return tmpl;
1834 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1835 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1837 /* Inherit default function arguments from the template
1838 DECL is specializing. */
1839 copy_default_args_to_explicit_spec (decl);
1841 /* This specialization has the same protection as the
1842 template it specializes. */
1843 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1844 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1846 if (is_friend && !have_def)
1847 /* This is not really a declaration of a specialization.
1848 It's just the name of an instantiation. But, it's not
1849 a request for an instantiation, either. */
1850 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1851 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1852 /* This is indeed a specialization. In case of constructors
1853 and destructors, we need in-charge and not-in-charge
1854 versions in V3 ABI. */
1855 clone_function_decl (decl, /*update_method_vec_p=*/0);
1857 /* Register this specialization so that we can find it
1858 again. */
1859 decl = register_specialization (decl, gen_tmpl, targs);
1863 return decl;
1866 /* TYPE is being declared. Verify that the use of template headers
1867 and such is reasonable. Issue error messages if not. */
1869 void
1870 maybe_check_template_type (tree type)
1872 if (template_header_count)
1874 /* We are in the scope of some `template <...>' header. */
1876 int context_depth
1877 = template_class_depth_real (TYPE_CONTEXT (type),
1878 /*count_specializations=*/1);
1880 if (template_header_count <= context_depth)
1881 /* This is OK; the template headers are for the context. We
1882 are actually too lenient here; like
1883 check_explicit_specialization we should consider the number
1884 of template types included in the actual declaration. For
1885 example,
1887 template <class T> struct S {
1888 template <class U> template <class V>
1889 struct I {};
1892 is invalid, but:
1894 template <class T> struct S {
1895 template <class U> struct I;
1898 template <class T> template <class U.
1899 struct S<T>::I {};
1901 is not. */
1903 else if (template_header_count > context_depth + 1)
1904 /* There are two many template parameter lists. */
1905 error ("too many template parameter lists in declaration of `%T'", type);
1909 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1910 parameters. These are represented in the same format used for
1911 DECL_TEMPLATE_PARMS. */
1913 int comp_template_parms (tree parms1, tree parms2)
1915 tree p1;
1916 tree p2;
1918 if (parms1 == parms2)
1919 return 1;
1921 for (p1 = parms1, p2 = parms2;
1922 p1 != NULL_TREE && p2 != NULL_TREE;
1923 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1925 tree t1 = TREE_VALUE (p1);
1926 tree t2 = TREE_VALUE (p2);
1927 int i;
1929 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1930 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1932 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1933 return 0;
1935 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1937 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1938 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1940 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1941 return 0;
1943 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1944 continue;
1945 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1946 return 0;
1950 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1951 /* One set of parameters has more parameters lists than the
1952 other. */
1953 return 0;
1955 return 1;
1958 /* Complain if DECL shadows a template parameter.
1960 [temp.local]: A template-parameter shall not be redeclared within its
1961 scope (including nested scopes). */
1963 void
1964 check_template_shadow (tree decl)
1966 tree olddecl;
1968 /* If we're not in a template, we can't possibly shadow a template
1969 parameter. */
1970 if (!current_template_parms)
1971 return;
1973 /* Figure out what we're shadowing. */
1974 if (TREE_CODE (decl) == OVERLOAD)
1975 decl = OVL_CURRENT (decl);
1976 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1978 /* If there's no previous binding for this name, we're not shadowing
1979 anything, let alone a template parameter. */
1980 if (!olddecl)
1981 return;
1983 /* If we're not shadowing a template parameter, we're done. Note
1984 that OLDDECL might be an OVERLOAD (or perhaps even an
1985 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1986 node. */
1987 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1988 return;
1990 /* We check for decl != olddecl to avoid bogus errors for using a
1991 name inside a class. We check TPFI to avoid duplicate errors for
1992 inline member templates. */
1993 if (decl == olddecl
1994 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1995 return;
1997 cp_error_at ("declaration of `%#D'", decl);
1998 cp_error_at (" shadows template parm `%#D'", olddecl);
2001 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2002 ORIG_LEVEL, DECL, and TYPE. */
2004 static tree
2005 build_template_parm_index (int index,
2006 int level,
2007 int orig_level,
2008 tree decl,
2009 tree type)
2011 tree t = make_node (TEMPLATE_PARM_INDEX);
2012 TEMPLATE_PARM_IDX (t) = index;
2013 TEMPLATE_PARM_LEVEL (t) = level;
2014 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2015 TEMPLATE_PARM_DECL (t) = decl;
2016 TREE_TYPE (t) = type;
2017 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2018 TREE_READONLY (t) = TREE_READONLY (decl);
2020 return t;
2023 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2024 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2025 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2026 new one is created. */
2028 static tree
2029 reduce_template_parm_level (tree index, tree type, int levels)
2031 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2032 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2033 != TEMPLATE_PARM_LEVEL (index) - levels))
2035 tree orig_decl = TEMPLATE_PARM_DECL (index);
2036 tree decl, t;
2038 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2039 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2040 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2041 DECL_ARTIFICIAL (decl) = 1;
2042 SET_DECL_TEMPLATE_PARM_P (decl);
2044 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2045 TEMPLATE_PARM_LEVEL (index) - levels,
2046 TEMPLATE_PARM_ORIG_LEVEL (index),
2047 decl, type);
2048 TEMPLATE_PARM_DESCENDANTS (index) = t;
2050 /* Template template parameters need this. */
2051 DECL_TEMPLATE_PARMS (decl)
2052 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2055 return TEMPLATE_PARM_DESCENDANTS (index);
2058 /* Process information from new template parameter NEXT and append it to the
2059 LIST being built. */
2061 tree
2062 process_template_parm (tree list, tree next)
2064 tree parm;
2065 tree decl = 0;
2066 tree defval;
2067 int is_type, idx;
2069 parm = next;
2070 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
2071 defval = TREE_PURPOSE (parm);
2072 parm = TREE_VALUE (parm);
2073 is_type = TREE_PURPOSE (parm) == class_type_node;
2075 if (list)
2077 tree p = TREE_VALUE (tree_last (list));
2079 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2080 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2081 else
2082 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2083 ++idx;
2085 else
2086 idx = 0;
2088 if (!is_type)
2090 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
2091 /* is a const-param */
2092 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
2093 PARM, 0, NULL);
2094 SET_DECL_TEMPLATE_PARM_P (parm);
2096 /* [temp.param]
2098 The top-level cv-qualifiers on the template-parameter are
2099 ignored when determining its type. */
2100 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2102 /* A template parameter is not modifiable. */
2103 TREE_READONLY (parm) = TREE_CONSTANT (parm) = 1;
2104 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2105 TREE_TYPE (parm) = void_type_node;
2106 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2107 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
2108 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2109 = build_template_parm_index (idx, processing_template_decl,
2110 processing_template_decl,
2111 decl, TREE_TYPE (parm));
2113 else
2115 tree t;
2116 parm = TREE_VALUE (parm);
2118 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2120 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2121 /* This is for distinguishing between real templates and template
2122 template parameters */
2123 TREE_TYPE (parm) = t;
2124 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2125 decl = parm;
2127 else
2129 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2130 /* parm is either IDENTIFIER_NODE or NULL_TREE */
2131 decl = build_decl (TYPE_DECL, parm, t);
2134 TYPE_NAME (t) = decl;
2135 TYPE_STUB_DECL (t) = decl;
2136 parm = decl;
2137 TEMPLATE_TYPE_PARM_INDEX (t)
2138 = build_template_parm_index (idx, processing_template_decl,
2139 processing_template_decl,
2140 decl, TREE_TYPE (parm));
2142 DECL_ARTIFICIAL (decl) = 1;
2143 SET_DECL_TEMPLATE_PARM_P (decl);
2144 pushdecl (decl);
2145 parm = build_tree_list (defval, parm);
2146 return chainon (list, parm);
2149 /* The end of a template parameter list has been reached. Process the
2150 tree list into a parameter vector, converting each parameter into a more
2151 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2152 as PARM_DECLs. */
2154 tree
2155 end_template_parm_list (tree parms)
2157 int nparms;
2158 tree parm, next;
2159 tree saved_parmlist = make_tree_vec (list_length (parms));
2161 current_template_parms
2162 = tree_cons (size_int (processing_template_decl),
2163 saved_parmlist, current_template_parms);
2165 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2167 next = TREE_CHAIN (parm);
2168 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2169 TREE_CHAIN (parm) = NULL_TREE;
2172 --processing_template_parmlist;
2174 return saved_parmlist;
2177 /* end_template_decl is called after a template declaration is seen. */
2179 void
2180 end_template_decl (void)
2182 reset_specialization ();
2184 if (! processing_template_decl)
2185 return;
2187 /* This matches the pushlevel in begin_template_parm_list. */
2188 finish_scope ();
2190 --processing_template_decl;
2191 current_template_parms = TREE_CHAIN (current_template_parms);
2194 /* Given a template argument vector containing the template PARMS.
2195 The innermost PARMS are given first. */
2197 tree
2198 current_template_args (void)
2200 tree header;
2201 tree args = NULL_TREE;
2202 int length = TMPL_PARMS_DEPTH (current_template_parms);
2203 int l = length;
2205 /* If there is only one level of template parameters, we do not
2206 create a TREE_VEC of TREE_VECs. Instead, we return a single
2207 TREE_VEC containing the arguments. */
2208 if (length > 1)
2209 args = make_tree_vec (length);
2211 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2213 tree a = copy_node (TREE_VALUE (header));
2214 int i;
2216 TREE_TYPE (a) = NULL_TREE;
2217 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2219 tree t = TREE_VEC_ELT (a, i);
2221 /* T will be a list if we are called from within a
2222 begin/end_template_parm_list pair, but a vector directly
2223 if within a begin/end_member_template_processing pair. */
2224 if (TREE_CODE (t) == TREE_LIST)
2226 t = TREE_VALUE (t);
2228 if (TREE_CODE (t) == TYPE_DECL
2229 || TREE_CODE (t) == TEMPLATE_DECL)
2230 t = TREE_TYPE (t);
2231 else
2232 t = DECL_INITIAL (t);
2233 TREE_VEC_ELT (a, i) = t;
2237 if (length > 1)
2238 TREE_VEC_ELT (args, --l) = a;
2239 else
2240 args = a;
2243 return args;
2246 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2247 template PARMS. Used by push_template_decl below. */
2249 static tree
2250 build_template_decl (tree decl, tree parms)
2252 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2253 DECL_TEMPLATE_PARMS (tmpl) = parms;
2254 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2255 if (DECL_LANG_SPECIFIC (decl))
2257 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2258 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2259 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2260 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2261 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2262 if (DECL_OVERLOADED_OPERATOR_P (decl))
2263 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2264 DECL_OVERLOADED_OPERATOR_P (decl));
2267 return tmpl;
2270 struct template_parm_data
2272 /* The level of the template parameters we are currently
2273 processing. */
2274 int level;
2276 /* The index of the specialization argument we are currently
2277 processing. */
2278 int current_arg;
2280 /* An array whose size is the number of template parameters. The
2281 elements are nonzero if the parameter has been used in any one
2282 of the arguments processed so far. */
2283 int* parms;
2285 /* An array whose size is the number of template arguments. The
2286 elements are nonzero if the argument makes use of template
2287 parameters of this level. */
2288 int* arg_uses_template_parms;
2291 /* Subroutine of push_template_decl used to see if each template
2292 parameter in a partial specialization is used in the explicit
2293 argument list. If T is of the LEVEL given in DATA (which is
2294 treated as a template_parm_data*), then DATA->PARMS is marked
2295 appropriately. */
2297 static int
2298 mark_template_parm (tree t, void* data)
2300 int level;
2301 int idx;
2302 struct template_parm_data* tpd = (struct template_parm_data*) data;
2304 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2306 level = TEMPLATE_PARM_LEVEL (t);
2307 idx = TEMPLATE_PARM_IDX (t);
2309 else
2311 level = TEMPLATE_TYPE_LEVEL (t);
2312 idx = TEMPLATE_TYPE_IDX (t);
2315 if (level == tpd->level)
2317 tpd->parms[idx] = 1;
2318 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2321 /* Return zero so that for_each_template_parm will continue the
2322 traversal of the tree; we want to mark *every* template parm. */
2323 return 0;
2326 /* Process the partial specialization DECL. */
2328 static tree
2329 process_partial_specialization (tree decl)
2331 tree type = TREE_TYPE (decl);
2332 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2333 tree specargs = CLASSTYPE_TI_ARGS (type);
2334 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2335 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2336 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2337 int nargs = TREE_VEC_LENGTH (inner_args);
2338 int ntparms = TREE_VEC_LENGTH (inner_parms);
2339 int i;
2340 int did_error_intro = 0;
2341 struct template_parm_data tpd;
2342 struct template_parm_data tpd2;
2344 /* We check that each of the template parameters given in the
2345 partial specialization is used in the argument list to the
2346 specialization. For example:
2348 template <class T> struct S;
2349 template <class T> struct S<T*>;
2351 The second declaration is OK because `T*' uses the template
2352 parameter T, whereas
2354 template <class T> struct S<int>;
2356 is no good. Even trickier is:
2358 template <class T>
2359 struct S1
2361 template <class U>
2362 struct S2;
2363 template <class U>
2364 struct S2<T>;
2367 The S2<T> declaration is actually invalid; it is a
2368 full-specialization. Of course,
2370 template <class U>
2371 struct S2<T (*)(U)>;
2373 or some such would have been OK. */
2374 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2375 tpd.parms = alloca (sizeof (int) * ntparms);
2376 memset (tpd.parms, 0, sizeof (int) * ntparms);
2378 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2379 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2380 for (i = 0; i < nargs; ++i)
2382 tpd.current_arg = i;
2383 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2384 &mark_template_parm,
2385 &tpd,
2386 NULL);
2388 for (i = 0; i < ntparms; ++i)
2389 if (tpd.parms[i] == 0)
2391 /* One of the template parms was not used in the
2392 specialization. */
2393 if (!did_error_intro)
2395 error ("template parameters not used in partial specialization:");
2396 did_error_intro = 1;
2399 error (" `%D'",
2400 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2403 /* [temp.class.spec]
2405 The argument list of the specialization shall not be identical to
2406 the implicit argument list of the primary template. */
2407 if (comp_template_args
2408 (inner_args,
2409 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2410 (maintmpl)))))
2411 error ("partial specialization `%T' does not specialize any template arguments", type);
2413 /* [temp.class.spec]
2415 A partially specialized non-type argument expression shall not
2416 involve template parameters of the partial specialization except
2417 when the argument expression is a simple identifier.
2419 The type of a template parameter corresponding to a specialized
2420 non-type argument shall not be dependent on a parameter of the
2421 specialization. */
2422 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2423 tpd2.parms = 0;
2424 for (i = 0; i < nargs; ++i)
2426 tree arg = TREE_VEC_ELT (inner_args, i);
2427 if (/* These first two lines are the `non-type' bit. */
2428 !TYPE_P (arg)
2429 && TREE_CODE (arg) != TEMPLATE_DECL
2430 /* This next line is the `argument expression is not just a
2431 simple identifier' condition and also the `specialized
2432 non-type argument' bit. */
2433 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2435 if (tpd.arg_uses_template_parms[i])
2436 error ("template argument `%E' involves template parameter(s)", arg);
2437 else
2439 /* Look at the corresponding template parameter,
2440 marking which template parameters its type depends
2441 upon. */
2442 tree type =
2443 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2444 i)));
2446 if (!tpd2.parms)
2448 /* We haven't yet initialized TPD2. Do so now. */
2449 tpd2.arg_uses_template_parms
2450 = (int*) alloca (sizeof (int) * nargs);
2451 /* The number of parameters here is the number in the
2452 main template, which, as checked in the assertion
2453 above, is NARGS. */
2454 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2455 tpd2.level =
2456 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2459 /* Mark the template parameters. But this time, we're
2460 looking for the template parameters of the main
2461 template, not in the specialization. */
2462 tpd2.current_arg = i;
2463 tpd2.arg_uses_template_parms[i] = 0;
2464 memset (tpd2.parms, 0, sizeof (int) * nargs);
2465 for_each_template_parm (type,
2466 &mark_template_parm,
2467 &tpd2,
2468 NULL);
2470 if (tpd2.arg_uses_template_parms [i])
2472 /* The type depended on some template parameters.
2473 If they are fully specialized in the
2474 specialization, that's OK. */
2475 int j;
2476 for (j = 0; j < nargs; ++j)
2477 if (tpd2.parms[j] != 0
2478 && tpd.arg_uses_template_parms [j])
2480 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2481 type,
2482 arg);
2483 break;
2490 if (retrieve_specialization (maintmpl, specargs))
2491 /* We've already got this specialization. */
2492 return decl;
2494 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2495 = tree_cons (inner_args, inner_parms,
2496 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2497 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2498 return decl;
2501 /* Check that a template declaration's use of default arguments is not
2502 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2503 nonzero if DECL is the thing declared by a primary template.
2504 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2506 static void
2507 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2509 const char *msg;
2510 int last_level_to_check;
2511 tree parm_level;
2513 /* [temp.param]
2515 A default template-argument shall not be specified in a
2516 function template declaration or a function template definition, nor
2517 in the template-parameter-list of the definition of a member of a
2518 class template. */
2520 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2521 /* You can't have a function template declaration in a local
2522 scope, nor you can you define a member of a class template in a
2523 local scope. */
2524 return;
2526 if (current_class_type
2527 && !TYPE_BEING_DEFINED (current_class_type)
2528 && DECL_LANG_SPECIFIC (decl)
2529 /* If this is either a friend defined in the scope of the class
2530 or a member function. */
2531 && ((DECL_CONTEXT (decl)
2532 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2533 || (DECL_FRIEND_CONTEXT (decl)
2534 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2535 current_class_type)))
2536 /* And, if it was a member function, it really was defined in
2537 the scope of the class. */
2538 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2539 /* We already checked these parameters when the template was
2540 declared, so there's no need to do it again now. This function
2541 was defined in class scope, but we're processing it's body now
2542 that the class is complete. */
2543 return;
2545 /* [temp.param]
2547 If a template-parameter has a default template-argument, all
2548 subsequent template-parameters shall have a default
2549 template-argument supplied. */
2550 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2552 tree inner_parms = TREE_VALUE (parm_level);
2553 int ntparms = TREE_VEC_LENGTH (inner_parms);
2554 int seen_def_arg_p = 0;
2555 int i;
2557 for (i = 0; i < ntparms; ++i)
2559 tree parm = TREE_VEC_ELT (inner_parms, i);
2560 if (TREE_PURPOSE (parm))
2561 seen_def_arg_p = 1;
2562 else if (seen_def_arg_p)
2564 error ("no default argument for `%D'", TREE_VALUE (parm));
2565 /* For better subsequent error-recovery, we indicate that
2566 there should have been a default argument. */
2567 TREE_PURPOSE (parm) = error_mark_node;
2572 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2573 /* For an ordinary class template, default template arguments are
2574 allowed at the innermost level, e.g.:
2575 template <class T = int>
2576 struct S {};
2577 but, in a partial specialization, they're not allowed even
2578 there, as we have in [temp.class.spec]:
2580 The template parameter list of a specialization shall not
2581 contain default template argument values.
2583 So, for a partial specialization, or for a function template,
2584 we look at all of them. */
2586 else
2587 /* But, for a primary class template that is not a partial
2588 specialization we look at all template parameters except the
2589 innermost ones. */
2590 parms = TREE_CHAIN (parms);
2592 /* Figure out what error message to issue. */
2593 if (TREE_CODE (decl) == FUNCTION_DECL)
2594 msg = "default template arguments may not be used in function templates";
2595 else if (is_partial)
2596 msg = "default template arguments may not be used in partial specializations";
2597 else
2598 msg = "default argument for template parameter for class enclosing `%D'";
2600 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2601 /* If we're inside a class definition, there's no need to
2602 examine the parameters to the class itself. On the one
2603 hand, they will be checked when the class is defined, and,
2604 on the other, default arguments are valid in things like:
2605 template <class T = double>
2606 struct S { template <class U> void f(U); };
2607 Here the default argument for `S' has no bearing on the
2608 declaration of `f'. */
2609 last_level_to_check = template_class_depth (current_class_type) + 1;
2610 else
2611 /* Check everything. */
2612 last_level_to_check = 0;
2614 for (parm_level = parms;
2615 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2616 parm_level = TREE_CHAIN (parm_level))
2618 tree inner_parms = TREE_VALUE (parm_level);
2619 int i;
2620 int ntparms;
2622 ntparms = TREE_VEC_LENGTH (inner_parms);
2623 for (i = 0; i < ntparms; ++i)
2624 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2626 if (msg)
2628 error (msg, decl);
2629 msg = 0;
2632 /* Clear out the default argument so that we are not
2633 confused later. */
2634 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2637 /* At this point, if we're still interested in issuing messages,
2638 they must apply to classes surrounding the object declared. */
2639 if (msg)
2640 msg = "default argument for template parameter for class enclosing `%D'";
2644 /* Worker for push_template_decl_real, called via
2645 for_each_template_parm. DATA is really an int, indicating the
2646 level of the parameters we are interested in. If T is a template
2647 parameter of that level, return nonzero. */
2649 static int
2650 template_parm_this_level_p (tree t, void* data)
2652 int this_level = *(int *)data;
2653 int level;
2655 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2656 level = TEMPLATE_PARM_LEVEL (t);
2657 else
2658 level = TEMPLATE_TYPE_LEVEL (t);
2659 return level == this_level;
2662 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2663 parameters given by current_template_args, or reuses a
2664 previously existing one, if appropriate. Returns the DECL, or an
2665 equivalent one, if it is replaced via a call to duplicate_decls.
2667 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2669 tree
2670 push_template_decl_real (tree decl, int is_friend)
2672 tree tmpl;
2673 tree args;
2674 tree info;
2675 tree ctx;
2676 int primary;
2677 int is_partial;
2678 int new_template_p = 0;
2680 /* See if this is a partial specialization. */
2681 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2682 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2683 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2685 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2687 if (is_friend)
2688 /* For a friend, we want the context of the friend function, not
2689 the type of which it is a friend. */
2690 ctx = DECL_CONTEXT (decl);
2691 else if (CP_DECL_CONTEXT (decl)
2692 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2693 /* In the case of a virtual function, we want the class in which
2694 it is defined. */
2695 ctx = CP_DECL_CONTEXT (decl);
2696 else
2697 /* Otherwise, if we're currently defining some class, the DECL
2698 is assumed to be a member of the class. */
2699 ctx = current_scope ();
2701 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2702 ctx = NULL_TREE;
2704 if (!DECL_CONTEXT (decl))
2705 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2707 /* See if this is a primary template. */
2708 primary = template_parm_scope_p ();
2710 if (primary)
2712 if (current_lang_name == lang_name_c)
2713 error ("template with C linkage");
2714 else if (TREE_CODE (decl) == TYPE_DECL
2715 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2716 error ("template class without a name");
2717 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2718 && CLASS_TYPE_P (TREE_TYPE (decl)))
2719 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2720 || TREE_CODE (decl) == FUNCTION_DECL)
2721 /* OK */;
2722 else
2723 error ("template declaration of `%#D'", decl);
2726 /* Check to see that the rules regarding the use of default
2727 arguments are not being violated. */
2728 check_default_tmpl_args (decl, current_template_parms,
2729 primary, is_partial);
2731 if (is_partial)
2732 return process_partial_specialization (decl);
2734 args = current_template_args ();
2736 if (!ctx
2737 || TREE_CODE (ctx) == FUNCTION_DECL
2738 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2739 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2741 if (DECL_LANG_SPECIFIC (decl)
2742 && DECL_TEMPLATE_INFO (decl)
2743 && DECL_TI_TEMPLATE (decl))
2744 tmpl = DECL_TI_TEMPLATE (decl);
2745 /* If DECL is a TYPE_DECL for a class-template, then there won't
2746 be DECL_LANG_SPECIFIC. The information equivalent to
2747 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2748 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2749 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2750 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2752 /* Since a template declaration already existed for this
2753 class-type, we must be redeclaring it here. Make sure
2754 that the redeclaration is valid. */
2755 redeclare_class_template (TREE_TYPE (decl),
2756 current_template_parms);
2757 /* We don't need to create a new TEMPLATE_DECL; just use the
2758 one we already had. */
2759 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2761 else
2763 tmpl = build_template_decl (decl, current_template_parms);
2764 new_template_p = 1;
2766 if (DECL_LANG_SPECIFIC (decl)
2767 && DECL_TEMPLATE_SPECIALIZATION (decl))
2769 /* A specialization of a member template of a template
2770 class. */
2771 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2772 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2773 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2777 else
2779 tree a, t, current, parms;
2780 int i;
2782 if (TREE_CODE (decl) == TYPE_DECL)
2784 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2785 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2786 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2787 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2788 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2789 else
2791 error ("`%D' does not declare a template type", decl);
2792 return decl;
2795 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2797 error ("template definition of non-template `%#D'", decl);
2798 return decl;
2800 else
2801 tmpl = DECL_TI_TEMPLATE (decl);
2803 if (is_member_template (tmpl)
2804 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2805 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2806 && DECL_TEMPLATE_SPECIALIZATION (decl))
2808 tree new_tmpl;
2810 /* The declaration is a specialization of a member
2811 template, declared outside the class. Therefore, the
2812 innermost template arguments will be NULL, so we
2813 replace them with the arguments determined by the
2814 earlier call to check_explicit_specialization. */
2815 args = DECL_TI_ARGS (decl);
2817 new_tmpl
2818 = build_template_decl (decl, current_template_parms);
2819 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2820 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2821 DECL_TI_TEMPLATE (decl) = new_tmpl;
2822 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2823 DECL_TEMPLATE_INFO (new_tmpl)
2824 = tree_cons (tmpl, args, NULL_TREE);
2826 register_specialization (new_tmpl,
2827 most_general_template (tmpl),
2828 args);
2829 return decl;
2832 /* Make sure the template headers we got make sense. */
2834 parms = DECL_TEMPLATE_PARMS (tmpl);
2835 i = TMPL_PARMS_DEPTH (parms);
2836 if (TMPL_ARGS_DEPTH (args) != i)
2838 error ("expected %d levels of template parms for `%#D', got %d",
2839 i, decl, TMPL_ARGS_DEPTH (args));
2841 else
2842 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2844 a = TMPL_ARGS_LEVEL (args, i);
2845 t = INNERMOST_TEMPLATE_PARMS (parms);
2847 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2849 if (current == decl)
2850 error ("got %d template parameters for `%#D'",
2851 TREE_VEC_LENGTH (a), decl);
2852 else
2853 error ("got %d template parameters for `%#T'",
2854 TREE_VEC_LENGTH (a), current);
2855 error (" but %d required", TREE_VEC_LENGTH (t));
2858 /* Perhaps we should also check that the parms are used in the
2859 appropriate qualifying scopes in the declarator? */
2861 if (current == decl)
2862 current = ctx;
2863 else
2864 current = TYPE_CONTEXT (current);
2868 DECL_TEMPLATE_RESULT (tmpl) = decl;
2869 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2871 /* Push template declarations for global functions and types. Note
2872 that we do not try to push a global template friend declared in a
2873 template class; such a thing may well depend on the template
2874 parameters of the class. */
2875 if (new_template_p && !ctx
2876 && !(is_friend && template_class_depth (current_class_type) > 0))
2877 tmpl = pushdecl_namespace_level (tmpl);
2879 if (primary)
2881 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2882 if (DECL_CONV_FN_P (tmpl))
2884 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2886 /* It is a conversion operator. See if the type converted to
2887 depends on innermost template operands. */
2889 if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)),
2890 template_parm_this_level_p,
2891 &depth,
2892 NULL))
2893 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
2897 info = tree_cons (tmpl, args, NULL_TREE);
2899 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2901 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2902 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2903 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2904 /* Don't change the name if we've already set it up. */
2905 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2906 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2908 else if (DECL_LANG_SPECIFIC (decl))
2909 DECL_TEMPLATE_INFO (decl) = info;
2911 return DECL_TEMPLATE_RESULT (tmpl);
2914 tree
2915 push_template_decl (tree decl)
2917 return push_template_decl_real (decl, 0);
2920 /* Called when a class template TYPE is redeclared with the indicated
2921 template PARMS, e.g.:
2923 template <class T> struct S;
2924 template <class T> struct S {}; */
2926 void
2927 redeclare_class_template (tree type, tree parms)
2929 tree tmpl;
2930 tree tmpl_parms;
2931 int i;
2933 if (!TYPE_TEMPLATE_INFO (type))
2935 error ("`%T' is not a template type", type);
2936 return;
2939 tmpl = TYPE_TI_TEMPLATE (type);
2940 if (!PRIMARY_TEMPLATE_P (tmpl))
2941 /* The type is nested in some template class. Nothing to worry
2942 about here; there are no new template parameters for the nested
2943 type. */
2944 return;
2946 parms = INNERMOST_TEMPLATE_PARMS (parms);
2947 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2949 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2951 cp_error_at ("previous declaration `%D'", tmpl);
2952 error ("used %d template parameter%s instead of %d",
2953 TREE_VEC_LENGTH (tmpl_parms),
2954 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2955 TREE_VEC_LENGTH (parms));
2956 return;
2959 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2961 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2962 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2963 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2964 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2966 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2968 cp_error_at ("template parameter `%#D'", tmpl_parm);
2969 error ("redeclared here as `%#D'", parm);
2970 return;
2973 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2975 /* We have in [temp.param]:
2977 A template-parameter may not be given default arguments
2978 by two different declarations in the same scope. */
2979 error ("redefinition of default argument for `%#D'", parm);
2980 cp_error_at (" original definition appeared here", tmpl_parm);
2981 return;
2984 if (parm_default != NULL_TREE)
2985 /* Update the previous template parameters (which are the ones
2986 that will really count) with the new default value. */
2987 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2988 else if (tmpl_default != NULL_TREE)
2989 /* Update the new parameters, too; they'll be used as the
2990 parameters for any members. */
2991 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2995 /* Attempt to convert the non-type template parameter EXPR to the
2996 indicated TYPE. If the conversion is successful, return the
2997 converted value. If the conversion is unsuccessful, return
2998 NULL_TREE if we issued an error message, or error_mark_node if we
2999 did not. We issue error messages for out-and-out bad template
3000 parameters, but not simply because the conversion failed, since we
3001 might be just trying to do argument deduction. By the time this
3002 function is called, neither TYPE nor EXPR may make use of template
3003 parameters. */
3005 static tree
3006 convert_nontype_argument (tree type, tree expr)
3008 tree expr_type = TREE_TYPE (expr);
3010 /* A template-argument for a non-type, non-template
3011 template-parameter shall be one of:
3013 --an integral constant-expression of integral or enumeration
3014 type; or
3016 --the name of a non-type template-parameter; or
3018 --the name of an object or function with external linkage,
3019 including function templates and function template-ids but
3020 excluding non-static class members, expressed as id-expression;
3023 --the address of an object or function with external linkage,
3024 including function templates and function template-ids but
3025 excluding non-static class members, expressed as & id-expression
3026 where the & is optional if the name refers to a function or
3027 array; or
3029 --a pointer to member expressed as described in _expr.unary.op_. */
3031 /* An integral constant-expression can include const variables or
3032 enumerators. Simplify things by folding them to their values,
3033 unless we're about to bind the declaration to a reference
3034 parameter. */
3035 if (INTEGRAL_TYPE_P (expr_type)
3036 && TREE_CODE (type) != REFERENCE_TYPE)
3037 expr = decl_constant_value (expr);
3039 if (is_overloaded_fn (expr))
3040 /* OK for now. We'll check that it has external linkage later.
3041 Check this first since if expr_type is the unknown_type_node
3042 we would otherwise complain below. */
3044 else if (TYPE_PTRMEM_P (expr_type)
3045 || TYPE_PTRMEMFUNC_P (expr_type))
3047 if (TREE_CODE (expr) != PTRMEM_CST)
3048 goto bad_argument;
3050 else if (TYPE_PTR_P (expr_type)
3051 || TYPE_PTRMEM_P (expr_type)
3052 || TREE_CODE (expr_type) == ARRAY_TYPE
3053 || TREE_CODE (type) == REFERENCE_TYPE
3054 /* If expr is the address of an overloaded function, we
3055 will get the unknown_type_node at this point. */
3056 || expr_type == unknown_type_node)
3058 tree referent;
3059 tree e = expr;
3060 STRIP_NOPS (e);
3062 if (TREE_CODE (expr_type) == ARRAY_TYPE
3063 || (TREE_CODE (type) == REFERENCE_TYPE
3064 && TREE_CODE (e) != ADDR_EXPR))
3065 referent = e;
3066 else
3068 if (TREE_CODE (e) != ADDR_EXPR)
3070 bad_argument:
3071 error ("`%E' is not a valid template argument", expr);
3072 if (TYPE_PTR_P (expr_type))
3074 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
3075 error ("it must be the address of a function with external linkage");
3076 else
3077 error ("it must be the address of an object with external linkage");
3079 else if (TYPE_PTRMEM_P (expr_type)
3080 || TYPE_PTRMEMFUNC_P (expr_type))
3081 error ("it must be a pointer-to-member of the form `&X::Y'");
3083 return NULL_TREE;
3086 referent = TREE_OPERAND (e, 0);
3087 STRIP_NOPS (referent);
3090 if (TREE_CODE (referent) == STRING_CST)
3092 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
3093 referent);
3094 return NULL_TREE;
3097 if (is_overloaded_fn (referent))
3098 /* We'll check that it has external linkage later. */
3100 else if (TREE_CODE (referent) != VAR_DECL)
3101 goto bad_argument;
3102 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
3104 error ("address of non-extern `%E' cannot be used as template argument", referent);
3105 return error_mark_node;
3108 else if (INTEGRAL_TYPE_P (expr_type)
3109 || TYPE_PTRMEM_P (expr_type)
3110 || TYPE_PTRMEMFUNC_P (expr_type))
3112 if (! TREE_CONSTANT (expr))
3114 non_constant:
3115 error ("non-constant `%E' cannot be used as template argument",
3116 expr);
3117 return NULL_TREE;
3120 else
3122 error ("object `%E' cannot be used as template argument", expr);
3123 return NULL_TREE;
3126 switch (TREE_CODE (type))
3128 case INTEGER_TYPE:
3129 case BOOLEAN_TYPE:
3130 case ENUMERAL_TYPE:
3131 /* For a non-type template-parameter of integral or enumeration
3132 type, integral promotions (_conv.prom_) and integral
3133 conversions (_conv.integral_) are applied. */
3134 if (!INTEGRAL_TYPE_P (expr_type))
3135 return error_mark_node;
3137 /* It's safe to call digest_init in this case; we know we're
3138 just converting one integral constant expression to another. */
3139 expr = digest_init (type, expr, (tree*) 0);
3141 if (TREE_CODE (expr) != INTEGER_CST)
3142 /* Curiously, some TREE_CONSTANT integral expressions do not
3143 simplify to integer constants. For example, `3 % 0',
3144 remains a TRUNC_MOD_EXPR. */
3145 goto non_constant;
3147 return expr;
3149 case POINTER_TYPE:
3151 tree type_pointed_to = TREE_TYPE (type);
3153 if (TYPE_PTRMEM_P (type))
3155 tree e;
3157 /* For a non-type template-parameter of type pointer to data
3158 member, qualification conversions (_conv.qual_) are
3159 applied. */
3160 e = perform_qualification_conversions (type, expr);
3161 if (TREE_CODE (e) == NOP_EXPR)
3162 /* The call to perform_qualification_conversions will
3163 insert a NOP_EXPR over EXPR to do express conversion,
3164 if necessary. But, that will confuse us if we use
3165 this (converted) template parameter to instantiate
3166 another template; then the thing will not look like a
3167 valid template argument. So, just make a new
3168 constant, of the appropriate type. */
3169 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3170 return e;
3172 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3174 /* For a non-type template-parameter of type pointer to
3175 function, only the function-to-pointer conversion
3176 (_conv.func_) is applied. If the template-argument
3177 represents a set of overloaded functions (or a pointer to
3178 such), the matching function is selected from the set
3179 (_over.over_). */
3180 tree fns;
3181 tree fn;
3183 if (TREE_CODE (expr) == ADDR_EXPR)
3184 fns = TREE_OPERAND (expr, 0);
3185 else
3186 fns = expr;
3188 fn = instantiate_type (type_pointed_to, fns, tf_none);
3190 if (fn == error_mark_node)
3191 return error_mark_node;
3193 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3195 if (really_overloaded_fn (fns))
3196 return error_mark_node;
3197 else
3198 goto bad_argument;
3201 expr = build_unary_op (ADDR_EXPR, fn, 0);
3203 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3205 return expr;
3207 else
3209 /* For a non-type template-parameter of type pointer to
3210 object, qualification conversions (_conv.qual_) and the
3211 array-to-pointer conversion (_conv.array_) are applied.
3212 [Note: In particular, neither the null pointer conversion
3213 (_conv.ptr_) nor the derived-to-base conversion
3214 (_conv.ptr_) are applied. Although 0 is a valid
3215 template-argument for a non-type template-parameter of
3216 integral type, it is not a valid template-argument for a
3217 non-type template-parameter of pointer type.]
3219 The call to decay_conversion performs the
3220 array-to-pointer conversion, if appropriate. */
3221 expr = decay_conversion (expr);
3223 if (expr == error_mark_node)
3224 return error_mark_node;
3225 else
3226 return perform_qualification_conversions (type, expr);
3229 break;
3231 case REFERENCE_TYPE:
3233 tree type_referred_to = TREE_TYPE (type);
3235 /* If this expression already has reference type, get the
3236 underling object. */
3237 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3239 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3240 expr = TREE_OPERAND (expr, 0);
3241 expr_type = TREE_TYPE (expr);
3244 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3246 /* For a non-type template-parameter of type reference to
3247 function, no conversions apply. If the
3248 template-argument represents a set of overloaded
3249 functions, the matching function is selected from the
3250 set (_over.over_). */
3251 tree fn;
3253 fn = instantiate_type (type_referred_to, expr, tf_none);
3255 if (fn == error_mark_node)
3256 return error_mark_node;
3258 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3260 if (really_overloaded_fn (expr))
3261 /* Don't issue an error here; we might get a different
3262 function if the overloading had worked out
3263 differently. */
3264 return error_mark_node;
3265 else
3266 goto bad_argument;
3269 my_friendly_assert (same_type_p (type_referred_to,
3270 TREE_TYPE (fn)),
3273 expr = fn;
3275 else
3277 /* For a non-type template-parameter of type reference to
3278 object, no conversions apply. The type referred to by the
3279 reference may be more cv-qualified than the (otherwise
3280 identical) type of the template-argument. The
3281 template-parameter is bound directly to the
3282 template-argument, which must be an lvalue. */
3283 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3284 TYPE_MAIN_VARIANT (type_referred_to))
3285 || !at_least_as_qualified_p (type_referred_to,
3286 expr_type)
3287 || !real_lvalue_p (expr))
3288 return error_mark_node;
3291 cxx_mark_addressable (expr);
3292 return build1 (ADDR_EXPR, type, expr);
3294 break;
3296 case RECORD_TYPE:
3298 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3300 /* For a non-type template-parameter of type pointer to member
3301 function, no conversions apply. If the template-argument
3302 represents a set of overloaded member functions, the
3303 matching member function is selected from the set
3304 (_over.over_). */
3306 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3307 expr_type != unknown_type_node)
3308 return error_mark_node;
3310 if (TREE_CODE (expr) == PTRMEM_CST)
3312 /* A ptr-to-member constant. */
3313 if (!same_type_p (type, expr_type))
3314 return error_mark_node;
3315 else
3316 return expr;
3319 if (TREE_CODE (expr) != ADDR_EXPR)
3320 return error_mark_node;
3322 expr = instantiate_type (type, expr, tf_none);
3324 if (expr == error_mark_node)
3325 return error_mark_node;
3327 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3329 return expr;
3331 break;
3333 default:
3334 /* All non-type parameters must have one of these types. */
3335 abort ();
3336 break;
3339 return error_mark_node;
3342 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3343 template template parameters. Both PARM_PARMS and ARG_PARMS are
3344 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3345 or PARM_DECL.
3347 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3348 the case, then extra parameters must have default arguments.
3350 Consider the example:
3351 template <class T, class Allocator = allocator> class vector;
3352 template<template <class U> class TT> class C;
3354 C<vector> is a valid instantiation. PARM_PARMS for the above code
3355 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3356 T and Allocator) and OUTER_ARGS contains the argument that is used to
3357 substitute the TT parameter. */
3359 static int
3360 coerce_template_template_parms (tree parm_parms,
3361 tree arg_parms,
3362 tsubst_flags_t complain,
3363 tree in_decl,
3364 tree outer_args)
3366 int nparms, nargs, i;
3367 tree parm, arg;
3369 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3370 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3372 nparms = TREE_VEC_LENGTH (parm_parms);
3373 nargs = TREE_VEC_LENGTH (arg_parms);
3375 /* The rule here is opposite of coerce_template_parms. */
3376 if (nargs < nparms
3377 || (nargs > nparms
3378 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3379 return 0;
3381 for (i = 0; i < nparms; ++i)
3383 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3384 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3386 if (arg == NULL_TREE || arg == error_mark_node
3387 || parm == NULL_TREE || parm == error_mark_node)
3388 return 0;
3390 if (TREE_CODE (arg) != TREE_CODE (parm))
3391 return 0;
3393 switch (TREE_CODE (parm))
3395 case TYPE_DECL:
3396 break;
3398 case TEMPLATE_DECL:
3399 /* We encounter instantiations of templates like
3400 template <template <template <class> class> class TT>
3401 class C; */
3403 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3404 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3406 if (!coerce_template_template_parms (parmparm, argparm,
3407 complain, in_decl,
3408 outer_args))
3409 return 0;
3411 break;
3413 case PARM_DECL:
3414 /* The tsubst call is used to handle cases such as
3415 template <class T, template <T> class TT> class D;
3416 i.e. the parameter list of TT depends on earlier parameters. */
3417 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3418 complain, in_decl),
3419 TREE_TYPE (arg)))
3420 return 0;
3421 break;
3423 default:
3424 abort ();
3427 return 1;
3430 /* Convert the indicated template ARG as necessary to match the
3431 indicated template PARM. Returns the converted ARG, or
3432 error_mark_node if the conversion was unsuccessful. Error and
3433 warning messages are issued under control of COMPLAIN. This
3434 conversion is for the Ith parameter in the parameter list. ARGS is
3435 the full set of template arguments deduced so far. */
3437 static tree
3438 convert_template_argument (tree parm,
3439 tree arg,
3440 tree args,
3441 tsubst_flags_t complain,
3442 int i,
3443 tree in_decl)
3445 tree val;
3446 tree inner_args;
3447 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3449 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3451 if (TREE_CODE (arg) == TREE_LIST
3452 && TREE_TYPE (arg) != NULL_TREE
3453 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3455 /* The template argument was the name of some
3456 member function. That's usually
3457 invalid, but static members are OK. In any
3458 case, grab the underlying fields/functions
3459 and issue an error later if required. */
3460 arg = TREE_VALUE (arg);
3461 TREE_TYPE (arg) = unknown_type_node;
3464 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3465 requires_type = (TREE_CODE (parm) == TYPE_DECL
3466 || requires_tmpl_type);
3468 if (TREE_CODE (arg) != RECORD_TYPE)
3469 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3470 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3471 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3472 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3473 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3474 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3476 if (is_base_of_enclosing_class (arg, current_class_type))
3477 /* This is a template name used within the scope of the
3478 template. It could be the template, or it could be the
3479 instantiation. Choose whichever makes sense. */
3480 is_tmpl_type = requires_tmpl_type;
3481 else
3482 is_tmpl_type = 1;
3484 else
3485 /* It is a non-template class, or a specialization of a template
3486 class, or a non-template member of a template class. */
3487 is_tmpl_type = 0;
3489 if (is_tmpl_type
3490 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3491 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3492 arg = TYPE_STUB_DECL (arg);
3493 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3494 arg = CLASSTYPE_TI_TEMPLATE (arg);
3496 is_type = TYPE_P (arg) || is_tmpl_type;
3498 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3499 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3501 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3503 arg = make_typename_type (TREE_OPERAND (arg, 0),
3504 TREE_OPERAND (arg, 1),
3505 complain & tf_error);
3506 is_type = 1;
3508 if (is_type != requires_type)
3510 if (in_decl)
3512 if (complain & tf_error)
3514 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3515 i + 1, in_decl);
3516 if (is_type)
3517 error (" expected a constant of type `%T', got `%T'",
3518 TREE_TYPE (parm),
3519 (is_tmpl_type ? DECL_NAME (arg) : arg));
3520 else if (requires_tmpl_type)
3521 error (" expected a class template, got `%E'", arg);
3522 else
3523 error (" expected a type, got `%E'", arg);
3526 return error_mark_node;
3528 if (is_tmpl_type ^ requires_tmpl_type)
3530 if (in_decl && (complain & tf_error))
3532 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3533 i + 1, in_decl);
3534 if (is_tmpl_type)
3535 error (" expected a type, got `%T'", DECL_NAME (arg));
3536 else
3537 error (" expected a class template, got `%T'", arg);
3539 return error_mark_node;
3542 if (is_type)
3544 if (requires_tmpl_type)
3546 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3547 /* The number of argument required is not known yet.
3548 Just accept it for now. */
3549 val = TREE_TYPE (arg);
3550 else
3552 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3553 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3555 if (coerce_template_template_parms (parmparm, argparm,
3556 complain, in_decl,
3557 inner_args))
3559 val = arg;
3561 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3562 TEMPLATE_DECL. */
3563 if (val != error_mark_node
3564 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3565 val = TREE_TYPE (val);
3567 else
3569 if (in_decl && (complain & tf_error))
3571 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3572 i + 1, in_decl);
3573 error (" expected a template of type `%D', got `%D'", parm, arg);
3576 val = error_mark_node;
3580 else
3581 val = groktypename (arg);
3583 else
3585 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3587 if (invalid_nontype_parm_type_p (t, complain))
3588 return error_mark_node;
3590 if (processing_template_decl)
3591 arg = maybe_fold_nontype_arg (arg);
3593 if (!uses_template_parms (arg) && !uses_template_parms (t))
3594 /* We used to call digest_init here. However, digest_init
3595 will report errors, which we don't want when complain
3596 is zero. More importantly, digest_init will try too
3597 hard to convert things: for example, `0' should not be
3598 converted to pointer type at this point according to
3599 the standard. Accepting this is not merely an
3600 extension, since deciding whether or not these
3601 conversions can occur is part of determining which
3602 function template to call, or whether a given explicit
3603 argument specification is valid. */
3604 val = convert_nontype_argument (t, arg);
3605 else
3606 val = arg;
3608 if (val == NULL_TREE)
3609 val = error_mark_node;
3610 else if (val == error_mark_node && (complain & tf_error))
3611 error ("could not convert template argument `%E' to `%T'",
3612 arg, t);
3615 return val;
3618 /* Convert all template arguments to their appropriate types, and
3619 return a vector containing the innermost resulting template
3620 arguments. If any error occurs, return error_mark_node. Error and
3621 warning messages are issued under control of COMPLAIN.
3623 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3624 provided in ARGLIST, or else trailing parameters must have default
3625 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3626 deduction for any unspecified trailing arguments. */
3628 static tree
3629 coerce_template_parms (tree parms,
3630 tree args,
3631 tree in_decl,
3632 tsubst_flags_t complain,
3633 int require_all_arguments)
3635 int nparms, nargs, i, lost = 0;
3636 tree inner_args;
3637 tree new_args;
3638 tree new_inner_args;
3640 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3641 nargs = NUM_TMPL_ARGS (inner_args);
3642 nparms = TREE_VEC_LENGTH (parms);
3644 if (nargs > nparms
3645 || (nargs < nparms
3646 && require_all_arguments
3647 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3649 if (complain & tf_error)
3651 error ("wrong number of template arguments (%d, should be %d)",
3652 nargs, nparms);
3654 if (in_decl)
3655 cp_error_at ("provided for `%D'", in_decl);
3658 return error_mark_node;
3661 new_inner_args = make_tree_vec (nparms);
3662 new_args = add_outermost_template_args (args, new_inner_args);
3663 for (i = 0; i < nparms; i++)
3665 tree arg;
3666 tree parm;
3668 /* Get the Ith template parameter. */
3669 parm = TREE_VEC_ELT (parms, i);
3671 /* Calculate the Ith argument. */
3672 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3674 arg = TREE_VALUE (inner_args);
3675 inner_args = TREE_CHAIN (inner_args);
3677 else if (i < nargs)
3678 arg = TREE_VEC_ELT (inner_args, i);
3679 /* If no template argument was supplied, look for a default
3680 value. */
3681 else if (TREE_PURPOSE (parm) == NULL_TREE)
3683 /* There was no default value. */
3684 my_friendly_assert (!require_all_arguments, 0);
3685 break;
3687 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3688 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3689 else
3690 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3691 in_decl);
3693 /* Now, convert the Ith argument, as necessary. */
3694 if (arg == NULL_TREE)
3695 /* We're out of arguments. */
3697 my_friendly_assert (!require_all_arguments, 0);
3698 break;
3700 else if (arg == error_mark_node)
3702 error ("template argument %d is invalid", i + 1);
3703 arg = error_mark_node;
3705 else
3706 arg = convert_template_argument (TREE_VALUE (parm),
3707 arg, new_args, complain, i,
3708 in_decl);
3710 if (arg == error_mark_node)
3711 lost++;
3712 TREE_VEC_ELT (new_inner_args, i) = arg;
3715 if (lost)
3716 return error_mark_node;
3718 return new_inner_args;
3721 /* Returns 1 if template args OT and NT are equivalent. */
3723 static int
3724 template_args_equal (tree ot, tree nt)
3726 if (nt == ot)
3727 return 1;
3729 if (TREE_CODE (nt) == TREE_VEC)
3730 /* For member templates */
3731 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3732 else if (TYPE_P (nt))
3733 return TYPE_P (ot) && same_type_p (ot, nt);
3734 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3735 return 0;
3736 else
3737 return cp_tree_equal (ot, nt);
3740 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3741 of template arguments. Returns 0 otherwise. */
3744 comp_template_args (tree oldargs, tree newargs)
3746 int i;
3748 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3749 return 0;
3751 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3753 tree nt = TREE_VEC_ELT (newargs, i);
3754 tree ot = TREE_VEC_ELT (oldargs, i);
3756 if (! template_args_equal (ot, nt))
3757 return 0;
3759 return 1;
3762 /* Given class template name and parameter list, produce a user-friendly name
3763 for the instantiation. */
3765 static char *
3766 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
3768 static struct obstack scratch_obstack;
3769 static char *scratch_firstobj;
3770 int i, nparms;
3772 if (!scratch_firstobj)
3773 gcc_obstack_init (&scratch_obstack);
3774 else
3775 obstack_free (&scratch_obstack, scratch_firstobj);
3776 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3778 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3779 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3781 cat (name);
3782 ccat ('<');
3783 nparms = TREE_VEC_LENGTH (parms);
3784 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3785 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3786 for (i = 0; i < nparms; i++)
3788 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3789 tree arg = TREE_VEC_ELT (arglist, i);
3791 if (i)
3792 ccat (',');
3794 if (TREE_CODE (parm) == TYPE_DECL)
3796 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3797 continue;
3799 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3801 if (TREE_CODE (arg) == TEMPLATE_DECL)
3803 /* Already substituted with real template. Just output
3804 the template name here */
3805 tree context = DECL_CONTEXT (arg);
3806 if (context)
3808 /* The template may be defined in a namespace, or
3809 may be a member template. */
3810 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3811 || CLASS_TYPE_P (context),
3812 980422);
3813 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3814 cat("::");
3816 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3818 else
3819 /* Output the parameter declaration */
3820 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3821 continue;
3823 else
3824 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3826 if (TREE_CODE (arg) == TREE_LIST)
3828 /* New list cell was built because old chain link was in
3829 use. */
3830 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3831 arg = TREE_VALUE (arg);
3833 /* No need to check arglist against parmlist here; we did that
3834 in coerce_template_parms, called from lookup_template_class. */
3835 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3838 char *bufp = obstack_next_free (&scratch_obstack);
3839 int offset = 0;
3840 while (bufp[offset - 1] == ' ')
3841 offset--;
3842 obstack_blank_fast (&scratch_obstack, offset);
3844 /* B<C<char> >, not B<C<char>> */
3845 if (bufp[offset - 1] == '>')
3846 ccat (' ');
3848 ccat ('>');
3849 ccat ('\0');
3850 return (char *) obstack_base (&scratch_obstack);
3853 static tree
3854 classtype_mangled_name (tree t)
3856 if (CLASSTYPE_TEMPLATE_INFO (t)
3857 /* Specializations have already had their names set up in
3858 lookup_template_class. */
3859 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3861 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3863 /* For non-primary templates, the template parameters are
3864 implicit from their surrounding context. */
3865 if (PRIMARY_TEMPLATE_P (tmpl))
3867 tree name = DECL_NAME (tmpl);
3868 char *mangled_name = mangle_class_name_for_template
3869 (IDENTIFIER_POINTER (name),
3870 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3871 CLASSTYPE_TI_ARGS (t));
3872 tree id = get_identifier (mangled_name);
3873 IDENTIFIER_TEMPLATE (id) = name;
3874 return id;
3878 return TYPE_IDENTIFIER (t);
3881 static void
3882 add_pending_template (tree d)
3884 tree ti = (TYPE_P (d)
3885 ? CLASSTYPE_TEMPLATE_INFO (d)
3886 : DECL_TEMPLATE_INFO (d));
3887 tree pt;
3888 int level;
3890 if (TI_PENDING_TEMPLATE_FLAG (ti))
3891 return;
3893 /* We are called both from instantiate_decl, where we've already had a
3894 tinst_level pushed, and instantiate_template, where we haven't.
3895 Compensate. */
3896 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3898 if (level)
3899 push_tinst_level (d);
3901 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3902 if (last_pending_template)
3903 TREE_CHAIN (last_pending_template) = pt;
3904 else
3905 pending_templates = pt;
3907 last_pending_template = pt;
3909 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3911 if (level)
3912 pop_tinst_level ();
3916 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
3917 ARGLIST. Valid choices for FNS are given in the cp-tree.def
3918 documentation for TEMPLATE_ID_EXPR. */
3920 tree
3921 lookup_template_function (tree fns, tree arglist)
3923 tree type;
3925 if (fns == error_mark_node || arglist == error_mark_node)
3926 return error_mark_node;
3928 if (fns == NULL_TREE)
3930 error ("non-template used as template");
3931 return error_mark_node;
3934 my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL
3935 || TREE_CODE (fns) == OVERLOAD
3936 || BASELINK_P (fns)
3937 || TREE_CODE (fns) == IDENTIFIER_NODE
3938 || TREE_CODE (fns) == LOOKUP_EXPR,
3939 20020730);
3941 if (BASELINK_P (fns))
3943 BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR,
3944 unknown_type_node,
3945 BASELINK_FUNCTIONS (fns),
3946 arglist);
3947 return fns;
3950 type = TREE_TYPE (fns);
3951 if (TREE_CODE (fns) == OVERLOAD || !type)
3952 type = unknown_type_node;
3954 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3957 /* Within the scope of a template class S<T>, the name S gets bound
3958 (in build_self_reference) to a TYPE_DECL for the class, not a
3959 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3960 or one of its enclosing classes, and that type is a template,
3961 return the associated TEMPLATE_DECL. Otherwise, the original
3962 DECL is returned. */
3964 tree
3965 maybe_get_template_decl_from_type_decl (tree decl)
3967 return (decl != NULL_TREE
3968 && TREE_CODE (decl) == TYPE_DECL
3969 && DECL_ARTIFICIAL (decl)
3970 && CLASS_TYPE_P (TREE_TYPE (decl))
3971 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3972 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3975 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3976 parameters, find the desired type.
3978 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3979 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3980 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3981 otherwise.)
3983 IN_DECL, if non-NULL, is the template declaration we are trying to
3984 instantiate.
3986 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
3987 the class we are looking up.
3989 Issue error and warning messages under control of COMPLAIN.
3991 If the template class is really a local class in a template
3992 function, then the FUNCTION_CONTEXT is the function in which it is
3993 being instantiated. */
3995 tree
3996 lookup_template_class (tree d1,
3997 tree arglist,
3998 tree in_decl,
3999 tree context,
4000 int entering_scope,
4001 tsubst_flags_t complain)
4003 tree template = NULL_TREE, parmlist;
4004 tree t;
4006 timevar_push (TV_NAME_LOOKUP);
4007 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4009 if (IDENTIFIER_VALUE (d1)
4010 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
4011 template = IDENTIFIER_VALUE (d1);
4012 else
4014 if (context)
4015 push_decl_namespace (context);
4016 template = lookup_name (d1, /*prefer_type=*/0);
4017 template = maybe_get_template_decl_from_type_decl (template);
4018 if (context)
4019 pop_decl_namespace ();
4021 if (template)
4022 context = DECL_CONTEXT (template);
4024 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4026 tree type = TREE_TYPE (d1);
4028 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4029 an implicit typename for the second A. Deal with it. */
4030 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4031 type = TREE_TYPE (type);
4033 if (CLASSTYPE_TEMPLATE_INFO (type))
4035 template = CLASSTYPE_TI_TEMPLATE (type);
4036 d1 = DECL_NAME (template);
4039 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4040 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4042 template = TYPE_TI_TEMPLATE (d1);
4043 d1 = DECL_NAME (template);
4045 else if (TREE_CODE (d1) == TEMPLATE_DECL
4046 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4048 template = d1;
4049 d1 = DECL_NAME (template);
4050 context = DECL_CONTEXT (template);
4053 /* With something like `template <class T> class X class X { ... };'
4054 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
4055 We don't want to do that, but we have to deal with the situation,
4056 so let's give them some syntax errors to chew on instead of a
4057 crash. Alternatively D1 might not be a template type at all. */
4058 if (! template)
4060 if (complain & tf_error)
4061 error ("`%T' is not a template", d1);
4062 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4065 if (TREE_CODE (template) != TEMPLATE_DECL
4066 /* If we're called from the parser, make sure it's a user visible
4067 template. */
4068 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
4069 && !DECL_TEMPLATE_PARM_P (template)
4070 && !PRIMARY_TEMPLATE_P (template)))
4072 if (complain & tf_error)
4074 error ("non-template type `%T' used as a template", d1);
4075 if (in_decl)
4076 cp_error_at ("for template declaration `%D'", in_decl);
4078 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4081 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4083 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4084 template arguments */
4086 tree parm;
4087 tree arglist2;
4089 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4091 /* Consider an example where a template template parameter declared as
4093 template <class T, class U = std::allocator<T> > class TT
4095 The template parameter level of T and U are one level larger than
4096 of TT. To proper process the default argument of U, say when an
4097 instantiation `TT<int>' is seen, we need to build the full
4098 arguments containing {int} as the innermost level. Outer levels,
4099 available when not appearing as default template argument, can be
4100 obtained from `current_template_args ()'.
4102 Suppose that TT is later substituted with std::vector. The above
4103 instantiation is `TT<int, std::allocator<T> >' with TT at
4104 level 1, and T at level 2, while the template arguments at level 1
4105 becomes {std::vector} and the inner level 2 is {int}. */
4107 if (current_template_parms)
4108 arglist = add_to_template_args (current_template_args (), arglist);
4110 arglist2 = coerce_template_parms (parmlist, arglist, template,
4111 complain, /*require_all_args=*/1);
4112 if (arglist2 == error_mark_node
4113 || (!uses_template_parms (arglist2)
4114 && check_instantiated_args (template, arglist2, complain)))
4115 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4117 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4118 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4120 else
4122 tree template_type = TREE_TYPE (template);
4123 tree gen_tmpl;
4124 tree type_decl;
4125 tree found = NULL_TREE;
4126 tree *tp;
4127 int arg_depth;
4128 int parm_depth;
4129 int is_partial_instantiation;
4131 gen_tmpl = most_general_template (template);
4132 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4133 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4134 arg_depth = TMPL_ARGS_DEPTH (arglist);
4136 if (arg_depth == 1 && parm_depth > 1)
4138 /* We've been given an incomplete set of template arguments.
4139 For example, given:
4141 template <class T> struct S1 {
4142 template <class U> struct S2 {};
4143 template <class U> struct S2<U*> {};
4146 we will be called with an ARGLIST of `U*', but the
4147 TEMPLATE will be `template <class T> template
4148 <class U> struct S1<T>::S2'. We must fill in the missing
4149 arguments. */
4150 arglist
4151 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4152 arglist);
4153 arg_depth = TMPL_ARGS_DEPTH (arglist);
4156 /* Now we should have enough arguments. */
4157 my_friendly_assert (parm_depth == arg_depth, 0);
4159 /* From here on, we're only interested in the most general
4160 template. */
4161 template = gen_tmpl;
4163 /* Calculate the BOUND_ARGS. These will be the args that are
4164 actually tsubst'd into the definition to create the
4165 instantiation. */
4166 if (parm_depth > 1)
4168 /* We have multiple levels of arguments to coerce, at once. */
4169 int i;
4170 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4172 tree bound_args = make_tree_vec (parm_depth);
4174 for (i = saved_depth,
4175 t = DECL_TEMPLATE_PARMS (template);
4176 i > 0 && t != NULL_TREE;
4177 --i, t = TREE_CHAIN (t))
4179 tree a = coerce_template_parms (TREE_VALUE (t),
4180 arglist, template,
4181 complain, /*require_all_args=*/1);
4183 /* Don't process further if one of the levels fails. */
4184 if (a == error_mark_node)
4186 /* Restore the ARGLIST to its full size. */
4187 TREE_VEC_LENGTH (arglist) = saved_depth;
4188 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4191 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4193 /* We temporarily reduce the length of the ARGLIST so
4194 that coerce_template_parms will see only the arguments
4195 corresponding to the template parameters it is
4196 examining. */
4197 TREE_VEC_LENGTH (arglist)--;
4200 /* Restore the ARGLIST to its full size. */
4201 TREE_VEC_LENGTH (arglist) = saved_depth;
4203 arglist = bound_args;
4205 else
4206 arglist
4207 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4208 INNERMOST_TEMPLATE_ARGS (arglist),
4209 template,
4210 complain, /*require_all_args=*/1);
4212 if (arglist == error_mark_node
4213 || (!uses_template_parms (INNERMOST_TEMPLATE_ARGS (arglist))
4214 && check_instantiated_args (template,
4215 INNERMOST_TEMPLATE_ARGS (arglist),
4216 complain)))
4217 /* We were unable to bind the arguments. */
4218 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4220 /* In the scope of a template class, explicit references to the
4221 template class refer to the type of the template, not any
4222 instantiation of it. For example, in:
4224 template <class T> class C { void f(C<T>); }
4226 the `C<T>' is just the same as `C'. Outside of the
4227 class, however, such a reference is an instantiation. */
4228 if (comp_template_args (TYPE_TI_ARGS (template_type),
4229 arglist))
4231 found = template_type;
4233 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4235 tree ctx;
4237 /* Note that we use DECL_CONTEXT, rather than
4238 CP_DECL_CONTEXT, so that the termination test is
4239 always just `ctx'. We're not interested in namespace
4240 scopes. */
4241 for (ctx = current_class_type;
4242 ctx;
4243 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4244 if (same_type_p (ctx, template_type))
4245 break;
4247 if (!ctx)
4248 /* We're not in the scope of the class, so the
4249 TEMPLATE_TYPE is not the type we want after
4250 all. */
4251 found = NULL_TREE;
4254 if (found)
4255 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4257 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4258 *tp;
4259 tp = &TREE_CHAIN (*tp))
4260 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4262 found = *tp;
4264 /* Use the move-to-front heuristic to speed up future
4265 searches. */
4266 *tp = TREE_CHAIN (*tp);
4267 TREE_CHAIN (found)
4268 = DECL_TEMPLATE_INSTANTIATIONS (template);
4269 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4271 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
4274 /* This type is a "partial instantiation" if any of the template
4275 arguments still involve template parameters. Note that we set
4276 IS_PARTIAL_INSTANTIATION for partial specializations as
4277 well. */
4278 is_partial_instantiation = uses_template_parms (arglist);
4280 if (!is_partial_instantiation
4281 && !PRIMARY_TEMPLATE_P (template)
4282 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4284 found = xref_tag_from_type (TREE_TYPE (template),
4285 DECL_NAME (template),
4286 /*globalize=*/1);
4287 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4290 context = tsubst (DECL_CONTEXT (template), arglist,
4291 complain, in_decl);
4292 if (!context)
4293 context = global_namespace;
4295 /* Create the type. */
4296 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4298 if (!is_partial_instantiation)
4300 set_current_access_from_decl (TYPE_NAME (template_type));
4301 t = start_enum (TYPE_IDENTIFIER (template_type));
4303 else
4304 /* We don't want to call start_enum for this type, since
4305 the values for the enumeration constants may involve
4306 template parameters. And, no one should be interested
4307 in the enumeration constants for such a type. */
4308 t = make_node (ENUMERAL_TYPE);
4310 else
4312 t = make_aggr_type (TREE_CODE (template_type));
4313 CLASSTYPE_DECLARED_CLASS (t)
4314 = CLASSTYPE_DECLARED_CLASS (template_type);
4315 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4316 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4317 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4319 /* A local class. Make sure the decl gets registered properly. */
4320 if (context == current_function_decl)
4321 pushtag (DECL_NAME (template), t, 0);
4324 /* If we called start_enum or pushtag above, this information
4325 will already be set up. */
4326 if (!TYPE_NAME (t))
4328 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4330 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4331 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4332 TYPE_STUB_DECL (t) = type_decl;
4333 DECL_SOURCE_LOCATION (type_decl)
4334 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4336 else
4337 type_decl = TYPE_NAME (t);
4339 TREE_PRIVATE (type_decl)
4340 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4341 TREE_PROTECTED (type_decl)
4342 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4344 /* Set up the template information. We have to figure out which
4345 template is the immediate parent if this is a full
4346 instantiation. */
4347 if (parm_depth == 1 || is_partial_instantiation
4348 || !PRIMARY_TEMPLATE_P (template))
4349 /* This case is easy; there are no member templates involved. */
4350 found = template;
4351 else
4353 /* This is a full instantiation of a member template. Look
4354 for a partial instantiation of which this is an instance. */
4356 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4357 found; found = TREE_CHAIN (found))
4359 int success;
4360 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4362 /* We only want partial instantiations, here, not
4363 specializations or full instantiations. */
4364 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4365 || !uses_template_parms (TREE_VALUE (found)))
4366 continue;
4368 /* Temporarily reduce by one the number of levels in the
4369 ARGLIST and in FOUND so as to avoid comparing the
4370 last set of arguments. */
4371 TREE_VEC_LENGTH (arglist)--;
4372 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4374 /* See if the arguments match. If they do, then TMPL is
4375 the partial instantiation we want. */
4376 success = comp_template_args (TREE_PURPOSE (found), arglist);
4378 /* Restore the argument vectors to their full size. */
4379 TREE_VEC_LENGTH (arglist)++;
4380 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4382 if (success)
4384 found = tmpl;
4385 break;
4389 if (!found)
4391 /* There was no partial instantiation. This happens
4392 where C<T> is a member template of A<T> and it's used
4393 in something like
4395 template <typename T> struct B { A<T>::C<int> m; };
4396 B<float>;
4398 Create the partial instantiation.
4400 TREE_VEC_LENGTH (arglist)--;
4401 found = tsubst (template, arglist, complain, NULL_TREE);
4402 TREE_VEC_LENGTH (arglist)++;
4406 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4407 DECL_TEMPLATE_INSTANTIATIONS (template)
4408 = tree_cons (arglist, t,
4409 DECL_TEMPLATE_INSTANTIATIONS (template));
4411 if (TREE_CODE (t) == ENUMERAL_TYPE
4412 && !is_partial_instantiation)
4413 /* Now that the type has been registered on the instantiations
4414 list, we set up the enumerators. Because the enumeration
4415 constants may involve the enumeration type itself, we make
4416 sure to register the type first, and then create the
4417 constants. That way, doing tsubst_expr for the enumeration
4418 constants won't result in recursive calls here; we'll find
4419 the instantiation and exit above. */
4420 tsubst_enum (template_type, t, arglist);
4422 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4423 is set up. */
4424 if (TREE_CODE (t) != ENUMERAL_TYPE)
4425 DECL_NAME (type_decl) = classtype_mangled_name (t);
4426 if (!is_partial_instantiation)
4428 /* For backwards compatibility; code that uses
4429 -fexternal-templates expects looking up a template to
4430 instantiate it. I think DDD still relies on this.
4431 (jason 8/20/1998) */
4432 if (TREE_CODE (t) != ENUMERAL_TYPE
4433 && flag_external_templates
4434 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4435 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4436 add_pending_template (t);
4438 else
4439 /* If the type makes use of template parameters, the
4440 code that generates debugging information will crash. */
4441 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4443 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4445 timevar_pop (TV_NAME_LOOKUP);
4448 struct pair_fn_data
4450 tree_fn_t fn;
4451 void *data;
4452 htab_t visited;
4455 /* Called from for_each_template_parm via walk_tree. */
4457 static tree
4458 for_each_template_parm_r (tree* tp, int* walk_subtrees, void* d)
4460 tree t = *tp;
4461 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4462 tree_fn_t fn = pfd->fn;
4463 void *data = pfd->data;
4464 void **slot;
4466 /* If we have already visited this tree, there's no need to walk
4467 subtrees. Otherwise, add it to the visited table. */
4468 slot = htab_find_slot (pfd->visited, *tp, INSERT);
4469 if (*slot)
4471 *walk_subtrees = 0;
4472 return NULL_TREE;
4474 *slot = *tp;
4476 if (TYPE_P (t)
4477 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4478 return error_mark_node;
4480 switch (TREE_CODE (t))
4482 case RECORD_TYPE:
4483 if (TYPE_PTRMEMFUNC_P (t))
4484 break;
4485 /* Fall through. */
4487 case UNION_TYPE:
4488 case ENUMERAL_TYPE:
4489 if (!TYPE_TEMPLATE_INFO (t))
4490 *walk_subtrees = 0;
4491 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4492 fn, data, pfd->visited))
4493 return error_mark_node;
4494 break;
4496 case METHOD_TYPE:
4497 /* Since we're not going to walk subtrees, we have to do this
4498 explicitly here. */
4499 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4500 pfd->visited))
4501 return error_mark_node;
4502 /* Fall through. */
4504 case FUNCTION_TYPE:
4505 /* Check the return type. */
4506 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4507 return error_mark_node;
4509 /* Check the parameter types. Since default arguments are not
4510 instantiated until they are needed, the TYPE_ARG_TYPES may
4511 contain expressions that involve template parameters. But,
4512 no-one should be looking at them yet. And, once they're
4513 instantiated, they don't contain template parameters, so
4514 there's no point in looking at them then, either. */
4516 tree parm;
4518 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4519 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4520 pfd->visited))
4521 return error_mark_node;
4523 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4524 want walk_tree walking into them itself. */
4525 *walk_subtrees = 0;
4527 break;
4529 case TYPEOF_TYPE:
4530 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4531 pfd->visited))
4532 return error_mark_node;
4533 break;
4535 case FUNCTION_DECL:
4536 case VAR_DECL:
4537 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4538 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4539 pfd->visited))
4540 return error_mark_node;
4541 /* Fall through. */
4543 case PARM_DECL:
4544 case CONST_DECL:
4545 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4546 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4547 pfd->visited))
4548 return error_mark_node;
4549 if (DECL_CONTEXT (t)
4550 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4551 pfd->visited))
4552 return error_mark_node;
4553 break;
4555 case BOUND_TEMPLATE_TEMPLATE_PARM:
4556 /* Record template parameters such as `T' inside `TT<T>'. */
4557 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4558 return error_mark_node;
4559 /* Fall through. */
4561 case TEMPLATE_TEMPLATE_PARM:
4562 case TEMPLATE_TYPE_PARM:
4563 case TEMPLATE_PARM_INDEX:
4564 if (fn && (*fn)(t, data))
4565 return error_mark_node;
4566 else if (!fn)
4567 return error_mark_node;
4568 break;
4570 case TEMPLATE_DECL:
4571 /* A template template parameter is encountered */
4572 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4573 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4574 return error_mark_node;
4576 /* Already substituted template template parameter */
4577 *walk_subtrees = 0;
4578 break;
4580 case TYPENAME_TYPE:
4581 if (!fn
4582 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4583 data, pfd->visited))
4584 return error_mark_node;
4585 break;
4587 case CONSTRUCTOR:
4588 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4589 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4590 (TREE_TYPE (t)), fn, data,
4591 pfd->visited))
4592 return error_mark_node;
4593 break;
4595 case INDIRECT_REF:
4596 case COMPONENT_REF:
4597 /* If there's no type, then this thing must be some expression
4598 involving template parameters. */
4599 if (!fn && !TREE_TYPE (t))
4600 return error_mark_node;
4601 break;
4603 case MODOP_EXPR:
4604 case CAST_EXPR:
4605 case REINTERPRET_CAST_EXPR:
4606 case CONST_CAST_EXPR:
4607 case STATIC_CAST_EXPR:
4608 case DYNAMIC_CAST_EXPR:
4609 case ARROW_EXPR:
4610 case DOTSTAR_EXPR:
4611 case TYPEID_EXPR:
4612 case LOOKUP_EXPR:
4613 case PSEUDO_DTOR_EXPR:
4614 if (!fn)
4615 return error_mark_node;
4616 break;
4618 case BASELINK:
4619 /* If we do not handle this case specially, we end up walking
4620 the BINFO hierarchy, which is circular, and therefore
4621 confuses walk_tree. */
4622 *walk_subtrees = 0;
4623 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4624 pfd->visited))
4625 return error_mark_node;
4626 break;
4628 default:
4629 break;
4632 /* We didn't find any template parameters we liked. */
4633 return NULL_TREE;
4636 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4637 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4638 call FN with the parameter and the DATA.
4639 If FN returns nonzero, the iteration is terminated, and
4640 for_each_template_parm returns 1. Otherwise, the iteration
4641 continues. If FN never returns a nonzero value, the value
4642 returned by for_each_template_parm is 0. If FN is NULL, it is
4643 considered to be the function which always returns 1. */
4645 static int
4646 for_each_template_parm (tree t, tree_fn_t fn, void* data, htab_t visited)
4648 struct pair_fn_data pfd;
4649 int result;
4651 /* Set up. */
4652 pfd.fn = fn;
4653 pfd.data = data;
4655 /* Walk the tree. (Conceptually, we would like to walk without
4656 duplicates, but for_each_template_parm_r recursively calls
4657 for_each_template_parm, so we would need to reorganize a fair
4658 bit to use walk_tree_without_duplicates, so we keep our own
4659 visited list.) */
4660 if (visited)
4661 pfd.visited = visited;
4662 else
4663 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer,
4664 NULL);
4665 result = walk_tree (&t,
4666 for_each_template_parm_r,
4667 &pfd,
4668 NULL) != NULL_TREE;
4670 /* Clean up. */
4671 if (!visited)
4672 htab_delete (pfd.visited);
4674 return result;
4678 uses_template_parms (tree t)
4680 return for_each_template_parm (t, 0, 0, NULL);
4683 static int tinst_depth;
4684 extern int max_tinst_depth;
4685 #ifdef GATHER_STATISTICS
4686 int depth_reached;
4687 #endif
4688 static int tinst_level_tick;
4689 static int last_template_error_tick;
4691 /* We're starting to instantiate D; record the template instantiation context
4692 for diagnostics and to restore it later. */
4695 push_tinst_level (tree d)
4697 tree new;
4699 if (tinst_depth >= max_tinst_depth)
4701 /* If the instantiation in question still has unbound template parms,
4702 we don't really care if we can't instantiate it, so just return.
4703 This happens with base instantiation for implicit `typename'. */
4704 if (uses_template_parms (d))
4705 return 0;
4707 last_template_error_tick = tinst_level_tick;
4708 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4709 max_tinst_depth, d);
4711 print_instantiation_context ();
4713 return 0;
4716 new = build_expr_wfl (d, input_filename, input_line, 0);
4717 TREE_CHAIN (new) = current_tinst_level;
4718 current_tinst_level = new;
4720 ++tinst_depth;
4721 #ifdef GATHER_STATISTICS
4722 if (tinst_depth > depth_reached)
4723 depth_reached = tinst_depth;
4724 #endif
4726 ++tinst_level_tick;
4727 return 1;
4730 /* We're done instantiating this template; return to the instantiation
4731 context. */
4733 void
4734 pop_tinst_level (void)
4736 tree old = current_tinst_level;
4738 /* Restore the filename and line number stashed away when we started
4739 this instantiation. */
4740 input_line = TINST_LINE (old);
4741 input_filename = TINST_FILE (old);
4742 extract_interface_info ();
4744 current_tinst_level = TREE_CHAIN (old);
4745 --tinst_depth;
4746 ++tinst_level_tick;
4749 /* We're instantiating a deferred template; restore the template
4750 instantiation context in which the instantiation was requested, which
4751 is one step out from LEVEL. */
4753 static void
4754 reopen_tinst_level (tree level)
4756 tree t;
4758 tinst_depth = 0;
4759 for (t = level; t; t = TREE_CHAIN (t))
4760 ++tinst_depth;
4762 current_tinst_level = level;
4763 pop_tinst_level ();
4766 /* Return the outermost template instantiation context, for use with
4767 -falt-external-templates. */
4769 tree
4770 tinst_for_decl (void)
4772 tree p = current_tinst_level;
4774 if (p)
4775 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4777 return p;
4780 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4781 vector of template arguments, as for tsubst.
4783 Returns an appropriate tsubst'd friend declaration. */
4785 static tree
4786 tsubst_friend_function (tree decl, tree args)
4788 tree new_friend;
4789 location_t saved_loc = input_location;
4791 input_location = DECL_SOURCE_LOCATION (decl);
4793 if (TREE_CODE (decl) == FUNCTION_DECL
4794 && DECL_TEMPLATE_INSTANTIATION (decl)
4795 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4796 /* This was a friend declared with an explicit template
4797 argument list, e.g.:
4799 friend void f<>(T);
4801 to indicate that f was a template instantiation, not a new
4802 function declaration. Now, we have to figure out what
4803 instantiation of what template. */
4805 tree template_id, arglist, fns;
4806 tree new_args;
4807 tree tmpl;
4808 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
4810 /* Friend functions are looked up in the containing namespace scope.
4811 We must enter that scope, to avoid finding member functions of the
4812 current cless with same name. */
4813 push_nested_namespace (ns);
4814 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4815 tf_error | tf_warning, NULL_TREE);
4816 pop_nested_namespace (ns);
4817 arglist = tsubst (DECL_TI_ARGS (decl), args,
4818 tf_error | tf_warning, NULL_TREE);
4819 template_id = lookup_template_function (fns, arglist);
4821 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4822 tmpl = determine_specialization (template_id, new_friend,
4823 &new_args,
4824 /*need_member_template=*/0);
4825 new_friend = instantiate_template (tmpl, new_args, tf_error);
4826 goto done;
4829 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4831 /* The NEW_FRIEND will look like an instantiation, to the
4832 compiler, but is not an instantiation from the point of view of
4833 the language. For example, we might have had:
4835 template <class T> struct S {
4836 template <class U> friend void f(T, U);
4839 Then, in S<int>, template <class U> void f(int, U) is not an
4840 instantiation of anything. */
4841 DECL_USE_TEMPLATE (new_friend) = 0;
4842 if (TREE_CODE (decl) == TEMPLATE_DECL)
4844 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4845 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4846 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4849 /* The mangled name for the NEW_FRIEND is incorrect. The function
4850 is not a template instantiation and should not be mangled like
4851 one. Therefore, we forget the mangling here; we'll recompute it
4852 later if we need it. */
4853 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4855 SET_DECL_RTL (new_friend, NULL_RTX);
4856 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4859 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4861 tree old_decl;
4862 tree new_friend_template_info;
4863 tree new_friend_result_template_info;
4864 tree ns;
4865 int new_friend_is_defn;
4867 /* We must save some information from NEW_FRIEND before calling
4868 duplicate decls since that function will free NEW_FRIEND if
4869 possible. */
4870 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4871 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4873 /* This declaration is a `primary' template. */
4874 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4876 new_friend_is_defn
4877 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4878 new_friend_result_template_info
4879 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4881 else
4883 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4884 new_friend_result_template_info = NULL_TREE;
4887 /* Inside pushdecl_namespace_level, we will push into the
4888 current namespace. However, the friend function should go
4889 into the namespace of the template. */
4890 ns = decl_namespace_context (new_friend);
4891 push_nested_namespace (ns);
4892 old_decl = pushdecl_namespace_level (new_friend);
4893 pop_nested_namespace (ns);
4895 if (old_decl != new_friend)
4897 /* This new friend declaration matched an existing
4898 declaration. For example, given:
4900 template <class T> void f(T);
4901 template <class U> class C {
4902 template <class T> friend void f(T) {}
4905 the friend declaration actually provides the definition
4906 of `f', once C has been instantiated for some type. So,
4907 old_decl will be the out-of-class template declaration,
4908 while new_friend is the in-class definition.
4910 But, if `f' was called before this point, the
4911 instantiation of `f' will have DECL_TI_ARGS corresponding
4912 to `T' but not to `U', references to which might appear
4913 in the definition of `f'. Previously, the most general
4914 template for an instantiation of `f' was the out-of-class
4915 version; now it is the in-class version. Therefore, we
4916 run through all specialization of `f', adding to their
4917 DECL_TI_ARGS appropriately. In particular, they need a
4918 new set of outer arguments, corresponding to the
4919 arguments for this class instantiation.
4921 The same situation can arise with something like this:
4923 friend void f(int);
4924 template <class T> class C {
4925 friend void f(T) {}
4928 when `C<int>' is instantiated. Now, `f(int)' is defined
4929 in the class. */
4931 if (!new_friend_is_defn)
4932 /* On the other hand, if the in-class declaration does
4933 *not* provide a definition, then we don't want to alter
4934 existing definitions. We can just leave everything
4935 alone. */
4937 else
4939 /* Overwrite whatever template info was there before, if
4940 any, with the new template information pertaining to
4941 the declaration. */
4942 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4944 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4945 /* duplicate_decls will take care of this case. */
4947 else
4949 tree t;
4950 tree new_friend_args;
4952 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4953 = new_friend_result_template_info;
4955 new_friend_args = TI_ARGS (new_friend_template_info);
4956 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4957 t != NULL_TREE;
4958 t = TREE_CHAIN (t))
4960 tree spec = TREE_VALUE (t);
4962 DECL_TI_ARGS (spec)
4963 = add_outermost_template_args (new_friend_args,
4964 DECL_TI_ARGS (spec));
4967 /* Now, since specializations are always supposed to
4968 hang off of the most general template, we must move
4969 them. */
4970 t = most_general_template (old_decl);
4971 if (t != old_decl)
4973 DECL_TEMPLATE_SPECIALIZATIONS (t)
4974 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4975 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4976 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4981 /* The information from NEW_FRIEND has been merged into OLD_DECL
4982 by duplicate_decls. */
4983 new_friend = old_decl;
4986 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
4988 /* Check to see that the declaration is really present, and,
4989 possibly obtain an improved declaration. */
4990 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4991 new_friend);
4993 if (fn)
4994 new_friend = fn;
4997 done:
4998 input_location = saved_loc;
4999 return new_friend;
5002 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5003 template arguments, as for tsubst.
5005 Returns an appropriate tsubst'd friend type or error_mark_node on
5006 failure. */
5008 static tree
5009 tsubst_friend_class (tree friend_tmpl, tree args)
5011 tree friend_type;
5012 tree tmpl;
5013 tree context;
5015 context = DECL_CONTEXT (friend_tmpl);
5017 if (context)
5019 if (TREE_CODE (context) == NAMESPACE_DECL)
5020 push_nested_namespace (context);
5021 else
5022 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5025 /* First, we look for a class template. */
5026 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5028 /* But, if we don't find one, it might be because we're in a
5029 situation like this:
5031 template <class T>
5032 struct S {
5033 template <class U>
5034 friend struct S;
5037 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5038 for `S<int>', not the TEMPLATE_DECL. */
5039 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5041 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5042 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5045 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5047 /* The friend template has already been declared. Just
5048 check to see that the declarations match, and install any new
5049 default parameters. We must tsubst the default parameters,
5050 of course. We only need the innermost template parameters
5051 because that is all that redeclare_class_template will look
5052 at. */
5053 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5054 > TMPL_ARGS_DEPTH (args))
5056 tree parms;
5057 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5058 args, tf_error | tf_warning);
5059 redeclare_class_template (TREE_TYPE (tmpl), parms);
5062 friend_type = TREE_TYPE (tmpl);
5064 else
5066 /* The friend template has not already been declared. In this
5067 case, the instantiation of the template class will cause the
5068 injection of this template into the global scope. */
5069 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5071 /* The new TMPL is not an instantiation of anything, so we
5072 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5073 the new type because that is supposed to be the corresponding
5074 template decl, i.e., TMPL. */
5075 DECL_USE_TEMPLATE (tmpl) = 0;
5076 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5077 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5079 /* Inject this template into the global scope. */
5080 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5083 if (context)
5085 if (TREE_CODE (context) == NAMESPACE_DECL)
5086 pop_nested_namespace (context);
5087 else
5088 pop_nested_class ();
5091 return friend_type;
5094 /* Returns zero if TYPE cannot be completed later due to circularity.
5095 Otherwise returns one. */
5097 static int
5098 can_complete_type_without_circularity (tree type)
5100 if (type == NULL_TREE || type == error_mark_node)
5101 return 0;
5102 else if (COMPLETE_TYPE_P (type))
5103 return 1;
5104 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5105 return can_complete_type_without_circularity (TREE_TYPE (type));
5106 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5107 return 0;
5108 else
5109 return 1;
5112 tree
5113 instantiate_class_template (tree type)
5115 tree template, args, pattern, t, member;
5116 tree typedecl;
5117 tree pbinfo;
5119 if (type == error_mark_node)
5120 return error_mark_node;
5122 if (TYPE_BEING_DEFINED (type)
5123 || COMPLETE_TYPE_P (type)
5124 || dependent_type_p (type))
5125 return type;
5127 /* Figure out which template is being instantiated. */
5128 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5129 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
5131 /* Figure out which arguments are being used to do the
5132 instantiation. */
5133 args = CLASSTYPE_TI_ARGS (type);
5135 /* Determine what specialization of the original template to
5136 instantiate. */
5137 t = most_specialized_class (template, args);
5138 if (t == error_mark_node)
5140 const char *str = "candidates are:";
5141 error ("ambiguous class template instantiation for `%#T'", type);
5142 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5143 t = TREE_CHAIN (t))
5145 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5146 args))
5148 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5149 str = " ";
5152 TYPE_BEING_DEFINED (type) = 1;
5153 return error_mark_node;
5156 if (t)
5157 pattern = TREE_TYPE (t);
5158 else
5159 pattern = TREE_TYPE (template);
5161 /* If the template we're instantiating is incomplete, then clearly
5162 there's nothing we can do. */
5163 if (!COMPLETE_TYPE_P (pattern))
5164 return type;
5166 /* If we've recursively instantiated too many templates, stop. */
5167 if (! push_tinst_level (type))
5168 return type;
5170 /* Now we're really doing the instantiation. Mark the type as in
5171 the process of being defined. */
5172 TYPE_BEING_DEFINED (type) = 1;
5174 /* We may be in the middle of deferred access check. Disable
5175 it now. */
5176 push_deferring_access_checks (dk_no_deferred);
5178 maybe_push_to_top_level (uses_template_parms (type));
5180 if (t)
5182 /* This TYPE is actually an instantiation of a partial
5183 specialization. We replace the innermost set of ARGS with
5184 the arguments appropriate for substitution. For example,
5185 given:
5187 template <class T> struct S {};
5188 template <class T> struct S<T*> {};
5190 and supposing that we are instantiating S<int*>, ARGS will
5191 present be {int*} but we need {int}. */
5192 tree inner_args
5193 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5194 args);
5196 /* If there were multiple levels in ARGS, replacing the
5197 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5198 want, so we make a copy first. */
5199 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5201 args = copy_node (args);
5202 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5204 else
5205 args = inner_args;
5208 if (flag_external_templates)
5210 if (flag_alt_external_templates)
5212 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5213 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5215 else
5217 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5218 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5219 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5222 else
5224 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5227 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5228 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5229 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5230 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5231 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5232 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5233 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5234 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5235 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5236 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5237 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5238 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5239 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5240 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5241 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5242 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5243 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5244 TYPE_USES_MULTIPLE_INHERITANCE (type)
5245 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5246 TYPE_USES_VIRTUAL_BASECLASSES (type)
5247 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5248 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5249 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5250 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5251 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5252 if (ANON_AGGR_TYPE_P (pattern))
5253 SET_ANON_AGGR_TYPE_P (type);
5255 pbinfo = TYPE_BINFO (pattern);
5257 if (BINFO_BASETYPES (pbinfo))
5259 tree base_list = NULL_TREE;
5260 tree pbases = BINFO_BASETYPES (pbinfo);
5261 tree paccesses = BINFO_BASEACCESSES (pbinfo);
5262 int i;
5264 /* Substitute into each of the bases to determine the actual
5265 basetypes. */
5266 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5268 tree base;
5269 tree access;
5270 tree pbase;
5272 pbase = TREE_VEC_ELT (pbases, i);
5273 access = TREE_VEC_ELT (paccesses, i);
5275 /* Substitute to figure out the base class. */
5276 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5277 if (base == error_mark_node)
5278 continue;
5280 base_list = tree_cons (access, base, base_list);
5281 TREE_VIA_VIRTUAL (base_list) = TREE_VIA_VIRTUAL (pbase);
5284 /* The list is now in reverse order; correct that. */
5285 base_list = nreverse (base_list);
5287 /* Now call xref_basetypes to set up all the base-class
5288 information. */
5289 xref_basetypes (type, base_list);
5292 /* Now that our base classes are set up, enter the scope of the
5293 class, so that name lookups into base classes, etc. will work
5294 correctly. This is precisely analogous to what we do in
5295 begin_class_definition when defining an ordinary non-template
5296 class. */
5297 pushclass (type, true);
5299 /* Now members are processed in the order of declaration. */
5300 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
5302 tree t = TREE_VALUE (member);
5304 if (TREE_PURPOSE (member))
5306 if (TYPE_P (t))
5308 /* Build new CLASSTYPE_NESTED_UTDS. */
5310 tree tag = t;
5311 tree name = TYPE_IDENTIFIER (tag);
5312 tree newtag;
5314 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5315 my_friendly_assert (newtag != error_mark_node, 20010206);
5316 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5318 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5319 /* Unfortunately, lookup_template_class sets
5320 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5321 instantiation (i.e., for the type of a member template
5322 class nested within a template class.) This behavior is
5323 required for maybe_process_partial_specialization to work
5324 correctly, but is not accurate in this case; the TAG is not
5325 an instantiation of anything. (The corresponding
5326 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5327 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5329 /* Now, we call pushtag to put this NEWTAG into the scope of
5330 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5331 pushtag calling push_template_decl. We don't have to do
5332 this for enums because it will already have been done in
5333 tsubst_enum. */
5334 if (name)
5335 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5336 pushtag (name, newtag, /*globalize=*/0);
5339 else if (TREE_CODE (t) == FUNCTION_DECL
5340 || DECL_FUNCTION_TEMPLATE_P (t))
5342 /* Build new TYPE_METHODS. */
5344 tree r = tsubst (t, args, tf_error, NULL_TREE);
5345 set_current_access_from_decl (r);
5346 grok_special_member_properties (r);
5347 finish_member_declaration (r);
5349 else
5351 /* Build new TYPE_FIELDS. */
5353 if (TREE_CODE (t) != CONST_DECL)
5355 tree r;
5357 /* The the file and line for this declaration, to
5358 assist in error message reporting. Since we
5359 called push_tinst_level above, we don't need to
5360 restore these. */
5361 input_location = DECL_SOURCE_LOCATION (t);
5363 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5364 if (TREE_CODE (r) == VAR_DECL)
5366 tree init;
5368 if (DECL_INITIALIZED_IN_CLASS_P (r))
5369 init = tsubst_expr (DECL_INITIAL (t), args,
5370 tf_error | tf_warning, NULL_TREE);
5371 else
5372 init = NULL_TREE;
5374 finish_static_data_member_decl (r, init,
5375 /*asmspec_tree=*/NULL_TREE,
5376 /*flags=*/0);
5378 if (DECL_INITIALIZED_IN_CLASS_P (r))
5379 check_static_variable_definition (r, TREE_TYPE (r));
5381 else if (TREE_CODE (r) == FIELD_DECL)
5383 /* Determine whether R has a valid type and can be
5384 completed later. If R is invalid, then it is
5385 replaced by error_mark_node so that it will not be
5386 added to TYPE_FIELDS. */
5387 tree rtype = TREE_TYPE (r);
5388 if (can_complete_type_without_circularity (rtype))
5389 complete_type (rtype);
5391 if (!COMPLETE_TYPE_P (rtype))
5393 cxx_incomplete_type_error (r, rtype);
5394 r = error_mark_node;
5398 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5399 such a thing will already have been added to the field
5400 list by tsubst_enum in finish_member_declaration in the
5401 CLASSTYPE_NESTED_UTDS case above. */
5402 if (!(TREE_CODE (r) == TYPE_DECL
5403 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5404 && DECL_ARTIFICIAL (r)))
5406 set_current_access_from_decl (r);
5407 finish_member_declaration (r);
5412 else
5414 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5416 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5418 tree friend_type = t;
5419 tree new_friend_type;
5421 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5422 new_friend_type = tsubst_friend_class (friend_type, args);
5423 else if (uses_template_parms (friend_type))
5424 new_friend_type = tsubst (friend_type, args,
5425 tf_error | tf_warning, NULL_TREE);
5426 else
5428 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5430 /* The call to xref_tag_from_type does injection for friend
5431 classes. */
5432 push_nested_namespace (ns);
5433 new_friend_type =
5434 xref_tag_from_type (friend_type, NULL_TREE, 1);
5435 pop_nested_namespace (ns);
5438 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5439 /* Trick make_friend_class into realizing that the friend
5440 we're adding is a template, not an ordinary class. It's
5441 important that we use make_friend_class since it will
5442 perform some error-checking and output cross-reference
5443 information. */
5444 ++processing_template_decl;
5446 if (new_friend_type != error_mark_node)
5447 make_friend_class (type, new_friend_type);
5449 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5450 --processing_template_decl;
5452 else
5453 /* Build new DECL_FRIENDLIST. */
5454 add_friend (type, tsubst_friend_function (t, args));
5458 /* Set the file and line number information to whatever is given for
5459 the class itself. This puts error messages involving generated
5460 implicit functions at a predictable point, and the same point
5461 that would be used for non-template classes. */
5462 typedecl = TYPE_MAIN_DECL (type);
5463 input_location = DECL_SOURCE_LOCATION (typedecl);
5465 unreverse_member_declarations (type);
5466 finish_struct_1 (type);
5467 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5469 /* Clear this now so repo_template_used is happy. */
5470 TYPE_BEING_DEFINED (type) = 0;
5471 repo_template_used (type);
5473 /* Now that the class is complete, instantiate default arguments for
5474 any member functions. We don't do this earlier because the
5475 default arguments may reference members of the class. */
5476 if (!PRIMARY_TEMPLATE_P (template))
5477 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5478 if (TREE_CODE (t) == FUNCTION_DECL
5479 /* Implicitly generated member functions will not have template
5480 information; they are not instantiations, but instead are
5481 created "fresh" for each instantiation. */
5482 && DECL_TEMPLATE_INFO (t))
5483 tsubst_default_arguments (t);
5485 popclass ();
5486 pop_from_top_level ();
5487 pop_deferring_access_checks ();
5488 pop_tinst_level ();
5490 if (TYPE_CONTAINS_VPTR_P (type))
5491 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5493 return type;
5496 static int
5497 list_eq (tree t1, tree t2)
5499 if (t1 == NULL_TREE)
5500 return t2 == NULL_TREE;
5501 if (t2 == NULL_TREE)
5502 return 0;
5503 /* Don't care if one declares its arg const and the other doesn't -- the
5504 main variant of the arg type is all that matters. */
5505 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5506 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5507 return 0;
5508 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5511 /* If arg is a non-type template parameter that does not depend on template
5512 arguments, fold it like we weren't in the body of a template. */
5514 static tree
5515 maybe_fold_nontype_arg (tree arg)
5517 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5519 /* Sometimes, one of the args was an expression involving a
5520 template constant parameter, like N - 1. Now that we've
5521 tsubst'd, we might have something like 2 - 1. This will
5522 confuse lookup_template_class, so we do constant folding
5523 here. We have to unset processing_template_decl, to
5524 fool build_expr_from_tree() into building an actual
5525 tree. */
5527 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5528 as simple as it's going to get, and trying to reprocess
5529 the trees will break. */
5530 if (!TREE_TYPE (arg))
5532 int saved_processing_template_decl = processing_template_decl;
5533 processing_template_decl = 0;
5534 arg = build_expr_from_tree (arg);
5535 processing_template_decl = saved_processing_template_decl;
5538 arg = fold (arg);
5540 return arg;
5543 /* Apply maybe_fold_nontype_arg on a list or vector of args. */
5545 static void
5546 maybe_fold_nontype_args (tree targs)
5548 if (!targs)
5549 /*OK*/;
5550 else if (TREE_CODE (targs) == TREE_LIST)
5552 tree chain;
5553 for (chain = targs; chain; chain = TREE_CHAIN (chain))
5554 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
5556 else
5558 int i;
5559 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
5560 TREE_VEC_ELT (targs, i)
5561 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
5565 /* Substitute ARGS into the vector of template arguments T. */
5567 static tree
5568 tsubst_template_arg_vector (tree t, tree args, tsubst_flags_t complain)
5570 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5571 tree *elts = (tree *) alloca (len * sizeof (tree));
5573 memset ((char *) elts, 0, len * sizeof (tree));
5575 for (i = 0; i < len; i++)
5577 if (TREE_VEC_ELT (t, i) != NULL_TREE
5578 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5579 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5580 args, complain);
5581 else
5582 elts[i] = maybe_fold_nontype_arg
5583 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5584 NULL_TREE));
5586 if (elts[i] == error_mark_node)
5587 return error_mark_node;
5589 if (elts[i] != TREE_VEC_ELT (t, i))
5590 need_new = 1;
5593 if (!need_new)
5594 return t;
5596 t = make_tree_vec (len);
5597 for (i = 0; i < len; i++)
5598 TREE_VEC_ELT (t, i) = elts[i];
5600 return t;
5603 /* Return the result of substituting ARGS into the template parameters
5604 given by PARMS. If there are m levels of ARGS and m + n levels of
5605 PARMS, then the result will contain n levels of PARMS. For
5606 example, if PARMS is `template <class T> template <class U>
5607 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5608 result will be `template <int*, double, class V>'. */
5610 static tree
5611 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
5613 tree r = NULL_TREE;
5614 tree* new_parms;
5616 for (new_parms = &r;
5617 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5618 new_parms = &(TREE_CHAIN (*new_parms)),
5619 parms = TREE_CHAIN (parms))
5621 tree new_vec =
5622 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5623 int i;
5625 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5627 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5628 tree default_value = TREE_PURPOSE (tuple);
5629 tree parm_decl = TREE_VALUE (tuple);
5631 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5632 default_value = tsubst_expr (default_value, args,
5633 complain, NULL_TREE);
5634 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5635 parm_decl);
5636 TREE_VEC_ELT (new_vec, i) = tuple;
5639 *new_parms =
5640 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5641 - TMPL_ARGS_DEPTH (args)),
5642 new_vec, NULL_TREE);
5645 return r;
5648 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5649 type T. If T is not an aggregate or enumeration type, it is
5650 handled as if by tsubst. IN_DECL is as for tsubst. If
5651 ENTERING_SCOPE is nonzero, T is the context for a template which
5652 we are presently tsubst'ing. Return the substituted value. */
5654 static tree
5655 tsubst_aggr_type (tree t,
5656 tree args,
5657 tsubst_flags_t complain,
5658 tree in_decl,
5659 int entering_scope)
5661 if (t == NULL_TREE)
5662 return NULL_TREE;
5664 switch (TREE_CODE (t))
5666 case RECORD_TYPE:
5667 if (TYPE_PTRMEMFUNC_P (t))
5668 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5670 /* else fall through */
5671 case ENUMERAL_TYPE:
5672 case UNION_TYPE:
5673 if (TYPE_TEMPLATE_INFO (t))
5675 tree argvec;
5676 tree context;
5677 tree r;
5679 /* First, determine the context for the type we are looking
5680 up. */
5681 if (TYPE_CONTEXT (t) != NULL_TREE)
5682 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5683 complain,
5684 in_decl, /*entering_scope=*/1);
5685 else
5686 context = NULL_TREE;
5688 /* Then, figure out what arguments are appropriate for the
5689 type we are trying to find. For example, given:
5691 template <class T> struct S;
5692 template <class T, class U> void f(T, U) { S<U> su; }
5694 and supposing that we are instantiating f<int, double>,
5695 then our ARGS will be {int, double}, but, when looking up
5696 S we only want {double}. */
5697 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5698 complain);
5699 if (argvec == error_mark_node)
5700 return error_mark_node;
5702 r = lookup_template_class (t, argvec, in_decl, context,
5703 entering_scope, complain);
5705 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5707 else
5708 /* This is not a template type, so there's nothing to do. */
5709 return t;
5711 default:
5712 return tsubst (t, args, complain, in_decl);
5716 /* Substitute into the default argument ARG (a default argument for
5717 FN), which has the indicated TYPE. */
5719 tree
5720 tsubst_default_argument (tree fn, tree type, tree arg)
5722 /* This default argument came from a template. Instantiate the
5723 default argument here, not in tsubst. In the case of
5724 something like:
5726 template <class T>
5727 struct S {
5728 static T t();
5729 void f(T = t());
5732 we must be careful to do name lookup in the scope of S<T>,
5733 rather than in the current class.
5735 ??? current_class_type affects a lot more than name lookup. This is
5736 very fragile. Fortunately, it will go away when we do 2-phase name
5737 binding properly. */
5739 /* FN is already the desired FUNCTION_DECL. */
5740 push_access_scope (fn);
5742 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5743 tf_error | tf_warning, NULL_TREE);
5745 pop_access_scope (fn);
5747 /* Make sure the default argument is reasonable. */
5748 arg = check_default_argument (type, arg);
5750 return arg;
5753 /* Substitute into all the default arguments for FN. */
5755 static void
5756 tsubst_default_arguments (tree fn)
5758 tree arg;
5759 tree tmpl_args;
5761 tmpl_args = DECL_TI_ARGS (fn);
5763 /* If this function is not yet instantiated, we certainly don't need
5764 its default arguments. */
5765 if (uses_template_parms (tmpl_args))
5766 return;
5768 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5769 arg;
5770 arg = TREE_CHAIN (arg))
5771 if (TREE_PURPOSE (arg))
5772 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5773 TREE_VALUE (arg),
5774 TREE_PURPOSE (arg));
5777 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5778 (already computed) substitution of ARGS into TREE_TYPE (T), if
5779 appropriate. Return the result of the substitution. Issue error
5780 and warning messages under control of COMPLAIN. */
5782 static tree
5783 tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
5785 location_t saved_loc;
5786 tree r = NULL_TREE;
5787 tree in_decl = t;
5789 /* Set the filename and linenumber to improve error-reporting. */
5790 saved_loc = input_location;
5791 input_location = DECL_SOURCE_LOCATION (t);
5793 switch (TREE_CODE (t))
5795 case TEMPLATE_DECL:
5797 /* We can get here when processing a member template function
5798 of a template class. */
5799 tree decl = DECL_TEMPLATE_RESULT (t);
5800 tree spec;
5801 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5803 if (!is_template_template_parm)
5805 /* We might already have an instance of this template.
5806 The ARGS are for the surrounding class type, so the
5807 full args contain the tsubst'd args for the context,
5808 plus the innermost args from the template decl. */
5809 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5810 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5811 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5812 tree full_args;
5814 full_args = tsubst_template_arg_vector (tmpl_args, args,
5815 complain);
5817 /* tsubst_template_arg_vector doesn't copy the vector if
5818 nothing changed. But, *something* should have
5819 changed. */
5820 my_friendly_assert (full_args != tmpl_args, 0);
5822 spec = retrieve_specialization (t, full_args);
5823 if (spec != NULL_TREE)
5825 r = spec;
5826 break;
5830 /* Make a new template decl. It will be similar to the
5831 original, but will record the current template arguments.
5832 We also create a new function declaration, which is just
5833 like the old one, but points to this new template, rather
5834 than the old one. */
5835 r = copy_decl (t);
5836 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5837 TREE_CHAIN (r) = NULL_TREE;
5839 if (is_template_template_parm)
5841 tree new_decl = tsubst (decl, args, complain, in_decl);
5842 DECL_TEMPLATE_RESULT (r) = new_decl;
5843 TREE_TYPE (r) = TREE_TYPE (new_decl);
5844 break;
5847 DECL_CONTEXT (r)
5848 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5849 complain, in_decl,
5850 /*entering_scope=*/1);
5851 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5853 if (TREE_CODE (decl) == TYPE_DECL)
5855 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5856 TREE_TYPE (r) = new_type;
5857 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5858 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5859 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5861 else
5863 tree new_decl = tsubst (decl, args, complain, in_decl);
5865 DECL_TEMPLATE_RESULT (r) = new_decl;
5866 DECL_TI_TEMPLATE (new_decl) = r;
5867 TREE_TYPE (r) = TREE_TYPE (new_decl);
5868 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5871 SET_DECL_IMPLICIT_INSTANTIATION (r);
5872 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5873 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5875 /* The template parameters for this new template are all the
5876 template parameters for the old template, except the
5877 outermost level of parameters. */
5878 DECL_TEMPLATE_PARMS (r)
5879 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5880 complain);
5882 if (PRIMARY_TEMPLATE_P (t))
5883 DECL_PRIMARY_TEMPLATE (r) = r;
5885 /* We don't partially instantiate partial specializations. */
5886 if (TREE_CODE (decl) == TYPE_DECL)
5887 break;
5889 /* Record this partial instantiation. */
5890 register_specialization (r, t,
5891 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5894 break;
5896 case FUNCTION_DECL:
5898 tree ctx;
5899 tree argvec = NULL_TREE;
5900 tree *friends;
5901 tree gen_tmpl;
5902 int member;
5903 int args_depth;
5904 int parms_depth;
5906 /* Nobody should be tsubst'ing into non-template functions. */
5907 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5909 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5911 tree spec;
5912 bool dependent_p;
5914 /* If T is not dependent, just return it. We have to
5915 increment PROCESSING_TEMPLATE_DECL because
5916 value_dependent_expression_p assumes that nothing is
5917 dependent when PROCESSING_TEMPLATE_DECL is zero. */
5918 ++processing_template_decl;
5919 dependent_p = value_dependent_expression_p (t);
5920 --processing_template_decl;
5921 if (!dependent_p)
5922 return t;
5924 /* Calculate the most general template of which R is a
5925 specialization, and the complete set of arguments used to
5926 specialize R. */
5927 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5928 argvec
5929 = tsubst_template_arg_vector (DECL_TI_ARGS
5930 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5931 args, complain);
5933 /* Check to see if we already have this specialization. */
5934 spec = retrieve_specialization (gen_tmpl, argvec);
5936 if (spec)
5938 r = spec;
5939 break;
5942 /* We can see more levels of arguments than parameters if
5943 there was a specialization of a member template, like
5944 this:
5946 template <class T> struct S { template <class U> void f(); }
5947 template <> template <class U> void S<int>::f(U);
5949 Here, we'll be substituting into the specialization,
5950 because that's where we can find the code we actually
5951 want to generate, but we'll have enough arguments for
5952 the most general template.
5954 We also deal with the peculiar case:
5956 template <class T> struct S {
5957 template <class U> friend void f();
5959 template <class U> void f() {}
5960 template S<int>;
5961 template void f<double>();
5963 Here, the ARGS for the instantiation of will be {int,
5964 double}. But, we only need as many ARGS as there are
5965 levels of template parameters in CODE_PATTERN. We are
5966 careful not to get fooled into reducing the ARGS in
5967 situations like:
5969 template <class T> struct S { template <class U> void f(U); }
5970 template <class T> template <> void S<T>::f(int) {}
5972 which we can spot because the pattern will be a
5973 specialization in this case. */
5974 args_depth = TMPL_ARGS_DEPTH (args);
5975 parms_depth =
5976 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5977 if (args_depth > parms_depth
5978 && !DECL_TEMPLATE_SPECIALIZATION (t))
5979 args = get_innermost_template_args (args, parms_depth);
5981 else
5983 /* This special case arises when we have something like this:
5985 template <class T> struct S {
5986 friend void f<int>(int, double);
5989 Here, the DECL_TI_TEMPLATE for the friend declaration
5990 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5991 being called from tsubst_friend_function, and we want
5992 only to create a new decl (R) with appropriate types so
5993 that we can call determine_specialization. */
5994 gen_tmpl = NULL_TREE;
5997 if (DECL_CLASS_SCOPE_P (t))
5999 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6000 member = 2;
6001 else
6002 member = 1;
6003 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6004 complain, t,
6005 /*entering_scope=*/1);
6007 else
6009 member = 0;
6010 ctx = DECL_CONTEXT (t);
6012 type = tsubst (type, args, complain, in_decl);
6013 if (type == error_mark_node)
6014 return error_mark_node;
6016 /* We do NOT check for matching decls pushed separately at this
6017 point, as they may not represent instantiations of this
6018 template, and in any case are considered separate under the
6019 discrete model. */
6020 r = copy_decl (t);
6021 DECL_USE_TEMPLATE (r) = 0;
6022 TREE_TYPE (r) = type;
6023 /* Clear out the mangled name and RTL for the instantiation. */
6024 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6025 SET_DECL_RTL (r, NULL_RTX);
6027 DECL_CONTEXT (r) = ctx;
6029 if (member && DECL_CONV_FN_P (r))
6030 /* Type-conversion operator. Reconstruct the name, in
6031 case it's the name of one of the template's parameters. */
6032 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6034 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6035 complain, t);
6036 DECL_RESULT (r) = NULL_TREE;
6038 TREE_STATIC (r) = 0;
6039 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6040 DECL_EXTERNAL (r) = 1;
6041 DECL_INTERFACE_KNOWN (r) = 0;
6042 DECL_DEFER_OUTPUT (r) = 0;
6043 TREE_CHAIN (r) = NULL_TREE;
6044 DECL_PENDING_INLINE_INFO (r) = 0;
6045 DECL_PENDING_INLINE_P (r) = 0;
6046 DECL_SAVED_TREE (r) = NULL_TREE;
6047 TREE_USED (r) = 0;
6048 if (DECL_CLONED_FUNCTION (r))
6050 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6051 args, complain, t);
6052 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6053 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6056 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6057 this in the special friend case mentioned above where
6058 GEN_TMPL is NULL. */
6059 if (gen_tmpl)
6061 DECL_TEMPLATE_INFO (r)
6062 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6063 SET_DECL_IMPLICIT_INSTANTIATION (r);
6064 register_specialization (r, gen_tmpl, argvec);
6066 /* We're not supposed to instantiate default arguments
6067 until they are called, for a template. But, for a
6068 declaration like:
6070 template <class T> void f ()
6071 { extern void g(int i = T()); }
6073 we should do the substitution when the template is
6074 instantiated. We handle the member function case in
6075 instantiate_class_template since the default arguments
6076 might refer to other members of the class. */
6077 if (!member
6078 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6079 && !uses_template_parms (argvec))
6080 tsubst_default_arguments (r);
6083 /* Copy the list of befriending classes. */
6084 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6085 *friends;
6086 friends = &TREE_CHAIN (*friends))
6088 *friends = copy_node (*friends);
6089 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6090 args, complain,
6091 in_decl);
6094 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6096 maybe_retrofit_in_chrg (r);
6097 if (DECL_CONSTRUCTOR_P (r))
6098 grok_ctor_properties (ctx, r);
6099 /* If this is an instantiation of a member template, clone it.
6100 If it isn't, that'll be handled by
6101 clone_constructors_and_destructors. */
6102 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6103 clone_function_decl (r, /*update_method_vec_p=*/0);
6105 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6106 grok_op_properties (r, DECL_FRIEND_P (r));
6108 break;
6110 case PARM_DECL:
6112 r = copy_node (t);
6113 if (DECL_TEMPLATE_PARM_P (t))
6114 SET_DECL_TEMPLATE_PARM_P (r);
6116 TREE_TYPE (r) = type;
6117 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6119 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6120 DECL_INITIAL (r) = TREE_TYPE (r);
6121 else
6122 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6123 complain, in_decl);
6125 DECL_CONTEXT (r) = NULL_TREE;
6127 if (!DECL_TEMPLATE_PARM_P (r))
6128 DECL_ARG_TYPE (r) = type_passed_as (type);
6129 if (TREE_CHAIN (t))
6130 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6131 complain, TREE_CHAIN (t));
6133 break;
6135 case FIELD_DECL:
6137 r = copy_decl (t);
6138 TREE_TYPE (r) = type;
6139 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6141 /* We don't have to set DECL_CONTEXT here; it is set by
6142 finish_member_declaration. */
6143 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6144 complain, in_decl);
6145 TREE_CHAIN (r) = NULL_TREE;
6146 if (VOID_TYPE_P (type))
6147 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6149 break;
6151 case USING_DECL:
6153 r = copy_node (t);
6154 DECL_INITIAL (r)
6155 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6156 TREE_CHAIN (r) = NULL_TREE;
6158 break;
6160 case TYPE_DECL:
6161 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6162 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6164 /* If this is the canonical decl, we don't have to mess with
6165 instantiations, and often we can't (for typename, template
6166 type parms and such). Note that TYPE_NAME is not correct for
6167 the above test if we've copied the type for a typedef. */
6168 r = TYPE_NAME (type);
6169 break;
6172 /* Fall through. */
6174 case VAR_DECL:
6176 tree argvec = NULL_TREE;
6177 tree gen_tmpl = NULL_TREE;
6178 tree spec;
6179 tree tmpl = NULL_TREE;
6180 tree ctx;
6181 int local_p;
6183 /* Assume this is a non-local variable. */
6184 local_p = 0;
6186 if (TYPE_P (CP_DECL_CONTEXT (t)))
6187 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6188 complain,
6189 in_decl, /*entering_scope=*/1);
6190 else if (DECL_NAMESPACE_SCOPE_P (t))
6191 ctx = DECL_CONTEXT (t);
6192 else
6194 /* Subsequent calls to pushdecl will fill this in. */
6195 ctx = NULL_TREE;
6196 local_p = 1;
6199 /* Check to see if we already have this specialization. */
6200 if (!local_p)
6202 tmpl = DECL_TI_TEMPLATE (t);
6203 gen_tmpl = most_general_template (tmpl);
6204 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6205 spec = retrieve_specialization (gen_tmpl, argvec);
6207 else
6208 spec = retrieve_local_specialization (t);
6210 if (spec)
6212 r = spec;
6213 break;
6216 r = copy_decl (t);
6217 if (TREE_CODE (r) == VAR_DECL)
6218 type = complete_type (type);
6219 TREE_TYPE (r) = type;
6220 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6221 DECL_CONTEXT (r) = ctx;
6222 /* Clear out the mangled name and RTL for the instantiation. */
6223 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6224 SET_DECL_RTL (r, NULL_RTX);
6226 /* Don't try to expand the initializer until someone tries to use
6227 this variable; otherwise we run into circular dependencies. */
6228 DECL_INITIAL (r) = NULL_TREE;
6229 SET_DECL_RTL (r, NULL_RTX);
6230 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6232 /* Even if the original location is out of scope, the newly
6233 substituted one is not. */
6234 if (TREE_CODE (r) == VAR_DECL)
6236 DECL_DEAD_FOR_LOCAL (r) = 0;
6237 DECL_INITIALIZED_P (r) = 0;
6240 if (!local_p)
6242 /* A static data member declaration is always marked
6243 external when it is declared in-class, even if an
6244 initializer is present. We mimic the non-template
6245 processing here. */
6246 DECL_EXTERNAL (r) = 1;
6248 register_specialization (r, gen_tmpl, argvec);
6249 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6250 SET_DECL_IMPLICIT_INSTANTIATION (r);
6252 else
6253 register_local_specialization (r, t);
6255 TREE_CHAIN (r) = NULL_TREE;
6256 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6257 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6258 /* Compute the size, alignment, etc. of R. */
6259 layout_decl (r, 0);
6261 break;
6263 default:
6264 abort ();
6267 /* Restore the file and line information. */
6268 input_location = saved_loc;
6270 return r;
6273 /* Substitue into the ARG_TYPES of a function type. */
6275 static tree
6276 tsubst_arg_types (tree arg_types,
6277 tree args,
6278 tsubst_flags_t complain,
6279 tree in_decl)
6281 tree remaining_arg_types;
6282 tree type;
6284 if (!arg_types || arg_types == void_list_node)
6285 return arg_types;
6287 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6288 args, complain, in_decl);
6289 if (remaining_arg_types == error_mark_node)
6290 return error_mark_node;
6292 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6293 if (type == error_mark_node)
6294 return error_mark_node;
6295 if (VOID_TYPE_P (type))
6297 if (complain & tf_error)
6299 error ("invalid parameter type `%T'", type);
6300 if (in_decl)
6301 cp_error_at ("in declaration `%D'", in_decl);
6303 return error_mark_node;
6306 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6307 top-level qualifiers as required. */
6308 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6310 /* Note that we do not substitute into default arguments here. The
6311 standard mandates that they be instantiated only when needed,
6312 which is done in build_over_call. */
6313 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6314 remaining_arg_types);
6318 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6319 *not* handle the exception-specification for FNTYPE, because the
6320 initial substitution of explicitly provided template parameters
6321 during argument deduction forbids substitution into the
6322 exception-specification:
6324 [temp.deduct]
6326 All references in the function type of the function template to the
6327 corresponding template parameters are replaced by the specified tem-
6328 plate argument values. If a substitution in a template parameter or
6329 in the function type of the function template results in an invalid
6330 type, type deduction fails. [Note: The equivalent substitution in
6331 exception specifications is done only when the function is instanti-
6332 ated, at which point a program is ill-formed if the substitution
6333 results in an invalid type.] */
6335 static tree
6336 tsubst_function_type (tree t,
6337 tree args,
6338 tsubst_flags_t complain,
6339 tree in_decl)
6341 tree return_type;
6342 tree arg_types;
6343 tree fntype;
6345 /* The TYPE_CONTEXT is not used for function/method types. */
6346 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6348 /* Substitute the return type. */
6349 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6350 if (return_type == error_mark_node)
6351 return error_mark_node;
6353 /* Substitue the argument types. */
6354 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6355 complain, in_decl);
6356 if (arg_types == error_mark_node)
6357 return error_mark_node;
6359 /* Construct a new type node and return it. */
6360 if (TREE_CODE (t) == FUNCTION_TYPE)
6361 fntype = build_function_type (return_type, arg_types);
6362 else
6364 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6365 if (! IS_AGGR_TYPE (r))
6367 /* [temp.deduct]
6369 Type deduction may fail for any of the following
6370 reasons:
6372 -- Attempting to create "pointer to member of T" when T
6373 is not a class type. */
6374 if (complain & tf_error)
6375 error ("creating pointer to member function of non-class type `%T'",
6377 return error_mark_node;
6380 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6381 (arg_types));
6383 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6384 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6386 return fntype;
6389 /* Substitute into the PARMS of a call-declarator. */
6391 static tree
6392 tsubst_call_declarator_parms (tree parms,
6393 tree args,
6394 tsubst_flags_t complain,
6395 tree in_decl)
6397 tree new_parms;
6398 tree type;
6399 tree defarg;
6401 if (!parms || parms == void_list_node)
6402 return parms;
6404 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6405 args, complain, in_decl);
6407 /* Figure out the type of this parameter. */
6408 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6410 /* Figure out the default argument as well. Note that we use
6411 tsubst_expr since the default argument is really an expression. */
6412 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6414 /* Chain this parameter on to the front of those we have already
6415 processed. We don't use hash_tree_cons because that function
6416 doesn't check TREE_PARMLIST. */
6417 new_parms = tree_cons (defarg, type, new_parms);
6419 /* And note that these are parameters. */
6420 TREE_PARMLIST (new_parms) = 1;
6422 return new_parms;
6425 /* Take the tree structure T and replace template parameters used
6426 therein with the argument vector ARGS. IN_DECL is an associated
6427 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6428 Issue error and warning messages under control of COMPLAIN. Note
6429 that we must be relatively non-tolerant of extensions here, in
6430 order to preserve conformance; if we allow substitutions that
6431 should not be allowed, we may allow argument deductions that should
6432 not succeed, and therefore report ambiguous overload situations
6433 where there are none. In theory, we could allow the substitution,
6434 but indicate that it should have failed, and allow our caller to
6435 make sure that the right thing happens, but we don't try to do this
6436 yet.
6438 This function is used for dealing with types, decls and the like;
6439 for expressions, use tsubst_expr or tsubst_copy. */
6441 static tree
6442 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6444 tree type, r;
6446 if (t == NULL_TREE || t == error_mark_node
6447 || t == integer_type_node
6448 || t == void_type_node
6449 || t == char_type_node
6450 || TREE_CODE (t) == NAMESPACE_DECL)
6451 return t;
6453 if (TREE_CODE (t) == IDENTIFIER_NODE)
6454 type = IDENTIFIER_TYPE_VALUE (t);
6455 else
6456 type = TREE_TYPE (t);
6457 if (type == unknown_type_node)
6458 abort ();
6460 if (type && TREE_CODE (t) != FUNCTION_DECL
6461 && TREE_CODE (t) != TYPENAME_TYPE
6462 && TREE_CODE (t) != TEMPLATE_DECL
6463 && TREE_CODE (t) != IDENTIFIER_NODE
6464 && TREE_CODE (t) != FUNCTION_TYPE
6465 && TREE_CODE (t) != METHOD_TYPE)
6466 type = tsubst (type, args, complain, in_decl);
6467 if (type == error_mark_node)
6468 return error_mark_node;
6470 if (DECL_P (t))
6471 return tsubst_decl (t, args, type, complain);
6473 switch (TREE_CODE (t))
6475 case RECORD_TYPE:
6476 case UNION_TYPE:
6477 case ENUMERAL_TYPE:
6478 return tsubst_aggr_type (t, args, complain, in_decl,
6479 /*entering_scope=*/0);
6481 case ERROR_MARK:
6482 case IDENTIFIER_NODE:
6483 case VOID_TYPE:
6484 case REAL_TYPE:
6485 case COMPLEX_TYPE:
6486 case VECTOR_TYPE:
6487 case BOOLEAN_TYPE:
6488 case INTEGER_CST:
6489 case REAL_CST:
6490 case STRING_CST:
6491 return t;
6493 case INTEGER_TYPE:
6494 if (t == integer_type_node)
6495 return t;
6497 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6498 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6499 return t;
6502 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6504 max = tsubst_expr (omax, args, complain, in_decl);
6505 if (max == error_mark_node)
6506 return error_mark_node;
6508 /* See if we can reduce this expression to something simpler. */
6509 max = maybe_fold_nontype_arg (max);
6510 if (!processing_template_decl)
6511 max = decl_constant_value (max);
6513 if (processing_template_decl
6514 /* When providing explicit arguments to a template
6515 function, but leaving some arguments for subsequent
6516 deduction, MAX may be template-dependent even if we're
6517 not PROCESSING_TEMPLATE_DECL. We still need to check for
6518 template parms, though; MAX won't be an INTEGER_CST for
6519 dynamic arrays, either. */
6520 || (TREE_CODE (max) != INTEGER_CST
6521 && uses_template_parms (max)))
6523 tree itype = make_node (INTEGER_TYPE);
6524 TYPE_MIN_VALUE (itype) = size_zero_node;
6525 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6526 integer_one_node);
6527 return itype;
6530 if (integer_zerop (omax))
6532 /* Still allow an explicit array of size zero. */
6533 if (pedantic)
6534 pedwarn ("creating array with size zero");
6536 else if (integer_zerop (max)
6537 || (TREE_CODE (max) == INTEGER_CST
6538 && INT_CST_LT (max, integer_zero_node)))
6540 /* [temp.deduct]
6542 Type deduction may fail for any of the following
6543 reasons:
6545 Attempting to create an array with a size that is
6546 zero or negative. */
6547 if (complain & tf_error)
6548 error ("creating array with size zero (`%E')", max);
6550 return error_mark_node;
6553 return compute_array_index_type (NULL_TREE, max);
6556 case TEMPLATE_TYPE_PARM:
6557 case TEMPLATE_TEMPLATE_PARM:
6558 case BOUND_TEMPLATE_TEMPLATE_PARM:
6559 case TEMPLATE_PARM_INDEX:
6561 int idx;
6562 int level;
6563 int levels;
6565 r = NULL_TREE;
6567 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6568 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6569 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6571 idx = TEMPLATE_TYPE_IDX (t);
6572 level = TEMPLATE_TYPE_LEVEL (t);
6574 else
6576 idx = TEMPLATE_PARM_IDX (t);
6577 level = TEMPLATE_PARM_LEVEL (t);
6580 if (TREE_VEC_LENGTH (args) > 0)
6582 tree arg = NULL_TREE;
6584 levels = TMPL_ARGS_DEPTH (args);
6585 if (level <= levels)
6586 arg = TMPL_ARG (args, level, idx);
6588 if (arg == error_mark_node)
6589 return error_mark_node;
6590 else if (arg != NULL_TREE)
6592 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6594 my_friendly_assert (TYPE_P (arg), 0);
6595 return cp_build_qualified_type_real
6596 (arg, cp_type_quals (arg) | cp_type_quals (t),
6597 complain | tf_ignore_bad_quals);
6599 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6601 /* We are processing a type constructed from
6602 a template template parameter */
6603 tree argvec = tsubst (TYPE_TI_ARGS (t),
6604 args, complain, in_decl);
6605 if (argvec == error_mark_node)
6606 return error_mark_node;
6608 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6609 we are resolving nested-types in the signature of
6610 a member function templates.
6611 Otherwise ARG is a TEMPLATE_DECL and is the real
6612 template to be instantiated. */
6613 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6614 arg = TYPE_NAME (arg);
6616 r = lookup_template_class (arg,
6617 argvec, in_decl,
6618 DECL_CONTEXT (arg),
6619 /*entering_scope=*/0,
6620 complain);
6621 return cp_build_qualified_type_real
6622 (r, TYPE_QUALS (t), complain);
6624 else
6625 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6626 return arg;
6629 else
6630 abort ();
6632 if (level == 1)
6633 /* This can happen during the attempted tsubst'ing in
6634 unify. This means that we don't yet have any information
6635 about the template parameter in question. */
6636 return t;
6638 /* If we get here, we must have been looking at a parm for a
6639 more deeply nested template. Make a new version of this
6640 template parameter, but with a lower level. */
6641 switch (TREE_CODE (t))
6643 case TEMPLATE_TYPE_PARM:
6644 case TEMPLATE_TEMPLATE_PARM:
6645 case BOUND_TEMPLATE_TEMPLATE_PARM:
6646 if (cp_type_quals (t))
6648 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6649 r = cp_build_qualified_type_real
6650 (r, cp_type_quals (t),
6651 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6652 ? tf_ignore_bad_quals : 0));
6654 else
6656 r = copy_type (t);
6657 TEMPLATE_TYPE_PARM_INDEX (r)
6658 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6659 r, levels);
6660 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6661 TYPE_MAIN_VARIANT (r) = r;
6662 TYPE_POINTER_TO (r) = NULL_TREE;
6663 TYPE_REFERENCE_TO (r) = NULL_TREE;
6665 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6667 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6668 complain, in_decl);
6669 if (argvec == error_mark_node)
6670 return error_mark_node;
6672 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6673 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6676 break;
6678 case TEMPLATE_PARM_INDEX:
6679 r = reduce_template_parm_level (t, type, levels);
6680 break;
6682 default:
6683 abort ();
6686 return r;
6689 case TREE_LIST:
6691 tree purpose, value, chain, result;
6693 if (t == void_list_node)
6694 return t;
6696 purpose = TREE_PURPOSE (t);
6697 if (purpose)
6699 purpose = tsubst (purpose, args, complain, in_decl);
6700 if (purpose == error_mark_node)
6701 return error_mark_node;
6703 value = TREE_VALUE (t);
6704 if (value)
6706 value = tsubst (value, args, complain, in_decl);
6707 if (value == error_mark_node)
6708 return error_mark_node;
6710 chain = TREE_CHAIN (t);
6711 if (chain && chain != void_type_node)
6713 chain = tsubst (chain, args, complain, in_decl);
6714 if (chain == error_mark_node)
6715 return error_mark_node;
6717 if (purpose == TREE_PURPOSE (t)
6718 && value == TREE_VALUE (t)
6719 && chain == TREE_CHAIN (t))
6720 return t;
6721 if (TREE_PARMLIST (t))
6723 result = tree_cons (purpose, value, chain);
6724 TREE_PARMLIST (result) = 1;
6726 else
6727 result = hash_tree_cons (purpose, value, chain);
6728 return result;
6730 case TREE_VEC:
6731 if (type != NULL_TREE)
6733 /* A binfo node. We always need to make a copy, of the node
6734 itself and of its BINFO_BASETYPES. */
6736 t = copy_node (t);
6738 /* Make sure type isn't a typedef copy. */
6739 type = BINFO_TYPE (TYPE_BINFO (type));
6741 TREE_TYPE (t) = complete_type (type);
6742 if (IS_AGGR_TYPE (type))
6744 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6745 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6746 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6747 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6749 return t;
6752 /* Otherwise, a vector of template arguments. */
6753 return tsubst_template_arg_vector (t, args, complain);
6755 case POINTER_TYPE:
6756 case REFERENCE_TYPE:
6758 enum tree_code code;
6760 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6761 return t;
6763 code = TREE_CODE (t);
6766 /* [temp.deduct]
6768 Type deduction may fail for any of the following
6769 reasons:
6771 -- Attempting to create a pointer to reference type.
6772 -- Attempting to create a reference to a reference type or
6773 a reference to void. */
6774 if (TREE_CODE (type) == REFERENCE_TYPE
6775 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6777 static location_t last_loc;
6779 /* We keep track of the last time we issued this error
6780 message to avoid spewing a ton of messages during a
6781 single bad template instantiation. */
6782 if (complain & tf_error
6783 && (last_loc.line != input_line
6784 || last_loc.file != input_filename))
6786 if (TREE_CODE (type) == VOID_TYPE)
6787 error ("forming reference to void");
6788 else
6789 error ("forming %s to reference type `%T'",
6790 (code == POINTER_TYPE) ? "pointer" : "reference",
6791 type);
6792 last_loc = input_location;
6795 return error_mark_node;
6797 else if (code == POINTER_TYPE)
6799 r = build_pointer_type (type);
6800 if (TREE_CODE (type) == METHOD_TYPE)
6801 r = build_ptrmemfunc_type (r);
6803 else
6804 r = build_reference_type (type);
6805 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6807 if (r != error_mark_node)
6808 /* Will this ever be needed for TYPE_..._TO values? */
6809 layout_type (r);
6811 return r;
6813 case OFFSET_TYPE:
6815 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6816 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6818 /* [temp.deduct]
6820 Type deduction may fail for any of the following
6821 reasons:
6823 -- Attempting to create "pointer to member of T" when T
6824 is not a class type. */
6825 if (complain & tf_error)
6826 error ("creating pointer to member of non-class type `%T'", r);
6827 return error_mark_node;
6829 if (TREE_CODE (type) == REFERENCE_TYPE)
6831 if (complain & tf_error)
6832 error ("creating pointer to member reference type `%T'", type);
6834 return error_mark_node;
6836 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6837 if (TREE_CODE (type) == FUNCTION_TYPE)
6838 /* This is really a method type. The cv qualifiers of the
6839 this pointer should _not_ be determined by the cv
6840 qualifiers of the class type. They should be held
6841 somewhere in the FUNCTION_TYPE, but we don't do that at
6842 the moment. Consider
6843 typedef void (Func) () const;
6845 template <typename T1> void Foo (Func T1::*);
6848 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6849 TREE_TYPE (type),
6850 TYPE_ARG_TYPES (type));
6851 else
6852 return build_offset_type (r, type);
6854 case FUNCTION_TYPE:
6855 case METHOD_TYPE:
6857 tree fntype;
6858 tree raises;
6860 fntype = tsubst_function_type (t, args, complain, in_decl);
6861 if (fntype == error_mark_node)
6862 return error_mark_node;
6864 /* Substitue the exception specification. */
6865 raises = TYPE_RAISES_EXCEPTIONS (t);
6866 if (raises)
6868 tree list = NULL_TREE;
6870 if (! TREE_VALUE (raises))
6871 list = raises;
6872 else
6873 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6875 tree spec = TREE_VALUE (raises);
6877 spec = tsubst (spec, args, complain, in_decl);
6878 if (spec == error_mark_node)
6879 return spec;
6880 list = add_exception_specifier (list, spec, complain);
6882 fntype = build_exception_variant (fntype, list);
6884 return fntype;
6886 case ARRAY_TYPE:
6888 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6889 if (domain == error_mark_node)
6890 return error_mark_node;
6892 /* As an optimization, we avoid regenerating the array type if
6893 it will obviously be the same as T. */
6894 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6895 return t;
6897 /* These checks should match the ones in grokdeclarator.
6899 [temp.deduct]
6901 The deduction may fail for any of the following reasons:
6903 -- Attempting to create an array with an element type that
6904 is void, a function type, or a reference type. */
6905 if (TREE_CODE (type) == VOID_TYPE
6906 || TREE_CODE (type) == FUNCTION_TYPE
6907 || TREE_CODE (type) == REFERENCE_TYPE)
6909 if (complain & tf_error)
6910 error ("creating array of `%T'", type);
6911 return error_mark_node;
6914 r = build_cplus_array_type (type, domain);
6915 return r;
6918 case PLUS_EXPR:
6919 case MINUS_EXPR:
6921 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6922 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6924 if (e1 == error_mark_node || e2 == error_mark_node)
6925 return error_mark_node;
6927 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6930 case NEGATE_EXPR:
6931 case NOP_EXPR:
6933 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6934 if (e == error_mark_node)
6935 return error_mark_node;
6937 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6940 case TYPENAME_TYPE:
6942 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6943 in_decl, /*entering_scope=*/1);
6944 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6945 complain, in_decl);
6947 if (ctx == error_mark_node || f == error_mark_node)
6948 return error_mark_node;
6950 if (!IS_AGGR_TYPE (ctx))
6952 if (complain & tf_error)
6953 error ("`%T' is not a class, struct, or union type",
6954 ctx);
6955 return error_mark_node;
6957 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6959 /* Normally, make_typename_type does not require that the CTX
6960 have complete type in order to allow things like:
6962 template <class T> struct S { typename S<T>::X Y; };
6964 But, such constructs have already been resolved by this
6965 point, so here CTX really should have complete type, unless
6966 it's a partial instantiation. */
6967 ctx = complete_type (ctx);
6968 if (!COMPLETE_TYPE_P (ctx))
6970 if (complain & tf_error)
6971 cxx_incomplete_type_error (NULL_TREE, ctx);
6972 return error_mark_node;
6976 f = make_typename_type (ctx, f,
6977 (complain & tf_error) | tf_keep_type_decl);
6978 if (f == error_mark_node)
6979 return f;
6980 if (TREE_CODE (f) == TYPE_DECL)
6982 complain |= tf_ignore_bad_quals;
6983 f = TREE_TYPE (f);
6986 return cp_build_qualified_type_real
6987 (f, cp_type_quals (f) | cp_type_quals (t), complain);
6990 case UNBOUND_CLASS_TEMPLATE:
6992 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6993 in_decl, /*entering_scope=*/1);
6994 tree name = TYPE_IDENTIFIER (t);
6996 if (ctx == error_mark_node || name == error_mark_node)
6997 return error_mark_node;
6999 return make_unbound_class_template (ctx, name, complain);
7002 case INDIRECT_REF:
7004 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7005 if (e == error_mark_node)
7006 return error_mark_node;
7007 return make_pointer_declarator (type, e);
7010 case ADDR_EXPR:
7012 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7013 if (e == error_mark_node)
7014 return error_mark_node;
7015 return make_reference_declarator (type, e);
7018 case ARRAY_REF:
7020 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7021 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7022 if (e1 == error_mark_node || e2 == error_mark_node)
7023 return error_mark_node;
7025 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
7028 case CALL_EXPR:
7030 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7031 tree e2 = (tsubst_call_declarator_parms
7032 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
7033 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
7034 complain, in_decl);
7036 if (e1 == error_mark_node || e2 == error_mark_node
7037 || e3 == error_mark_node)
7038 return error_mark_node;
7040 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
7043 case SCOPE_REF:
7045 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7046 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7047 if (e1 == error_mark_node || e2 == error_mark_node)
7048 return error_mark_node;
7050 return build_nt (TREE_CODE (t), e1, e2);
7053 case TYPEOF_TYPE:
7055 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
7056 if (e1 == error_mark_node)
7057 return error_mark_node;
7059 return TREE_TYPE (e1);
7062 default:
7063 sorry ("use of `%s' in template",
7064 tree_code_name [(int) TREE_CODE (t)]);
7065 return error_mark_node;
7069 /* Like tsubst, but deals with expressions. This function just replaces
7070 template parms; to finish processing the resultant expression, use
7071 tsubst_expr. */
7073 static tree
7074 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7076 enum tree_code code;
7077 tree r;
7079 if (t == NULL_TREE || t == error_mark_node)
7080 return t;
7082 code = TREE_CODE (t);
7084 switch (code)
7086 case PARM_DECL:
7087 r = retrieve_local_specialization (t);
7088 my_friendly_assert (r != NULL, 20020903);
7089 return r;
7091 case CONST_DECL:
7093 tree enum_type;
7094 tree v;
7096 if (DECL_TEMPLATE_PARM_P (t))
7097 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7098 if (!DECL_CONTEXT (t))
7099 /* This is a global enumeration constant. */
7100 return t;
7102 /* Unfortunately, we cannot just call lookup_name here.
7103 Consider:
7105 template <int I> int f() {
7106 enum E { a = I };
7107 struct S { void g() { E e = a; } };
7110 When we instantiate f<7>::S::g(), say, lookup_name is not
7111 clever enough to find f<7>::a. */
7112 enum_type
7113 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7114 /*entering_scope=*/0);
7116 for (v = TYPE_VALUES (enum_type);
7117 v != NULL_TREE;
7118 v = TREE_CHAIN (v))
7119 if (TREE_PURPOSE (v) == DECL_NAME (t))
7120 return TREE_VALUE (v);
7122 /* We didn't find the name. That should never happen; if
7123 name-lookup found it during preliminary parsing, we
7124 should find it again here during instantiation. */
7125 abort ();
7127 return t;
7129 case FIELD_DECL:
7130 if (DECL_CONTEXT (t))
7132 tree ctx;
7134 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7135 /*entering_scope=*/1);
7136 if (ctx != DECL_CONTEXT (t))
7137 return lookup_field (ctx, DECL_NAME (t), 0, false);
7139 return t;
7141 case VAR_DECL:
7142 case FUNCTION_DECL:
7143 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7144 || local_variable_p (t))
7145 t = tsubst (t, args, complain, in_decl);
7146 mark_used (t);
7147 return t;
7149 case BASELINK:
7151 tree name;
7152 tree qualifying_scope;
7153 tree fns;
7154 tree template_args = 0;
7155 bool template_id_p = false;
7157 /* A baselink indicates a function from a base class. The
7158 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7159 non-dependent types; otherwise, the lookup could not have
7160 succeeded. However, they may indicate bases of the template
7161 class, rather than the instantiated class.
7163 In addition, lookups that were not ambiguous before may be
7164 ambiguous now. Therefore, we perform the lookup again. */
7165 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t));
7166 fns = BASELINK_FUNCTIONS (t);
7167 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7169 template_id_p = true;
7170 template_args = TREE_OPERAND (fns, 1);
7171 fns = TREE_OPERAND (fns, 0);
7172 template_args = tsubst_copy (template_args, args,
7173 complain, in_decl);
7174 maybe_fold_nontype_args (template_args);
7176 name = DECL_NAME (get_first_fn (fns));
7177 t = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7178 if (BASELINK_P (t) && template_id_p)
7179 BASELINK_FUNCTIONS (t)
7180 = build_nt (TEMPLATE_ID_EXPR,
7181 BASELINK_FUNCTIONS (t),
7182 template_args);
7183 return adjust_result_of_qualified_name_lookup (t,
7184 qualifying_scope,
7185 current_class_type);
7188 case TEMPLATE_DECL:
7189 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7190 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7191 args, complain, in_decl);
7192 else if (is_member_template (t))
7193 return tsubst (t, args, complain, in_decl);
7194 else if (DECL_CLASS_SCOPE_P (t)
7195 && uses_template_parms (DECL_CONTEXT (t)))
7197 /* Template template argument like the following example need
7198 special treatment:
7200 template <template <class> class TT> struct C {};
7201 template <class T> struct D {
7202 template <class U> struct E {};
7203 C<E> c; // #1
7205 D<int> d; // #2
7207 We are processing the template argument `E' in #1 for
7208 the template instantiation #2. Originally, `E' is a
7209 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7210 have to substitute this with one having context `D<int>'. */
7212 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7213 return lookup_field (context, DECL_NAME(t), 0, false);
7215 else
7216 /* Ordinary template template argument. */
7217 return t;
7219 case LOOKUP_EXPR:
7221 /* We must tsubst into a LOOKUP_EXPR in case the names to
7222 which it refers is a conversion operator; in that case the
7223 name will change. We avoid making unnecessary copies,
7224 however. */
7226 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7228 if (id != TREE_OPERAND (t, 0))
7230 r = build_nt (LOOKUP_EXPR, id);
7231 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7232 t = r;
7235 return t;
7238 case CAST_EXPR:
7239 case REINTERPRET_CAST_EXPR:
7240 case CONST_CAST_EXPR:
7241 case STATIC_CAST_EXPR:
7242 case DYNAMIC_CAST_EXPR:
7243 case NOP_EXPR:
7244 return build1
7245 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7246 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7248 case INDIRECT_REF:
7249 case NEGATE_EXPR:
7250 case TRUTH_NOT_EXPR:
7251 case BIT_NOT_EXPR:
7252 case ADDR_EXPR:
7253 case CONVERT_EXPR: /* Unary + */
7254 case SIZEOF_EXPR:
7255 case ALIGNOF_EXPR:
7256 case ARROW_EXPR:
7257 case THROW_EXPR:
7258 case TYPEID_EXPR:
7259 case REALPART_EXPR:
7260 case IMAGPART_EXPR:
7261 return build1
7262 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7263 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7265 case COMPONENT_REF:
7267 tree object;
7268 tree name;
7270 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7271 name = TREE_OPERAND (t, 1);
7272 if (TREE_CODE (name) == BIT_NOT_EXPR)
7274 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7275 complain, in_decl);
7276 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7278 else if (TREE_CODE (name) == SCOPE_REF
7279 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7281 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7282 complain, in_decl);
7283 name = TREE_OPERAND (name, 1);
7284 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7285 complain, in_decl);
7286 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7287 name = build_nt (SCOPE_REF, base, name);
7289 else
7290 name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7291 return build_nt (COMPONENT_REF, object, name);
7294 case PLUS_EXPR:
7295 case MINUS_EXPR:
7296 case MULT_EXPR:
7297 case TRUNC_DIV_EXPR:
7298 case CEIL_DIV_EXPR:
7299 case FLOOR_DIV_EXPR:
7300 case ROUND_DIV_EXPR:
7301 case EXACT_DIV_EXPR:
7302 case BIT_AND_EXPR:
7303 case BIT_ANDTC_EXPR:
7304 case BIT_IOR_EXPR:
7305 case BIT_XOR_EXPR:
7306 case TRUNC_MOD_EXPR:
7307 case FLOOR_MOD_EXPR:
7308 case TRUTH_ANDIF_EXPR:
7309 case TRUTH_ORIF_EXPR:
7310 case TRUTH_AND_EXPR:
7311 case TRUTH_OR_EXPR:
7312 case RSHIFT_EXPR:
7313 case LSHIFT_EXPR:
7314 case RROTATE_EXPR:
7315 case LROTATE_EXPR:
7316 case EQ_EXPR:
7317 case NE_EXPR:
7318 case MAX_EXPR:
7319 case MIN_EXPR:
7320 case LE_EXPR:
7321 case GE_EXPR:
7322 case LT_EXPR:
7323 case GT_EXPR:
7324 case ARRAY_REF:
7325 case COMPOUND_EXPR:
7326 case SCOPE_REF:
7327 case DOTSTAR_EXPR:
7328 case MEMBER_REF:
7329 case PREDECREMENT_EXPR:
7330 case PREINCREMENT_EXPR:
7331 case POSTDECREMENT_EXPR:
7332 case POSTINCREMENT_EXPR:
7333 return build_nt
7334 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7335 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7337 case CALL_EXPR:
7338 return build_nt (code,
7339 tsubst_copy (TREE_OPERAND (t, 0), args,
7340 complain, in_decl),
7341 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7342 in_decl),
7343 NULL_TREE);
7345 case METHOD_CALL_EXPR:
7346 return build_nt
7347 (code,
7348 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7349 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7350 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7351 NULL_TREE);
7353 case STMT_EXPR:
7354 /* This processing should really occur in tsubst_expr. However,
7355 tsubst_expr does not recurse into expressions, since it
7356 assumes that there aren't any statements inside them. So, we
7357 need to expand the STMT_EXPR here. */
7358 if (!processing_template_decl)
7360 tree stmt_expr = begin_stmt_expr ();
7361 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7362 return finish_stmt_expr (stmt_expr);
7365 return t;
7367 case COND_EXPR:
7368 case MODOP_EXPR:
7369 case PSEUDO_DTOR_EXPR:
7371 r = build_nt
7372 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7373 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7374 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7375 return r;
7378 case NEW_EXPR:
7380 r = build_nt
7381 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7382 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7383 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7384 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7385 return r;
7388 case DELETE_EXPR:
7390 r = build_nt
7391 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7392 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7393 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7394 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7395 return r;
7398 case TEMPLATE_ID_EXPR:
7400 /* Substituted template arguments */
7401 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7402 in_decl);
7404 maybe_fold_nontype_args (targs);
7405 return lookup_template_function
7406 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7409 case TREE_LIST:
7411 tree purpose, value, chain;
7413 if (t == void_list_node)
7414 return t;
7416 purpose = TREE_PURPOSE (t);
7417 if (purpose)
7418 purpose = tsubst_copy (purpose, args, complain, in_decl);
7419 value = TREE_VALUE (t);
7420 if (value)
7421 value = tsubst_copy (value, args, complain, in_decl);
7422 chain = TREE_CHAIN (t);
7423 if (chain && chain != void_type_node)
7424 chain = tsubst_copy (chain, args, complain, in_decl);
7425 if (purpose == TREE_PURPOSE (t)
7426 && value == TREE_VALUE (t)
7427 && chain == TREE_CHAIN (t))
7428 return t;
7429 return tree_cons (purpose, value, chain);
7432 case RECORD_TYPE:
7433 case UNION_TYPE:
7434 case ENUMERAL_TYPE:
7435 case INTEGER_TYPE:
7436 case TEMPLATE_TYPE_PARM:
7437 case TEMPLATE_TEMPLATE_PARM:
7438 case BOUND_TEMPLATE_TEMPLATE_PARM:
7439 case TEMPLATE_PARM_INDEX:
7440 case POINTER_TYPE:
7441 case REFERENCE_TYPE:
7442 case OFFSET_TYPE:
7443 case FUNCTION_TYPE:
7444 case METHOD_TYPE:
7445 case ARRAY_TYPE:
7446 case TYPENAME_TYPE:
7447 case UNBOUND_CLASS_TEMPLATE:
7448 case TYPEOF_TYPE:
7449 case TYPE_DECL:
7450 return tsubst (t, args, complain, in_decl);
7452 case IDENTIFIER_NODE:
7453 if (IDENTIFIER_TYPENAME_P (t))
7455 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7456 return mangle_conv_op_name_for_type (new_type);
7458 else
7459 return t;
7461 case CONSTRUCTOR:
7463 r = build_constructor
7464 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7465 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
7466 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7467 return r;
7470 case VA_ARG_EXPR:
7471 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7472 in_decl),
7473 tsubst (TREE_TYPE (t), args, complain, in_decl));
7475 default:
7476 return t;
7480 /* Like tsubst_copy for expressions, etc. but also does semantic
7481 processing. */
7483 static tree
7484 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7486 tree stmt, tmp;
7488 if (t == NULL_TREE || t == error_mark_node)
7489 return t;
7491 if (processing_template_decl)
7492 return tsubst_copy (t, args, complain, in_decl);
7494 if (!STATEMENT_CODE_P (TREE_CODE (t)))
7495 return tsubst_copy_and_build (t, args, complain, in_decl);
7497 switch (TREE_CODE (t))
7499 case CTOR_INITIALIZER:
7500 prep_stmt (t);
7501 finish_mem_initializers (tsubst_initializer_list
7502 (TREE_OPERAND (t, 0), args));
7503 break;
7505 case RETURN_STMT:
7506 prep_stmt (t);
7507 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
7508 args, complain, in_decl));
7509 break;
7511 case EXPR_STMT:
7512 prep_stmt (t);
7513 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7514 args, complain, in_decl));
7515 break;
7517 case USING_STMT:
7518 prep_stmt (t);
7519 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7520 args, complain, in_decl));
7521 break;
7523 case DECL_STMT:
7525 tree decl;
7526 tree init;
7528 prep_stmt (t);
7529 decl = DECL_STMT_DECL (t);
7530 if (TREE_CODE (decl) == LABEL_DECL)
7531 finish_label_decl (DECL_NAME (decl));
7532 else if (TREE_CODE (decl) == USING_DECL)
7534 tree scope = DECL_INITIAL (decl);
7535 tree name = DECL_NAME (decl);
7537 scope = tsubst_expr (scope, args, complain, in_decl);
7538 do_local_using_decl (lookup_qualified_name (scope,
7539 name,
7540 /*is_type_p=*/0,
7541 /*flags=*/0));
7543 else
7545 init = DECL_INITIAL (decl);
7546 decl = tsubst (decl, args, complain, in_decl);
7547 if (decl != error_mark_node)
7549 if (init)
7550 DECL_INITIAL (decl) = error_mark_node;
7551 /* By marking the declaration as instantiated, we avoid
7552 trying to instantiate it. Since instantiate_decl can't
7553 handle local variables, and since we've already done
7554 all that needs to be done, that's the right thing to
7555 do. */
7556 if (TREE_CODE (decl) == VAR_DECL)
7557 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7558 if (TREE_CODE (decl) == VAR_DECL
7559 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7560 /* Anonymous aggregates are a special case. */
7561 finish_anon_union (decl);
7562 else
7564 maybe_push_decl (decl);
7565 if (DECL_PRETTY_FUNCTION_P (decl))
7567 /* For __PRETTY_FUNCTION__ we have to adjust the
7568 initializer. */
7569 const char *const name
7570 = cxx_printable_name (current_function_decl, 2);
7571 init = cp_fname_init (name);
7572 TREE_TYPE (decl) = TREE_TYPE (init);
7574 else
7575 init = tsubst_expr (init, args, complain, in_decl);
7576 cp_finish_decl (decl, init, NULL_TREE, 0);
7581 /* A DECL_STMT can also be used as an expression, in the condition
7582 clause of an if/for/while construct. If we aren't followed by
7583 another statement, return our decl. */
7584 if (TREE_CHAIN (t) == NULL_TREE)
7585 return decl;
7587 break;
7589 case FOR_STMT:
7591 prep_stmt (t);
7593 stmt = begin_for_stmt ();
7594 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7595 finish_for_init_stmt (stmt);
7596 finish_for_cond (tsubst_expr (FOR_COND (t),
7597 args, complain, in_decl),
7598 stmt);
7599 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7600 finish_for_expr (tmp, stmt);
7601 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7602 finish_for_stmt (stmt);
7604 break;
7606 case WHILE_STMT:
7608 prep_stmt (t);
7609 stmt = begin_while_stmt ();
7610 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7611 args, complain, in_decl),
7612 stmt);
7613 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7614 finish_while_stmt (stmt);
7616 break;
7618 case DO_STMT:
7620 prep_stmt (t);
7621 stmt = begin_do_stmt ();
7622 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7623 finish_do_body (stmt);
7624 finish_do_stmt (tsubst_expr (DO_COND (t),
7625 args, complain, in_decl),
7626 stmt);
7628 break;
7630 case IF_STMT:
7632 prep_stmt (t);
7633 stmt = begin_if_stmt ();
7634 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7635 args, complain, in_decl),
7636 stmt);
7638 if (tmp = THEN_CLAUSE (t), tmp)
7640 tsubst_expr (tmp, args, complain, in_decl);
7641 finish_then_clause (stmt);
7644 if (tmp = ELSE_CLAUSE (t), tmp)
7646 begin_else_clause ();
7647 tsubst_expr (tmp, args, complain, in_decl);
7648 finish_else_clause (stmt);
7651 finish_if_stmt ();
7653 break;
7655 case COMPOUND_STMT:
7657 prep_stmt (t);
7658 if (COMPOUND_STMT_BODY_BLOCK (t))
7659 stmt = begin_function_body ();
7660 else
7661 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7663 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7665 if (COMPOUND_STMT_BODY_BLOCK (t))
7666 finish_function_body (stmt);
7667 else
7668 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7670 break;
7672 case BREAK_STMT:
7673 prep_stmt (t);
7674 finish_break_stmt ();
7675 break;
7677 case CONTINUE_STMT:
7678 prep_stmt (t);
7679 finish_continue_stmt ();
7680 break;
7682 case SWITCH_STMT:
7684 tree val;
7686 prep_stmt (t);
7687 stmt = begin_switch_stmt ();
7688 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7689 finish_switch_cond (val, stmt);
7690 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7691 finish_switch_stmt (stmt);
7693 break;
7695 case CASE_LABEL:
7696 prep_stmt (t);
7697 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7698 tsubst_expr (CASE_HIGH (t), args, complain,
7699 in_decl));
7700 break;
7702 case LABEL_STMT:
7703 input_line = STMT_LINENO (t);
7704 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7705 break;
7707 case FILE_STMT:
7708 input_filename = FILE_STMT_FILENAME (t);
7709 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7710 break;
7712 case GOTO_STMT:
7713 prep_stmt (t);
7714 tmp = GOTO_DESTINATION (t);
7715 if (TREE_CODE (tmp) != LABEL_DECL)
7716 /* Computed goto's must be tsubst'd into. On the other hand,
7717 non-computed gotos must not be; the identifier in question
7718 will have no binding. */
7719 tmp = tsubst_expr (tmp, args, complain, in_decl);
7720 else
7721 tmp = DECL_NAME (tmp);
7722 finish_goto_stmt (tmp);
7723 break;
7725 case ASM_STMT:
7726 prep_stmt (t);
7727 tmp = finish_asm_stmt
7728 (ASM_CV_QUAL (t),
7729 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7730 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7731 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7732 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7733 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
7734 break;
7736 case TRY_BLOCK:
7737 prep_stmt (t);
7738 if (CLEANUP_P (t))
7740 stmt = begin_try_block ();
7741 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7742 finish_cleanup_try_block (stmt);
7743 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7744 complain, in_decl),
7745 stmt);
7747 else
7749 if (FN_TRY_BLOCK_P (t))
7750 stmt = begin_function_try_block ();
7751 else
7752 stmt = begin_try_block ();
7754 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7756 if (FN_TRY_BLOCK_P (t))
7757 finish_function_try_block (stmt);
7758 else
7759 finish_try_block (stmt);
7761 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7762 if (FN_TRY_BLOCK_P (t))
7763 finish_function_handler_sequence (stmt);
7764 else
7765 finish_handler_sequence (stmt);
7767 break;
7769 case HANDLER:
7771 tree decl;
7773 prep_stmt (t);
7774 stmt = begin_handler ();
7775 if (HANDLER_PARMS (t))
7777 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7778 decl = tsubst (decl, args, complain, in_decl);
7779 /* Prevent instantiate_decl from trying to instantiate
7780 this variable. We've already done all that needs to be
7781 done. */
7782 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7784 else
7785 decl = NULL_TREE;
7786 finish_handler_parms (decl, stmt);
7787 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7788 finish_handler (stmt);
7790 break;
7792 case TAG_DEFN:
7793 prep_stmt (t);
7794 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7795 break;
7797 default:
7798 abort ();
7801 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7804 /* Like tsubst but deals with expressions and performs semantic
7805 analysis. */
7807 static tree
7808 tsubst_copy_and_build (tree t,
7809 tree args,
7810 tsubst_flags_t complain,
7811 tree in_decl)
7813 if (t == NULL_TREE || t == error_mark_node)
7814 return t;
7816 switch (TREE_CODE (t))
7818 case IDENTIFIER_NODE:
7819 if (IDENTIFIER_TYPENAME_P (t))
7821 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7822 return do_identifier (mangle_conv_op_name_for_type (new_type),
7823 NULL_TREE);
7825 else
7826 return do_identifier (t, NULL_TREE);
7828 case LOOKUP_EXPR:
7830 if (LOOKUP_EXPR_GLOBAL (t))
7832 tree token
7833 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7834 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
7836 else
7838 t = do_identifier
7839 (tsubst_copy
7840 (TREE_OPERAND (t, 0), args, complain, in_decl),
7841 NULL_TREE);
7842 if (TREE_CODE (t) == ALIAS_DECL)
7843 t = DECL_INITIAL (t);
7844 return t;
7848 case TEMPLATE_ID_EXPR:
7850 tree object;
7851 tree template
7852 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7853 tree targs
7854 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7856 if (TREE_CODE (template) == COMPONENT_REF)
7858 object = TREE_OPERAND (template, 0);
7859 template = TREE_OPERAND (template, 1);
7861 else
7862 object = NULL_TREE;
7863 maybe_fold_nontype_args (targs);
7864 template = lookup_template_function (template, targs);
7866 if (object)
7867 return build (COMPONENT_REF, TREE_TYPE (template),
7868 object, template);
7869 else
7870 return template;
7873 case INDIRECT_REF:
7874 return build_x_indirect_ref
7875 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7876 "unary *");
7878 case CAST_EXPR:
7879 return build_functional_cast
7880 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7881 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7883 case REINTERPRET_CAST_EXPR:
7884 return build_reinterpret_cast
7885 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7886 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7888 case CONST_CAST_EXPR:
7889 return build_const_cast
7890 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7891 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7893 case DYNAMIC_CAST_EXPR:
7894 return build_dynamic_cast
7895 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7896 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7898 case STATIC_CAST_EXPR:
7899 return build_static_cast
7900 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7901 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7903 case PREDECREMENT_EXPR:
7904 case PREINCREMENT_EXPR:
7905 case POSTDECREMENT_EXPR:
7906 case POSTINCREMENT_EXPR:
7907 if (TREE_TYPE (t))
7908 return tsubst_copy (t, args, complain, in_decl);
7909 else
7910 return build_x_unary_op
7911 (TREE_CODE (t),
7912 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7913 in_decl));
7915 case NEGATE_EXPR:
7916 case BIT_NOT_EXPR:
7917 if (TREE_TYPE (t))
7918 return tsubst_copy (t, args, complain, in_decl);
7919 else
7920 return build_x_unary_op
7921 (TREE_CODE (t),
7922 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7923 in_decl));
7925 case ABS_EXPR:
7926 if (TREE_TYPE (t))
7927 return t;
7928 return build_x_unary_op
7929 (TREE_CODE (t),
7930 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7932 case TRUTH_NOT_EXPR:
7933 case ADDR_EXPR:
7934 case CONVERT_EXPR: /* Unary + */
7935 case REALPART_EXPR:
7936 case IMAGPART_EXPR:
7937 if (TREE_TYPE (t))
7938 return tsubst_copy (t, args, complain, in_decl);
7939 else
7940 return build_x_unary_op
7941 (TREE_CODE (t),
7942 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7943 in_decl));
7945 case PLUS_EXPR:
7946 case MINUS_EXPR:
7947 case MULT_EXPR:
7948 case TRUNC_DIV_EXPR:
7949 case CEIL_DIV_EXPR:
7950 case FLOOR_DIV_EXPR:
7951 case ROUND_DIV_EXPR:
7952 case EXACT_DIV_EXPR:
7953 case BIT_AND_EXPR:
7954 case BIT_ANDTC_EXPR:
7955 case BIT_IOR_EXPR:
7956 case BIT_XOR_EXPR:
7957 case TRUNC_MOD_EXPR:
7958 case FLOOR_MOD_EXPR:
7959 case TRUTH_ANDIF_EXPR:
7960 case TRUTH_ORIF_EXPR:
7961 case TRUTH_AND_EXPR:
7962 case TRUTH_OR_EXPR:
7963 case RSHIFT_EXPR:
7964 case LSHIFT_EXPR:
7965 case RROTATE_EXPR:
7966 case LROTATE_EXPR:
7967 case EQ_EXPR:
7968 case NE_EXPR:
7969 case MAX_EXPR:
7970 case MIN_EXPR:
7971 case LE_EXPR:
7972 case GE_EXPR:
7973 case LT_EXPR:
7974 case GT_EXPR:
7975 case MEMBER_REF:
7976 return build_x_binary_op
7977 (TREE_CODE (t),
7978 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7979 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
7981 case DOTSTAR_EXPR:
7982 return build_m_component_ref
7983 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7984 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
7986 case SCOPE_REF:
7987 return build_offset_ref
7988 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7989 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7991 case ARRAY_REF:
7993 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
7994 == NULL_TREE)
7995 /* new-type-id */
7996 return build_nt
7997 (ARRAY_REF, NULL_TREE,
7998 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
7999 in_decl));
8001 return grok_array_decl
8002 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8003 in_decl),
8004 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8005 in_decl));
8008 case SIZEOF_EXPR:
8009 case ALIGNOF_EXPR:
8011 tree r =
8012 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8013 if (!TYPE_P (r))
8014 return TREE_CODE (t) == SIZEOF_EXPR ?
8015 expr_sizeof (r) : c_alignof_expr (r);
8016 else
8017 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
8020 case MODOP_EXPR:
8021 return build_x_modify_expr
8022 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8023 TREE_CODE (TREE_OPERAND (t, 1)),
8024 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8026 case ARROW_EXPR:
8027 return build_x_arrow
8028 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8030 case NEW_EXPR:
8031 return build_new
8032 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8033 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8034 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl),
8035 NEW_EXPR_USE_GLOBAL (t));
8037 case DELETE_EXPR:
8038 return delete_sanity
8039 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8040 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8041 DELETE_EXPR_USE_VEC (t),
8042 DELETE_EXPR_USE_GLOBAL (t));
8044 case COMPOUND_EXPR:
8046 if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)
8047 == NULL_TREE)
8048 return build_x_compound_expr
8049 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8050 in_decl));
8051 else
8052 abort ();
8055 case METHOD_CALL_EXPR:
8057 tree method
8058 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8060 if (TREE_CODE (method) == SCOPE_REF)
8062 tree name = TREE_OPERAND (method, 1);
8064 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8065 name = build_nt (TEMPLATE_ID_EXPR,
8066 TREE_OPERAND (name, 0),
8067 TREE_OPERAND (name, 1));
8069 return build_scoped_method_call
8070 (tsubst_copy_and_build
8071 (TREE_OPERAND (t, 1), args, complain, in_decl),
8072 TREE_OPERAND (method, 0),
8073 name,
8074 tsubst_copy_and_build
8075 (TREE_OPERAND (t, 2), args, complain, in_decl));
8077 else
8079 /* We can get a TEMPLATE_ID_EXPR here on code like:
8081 x->f<2>();
8083 so we must resolve that. However, we can also get things
8084 like a BIT_NOT_EXPR here, when referring to a destructor,
8085 and things like that are not correctly resolved by this
8086 function so just use it when we really need it. */
8087 if (TREE_CODE (method) == TEMPLATE_ID_EXPR)
8088 method = lookup_template_function
8089 (TREE_OPERAND (method, 0),
8090 TREE_OPERAND (method, 1));
8092 return build_method_call
8093 (tsubst_copy_and_build
8094 (TREE_OPERAND (t, 1), args, complain, in_decl),
8095 method,
8096 tsubst_copy_and_build
8097 (TREE_OPERAND (t, 2), args, complain, in_decl),
8098 NULL_TREE, LOOKUP_NORMAL);
8102 case CALL_EXPR:
8104 tree function, copy_args;
8106 function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8107 copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
8108 complain, in_decl);
8110 if (BASELINK_P (function))
8111 return build_call_from_tree (function, copy_args, 1);
8112 else if (TREE_CODE (function) == SCOPE_REF)
8114 tree name = TREE_OPERAND (function, 1);
8115 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8116 name = build_nt (TEMPLATE_ID_EXPR,
8117 TREE_OPERAND (name, 0),
8118 build_expr_from_tree (TREE_OPERAND (name, 1)));
8120 function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
8121 name);
8123 return build_call_from_tree (function, copy_args, 1);
8125 else
8127 tree name = function;
8128 tree id;
8130 if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
8131 && !LOOKUP_EXPR_GLOBAL (name)
8132 && (TREE_CODE ((id = TREE_OPERAND (name, 0)))
8133 == IDENTIFIER_NODE)
8134 && (!current_class_type
8135 || !lookup_member (current_class_type, id, 0, false)))
8137 /* Do Koenig lookup if there are no class members. */
8138 name = do_identifier (id, copy_args);
8140 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
8141 || ! really_overloaded_fn (name))
8142 name = build_expr_from_tree (name);
8144 if (TREE_CODE (name) == OFFSET_REF)
8145 return build_offset_ref_call_from_tree (name, copy_args);
8146 if (TREE_CODE (name) == COMPONENT_REF)
8147 return finish_object_call_expr (TREE_OPERAND (name, 1),
8148 TREE_OPERAND (name, 0),
8149 copy_args);
8150 name = convert_from_reference (name);
8151 return build_call_from_tree (name, copy_args,
8152 /*disallow_virtual=*/false);
8156 case COND_EXPR:
8157 return build_x_conditional_expr
8158 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8159 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8160 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8162 case PSEUDO_DTOR_EXPR:
8163 return finish_pseudo_destructor_expr
8164 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8165 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8166 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8168 case TREE_LIST:
8170 tree purpose, value, chain;
8172 if (t == void_list_node)
8173 return t;
8175 purpose = TREE_PURPOSE (t);
8176 if (purpose)
8177 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
8178 value = TREE_VALUE (t);
8179 if (value)
8180 value = tsubst_copy_and_build (value, args, complain, in_decl);
8181 chain = TREE_CHAIN (t);
8182 if (chain && chain != void_type_node)
8183 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
8184 if (purpose == TREE_PURPOSE (t)
8185 && value == TREE_VALUE (t)
8186 && chain == TREE_CHAIN (t))
8187 return t;
8188 return tree_cons (purpose, value, chain);
8191 case COMPONENT_REF:
8193 tree object =
8194 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8195 tree member =
8196 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
8198 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8200 if (TREE_CODE (member) == BIT_NOT_EXPR)
8201 return finish_pseudo_destructor_expr (object,
8202 NULL_TREE,
8203 TREE_TYPE (object));
8204 else if (TREE_CODE (member) == SCOPE_REF
8205 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8206 return finish_pseudo_destructor_expr (object,
8207 object,
8208 TREE_TYPE (object));
8210 else if (TREE_CODE (member) == SCOPE_REF
8211 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8213 tree tmpl;
8214 tree args;
8216 /* Lookup the template functions now that we know what the
8217 scope is. */
8218 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8219 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8220 member = lookup_qualified_name (TREE_OPERAND (member, 0),
8221 tmpl,
8222 /*is_type=*/0,
8223 /*flags=*/0);
8224 if (BASELINK_P (member))
8225 BASELINK_FUNCTIONS (member)
8226 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8227 args);
8228 else
8230 error ("`%D' is not a member of `%T'",
8231 tmpl, TREE_TYPE (object));
8232 return error_mark_node;
8236 return finish_class_member_access_expr (object, member);
8239 case THROW_EXPR:
8240 return build_throw
8241 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8243 case CONSTRUCTOR:
8245 tree r;
8246 tree elts;
8247 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8248 bool purpose_p;
8250 /* digest_init will do the wrong thing if we let it. */
8251 if (type && TYPE_PTRMEMFUNC_P (type))
8252 return t;
8254 r = NULL_TREE;
8255 /* We do not want to process the purpose of aggregate
8256 initializers as they are identifier nodes which will be
8257 looked up by digest_init. */
8258 purpose_p = !(type && IS_AGGR_TYPE (type));
8259 for (elts = CONSTRUCTOR_ELTS (t);
8260 elts;
8261 elts = TREE_CHAIN (elts))
8263 tree purpose = TREE_PURPOSE (elts);
8264 tree value = TREE_VALUE (elts);
8266 if (purpose && purpose_p)
8267 purpose
8268 = tsubst_copy_and_build (purpose, args, complain, in_decl);
8269 value = tsubst_copy_and_build (value, args, complain, in_decl);
8270 r = tree_cons (purpose, value, r);
8273 r = build_constructor (NULL_TREE, nreverse (r));
8274 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8276 if (type)
8277 return digest_init (type, r, 0);
8278 return r;
8281 case TYPEID_EXPR:
8283 tree operand_0
8284 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8285 in_decl);
8287 if (TYPE_P (operand_0))
8288 return get_typeid (operand_0);
8289 return build_typeid (operand_0);
8292 case PARM_DECL:
8293 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8295 case VAR_DECL:
8296 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8298 case VA_ARG_EXPR:
8299 return build_x_va_arg
8300 (tsubst_copy_and_build
8301 (TREE_OPERAND (t, 0), args, complain, in_decl),
8302 tsubst_copy (TREE_TYPE (t), args, complain, in_decl));
8304 default:
8305 return tsubst_copy (t, args, complain, in_decl);
8309 /* Verify that the instantiated ARGS are valid. For type arguments,
8310 make sure that the type's linkage is ok. For non-type arguments,
8311 make sure they are constants if they are integral or enumarations.
8312 Emit an error under control of COMPLAIN, and return TRUE on error. */
8314 static bool
8315 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
8317 int ix, len = DECL_NTPARMS (tmpl);
8318 bool result = false;
8320 for (ix = 0; ix != len; ix++)
8322 tree t = TREE_VEC_ELT (args, ix);
8324 if (TYPE_P (t))
8326 /* [basic.link]: A name with no linkage (notably, the name
8327 of a class or enumeration declared in a local scope)
8328 shall not be used to declare an entity with linkage.
8329 This implies that names with no linkage cannot be used as
8330 template arguments. */
8331 tree nt = no_linkage_check (t);
8333 if (nt)
8335 if (!(complain & tf_error))
8336 /*OK*/;
8337 else if (TYPE_ANONYMOUS_P (nt))
8338 error ("`%T' uses anonymous type", t);
8339 else
8340 error ("`%T' uses local type `%T'", t, nt);
8341 result = true;
8343 /* In order to avoid all sorts of complications, we do not
8344 allow variably-modified types as template arguments. */
8345 else if (variably_modified_type_p (t))
8347 if (complain & tf_error)
8348 error ("`%T' is a variably modified type", t);
8349 result = true;
8352 /* A non-type argument of integral or enumerated type must be a
8353 constant. */
8354 else if (TREE_TYPE (t)
8355 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
8356 && !TREE_CONSTANT (t))
8358 if (complain & tf_error)
8359 error ("integral expression `%E' is not constant", t);
8360 result = true;
8363 if (result && complain & tf_error)
8364 error (" trying to instantiate `%D'", tmpl);
8365 return result;
8368 /* Instantiate the indicated variable or function template TMPL with
8369 the template arguments in TARG_PTR. */
8371 tree
8372 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8374 tree fndecl;
8375 tree gen_tmpl;
8376 tree spec;
8378 if (tmpl == error_mark_node)
8379 return error_mark_node;
8381 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
8383 /* If this function is a clone, handle it specially. */
8384 if (DECL_CLONED_FUNCTION_P (tmpl))
8386 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
8387 complain);
8388 tree clone;
8390 /* Look for the clone. */
8391 for (clone = TREE_CHAIN (spec);
8392 clone && DECL_CLONED_FUNCTION_P (clone);
8393 clone = TREE_CHAIN (clone))
8394 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8395 return clone;
8396 /* We should always have found the clone by now. */
8397 abort ();
8398 return NULL_TREE;
8401 /* Check to see if we already have this specialization. */
8402 spec = retrieve_specialization (tmpl, targ_ptr);
8403 if (spec != NULL_TREE)
8404 return spec;
8406 gen_tmpl = most_general_template (tmpl);
8407 if (tmpl != gen_tmpl)
8409 /* The TMPL is a partial instantiation. To get a full set of
8410 arguments we must add the arguments used to perform the
8411 partial instantiation. */
8412 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8413 targ_ptr);
8415 /* Check to see if we already have this specialization. */
8416 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8417 if (spec != NULL_TREE)
8418 return spec;
8421 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
8422 complain))
8423 return error_mark_node;
8425 /* Make sure that we can see identifiers, and compute access
8426 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8427 created earlier. Let push_access_scope_real figure that out. */
8428 push_access_scope_real
8429 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
8430 complain, gen_tmpl));
8432 /* substitute template parameters */
8433 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8434 targ_ptr, complain, gen_tmpl);
8436 pop_access_scope (gen_tmpl);
8438 /* The DECL_TI_TEMPLATE should always be the immediate parent
8439 template, not the most general template. */
8440 DECL_TI_TEMPLATE (fndecl) = tmpl;
8442 if (flag_external_templates)
8443 add_pending_template (fndecl);
8445 /* If we've just instantiated the main entry point for a function,
8446 instantiate all the alternate entry points as well. We do this
8447 by cloning the instantiation of the main entry point, not by
8448 instantiating the template clones. */
8449 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8450 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8452 return fndecl;
8455 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8456 arguments that are being used when calling it. TARGS is a vector
8457 into which the deduced template arguments are placed.
8459 Return zero for success, 2 for an incomplete match that doesn't resolve
8460 all the types, and 1 for complete failure. An error message will be
8461 printed only for an incomplete match.
8463 If FN is a conversion operator, or we are trying to produce a specific
8464 specialization, RETURN_TYPE is the return type desired.
8466 The EXPLICIT_TARGS are explicit template arguments provided via a
8467 template-id.
8469 The parameter STRICT is one of:
8471 DEDUCE_CALL:
8472 We are deducing arguments for a function call, as in
8473 [temp.deduct.call].
8475 DEDUCE_CONV:
8476 We are deducing arguments for a conversion function, as in
8477 [temp.deduct.conv].
8479 DEDUCE_EXACT:
8480 We are deducing arguments when doing an explicit instantiation
8481 as in [temp.explicit], when determining an explicit specialization
8482 as in [temp.expl.spec], or when taking the address of a function
8483 template, as in [temp.deduct.funcaddr].
8485 DEDUCE_ORDER:
8486 We are deducing arguments when calculating the partial
8487 ordering between specializations of function or class
8488 templates, as in [temp.func.order] and [temp.class.order].
8490 LEN is the number of parms to consider before returning success, or -1
8491 for all. This is used in partial ordering to avoid comparing parms for
8492 which no actual argument was passed, since they are not considered in
8493 overload resolution (and are explicitly excluded from consideration in
8494 partial ordering in [temp.func.order]/6). */
8497 fn_type_unification (tree fn,
8498 tree explicit_targs,
8499 tree targs,
8500 tree args,
8501 tree return_type,
8502 unification_kind_t strict,
8503 int len)
8505 tree parms;
8506 tree fntype;
8507 int result;
8509 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
8511 fntype = TREE_TYPE (fn);
8512 if (explicit_targs)
8514 /* [temp.deduct]
8516 The specified template arguments must match the template
8517 parameters in kind (i.e., type, nontype, template), and there
8518 must not be more arguments than there are parameters;
8519 otherwise type deduction fails.
8521 Nontype arguments must match the types of the corresponding
8522 nontype template parameters, or must be convertible to the
8523 types of the corresponding nontype parameters as specified in
8524 _temp.arg.nontype_, otherwise type deduction fails.
8526 All references in the function type of the function template
8527 to the corresponding template parameters are replaced by the
8528 specified template argument values. If a substitution in a
8529 template parameter or in the function type of the function
8530 template results in an invalid type, type deduction fails. */
8531 int i;
8532 tree converted_args;
8534 converted_args
8535 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8536 explicit_targs, NULL_TREE, tf_none,
8537 /*require_all_arguments=*/0));
8538 if (converted_args == error_mark_node)
8539 return 1;
8541 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8542 if (fntype == error_mark_node)
8543 return 1;
8545 /* Place the explicitly specified arguments in TARGS. */
8546 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
8547 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8550 parms = TYPE_ARG_TYPES (fntype);
8551 /* Never do unification on the 'this' parameter. */
8552 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8553 parms = TREE_CHAIN (parms);
8555 if (return_type)
8557 /* We've been given a return type to match, prepend it. */
8558 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8559 args = tree_cons (NULL_TREE, return_type, args);
8560 if (len >= 0)
8561 ++len;
8564 /* We allow incomplete unification without an error message here
8565 because the standard doesn't seem to explicitly prohibit it. Our
8566 callers must be ready to deal with unification failures in any
8567 event. */
8568 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8569 targs, parms, args, /*subr=*/0,
8570 strict, /*allow_incomplete*/1, len);
8572 if (result == 0)
8573 /* All is well so far. Now, check:
8575 [temp.deduct]
8577 When all template arguments have been deduced, all uses of
8578 template parameters in nondeduced contexts are replaced with
8579 the corresponding deduced argument values. If the
8580 substitution results in an invalid type, as described above,
8581 type deduction fails. */
8582 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8583 == error_mark_node)
8584 return 1;
8586 return result;
8589 /* Adjust types before performing type deduction, as described in
8590 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8591 sections are symmetric. PARM is the type of a function parameter
8592 or the return type of the conversion function. ARG is the type of
8593 the argument passed to the call, or the type of the value
8594 initialized with the result of the conversion function. */
8596 static int
8597 maybe_adjust_types_for_deduction (unification_kind_t strict,
8598 tree* parm,
8599 tree* arg)
8601 int result = 0;
8603 switch (strict)
8605 case DEDUCE_CALL:
8606 break;
8608 case DEDUCE_CONV:
8610 /* Swap PARM and ARG throughout the remainder of this
8611 function; the handling is precisely symmetric since PARM
8612 will initialize ARG rather than vice versa. */
8613 tree* temp = parm;
8614 parm = arg;
8615 arg = temp;
8616 break;
8619 case DEDUCE_EXACT:
8620 /* There is nothing to do in this case. */
8621 return 0;
8623 case DEDUCE_ORDER:
8624 /* DR 214. [temp.func.order] is underspecified, and leads to no
8625 ordering between things like `T *' and `T const &' for `U *'.
8626 The former has T=U and the latter T=U*. The former looks more
8627 specialized and John Spicer considers it well-formed (the EDG
8628 compiler accepts it).
8630 John also confirms that deduction should proceed as in a function
8631 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8632 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8633 to an actual call can have such a type.
8635 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8636 If only ARG is a REFERENCE_TYPE, we look through that and then
8637 proceed as with DEDUCE_CALL (which could further convert it). */
8638 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8640 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8641 return 0;
8642 *arg = TREE_TYPE (*arg);
8644 break;
8645 default:
8646 abort ();
8649 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8651 /* [temp.deduct.call]
8653 If P is not a reference type:
8655 --If A is an array type, the pointer type produced by the
8656 array-to-pointer standard conversion (_conv.array_) is
8657 used in place of A for type deduction; otherwise,
8659 --If A is a function type, the pointer type produced by
8660 the function-to-pointer standard conversion
8661 (_conv.func_) is used in place of A for type deduction;
8662 otherwise,
8664 --If A is a cv-qualified type, the top level
8665 cv-qualifiers of A's type are ignored for type
8666 deduction. */
8667 if (TREE_CODE (*arg) == ARRAY_TYPE)
8668 *arg = build_pointer_type (TREE_TYPE (*arg));
8669 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
8670 *arg = build_pointer_type (*arg);
8671 else
8672 *arg = TYPE_MAIN_VARIANT (*arg);
8675 /* [temp.deduct.call]
8677 If P is a cv-qualified type, the top level cv-qualifiers
8678 of P's type are ignored for type deduction. If P is a
8679 reference type, the type referred to by P is used for
8680 type deduction. */
8681 *parm = TYPE_MAIN_VARIANT (*parm);
8682 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8684 *parm = TREE_TYPE (*parm);
8685 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8688 /* DR 322. For conversion deduction, remove a reference type on parm
8689 too (which has been swapped into ARG). */
8690 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
8691 *arg = TREE_TYPE (*arg);
8693 return result;
8696 /* Most parms like fn_type_unification.
8698 If SUBR is 1, we're being called recursively (to unify the
8699 arguments of a function or method parameter of a function
8700 template). */
8702 static int
8703 type_unification_real (tree tparms,
8704 tree targs,
8705 tree xparms,
8706 tree xargs,
8707 int subr,
8708 unification_kind_t strict,
8709 int allow_incomplete,
8710 int xlen)
8712 tree parm, arg;
8713 int i;
8714 int ntparms = TREE_VEC_LENGTH (tparms);
8715 int sub_strict;
8716 int saw_undeduced = 0;
8717 tree parms, args;
8718 int len;
8720 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
8721 my_friendly_assert (xparms == NULL_TREE
8722 || TREE_CODE (xparms) == TREE_LIST, 290);
8723 /* ARGS could be NULL. */
8724 if (xargs)
8725 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8726 my_friendly_assert (ntparms > 0, 292);
8728 switch (strict)
8730 case DEDUCE_CALL:
8731 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8732 | UNIFY_ALLOW_DERIVED);
8733 break;
8735 case DEDUCE_CONV:
8736 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8737 break;
8739 case DEDUCE_EXACT:
8740 sub_strict = UNIFY_ALLOW_NONE;
8741 break;
8743 case DEDUCE_ORDER:
8744 sub_strict = UNIFY_ALLOW_NONE;
8745 break;
8747 default:
8748 abort ();
8751 if (xlen == 0)
8752 return 0;
8754 again:
8755 parms = xparms;
8756 args = xargs;
8757 len = xlen;
8759 while (parms
8760 && parms != void_list_node
8761 && args
8762 && args != void_list_node)
8764 parm = TREE_VALUE (parms);
8765 parms = TREE_CHAIN (parms);
8766 arg = TREE_VALUE (args);
8767 args = TREE_CHAIN (args);
8769 if (arg == error_mark_node)
8770 return 1;
8771 if (arg == unknown_type_node)
8772 /* We can't deduce anything from this, but we might get all the
8773 template args from other function args. */
8774 continue;
8776 /* Conversions will be performed on a function argument that
8777 corresponds with a function parameter that contains only
8778 non-deducible template parameters and explicitly specified
8779 template parameters. */
8780 if (! uses_template_parms (parm))
8782 tree type;
8784 if (!TYPE_P (arg))
8785 type = TREE_TYPE (arg);
8786 else
8788 type = arg;
8789 arg = NULL_TREE;
8792 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
8794 if (same_type_p (parm, type))
8795 continue;
8797 else
8798 /* It might work; we shouldn't check now, because we might
8799 get into infinite recursion. Overload resolution will
8800 handle it. */
8801 continue;
8803 return 1;
8806 if (!TYPE_P (arg))
8808 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8809 if (type_unknown_p (arg))
8811 /* [temp.deduct.type] A template-argument can be deduced from
8812 a pointer to function or pointer to member function
8813 argument if the set of overloaded functions does not
8814 contain function templates and at most one of a set of
8815 overloaded functions provides a unique match. */
8817 if (resolve_overloaded_unification
8818 (tparms, targs, parm, arg, strict, sub_strict)
8819 != 0)
8820 return 1;
8821 continue;
8823 arg = TREE_TYPE (arg);
8827 int arg_strict = sub_strict;
8829 if (!subr)
8830 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8832 if (unify (tparms, targs, parm, arg, arg_strict))
8833 return 1;
8836 /* Are we done with the interesting parms? */
8837 if (--len == 0)
8838 goto done;
8840 /* Fail if we've reached the end of the parm list, and more args
8841 are present, and the parm list isn't variadic. */
8842 if (args && args != void_list_node && parms == void_list_node)
8843 return 1;
8844 /* Fail if parms are left and they don't have default values. */
8845 if (parms
8846 && parms != void_list_node
8847 && TREE_PURPOSE (parms) == NULL_TREE)
8848 return 1;
8850 done:
8851 if (!subr)
8852 for (i = 0; i < ntparms; i++)
8853 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8855 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8857 /* If this is an undeduced nontype parameter that depends on
8858 a type parameter, try another pass; its type may have been
8859 deduced from a later argument than the one from which
8860 this parameter can be deduced. */
8861 if (TREE_CODE (tparm) == PARM_DECL
8862 && uses_template_parms (TREE_TYPE (tparm))
8863 && !saw_undeduced++)
8864 goto again;
8866 if (!allow_incomplete)
8867 error ("incomplete type unification");
8868 return 2;
8870 return 0;
8873 /* Subroutine of type_unification_real. Args are like the variables at the
8874 call site. ARG is an overloaded function (or template-id); we try
8875 deducing template args from each of the overloads, and if only one
8876 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8878 static int
8879 resolve_overloaded_unification (tree tparms,
8880 tree targs,
8881 tree parm,
8882 tree arg,
8883 unification_kind_t strict,
8884 int sub_strict)
8886 tree tempargs = copy_node (targs);
8887 int good = 0;
8889 if (TREE_CODE (arg) == ADDR_EXPR)
8890 arg = TREE_OPERAND (arg, 0);
8892 if (TREE_CODE (arg) == COMPONENT_REF)
8893 /* Handle `&x' where `x' is some static or non-static member
8894 function name. */
8895 arg = TREE_OPERAND (arg, 1);
8897 if (TREE_CODE (arg) == OFFSET_REF)
8898 arg = TREE_OPERAND (arg, 1);
8900 /* Strip baselink information. */
8901 if (BASELINK_P (arg))
8902 arg = BASELINK_FUNCTIONS (arg);
8904 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8906 /* If we got some explicit template args, we need to plug them into
8907 the affected templates before we try to unify, in case the
8908 explicit args will completely resolve the templates in question. */
8910 tree expl_subargs = TREE_OPERAND (arg, 1);
8911 arg = TREE_OPERAND (arg, 0);
8913 for (; arg; arg = OVL_NEXT (arg))
8915 tree fn = OVL_CURRENT (arg);
8916 tree subargs, elem;
8918 if (TREE_CODE (fn) != TEMPLATE_DECL)
8919 continue;
8921 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8922 expl_subargs);
8923 if (subargs)
8925 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
8926 if (TREE_CODE (elem) == METHOD_TYPE)
8927 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8928 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8929 strict, sub_strict);
8933 else if (TREE_CODE (arg) == OVERLOAD
8934 || TREE_CODE (arg) == FUNCTION_DECL)
8936 for (; arg; arg = OVL_NEXT (arg))
8938 tree type = TREE_TYPE (OVL_CURRENT (arg));
8939 if (TREE_CODE (type) == METHOD_TYPE)
8940 type = build_ptrmemfunc_type (build_pointer_type (type));
8941 good += try_one_overload (tparms, targs, tempargs, parm,
8942 type,
8943 strict, sub_strict);
8946 else
8947 abort ();
8949 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8950 to function or pointer to member function argument if the set of
8951 overloaded functions does not contain function templates and at most
8952 one of a set of overloaded functions provides a unique match.
8954 So if we found multiple possibilities, we return success but don't
8955 deduce anything. */
8957 if (good == 1)
8959 int i = TREE_VEC_LENGTH (targs);
8960 for (; i--; )
8961 if (TREE_VEC_ELT (tempargs, i))
8962 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8964 if (good)
8965 return 0;
8967 return 1;
8970 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8971 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8972 different overloads deduce different arguments for a given parm.
8973 Returns 1 on success. */
8975 static int
8976 try_one_overload (tree tparms,
8977 tree orig_targs,
8978 tree targs,
8979 tree parm,
8980 tree arg,
8981 unification_kind_t strict,
8982 int sub_strict)
8984 int nargs;
8985 tree tempargs;
8986 int i;
8988 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8989 to function or pointer to member function argument if the set of
8990 overloaded functions does not contain function templates and at most
8991 one of a set of overloaded functions provides a unique match.
8993 So if this is a template, just return success. */
8995 if (uses_template_parms (arg))
8996 return 1;
8998 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9000 /* We don't copy orig_targs for this because if we have already deduced
9001 some template args from previous args, unify would complain when we
9002 try to deduce a template parameter for the same argument, even though
9003 there isn't really a conflict. */
9004 nargs = TREE_VEC_LENGTH (targs);
9005 tempargs = make_tree_vec (nargs);
9007 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9008 return 0;
9010 /* First make sure we didn't deduce anything that conflicts with
9011 explicitly specified args. */
9012 for (i = nargs; i--; )
9014 tree elt = TREE_VEC_ELT (tempargs, i);
9015 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9017 if (elt == NULL_TREE)
9018 continue;
9019 else if (uses_template_parms (elt))
9021 /* Since we're unifying against ourselves, we will fill in template
9022 args used in the function parm list with our own template parms.
9023 Discard them. */
9024 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9025 continue;
9027 else if (oldelt && ! template_args_equal (oldelt, elt))
9028 return 0;
9031 for (i = nargs; i--; )
9033 tree elt = TREE_VEC_ELT (tempargs, i);
9035 if (elt)
9036 TREE_VEC_ELT (targs, i) = elt;
9039 return 1;
9042 /* Verify that nondeduce template argument agrees with the type
9043 obtained from argument deduction. Return nonzero if the
9044 verification fails.
9046 For example:
9048 struct A { typedef int X; };
9049 template <class T, class U> struct C {};
9050 template <class T> struct C<T, typename T::X> {};
9052 Then with the instantiation `C<A, int>', we can deduce that
9053 `T' is `A' but unify () does not check whether `typename T::X'
9054 is `int'. This function ensure that they agree.
9056 TARGS, PARMS are the same as the arguments of unify.
9057 ARGS contains template arguments from all levels. */
9059 static int
9060 verify_class_unification (tree targs, tree parms, tree args)
9062 parms = tsubst (parms, add_outermost_template_args (args, targs),
9063 tf_none, NULL_TREE);
9064 if (parms == error_mark_node)
9065 return 1;
9067 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9070 /* PARM is a template class (perhaps with unbound template
9071 parameters). ARG is a fully instantiated type. If ARG can be
9072 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9073 TARGS are as for unify. */
9075 static tree
9076 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9078 tree copy_of_targs;
9080 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9081 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9082 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9083 return NULL_TREE;
9085 /* We need to make a new template argument vector for the call to
9086 unify. If we used TARGS, we'd clutter it up with the result of
9087 the attempted unification, even if this class didn't work out.
9088 We also don't want to commit ourselves to all the unifications
9089 we've already done, since unification is supposed to be done on
9090 an argument-by-argument basis. In other words, consider the
9091 following pathological case:
9093 template <int I, int J, int K>
9094 struct S {};
9096 template <int I, int J>
9097 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9099 template <int I, int J, int K>
9100 void f(S<I, J, K>, S<I, I, I>);
9102 void g() {
9103 S<0, 0, 0> s0;
9104 S<0, 1, 2> s2;
9106 f(s0, s2);
9109 Now, by the time we consider the unification involving `s2', we
9110 already know that we must have `f<0, 0, 0>'. But, even though
9111 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9112 because there are two ways to unify base classes of S<0, 1, 2>
9113 with S<I, I, I>. If we kept the already deduced knowledge, we
9114 would reject the possibility I=1. */
9115 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9117 /* If unification failed, we're done. */
9118 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9119 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9120 return NULL_TREE;
9122 return arg;
9125 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9126 have already discovered to be satisfactory. ARG_BINFO is the binfo
9127 for the base class of ARG that we are currently examining. */
9129 static tree
9130 get_template_base_recursive (tree tparms,
9131 tree targs,
9132 tree parm,
9133 tree arg_binfo,
9134 tree rval,
9135 int flags)
9137 tree binfos;
9138 int i, n_baselinks;
9139 tree arg = BINFO_TYPE (arg_binfo);
9141 if (!(flags & GTB_IGNORE_TYPE))
9143 tree r = try_class_unification (tparms, targs,
9144 parm, arg);
9146 /* If there is more than one satisfactory baseclass, then:
9148 [temp.deduct.call]
9150 If they yield more than one possible deduced A, the type
9151 deduction fails.
9153 applies. */
9154 if (r && rval && !same_type_p (r, rval))
9155 return error_mark_node;
9156 else if (r)
9157 rval = r;
9160 binfos = BINFO_BASETYPES (arg_binfo);
9161 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9163 /* Process base types. */
9164 for (i = 0; i < n_baselinks; i++)
9166 tree base_binfo = TREE_VEC_ELT (binfos, i);
9167 int this_virtual;
9169 /* Skip this base, if we've already seen it. */
9170 if (BINFO_MARKED (base_binfo))
9171 continue;
9173 this_virtual =
9174 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9176 /* When searching for a non-virtual, we cannot mark virtually
9177 found binfos. */
9178 if (! this_virtual)
9179 BINFO_MARKED (base_binfo) = 1;
9181 rval = get_template_base_recursive (tparms, targs,
9182 parm,
9183 base_binfo,
9184 rval,
9185 GTB_VIA_VIRTUAL * this_virtual);
9187 /* If we discovered more than one matching base class, we can
9188 stop now. */
9189 if (rval == error_mark_node)
9190 return error_mark_node;
9193 return rval;
9196 /* Given a template type PARM and a class type ARG, find the unique
9197 base type in ARG that is an instance of PARM. We do not examine
9198 ARG itself; only its base-classes. If there is no appropriate base
9199 class, return NULL_TREE. If there is more than one, return
9200 error_mark_node. PARM may be the type of a partial specialization,
9201 as well as a plain template type. Used by unify. */
9203 static tree
9204 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9206 tree rval;
9207 tree arg_binfo;
9209 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9211 arg_binfo = TYPE_BINFO (complete_type (arg));
9212 rval = get_template_base_recursive (tparms, targs,
9213 parm, arg_binfo,
9214 NULL_TREE,
9215 GTB_IGNORE_TYPE);
9217 /* Since get_template_base_recursive marks the bases classes, we
9218 must unmark them here. */
9219 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9221 return rval;
9224 /* Returns the level of DECL, which declares a template parameter. */
9226 static int
9227 template_decl_level (tree decl)
9229 switch (TREE_CODE (decl))
9231 case TYPE_DECL:
9232 case TEMPLATE_DECL:
9233 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9235 case PARM_DECL:
9236 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9238 default:
9239 abort ();
9240 return 0;
9244 /* Decide whether ARG can be unified with PARM, considering only the
9245 cv-qualifiers of each type, given STRICT as documented for unify.
9246 Returns nonzero iff the unification is OK on that basis.*/
9248 static int
9249 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9251 int arg_quals = cp_type_quals (arg);
9252 int parm_quals = cp_type_quals (parm);
9254 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
9256 /* If the cvr quals of parm will not unify with ARG, they'll be
9257 ignored in instantiation, so we have to do the same here. */
9258 if (TREE_CODE (arg) == REFERENCE_TYPE)
9259 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
9260 if (!POINTER_TYPE_P (arg) &&
9261 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9262 parm_quals &= ~TYPE_QUAL_RESTRICT;
9265 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9266 && (arg_quals & parm_quals) != parm_quals)
9267 return 0;
9269 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9270 && (parm_quals & arg_quals) != arg_quals)
9271 return 0;
9273 return 1;
9276 /* Takes parameters as for type_unification. Returns 0 if the
9277 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9278 bitwise or of the following flags:
9280 UNIFY_ALLOW_NONE:
9281 Require an exact match between PARM and ARG.
9282 UNIFY_ALLOW_MORE_CV_QUAL:
9283 Allow the deduced ARG to be more cv-qualified (by qualification
9284 conversion) than ARG.
9285 UNIFY_ALLOW_LESS_CV_QUAL:
9286 Allow the deduced ARG to be less cv-qualified than ARG.
9287 UNIFY_ALLOW_DERIVED:
9288 Allow the deduced ARG to be a template base class of ARG,
9289 or a pointer to a template base class of the type pointed to by
9290 ARG.
9291 UNIFY_ALLOW_INTEGER:
9292 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9293 case for more information.
9294 UNIFY_ALLOW_OUTER_LEVEL:
9295 This is the outermost level of a deduction. Used to determine validity
9296 of qualification conversions. A valid qualification conversion must
9297 have const qualified pointers leading up to the inner type which
9298 requires additional CV quals, except at the outer level, where const
9299 is not required [conv.qual]. It would be normal to set this flag in
9300 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9301 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9302 This is the outermost level of a deduction, and PARM can be more CV
9303 qualified at this point.
9304 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9305 This is the outermost level of a deduction, and PARM can be less CV
9306 qualified at this point.
9307 UNIFY_ALLOW_MAX_CORRECTION:
9308 This is an INTEGER_TYPE's maximum value. Used if the range may
9309 have been derived from a size specification, such as an array size.
9310 If the size was given by a nontype template parameter N, the maximum
9311 value will have the form N-1. The flag says that we can (and indeed
9312 must) unify N with (ARG + 1), an exception to the normal rules on
9313 folding PARM. */
9315 static int
9316 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
9318 int idx;
9319 tree targ;
9320 tree tparm;
9321 int strict_in = strict;
9323 /* I don't think this will do the right thing with respect to types.
9324 But the only case I've seen it in so far has been array bounds, where
9325 signedness is the only information lost, and I think that will be
9326 okay. */
9327 while (TREE_CODE (parm) == NOP_EXPR)
9328 parm = TREE_OPERAND (parm, 0);
9330 if (arg == error_mark_node)
9331 return 1;
9332 if (arg == unknown_type_node)
9333 /* We can't deduce anything from this, but we might get all the
9334 template args from other function args. */
9335 return 0;
9337 /* If PARM uses template parameters, then we can't bail out here,
9338 even if ARG == PARM, since we won't record unifications for the
9339 template parameters. We might need them if we're trying to
9340 figure out which of two things is more specialized. */
9341 if (arg == parm && !uses_template_parms (parm))
9342 return 0;
9344 /* Immediately reject some pairs that won't unify because of
9345 cv-qualification mismatches. */
9346 if (TREE_CODE (arg) == TREE_CODE (parm)
9347 && TYPE_P (arg)
9348 /* It is the elements of the array which hold the cv quals of an array
9349 type, and the elements might be template type parms. We'll check
9350 when we recurse. */
9351 && TREE_CODE (arg) != ARRAY_TYPE
9352 /* We check the cv-qualifiers when unifying with template type
9353 parameters below. We want to allow ARG `const T' to unify with
9354 PARM `T' for example, when computing which of two templates
9355 is more specialized, for example. */
9356 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9357 && !check_cv_quals_for_unify (strict_in, arg, parm))
9358 return 1;
9360 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9361 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9362 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9363 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9364 strict &= ~UNIFY_ALLOW_DERIVED;
9365 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9366 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9367 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
9369 switch (TREE_CODE (parm))
9371 case TYPENAME_TYPE:
9372 case SCOPE_REF:
9373 case UNBOUND_CLASS_TEMPLATE:
9374 /* In a type which contains a nested-name-specifier, template
9375 argument values cannot be deduced for template parameters used
9376 within the nested-name-specifier. */
9377 return 0;
9379 case TEMPLATE_TYPE_PARM:
9380 case TEMPLATE_TEMPLATE_PARM:
9381 case BOUND_TEMPLATE_TEMPLATE_PARM:
9382 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9384 if (TEMPLATE_TYPE_LEVEL (parm)
9385 != template_decl_level (tparm))
9386 /* The PARM is not one we're trying to unify. Just check
9387 to see if it matches ARG. */
9388 return (TREE_CODE (arg) == TREE_CODE (parm)
9389 && same_type_p (parm, arg)) ? 0 : 1;
9390 idx = TEMPLATE_TYPE_IDX (parm);
9391 targ = TREE_VEC_ELT (targs, idx);
9392 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9394 /* Check for mixed types and values. */
9395 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9396 && TREE_CODE (tparm) != TYPE_DECL)
9397 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9398 && TREE_CODE (tparm) != TEMPLATE_DECL))
9399 return 1;
9401 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9403 /* ARG must be constructed from a template class or a template
9404 template parameter. */
9405 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9406 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9407 return 1;
9410 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9411 tree parmvec = TYPE_TI_ARGS (parm);
9412 tree argvec = TYPE_TI_ARGS (arg);
9413 tree argtmplvec
9414 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9415 int i;
9417 /* The parameter and argument roles have to be switched here
9418 in order to handle default arguments properly. For example,
9419 template<template <class> class TT> void f(TT<int>)
9420 should be able to accept vector<int> which comes from
9421 template <class T, class Allocator = allocator>
9422 class vector. */
9424 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9425 == error_mark_node)
9426 return 1;
9428 /* Deduce arguments T, i from TT<T> or TT<i>.
9429 We check each element of PARMVEC and ARGVEC individually
9430 rather than the whole TREE_VEC since they can have
9431 different number of elements. */
9433 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9435 tree t = TREE_VEC_ELT (parmvec, i);
9437 if (unify (tparms, targs, t,
9438 TREE_VEC_ELT (argvec, i),
9439 UNIFY_ALLOW_NONE))
9440 return 1;
9443 arg = TYPE_TI_TEMPLATE (arg);
9445 /* Fall through to deduce template name. */
9448 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9449 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9451 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9453 /* Simple cases: Value already set, does match or doesn't. */
9454 if (targ != NULL_TREE && template_args_equal (targ, arg))
9455 return 0;
9456 else if (targ)
9457 return 1;
9459 else
9461 /* If ARG is an offset type, we're trying to unify '*T' with
9462 'U C::*', which is ill-formed. See the comment in the
9463 POINTER_TYPE case about this ugliness. */
9464 if (TREE_CODE (arg) == OFFSET_TYPE)
9465 return 1;
9467 /* If PARM is `const T' and ARG is only `int', we don't have
9468 a match unless we are allowing additional qualification.
9469 If ARG is `const int' and PARM is just `T' that's OK;
9470 that binds `const int' to `T'. */
9471 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9472 arg, parm))
9473 return 1;
9475 /* Consider the case where ARG is `const volatile int' and
9476 PARM is `const T'. Then, T should be `volatile int'. */
9477 arg = cp_build_qualified_type_real
9478 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9479 if (arg == error_mark_node)
9480 return 1;
9482 /* Simple cases: Value already set, does match or doesn't. */
9483 if (targ != NULL_TREE && same_type_p (targ, arg))
9484 return 0;
9485 else if (targ)
9486 return 1;
9488 /* Make sure that ARG is not a variable-sized array. (Note
9489 that were talking about variable-sized arrays (like
9490 `int[n]'), rather than arrays of unknown size (like
9491 `int[]').) We'll get very confused by such a type since
9492 the bound of the array will not be computable in an
9493 instantiation. Besides, such types are not allowed in
9494 ISO C++, so we can do as we please here. */
9495 if (variably_modified_type_p (arg))
9496 return 1;
9499 TREE_VEC_ELT (targs, idx) = arg;
9500 return 0;
9502 case TEMPLATE_PARM_INDEX:
9503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9505 if (TEMPLATE_PARM_LEVEL (parm)
9506 != template_decl_level (tparm))
9507 /* The PARM is not one we're trying to unify. Just check
9508 to see if it matches ARG. */
9509 return !(TREE_CODE (arg) == TREE_CODE (parm)
9510 && cp_tree_equal (parm, arg));
9512 idx = TEMPLATE_PARM_IDX (parm);
9513 targ = TREE_VEC_ELT (targs, idx);
9515 if (targ)
9516 return !cp_tree_equal (targ, arg);
9518 /* [temp.deduct.type] If, in the declaration of a function template
9519 with a non-type template-parameter, the non-type
9520 template-parameter is used in an expression in the function
9521 parameter-list and, if the corresponding template-argument is
9522 deduced, the template-argument type shall match the type of the
9523 template-parameter exactly, except that a template-argument
9524 deduced from an array bound may be of any integral type.
9525 The non-type parameter might use already deduced type parameters. */
9526 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9527 if (!TREE_TYPE (arg))
9528 /* Template-parameter dependent expression. Just accept it for now.
9529 It will later be processed in convert_template_argument. */
9531 else if (same_type_p (TREE_TYPE (arg), tparm))
9532 /* OK */;
9533 else if ((strict & UNIFY_ALLOW_INTEGER)
9534 && (TREE_CODE (tparm) == INTEGER_TYPE
9535 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9536 /* OK */;
9537 else if (uses_template_parms (tparm))
9538 /* We haven't deduced the type of this parameter yet. Try again
9539 later. */
9540 return 0;
9541 else
9542 return 1;
9544 TREE_VEC_ELT (targs, idx) = arg;
9545 return 0;
9547 case POINTER_TYPE:
9549 if (TREE_CODE (arg) != POINTER_TYPE)
9550 return 1;
9552 /* [temp.deduct.call]
9554 A can be another pointer or pointer to member type that can
9555 be converted to the deduced A via a qualification
9556 conversion (_conv.qual_).
9558 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9559 This will allow for additional cv-qualification of the
9560 pointed-to types if appropriate. */
9562 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9563 /* The derived-to-base conversion only persists through one
9564 level of pointers. */
9565 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9567 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
9568 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
9570 /* Avoid getting confused about cv-quals; don't recurse here.
9571 Pointers to members should really be just OFFSET_TYPE, not
9572 this two-level nonsense... */
9574 parm = TREE_TYPE (parm);
9575 arg = TREE_TYPE (arg);
9576 goto offset;
9579 return unify (tparms, targs, TREE_TYPE (parm),
9580 TREE_TYPE (arg), strict);
9583 case REFERENCE_TYPE:
9584 if (TREE_CODE (arg) != REFERENCE_TYPE)
9585 return 1;
9586 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9587 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9589 case ARRAY_TYPE:
9590 if (TREE_CODE (arg) != ARRAY_TYPE)
9591 return 1;
9592 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9593 != (TYPE_DOMAIN (arg) == NULL_TREE))
9594 return 1;
9595 if (TYPE_DOMAIN (parm) != NULL_TREE
9596 && unify (tparms, targs, TYPE_DOMAIN (parm),
9597 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
9598 return 1;
9599 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9600 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9602 case REAL_TYPE:
9603 case COMPLEX_TYPE:
9604 case VECTOR_TYPE:
9605 case INTEGER_TYPE:
9606 case BOOLEAN_TYPE:
9607 case VOID_TYPE:
9608 if (TREE_CODE (arg) != TREE_CODE (parm))
9609 return 1;
9611 if (TREE_CODE (parm) == INTEGER_TYPE
9612 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
9614 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
9615 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
9616 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
9617 return 1;
9618 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
9619 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
9620 TYPE_MAX_VALUE (arg),
9621 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
9622 return 1;
9624 /* We have already checked cv-qualification at the top of the
9625 function. */
9626 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9627 return 1;
9629 /* As far as unification is concerned, this wins. Later checks
9630 will invalidate it if necessary. */
9631 return 0;
9633 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9634 /* Type INTEGER_CST can come from ordinary constant template args. */
9635 case INTEGER_CST:
9636 while (TREE_CODE (arg) == NOP_EXPR)
9637 arg = TREE_OPERAND (arg, 0);
9639 if (TREE_CODE (arg) != INTEGER_CST)
9640 return 1;
9641 return !tree_int_cst_equal (parm, arg);
9643 case TREE_VEC:
9645 int i;
9646 if (TREE_CODE (arg) != TREE_VEC)
9647 return 1;
9648 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9649 return 1;
9650 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
9651 if (unify (tparms, targs,
9652 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
9653 UNIFY_ALLOW_NONE))
9654 return 1;
9655 return 0;
9658 case RECORD_TYPE:
9659 case UNION_TYPE:
9660 if (TREE_CODE (arg) != TREE_CODE (parm))
9661 return 1;
9663 if (TYPE_PTRMEMFUNC_P (parm))
9665 if (!TYPE_PTRMEMFUNC_P (arg))
9666 return 1;
9668 return unify (tparms, targs,
9669 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9670 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9671 strict);
9674 if (CLASSTYPE_TEMPLATE_INFO (parm))
9676 tree t = NULL_TREE;
9678 if (strict_in & UNIFY_ALLOW_DERIVED)
9680 /* First, we try to unify the PARM and ARG directly. */
9681 t = try_class_unification (tparms, targs,
9682 parm, arg);
9684 if (!t)
9686 /* Fallback to the special case allowed in
9687 [temp.deduct.call]:
9689 If P is a class, and P has the form
9690 template-id, then A can be a derived class of
9691 the deduced A. Likewise, if P is a pointer to
9692 a class of the form template-id, A can be a
9693 pointer to a derived class pointed to by the
9694 deduced A. */
9695 t = get_template_base (tparms, targs,
9696 parm, arg);
9698 if (! t || t == error_mark_node)
9699 return 1;
9702 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9703 && (CLASSTYPE_TI_TEMPLATE (parm)
9704 == CLASSTYPE_TI_TEMPLATE (arg)))
9705 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9706 Then, we should unify `int' and `U'. */
9707 t = arg;
9708 else
9709 /* There's no chance of unification succeeding. */
9710 return 1;
9712 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
9713 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
9715 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
9716 return 1;
9717 return 0;
9719 case METHOD_TYPE:
9720 case FUNCTION_TYPE:
9721 if (TREE_CODE (arg) != TREE_CODE (parm))
9722 return 1;
9724 if (unify (tparms, targs, TREE_TYPE (parm),
9725 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
9726 return 1;
9727 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
9728 TYPE_ARG_TYPES (arg), 1,
9729 DEDUCE_EXACT, 0, -1);
9731 case OFFSET_TYPE:
9732 offset:
9733 if (TREE_CODE (arg) != OFFSET_TYPE)
9734 return 1;
9735 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
9736 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
9737 return 1;
9738 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9739 strict);
9741 case CONST_DECL:
9742 if (DECL_TEMPLATE_PARM_P (parm))
9743 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
9744 if (arg != decl_constant_value (parm))
9745 return 1;
9746 return 0;
9748 case TEMPLATE_DECL:
9749 /* Matched cases are handled by the ARG == PARM test above. */
9750 return 1;
9752 case MINUS_EXPR:
9753 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9754 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
9756 /* We handle this case specially, since it comes up with
9757 arrays. In particular, something like:
9759 template <int N> void f(int (&x)[N]);
9761 Here, we are trying to unify the range type, which
9762 looks like [0 ... (N - 1)]. */
9763 tree t, t1, t2;
9764 t1 = TREE_OPERAND (parm, 0);
9765 t2 = TREE_OPERAND (parm, 1);
9767 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
9769 return unify (tparms, targs, t1, t, strict);
9771 /* else fall through */
9773 default:
9774 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
9777 /* We're looking at an expression. This can happen with
9778 something like:
9780 template <int I>
9781 void foo(S<I>, S<I + 2>);
9783 This is a "nondeduced context":
9785 [deduct.type]
9787 The nondeduced contexts are:
9789 --A type that is a template-id in which one or more of
9790 the template-arguments is an expression that references
9791 a template-parameter.
9793 In these cases, we assume deduction succeeded, but don't
9794 actually infer any unifications. */
9796 if (!uses_template_parms (parm)
9797 && !template_args_equal (parm, arg))
9798 return 1;
9799 else
9800 return 0;
9802 else
9803 sorry ("use of `%s' in template type unification",
9804 tree_code_name [(int) TREE_CODE (parm)]);
9806 return 1;
9810 /* Called if RESULT is explicitly instantiated, or is a member of an
9811 explicitly instantiated class, or if using -frepo and the
9812 instantiation of RESULT has been assigned to this file. */
9814 void
9815 mark_decl_instantiated (tree result, int extern_p)
9817 /* We used to set this unconditionally; we moved that to
9818 do_decl_instantiation so it wouldn't get set on members of
9819 explicit class template instantiations. But we still need to set
9820 it here for the 'extern template' case in order to suppress
9821 implicit instantiations. */
9822 if (extern_p)
9823 SET_DECL_EXPLICIT_INSTANTIATION (result);
9825 /* If this entity has already been written out, it's too late to
9826 make any modifications. */
9827 if (TREE_ASM_WRITTEN (result))
9828 return;
9830 if (TREE_CODE (result) != FUNCTION_DECL)
9831 /* The TREE_PUBLIC flag for function declarations will have been
9832 set correctly by tsubst. */
9833 TREE_PUBLIC (result) = 1;
9835 /* This might have been set by an earlier implicit instantiation. */
9836 DECL_COMDAT (result) = 0;
9838 if (! extern_p)
9840 DECL_INTERFACE_KNOWN (result) = 1;
9841 DECL_NOT_REALLY_EXTERN (result) = 1;
9843 /* Always make artificials weak. */
9844 if (DECL_ARTIFICIAL (result) && flag_weak)
9845 comdat_linkage (result);
9846 /* For WIN32 we also want to put explicit instantiations in
9847 linkonce sections. */
9848 else if (TREE_PUBLIC (result))
9849 maybe_make_one_only (result);
9852 if (TREE_CODE (result) == FUNCTION_DECL)
9853 defer_fn (result);
9856 /* Given two function templates PAT1 and PAT2, return:
9858 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9860 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9861 -1 if PAT2 is more specialized than PAT1.
9862 0 if neither is more specialized.
9864 LEN is passed through to fn_type_unification. */
9867 more_specialized (tree pat1, tree pat2, int deduce, int len)
9869 tree targs;
9870 int winner = 0;
9872 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9873 NULL_TREE, 0, deduce, len);
9874 if (targs)
9875 --winner;
9877 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9878 NULL_TREE, 0, deduce, len);
9879 if (targs)
9880 ++winner;
9882 return winner;
9885 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9887 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9888 -1 if PAT2 is more specialized than PAT1.
9889 0 if neither is more specialized. */
9892 more_specialized_class (tree pat1, tree pat2)
9894 tree targs;
9895 int winner = 0;
9897 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9898 TREE_PURPOSE (pat2));
9899 if (targs)
9900 --winner;
9902 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9903 TREE_PURPOSE (pat1));
9904 if (targs)
9905 ++winner;
9907 return winner;
9910 /* Return the template arguments that will produce the function signature
9911 DECL from the function template FN, with the explicit template
9912 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9913 also match. Return NULL_TREE if no satisfactory arguments could be
9914 found. DEDUCE and LEN are passed through to fn_type_unification. */
9916 static tree
9917 get_bindings_real (tree fn,
9918 tree decl,
9919 tree explicit_args,
9920 int check_rettype,
9921 int deduce,
9922 int len)
9924 int ntparms = DECL_NTPARMS (fn);
9925 tree targs = make_tree_vec (ntparms);
9926 tree decl_type;
9927 tree decl_arg_types;
9928 int i;
9930 /* Substitute the explicit template arguments into the type of DECL.
9931 The call to fn_type_unification will handle substitution into the
9932 FN. */
9933 decl_type = TREE_TYPE (decl);
9934 if (explicit_args && uses_template_parms (decl_type))
9936 tree tmpl;
9937 tree converted_args;
9939 if (DECL_TEMPLATE_INFO (decl))
9940 tmpl = DECL_TI_TEMPLATE (decl);
9941 else
9942 /* We can get here for some invalid specializations. */
9943 return NULL_TREE;
9945 converted_args
9946 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9947 explicit_args, NULL_TREE,
9948 tf_none, /*require_all_arguments=*/0));
9949 if (converted_args == error_mark_node)
9950 return NULL_TREE;
9952 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
9953 if (decl_type == error_mark_node)
9954 return NULL_TREE;
9957 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9958 /* Never do unification on the 'this' parameter. */
9959 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9960 decl_arg_types = TREE_CHAIN (decl_arg_types);
9962 i = fn_type_unification (fn, explicit_args, targs,
9963 decl_arg_types,
9964 (check_rettype || DECL_CONV_FN_P (fn)
9965 ? TREE_TYPE (decl_type) : NULL_TREE),
9966 deduce, len);
9968 if (i != 0)
9969 return NULL_TREE;
9971 return targs;
9974 /* For most uses, we want to check the return type. */
9976 tree
9977 get_bindings (tree fn, tree decl, tree explicit_args)
9979 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9982 /* But for resolve_overloaded_unification, we only care about the parameter
9983 types. */
9985 static tree
9986 get_bindings_overload (tree fn, tree decl, tree explicit_args)
9988 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9991 /* Return the innermost template arguments that, when applied to a
9992 template specialization whose innermost template parameters are
9993 TPARMS, and whose specialization arguments are PARMS, yield the
9994 ARGS.
9996 For example, suppose we have:
9998 template <class T, class U> struct S {};
9999 template <class T> struct S<T*, int> {};
10001 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10002 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10003 int}. The resulting vector will be {double}, indicating that `T'
10004 is bound to `double'. */
10006 static tree
10007 get_class_bindings (tree tparms, tree parms, tree args)
10009 int i, ntparms = TREE_VEC_LENGTH (tparms);
10010 tree vec = make_tree_vec (ntparms);
10012 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10013 UNIFY_ALLOW_NONE))
10014 return NULL_TREE;
10016 for (i = 0; i < ntparms; ++i)
10017 if (! TREE_VEC_ELT (vec, i))
10018 return NULL_TREE;
10020 if (verify_class_unification (vec, parms, args))
10021 return NULL_TREE;
10023 return vec;
10026 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10027 Pick the most specialized template, and return the corresponding
10028 instantiation, or if there is no corresponding instantiation, the
10029 template itself. If there is no most specialized template,
10030 error_mark_node is returned. If there are no templates at all,
10031 NULL_TREE is returned. */
10033 tree
10034 most_specialized_instantiation (tree instantiations)
10036 tree fn, champ;
10037 int fate;
10039 if (!instantiations)
10040 return NULL_TREE;
10042 champ = instantiations;
10043 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10045 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10046 DEDUCE_EXACT, -1);
10047 if (fate == 1)
10049 else
10051 if (fate == 0)
10053 fn = TREE_CHAIN (fn);
10054 if (! fn)
10055 return error_mark_node;
10057 champ = fn;
10061 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10063 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10064 DEDUCE_EXACT, -1);
10065 if (fate != 1)
10066 return error_mark_node;
10069 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10072 /* Return the most specialized of the list of templates in FNS that can
10073 produce an instantiation matching DECL, given the explicit template
10074 arguments EXPLICIT_ARGS. */
10076 static tree
10077 most_specialized (tree fns, tree decl, tree explicit_args)
10079 tree candidates = NULL_TREE;
10080 tree fn, args;
10082 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10084 tree candidate = TREE_VALUE (fn);
10086 args = get_bindings (candidate, decl, explicit_args);
10087 if (args)
10088 candidates = tree_cons (NULL_TREE, candidate, candidates);
10091 return most_specialized_instantiation (candidates);
10094 /* If DECL is a specialization of some template, return the most
10095 general such template. Otherwise, returns NULL_TREE.
10097 For example, given:
10099 template <class T> struct S { template <class U> void f(U); };
10101 if TMPL is `template <class U> void S<int>::f(U)' this will return
10102 the full template. This function will not trace past partial
10103 specializations, however. For example, given in addition:
10105 template <class T> struct S<T*> { template <class U> void f(U); };
10107 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10108 `template <class T> template <class U> S<T*>::f(U)'. */
10110 tree
10111 most_general_template (tree decl)
10113 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10114 an immediate specialization. */
10115 if (TREE_CODE (decl) == FUNCTION_DECL)
10117 if (DECL_TEMPLATE_INFO (decl)) {
10118 decl = DECL_TI_TEMPLATE (decl);
10120 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10121 template friend. */
10122 if (TREE_CODE (decl) != TEMPLATE_DECL)
10123 return NULL_TREE;
10124 } else
10125 return NULL_TREE;
10128 /* Look for more and more general templates. */
10129 while (DECL_TEMPLATE_INFO (decl))
10131 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10132 in some cases. (See cp-tree.h for details.) */
10133 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10134 break;
10136 if (CLASS_TYPE_P (TREE_TYPE (decl))
10137 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10138 break;
10140 /* Stop if we run into an explicitly specialized class template. */
10141 if (!DECL_NAMESPACE_SCOPE_P (decl)
10142 && DECL_CONTEXT (decl)
10143 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10144 break;
10146 decl = DECL_TI_TEMPLATE (decl);
10149 return decl;
10152 /* Return the most specialized of the class template specializations
10153 of TMPL which can produce an instantiation matching ARGS, or
10154 error_mark_node if the choice is ambiguous. */
10156 static tree
10157 most_specialized_class (tree tmpl, tree args)
10159 tree list = NULL_TREE;
10160 tree t;
10161 tree champ;
10162 int fate;
10164 tmpl = most_general_template (tmpl);
10165 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10167 tree spec_args
10168 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10169 if (spec_args)
10171 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10172 TREE_TYPE (list) = TREE_TYPE (t);
10176 if (! list)
10177 return NULL_TREE;
10179 t = list;
10180 champ = t;
10181 t = TREE_CHAIN (t);
10182 for (; t; t = TREE_CHAIN (t))
10184 fate = more_specialized_class (champ, t);
10185 if (fate == 1)
10187 else
10189 if (fate == 0)
10191 t = TREE_CHAIN (t);
10192 if (! t)
10193 return error_mark_node;
10195 champ = t;
10199 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10201 fate = more_specialized_class (champ, t);
10202 if (fate != 1)
10203 return error_mark_node;
10206 return champ;
10209 /* Explicitly instantiate DECL. */
10211 void
10212 do_decl_instantiation (tree decl, tree storage)
10214 tree result = NULL_TREE;
10215 int extern_p = 0;
10217 if (!decl)
10218 /* An error occurred, for which grokdeclarator has already issued
10219 an appropriate message. */
10220 return;
10221 else if (! DECL_LANG_SPECIFIC (decl))
10223 error ("explicit instantiation of non-template `%#D'", decl);
10224 return;
10226 else if (TREE_CODE (decl) == VAR_DECL)
10228 /* There is an asymmetry here in the way VAR_DECLs and
10229 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10230 the latter, the DECL we get back will be marked as a
10231 template instantiation, and the appropriate
10232 DECL_TEMPLATE_INFO will be set up. This does not happen for
10233 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10234 should handle VAR_DECLs as it currently handles
10235 FUNCTION_DECLs. */
10236 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10237 if (!result || TREE_CODE (result) != VAR_DECL)
10239 error ("no matching template for `%D' found", decl);
10240 return;
10243 else if (TREE_CODE (decl) != FUNCTION_DECL)
10245 error ("explicit instantiation of `%#D'", decl);
10246 return;
10248 else
10249 result = decl;
10251 /* Check for various error cases. Note that if the explicit
10252 instantiation is valid the RESULT will currently be marked as an
10253 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10254 until we get here. */
10256 if (DECL_TEMPLATE_SPECIALIZATION (result))
10258 /* DR 259 [temp.spec].
10260 Both an explicit instantiation and a declaration of an explicit
10261 specialization shall not appear in a program unless the explicit
10262 instantiation follows a declaration of the explicit specialization.
10264 For a given set of template parameters, if an explicit
10265 instantiation of a template appears after a declaration of an
10266 explicit specialization for that template, the explicit
10267 instantiation has no effect. */
10268 return;
10270 else if (DECL_EXPLICIT_INSTANTIATION (result))
10272 /* [temp.spec]
10274 No program shall explicitly instantiate any template more
10275 than once.
10277 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10278 instantiation was `extern' and the second is not, and EXTERN_P for
10279 the opposite case. If -frepo, chances are we already got marked
10280 as an explicit instantiation because of the repo file. */
10281 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
10282 pedwarn ("duplicate explicit instantiation of `%#D'", result);
10284 /* If we've already instantiated the template, just return now. */
10285 if (DECL_INTERFACE_KNOWN (result))
10286 return;
10288 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10290 error ("no matching template for `%D' found", result);
10291 return;
10293 else if (!DECL_TEMPLATE_INFO (result))
10295 pedwarn ("explicit instantiation of non-template `%#D'", result);
10296 return;
10299 if (flag_external_templates)
10300 return;
10302 if (storage == NULL_TREE)
10304 else if (storage == ridpointers[(int) RID_EXTERN])
10306 if (pedantic && !in_system_header)
10307 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10308 extern_p = 1;
10310 else
10311 error ("storage class `%D' applied to template instantiation",
10312 storage);
10314 SET_DECL_EXPLICIT_INSTANTIATION (result);
10315 mark_decl_instantiated (result, extern_p);
10316 repo_template_instantiated (result, extern_p);
10317 if (! extern_p)
10318 instantiate_decl (result, /*defer_ok=*/1);
10321 void
10322 mark_class_instantiated (tree t, int extern_p)
10324 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10325 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10326 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10327 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10328 if (! extern_p)
10330 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10331 rest_of_type_compilation (t, 1);
10335 /* Called from do_type_instantiation through binding_table_foreach to
10336 do recursive instantiation for the type bound in ENTRY. */
10337 static void
10338 bt_instantiate_type_proc (binding_entry entry, void *data)
10340 tree storage = *(tree *) data;
10342 if (IS_AGGR_TYPE (entry->type)
10343 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
10344 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
10347 /* Perform an explicit instantiation of template class T. STORAGE, if
10348 non-null, is the RID for extern, inline or static. COMPLAIN is
10349 nonzero if this is called from the parser, zero if called recursively,
10350 since the standard is unclear (as detailed below). */
10352 void
10353 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
10355 int extern_p = 0;
10356 int nomem_p = 0;
10357 int static_p = 0;
10359 if (TREE_CODE (t) == TYPE_DECL)
10360 t = TREE_TYPE (t);
10362 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10364 error ("explicit instantiation of non-template type `%T'", t);
10365 return;
10368 complete_type (t);
10370 /* With -fexternal-templates, explicit instantiations are treated the same
10371 as implicit ones. */
10372 if (flag_external_templates)
10373 return;
10375 if (!COMPLETE_TYPE_P (t))
10377 if (complain & tf_error)
10378 error ("explicit instantiation of `%#T' before definition of template",
10380 return;
10383 if (storage != NULL_TREE)
10385 if (pedantic && !in_system_header)
10386 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
10387 IDENTIFIER_POINTER (storage));
10389 if (storage == ridpointers[(int) RID_INLINE])
10390 nomem_p = 1;
10391 else if (storage == ridpointers[(int) RID_EXTERN])
10392 extern_p = 1;
10393 else if (storage == ridpointers[(int) RID_STATIC])
10394 static_p = 1;
10395 else
10397 error ("storage class `%D' applied to template instantiation",
10398 storage);
10399 extern_p = 0;
10403 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10405 /* DR 259 [temp.spec].
10407 Both an explicit instantiation and a declaration of an explicit
10408 specialization shall not appear in a program unless the explicit
10409 instantiation follows a declaration of the explicit specialization.
10411 For a given set of template parameters, if an explicit
10412 instantiation of a template appears after a declaration of an
10413 explicit specialization for that template, the explicit
10414 instantiation has no effect. */
10415 return;
10417 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10419 /* [temp.spec]
10421 No program shall explicitly instantiate any template more
10422 than once.
10424 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10425 was `extern'. If EXTERN_P then the second is. If -frepo, chances
10426 are we already got marked as an explicit instantiation because of the
10427 repo file. All these cases are OK. */
10428 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
10429 && (complain & tf_error))
10430 pedwarn ("duplicate explicit instantiation of `%#T'", t);
10432 /* If we've already instantiated the template, just return now. */
10433 if (!CLASSTYPE_INTERFACE_ONLY (t))
10434 return;
10437 mark_class_instantiated (t, extern_p);
10438 repo_template_instantiated (t, extern_p);
10440 if (nomem_p)
10441 return;
10444 tree tmp;
10446 /* In contrast to implicit instantiation, where only the
10447 declarations, and not the definitions, of members are
10448 instantiated, we have here:
10450 [temp.explicit]
10452 The explicit instantiation of a class template specialization
10453 implies the instantiation of all of its members not
10454 previously explicitly specialized in the translation unit
10455 containing the explicit instantiation.
10457 Of course, we can't instantiate member template classes, since
10458 we don't have any arguments for them. Note that the standard
10459 is unclear on whether the instantiation of the members are
10460 *explicit* instantiations or not. We choose to be generous,
10461 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10462 the explicit instantiation of a class where some of the members
10463 have no definition in the current translation unit. */
10465 if (! static_p)
10466 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10467 if (TREE_CODE (tmp) == FUNCTION_DECL
10468 && DECL_TEMPLATE_INSTANTIATION (tmp))
10470 mark_decl_instantiated (tmp, extern_p);
10471 repo_template_instantiated (tmp, extern_p);
10472 if (! extern_p)
10473 instantiate_decl (tmp, /*defer_ok=*/1);
10476 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10477 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10479 mark_decl_instantiated (tmp, extern_p);
10480 repo_template_instantiated (tmp, extern_p);
10481 if (! extern_p)
10482 instantiate_decl (tmp, /*defer_ok=*/1);
10485 if (CLASSTYPE_NESTED_UTDS (t))
10486 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
10487 bt_instantiate_type_proc, &storage);
10491 /* Given a function DECL, which is a specialization of TMPL, modify
10492 DECL to be a re-instantiation of TMPL with the same template
10493 arguments. TMPL should be the template into which tsubst'ing
10494 should occur for DECL, not the most general template.
10496 One reason for doing this is a scenario like this:
10498 template <class T>
10499 void f(const T&, int i);
10501 void g() { f(3, 7); }
10503 template <class T>
10504 void f(const T& t, const int i) { }
10506 Note that when the template is first instantiated, with
10507 instantiate_template, the resulting DECL will have no name for the
10508 first parameter, and the wrong type for the second. So, when we go
10509 to instantiate the DECL, we regenerate it. */
10511 static void
10512 regenerate_decl_from_template (tree decl, tree tmpl)
10514 /* The most general version of TMPL. */
10515 tree gen_tmpl;
10516 /* The arguments used to instantiate DECL, from the most general
10517 template. */
10518 tree args;
10519 tree code_pattern;
10520 tree new_decl;
10521 int unregistered;
10523 args = DECL_TI_ARGS (decl);
10524 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10526 /* Unregister the specialization so that when we tsubst we will not
10527 just return DECL. We don't have to unregister DECL from TMPL
10528 because if would only be registered there if it were a partial
10529 instantiation of a specialization, which it isn't: it's a full
10530 instantiation. */
10531 gen_tmpl = most_general_template (tmpl);
10532 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl));
10533 unregistered = unregister_specialization (decl, gen_tmpl);
10535 /* If the DECL was not unregistered then something peculiar is
10536 happening: we created a specialization but did not call
10537 register_specialization for it. */
10538 my_friendly_assert (unregistered, 0);
10540 /* Do the substitution to get the new declaration. */
10541 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10543 if (TREE_CODE (decl) == VAR_DECL)
10545 /* Set up DECL_INITIAL, since tsubst doesn't. */
10546 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10547 DECL_INITIAL (new_decl) =
10548 tsubst_expr (DECL_INITIAL (code_pattern), args,
10549 tf_error, DECL_TI_TEMPLATE (decl));
10551 else if (TREE_CODE (decl) == FUNCTION_DECL)
10553 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10554 new decl. */
10555 DECL_INITIAL (new_decl) = error_mark_node;
10556 /* And don't complain about a duplicate definition. */
10557 DECL_INITIAL (decl) = NULL_TREE;
10560 pop_access_scope (decl);
10562 /* The immediate parent of the new template is still whatever it was
10563 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10564 general template. We also reset the DECL_ASSEMBLER_NAME since
10565 tsubst always calculates the name as if the function in question
10566 were really a template instance, and sometimes, with friend
10567 functions, this is not so. See tsubst_friend_function for
10568 details. */
10569 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10570 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10571 COPY_DECL_RTL (decl, new_decl);
10572 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10574 /* Call duplicate decls to merge the old and new declarations. */
10575 duplicate_decls (new_decl, decl);
10577 /* Now, re-register the specialization. */
10578 register_specialization (decl, gen_tmpl, args);
10581 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10582 substituted to get DECL. */
10584 tree
10585 template_for_substitution (tree decl)
10587 tree tmpl = DECL_TI_TEMPLATE (decl);
10589 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10590 for the instantiation. This is not always the most general
10591 template. Consider, for example:
10593 template <class T>
10594 struct S { template <class U> void f();
10595 template <> void f<int>(); };
10597 and an instantiation of S<double>::f<int>. We want TD to be the
10598 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10599 while (/* An instantiation cannot have a definition, so we need a
10600 more general template. */
10601 DECL_TEMPLATE_INSTANTIATION (tmpl)
10602 /* We must also deal with friend templates. Given:
10604 template <class T> struct S {
10605 template <class U> friend void f() {};
10608 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10609 so far as the language is concerned, but that's still
10610 where we get the pattern for the instantiation from. On
10611 other hand, if the definition comes outside the class, say:
10613 template <class T> struct S {
10614 template <class U> friend void f();
10616 template <class U> friend void f() {}
10618 we don't need to look any further. That's what the check for
10619 DECL_INITIAL is for. */
10620 || (TREE_CODE (decl) == FUNCTION_DECL
10621 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10622 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10624 /* The present template, TD, should not be a definition. If it
10625 were a definition, we should be using it! Note that we
10626 cannot restructure the loop to just keep going until we find
10627 a template with a definition, since that might go too far if
10628 a specialization was declared, but not defined. */
10629 my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL
10630 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))),
10631 0);
10633 /* Fetch the more general template. */
10634 tmpl = DECL_TI_TEMPLATE (tmpl);
10637 return tmpl;
10640 /* Produce the definition of D, a _DECL generated from a template. If
10641 DEFER_OK is nonzero, then we don't have to actually do the
10642 instantiation now; we just have to do it sometime. */
10644 tree
10645 instantiate_decl (tree d, int defer_ok)
10647 tree tmpl = DECL_TI_TEMPLATE (d);
10648 tree gen_args;
10649 tree args;
10650 tree td;
10651 tree code_pattern;
10652 tree spec;
10653 tree gen_tmpl;
10654 int pattern_defined;
10655 int need_push;
10656 location_t saved_loc = input_location;
10658 /* This function should only be used to instantiate templates for
10659 functions and static member variables. */
10660 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
10661 || TREE_CODE (d) == VAR_DECL, 0);
10663 /* Variables are never deferred; if instantiation is required, they
10664 are instantiated right away. That allows for better code in the
10665 case that an expression refers to the value of the variable --
10666 if the variable has a constant value the referring expression can
10667 take advantage of that fact. */
10668 if (TREE_CODE (d) == VAR_DECL)
10669 defer_ok = 0;
10671 /* Don't instantiate cloned functions. Instead, instantiate the
10672 functions they cloned. */
10673 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10674 d = DECL_CLONED_FUNCTION (d);
10676 if (DECL_TEMPLATE_INSTANTIATED (d))
10677 /* D has already been instantiated. It might seem reasonable to
10678 check whether or not D is an explicit instantiation, and, if so,
10679 stop here. But when an explicit instantiation is deferred
10680 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10681 is set, even though we still need to do the instantiation. */
10682 return d;
10684 /* If we already have a specialization of this declaration, then
10685 there's no reason to instantiate it. Note that
10686 retrieve_specialization gives us both instantiations and
10687 specializations, so we must explicitly check
10688 DECL_TEMPLATE_SPECIALIZATION. */
10689 gen_tmpl = most_general_template (tmpl);
10690 gen_args = DECL_TI_ARGS (d);
10691 spec = retrieve_specialization (gen_tmpl, gen_args);
10692 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10693 return spec;
10695 /* This needs to happen before any tsubsting. */
10696 if (! push_tinst_level (d))
10697 return d;
10699 timevar_push (TV_PARSE);
10701 /* We may be in the middle of deferred access check. Disable
10702 it now. */
10703 push_deferring_access_checks (dk_no_deferred);
10705 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
10706 for the instantiation. */
10707 td = template_for_substitution (d);
10708 code_pattern = DECL_TEMPLATE_RESULT (td);
10710 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
10711 || DECL_TEMPLATE_SPECIALIZATION (td))
10712 /* In the case of a friend template whose definition is provided
10713 outside the class, we may have too many arguments. Drop the
10714 ones we don't need. The same is true for specializations. */
10715 args = get_innermost_template_args
10716 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
10717 else
10718 args = gen_args;
10720 if (TREE_CODE (d) == FUNCTION_DECL)
10721 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
10722 else
10723 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
10725 input_location = DECL_SOURCE_LOCATION (d);
10727 if (pattern_defined)
10729 /* Let the repository code that this template definition is
10730 available.
10732 The repository doesn't need to know about cloned functions
10733 because they never actually show up in the object file. It
10734 does need to know about the clones; those are the symbols
10735 that the linker will be emitting error messages about. */
10736 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10737 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10739 tree t;
10741 for (t = TREE_CHAIN (d);
10742 t && DECL_CLONED_FUNCTION_P (t);
10743 t = TREE_CHAIN (t))
10744 repo_template_used (t);
10746 else
10747 repo_template_used (d);
10749 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10751 if (flag_alt_external_templates)
10753 if (interface_unknown)
10754 warn_if_unknown_interface (d);
10756 else if (DECL_INTERFACE_KNOWN (code_pattern))
10758 DECL_INTERFACE_KNOWN (d) = 1;
10759 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
10761 else
10762 warn_if_unknown_interface (code_pattern);
10765 if (at_eof)
10766 import_export_decl (d);
10769 if (!defer_ok)
10771 /* Recheck the substitutions to obtain any warning messages
10772 about ignoring cv qualifiers. */
10773 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10774 tree type = TREE_TYPE (gen);
10776 /* Make sure that we can see identifiers, and compute access
10777 correctly. D is already the target FUNCTION_DECL with the
10778 right context. */
10779 push_access_scope (d);
10781 if (TREE_CODE (gen) == FUNCTION_DECL)
10783 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
10784 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
10785 tf_error | tf_warning, d);
10786 /* Don't simply tsubst the function type, as that will give
10787 duplicate warnings about poor parameter qualifications.
10788 The function arguments are the same as the decl_arguments
10789 without the top level cv qualifiers. */
10790 type = TREE_TYPE (type);
10792 tsubst (type, gen_args, tf_error | tf_warning, d);
10794 pop_access_scope (d);
10797 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10798 && DECL_INITIAL (d) == NULL_TREE)
10799 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10800 abort ();
10801 /* Reject all external templates except inline functions. */
10802 else if (DECL_INTERFACE_KNOWN (d)
10803 && ! DECL_NOT_REALLY_EXTERN (d)
10804 && ! (TREE_CODE (d) == FUNCTION_DECL
10805 && DECL_INLINE (d)))
10806 goto out;
10807 /* Defer all other templates, unless we have been explicitly
10808 forbidden from doing so. We restore the source position here
10809 because it's used by add_pending_template. */
10810 else if (! pattern_defined || defer_ok)
10812 input_location = saved_loc;
10814 if (at_eof && !pattern_defined
10815 && DECL_EXPLICIT_INSTANTIATION (d))
10816 /* [temp.explicit]
10818 The definition of a non-exported function template, a
10819 non-exported member function template, or a non-exported
10820 member function or static data member of a class template
10821 shall be present in every translation unit in which it is
10822 explicitly instantiated. */
10823 pedwarn
10824 ("explicit instantiation of `%D' but no definition available", d);
10826 add_pending_template (d);
10827 goto out;
10830 need_push = !global_bindings_p ();
10831 if (need_push)
10832 push_to_top_level ();
10834 /* Regenerate the declaration in case the template has been modified
10835 by a subsequent redeclaration. */
10836 regenerate_decl_from_template (d, td);
10838 /* We already set the file and line above. Reset them now in case
10839 they changed as a result of calling
10840 regenerate_decl_from_template. */
10841 input_location = DECL_SOURCE_LOCATION (d);
10843 if (TREE_CODE (d) == VAR_DECL)
10845 /* Clear out DECL_RTL; whatever was there before may not be right
10846 since we've reset the type of the declaration. */
10847 SET_DECL_RTL (d, NULL_RTX);
10849 DECL_IN_AGGR_P (d) = 0;
10850 import_export_decl (d);
10851 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10853 if (DECL_EXTERNAL (d))
10855 /* The fact that this code is executing indicates that:
10857 (1) D is a template static data member, for which a
10858 definition is available.
10860 (2) An implicit or explicit instantiation has occured.
10862 (3) We are not going to emit a definition of the static
10863 data member at this time.
10865 This situation is peculiar, but it occurs on platforms
10866 without weak symbols when performing an implicit
10867 instantiation. There, we cannot implicitly instantiate a
10868 defined static data member in more than one translation
10869 unit, so import_export_decl marks the declaration as
10870 external; we must rely on explicit instantiation. */
10872 else
10874 /* Mark D as instantiated so that recursive calls to
10875 instantiate_decl do not try to instantiate it again. */
10876 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10877 cp_finish_decl (d,
10878 (!DECL_INITIALIZED_IN_CLASS_P (d)
10879 ? DECL_INITIAL (d) : NULL_TREE),
10880 NULL_TREE, 0);
10883 else if (TREE_CODE (d) == FUNCTION_DECL)
10885 htab_t saved_local_specializations;
10886 tree subst_decl;
10887 tree tmpl_parm;
10888 tree spec_parm;
10890 /* Mark D as instantiated so that recursive calls to
10891 instantiate_decl do not try to instantiate it again. */
10892 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10894 /* Save away the current list, in case we are instantiating one
10895 template from within the body of another. */
10896 saved_local_specializations = local_specializations;
10898 /* Set up the list of local specializations. */
10899 local_specializations = htab_create (37,
10900 hash_local_specialization,
10901 eq_local_specializations,
10902 NULL);
10904 /* Set up context. */
10905 import_export_decl (d);
10906 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10908 /* Create substitution entries for the parameters. */
10909 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
10910 tmpl_parm = DECL_ARGUMENTS (subst_decl);
10911 spec_parm = DECL_ARGUMENTS (d);
10912 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
10914 register_local_specialization (spec_parm, tmpl_parm);
10915 spec_parm = skip_artificial_parms_for (d, spec_parm);
10916 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
10918 while (tmpl_parm)
10920 register_local_specialization (spec_parm, tmpl_parm);
10921 tmpl_parm = TREE_CHAIN (tmpl_parm);
10922 spec_parm = TREE_CHAIN (spec_parm);
10924 my_friendly_assert (!spec_parm, 20020813);
10926 /* Substitute into the body of the function. */
10927 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10928 tf_error | tf_warning, tmpl);
10930 /* We don't need the local specializations any more. */
10931 htab_delete (local_specializations);
10932 local_specializations = saved_local_specializations;
10934 /* Finish the function. */
10935 d = finish_function (0);
10936 expand_body (d);
10939 /* We're not deferring instantiation any more. */
10940 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10942 if (need_push)
10943 pop_from_top_level ();
10945 out:
10946 input_location = saved_loc;
10947 pop_deferring_access_checks ();
10948 pop_tinst_level ();
10950 timevar_pop (TV_PARSE);
10952 return d;
10955 /* Run through the list of templates that we wish we could
10956 instantiate, and instantiate any we can. */
10959 instantiate_pending_templates (void)
10961 tree *t;
10962 tree last = NULL_TREE;
10963 int instantiated_something = 0;
10964 int reconsider;
10968 reconsider = 0;
10970 t = &pending_templates;
10971 while (*t)
10973 tree instantiation = TREE_VALUE (*t);
10975 reopen_tinst_level (TREE_PURPOSE (*t));
10977 if (TYPE_P (instantiation))
10979 tree fn;
10981 if (!COMPLETE_TYPE_P (instantiation))
10983 instantiate_class_template (instantiation);
10984 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10985 for (fn = TYPE_METHODS (instantiation);
10987 fn = TREE_CHAIN (fn))
10988 if (! DECL_ARTIFICIAL (fn))
10989 instantiate_decl (fn, /*defer_ok=*/0);
10990 if (COMPLETE_TYPE_P (instantiation))
10992 instantiated_something = 1;
10993 reconsider = 1;
10997 if (COMPLETE_TYPE_P (instantiation))
10998 /* If INSTANTIATION has been instantiated, then we don't
10999 need to consider it again in the future. */
11000 *t = TREE_CHAIN (*t);
11001 else
11003 last = *t;
11004 t = &TREE_CHAIN (*t);
11007 else
11009 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11010 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11012 instantiation = instantiate_decl (instantiation,
11013 /*defer_ok=*/0);
11014 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11016 instantiated_something = 1;
11017 reconsider = 1;
11021 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11022 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11023 /* If INSTANTIATION has been instantiated, then we don't
11024 need to consider it again in the future. */
11025 *t = TREE_CHAIN (*t);
11026 else
11028 last = *t;
11029 t = &TREE_CHAIN (*t);
11032 tinst_depth = 0;
11033 current_tinst_level = NULL_TREE;
11035 last_pending_template = last;
11037 while (reconsider);
11039 return instantiated_something;
11042 /* Substitute ARGVEC into T, which is a list of initializers for
11043 either base class or a non-static data member. The TREE_PURPOSEs
11044 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11045 instantiate_decl. */
11047 static tree
11048 tsubst_initializer_list (tree t, tree argvec)
11050 tree inits = NULL_TREE;
11052 for (; t; t = TREE_CHAIN (t))
11054 tree decl;
11055 tree init;
11056 tree val;
11058 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11059 NULL_TREE);
11060 decl = expand_member_init (decl);
11061 if (decl && !DECL_P (decl))
11062 in_base_initializer = 1;
11064 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11065 NULL_TREE);
11066 if (!init)
11068 else if (TREE_CODE (init) == TREE_LIST)
11069 for (val = init; val; val = TREE_CHAIN (val))
11070 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11071 else if (init != void_type_node)
11072 init = convert_from_reference (init);
11074 in_base_initializer = 0;
11076 if (decl)
11078 init = build_tree_list (decl, init);
11079 TREE_CHAIN (init) = inits;
11080 inits = init;
11083 return inits;
11086 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11088 static void
11089 set_current_access_from_decl (tree decl)
11091 if (TREE_PRIVATE (decl))
11092 current_access_specifier = access_private_node;
11093 else if (TREE_PROTECTED (decl))
11094 current_access_specifier = access_protected_node;
11095 else
11096 current_access_specifier = access_public_node;
11099 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11100 is the instantiation (which should have been created with
11101 start_enum) and ARGS are the template arguments to use. */
11103 static void
11104 tsubst_enum (tree tag, tree newtag, tree args)
11106 tree e;
11108 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11110 tree value;
11112 /* Note that in a template enum, the TREE_VALUE is the
11113 CONST_DECL, not the corresponding INTEGER_CST. */
11114 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
11115 args, tf_error | tf_warning,
11116 NULL_TREE);
11118 /* Give this enumeration constant the correct access. */
11119 set_current_access_from_decl (TREE_VALUE (e));
11121 /* Actually build the enumerator itself. */
11122 build_enumerator (TREE_PURPOSE (e), value, newtag);
11125 finish_enum (newtag);
11126 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11127 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11130 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11131 its type -- but without substituting the innermost set of template
11132 arguments. So, innermost set of template parameters will appear in
11133 the type. */
11135 tree
11136 get_mostly_instantiated_function_type (tree decl)
11138 tree fn_type;
11139 tree tmpl;
11140 tree targs;
11141 tree tparms;
11142 int parm_depth;
11144 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11145 targs = DECL_TI_ARGS (decl);
11146 tparms = DECL_TEMPLATE_PARMS (tmpl);
11147 parm_depth = TMPL_PARMS_DEPTH (tparms);
11149 /* There should be as many levels of arguments as there are levels
11150 of parameters. */
11151 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
11153 fn_type = TREE_TYPE (tmpl);
11155 if (parm_depth == 1)
11156 /* No substitution is necessary. */
11158 else
11160 int i;
11161 tree partial_args;
11163 /* Replace the innermost level of the TARGS with NULL_TREEs to
11164 let tsubst know not to substitute for those parameters. */
11165 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11166 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11167 SET_TMPL_ARGS_LEVEL (partial_args, i,
11168 TMPL_ARGS_LEVEL (targs, i));
11169 SET_TMPL_ARGS_LEVEL (partial_args,
11170 TMPL_ARGS_DEPTH (targs),
11171 make_tree_vec (DECL_NTPARMS (tmpl)));
11173 /* Make sure that we can see identifiers, and compute access
11174 correctly. We can just use the context of DECL for the
11175 partial substitution here. It depends only on outer template
11176 parameters, regardless of whether the innermost level is
11177 specialized or not. */
11178 push_access_scope (decl);
11180 /* Now, do the (partial) substitution to figure out the
11181 appropriate function type. */
11182 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11184 /* Substitute into the template parameters to obtain the real
11185 innermost set of parameters. This step is important if the
11186 innermost set of template parameters contains value
11187 parameters whose types depend on outer template parameters. */
11188 TREE_VEC_LENGTH (partial_args)--;
11189 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11191 pop_access_scope (decl);
11194 return fn_type;
11197 /* Return truthvalue if we're processing a template different from
11198 the last one involved in diagnostics. */
11200 problematic_instantiation_changed (void)
11202 return last_template_error_tick != tinst_level_tick;
11205 /* Remember current template involved in diagnostics. */
11206 void
11207 record_last_problematic_instantiation (void)
11209 last_template_error_tick = tinst_level_tick;
11212 tree
11213 current_instantiation (void)
11215 return current_tinst_level;
11218 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11219 type. Return zero for ok, nonzero for disallowed. Issue error and
11220 warning messages under control of COMPLAIN. */
11222 static int
11223 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
11225 if (INTEGRAL_TYPE_P (type))
11226 return 0;
11227 else if (POINTER_TYPE_P (type))
11228 return 0;
11229 else if (TYPE_PTRMEM_P (type))
11230 return 0;
11231 else if (TYPE_PTRMEMFUNC_P (type))
11232 return 0;
11233 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11234 return 0;
11235 else if (TREE_CODE (type) == TYPENAME_TYPE)
11236 return 0;
11238 if (complain & tf_error)
11239 error ("`%#T' is not a valid type for a template constant parameter",
11240 type);
11241 return 1;
11244 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11245 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11247 static bool
11248 dependent_type_p_r (tree type)
11250 tree scope;
11252 /* [temp.dep.type]
11254 A type is dependent if it is:
11256 -- a template parameter. */
11257 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11258 return true;
11259 /* -- a qualified-id with a nested-name-specifier which contains a
11260 class-name that names a dependent type or whose unqualified-id
11261 names a dependent type. */
11262 if (TREE_CODE (type) == TYPENAME_TYPE)
11263 return true;
11264 /* -- a cv-qualified type where the cv-unqualified type is
11265 dependent. */
11266 type = TYPE_MAIN_VARIANT (type);
11267 /* -- a compound type constructed from any dependent type. */
11268 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
11269 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11270 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11271 (type)));
11272 else if (TREE_CODE (type) == POINTER_TYPE
11273 || TREE_CODE (type) == REFERENCE_TYPE)
11274 return dependent_type_p (TREE_TYPE (type));
11275 else if (TREE_CODE (type) == FUNCTION_TYPE
11276 || TREE_CODE (type) == METHOD_TYPE)
11278 tree arg_type;
11280 if (dependent_type_p (TREE_TYPE (type)))
11281 return true;
11282 for (arg_type = TYPE_ARG_TYPES (type);
11283 arg_type;
11284 arg_type = TREE_CHAIN (arg_type))
11285 if (dependent_type_p (TREE_VALUE (arg_type)))
11286 return true;
11287 return false;
11289 /* -- an array type constructed from any dependent type or whose
11290 size is specified by a constant expression that is
11291 value-dependent. */
11292 if (TREE_CODE (type) == ARRAY_TYPE)
11294 if (TYPE_DOMAIN (type)
11295 && ((value_dependent_expression_p
11296 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11297 || (type_dependent_expression_p
11298 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11299 return true;
11300 return dependent_type_p (TREE_TYPE (type));
11302 /* -- a template-id in which either the template name is a template
11303 parameter or any of the template arguments is a dependent type or
11304 an expression that is type-dependent or value-dependent.
11306 This language seems somewhat confused; for example, it does not
11307 discuss template template arguments. Therefore, we use the
11308 definition for dependent template arguments in [temp.dep.temp]. */
11309 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11310 && (dependent_template_id_p
11311 (CLASSTYPE_TI_TEMPLATE (type),
11312 CLASSTYPE_TI_ARGS (type))))
11313 return true;
11314 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11315 return true;
11316 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11317 expression is not type-dependent, then it should already been
11318 have resolved. */
11319 if (TREE_CODE (type) == TYPEOF_TYPE)
11320 return true;
11321 /* The standard does not specifically mention types that are local
11322 to template functions or local classes, but they should be
11323 considered dependent too. For example:
11325 template <int I> void f() {
11326 enum E { a = I };
11327 S<sizeof (E)> s;
11330 The size of `E' cannot be known until the value of `I' has been
11331 determined. Therefore, `E' must be considered dependent. */
11332 scope = TYPE_CONTEXT (type);
11333 if (scope && TYPE_P (scope))
11334 return dependent_type_p (scope);
11335 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11336 return type_dependent_expression_p (scope);
11338 /* Other types are non-dependent. */
11339 return false;
11342 /* Returns TRUE if TYPE is dependent, in the sense of
11343 [temp.dep.type]. */
11345 bool
11346 dependent_type_p (tree type)
11348 /* If there are no template parameters in scope, then there can't be
11349 any dependent types. */
11350 if (!processing_template_decl)
11351 return false;
11353 /* If the type is NULL, we have not computed a type for the entity
11354 in question; in that case, the type is dependent. */
11355 if (!type)
11356 return true;
11358 /* Erroneous types can be considered non-dependent. */
11359 if (type == error_mark_node)
11360 return false;
11362 /* If we have not already computed the appropriate value for TYPE,
11363 do so now. */
11364 if (!TYPE_DEPENDENT_P_VALID (type))
11366 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11367 TYPE_DEPENDENT_P_VALID (type) = 1;
11370 return TYPE_DEPENDENT_P (type);
11373 /* Returns TRUE if the EXPRESSION is value-dependent. */
11375 bool
11376 value_dependent_expression_p (tree expression)
11378 if (!processing_template_decl)
11379 return false;
11381 /* A name declared with a dependent type. */
11382 if (TREE_CODE (expression) == LOOKUP_EXPR
11383 || (DECL_P (expression)
11384 && type_dependent_expression_p (expression)))
11385 return true;
11386 /* A non-type template parameter. */
11387 if ((TREE_CODE (expression) == CONST_DECL
11388 && DECL_TEMPLATE_PARM_P (expression))
11389 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11390 return true;
11391 /* A constant with integral or enumeration type and is initialized
11392 with an expression that is value-dependent. */
11393 if (TREE_CODE (expression) == VAR_DECL
11394 && DECL_INITIAL (expression)
11395 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
11396 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
11397 && value_dependent_expression_p (DECL_INITIAL (expression)))
11398 return true;
11399 /* These expressions are value-dependent if the type to which the
11400 cast occurs is dependent. */
11401 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11402 || TREE_CODE (expression) == STATIC_CAST_EXPR
11403 || TREE_CODE (expression) == CONST_CAST_EXPR
11404 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11405 || TREE_CODE (expression) == CAST_EXPR)
11406 && dependent_type_p (TREE_TYPE (expression)))
11407 return true;
11408 /* A `sizeof' expression where the sizeof operand is a type is
11409 value-dependent if the type is dependent. If the type was not
11410 dependent, we would no longer have a SIZEOF_EXPR, so any
11411 SIZEOF_EXPR is dependent. */
11412 if (TREE_CODE (expression) == SIZEOF_EXPR)
11413 return true;
11414 /* A constant expression is value-dependent if any subexpression is
11415 value-dependent. */
11416 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
11418 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11420 case '1':
11421 return (value_dependent_expression_p
11422 (TREE_OPERAND (expression, 0)));
11423 case '<':
11424 case '2':
11425 return ((value_dependent_expression_p
11426 (TREE_OPERAND (expression, 0)))
11427 || (value_dependent_expression_p
11428 (TREE_OPERAND (expression, 1))));
11429 case 'e':
11431 int i;
11432 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11433 /* In some cases, some of the operands may be missing.
11434 (For example, in the case of PREDECREMENT_EXPR, the
11435 amount to increment by may be missing.) That doesn't
11436 make the expression dependent. */
11437 if (TREE_OPERAND (expression, i)
11438 && (value_dependent_expression_p
11439 (TREE_OPERAND (expression, i))))
11440 return true;
11441 return false;
11446 /* The expression is not value-dependent. */
11447 return false;
11450 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11451 [temp.dep.expr]. */
11453 bool
11454 type_dependent_expression_p (tree expression)
11456 if (!processing_template_decl)
11457 return false;
11459 /* Some expression forms are never type-dependent. */
11460 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11461 || TREE_CODE (expression) == SIZEOF_EXPR
11462 || TREE_CODE (expression) == ALIGNOF_EXPR
11463 || TREE_CODE (expression) == TYPEID_EXPR
11464 || TREE_CODE (expression) == DELETE_EXPR
11465 || TREE_CODE (expression) == VEC_DELETE_EXPR
11466 || TREE_CODE (expression) == THROW_EXPR)
11467 return false;
11469 /* The types of these expressions depends only on the type to which
11470 the cast occurs. */
11471 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11472 || TREE_CODE (expression) == STATIC_CAST_EXPR
11473 || TREE_CODE (expression) == CONST_CAST_EXPR
11474 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11475 || TREE_CODE (expression) == CAST_EXPR)
11476 return dependent_type_p (TREE_TYPE (expression));
11477 /* The types of these expressions depends only on the type created
11478 by the expression. */
11479 else if (TREE_CODE (expression) == NEW_EXPR
11480 || TREE_CODE (expression) == VEC_NEW_EXPR)
11482 /* For NEW_EXPR tree nodes created inside a template, either
11483 the object type itself or a TREE_LIST may appear as the
11484 operand 1. */
11485 tree type = TREE_OPERAND (expression, 1);
11486 if (TREE_CODE (type) == TREE_LIST)
11487 /* This is an array type. We need to check array dimensions
11488 as well. */
11489 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
11490 || value_dependent_expression_p
11491 (TREE_OPERAND (TREE_VALUE (type), 1));
11492 else
11493 return dependent_type_p (type);
11496 if (TREE_CODE (expression) == FUNCTION_DECL
11497 && DECL_LANG_SPECIFIC (expression)
11498 && DECL_TEMPLATE_INFO (expression)
11499 && (dependent_template_id_p
11500 (DECL_TI_TEMPLATE (expression),
11501 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11502 return true;
11504 return (dependent_type_p (TREE_TYPE (expression)));
11507 /* Returns TRUE if the ARG (a template argument) is dependent. */
11509 bool
11510 dependent_template_arg_p (tree arg)
11512 if (!processing_template_decl)
11513 return false;
11515 if (TREE_CODE (arg) == TEMPLATE_DECL
11516 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11517 return dependent_template_p (arg);
11518 else if (TYPE_P (arg))
11519 return dependent_type_p (arg);
11520 else
11521 return (type_dependent_expression_p (arg)
11522 || value_dependent_expression_p (arg));
11525 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11527 static bool
11528 dependent_template_id_p (tree tmpl, tree args)
11530 int i;
11532 if (dependent_template_p (tmpl))
11533 return true;
11534 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
11535 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
11536 return true;
11537 return false;
11540 /* Returns TRUE if the template TMPL is dependent. */
11542 bool
11543 dependent_template_p (tree tmpl)
11545 /* Template template parameters are dependent. */
11546 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
11547 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
11548 return true;
11549 /* So are member templates of dependent classes. */
11550 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
11551 return dependent_type_p (DECL_CONTEXT (tmpl));
11552 return false;
11555 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11556 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11557 can be found. Note that this function peers inside uninstantiated
11558 templates and therefore should be used only in extremely limited
11559 situations. */
11561 tree
11562 resolve_typename_type (tree type, bool only_current_p)
11564 tree scope;
11565 tree name;
11566 tree decl;
11567 int quals;
11569 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
11570 20010702);
11572 scope = TYPE_CONTEXT (type);
11573 name = TYPE_IDENTIFIER (type);
11575 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11576 it first before we can figure out what NAME refers to. */
11577 if (TREE_CODE (scope) == TYPENAME_TYPE)
11578 scope = resolve_typename_type (scope, only_current_p);
11579 /* If we don't know what SCOPE refers to, then we cannot resolve the
11580 TYPENAME_TYPE. */
11581 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
11582 return error_mark_node;
11583 /* If the SCOPE is a template type parameter, we have no way of
11584 resolving the name. */
11585 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
11586 return type;
11587 /* If the SCOPE is not the current instantiation, there's no reason
11588 to look inside it. */
11589 if (only_current_p && !currently_open_class (scope))
11590 return error_mark_node;
11591 /* If SCOPE is a partial instantiation, it will not have a valid
11592 TYPE_FIELDS list, so use the original template. */
11593 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
11594 /* Enter the SCOPE so that name lookup will be resolved as if we
11595 were in the class definition. In particular, SCOPE will no
11596 longer be considered a dependent type. */
11597 push_scope (scope);
11598 /* Look up the declaration. */
11599 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
11600 /* Obtain the set of qualifiers applied to the TYPE. */
11601 quals = cp_type_quals (type);
11602 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11603 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11604 if (!decl)
11605 type = error_mark_node;
11606 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
11607 && TREE_CODE (decl) == TYPE_DECL)
11608 type = TREE_TYPE (decl);
11609 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
11610 && DECL_CLASS_TEMPLATE_P (decl))
11612 tree tmpl;
11613 tree args;
11614 /* Obtain the template and the arguments. */
11615 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
11616 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
11617 /* Instantiate the template. */
11618 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
11619 /*entering_scope=*/0,
11620 tf_error);
11622 else
11623 type = error_mark_node;
11624 /* Qualify the resulting type. */
11625 if (type != error_mark_node && quals)
11626 type = cp_build_qualified_type (type, quals);
11627 /* Leave the SCOPE. */
11628 pop_scope (scope);
11630 return type;
11633 #include "gt-cp-pt.h"