FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / pt.c
blobadd427d4ed1b8b541ef57a0856340d67863d7968
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) PARAMS ((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
53 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
54 the instantiate request occurred; the TREE_VALUE is either a DECL
55 (for a function or static data member), or a TYPE (for a class)
56 indicating what we are hoping to instantiate. */
57 static GTY(()) tree pending_templates;
58 static GTY(()) tree last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static GTY(()) varray_type inline_parm_levels;
65 static size_t inline_parm_levels_used;
67 static GTY(()) tree current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* A map from local variable declarations in the body of the template
72 presently being instantiated to the corresponding instantiated
73 local variables. */
74 static htab_t local_specializations;
76 #define UNIFY_ALLOW_NONE 0
77 #define UNIFY_ALLOW_MORE_CV_QUAL 1
78 #define UNIFY_ALLOW_LESS_CV_QUAL 2
79 #define UNIFY_ALLOW_DERIVED 4
80 #define UNIFY_ALLOW_INTEGER 8
81 #define UNIFY_ALLOW_OUTER_LEVEL 16
82 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
83 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
84 #define UNIFY_ALLOW_MAX_CORRECTION 128
86 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
87 virtual, or a base class of a virtual
88 base. */
89 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
90 type with the desired type. */
92 static void push_access_scope_real PARAMS ((tree, tree, tree));
93 static void push_access_scope PARAMS ((tree));
94 static void pop_access_scope PARAMS ((tree));
95 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
96 unification_kind_t, int));
97 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
98 unification_kind_t, int));
99 static int unify PARAMS ((tree, tree, tree, tree, int));
100 static void add_pending_template PARAMS ((tree));
101 static void reopen_tinst_level PARAMS ((tree));
102 static tree classtype_mangled_name PARAMS ((tree));
103 static char *mangle_class_name_for_template PARAMS ((const char *,
104 tree, tree));
105 static tree tsubst_initializer_list PARAMS ((tree, tree));
106 static int list_eq PARAMS ((tree, tree));
107 static tree get_class_bindings PARAMS ((tree, tree, tree));
108 static tree coerce_template_parms PARAMS ((tree, tree, tree,
109 tsubst_flags_t, int));
110 static void tsubst_enum PARAMS ((tree, tree, tree));
111 static tree add_to_template_args PARAMS ((tree, tree));
112 static tree add_outermost_template_args PARAMS ((tree, tree));
113 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
114 tree*));
115 static int type_unification_real PARAMS ((tree, tree, tree, tree,
116 int, unification_kind_t, int, int));
117 static void note_template_header PARAMS ((int));
118 static tree maybe_fold_nontype_arg PARAMS ((tree));
119 static tree convert_nontype_argument PARAMS ((tree, tree));
120 static tree convert_template_argument PARAMS ((tree, tree, tree,
121 tsubst_flags_t, int, tree));
122 static tree get_bindings_overload PARAMS ((tree, tree, tree));
123 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t));
124 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
125 static int inline_needs_template_parms PARAMS ((tree));
126 static void push_inline_template_parms_recursive PARAMS ((tree, int));
127 static tree retrieve_specialization PARAMS ((tree, tree));
128 static tree retrieve_local_specialization PARAMS ((tree));
129 static tree register_specialization PARAMS ((tree, tree, tree));
130 static void register_local_specialization PARAMS ((tree, tree));
131 static int unregister_specialization PARAMS ((tree, tree));
132 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
133 static tree build_template_decl PARAMS ((tree, tree));
134 static int mark_template_parm PARAMS ((tree, void *));
135 static int template_parm_this_level_p PARAMS ((tree, void *));
136 static tree tsubst_friend_function PARAMS ((tree, tree));
137 static tree tsubst_friend_class PARAMS ((tree, tree));
138 static int can_complete_type_without_circularity PARAMS ((tree));
139 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
140 static int template_decl_level PARAMS ((tree));
141 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
142 static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t));
143 static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t));
144 static void regenerate_decl_from_template PARAMS ((tree, tree));
145 static tree most_specialized PARAMS ((tree, tree, tree));
146 static tree most_specialized_class PARAMS ((tree, tree));
147 static int template_class_depth_real PARAMS ((tree, int));
148 static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int));
149 static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t));
150 static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree));
151 static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree));
152 static void check_specialization_scope PARAMS ((void));
153 static tree process_partial_specialization PARAMS ((tree));
154 static void set_current_access_from_decl PARAMS ((tree));
155 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
156 static tree tsubst_call_declarator_parms PARAMS ((tree, tree,
157 tsubst_flags_t, tree));
158 static tree get_template_base_recursive PARAMS ((tree, tree,
159 tree, tree, tree, int));
160 static tree get_template_base PARAMS ((tree, tree, tree, tree));
161 static int verify_class_unification PARAMS ((tree, tree, tree));
162 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
163 static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t,
164 tree, tree));
165 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
166 static int template_args_equal PARAMS ((tree, tree));
167 static void tsubst_default_arguments PARAMS ((tree));
168 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
169 static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
170 static void copy_default_args_to_explicit_spec PARAMS ((tree));
171 static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t));
172 static int eq_local_specializations (const void *, const void *);
173 static tree template_for_substitution (tree);
174 static bool dependent_type_p_r (tree);
175 static bool dependent_template_id_p (tree, tree);
176 static tree tsubst (tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
178 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_copy_and_build (tree, tree, tsubst_flags_t, tree);
181 /* Make the current scope suitable for access checking when we are
182 processing T. T can be FUNCTION_DECL for instantiated function
183 template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for
184 static member variable (need by instantiate_decl). ARGS is the
185 template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE,
186 this is used instead of the context of T. */
188 void
189 push_access_scope_real (t, args, context)
190 tree t, args, context;
192 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
194 /* When we are processing specialization `foo<Outer>' for code like
196 template <class U> typename U::Inner foo ();
197 class Outer {
198 struct Inner {};
199 friend Outer::Inner foo<Outer> ();
202 `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of
203 its specialization. We can get the FUNCTION_DECL with the right
204 information because this specialization has already been
205 registered by the friend declaration above. */
207 if (DECL_FUNCTION_TEMPLATE_P (t) && args)
209 tree full_args = tsubst_template_arg_vector
210 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none);
211 tree spec = NULL_TREE;
212 if (full_args != error_mark_node)
213 spec = retrieve_specialization (t, full_args);
214 if (spec)
215 t = spec;
219 if (!context)
220 context = DECL_CONTEXT (t);
221 if (context && TYPE_P (context))
222 push_nested_class (context);
223 else
224 push_to_top_level ();
226 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
228 saved_access_scope = tree_cons
229 (NULL_TREE, current_function_decl, saved_access_scope);
230 current_function_decl = t;
234 /* Like push_access_scope_real, but always uses DECL_CONTEXT. */
236 void
237 push_access_scope (t)
238 tree t;
240 push_access_scope_real (t, NULL_TREE, NULL_TREE);
243 /* Restore the scope set up by push_access_scope. T is the node we
244 are processing. */
246 void
247 pop_access_scope (t)
248 tree t;
250 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
252 current_function_decl = TREE_VALUE (saved_access_scope);
253 saved_access_scope = TREE_CHAIN (saved_access_scope);
256 if (DECL_CLASS_SCOPE_P (t))
257 pop_nested_class ();
258 else
259 pop_from_top_level ();
262 /* Do any processing required when DECL (a member template
263 declaration) is finished. Returns the TEMPLATE_DECL corresponding
264 to DECL, unless it is a specialization, in which case the DECL
265 itself is returned. */
267 tree
268 finish_member_template_decl (decl)
269 tree decl;
271 if (decl == error_mark_node)
272 return error_mark_node;
274 my_friendly_assert (DECL_P (decl), 20020812);
276 if (TREE_CODE (decl) == TYPE_DECL)
278 tree type;
280 type = TREE_TYPE (decl);
281 if (IS_AGGR_TYPE (type)
282 && CLASSTYPE_TEMPLATE_INFO (type)
283 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
285 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
286 check_member_template (tmpl);
287 return tmpl;
289 return NULL_TREE;
291 else if (TREE_CODE (decl) == FIELD_DECL)
292 error ("data member `%D' cannot be a member template", decl);
293 else if (DECL_TEMPLATE_INFO (decl))
295 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
297 check_member_template (DECL_TI_TEMPLATE (decl));
298 return DECL_TI_TEMPLATE (decl);
300 else
301 return decl;
303 else
304 error ("invalid member template declaration `%D'", decl);
306 return error_mark_node;
309 /* Returns the template nesting level of the indicated class TYPE.
311 For example, in:
312 template <class T>
313 struct A
315 template <class U>
316 struct B {};
319 A<T>::B<U> has depth two, while A<T> has depth one.
320 Both A<T>::B<int> and A<int>::B<U> have depth one, if
321 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
322 specializations.
324 This function is guaranteed to return 0 if passed NULL_TREE so
325 that, for example, `template_class_depth (current_class_type)' is
326 always safe. */
328 static int
329 template_class_depth_real (type, count_specializations)
330 tree type;
331 int count_specializations;
333 int depth;
335 for (depth = 0;
336 type && TREE_CODE (type) != NAMESPACE_DECL;
337 type = (TREE_CODE (type) == FUNCTION_DECL)
338 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
340 if (TREE_CODE (type) != FUNCTION_DECL)
342 if (CLASSTYPE_TEMPLATE_INFO (type)
343 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
344 && ((count_specializations
345 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
346 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
347 ++depth;
349 else
351 if (DECL_TEMPLATE_INFO (type)
352 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
353 && ((count_specializations
354 && DECL_TEMPLATE_SPECIALIZATION (type))
355 || uses_template_parms (DECL_TI_ARGS (type))))
356 ++depth;
360 return depth;
363 /* Returns the template nesting level of the indicated class TYPE.
364 Like template_class_depth_real, but instantiations do not count in
365 the depth. */
367 int
368 template_class_depth (type)
369 tree type;
371 return template_class_depth_real (type, /*count_specializations=*/0);
374 /* Returns 1 if processing DECL as part of do_pending_inlines
375 needs us to push template parms. */
377 static int
378 inline_needs_template_parms (decl)
379 tree decl;
381 if (! DECL_TEMPLATE_INFO (decl))
382 return 0;
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
391 innermost first. */
393 static void
394 push_inline_template_parms_recursive (parmlist, levels)
395 tree parmlist;
396 int levels;
398 tree parms = TREE_VALUE (parmlist);
399 int i;
401 if (levels > 1)
402 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
404 ++processing_template_decl;
405 current_template_parms
406 = tree_cons (size_int (processing_template_decl),
407 parms, current_template_parms);
408 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
410 pushlevel (0);
411 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
413 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
414 my_friendly_assert (DECL_P (parm), 0);
416 switch (TREE_CODE (parm))
418 case TYPE_DECL:
419 case TEMPLATE_DECL:
420 pushdecl (parm);
421 break;
423 case PARM_DECL:
425 /* Make a CONST_DECL as is done in process_template_parm.
426 It is ugly that we recreate this here; the original
427 version built in process_template_parm is no longer
428 available. */
429 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
430 TREE_TYPE (parm));
431 DECL_ARTIFICIAL (decl) = 1;
432 DECL_INITIAL (decl) = DECL_INITIAL (parm);
433 SET_DECL_TEMPLATE_PARM_P (decl);
434 pushdecl (decl);
436 break;
438 default:
439 abort ();
444 /* Restore the template parameter context for a member template or
445 a friend template defined in a class definition. */
447 void
448 maybe_begin_member_template_processing (decl)
449 tree decl;
451 tree parms;
452 int levels = 0;
454 if (inline_needs_template_parms (decl))
456 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
457 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
459 if (DECL_TEMPLATE_SPECIALIZATION (decl))
461 --levels;
462 parms = TREE_CHAIN (parms);
465 push_inline_template_parms_recursive (parms, levels);
468 /* Remember how many levels of template parameters we pushed so that
469 we can pop them later. */
470 if (!inline_parm_levels)
471 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
472 if (inline_parm_levels_used == inline_parm_levels->num_elements)
473 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
474 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
475 ++inline_parm_levels_used;
478 /* Undo the effects of begin_member_template_processing. */
480 void
481 maybe_end_member_template_processing ()
483 int i;
485 if (!inline_parm_levels_used)
486 return;
488 --inline_parm_levels_used;
489 for (i = 0;
490 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
491 ++i)
493 --processing_template_decl;
494 current_template_parms = TREE_CHAIN (current_template_parms);
495 poplevel (0, 0, 0);
499 /* Returns nonzero iff T is a member template function. We must be
500 careful as in
502 template <class T> class C { void f(); }
504 Here, f is a template function, and a member, but not a member
505 template. This function does not concern itself with the origin of
506 T, only its present state. So if we have
508 template <class T> class C { template <class U> void f(U); }
510 then neither C<int>::f<char> nor C<T>::f<double> is considered
511 to be a member template. But, `template <class U> void
512 C<int>::f(U)' is considered a member template. */
515 is_member_template (t)
516 tree t;
518 if (!DECL_FUNCTION_TEMPLATE_P (t))
519 /* Anything that isn't a function or a template function is
520 certainly not a member template. */
521 return 0;
523 /* A local class can't have member templates. */
524 if (decl_function_context (t))
525 return 0;
527 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
528 /* If there are more levels of template parameters than
529 there are template classes surrounding the declaration,
530 then we have a member template. */
531 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
532 template_class_depth (DECL_CONTEXT (t))));
535 #if 0 /* UNUSED */
536 /* Returns nonzero iff T is a member template class. See
537 is_member_template for a description of what precisely constitutes
538 a member template. */
541 is_member_template_class (t)
542 tree t;
544 if (!DECL_CLASS_TEMPLATE_P (t))
545 /* Anything that isn't a class template, is certainly not a member
546 template. */
547 return 0;
549 if (!DECL_CLASS_SCOPE_P (t))
550 /* Anything whose context isn't a class type is surely not a
551 member template. */
552 return 0;
554 /* If there are more levels of template parameters than there are
555 template classes surrounding the declaration, then we have a
556 member template. */
557 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
558 template_class_depth (DECL_CONTEXT (t)));
560 #endif
562 /* Return a new template argument vector which contains all of ARGS,
563 but has as its innermost set of arguments the EXTRA_ARGS. */
565 static tree
566 add_to_template_args (args, extra_args)
567 tree args;
568 tree extra_args;
570 tree new_args;
571 int extra_depth;
572 int i;
573 int j;
575 extra_depth = TMPL_ARGS_DEPTH (extra_args);
576 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
578 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
579 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
581 for (j = 1; j <= extra_depth; ++j, ++i)
582 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
584 return new_args;
587 /* Like add_to_template_args, but only the outermost ARGS are added to
588 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
589 (EXTRA_ARGS) levels are added. This function is used to combine
590 the template arguments from a partial instantiation with the
591 template arguments used to attain the full instantiation from the
592 partial instantiation. */
594 static tree
595 add_outermost_template_args (args, extra_args)
596 tree args;
597 tree extra_args;
599 tree new_args;
601 /* If there are more levels of EXTRA_ARGS than there are ARGS,
602 something very fishy is going on. */
603 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
606 /* If *all* the new arguments will be the EXTRA_ARGS, just return
607 them. */
608 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
609 return extra_args;
611 /* For the moment, we make ARGS look like it contains fewer levels. */
612 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
614 new_args = add_to_template_args (args, extra_args);
616 /* Now, we restore ARGS to its full dimensions. */
617 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
619 return new_args;
622 /* Return the N levels of innermost template arguments from the ARGS. */
624 tree
625 get_innermost_template_args (args, n)
626 tree args;
627 int n;
629 tree new_args;
630 int extra_levels;
631 int i;
633 my_friendly_assert (n >= 0, 20000603);
635 /* If N is 1, just return the innermost set of template arguments. */
636 if (n == 1)
637 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
639 /* If we're not removing anything, just return the arguments we were
640 given. */
641 extra_levels = TMPL_ARGS_DEPTH (args) - n;
642 my_friendly_assert (extra_levels >= 0, 20000603);
643 if (extra_levels == 0)
644 return args;
646 /* Make a new set of arguments, not containing the outer arguments. */
647 new_args = make_tree_vec (n);
648 for (i = 1; i <= n; ++i)
649 SET_TMPL_ARGS_LEVEL (new_args, i,
650 TMPL_ARGS_LEVEL (args, i + extra_levels));
652 return new_args;
655 /* We've got a template header coming up; push to a new level for storing
656 the parms. */
658 void
659 begin_template_parm_list ()
661 /* We use a non-tag-transparent scope here, which causes pushtag to
662 put tags in this scope, rather than in the enclosing class or
663 namespace scope. This is the right thing, since we want
664 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
665 global template class, push_template_decl handles putting the
666 TEMPLATE_DECL into top-level scope. For a nested template class,
667 e.g.:
669 template <class T> struct S1 {
670 template <class T> struct S2 {};
673 pushtag contains special code to call pushdecl_with_scope on the
674 TEMPLATE_DECL for S2. */
675 begin_scope (sk_template_parms);
676 ++processing_template_decl;
677 ++processing_template_parmlist;
678 note_template_header (0);
681 /* This routine is called when a specialization is declared. If it is
682 invalid to declare a specialization here, an error is reported. */
684 static void
685 check_specialization_scope ()
687 tree scope = current_scope ();
689 /* [temp.expl.spec]
691 An explicit specialization shall be declared in the namespace of
692 which the template is a member, or, for member templates, in the
693 namespace of which the enclosing class or enclosing class
694 template is a member. An explicit specialization of a member
695 function, member class or static data member of a class template
696 shall be declared in the namespace of which the class template
697 is a member. */
698 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
699 error ("explicit specialization in non-namespace scope `%D'",
700 scope);
702 /* [temp.expl.spec]
704 In an explicit specialization declaration for a member of a class
705 template or a member template that appears in namespace scope,
706 the member template and some of its enclosing class templates may
707 remain unspecialized, except that the declaration shall not
708 explicitly specialize a class member template if its enclosing
709 class templates are not explicitly specialized as well. */
710 if (current_template_parms)
711 error ("enclosing class templates are not explicitly specialized");
714 /* We've just seen template <>. */
716 void
717 begin_specialization ()
719 begin_scope (sk_template_spec);
720 note_template_header (1);
721 check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
727 void
728 end_specialization ()
730 finish_scope ();
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
737 void
738 reset_specialization ()
740 processing_specialization = 0;
741 template_header_count = 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
747 static void
748 note_template_header (specialization)
749 int specialization;
751 processing_specialization = specialization;
752 template_header_count++;
755 /* We're beginning an explicit instantiation. */
757 void
758 begin_explicit_instantiation ()
760 my_friendly_assert (!processing_explicit_instantiation, 20020913);
761 processing_explicit_instantiation = true;
765 void
766 end_explicit_instantiation ()
768 my_friendly_assert(processing_explicit_instantiation, 20020913);
769 processing_explicit_instantiation = false;
772 /* The TYPE is being declared. If it is a template type, that means it
773 is a partial specialization. Do appropriate error-checking. */
775 void
776 maybe_process_partial_specialization (type)
777 tree type;
779 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
781 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
782 && !COMPLETE_TYPE_P (type))
784 if (current_namespace
785 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
787 pedwarn ("specializing `%#T' in different namespace", type);
788 cp_pedwarn_at (" from definition of `%#D'",
789 CLASSTYPE_TI_TEMPLATE (type));
791 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
792 if (processing_template_decl)
793 push_template_decl (TYPE_MAIN_DECL (type));
795 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
796 error ("specialization of `%T' after instantiation", type);
798 else if (processing_specialization)
799 error ("explicit specialization of non-template `%T'", type);
802 /* Retrieve the specialization (in the sense of [temp.spec] - a
803 specialization is either an instantiation or an explicit
804 specialization) of TMPL for the given template ARGS. If there is
805 no such specialization, return NULL_TREE. The ARGS are a vector of
806 arguments, or a vector of vectors of arguments, in the case of
807 templates with more than one level of parameters. */
809 static tree
810 retrieve_specialization (tmpl, args)
811 tree tmpl;
812 tree args;
814 tree s;
816 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
818 /* There should be as many levels of arguments as there are
819 levels of parameters. */
820 my_friendly_assert (TMPL_ARGS_DEPTH (args)
821 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
824 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
825 s != NULL_TREE;
826 s = TREE_CHAIN (s))
827 if (comp_template_args (TREE_PURPOSE (s), args))
828 return TREE_VALUE (s);
830 return NULL_TREE;
833 /* Like retrieve_specialization, but for local declarations. */
835 static tree
836 retrieve_local_specialization (tmpl)
837 tree tmpl;
839 tree spec =
840 (tree) htab_find_with_hash (local_specializations, tmpl,
841 htab_hash_pointer (tmpl));
842 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
845 /* Returns nonzero iff DECL is a specialization of TMPL. */
848 is_specialization_of (decl, tmpl)
849 tree decl;
850 tree tmpl;
852 tree t;
854 if (TREE_CODE (decl) == FUNCTION_DECL)
856 for (t = decl;
857 t != NULL_TREE;
858 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
859 if (t == tmpl)
860 return 1;
862 else
864 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
866 for (t = TREE_TYPE (decl);
867 t != NULL_TREE;
868 t = CLASSTYPE_USE_TEMPLATE (t)
869 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
870 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
871 return 1;
874 return 0;
877 /* Register the specialization SPEC as a specialization of TMPL with
878 the indicated ARGS. Returns SPEC, or an equivalent prior
879 declaration, if available. */
881 static tree
882 register_specialization (spec, tmpl, args)
883 tree spec;
884 tree tmpl;
885 tree args;
887 tree s;
889 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
891 if (TREE_CODE (spec) == FUNCTION_DECL
892 && uses_template_parms (DECL_TI_ARGS (spec)))
893 /* This is the FUNCTION_DECL for a partial instantiation. Don't
894 register it; we want the corresponding TEMPLATE_DECL instead.
895 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
896 the more obvious `uses_template_parms (spec)' to avoid problems
897 with default function arguments. In particular, given
898 something like this:
900 template <class T> void f(T t1, T t = T())
902 the default argument expression is not substituted for in an
903 instantiation unless and until it is actually needed. */
904 return spec;
906 /* There should be as many levels of arguments as there are
907 levels of parameters. */
908 my_friendly_assert (TMPL_ARGS_DEPTH (args)
909 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
912 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
913 s != NULL_TREE;
914 s = TREE_CHAIN (s))
916 tree fn = TREE_VALUE (s);
918 /* We can sometimes try to re-register a specialization that we've
919 already got. In particular, regenerate_decl_from_template
920 calls duplicate_decls which will update the specialization
921 list. But, we'll still get called again here anyhow. It's
922 more convenient to simply allow this than to try to prevent it. */
923 if (fn == spec)
924 return spec;
925 else if (comp_template_args (TREE_PURPOSE (s), args))
927 if (DECL_TEMPLATE_SPECIALIZATION (spec))
929 if (DECL_TEMPLATE_INSTANTIATION (fn))
931 if (TREE_USED (fn)
932 || DECL_EXPLICIT_INSTANTIATION (fn))
934 error ("specialization of %D after instantiation",
935 fn);
936 return spec;
938 else
940 /* This situation should occur only if the first
941 specialization is an implicit instantiation,
942 the second is an explicit specialization, and
943 the implicit instantiation has not yet been
944 used. That situation can occur if we have
945 implicitly instantiated a member function and
946 then specialized it later.
948 We can also wind up here if a friend
949 declaration that looked like an instantiation
950 turns out to be a specialization:
952 template <class T> void foo(T);
953 class S { friend void foo<>(int) };
954 template <> void foo(int);
956 We transform the existing DECL in place so that
957 any pointers to it become pointers to the
958 updated declaration.
960 If there was a definition for the template, but
961 not for the specialization, we want this to
962 look as if there is no definition, and vice
963 versa. */
964 DECL_INITIAL (fn) = NULL_TREE;
965 duplicate_decls (spec, fn);
967 return fn;
970 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
972 duplicate_decls (spec, fn);
973 return fn;
979 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
980 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
982 return spec;
985 /* Unregister the specialization SPEC as a specialization of TMPL.
986 Returns nonzero if the SPEC was listed as a specialization of
987 TMPL. */
989 static int
990 unregister_specialization (spec, tmpl)
991 tree spec;
992 tree tmpl;
994 tree* s;
996 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
997 *s != NULL_TREE;
998 s = &TREE_CHAIN (*s))
999 if (TREE_VALUE (*s) == spec)
1001 *s = TREE_CHAIN (*s);
1002 return 1;
1005 return 0;
1008 /* Compare an entry in the local specializations hash table P1 (which
1009 is really a pointer to a TREE_LIST) with P2 (which is really a
1010 DECL). */
1012 static int
1013 eq_local_specializations (const void *p1, const void *p2)
1015 return TREE_VALUE ((tree) p1) == (tree) p2;
1018 /* Hash P1, an entry in the local specializations table. */
1020 static hashval_t
1021 hash_local_specialization (const void* p1)
1023 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1026 /* Like register_specialization, but for local declarations. We are
1027 registering SPEC, an instantiation of TMPL. */
1029 static void
1030 register_local_specialization (spec, tmpl)
1031 tree spec;
1032 tree tmpl;
1034 void **slot;
1036 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1037 htab_hash_pointer (tmpl), INSERT);
1038 *slot = build_tree_list (spec, tmpl);
1041 /* Print the list of candidate FNS in an error message. */
1043 void
1044 print_candidates (fns)
1045 tree fns;
1047 tree fn;
1049 const char *str = "candidates are:";
1051 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1053 tree f;
1055 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1056 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1057 str = " ";
1061 /* Returns the template (one of the functions given by TEMPLATE_ID)
1062 which can be specialized to match the indicated DECL with the
1063 explicit template args given in TEMPLATE_ID. The DECL may be
1064 NULL_TREE if none is available. In that case, the functions in
1065 TEMPLATE_ID are non-members.
1067 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1068 specialization of a member template.
1070 The template args (those explicitly specified and those deduced)
1071 are output in a newly created vector *TARGS_OUT.
1073 If it is impossible to determine the result, an error message is
1074 issued. The error_mark_node is returned to indicate failure. */
1076 static tree
1077 determine_specialization (template_id, decl, targs_out,
1078 need_member_template)
1079 tree template_id;
1080 tree decl;
1081 tree* targs_out;
1082 int need_member_template;
1084 tree fns;
1085 tree targs;
1086 tree explicit_targs;
1087 tree candidates = NULL_TREE;
1088 tree templates = NULL_TREE;
1090 *targs_out = NULL_TREE;
1092 if (template_id == error_mark_node)
1093 return error_mark_node;
1095 fns = TREE_OPERAND (template_id, 0);
1096 explicit_targs = TREE_OPERAND (template_id, 1);
1098 if (fns == error_mark_node)
1099 return error_mark_node;
1101 /* Check for baselinks. */
1102 if (BASELINK_P (fns))
1103 fns = BASELINK_FUNCTIONS (fns);
1105 if (!is_overloaded_fn (fns))
1107 error ("`%D' is not a function template", fns);
1108 return error_mark_node;
1111 for (; fns; fns = OVL_NEXT (fns))
1113 tree fn = OVL_CURRENT (fns);
1115 if (TREE_CODE (fn) == TEMPLATE_DECL)
1117 tree decl_arg_types;
1119 /* DECL might be a specialization of FN. */
1121 /* Adjust the type of DECL in case FN is a static member. */
1122 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1123 if (DECL_STATIC_FUNCTION_P (fn)
1124 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1125 decl_arg_types = TREE_CHAIN (decl_arg_types);
1127 /* Check that the number of function parameters matches.
1128 For example,
1129 template <class T> void f(int i = 0);
1130 template <> void f<int>();
1131 The specialization f<int> is invalid but is not caught
1132 by get_bindings below. */
1134 if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1135 != list_length (decl_arg_types))
1136 continue;
1138 /* See whether this function might be a specialization of this
1139 template. */
1140 targs = get_bindings (fn, decl, explicit_targs);
1142 if (!targs)
1143 /* We cannot deduce template arguments that when used to
1144 specialize TMPL will produce DECL. */
1145 continue;
1147 /* Save this template, and the arguments deduced. */
1148 templates = tree_cons (targs, fn, templates);
1150 else if (need_member_template)
1151 /* FN is an ordinary member function, and we need a
1152 specialization of a member template. */
1154 else if (TREE_CODE (fn) != FUNCTION_DECL)
1155 /* We can get IDENTIFIER_NODEs here in certain erroneous
1156 cases. */
1158 else if (!DECL_FUNCTION_MEMBER_P (fn))
1159 /* This is just an ordinary non-member function. Nothing can
1160 be a specialization of that. */
1162 else if (DECL_ARTIFICIAL (fn))
1163 /* Cannot specialize functions that are created implicitly. */
1165 else
1167 tree decl_arg_types;
1169 /* This is an ordinary member function. However, since
1170 we're here, we can assume it's enclosing class is a
1171 template class. For example,
1173 template <typename T> struct S { void f(); };
1174 template <> void S<int>::f() {}
1176 Here, S<int>::f is a non-template, but S<int> is a
1177 template class. If FN has the same type as DECL, we
1178 might be in business. */
1180 if (!DECL_TEMPLATE_INFO (fn))
1181 /* Its enclosing class is an explicit specialization
1182 of a template class. This is not a candidate. */
1183 continue;
1185 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1186 TREE_TYPE (TREE_TYPE (fn))))
1187 /* The return types differ. */
1188 continue;
1190 /* Adjust the type of DECL in case FN is a static member. */
1191 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1192 if (DECL_STATIC_FUNCTION_P (fn)
1193 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1194 decl_arg_types = TREE_CHAIN (decl_arg_types);
1196 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1197 decl_arg_types))
1198 /* They match! */
1199 candidates = tree_cons (NULL_TREE, fn, candidates);
1203 if (templates && TREE_CHAIN (templates))
1205 /* We have:
1207 [temp.expl.spec]
1209 It is possible for a specialization with a given function
1210 signature to be instantiated from more than one function
1211 template. In such cases, explicit specification of the
1212 template arguments must be used to uniquely identify the
1213 function template specialization being specialized.
1215 Note that here, there's no suggestion that we're supposed to
1216 determine which of the candidate templates is most
1217 specialized. However, we, also have:
1219 [temp.func.order]
1221 Partial ordering of overloaded function template
1222 declarations is used in the following contexts to select
1223 the function template to which a function template
1224 specialization refers:
1226 -- when an explicit specialization refers to a function
1227 template.
1229 So, we do use the partial ordering rules, at least for now.
1230 This extension can only serve to make invalid programs valid,
1231 so it's safe. And, there is strong anecdotal evidence that
1232 the committee intended the partial ordering rules to apply;
1233 the EDG front-end has that behavior, and John Spicer claims
1234 that the committee simply forgot to delete the wording in
1235 [temp.expl.spec]. */
1236 tree tmpl = most_specialized (templates, decl, explicit_targs);
1237 if (tmpl && tmpl != error_mark_node)
1239 targs = get_bindings (tmpl, decl, explicit_targs);
1240 templates = tree_cons (targs, tmpl, NULL_TREE);
1244 if (templates == NULL_TREE && candidates == NULL_TREE)
1246 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1247 template_id, decl);
1248 return error_mark_node;
1250 else if ((templates && TREE_CHAIN (templates))
1251 || (candidates && TREE_CHAIN (candidates))
1252 || (templates && candidates))
1254 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1255 template_id, decl);
1256 chainon (candidates, templates);
1257 print_candidates (candidates);
1258 return error_mark_node;
1261 /* We have one, and exactly one, match. */
1262 if (candidates)
1264 /* It was a specialization of an ordinary member function in a
1265 template class. */
1266 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1267 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1270 /* It was a specialization of a template. */
1271 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1272 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1274 *targs_out = copy_node (targs);
1275 SET_TMPL_ARGS_LEVEL (*targs_out,
1276 TMPL_ARGS_DEPTH (*targs_out),
1277 TREE_PURPOSE (templates));
1279 else
1280 *targs_out = TREE_PURPOSE (templates);
1281 return TREE_VALUE (templates);
1284 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1285 but with the default argument values filled in from those in the
1286 TMPL_TYPES. */
1288 static tree
1289 copy_default_args_to_explicit_spec_1 (spec_types,
1290 tmpl_types)
1291 tree spec_types;
1292 tree tmpl_types;
1294 tree new_spec_types;
1296 if (!spec_types)
1297 return NULL_TREE;
1299 if (spec_types == void_list_node)
1300 return void_list_node;
1302 /* Substitute into the rest of the list. */
1303 new_spec_types =
1304 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1305 TREE_CHAIN (tmpl_types));
1307 /* Add the default argument for this parameter. */
1308 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1309 TREE_VALUE (spec_types),
1310 new_spec_types);
1313 /* DECL is an explicit specialization. Replicate default arguments
1314 from the template it specializes. (That way, code like:
1316 template <class T> void f(T = 3);
1317 template <> void f(double);
1318 void g () { f (); }
1320 works, as required.) An alternative approach would be to look up
1321 the correct default arguments at the call-site, but this approach
1322 is consistent with how implicit instantiations are handled. */
1324 static void
1325 copy_default_args_to_explicit_spec (decl)
1326 tree decl;
1328 tree tmpl;
1329 tree spec_types;
1330 tree tmpl_types;
1331 tree new_spec_types;
1332 tree old_type;
1333 tree new_type;
1334 tree t;
1335 tree object_type = NULL_TREE;
1336 tree in_charge = NULL_TREE;
1337 tree vtt = NULL_TREE;
1339 /* See if there's anything we need to do. */
1340 tmpl = DECL_TI_TEMPLATE (decl);
1341 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1342 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1343 if (TREE_PURPOSE (t))
1344 break;
1345 if (!t)
1346 return;
1348 old_type = TREE_TYPE (decl);
1349 spec_types = TYPE_ARG_TYPES (old_type);
1351 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1353 /* Remove the this pointer, but remember the object's type for
1354 CV quals. */
1355 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1356 spec_types = TREE_CHAIN (spec_types);
1357 tmpl_types = TREE_CHAIN (tmpl_types);
1359 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1361 /* DECL may contain more parameters than TMPL due to the extra
1362 in-charge parameter in constructors and destructors. */
1363 in_charge = spec_types;
1364 spec_types = TREE_CHAIN (spec_types);
1366 if (DECL_HAS_VTT_PARM_P (decl))
1368 vtt = spec_types;
1369 spec_types = TREE_CHAIN (spec_types);
1373 /* Compute the merged default arguments. */
1374 new_spec_types =
1375 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1377 /* Compute the new FUNCTION_TYPE. */
1378 if (object_type)
1380 if (vtt)
1381 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1382 TREE_VALUE (vtt),
1383 new_spec_types);
1385 if (in_charge)
1386 /* Put the in-charge parameter back. */
1387 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1388 TREE_VALUE (in_charge),
1389 new_spec_types);
1391 new_type = build_cplus_method_type (object_type,
1392 TREE_TYPE (old_type),
1393 new_spec_types);
1395 else
1396 new_type = build_function_type (TREE_TYPE (old_type),
1397 new_spec_types);
1398 new_type = build_type_attribute_variant (new_type,
1399 TYPE_ATTRIBUTES (old_type));
1400 new_type = build_exception_variant (new_type,
1401 TYPE_RAISES_EXCEPTIONS (old_type));
1402 TREE_TYPE (decl) = new_type;
1405 /* Check to see if the function just declared, as indicated in
1406 DECLARATOR, and in DECL, is a specialization of a function
1407 template. We may also discover that the declaration is an explicit
1408 instantiation at this point.
1410 Returns DECL, or an equivalent declaration that should be used
1411 instead if all goes well. Issues an error message if something is
1412 amiss. Returns error_mark_node if the error is not easily
1413 recoverable.
1415 FLAGS is a bitmask consisting of the following flags:
1417 2: The function has a definition.
1418 4: The function is a friend.
1420 The TEMPLATE_COUNT is the number of references to qualifying
1421 template classes that appeared in the name of the function. For
1422 example, in
1424 template <class T> struct S { void f(); };
1425 void S<int>::f();
1427 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1428 classes are not counted in the TEMPLATE_COUNT, so that in
1430 template <class T> struct S {};
1431 template <> struct S<int> { void f(); }
1432 template <> void S<int>::f();
1434 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1435 invalid; there should be no template <>.)
1437 If the function is a specialization, it is marked as such via
1438 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1439 is set up correctly, and it is added to the list of specializations
1440 for that template. */
1442 tree
1443 check_explicit_specialization (declarator, decl, template_count, flags)
1444 tree declarator;
1445 tree decl;
1446 int template_count;
1447 int flags;
1449 int have_def = flags & 2;
1450 int is_friend = flags & 4;
1451 int specialization = 0;
1452 int explicit_instantiation = 0;
1453 int member_specialization = 0;
1454 tree ctype = DECL_CLASS_CONTEXT (decl);
1455 tree dname = DECL_NAME (decl);
1456 tmpl_spec_kind tsk;
1458 tsk = current_tmpl_spec_kind (template_count);
1460 switch (tsk)
1462 case tsk_none:
1463 if (processing_specialization)
1465 specialization = 1;
1466 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1468 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1470 if (is_friend)
1471 /* This could be something like:
1473 template <class T> void f(T);
1474 class S { friend void f<>(int); } */
1475 specialization = 1;
1476 else
1478 /* This case handles bogus declarations like template <>
1479 template <class T> void f<int>(); */
1481 error ("template-id `%D' in declaration of primary template",
1482 declarator);
1483 return decl;
1486 break;
1488 case tsk_invalid_member_spec:
1489 /* The error has already been reported in
1490 check_specialization_scope. */
1491 return error_mark_node;
1493 case tsk_invalid_expl_inst:
1494 error ("template parameter list used in explicit instantiation");
1496 /* Fall through. */
1498 case tsk_expl_inst:
1499 if (have_def)
1500 error ("definition provided for explicit instantiation");
1502 explicit_instantiation = 1;
1503 break;
1505 case tsk_excessive_parms:
1506 error ("too many template parameter lists in declaration of `%D'",
1507 decl);
1508 return error_mark_node;
1510 /* Fall through. */
1511 case tsk_expl_spec:
1512 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1513 if (ctype)
1514 member_specialization = 1;
1515 else
1516 specialization = 1;
1517 break;
1519 case tsk_insufficient_parms:
1520 if (template_header_count)
1522 error("too few template parameter lists in declaration of `%D'",
1523 decl);
1524 return decl;
1526 else if (ctype != NULL_TREE
1527 && !TYPE_BEING_DEFINED (ctype)
1528 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1529 && !is_friend)
1531 /* For backwards compatibility, we accept:
1533 template <class T> struct S { void f(); };
1534 void S<int>::f() {} // Missing template <>
1536 That used to be valid C++. */
1537 if (pedantic)
1538 pedwarn
1539 ("explicit specialization not preceded by `template <>'");
1540 specialization = 1;
1541 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1543 break;
1545 case tsk_template:
1546 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1548 /* This case handles bogus declarations like template <>
1549 template <class T> void f<int>(); */
1551 if (uses_template_parms (declarator))
1552 error ("partial specialization `%D' of function template",
1553 declarator);
1554 else
1555 error ("template-id `%D' in declaration of primary template",
1556 declarator);
1557 return decl;
1560 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1561 /* This is a specialization of a member template, without
1562 specialization the containing class. Something like:
1564 template <class T> struct S {
1565 template <class U> void f (U);
1567 template <> template <class U> void S<int>::f(U) {}
1569 That's a specialization -- but of the entire template. */
1570 specialization = 1;
1571 break;
1573 default:
1574 abort ();
1577 if (specialization || member_specialization)
1579 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1580 for (; t; t = TREE_CHAIN (t))
1581 if (TREE_PURPOSE (t))
1583 pedwarn
1584 ("default argument specified in explicit specialization");
1585 break;
1587 if (current_lang_name == lang_name_c)
1588 error ("template specialization with C linkage");
1591 if (specialization || member_specialization || explicit_instantiation)
1593 tree tmpl = NULL_TREE;
1594 tree targs = NULL_TREE;
1596 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1597 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1599 tree fns;
1601 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1603 if (!ctype)
1604 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1605 else
1606 fns = dname;
1608 declarator =
1609 lookup_template_function (fns, NULL_TREE);
1612 if (declarator == error_mark_node)
1613 return error_mark_node;
1615 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1617 if (!explicit_instantiation)
1618 /* A specialization in class scope. This is invalid,
1619 but the error will already have been flagged by
1620 check_specialization_scope. */
1621 return error_mark_node;
1622 else
1624 /* It's not valid to write an explicit instantiation in
1625 class scope, e.g.:
1627 class C { template void f(); }
1629 This case is caught by the parser. However, on
1630 something like:
1632 template class C { void f(); };
1634 (which is invalid) we can get here. The error will be
1635 issued later. */
1639 return decl;
1641 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1643 /* A friend declaration. We can't do much, because we don't
1644 know what this resolves to, yet. */
1645 my_friendly_assert (is_friend != 0, 0);
1646 my_friendly_assert (!explicit_instantiation, 0);
1647 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1648 return decl;
1650 else if (ctype != NULL_TREE
1651 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1652 IDENTIFIER_NODE))
1654 /* Find the list of functions in ctype that have the same
1655 name as the declared function. */
1656 tree name = TREE_OPERAND (declarator, 0);
1657 tree fns = NULL_TREE;
1658 int idx;
1660 if (constructor_name_p (name, ctype))
1662 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1664 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1665 : !TYPE_HAS_DESTRUCTOR (ctype))
1667 /* From [temp.expl.spec]:
1669 If such an explicit specialization for the member
1670 of a class template names an implicitly-declared
1671 special member function (clause _special_), the
1672 program is ill-formed.
1674 Similar language is found in [temp.explicit]. */
1675 error ("specialization of implicitly-declared special member function");
1676 return error_mark_node;
1679 name = is_constructor ? ctor_identifier : dtor_identifier;
1682 if (!DECL_CONV_FN_P (decl))
1684 idx = lookup_fnfields_1 (ctype, name);
1685 if (idx >= 0)
1686 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1688 else
1690 tree methods;
1692 /* For a type-conversion operator, we cannot do a
1693 name-based lookup. We might be looking for `operator
1694 int' which will be a specialization of `operator T'.
1695 So, we find *all* the conversion operators, and then
1696 select from them. */
1697 fns = NULL_TREE;
1699 methods = CLASSTYPE_METHOD_VEC (ctype);
1700 if (methods)
1701 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1702 idx < TREE_VEC_LENGTH (methods); ++idx)
1704 tree ovl = TREE_VEC_ELT (methods, idx);
1706 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1707 /* There are no more conversion functions. */
1708 break;
1710 /* Glue all these conversion functions together
1711 with those we already have. */
1712 for (; ovl; ovl = OVL_NEXT (ovl))
1713 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1717 if (fns == NULL_TREE)
1719 error ("no member function `%D' declared in `%T'",
1720 name, ctype);
1721 return error_mark_node;
1723 else
1724 TREE_OPERAND (declarator, 0) = fns;
1727 /* Figure out what exactly is being specialized at this point.
1728 Note that for an explicit instantiation, even one for a
1729 member function, we cannot tell apriori whether the
1730 instantiation is for a member template, or just a member
1731 function of a template class. Even if a member template is
1732 being instantiated, the member template arguments may be
1733 elided if they can be deduced from the rest of the
1734 declaration. */
1735 tmpl = determine_specialization (declarator, decl,
1736 &targs,
1737 member_specialization);
1739 if (!tmpl || tmpl == error_mark_node)
1740 /* We couldn't figure out what this declaration was
1741 specializing. */
1742 return error_mark_node;
1743 else
1745 tree gen_tmpl = most_general_template (tmpl);
1747 if (explicit_instantiation)
1749 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1750 is done by do_decl_instantiation later. */
1752 int arg_depth = TMPL_ARGS_DEPTH (targs);
1753 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1755 if (arg_depth > parm_depth)
1757 /* If TMPL is not the most general template (for
1758 example, if TMPL is a friend template that is
1759 injected into namespace scope), then there will
1760 be too many levels of TARGS. Remove some of them
1761 here. */
1762 int i;
1763 tree new_targs;
1765 new_targs = make_tree_vec (parm_depth);
1766 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1767 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1768 = TREE_VEC_ELT (targs, i);
1769 targs = new_targs;
1772 return instantiate_template (tmpl, targs);
1775 /* If this is a specialization of a member template of a
1776 template class. In we want to return the TEMPLATE_DECL,
1777 not the specialization of it. */
1778 if (tsk == tsk_template)
1780 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1781 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1782 return tmpl;
1785 /* If we thought that the DECL was a member function, but it
1786 turns out to be specializing a static member function,
1787 make DECL a static member function as well. */
1788 if (DECL_STATIC_FUNCTION_P (tmpl)
1789 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1791 revert_static_member_fn (decl);
1792 last_function_parms = TREE_CHAIN (last_function_parms);
1795 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1796 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1798 /* Inherit default function arguments from the template
1799 DECL is specializing. */
1800 copy_default_args_to_explicit_spec (decl);
1802 /* This specialization has the same protection as the
1803 template it specializes. */
1804 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1805 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1807 if (is_friend && !have_def)
1808 /* This is not really a declaration of a specialization.
1809 It's just the name of an instantiation. But, it's not
1810 a request for an instantiation, either. */
1811 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1812 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1813 /* This is indeed a specialization. In case of constructors
1814 and destructors, we need in-charge and not-in-charge
1815 versions in V3 ABI. */
1816 clone_function_decl (decl, /*update_method_vec_p=*/0);
1818 /* Register this specialization so that we can find it
1819 again. */
1820 decl = register_specialization (decl, gen_tmpl, targs);
1824 return decl;
1827 /* TYPE is being declared. Verify that the use of template headers
1828 and such is reasonable. Issue error messages if not. */
1830 void
1831 maybe_check_template_type (type)
1832 tree type;
1834 if (template_header_count)
1836 /* We are in the scope of some `template <...>' header. */
1838 int context_depth
1839 = template_class_depth_real (TYPE_CONTEXT (type),
1840 /*count_specializations=*/1);
1842 if (template_header_count <= context_depth)
1843 /* This is OK; the template headers are for the context. We
1844 are actually too lenient here; like
1845 check_explicit_specialization we should consider the number
1846 of template types included in the actual declaration. For
1847 example,
1849 template <class T> struct S {
1850 template <class U> template <class V>
1851 struct I {};
1854 is invalid, but:
1856 template <class T> struct S {
1857 template <class U> struct I;
1860 template <class T> template <class U.
1861 struct S<T>::I {};
1863 is not. */
1865 else if (template_header_count > context_depth + 1)
1866 /* There are two many template parameter lists. */
1867 error ("too many template parameter lists in declaration of `%T'", type);
1871 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1872 parameters. These are represented in the same format used for
1873 DECL_TEMPLATE_PARMS. */
1875 int comp_template_parms (parms1, parms2)
1876 tree parms1;
1877 tree parms2;
1879 tree p1;
1880 tree p2;
1882 if (parms1 == parms2)
1883 return 1;
1885 for (p1 = parms1, p2 = parms2;
1886 p1 != NULL_TREE && p2 != NULL_TREE;
1887 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1889 tree t1 = TREE_VALUE (p1);
1890 tree t2 = TREE_VALUE (p2);
1891 int i;
1893 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1894 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1896 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1897 return 0;
1899 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1901 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1902 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1904 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1905 return 0;
1907 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1908 continue;
1909 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1910 return 0;
1914 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1915 /* One set of parameters has more parameters lists than the
1916 other. */
1917 return 0;
1919 return 1;
1922 /* Complain if DECL shadows a template parameter.
1924 [temp.local]: A template-parameter shall not be redeclared within its
1925 scope (including nested scopes). */
1927 void
1928 check_template_shadow (decl)
1929 tree decl;
1931 tree olddecl;
1933 /* If we're not in a template, we can't possibly shadow a template
1934 parameter. */
1935 if (!current_template_parms)
1936 return;
1938 /* Figure out what we're shadowing. */
1939 if (TREE_CODE (decl) == OVERLOAD)
1940 decl = OVL_CURRENT (decl);
1941 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1943 /* If there's no previous binding for this name, we're not shadowing
1944 anything, let alone a template parameter. */
1945 if (!olddecl)
1946 return;
1948 /* If we're not shadowing a template parameter, we're done. Note
1949 that OLDDECL might be an OVERLOAD (or perhaps even an
1950 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1951 node. */
1952 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1953 return;
1955 /* We check for decl != olddecl to avoid bogus errors for using a
1956 name inside a class. We check TPFI to avoid duplicate errors for
1957 inline member templates. */
1958 if (decl == olddecl
1959 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1960 return;
1962 cp_error_at ("declaration of `%#D'", decl);
1963 cp_error_at (" shadows template parm `%#D'", olddecl);
1966 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1967 ORIG_LEVEL, DECL, and TYPE. */
1969 static tree
1970 build_template_parm_index (index, level, orig_level, decl, type)
1971 int index;
1972 int level;
1973 int orig_level;
1974 tree decl;
1975 tree type;
1977 tree t = make_node (TEMPLATE_PARM_INDEX);
1978 TEMPLATE_PARM_IDX (t) = index;
1979 TEMPLATE_PARM_LEVEL (t) = level;
1980 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1981 TEMPLATE_PARM_DECL (t) = decl;
1982 TREE_TYPE (t) = type;
1984 return t;
1987 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1988 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1989 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1990 new one is created. */
1992 static tree
1993 reduce_template_parm_level (index, type, levels)
1994 tree index;
1995 tree type;
1996 int levels;
1998 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1999 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2000 != TEMPLATE_PARM_LEVEL (index) - levels))
2002 tree decl
2003 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
2004 DECL_NAME (TEMPLATE_PARM_DECL (index)),
2005 type);
2006 tree t
2007 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2008 TEMPLATE_PARM_LEVEL (index) - levels,
2009 TEMPLATE_PARM_ORIG_LEVEL (index),
2010 decl, type);
2011 TEMPLATE_PARM_DESCENDANTS (index) = t;
2013 DECL_ARTIFICIAL (decl) = 1;
2014 SET_DECL_TEMPLATE_PARM_P (decl);
2016 /* Template template parameters need this. */
2017 DECL_TEMPLATE_PARMS (decl)
2018 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2021 return TEMPLATE_PARM_DESCENDANTS (index);
2024 /* Process information from new template parameter NEXT and append it to the
2025 LIST being built. */
2027 tree
2028 process_template_parm (list, next)
2029 tree list, next;
2031 tree parm;
2032 tree decl = 0;
2033 tree defval;
2034 int is_type, idx;
2036 parm = next;
2037 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
2038 defval = TREE_PURPOSE (parm);
2039 parm = TREE_VALUE (parm);
2040 is_type = TREE_PURPOSE (parm) == class_type_node;
2042 if (list)
2044 tree p = TREE_VALUE (tree_last (list));
2046 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2047 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2048 else
2049 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2050 ++idx;
2052 else
2053 idx = 0;
2055 if (!is_type)
2057 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
2058 /* is a const-param */
2059 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
2060 PARM, 0, NULL);
2061 SET_DECL_TEMPLATE_PARM_P (parm);
2063 /* [temp.param]
2065 The top-level cv-qualifiers on the template-parameter are
2066 ignored when determining its type. */
2067 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2069 /* A template parameter is not modifiable. */
2070 TREE_READONLY (parm) = 1;
2071 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2072 TREE_TYPE (parm) = void_type_node;
2073 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2074 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2075 = build_template_parm_index (idx, processing_template_decl,
2076 processing_template_decl,
2077 decl, TREE_TYPE (parm));
2079 else
2081 tree t;
2082 parm = TREE_VALUE (parm);
2084 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2086 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2087 /* This is for distinguishing between real templates and template
2088 template parameters */
2089 TREE_TYPE (parm) = t;
2090 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2091 decl = parm;
2093 else
2095 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2096 /* parm is either IDENTIFIER_NODE or NULL_TREE */
2097 decl = build_decl (TYPE_DECL, parm, t);
2100 TYPE_NAME (t) = decl;
2101 TYPE_STUB_DECL (t) = decl;
2102 parm = decl;
2103 TEMPLATE_TYPE_PARM_INDEX (t)
2104 = build_template_parm_index (idx, processing_template_decl,
2105 processing_template_decl,
2106 decl, TREE_TYPE (parm));
2108 DECL_ARTIFICIAL (decl) = 1;
2109 SET_DECL_TEMPLATE_PARM_P (decl);
2110 pushdecl (decl);
2111 parm = build_tree_list (defval, parm);
2112 return chainon (list, parm);
2115 /* The end of a template parameter list has been reached. Process the
2116 tree list into a parameter vector, converting each parameter into a more
2117 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2118 as PARM_DECLs. */
2120 tree
2121 end_template_parm_list (parms)
2122 tree parms;
2124 int nparms;
2125 tree parm, next;
2126 tree saved_parmlist = make_tree_vec (list_length (parms));
2128 current_template_parms
2129 = tree_cons (size_int (processing_template_decl),
2130 saved_parmlist, current_template_parms);
2132 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2134 next = TREE_CHAIN (parm);
2135 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2136 TREE_CHAIN (parm) = NULL_TREE;
2139 --processing_template_parmlist;
2141 return saved_parmlist;
2144 /* end_template_decl is called after a template declaration is seen. */
2146 void
2147 end_template_decl ()
2149 reset_specialization ();
2151 if (! processing_template_decl)
2152 return;
2154 /* This matches the pushlevel in begin_template_parm_list. */
2155 finish_scope ();
2157 --processing_template_decl;
2158 current_template_parms = TREE_CHAIN (current_template_parms);
2161 /* Given a template argument vector containing the template PARMS.
2162 The innermost PARMS are given first. */
2164 tree
2165 current_template_args ()
2167 tree header;
2168 tree args = NULL_TREE;
2169 int length = TMPL_PARMS_DEPTH (current_template_parms);
2170 int l = length;
2172 /* If there is only one level of template parameters, we do not
2173 create a TREE_VEC of TREE_VECs. Instead, we return a single
2174 TREE_VEC containing the arguments. */
2175 if (length > 1)
2176 args = make_tree_vec (length);
2178 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2180 tree a = copy_node (TREE_VALUE (header));
2181 int i;
2183 TREE_TYPE (a) = NULL_TREE;
2184 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2186 tree t = TREE_VEC_ELT (a, i);
2188 /* T will be a list if we are called from within a
2189 begin/end_template_parm_list pair, but a vector directly
2190 if within a begin/end_member_template_processing pair. */
2191 if (TREE_CODE (t) == TREE_LIST)
2193 t = TREE_VALUE (t);
2195 if (TREE_CODE (t) == TYPE_DECL
2196 || TREE_CODE (t) == TEMPLATE_DECL)
2197 t = TREE_TYPE (t);
2198 else
2199 t = DECL_INITIAL (t);
2200 TREE_VEC_ELT (a, i) = t;
2204 if (length > 1)
2205 TREE_VEC_ELT (args, --l) = a;
2206 else
2207 args = a;
2210 return args;
2213 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2214 template PARMS. Used by push_template_decl below. */
2216 static tree
2217 build_template_decl (decl, parms)
2218 tree decl;
2219 tree parms;
2221 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2222 DECL_TEMPLATE_PARMS (tmpl) = parms;
2223 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2224 if (DECL_LANG_SPECIFIC (decl))
2226 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2227 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2228 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2229 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2230 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2231 if (DECL_OVERLOADED_OPERATOR_P (decl))
2232 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2233 DECL_OVERLOADED_OPERATOR_P (decl));
2236 return tmpl;
2239 struct template_parm_data
2241 /* The level of the template parameters we are currently
2242 processing. */
2243 int level;
2245 /* The index of the specialization argument we are currently
2246 processing. */
2247 int current_arg;
2249 /* An array whose size is the number of template parameters. The
2250 elements are nonzero if the parameter has been used in any one
2251 of the arguments processed so far. */
2252 int* parms;
2254 /* An array whose size is the number of template arguments. The
2255 elements are nonzero if the argument makes use of template
2256 parameters of this level. */
2257 int* arg_uses_template_parms;
2260 /* Subroutine of push_template_decl used to see if each template
2261 parameter in a partial specialization is used in the explicit
2262 argument list. If T is of the LEVEL given in DATA (which is
2263 treated as a template_parm_data*), then DATA->PARMS is marked
2264 appropriately. */
2266 static int
2267 mark_template_parm (t, data)
2268 tree t;
2269 void* data;
2271 int level;
2272 int idx;
2273 struct template_parm_data* tpd = (struct template_parm_data*) data;
2275 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2277 level = TEMPLATE_PARM_LEVEL (t);
2278 idx = TEMPLATE_PARM_IDX (t);
2280 else
2282 level = TEMPLATE_TYPE_LEVEL (t);
2283 idx = TEMPLATE_TYPE_IDX (t);
2286 if (level == tpd->level)
2288 tpd->parms[idx] = 1;
2289 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2292 /* Return zero so that for_each_template_parm will continue the
2293 traversal of the tree; we want to mark *every* template parm. */
2294 return 0;
2297 /* Process the partial specialization DECL. */
2299 static tree
2300 process_partial_specialization (decl)
2301 tree decl;
2303 tree type = TREE_TYPE (decl);
2304 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2305 tree specargs = CLASSTYPE_TI_ARGS (type);
2306 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2307 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2308 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2309 int nargs = TREE_VEC_LENGTH (inner_args);
2310 int ntparms = TREE_VEC_LENGTH (inner_parms);
2311 int i;
2312 int did_error_intro = 0;
2313 struct template_parm_data tpd;
2314 struct template_parm_data tpd2;
2316 /* We check that each of the template parameters given in the
2317 partial specialization is used in the argument list to the
2318 specialization. For example:
2320 template <class T> struct S;
2321 template <class T> struct S<T*>;
2323 The second declaration is OK because `T*' uses the template
2324 parameter T, whereas
2326 template <class T> struct S<int>;
2328 is no good. Even trickier is:
2330 template <class T>
2331 struct S1
2333 template <class U>
2334 struct S2;
2335 template <class U>
2336 struct S2<T>;
2339 The S2<T> declaration is actually invalid; it is a
2340 full-specialization. Of course,
2342 template <class U>
2343 struct S2<T (*)(U)>;
2345 or some such would have been OK. */
2346 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2347 tpd.parms = alloca (sizeof (int) * ntparms);
2348 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2350 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2351 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2352 for (i = 0; i < nargs; ++i)
2354 tpd.current_arg = i;
2355 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2356 &mark_template_parm,
2357 &tpd,
2358 NULL);
2360 for (i = 0; i < ntparms; ++i)
2361 if (tpd.parms[i] == 0)
2363 /* One of the template parms was not used in the
2364 specialization. */
2365 if (!did_error_intro)
2367 error ("template parameters not used in partial specialization:");
2368 did_error_intro = 1;
2371 error (" `%D'",
2372 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2375 /* [temp.class.spec]
2377 The argument list of the specialization shall not be identical to
2378 the implicit argument list of the primary template. */
2379 if (comp_template_args
2380 (inner_args,
2381 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2382 (maintmpl)))))
2383 error ("partial specialization `%T' does not specialize any template arguments", type);
2385 /* [temp.class.spec]
2387 A partially specialized non-type argument expression shall not
2388 involve template parameters of the partial specialization except
2389 when the argument expression is a simple identifier.
2391 The type of a template parameter corresponding to a specialized
2392 non-type argument shall not be dependent on a parameter of the
2393 specialization. */
2394 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2395 tpd2.parms = 0;
2396 for (i = 0; i < nargs; ++i)
2398 tree arg = TREE_VEC_ELT (inner_args, i);
2399 if (/* These first two lines are the `non-type' bit. */
2400 !TYPE_P (arg)
2401 && TREE_CODE (arg) != TEMPLATE_DECL
2402 /* This next line is the `argument expression is not just a
2403 simple identifier' condition and also the `specialized
2404 non-type argument' bit. */
2405 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2407 if (tpd.arg_uses_template_parms[i])
2408 error ("template argument `%E' involves template parameter(s)", arg);
2409 else
2411 /* Look at the corresponding template parameter,
2412 marking which template parameters its type depends
2413 upon. */
2414 tree type =
2415 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2416 i)));
2418 if (!tpd2.parms)
2420 /* We haven't yet initialized TPD2. Do so now. */
2421 tpd2.arg_uses_template_parms
2422 = (int*) alloca (sizeof (int) * nargs);
2423 /* The number of parameters here is the number in the
2424 main template, which, as checked in the assertion
2425 above, is NARGS. */
2426 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2427 tpd2.level =
2428 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2431 /* Mark the template parameters. But this time, we're
2432 looking for the template parameters of the main
2433 template, not in the specialization. */
2434 tpd2.current_arg = i;
2435 tpd2.arg_uses_template_parms[i] = 0;
2436 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2437 for_each_template_parm (type,
2438 &mark_template_parm,
2439 &tpd2,
2440 NULL);
2442 if (tpd2.arg_uses_template_parms [i])
2444 /* The type depended on some template parameters.
2445 If they are fully specialized in the
2446 specialization, that's OK. */
2447 int j;
2448 for (j = 0; j < nargs; ++j)
2449 if (tpd2.parms[j] != 0
2450 && tpd.arg_uses_template_parms [j])
2452 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2453 type,
2454 arg);
2455 break;
2462 if (retrieve_specialization (maintmpl, specargs))
2463 /* We've already got this specialization. */
2464 return decl;
2466 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2467 = tree_cons (inner_args, inner_parms,
2468 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2469 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2470 return decl;
2473 /* Check that a template declaration's use of default arguments is not
2474 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2475 nonzero if DECL is the thing declared by a primary template.
2476 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2478 static void
2479 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2480 tree decl;
2481 tree parms;
2482 int is_primary;
2483 int is_partial;
2485 const char *msg;
2486 int last_level_to_check;
2487 tree parm_level;
2489 /* [temp.param]
2491 A default template-argument shall not be specified in a
2492 function template declaration or a function template definition, nor
2493 in the template-parameter-list of the definition of a member of a
2494 class template. */
2496 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2497 /* You can't have a function template declaration in a local
2498 scope, nor you can you define a member of a class template in a
2499 local scope. */
2500 return;
2502 if (current_class_type
2503 && !TYPE_BEING_DEFINED (current_class_type)
2504 && DECL_LANG_SPECIFIC (decl)
2505 /* If this is either a friend defined in the scope of the class
2506 or a member function. */
2507 && ((DECL_CONTEXT (decl)
2508 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2509 || (DECL_FRIEND_CONTEXT (decl)
2510 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2511 current_class_type)))
2512 /* And, if it was a member function, it really was defined in
2513 the scope of the class. */
2514 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2515 /* We already checked these parameters when the template was
2516 declared, so there's no need to do it again now. This function
2517 was defined in class scope, but we're processing it's body now
2518 that the class is complete. */
2519 return;
2521 /* [temp.param]
2523 If a template-parameter has a default template-argument, all
2524 subsequent template-parameters shall have a default
2525 template-argument supplied. */
2526 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2528 tree inner_parms = TREE_VALUE (parm_level);
2529 int ntparms = TREE_VEC_LENGTH (inner_parms);
2530 int seen_def_arg_p = 0;
2531 int i;
2533 for (i = 0; i < ntparms; ++i)
2535 tree parm = TREE_VEC_ELT (inner_parms, i);
2536 if (TREE_PURPOSE (parm))
2537 seen_def_arg_p = 1;
2538 else if (seen_def_arg_p)
2540 error ("no default argument for `%D'", TREE_VALUE (parm));
2541 /* For better subsequent error-recovery, we indicate that
2542 there should have been a default argument. */
2543 TREE_PURPOSE (parm) = error_mark_node;
2548 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2549 /* For an ordinary class template, default template arguments are
2550 allowed at the innermost level, e.g.:
2551 template <class T = int>
2552 struct S {};
2553 but, in a partial specialization, they're not allowed even
2554 there, as we have in [temp.class.spec]:
2556 The template parameter list of a specialization shall not
2557 contain default template argument values.
2559 So, for a partial specialization, or for a function template,
2560 we look at all of them. */
2562 else
2563 /* But, for a primary class template that is not a partial
2564 specialization we look at all template parameters except the
2565 innermost ones. */
2566 parms = TREE_CHAIN (parms);
2568 /* Figure out what error message to issue. */
2569 if (TREE_CODE (decl) == FUNCTION_DECL)
2570 msg = "default template arguments may not be used in function templates";
2571 else if (is_partial)
2572 msg = "default template arguments may not be used in partial specializations";
2573 else
2574 msg = "default argument for template parameter for class enclosing `%D'";
2576 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2577 /* If we're inside a class definition, there's no need to
2578 examine the parameters to the class itself. On the one
2579 hand, they will be checked when the class is defined, and,
2580 on the other, default arguments are valid in things like:
2581 template <class T = double>
2582 struct S { template <class U> void f(U); };
2583 Here the default argument for `S' has no bearing on the
2584 declaration of `f'. */
2585 last_level_to_check = template_class_depth (current_class_type) + 1;
2586 else
2587 /* Check everything. */
2588 last_level_to_check = 0;
2590 for (parm_level = parms;
2591 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2592 parm_level = TREE_CHAIN (parm_level))
2594 tree inner_parms = TREE_VALUE (parm_level);
2595 int i;
2596 int ntparms;
2598 ntparms = TREE_VEC_LENGTH (inner_parms);
2599 for (i = 0; i < ntparms; ++i)
2600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2602 if (msg)
2604 error (msg, decl);
2605 msg = 0;
2608 /* Clear out the default argument so that we are not
2609 confused later. */
2610 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2613 /* At this point, if we're still interested in issuing messages,
2614 they must apply to classes surrounding the object declared. */
2615 if (msg)
2616 msg = "default argument for template parameter for class enclosing `%D'";
2620 /* Worker for push_template_decl_real, called via
2621 for_each_template_parm. DATA is really an int, indicating the
2622 level of the parameters we are interested in. If T is a template
2623 parameter of that level, return nonzero. */
2625 static int
2626 template_parm_this_level_p (t, data)
2627 tree t;
2628 void *data;
2630 int this_level = *(int *)data;
2631 int level;
2633 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2634 level = TEMPLATE_PARM_LEVEL (t);
2635 else
2636 level = TEMPLATE_TYPE_LEVEL (t);
2637 return level == this_level;
2640 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2641 parameters given by current_template_args, or reuses a
2642 previously existing one, if appropriate. Returns the DECL, or an
2643 equivalent one, if it is replaced via a call to duplicate_decls.
2645 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2647 tree
2648 push_template_decl_real (decl, is_friend)
2649 tree decl;
2650 int is_friend;
2652 tree tmpl;
2653 tree args;
2654 tree info;
2655 tree ctx;
2656 int primary;
2657 int is_partial;
2658 int new_template_p = 0;
2660 /* See if this is a partial specialization. */
2661 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2662 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2663 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2665 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2667 if (is_friend)
2668 /* For a friend, we want the context of the friend function, not
2669 the type of which it is a friend. */
2670 ctx = DECL_CONTEXT (decl);
2671 else if (CP_DECL_CONTEXT (decl)
2672 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2673 /* In the case of a virtual function, we want the class in which
2674 it is defined. */
2675 ctx = CP_DECL_CONTEXT (decl);
2676 else
2677 /* Otherwise, if we're currently defining some class, the DECL
2678 is assumed to be a member of the class. */
2679 ctx = current_scope ();
2681 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2682 ctx = NULL_TREE;
2684 if (!DECL_CONTEXT (decl))
2685 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2687 /* See if this is a primary template. */
2688 primary = template_parm_scope_p ();
2690 if (primary)
2692 if (current_lang_name == lang_name_c)
2693 error ("template with C linkage");
2694 else if (TREE_CODE (decl) == TYPE_DECL
2695 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2696 error ("template class without a name");
2697 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2698 && CLASS_TYPE_P (TREE_TYPE (decl)))
2699 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2700 || TREE_CODE (decl) == FUNCTION_DECL)
2701 /* OK */;
2702 else
2703 error ("template declaration of `%#D'", decl);
2706 /* Check to see that the rules regarding the use of default
2707 arguments are not being violated. */
2708 check_default_tmpl_args (decl, current_template_parms,
2709 primary, is_partial);
2711 if (is_partial)
2712 return process_partial_specialization (decl);
2714 args = current_template_args ();
2716 if (!ctx
2717 || TREE_CODE (ctx) == FUNCTION_DECL
2718 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2719 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2721 if (DECL_LANG_SPECIFIC (decl)
2722 && DECL_TEMPLATE_INFO (decl)
2723 && DECL_TI_TEMPLATE (decl))
2724 tmpl = DECL_TI_TEMPLATE (decl);
2725 /* If DECL is a TYPE_DECL for a class-template, then there won't
2726 be DECL_LANG_SPECIFIC. The information equivalent to
2727 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2728 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2729 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2730 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2732 /* Since a template declaration already existed for this
2733 class-type, we must be redeclaring it here. Make sure
2734 that the redeclaration is valid. */
2735 redeclare_class_template (TREE_TYPE (decl),
2736 current_template_parms);
2737 /* We don't need to create a new TEMPLATE_DECL; just use the
2738 one we already had. */
2739 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2741 else
2743 tmpl = build_template_decl (decl, current_template_parms);
2744 new_template_p = 1;
2746 if (DECL_LANG_SPECIFIC (decl)
2747 && DECL_TEMPLATE_SPECIALIZATION (decl))
2749 /* A specialization of a member template of a template
2750 class. */
2751 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2752 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2753 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2757 else
2759 tree a, t, current, parms;
2760 int i;
2762 if (TREE_CODE (decl) == TYPE_DECL)
2764 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2765 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2766 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2767 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2768 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2769 else
2771 error ("`%D' does not declare a template type", decl);
2772 return decl;
2775 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2777 error ("template definition of non-template `%#D'", decl);
2778 return decl;
2780 else
2781 tmpl = DECL_TI_TEMPLATE (decl);
2783 if (is_member_template (tmpl)
2784 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2785 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2786 && DECL_TEMPLATE_SPECIALIZATION (decl))
2788 tree new_tmpl;
2790 /* The declaration is a specialization of a member
2791 template, declared outside the class. Therefore, the
2792 innermost template arguments will be NULL, so we
2793 replace them with the arguments determined by the
2794 earlier call to check_explicit_specialization. */
2795 args = DECL_TI_ARGS (decl);
2797 new_tmpl
2798 = build_template_decl (decl, current_template_parms);
2799 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2800 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2801 DECL_TI_TEMPLATE (decl) = new_tmpl;
2802 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2803 DECL_TEMPLATE_INFO (new_tmpl)
2804 = tree_cons (tmpl, args, NULL_TREE);
2806 register_specialization (new_tmpl,
2807 most_general_template (tmpl),
2808 args);
2809 return decl;
2812 /* Make sure the template headers we got make sense. */
2814 parms = DECL_TEMPLATE_PARMS (tmpl);
2815 i = TMPL_PARMS_DEPTH (parms);
2816 if (TMPL_ARGS_DEPTH (args) != i)
2818 error ("expected %d levels of template parms for `%#D', got %d",
2819 i, decl, TMPL_ARGS_DEPTH (args));
2821 else
2822 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2824 a = TMPL_ARGS_LEVEL (args, i);
2825 t = INNERMOST_TEMPLATE_PARMS (parms);
2827 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2829 if (current == decl)
2830 error ("got %d template parameters for `%#D'",
2831 TREE_VEC_LENGTH (a), decl);
2832 else
2833 error ("got %d template parameters for `%#T'",
2834 TREE_VEC_LENGTH (a), current);
2835 error (" but %d required", TREE_VEC_LENGTH (t));
2838 /* Perhaps we should also check that the parms are used in the
2839 appropriate qualifying scopes in the declarator? */
2841 if (current == decl)
2842 current = ctx;
2843 else
2844 current = TYPE_CONTEXT (current);
2848 DECL_TEMPLATE_RESULT (tmpl) = decl;
2849 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2851 /* Push template declarations for global functions and types. Note
2852 that we do not try to push a global template friend declared in a
2853 template class; such a thing may well depend on the template
2854 parameters of the class. */
2855 if (new_template_p && !ctx
2856 && !(is_friend && template_class_depth (current_class_type) > 0))
2857 tmpl = pushdecl_namespace_level (tmpl);
2859 if (primary)
2861 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2862 if (DECL_CONV_FN_P (tmpl))
2864 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2866 /* It is a conversion operator. See if the type converted to
2867 depends on innermost template operands. */
2869 if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)),
2870 template_parm_this_level_p,
2871 &depth,
2872 NULL))
2873 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
2877 info = tree_cons (tmpl, args, NULL_TREE);
2879 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2881 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2882 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2883 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2884 /* Don't change the name if we've already set it up. */
2885 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2886 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2888 else if (DECL_LANG_SPECIFIC (decl))
2889 DECL_TEMPLATE_INFO (decl) = info;
2891 return DECL_TEMPLATE_RESULT (tmpl);
2894 tree
2895 push_template_decl (decl)
2896 tree decl;
2898 return push_template_decl_real (decl, 0);
2901 /* Called when a class template TYPE is redeclared with the indicated
2902 template PARMS, e.g.:
2904 template <class T> struct S;
2905 template <class T> struct S {}; */
2907 void
2908 redeclare_class_template (type, parms)
2909 tree type;
2910 tree parms;
2912 tree tmpl;
2913 tree tmpl_parms;
2914 int i;
2916 if (!TYPE_TEMPLATE_INFO (type))
2918 error ("`%T' is not a template type", type);
2919 return;
2922 tmpl = TYPE_TI_TEMPLATE (type);
2923 if (!PRIMARY_TEMPLATE_P (tmpl))
2924 /* The type is nested in some template class. Nothing to worry
2925 about here; there are no new template parameters for the nested
2926 type. */
2927 return;
2929 parms = INNERMOST_TEMPLATE_PARMS (parms);
2930 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2932 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2934 cp_error_at ("previous declaration `%D'", tmpl);
2935 error ("used %d template parameter%s instead of %d",
2936 TREE_VEC_LENGTH (tmpl_parms),
2937 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2938 TREE_VEC_LENGTH (parms));
2939 return;
2942 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2944 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2945 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2946 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2947 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2949 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2951 cp_error_at ("template parameter `%#D'", tmpl_parm);
2952 error ("redeclared here as `%#D'", parm);
2953 return;
2956 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2958 /* We have in [temp.param]:
2960 A template-parameter may not be given default arguments
2961 by two different declarations in the same scope. */
2962 error ("redefinition of default argument for `%#D'", parm);
2963 cp_error_at (" original definition appeared here", tmpl_parm);
2964 return;
2967 if (parm_default != NULL_TREE)
2968 /* Update the previous template parameters (which are the ones
2969 that will really count) with the new default value. */
2970 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2971 else if (tmpl_default != NULL_TREE)
2972 /* Update the new parameters, too; they'll be used as the
2973 parameters for any members. */
2974 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2978 /* Attempt to convert the non-type template parameter EXPR to the
2979 indicated TYPE. If the conversion is successful, return the
2980 converted value. If the conversion is unsuccessful, return
2981 NULL_TREE if we issued an error message, or error_mark_node if we
2982 did not. We issue error messages for out-and-out bad template
2983 parameters, but not simply because the conversion failed, since we
2984 might be just trying to do argument deduction. By the time this
2985 function is called, neither TYPE nor EXPR may make use of template
2986 parameters. */
2988 static tree
2989 convert_nontype_argument (type, expr)
2990 tree type;
2991 tree expr;
2993 tree expr_type = TREE_TYPE (expr);
2995 /* A template-argument for a non-type, non-template
2996 template-parameter shall be one of:
2998 --an integral constant-expression of integral or enumeration
2999 type; or
3001 --the name of a non-type template-parameter; or
3003 --the name of an object or function with external linkage,
3004 including function templates and function template-ids but
3005 excluding non-static class members, expressed as id-expression;
3008 --the address of an object or function with external linkage,
3009 including function templates and function template-ids but
3010 excluding non-static class members, expressed as & id-expression
3011 where the & is optional if the name refers to a function or
3012 array; or
3014 --a pointer to member expressed as described in _expr.unary.op_. */
3016 /* An integral constant-expression can include const variables or
3017 enumerators. Simplify things by folding them to their values,
3018 unless we're about to bind the declaration to a reference
3019 parameter. */
3020 if (INTEGRAL_TYPE_P (expr_type)
3021 && TREE_CODE (type) != REFERENCE_TYPE)
3022 expr = decl_constant_value (expr);
3024 if (is_overloaded_fn (expr))
3025 /* OK for now. We'll check that it has external linkage later.
3026 Check this first since if expr_type is the unknown_type_node
3027 we would otherwise complain below. */
3029 else if (TYPE_PTRMEM_P (expr_type)
3030 || TYPE_PTRMEMFUNC_P (expr_type))
3032 if (TREE_CODE (expr) != PTRMEM_CST)
3033 goto bad_argument;
3035 else if (TYPE_PTR_P (expr_type)
3036 || TYPE_PTRMEM_P (expr_type)
3037 || TREE_CODE (expr_type) == ARRAY_TYPE
3038 || TREE_CODE (type) == REFERENCE_TYPE
3039 /* If expr is the address of an overloaded function, we
3040 will get the unknown_type_node at this point. */
3041 || expr_type == unknown_type_node)
3043 tree referent;
3044 tree e = expr;
3045 STRIP_NOPS (e);
3047 if (TREE_CODE (expr_type) == ARRAY_TYPE
3048 || (TREE_CODE (type) == REFERENCE_TYPE
3049 && TREE_CODE (e) != ADDR_EXPR))
3050 referent = e;
3051 else
3053 if (TREE_CODE (e) != ADDR_EXPR)
3055 bad_argument:
3056 error ("`%E' is not a valid template argument", expr);
3057 if (TYPE_PTR_P (expr_type))
3059 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
3060 error ("it must be the address of a function with external linkage");
3061 else
3062 error ("it must be the address of an object with external linkage");
3064 else if (TYPE_PTRMEM_P (expr_type)
3065 || TYPE_PTRMEMFUNC_P (expr_type))
3066 error ("it must be a pointer-to-member of the form `&X::Y'");
3068 return NULL_TREE;
3071 referent = TREE_OPERAND (e, 0);
3072 STRIP_NOPS (referent);
3075 if (TREE_CODE (referent) == STRING_CST)
3077 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
3078 referent);
3079 return NULL_TREE;
3082 if (is_overloaded_fn (referent))
3083 /* We'll check that it has external linkage later. */
3085 else if (TREE_CODE (referent) != VAR_DECL)
3086 goto bad_argument;
3087 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
3089 error ("address of non-extern `%E' cannot be used as template argument", referent);
3090 return error_mark_node;
3093 else if (INTEGRAL_TYPE_P (expr_type)
3094 || TYPE_PTRMEM_P (expr_type)
3095 || TYPE_PTRMEMFUNC_P (expr_type))
3097 if (! TREE_CONSTANT (expr))
3099 non_constant:
3100 error ("non-constant `%E' cannot be used as template argument",
3101 expr);
3102 return NULL_TREE;
3105 else
3107 error ("object `%E' cannot be used as template argument", expr);
3108 return NULL_TREE;
3111 switch (TREE_CODE (type))
3113 case INTEGER_TYPE:
3114 case BOOLEAN_TYPE:
3115 case ENUMERAL_TYPE:
3116 /* For a non-type template-parameter of integral or enumeration
3117 type, integral promotions (_conv.prom_) and integral
3118 conversions (_conv.integral_) are applied. */
3119 if (!INTEGRAL_TYPE_P (expr_type))
3120 return error_mark_node;
3122 /* It's safe to call digest_init in this case; we know we're
3123 just converting one integral constant expression to another. */
3124 expr = digest_init (type, expr, (tree*) 0);
3126 if (TREE_CODE (expr) != INTEGER_CST)
3127 /* Curiously, some TREE_CONSTANT integral expressions do not
3128 simplify to integer constants. For example, `3 % 0',
3129 remains a TRUNC_MOD_EXPR. */
3130 goto non_constant;
3132 return expr;
3134 case POINTER_TYPE:
3136 tree type_pointed_to = TREE_TYPE (type);
3138 if (TYPE_PTRMEM_P (type))
3140 tree e;
3142 /* For a non-type template-parameter of type pointer to data
3143 member, qualification conversions (_conv.qual_) are
3144 applied. */
3145 e = perform_qualification_conversions (type, expr);
3146 if (TREE_CODE (e) == NOP_EXPR)
3147 /* The call to perform_qualification_conversions will
3148 insert a NOP_EXPR over EXPR to do express conversion,
3149 if necessary. But, that will confuse us if we use
3150 this (converted) template parameter to instantiate
3151 another template; then the thing will not look like a
3152 valid template argument. So, just make a new
3153 constant, of the appropriate type. */
3154 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3155 return e;
3157 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3159 /* For a non-type template-parameter of type pointer to
3160 function, only the function-to-pointer conversion
3161 (_conv.func_) is applied. If the template-argument
3162 represents a set of overloaded functions (or a pointer to
3163 such), the matching function is selected from the set
3164 (_over.over_). */
3165 tree fns;
3166 tree fn;
3168 if (TREE_CODE (expr) == ADDR_EXPR)
3169 fns = TREE_OPERAND (expr, 0);
3170 else
3171 fns = expr;
3173 fn = instantiate_type (type_pointed_to, fns, tf_none);
3175 if (fn == error_mark_node)
3176 return error_mark_node;
3178 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3180 if (really_overloaded_fn (fns))
3181 return error_mark_node;
3182 else
3183 goto bad_argument;
3186 expr = build_unary_op (ADDR_EXPR, fn, 0);
3188 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3190 return expr;
3192 else
3194 /* For a non-type template-parameter of type pointer to
3195 object, qualification conversions (_conv.qual_) and the
3196 array-to-pointer conversion (_conv.array_) are applied.
3197 [Note: In particular, neither the null pointer conversion
3198 (_conv.ptr_) nor the derived-to-base conversion
3199 (_conv.ptr_) are applied. Although 0 is a valid
3200 template-argument for a non-type template-parameter of
3201 integral type, it is not a valid template-argument for a
3202 non-type template-parameter of pointer type.]
3204 The call to decay_conversion performs the
3205 array-to-pointer conversion, if appropriate. */
3206 expr = decay_conversion (expr);
3208 if (expr == error_mark_node)
3209 return error_mark_node;
3210 else
3211 return perform_qualification_conversions (type, expr);
3214 break;
3216 case REFERENCE_TYPE:
3218 tree type_referred_to = TREE_TYPE (type);
3220 /* If this expression already has reference type, get the
3221 underling object. */
3222 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3224 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3225 expr = TREE_OPERAND (expr, 0);
3226 expr_type = TREE_TYPE (expr);
3229 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3231 /* For a non-type template-parameter of type reference to
3232 function, no conversions apply. If the
3233 template-argument represents a set of overloaded
3234 functions, the matching function is selected from the
3235 set (_over.over_). */
3236 tree fn;
3238 fn = instantiate_type (type_referred_to, expr, tf_none);
3240 if (fn == error_mark_node)
3241 return error_mark_node;
3243 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3245 if (really_overloaded_fn (expr))
3246 /* Don't issue an error here; we might get a different
3247 function if the overloading had worked out
3248 differently. */
3249 return error_mark_node;
3250 else
3251 goto bad_argument;
3254 my_friendly_assert (same_type_p (type_referred_to,
3255 TREE_TYPE (fn)),
3258 expr = fn;
3260 else
3262 /* For a non-type template-parameter of type reference to
3263 object, no conversions apply. The type referred to by the
3264 reference may be more cv-qualified than the (otherwise
3265 identical) type of the template-argument. The
3266 template-parameter is bound directly to the
3267 template-argument, which must be an lvalue. */
3268 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3269 TYPE_MAIN_VARIANT (type_referred_to))
3270 || !at_least_as_qualified_p (type_referred_to,
3271 expr_type)
3272 || !real_lvalue_p (expr))
3273 return error_mark_node;
3276 cxx_mark_addressable (expr);
3277 return build1 (ADDR_EXPR, type, expr);
3279 break;
3281 case RECORD_TYPE:
3283 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3285 /* For a non-type template-parameter of type pointer to member
3286 function, no conversions apply. If the template-argument
3287 represents a set of overloaded member functions, the
3288 matching member function is selected from the set
3289 (_over.over_). */
3291 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3292 expr_type != unknown_type_node)
3293 return error_mark_node;
3295 if (TREE_CODE (expr) == PTRMEM_CST)
3297 /* A ptr-to-member constant. */
3298 if (!same_type_p (type, expr_type))
3299 return error_mark_node;
3300 else
3301 return expr;
3304 if (TREE_CODE (expr) != ADDR_EXPR)
3305 return error_mark_node;
3307 expr = instantiate_type (type, expr, tf_none);
3309 if (expr == error_mark_node)
3310 return error_mark_node;
3312 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3314 return expr;
3316 break;
3318 default:
3319 /* All non-type parameters must have one of these types. */
3320 abort ();
3321 break;
3324 return error_mark_node;
3327 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3328 template template parameters. Both PARM_PARMS and ARG_PARMS are
3329 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3330 or PARM_DECL.
3332 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3333 the case, then extra parameters must have default arguments.
3335 Consider the example:
3336 template <class T, class Allocator = allocator> class vector;
3337 template<template <class U> class TT> class C;
3339 C<vector> is a valid instantiation. PARM_PARMS for the above code
3340 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3341 T and Allocator) and OUTER_ARGS contains the argument that is used to
3342 substitute the TT parameter. */
3344 static int
3345 coerce_template_template_parms (parm_parms, arg_parms, complain,
3346 in_decl, outer_args)
3347 tree parm_parms, arg_parms;
3348 tsubst_flags_t complain;
3349 tree in_decl, outer_args;
3351 int nparms, nargs, i;
3352 tree parm, arg;
3354 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3355 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3357 nparms = TREE_VEC_LENGTH (parm_parms);
3358 nargs = TREE_VEC_LENGTH (arg_parms);
3360 /* The rule here is opposite of coerce_template_parms. */
3361 if (nargs < nparms
3362 || (nargs > nparms
3363 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3364 return 0;
3366 for (i = 0; i < nparms; ++i)
3368 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3369 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3371 if (arg == NULL_TREE || arg == error_mark_node
3372 || parm == NULL_TREE || parm == error_mark_node)
3373 return 0;
3375 if (TREE_CODE (arg) != TREE_CODE (parm))
3376 return 0;
3378 switch (TREE_CODE (parm))
3380 case TYPE_DECL:
3381 break;
3383 case TEMPLATE_DECL:
3384 /* We encounter instantiations of templates like
3385 template <template <template <class> class> class TT>
3386 class C; */
3388 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3389 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3391 if (!coerce_template_template_parms (parmparm, argparm,
3392 complain, in_decl,
3393 outer_args))
3394 return 0;
3396 break;
3398 case PARM_DECL:
3399 /* The tsubst call is used to handle cases such as
3400 template <class T, template <T> class TT> class D;
3401 i.e. the parameter list of TT depends on earlier parameters. */
3402 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3403 complain, in_decl),
3404 TREE_TYPE (arg)))
3405 return 0;
3406 break;
3408 default:
3409 abort ();
3412 return 1;
3415 /* Convert the indicated template ARG as necessary to match the
3416 indicated template PARM. Returns the converted ARG, or
3417 error_mark_node if the conversion was unsuccessful. Error and
3418 warning messages are issued under control of COMPLAIN. This
3419 conversion is for the Ith parameter in the parameter list. ARGS is
3420 the full set of template arguments deduced so far. */
3422 static tree
3423 convert_template_argument (parm, arg, args, complain, i, in_decl)
3424 tree parm;
3425 tree arg;
3426 tree args;
3427 tsubst_flags_t complain;
3428 int i;
3429 tree in_decl;
3431 tree val;
3432 tree inner_args;
3433 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3435 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3437 if (TREE_CODE (arg) == TREE_LIST
3438 && TREE_TYPE (arg) != NULL_TREE
3439 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3441 /* The template argument was the name of some
3442 member function. That's usually
3443 invalid, but static members are OK. In any
3444 case, grab the underlying fields/functions
3445 and issue an error later if required. */
3446 arg = TREE_VALUE (arg);
3447 TREE_TYPE (arg) = unknown_type_node;
3450 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3451 requires_type = (TREE_CODE (parm) == TYPE_DECL
3452 || requires_tmpl_type);
3454 if (TREE_CODE (arg) != RECORD_TYPE)
3455 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3456 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3457 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3458 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3459 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3460 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3462 if (is_base_of_enclosing_class (arg, current_class_type))
3463 /* This is a template name used within the scope of the
3464 template. It could be the template, or it could be the
3465 instantiation. Choose whichever makes sense. */
3466 is_tmpl_type = requires_tmpl_type;
3467 else
3468 is_tmpl_type = 1;
3470 else
3471 /* It is a non-template class, or a specialization of a template
3472 class, or a non-template member of a template class. */
3473 is_tmpl_type = 0;
3475 if (is_tmpl_type
3476 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3477 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3478 arg = TYPE_STUB_DECL (arg);
3479 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3480 arg = CLASSTYPE_TI_TEMPLATE (arg);
3482 is_type = TYPE_P (arg) || is_tmpl_type;
3484 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3485 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3487 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3489 arg = make_typename_type (TREE_OPERAND (arg, 0),
3490 TREE_OPERAND (arg, 1),
3491 complain & tf_error);
3492 is_type = 1;
3494 if (is_type != requires_type)
3496 if (in_decl)
3498 if (complain & tf_error)
3500 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3501 i + 1, in_decl);
3502 if (is_type)
3503 error (" expected a constant of type `%T', got `%T'",
3504 TREE_TYPE (parm),
3505 (is_tmpl_type ? DECL_NAME (arg) : arg));
3506 else
3507 error (" expected a type, got `%E'", arg);
3510 return error_mark_node;
3512 if (is_tmpl_type ^ requires_tmpl_type)
3514 if (in_decl && (complain & tf_error))
3516 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3517 i + 1, in_decl);
3518 if (is_tmpl_type)
3519 error (" expected a type, got `%T'", DECL_NAME (arg));
3520 else
3521 error (" expected a class template, got `%T'", arg);
3523 return error_mark_node;
3526 if (is_type)
3528 if (requires_tmpl_type)
3530 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3531 /* The number of argument required is not known yet.
3532 Just accept it for now. */
3533 val = TREE_TYPE (arg);
3534 else
3536 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3537 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3539 if (coerce_template_template_parms (parmparm, argparm,
3540 complain, in_decl,
3541 inner_args))
3543 val = arg;
3545 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3546 TEMPLATE_DECL. */
3547 if (val != error_mark_node
3548 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3549 val = TREE_TYPE (val);
3551 else
3553 if (in_decl && (complain & tf_error))
3555 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3556 i + 1, in_decl);
3557 error (" expected a template of type `%D', got `%D'", parm, arg);
3560 val = error_mark_node;
3564 else
3566 val = groktypename (arg);
3567 if (! processing_template_decl)
3569 /* [basic.link]: A name with no linkage (notably, the
3570 name of a class or enumeration declared in a local
3571 scope) shall not be used to declare an entity with
3572 linkage. This implies that names with no linkage
3573 cannot be used as template arguments. */
3574 tree t = no_linkage_check (val);
3575 if (t)
3577 if (TYPE_ANONYMOUS_P (t))
3578 pedwarn
3579 ("template-argument `%T' uses anonymous type", val);
3580 else
3581 error
3582 ("template-argument `%T' uses local type `%T'",
3583 val, t);
3584 return error_mark_node;
3587 /* In order to avoid all sorts of complications, we do
3588 not allow variably-modified types as template
3589 arguments. */
3590 if (variably_modified_type_p (val))
3592 error ("template-argument `%T' is a variably modified type",
3593 val);
3594 return error_mark_node;
3599 else
3601 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3603 if (invalid_nontype_parm_type_p (t, complain))
3604 return error_mark_node;
3606 if (processing_template_decl)
3607 arg = maybe_fold_nontype_arg (arg);
3609 if (!uses_template_parms (arg) && !uses_template_parms (t))
3610 /* We used to call digest_init here. However, digest_init
3611 will report errors, which we don't want when complain
3612 is zero. More importantly, digest_init will try too
3613 hard to convert things: for example, `0' should not be
3614 converted to pointer type at this point according to
3615 the standard. Accepting this is not merely an
3616 extension, since deciding whether or not these
3617 conversions can occur is part of determining which
3618 function template to call, or whether a given explicit
3619 argument specification is valid. */
3620 val = convert_nontype_argument (t, arg);
3621 else
3622 val = arg;
3624 if (val == NULL_TREE)
3625 val = error_mark_node;
3626 else if (val == error_mark_node && (complain & tf_error))
3627 error ("could not convert template argument `%E' to `%T'",
3628 arg, t);
3631 return val;
3634 /* Convert all template arguments to their appropriate types, and
3635 return a vector containing the innermost resulting template
3636 arguments. If any error occurs, return error_mark_node. Error and
3637 warning messages are issued under control of COMPLAIN. Some error
3638 messages are issued even if COMPLAIN is zero; for instance, if a
3639 template argument is composed from a local class.
3641 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3642 provided in ARGLIST, or else trailing parameters must have default
3643 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3644 deduction for any unspecified trailing arguments. */
3646 static tree
3647 coerce_template_parms (parms, args, in_decl,
3648 complain,
3649 require_all_arguments)
3650 tree parms, args;
3651 tree in_decl;
3652 tsubst_flags_t complain;
3653 int require_all_arguments;
3655 int nparms, nargs, i, lost = 0;
3656 tree inner_args;
3657 tree new_args;
3658 tree new_inner_args;
3660 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3661 nargs = NUM_TMPL_ARGS (inner_args);
3662 nparms = TREE_VEC_LENGTH (parms);
3664 if (nargs > nparms
3665 || (nargs < nparms
3666 && require_all_arguments
3667 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3669 if (complain & tf_error)
3671 error ("wrong number of template arguments (%d, should be %d)",
3672 nargs, nparms);
3674 if (in_decl)
3675 cp_error_at ("provided for `%D'", in_decl);
3678 return error_mark_node;
3681 new_inner_args = make_tree_vec (nparms);
3682 new_args = add_outermost_template_args (args, new_inner_args);
3683 for (i = 0; i < nparms; i++)
3685 tree arg;
3686 tree parm;
3688 /* Get the Ith template parameter. */
3689 parm = TREE_VEC_ELT (parms, i);
3691 /* Calculate the Ith argument. */
3692 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3694 arg = TREE_VALUE (inner_args);
3695 inner_args = TREE_CHAIN (inner_args);
3697 else if (i < nargs)
3698 arg = TREE_VEC_ELT (inner_args, i);
3699 /* If no template argument was supplied, look for a default
3700 value. */
3701 else if (TREE_PURPOSE (parm) == NULL_TREE)
3703 /* There was no default value. */
3704 my_friendly_assert (!require_all_arguments, 0);
3705 break;
3707 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3708 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3709 else
3710 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3711 in_decl);
3713 /* Now, convert the Ith argument, as necessary. */
3714 if (arg == NULL_TREE)
3715 /* We're out of arguments. */
3717 my_friendly_assert (!require_all_arguments, 0);
3718 break;
3720 else if (arg == error_mark_node)
3722 error ("template argument %d is invalid", i + 1);
3723 arg = error_mark_node;
3725 else
3726 arg = convert_template_argument (TREE_VALUE (parm),
3727 arg, new_args, complain, i,
3728 in_decl);
3730 if (arg == error_mark_node)
3731 lost++;
3732 TREE_VEC_ELT (new_inner_args, i) = arg;
3735 if (lost)
3736 return error_mark_node;
3738 return new_inner_args;
3741 /* Returns 1 if template args OT and NT are equivalent. */
3743 static int
3744 template_args_equal (ot, nt)
3745 tree ot, nt;
3747 if (nt == ot)
3748 return 1;
3750 if (TREE_CODE (nt) == TREE_VEC)
3751 /* For member templates */
3752 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3753 else if (TYPE_P (nt))
3754 return TYPE_P (ot) && same_type_p (ot, nt);
3755 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3756 return 0;
3757 else
3758 return (cp_tree_equal (ot, nt) > 0);
3761 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3762 of template arguments. Returns 0 otherwise. */
3765 comp_template_args (oldargs, newargs)
3766 tree oldargs, newargs;
3768 int i;
3770 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3771 return 0;
3773 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3775 tree nt = TREE_VEC_ELT (newargs, i);
3776 tree ot = TREE_VEC_ELT (oldargs, i);
3778 if (! template_args_equal (ot, nt))
3779 return 0;
3781 return 1;
3784 /* Given class template name and parameter list, produce a user-friendly name
3785 for the instantiation. */
3787 static char *
3788 mangle_class_name_for_template (name, parms, arglist)
3789 const char *name;
3790 tree parms, arglist;
3792 static struct obstack scratch_obstack;
3793 static char *scratch_firstobj;
3794 int i, nparms;
3796 if (!scratch_firstobj)
3797 gcc_obstack_init (&scratch_obstack);
3798 else
3799 obstack_free (&scratch_obstack, scratch_firstobj);
3800 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3802 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3803 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3805 cat (name);
3806 ccat ('<');
3807 nparms = TREE_VEC_LENGTH (parms);
3808 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3809 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3810 for (i = 0; i < nparms; i++)
3812 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3813 tree arg = TREE_VEC_ELT (arglist, i);
3815 if (i)
3816 ccat (',');
3818 if (TREE_CODE (parm) == TYPE_DECL)
3820 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3821 continue;
3823 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3825 if (TREE_CODE (arg) == TEMPLATE_DECL)
3827 /* Already substituted with real template. Just output
3828 the template name here */
3829 tree context = DECL_CONTEXT (arg);
3830 if (context)
3832 /* The template may be defined in a namespace, or
3833 may be a member template. */
3834 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3835 || CLASS_TYPE_P (context),
3836 980422);
3837 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3838 cat("::");
3840 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3842 else
3843 /* Output the parameter declaration */
3844 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3845 continue;
3847 else
3848 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3850 if (TREE_CODE (arg) == TREE_LIST)
3852 /* New list cell was built because old chain link was in
3853 use. */
3854 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3855 arg = TREE_VALUE (arg);
3857 /* No need to check arglist against parmlist here; we did that
3858 in coerce_template_parms, called from lookup_template_class. */
3859 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3862 char *bufp = obstack_next_free (&scratch_obstack);
3863 int offset = 0;
3864 while (bufp[offset - 1] == ' ')
3865 offset--;
3866 obstack_blank_fast (&scratch_obstack, offset);
3868 /* B<C<char> >, not B<C<char>> */
3869 if (bufp[offset - 1] == '>')
3870 ccat (' ');
3872 ccat ('>');
3873 ccat ('\0');
3874 return (char *) obstack_base (&scratch_obstack);
3877 static tree
3878 classtype_mangled_name (t)
3879 tree t;
3881 if (CLASSTYPE_TEMPLATE_INFO (t)
3882 /* Specializations have already had their names set up in
3883 lookup_template_class. */
3884 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3886 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3888 /* For non-primary templates, the template parameters are
3889 implicit from their surrounding context. */
3890 if (PRIMARY_TEMPLATE_P (tmpl))
3892 tree name = DECL_NAME (tmpl);
3893 char *mangled_name = mangle_class_name_for_template
3894 (IDENTIFIER_POINTER (name),
3895 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3896 CLASSTYPE_TI_ARGS (t));
3897 tree id = get_identifier (mangled_name);
3898 IDENTIFIER_TEMPLATE (id) = name;
3899 return id;
3903 return TYPE_IDENTIFIER (t);
3906 static void
3907 add_pending_template (d)
3908 tree d;
3910 tree ti = (TYPE_P (d)
3911 ? CLASSTYPE_TEMPLATE_INFO (d)
3912 : DECL_TEMPLATE_INFO (d));
3913 tree pt;
3914 int level;
3916 if (TI_PENDING_TEMPLATE_FLAG (ti))
3917 return;
3919 /* We are called both from instantiate_decl, where we've already had a
3920 tinst_level pushed, and instantiate_template, where we haven't.
3921 Compensate. */
3922 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3924 if (level)
3925 push_tinst_level (d);
3927 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3928 if (last_pending_template)
3929 TREE_CHAIN (last_pending_template) = pt;
3930 else
3931 pending_templates = pt;
3933 last_pending_template = pt;
3935 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3937 if (level)
3938 pop_tinst_level ();
3942 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
3943 ARGLIST. Valid choices for FNS are given in the cp-tree.def
3944 documentation for TEMPLATE_ID_EXPR. */
3946 tree
3947 lookup_template_function (fns, arglist)
3948 tree fns, arglist;
3950 tree type;
3952 if (fns == error_mark_node || arglist == error_mark_node)
3953 return error_mark_node;
3955 if (fns == NULL_TREE)
3957 error ("non-template used as template");
3958 return error_mark_node;
3961 my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL
3962 || TREE_CODE (fns) == OVERLOAD
3963 || BASELINK_P (fns)
3964 || TREE_CODE (fns) == IDENTIFIER_NODE
3965 || TREE_CODE (fns) == LOOKUP_EXPR,
3966 20020730);
3968 if (BASELINK_P (fns))
3970 BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR,
3971 unknown_type_node,
3972 BASELINK_FUNCTIONS (fns),
3973 arglist);
3974 return fns;
3977 type = TREE_TYPE (fns);
3978 if (TREE_CODE (fns) == OVERLOAD || !type)
3979 type = unknown_type_node;
3981 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3984 /* Within the scope of a template class S<T>, the name S gets bound
3985 (in build_self_reference) to a TYPE_DECL for the class, not a
3986 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3987 or one of its enclosing classes, and that type is a template,
3988 return the associated TEMPLATE_DECL. Otherwise, the original
3989 DECL is returned. */
3991 tree
3992 maybe_get_template_decl_from_type_decl (decl)
3993 tree decl;
3995 return (decl != NULL_TREE
3996 && TREE_CODE (decl) == TYPE_DECL
3997 && DECL_ARTIFICIAL (decl)
3998 && CLASS_TYPE_P (TREE_TYPE (decl))
3999 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4000 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4003 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4004 parameters, find the desired type.
4006 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4007 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
4008 be a TREE_LIST if called directly from the parser, and a TREE_VEC
4009 otherwise.)
4011 IN_DECL, if non-NULL, is the template declaration we are trying to
4012 instantiate.
4014 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4015 the class we are looking up.
4017 Issue error and warning messages under control of COMPLAIN.
4019 If the template class is really a local class in a template
4020 function, then the FUNCTION_CONTEXT is the function in which it is
4021 being instantiated. */
4023 tree
4024 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
4025 tree d1, arglist;
4026 tree in_decl;
4027 tree context;
4028 int entering_scope;
4029 tsubst_flags_t complain;
4031 tree template = NULL_TREE, parmlist;
4032 tree t;
4034 timevar_push (TV_NAME_LOOKUP);
4035 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4037 if (IDENTIFIER_VALUE (d1)
4038 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
4039 template = IDENTIFIER_VALUE (d1);
4040 else
4042 if (context)
4043 push_decl_namespace (context);
4044 template = lookup_name (d1, /*prefer_type=*/0);
4045 template = maybe_get_template_decl_from_type_decl (template);
4046 if (context)
4047 pop_decl_namespace ();
4049 if (template)
4050 context = DECL_CONTEXT (template);
4052 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4054 tree type = TREE_TYPE (d1);
4056 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4057 an implicit typename for the second A. Deal with it. */
4058 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4059 type = TREE_TYPE (type);
4061 if (CLASSTYPE_TEMPLATE_INFO (type))
4063 template = CLASSTYPE_TI_TEMPLATE (type);
4064 d1 = DECL_NAME (template);
4067 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4068 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4070 template = TYPE_TI_TEMPLATE (d1);
4071 d1 = DECL_NAME (template);
4073 else if (TREE_CODE (d1) == TEMPLATE_DECL
4074 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4076 template = d1;
4077 d1 = DECL_NAME (template);
4078 context = DECL_CONTEXT (template);
4081 /* With something like `template <class T> class X class X { ... };'
4082 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
4083 We don't want to do that, but we have to deal with the situation,
4084 so let's give them some syntax errors to chew on instead of a
4085 crash. Alternatively D1 might not be a template type at all. */
4086 if (! template)
4088 if (complain & tf_error)
4089 error ("`%T' is not a template", d1);
4090 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4093 if (TREE_CODE (template) != TEMPLATE_DECL
4094 /* If we're called from the parser, make sure it's a user visible
4095 template. */
4096 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
4097 && !DECL_TEMPLATE_PARM_P (template)
4098 && !PRIMARY_TEMPLATE_P (template)))
4100 if (complain & tf_error)
4102 error ("non-template type `%T' used as a template", d1);
4103 if (in_decl)
4104 cp_error_at ("for template declaration `%D'", in_decl);
4106 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4111 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4112 template arguments */
4114 tree parm;
4115 tree arglist2;
4117 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4119 /* Consider an example where a template template parameter declared as
4121 template <class T, class U = std::allocator<T> > class TT
4123 The template parameter level of T and U are one level larger than
4124 of TT. To proper process the default argument of U, say when an
4125 instantiation `TT<int>' is seen, we need to build the full
4126 arguments containing {int} as the innermost level. Outer levels,
4127 available when not appearing as default template argument, can be
4128 obtained from `current_template_args ()'.
4130 Suppose that TT is later substituted with std::vector. The above
4131 instantiation is `TT<int, std::allocator<T> >' with TT at
4132 level 1, and T at level 2, while the template arguments at level 1
4133 becomes {std::vector} and the inner level 2 is {int}. */
4135 if (current_template_parms)
4136 arglist = add_to_template_args (current_template_args (), arglist);
4138 arglist2 = coerce_template_parms (parmlist, arglist, template,
4139 complain, /*require_all_args=*/1);
4140 if (arglist2 == error_mark_node)
4141 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4143 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4144 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4146 else
4148 tree template_type = TREE_TYPE (template);
4149 tree gen_tmpl;
4150 tree type_decl;
4151 tree found = NULL_TREE;
4152 tree *tp;
4153 int arg_depth;
4154 int parm_depth;
4155 int is_partial_instantiation;
4157 gen_tmpl = most_general_template (template);
4158 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4159 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4160 arg_depth = TMPL_ARGS_DEPTH (arglist);
4162 if (arg_depth == 1 && parm_depth > 1)
4164 /* We've been given an incomplete set of template arguments.
4165 For example, given:
4167 template <class T> struct S1 {
4168 template <class U> struct S2 {};
4169 template <class U> struct S2<U*> {};
4172 we will be called with an ARGLIST of `U*', but the
4173 TEMPLATE will be `template <class T> template
4174 <class U> struct S1<T>::S2'. We must fill in the missing
4175 arguments. */
4176 arglist
4177 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4178 arglist);
4179 arg_depth = TMPL_ARGS_DEPTH (arglist);
4182 /* Now we should have enough arguments. */
4183 my_friendly_assert (parm_depth == arg_depth, 0);
4185 /* From here on, we're only interested in the most general
4186 template. */
4187 template = gen_tmpl;
4189 /* Calculate the BOUND_ARGS. These will be the args that are
4190 actually tsubst'd into the definition to create the
4191 instantiation. */
4192 if (parm_depth > 1)
4194 /* We have multiple levels of arguments to coerce, at once. */
4195 int i;
4196 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4198 tree bound_args = make_tree_vec (parm_depth);
4200 for (i = saved_depth,
4201 t = DECL_TEMPLATE_PARMS (template);
4202 i > 0 && t != NULL_TREE;
4203 --i, t = TREE_CHAIN (t))
4205 tree a = coerce_template_parms (TREE_VALUE (t),
4206 arglist, template,
4207 complain, /*require_all_args=*/1);
4208 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4210 /* We temporarily reduce the length of the ARGLIST so
4211 that coerce_template_parms will see only the arguments
4212 corresponding to the template parameters it is
4213 examining. */
4214 TREE_VEC_LENGTH (arglist)--;
4217 /* Restore the ARGLIST to its full size. */
4218 TREE_VEC_LENGTH (arglist) = saved_depth;
4220 arglist = bound_args;
4222 else
4223 arglist
4224 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4225 INNERMOST_TEMPLATE_ARGS (arglist),
4226 template,
4227 complain, /*require_all_args=*/1);
4229 if (arglist == error_mark_node)
4230 /* We were unable to bind the arguments. */
4231 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4233 /* In the scope of a template class, explicit references to the
4234 template class refer to the type of the template, not any
4235 instantiation of it. For example, in:
4237 template <class T> class C { void f(C<T>); }
4239 the `C<T>' is just the same as `C'. Outside of the
4240 class, however, such a reference is an instantiation. */
4241 if (comp_template_args (TYPE_TI_ARGS (template_type),
4242 arglist))
4244 found = template_type;
4246 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4248 tree ctx;
4250 /* Note that we use DECL_CONTEXT, rather than
4251 CP_DECL_CONTEXT, so that the termination test is
4252 always just `ctx'. We're not interested in namespace
4253 scopes. */
4254 for (ctx = current_class_type;
4255 ctx;
4256 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4257 if (same_type_p (ctx, template_type))
4258 break;
4260 if (!ctx)
4261 /* We're not in the scope of the class, so the
4262 TEMPLATE_TYPE is not the type we want after
4263 all. */
4264 found = NULL_TREE;
4267 if (found)
4268 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4270 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4271 *tp;
4272 tp = &TREE_CHAIN (*tp))
4273 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4275 found = *tp;
4277 /* Use the move-to-front heuristic to speed up future
4278 searches. */
4279 *tp = TREE_CHAIN (*tp);
4280 TREE_CHAIN (found)
4281 = DECL_TEMPLATE_INSTANTIATIONS (template);
4282 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4284 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
4287 /* This type is a "partial instantiation" if any of the template
4288 arguments still involve template parameters. Note that we set
4289 IS_PARTIAL_INSTANTIATION for partial specializations as
4290 well. */
4291 is_partial_instantiation = uses_template_parms (arglist);
4293 if (!is_partial_instantiation
4294 && !PRIMARY_TEMPLATE_P (template)
4295 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4297 found = xref_tag_from_type (TREE_TYPE (template),
4298 DECL_NAME (template),
4299 /*globalize=*/1);
4300 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4303 context = tsubst (DECL_CONTEXT (template), arglist,
4304 complain, in_decl);
4305 if (!context)
4306 context = global_namespace;
4308 /* Create the type. */
4309 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4311 if (!is_partial_instantiation)
4313 set_current_access_from_decl (TYPE_NAME (template_type));
4314 t = start_enum (TYPE_IDENTIFIER (template_type));
4316 else
4317 /* We don't want to call start_enum for this type, since
4318 the values for the enumeration constants may involve
4319 template parameters. And, no one should be interested
4320 in the enumeration constants for such a type. */
4321 t = make_node (ENUMERAL_TYPE);
4323 else
4325 t = make_aggr_type (TREE_CODE (template_type));
4326 CLASSTYPE_DECLARED_CLASS (t)
4327 = CLASSTYPE_DECLARED_CLASS (template_type);
4328 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4329 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4330 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4332 /* A local class. Make sure the decl gets registered properly. */
4333 if (context == current_function_decl)
4334 pushtag (DECL_NAME (template), t, 0);
4337 /* If we called start_enum or pushtag above, this information
4338 will already be set up. */
4339 if (!TYPE_NAME (t))
4341 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4343 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4344 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4345 TYPE_STUB_DECL (t) = type_decl;
4346 DECL_SOURCE_LOCATION (type_decl)
4347 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4349 else
4350 type_decl = TYPE_NAME (t);
4352 TREE_PRIVATE (type_decl)
4353 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4354 TREE_PROTECTED (type_decl)
4355 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4357 /* Set up the template information. We have to figure out which
4358 template is the immediate parent if this is a full
4359 instantiation. */
4360 if (parm_depth == 1 || is_partial_instantiation
4361 || !PRIMARY_TEMPLATE_P (template))
4362 /* This case is easy; there are no member templates involved. */
4363 found = template;
4364 else
4366 /* This is a full instantiation of a member template. Look
4367 for a partial instantiation of which this is an instance. */
4369 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4370 found; found = TREE_CHAIN (found))
4372 int success;
4373 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4375 /* We only want partial instantiations, here, not
4376 specializations or full instantiations. */
4377 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4378 || !uses_template_parms (TREE_VALUE (found)))
4379 continue;
4381 /* Temporarily reduce by one the number of levels in the
4382 ARGLIST and in FOUND so as to avoid comparing the
4383 last set of arguments. */
4384 TREE_VEC_LENGTH (arglist)--;
4385 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4387 /* See if the arguments match. If they do, then TMPL is
4388 the partial instantiation we want. */
4389 success = comp_template_args (TREE_PURPOSE (found), arglist);
4391 /* Restore the argument vectors to their full size. */
4392 TREE_VEC_LENGTH (arglist)++;
4393 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4395 if (success)
4397 found = tmpl;
4398 break;
4402 if (!found)
4404 /* There was no partial instantiation. This happens
4405 where C<T> is a member template of A<T> and it's used
4406 in something like
4408 template <typename T> struct B { A<T>::C<int> m; };
4409 B<float>;
4411 Create the partial instantiation.
4413 TREE_VEC_LENGTH (arglist)--;
4414 found = tsubst (template, arglist, complain, NULL_TREE);
4415 TREE_VEC_LENGTH (arglist)++;
4419 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4420 DECL_TEMPLATE_INSTANTIATIONS (template)
4421 = tree_cons (arglist, t,
4422 DECL_TEMPLATE_INSTANTIATIONS (template));
4424 if (TREE_CODE (t) == ENUMERAL_TYPE
4425 && !is_partial_instantiation)
4426 /* Now that the type has been registered on the instantiations
4427 list, we set up the enumerators. Because the enumeration
4428 constants may involve the enumeration type itself, we make
4429 sure to register the type first, and then create the
4430 constants. That way, doing tsubst_expr for the enumeration
4431 constants won't result in recursive calls here; we'll find
4432 the instantiation and exit above. */
4433 tsubst_enum (template_type, t, arglist);
4435 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4436 is set up. */
4437 if (TREE_CODE (t) != ENUMERAL_TYPE)
4438 DECL_NAME (type_decl) = classtype_mangled_name (t);
4439 if (!is_partial_instantiation)
4441 /* For backwards compatibility; code that uses
4442 -fexternal-templates expects looking up a template to
4443 instantiate it. I think DDD still relies on this.
4444 (jason 8/20/1998) */
4445 if (TREE_CODE (t) != ENUMERAL_TYPE
4446 && flag_external_templates
4447 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4448 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4449 add_pending_template (t);
4451 else
4452 /* If the type makes use of template parameters, the
4453 code that generates debugging information will crash. */
4454 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4456 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4458 timevar_pop (TV_NAME_LOOKUP);
4461 struct pair_fn_data
4463 tree_fn_t fn;
4464 void *data;
4465 htab_t visited;
4468 /* Called from for_each_template_parm via walk_tree. */
4470 static tree
4471 for_each_template_parm_r (tp, walk_subtrees, d)
4472 tree *tp;
4473 int *walk_subtrees;
4474 void *d;
4476 tree t = *tp;
4477 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4478 tree_fn_t fn = pfd->fn;
4479 void *data = pfd->data;
4480 void **slot;
4482 /* If we have already visited this tree, there's no need to walk
4483 subtrees. Otherwise, add it to the visited table. */
4484 slot = htab_find_slot (pfd->visited, *tp, INSERT);
4485 if (*slot)
4487 *walk_subtrees = 0;
4488 return NULL_TREE;
4490 *slot = *tp;
4492 if (TYPE_P (t)
4493 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4494 return error_mark_node;
4496 switch (TREE_CODE (t))
4498 case RECORD_TYPE:
4499 if (TYPE_PTRMEMFUNC_P (t))
4500 break;
4501 /* Fall through. */
4503 case UNION_TYPE:
4504 case ENUMERAL_TYPE:
4505 if (!TYPE_TEMPLATE_INFO (t))
4506 *walk_subtrees = 0;
4507 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4508 fn, data, pfd->visited))
4509 return error_mark_node;
4510 break;
4512 case METHOD_TYPE:
4513 /* Since we're not going to walk subtrees, we have to do this
4514 explicitly here. */
4515 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4516 pfd->visited))
4517 return error_mark_node;
4518 /* Fall through. */
4520 case FUNCTION_TYPE:
4521 /* Check the return type. */
4522 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4523 return error_mark_node;
4525 /* Check the parameter types. Since default arguments are not
4526 instantiated until they are needed, the TYPE_ARG_TYPES may
4527 contain expressions that involve template parameters. But,
4528 no-one should be looking at them yet. And, once they're
4529 instantiated, they don't contain template parameters, so
4530 there's no point in looking at them then, either. */
4532 tree parm;
4534 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4535 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4536 pfd->visited))
4537 return error_mark_node;
4539 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4540 want walk_tree walking into them itself. */
4541 *walk_subtrees = 0;
4543 break;
4545 case TYPEOF_TYPE:
4546 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4547 pfd->visited))
4548 return error_mark_node;
4549 break;
4551 case FUNCTION_DECL:
4552 case VAR_DECL:
4553 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4554 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4555 pfd->visited))
4556 return error_mark_node;
4557 /* Fall through. */
4559 case PARM_DECL:
4560 case CONST_DECL:
4561 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4562 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4563 pfd->visited))
4564 return error_mark_node;
4565 if (DECL_CONTEXT (t)
4566 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4567 pfd->visited))
4568 return error_mark_node;
4569 break;
4571 case BOUND_TEMPLATE_TEMPLATE_PARM:
4572 /* Record template parameters such as `T' inside `TT<T>'. */
4573 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4574 return error_mark_node;
4575 /* Fall through. */
4577 case TEMPLATE_TEMPLATE_PARM:
4578 case TEMPLATE_TYPE_PARM:
4579 case TEMPLATE_PARM_INDEX:
4580 if (fn && (*fn)(t, data))
4581 return error_mark_node;
4582 else if (!fn)
4583 return error_mark_node;
4584 break;
4586 case TEMPLATE_DECL:
4587 /* A template template parameter is encountered */
4588 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4589 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4590 return error_mark_node;
4592 /* Already substituted template template parameter */
4593 *walk_subtrees = 0;
4594 break;
4596 case TYPENAME_TYPE:
4597 if (!fn
4598 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4599 data, pfd->visited))
4600 return error_mark_node;
4601 break;
4603 case CONSTRUCTOR:
4604 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4605 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4606 (TREE_TYPE (t)), fn, data,
4607 pfd->visited))
4608 return error_mark_node;
4609 break;
4611 case INDIRECT_REF:
4612 case COMPONENT_REF:
4613 /* If there's no type, then this thing must be some expression
4614 involving template parameters. */
4615 if (!fn && !TREE_TYPE (t))
4616 return error_mark_node;
4617 break;
4619 case MODOP_EXPR:
4620 case CAST_EXPR:
4621 case REINTERPRET_CAST_EXPR:
4622 case CONST_CAST_EXPR:
4623 case STATIC_CAST_EXPR:
4624 case DYNAMIC_CAST_EXPR:
4625 case ARROW_EXPR:
4626 case DOTSTAR_EXPR:
4627 case TYPEID_EXPR:
4628 case LOOKUP_EXPR:
4629 case PSEUDO_DTOR_EXPR:
4630 if (!fn)
4631 return error_mark_node;
4632 break;
4634 case BASELINK:
4635 /* If we do not handle this case specially, we end up walking
4636 the BINFO hierarchy, which is circular, and therefore
4637 confuses walk_tree. */
4638 *walk_subtrees = 0;
4639 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4640 pfd->visited))
4641 return error_mark_node;
4642 break;
4644 default:
4645 break;
4648 /* We didn't find any template parameters we liked. */
4649 return NULL_TREE;
4652 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4653 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4654 call FN with the parameter and the DATA.
4655 If FN returns nonzero, the iteration is terminated, and
4656 for_each_template_parm returns 1. Otherwise, the iteration
4657 continues. If FN never returns a nonzero value, the value
4658 returned by for_each_template_parm is 0. If FN is NULL, it is
4659 considered to be the function which always returns 1. */
4661 static int
4662 for_each_template_parm (t, fn, data, visited)
4663 tree t;
4664 tree_fn_t fn;
4665 void* data;
4666 htab_t visited;
4668 struct pair_fn_data pfd;
4669 int result;
4671 /* Set up. */
4672 pfd.fn = fn;
4673 pfd.data = data;
4675 /* Walk the tree. (Conceptually, we would like to walk without
4676 duplicates, but for_each_template_parm_r recursively calls
4677 for_each_template_parm, so we would need to reorganize a fair
4678 bit to use walk_tree_without_duplicates, so we keep our own
4679 visited list.) */
4680 if (visited)
4681 pfd.visited = visited;
4682 else
4683 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer,
4684 NULL);
4685 result = walk_tree (&t,
4686 for_each_template_parm_r,
4687 &pfd,
4688 NULL) != NULL_TREE;
4690 /* Clean up. */
4691 if (!visited)
4692 htab_delete (pfd.visited);
4694 return result;
4698 uses_template_parms (t)
4699 tree t;
4701 return for_each_template_parm (t, 0, 0, NULL);
4704 static int tinst_depth;
4705 extern int max_tinst_depth;
4706 #ifdef GATHER_STATISTICS
4707 int depth_reached;
4708 #endif
4709 static int tinst_level_tick;
4710 static int last_template_error_tick;
4712 /* We're starting to instantiate D; record the template instantiation context
4713 for diagnostics and to restore it later. */
4716 push_tinst_level (d)
4717 tree d;
4719 tree new;
4721 if (tinst_depth >= max_tinst_depth)
4723 /* If the instantiation in question still has unbound template parms,
4724 we don't really care if we can't instantiate it, so just return.
4725 This happens with base instantiation for implicit `typename'. */
4726 if (uses_template_parms (d))
4727 return 0;
4729 last_template_error_tick = tinst_level_tick;
4730 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4731 max_tinst_depth, d);
4733 print_instantiation_context ();
4735 return 0;
4738 new = build_expr_wfl (d, input_filename, lineno, 0);
4739 TREE_CHAIN (new) = current_tinst_level;
4740 current_tinst_level = new;
4742 ++tinst_depth;
4743 #ifdef GATHER_STATISTICS
4744 if (tinst_depth > depth_reached)
4745 depth_reached = tinst_depth;
4746 #endif
4748 ++tinst_level_tick;
4749 return 1;
4752 /* We're done instantiating this template; return to the instantiation
4753 context. */
4755 void
4756 pop_tinst_level ()
4758 tree old = current_tinst_level;
4760 /* Restore the filename and line number stashed away when we started
4761 this instantiation. */
4762 lineno = TINST_LINE (old);
4763 input_filename = TINST_FILE (old);
4764 extract_interface_info ();
4766 current_tinst_level = TREE_CHAIN (old);
4767 --tinst_depth;
4768 ++tinst_level_tick;
4771 /* We're instantiating a deferred template; restore the template
4772 instantiation context in which the instantiation was requested, which
4773 is one step out from LEVEL. */
4775 static void
4776 reopen_tinst_level (level)
4777 tree level;
4779 tree t;
4781 tinst_depth = 0;
4782 for (t = level; t; t = TREE_CHAIN (t))
4783 ++tinst_depth;
4785 current_tinst_level = level;
4786 pop_tinst_level ();
4789 /* Return the outermost template instantiation context, for use with
4790 -falt-external-templates. */
4792 tree
4793 tinst_for_decl ()
4795 tree p = current_tinst_level;
4797 if (p)
4798 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4800 return p;
4803 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4804 vector of template arguments, as for tsubst.
4806 Returns an appropriate tsubst'd friend declaration. */
4808 static tree
4809 tsubst_friend_function (decl, args)
4810 tree decl;
4811 tree args;
4813 tree new_friend;
4814 int line = lineno;
4815 const char *file = input_filename;
4817 lineno = DECL_SOURCE_LINE (decl);
4818 input_filename = DECL_SOURCE_FILE (decl);
4820 if (TREE_CODE (decl) == FUNCTION_DECL
4821 && DECL_TEMPLATE_INSTANTIATION (decl)
4822 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4823 /* This was a friend declared with an explicit template
4824 argument list, e.g.:
4826 friend void f<>(T);
4828 to indicate that f was a template instantiation, not a new
4829 function declaration. Now, we have to figure out what
4830 instantiation of what template. */
4832 tree template_id, arglist, fns;
4833 tree new_args;
4834 tree tmpl;
4835 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
4837 /* Friend functions are looked up in the containing namespace scope.
4838 We must enter that scope, to avoid finding member functions of the
4839 current cless with same name. */
4840 push_nested_namespace (ns);
4841 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4842 tf_error | tf_warning, NULL_TREE);
4843 pop_nested_namespace (ns);
4844 arglist = tsubst (DECL_TI_ARGS (decl), args,
4845 tf_error | tf_warning, NULL_TREE);
4846 template_id = lookup_template_function (fns, arglist);
4848 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4849 tmpl = determine_specialization (template_id, new_friend,
4850 &new_args,
4851 /*need_member_template=*/0);
4852 new_friend = instantiate_template (tmpl, new_args);
4853 goto done;
4856 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4858 /* The NEW_FRIEND will look like an instantiation, to the
4859 compiler, but is not an instantiation from the point of view of
4860 the language. For example, we might have had:
4862 template <class T> struct S {
4863 template <class U> friend void f(T, U);
4866 Then, in S<int>, template <class U> void f(int, U) is not an
4867 instantiation of anything. */
4868 DECL_USE_TEMPLATE (new_friend) = 0;
4869 if (TREE_CODE (decl) == TEMPLATE_DECL)
4871 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4872 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4873 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4876 /* The mangled name for the NEW_FRIEND is incorrect. The function
4877 is not a template instantiation and should not be mangled like
4878 one. Therefore, we forget the mangling here; we'll recompute it
4879 later if we need it. */
4880 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4882 SET_DECL_RTL (new_friend, NULL_RTX);
4883 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4886 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4888 tree old_decl;
4889 tree new_friend_template_info;
4890 tree new_friend_result_template_info;
4891 tree ns;
4892 int new_friend_is_defn;
4894 /* We must save some information from NEW_FRIEND before calling
4895 duplicate decls since that function will free NEW_FRIEND if
4896 possible. */
4897 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4898 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4900 /* This declaration is a `primary' template. */
4901 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4903 new_friend_is_defn
4904 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4905 new_friend_result_template_info
4906 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4908 else
4910 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4911 new_friend_result_template_info = NULL_TREE;
4914 /* Inside pushdecl_namespace_level, we will push into the
4915 current namespace. However, the friend function should go
4916 into the namespace of the template. */
4917 ns = decl_namespace_context (new_friend);
4918 push_nested_namespace (ns);
4919 old_decl = pushdecl_namespace_level (new_friend);
4920 pop_nested_namespace (ns);
4922 if (old_decl != new_friend)
4924 /* This new friend declaration matched an existing
4925 declaration. For example, given:
4927 template <class T> void f(T);
4928 template <class U> class C {
4929 template <class T> friend void f(T) {}
4932 the friend declaration actually provides the definition
4933 of `f', once C has been instantiated for some type. So,
4934 old_decl will be the out-of-class template declaration,
4935 while new_friend is the in-class definition.
4937 But, if `f' was called before this point, the
4938 instantiation of `f' will have DECL_TI_ARGS corresponding
4939 to `T' but not to `U', references to which might appear
4940 in the definition of `f'. Previously, the most general
4941 template for an instantiation of `f' was the out-of-class
4942 version; now it is the in-class version. Therefore, we
4943 run through all specialization of `f', adding to their
4944 DECL_TI_ARGS appropriately. In particular, they need a
4945 new set of outer arguments, corresponding to the
4946 arguments for this class instantiation.
4948 The same situation can arise with something like this:
4950 friend void f(int);
4951 template <class T> class C {
4952 friend void f(T) {}
4955 when `C<int>' is instantiated. Now, `f(int)' is defined
4956 in the class. */
4958 if (!new_friend_is_defn)
4959 /* On the other hand, if the in-class declaration does
4960 *not* provide a definition, then we don't want to alter
4961 existing definitions. We can just leave everything
4962 alone. */
4964 else
4966 /* Overwrite whatever template info was there before, if
4967 any, with the new template information pertaining to
4968 the declaration. */
4969 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4971 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4972 /* duplicate_decls will take care of this case. */
4974 else
4976 tree t;
4977 tree new_friend_args;
4979 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4980 = new_friend_result_template_info;
4982 new_friend_args = TI_ARGS (new_friend_template_info);
4983 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4984 t != NULL_TREE;
4985 t = TREE_CHAIN (t))
4987 tree spec = TREE_VALUE (t);
4989 DECL_TI_ARGS (spec)
4990 = add_outermost_template_args (new_friend_args,
4991 DECL_TI_ARGS (spec));
4994 /* Now, since specializations are always supposed to
4995 hang off of the most general template, we must move
4996 them. */
4997 t = most_general_template (old_decl);
4998 if (t != old_decl)
5000 DECL_TEMPLATE_SPECIALIZATIONS (t)
5001 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5002 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5003 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5008 /* The information from NEW_FRIEND has been merged into OLD_DECL
5009 by duplicate_decls. */
5010 new_friend = old_decl;
5013 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
5015 /* Check to see that the declaration is really present, and,
5016 possibly obtain an improved declaration. */
5017 tree fn = check_classfn (DECL_CONTEXT (new_friend),
5018 new_friend);
5020 if (fn)
5021 new_friend = fn;
5024 done:
5025 lineno = line;
5026 input_filename = file;
5027 return new_friend;
5030 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5031 template arguments, as for tsubst.
5033 Returns an appropriate tsubst'd friend type or error_mark_node on
5034 failure. */
5036 static tree
5037 tsubst_friend_class (friend_tmpl, args)
5038 tree friend_tmpl;
5039 tree args;
5041 tree friend_type;
5042 tree tmpl;
5043 tree context;
5045 context = DECL_CONTEXT (friend_tmpl);
5047 if (context)
5049 if (TREE_CODE (context) == NAMESPACE_DECL)
5050 push_nested_namespace (context);
5051 else
5052 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5055 /* First, we look for a class template. */
5056 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5058 /* But, if we don't find one, it might be because we're in a
5059 situation like this:
5061 template <class T>
5062 struct S {
5063 template <class U>
5064 friend struct S;
5067 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5068 for `S<int>', not the TEMPLATE_DECL. */
5069 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5071 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5072 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5075 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5077 /* The friend template has already been declared. Just
5078 check to see that the declarations match, and install any new
5079 default parameters. We must tsubst the default parameters,
5080 of course. We only need the innermost template parameters
5081 because that is all that redeclare_class_template will look
5082 at. */
5083 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5084 > TMPL_ARGS_DEPTH (args))
5086 tree parms;
5087 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5088 args, tf_error | tf_warning);
5089 redeclare_class_template (TREE_TYPE (tmpl), parms);
5092 friend_type = TREE_TYPE (tmpl);
5094 else
5096 /* The friend template has not already been declared. In this
5097 case, the instantiation of the template class will cause the
5098 injection of this template into the global scope. */
5099 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5101 /* The new TMPL is not an instantiation of anything, so we
5102 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5103 the new type because that is supposed to be the corresponding
5104 template decl, i.e., TMPL. */
5105 DECL_USE_TEMPLATE (tmpl) = 0;
5106 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5107 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5109 /* Inject this template into the global scope. */
5110 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5113 if (context)
5115 if (TREE_CODE (context) == NAMESPACE_DECL)
5116 pop_nested_namespace (context);
5117 else
5118 pop_nested_class ();
5121 return friend_type;
5124 /* Returns zero if TYPE cannot be completed later due to circularity.
5125 Otherwise returns one. */
5127 static int
5128 can_complete_type_without_circularity (type)
5129 tree type;
5131 if (type == NULL_TREE || type == error_mark_node)
5132 return 0;
5133 else if (COMPLETE_TYPE_P (type))
5134 return 1;
5135 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5136 return can_complete_type_without_circularity (TREE_TYPE (type));
5137 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5138 return 0;
5139 else
5140 return 1;
5143 tree
5144 instantiate_class_template (type)
5145 tree type;
5147 tree template, args, pattern, t, member;
5148 tree typedecl;
5149 tree pbinfo;
5151 if (type == error_mark_node)
5152 return error_mark_node;
5154 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
5155 return type;
5157 /* Figure out which template is being instantiated. */
5158 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5159 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
5161 /* Figure out which arguments are being used to do the
5162 instantiation. */
5163 args = CLASSTYPE_TI_ARGS (type);
5165 /* Determine what specialization of the original template to
5166 instantiate. */
5167 t = most_specialized_class (template, args);
5168 if (t == error_mark_node)
5170 const char *str = "candidates are:";
5171 error ("ambiguous class template instantiation for `%#T'", type);
5172 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5173 t = TREE_CHAIN (t))
5175 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5176 args))
5178 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5179 str = " ";
5182 TYPE_BEING_DEFINED (type) = 1;
5183 return error_mark_node;
5186 if (t)
5187 pattern = TREE_TYPE (t);
5188 else
5189 pattern = TREE_TYPE (template);
5191 /* If the template we're instantiating is incomplete, then clearly
5192 there's nothing we can do. */
5193 if (!COMPLETE_TYPE_P (pattern))
5194 return type;
5196 /* If we've recursively instantiated too many templates, stop. */
5197 if (! push_tinst_level (type))
5198 return type;
5200 /* Now we're really doing the instantiation. Mark the type as in
5201 the process of being defined. */
5202 TYPE_BEING_DEFINED (type) = 1;
5204 maybe_push_to_top_level (uses_template_parms (type));
5206 if (t)
5208 /* This TYPE is actually an instantiation of a partial
5209 specialization. We replace the innermost set of ARGS with
5210 the arguments appropriate for substitution. For example,
5211 given:
5213 template <class T> struct S {};
5214 template <class T> struct S<T*> {};
5216 and supposing that we are instantiating S<int*>, ARGS will
5217 present be {int*} but we need {int}. */
5218 tree inner_args
5219 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5220 args);
5222 /* If there were multiple levels in ARGS, replacing the
5223 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5224 want, so we make a copy first. */
5225 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5227 args = copy_node (args);
5228 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5230 else
5231 args = inner_args;
5234 if (flag_external_templates)
5236 if (flag_alt_external_templates)
5238 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5239 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5241 else
5243 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5244 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5245 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5248 else
5250 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5253 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5254 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5255 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5256 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5257 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5258 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5259 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5260 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5261 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5262 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5263 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5264 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5265 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5266 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5267 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5268 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5269 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5270 TYPE_USES_MULTIPLE_INHERITANCE (type)
5271 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5272 TYPE_USES_VIRTUAL_BASECLASSES (type)
5273 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5274 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5275 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5276 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5277 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5278 if (ANON_AGGR_TYPE_P (pattern))
5279 SET_ANON_AGGR_TYPE_P (type);
5281 pbinfo = TYPE_BINFO (pattern);
5283 if (BINFO_BASETYPES (pbinfo))
5285 tree base_list = NULL_TREE;
5286 tree pbases = BINFO_BASETYPES (pbinfo);
5287 tree paccesses = BINFO_BASEACCESSES (pbinfo);
5288 int i;
5290 /* Substitute into each of the bases to determine the actual
5291 basetypes. */
5292 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5294 tree base;
5295 tree access;
5296 tree pbase;
5298 pbase = TREE_VEC_ELT (pbases, i);
5299 access = TREE_VEC_ELT (paccesses, i);
5301 /* Substitute to figure out the base class. */
5302 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5303 if (base == error_mark_node)
5304 continue;
5306 base_list = tree_cons (access, base, base_list);
5307 TREE_VIA_VIRTUAL (base_list) = TREE_VIA_VIRTUAL (pbase);
5310 /* The list is now in reverse order; correct that. */
5311 base_list = nreverse (base_list);
5313 /* Now call xref_basetypes to set up all the base-class
5314 information. */
5315 xref_basetypes (type, base_list);
5318 /* Now that our base classes are set up, enter the scope of the
5319 class, so that name lookups into base classes, etc. will work
5320 correctly. This is precisely analogous to what we do in
5321 begin_class_definition when defining an ordinary non-template
5322 class. */
5323 pushclass (type, true);
5325 /* Now members are processed in the order of declaration. */
5326 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
5328 tree t = TREE_VALUE (member);
5330 if (TREE_PURPOSE (member))
5332 if (TYPE_P (t))
5334 /* Build new CLASSTYPE_TAGS. */
5336 tree tag = t;
5337 tree name = TYPE_IDENTIFIER (tag);
5338 tree newtag;
5340 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5341 my_friendly_assert (newtag != error_mark_node, 20010206);
5342 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5344 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5345 /* Unfortunately, lookup_template_class sets
5346 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5347 instantiation (i.e., for the type of a member template
5348 class nested within a template class.) This behavior is
5349 required for maybe_process_partial_specialization to work
5350 correctly, but is not accurate in this case; the TAG is not
5351 an instantiation of anything. (The corresponding
5352 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5353 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5355 /* Now, we call pushtag to put this NEWTAG into the scope of
5356 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5357 pushtag calling push_template_decl. We don't have to do
5358 this for enums because it will already have been done in
5359 tsubst_enum. */
5360 if (name)
5361 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5362 pushtag (name, newtag, /*globalize=*/0);
5365 else if (TREE_CODE (t) == FUNCTION_DECL
5366 || DECL_FUNCTION_TEMPLATE_P (t))
5368 /* Build new TYPE_METHODS. */
5370 tree r = tsubst (t, args, tf_error, NULL_TREE);
5371 set_current_access_from_decl (r);
5372 grok_special_member_properties (r);
5373 finish_member_declaration (r);
5375 else
5377 /* Build new TYPE_FIELDS. */
5379 if (TREE_CODE (t) != CONST_DECL)
5381 tree r;
5383 /* The the file and line for this declaration, to assist
5384 in error message reporting. Since we called
5385 push_tinst_level above, we don't need to restore these. */
5386 lineno = DECL_SOURCE_LINE (t);
5387 input_filename = DECL_SOURCE_FILE (t);
5389 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5390 if (TREE_CODE (r) == VAR_DECL)
5392 tree init;
5394 if (DECL_INITIALIZED_IN_CLASS_P (r))
5395 init = tsubst_expr (DECL_INITIAL (t), args,
5396 tf_error | tf_warning, NULL_TREE);
5397 else
5398 init = NULL_TREE;
5400 finish_static_data_member_decl (r, init,
5401 /*asmspec_tree=*/NULL_TREE,
5402 /*flags=*/0);
5404 if (DECL_INITIALIZED_IN_CLASS_P (r))
5405 check_static_variable_definition (r, TREE_TYPE (r));
5407 else if (TREE_CODE (r) == FIELD_DECL)
5409 /* Determine whether R has a valid type and can be
5410 completed later. If R is invalid, then it is
5411 replaced by error_mark_node so that it will not be
5412 added to TYPE_FIELDS. */
5413 tree rtype = TREE_TYPE (r);
5414 if (can_complete_type_without_circularity (rtype))
5415 complete_type (rtype);
5417 if (!COMPLETE_TYPE_P (rtype))
5419 cxx_incomplete_type_error (r, rtype);
5420 r = error_mark_node;
5424 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5425 such a thing will already have been added to the field
5426 list by tsubst_enum in finish_member_declaration in the
5427 CLASSTYPE_TAGS case above. */
5428 if (!(TREE_CODE (r) == TYPE_DECL
5429 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5430 && TYPE_CONTEXT (TREE_TYPE (r)) == type))
5432 set_current_access_from_decl (r);
5433 finish_member_declaration (r);
5438 else
5440 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5442 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5444 tree friend_type = t;
5445 tree new_friend_type;
5447 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5448 new_friend_type = tsubst_friend_class (friend_type, args);
5449 else if (uses_template_parms (friend_type))
5450 new_friend_type = tsubst (friend_type, args,
5451 tf_error | tf_warning, NULL_TREE);
5452 else
5454 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5456 /* The call to xref_tag_from_type does injection for friend
5457 classes. */
5458 push_nested_namespace (ns);
5459 new_friend_type =
5460 xref_tag_from_type (friend_type, NULL_TREE, 1);
5461 pop_nested_namespace (ns);
5464 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5465 /* Trick make_friend_class into realizing that the friend
5466 we're adding is a template, not an ordinary class. It's
5467 important that we use make_friend_class since it will
5468 perform some error-checking and output cross-reference
5469 information. */
5470 ++processing_template_decl;
5472 if (new_friend_type != error_mark_node)
5473 make_friend_class (type, new_friend_type);
5475 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5476 --processing_template_decl;
5478 else
5480 /* Build new DECL_FRIENDLIST. */
5482 add_friend (type,
5483 tsubst_friend_function (t, args));
5488 /* Set the file and line number information to whatever is given for
5489 the class itself. This puts error messages involving generated
5490 implicit functions at a predictable point, and the same point
5491 that would be used for non-template classes. */
5492 typedecl = TYPE_MAIN_DECL (type);
5493 lineno = DECL_SOURCE_LINE (typedecl);
5494 input_filename = DECL_SOURCE_FILE (typedecl);
5496 unreverse_member_declarations (type);
5497 finish_struct_1 (type);
5498 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5500 /* Clear this now so repo_template_used is happy. */
5501 TYPE_BEING_DEFINED (type) = 0;
5502 repo_template_used (type);
5504 /* Now that the class is complete, instantiate default arguments for
5505 any member functions. We don't do this earlier because the
5506 default arguments may reference members of the class. */
5507 if (!PRIMARY_TEMPLATE_P (template))
5508 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5509 if (TREE_CODE (t) == FUNCTION_DECL
5510 /* Implicitly generated member functions will not have template
5511 information; they are not instantiations, but instead are
5512 created "fresh" for each instantiation. */
5513 && DECL_TEMPLATE_INFO (t))
5514 tsubst_default_arguments (t);
5516 popclass ();
5517 pop_from_top_level ();
5518 pop_tinst_level ();
5520 if (TYPE_CONTAINS_VPTR_P (type))
5521 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5523 return type;
5526 static int
5527 list_eq (t1, t2)
5528 tree t1, t2;
5530 if (t1 == NULL_TREE)
5531 return t2 == NULL_TREE;
5532 if (t2 == NULL_TREE)
5533 return 0;
5534 /* Don't care if one declares its arg const and the other doesn't -- the
5535 main variant of the arg type is all that matters. */
5536 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5537 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5538 return 0;
5539 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5542 /* If arg is a non-type template parameter that does not depend on template
5543 arguments, fold it like we weren't in the body of a template. */
5545 static tree
5546 maybe_fold_nontype_arg (arg)
5547 tree arg;
5549 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5551 /* Sometimes, one of the args was an expression involving a
5552 template constant parameter, like N - 1. Now that we've
5553 tsubst'd, we might have something like 2 - 1. This will
5554 confuse lookup_template_class, so we do constant folding
5555 here. We have to unset processing_template_decl, to
5556 fool build_expr_from_tree() into building an actual
5557 tree. */
5559 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5560 as simple as it's going to get, and trying to reprocess
5561 the trees will break. */
5562 if (!TREE_TYPE (arg))
5564 int saved_processing_template_decl = processing_template_decl;
5565 processing_template_decl = 0;
5566 arg = build_expr_from_tree (arg);
5567 processing_template_decl = saved_processing_template_decl;
5570 arg = fold (arg);
5572 return arg;
5575 /* Substitute ARGS into the vector of template arguments T. */
5577 static tree
5578 tsubst_template_arg_vector (t, args, complain)
5579 tree t;
5580 tree args;
5581 tsubst_flags_t complain;
5583 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5584 tree *elts = (tree *) alloca (len * sizeof (tree));
5586 memset ((char *) elts, 0, len * sizeof (tree));
5588 for (i = 0; i < len; i++)
5590 if (TREE_VEC_ELT (t, i) != NULL_TREE
5591 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5592 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5593 args, complain);
5594 else
5595 elts[i] = maybe_fold_nontype_arg
5596 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5597 NULL_TREE));
5599 if (elts[i] == error_mark_node)
5600 return error_mark_node;
5602 if (elts[i] != TREE_VEC_ELT (t, i))
5603 need_new = 1;
5606 if (!need_new)
5607 return t;
5609 t = make_tree_vec (len);
5610 for (i = 0; i < len; i++)
5611 TREE_VEC_ELT (t, i) = elts[i];
5613 return t;
5616 /* Return the result of substituting ARGS into the template parameters
5617 given by PARMS. If there are m levels of ARGS and m + n levels of
5618 PARMS, then the result will contain n levels of PARMS. For
5619 example, if PARMS is `template <class T> template <class U>
5620 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5621 result will be `template <int*, double, class V>'. */
5623 static tree
5624 tsubst_template_parms (parms, args, complain)
5625 tree parms;
5626 tree args;
5627 tsubst_flags_t complain;
5629 tree r = NULL_TREE;
5630 tree* new_parms;
5632 for (new_parms = &r;
5633 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5634 new_parms = &(TREE_CHAIN (*new_parms)),
5635 parms = TREE_CHAIN (parms))
5637 tree new_vec =
5638 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5639 int i;
5641 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5643 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5644 tree default_value = TREE_PURPOSE (tuple);
5645 tree parm_decl = TREE_VALUE (tuple);
5647 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5648 default_value = tsubst_expr (default_value, args,
5649 complain, NULL_TREE);
5650 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5651 parm_decl);
5652 TREE_VEC_ELT (new_vec, i) = tuple;
5655 *new_parms =
5656 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5657 - TMPL_ARGS_DEPTH (args)),
5658 new_vec, NULL_TREE);
5661 return r;
5664 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5665 type T. If T is not an aggregate or enumeration type, it is
5666 handled as if by tsubst. IN_DECL is as for tsubst. If
5667 ENTERING_SCOPE is nonzero, T is the context for a template which
5668 we are presently tsubst'ing. Return the substituted value. */
5670 static tree
5671 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5672 tree t;
5673 tree args;
5674 tsubst_flags_t complain;
5675 tree in_decl;
5676 int entering_scope;
5678 if (t == NULL_TREE)
5679 return NULL_TREE;
5681 switch (TREE_CODE (t))
5683 case RECORD_TYPE:
5684 if (TYPE_PTRMEMFUNC_P (t))
5685 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5687 /* else fall through */
5688 case ENUMERAL_TYPE:
5689 case UNION_TYPE:
5690 if (TYPE_TEMPLATE_INFO (t))
5692 tree argvec;
5693 tree context;
5694 tree r;
5696 /* First, determine the context for the type we are looking
5697 up. */
5698 if (TYPE_CONTEXT (t) != NULL_TREE)
5699 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5700 complain,
5701 in_decl, /*entering_scope=*/1);
5702 else
5703 context = NULL_TREE;
5705 /* Then, figure out what arguments are appropriate for the
5706 type we are trying to find. For example, given:
5708 template <class T> struct S;
5709 template <class T, class U> void f(T, U) { S<U> su; }
5711 and supposing that we are instantiating f<int, double>,
5712 then our ARGS will be {int, double}, but, when looking up
5713 S we only want {double}. */
5714 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5715 complain);
5716 if (argvec == error_mark_node)
5717 return error_mark_node;
5719 r = lookup_template_class (t, argvec, in_decl, context,
5720 entering_scope, complain);
5722 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5724 else
5725 /* This is not a template type, so there's nothing to do. */
5726 return t;
5728 default:
5729 return tsubst (t, args, complain, in_decl);
5733 /* Substitute into the default argument ARG (a default argument for
5734 FN), which has the indicated TYPE. */
5736 tree
5737 tsubst_default_argument (fn, type, arg)
5738 tree fn;
5739 tree type;
5740 tree arg;
5742 /* This default argument came from a template. Instantiate the
5743 default argument here, not in tsubst. In the case of
5744 something like:
5746 template <class T>
5747 struct S {
5748 static T t();
5749 void f(T = t());
5752 we must be careful to do name lookup in the scope of S<T>,
5753 rather than in the current class.
5755 ??? current_class_type affects a lot more than name lookup. This is
5756 very fragile. Fortunately, it will go away when we do 2-phase name
5757 binding properly. */
5759 /* FN is already the desired FUNCTION_DECL. */
5760 push_access_scope (fn);
5762 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5763 tf_error | tf_warning, NULL_TREE);
5765 pop_access_scope (fn);
5767 /* Make sure the default argument is reasonable. */
5768 arg = check_default_argument (type, arg);
5770 return arg;
5773 /* Substitute into all the default arguments for FN. */
5775 static void
5776 tsubst_default_arguments (fn)
5777 tree fn;
5779 tree arg;
5780 tree tmpl_args;
5782 tmpl_args = DECL_TI_ARGS (fn);
5784 /* If this function is not yet instantiated, we certainly don't need
5785 its default arguments. */
5786 if (uses_template_parms (tmpl_args))
5787 return;
5789 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5790 arg;
5791 arg = TREE_CHAIN (arg))
5792 if (TREE_PURPOSE (arg))
5793 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5794 TREE_VALUE (arg),
5795 TREE_PURPOSE (arg));
5798 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5799 (already computed) substitution of ARGS into TREE_TYPE (T), if
5800 appropriate. Return the result of the substitution. Issue error
5801 and warning messages under control of COMPLAIN. */
5803 static tree
5804 tsubst_decl (t, args, type, complain)
5805 tree t;
5806 tree args;
5807 tree type;
5808 tsubst_flags_t complain;
5810 int saved_lineno;
5811 const char *saved_filename;
5812 tree r = NULL_TREE;
5813 tree in_decl = t;
5815 /* Set the filename and linenumber to improve error-reporting. */
5816 saved_lineno = lineno;
5817 saved_filename = input_filename;
5818 lineno = DECL_SOURCE_LINE (t);
5819 input_filename = DECL_SOURCE_FILE (t);
5821 switch (TREE_CODE (t))
5823 case TEMPLATE_DECL:
5825 /* We can get here when processing a member template function
5826 of a template class. */
5827 tree decl = DECL_TEMPLATE_RESULT (t);
5828 tree spec;
5829 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5831 if (!is_template_template_parm)
5833 /* We might already have an instance of this template.
5834 The ARGS are for the surrounding class type, so the
5835 full args contain the tsubst'd args for the context,
5836 plus the innermost args from the template decl. */
5837 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5838 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5839 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5840 tree full_args;
5842 full_args = tsubst_template_arg_vector (tmpl_args, args,
5843 complain);
5845 /* tsubst_template_arg_vector doesn't copy the vector if
5846 nothing changed. But, *something* should have
5847 changed. */
5848 my_friendly_assert (full_args != tmpl_args, 0);
5850 spec = retrieve_specialization (t, full_args);
5851 if (spec != NULL_TREE)
5853 r = spec;
5854 break;
5858 /* Make a new template decl. It will be similar to the
5859 original, but will record the current template arguments.
5860 We also create a new function declaration, which is just
5861 like the old one, but points to this new template, rather
5862 than the old one. */
5863 r = copy_decl (t);
5864 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5865 TREE_CHAIN (r) = NULL_TREE;
5867 if (is_template_template_parm)
5869 tree new_decl = tsubst (decl, args, complain, in_decl);
5870 DECL_TEMPLATE_RESULT (r) = new_decl;
5871 TREE_TYPE (r) = TREE_TYPE (new_decl);
5872 break;
5875 DECL_CONTEXT (r)
5876 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5877 complain, in_decl,
5878 /*entering_scope=*/1);
5879 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5881 if (TREE_CODE (decl) == TYPE_DECL)
5883 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5884 TREE_TYPE (r) = new_type;
5885 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5886 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5887 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5889 else
5891 tree new_decl = tsubst (decl, args, complain, in_decl);
5893 DECL_TEMPLATE_RESULT (r) = new_decl;
5894 DECL_TI_TEMPLATE (new_decl) = r;
5895 TREE_TYPE (r) = TREE_TYPE (new_decl);
5896 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5899 SET_DECL_IMPLICIT_INSTANTIATION (r);
5900 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5901 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5903 /* The template parameters for this new template are all the
5904 template parameters for the old template, except the
5905 outermost level of parameters. */
5906 DECL_TEMPLATE_PARMS (r)
5907 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5908 complain);
5910 if (PRIMARY_TEMPLATE_P (t))
5911 DECL_PRIMARY_TEMPLATE (r) = r;
5913 /* We don't partially instantiate partial specializations. */
5914 if (TREE_CODE (decl) == TYPE_DECL)
5915 break;
5917 /* Record this partial instantiation. */
5918 register_specialization (r, t,
5919 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5922 break;
5924 case FUNCTION_DECL:
5926 tree ctx;
5927 tree argvec = NULL_TREE;
5928 tree *friends;
5929 tree gen_tmpl;
5930 int member;
5931 int args_depth;
5932 int parms_depth;
5934 /* Nobody should be tsubst'ing into non-template functions. */
5935 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5937 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5939 tree spec;
5941 /* Calculate the most general template of which R is a
5942 specialization, and the complete set of arguments used to
5943 specialize R. */
5944 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5945 argvec
5946 = tsubst_template_arg_vector (DECL_TI_ARGS
5947 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5948 args, complain);
5950 /* Check to see if we already have this specialization. */
5951 spec = retrieve_specialization (gen_tmpl, argvec);
5953 if (spec)
5955 r = spec;
5956 break;
5959 /* We can see more levels of arguments than parameters if
5960 there was a specialization of a member template, like
5961 this:
5963 template <class T> struct S { template <class U> void f(); }
5964 template <> template <class U> void S<int>::f(U);
5966 Here, we'll be substituting into the specialization,
5967 because that's where we can find the code we actually
5968 want to generate, but we'll have enough arguments for
5969 the most general template.
5971 We also deal with the peculiar case:
5973 template <class T> struct S {
5974 template <class U> friend void f();
5976 template <class U> void f() {}
5977 template S<int>;
5978 template void f<double>();
5980 Here, the ARGS for the instantiation of will be {int,
5981 double}. But, we only need as many ARGS as there are
5982 levels of template parameters in CODE_PATTERN. We are
5983 careful not to get fooled into reducing the ARGS in
5984 situations like:
5986 template <class T> struct S { template <class U> void f(U); }
5987 template <class T> template <> void S<T>::f(int) {}
5989 which we can spot because the pattern will be a
5990 specialization in this case. */
5991 args_depth = TMPL_ARGS_DEPTH (args);
5992 parms_depth =
5993 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5994 if (args_depth > parms_depth
5995 && !DECL_TEMPLATE_SPECIALIZATION (t))
5996 args = get_innermost_template_args (args, parms_depth);
5998 else
6000 /* This special case arises when we have something like this:
6002 template <class T> struct S {
6003 friend void f<int>(int, double);
6006 Here, the DECL_TI_TEMPLATE for the friend declaration
6007 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
6008 being called from tsubst_friend_function, and we want
6009 only to create a new decl (R) with appropriate types so
6010 that we can call determine_specialization. */
6011 gen_tmpl = NULL_TREE;
6014 if (DECL_CLASS_SCOPE_P (t))
6016 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6017 member = 2;
6018 else
6019 member = 1;
6020 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6021 complain, t,
6022 /*entering_scope=*/1);
6024 else
6026 member = 0;
6027 ctx = DECL_CONTEXT (t);
6029 type = tsubst (type, args, complain, in_decl);
6030 if (type == error_mark_node)
6031 return error_mark_node;
6033 /* We do NOT check for matching decls pushed separately at this
6034 point, as they may not represent instantiations of this
6035 template, and in any case are considered separate under the
6036 discrete model. */
6037 r = copy_decl (t);
6038 DECL_USE_TEMPLATE (r) = 0;
6039 TREE_TYPE (r) = type;
6040 /* Clear out the mangled name and RTL for the instantiation. */
6041 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6042 SET_DECL_RTL (r, NULL_RTX);
6044 DECL_CONTEXT (r) = ctx;
6046 if (member && DECL_CONV_FN_P (r))
6047 /* Type-conversion operator. Reconstruct the name, in
6048 case it's the name of one of the template's parameters. */
6049 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6051 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6052 complain, t);
6053 DECL_RESULT (r) = NULL_TREE;
6055 TREE_STATIC (r) = 0;
6056 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6057 DECL_EXTERNAL (r) = 1;
6058 DECL_INTERFACE_KNOWN (r) = 0;
6059 DECL_DEFER_OUTPUT (r) = 0;
6060 TREE_CHAIN (r) = NULL_TREE;
6061 DECL_PENDING_INLINE_INFO (r) = 0;
6062 DECL_PENDING_INLINE_P (r) = 0;
6063 DECL_SAVED_TREE (r) = NULL_TREE;
6064 TREE_USED (r) = 0;
6065 if (DECL_CLONED_FUNCTION (r))
6067 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6068 args, complain, t);
6069 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6070 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6073 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6074 this in the special friend case mentioned above where
6075 GEN_TMPL is NULL. */
6076 if (gen_tmpl)
6078 DECL_TEMPLATE_INFO (r)
6079 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6080 SET_DECL_IMPLICIT_INSTANTIATION (r);
6081 register_specialization (r, gen_tmpl, argvec);
6083 /* We're not supposed to instantiate default arguments
6084 until they are called, for a template. But, for a
6085 declaration like:
6087 template <class T> void f ()
6088 { extern void g(int i = T()); }
6090 we should do the substitution when the template is
6091 instantiated. We handle the member function case in
6092 instantiate_class_template since the default arguments
6093 might refer to other members of the class. */
6094 if (!member
6095 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6096 && !uses_template_parms (argvec))
6097 tsubst_default_arguments (r);
6100 /* Copy the list of befriending classes. */
6101 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6102 *friends;
6103 friends = &TREE_CHAIN (*friends))
6105 *friends = copy_node (*friends);
6106 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6107 args, complain,
6108 in_decl);
6111 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6113 maybe_retrofit_in_chrg (r);
6114 if (DECL_CONSTRUCTOR_P (r))
6115 grok_ctor_properties (ctx, r);
6116 /* If this is an instantiation of a member template, clone it.
6117 If it isn't, that'll be handled by
6118 clone_constructors_and_destructors. */
6119 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6120 clone_function_decl (r, /*update_method_vec_p=*/0);
6122 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6123 grok_op_properties (r, DECL_FRIEND_P (r));
6125 break;
6127 case PARM_DECL:
6129 r = copy_node (t);
6130 if (DECL_TEMPLATE_PARM_P (t))
6131 SET_DECL_TEMPLATE_PARM_P (r);
6133 TREE_TYPE (r) = type;
6134 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6136 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6137 DECL_INITIAL (r) = TREE_TYPE (r);
6138 else
6139 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6140 complain, in_decl);
6142 DECL_CONTEXT (r) = NULL_TREE;
6144 if (!DECL_TEMPLATE_PARM_P (r))
6145 DECL_ARG_TYPE (r) = type_passed_as (type);
6146 if (TREE_CHAIN (t))
6147 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6148 complain, TREE_CHAIN (t));
6150 break;
6152 case FIELD_DECL:
6154 r = copy_decl (t);
6155 TREE_TYPE (r) = type;
6156 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6158 /* We don't have to set DECL_CONTEXT here; it is set by
6159 finish_member_declaration. */
6160 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6161 complain, in_decl);
6162 TREE_CHAIN (r) = NULL_TREE;
6163 if (VOID_TYPE_P (type))
6164 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6166 break;
6168 case USING_DECL:
6170 r = copy_node (t);
6171 DECL_INITIAL (r)
6172 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6173 TREE_CHAIN (r) = NULL_TREE;
6175 break;
6177 case TYPE_DECL:
6178 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6179 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6181 /* If this is the canonical decl, we don't have to mess with
6182 instantiations, and often we can't (for typename, template
6183 type parms and such). Note that TYPE_NAME is not correct for
6184 the above test if we've copied the type for a typedef. */
6185 r = TYPE_NAME (type);
6186 break;
6189 /* Fall through. */
6191 case VAR_DECL:
6193 tree argvec = NULL_TREE;
6194 tree gen_tmpl = NULL_TREE;
6195 tree spec;
6196 tree tmpl = NULL_TREE;
6197 tree ctx;
6198 int local_p;
6200 /* Assume this is a non-local variable. */
6201 local_p = 0;
6203 if (TYPE_P (CP_DECL_CONTEXT (t)))
6204 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6205 complain,
6206 in_decl, /*entering_scope=*/1);
6207 else if (DECL_NAMESPACE_SCOPE_P (t))
6208 ctx = DECL_CONTEXT (t);
6209 else
6211 /* Subsequent calls to pushdecl will fill this in. */
6212 ctx = NULL_TREE;
6213 local_p = 1;
6216 /* Check to see if we already have this specialization. */
6217 if (!local_p)
6219 tmpl = DECL_TI_TEMPLATE (t);
6220 gen_tmpl = most_general_template (tmpl);
6221 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6222 spec = retrieve_specialization (gen_tmpl, argvec);
6224 else
6225 spec = retrieve_local_specialization (t);
6227 if (spec)
6229 r = spec;
6230 break;
6233 r = copy_decl (t);
6234 if (TREE_CODE (r) == VAR_DECL)
6235 type = complete_type (type);
6236 TREE_TYPE (r) = type;
6237 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6238 DECL_CONTEXT (r) = ctx;
6239 /* Clear out the mangled name and RTL for the instantiation. */
6240 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6241 SET_DECL_RTL (r, NULL_RTX);
6243 /* Don't try to expand the initializer until someone tries to use
6244 this variable; otherwise we run into circular dependencies. */
6245 DECL_INITIAL (r) = NULL_TREE;
6246 SET_DECL_RTL (r, NULL_RTX);
6247 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6249 /* Even if the original location is out of scope, the newly
6250 substituted one is not. */
6251 if (TREE_CODE (r) == VAR_DECL)
6253 DECL_DEAD_FOR_LOCAL (r) = 0;
6254 DECL_INITIALIZED_P (r) = 0;
6257 if (!local_p)
6259 /* A static data member declaration is always marked
6260 external when it is declared in-class, even if an
6261 initializer is present. We mimic the non-template
6262 processing here. */
6263 DECL_EXTERNAL (r) = 1;
6265 register_specialization (r, gen_tmpl, argvec);
6266 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6267 SET_DECL_IMPLICIT_INSTANTIATION (r);
6269 else
6270 register_local_specialization (r, t);
6272 TREE_CHAIN (r) = NULL_TREE;
6273 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6274 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6275 /* Compute the size, alignment, etc. of R. */
6276 layout_decl (r, 0);
6278 break;
6280 default:
6281 abort ();
6284 /* Restore the file and line information. */
6285 lineno = saved_lineno;
6286 input_filename = saved_filename;
6288 return r;
6291 /* Substitue into the ARG_TYPES of a function type. */
6293 static tree
6294 tsubst_arg_types (arg_types, args, complain, in_decl)
6295 tree arg_types;
6296 tree args;
6297 tsubst_flags_t complain;
6298 tree in_decl;
6300 tree remaining_arg_types;
6301 tree type;
6303 if (!arg_types || arg_types == void_list_node)
6304 return arg_types;
6306 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6307 args, complain, in_decl);
6308 if (remaining_arg_types == error_mark_node)
6309 return error_mark_node;
6311 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6312 if (type == error_mark_node)
6313 return error_mark_node;
6314 if (VOID_TYPE_P (type))
6316 if (complain & tf_error)
6318 error ("invalid parameter type `%T'", type);
6319 if (in_decl)
6320 cp_error_at ("in declaration `%D'", in_decl);
6322 return error_mark_node;
6325 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6326 top-level qualifiers as required. */
6327 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6329 /* Note that we do not substitute into default arguments here. The
6330 standard mandates that they be instantiated only when needed,
6331 which is done in build_over_call. */
6332 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6333 remaining_arg_types);
6337 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6338 *not* handle the exception-specification for FNTYPE, because the
6339 initial substitution of explicitly provided template parameters
6340 during argument deduction forbids substitution into the
6341 exception-specification:
6343 [temp.deduct]
6345 All references in the function type of the function template to the
6346 corresponding template parameters are replaced by the specified tem-
6347 plate argument values. If a substitution in a template parameter or
6348 in the function type of the function template results in an invalid
6349 type, type deduction fails. [Note: The equivalent substitution in
6350 exception specifications is done only when the function is instanti-
6351 ated, at which point a program is ill-formed if the substitution
6352 results in an invalid type.] */
6354 static tree
6355 tsubst_function_type (t, args, complain, in_decl)
6356 tree t;
6357 tree args;
6358 tsubst_flags_t complain;
6359 tree in_decl;
6361 tree return_type;
6362 tree arg_types;
6363 tree fntype;
6365 /* The TYPE_CONTEXT is not used for function/method types. */
6366 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6368 /* Substitute the return type. */
6369 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6370 if (return_type == error_mark_node)
6371 return error_mark_node;
6373 /* Substitue the argument types. */
6374 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6375 complain, in_decl);
6376 if (arg_types == error_mark_node)
6377 return error_mark_node;
6379 /* Construct a new type node and return it. */
6380 if (TREE_CODE (t) == FUNCTION_TYPE)
6381 fntype = build_function_type (return_type, arg_types);
6382 else
6384 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6385 if (! IS_AGGR_TYPE (r))
6387 /* [temp.deduct]
6389 Type deduction may fail for any of the following
6390 reasons:
6392 -- Attempting to create "pointer to member of T" when T
6393 is not a class type. */
6394 if (complain & tf_error)
6395 error ("creating pointer to member function of non-class type `%T'",
6397 return error_mark_node;
6400 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6401 (arg_types));
6403 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6404 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6406 return fntype;
6409 /* Substitute into the PARMS of a call-declarator. */
6411 static tree
6412 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6413 tree parms;
6414 tree args;
6415 tsubst_flags_t complain;
6416 tree in_decl;
6418 tree new_parms;
6419 tree type;
6420 tree defarg;
6422 if (!parms || parms == void_list_node)
6423 return parms;
6425 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6426 args, complain, in_decl);
6428 /* Figure out the type of this parameter. */
6429 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6431 /* Figure out the default argument as well. Note that we use
6432 tsubst_expr since the default argument is really an expression. */
6433 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6435 /* Chain this parameter on to the front of those we have already
6436 processed. We don't use hash_tree_cons because that function
6437 doesn't check TREE_PARMLIST. */
6438 new_parms = tree_cons (defarg, type, new_parms);
6440 /* And note that these are parameters. */
6441 TREE_PARMLIST (new_parms) = 1;
6443 return new_parms;
6446 /* Take the tree structure T and replace template parameters used
6447 therein with the argument vector ARGS. IN_DECL is an associated
6448 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6449 Issue error and warning messages under control of COMPLAIN. Note
6450 that we must be relatively non-tolerant of extensions here, in
6451 order to preserve conformance; if we allow substitutions that
6452 should not be allowed, we may allow argument deductions that should
6453 not succeed, and therefore report ambiguous overload situations
6454 where there are none. In theory, we could allow the substitution,
6455 but indicate that it should have failed, and allow our caller to
6456 make sure that the right thing happens, but we don't try to do this
6457 yet.
6459 This function is used for dealing with types, decls and the like;
6460 for expressions, use tsubst_expr or tsubst_copy. */
6462 static tree
6463 tsubst (t, args, complain, in_decl)
6464 tree t, args;
6465 tsubst_flags_t complain;
6466 tree in_decl;
6468 tree type, r;
6470 if (t == NULL_TREE || t == error_mark_node
6471 || t == integer_type_node
6472 || t == void_type_node
6473 || t == char_type_node
6474 || TREE_CODE (t) == NAMESPACE_DECL)
6475 return t;
6477 if (TREE_CODE (t) == IDENTIFIER_NODE)
6478 type = IDENTIFIER_TYPE_VALUE (t);
6479 else
6480 type = TREE_TYPE (t);
6481 if (type == unknown_type_node)
6482 abort ();
6484 if (type && TREE_CODE (t) != FUNCTION_DECL
6485 && TREE_CODE (t) != TYPENAME_TYPE
6486 && TREE_CODE (t) != TEMPLATE_DECL
6487 && TREE_CODE (t) != IDENTIFIER_NODE
6488 && TREE_CODE (t) != FUNCTION_TYPE
6489 && TREE_CODE (t) != METHOD_TYPE)
6490 type = tsubst (type, args, complain, in_decl);
6491 if (type == error_mark_node)
6492 return error_mark_node;
6494 if (DECL_P (t))
6495 return tsubst_decl (t, args, type, complain);
6497 switch (TREE_CODE (t))
6499 case RECORD_TYPE:
6500 case UNION_TYPE:
6501 case ENUMERAL_TYPE:
6502 return tsubst_aggr_type (t, args, complain, in_decl,
6503 /*entering_scope=*/0);
6505 case ERROR_MARK:
6506 case IDENTIFIER_NODE:
6507 case VOID_TYPE:
6508 case REAL_TYPE:
6509 case COMPLEX_TYPE:
6510 case VECTOR_TYPE:
6511 case BOOLEAN_TYPE:
6512 case INTEGER_CST:
6513 case REAL_CST:
6514 case STRING_CST:
6515 return t;
6517 case INTEGER_TYPE:
6518 if (t == integer_type_node)
6519 return t;
6521 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6522 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6523 return t;
6526 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6528 max = tsubst_expr (omax, args, complain, in_decl);
6529 if (max == error_mark_node)
6530 return error_mark_node;
6532 /* See if we can reduce this expression to something simpler. */
6533 max = maybe_fold_nontype_arg (max);
6534 if (!processing_template_decl)
6535 max = decl_constant_value (max);
6537 if (processing_template_decl
6538 /* When providing explicit arguments to a template
6539 function, but leaving some arguments for subsequent
6540 deduction, MAX may be template-dependent even if we're
6541 not PROCESSING_TEMPLATE_DECL. We still need to check for
6542 template parms, though; MAX won't be an INTEGER_CST for
6543 dynamic arrays, either. */
6544 || (TREE_CODE (max) != INTEGER_CST
6545 && uses_template_parms (max)))
6547 tree itype = make_node (INTEGER_TYPE);
6548 TYPE_MIN_VALUE (itype) = size_zero_node;
6549 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6550 integer_one_node);
6551 return itype;
6554 if (integer_zerop (omax))
6556 /* Still allow an explicit array of size zero. */
6557 if (pedantic)
6558 pedwarn ("creating array with size zero");
6560 else if (integer_zerop (max)
6561 || (TREE_CODE (max) == INTEGER_CST
6562 && INT_CST_LT (max, integer_zero_node)))
6564 /* [temp.deduct]
6566 Type deduction may fail for any of the following
6567 reasons:
6569 Attempting to create an array with a size that is
6570 zero or negative. */
6571 if (complain & tf_error)
6572 error ("creating array with size zero (`%E')", max);
6574 return error_mark_node;
6577 return compute_array_index_type (NULL_TREE, max);
6580 case TEMPLATE_TYPE_PARM:
6581 case TEMPLATE_TEMPLATE_PARM:
6582 case BOUND_TEMPLATE_TEMPLATE_PARM:
6583 case TEMPLATE_PARM_INDEX:
6585 int idx;
6586 int level;
6587 int levels;
6589 r = NULL_TREE;
6591 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6592 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6593 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6595 idx = TEMPLATE_TYPE_IDX (t);
6596 level = TEMPLATE_TYPE_LEVEL (t);
6598 else
6600 idx = TEMPLATE_PARM_IDX (t);
6601 level = TEMPLATE_PARM_LEVEL (t);
6604 if (TREE_VEC_LENGTH (args) > 0)
6606 tree arg = NULL_TREE;
6608 levels = TMPL_ARGS_DEPTH (args);
6609 if (level <= levels)
6610 arg = TMPL_ARG (args, level, idx);
6612 if (arg == error_mark_node)
6613 return error_mark_node;
6614 else if (arg != NULL_TREE)
6616 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6618 my_friendly_assert (TYPE_P (arg), 0);
6619 return cp_build_qualified_type_real
6620 (arg, cp_type_quals (arg) | cp_type_quals (t),
6621 complain | tf_ignore_bad_quals);
6623 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6625 /* We are processing a type constructed from
6626 a template template parameter */
6627 tree argvec = tsubst (TYPE_TI_ARGS (t),
6628 args, complain, in_decl);
6629 if (argvec == error_mark_node)
6630 return error_mark_node;
6632 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6633 we are resolving nested-types in the signature of
6634 a member function templates.
6635 Otherwise ARG is a TEMPLATE_DECL and is the real
6636 template to be instantiated. */
6637 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6638 arg = TYPE_NAME (arg);
6640 r = lookup_template_class (arg,
6641 argvec, in_decl,
6642 DECL_CONTEXT (arg),
6643 /*entering_scope=*/0,
6644 complain);
6645 return cp_build_qualified_type_real
6646 (r, TYPE_QUALS (t), complain);
6648 else
6649 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6650 return arg;
6653 else
6654 abort ();
6656 if (level == 1)
6657 /* This can happen during the attempted tsubst'ing in
6658 unify. This means that we don't yet have any information
6659 about the template parameter in question. */
6660 return t;
6662 /* If we get here, we must have been looking at a parm for a
6663 more deeply nested template. Make a new version of this
6664 template parameter, but with a lower level. */
6665 switch (TREE_CODE (t))
6667 case TEMPLATE_TYPE_PARM:
6668 case TEMPLATE_TEMPLATE_PARM:
6669 case BOUND_TEMPLATE_TEMPLATE_PARM:
6670 if (cp_type_quals (t))
6672 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6673 r = cp_build_qualified_type_real
6674 (r, cp_type_quals (t),
6675 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6676 ? tf_ignore_bad_quals : 0));
6678 else
6680 r = copy_type (t);
6681 TEMPLATE_TYPE_PARM_INDEX (r)
6682 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6683 r, levels);
6684 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6685 TYPE_MAIN_VARIANT (r) = r;
6686 TYPE_POINTER_TO (r) = NULL_TREE;
6687 TYPE_REFERENCE_TO (r) = NULL_TREE;
6689 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6691 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6692 complain, in_decl);
6693 if (argvec == error_mark_node)
6694 return error_mark_node;
6696 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6697 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6700 break;
6702 case TEMPLATE_PARM_INDEX:
6703 r = reduce_template_parm_level (t, type, levels);
6704 break;
6706 default:
6707 abort ();
6710 return r;
6713 case TREE_LIST:
6715 tree purpose, value, chain, result;
6717 if (t == void_list_node)
6718 return t;
6720 purpose = TREE_PURPOSE (t);
6721 if (purpose)
6723 purpose = tsubst (purpose, args, complain, in_decl);
6724 if (purpose == error_mark_node)
6725 return error_mark_node;
6727 value = TREE_VALUE (t);
6728 if (value)
6730 value = tsubst (value, args, complain, in_decl);
6731 if (value == error_mark_node)
6732 return error_mark_node;
6734 chain = TREE_CHAIN (t);
6735 if (chain && chain != void_type_node)
6737 chain = tsubst (chain, args, complain, in_decl);
6738 if (chain == error_mark_node)
6739 return error_mark_node;
6741 if (purpose == TREE_PURPOSE (t)
6742 && value == TREE_VALUE (t)
6743 && chain == TREE_CHAIN (t))
6744 return t;
6745 if (TREE_PARMLIST (t))
6747 result = tree_cons (purpose, value, chain);
6748 TREE_PARMLIST (result) = 1;
6750 else
6751 result = hash_tree_cons (purpose, value, chain);
6752 return result;
6754 case TREE_VEC:
6755 if (type != NULL_TREE)
6757 /* A binfo node. We always need to make a copy, of the node
6758 itself and of its BINFO_BASETYPES. */
6760 t = copy_node (t);
6762 /* Make sure type isn't a typedef copy. */
6763 type = BINFO_TYPE (TYPE_BINFO (type));
6765 TREE_TYPE (t) = complete_type (type);
6766 if (IS_AGGR_TYPE (type))
6768 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6769 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6770 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6771 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6773 return t;
6776 /* Otherwise, a vector of template arguments. */
6777 return tsubst_template_arg_vector (t, args, complain);
6779 case POINTER_TYPE:
6780 case REFERENCE_TYPE:
6782 enum tree_code code;
6784 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6785 return t;
6787 code = TREE_CODE (t);
6790 /* [temp.deduct]
6792 Type deduction may fail for any of the following
6793 reasons:
6795 -- Attempting to create a pointer to reference type.
6796 -- Attempting to create a reference to a reference type or
6797 a reference to void. */
6798 if (TREE_CODE (type) == REFERENCE_TYPE
6799 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6801 static int last_line = 0;
6802 static const char* last_file = 0;
6804 /* We keep track of the last time we issued this error
6805 message to avoid spewing a ton of messages during a
6806 single bad template instantiation. */
6807 if (complain & tf_error
6808 && (last_line != lineno || last_file != input_filename))
6810 if (TREE_CODE (type) == VOID_TYPE)
6811 error ("forming reference to void");
6812 else
6813 error ("forming %s to reference type `%T'",
6814 (code == POINTER_TYPE) ? "pointer" : "reference",
6815 type);
6816 last_line = lineno;
6817 last_file = input_filename;
6820 return error_mark_node;
6822 else if (code == POINTER_TYPE)
6824 r = build_pointer_type (type);
6825 if (TREE_CODE (type) == METHOD_TYPE)
6826 r = build_ptrmemfunc_type (r);
6828 else
6829 r = build_reference_type (type);
6830 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6832 if (r != error_mark_node)
6833 /* Will this ever be needed for TYPE_..._TO values? */
6834 layout_type (r);
6836 return r;
6838 case OFFSET_TYPE:
6840 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6841 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6843 /* [temp.deduct]
6845 Type deduction may fail for any of the following
6846 reasons:
6848 -- Attempting to create "pointer to member of T" when T
6849 is not a class type. */
6850 if (complain & tf_error)
6851 error ("creating pointer to member of non-class type `%T'", r);
6852 return error_mark_node;
6854 if (TREE_CODE (type) == REFERENCE_TYPE)
6856 if (complain & tf_error)
6857 error ("creating pointer to member reference type `%T'", type);
6859 return error_mark_node;
6861 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6862 if (TREE_CODE (type) == FUNCTION_TYPE)
6863 /* This is really a method type. The cv qualifiers of the
6864 this pointer should _not_ be determined by the cv
6865 qualifiers of the class type. They should be held
6866 somewhere in the FUNCTION_TYPE, but we don't do that at
6867 the moment. Consider
6868 typedef void (Func) () const;
6870 template <typename T1> void Foo (Func T1::*);
6873 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6874 TREE_TYPE (type),
6875 TYPE_ARG_TYPES (type));
6876 else
6877 return build_offset_type (r, type);
6879 case FUNCTION_TYPE:
6880 case METHOD_TYPE:
6882 tree fntype;
6883 tree raises;
6885 fntype = tsubst_function_type (t, args, complain, in_decl);
6886 if (fntype == error_mark_node)
6887 return error_mark_node;
6889 /* Substitue the exception specification. */
6890 raises = TYPE_RAISES_EXCEPTIONS (t);
6891 if (raises)
6893 tree list = NULL_TREE;
6895 if (! TREE_VALUE (raises))
6896 list = raises;
6897 else
6898 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6900 tree spec = TREE_VALUE (raises);
6902 spec = tsubst (spec, args, complain, in_decl);
6903 if (spec == error_mark_node)
6904 return spec;
6905 list = add_exception_specifier (list, spec, complain);
6907 fntype = build_exception_variant (fntype, list);
6909 return fntype;
6911 case ARRAY_TYPE:
6913 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6914 if (domain == error_mark_node)
6915 return error_mark_node;
6917 /* As an optimization, we avoid regenerating the array type if
6918 it will obviously be the same as T. */
6919 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6920 return t;
6922 /* These checks should match the ones in grokdeclarator.
6924 [temp.deduct]
6926 The deduction may fail for any of the following reasons:
6928 -- Attempting to create an array with an element type that
6929 is void, a function type, or a reference type. */
6930 if (TREE_CODE (type) == VOID_TYPE
6931 || TREE_CODE (type) == FUNCTION_TYPE
6932 || TREE_CODE (type) == REFERENCE_TYPE)
6934 if (complain & tf_error)
6935 error ("creating array of `%T'", type);
6936 return error_mark_node;
6939 r = build_cplus_array_type (type, domain);
6940 return r;
6943 case PLUS_EXPR:
6944 case MINUS_EXPR:
6946 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6947 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6949 if (e1 == error_mark_node || e2 == error_mark_node)
6950 return error_mark_node;
6952 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6955 case NEGATE_EXPR:
6956 case NOP_EXPR:
6958 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6959 if (e == error_mark_node)
6960 return error_mark_node;
6962 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6965 case TYPENAME_TYPE:
6967 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6968 in_decl, /*entering_scope=*/1);
6969 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6970 complain, in_decl);
6972 if (ctx == error_mark_node || f == error_mark_node)
6973 return error_mark_node;
6975 if (!IS_AGGR_TYPE (ctx))
6977 if (complain & tf_error)
6978 error ("`%T' is not a class, struct, or union type",
6979 ctx);
6980 return error_mark_node;
6982 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6984 /* Normally, make_typename_type does not require that the CTX
6985 have complete type in order to allow things like:
6987 template <class T> struct S { typename S<T>::X Y; };
6989 But, such constructs have already been resolved by this
6990 point, so here CTX really should have complete type, unless
6991 it's a partial instantiation. */
6992 ctx = complete_type (ctx);
6993 if (!COMPLETE_TYPE_P (ctx))
6995 if (complain & tf_error)
6996 cxx_incomplete_type_error (NULL_TREE, ctx);
6997 return error_mark_node;
7001 f = make_typename_type (ctx, f,
7002 (complain & tf_error) | tf_keep_type_decl);
7003 if (f == error_mark_node)
7004 return f;
7005 if (TREE_CODE (f) == TYPE_DECL)
7007 complain |= tf_ignore_bad_quals;
7008 f = TREE_TYPE (f);
7011 return cp_build_qualified_type_real
7012 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7015 case UNBOUND_CLASS_TEMPLATE:
7017 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7018 in_decl, /*entering_scope=*/1);
7019 tree name = TYPE_IDENTIFIER (t);
7021 if (ctx == error_mark_node || name == error_mark_node)
7022 return error_mark_node;
7024 return make_unbound_class_template (ctx, name, complain);
7027 case INDIRECT_REF:
7029 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7030 if (e == error_mark_node)
7031 return error_mark_node;
7032 return make_pointer_declarator (type, e);
7035 case ADDR_EXPR:
7037 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7038 if (e == error_mark_node)
7039 return error_mark_node;
7040 return make_reference_declarator (type, e);
7043 case ARRAY_REF:
7045 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7046 tree e2 = tsubst_expr (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 (ARRAY_REF, e1, e2, tsubst_expr);
7053 case CALL_EXPR:
7055 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7056 tree e2 = (tsubst_call_declarator_parms
7057 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
7058 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
7059 complain, in_decl);
7061 if (e1 == error_mark_node || e2 == error_mark_node
7062 || e3 == error_mark_node)
7063 return error_mark_node;
7065 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
7068 case SCOPE_REF:
7070 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7071 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7072 if (e1 == error_mark_node || e2 == error_mark_node)
7073 return error_mark_node;
7075 return build_nt (TREE_CODE (t), e1, e2);
7078 case TYPEOF_TYPE:
7080 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
7081 if (e1 == error_mark_node)
7082 return error_mark_node;
7084 return TREE_TYPE (e1);
7087 default:
7088 sorry ("use of `%s' in template",
7089 tree_code_name [(int) TREE_CODE (t)]);
7090 return error_mark_node;
7094 /* Like tsubst, but deals with expressions. This function just replaces
7095 template parms; to finish processing the resultant expression, use
7096 tsubst_expr. */
7098 static tree
7099 tsubst_copy (t, args, complain, in_decl)
7100 tree t, args;
7101 tsubst_flags_t complain;
7102 tree in_decl;
7104 enum tree_code code;
7105 tree r;
7107 if (t == NULL_TREE || t == error_mark_node)
7108 return t;
7110 code = TREE_CODE (t);
7112 switch (code)
7114 case PARM_DECL:
7115 r = retrieve_local_specialization (t);
7116 my_friendly_assert (r != NULL, 20020903);
7117 return r;
7119 case CONST_DECL:
7121 tree enum_type;
7122 tree v;
7124 if (DECL_TEMPLATE_PARM_P (t))
7125 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7126 if (!DECL_CONTEXT (t))
7127 /* This is a global enumeration constant. */
7128 return t;
7130 /* Unfortunately, we cannot just call lookup_name here.
7131 Consider:
7133 template <int I> int f() {
7134 enum E { a = I };
7135 struct S { void g() { E e = a; } };
7138 When we instantiate f<7>::S::g(), say, lookup_name is not
7139 clever enough to find f<7>::a. */
7140 enum_type
7141 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7142 /*entering_scope=*/0);
7144 for (v = TYPE_VALUES (enum_type);
7145 v != NULL_TREE;
7146 v = TREE_CHAIN (v))
7147 if (TREE_PURPOSE (v) == DECL_NAME (t))
7148 return TREE_VALUE (v);
7150 /* We didn't find the name. That should never happen; if
7151 name-lookup found it during preliminary parsing, we
7152 should find it again here during instantiation. */
7153 abort ();
7155 return t;
7157 case FIELD_DECL:
7158 if (DECL_CONTEXT (t))
7160 tree ctx;
7162 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7163 /*entering_scope=*/1);
7164 if (ctx != DECL_CONTEXT (t))
7165 return lookup_field (ctx, DECL_NAME (t), 0, false);
7167 return t;
7169 case VAR_DECL:
7170 case FUNCTION_DECL:
7171 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7172 || local_variable_p (t))
7173 t = tsubst (t, args, complain, in_decl);
7174 mark_used (t);
7175 return t;
7177 case BASELINK:
7179 tree name;
7180 tree qualifying_scope;
7181 tree fns;
7182 tree template_args = 0;
7183 bool template_id_p = false;
7185 /* A baselink indicates a function from a base class. The
7186 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7187 non-dependent types; otherwise, the lookup could not have
7188 succeeded. However, they may indicate bases of the template
7189 class, rather than the instantiated class.
7191 In addition, lookups that were not ambiguous before may be
7192 ambiguous now. Therefore, we perform the lookup again. */
7193 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t));
7194 fns = BASELINK_FUNCTIONS (t);
7195 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7197 template_id_p = true;
7198 template_args = TREE_OPERAND (fns, 1);
7199 fns = TREE_OPERAND (fns, 0);
7201 name = DECL_NAME (get_first_fn (fns));
7202 t = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7203 if (BASELINK_P (t) && template_id_p)
7204 BASELINK_FUNCTIONS (t)
7205 = build_nt (TEMPLATE_ID_EXPR,
7206 BASELINK_FUNCTIONS (t),
7207 template_args);
7208 return adjust_result_of_qualified_name_lookup (t,
7209 qualifying_scope,
7210 current_class_type);
7213 case TEMPLATE_DECL:
7214 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7215 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7216 args, complain, in_decl);
7217 else if (is_member_template (t))
7218 return tsubst (t, args, complain, in_decl);
7219 else
7220 return t;
7222 case LOOKUP_EXPR:
7224 /* We must tsubst into a LOOKUP_EXPR in case the names to
7225 which it refers is a conversion operator; in that case the
7226 name will change. We avoid making unnecessary copies,
7227 however. */
7229 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7231 if (id != TREE_OPERAND (t, 0))
7233 r = build_nt (LOOKUP_EXPR, id);
7234 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7235 t = r;
7238 return t;
7241 case CAST_EXPR:
7242 case REINTERPRET_CAST_EXPR:
7243 case CONST_CAST_EXPR:
7244 case STATIC_CAST_EXPR:
7245 case DYNAMIC_CAST_EXPR:
7246 case NOP_EXPR:
7247 return build1
7248 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7249 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7251 case INDIRECT_REF:
7252 case NEGATE_EXPR:
7253 case TRUTH_NOT_EXPR:
7254 case BIT_NOT_EXPR:
7255 case ADDR_EXPR:
7256 case CONVERT_EXPR: /* Unary + */
7257 case SIZEOF_EXPR:
7258 case ALIGNOF_EXPR:
7259 case ARROW_EXPR:
7260 case THROW_EXPR:
7261 case TYPEID_EXPR:
7262 case REALPART_EXPR:
7263 case IMAGPART_EXPR:
7264 return build1
7265 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7266 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7268 case COMPONENT_REF:
7270 tree object;
7271 tree name;
7273 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7274 name = TREE_OPERAND (t, 1);
7275 if (TREE_CODE (name) == BIT_NOT_EXPR)
7277 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7278 complain, in_decl);
7279 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7281 else if (TREE_CODE (name) == SCOPE_REF
7282 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7284 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7285 complain, in_decl);
7286 name = TREE_OPERAND (name, 1);
7287 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7288 complain, in_decl);
7289 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7290 name = build_nt (SCOPE_REF, base, name);
7292 else
7293 name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7294 return build_nt (COMPONENT_REF, object, name);
7297 case PLUS_EXPR:
7298 case MINUS_EXPR:
7299 case MULT_EXPR:
7300 case TRUNC_DIV_EXPR:
7301 case CEIL_DIV_EXPR:
7302 case FLOOR_DIV_EXPR:
7303 case ROUND_DIV_EXPR:
7304 case EXACT_DIV_EXPR:
7305 case BIT_AND_EXPR:
7306 case BIT_ANDTC_EXPR:
7307 case BIT_IOR_EXPR:
7308 case BIT_XOR_EXPR:
7309 case TRUNC_MOD_EXPR:
7310 case FLOOR_MOD_EXPR:
7311 case TRUTH_ANDIF_EXPR:
7312 case TRUTH_ORIF_EXPR:
7313 case TRUTH_AND_EXPR:
7314 case TRUTH_OR_EXPR:
7315 case RSHIFT_EXPR:
7316 case LSHIFT_EXPR:
7317 case RROTATE_EXPR:
7318 case LROTATE_EXPR:
7319 case EQ_EXPR:
7320 case NE_EXPR:
7321 case MAX_EXPR:
7322 case MIN_EXPR:
7323 case LE_EXPR:
7324 case GE_EXPR:
7325 case LT_EXPR:
7326 case GT_EXPR:
7327 case ARRAY_REF:
7328 case COMPOUND_EXPR:
7329 case SCOPE_REF:
7330 case DOTSTAR_EXPR:
7331 case MEMBER_REF:
7332 case PREDECREMENT_EXPR:
7333 case PREINCREMENT_EXPR:
7334 case POSTDECREMENT_EXPR:
7335 case POSTINCREMENT_EXPR:
7336 return build_nt
7337 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7338 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7340 case CALL_EXPR:
7341 return build_nt (code,
7342 tsubst_copy (TREE_OPERAND (t, 0), args,
7343 complain, in_decl),
7344 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7345 in_decl),
7346 NULL_TREE);
7348 case METHOD_CALL_EXPR:
7349 return build_nt
7350 (code,
7351 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7352 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7353 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7354 NULL_TREE);
7356 case STMT_EXPR:
7357 /* This processing should really occur in tsubst_expr. However,
7358 tsubst_expr does not recurse into expressions, since it
7359 assumes that there aren't any statements inside them. So, we
7360 need to expand the STMT_EXPR here. */
7361 if (!processing_template_decl)
7363 tree stmt_expr = begin_stmt_expr ();
7364 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7365 return finish_stmt_expr (stmt_expr);
7368 return t;
7370 case COND_EXPR:
7371 case MODOP_EXPR:
7372 case PSEUDO_DTOR_EXPR:
7374 r = build_nt
7375 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7376 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7377 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7378 return r;
7381 case NEW_EXPR:
7383 r = build_nt
7384 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7385 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7386 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7387 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7388 return r;
7391 case DELETE_EXPR:
7393 r = build_nt
7394 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7395 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7396 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7397 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7398 return r;
7401 case TEMPLATE_ID_EXPR:
7403 /* Substituted template arguments */
7404 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7405 in_decl);
7407 if (targs && TREE_CODE (targs) == TREE_LIST)
7409 tree chain;
7410 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7411 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7413 else if (targs)
7415 int i;
7416 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7417 TREE_VEC_ELT (targs, i)
7418 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7421 return lookup_template_function
7422 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7425 case TREE_LIST:
7427 tree purpose, value, chain;
7429 if (t == void_list_node)
7430 return t;
7432 purpose = TREE_PURPOSE (t);
7433 if (purpose)
7434 purpose = tsubst_copy (purpose, args, complain, in_decl);
7435 value = TREE_VALUE (t);
7436 if (value)
7437 value = tsubst_copy (value, args, complain, in_decl);
7438 chain = TREE_CHAIN (t);
7439 if (chain && chain != void_type_node)
7440 chain = tsubst_copy (chain, args, complain, in_decl);
7441 if (purpose == TREE_PURPOSE (t)
7442 && value == TREE_VALUE (t)
7443 && chain == TREE_CHAIN (t))
7444 return t;
7445 return tree_cons (purpose, value, chain);
7448 case RECORD_TYPE:
7449 case UNION_TYPE:
7450 case ENUMERAL_TYPE:
7451 case INTEGER_TYPE:
7452 case TEMPLATE_TYPE_PARM:
7453 case TEMPLATE_TEMPLATE_PARM:
7454 case BOUND_TEMPLATE_TEMPLATE_PARM:
7455 case TEMPLATE_PARM_INDEX:
7456 case POINTER_TYPE:
7457 case REFERENCE_TYPE:
7458 case OFFSET_TYPE:
7459 case FUNCTION_TYPE:
7460 case METHOD_TYPE:
7461 case ARRAY_TYPE:
7462 case TYPENAME_TYPE:
7463 case UNBOUND_CLASS_TEMPLATE:
7464 case TYPEOF_TYPE:
7465 case TYPE_DECL:
7466 return tsubst (t, args, complain, in_decl);
7468 case IDENTIFIER_NODE:
7469 if (IDENTIFIER_TYPENAME_P (t))
7471 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7472 return mangle_conv_op_name_for_type (new_type);
7474 else
7475 return t;
7477 case CONSTRUCTOR:
7479 r = build
7480 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7481 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7482 complain, in_decl));
7483 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7484 return r;
7487 case VA_ARG_EXPR:
7488 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7489 in_decl),
7490 tsubst (TREE_TYPE (t), args, complain, in_decl));
7492 default:
7493 return t;
7497 /* Like tsubst_copy for expressions, etc. but also does semantic
7498 processing. */
7500 static tree
7501 tsubst_expr (t, args, complain, in_decl)
7502 tree t, args;
7503 tsubst_flags_t complain;
7504 tree in_decl;
7506 tree stmt, tmp;
7508 if (t == NULL_TREE || t == error_mark_node)
7509 return t;
7511 if (processing_template_decl)
7512 return tsubst_copy (t, args, complain, in_decl);
7514 if (!statement_code_p (TREE_CODE (t)))
7515 return tsubst_copy_and_build (t, args, complain, in_decl);
7517 switch (TREE_CODE (t))
7519 case CTOR_INITIALIZER:
7520 prep_stmt (t);
7521 finish_mem_initializers (tsubst_initializer_list
7522 (TREE_OPERAND (t, 0), args));
7523 break;
7525 case RETURN_STMT:
7526 prep_stmt (t);
7527 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
7528 args, complain, in_decl));
7529 break;
7531 case EXPR_STMT:
7532 prep_stmt (t);
7533 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7534 args, complain, in_decl));
7535 break;
7537 case USING_STMT:
7538 prep_stmt (t);
7539 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7540 args, complain, in_decl));
7541 break;
7543 case DECL_STMT:
7545 tree decl;
7546 tree init;
7548 prep_stmt (t);
7549 decl = DECL_STMT_DECL (t);
7550 if (TREE_CODE (decl) == LABEL_DECL)
7551 finish_label_decl (DECL_NAME (decl));
7552 else if (TREE_CODE (decl) == USING_DECL)
7554 tree scope = DECL_INITIAL (decl);
7555 tree name = DECL_NAME (decl);
7557 scope = tsubst_expr (scope, args, complain, in_decl);
7558 do_local_using_decl (lookup_qualified_name (scope,
7559 name,
7560 /*is_type_p=*/0,
7561 /*flags=*/0));
7563 else
7565 init = DECL_INITIAL (decl);
7566 decl = tsubst (decl, args, complain, in_decl);
7567 if (decl != error_mark_node)
7569 if (init)
7570 DECL_INITIAL (decl) = error_mark_node;
7571 /* By marking the declaration as instantiated, we avoid
7572 trying to instantiate it. Since instantiate_decl can't
7573 handle local variables, and since we've already done
7574 all that needs to be done, that's the right thing to
7575 do. */
7576 if (TREE_CODE (decl) == VAR_DECL)
7577 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7578 if (TREE_CODE (decl) == VAR_DECL
7579 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7580 /* Anonymous aggregates are a special case. */
7581 finish_anon_union (decl);
7582 else
7584 maybe_push_decl (decl);
7585 if (DECL_PRETTY_FUNCTION_P (decl))
7587 /* For __PRETTY_FUNCTION__ we have to adjust the
7588 initializer. */
7589 const char *const name
7590 = cxx_printable_name (current_function_decl, 2);
7591 init = cp_fname_init (name);
7592 TREE_TYPE (decl) = TREE_TYPE (init);
7594 else
7595 init = tsubst_expr (init, args, complain, in_decl);
7596 cp_finish_decl (decl, init, NULL_TREE, 0);
7601 /* A DECL_STMT can also be used as an expression, in the condition
7602 clause of an if/for/while construct. If we aren't followed by
7603 another statement, return our decl. */
7604 if (TREE_CHAIN (t) == NULL_TREE)
7605 return decl;
7607 break;
7609 case FOR_STMT:
7611 prep_stmt (t);
7613 stmt = begin_for_stmt ();
7614 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7615 finish_for_init_stmt (stmt);
7616 finish_for_cond (tsubst_expr (FOR_COND (t),
7617 args, complain, in_decl),
7618 stmt);
7619 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7620 finish_for_expr (tmp, stmt);
7621 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7622 finish_for_stmt (stmt);
7624 break;
7626 case WHILE_STMT:
7628 prep_stmt (t);
7629 stmt = begin_while_stmt ();
7630 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7631 args, complain, in_decl),
7632 stmt);
7633 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7634 finish_while_stmt (stmt);
7636 break;
7638 case DO_STMT:
7640 prep_stmt (t);
7641 stmt = begin_do_stmt ();
7642 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7643 finish_do_body (stmt);
7644 finish_do_stmt (tsubst_expr (DO_COND (t),
7645 args, complain, in_decl),
7646 stmt);
7648 break;
7650 case IF_STMT:
7652 prep_stmt (t);
7653 stmt = begin_if_stmt ();
7654 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7655 args, complain, in_decl),
7656 stmt);
7658 if (tmp = THEN_CLAUSE (t), tmp)
7660 tsubst_expr (tmp, args, complain, in_decl);
7661 finish_then_clause (stmt);
7664 if (tmp = ELSE_CLAUSE (t), tmp)
7666 begin_else_clause ();
7667 tsubst_expr (tmp, args, complain, in_decl);
7668 finish_else_clause (stmt);
7671 finish_if_stmt ();
7673 break;
7675 case COMPOUND_STMT:
7677 prep_stmt (t);
7678 if (COMPOUND_STMT_BODY_BLOCK (t))
7679 stmt = begin_function_body ();
7680 else
7681 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7683 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7685 if (COMPOUND_STMT_BODY_BLOCK (t))
7686 finish_function_body (stmt);
7687 else
7688 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7690 break;
7692 case BREAK_STMT:
7693 prep_stmt (t);
7694 finish_break_stmt ();
7695 break;
7697 case CONTINUE_STMT:
7698 prep_stmt (t);
7699 finish_continue_stmt ();
7700 break;
7702 case SWITCH_STMT:
7704 tree val;
7706 prep_stmt (t);
7707 stmt = begin_switch_stmt ();
7708 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7709 finish_switch_cond (val, stmt);
7710 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7711 finish_switch_stmt (stmt);
7713 break;
7715 case CASE_LABEL:
7716 prep_stmt (t);
7717 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7718 tsubst_expr (CASE_HIGH (t), args, complain,
7719 in_decl));
7720 break;
7722 case LABEL_STMT:
7723 lineno = STMT_LINENO (t);
7724 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7725 break;
7727 case FILE_STMT:
7728 input_filename = FILE_STMT_FILENAME (t);
7729 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7730 break;
7732 case GOTO_STMT:
7733 prep_stmt (t);
7734 tmp = GOTO_DESTINATION (t);
7735 if (TREE_CODE (tmp) != LABEL_DECL)
7736 /* Computed goto's must be tsubst'd into. On the other hand,
7737 non-computed gotos must not be; the identifier in question
7738 will have no binding. */
7739 tmp = tsubst_expr (tmp, args, complain, in_decl);
7740 else
7741 tmp = DECL_NAME (tmp);
7742 finish_goto_stmt (tmp);
7743 break;
7745 case ASM_STMT:
7746 prep_stmt (t);
7747 tmp = finish_asm_stmt
7748 (ASM_CV_QUAL (t),
7749 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7750 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7751 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7752 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7753 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
7754 break;
7756 case TRY_BLOCK:
7757 prep_stmt (t);
7758 if (CLEANUP_P (t))
7760 stmt = begin_try_block ();
7761 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7762 finish_cleanup_try_block (stmt);
7763 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7764 complain, in_decl),
7765 stmt);
7767 else
7769 if (FN_TRY_BLOCK_P (t))
7770 stmt = begin_function_try_block ();
7771 else
7772 stmt = begin_try_block ();
7774 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7776 if (FN_TRY_BLOCK_P (t))
7777 finish_function_try_block (stmt);
7778 else
7779 finish_try_block (stmt);
7781 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7782 if (FN_TRY_BLOCK_P (t))
7783 finish_function_handler_sequence (stmt);
7784 else
7785 finish_handler_sequence (stmt);
7787 break;
7789 case HANDLER:
7791 tree decl;
7793 prep_stmt (t);
7794 stmt = begin_handler ();
7795 if (HANDLER_PARMS (t))
7797 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7798 decl = tsubst (decl, args, complain, in_decl);
7799 /* Prevent instantiate_decl from trying to instantiate
7800 this variable. We've already done all that needs to be
7801 done. */
7802 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7804 else
7805 decl = NULL_TREE;
7806 finish_handler_parms (decl, stmt);
7807 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7808 finish_handler (stmt);
7810 break;
7812 case TAG_DEFN:
7813 prep_stmt (t);
7814 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7815 break;
7817 default:
7818 abort ();
7821 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7824 /* Like tsubst but deals with expressions and performs semantic
7825 analysis. */
7827 static tree
7828 tsubst_copy_and_build (t, args, complain, in_decl)
7829 tree t, args;
7830 tsubst_flags_t complain;
7831 tree in_decl;
7833 if (t == NULL_TREE || t == error_mark_node)
7834 return t;
7836 switch (TREE_CODE (t))
7838 case IDENTIFIER_NODE:
7839 if (IDENTIFIER_TYPENAME_P (t))
7841 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7842 return do_identifier (mangle_conv_op_name_for_type (new_type),
7843 NULL_TREE);
7845 else
7846 return do_identifier (t, NULL_TREE);
7848 case LOOKUP_EXPR:
7850 if (LOOKUP_EXPR_GLOBAL (t))
7852 tree token
7853 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7854 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
7856 else
7858 t = do_identifier
7859 (tsubst_copy
7860 (TREE_OPERAND (t, 0), args, complain, in_decl),
7861 NULL_TREE);
7862 if (TREE_CODE (t) == ALIAS_DECL)
7863 t = DECL_INITIAL (t);
7864 return t;
7868 case TEMPLATE_ID_EXPR:
7870 tree object;
7871 tree template
7872 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7874 if (TREE_CODE (template) == COMPONENT_REF)
7876 object = TREE_OPERAND (template, 0);
7877 template = TREE_OPERAND (template, 1);
7879 else
7880 object = NULL_TREE;
7882 template = lookup_template_function
7883 (template,
7884 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7886 if (object)
7887 return build (COMPONENT_REF, TREE_TYPE (template),
7888 object, template);
7889 else
7890 return template;
7893 case INDIRECT_REF:
7894 return build_x_indirect_ref
7895 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7896 "unary *");
7898 case CAST_EXPR:
7899 return build_functional_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 REINTERPRET_CAST_EXPR:
7904 return build_reinterpret_cast
7905 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7906 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7908 case CONST_CAST_EXPR:
7909 return build_const_cast
7910 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7911 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7913 case DYNAMIC_CAST_EXPR:
7914 return build_dynamic_cast
7915 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7916 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7918 case STATIC_CAST_EXPR:
7919 return build_static_cast
7920 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7921 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7923 case PREDECREMENT_EXPR:
7924 case PREINCREMENT_EXPR:
7925 case POSTDECREMENT_EXPR:
7926 case POSTINCREMENT_EXPR:
7927 if (TREE_TYPE (t))
7928 return tsubst_copy (t, args, complain, in_decl);
7929 else
7930 return build_x_unary_op
7931 (TREE_CODE (t),
7932 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7933 in_decl));
7935 case NEGATE_EXPR:
7936 case BIT_NOT_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 ABS_EXPR:
7946 if (TREE_TYPE (t))
7947 return t;
7948 return build_x_unary_op
7949 (TREE_CODE (t),
7950 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7952 case TRUTH_NOT_EXPR:
7953 case ADDR_EXPR:
7954 case CONVERT_EXPR: /* Unary + */
7955 case REALPART_EXPR:
7956 case IMAGPART_EXPR:
7957 if (TREE_TYPE (t))
7958 return tsubst_copy (t, args, complain, in_decl);
7959 else
7960 return build_x_unary_op
7961 (TREE_CODE (t),
7962 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7963 in_decl));
7965 case PLUS_EXPR:
7966 case MINUS_EXPR:
7967 case MULT_EXPR:
7968 case TRUNC_DIV_EXPR:
7969 case CEIL_DIV_EXPR:
7970 case FLOOR_DIV_EXPR:
7971 case ROUND_DIV_EXPR:
7972 case EXACT_DIV_EXPR:
7973 case BIT_AND_EXPR:
7974 case BIT_ANDTC_EXPR:
7975 case BIT_IOR_EXPR:
7976 case BIT_XOR_EXPR:
7977 case TRUNC_MOD_EXPR:
7978 case FLOOR_MOD_EXPR:
7979 case TRUTH_ANDIF_EXPR:
7980 case TRUTH_ORIF_EXPR:
7981 case TRUTH_AND_EXPR:
7982 case TRUTH_OR_EXPR:
7983 case RSHIFT_EXPR:
7984 case LSHIFT_EXPR:
7985 case RROTATE_EXPR:
7986 case LROTATE_EXPR:
7987 case EQ_EXPR:
7988 case NE_EXPR:
7989 case MAX_EXPR:
7990 case MIN_EXPR:
7991 case LE_EXPR:
7992 case GE_EXPR:
7993 case LT_EXPR:
7994 case GT_EXPR:
7995 case MEMBER_REF:
7996 return build_x_binary_op
7997 (TREE_CODE (t),
7998 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7999 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8001 case DOTSTAR_EXPR:
8002 return build_m_component_ref
8003 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8004 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8006 case SCOPE_REF:
8007 return build_offset_ref
8008 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8009 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8011 case ARRAY_REF:
8013 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8014 == NULL_TREE)
8015 /* new-type-id */
8016 return build_nt
8017 (ARRAY_REF, NULL_TREE,
8018 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8019 in_decl));
8021 return grok_array_decl
8022 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8023 in_decl),
8024 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8025 in_decl));
8028 case SIZEOF_EXPR:
8029 case ALIGNOF_EXPR:
8031 tree r =
8032 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8033 if (!TYPE_P (r))
8034 return TREE_CODE (t) == SIZEOF_EXPR ?
8035 expr_sizeof (r) : c_alignof_expr (r);
8036 else
8037 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
8040 case MODOP_EXPR:
8041 return build_x_modify_expr
8042 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8043 TREE_CODE (TREE_OPERAND (t, 1)),
8044 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8046 case ARROW_EXPR:
8047 return build_x_arrow
8048 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8050 case NEW_EXPR:
8051 return build_new
8052 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8053 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8054 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl),
8055 NEW_EXPR_USE_GLOBAL (t));
8057 case DELETE_EXPR:
8058 return delete_sanity
8059 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8060 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8061 DELETE_EXPR_USE_VEC (t),
8062 DELETE_EXPR_USE_GLOBAL (t));
8064 case COMPOUND_EXPR:
8066 if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)
8067 == NULL_TREE)
8068 return build_x_compound_expr
8069 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8070 in_decl));
8071 else
8072 abort ();
8075 case METHOD_CALL_EXPR:
8077 tree method
8078 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8080 if (TREE_CODE (method) == SCOPE_REF)
8082 tree name = TREE_OPERAND (method, 1);
8084 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8085 name = build_nt (TEMPLATE_ID_EXPR,
8086 TREE_OPERAND (name, 0),
8087 TREE_OPERAND (name, 1));
8089 return build_scoped_method_call
8090 (tsubst_copy_and_build
8091 (TREE_OPERAND (t, 1), args, complain, in_decl),
8092 TREE_OPERAND (method, 0),
8093 name,
8094 tsubst_copy_and_build
8095 (TREE_OPERAND (t, 2), args, complain, in_decl));
8097 else
8099 /* We can get a TEMPLATE_ID_EXPR here on code like:
8101 x->f<2>();
8103 so we must resolve that. However, we can also get things
8104 like a BIT_NOT_EXPR here, when referring to a destructor,
8105 and things like that are not correctly resolved by this
8106 function so just use it when we really need it. */
8107 if (TREE_CODE (method) == TEMPLATE_ID_EXPR)
8108 method = lookup_template_function
8109 (TREE_OPERAND (method, 0),
8110 TREE_OPERAND (method, 1));
8112 return build_method_call
8113 (tsubst_copy_and_build
8114 (TREE_OPERAND (t, 1), args, complain, in_decl),
8115 method,
8116 tsubst_copy_and_build
8117 (TREE_OPERAND (t, 2), args, complain, in_decl),
8118 NULL_TREE, LOOKUP_NORMAL);
8122 case CALL_EXPR:
8124 tree function, copy_args;
8126 function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8127 copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
8128 complain, in_decl);
8130 if (BASELINK_P (function))
8131 return build_call_from_tree (function, copy_args, 1);
8132 else if (TREE_CODE (function) == SCOPE_REF)
8134 tree name = TREE_OPERAND (function, 1);
8135 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8136 name = build_nt (TEMPLATE_ID_EXPR,
8137 TREE_OPERAND (name, 0),
8138 TREE_OPERAND (name, 1));
8140 function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
8141 name);
8143 return build_call_from_tree (function, copy_args, 1);
8145 else
8147 tree name = function;
8148 tree id;
8150 if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
8151 && !LOOKUP_EXPR_GLOBAL (name)
8152 && (TREE_CODE ((id = TREE_OPERAND (name, 0)))
8153 == IDENTIFIER_NODE)
8154 && (!current_class_type
8155 || !lookup_member (current_class_type, id, 0, false)))
8157 /* Do Koenig lookup if there are no class members. */
8158 name = do_identifier (id, copy_args);
8160 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
8161 || ! really_overloaded_fn (name))
8162 name = build_expr_from_tree (name);
8164 if (TREE_CODE (name) == OFFSET_REF)
8165 return build_offset_ref_call_from_tree (name, copy_args);
8166 if (TREE_CODE (name) == COMPONENT_REF)
8167 return finish_object_call_expr (TREE_OPERAND (name, 1),
8168 TREE_OPERAND (name, 0),
8169 copy_args);
8170 name = convert_from_reference (name);
8171 return build_call_from_tree (name, copy_args,
8172 /*disallow_virtual=*/false);
8176 case COND_EXPR:
8177 return build_x_conditional_expr
8178 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8179 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8180 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8182 case PSEUDO_DTOR_EXPR:
8183 return finish_pseudo_destructor_expr
8184 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8185 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8186 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8188 case TREE_LIST:
8190 tree purpose, value, chain;
8192 if (t == void_list_node)
8193 return t;
8195 purpose = TREE_PURPOSE (t);
8196 if (purpose)
8197 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
8198 value = TREE_VALUE (t);
8199 if (value)
8200 value = tsubst_copy_and_build (value, args, complain, in_decl);
8201 chain = TREE_CHAIN (t);
8202 if (chain && chain != void_type_node)
8203 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
8204 if (purpose == TREE_PURPOSE (t)
8205 && value == TREE_VALUE (t)
8206 && chain == TREE_CHAIN (t))
8207 return t;
8208 return tree_cons (purpose, value, chain);
8211 case COMPONENT_REF:
8213 tree object =
8214 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8215 tree member =
8216 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
8218 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8220 if (TREE_CODE (member) == BIT_NOT_EXPR)
8221 return finish_pseudo_destructor_expr (object,
8222 NULL_TREE,
8223 TREE_TYPE (object));
8224 else if (TREE_CODE (member) == SCOPE_REF
8225 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8226 return finish_pseudo_destructor_expr (object,
8227 object,
8228 TREE_TYPE (object));
8230 else if (TREE_CODE (member) == SCOPE_REF
8231 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8233 tree tmpl;
8234 tree args;
8236 /* Lookup the template functions now that we know what the
8237 scope is. */
8238 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8239 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8240 member = lookup_qualified_name (TREE_OPERAND (member, 0),
8241 tmpl,
8242 /*is_type=*/0,
8243 /*flags=*/0);
8244 if (BASELINK_P (member))
8245 BASELINK_FUNCTIONS (member)
8246 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8247 args);
8248 else
8250 error ("`%D' is not a member of `%T'",
8251 tmpl, TREE_TYPE (object));
8252 return error_mark_node;
8256 return finish_class_member_access_expr (object, member);
8259 case THROW_EXPR:
8260 return build_throw
8261 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8263 case CONSTRUCTOR:
8265 tree r;
8266 tree elts;
8267 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8268 bool purpose_p;
8270 /* digest_init will do the wrong thing if we let it. */
8271 if (type && TYPE_PTRMEMFUNC_P (type))
8272 return t;
8274 r = NULL_TREE;
8275 /* We do not want to process the purpose of aggregate
8276 initializers as they are identifier nodes which will be
8277 looked up by digest_init. */
8278 purpose_p = !(type && IS_AGGR_TYPE (type));
8279 for (elts = CONSTRUCTOR_ELTS (t);
8280 elts;
8281 elts = TREE_CHAIN (elts))
8283 tree purpose = TREE_PURPOSE (elts);
8284 tree value = TREE_VALUE (elts);
8286 if (purpose && purpose_p)
8287 purpose
8288 = tsubst_copy_and_build (purpose, args, complain, in_decl);
8289 value = tsubst_copy_and_build (value, args, complain, in_decl);
8290 r = tree_cons (purpose, value, r);
8293 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
8294 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8296 if (type)
8297 return digest_init (type, r, 0);
8298 return r;
8301 case TYPEID_EXPR:
8303 tree operand_0
8304 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8305 in_decl);
8307 if (TYPE_P (operand_0))
8308 return get_typeid (operand_0);
8309 return build_typeid (operand_0);
8312 case PARM_DECL:
8313 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8315 case VAR_DECL:
8316 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8318 case VA_ARG_EXPR:
8319 return build_x_va_arg
8320 (tsubst_copy_and_build
8321 (TREE_OPERAND (t, 0), args, complain, in_decl),
8322 tsubst_copy (TREE_TYPE (t), args, complain, in_decl));
8324 default:
8325 return tsubst_copy (t, args, complain, in_decl);
8329 /* Instantiate the indicated variable or function template TMPL with
8330 the template arguments in TARG_PTR. */
8332 tree
8333 instantiate_template (tmpl, targ_ptr)
8334 tree tmpl, targ_ptr;
8336 tree fndecl;
8337 tree gen_tmpl;
8338 tree spec;
8339 int i, len;
8340 tree inner_args;
8342 if (tmpl == error_mark_node)
8343 return error_mark_node;
8345 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
8347 /* If this function is a clone, handle it specially. */
8348 if (DECL_CLONED_FUNCTION_P (tmpl))
8350 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
8351 tree clone;
8353 /* Look for the clone. */
8354 for (clone = TREE_CHAIN (spec);
8355 clone && DECL_CLONED_FUNCTION_P (clone);
8356 clone = TREE_CHAIN (clone))
8357 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8358 return clone;
8359 /* We should always have found the clone by now. */
8360 abort ();
8361 return NULL_TREE;
8364 /* Check to see if we already have this specialization. */
8365 spec = retrieve_specialization (tmpl, targ_ptr);
8366 if (spec != NULL_TREE)
8367 return spec;
8369 gen_tmpl = most_general_template (tmpl);
8370 if (tmpl != gen_tmpl)
8372 /* The TMPL is a partial instantiation. To get a full set of
8373 arguments we must add the arguments used to perform the
8374 partial instantiation. */
8375 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8376 targ_ptr);
8378 /* Check to see if we already have this specialization. */
8379 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8380 if (spec != NULL_TREE)
8381 return spec;
8384 len = DECL_NTPARMS (gen_tmpl);
8385 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
8386 i = len;
8387 while (i--)
8389 tree t = TREE_VEC_ELT (inner_args, i);
8390 if (TYPE_P (t))
8392 tree nt = target_type (t);
8393 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
8395 error ("type `%T' composed from a local class is not a valid template-argument", t);
8396 error (" trying to instantiate `%D'", gen_tmpl);
8397 return error_mark_node;
8402 /* Make sure that we can see identifiers, and compute access
8403 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8404 created earlier. Let push_access_scope_real figure that out. */
8405 push_access_scope_real
8406 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
8407 tf_error, gen_tmpl));
8409 /* substitute template parameters */
8410 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8411 targ_ptr, tf_error, gen_tmpl);
8413 pop_access_scope (gen_tmpl);
8415 /* The DECL_TI_TEMPLATE should always be the immediate parent
8416 template, not the most general template. */
8417 DECL_TI_TEMPLATE (fndecl) = tmpl;
8419 if (flag_external_templates)
8420 add_pending_template (fndecl);
8422 /* If we've just instantiated the main entry point for a function,
8423 instantiate all the alternate entry points as well. We do this
8424 by cloning the instantiation of the main entry point, not by
8425 instantiating the template clones. */
8426 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8427 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8429 return fndecl;
8432 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8433 arguments that are being used when calling it. TARGS is a vector
8434 into which the deduced template arguments are placed.
8436 Return zero for success, 2 for an incomplete match that doesn't resolve
8437 all the types, and 1 for complete failure. An error message will be
8438 printed only for an incomplete match.
8440 If FN is a conversion operator, or we are trying to produce a specific
8441 specialization, RETURN_TYPE is the return type desired.
8443 The EXPLICIT_TARGS are explicit template arguments provided via a
8444 template-id.
8446 The parameter STRICT is one of:
8448 DEDUCE_CALL:
8449 We are deducing arguments for a function call, as in
8450 [temp.deduct.call].
8452 DEDUCE_CONV:
8453 We are deducing arguments for a conversion function, as in
8454 [temp.deduct.conv].
8456 DEDUCE_EXACT:
8457 We are deducing arguments when doing an explicit instantiation
8458 as in [temp.explicit], when determining an explicit specialization
8459 as in [temp.expl.spec], or when taking the address of a function
8460 template, as in [temp.deduct.funcaddr].
8462 DEDUCE_ORDER:
8463 We are deducing arguments when calculating the partial
8464 ordering between specializations of function or class
8465 templates, as in [temp.func.order] and [temp.class.order].
8467 LEN is the number of parms to consider before returning success, or -1
8468 for all. This is used in partial ordering to avoid comparing parms for
8469 which no actual argument was passed, since they are not considered in
8470 overload resolution (and are explicitly excluded from consideration in
8471 partial ordering in [temp.func.order]/6). */
8474 fn_type_unification (fn, explicit_targs, targs, args, return_type,
8475 strict, len)
8476 tree fn, explicit_targs, targs, args, return_type;
8477 unification_kind_t strict;
8478 int len;
8480 tree parms;
8481 tree fntype;
8482 int result;
8484 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
8486 fntype = TREE_TYPE (fn);
8487 if (explicit_targs)
8489 /* [temp.deduct]
8491 The specified template arguments must match the template
8492 parameters in kind (i.e., type, nontype, template), and there
8493 must not be more arguments than there are parameters;
8494 otherwise type deduction fails.
8496 Nontype arguments must match the types of the corresponding
8497 nontype template parameters, or must be convertible to the
8498 types of the corresponding nontype parameters as specified in
8499 _temp.arg.nontype_, otherwise type deduction fails.
8501 All references in the function type of the function template
8502 to the corresponding template parameters are replaced by the
8503 specified template argument values. If a substitution in a
8504 template parameter or in the function type of the function
8505 template results in an invalid type, type deduction fails. */
8506 int i;
8507 tree converted_args;
8509 converted_args
8510 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8511 explicit_targs, NULL_TREE, tf_none,
8512 /*require_all_arguments=*/0));
8513 if (converted_args == error_mark_node)
8514 return 1;
8516 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8517 if (fntype == error_mark_node)
8518 return 1;
8520 /* Place the explicitly specified arguments in TARGS. */
8521 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
8522 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8525 parms = TYPE_ARG_TYPES (fntype);
8526 /* Never do unification on the 'this' parameter. */
8527 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8528 parms = TREE_CHAIN (parms);
8530 if (return_type)
8532 /* We've been given a return type to match, prepend it. */
8533 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8534 args = tree_cons (NULL_TREE, return_type, args);
8535 if (len >= 0)
8536 ++len;
8539 /* We allow incomplete unification without an error message here
8540 because the standard doesn't seem to explicitly prohibit it. Our
8541 callers must be ready to deal with unification failures in any
8542 event. */
8543 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8544 targs, parms, args, /*subr=*/0,
8545 strict, /*allow_incomplete*/1, len);
8547 if (result == 0)
8548 /* All is well so far. Now, check:
8550 [temp.deduct]
8552 When all template arguments have been deduced, all uses of
8553 template parameters in nondeduced contexts are replaced with
8554 the corresponding deduced argument values. If the
8555 substitution results in an invalid type, as described above,
8556 type deduction fails. */
8557 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8558 == error_mark_node)
8559 return 1;
8561 return result;
8564 /* Adjust types before performing type deduction, as described in
8565 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8566 sections are symmetric. PARM is the type of a function parameter
8567 or the return type of the conversion function. ARG is the type of
8568 the argument passed to the call, or the type of the value
8569 initialized with the result of the conversion function. */
8571 static int
8572 maybe_adjust_types_for_deduction (strict, parm, arg)
8573 unification_kind_t strict;
8574 tree* parm;
8575 tree* arg;
8577 int result = 0;
8579 switch (strict)
8581 case DEDUCE_CALL:
8582 break;
8584 case DEDUCE_CONV:
8586 /* Swap PARM and ARG throughout the remainder of this
8587 function; the handling is precisely symmetric since PARM
8588 will initialize ARG rather than vice versa. */
8589 tree* temp = parm;
8590 parm = arg;
8591 arg = temp;
8592 break;
8595 case DEDUCE_EXACT:
8596 /* There is nothing to do in this case. */
8597 return 0;
8599 case DEDUCE_ORDER:
8600 /* DR 214. [temp.func.order] is underspecified, and leads to no
8601 ordering between things like `T *' and `T const &' for `U *'.
8602 The former has T=U and the latter T=U*. The former looks more
8603 specialized and John Spicer considers it well-formed (the EDG
8604 compiler accepts it).
8606 John also confirms that deduction should proceed as in a function
8607 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8608 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8609 to an actual call can have such a type.
8611 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8612 If only ARG is a REFERENCE_TYPE, we look through that and then
8613 proceed as with DEDUCE_CALL (which could further convert it). */
8614 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8616 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8617 return 0;
8618 *arg = TREE_TYPE (*arg);
8620 break;
8621 default:
8622 abort ();
8625 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8627 /* [temp.deduct.call]
8629 If P is not a reference type:
8631 --If A is an array type, the pointer type produced by the
8632 array-to-pointer standard conversion (_conv.array_) is
8633 used in place of A for type deduction; otherwise,
8635 --If A is a function type, the pointer type produced by
8636 the function-to-pointer standard conversion
8637 (_conv.func_) is used in place of A for type deduction;
8638 otherwise,
8640 --If A is a cv-qualified type, the top level
8641 cv-qualifiers of A's type are ignored for type
8642 deduction. */
8643 if (TREE_CODE (*arg) == ARRAY_TYPE)
8644 *arg = build_pointer_type (TREE_TYPE (*arg));
8645 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
8646 *arg = build_pointer_type (*arg);
8647 else
8648 *arg = TYPE_MAIN_VARIANT (*arg);
8651 /* [temp.deduct.call]
8653 If P is a cv-qualified type, the top level cv-qualifiers
8654 of P's type are ignored for type deduction. If P is a
8655 reference type, the type referred to by P is used for
8656 type deduction. */
8657 *parm = TYPE_MAIN_VARIANT (*parm);
8658 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8660 *parm = TREE_TYPE (*parm);
8661 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8663 return result;
8666 /* Most parms like fn_type_unification.
8668 If SUBR is 1, we're being called recursively (to unify the
8669 arguments of a function or method parameter of a function
8670 template). */
8672 static int
8673 type_unification_real (tparms, targs, xparms, xargs, subr,
8674 strict, allow_incomplete, xlen)
8675 tree tparms, targs, xparms, xargs;
8676 int subr;
8677 unification_kind_t strict;
8678 int allow_incomplete, xlen;
8680 tree parm, arg;
8681 int i;
8682 int ntparms = TREE_VEC_LENGTH (tparms);
8683 int sub_strict;
8684 int saw_undeduced = 0;
8685 tree parms, args;
8686 int len;
8688 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
8689 my_friendly_assert (xparms == NULL_TREE
8690 || TREE_CODE (xparms) == TREE_LIST, 290);
8691 /* ARGS could be NULL. */
8692 if (xargs)
8693 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8694 my_friendly_assert (ntparms > 0, 292);
8696 switch (strict)
8698 case DEDUCE_CALL:
8699 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8700 | UNIFY_ALLOW_DERIVED);
8701 break;
8703 case DEDUCE_CONV:
8704 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8705 break;
8707 case DEDUCE_EXACT:
8708 sub_strict = UNIFY_ALLOW_NONE;
8709 break;
8711 case DEDUCE_ORDER:
8712 sub_strict = UNIFY_ALLOW_NONE;
8713 break;
8715 default:
8716 abort ();
8719 if (xlen == 0)
8720 return 0;
8722 again:
8723 parms = xparms;
8724 args = xargs;
8725 len = xlen;
8727 while (parms
8728 && parms != void_list_node
8729 && args
8730 && args != void_list_node)
8732 parm = TREE_VALUE (parms);
8733 parms = TREE_CHAIN (parms);
8734 arg = TREE_VALUE (args);
8735 args = TREE_CHAIN (args);
8737 if (arg == error_mark_node)
8738 return 1;
8739 if (arg == unknown_type_node)
8740 /* We can't deduce anything from this, but we might get all the
8741 template args from other function args. */
8742 continue;
8744 /* Conversions will be performed on a function argument that
8745 corresponds with a function parameter that contains only
8746 non-deducible template parameters and explicitly specified
8747 template parameters. */
8748 if (! uses_template_parms (parm))
8750 tree type;
8752 if (!TYPE_P (arg))
8753 type = TREE_TYPE (arg);
8754 else
8756 type = arg;
8757 arg = NULL_TREE;
8760 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
8762 if (same_type_p (parm, type))
8763 continue;
8765 else
8766 /* It might work; we shouldn't check now, because we might
8767 get into infinite recursion. Overload resolution will
8768 handle it. */
8769 continue;
8771 return 1;
8774 if (!TYPE_P (arg))
8776 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8777 if (type_unknown_p (arg))
8779 /* [temp.deduct.type] A template-argument can be deduced from
8780 a pointer to function or pointer to member function
8781 argument if the set of overloaded functions does not
8782 contain function templates and at most one of a set of
8783 overloaded functions provides a unique match. */
8785 if (resolve_overloaded_unification
8786 (tparms, targs, parm, arg, strict, sub_strict)
8787 != 0)
8788 return 1;
8789 continue;
8791 arg = TREE_TYPE (arg);
8795 int arg_strict = sub_strict;
8797 if (!subr)
8798 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8800 if (unify (tparms, targs, parm, arg, arg_strict))
8801 return 1;
8804 /* Are we done with the interesting parms? */
8805 if (--len == 0)
8806 goto done;
8808 /* Fail if we've reached the end of the parm list, and more args
8809 are present, and the parm list isn't variadic. */
8810 if (args && args != void_list_node && parms == void_list_node)
8811 return 1;
8812 /* Fail if parms are left and they don't have default values. */
8813 if (parms
8814 && parms != void_list_node
8815 && TREE_PURPOSE (parms) == NULL_TREE)
8816 return 1;
8818 done:
8819 if (!subr)
8820 for (i = 0; i < ntparms; i++)
8821 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8823 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8825 /* If this is an undeduced nontype parameter that depends on
8826 a type parameter, try another pass; its type may have been
8827 deduced from a later argument than the one from which
8828 this parameter can be deduced. */
8829 if (TREE_CODE (tparm) == PARM_DECL
8830 && uses_template_parms (TREE_TYPE (tparm))
8831 && !saw_undeduced++)
8832 goto again;
8834 if (!allow_incomplete)
8835 error ("incomplete type unification");
8836 return 2;
8838 return 0;
8841 /* Subroutine of type_unification_real. Args are like the variables at the
8842 call site. ARG is an overloaded function (or template-id); we try
8843 deducing template args from each of the overloads, and if only one
8844 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8846 static int
8847 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8848 sub_strict)
8849 tree tparms, targs, parm, arg;
8850 unification_kind_t strict;
8851 int sub_strict;
8853 tree tempargs = copy_node (targs);
8854 int good = 0;
8856 if (TREE_CODE (arg) == ADDR_EXPR)
8857 arg = TREE_OPERAND (arg, 0);
8859 if (TREE_CODE (arg) == COMPONENT_REF)
8860 /* Handle `&x' where `x' is some static or non-static member
8861 function name. */
8862 arg = TREE_OPERAND (arg, 1);
8864 if (TREE_CODE (arg) == OFFSET_REF)
8865 arg = TREE_OPERAND (arg, 1);
8867 /* Strip baselink information. */
8868 if (BASELINK_P (arg))
8869 arg = BASELINK_FUNCTIONS (arg);
8871 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8873 /* If we got some explicit template args, we need to plug them into
8874 the affected templates before we try to unify, in case the
8875 explicit args will completely resolve the templates in question. */
8877 tree expl_subargs = TREE_OPERAND (arg, 1);
8878 arg = TREE_OPERAND (arg, 0);
8880 for (; arg; arg = OVL_NEXT (arg))
8882 tree fn = OVL_CURRENT (arg);
8883 tree subargs, elem;
8885 if (TREE_CODE (fn) != TEMPLATE_DECL)
8886 continue;
8888 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8889 expl_subargs);
8890 if (subargs)
8892 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
8893 if (TREE_CODE (elem) == METHOD_TYPE)
8894 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8895 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8896 strict, sub_strict);
8900 else if (TREE_CODE (arg) == OVERLOAD
8901 || TREE_CODE (arg) == FUNCTION_DECL)
8903 for (; arg; arg = OVL_NEXT (arg))
8905 tree type = TREE_TYPE (OVL_CURRENT (arg));
8906 if (TREE_CODE (type) == METHOD_TYPE)
8907 type = build_ptrmemfunc_type (build_pointer_type (type));
8908 good += try_one_overload (tparms, targs, tempargs, parm,
8909 type,
8910 strict, sub_strict);
8913 else
8914 abort ();
8916 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8917 to function or pointer to member function argument if the set of
8918 overloaded functions does not contain function templates and at most
8919 one of a set of overloaded functions provides a unique match.
8921 So if we found multiple possibilities, we return success but don't
8922 deduce anything. */
8924 if (good == 1)
8926 int i = TREE_VEC_LENGTH (targs);
8927 for (; i--; )
8928 if (TREE_VEC_ELT (tempargs, i))
8929 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8931 if (good)
8932 return 0;
8934 return 1;
8937 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8938 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8939 different overloads deduce different arguments for a given parm.
8940 Returns 1 on success. */
8942 static int
8943 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8944 sub_strict)
8945 tree tparms, orig_targs, targs, parm, arg;
8946 unification_kind_t strict;
8947 int sub_strict;
8949 int nargs;
8950 tree tempargs;
8951 int i;
8953 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8954 to function or pointer to member function argument if the set of
8955 overloaded functions does not contain function templates and at most
8956 one of a set of overloaded functions provides a unique match.
8958 So if this is a template, just return success. */
8960 if (uses_template_parms (arg))
8961 return 1;
8963 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8965 /* We don't copy orig_targs for this because if we have already deduced
8966 some template args from previous args, unify would complain when we
8967 try to deduce a template parameter for the same argument, even though
8968 there isn't really a conflict. */
8969 nargs = TREE_VEC_LENGTH (targs);
8970 tempargs = make_tree_vec (nargs);
8972 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8973 return 0;
8975 /* First make sure we didn't deduce anything that conflicts with
8976 explicitly specified args. */
8977 for (i = nargs; i--; )
8979 tree elt = TREE_VEC_ELT (tempargs, i);
8980 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8982 if (elt == NULL_TREE)
8983 continue;
8984 else if (uses_template_parms (elt))
8986 /* Since we're unifying against ourselves, we will fill in template
8987 args used in the function parm list with our own template parms.
8988 Discard them. */
8989 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8990 continue;
8992 else if (oldelt && ! template_args_equal (oldelt, elt))
8993 return 0;
8996 for (i = nargs; i--; )
8998 tree elt = TREE_VEC_ELT (tempargs, i);
9000 if (elt)
9001 TREE_VEC_ELT (targs, i) = elt;
9004 return 1;
9007 /* Verify that nondeduce template argument agrees with the type
9008 obtained from argument deduction. Return nonzero if the
9009 verification fails.
9011 For example:
9013 struct A { typedef int X; };
9014 template <class T, class U> struct C {};
9015 template <class T> struct C<T, typename T::X> {};
9017 Then with the instantiation `C<A, int>', we can deduce that
9018 `T' is `A' but unify () does not check whether `typename T::X'
9019 is `int'. This function ensure that they agree.
9021 TARGS, PARMS are the same as the arguments of unify.
9022 ARGS contains template arguments from all levels. */
9024 static int
9025 verify_class_unification (targs, parms, args)
9026 tree targs, parms, args;
9028 parms = tsubst (parms, add_outermost_template_args (args, targs),
9029 tf_none, NULL_TREE);
9030 if (parms == error_mark_node)
9031 return 1;
9033 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9036 /* PARM is a template class (perhaps with unbound template
9037 parameters). ARG is a fully instantiated type. If ARG can be
9038 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9039 TARGS are as for unify. */
9041 static tree
9042 try_class_unification (tparms, targs, parm, arg)
9043 tree tparms;
9044 tree targs;
9045 tree parm;
9046 tree arg;
9048 tree copy_of_targs;
9050 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9051 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
9052 return NULL_TREE;
9054 /* We need to make a new template argument vector for the call to
9055 unify. If we used TARGS, we'd clutter it up with the result of
9056 the attempted unification, even if this class didn't work out.
9057 We also don't want to commit ourselves to all the unifications
9058 we've already done, since unification is supposed to be done on
9059 an argument-by-argument basis. In other words, consider the
9060 following pathological case:
9062 template <int I, int J, int K>
9063 struct S {};
9065 template <int I, int J>
9066 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9068 template <int I, int J, int K>
9069 void f(S<I, J, K>, S<I, I, I>);
9071 void g() {
9072 S<0, 0, 0> s0;
9073 S<0, 1, 2> s2;
9075 f(s0, s2);
9078 Now, by the time we consider the unification involving `s2', we
9079 already know that we must have `f<0, 0, 0>'. But, even though
9080 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9081 because there are two ways to unify base classes of S<0, 1, 2>
9082 with S<I, I, I>. If we kept the already deduced knowledge, we
9083 would reject the possibility I=1. */
9084 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9086 /* If unification failed, we're done. */
9087 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9088 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9089 return NULL_TREE;
9091 return arg;
9094 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9095 have already discovered to be satisfactory. ARG_BINFO is the binfo
9096 for the base class of ARG that we are currently examining. */
9098 static tree
9099 get_template_base_recursive (tparms, targs, parm,
9100 arg_binfo, rval, flags)
9101 tree tparms;
9102 tree targs;
9103 tree arg_binfo;
9104 tree rval;
9105 tree parm;
9106 int flags;
9108 tree binfos;
9109 int i, n_baselinks;
9110 tree arg = BINFO_TYPE (arg_binfo);
9112 if (!(flags & GTB_IGNORE_TYPE))
9114 tree r = try_class_unification (tparms, targs,
9115 parm, arg);
9117 /* If there is more than one satisfactory baseclass, then:
9119 [temp.deduct.call]
9121 If they yield more than one possible deduced A, the type
9122 deduction fails.
9124 applies. */
9125 if (r && rval && !same_type_p (r, rval))
9126 return error_mark_node;
9127 else if (r)
9128 rval = r;
9131 binfos = BINFO_BASETYPES (arg_binfo);
9132 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9134 /* Process base types. */
9135 for (i = 0; i < n_baselinks; i++)
9137 tree base_binfo = TREE_VEC_ELT (binfos, i);
9138 int this_virtual;
9140 /* Skip this base, if we've already seen it. */
9141 if (BINFO_MARKED (base_binfo))
9142 continue;
9144 this_virtual =
9145 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9147 /* When searching for a non-virtual, we cannot mark virtually
9148 found binfos. */
9149 if (! this_virtual)
9150 BINFO_MARKED (base_binfo) = 1;
9152 rval = get_template_base_recursive (tparms, targs,
9153 parm,
9154 base_binfo,
9155 rval,
9156 GTB_VIA_VIRTUAL * this_virtual);
9158 /* If we discovered more than one matching base class, we can
9159 stop now. */
9160 if (rval == error_mark_node)
9161 return error_mark_node;
9164 return rval;
9167 /* Given a template type PARM and a class type ARG, find the unique
9168 base type in ARG that is an instance of PARM. We do not examine
9169 ARG itself; only its base-classes. If there is no appropriate base
9170 class, return NULL_TREE. If there is more than one, return
9171 error_mark_node. PARM may be the type of a partial specialization,
9172 as well as a plain template type. Used by unify. */
9174 static tree
9175 get_template_base (tparms, targs, parm, arg)
9176 tree tparms;
9177 tree targs;
9178 tree parm;
9179 tree arg;
9181 tree rval;
9182 tree arg_binfo;
9184 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9186 arg_binfo = TYPE_BINFO (complete_type (arg));
9187 rval = get_template_base_recursive (tparms, targs,
9188 parm, arg_binfo,
9189 NULL_TREE,
9190 GTB_IGNORE_TYPE);
9192 /* Since get_template_base_recursive marks the bases classes, we
9193 must unmark them here. */
9194 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9196 return rval;
9199 /* Returns the level of DECL, which declares a template parameter. */
9201 static int
9202 template_decl_level (decl)
9203 tree decl;
9205 switch (TREE_CODE (decl))
9207 case TYPE_DECL:
9208 case TEMPLATE_DECL:
9209 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9211 case PARM_DECL:
9212 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9214 default:
9215 abort ();
9216 return 0;
9220 /* Decide whether ARG can be unified with PARM, considering only the
9221 cv-qualifiers of each type, given STRICT as documented for unify.
9222 Returns nonzero iff the unification is OK on that basis.*/
9224 static int
9225 check_cv_quals_for_unify (strict, arg, parm)
9226 int strict;
9227 tree arg;
9228 tree parm;
9230 int arg_quals = cp_type_quals (arg);
9231 int parm_quals = cp_type_quals (parm);
9233 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
9235 /* If the cvr quals of parm will not unify with ARG, they'll be
9236 ignored in instantiation, so we have to do the same here. */
9237 if (TREE_CODE (arg) == REFERENCE_TYPE)
9238 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
9239 if (!POINTER_TYPE_P (arg) &&
9240 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9241 parm_quals &= ~TYPE_QUAL_RESTRICT;
9244 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9245 && (arg_quals & parm_quals) != parm_quals)
9246 return 0;
9248 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9249 && (parm_quals & arg_quals) != arg_quals)
9250 return 0;
9252 return 1;
9255 /* Takes parameters as for type_unification. Returns 0 if the
9256 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9257 bitwise or of the following flags:
9259 UNIFY_ALLOW_NONE:
9260 Require an exact match between PARM and ARG.
9261 UNIFY_ALLOW_MORE_CV_QUAL:
9262 Allow the deduced ARG to be more cv-qualified (by qualification
9263 conversion) than ARG.
9264 UNIFY_ALLOW_LESS_CV_QUAL:
9265 Allow the deduced ARG to be less cv-qualified than ARG.
9266 UNIFY_ALLOW_DERIVED:
9267 Allow the deduced ARG to be a template base class of ARG,
9268 or a pointer to a template base class of the type pointed to by
9269 ARG.
9270 UNIFY_ALLOW_INTEGER:
9271 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9272 case for more information.
9273 UNIFY_ALLOW_OUTER_LEVEL:
9274 This is the outermost level of a deduction. Used to determine validity
9275 of qualification conversions. A valid qualification conversion must
9276 have const qualified pointers leading up to the inner type which
9277 requires additional CV quals, except at the outer level, where const
9278 is not required [conv.qual]. It would be normal to set this flag in
9279 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9280 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9281 This is the outermost level of a deduction, and PARM can be more CV
9282 qualified at this point.
9283 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9284 This is the outermost level of a deduction, and PARM can be less CV
9285 qualified at this point.
9286 UNIFY_ALLOW_MAX_CORRECTION:
9287 This is an INTEGER_TYPE's maximum value. Used if the range may
9288 have been derived from a size specification, such as an array size.
9289 If the size was given by a nontype template parameter N, the maximum
9290 value will have the form N-1. The flag says that we can (and indeed
9291 must) unify N with (ARG + 1), an exception to the normal rules on
9292 folding PARM. */
9294 static int
9295 unify (tparms, targs, parm, arg, strict)
9296 tree tparms, targs, parm, arg;
9297 int strict;
9299 int idx;
9300 tree targ;
9301 tree tparm;
9302 int strict_in = strict;
9304 /* I don't think this will do the right thing with respect to types.
9305 But the only case I've seen it in so far has been array bounds, where
9306 signedness is the only information lost, and I think that will be
9307 okay. */
9308 while (TREE_CODE (parm) == NOP_EXPR)
9309 parm = TREE_OPERAND (parm, 0);
9311 if (arg == error_mark_node)
9312 return 1;
9313 if (arg == unknown_type_node)
9314 /* We can't deduce anything from this, but we might get all the
9315 template args from other function args. */
9316 return 0;
9318 /* If PARM uses template parameters, then we can't bail out here,
9319 even if ARG == PARM, since we won't record unifications for the
9320 template parameters. We might need them if we're trying to
9321 figure out which of two things is more specialized. */
9322 if (arg == parm && !uses_template_parms (parm))
9323 return 0;
9325 /* Immediately reject some pairs that won't unify because of
9326 cv-qualification mismatches. */
9327 if (TREE_CODE (arg) == TREE_CODE (parm)
9328 && TYPE_P (arg)
9329 /* It is the elements of the array which hold the cv quals of an array
9330 type, and the elements might be template type parms. We'll check
9331 when we recurse. */
9332 && TREE_CODE (arg) != ARRAY_TYPE
9333 /* We check the cv-qualifiers when unifying with template type
9334 parameters below. We want to allow ARG `const T' to unify with
9335 PARM `T' for example, when computing which of two templates
9336 is more specialized, for example. */
9337 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9338 && !check_cv_quals_for_unify (strict_in, arg, parm))
9339 return 1;
9341 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9342 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9343 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9344 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9345 strict &= ~UNIFY_ALLOW_DERIVED;
9346 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9347 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9348 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
9350 switch (TREE_CODE (parm))
9352 case TYPENAME_TYPE:
9353 case SCOPE_REF:
9354 case UNBOUND_CLASS_TEMPLATE:
9355 /* In a type which contains a nested-name-specifier, template
9356 argument values cannot be deduced for template parameters used
9357 within the nested-name-specifier. */
9358 return 0;
9360 case TEMPLATE_TYPE_PARM:
9361 case TEMPLATE_TEMPLATE_PARM:
9362 case BOUND_TEMPLATE_TEMPLATE_PARM:
9363 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9365 if (TEMPLATE_TYPE_LEVEL (parm)
9366 != template_decl_level (tparm))
9367 /* The PARM is not one we're trying to unify. Just check
9368 to see if it matches ARG. */
9369 return (TREE_CODE (arg) == TREE_CODE (parm)
9370 && same_type_p (parm, arg)) ? 0 : 1;
9371 idx = TEMPLATE_TYPE_IDX (parm);
9372 targ = TREE_VEC_ELT (targs, idx);
9373 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9375 /* Check for mixed types and values. */
9376 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9377 && TREE_CODE (tparm) != TYPE_DECL)
9378 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9379 && TREE_CODE (tparm) != TEMPLATE_DECL))
9380 return 1;
9382 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9384 /* ARG must be constructed from a template class or a template
9385 template parameter. */
9386 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9387 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9388 return 1;
9391 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9392 tree parmvec = TYPE_TI_ARGS (parm);
9393 tree argvec = TYPE_TI_ARGS (arg);
9394 tree argtmplvec
9395 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9396 int i;
9398 /* The parameter and argument roles have to be switched here
9399 in order to handle default arguments properly. For example,
9400 template<template <class> class TT> void f(TT<int>)
9401 should be able to accept vector<int> which comes from
9402 template <class T, class Allocator = allocator>
9403 class vector. */
9405 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9406 == error_mark_node)
9407 return 1;
9409 /* Deduce arguments T, i from TT<T> or TT<i>.
9410 We check each element of PARMVEC and ARGVEC individually
9411 rather than the whole TREE_VEC since they can have
9412 different number of elements. */
9414 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9416 tree t = TREE_VEC_ELT (parmvec, i);
9418 if (unify (tparms, targs, t,
9419 TREE_VEC_ELT (argvec, i),
9420 UNIFY_ALLOW_NONE))
9421 return 1;
9424 arg = TYPE_TI_TEMPLATE (arg);
9426 /* Fall through to deduce template name. */
9429 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9430 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9432 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9434 /* Simple cases: Value already set, does match or doesn't. */
9435 if (targ != NULL_TREE && template_args_equal (targ, arg))
9436 return 0;
9437 else if (targ)
9438 return 1;
9440 else
9442 /* If ARG is an offset type, we're trying to unify '*T' with
9443 'U C::*', which is ill-formed. See the comment in the
9444 POINTER_TYPE case about this ugliness. */
9445 if (TREE_CODE (arg) == OFFSET_TYPE)
9446 return 1;
9448 /* If PARM is `const T' and ARG is only `int', we don't have
9449 a match unless we are allowing additional qualification.
9450 If ARG is `const int' and PARM is just `T' that's OK;
9451 that binds `const int' to `T'. */
9452 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9453 arg, parm))
9454 return 1;
9456 /* Consider the case where ARG is `const volatile int' and
9457 PARM is `const T'. Then, T should be `volatile int'. */
9458 arg = cp_build_qualified_type_real
9459 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9460 if (arg == error_mark_node)
9461 return 1;
9463 /* Simple cases: Value already set, does match or doesn't. */
9464 if (targ != NULL_TREE && same_type_p (targ, arg))
9465 return 0;
9466 else if (targ)
9467 return 1;
9469 /* Make sure that ARG is not a variable-sized array. (Note
9470 that were talking about variable-sized arrays (like
9471 `int[n]'), rather than arrays of unknown size (like
9472 `int[]').) We'll get very confused by such a type since
9473 the bound of the array will not be computable in an
9474 instantiation. Besides, such types are not allowed in
9475 ISO C++, so we can do as we please here. */
9476 if (variably_modified_type_p (arg))
9477 return 1;
9480 TREE_VEC_ELT (targs, idx) = arg;
9481 return 0;
9483 case TEMPLATE_PARM_INDEX:
9484 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9486 if (TEMPLATE_PARM_LEVEL (parm)
9487 != template_decl_level (tparm))
9488 /* The PARM is not one we're trying to unify. Just check
9489 to see if it matches ARG. */
9490 return (TREE_CODE (arg) == TREE_CODE (parm)
9491 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
9493 idx = TEMPLATE_PARM_IDX (parm);
9494 targ = TREE_VEC_ELT (targs, idx);
9496 if (targ)
9498 int i = (cp_tree_equal (targ, arg) > 0);
9499 if (i == 1)
9500 return 0;
9501 else if (i == 0)
9502 return 1;
9503 else
9504 abort ();
9507 /* [temp.deduct.type] If, in the declaration of a function template
9508 with a non-type template-parameter, the non-type
9509 template-parameter is used in an expression in the function
9510 parameter-list and, if the corresponding template-argument is
9511 deduced, the template-argument type shall match the type of the
9512 template-parameter exactly, except that a template-argument
9513 deduced from an array bound may be of any integral type.
9514 The non-type parameter might use already deduced type parameters. */
9515 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9516 if (!TREE_TYPE (arg))
9517 /* Template-parameter dependent expression. Just accept it for now.
9518 It will later be processed in convert_template_argument. */
9520 else if (same_type_p (TREE_TYPE (arg), tparm))
9521 /* OK */;
9522 else if ((strict & UNIFY_ALLOW_INTEGER)
9523 && (TREE_CODE (tparm) == INTEGER_TYPE
9524 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9525 /* OK */;
9526 else if (uses_template_parms (tparm))
9527 /* We haven't deduced the type of this parameter yet. Try again
9528 later. */
9529 return 0;
9530 else
9531 return 1;
9533 TREE_VEC_ELT (targs, idx) = arg;
9534 return 0;
9536 case POINTER_TYPE:
9538 if (TREE_CODE (arg) != POINTER_TYPE)
9539 return 1;
9541 /* [temp.deduct.call]
9543 A can be another pointer or pointer to member type that can
9544 be converted to the deduced A via a qualification
9545 conversion (_conv.qual_).
9547 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9548 This will allow for additional cv-qualification of the
9549 pointed-to types if appropriate. */
9551 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9552 /* The derived-to-base conversion only persists through one
9553 level of pointers. */
9554 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9556 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
9557 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
9559 /* Avoid getting confused about cv-quals; don't recurse here.
9560 Pointers to members should really be just OFFSET_TYPE, not
9561 this two-level nonsense... */
9563 parm = TREE_TYPE (parm);
9564 arg = TREE_TYPE (arg);
9565 goto offset;
9568 return unify (tparms, targs, TREE_TYPE (parm),
9569 TREE_TYPE (arg), strict);
9572 case REFERENCE_TYPE:
9573 if (TREE_CODE (arg) != REFERENCE_TYPE)
9574 return 1;
9575 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9576 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9578 case ARRAY_TYPE:
9579 if (TREE_CODE (arg) != ARRAY_TYPE)
9580 return 1;
9581 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9582 != (TYPE_DOMAIN (arg) == NULL_TREE))
9583 return 1;
9584 if (TYPE_DOMAIN (parm) != NULL_TREE
9585 && unify (tparms, targs, TYPE_DOMAIN (parm),
9586 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
9587 return 1;
9588 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9589 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9591 case REAL_TYPE:
9592 case COMPLEX_TYPE:
9593 case VECTOR_TYPE:
9594 case INTEGER_TYPE:
9595 case BOOLEAN_TYPE:
9596 case VOID_TYPE:
9597 if (TREE_CODE (arg) != TREE_CODE (parm))
9598 return 1;
9600 if (TREE_CODE (parm) == INTEGER_TYPE
9601 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
9603 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
9604 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
9605 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
9606 return 1;
9607 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
9608 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
9609 TYPE_MAX_VALUE (arg),
9610 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
9611 return 1;
9613 /* We have already checked cv-qualification at the top of the
9614 function. */
9615 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9616 return 1;
9618 /* As far as unification is concerned, this wins. Later checks
9619 will invalidate it if necessary. */
9620 return 0;
9622 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9623 /* Type INTEGER_CST can come from ordinary constant template args. */
9624 case INTEGER_CST:
9625 while (TREE_CODE (arg) == NOP_EXPR)
9626 arg = TREE_OPERAND (arg, 0);
9628 if (TREE_CODE (arg) != INTEGER_CST)
9629 return 1;
9630 return !tree_int_cst_equal (parm, arg);
9632 case TREE_VEC:
9634 int i;
9635 if (TREE_CODE (arg) != TREE_VEC)
9636 return 1;
9637 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9638 return 1;
9639 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
9640 if (unify (tparms, targs,
9641 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
9642 UNIFY_ALLOW_NONE))
9643 return 1;
9644 return 0;
9647 case RECORD_TYPE:
9648 case UNION_TYPE:
9649 if (TREE_CODE (arg) != TREE_CODE (parm))
9650 return 1;
9652 if (TYPE_PTRMEMFUNC_P (parm))
9654 if (!TYPE_PTRMEMFUNC_P (arg))
9655 return 1;
9657 return unify (tparms, targs,
9658 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9659 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9660 strict);
9663 if (CLASSTYPE_TEMPLATE_INFO (parm))
9665 tree t = NULL_TREE;
9667 if (strict_in & UNIFY_ALLOW_DERIVED)
9669 /* First, we try to unify the PARM and ARG directly. */
9670 t = try_class_unification (tparms, targs,
9671 parm, arg);
9673 if (!t)
9675 /* Fallback to the special case allowed in
9676 [temp.deduct.call]:
9678 If P is a class, and P has the form
9679 template-id, then A can be a derived class of
9680 the deduced A. Likewise, if P is a pointer to
9681 a class of the form template-id, A can be a
9682 pointer to a derived class pointed to by the
9683 deduced A. */
9684 t = get_template_base (tparms, targs,
9685 parm, arg);
9687 if (! t || t == error_mark_node)
9688 return 1;
9691 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9692 && (CLASSTYPE_TI_TEMPLATE (parm)
9693 == CLASSTYPE_TI_TEMPLATE (arg)))
9694 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9695 Then, we should unify `int' and `U'. */
9696 t = arg;
9697 else
9698 /* There's no chance of unification succeeding. */
9699 return 1;
9701 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
9702 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
9704 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
9705 return 1;
9706 return 0;
9708 case METHOD_TYPE:
9709 case FUNCTION_TYPE:
9710 if (TREE_CODE (arg) != TREE_CODE (parm))
9711 return 1;
9713 if (unify (tparms, targs, TREE_TYPE (parm),
9714 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
9715 return 1;
9716 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
9717 TYPE_ARG_TYPES (arg), 1,
9718 DEDUCE_EXACT, 0, -1);
9720 case OFFSET_TYPE:
9721 offset:
9722 if (TREE_CODE (arg) != OFFSET_TYPE)
9723 return 1;
9724 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
9725 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
9726 return 1;
9727 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9728 strict);
9730 case CONST_DECL:
9731 if (DECL_TEMPLATE_PARM_P (parm))
9732 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
9733 if (arg != decl_constant_value (parm))
9734 return 1;
9735 return 0;
9737 case TEMPLATE_DECL:
9738 /* Matched cases are handled by the ARG == PARM test above. */
9739 return 1;
9741 case MINUS_EXPR:
9742 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9743 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
9745 /* We handle this case specially, since it comes up with
9746 arrays. In particular, something like:
9748 template <int N> void f(int (&x)[N]);
9750 Here, we are trying to unify the range type, which
9751 looks like [0 ... (N - 1)]. */
9752 tree t, t1, t2;
9753 t1 = TREE_OPERAND (parm, 0);
9754 t2 = TREE_OPERAND (parm, 1);
9756 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
9758 return unify (tparms, targs, t1, t, strict);
9760 /* else fall through */
9762 default:
9763 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
9766 /* We're looking at an expression. This can happen with
9767 something like:
9769 template <int I>
9770 void foo(S<I>, S<I + 2>);
9772 This is a "nondeduced context":
9774 [deduct.type]
9776 The nondeduced contexts are:
9778 --A type that is a template-id in which one or more of
9779 the template-arguments is an expression that references
9780 a template-parameter.
9782 In these cases, we assume deduction succeeded, but don't
9783 actually infer any unifications. */
9785 if (!uses_template_parms (parm)
9786 && !template_args_equal (parm, arg))
9787 return 1;
9788 else
9789 return 0;
9791 else
9792 sorry ("use of `%s' in template type unification",
9793 tree_code_name [(int) TREE_CODE (parm)]);
9795 return 1;
9799 /* Called if RESULT is explicitly instantiated, or is a member of an
9800 explicitly instantiated class, or if using -frepo and the
9801 instantiation of RESULT has been assigned to this file. */
9803 void
9804 mark_decl_instantiated (result, extern_p)
9805 tree result;
9806 int extern_p;
9808 if (TREE_CODE (result) != FUNCTION_DECL)
9809 /* The TREE_PUBLIC flag for function declarations will have been
9810 set correctly by tsubst. */
9811 TREE_PUBLIC (result) = 1;
9813 /* We used to set this unconditionally; we moved that to
9814 do_decl_instantiation so it wouldn't get set on members of
9815 explicit class template instantiations. But we still need to set
9816 it here for the 'extern template' case in order to suppress
9817 implicit instantiations. */
9818 if (extern_p)
9819 SET_DECL_EXPLICIT_INSTANTIATION (result);
9821 if (! extern_p)
9823 DECL_INTERFACE_KNOWN (result) = 1;
9824 DECL_NOT_REALLY_EXTERN (result) = 1;
9826 /* Always make artificials weak. */
9827 if (DECL_ARTIFICIAL (result) && flag_weak)
9828 comdat_linkage (result);
9829 /* For WIN32 we also want to put explicit instantiations in
9830 linkonce sections. */
9831 else if (TREE_PUBLIC (result))
9832 maybe_make_one_only (result);
9834 else if (TREE_CODE (result) == FUNCTION_DECL)
9835 defer_fn (result);
9838 /* Given two function templates PAT1 and PAT2, return:
9840 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9842 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9843 -1 if PAT2 is more specialized than PAT1.
9844 0 if neither is more specialized.
9846 LEN is passed through to fn_type_unification. */
9849 more_specialized (pat1, pat2, deduce, len)
9850 tree pat1, pat2;
9851 int deduce;
9852 int len;
9854 tree targs;
9855 int winner = 0;
9857 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9858 NULL_TREE, 0, deduce, len);
9859 if (targs)
9860 --winner;
9862 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9863 NULL_TREE, 0, deduce, len);
9864 if (targs)
9865 ++winner;
9867 return winner;
9870 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9872 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9873 -1 if PAT2 is more specialized than PAT1.
9874 0 if neither is more specialized. */
9877 more_specialized_class (pat1, pat2)
9878 tree pat1, pat2;
9880 tree targs;
9881 int winner = 0;
9883 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9884 TREE_PURPOSE (pat2));
9885 if (targs)
9886 --winner;
9888 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9889 TREE_PURPOSE (pat1));
9890 if (targs)
9891 ++winner;
9893 return winner;
9896 /* Return the template arguments that will produce the function signature
9897 DECL from the function template FN, with the explicit template
9898 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9899 also match. Return NULL_TREE if no satisfactory arguments could be
9900 found. DEDUCE and LEN are passed through to fn_type_unification. */
9902 static tree
9903 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9904 tree fn, decl, explicit_args;
9905 int check_rettype, deduce, len;
9907 int ntparms = DECL_NTPARMS (fn);
9908 tree targs = make_tree_vec (ntparms);
9909 tree decl_type;
9910 tree decl_arg_types;
9911 int i;
9913 /* Substitute the explicit template arguments into the type of DECL.
9914 The call to fn_type_unification will handle substitution into the
9915 FN. */
9916 decl_type = TREE_TYPE (decl);
9917 if (explicit_args && uses_template_parms (decl_type))
9919 tree tmpl;
9920 tree converted_args;
9922 if (DECL_TEMPLATE_INFO (decl))
9923 tmpl = DECL_TI_TEMPLATE (decl);
9924 else
9925 /* We can get here for some invalid specializations. */
9926 return NULL_TREE;
9928 converted_args
9929 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9930 explicit_args, NULL_TREE,
9931 tf_none, /*require_all_arguments=*/0));
9932 if (converted_args == error_mark_node)
9933 return NULL_TREE;
9935 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
9936 if (decl_type == error_mark_node)
9937 return NULL_TREE;
9940 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9941 /* Never do unification on the 'this' parameter. */
9942 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9943 decl_arg_types = TREE_CHAIN (decl_arg_types);
9945 i = fn_type_unification (fn, explicit_args, targs,
9946 decl_arg_types,
9947 (check_rettype || DECL_CONV_FN_P (fn)
9948 ? TREE_TYPE (decl_type) : NULL_TREE),
9949 deduce, len);
9951 if (i != 0)
9952 return NULL_TREE;
9954 return targs;
9957 /* For most uses, we want to check the return type. */
9959 tree
9960 get_bindings (fn, decl, explicit_args)
9961 tree fn, decl, explicit_args;
9963 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9966 /* But for resolve_overloaded_unification, we only care about the parameter
9967 types. */
9969 static tree
9970 get_bindings_overload (fn, decl, explicit_args)
9971 tree fn, decl, explicit_args;
9973 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9976 /* Return the innermost template arguments that, when applied to a
9977 template specialization whose innermost template parameters are
9978 TPARMS, and whose specialization arguments are PARMS, yield the
9979 ARGS.
9981 For example, suppose we have:
9983 template <class T, class U> struct S {};
9984 template <class T> struct S<T*, int> {};
9986 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9987 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9988 int}. The resulting vector will be {double}, indicating that `T'
9989 is bound to `double'. */
9991 static tree
9992 get_class_bindings (tparms, parms, args)
9993 tree tparms, parms, args;
9995 int i, ntparms = TREE_VEC_LENGTH (tparms);
9996 tree vec = make_tree_vec (ntparms);
9998 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9999 UNIFY_ALLOW_NONE))
10000 return NULL_TREE;
10002 for (i = 0; i < ntparms; ++i)
10003 if (! TREE_VEC_ELT (vec, i))
10004 return NULL_TREE;
10006 if (verify_class_unification (vec, parms, args))
10007 return NULL_TREE;
10009 return vec;
10012 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10013 Pick the most specialized template, and return the corresponding
10014 instantiation, or if there is no corresponding instantiation, the
10015 template itself. If there is no most specialized template,
10016 error_mark_node is returned. If there are no templates at all,
10017 NULL_TREE is returned. */
10019 tree
10020 most_specialized_instantiation (instantiations)
10021 tree instantiations;
10023 tree fn, champ;
10024 int fate;
10026 if (!instantiations)
10027 return NULL_TREE;
10029 champ = instantiations;
10030 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10032 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10033 DEDUCE_EXACT, -1);
10034 if (fate == 1)
10036 else
10038 if (fate == 0)
10040 fn = TREE_CHAIN (fn);
10041 if (! fn)
10042 return error_mark_node;
10044 champ = fn;
10048 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10050 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10051 DEDUCE_EXACT, -1);
10052 if (fate != 1)
10053 return error_mark_node;
10056 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10059 /* Return the most specialized of the list of templates in FNS that can
10060 produce an instantiation matching DECL, given the explicit template
10061 arguments EXPLICIT_ARGS. */
10063 static tree
10064 most_specialized (fns, decl, explicit_args)
10065 tree fns, decl, explicit_args;
10067 tree candidates = NULL_TREE;
10068 tree fn, args;
10070 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10072 tree candidate = TREE_VALUE (fn);
10074 args = get_bindings (candidate, decl, explicit_args);
10075 if (args)
10076 candidates = tree_cons (NULL_TREE, candidate, candidates);
10079 return most_specialized_instantiation (candidates);
10082 /* If DECL is a specialization of some template, return the most
10083 general such template. Otherwise, returns NULL_TREE.
10085 For example, given:
10087 template <class T> struct S { template <class U> void f(U); };
10089 if TMPL is `template <class U> void S<int>::f(U)' this will return
10090 the full template. This function will not trace past partial
10091 specializations, however. For example, given in addition:
10093 template <class T> struct S<T*> { template <class U> void f(U); };
10095 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10096 `template <class T> template <class U> S<T*>::f(U)'. */
10098 tree
10099 most_general_template (decl)
10100 tree decl;
10102 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10103 an immediate specialization. */
10104 if (TREE_CODE (decl) == FUNCTION_DECL)
10106 if (DECL_TEMPLATE_INFO (decl)) {
10107 decl = DECL_TI_TEMPLATE (decl);
10109 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10110 template friend. */
10111 if (TREE_CODE (decl) != TEMPLATE_DECL)
10112 return NULL_TREE;
10113 } else
10114 return NULL_TREE;
10117 /* Look for more and more general templates. */
10118 while (DECL_TEMPLATE_INFO (decl))
10120 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10121 in some cases. (See cp-tree.h for details.) */
10122 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10123 break;
10125 /* Stop if we run into an explicitly specialized class template. */
10126 if (!DECL_NAMESPACE_SCOPE_P (decl)
10127 && DECL_CONTEXT (decl)
10128 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10129 break;
10131 decl = DECL_TI_TEMPLATE (decl);
10134 return decl;
10137 /* Return the most specialized of the class template specializations
10138 of TMPL which can produce an instantiation matching ARGS, or
10139 error_mark_node if the choice is ambiguous. */
10141 static tree
10142 most_specialized_class (tmpl, args)
10143 tree tmpl;
10144 tree args;
10146 tree list = NULL_TREE;
10147 tree t;
10148 tree champ;
10149 int fate;
10151 tmpl = most_general_template (tmpl);
10152 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10154 tree spec_args
10155 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10156 if (spec_args)
10158 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10159 TREE_TYPE (list) = TREE_TYPE (t);
10163 if (! list)
10164 return NULL_TREE;
10166 t = list;
10167 champ = t;
10168 t = TREE_CHAIN (t);
10169 for (; t; t = TREE_CHAIN (t))
10171 fate = more_specialized_class (champ, t);
10172 if (fate == 1)
10174 else
10176 if (fate == 0)
10178 t = TREE_CHAIN (t);
10179 if (! t)
10180 return error_mark_node;
10182 champ = t;
10186 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10188 fate = more_specialized_class (champ, t);
10189 if (fate != 1)
10190 return error_mark_node;
10193 return champ;
10196 /* Explicitly instantiate DECL. */
10198 void
10199 do_decl_instantiation (tree decl, tree storage)
10201 tree result = NULL_TREE;
10202 int extern_p = 0;
10204 if (!decl)
10205 /* An error occurred, for which grokdeclarator has already issued
10206 an appropriate message. */
10207 return;
10208 else if (! DECL_LANG_SPECIFIC (decl))
10210 error ("explicit instantiation of non-template `%#D'", decl);
10211 return;
10213 else if (TREE_CODE (decl) == VAR_DECL)
10215 /* There is an asymmetry here in the way VAR_DECLs and
10216 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10217 the latter, the DECL we get back will be marked as a
10218 template instantiation, and the appropriate
10219 DECL_TEMPLATE_INFO will be set up. This does not happen for
10220 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10221 should handle VAR_DECLs as it currently handles
10222 FUNCTION_DECLs. */
10223 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10224 if (!result || TREE_CODE (result) != VAR_DECL)
10226 error ("no matching template for `%D' found", decl);
10227 return;
10230 else if (TREE_CODE (decl) != FUNCTION_DECL)
10232 error ("explicit instantiation of `%#D'", decl);
10233 return;
10235 else
10236 result = decl;
10238 /* Check for various error cases. Note that if the explicit
10239 instantiation is valid the RESULT will currently be marked as an
10240 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10241 until we get here. */
10243 if (DECL_TEMPLATE_SPECIALIZATION (result))
10245 /* DR 259 [temp.spec].
10247 Both an explicit instantiation and a declaration of an explicit
10248 specialization shall not appear in a program unless the explicit
10249 instantiation follows a declaration of the explicit specialization.
10251 For a given set of template parameters, if an explicit
10252 instantiation of a template appears after a declaration of an
10253 explicit specialization for that template, the explicit
10254 instantiation has no effect. */
10255 return;
10257 else if (DECL_EXPLICIT_INSTANTIATION (result))
10259 /* [temp.spec]
10261 No program shall explicitly instantiate any template more
10262 than once.
10264 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10265 instantiation was `extern' and the second is not, and EXTERN_P for
10266 the opposite case. If -frepo, chances are we already got marked
10267 as an explicit instantiation because of the repo file. */
10268 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
10269 pedwarn ("duplicate explicit instantiation of `%#D'", result);
10271 /* If we've already instantiated the template, just return now. */
10272 if (DECL_INTERFACE_KNOWN (result))
10273 return;
10275 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10277 error ("no matching template for `%D' found", result);
10278 return;
10280 else if (!DECL_TEMPLATE_INFO (result))
10282 pedwarn ("explicit instantiation of non-template `%#D'", result);
10283 return;
10286 if (flag_external_templates)
10287 return;
10289 if (storage == NULL_TREE)
10291 else if (storage == ridpointers[(int) RID_EXTERN])
10293 if (pedantic && !in_system_header)
10294 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10295 extern_p = 1;
10297 else
10298 error ("storage class `%D' applied to template instantiation",
10299 storage);
10301 SET_DECL_EXPLICIT_INSTANTIATION (result);
10302 mark_decl_instantiated (result, extern_p);
10303 repo_template_instantiated (result, extern_p);
10304 if (! extern_p)
10305 instantiate_decl (result, /*defer_ok=*/1);
10308 void
10309 mark_class_instantiated (t, extern_p)
10310 tree t;
10311 int extern_p;
10313 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10314 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10315 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10316 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10317 if (! extern_p)
10319 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10320 rest_of_type_compilation (t, 1);
10324 /* Perform an explicit instantiation of template class T. STORAGE, if
10325 non-null, is the RID for extern, inline or static. COMPLAIN is
10326 nonzero if this is called from the parser, zero if called recursively,
10327 since the standard is unclear (as detailed below). */
10329 void
10330 do_type_instantiation (t, storage, complain)
10331 tree t, storage;
10332 tsubst_flags_t complain;
10334 int extern_p = 0;
10335 int nomem_p = 0;
10336 int static_p = 0;
10338 if (TREE_CODE (t) == TYPE_DECL)
10339 t = TREE_TYPE (t);
10341 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10343 error ("explicit instantiation of non-template type `%T'", t);
10344 return;
10347 complete_type (t);
10349 /* With -fexternal-templates, explicit instantiations are treated the same
10350 as implicit ones. */
10351 if (flag_external_templates)
10352 return;
10354 if (!COMPLETE_TYPE_P (t))
10356 if (complain & tf_error)
10357 error ("explicit instantiation of `%#T' before definition of template",
10359 return;
10362 if (storage != NULL_TREE)
10364 if (pedantic && !in_system_header)
10365 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
10366 IDENTIFIER_POINTER (storage));
10368 if (storage == ridpointers[(int) RID_INLINE])
10369 nomem_p = 1;
10370 else if (storage == ridpointers[(int) RID_EXTERN])
10371 extern_p = 1;
10372 else if (storage == ridpointers[(int) RID_STATIC])
10373 static_p = 1;
10374 else
10376 error ("storage class `%D' applied to template instantiation",
10377 storage);
10378 extern_p = 0;
10382 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10384 /* DR 259 [temp.spec].
10386 Both an explicit instantiation and a declaration of an explicit
10387 specialization shall not appear in a program unless the explicit
10388 instantiation follows a declaration of the explicit specialization.
10390 For a given set of template parameters, if an explicit
10391 instantiation of a template appears after a declaration of an
10392 explicit specialization for that template, the explicit
10393 instantiation has no effect. */
10394 return;
10396 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10398 /* [temp.spec]
10400 No program shall explicitly instantiate any template more
10401 than once.
10403 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10404 was `extern'. If EXTERN_P then the second is. If -frepo, chances
10405 are we already got marked as an explicit instantiation because of the
10406 repo file. All these cases are OK. */
10407 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
10408 && (complain & tf_error))
10409 pedwarn ("duplicate explicit instantiation of `%#T'", t);
10411 /* If we've already instantiated the template, just return now. */
10412 if (!CLASSTYPE_INTERFACE_ONLY (t))
10413 return;
10416 mark_class_instantiated (t, extern_p);
10417 repo_template_instantiated (t, extern_p);
10419 if (nomem_p)
10420 return;
10423 tree tmp;
10425 /* In contrast to implicit instantiation, where only the
10426 declarations, and not the definitions, of members are
10427 instantiated, we have here:
10429 [temp.explicit]
10431 The explicit instantiation of a class template specialization
10432 implies the instantiation of all of its members not
10433 previously explicitly specialized in the translation unit
10434 containing the explicit instantiation.
10436 Of course, we can't instantiate member template classes, since
10437 we don't have any arguments for them. Note that the standard
10438 is unclear on whether the instantiation of the members are
10439 *explicit* instantiations or not. We choose to be generous,
10440 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10441 the explicit instantiation of a class where some of the members
10442 have no definition in the current translation unit. */
10444 if (! static_p)
10445 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10446 if (TREE_CODE (tmp) == FUNCTION_DECL
10447 && DECL_TEMPLATE_INSTANTIATION (tmp))
10449 mark_decl_instantiated (tmp, extern_p);
10450 repo_template_instantiated (tmp, extern_p);
10451 if (! extern_p)
10452 instantiate_decl (tmp, /*defer_ok=*/1);
10455 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10456 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10458 mark_decl_instantiated (tmp, extern_p);
10459 repo_template_instantiated (tmp, extern_p);
10460 if (! extern_p)
10461 instantiate_decl (tmp, /*defer_ok=*/1);
10464 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
10465 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
10466 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
10467 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
10471 /* Given a function DECL, which is a specialization of TMPL, modify
10472 DECL to be a re-instantiation of TMPL with the same template
10473 arguments. TMPL should be the template into which tsubst'ing
10474 should occur for DECL, not the most general template.
10476 One reason for doing this is a scenario like this:
10478 template <class T>
10479 void f(const T&, int i);
10481 void g() { f(3, 7); }
10483 template <class T>
10484 void f(const T& t, const int i) { }
10486 Note that when the template is first instantiated, with
10487 instantiate_template, the resulting DECL will have no name for the
10488 first parameter, and the wrong type for the second. So, when we go
10489 to instantiate the DECL, we regenerate it. */
10491 static void
10492 regenerate_decl_from_template (decl, tmpl)
10493 tree decl;
10494 tree tmpl;
10496 /* The most general version of TMPL. */
10497 tree gen_tmpl;
10498 /* The arguments used to instantiate DECL, from the most general
10499 template. */
10500 tree args;
10501 tree code_pattern;
10502 tree new_decl;
10503 int unregistered;
10505 args = DECL_TI_ARGS (decl);
10506 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10508 /* Unregister the specialization so that when we tsubst we will not
10509 just return DECL. We don't have to unregister DECL from TMPL
10510 because if would only be registered there if it were a partial
10511 instantiation of a specialization, which it isn't: it's a full
10512 instantiation. */
10513 gen_tmpl = most_general_template (tmpl);
10514 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl));
10515 unregistered = unregister_specialization (decl, gen_tmpl);
10517 /* If the DECL was not unregistered then something peculiar is
10518 happening: we created a specialization but did not call
10519 register_specialization for it. */
10520 my_friendly_assert (unregistered, 0);
10522 /* Do the substitution to get the new declaration. */
10523 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10525 if (TREE_CODE (decl) == VAR_DECL)
10527 /* Set up DECL_INITIAL, since tsubst doesn't. */
10528 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10529 DECL_INITIAL (new_decl) =
10530 tsubst_expr (DECL_INITIAL (code_pattern), args,
10531 tf_error, DECL_TI_TEMPLATE (decl));
10533 else if (TREE_CODE (decl) == FUNCTION_DECL)
10535 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10536 new decl. */
10537 DECL_INITIAL (new_decl) = error_mark_node;
10538 /* And don't complain about a duplicate definition. */
10539 DECL_INITIAL (decl) = NULL_TREE;
10542 pop_access_scope (decl);
10544 /* The immediate parent of the new template is still whatever it was
10545 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10546 general template. We also reset the DECL_ASSEMBLER_NAME since
10547 tsubst always calculates the name as if the function in question
10548 were really a template instance, and sometimes, with friend
10549 functions, this is not so. See tsubst_friend_function for
10550 details. */
10551 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10552 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10553 COPY_DECL_RTL (decl, new_decl);
10554 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10556 /* Call duplicate decls to merge the old and new declarations. */
10557 duplicate_decls (new_decl, decl);
10559 /* Now, re-register the specialization. */
10560 register_specialization (decl, gen_tmpl, args);
10563 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10564 substituted to get DECL. */
10566 static tree
10567 template_for_substitution (tree decl)
10569 tree tmpl = DECL_TI_TEMPLATE (decl);
10571 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10572 for the instantiation. This is not always the most general
10573 template. Consider, for example:
10575 template <class T>
10576 struct S { template <class U> void f();
10577 template <> void f<int>(); };
10579 and an instantiation of S<double>::f<int>. We want TD to be the
10580 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10581 while (/* An instantiation cannot have a definition, so we need a
10582 more general template. */
10583 DECL_TEMPLATE_INSTANTIATION (tmpl)
10584 /* We must also deal with friend templates. Given:
10586 template <class T> struct S {
10587 template <class U> friend void f() {};
10590 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10591 so far as the language is concerned, but that's still
10592 where we get the pattern for the instantiation from. On
10593 other hand, if the definition comes outside the class, say:
10595 template <class T> struct S {
10596 template <class U> friend void f();
10598 template <class U> friend void f() {}
10600 we don't need to look any further. That's what the check for
10601 DECL_INITIAL is for. */
10602 || (TREE_CODE (decl) == FUNCTION_DECL
10603 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10604 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10606 /* The present template, TD, should not be a definition. If it
10607 were a definition, we should be using it! Note that we
10608 cannot restructure the loop to just keep going until we find
10609 a template with a definition, since that might go too far if
10610 a specialization was declared, but not defined. */
10611 my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL
10612 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))),
10613 0);
10615 /* Fetch the more general template. */
10616 tmpl = DECL_TI_TEMPLATE (tmpl);
10619 return tmpl;
10622 /* Produce the definition of D, a _DECL generated from a template. If
10623 DEFER_OK is nonzero, then we don't have to actually do the
10624 instantiation now; we just have to do it sometime. */
10626 tree
10627 instantiate_decl (d, defer_ok)
10628 tree d;
10629 int defer_ok;
10631 tree tmpl = DECL_TI_TEMPLATE (d);
10632 tree gen_args;
10633 tree args;
10634 tree td;
10635 tree code_pattern;
10636 tree spec;
10637 tree gen_tmpl;
10638 int pattern_defined;
10639 int line = lineno;
10640 int need_push;
10641 const char *file = input_filename;
10643 /* This function should only be used to instantiate templates for
10644 functions and static member variables. */
10645 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
10646 || TREE_CODE (d) == VAR_DECL, 0);
10648 /* Don't instantiate cloned functions. Instead, instantiate the
10649 functions they cloned. */
10650 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10651 d = DECL_CLONED_FUNCTION (d);
10653 if (DECL_TEMPLATE_INSTANTIATED (d))
10654 /* D has already been instantiated. It might seem reasonable to
10655 check whether or not D is an explicit instantiation, and, if so,
10656 stop here. But when an explicit instantiation is deferred
10657 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10658 is set, even though we still need to do the instantiation. */
10659 return d;
10661 /* If we already have a specialization of this declaration, then
10662 there's no reason to instantiate it. Note that
10663 retrieve_specialization gives us both instantiations and
10664 specializations, so we must explicitly check
10665 DECL_TEMPLATE_SPECIALIZATION. */
10666 gen_tmpl = most_general_template (tmpl);
10667 gen_args = DECL_TI_ARGS (d);
10668 spec = retrieve_specialization (gen_tmpl, gen_args);
10669 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10670 return spec;
10672 /* This needs to happen before any tsubsting. */
10673 if (! push_tinst_level (d))
10674 return d;
10676 timevar_push (TV_PARSE);
10678 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
10679 for the instantiation. */
10680 td = template_for_substitution (d);
10681 code_pattern = DECL_TEMPLATE_RESULT (td);
10683 /* In the case of a friend template whose definition is provided
10684 outside the class, we may have too many arguments. Drop the ones
10685 we don't need. */
10686 args = get_innermost_template_args (gen_args,
10687 TMPL_PARMS_DEPTH
10688 (DECL_TEMPLATE_PARMS (td)));
10690 if (TREE_CODE (d) == FUNCTION_DECL)
10691 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
10692 else
10693 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
10695 lineno = DECL_SOURCE_LINE (d);
10696 input_filename = DECL_SOURCE_FILE (d);
10698 if (pattern_defined)
10700 /* Let the repository code that this template definition is
10701 available.
10703 The repository doesn't need to know about cloned functions
10704 because they never actually show up in the object file. It
10705 does need to know about the clones; those are the symbols
10706 that the linker will be emitting error messages about. */
10707 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10708 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10710 tree t;
10712 for (t = TREE_CHAIN (d);
10713 t && DECL_CLONED_FUNCTION_P (t);
10714 t = TREE_CHAIN (t))
10715 repo_template_used (t);
10717 else
10718 repo_template_used (d);
10720 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10722 if (flag_alt_external_templates)
10724 if (interface_unknown)
10725 warn_if_unknown_interface (d);
10727 else if (DECL_INTERFACE_KNOWN (code_pattern))
10729 DECL_INTERFACE_KNOWN (d) = 1;
10730 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
10732 else
10733 warn_if_unknown_interface (code_pattern);
10736 if (at_eof)
10737 import_export_decl (d);
10740 if (!defer_ok)
10742 /* Recheck the substitutions to obtain any warning messages
10743 about ignoring cv qualifiers. */
10744 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10745 tree type = TREE_TYPE (gen);
10747 /* Make sure that we can see identifiers, and compute access
10748 correctly. D is already the target FUNCTION_DECL with the
10749 right context. */
10750 push_access_scope (d);
10752 if (TREE_CODE (gen) == FUNCTION_DECL)
10754 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
10755 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
10756 tf_error | tf_warning, d);
10757 /* Don't simply tsubst the function type, as that will give
10758 duplicate warnings about poor parameter qualifications.
10759 The function arguments are the same as the decl_arguments
10760 without the top level cv qualifiers. */
10761 type = TREE_TYPE (type);
10763 tsubst (type, gen_args, tf_error | tf_warning, d);
10765 pop_access_scope (d);
10768 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10769 && DECL_INITIAL (d) == NULL_TREE)
10770 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10771 abort ();
10772 /* Reject all external templates except inline functions. */
10773 else if (DECL_INTERFACE_KNOWN (d)
10774 && ! DECL_NOT_REALLY_EXTERN (d)
10775 && ! (TREE_CODE (d) == FUNCTION_DECL
10776 && DECL_INLINE (d)))
10777 goto out;
10778 /* Defer all other templates, unless we have been explicitly
10779 forbidden from doing so. We restore the source position here
10780 because it's used by add_pending_template. */
10781 else if (! pattern_defined || defer_ok)
10783 lineno = line;
10784 input_filename = file;
10786 if (at_eof && !pattern_defined
10787 && DECL_EXPLICIT_INSTANTIATION (d))
10788 /* [temp.explicit]
10790 The definition of a non-exported function template, a
10791 non-exported member function template, or a non-exported
10792 member function or static data member of a class template
10793 shall be present in every translation unit in which it is
10794 explicitly instantiated. */
10795 pedwarn
10796 ("explicit instantiation of `%D' but no definition available", d);
10798 add_pending_template (d);
10799 goto out;
10802 need_push = !global_bindings_p ();
10803 if (need_push)
10804 push_to_top_level ();
10806 /* We're now committed to instantiating this template. Mark it as
10807 instantiated so that recursive calls to instantiate_decl do not
10808 try to instantiate it again. */
10809 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10811 /* Regenerate the declaration in case the template has been modified
10812 by a subsequent redeclaration. */
10813 regenerate_decl_from_template (d, td);
10815 /* We already set the file and line above. Reset them now in case
10816 they changed as a result of calling regenerate_decl_from_template. */
10817 lineno = DECL_SOURCE_LINE (d);
10818 input_filename = DECL_SOURCE_FILE (d);
10820 if (TREE_CODE (d) == VAR_DECL)
10822 DECL_IN_AGGR_P (d) = 0;
10823 if (DECL_INTERFACE_KNOWN (d))
10824 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10825 else
10827 DECL_EXTERNAL (d) = 1;
10828 DECL_NOT_REALLY_EXTERN (d) = 1;
10830 cp_finish_decl (d,
10831 (!DECL_INITIALIZED_IN_CLASS_P (d)
10832 ? DECL_INITIAL (d) : NULL_TREE),
10833 NULL_TREE, 0);
10835 else if (TREE_CODE (d) == FUNCTION_DECL)
10837 htab_t saved_local_specializations;
10838 tree subst_decl;
10839 tree tmpl_parm;
10840 tree spec_parm;
10842 /* Save away the current list, in case we are instantiating one
10843 template from within the body of another. */
10844 saved_local_specializations = local_specializations;
10846 /* Set up the list of local specializations. */
10847 local_specializations = htab_create (37,
10848 hash_local_specialization,
10849 eq_local_specializations,
10850 NULL);
10852 /* Set up context. */
10853 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10855 /* Create substitution entries for the parameters. */
10856 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
10857 tmpl_parm = DECL_ARGUMENTS (subst_decl);
10858 spec_parm = DECL_ARGUMENTS (d);
10859 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
10861 register_local_specialization (spec_parm, tmpl_parm);
10862 spec_parm = skip_artificial_parms_for (d, spec_parm);
10863 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
10865 while (tmpl_parm)
10867 register_local_specialization (spec_parm, tmpl_parm);
10868 tmpl_parm = TREE_CHAIN (tmpl_parm);
10869 spec_parm = TREE_CHAIN (spec_parm);
10871 my_friendly_assert (!spec_parm, 20020813);
10873 /* Substitute into the body of the function. */
10874 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10875 tf_error | tf_warning, tmpl);
10877 /* We don't need the local specializations any more. */
10878 htab_delete (local_specializations);
10879 local_specializations = saved_local_specializations;
10881 /* Finish the function. */
10882 expand_body (finish_function (0));
10885 /* We're not deferring instantiation any more. */
10886 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10888 if (need_push)
10889 pop_from_top_level ();
10891 out:
10892 lineno = line;
10893 input_filename = file;
10895 pop_tinst_level ();
10897 timevar_pop (TV_PARSE);
10899 return d;
10902 /* Run through the list of templates that we wish we could
10903 instantiate, and instantiate any we can. */
10906 instantiate_pending_templates ()
10908 tree *t;
10909 tree last = NULL_TREE;
10910 int instantiated_something = 0;
10911 int reconsider;
10915 reconsider = 0;
10917 t = &pending_templates;
10918 while (*t)
10920 tree instantiation = TREE_VALUE (*t);
10922 reopen_tinst_level (TREE_PURPOSE (*t));
10924 if (TYPE_P (instantiation))
10926 tree fn;
10928 if (!COMPLETE_TYPE_P (instantiation))
10930 instantiate_class_template (instantiation);
10931 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10932 for (fn = TYPE_METHODS (instantiation);
10934 fn = TREE_CHAIN (fn))
10935 if (! DECL_ARTIFICIAL (fn))
10936 instantiate_decl (fn, /*defer_ok=*/0);
10937 if (COMPLETE_TYPE_P (instantiation))
10939 instantiated_something = 1;
10940 reconsider = 1;
10944 if (COMPLETE_TYPE_P (instantiation))
10945 /* If INSTANTIATION has been instantiated, then we don't
10946 need to consider it again in the future. */
10947 *t = TREE_CHAIN (*t);
10948 else
10950 last = *t;
10951 t = &TREE_CHAIN (*t);
10954 else
10956 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10957 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10959 instantiation = instantiate_decl (instantiation,
10960 /*defer_ok=*/0);
10961 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10963 instantiated_something = 1;
10964 reconsider = 1;
10968 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10969 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10970 /* If INSTANTIATION has been instantiated, then we don't
10971 need to consider it again in the future. */
10972 *t = TREE_CHAIN (*t);
10973 else
10975 last = *t;
10976 t = &TREE_CHAIN (*t);
10979 tinst_depth = 0;
10980 current_tinst_level = NULL_TREE;
10982 last_pending_template = last;
10984 while (reconsider);
10986 return instantiated_something;
10989 /* Substitute ARGVEC into T, which is a list of initializers for
10990 either base class or a non-static data member. The TREE_PURPOSEs
10991 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10992 instantiate_decl. */
10994 static tree
10995 tsubst_initializer_list (t, argvec)
10996 tree t, argvec;
10998 tree inits = NULL_TREE;
11000 for (; t; t = TREE_CHAIN (t))
11002 tree decl;
11003 tree init;
11004 tree val;
11006 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11007 NULL_TREE);
11008 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11009 NULL_TREE);
11011 if (!init)
11013 else if (TREE_CODE (init) == TREE_LIST)
11014 for (val = init; val; val = TREE_CHAIN (val))
11015 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11016 else if (init != void_type_node)
11017 init = convert_from_reference (init);
11019 init = expand_member_init (decl, init);
11020 if (init)
11022 TREE_CHAIN (init) = inits;
11023 inits = init;
11026 return inits;
11029 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11031 static void
11032 set_current_access_from_decl (decl)
11033 tree decl;
11035 if (TREE_PRIVATE (decl))
11036 current_access_specifier = access_private_node;
11037 else if (TREE_PROTECTED (decl))
11038 current_access_specifier = access_protected_node;
11039 else
11040 current_access_specifier = access_public_node;
11043 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11044 is the instantiation (which should have been created with
11045 start_enum) and ARGS are the template arguments to use. */
11047 static void
11048 tsubst_enum (tag, newtag, args)
11049 tree tag;
11050 tree newtag;
11051 tree args;
11053 tree e;
11055 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11057 tree value;
11059 /* Note that in a template enum, the TREE_VALUE is the
11060 CONST_DECL, not the corresponding INTEGER_CST. */
11061 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
11062 args, tf_error | tf_warning,
11063 NULL_TREE);
11065 /* Give this enumeration constant the correct access. */
11066 set_current_access_from_decl (TREE_VALUE (e));
11068 /* Actually build the enumerator itself. */
11069 build_enumerator (TREE_PURPOSE (e), value, newtag);
11072 finish_enum (newtag);
11073 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11074 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11077 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11078 its type -- but without substituting the innermost set of template
11079 arguments. So, innermost set of template parameters will appear in
11080 the type. */
11082 tree
11083 get_mostly_instantiated_function_type (decl)
11084 tree decl;
11086 tree fn_type;
11087 tree tmpl;
11088 tree targs;
11089 tree tparms;
11090 int parm_depth;
11092 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11093 targs = DECL_TI_ARGS (decl);
11094 tparms = DECL_TEMPLATE_PARMS (tmpl);
11095 parm_depth = TMPL_PARMS_DEPTH (tparms);
11097 /* There should be as many levels of arguments as there are levels
11098 of parameters. */
11099 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
11101 fn_type = TREE_TYPE (tmpl);
11103 if (parm_depth == 1)
11104 /* No substitution is necessary. */
11106 else
11108 int i;
11109 tree partial_args;
11111 /* Replace the innermost level of the TARGS with NULL_TREEs to
11112 let tsubst know not to substitute for those parameters. */
11113 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11114 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11115 SET_TMPL_ARGS_LEVEL (partial_args, i,
11116 TMPL_ARGS_LEVEL (targs, i));
11117 SET_TMPL_ARGS_LEVEL (partial_args,
11118 TMPL_ARGS_DEPTH (targs),
11119 make_tree_vec (DECL_NTPARMS (tmpl)));
11121 /* Make sure that we can see identifiers, and compute access
11122 correctly. We can just use the context of DECL for the
11123 partial substitution here. It depends only on outer template
11124 parameters, regardless of whether the innermost level is
11125 specialized or not. */
11126 push_access_scope (decl);
11128 /* Now, do the (partial) substitution to figure out the
11129 appropriate function type. */
11130 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11132 /* Substitute into the template parameters to obtain the real
11133 innermost set of parameters. This step is important if the
11134 innermost set of template parameters contains value
11135 parameters whose types depend on outer template parameters. */
11136 TREE_VEC_LENGTH (partial_args)--;
11137 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11139 pop_access_scope (decl);
11142 return fn_type;
11145 /* Return truthvalue if we're processing a template different from
11146 the last one involved in diagnostics. */
11148 problematic_instantiation_changed ()
11150 return last_template_error_tick != tinst_level_tick;
11153 /* Remember current template involved in diagnostics. */
11154 void
11155 record_last_problematic_instantiation ()
11157 last_template_error_tick = tinst_level_tick;
11160 tree
11161 current_instantiation ()
11163 return current_tinst_level;
11166 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11167 type. Return zero for ok, nonzero for disallowed. Issue error and
11168 warning messages under control of COMPLAIN. */
11170 static int
11171 invalid_nontype_parm_type_p (type, complain)
11172 tree type;
11173 tsubst_flags_t complain;
11175 if (INTEGRAL_TYPE_P (type))
11176 return 0;
11177 else if (POINTER_TYPE_P (type))
11178 return 0;
11179 else if (TYPE_PTRMEM_P (type))
11180 return 0;
11181 else if (TYPE_PTRMEMFUNC_P (type))
11182 return 0;
11183 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11184 return 0;
11185 else if (TREE_CODE (type) == TYPENAME_TYPE)
11186 return 0;
11188 if (complain & tf_error)
11189 error ("`%#T' is not a valid type for a template constant parameter",
11190 type);
11191 return 1;
11194 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11195 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11197 static bool
11198 dependent_type_p_r (tree type)
11200 tree scope;
11202 /* [temp.dep.type]
11204 A type is dependent if it is:
11206 -- a template parameter. */
11207 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11208 return true;
11209 /* -- a qualified-id with a nested-name-specifier which contains a
11210 class-name that names a dependent type or whose unqualified-id
11211 names a dependent type. */
11212 if (TREE_CODE (type) == TYPENAME_TYPE)
11213 return true;
11214 /* -- a cv-qualified type where the cv-unqualified type is
11215 dependent. */
11216 type = TYPE_MAIN_VARIANT (type);
11217 /* -- a compound type constructed from any dependent type. */
11218 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
11219 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11220 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11221 (type)));
11222 else if (TREE_CODE (type) == POINTER_TYPE
11223 || TREE_CODE (type) == REFERENCE_TYPE)
11224 return dependent_type_p (TREE_TYPE (type));
11225 else if (TREE_CODE (type) == FUNCTION_TYPE
11226 || TREE_CODE (type) == METHOD_TYPE)
11228 tree arg_type;
11230 if (dependent_type_p (TREE_TYPE (type)))
11231 return true;
11232 for (arg_type = TYPE_ARG_TYPES (type);
11233 arg_type;
11234 arg_type = TREE_CHAIN (arg_type))
11235 if (dependent_type_p (TREE_VALUE (arg_type)))
11236 return true;
11237 return false;
11239 /* -- an array type constructed from any dependent type or whose
11240 size is specified by a constant expression that is
11241 value-dependent. */
11242 if (TREE_CODE (type) == ARRAY_TYPE)
11244 if (TYPE_DOMAIN (type)
11245 && ((value_dependent_expression_p
11246 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11247 || (type_dependent_expression_p
11248 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11249 return true;
11250 return dependent_type_p (TREE_TYPE (type));
11252 /* -- a template-id in which either the template name is a template
11253 parameter or any of the template arguments is a dependent type or
11254 an expression that is type-dependent or value-dependent.
11256 This language seems somewhat confused; for example, it does not
11257 discuss template template arguments. Therefore, we use the
11258 definition for dependent template arguments in [temp.dep.temp]. */
11259 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11260 && (dependent_template_id_p
11261 (CLASSTYPE_TI_TEMPLATE (type),
11262 CLASSTYPE_TI_ARGS (type))))
11263 return true;
11264 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11265 return true;
11266 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11267 expression is not type-dependent, then it should already been
11268 have resolved. */
11269 if (TREE_CODE (type) == TYPEOF_TYPE)
11270 return true;
11271 /* The standard does not specifically mention types that are local
11272 to template functions or local classes, but they should be
11273 considered dependent too. For example:
11275 template <int I> void f() {
11276 enum E { a = I };
11277 S<sizeof (E)> s;
11280 The size of `E' cannot be known until the value of `I' has been
11281 determined. Therefore, `E' must be considered dependent. */
11282 scope = TYPE_CONTEXT (type);
11283 if (scope && TYPE_P (scope))
11284 return dependent_type_p (scope);
11285 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11286 return type_dependent_expression_p (scope);
11288 /* Other types are non-dependent. */
11289 return false;
11292 /* Returns TRUE if TYPE is dependent, in the sense of
11293 [temp.dep.type]. */
11295 bool
11296 dependent_type_p (tree type)
11298 /* If there are no template parameters in scope, then there can't be
11299 any dependent types. */
11300 if (!processing_template_decl)
11301 return false;
11303 /* If the type is NULL, we have not computed a type for the entity
11304 in question; in that case, the type is dependent. */
11305 if (!type)
11306 return true;
11308 /* Erroneous types can be considered non-dependent. */
11309 if (type == error_mark_node)
11310 return false;
11312 /* If we have not already computed the appropriate value for TYPE,
11313 do so now. */
11314 if (!TYPE_DEPENDENT_P_VALID (type))
11316 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11317 TYPE_DEPENDENT_P_VALID (type) = 1;
11320 return TYPE_DEPENDENT_P (type);
11323 /* Returns TRUE if the EXPRESSION is value-dependent. */
11325 bool
11326 value_dependent_expression_p (tree expression)
11328 if (!processing_template_decl)
11329 return false;
11331 /* A name declared with a dependent type. */
11332 if (TREE_CODE (expression) == LOOKUP_EXPR
11333 || (DECL_P (expression)
11334 && dependent_type_p (TREE_TYPE (expression))))
11335 return true;
11336 /* A non-type template parameter. */
11337 if ((TREE_CODE (expression) == CONST_DECL
11338 && DECL_TEMPLATE_PARM_P (expression))
11339 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11340 return true;
11341 /* A constant with integral or enumeration type and is initialized
11342 with an expression that is value-dependent. */
11343 if (TREE_CODE (expression) == VAR_DECL
11344 && DECL_INITIAL (expression)
11345 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
11346 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
11347 && value_dependent_expression_p (DECL_INITIAL (expression)))
11348 return true;
11349 /* These expressions are value-dependent if the type to which the
11350 cast occurs is dependent. */
11351 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11352 || TREE_CODE (expression) == STATIC_CAST_EXPR
11353 || TREE_CODE (expression) == CONST_CAST_EXPR
11354 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11355 || TREE_CODE (expression) == CAST_EXPR)
11356 && dependent_type_p (TREE_TYPE (expression)))
11357 return true;
11358 /* A `sizeof' expression where the sizeof operand is a type is
11359 value-dependent if the type is dependent. If the type was not
11360 dependent, we would no longer have a SIZEOF_EXPR, so any
11361 SIZEOF_EXPR is dependent. */
11362 if (TREE_CODE (expression) == SIZEOF_EXPR)
11363 return true;
11364 /* A constant expression is value-dependent if any subexpression is
11365 value-dependent. */
11366 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
11368 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11370 case '1':
11371 return (value_dependent_expression_p
11372 (TREE_OPERAND (expression, 0)));
11373 case '<':
11374 case '2':
11375 return ((value_dependent_expression_p
11376 (TREE_OPERAND (expression, 0)))
11377 || (value_dependent_expression_p
11378 (TREE_OPERAND (expression, 1))));
11379 case 'e':
11381 int i;
11382 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11383 /* In some cases, some of the operands may be missing.
11384 (For example, in the case of PREDECREMENT_EXPR, the
11385 amount to increment by may be missing.) That doesn't
11386 make the expression dependent. */
11387 if (TREE_OPERAND (expression, i)
11388 && (value_dependent_expression_p
11389 (TREE_OPERAND (expression, i))))
11390 return true;
11391 return false;
11396 /* The expression is not value-dependent. */
11397 return false;
11400 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11401 [temp.dep.expr]. */
11403 bool
11404 type_dependent_expression_p (expression)
11405 tree expression;
11407 if (!processing_template_decl)
11408 return false;
11410 /* Some expression forms are never type-dependent. */
11411 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11412 || TREE_CODE (expression) == SIZEOF_EXPR
11413 || TREE_CODE (expression) == ALIGNOF_EXPR
11414 || TREE_CODE (expression) == TYPEID_EXPR
11415 || TREE_CODE (expression) == DELETE_EXPR
11416 || TREE_CODE (expression) == VEC_DELETE_EXPR
11417 || TREE_CODE (expression) == THROW_EXPR)
11418 return false;
11420 /* The types of these expressions depends only on the type to which
11421 the cast occurs. */
11422 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11423 || TREE_CODE (expression) == STATIC_CAST_EXPR
11424 || TREE_CODE (expression) == CONST_CAST_EXPR
11425 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11426 || TREE_CODE (expression) == CAST_EXPR)
11427 return dependent_type_p (TREE_TYPE (expression));
11428 /* The types of these expressions depends only on the type created
11429 by the expression. */
11430 else if (TREE_CODE (expression) == NEW_EXPR
11431 || TREE_CODE (expression) == VEC_NEW_EXPR)
11432 return dependent_type_p (TREE_OPERAND (expression, 1));
11434 if (TREE_CODE (expression) == FUNCTION_DECL
11435 && DECL_LANG_SPECIFIC (expression)
11436 && DECL_TEMPLATE_INFO (expression)
11437 && (dependent_template_id_p
11438 (DECL_TI_TEMPLATE (expression),
11439 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11440 return true;
11442 return (dependent_type_p (TREE_TYPE (expression)));
11445 /* Returns TRUE if the ARG (a template argument) is dependent. */
11447 bool
11448 dependent_template_arg_p (tree arg)
11450 if (!processing_template_decl)
11451 return false;
11453 if (TREE_CODE (arg) == TEMPLATE_DECL
11454 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11455 return dependent_template_p (arg);
11456 else if (TYPE_P (arg))
11457 return dependent_type_p (arg);
11458 else
11459 return (type_dependent_expression_p (arg)
11460 || value_dependent_expression_p (arg));
11463 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11465 static bool
11466 dependent_template_id_p (tree tmpl, tree args)
11468 int i;
11470 if (dependent_template_p (tmpl))
11471 return true;
11472 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
11473 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
11474 return true;
11475 return false;
11478 /* Returns TRUE if the template TMPL is dependent. */
11480 bool
11481 dependent_template_p (tree tmpl)
11483 /* Template template parameters are dependent. */
11484 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
11485 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
11486 return true;
11487 /* So are member templates of dependent classes. */
11488 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
11489 return dependent_type_p (DECL_CONTEXT (tmpl));
11490 return false;
11493 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11494 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11495 can be found. Note that this function peers inside uninstantiated
11496 templates and therefore should be used only in extremely limited
11497 situations. */
11499 tree
11500 resolve_typename_type (tree type, bool only_current_p)
11502 tree scope;
11503 tree name;
11504 tree decl;
11505 int quals;
11507 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
11508 20010702);
11510 scope = TYPE_CONTEXT (type);
11511 name = TYPE_IDENTIFIER (type);
11513 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11514 it first before we can figure out what NAME refers to. */
11515 if (TREE_CODE (scope) == TYPENAME_TYPE)
11516 scope = resolve_typename_type (scope, only_current_p);
11517 /* If we don't know what SCOPE refers to, then we cannot resolve the
11518 TYPENAME_TYPE. */
11519 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
11520 return error_mark_node;
11521 /* If the SCOPE is a template type parameter, we have no way of
11522 resolving the name. */
11523 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
11524 return type;
11525 /* If the SCOPE is not the current instantiation, there's no reason
11526 to look inside it. */
11527 if (only_current_p && !currently_open_class (scope))
11528 return error_mark_node;
11529 /* Enter the SCOPE so that name lookup will be resolved as if we
11530 were in the class definition. In particular, SCOPE will no
11531 longer be considered a dependent type. */
11532 push_scope (scope);
11533 /* Look up the declaration. */
11534 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
11535 /* Obtain the set of qualifiers applied to the TYPE. */
11536 quals = cp_type_quals (type);
11537 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11538 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11539 if (!decl)
11540 type = error_mark_node;
11541 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
11542 && TREE_CODE (decl) == TYPE_DECL)
11543 type = TREE_TYPE (decl);
11544 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
11545 && DECL_CLASS_TEMPLATE_P (decl))
11547 tree tmpl;
11548 tree args;
11549 /* Obtain the template and the arguments. */
11550 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
11551 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
11552 /* Instantiate the template. */
11553 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
11554 /*entering_scope=*/0,
11555 tf_error);
11557 else
11558 type = error_mark_node;
11559 /* Qualify the resulting type. */
11560 if (type != error_mark_node && quals)
11561 type = cp_build_qualified_type (type, quals);
11562 /* Leave the SCOPE. */
11563 pop_scope (scope);
11565 return type;
11568 tree
11569 resolve_typename_type_in_current_instantiation (tree type)
11571 tree t;
11573 t = resolve_typename_type (type, /*only_current_p=*/true);
11574 return (t != error_mark_node) ? t : type;
11577 #include "gt-cp-pt.h"