* predict.c (estimate_bb_frequencies): Correctly set
[official-gcc.git] / gcc / cp / pt.c
blobac5e4e1c8ef37a04194005cad7ab81be24d404be
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)
5155 || COMPLETE_TYPE_P (type)
5156 || dependent_type_p (type))
5157 return type;
5159 /* Figure out which template is being instantiated. */
5160 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5161 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
5163 /* Figure out which arguments are being used to do the
5164 instantiation. */
5165 args = CLASSTYPE_TI_ARGS (type);
5167 /* Determine what specialization of the original template to
5168 instantiate. */
5169 t = most_specialized_class (template, args);
5170 if (t == error_mark_node)
5172 const char *str = "candidates are:";
5173 error ("ambiguous class template instantiation for `%#T'", type);
5174 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5175 t = TREE_CHAIN (t))
5177 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5178 args))
5180 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5181 str = " ";
5184 TYPE_BEING_DEFINED (type) = 1;
5185 return error_mark_node;
5188 if (t)
5189 pattern = TREE_TYPE (t);
5190 else
5191 pattern = TREE_TYPE (template);
5193 /* If the template we're instantiating is incomplete, then clearly
5194 there's nothing we can do. */
5195 if (!COMPLETE_TYPE_P (pattern))
5196 return type;
5198 /* If we've recursively instantiated too many templates, stop. */
5199 if (! push_tinst_level (type))
5200 return type;
5202 /* Now we're really doing the instantiation. Mark the type as in
5203 the process of being defined. */
5204 TYPE_BEING_DEFINED (type) = 1;
5206 maybe_push_to_top_level (uses_template_parms (type));
5208 if (t)
5210 /* This TYPE is actually an instantiation of a partial
5211 specialization. We replace the innermost set of ARGS with
5212 the arguments appropriate for substitution. For example,
5213 given:
5215 template <class T> struct S {};
5216 template <class T> struct S<T*> {};
5218 and supposing that we are instantiating S<int*>, ARGS will
5219 present be {int*} but we need {int}. */
5220 tree inner_args
5221 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5222 args);
5224 /* If there were multiple levels in ARGS, replacing the
5225 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5226 want, so we make a copy first. */
5227 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5229 args = copy_node (args);
5230 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5232 else
5233 args = inner_args;
5236 if (flag_external_templates)
5238 if (flag_alt_external_templates)
5240 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5241 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5243 else
5245 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5246 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5247 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5250 else
5252 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5255 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5256 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5257 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5258 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5259 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5260 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5261 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5262 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5263 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5264 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5265 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5266 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5267 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5268 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5269 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5270 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5271 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5272 TYPE_USES_MULTIPLE_INHERITANCE (type)
5273 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5274 TYPE_USES_VIRTUAL_BASECLASSES (type)
5275 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5276 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5277 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5278 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5279 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5280 if (ANON_AGGR_TYPE_P (pattern))
5281 SET_ANON_AGGR_TYPE_P (type);
5283 pbinfo = TYPE_BINFO (pattern);
5285 if (BINFO_BASETYPES (pbinfo))
5287 tree base_list = NULL_TREE;
5288 tree pbases = BINFO_BASETYPES (pbinfo);
5289 tree paccesses = BINFO_BASEACCESSES (pbinfo);
5290 int i;
5292 /* Substitute into each of the bases to determine the actual
5293 basetypes. */
5294 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5296 tree base;
5297 tree access;
5298 tree pbase;
5300 pbase = TREE_VEC_ELT (pbases, i);
5301 access = TREE_VEC_ELT (paccesses, i);
5303 /* Substitute to figure out the base class. */
5304 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5305 if (base == error_mark_node)
5306 continue;
5308 base_list = tree_cons (access, base, base_list);
5309 TREE_VIA_VIRTUAL (base_list) = TREE_VIA_VIRTUAL (pbase);
5312 /* The list is now in reverse order; correct that. */
5313 base_list = nreverse (base_list);
5315 /* Now call xref_basetypes to set up all the base-class
5316 information. */
5317 xref_basetypes (type, base_list);
5320 /* Now that our base classes are set up, enter the scope of the
5321 class, so that name lookups into base classes, etc. will work
5322 correctly. This is precisely analogous to what we do in
5323 begin_class_definition when defining an ordinary non-template
5324 class. */
5325 pushclass (type, true);
5327 /* Now members are processed in the order of declaration. */
5328 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
5330 tree t = TREE_VALUE (member);
5332 if (TREE_PURPOSE (member))
5334 if (TYPE_P (t))
5336 /* Build new CLASSTYPE_TAGS. */
5338 tree tag = t;
5339 tree name = TYPE_IDENTIFIER (tag);
5340 tree newtag;
5342 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5343 my_friendly_assert (newtag != error_mark_node, 20010206);
5344 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5346 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5347 /* Unfortunately, lookup_template_class sets
5348 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5349 instantiation (i.e., for the type of a member template
5350 class nested within a template class.) This behavior is
5351 required for maybe_process_partial_specialization to work
5352 correctly, but is not accurate in this case; the TAG is not
5353 an instantiation of anything. (The corresponding
5354 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5355 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5357 /* Now, we call pushtag to put this NEWTAG into the scope of
5358 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5359 pushtag calling push_template_decl. We don't have to do
5360 this for enums because it will already have been done in
5361 tsubst_enum. */
5362 if (name)
5363 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5364 pushtag (name, newtag, /*globalize=*/0);
5367 else if (TREE_CODE (t) == FUNCTION_DECL
5368 || DECL_FUNCTION_TEMPLATE_P (t))
5370 /* Build new TYPE_METHODS. */
5372 tree r = tsubst (t, args, tf_error, NULL_TREE);
5373 set_current_access_from_decl (r);
5374 grok_special_member_properties (r);
5375 finish_member_declaration (r);
5377 else
5379 /* Build new TYPE_FIELDS. */
5381 if (TREE_CODE (t) != CONST_DECL)
5383 tree r;
5385 /* The the file and line for this declaration, to assist
5386 in error message reporting. Since we called
5387 push_tinst_level above, we don't need to restore these. */
5388 lineno = DECL_SOURCE_LINE (t);
5389 input_filename = DECL_SOURCE_FILE (t);
5391 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5392 if (TREE_CODE (r) == VAR_DECL)
5394 tree init;
5396 if (DECL_INITIALIZED_IN_CLASS_P (r))
5397 init = tsubst_expr (DECL_INITIAL (t), args,
5398 tf_error | tf_warning, NULL_TREE);
5399 else
5400 init = NULL_TREE;
5402 finish_static_data_member_decl (r, init,
5403 /*asmspec_tree=*/NULL_TREE,
5404 /*flags=*/0);
5406 if (DECL_INITIALIZED_IN_CLASS_P (r))
5407 check_static_variable_definition (r, TREE_TYPE (r));
5409 else if (TREE_CODE (r) == FIELD_DECL)
5411 /* Determine whether R has a valid type and can be
5412 completed later. If R is invalid, then it is
5413 replaced by error_mark_node so that it will not be
5414 added to TYPE_FIELDS. */
5415 tree rtype = TREE_TYPE (r);
5416 if (can_complete_type_without_circularity (rtype))
5417 complete_type (rtype);
5419 if (!COMPLETE_TYPE_P (rtype))
5421 cxx_incomplete_type_error (r, rtype);
5422 r = error_mark_node;
5426 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5427 such a thing will already have been added to the field
5428 list by tsubst_enum in finish_member_declaration in the
5429 CLASSTYPE_TAGS case above. */
5430 if (!(TREE_CODE (r) == TYPE_DECL
5431 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5432 && TYPE_CONTEXT (TREE_TYPE (r)) == type))
5434 set_current_access_from_decl (r);
5435 finish_member_declaration (r);
5440 else
5442 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5444 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5446 tree friend_type = t;
5447 tree new_friend_type;
5449 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5450 new_friend_type = tsubst_friend_class (friend_type, args);
5451 else if (uses_template_parms (friend_type))
5452 new_friend_type = tsubst (friend_type, args,
5453 tf_error | tf_warning, NULL_TREE);
5454 else
5456 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5458 /* The call to xref_tag_from_type does injection for friend
5459 classes. */
5460 push_nested_namespace (ns);
5461 new_friend_type =
5462 xref_tag_from_type (friend_type, NULL_TREE, 1);
5463 pop_nested_namespace (ns);
5466 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5467 /* Trick make_friend_class into realizing that the friend
5468 we're adding is a template, not an ordinary class. It's
5469 important that we use make_friend_class since it will
5470 perform some error-checking and output cross-reference
5471 information. */
5472 ++processing_template_decl;
5474 if (new_friend_type != error_mark_node)
5475 make_friend_class (type, new_friend_type);
5477 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5478 --processing_template_decl;
5480 else
5482 /* Build new DECL_FRIENDLIST. */
5484 add_friend (type,
5485 tsubst_friend_function (t, args));
5490 /* Set the file and line number information to whatever is given for
5491 the class itself. This puts error messages involving generated
5492 implicit functions at a predictable point, and the same point
5493 that would be used for non-template classes. */
5494 typedecl = TYPE_MAIN_DECL (type);
5495 lineno = DECL_SOURCE_LINE (typedecl);
5496 input_filename = DECL_SOURCE_FILE (typedecl);
5498 unreverse_member_declarations (type);
5499 finish_struct_1 (type);
5500 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5502 /* Clear this now so repo_template_used is happy. */
5503 TYPE_BEING_DEFINED (type) = 0;
5504 repo_template_used (type);
5506 /* Now that the class is complete, instantiate default arguments for
5507 any member functions. We don't do this earlier because the
5508 default arguments may reference members of the class. */
5509 if (!PRIMARY_TEMPLATE_P (template))
5510 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5511 if (TREE_CODE (t) == FUNCTION_DECL
5512 /* Implicitly generated member functions will not have template
5513 information; they are not instantiations, but instead are
5514 created "fresh" for each instantiation. */
5515 && DECL_TEMPLATE_INFO (t))
5516 tsubst_default_arguments (t);
5518 popclass ();
5519 pop_from_top_level ();
5520 pop_tinst_level ();
5522 if (TYPE_CONTAINS_VPTR_P (type))
5523 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5525 return type;
5528 static int
5529 list_eq (t1, t2)
5530 tree t1, t2;
5532 if (t1 == NULL_TREE)
5533 return t2 == NULL_TREE;
5534 if (t2 == NULL_TREE)
5535 return 0;
5536 /* Don't care if one declares its arg const and the other doesn't -- the
5537 main variant of the arg type is all that matters. */
5538 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5539 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5540 return 0;
5541 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5544 /* If arg is a non-type template parameter that does not depend on template
5545 arguments, fold it like we weren't in the body of a template. */
5547 static tree
5548 maybe_fold_nontype_arg (arg)
5549 tree arg;
5551 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5553 /* Sometimes, one of the args was an expression involving a
5554 template constant parameter, like N - 1. Now that we've
5555 tsubst'd, we might have something like 2 - 1. This will
5556 confuse lookup_template_class, so we do constant folding
5557 here. We have to unset processing_template_decl, to
5558 fool build_expr_from_tree() into building an actual
5559 tree. */
5561 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5562 as simple as it's going to get, and trying to reprocess
5563 the trees will break. */
5564 if (!TREE_TYPE (arg))
5566 int saved_processing_template_decl = processing_template_decl;
5567 processing_template_decl = 0;
5568 arg = build_expr_from_tree (arg);
5569 processing_template_decl = saved_processing_template_decl;
5572 arg = fold (arg);
5574 return arg;
5577 /* Substitute ARGS into the vector of template arguments T. */
5579 static tree
5580 tsubst_template_arg_vector (t, args, complain)
5581 tree t;
5582 tree args;
5583 tsubst_flags_t complain;
5585 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5586 tree *elts = (tree *) alloca (len * sizeof (tree));
5588 memset ((char *) elts, 0, len * sizeof (tree));
5590 for (i = 0; i < len; i++)
5592 if (TREE_VEC_ELT (t, i) != NULL_TREE
5593 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5594 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5595 args, complain);
5596 else
5597 elts[i] = maybe_fold_nontype_arg
5598 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5599 NULL_TREE));
5601 if (elts[i] == error_mark_node)
5602 return error_mark_node;
5604 if (elts[i] != TREE_VEC_ELT (t, i))
5605 need_new = 1;
5608 if (!need_new)
5609 return t;
5611 t = make_tree_vec (len);
5612 for (i = 0; i < len; i++)
5613 TREE_VEC_ELT (t, i) = elts[i];
5615 return t;
5618 /* Return the result of substituting ARGS into the template parameters
5619 given by PARMS. If there are m levels of ARGS and m + n levels of
5620 PARMS, then the result will contain n levels of PARMS. For
5621 example, if PARMS is `template <class T> template <class U>
5622 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5623 result will be `template <int*, double, class V>'. */
5625 static tree
5626 tsubst_template_parms (parms, args, complain)
5627 tree parms;
5628 tree args;
5629 tsubst_flags_t complain;
5631 tree r = NULL_TREE;
5632 tree* new_parms;
5634 for (new_parms = &r;
5635 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5636 new_parms = &(TREE_CHAIN (*new_parms)),
5637 parms = TREE_CHAIN (parms))
5639 tree new_vec =
5640 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5641 int i;
5643 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5645 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5646 tree default_value = TREE_PURPOSE (tuple);
5647 tree parm_decl = TREE_VALUE (tuple);
5649 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5650 default_value = tsubst_expr (default_value, args,
5651 complain, NULL_TREE);
5652 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5653 parm_decl);
5654 TREE_VEC_ELT (new_vec, i) = tuple;
5657 *new_parms =
5658 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5659 - TMPL_ARGS_DEPTH (args)),
5660 new_vec, NULL_TREE);
5663 return r;
5666 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5667 type T. If T is not an aggregate or enumeration type, it is
5668 handled as if by tsubst. IN_DECL is as for tsubst. If
5669 ENTERING_SCOPE is nonzero, T is the context for a template which
5670 we are presently tsubst'ing. Return the substituted value. */
5672 static tree
5673 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5674 tree t;
5675 tree args;
5676 tsubst_flags_t complain;
5677 tree in_decl;
5678 int entering_scope;
5680 if (t == NULL_TREE)
5681 return NULL_TREE;
5683 switch (TREE_CODE (t))
5685 case RECORD_TYPE:
5686 if (TYPE_PTRMEMFUNC_P (t))
5687 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5689 /* else fall through */
5690 case ENUMERAL_TYPE:
5691 case UNION_TYPE:
5692 if (TYPE_TEMPLATE_INFO (t))
5694 tree argvec;
5695 tree context;
5696 tree r;
5698 /* First, determine the context for the type we are looking
5699 up. */
5700 if (TYPE_CONTEXT (t) != NULL_TREE)
5701 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5702 complain,
5703 in_decl, /*entering_scope=*/1);
5704 else
5705 context = NULL_TREE;
5707 /* Then, figure out what arguments are appropriate for the
5708 type we are trying to find. For example, given:
5710 template <class T> struct S;
5711 template <class T, class U> void f(T, U) { S<U> su; }
5713 and supposing that we are instantiating f<int, double>,
5714 then our ARGS will be {int, double}, but, when looking up
5715 S we only want {double}. */
5716 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5717 complain);
5718 if (argvec == error_mark_node)
5719 return error_mark_node;
5721 r = lookup_template_class (t, argvec, in_decl, context,
5722 entering_scope, complain);
5724 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5726 else
5727 /* This is not a template type, so there's nothing to do. */
5728 return t;
5730 default:
5731 return tsubst (t, args, complain, in_decl);
5735 /* Substitute into the default argument ARG (a default argument for
5736 FN), which has the indicated TYPE. */
5738 tree
5739 tsubst_default_argument (fn, type, arg)
5740 tree fn;
5741 tree type;
5742 tree arg;
5744 /* This default argument came from a template. Instantiate the
5745 default argument here, not in tsubst. In the case of
5746 something like:
5748 template <class T>
5749 struct S {
5750 static T t();
5751 void f(T = t());
5754 we must be careful to do name lookup in the scope of S<T>,
5755 rather than in the current class.
5757 ??? current_class_type affects a lot more than name lookup. This is
5758 very fragile. Fortunately, it will go away when we do 2-phase name
5759 binding properly. */
5761 /* FN is already the desired FUNCTION_DECL. */
5762 push_access_scope (fn);
5764 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5765 tf_error | tf_warning, NULL_TREE);
5767 pop_access_scope (fn);
5769 /* Make sure the default argument is reasonable. */
5770 arg = check_default_argument (type, arg);
5772 return arg;
5775 /* Substitute into all the default arguments for FN. */
5777 static void
5778 tsubst_default_arguments (fn)
5779 tree fn;
5781 tree arg;
5782 tree tmpl_args;
5784 tmpl_args = DECL_TI_ARGS (fn);
5786 /* If this function is not yet instantiated, we certainly don't need
5787 its default arguments. */
5788 if (uses_template_parms (tmpl_args))
5789 return;
5791 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5792 arg;
5793 arg = TREE_CHAIN (arg))
5794 if (TREE_PURPOSE (arg))
5795 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5796 TREE_VALUE (arg),
5797 TREE_PURPOSE (arg));
5800 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5801 (already computed) substitution of ARGS into TREE_TYPE (T), if
5802 appropriate. Return the result of the substitution. Issue error
5803 and warning messages under control of COMPLAIN. */
5805 static tree
5806 tsubst_decl (t, args, type, complain)
5807 tree t;
5808 tree args;
5809 tree type;
5810 tsubst_flags_t complain;
5812 int saved_lineno;
5813 const char *saved_filename;
5814 tree r = NULL_TREE;
5815 tree in_decl = t;
5817 /* Set the filename and linenumber to improve error-reporting. */
5818 saved_lineno = lineno;
5819 saved_filename = input_filename;
5820 lineno = DECL_SOURCE_LINE (t);
5821 input_filename = DECL_SOURCE_FILE (t);
5823 switch (TREE_CODE (t))
5825 case TEMPLATE_DECL:
5827 /* We can get here when processing a member template function
5828 of a template class. */
5829 tree decl = DECL_TEMPLATE_RESULT (t);
5830 tree spec;
5831 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5833 if (!is_template_template_parm)
5835 /* We might already have an instance of this template.
5836 The ARGS are for the surrounding class type, so the
5837 full args contain the tsubst'd args for the context,
5838 plus the innermost args from the template decl. */
5839 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5840 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5841 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5842 tree full_args;
5844 full_args = tsubst_template_arg_vector (tmpl_args, args,
5845 complain);
5847 /* tsubst_template_arg_vector doesn't copy the vector if
5848 nothing changed. But, *something* should have
5849 changed. */
5850 my_friendly_assert (full_args != tmpl_args, 0);
5852 spec = retrieve_specialization (t, full_args);
5853 if (spec != NULL_TREE)
5855 r = spec;
5856 break;
5860 /* Make a new template decl. It will be similar to the
5861 original, but will record the current template arguments.
5862 We also create a new function declaration, which is just
5863 like the old one, but points to this new template, rather
5864 than the old one. */
5865 r = copy_decl (t);
5866 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5867 TREE_CHAIN (r) = NULL_TREE;
5869 if (is_template_template_parm)
5871 tree new_decl = tsubst (decl, args, complain, in_decl);
5872 DECL_TEMPLATE_RESULT (r) = new_decl;
5873 TREE_TYPE (r) = TREE_TYPE (new_decl);
5874 break;
5877 DECL_CONTEXT (r)
5878 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5879 complain, in_decl,
5880 /*entering_scope=*/1);
5881 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5883 if (TREE_CODE (decl) == TYPE_DECL)
5885 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5886 TREE_TYPE (r) = new_type;
5887 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5888 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5889 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5891 else
5893 tree new_decl = tsubst (decl, args, complain, in_decl);
5895 DECL_TEMPLATE_RESULT (r) = new_decl;
5896 DECL_TI_TEMPLATE (new_decl) = r;
5897 TREE_TYPE (r) = TREE_TYPE (new_decl);
5898 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5901 SET_DECL_IMPLICIT_INSTANTIATION (r);
5902 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5903 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5905 /* The template parameters for this new template are all the
5906 template parameters for the old template, except the
5907 outermost level of parameters. */
5908 DECL_TEMPLATE_PARMS (r)
5909 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5910 complain);
5912 if (PRIMARY_TEMPLATE_P (t))
5913 DECL_PRIMARY_TEMPLATE (r) = r;
5915 /* We don't partially instantiate partial specializations. */
5916 if (TREE_CODE (decl) == TYPE_DECL)
5917 break;
5919 /* Record this partial instantiation. */
5920 register_specialization (r, t,
5921 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5924 break;
5926 case FUNCTION_DECL:
5928 tree ctx;
5929 tree argvec = NULL_TREE;
5930 tree *friends;
5931 tree gen_tmpl;
5932 int member;
5933 int args_depth;
5934 int parms_depth;
5936 /* Nobody should be tsubst'ing into non-template functions. */
5937 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5939 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5941 tree spec;
5943 /* Calculate the most general template of which R is a
5944 specialization, and the complete set of arguments used to
5945 specialize R. */
5946 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5947 argvec
5948 = tsubst_template_arg_vector (DECL_TI_ARGS
5949 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5950 args, complain);
5952 /* Check to see if we already have this specialization. */
5953 spec = retrieve_specialization (gen_tmpl, argvec);
5955 if (spec)
5957 r = spec;
5958 break;
5961 /* We can see more levels of arguments than parameters if
5962 there was a specialization of a member template, like
5963 this:
5965 template <class T> struct S { template <class U> void f(); }
5966 template <> template <class U> void S<int>::f(U);
5968 Here, we'll be substituting into the specialization,
5969 because that's where we can find the code we actually
5970 want to generate, but we'll have enough arguments for
5971 the most general template.
5973 We also deal with the peculiar case:
5975 template <class T> struct S {
5976 template <class U> friend void f();
5978 template <class U> void f() {}
5979 template S<int>;
5980 template void f<double>();
5982 Here, the ARGS for the instantiation of will be {int,
5983 double}. But, we only need as many ARGS as there are
5984 levels of template parameters in CODE_PATTERN. We are
5985 careful not to get fooled into reducing the ARGS in
5986 situations like:
5988 template <class T> struct S { template <class U> void f(U); }
5989 template <class T> template <> void S<T>::f(int) {}
5991 which we can spot because the pattern will be a
5992 specialization in this case. */
5993 args_depth = TMPL_ARGS_DEPTH (args);
5994 parms_depth =
5995 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5996 if (args_depth > parms_depth
5997 && !DECL_TEMPLATE_SPECIALIZATION (t))
5998 args = get_innermost_template_args (args, parms_depth);
6000 else
6002 /* This special case arises when we have something like this:
6004 template <class T> struct S {
6005 friend void f<int>(int, double);
6008 Here, the DECL_TI_TEMPLATE for the friend declaration
6009 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
6010 being called from tsubst_friend_function, and we want
6011 only to create a new decl (R) with appropriate types so
6012 that we can call determine_specialization. */
6013 gen_tmpl = NULL_TREE;
6016 if (DECL_CLASS_SCOPE_P (t))
6018 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6019 member = 2;
6020 else
6021 member = 1;
6022 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6023 complain, t,
6024 /*entering_scope=*/1);
6026 else
6028 member = 0;
6029 ctx = DECL_CONTEXT (t);
6031 type = tsubst (type, args, complain, in_decl);
6032 if (type == error_mark_node)
6033 return error_mark_node;
6035 /* We do NOT check for matching decls pushed separately at this
6036 point, as they may not represent instantiations of this
6037 template, and in any case are considered separate under the
6038 discrete model. */
6039 r = copy_decl (t);
6040 DECL_USE_TEMPLATE (r) = 0;
6041 TREE_TYPE (r) = type;
6042 /* Clear out the mangled name and RTL for the instantiation. */
6043 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6044 SET_DECL_RTL (r, NULL_RTX);
6046 DECL_CONTEXT (r) = ctx;
6048 if (member && DECL_CONV_FN_P (r))
6049 /* Type-conversion operator. Reconstruct the name, in
6050 case it's the name of one of the template's parameters. */
6051 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6053 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6054 complain, t);
6055 DECL_RESULT (r) = NULL_TREE;
6057 TREE_STATIC (r) = 0;
6058 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6059 DECL_EXTERNAL (r) = 1;
6060 DECL_INTERFACE_KNOWN (r) = 0;
6061 DECL_DEFER_OUTPUT (r) = 0;
6062 TREE_CHAIN (r) = NULL_TREE;
6063 DECL_PENDING_INLINE_INFO (r) = 0;
6064 DECL_PENDING_INLINE_P (r) = 0;
6065 DECL_SAVED_TREE (r) = NULL_TREE;
6066 TREE_USED (r) = 0;
6067 if (DECL_CLONED_FUNCTION (r))
6069 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6070 args, complain, t);
6071 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6072 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6075 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6076 this in the special friend case mentioned above where
6077 GEN_TMPL is NULL. */
6078 if (gen_tmpl)
6080 DECL_TEMPLATE_INFO (r)
6081 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6082 SET_DECL_IMPLICIT_INSTANTIATION (r);
6083 register_specialization (r, gen_tmpl, argvec);
6085 /* We're not supposed to instantiate default arguments
6086 until they are called, for a template. But, for a
6087 declaration like:
6089 template <class T> void f ()
6090 { extern void g(int i = T()); }
6092 we should do the substitution when the template is
6093 instantiated. We handle the member function case in
6094 instantiate_class_template since the default arguments
6095 might refer to other members of the class. */
6096 if (!member
6097 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6098 && !uses_template_parms (argvec))
6099 tsubst_default_arguments (r);
6102 /* Copy the list of befriending classes. */
6103 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6104 *friends;
6105 friends = &TREE_CHAIN (*friends))
6107 *friends = copy_node (*friends);
6108 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6109 args, complain,
6110 in_decl);
6113 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6115 maybe_retrofit_in_chrg (r);
6116 if (DECL_CONSTRUCTOR_P (r))
6117 grok_ctor_properties (ctx, r);
6118 /* If this is an instantiation of a member template, clone it.
6119 If it isn't, that'll be handled by
6120 clone_constructors_and_destructors. */
6121 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6122 clone_function_decl (r, /*update_method_vec_p=*/0);
6124 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6125 grok_op_properties (r, DECL_FRIEND_P (r));
6127 break;
6129 case PARM_DECL:
6131 r = copy_node (t);
6132 if (DECL_TEMPLATE_PARM_P (t))
6133 SET_DECL_TEMPLATE_PARM_P (r);
6135 TREE_TYPE (r) = type;
6136 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6138 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6139 DECL_INITIAL (r) = TREE_TYPE (r);
6140 else
6141 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6142 complain, in_decl);
6144 DECL_CONTEXT (r) = NULL_TREE;
6146 if (!DECL_TEMPLATE_PARM_P (r))
6147 DECL_ARG_TYPE (r) = type_passed_as (type);
6148 if (TREE_CHAIN (t))
6149 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6150 complain, TREE_CHAIN (t));
6152 break;
6154 case FIELD_DECL:
6156 r = copy_decl (t);
6157 TREE_TYPE (r) = type;
6158 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6160 /* We don't have to set DECL_CONTEXT here; it is set by
6161 finish_member_declaration. */
6162 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6163 complain, in_decl);
6164 TREE_CHAIN (r) = NULL_TREE;
6165 if (VOID_TYPE_P (type))
6166 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6168 break;
6170 case USING_DECL:
6172 r = copy_node (t);
6173 DECL_INITIAL (r)
6174 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6175 TREE_CHAIN (r) = NULL_TREE;
6177 break;
6179 case TYPE_DECL:
6180 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6181 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6183 /* If this is the canonical decl, we don't have to mess with
6184 instantiations, and often we can't (for typename, template
6185 type parms and such). Note that TYPE_NAME is not correct for
6186 the above test if we've copied the type for a typedef. */
6187 r = TYPE_NAME (type);
6188 break;
6191 /* Fall through. */
6193 case VAR_DECL:
6195 tree argvec = NULL_TREE;
6196 tree gen_tmpl = NULL_TREE;
6197 tree spec;
6198 tree tmpl = NULL_TREE;
6199 tree ctx;
6200 int local_p;
6202 /* Assume this is a non-local variable. */
6203 local_p = 0;
6205 if (TYPE_P (CP_DECL_CONTEXT (t)))
6206 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6207 complain,
6208 in_decl, /*entering_scope=*/1);
6209 else if (DECL_NAMESPACE_SCOPE_P (t))
6210 ctx = DECL_CONTEXT (t);
6211 else
6213 /* Subsequent calls to pushdecl will fill this in. */
6214 ctx = NULL_TREE;
6215 local_p = 1;
6218 /* Check to see if we already have this specialization. */
6219 if (!local_p)
6221 tmpl = DECL_TI_TEMPLATE (t);
6222 gen_tmpl = most_general_template (tmpl);
6223 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6224 spec = retrieve_specialization (gen_tmpl, argvec);
6226 else
6227 spec = retrieve_local_specialization (t);
6229 if (spec)
6231 r = spec;
6232 break;
6235 r = copy_decl (t);
6236 if (TREE_CODE (r) == VAR_DECL)
6237 type = complete_type (type);
6238 TREE_TYPE (r) = type;
6239 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6240 DECL_CONTEXT (r) = ctx;
6241 /* Clear out the mangled name and RTL for the instantiation. */
6242 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6243 SET_DECL_RTL (r, NULL_RTX);
6245 /* Don't try to expand the initializer until someone tries to use
6246 this variable; otherwise we run into circular dependencies. */
6247 DECL_INITIAL (r) = NULL_TREE;
6248 SET_DECL_RTL (r, NULL_RTX);
6249 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6251 /* Even if the original location is out of scope, the newly
6252 substituted one is not. */
6253 if (TREE_CODE (r) == VAR_DECL)
6255 DECL_DEAD_FOR_LOCAL (r) = 0;
6256 DECL_INITIALIZED_P (r) = 0;
6259 if (!local_p)
6261 /* A static data member declaration is always marked
6262 external when it is declared in-class, even if an
6263 initializer is present. We mimic the non-template
6264 processing here. */
6265 DECL_EXTERNAL (r) = 1;
6267 register_specialization (r, gen_tmpl, argvec);
6268 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6269 SET_DECL_IMPLICIT_INSTANTIATION (r);
6271 else
6272 register_local_specialization (r, t);
6274 TREE_CHAIN (r) = NULL_TREE;
6275 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6276 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6277 /* Compute the size, alignment, etc. of R. */
6278 layout_decl (r, 0);
6280 break;
6282 default:
6283 abort ();
6286 /* Restore the file and line information. */
6287 lineno = saved_lineno;
6288 input_filename = saved_filename;
6290 return r;
6293 /* Substitue into the ARG_TYPES of a function type. */
6295 static tree
6296 tsubst_arg_types (arg_types, args, complain, in_decl)
6297 tree arg_types;
6298 tree args;
6299 tsubst_flags_t complain;
6300 tree in_decl;
6302 tree remaining_arg_types;
6303 tree type;
6305 if (!arg_types || arg_types == void_list_node)
6306 return arg_types;
6308 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6309 args, complain, in_decl);
6310 if (remaining_arg_types == error_mark_node)
6311 return error_mark_node;
6313 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6314 if (type == error_mark_node)
6315 return error_mark_node;
6316 if (VOID_TYPE_P (type))
6318 if (complain & tf_error)
6320 error ("invalid parameter type `%T'", type);
6321 if (in_decl)
6322 cp_error_at ("in declaration `%D'", in_decl);
6324 return error_mark_node;
6327 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6328 top-level qualifiers as required. */
6329 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6331 /* Note that we do not substitute into default arguments here. The
6332 standard mandates that they be instantiated only when needed,
6333 which is done in build_over_call. */
6334 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6335 remaining_arg_types);
6339 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6340 *not* handle the exception-specification for FNTYPE, because the
6341 initial substitution of explicitly provided template parameters
6342 during argument deduction forbids substitution into the
6343 exception-specification:
6345 [temp.deduct]
6347 All references in the function type of the function template to the
6348 corresponding template parameters are replaced by the specified tem-
6349 plate argument values. If a substitution in a template parameter or
6350 in the function type of the function template results in an invalid
6351 type, type deduction fails. [Note: The equivalent substitution in
6352 exception specifications is done only when the function is instanti-
6353 ated, at which point a program is ill-formed if the substitution
6354 results in an invalid type.] */
6356 static tree
6357 tsubst_function_type (t, args, complain, in_decl)
6358 tree t;
6359 tree args;
6360 tsubst_flags_t complain;
6361 tree in_decl;
6363 tree return_type;
6364 tree arg_types;
6365 tree fntype;
6367 /* The TYPE_CONTEXT is not used for function/method types. */
6368 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6370 /* Substitute the return type. */
6371 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6372 if (return_type == error_mark_node)
6373 return error_mark_node;
6375 /* Substitue the argument types. */
6376 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6377 complain, in_decl);
6378 if (arg_types == error_mark_node)
6379 return error_mark_node;
6381 /* Construct a new type node and return it. */
6382 if (TREE_CODE (t) == FUNCTION_TYPE)
6383 fntype = build_function_type (return_type, arg_types);
6384 else
6386 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6387 if (! IS_AGGR_TYPE (r))
6389 /* [temp.deduct]
6391 Type deduction may fail for any of the following
6392 reasons:
6394 -- Attempting to create "pointer to member of T" when T
6395 is not a class type. */
6396 if (complain & tf_error)
6397 error ("creating pointer to member function of non-class type `%T'",
6399 return error_mark_node;
6402 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6403 (arg_types));
6405 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6406 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6408 return fntype;
6411 /* Substitute into the PARMS of a call-declarator. */
6413 static tree
6414 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6415 tree parms;
6416 tree args;
6417 tsubst_flags_t complain;
6418 tree in_decl;
6420 tree new_parms;
6421 tree type;
6422 tree defarg;
6424 if (!parms || parms == void_list_node)
6425 return parms;
6427 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6428 args, complain, in_decl);
6430 /* Figure out the type of this parameter. */
6431 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6433 /* Figure out the default argument as well. Note that we use
6434 tsubst_expr since the default argument is really an expression. */
6435 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6437 /* Chain this parameter on to the front of those we have already
6438 processed. We don't use hash_tree_cons because that function
6439 doesn't check TREE_PARMLIST. */
6440 new_parms = tree_cons (defarg, type, new_parms);
6442 /* And note that these are parameters. */
6443 TREE_PARMLIST (new_parms) = 1;
6445 return new_parms;
6448 /* Take the tree structure T and replace template parameters used
6449 therein with the argument vector ARGS. IN_DECL is an associated
6450 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6451 Issue error and warning messages under control of COMPLAIN. Note
6452 that we must be relatively non-tolerant of extensions here, in
6453 order to preserve conformance; if we allow substitutions that
6454 should not be allowed, we may allow argument deductions that should
6455 not succeed, and therefore report ambiguous overload situations
6456 where there are none. In theory, we could allow the substitution,
6457 but indicate that it should have failed, and allow our caller to
6458 make sure that the right thing happens, but we don't try to do this
6459 yet.
6461 This function is used for dealing with types, decls and the like;
6462 for expressions, use tsubst_expr or tsubst_copy. */
6464 static tree
6465 tsubst (t, args, complain, in_decl)
6466 tree t, args;
6467 tsubst_flags_t complain;
6468 tree in_decl;
6470 tree type, r;
6472 if (t == NULL_TREE || t == error_mark_node
6473 || t == integer_type_node
6474 || t == void_type_node
6475 || t == char_type_node
6476 || TREE_CODE (t) == NAMESPACE_DECL)
6477 return t;
6479 if (TREE_CODE (t) == IDENTIFIER_NODE)
6480 type = IDENTIFIER_TYPE_VALUE (t);
6481 else
6482 type = TREE_TYPE (t);
6483 if (type == unknown_type_node)
6484 abort ();
6486 if (type && TREE_CODE (t) != FUNCTION_DECL
6487 && TREE_CODE (t) != TYPENAME_TYPE
6488 && TREE_CODE (t) != TEMPLATE_DECL
6489 && TREE_CODE (t) != IDENTIFIER_NODE
6490 && TREE_CODE (t) != FUNCTION_TYPE
6491 && TREE_CODE (t) != METHOD_TYPE)
6492 type = tsubst (type, args, complain, in_decl);
6493 if (type == error_mark_node)
6494 return error_mark_node;
6496 if (DECL_P (t))
6497 return tsubst_decl (t, args, type, complain);
6499 switch (TREE_CODE (t))
6501 case RECORD_TYPE:
6502 case UNION_TYPE:
6503 case ENUMERAL_TYPE:
6504 return tsubst_aggr_type (t, args, complain, in_decl,
6505 /*entering_scope=*/0);
6507 case ERROR_MARK:
6508 case IDENTIFIER_NODE:
6509 case VOID_TYPE:
6510 case REAL_TYPE:
6511 case COMPLEX_TYPE:
6512 case VECTOR_TYPE:
6513 case BOOLEAN_TYPE:
6514 case INTEGER_CST:
6515 case REAL_CST:
6516 case STRING_CST:
6517 return t;
6519 case INTEGER_TYPE:
6520 if (t == integer_type_node)
6521 return t;
6523 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6524 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6525 return t;
6528 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6530 max = tsubst_expr (omax, args, complain, in_decl);
6531 if (max == error_mark_node)
6532 return error_mark_node;
6534 /* See if we can reduce this expression to something simpler. */
6535 max = maybe_fold_nontype_arg (max);
6536 if (!processing_template_decl)
6537 max = decl_constant_value (max);
6539 if (processing_template_decl
6540 /* When providing explicit arguments to a template
6541 function, but leaving some arguments for subsequent
6542 deduction, MAX may be template-dependent even if we're
6543 not PROCESSING_TEMPLATE_DECL. We still need to check for
6544 template parms, though; MAX won't be an INTEGER_CST for
6545 dynamic arrays, either. */
6546 || (TREE_CODE (max) != INTEGER_CST
6547 && uses_template_parms (max)))
6549 tree itype = make_node (INTEGER_TYPE);
6550 TYPE_MIN_VALUE (itype) = size_zero_node;
6551 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6552 integer_one_node);
6553 return itype;
6556 if (integer_zerop (omax))
6558 /* Still allow an explicit array of size zero. */
6559 if (pedantic)
6560 pedwarn ("creating array with size zero");
6562 else if (integer_zerop (max)
6563 || (TREE_CODE (max) == INTEGER_CST
6564 && INT_CST_LT (max, integer_zero_node)))
6566 /* [temp.deduct]
6568 Type deduction may fail for any of the following
6569 reasons:
6571 Attempting to create an array with a size that is
6572 zero or negative. */
6573 if (complain & tf_error)
6574 error ("creating array with size zero (`%E')", max);
6576 return error_mark_node;
6579 return compute_array_index_type (NULL_TREE, max);
6582 case TEMPLATE_TYPE_PARM:
6583 case TEMPLATE_TEMPLATE_PARM:
6584 case BOUND_TEMPLATE_TEMPLATE_PARM:
6585 case TEMPLATE_PARM_INDEX:
6587 int idx;
6588 int level;
6589 int levels;
6591 r = NULL_TREE;
6593 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6594 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6595 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6597 idx = TEMPLATE_TYPE_IDX (t);
6598 level = TEMPLATE_TYPE_LEVEL (t);
6600 else
6602 idx = TEMPLATE_PARM_IDX (t);
6603 level = TEMPLATE_PARM_LEVEL (t);
6606 if (TREE_VEC_LENGTH (args) > 0)
6608 tree arg = NULL_TREE;
6610 levels = TMPL_ARGS_DEPTH (args);
6611 if (level <= levels)
6612 arg = TMPL_ARG (args, level, idx);
6614 if (arg == error_mark_node)
6615 return error_mark_node;
6616 else if (arg != NULL_TREE)
6618 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6620 my_friendly_assert (TYPE_P (arg), 0);
6621 return cp_build_qualified_type_real
6622 (arg, cp_type_quals (arg) | cp_type_quals (t),
6623 complain | tf_ignore_bad_quals);
6625 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6627 /* We are processing a type constructed from
6628 a template template parameter */
6629 tree argvec = tsubst (TYPE_TI_ARGS (t),
6630 args, complain, in_decl);
6631 if (argvec == error_mark_node)
6632 return error_mark_node;
6634 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6635 we are resolving nested-types in the signature of
6636 a member function templates.
6637 Otherwise ARG is a TEMPLATE_DECL and is the real
6638 template to be instantiated. */
6639 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6640 arg = TYPE_NAME (arg);
6642 r = lookup_template_class (arg,
6643 argvec, in_decl,
6644 DECL_CONTEXT (arg),
6645 /*entering_scope=*/0,
6646 complain);
6647 return cp_build_qualified_type_real
6648 (r, TYPE_QUALS (t), complain);
6650 else
6651 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6652 return arg;
6655 else
6656 abort ();
6658 if (level == 1)
6659 /* This can happen during the attempted tsubst'ing in
6660 unify. This means that we don't yet have any information
6661 about the template parameter in question. */
6662 return t;
6664 /* If we get here, we must have been looking at a parm for a
6665 more deeply nested template. Make a new version of this
6666 template parameter, but with a lower level. */
6667 switch (TREE_CODE (t))
6669 case TEMPLATE_TYPE_PARM:
6670 case TEMPLATE_TEMPLATE_PARM:
6671 case BOUND_TEMPLATE_TEMPLATE_PARM:
6672 if (cp_type_quals (t))
6674 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6675 r = cp_build_qualified_type_real
6676 (r, cp_type_quals (t),
6677 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6678 ? tf_ignore_bad_quals : 0));
6680 else
6682 r = copy_type (t);
6683 TEMPLATE_TYPE_PARM_INDEX (r)
6684 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6685 r, levels);
6686 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6687 TYPE_MAIN_VARIANT (r) = r;
6688 TYPE_POINTER_TO (r) = NULL_TREE;
6689 TYPE_REFERENCE_TO (r) = NULL_TREE;
6691 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6693 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6694 complain, in_decl);
6695 if (argvec == error_mark_node)
6696 return error_mark_node;
6698 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6699 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6702 break;
6704 case TEMPLATE_PARM_INDEX:
6705 r = reduce_template_parm_level (t, type, levels);
6706 break;
6708 default:
6709 abort ();
6712 return r;
6715 case TREE_LIST:
6717 tree purpose, value, chain, result;
6719 if (t == void_list_node)
6720 return t;
6722 purpose = TREE_PURPOSE (t);
6723 if (purpose)
6725 purpose = tsubst (purpose, args, complain, in_decl);
6726 if (purpose == error_mark_node)
6727 return error_mark_node;
6729 value = TREE_VALUE (t);
6730 if (value)
6732 value = tsubst (value, args, complain, in_decl);
6733 if (value == error_mark_node)
6734 return error_mark_node;
6736 chain = TREE_CHAIN (t);
6737 if (chain && chain != void_type_node)
6739 chain = tsubst (chain, args, complain, in_decl);
6740 if (chain == error_mark_node)
6741 return error_mark_node;
6743 if (purpose == TREE_PURPOSE (t)
6744 && value == TREE_VALUE (t)
6745 && chain == TREE_CHAIN (t))
6746 return t;
6747 if (TREE_PARMLIST (t))
6749 result = tree_cons (purpose, value, chain);
6750 TREE_PARMLIST (result) = 1;
6752 else
6753 result = hash_tree_cons (purpose, value, chain);
6754 return result;
6756 case TREE_VEC:
6757 if (type != NULL_TREE)
6759 /* A binfo node. We always need to make a copy, of the node
6760 itself and of its BINFO_BASETYPES. */
6762 t = copy_node (t);
6764 /* Make sure type isn't a typedef copy. */
6765 type = BINFO_TYPE (TYPE_BINFO (type));
6767 TREE_TYPE (t) = complete_type (type);
6768 if (IS_AGGR_TYPE (type))
6770 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6771 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6772 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6773 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6775 return t;
6778 /* Otherwise, a vector of template arguments. */
6779 return tsubst_template_arg_vector (t, args, complain);
6781 case POINTER_TYPE:
6782 case REFERENCE_TYPE:
6784 enum tree_code code;
6786 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6787 return t;
6789 code = TREE_CODE (t);
6792 /* [temp.deduct]
6794 Type deduction may fail for any of the following
6795 reasons:
6797 -- Attempting to create a pointer to reference type.
6798 -- Attempting to create a reference to a reference type or
6799 a reference to void. */
6800 if (TREE_CODE (type) == REFERENCE_TYPE
6801 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6803 static int last_line = 0;
6804 static const char* last_file = 0;
6806 /* We keep track of the last time we issued this error
6807 message to avoid spewing a ton of messages during a
6808 single bad template instantiation. */
6809 if (complain & tf_error
6810 && (last_line != lineno || last_file != input_filename))
6812 if (TREE_CODE (type) == VOID_TYPE)
6813 error ("forming reference to void");
6814 else
6815 error ("forming %s to reference type `%T'",
6816 (code == POINTER_TYPE) ? "pointer" : "reference",
6817 type);
6818 last_line = lineno;
6819 last_file = input_filename;
6822 return error_mark_node;
6824 else if (code == POINTER_TYPE)
6826 r = build_pointer_type (type);
6827 if (TREE_CODE (type) == METHOD_TYPE)
6828 r = build_ptrmemfunc_type (r);
6830 else
6831 r = build_reference_type (type);
6832 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6834 if (r != error_mark_node)
6835 /* Will this ever be needed for TYPE_..._TO values? */
6836 layout_type (r);
6838 return r;
6840 case OFFSET_TYPE:
6842 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6843 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6845 /* [temp.deduct]
6847 Type deduction may fail for any of the following
6848 reasons:
6850 -- Attempting to create "pointer to member of T" when T
6851 is not a class type. */
6852 if (complain & tf_error)
6853 error ("creating pointer to member of non-class type `%T'", r);
6854 return error_mark_node;
6856 if (TREE_CODE (type) == REFERENCE_TYPE)
6858 if (complain & tf_error)
6859 error ("creating pointer to member reference type `%T'", type);
6861 return error_mark_node;
6863 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6864 if (TREE_CODE (type) == FUNCTION_TYPE)
6865 /* This is really a method type. The cv qualifiers of the
6866 this pointer should _not_ be determined by the cv
6867 qualifiers of the class type. They should be held
6868 somewhere in the FUNCTION_TYPE, but we don't do that at
6869 the moment. Consider
6870 typedef void (Func) () const;
6872 template <typename T1> void Foo (Func T1::*);
6875 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6876 TREE_TYPE (type),
6877 TYPE_ARG_TYPES (type));
6878 else
6879 return build_offset_type (r, type);
6881 case FUNCTION_TYPE:
6882 case METHOD_TYPE:
6884 tree fntype;
6885 tree raises;
6887 fntype = tsubst_function_type (t, args, complain, in_decl);
6888 if (fntype == error_mark_node)
6889 return error_mark_node;
6891 /* Substitue the exception specification. */
6892 raises = TYPE_RAISES_EXCEPTIONS (t);
6893 if (raises)
6895 tree list = NULL_TREE;
6897 if (! TREE_VALUE (raises))
6898 list = raises;
6899 else
6900 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6902 tree spec = TREE_VALUE (raises);
6904 spec = tsubst (spec, args, complain, in_decl);
6905 if (spec == error_mark_node)
6906 return spec;
6907 list = add_exception_specifier (list, spec, complain);
6909 fntype = build_exception_variant (fntype, list);
6911 return fntype;
6913 case ARRAY_TYPE:
6915 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6916 if (domain == error_mark_node)
6917 return error_mark_node;
6919 /* As an optimization, we avoid regenerating the array type if
6920 it will obviously be the same as T. */
6921 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6922 return t;
6924 /* These checks should match the ones in grokdeclarator.
6926 [temp.deduct]
6928 The deduction may fail for any of the following reasons:
6930 -- Attempting to create an array with an element type that
6931 is void, a function type, or a reference type. */
6932 if (TREE_CODE (type) == VOID_TYPE
6933 || TREE_CODE (type) == FUNCTION_TYPE
6934 || TREE_CODE (type) == REFERENCE_TYPE)
6936 if (complain & tf_error)
6937 error ("creating array of `%T'", type);
6938 return error_mark_node;
6941 r = build_cplus_array_type (type, domain);
6942 return r;
6945 case PLUS_EXPR:
6946 case MINUS_EXPR:
6948 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6949 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6951 if (e1 == error_mark_node || e2 == error_mark_node)
6952 return error_mark_node;
6954 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6957 case NEGATE_EXPR:
6958 case NOP_EXPR:
6960 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6961 if (e == error_mark_node)
6962 return error_mark_node;
6964 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6967 case TYPENAME_TYPE:
6969 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6970 in_decl, /*entering_scope=*/1);
6971 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6972 complain, in_decl);
6974 if (ctx == error_mark_node || f == error_mark_node)
6975 return error_mark_node;
6977 if (!IS_AGGR_TYPE (ctx))
6979 if (complain & tf_error)
6980 error ("`%T' is not a class, struct, or union type",
6981 ctx);
6982 return error_mark_node;
6984 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6986 /* Normally, make_typename_type does not require that the CTX
6987 have complete type in order to allow things like:
6989 template <class T> struct S { typename S<T>::X Y; };
6991 But, such constructs have already been resolved by this
6992 point, so here CTX really should have complete type, unless
6993 it's a partial instantiation. */
6994 ctx = complete_type (ctx);
6995 if (!COMPLETE_TYPE_P (ctx))
6997 if (complain & tf_error)
6998 cxx_incomplete_type_error (NULL_TREE, ctx);
6999 return error_mark_node;
7003 f = make_typename_type (ctx, f,
7004 (complain & tf_error) | tf_keep_type_decl);
7005 if (f == error_mark_node)
7006 return f;
7007 if (TREE_CODE (f) == TYPE_DECL)
7009 complain |= tf_ignore_bad_quals;
7010 f = TREE_TYPE (f);
7013 return cp_build_qualified_type_real
7014 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7017 case UNBOUND_CLASS_TEMPLATE:
7019 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7020 in_decl, /*entering_scope=*/1);
7021 tree name = TYPE_IDENTIFIER (t);
7023 if (ctx == error_mark_node || name == error_mark_node)
7024 return error_mark_node;
7026 return make_unbound_class_template (ctx, name, complain);
7029 case INDIRECT_REF:
7031 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7032 if (e == error_mark_node)
7033 return error_mark_node;
7034 return make_pointer_declarator (type, e);
7037 case ADDR_EXPR:
7039 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7040 if (e == error_mark_node)
7041 return error_mark_node;
7042 return make_reference_declarator (type, e);
7045 case ARRAY_REF:
7047 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7048 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7049 if (e1 == error_mark_node || e2 == error_mark_node)
7050 return error_mark_node;
7052 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
7055 case CALL_EXPR:
7057 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7058 tree e2 = (tsubst_call_declarator_parms
7059 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
7060 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
7061 complain, in_decl);
7063 if (e1 == error_mark_node || e2 == error_mark_node
7064 || e3 == error_mark_node)
7065 return error_mark_node;
7067 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
7070 case SCOPE_REF:
7072 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7073 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7074 if (e1 == error_mark_node || e2 == error_mark_node)
7075 return error_mark_node;
7077 return build_nt (TREE_CODE (t), e1, e2);
7080 case TYPEOF_TYPE:
7082 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
7083 if (e1 == error_mark_node)
7084 return error_mark_node;
7086 return TREE_TYPE (e1);
7089 default:
7090 sorry ("use of `%s' in template",
7091 tree_code_name [(int) TREE_CODE (t)]);
7092 return error_mark_node;
7096 /* Like tsubst, but deals with expressions. This function just replaces
7097 template parms; to finish processing the resultant expression, use
7098 tsubst_expr. */
7100 static tree
7101 tsubst_copy (t, args, complain, in_decl)
7102 tree t, args;
7103 tsubst_flags_t complain;
7104 tree in_decl;
7106 enum tree_code code;
7107 tree r;
7109 if (t == NULL_TREE || t == error_mark_node)
7110 return t;
7112 code = TREE_CODE (t);
7114 switch (code)
7116 case PARM_DECL:
7117 r = retrieve_local_specialization (t);
7118 my_friendly_assert (r != NULL, 20020903);
7119 return r;
7121 case CONST_DECL:
7123 tree enum_type;
7124 tree v;
7126 if (DECL_TEMPLATE_PARM_P (t))
7127 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7128 if (!DECL_CONTEXT (t))
7129 /* This is a global enumeration constant. */
7130 return t;
7132 /* Unfortunately, we cannot just call lookup_name here.
7133 Consider:
7135 template <int I> int f() {
7136 enum E { a = I };
7137 struct S { void g() { E e = a; } };
7140 When we instantiate f<7>::S::g(), say, lookup_name is not
7141 clever enough to find f<7>::a. */
7142 enum_type
7143 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7144 /*entering_scope=*/0);
7146 /* Not yet available. */
7147 if (!enum_type || enum_type == (TREE_TYPE (t)))
7148 return t;
7150 for (v = TYPE_VALUES (enum_type);
7151 v != NULL_TREE;
7152 v = TREE_CHAIN (v))
7153 if (TREE_PURPOSE (v) == DECL_NAME (t))
7154 return TREE_VALUE (v);
7156 /* We didn't find the name. That should never happen; if
7157 name-lookup found it during preliminary parsing, we
7158 should find it again here during instantiation. */
7159 abort ();
7161 return t;
7163 case FIELD_DECL:
7164 if (DECL_CONTEXT (t))
7166 tree ctx;
7168 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7169 /*entering_scope=*/1);
7170 if (ctx != DECL_CONTEXT (t))
7171 return lookup_field (ctx, DECL_NAME (t), 0, false);
7173 return t;
7175 case VAR_DECL:
7176 case FUNCTION_DECL:
7177 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7178 || local_variable_p (t))
7179 t = tsubst (t, args, complain, in_decl);
7180 mark_used (t);
7181 return t;
7183 case BASELINK:
7185 tree name;
7186 tree qualifying_scope;
7187 tree fns;
7188 tree template_args = 0;
7189 bool template_id_p = false;
7191 /* A baselink indicates a function from a base class. The
7192 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7193 non-dependent types; otherwise, the lookup could not have
7194 succeeded. However, they may indicate bases of the template
7195 class, rather than the instantiated class.
7197 In addition, lookups that were not ambiguous before may be
7198 ambiguous now. Therefore, we perform the lookup again. */
7199 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t));
7200 fns = BASELINK_FUNCTIONS (t);
7201 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7203 template_id_p = true;
7204 template_args = TREE_OPERAND (fns, 1);
7205 fns = TREE_OPERAND (fns, 0);
7207 name = DECL_NAME (get_first_fn (fns));
7208 t = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7209 if (BASELINK_P (t) && template_id_p)
7210 BASELINK_FUNCTIONS (t)
7211 = build_nt (TEMPLATE_ID_EXPR,
7212 BASELINK_FUNCTIONS (t),
7213 template_args);
7214 return adjust_result_of_qualified_name_lookup (t,
7215 qualifying_scope,
7216 current_class_type);
7219 case TEMPLATE_DECL:
7220 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7221 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7222 args, complain, in_decl);
7223 else if (is_member_template (t))
7224 return tsubst (t, args, complain, in_decl);
7225 else
7226 return t;
7228 case LOOKUP_EXPR:
7230 /* We must tsubst into a LOOKUP_EXPR in case the names to
7231 which it refers is a conversion operator; in that case the
7232 name will change. We avoid making unnecessary copies,
7233 however. */
7235 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7237 if (id != TREE_OPERAND (t, 0))
7239 r = build_nt (LOOKUP_EXPR, id);
7240 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7241 t = r;
7244 return t;
7247 case CAST_EXPR:
7248 case REINTERPRET_CAST_EXPR:
7249 case CONST_CAST_EXPR:
7250 case STATIC_CAST_EXPR:
7251 case DYNAMIC_CAST_EXPR:
7252 case NOP_EXPR:
7253 return build1
7254 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7255 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7257 case INDIRECT_REF:
7258 case NEGATE_EXPR:
7259 case TRUTH_NOT_EXPR:
7260 case BIT_NOT_EXPR:
7261 case ADDR_EXPR:
7262 case CONVERT_EXPR: /* Unary + */
7263 case SIZEOF_EXPR:
7264 case ALIGNOF_EXPR:
7265 case ARROW_EXPR:
7266 case THROW_EXPR:
7267 case TYPEID_EXPR:
7268 case REALPART_EXPR:
7269 case IMAGPART_EXPR:
7270 return build1
7271 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7272 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7274 case COMPONENT_REF:
7276 tree object;
7277 tree name;
7279 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7280 name = TREE_OPERAND (t, 1);
7281 if (TREE_CODE (name) == BIT_NOT_EXPR)
7283 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7284 complain, in_decl);
7285 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7287 else if (TREE_CODE (name) == SCOPE_REF
7288 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7290 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7291 complain, in_decl);
7292 name = TREE_OPERAND (name, 1);
7293 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7294 complain, in_decl);
7295 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7296 name = build_nt (SCOPE_REF, base, name);
7298 else
7299 name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7300 return build_nt (COMPONENT_REF, object, name);
7303 case PLUS_EXPR:
7304 case MINUS_EXPR:
7305 case MULT_EXPR:
7306 case TRUNC_DIV_EXPR:
7307 case CEIL_DIV_EXPR:
7308 case FLOOR_DIV_EXPR:
7309 case ROUND_DIV_EXPR:
7310 case EXACT_DIV_EXPR:
7311 case BIT_AND_EXPR:
7312 case BIT_ANDTC_EXPR:
7313 case BIT_IOR_EXPR:
7314 case BIT_XOR_EXPR:
7315 case TRUNC_MOD_EXPR:
7316 case FLOOR_MOD_EXPR:
7317 case TRUTH_ANDIF_EXPR:
7318 case TRUTH_ORIF_EXPR:
7319 case TRUTH_AND_EXPR:
7320 case TRUTH_OR_EXPR:
7321 case RSHIFT_EXPR:
7322 case LSHIFT_EXPR:
7323 case RROTATE_EXPR:
7324 case LROTATE_EXPR:
7325 case EQ_EXPR:
7326 case NE_EXPR:
7327 case MAX_EXPR:
7328 case MIN_EXPR:
7329 case LE_EXPR:
7330 case GE_EXPR:
7331 case LT_EXPR:
7332 case GT_EXPR:
7333 case ARRAY_REF:
7334 case COMPOUND_EXPR:
7335 case SCOPE_REF:
7336 case DOTSTAR_EXPR:
7337 case MEMBER_REF:
7338 case PREDECREMENT_EXPR:
7339 case PREINCREMENT_EXPR:
7340 case POSTDECREMENT_EXPR:
7341 case POSTINCREMENT_EXPR:
7342 return build_nt
7343 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7344 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7346 case CALL_EXPR:
7347 return build_nt (code,
7348 tsubst_copy (TREE_OPERAND (t, 0), args,
7349 complain, in_decl),
7350 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7351 in_decl),
7352 NULL_TREE);
7354 case METHOD_CALL_EXPR:
7355 return build_nt
7356 (code,
7357 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7358 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7359 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7360 NULL_TREE);
7362 case STMT_EXPR:
7363 /* This processing should really occur in tsubst_expr. However,
7364 tsubst_expr does not recurse into expressions, since it
7365 assumes that there aren't any statements inside them. So, we
7366 need to expand the STMT_EXPR here. */
7367 if (!processing_template_decl)
7369 tree stmt_expr = begin_stmt_expr ();
7370 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7371 return finish_stmt_expr (stmt_expr);
7374 return t;
7376 case COND_EXPR:
7377 case MODOP_EXPR:
7378 case PSEUDO_DTOR_EXPR:
7380 r = build_nt
7381 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7382 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7383 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7384 return r;
7387 case NEW_EXPR:
7389 r = build_nt
7390 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7391 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7392 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7393 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7394 return r;
7397 case DELETE_EXPR:
7399 r = build_nt
7400 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7401 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7402 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7403 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7404 return r;
7407 case TEMPLATE_ID_EXPR:
7409 /* Substituted template arguments */
7410 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7411 in_decl);
7413 if (targs && TREE_CODE (targs) == TREE_LIST)
7415 tree chain;
7416 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7417 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7419 else if (targs)
7421 int i;
7422 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7423 TREE_VEC_ELT (targs, i)
7424 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7427 return lookup_template_function
7428 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7431 case TREE_LIST:
7433 tree purpose, value, chain;
7435 if (t == void_list_node)
7436 return t;
7438 purpose = TREE_PURPOSE (t);
7439 if (purpose)
7440 purpose = tsubst_copy (purpose, args, complain, in_decl);
7441 value = TREE_VALUE (t);
7442 if (value)
7443 value = tsubst_copy (value, args, complain, in_decl);
7444 chain = TREE_CHAIN (t);
7445 if (chain && chain != void_type_node)
7446 chain = tsubst_copy (chain, args, complain, in_decl);
7447 if (purpose == TREE_PURPOSE (t)
7448 && value == TREE_VALUE (t)
7449 && chain == TREE_CHAIN (t))
7450 return t;
7451 return tree_cons (purpose, value, chain);
7454 case RECORD_TYPE:
7455 case UNION_TYPE:
7456 case ENUMERAL_TYPE:
7457 case INTEGER_TYPE:
7458 case TEMPLATE_TYPE_PARM:
7459 case TEMPLATE_TEMPLATE_PARM:
7460 case BOUND_TEMPLATE_TEMPLATE_PARM:
7461 case TEMPLATE_PARM_INDEX:
7462 case POINTER_TYPE:
7463 case REFERENCE_TYPE:
7464 case OFFSET_TYPE:
7465 case FUNCTION_TYPE:
7466 case METHOD_TYPE:
7467 case ARRAY_TYPE:
7468 case TYPENAME_TYPE:
7469 case UNBOUND_CLASS_TEMPLATE:
7470 case TYPEOF_TYPE:
7471 case TYPE_DECL:
7472 return tsubst (t, args, complain, in_decl);
7474 case IDENTIFIER_NODE:
7475 if (IDENTIFIER_TYPENAME_P (t))
7477 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7478 return mangle_conv_op_name_for_type (new_type);
7480 else
7481 return t;
7483 case CONSTRUCTOR:
7485 r = build
7486 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7487 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7488 complain, in_decl));
7489 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7490 return r;
7493 case VA_ARG_EXPR:
7494 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7495 in_decl),
7496 tsubst (TREE_TYPE (t), args, complain, in_decl));
7498 default:
7499 return t;
7503 /* Like tsubst_copy for expressions, etc. but also does semantic
7504 processing. */
7506 static tree
7507 tsubst_expr (t, args, complain, in_decl)
7508 tree t, args;
7509 tsubst_flags_t complain;
7510 tree in_decl;
7512 tree stmt, tmp;
7514 if (t == NULL_TREE || t == error_mark_node)
7515 return t;
7517 if (processing_template_decl)
7518 return tsubst_copy (t, args, complain, in_decl);
7520 if (!statement_code_p (TREE_CODE (t)))
7521 return tsubst_copy_and_build (t, args, complain, in_decl);
7523 switch (TREE_CODE (t))
7525 case CTOR_INITIALIZER:
7526 prep_stmt (t);
7527 finish_mem_initializers (tsubst_initializer_list
7528 (TREE_OPERAND (t, 0), args));
7529 break;
7531 case RETURN_STMT:
7532 prep_stmt (t);
7533 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
7534 args, complain, in_decl));
7535 break;
7537 case EXPR_STMT:
7538 prep_stmt (t);
7539 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7540 args, complain, in_decl));
7541 break;
7543 case USING_STMT:
7544 prep_stmt (t);
7545 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7546 args, complain, in_decl));
7547 break;
7549 case DECL_STMT:
7551 tree decl;
7552 tree init;
7554 prep_stmt (t);
7555 decl = DECL_STMT_DECL (t);
7556 if (TREE_CODE (decl) == LABEL_DECL)
7557 finish_label_decl (DECL_NAME (decl));
7558 else if (TREE_CODE (decl) == USING_DECL)
7560 tree scope = DECL_INITIAL (decl);
7561 tree name = DECL_NAME (decl);
7563 scope = tsubst_expr (scope, args, complain, in_decl);
7564 do_local_using_decl (lookup_qualified_name (scope,
7565 name,
7566 /*is_type_p=*/0,
7567 /*flags=*/0));
7569 else
7571 init = DECL_INITIAL (decl);
7572 decl = tsubst (decl, args, complain, in_decl);
7573 if (decl != error_mark_node)
7575 if (init)
7576 DECL_INITIAL (decl) = error_mark_node;
7577 /* By marking the declaration as instantiated, we avoid
7578 trying to instantiate it. Since instantiate_decl can't
7579 handle local variables, and since we've already done
7580 all that needs to be done, that's the right thing to
7581 do. */
7582 if (TREE_CODE (decl) == VAR_DECL)
7583 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7584 if (TREE_CODE (decl) == VAR_DECL
7585 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7586 /* Anonymous aggregates are a special case. */
7587 finish_anon_union (decl);
7588 else
7590 maybe_push_decl (decl);
7591 if (DECL_PRETTY_FUNCTION_P (decl))
7593 /* For __PRETTY_FUNCTION__ we have to adjust the
7594 initializer. */
7595 const char *const name
7596 = cxx_printable_name (current_function_decl, 2);
7597 init = cp_fname_init (name);
7598 TREE_TYPE (decl) = TREE_TYPE (init);
7600 else
7601 init = tsubst_expr (init, args, complain, in_decl);
7602 cp_finish_decl (decl, init, NULL_TREE, 0);
7607 /* A DECL_STMT can also be used as an expression, in the condition
7608 clause of an if/for/while construct. If we aren't followed by
7609 another statement, return our decl. */
7610 if (TREE_CHAIN (t) == NULL_TREE)
7611 return decl;
7613 break;
7615 case FOR_STMT:
7617 prep_stmt (t);
7619 stmt = begin_for_stmt ();
7620 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7621 finish_for_init_stmt (stmt);
7622 finish_for_cond (tsubst_expr (FOR_COND (t),
7623 args, complain, in_decl),
7624 stmt);
7625 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7626 finish_for_expr (tmp, stmt);
7627 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7628 finish_for_stmt (stmt);
7630 break;
7632 case WHILE_STMT:
7634 prep_stmt (t);
7635 stmt = begin_while_stmt ();
7636 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7637 args, complain, in_decl),
7638 stmt);
7639 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7640 finish_while_stmt (stmt);
7642 break;
7644 case DO_STMT:
7646 prep_stmt (t);
7647 stmt = begin_do_stmt ();
7648 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7649 finish_do_body (stmt);
7650 finish_do_stmt (tsubst_expr (DO_COND (t),
7651 args, complain, in_decl),
7652 stmt);
7654 break;
7656 case IF_STMT:
7658 prep_stmt (t);
7659 stmt = begin_if_stmt ();
7660 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7661 args, complain, in_decl),
7662 stmt);
7664 if (tmp = THEN_CLAUSE (t), tmp)
7666 tsubst_expr (tmp, args, complain, in_decl);
7667 finish_then_clause (stmt);
7670 if (tmp = ELSE_CLAUSE (t), tmp)
7672 begin_else_clause ();
7673 tsubst_expr (tmp, args, complain, in_decl);
7674 finish_else_clause (stmt);
7677 finish_if_stmt ();
7679 break;
7681 case COMPOUND_STMT:
7683 prep_stmt (t);
7684 if (COMPOUND_STMT_BODY_BLOCK (t))
7685 stmt = begin_function_body ();
7686 else
7687 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7689 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7691 if (COMPOUND_STMT_BODY_BLOCK (t))
7692 finish_function_body (stmt);
7693 else
7694 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7696 break;
7698 case BREAK_STMT:
7699 prep_stmt (t);
7700 finish_break_stmt ();
7701 break;
7703 case CONTINUE_STMT:
7704 prep_stmt (t);
7705 finish_continue_stmt ();
7706 break;
7708 case SWITCH_STMT:
7710 tree val;
7712 prep_stmt (t);
7713 stmt = begin_switch_stmt ();
7714 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7715 finish_switch_cond (val, stmt);
7716 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7717 finish_switch_stmt (stmt);
7719 break;
7721 case CASE_LABEL:
7722 prep_stmt (t);
7723 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7724 tsubst_expr (CASE_HIGH (t), args, complain,
7725 in_decl));
7726 break;
7728 case LABEL_STMT:
7729 lineno = STMT_LINENO (t);
7730 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7731 break;
7733 case FILE_STMT:
7734 input_filename = FILE_STMT_FILENAME (t);
7735 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7736 break;
7738 case GOTO_STMT:
7739 prep_stmt (t);
7740 tmp = GOTO_DESTINATION (t);
7741 if (TREE_CODE (tmp) != LABEL_DECL)
7742 /* Computed goto's must be tsubst'd into. On the other hand,
7743 non-computed gotos must not be; the identifier in question
7744 will have no binding. */
7745 tmp = tsubst_expr (tmp, args, complain, in_decl);
7746 else
7747 tmp = DECL_NAME (tmp);
7748 finish_goto_stmt (tmp);
7749 break;
7751 case ASM_STMT:
7752 prep_stmt (t);
7753 tmp = finish_asm_stmt
7754 (ASM_CV_QUAL (t),
7755 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7756 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7757 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7758 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7759 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
7760 break;
7762 case TRY_BLOCK:
7763 prep_stmt (t);
7764 if (CLEANUP_P (t))
7766 stmt = begin_try_block ();
7767 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7768 finish_cleanup_try_block (stmt);
7769 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7770 complain, in_decl),
7771 stmt);
7773 else
7775 if (FN_TRY_BLOCK_P (t))
7776 stmt = begin_function_try_block ();
7777 else
7778 stmt = begin_try_block ();
7780 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7782 if (FN_TRY_BLOCK_P (t))
7783 finish_function_try_block (stmt);
7784 else
7785 finish_try_block (stmt);
7787 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7788 if (FN_TRY_BLOCK_P (t))
7789 finish_function_handler_sequence (stmt);
7790 else
7791 finish_handler_sequence (stmt);
7793 break;
7795 case HANDLER:
7797 tree decl;
7799 prep_stmt (t);
7800 stmt = begin_handler ();
7801 if (HANDLER_PARMS (t))
7803 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7804 decl = tsubst (decl, args, complain, in_decl);
7805 /* Prevent instantiate_decl from trying to instantiate
7806 this variable. We've already done all that needs to be
7807 done. */
7808 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7810 else
7811 decl = NULL_TREE;
7812 finish_handler_parms (decl, stmt);
7813 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7814 finish_handler (stmt);
7816 break;
7818 case TAG_DEFN:
7819 prep_stmt (t);
7820 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7821 break;
7823 default:
7824 abort ();
7827 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7830 /* Like tsubst but deals with expressions and performs semantic
7831 analysis. */
7833 static tree
7834 tsubst_copy_and_build (t, args, complain, in_decl)
7835 tree t, args;
7836 tsubst_flags_t complain;
7837 tree in_decl;
7839 if (t == NULL_TREE || t == error_mark_node)
7840 return t;
7842 switch (TREE_CODE (t))
7844 case IDENTIFIER_NODE:
7845 if (IDENTIFIER_TYPENAME_P (t))
7847 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7848 return do_identifier (mangle_conv_op_name_for_type (new_type),
7849 NULL_TREE);
7851 else
7852 return do_identifier (t, NULL_TREE);
7854 case LOOKUP_EXPR:
7856 if (LOOKUP_EXPR_GLOBAL (t))
7858 tree token
7859 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7860 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
7862 else
7864 t = do_identifier
7865 (tsubst_copy
7866 (TREE_OPERAND (t, 0), args, complain, in_decl),
7867 NULL_TREE);
7868 if (TREE_CODE (t) == ALIAS_DECL)
7869 t = DECL_INITIAL (t);
7870 return t;
7874 case TEMPLATE_ID_EXPR:
7876 tree object;
7877 tree template
7878 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7880 if (TREE_CODE (template) == COMPONENT_REF)
7882 object = TREE_OPERAND (template, 0);
7883 template = TREE_OPERAND (template, 1);
7885 else
7886 object = NULL_TREE;
7888 template = lookup_template_function
7889 (template,
7890 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7892 if (object)
7893 return build (COMPONENT_REF, TREE_TYPE (template),
7894 object, template);
7895 else
7896 return template;
7899 case INDIRECT_REF:
7900 return build_x_indirect_ref
7901 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7902 "unary *");
7904 case CAST_EXPR:
7905 return build_functional_cast
7906 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7907 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7909 case REINTERPRET_CAST_EXPR:
7910 return build_reinterpret_cast
7911 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7912 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7914 case CONST_CAST_EXPR:
7915 return build_const_cast
7916 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7917 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7919 case DYNAMIC_CAST_EXPR:
7920 return build_dynamic_cast
7921 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7922 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7924 case STATIC_CAST_EXPR:
7925 return build_static_cast
7926 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7927 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7929 case PREDECREMENT_EXPR:
7930 case PREINCREMENT_EXPR:
7931 case POSTDECREMENT_EXPR:
7932 case POSTINCREMENT_EXPR:
7933 if (TREE_TYPE (t))
7934 return tsubst_copy (t, args, complain, in_decl);
7935 else
7936 return build_x_unary_op
7937 (TREE_CODE (t),
7938 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7939 in_decl));
7941 case NEGATE_EXPR:
7942 case BIT_NOT_EXPR:
7943 if (TREE_TYPE (t))
7944 return tsubst_copy (t, args, complain, in_decl);
7945 else
7946 return build_x_unary_op
7947 (TREE_CODE (t),
7948 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7949 in_decl));
7951 case ABS_EXPR:
7952 if (TREE_TYPE (t))
7953 return t;
7954 return build_x_unary_op
7955 (TREE_CODE (t),
7956 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7958 case TRUTH_NOT_EXPR:
7959 case ADDR_EXPR:
7960 case CONVERT_EXPR: /* Unary + */
7961 case REALPART_EXPR:
7962 case IMAGPART_EXPR:
7963 if (TREE_TYPE (t))
7964 return tsubst_copy (t, args, complain, in_decl);
7965 else
7966 return build_x_unary_op
7967 (TREE_CODE (t),
7968 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7969 in_decl));
7971 case PLUS_EXPR:
7972 case MINUS_EXPR:
7973 case MULT_EXPR:
7974 case TRUNC_DIV_EXPR:
7975 case CEIL_DIV_EXPR:
7976 case FLOOR_DIV_EXPR:
7977 case ROUND_DIV_EXPR:
7978 case EXACT_DIV_EXPR:
7979 case BIT_AND_EXPR:
7980 case BIT_ANDTC_EXPR:
7981 case BIT_IOR_EXPR:
7982 case BIT_XOR_EXPR:
7983 case TRUNC_MOD_EXPR:
7984 case FLOOR_MOD_EXPR:
7985 case TRUTH_ANDIF_EXPR:
7986 case TRUTH_ORIF_EXPR:
7987 case TRUTH_AND_EXPR:
7988 case TRUTH_OR_EXPR:
7989 case RSHIFT_EXPR:
7990 case LSHIFT_EXPR:
7991 case RROTATE_EXPR:
7992 case LROTATE_EXPR:
7993 case EQ_EXPR:
7994 case NE_EXPR:
7995 case MAX_EXPR:
7996 case MIN_EXPR:
7997 case LE_EXPR:
7998 case GE_EXPR:
7999 case LT_EXPR:
8000 case GT_EXPR:
8001 case MEMBER_REF:
8002 return build_x_binary_op
8003 (TREE_CODE (t),
8004 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8005 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8007 case DOTSTAR_EXPR:
8008 return build_m_component_ref
8009 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8010 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8012 case SCOPE_REF:
8013 return build_offset_ref
8014 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8015 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8017 case ARRAY_REF:
8019 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8020 == NULL_TREE)
8021 /* new-type-id */
8022 return build_nt
8023 (ARRAY_REF, NULL_TREE,
8024 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8025 in_decl));
8027 return grok_array_decl
8028 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8029 in_decl),
8030 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8031 in_decl));
8034 case SIZEOF_EXPR:
8035 case ALIGNOF_EXPR:
8037 tree r =
8038 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8039 if (!TYPE_P (r))
8040 return TREE_CODE (t) == SIZEOF_EXPR ?
8041 expr_sizeof (r) : c_alignof_expr (r);
8042 else
8043 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
8046 case MODOP_EXPR:
8047 return build_x_modify_expr
8048 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8049 TREE_CODE (TREE_OPERAND (t, 1)),
8050 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8052 case ARROW_EXPR:
8053 return build_x_arrow
8054 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8056 case NEW_EXPR:
8057 return build_new
8058 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8059 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8060 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl),
8061 NEW_EXPR_USE_GLOBAL (t));
8063 case DELETE_EXPR:
8064 return delete_sanity
8065 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8066 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8067 DELETE_EXPR_USE_VEC (t),
8068 DELETE_EXPR_USE_GLOBAL (t));
8070 case COMPOUND_EXPR:
8072 if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)
8073 == NULL_TREE)
8074 return build_x_compound_expr
8075 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8076 in_decl));
8077 else
8078 abort ();
8081 case METHOD_CALL_EXPR:
8083 tree method
8084 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8086 if (TREE_CODE (method) == SCOPE_REF)
8088 tree name = TREE_OPERAND (method, 1);
8090 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8091 name = build_nt (TEMPLATE_ID_EXPR,
8092 TREE_OPERAND (name, 0),
8093 TREE_OPERAND (name, 1));
8095 return build_scoped_method_call
8096 (tsubst_copy_and_build
8097 (TREE_OPERAND (t, 1), args, complain, in_decl),
8098 TREE_OPERAND (method, 0),
8099 name,
8100 tsubst_copy_and_build
8101 (TREE_OPERAND (t, 2), args, complain, in_decl));
8103 else
8105 /* We can get a TEMPLATE_ID_EXPR here on code like:
8107 x->f<2>();
8109 so we must resolve that. However, we can also get things
8110 like a BIT_NOT_EXPR here, when referring to a destructor,
8111 and things like that are not correctly resolved by this
8112 function so just use it when we really need it. */
8113 if (TREE_CODE (method) == TEMPLATE_ID_EXPR)
8114 method = lookup_template_function
8115 (TREE_OPERAND (method, 0),
8116 TREE_OPERAND (method, 1));
8118 return build_method_call
8119 (tsubst_copy_and_build
8120 (TREE_OPERAND (t, 1), args, complain, in_decl),
8121 method,
8122 tsubst_copy_and_build
8123 (TREE_OPERAND (t, 2), args, complain, in_decl),
8124 NULL_TREE, LOOKUP_NORMAL);
8128 case CALL_EXPR:
8130 tree function, copy_args;
8132 function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8133 copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
8134 complain, in_decl);
8136 if (BASELINK_P (function))
8137 return build_call_from_tree (function, copy_args, 1);
8138 else if (TREE_CODE (function) == SCOPE_REF)
8140 tree name = TREE_OPERAND (function, 1);
8141 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8142 name = build_nt (TEMPLATE_ID_EXPR,
8143 TREE_OPERAND (name, 0),
8144 build_expr_from_tree (TREE_OPERAND (name, 1)));
8146 function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
8147 name);
8149 return build_call_from_tree (function, copy_args, 1);
8151 else
8153 tree name = function;
8154 tree id;
8156 if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
8157 && !LOOKUP_EXPR_GLOBAL (name)
8158 && (TREE_CODE ((id = TREE_OPERAND (name, 0)))
8159 == IDENTIFIER_NODE)
8160 && (!current_class_type
8161 || !lookup_member (current_class_type, id, 0, false)))
8163 /* Do Koenig lookup if there are no class members. */
8164 name = do_identifier (id, copy_args);
8166 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
8167 || ! really_overloaded_fn (name))
8168 name = build_expr_from_tree (name);
8170 if (TREE_CODE (name) == OFFSET_REF)
8171 return build_offset_ref_call_from_tree (name, copy_args);
8172 if (TREE_CODE (name) == COMPONENT_REF)
8173 return finish_object_call_expr (TREE_OPERAND (name, 1),
8174 TREE_OPERAND (name, 0),
8175 copy_args);
8176 name = convert_from_reference (name);
8177 return build_call_from_tree (name, copy_args,
8178 /*disallow_virtual=*/false);
8182 case COND_EXPR:
8183 return build_x_conditional_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 PSEUDO_DTOR_EXPR:
8189 return finish_pseudo_destructor_expr
8190 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8191 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8192 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8194 case TREE_LIST:
8196 tree purpose, value, chain;
8198 if (t == void_list_node)
8199 return t;
8201 purpose = TREE_PURPOSE (t);
8202 if (purpose)
8203 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
8204 value = TREE_VALUE (t);
8205 if (value)
8206 value = tsubst_copy_and_build (value, args, complain, in_decl);
8207 chain = TREE_CHAIN (t);
8208 if (chain && chain != void_type_node)
8209 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
8210 if (purpose == TREE_PURPOSE (t)
8211 && value == TREE_VALUE (t)
8212 && chain == TREE_CHAIN (t))
8213 return t;
8214 return tree_cons (purpose, value, chain);
8217 case COMPONENT_REF:
8219 tree object =
8220 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8221 tree member =
8222 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
8224 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8226 if (TREE_CODE (member) == BIT_NOT_EXPR)
8227 return finish_pseudo_destructor_expr (object,
8228 NULL_TREE,
8229 TREE_TYPE (object));
8230 else if (TREE_CODE (member) == SCOPE_REF
8231 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8232 return finish_pseudo_destructor_expr (object,
8233 object,
8234 TREE_TYPE (object));
8236 else if (TREE_CODE (member) == SCOPE_REF
8237 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8239 tree tmpl;
8240 tree args;
8242 /* Lookup the template functions now that we know what the
8243 scope is. */
8244 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8245 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8246 member = lookup_qualified_name (TREE_OPERAND (member, 0),
8247 tmpl,
8248 /*is_type=*/0,
8249 /*flags=*/0);
8250 if (BASELINK_P (member))
8251 BASELINK_FUNCTIONS (member)
8252 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8253 args);
8254 else
8256 error ("`%D' is not a member of `%T'",
8257 tmpl, TREE_TYPE (object));
8258 return error_mark_node;
8262 return finish_class_member_access_expr (object, member);
8265 case THROW_EXPR:
8266 return build_throw
8267 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8269 case CONSTRUCTOR:
8271 tree r;
8272 tree elts;
8273 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8274 bool purpose_p;
8276 /* digest_init will do the wrong thing if we let it. */
8277 if (type && TYPE_PTRMEMFUNC_P (type))
8278 return t;
8280 r = NULL_TREE;
8281 /* We do not want to process the purpose of aggregate
8282 initializers as they are identifier nodes which will be
8283 looked up by digest_init. */
8284 purpose_p = !(type && IS_AGGR_TYPE (type));
8285 for (elts = CONSTRUCTOR_ELTS (t);
8286 elts;
8287 elts = TREE_CHAIN (elts))
8289 tree purpose = TREE_PURPOSE (elts);
8290 tree value = TREE_VALUE (elts);
8292 if (purpose && purpose_p)
8293 purpose
8294 = tsubst_copy_and_build (purpose, args, complain, in_decl);
8295 value = tsubst_copy_and_build (value, args, complain, in_decl);
8296 r = tree_cons (purpose, value, r);
8299 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
8300 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8302 if (type)
8303 return digest_init (type, r, 0);
8304 return r;
8307 case TYPEID_EXPR:
8309 tree operand_0
8310 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8311 in_decl);
8313 if (TYPE_P (operand_0))
8314 return get_typeid (operand_0);
8315 return build_typeid (operand_0);
8318 case PARM_DECL:
8319 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8321 case VAR_DECL:
8322 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8324 case VA_ARG_EXPR:
8325 return build_x_va_arg
8326 (tsubst_copy_and_build
8327 (TREE_OPERAND (t, 0), args, complain, in_decl),
8328 tsubst_copy (TREE_TYPE (t), args, complain, in_decl));
8330 default:
8331 return tsubst_copy (t, args, complain, in_decl);
8335 /* Instantiate the indicated variable or function template TMPL with
8336 the template arguments in TARG_PTR. */
8338 tree
8339 instantiate_template (tmpl, targ_ptr)
8340 tree tmpl, targ_ptr;
8342 tree fndecl;
8343 tree gen_tmpl;
8344 tree spec;
8345 int i, len;
8346 tree inner_args;
8348 if (tmpl == error_mark_node)
8349 return error_mark_node;
8351 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
8353 /* If this function is a clone, handle it specially. */
8354 if (DECL_CLONED_FUNCTION_P (tmpl))
8356 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
8357 tree clone;
8359 /* Look for the clone. */
8360 for (clone = TREE_CHAIN (spec);
8361 clone && DECL_CLONED_FUNCTION_P (clone);
8362 clone = TREE_CHAIN (clone))
8363 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8364 return clone;
8365 /* We should always have found the clone by now. */
8366 abort ();
8367 return NULL_TREE;
8370 /* Check to see if we already have this specialization. */
8371 spec = retrieve_specialization (tmpl, targ_ptr);
8372 if (spec != NULL_TREE)
8373 return spec;
8375 gen_tmpl = most_general_template (tmpl);
8376 if (tmpl != gen_tmpl)
8378 /* The TMPL is a partial instantiation. To get a full set of
8379 arguments we must add the arguments used to perform the
8380 partial instantiation. */
8381 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8382 targ_ptr);
8384 /* Check to see if we already have this specialization. */
8385 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8386 if (spec != NULL_TREE)
8387 return spec;
8390 len = DECL_NTPARMS (gen_tmpl);
8391 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
8392 i = len;
8393 while (i--)
8395 tree t = TREE_VEC_ELT (inner_args, i);
8396 if (TYPE_P (t))
8398 tree nt = target_type (t);
8399 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
8401 error ("type `%T' composed from a local class is not a valid template-argument", t);
8402 error (" trying to instantiate `%D'", gen_tmpl);
8403 return error_mark_node;
8408 /* Make sure that we can see identifiers, and compute access
8409 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8410 created earlier. Let push_access_scope_real figure that out. */
8411 push_access_scope_real
8412 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
8413 tf_error, gen_tmpl));
8415 /* substitute template parameters */
8416 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8417 targ_ptr, tf_error, gen_tmpl);
8419 pop_access_scope (gen_tmpl);
8421 /* The DECL_TI_TEMPLATE should always be the immediate parent
8422 template, not the most general template. */
8423 DECL_TI_TEMPLATE (fndecl) = tmpl;
8425 if (flag_external_templates)
8426 add_pending_template (fndecl);
8428 /* If we've just instantiated the main entry point for a function,
8429 instantiate all the alternate entry points as well. We do this
8430 by cloning the instantiation of the main entry point, not by
8431 instantiating the template clones. */
8432 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8433 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8435 return fndecl;
8438 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8439 arguments that are being used when calling it. TARGS is a vector
8440 into which the deduced template arguments are placed.
8442 Return zero for success, 2 for an incomplete match that doesn't resolve
8443 all the types, and 1 for complete failure. An error message will be
8444 printed only for an incomplete match.
8446 If FN is a conversion operator, or we are trying to produce a specific
8447 specialization, RETURN_TYPE is the return type desired.
8449 The EXPLICIT_TARGS are explicit template arguments provided via a
8450 template-id.
8452 The parameter STRICT is one of:
8454 DEDUCE_CALL:
8455 We are deducing arguments for a function call, as in
8456 [temp.deduct.call].
8458 DEDUCE_CONV:
8459 We are deducing arguments for a conversion function, as in
8460 [temp.deduct.conv].
8462 DEDUCE_EXACT:
8463 We are deducing arguments when doing an explicit instantiation
8464 as in [temp.explicit], when determining an explicit specialization
8465 as in [temp.expl.spec], or when taking the address of a function
8466 template, as in [temp.deduct.funcaddr].
8468 DEDUCE_ORDER:
8469 We are deducing arguments when calculating the partial
8470 ordering between specializations of function or class
8471 templates, as in [temp.func.order] and [temp.class.order].
8473 LEN is the number of parms to consider before returning success, or -1
8474 for all. This is used in partial ordering to avoid comparing parms for
8475 which no actual argument was passed, since they are not considered in
8476 overload resolution (and are explicitly excluded from consideration in
8477 partial ordering in [temp.func.order]/6). */
8480 fn_type_unification (fn, explicit_targs, targs, args, return_type,
8481 strict, len)
8482 tree fn, explicit_targs, targs, args, return_type;
8483 unification_kind_t strict;
8484 int len;
8486 tree parms;
8487 tree fntype;
8488 int result;
8490 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
8492 fntype = TREE_TYPE (fn);
8493 if (explicit_targs)
8495 /* [temp.deduct]
8497 The specified template arguments must match the template
8498 parameters in kind (i.e., type, nontype, template), and there
8499 must not be more arguments than there are parameters;
8500 otherwise type deduction fails.
8502 Nontype arguments must match the types of the corresponding
8503 nontype template parameters, or must be convertible to the
8504 types of the corresponding nontype parameters as specified in
8505 _temp.arg.nontype_, otherwise type deduction fails.
8507 All references in the function type of the function template
8508 to the corresponding template parameters are replaced by the
8509 specified template argument values. If a substitution in a
8510 template parameter or in the function type of the function
8511 template results in an invalid type, type deduction fails. */
8512 int i;
8513 tree converted_args;
8515 converted_args
8516 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8517 explicit_targs, NULL_TREE, tf_none,
8518 /*require_all_arguments=*/0));
8519 if (converted_args == error_mark_node)
8520 return 1;
8522 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8523 if (fntype == error_mark_node)
8524 return 1;
8526 /* Place the explicitly specified arguments in TARGS. */
8527 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
8528 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8531 parms = TYPE_ARG_TYPES (fntype);
8532 /* Never do unification on the 'this' parameter. */
8533 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8534 parms = TREE_CHAIN (parms);
8536 if (return_type)
8538 /* We've been given a return type to match, prepend it. */
8539 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8540 args = tree_cons (NULL_TREE, return_type, args);
8541 if (len >= 0)
8542 ++len;
8545 /* We allow incomplete unification without an error message here
8546 because the standard doesn't seem to explicitly prohibit it. Our
8547 callers must be ready to deal with unification failures in any
8548 event. */
8549 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8550 targs, parms, args, /*subr=*/0,
8551 strict, /*allow_incomplete*/1, len);
8553 if (result == 0)
8554 /* All is well so far. Now, check:
8556 [temp.deduct]
8558 When all template arguments have been deduced, all uses of
8559 template parameters in nondeduced contexts are replaced with
8560 the corresponding deduced argument values. If the
8561 substitution results in an invalid type, as described above,
8562 type deduction fails. */
8563 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8564 == error_mark_node)
8565 return 1;
8567 return result;
8570 /* Adjust types before performing type deduction, as described in
8571 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8572 sections are symmetric. PARM is the type of a function parameter
8573 or the return type of the conversion function. ARG is the type of
8574 the argument passed to the call, or the type of the value
8575 initialized with the result of the conversion function. */
8577 static int
8578 maybe_adjust_types_for_deduction (strict, parm, arg)
8579 unification_kind_t strict;
8580 tree* parm;
8581 tree* arg;
8583 int result = 0;
8585 switch (strict)
8587 case DEDUCE_CALL:
8588 break;
8590 case DEDUCE_CONV:
8592 /* Swap PARM and ARG throughout the remainder of this
8593 function; the handling is precisely symmetric since PARM
8594 will initialize ARG rather than vice versa. */
8595 tree* temp = parm;
8596 parm = arg;
8597 arg = temp;
8598 break;
8601 case DEDUCE_EXACT:
8602 /* There is nothing to do in this case. */
8603 return 0;
8605 case DEDUCE_ORDER:
8606 /* DR 214. [temp.func.order] is underspecified, and leads to no
8607 ordering between things like `T *' and `T const &' for `U *'.
8608 The former has T=U and the latter T=U*. The former looks more
8609 specialized and John Spicer considers it well-formed (the EDG
8610 compiler accepts it).
8612 John also confirms that deduction should proceed as in a function
8613 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8614 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8615 to an actual call can have such a type.
8617 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8618 If only ARG is a REFERENCE_TYPE, we look through that and then
8619 proceed as with DEDUCE_CALL (which could further convert it). */
8620 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8622 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8623 return 0;
8624 *arg = TREE_TYPE (*arg);
8626 break;
8627 default:
8628 abort ();
8631 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8633 /* [temp.deduct.call]
8635 If P is not a reference type:
8637 --If A is an array type, the pointer type produced by the
8638 array-to-pointer standard conversion (_conv.array_) is
8639 used in place of A for type deduction; otherwise,
8641 --If A is a function type, the pointer type produced by
8642 the function-to-pointer standard conversion
8643 (_conv.func_) is used in place of A for type deduction;
8644 otherwise,
8646 --If A is a cv-qualified type, the top level
8647 cv-qualifiers of A's type are ignored for type
8648 deduction. */
8649 if (TREE_CODE (*arg) == ARRAY_TYPE)
8650 *arg = build_pointer_type (TREE_TYPE (*arg));
8651 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
8652 *arg = build_pointer_type (*arg);
8653 else
8654 *arg = TYPE_MAIN_VARIANT (*arg);
8657 /* [temp.deduct.call]
8659 If P is a cv-qualified type, the top level cv-qualifiers
8660 of P's type are ignored for type deduction. If P is a
8661 reference type, the type referred to by P is used for
8662 type deduction. */
8663 *parm = TYPE_MAIN_VARIANT (*parm);
8664 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8666 *parm = TREE_TYPE (*parm);
8667 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8669 return result;
8672 /* Most parms like fn_type_unification.
8674 If SUBR is 1, we're being called recursively (to unify the
8675 arguments of a function or method parameter of a function
8676 template). */
8678 static int
8679 type_unification_real (tparms, targs, xparms, xargs, subr,
8680 strict, allow_incomplete, xlen)
8681 tree tparms, targs, xparms, xargs;
8682 int subr;
8683 unification_kind_t strict;
8684 int allow_incomplete, xlen;
8686 tree parm, arg;
8687 int i;
8688 int ntparms = TREE_VEC_LENGTH (tparms);
8689 int sub_strict;
8690 int saw_undeduced = 0;
8691 tree parms, args;
8692 int len;
8694 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
8695 my_friendly_assert (xparms == NULL_TREE
8696 || TREE_CODE (xparms) == TREE_LIST, 290);
8697 /* ARGS could be NULL. */
8698 if (xargs)
8699 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8700 my_friendly_assert (ntparms > 0, 292);
8702 switch (strict)
8704 case DEDUCE_CALL:
8705 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8706 | UNIFY_ALLOW_DERIVED);
8707 break;
8709 case DEDUCE_CONV:
8710 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8711 break;
8713 case DEDUCE_EXACT:
8714 sub_strict = UNIFY_ALLOW_NONE;
8715 break;
8717 case DEDUCE_ORDER:
8718 sub_strict = UNIFY_ALLOW_NONE;
8719 break;
8721 default:
8722 abort ();
8725 if (xlen == 0)
8726 return 0;
8728 again:
8729 parms = xparms;
8730 args = xargs;
8731 len = xlen;
8733 while (parms
8734 && parms != void_list_node
8735 && args
8736 && args != void_list_node)
8738 parm = TREE_VALUE (parms);
8739 parms = TREE_CHAIN (parms);
8740 arg = TREE_VALUE (args);
8741 args = TREE_CHAIN (args);
8743 if (arg == error_mark_node)
8744 return 1;
8745 if (arg == unknown_type_node)
8746 /* We can't deduce anything from this, but we might get all the
8747 template args from other function args. */
8748 continue;
8750 /* Conversions will be performed on a function argument that
8751 corresponds with a function parameter that contains only
8752 non-deducible template parameters and explicitly specified
8753 template parameters. */
8754 if (! uses_template_parms (parm))
8756 tree type;
8758 if (!TYPE_P (arg))
8759 type = TREE_TYPE (arg);
8760 else
8762 type = arg;
8763 arg = NULL_TREE;
8766 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
8768 if (same_type_p (parm, type))
8769 continue;
8771 else
8772 /* It might work; we shouldn't check now, because we might
8773 get into infinite recursion. Overload resolution will
8774 handle it. */
8775 continue;
8777 return 1;
8780 if (!TYPE_P (arg))
8782 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8783 if (type_unknown_p (arg))
8785 /* [temp.deduct.type] A template-argument can be deduced from
8786 a pointer to function or pointer to member function
8787 argument if the set of overloaded functions does not
8788 contain function templates and at most one of a set of
8789 overloaded functions provides a unique match. */
8791 if (resolve_overloaded_unification
8792 (tparms, targs, parm, arg, strict, sub_strict)
8793 != 0)
8794 return 1;
8795 continue;
8797 arg = TREE_TYPE (arg);
8801 int arg_strict = sub_strict;
8803 if (!subr)
8804 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8806 if (unify (tparms, targs, parm, arg, arg_strict))
8807 return 1;
8810 /* Are we done with the interesting parms? */
8811 if (--len == 0)
8812 goto done;
8814 /* Fail if we've reached the end of the parm list, and more args
8815 are present, and the parm list isn't variadic. */
8816 if (args && args != void_list_node && parms == void_list_node)
8817 return 1;
8818 /* Fail if parms are left and they don't have default values. */
8819 if (parms
8820 && parms != void_list_node
8821 && TREE_PURPOSE (parms) == NULL_TREE)
8822 return 1;
8824 done:
8825 if (!subr)
8826 for (i = 0; i < ntparms; i++)
8827 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8829 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8831 /* If this is an undeduced nontype parameter that depends on
8832 a type parameter, try another pass; its type may have been
8833 deduced from a later argument than the one from which
8834 this parameter can be deduced. */
8835 if (TREE_CODE (tparm) == PARM_DECL
8836 && uses_template_parms (TREE_TYPE (tparm))
8837 && !saw_undeduced++)
8838 goto again;
8840 if (!allow_incomplete)
8841 error ("incomplete type unification");
8842 return 2;
8844 return 0;
8847 /* Subroutine of type_unification_real. Args are like the variables at the
8848 call site. ARG is an overloaded function (or template-id); we try
8849 deducing template args from each of the overloads, and if only one
8850 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8852 static int
8853 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8854 sub_strict)
8855 tree tparms, targs, parm, arg;
8856 unification_kind_t strict;
8857 int sub_strict;
8859 tree tempargs = copy_node (targs);
8860 int good = 0;
8862 if (TREE_CODE (arg) == ADDR_EXPR)
8863 arg = TREE_OPERAND (arg, 0);
8865 if (TREE_CODE (arg) == COMPONENT_REF)
8866 /* Handle `&x' where `x' is some static or non-static member
8867 function name. */
8868 arg = TREE_OPERAND (arg, 1);
8870 if (TREE_CODE (arg) == OFFSET_REF)
8871 arg = TREE_OPERAND (arg, 1);
8873 /* Strip baselink information. */
8874 if (BASELINK_P (arg))
8875 arg = BASELINK_FUNCTIONS (arg);
8877 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8879 /* If we got some explicit template args, we need to plug them into
8880 the affected templates before we try to unify, in case the
8881 explicit args will completely resolve the templates in question. */
8883 tree expl_subargs = TREE_OPERAND (arg, 1);
8884 arg = TREE_OPERAND (arg, 0);
8886 for (; arg; arg = OVL_NEXT (arg))
8888 tree fn = OVL_CURRENT (arg);
8889 tree subargs, elem;
8891 if (TREE_CODE (fn) != TEMPLATE_DECL)
8892 continue;
8894 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8895 expl_subargs);
8896 if (subargs)
8898 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
8899 if (TREE_CODE (elem) == METHOD_TYPE)
8900 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8901 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8902 strict, sub_strict);
8906 else if (TREE_CODE (arg) == OVERLOAD
8907 || TREE_CODE (arg) == FUNCTION_DECL)
8909 for (; arg; arg = OVL_NEXT (arg))
8911 tree type = TREE_TYPE (OVL_CURRENT (arg));
8912 if (TREE_CODE (type) == METHOD_TYPE)
8913 type = build_ptrmemfunc_type (build_pointer_type (type));
8914 good += try_one_overload (tparms, targs, tempargs, parm,
8915 type,
8916 strict, sub_strict);
8919 else
8920 abort ();
8922 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8923 to function or pointer to member function argument if the set of
8924 overloaded functions does not contain function templates and at most
8925 one of a set of overloaded functions provides a unique match.
8927 So if we found multiple possibilities, we return success but don't
8928 deduce anything. */
8930 if (good == 1)
8932 int i = TREE_VEC_LENGTH (targs);
8933 for (; i--; )
8934 if (TREE_VEC_ELT (tempargs, i))
8935 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8937 if (good)
8938 return 0;
8940 return 1;
8943 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8944 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8945 different overloads deduce different arguments for a given parm.
8946 Returns 1 on success. */
8948 static int
8949 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8950 sub_strict)
8951 tree tparms, orig_targs, targs, parm, arg;
8952 unification_kind_t strict;
8953 int sub_strict;
8955 int nargs;
8956 tree tempargs;
8957 int i;
8959 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8960 to function or pointer to member function argument if the set of
8961 overloaded functions does not contain function templates and at most
8962 one of a set of overloaded functions provides a unique match.
8964 So if this is a template, just return success. */
8966 if (uses_template_parms (arg))
8967 return 1;
8969 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8971 /* We don't copy orig_targs for this because if we have already deduced
8972 some template args from previous args, unify would complain when we
8973 try to deduce a template parameter for the same argument, even though
8974 there isn't really a conflict. */
8975 nargs = TREE_VEC_LENGTH (targs);
8976 tempargs = make_tree_vec (nargs);
8978 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8979 return 0;
8981 /* First make sure we didn't deduce anything that conflicts with
8982 explicitly specified args. */
8983 for (i = nargs; i--; )
8985 tree elt = TREE_VEC_ELT (tempargs, i);
8986 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8988 if (elt == NULL_TREE)
8989 continue;
8990 else if (uses_template_parms (elt))
8992 /* Since we're unifying against ourselves, we will fill in template
8993 args used in the function parm list with our own template parms.
8994 Discard them. */
8995 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8996 continue;
8998 else if (oldelt && ! template_args_equal (oldelt, elt))
8999 return 0;
9002 for (i = nargs; i--; )
9004 tree elt = TREE_VEC_ELT (tempargs, i);
9006 if (elt)
9007 TREE_VEC_ELT (targs, i) = elt;
9010 return 1;
9013 /* Verify that nondeduce template argument agrees with the type
9014 obtained from argument deduction. Return nonzero if the
9015 verification fails.
9017 For example:
9019 struct A { typedef int X; };
9020 template <class T, class U> struct C {};
9021 template <class T> struct C<T, typename T::X> {};
9023 Then with the instantiation `C<A, int>', we can deduce that
9024 `T' is `A' but unify () does not check whether `typename T::X'
9025 is `int'. This function ensure that they agree.
9027 TARGS, PARMS are the same as the arguments of unify.
9028 ARGS contains template arguments from all levels. */
9030 static int
9031 verify_class_unification (targs, parms, args)
9032 tree targs, parms, args;
9034 parms = tsubst (parms, add_outermost_template_args (args, targs),
9035 tf_none, NULL_TREE);
9036 if (parms == error_mark_node)
9037 return 1;
9039 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9042 /* PARM is a template class (perhaps with unbound template
9043 parameters). ARG is a fully instantiated type. If ARG can be
9044 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9045 TARGS are as for unify. */
9047 static tree
9048 try_class_unification (tparms, targs, parm, arg)
9049 tree tparms;
9050 tree targs;
9051 tree parm;
9052 tree arg;
9054 tree copy_of_targs;
9056 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9057 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
9058 return NULL_TREE;
9060 /* We need to make a new template argument vector for the call to
9061 unify. If we used TARGS, we'd clutter it up with the result of
9062 the attempted unification, even if this class didn't work out.
9063 We also don't want to commit ourselves to all the unifications
9064 we've already done, since unification is supposed to be done on
9065 an argument-by-argument basis. In other words, consider the
9066 following pathological case:
9068 template <int I, int J, int K>
9069 struct S {};
9071 template <int I, int J>
9072 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9074 template <int I, int J, int K>
9075 void f(S<I, J, K>, S<I, I, I>);
9077 void g() {
9078 S<0, 0, 0> s0;
9079 S<0, 1, 2> s2;
9081 f(s0, s2);
9084 Now, by the time we consider the unification involving `s2', we
9085 already know that we must have `f<0, 0, 0>'. But, even though
9086 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9087 because there are two ways to unify base classes of S<0, 1, 2>
9088 with S<I, I, I>. If we kept the already deduced knowledge, we
9089 would reject the possibility I=1. */
9090 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9092 /* If unification failed, we're done. */
9093 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9094 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9095 return NULL_TREE;
9097 return arg;
9100 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9101 have already discovered to be satisfactory. ARG_BINFO is the binfo
9102 for the base class of ARG that we are currently examining. */
9104 static tree
9105 get_template_base_recursive (tparms, targs, parm,
9106 arg_binfo, rval, flags)
9107 tree tparms;
9108 tree targs;
9109 tree arg_binfo;
9110 tree rval;
9111 tree parm;
9112 int flags;
9114 tree binfos;
9115 int i, n_baselinks;
9116 tree arg = BINFO_TYPE (arg_binfo);
9118 if (!(flags & GTB_IGNORE_TYPE))
9120 tree r = try_class_unification (tparms, targs,
9121 parm, arg);
9123 /* If there is more than one satisfactory baseclass, then:
9125 [temp.deduct.call]
9127 If they yield more than one possible deduced A, the type
9128 deduction fails.
9130 applies. */
9131 if (r && rval && !same_type_p (r, rval))
9132 return error_mark_node;
9133 else if (r)
9134 rval = r;
9137 binfos = BINFO_BASETYPES (arg_binfo);
9138 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9140 /* Process base types. */
9141 for (i = 0; i < n_baselinks; i++)
9143 tree base_binfo = TREE_VEC_ELT (binfos, i);
9144 int this_virtual;
9146 /* Skip this base, if we've already seen it. */
9147 if (BINFO_MARKED (base_binfo))
9148 continue;
9150 this_virtual =
9151 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9153 /* When searching for a non-virtual, we cannot mark virtually
9154 found binfos. */
9155 if (! this_virtual)
9156 BINFO_MARKED (base_binfo) = 1;
9158 rval = get_template_base_recursive (tparms, targs,
9159 parm,
9160 base_binfo,
9161 rval,
9162 GTB_VIA_VIRTUAL * this_virtual);
9164 /* If we discovered more than one matching base class, we can
9165 stop now. */
9166 if (rval == error_mark_node)
9167 return error_mark_node;
9170 return rval;
9173 /* Given a template type PARM and a class type ARG, find the unique
9174 base type in ARG that is an instance of PARM. We do not examine
9175 ARG itself; only its base-classes. If there is no appropriate base
9176 class, return NULL_TREE. If there is more than one, return
9177 error_mark_node. PARM may be the type of a partial specialization,
9178 as well as a plain template type. Used by unify. */
9180 static tree
9181 get_template_base (tparms, targs, parm, arg)
9182 tree tparms;
9183 tree targs;
9184 tree parm;
9185 tree arg;
9187 tree rval;
9188 tree arg_binfo;
9190 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9192 arg_binfo = TYPE_BINFO (complete_type (arg));
9193 rval = get_template_base_recursive (tparms, targs,
9194 parm, arg_binfo,
9195 NULL_TREE,
9196 GTB_IGNORE_TYPE);
9198 /* Since get_template_base_recursive marks the bases classes, we
9199 must unmark them here. */
9200 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9202 return rval;
9205 /* Returns the level of DECL, which declares a template parameter. */
9207 static int
9208 template_decl_level (decl)
9209 tree decl;
9211 switch (TREE_CODE (decl))
9213 case TYPE_DECL:
9214 case TEMPLATE_DECL:
9215 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9217 case PARM_DECL:
9218 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9220 default:
9221 abort ();
9222 return 0;
9226 /* Decide whether ARG can be unified with PARM, considering only the
9227 cv-qualifiers of each type, given STRICT as documented for unify.
9228 Returns nonzero iff the unification is OK on that basis.*/
9230 static int
9231 check_cv_quals_for_unify (strict, arg, parm)
9232 int strict;
9233 tree arg;
9234 tree parm;
9236 int arg_quals = cp_type_quals (arg);
9237 int parm_quals = cp_type_quals (parm);
9239 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
9241 /* If the cvr quals of parm will not unify with ARG, they'll be
9242 ignored in instantiation, so we have to do the same here. */
9243 if (TREE_CODE (arg) == REFERENCE_TYPE)
9244 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
9245 if (!POINTER_TYPE_P (arg) &&
9246 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9247 parm_quals &= ~TYPE_QUAL_RESTRICT;
9250 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9251 && (arg_quals & parm_quals) != parm_quals)
9252 return 0;
9254 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9255 && (parm_quals & arg_quals) != arg_quals)
9256 return 0;
9258 return 1;
9261 /* Takes parameters as for type_unification. Returns 0 if the
9262 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9263 bitwise or of the following flags:
9265 UNIFY_ALLOW_NONE:
9266 Require an exact match between PARM and ARG.
9267 UNIFY_ALLOW_MORE_CV_QUAL:
9268 Allow the deduced ARG to be more cv-qualified (by qualification
9269 conversion) than ARG.
9270 UNIFY_ALLOW_LESS_CV_QUAL:
9271 Allow the deduced ARG to be less cv-qualified than ARG.
9272 UNIFY_ALLOW_DERIVED:
9273 Allow the deduced ARG to be a template base class of ARG,
9274 or a pointer to a template base class of the type pointed to by
9275 ARG.
9276 UNIFY_ALLOW_INTEGER:
9277 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9278 case for more information.
9279 UNIFY_ALLOW_OUTER_LEVEL:
9280 This is the outermost level of a deduction. Used to determine validity
9281 of qualification conversions. A valid qualification conversion must
9282 have const qualified pointers leading up to the inner type which
9283 requires additional CV quals, except at the outer level, where const
9284 is not required [conv.qual]. It would be normal to set this flag in
9285 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9286 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9287 This is the outermost level of a deduction, and PARM can be more CV
9288 qualified at this point.
9289 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9290 This is the outermost level of a deduction, and PARM can be less CV
9291 qualified at this point.
9292 UNIFY_ALLOW_MAX_CORRECTION:
9293 This is an INTEGER_TYPE's maximum value. Used if the range may
9294 have been derived from a size specification, such as an array size.
9295 If the size was given by a nontype template parameter N, the maximum
9296 value will have the form N-1. The flag says that we can (and indeed
9297 must) unify N with (ARG + 1), an exception to the normal rules on
9298 folding PARM. */
9300 static int
9301 unify (tparms, targs, parm, arg, strict)
9302 tree tparms, targs, parm, arg;
9303 int strict;
9305 int idx;
9306 tree targ;
9307 tree tparm;
9308 int strict_in = strict;
9310 /* I don't think this will do the right thing with respect to types.
9311 But the only case I've seen it in so far has been array bounds, where
9312 signedness is the only information lost, and I think that will be
9313 okay. */
9314 while (TREE_CODE (parm) == NOP_EXPR)
9315 parm = TREE_OPERAND (parm, 0);
9317 if (arg == error_mark_node)
9318 return 1;
9319 if (arg == unknown_type_node)
9320 /* We can't deduce anything from this, but we might get all the
9321 template args from other function args. */
9322 return 0;
9324 /* If PARM uses template parameters, then we can't bail out here,
9325 even if ARG == PARM, since we won't record unifications for the
9326 template parameters. We might need them if we're trying to
9327 figure out which of two things is more specialized. */
9328 if (arg == parm && !uses_template_parms (parm))
9329 return 0;
9331 /* Immediately reject some pairs that won't unify because of
9332 cv-qualification mismatches. */
9333 if (TREE_CODE (arg) == TREE_CODE (parm)
9334 && TYPE_P (arg)
9335 /* It is the elements of the array which hold the cv quals of an array
9336 type, and the elements might be template type parms. We'll check
9337 when we recurse. */
9338 && TREE_CODE (arg) != ARRAY_TYPE
9339 /* We check the cv-qualifiers when unifying with template type
9340 parameters below. We want to allow ARG `const T' to unify with
9341 PARM `T' for example, when computing which of two templates
9342 is more specialized, for example. */
9343 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9344 && !check_cv_quals_for_unify (strict_in, arg, parm))
9345 return 1;
9347 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9348 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9349 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9350 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9351 strict &= ~UNIFY_ALLOW_DERIVED;
9352 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9353 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9354 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
9356 switch (TREE_CODE (parm))
9358 case TYPENAME_TYPE:
9359 case SCOPE_REF:
9360 case UNBOUND_CLASS_TEMPLATE:
9361 /* In a type which contains a nested-name-specifier, template
9362 argument values cannot be deduced for template parameters used
9363 within the nested-name-specifier. */
9364 return 0;
9366 case TEMPLATE_TYPE_PARM:
9367 case TEMPLATE_TEMPLATE_PARM:
9368 case BOUND_TEMPLATE_TEMPLATE_PARM:
9369 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9371 if (TEMPLATE_TYPE_LEVEL (parm)
9372 != template_decl_level (tparm))
9373 /* The PARM is not one we're trying to unify. Just check
9374 to see if it matches ARG. */
9375 return (TREE_CODE (arg) == TREE_CODE (parm)
9376 && same_type_p (parm, arg)) ? 0 : 1;
9377 idx = TEMPLATE_TYPE_IDX (parm);
9378 targ = TREE_VEC_ELT (targs, idx);
9379 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9381 /* Check for mixed types and values. */
9382 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9383 && TREE_CODE (tparm) != TYPE_DECL)
9384 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9385 && TREE_CODE (tparm) != TEMPLATE_DECL))
9386 return 1;
9388 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9390 /* ARG must be constructed from a template class or a template
9391 template parameter. */
9392 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9393 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9394 return 1;
9397 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9398 tree parmvec = TYPE_TI_ARGS (parm);
9399 tree argvec = TYPE_TI_ARGS (arg);
9400 tree argtmplvec
9401 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9402 int i;
9404 /* The parameter and argument roles have to be switched here
9405 in order to handle default arguments properly. For example,
9406 template<template <class> class TT> void f(TT<int>)
9407 should be able to accept vector<int> which comes from
9408 template <class T, class Allocator = allocator>
9409 class vector. */
9411 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9412 == error_mark_node)
9413 return 1;
9415 /* Deduce arguments T, i from TT<T> or TT<i>.
9416 We check each element of PARMVEC and ARGVEC individually
9417 rather than the whole TREE_VEC since they can have
9418 different number of elements. */
9420 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9422 tree t = TREE_VEC_ELT (parmvec, i);
9424 if (unify (tparms, targs, t,
9425 TREE_VEC_ELT (argvec, i),
9426 UNIFY_ALLOW_NONE))
9427 return 1;
9430 arg = TYPE_TI_TEMPLATE (arg);
9432 /* Fall through to deduce template name. */
9435 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9436 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9438 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9440 /* Simple cases: Value already set, does match or doesn't. */
9441 if (targ != NULL_TREE && template_args_equal (targ, arg))
9442 return 0;
9443 else if (targ)
9444 return 1;
9446 else
9448 /* If ARG is an offset type, we're trying to unify '*T' with
9449 'U C::*', which is ill-formed. See the comment in the
9450 POINTER_TYPE case about this ugliness. */
9451 if (TREE_CODE (arg) == OFFSET_TYPE)
9452 return 1;
9454 /* If PARM is `const T' and ARG is only `int', we don't have
9455 a match unless we are allowing additional qualification.
9456 If ARG is `const int' and PARM is just `T' that's OK;
9457 that binds `const int' to `T'. */
9458 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9459 arg, parm))
9460 return 1;
9462 /* Consider the case where ARG is `const volatile int' and
9463 PARM is `const T'. Then, T should be `volatile int'. */
9464 arg = cp_build_qualified_type_real
9465 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9466 if (arg == error_mark_node)
9467 return 1;
9469 /* Simple cases: Value already set, does match or doesn't. */
9470 if (targ != NULL_TREE && same_type_p (targ, arg))
9471 return 0;
9472 else if (targ)
9473 return 1;
9475 /* Make sure that ARG is not a variable-sized array. (Note
9476 that were talking about variable-sized arrays (like
9477 `int[n]'), rather than arrays of unknown size (like
9478 `int[]').) We'll get very confused by such a type since
9479 the bound of the array will not be computable in an
9480 instantiation. Besides, such types are not allowed in
9481 ISO C++, so we can do as we please here. */
9482 if (variably_modified_type_p (arg))
9483 return 1;
9486 TREE_VEC_ELT (targs, idx) = arg;
9487 return 0;
9489 case TEMPLATE_PARM_INDEX:
9490 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9492 if (TEMPLATE_PARM_LEVEL (parm)
9493 != template_decl_level (tparm))
9494 /* The PARM is not one we're trying to unify. Just check
9495 to see if it matches ARG. */
9496 return (TREE_CODE (arg) == TREE_CODE (parm)
9497 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
9499 idx = TEMPLATE_PARM_IDX (parm);
9500 targ = TREE_VEC_ELT (targs, idx);
9502 if (targ)
9504 int i = (cp_tree_equal (targ, arg) > 0);
9505 if (i == 1)
9506 return 0;
9507 else if (i == 0)
9508 return 1;
9509 else
9510 abort ();
9513 /* [temp.deduct.type] If, in the declaration of a function template
9514 with a non-type template-parameter, the non-type
9515 template-parameter is used in an expression in the function
9516 parameter-list and, if the corresponding template-argument is
9517 deduced, the template-argument type shall match the type of the
9518 template-parameter exactly, except that a template-argument
9519 deduced from an array bound may be of any integral type.
9520 The non-type parameter might use already deduced type parameters. */
9521 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9522 if (!TREE_TYPE (arg))
9523 /* Template-parameter dependent expression. Just accept it for now.
9524 It will later be processed in convert_template_argument. */
9526 else if (same_type_p (TREE_TYPE (arg), tparm))
9527 /* OK */;
9528 else if ((strict & UNIFY_ALLOW_INTEGER)
9529 && (TREE_CODE (tparm) == INTEGER_TYPE
9530 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9531 /* OK */;
9532 else if (uses_template_parms (tparm))
9533 /* We haven't deduced the type of this parameter yet. Try again
9534 later. */
9535 return 0;
9536 else
9537 return 1;
9539 TREE_VEC_ELT (targs, idx) = arg;
9540 return 0;
9542 case POINTER_TYPE:
9544 if (TREE_CODE (arg) != POINTER_TYPE)
9545 return 1;
9547 /* [temp.deduct.call]
9549 A can be another pointer or pointer to member type that can
9550 be converted to the deduced A via a qualification
9551 conversion (_conv.qual_).
9553 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9554 This will allow for additional cv-qualification of the
9555 pointed-to types if appropriate. */
9557 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9558 /* The derived-to-base conversion only persists through one
9559 level of pointers. */
9560 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9562 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
9563 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
9565 /* Avoid getting confused about cv-quals; don't recurse here.
9566 Pointers to members should really be just OFFSET_TYPE, not
9567 this two-level nonsense... */
9569 parm = TREE_TYPE (parm);
9570 arg = TREE_TYPE (arg);
9571 goto offset;
9574 return unify (tparms, targs, TREE_TYPE (parm),
9575 TREE_TYPE (arg), strict);
9578 case REFERENCE_TYPE:
9579 if (TREE_CODE (arg) != REFERENCE_TYPE)
9580 return 1;
9581 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9582 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9584 case ARRAY_TYPE:
9585 if (TREE_CODE (arg) != ARRAY_TYPE)
9586 return 1;
9587 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9588 != (TYPE_DOMAIN (arg) == NULL_TREE))
9589 return 1;
9590 if (TYPE_DOMAIN (parm) != NULL_TREE
9591 && unify (tparms, targs, TYPE_DOMAIN (parm),
9592 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
9593 return 1;
9594 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9595 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9597 case REAL_TYPE:
9598 case COMPLEX_TYPE:
9599 case VECTOR_TYPE:
9600 case INTEGER_TYPE:
9601 case BOOLEAN_TYPE:
9602 case VOID_TYPE:
9603 if (TREE_CODE (arg) != TREE_CODE (parm))
9604 return 1;
9606 if (TREE_CODE (parm) == INTEGER_TYPE
9607 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
9609 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
9610 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
9611 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
9612 return 1;
9613 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
9614 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
9615 TYPE_MAX_VALUE (arg),
9616 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
9617 return 1;
9619 /* We have already checked cv-qualification at the top of the
9620 function. */
9621 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9622 return 1;
9624 /* As far as unification is concerned, this wins. Later checks
9625 will invalidate it if necessary. */
9626 return 0;
9628 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9629 /* Type INTEGER_CST can come from ordinary constant template args. */
9630 case INTEGER_CST:
9631 while (TREE_CODE (arg) == NOP_EXPR)
9632 arg = TREE_OPERAND (arg, 0);
9634 if (TREE_CODE (arg) != INTEGER_CST)
9635 return 1;
9636 return !tree_int_cst_equal (parm, arg);
9638 case TREE_VEC:
9640 int i;
9641 if (TREE_CODE (arg) != TREE_VEC)
9642 return 1;
9643 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9644 return 1;
9645 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
9646 if (unify (tparms, targs,
9647 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
9648 UNIFY_ALLOW_NONE))
9649 return 1;
9650 return 0;
9653 case RECORD_TYPE:
9654 case UNION_TYPE:
9655 if (TREE_CODE (arg) != TREE_CODE (parm))
9656 return 1;
9658 if (TYPE_PTRMEMFUNC_P (parm))
9660 if (!TYPE_PTRMEMFUNC_P (arg))
9661 return 1;
9663 return unify (tparms, targs,
9664 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9665 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9666 strict);
9669 if (CLASSTYPE_TEMPLATE_INFO (parm))
9671 tree t = NULL_TREE;
9673 if (strict_in & UNIFY_ALLOW_DERIVED)
9675 /* First, we try to unify the PARM and ARG directly. */
9676 t = try_class_unification (tparms, targs,
9677 parm, arg);
9679 if (!t)
9681 /* Fallback to the special case allowed in
9682 [temp.deduct.call]:
9684 If P is a class, and P has the form
9685 template-id, then A can be a derived class of
9686 the deduced A. Likewise, if P is a pointer to
9687 a class of the form template-id, A can be a
9688 pointer to a derived class pointed to by the
9689 deduced A. */
9690 t = get_template_base (tparms, targs,
9691 parm, arg);
9693 if (! t || t == error_mark_node)
9694 return 1;
9697 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9698 && (CLASSTYPE_TI_TEMPLATE (parm)
9699 == CLASSTYPE_TI_TEMPLATE (arg)))
9700 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9701 Then, we should unify `int' and `U'. */
9702 t = arg;
9703 else
9704 /* There's no chance of unification succeeding. */
9705 return 1;
9707 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
9708 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
9710 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
9711 return 1;
9712 return 0;
9714 case METHOD_TYPE:
9715 case FUNCTION_TYPE:
9716 if (TREE_CODE (arg) != TREE_CODE (parm))
9717 return 1;
9719 if (unify (tparms, targs, TREE_TYPE (parm),
9720 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
9721 return 1;
9722 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
9723 TYPE_ARG_TYPES (arg), 1,
9724 DEDUCE_EXACT, 0, -1);
9726 case OFFSET_TYPE:
9727 offset:
9728 if (TREE_CODE (arg) != OFFSET_TYPE)
9729 return 1;
9730 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
9731 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
9732 return 1;
9733 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9734 strict);
9736 case CONST_DECL:
9737 if (DECL_TEMPLATE_PARM_P (parm))
9738 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
9739 if (arg != decl_constant_value (parm))
9740 return 1;
9741 return 0;
9743 case TEMPLATE_DECL:
9744 /* Matched cases are handled by the ARG == PARM test above. */
9745 return 1;
9747 case MINUS_EXPR:
9748 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9749 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
9751 /* We handle this case specially, since it comes up with
9752 arrays. In particular, something like:
9754 template <int N> void f(int (&x)[N]);
9756 Here, we are trying to unify the range type, which
9757 looks like [0 ... (N - 1)]. */
9758 tree t, t1, t2;
9759 t1 = TREE_OPERAND (parm, 0);
9760 t2 = TREE_OPERAND (parm, 1);
9762 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
9764 return unify (tparms, targs, t1, t, strict);
9766 /* else fall through */
9768 default:
9769 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
9772 /* We're looking at an expression. This can happen with
9773 something like:
9775 template <int I>
9776 void foo(S<I>, S<I + 2>);
9778 This is a "nondeduced context":
9780 [deduct.type]
9782 The nondeduced contexts are:
9784 --A type that is a template-id in which one or more of
9785 the template-arguments is an expression that references
9786 a template-parameter.
9788 In these cases, we assume deduction succeeded, but don't
9789 actually infer any unifications. */
9791 if (!uses_template_parms (parm)
9792 && !template_args_equal (parm, arg))
9793 return 1;
9794 else
9795 return 0;
9797 else
9798 sorry ("use of `%s' in template type unification",
9799 tree_code_name [(int) TREE_CODE (parm)]);
9801 return 1;
9805 /* Called if RESULT is explicitly instantiated, or is a member of an
9806 explicitly instantiated class, or if using -frepo and the
9807 instantiation of RESULT has been assigned to this file. */
9809 void
9810 mark_decl_instantiated (result, extern_p)
9811 tree result;
9812 int extern_p;
9814 if (TREE_CODE (result) != FUNCTION_DECL)
9815 /* The TREE_PUBLIC flag for function declarations will have been
9816 set correctly by tsubst. */
9817 TREE_PUBLIC (result) = 1;
9819 /* We used to set this unconditionally; we moved that to
9820 do_decl_instantiation so it wouldn't get set on members of
9821 explicit class template instantiations. But we still need to set
9822 it here for the 'extern template' case in order to suppress
9823 implicit instantiations. */
9824 if (extern_p)
9825 SET_DECL_EXPLICIT_INSTANTIATION (result);
9827 if (! extern_p)
9829 DECL_INTERFACE_KNOWN (result) = 1;
9830 DECL_NOT_REALLY_EXTERN (result) = 1;
9832 /* Always make artificials weak. */
9833 if (DECL_ARTIFICIAL (result) && flag_weak)
9834 comdat_linkage (result);
9835 /* For WIN32 we also want to put explicit instantiations in
9836 linkonce sections. */
9837 else if (TREE_PUBLIC (result))
9838 maybe_make_one_only (result);
9840 else if (TREE_CODE (result) == FUNCTION_DECL)
9841 defer_fn (result);
9844 /* Given two function templates PAT1 and PAT2, return:
9846 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9848 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9849 -1 if PAT2 is more specialized than PAT1.
9850 0 if neither is more specialized.
9852 LEN is passed through to fn_type_unification. */
9855 more_specialized (pat1, pat2, deduce, len)
9856 tree pat1, pat2;
9857 int deduce;
9858 int len;
9860 tree targs;
9861 int winner = 0;
9863 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9864 NULL_TREE, 0, deduce, len);
9865 if (targs)
9866 --winner;
9868 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9869 NULL_TREE, 0, deduce, len);
9870 if (targs)
9871 ++winner;
9873 return winner;
9876 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9878 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9879 -1 if PAT2 is more specialized than PAT1.
9880 0 if neither is more specialized. */
9883 more_specialized_class (pat1, pat2)
9884 tree pat1, pat2;
9886 tree targs;
9887 int winner = 0;
9889 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9890 TREE_PURPOSE (pat2));
9891 if (targs)
9892 --winner;
9894 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9895 TREE_PURPOSE (pat1));
9896 if (targs)
9897 ++winner;
9899 return winner;
9902 /* Return the template arguments that will produce the function signature
9903 DECL from the function template FN, with the explicit template
9904 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9905 also match. Return NULL_TREE if no satisfactory arguments could be
9906 found. DEDUCE and LEN are passed through to fn_type_unification. */
9908 static tree
9909 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9910 tree fn, decl, explicit_args;
9911 int check_rettype, deduce, len;
9913 int ntparms = DECL_NTPARMS (fn);
9914 tree targs = make_tree_vec (ntparms);
9915 tree decl_type;
9916 tree decl_arg_types;
9917 int i;
9919 /* Substitute the explicit template arguments into the type of DECL.
9920 The call to fn_type_unification will handle substitution into the
9921 FN. */
9922 decl_type = TREE_TYPE (decl);
9923 if (explicit_args && uses_template_parms (decl_type))
9925 tree tmpl;
9926 tree converted_args;
9928 if (DECL_TEMPLATE_INFO (decl))
9929 tmpl = DECL_TI_TEMPLATE (decl);
9930 else
9931 /* We can get here for some invalid specializations. */
9932 return NULL_TREE;
9934 converted_args
9935 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9936 explicit_args, NULL_TREE,
9937 tf_none, /*require_all_arguments=*/0));
9938 if (converted_args == error_mark_node)
9939 return NULL_TREE;
9941 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
9942 if (decl_type == error_mark_node)
9943 return NULL_TREE;
9946 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9947 /* Never do unification on the 'this' parameter. */
9948 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9949 decl_arg_types = TREE_CHAIN (decl_arg_types);
9951 i = fn_type_unification (fn, explicit_args, targs,
9952 decl_arg_types,
9953 (check_rettype || DECL_CONV_FN_P (fn)
9954 ? TREE_TYPE (decl_type) : NULL_TREE),
9955 deduce, len);
9957 if (i != 0)
9958 return NULL_TREE;
9960 return targs;
9963 /* For most uses, we want to check the return type. */
9965 tree
9966 get_bindings (fn, decl, explicit_args)
9967 tree fn, decl, explicit_args;
9969 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9972 /* But for resolve_overloaded_unification, we only care about the parameter
9973 types. */
9975 static tree
9976 get_bindings_overload (fn, decl, explicit_args)
9977 tree fn, decl, explicit_args;
9979 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9982 /* Return the innermost template arguments that, when applied to a
9983 template specialization whose innermost template parameters are
9984 TPARMS, and whose specialization arguments are PARMS, yield the
9985 ARGS.
9987 For example, suppose we have:
9989 template <class T, class U> struct S {};
9990 template <class T> struct S<T*, int> {};
9992 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9993 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9994 int}. The resulting vector will be {double}, indicating that `T'
9995 is bound to `double'. */
9997 static tree
9998 get_class_bindings (tparms, parms, args)
9999 tree tparms, parms, args;
10001 int i, ntparms = TREE_VEC_LENGTH (tparms);
10002 tree vec = make_tree_vec (ntparms);
10004 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10005 UNIFY_ALLOW_NONE))
10006 return NULL_TREE;
10008 for (i = 0; i < ntparms; ++i)
10009 if (! TREE_VEC_ELT (vec, i))
10010 return NULL_TREE;
10012 if (verify_class_unification (vec, parms, args))
10013 return NULL_TREE;
10015 return vec;
10018 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10019 Pick the most specialized template, and return the corresponding
10020 instantiation, or if there is no corresponding instantiation, the
10021 template itself. If there is no most specialized template,
10022 error_mark_node is returned. If there are no templates at all,
10023 NULL_TREE is returned. */
10025 tree
10026 most_specialized_instantiation (instantiations)
10027 tree instantiations;
10029 tree fn, champ;
10030 int fate;
10032 if (!instantiations)
10033 return NULL_TREE;
10035 champ = instantiations;
10036 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10038 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10039 DEDUCE_EXACT, -1);
10040 if (fate == 1)
10042 else
10044 if (fate == 0)
10046 fn = TREE_CHAIN (fn);
10047 if (! fn)
10048 return error_mark_node;
10050 champ = fn;
10054 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10056 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10057 DEDUCE_EXACT, -1);
10058 if (fate != 1)
10059 return error_mark_node;
10062 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10065 /* Return the most specialized of the list of templates in FNS that can
10066 produce an instantiation matching DECL, given the explicit template
10067 arguments EXPLICIT_ARGS. */
10069 static tree
10070 most_specialized (fns, decl, explicit_args)
10071 tree fns, decl, explicit_args;
10073 tree candidates = NULL_TREE;
10074 tree fn, args;
10076 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10078 tree candidate = TREE_VALUE (fn);
10080 args = get_bindings (candidate, decl, explicit_args);
10081 if (args)
10082 candidates = tree_cons (NULL_TREE, candidate, candidates);
10085 return most_specialized_instantiation (candidates);
10088 /* If DECL is a specialization of some template, return the most
10089 general such template. Otherwise, returns NULL_TREE.
10091 For example, given:
10093 template <class T> struct S { template <class U> void f(U); };
10095 if TMPL is `template <class U> void S<int>::f(U)' this will return
10096 the full template. This function will not trace past partial
10097 specializations, however. For example, given in addition:
10099 template <class T> struct S<T*> { template <class U> void f(U); };
10101 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10102 `template <class T> template <class U> S<T*>::f(U)'. */
10104 tree
10105 most_general_template (decl)
10106 tree decl;
10108 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10109 an immediate specialization. */
10110 if (TREE_CODE (decl) == FUNCTION_DECL)
10112 if (DECL_TEMPLATE_INFO (decl)) {
10113 decl = DECL_TI_TEMPLATE (decl);
10115 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10116 template friend. */
10117 if (TREE_CODE (decl) != TEMPLATE_DECL)
10118 return NULL_TREE;
10119 } else
10120 return NULL_TREE;
10123 /* Look for more and more general templates. */
10124 while (DECL_TEMPLATE_INFO (decl))
10126 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10127 in some cases. (See cp-tree.h for details.) */
10128 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10129 break;
10131 /* Stop if we run into an explicitly specialized class template. */
10132 if (!DECL_NAMESPACE_SCOPE_P (decl)
10133 && DECL_CONTEXT (decl)
10134 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10135 break;
10137 decl = DECL_TI_TEMPLATE (decl);
10140 return decl;
10143 /* Return the most specialized of the class template specializations
10144 of TMPL which can produce an instantiation matching ARGS, or
10145 error_mark_node if the choice is ambiguous. */
10147 static tree
10148 most_specialized_class (tmpl, args)
10149 tree tmpl;
10150 tree args;
10152 tree list = NULL_TREE;
10153 tree t;
10154 tree champ;
10155 int fate;
10157 tmpl = most_general_template (tmpl);
10158 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10160 tree spec_args
10161 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10162 if (spec_args)
10164 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10165 TREE_TYPE (list) = TREE_TYPE (t);
10169 if (! list)
10170 return NULL_TREE;
10172 t = list;
10173 champ = t;
10174 t = TREE_CHAIN (t);
10175 for (; t; t = TREE_CHAIN (t))
10177 fate = more_specialized_class (champ, t);
10178 if (fate == 1)
10180 else
10182 if (fate == 0)
10184 t = TREE_CHAIN (t);
10185 if (! t)
10186 return error_mark_node;
10188 champ = t;
10192 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10194 fate = more_specialized_class (champ, t);
10195 if (fate != 1)
10196 return error_mark_node;
10199 return champ;
10202 /* Explicitly instantiate DECL. */
10204 void
10205 do_decl_instantiation (tree decl, tree storage)
10207 tree result = NULL_TREE;
10208 int extern_p = 0;
10210 if (!decl)
10211 /* An error occurred, for which grokdeclarator has already issued
10212 an appropriate message. */
10213 return;
10214 else if (! DECL_LANG_SPECIFIC (decl))
10216 error ("explicit instantiation of non-template `%#D'", decl);
10217 return;
10219 else if (TREE_CODE (decl) == VAR_DECL)
10221 /* There is an asymmetry here in the way VAR_DECLs and
10222 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10223 the latter, the DECL we get back will be marked as a
10224 template instantiation, and the appropriate
10225 DECL_TEMPLATE_INFO will be set up. This does not happen for
10226 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10227 should handle VAR_DECLs as it currently handles
10228 FUNCTION_DECLs. */
10229 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10230 if (!result || TREE_CODE (result) != VAR_DECL)
10232 error ("no matching template for `%D' found", decl);
10233 return;
10236 else if (TREE_CODE (decl) != FUNCTION_DECL)
10238 error ("explicit instantiation of `%#D'", decl);
10239 return;
10241 else
10242 result = decl;
10244 /* Check for various error cases. Note that if the explicit
10245 instantiation is valid the RESULT will currently be marked as an
10246 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10247 until we get here. */
10249 if (DECL_TEMPLATE_SPECIALIZATION (result))
10251 /* DR 259 [temp.spec].
10253 Both an explicit instantiation and a declaration of an explicit
10254 specialization shall not appear in a program unless the explicit
10255 instantiation follows a declaration of the explicit specialization.
10257 For a given set of template parameters, if an explicit
10258 instantiation of a template appears after a declaration of an
10259 explicit specialization for that template, the explicit
10260 instantiation has no effect. */
10261 return;
10263 else if (DECL_EXPLICIT_INSTANTIATION (result))
10265 /* [temp.spec]
10267 No program shall explicitly instantiate any template more
10268 than once.
10270 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10271 instantiation was `extern' and the second is not, and EXTERN_P for
10272 the opposite case. If -frepo, chances are we already got marked
10273 as an explicit instantiation because of the repo file. */
10274 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
10275 pedwarn ("duplicate explicit instantiation of `%#D'", result);
10277 /* If we've already instantiated the template, just return now. */
10278 if (DECL_INTERFACE_KNOWN (result))
10279 return;
10281 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10283 error ("no matching template for `%D' found", result);
10284 return;
10286 else if (!DECL_TEMPLATE_INFO (result))
10288 pedwarn ("explicit instantiation of non-template `%#D'", result);
10289 return;
10292 if (flag_external_templates)
10293 return;
10295 if (storage == NULL_TREE)
10297 else if (storage == ridpointers[(int) RID_EXTERN])
10299 if (pedantic && !in_system_header)
10300 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10301 extern_p = 1;
10303 else
10304 error ("storage class `%D' applied to template instantiation",
10305 storage);
10307 SET_DECL_EXPLICIT_INSTANTIATION (result);
10308 mark_decl_instantiated (result, extern_p);
10309 repo_template_instantiated (result, extern_p);
10310 if (! extern_p)
10311 instantiate_decl (result, /*defer_ok=*/1);
10314 void
10315 mark_class_instantiated (t, extern_p)
10316 tree t;
10317 int extern_p;
10319 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10320 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10321 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10322 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10323 if (! extern_p)
10325 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10326 rest_of_type_compilation (t, 1);
10330 /* Perform an explicit instantiation of template class T. STORAGE, if
10331 non-null, is the RID for extern, inline or static. COMPLAIN is
10332 nonzero if this is called from the parser, zero if called recursively,
10333 since the standard is unclear (as detailed below). */
10335 void
10336 do_type_instantiation (t, storage, complain)
10337 tree t, storage;
10338 tsubst_flags_t complain;
10340 int extern_p = 0;
10341 int nomem_p = 0;
10342 int static_p = 0;
10344 if (TREE_CODE (t) == TYPE_DECL)
10345 t = TREE_TYPE (t);
10347 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10349 error ("explicit instantiation of non-template type `%T'", t);
10350 return;
10353 complete_type (t);
10355 /* With -fexternal-templates, explicit instantiations are treated the same
10356 as implicit ones. */
10357 if (flag_external_templates)
10358 return;
10360 if (!COMPLETE_TYPE_P (t))
10362 if (complain & tf_error)
10363 error ("explicit instantiation of `%#T' before definition of template",
10365 return;
10368 if (storage != NULL_TREE)
10370 if (pedantic && !in_system_header)
10371 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
10372 IDENTIFIER_POINTER (storage));
10374 if (storage == ridpointers[(int) RID_INLINE])
10375 nomem_p = 1;
10376 else if (storage == ridpointers[(int) RID_EXTERN])
10377 extern_p = 1;
10378 else if (storage == ridpointers[(int) RID_STATIC])
10379 static_p = 1;
10380 else
10382 error ("storage class `%D' applied to template instantiation",
10383 storage);
10384 extern_p = 0;
10388 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10390 /* DR 259 [temp.spec].
10392 Both an explicit instantiation and a declaration of an explicit
10393 specialization shall not appear in a program unless the explicit
10394 instantiation follows a declaration of the explicit specialization.
10396 For a given set of template parameters, if an explicit
10397 instantiation of a template appears after a declaration of an
10398 explicit specialization for that template, the explicit
10399 instantiation has no effect. */
10400 return;
10402 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10404 /* [temp.spec]
10406 No program shall explicitly instantiate any template more
10407 than once.
10409 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10410 was `extern'. If EXTERN_P then the second is. If -frepo, chances
10411 are we already got marked as an explicit instantiation because of the
10412 repo file. All these cases are OK. */
10413 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
10414 && (complain & tf_error))
10415 pedwarn ("duplicate explicit instantiation of `%#T'", t);
10417 /* If we've already instantiated the template, just return now. */
10418 if (!CLASSTYPE_INTERFACE_ONLY (t))
10419 return;
10422 mark_class_instantiated (t, extern_p);
10423 repo_template_instantiated (t, extern_p);
10425 if (nomem_p)
10426 return;
10429 tree tmp;
10431 /* In contrast to implicit instantiation, where only the
10432 declarations, and not the definitions, of members are
10433 instantiated, we have here:
10435 [temp.explicit]
10437 The explicit instantiation of a class template specialization
10438 implies the instantiation of all of its members not
10439 previously explicitly specialized in the translation unit
10440 containing the explicit instantiation.
10442 Of course, we can't instantiate member template classes, since
10443 we don't have any arguments for them. Note that the standard
10444 is unclear on whether the instantiation of the members are
10445 *explicit* instantiations or not. We choose to be generous,
10446 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10447 the explicit instantiation of a class where some of the members
10448 have no definition in the current translation unit. */
10450 if (! static_p)
10451 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10452 if (TREE_CODE (tmp) == FUNCTION_DECL
10453 && DECL_TEMPLATE_INSTANTIATION (tmp))
10455 mark_decl_instantiated (tmp, extern_p);
10456 repo_template_instantiated (tmp, extern_p);
10457 if (! extern_p)
10458 instantiate_decl (tmp, /*defer_ok=*/1);
10461 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10462 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10464 mark_decl_instantiated (tmp, extern_p);
10465 repo_template_instantiated (tmp, extern_p);
10466 if (! extern_p)
10467 instantiate_decl (tmp, /*defer_ok=*/1);
10470 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
10471 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
10472 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
10473 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
10477 /* Given a function DECL, which is a specialization of TMPL, modify
10478 DECL to be a re-instantiation of TMPL with the same template
10479 arguments. TMPL should be the template into which tsubst'ing
10480 should occur for DECL, not the most general template.
10482 One reason for doing this is a scenario like this:
10484 template <class T>
10485 void f(const T&, int i);
10487 void g() { f(3, 7); }
10489 template <class T>
10490 void f(const T& t, const int i) { }
10492 Note that when the template is first instantiated, with
10493 instantiate_template, the resulting DECL will have no name for the
10494 first parameter, and the wrong type for the second. So, when we go
10495 to instantiate the DECL, we regenerate it. */
10497 static void
10498 regenerate_decl_from_template (decl, tmpl)
10499 tree decl;
10500 tree tmpl;
10502 /* The most general version of TMPL. */
10503 tree gen_tmpl;
10504 /* The arguments used to instantiate DECL, from the most general
10505 template. */
10506 tree args;
10507 tree code_pattern;
10508 tree new_decl;
10509 int unregistered;
10511 args = DECL_TI_ARGS (decl);
10512 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10514 /* Unregister the specialization so that when we tsubst we will not
10515 just return DECL. We don't have to unregister DECL from TMPL
10516 because if would only be registered there if it were a partial
10517 instantiation of a specialization, which it isn't: it's a full
10518 instantiation. */
10519 gen_tmpl = most_general_template (tmpl);
10520 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl));
10521 unregistered = unregister_specialization (decl, gen_tmpl);
10523 /* If the DECL was not unregistered then something peculiar is
10524 happening: we created a specialization but did not call
10525 register_specialization for it. */
10526 my_friendly_assert (unregistered, 0);
10528 /* Do the substitution to get the new declaration. */
10529 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10531 if (TREE_CODE (decl) == VAR_DECL)
10533 /* Set up DECL_INITIAL, since tsubst doesn't. */
10534 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10535 DECL_INITIAL (new_decl) =
10536 tsubst_expr (DECL_INITIAL (code_pattern), args,
10537 tf_error, DECL_TI_TEMPLATE (decl));
10539 else if (TREE_CODE (decl) == FUNCTION_DECL)
10541 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10542 new decl. */
10543 DECL_INITIAL (new_decl) = error_mark_node;
10544 /* And don't complain about a duplicate definition. */
10545 DECL_INITIAL (decl) = NULL_TREE;
10548 pop_access_scope (decl);
10550 /* The immediate parent of the new template is still whatever it was
10551 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10552 general template. We also reset the DECL_ASSEMBLER_NAME since
10553 tsubst always calculates the name as if the function in question
10554 were really a template instance, and sometimes, with friend
10555 functions, this is not so. See tsubst_friend_function for
10556 details. */
10557 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10558 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10559 COPY_DECL_RTL (decl, new_decl);
10560 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10562 /* Call duplicate decls to merge the old and new declarations. */
10563 duplicate_decls (new_decl, decl);
10565 /* Now, re-register the specialization. */
10566 register_specialization (decl, gen_tmpl, args);
10569 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10570 substituted to get DECL. */
10572 static tree
10573 template_for_substitution (tree decl)
10575 tree tmpl = DECL_TI_TEMPLATE (decl);
10577 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10578 for the instantiation. This is not always the most general
10579 template. Consider, for example:
10581 template <class T>
10582 struct S { template <class U> void f();
10583 template <> void f<int>(); };
10585 and an instantiation of S<double>::f<int>. We want TD to be the
10586 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10587 while (/* An instantiation cannot have a definition, so we need a
10588 more general template. */
10589 DECL_TEMPLATE_INSTANTIATION (tmpl)
10590 /* We must also deal with friend templates. Given:
10592 template <class T> struct S {
10593 template <class U> friend void f() {};
10596 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10597 so far as the language is concerned, but that's still
10598 where we get the pattern for the instantiation from. On
10599 other hand, if the definition comes outside the class, say:
10601 template <class T> struct S {
10602 template <class U> friend void f();
10604 template <class U> friend void f() {}
10606 we don't need to look any further. That's what the check for
10607 DECL_INITIAL is for. */
10608 || (TREE_CODE (decl) == FUNCTION_DECL
10609 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10610 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10612 /* The present template, TD, should not be a definition. If it
10613 were a definition, we should be using it! Note that we
10614 cannot restructure the loop to just keep going until we find
10615 a template with a definition, since that might go too far if
10616 a specialization was declared, but not defined. */
10617 my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL
10618 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))),
10619 0);
10621 /* Fetch the more general template. */
10622 tmpl = DECL_TI_TEMPLATE (tmpl);
10625 return tmpl;
10628 /* Produce the definition of D, a _DECL generated from a template. If
10629 DEFER_OK is nonzero, then we don't have to actually do the
10630 instantiation now; we just have to do it sometime. */
10632 tree
10633 instantiate_decl (d, defer_ok)
10634 tree d;
10635 int defer_ok;
10637 tree tmpl = DECL_TI_TEMPLATE (d);
10638 tree gen_args;
10639 tree args;
10640 tree td;
10641 tree code_pattern;
10642 tree spec;
10643 tree gen_tmpl;
10644 int pattern_defined;
10645 int line = lineno;
10646 int need_push;
10647 const char *file = input_filename;
10649 /* This function should only be used to instantiate templates for
10650 functions and static member variables. */
10651 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
10652 || TREE_CODE (d) == VAR_DECL, 0);
10654 /* Variables are never deferred; if instantiation is required, they
10655 are instantiated right away. That allows for better code in the
10656 case that an expression refers to the value of the variable --
10657 if the variable has a constant value the referring expression can
10658 take advantage of that fact. */
10659 if (TREE_CODE (d) == VAR_DECL)
10660 defer_ok = 0;
10662 /* Don't instantiate cloned functions. Instead, instantiate the
10663 functions they cloned. */
10664 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10665 d = DECL_CLONED_FUNCTION (d);
10667 if (DECL_TEMPLATE_INSTANTIATED (d))
10668 /* D has already been instantiated. It might seem reasonable to
10669 check whether or not D is an explicit instantiation, and, if so,
10670 stop here. But when an explicit instantiation is deferred
10671 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10672 is set, even though we still need to do the instantiation. */
10673 return d;
10675 /* If we already have a specialization of this declaration, then
10676 there's no reason to instantiate it. Note that
10677 retrieve_specialization gives us both instantiations and
10678 specializations, so we must explicitly check
10679 DECL_TEMPLATE_SPECIALIZATION. */
10680 gen_tmpl = most_general_template (tmpl);
10681 gen_args = DECL_TI_ARGS (d);
10682 spec = retrieve_specialization (gen_tmpl, gen_args);
10683 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10684 return spec;
10686 /* This needs to happen before any tsubsting. */
10687 if (! push_tinst_level (d))
10688 return d;
10690 timevar_push (TV_PARSE);
10692 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
10693 for the instantiation. */
10694 td = template_for_substitution (d);
10695 code_pattern = DECL_TEMPLATE_RESULT (td);
10697 /* In the case of a friend template whose definition is provided
10698 outside the class, we may have too many arguments. Drop the ones
10699 we don't need. */
10700 args = get_innermost_template_args (gen_args,
10701 TMPL_PARMS_DEPTH
10702 (DECL_TEMPLATE_PARMS (td)));
10704 if (TREE_CODE (d) == FUNCTION_DECL)
10705 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
10706 else
10707 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
10709 lineno = DECL_SOURCE_LINE (d);
10710 input_filename = DECL_SOURCE_FILE (d);
10712 if (pattern_defined)
10714 /* Let the repository code that this template definition is
10715 available.
10717 The repository doesn't need to know about cloned functions
10718 because they never actually show up in the object file. It
10719 does need to know about the clones; those are the symbols
10720 that the linker will be emitting error messages about. */
10721 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10722 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10724 tree t;
10726 for (t = TREE_CHAIN (d);
10727 t && DECL_CLONED_FUNCTION_P (t);
10728 t = TREE_CHAIN (t))
10729 repo_template_used (t);
10731 else
10732 repo_template_used (d);
10734 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10736 if (flag_alt_external_templates)
10738 if (interface_unknown)
10739 warn_if_unknown_interface (d);
10741 else if (DECL_INTERFACE_KNOWN (code_pattern))
10743 DECL_INTERFACE_KNOWN (d) = 1;
10744 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
10746 else
10747 warn_if_unknown_interface (code_pattern);
10750 if (at_eof)
10751 import_export_decl (d);
10754 if (!defer_ok)
10756 /* Recheck the substitutions to obtain any warning messages
10757 about ignoring cv qualifiers. */
10758 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10759 tree type = TREE_TYPE (gen);
10761 /* Make sure that we can see identifiers, and compute access
10762 correctly. D is already the target FUNCTION_DECL with the
10763 right context. */
10764 push_access_scope (d);
10766 if (TREE_CODE (gen) == FUNCTION_DECL)
10768 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
10769 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
10770 tf_error | tf_warning, d);
10771 /* Don't simply tsubst the function type, as that will give
10772 duplicate warnings about poor parameter qualifications.
10773 The function arguments are the same as the decl_arguments
10774 without the top level cv qualifiers. */
10775 type = TREE_TYPE (type);
10777 tsubst (type, gen_args, tf_error | tf_warning, d);
10779 pop_access_scope (d);
10782 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10783 && DECL_INITIAL (d) == NULL_TREE)
10784 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10785 abort ();
10786 /* Reject all external templates except inline functions. */
10787 else if (DECL_INTERFACE_KNOWN (d)
10788 && ! DECL_NOT_REALLY_EXTERN (d)
10789 && ! (TREE_CODE (d) == FUNCTION_DECL
10790 && DECL_INLINE (d)))
10791 goto out;
10792 /* Defer all other templates, unless we have been explicitly
10793 forbidden from doing so. We restore the source position here
10794 because it's used by add_pending_template. */
10795 else if (! pattern_defined || defer_ok)
10797 lineno = line;
10798 input_filename = file;
10800 if (at_eof && !pattern_defined
10801 && DECL_EXPLICIT_INSTANTIATION (d))
10802 /* [temp.explicit]
10804 The definition of a non-exported function template, a
10805 non-exported member function template, or a non-exported
10806 member function or static data member of a class template
10807 shall be present in every translation unit in which it is
10808 explicitly instantiated. */
10809 pedwarn
10810 ("explicit instantiation of `%D' but no definition available", d);
10812 add_pending_template (d);
10813 goto out;
10816 need_push = !global_bindings_p ();
10817 if (need_push)
10818 push_to_top_level ();
10820 /* We're now committed to instantiating this template. Mark it as
10821 instantiated so that recursive calls to instantiate_decl do not
10822 try to instantiate it again. */
10823 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10825 /* Regenerate the declaration in case the template has been modified
10826 by a subsequent redeclaration. */
10827 regenerate_decl_from_template (d, td);
10829 /* We already set the file and line above. Reset them now in case
10830 they changed as a result of calling regenerate_decl_from_template. */
10831 lineno = DECL_SOURCE_LINE (d);
10832 input_filename = DECL_SOURCE_FILE (d);
10834 if (TREE_CODE (d) == VAR_DECL)
10836 /* Clear out DECL_RTL; whatever was there before may not be right
10837 since we've reset the type of the declaration. */
10838 SET_DECL_RTL (d, NULL_RTX);
10840 DECL_IN_AGGR_P (d) = 0;
10841 if (DECL_INTERFACE_KNOWN (d))
10842 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10843 else
10845 DECL_EXTERNAL (d) = 1;
10846 DECL_NOT_REALLY_EXTERN (d) = 1;
10848 cp_finish_decl (d,
10849 (!DECL_INITIALIZED_IN_CLASS_P (d)
10850 ? DECL_INITIAL (d) : NULL_TREE),
10851 NULL_TREE, 0);
10853 else if (TREE_CODE (d) == FUNCTION_DECL)
10855 htab_t saved_local_specializations;
10856 tree subst_decl;
10857 tree tmpl_parm;
10858 tree spec_parm;
10860 /* Save away the current list, in case we are instantiating one
10861 template from within the body of another. */
10862 saved_local_specializations = local_specializations;
10864 /* Set up the list of local specializations. */
10865 local_specializations = htab_create (37,
10866 hash_local_specialization,
10867 eq_local_specializations,
10868 NULL);
10870 /* Set up context. */
10871 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10873 /* Create substitution entries for the parameters. */
10874 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
10875 tmpl_parm = DECL_ARGUMENTS (subst_decl);
10876 spec_parm = DECL_ARGUMENTS (d);
10877 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
10879 register_local_specialization (spec_parm, tmpl_parm);
10880 spec_parm = skip_artificial_parms_for (d, spec_parm);
10881 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
10883 while (tmpl_parm)
10885 register_local_specialization (spec_parm, tmpl_parm);
10886 tmpl_parm = TREE_CHAIN (tmpl_parm);
10887 spec_parm = TREE_CHAIN (spec_parm);
10889 my_friendly_assert (!spec_parm, 20020813);
10891 /* Substitute into the body of the function. */
10892 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10893 tf_error | tf_warning, tmpl);
10895 /* We don't need the local specializations any more. */
10896 htab_delete (local_specializations);
10897 local_specializations = saved_local_specializations;
10899 /* Finish the function. */
10900 expand_body (finish_function (0));
10903 /* We're not deferring instantiation any more. */
10904 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10906 if (need_push)
10907 pop_from_top_level ();
10909 out:
10910 lineno = line;
10911 input_filename = file;
10913 pop_tinst_level ();
10915 timevar_pop (TV_PARSE);
10917 return d;
10920 /* Run through the list of templates that we wish we could
10921 instantiate, and instantiate any we can. */
10924 instantiate_pending_templates ()
10926 tree *t;
10927 tree last = NULL_TREE;
10928 int instantiated_something = 0;
10929 int reconsider;
10933 reconsider = 0;
10935 t = &pending_templates;
10936 while (*t)
10938 tree instantiation = TREE_VALUE (*t);
10940 reopen_tinst_level (TREE_PURPOSE (*t));
10942 if (TYPE_P (instantiation))
10944 tree fn;
10946 if (!COMPLETE_TYPE_P (instantiation))
10948 instantiate_class_template (instantiation);
10949 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10950 for (fn = TYPE_METHODS (instantiation);
10952 fn = TREE_CHAIN (fn))
10953 if (! DECL_ARTIFICIAL (fn))
10954 instantiate_decl (fn, /*defer_ok=*/0);
10955 if (COMPLETE_TYPE_P (instantiation))
10957 instantiated_something = 1;
10958 reconsider = 1;
10962 if (COMPLETE_TYPE_P (instantiation))
10963 /* If INSTANTIATION has been instantiated, then we don't
10964 need to consider it again in the future. */
10965 *t = TREE_CHAIN (*t);
10966 else
10968 last = *t;
10969 t = &TREE_CHAIN (*t);
10972 else
10974 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10975 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10977 instantiation = instantiate_decl (instantiation,
10978 /*defer_ok=*/0);
10979 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10981 instantiated_something = 1;
10982 reconsider = 1;
10986 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10987 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10988 /* If INSTANTIATION has been instantiated, then we don't
10989 need to consider it again in the future. */
10990 *t = TREE_CHAIN (*t);
10991 else
10993 last = *t;
10994 t = &TREE_CHAIN (*t);
10997 tinst_depth = 0;
10998 current_tinst_level = NULL_TREE;
11000 last_pending_template = last;
11002 while (reconsider);
11004 return instantiated_something;
11007 /* Substitute ARGVEC into T, which is a list of initializers for
11008 either base class or a non-static data member. The TREE_PURPOSEs
11009 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11010 instantiate_decl. */
11012 static tree
11013 tsubst_initializer_list (t, argvec)
11014 tree t, argvec;
11016 tree inits = NULL_TREE;
11018 for (; t; t = TREE_CHAIN (t))
11020 tree decl;
11021 tree init;
11022 tree val;
11024 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11025 NULL_TREE);
11026 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11027 NULL_TREE);
11029 if (!init)
11031 else if (TREE_CODE (init) == TREE_LIST)
11032 for (val = init; val; val = TREE_CHAIN (val))
11033 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11034 else if (init != void_type_node)
11035 init = convert_from_reference (init);
11037 init = expand_member_init (decl, init);
11038 if (init)
11040 TREE_CHAIN (init) = inits;
11041 inits = init;
11044 return inits;
11047 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11049 static void
11050 set_current_access_from_decl (decl)
11051 tree decl;
11053 if (TREE_PRIVATE (decl))
11054 current_access_specifier = access_private_node;
11055 else if (TREE_PROTECTED (decl))
11056 current_access_specifier = access_protected_node;
11057 else
11058 current_access_specifier = access_public_node;
11061 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11062 is the instantiation (which should have been created with
11063 start_enum) and ARGS are the template arguments to use. */
11065 static void
11066 tsubst_enum (tag, newtag, args)
11067 tree tag;
11068 tree newtag;
11069 tree args;
11071 tree e;
11073 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11075 tree value;
11077 /* Copy node and set type */
11078 if (DECL_INITIAL (TREE_VALUE (e)))
11079 DECL_INITIAL (TREE_VALUE (e)) = copy_node (DECL_INITIAL (TREE_VALUE (e)));
11080 TREE_TYPE (TREE_VALUE (e)) = tag;
11082 /* Note that in a template enum, the TREE_VALUE is the
11083 CONST_DECL, not the corresponding INTEGER_CST. */
11084 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
11085 args, tf_error | tf_warning,
11086 NULL_TREE);
11088 /* Give this enumeration constant the correct access. */
11089 set_current_access_from_decl (TREE_VALUE (e));
11091 /* Actually build the enumerator itself. */
11092 build_enumerator (TREE_PURPOSE (e), value, newtag);
11095 finish_enum (newtag);
11096 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11097 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11100 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11101 its type -- but without substituting the innermost set of template
11102 arguments. So, innermost set of template parameters will appear in
11103 the type. */
11105 tree
11106 get_mostly_instantiated_function_type (decl)
11107 tree decl;
11109 tree fn_type;
11110 tree tmpl;
11111 tree targs;
11112 tree tparms;
11113 int parm_depth;
11115 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11116 targs = DECL_TI_ARGS (decl);
11117 tparms = DECL_TEMPLATE_PARMS (tmpl);
11118 parm_depth = TMPL_PARMS_DEPTH (tparms);
11120 /* There should be as many levels of arguments as there are levels
11121 of parameters. */
11122 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
11124 fn_type = TREE_TYPE (tmpl);
11126 if (parm_depth == 1)
11127 /* No substitution is necessary. */
11129 else
11131 int i;
11132 tree partial_args;
11134 /* Replace the innermost level of the TARGS with NULL_TREEs to
11135 let tsubst know not to substitute for those parameters. */
11136 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11137 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11138 SET_TMPL_ARGS_LEVEL (partial_args, i,
11139 TMPL_ARGS_LEVEL (targs, i));
11140 SET_TMPL_ARGS_LEVEL (partial_args,
11141 TMPL_ARGS_DEPTH (targs),
11142 make_tree_vec (DECL_NTPARMS (tmpl)));
11144 /* Make sure that we can see identifiers, and compute access
11145 correctly. We can just use the context of DECL for the
11146 partial substitution here. It depends only on outer template
11147 parameters, regardless of whether the innermost level is
11148 specialized or not. */
11149 push_access_scope (decl);
11151 /* Now, do the (partial) substitution to figure out the
11152 appropriate function type. */
11153 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11155 /* Substitute into the template parameters to obtain the real
11156 innermost set of parameters. This step is important if the
11157 innermost set of template parameters contains value
11158 parameters whose types depend on outer template parameters. */
11159 TREE_VEC_LENGTH (partial_args)--;
11160 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11162 pop_access_scope (decl);
11165 return fn_type;
11168 /* Return truthvalue if we're processing a template different from
11169 the last one involved in diagnostics. */
11171 problematic_instantiation_changed ()
11173 return last_template_error_tick != tinst_level_tick;
11176 /* Remember current template involved in diagnostics. */
11177 void
11178 record_last_problematic_instantiation ()
11180 last_template_error_tick = tinst_level_tick;
11183 tree
11184 current_instantiation ()
11186 return current_tinst_level;
11189 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11190 type. Return zero for ok, nonzero for disallowed. Issue error and
11191 warning messages under control of COMPLAIN. */
11193 static int
11194 invalid_nontype_parm_type_p (type, complain)
11195 tree type;
11196 tsubst_flags_t complain;
11198 if (INTEGRAL_TYPE_P (type))
11199 return 0;
11200 else if (POINTER_TYPE_P (type))
11201 return 0;
11202 else if (TYPE_PTRMEM_P (type))
11203 return 0;
11204 else if (TYPE_PTRMEMFUNC_P (type))
11205 return 0;
11206 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11207 return 0;
11208 else if (TREE_CODE (type) == TYPENAME_TYPE)
11209 return 0;
11211 if (complain & tf_error)
11212 error ("`%#T' is not a valid type for a template constant parameter",
11213 type);
11214 return 1;
11217 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11218 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11220 static bool
11221 dependent_type_p_r (tree type)
11223 tree scope;
11225 /* [temp.dep.type]
11227 A type is dependent if it is:
11229 -- a template parameter. */
11230 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11231 return true;
11232 /* -- a qualified-id with a nested-name-specifier which contains a
11233 class-name that names a dependent type or whose unqualified-id
11234 names a dependent type. */
11235 if (TREE_CODE (type) == TYPENAME_TYPE)
11236 return true;
11237 /* -- a cv-qualified type where the cv-unqualified type is
11238 dependent. */
11239 type = TYPE_MAIN_VARIANT (type);
11240 /* -- a compound type constructed from any dependent type. */
11241 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
11242 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11243 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11244 (type)));
11245 else if (TREE_CODE (type) == POINTER_TYPE
11246 || TREE_CODE (type) == REFERENCE_TYPE)
11247 return dependent_type_p (TREE_TYPE (type));
11248 else if (TREE_CODE (type) == FUNCTION_TYPE
11249 || TREE_CODE (type) == METHOD_TYPE)
11251 tree arg_type;
11253 if (dependent_type_p (TREE_TYPE (type)))
11254 return true;
11255 for (arg_type = TYPE_ARG_TYPES (type);
11256 arg_type;
11257 arg_type = TREE_CHAIN (arg_type))
11258 if (dependent_type_p (TREE_VALUE (arg_type)))
11259 return true;
11260 return false;
11262 /* -- an array type constructed from any dependent type or whose
11263 size is specified by a constant expression that is
11264 value-dependent. */
11265 if (TREE_CODE (type) == ARRAY_TYPE)
11267 if (TYPE_DOMAIN (type)
11268 && ((value_dependent_expression_p
11269 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11270 || (type_dependent_expression_p
11271 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11272 return true;
11273 return dependent_type_p (TREE_TYPE (type));
11275 /* -- a template-id in which either the template name is a template
11276 parameter or any of the template arguments is a dependent type or
11277 an expression that is type-dependent or value-dependent.
11279 This language seems somewhat confused; for example, it does not
11280 discuss template template arguments. Therefore, we use the
11281 definition for dependent template arguments in [temp.dep.temp]. */
11282 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11283 && (dependent_template_id_p
11284 (CLASSTYPE_TI_TEMPLATE (type),
11285 CLASSTYPE_TI_ARGS (type))))
11286 return true;
11287 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11288 return true;
11289 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11290 expression is not type-dependent, then it should already been
11291 have resolved. */
11292 if (TREE_CODE (type) == TYPEOF_TYPE)
11293 return true;
11294 /* The standard does not specifically mention types that are local
11295 to template functions or local classes, but they should be
11296 considered dependent too. For example:
11298 template <int I> void f() {
11299 enum E { a = I };
11300 S<sizeof (E)> s;
11303 The size of `E' cannot be known until the value of `I' has been
11304 determined. Therefore, `E' must be considered dependent. */
11305 scope = TYPE_CONTEXT (type);
11306 if (scope && TYPE_P (scope))
11307 return dependent_type_p (scope);
11308 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11309 return type_dependent_expression_p (scope);
11311 /* Other types are non-dependent. */
11312 return false;
11315 /* Returns TRUE if TYPE is dependent, in the sense of
11316 [temp.dep.type]. */
11318 bool
11319 dependent_type_p (tree type)
11321 /* If there are no template parameters in scope, then there can't be
11322 any dependent types. */
11323 if (!processing_template_decl)
11324 return false;
11326 /* If the type is NULL, we have not computed a type for the entity
11327 in question; in that case, the type is dependent. */
11328 if (!type)
11329 return true;
11331 /* Erroneous types can be considered non-dependent. */
11332 if (type == error_mark_node)
11333 return false;
11335 /* If we have not already computed the appropriate value for TYPE,
11336 do so now. */
11337 if (!TYPE_DEPENDENT_P_VALID (type))
11339 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11340 TYPE_DEPENDENT_P_VALID (type) = 1;
11343 return TYPE_DEPENDENT_P (type);
11346 /* Returns TRUE if the EXPRESSION is value-dependent. */
11348 bool
11349 value_dependent_expression_p (tree expression)
11351 if (!processing_template_decl)
11352 return false;
11354 /* A name declared with a dependent type. */
11355 if (TREE_CODE (expression) == LOOKUP_EXPR
11356 || (DECL_P (expression)
11357 && dependent_type_p (TREE_TYPE (expression))))
11358 return true;
11359 /* A non-type template parameter. */
11360 if ((TREE_CODE (expression) == CONST_DECL
11361 && DECL_TEMPLATE_PARM_P (expression))
11362 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11363 return true;
11364 /* A constant with integral or enumeration type and is initialized
11365 with an expression that is value-dependent. */
11366 if (TREE_CODE (expression) == VAR_DECL
11367 && DECL_INITIAL (expression)
11368 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
11369 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
11370 && value_dependent_expression_p (DECL_INITIAL (expression)))
11371 return true;
11372 /* These expressions are value-dependent if the type to which the
11373 cast occurs is dependent. */
11374 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11375 || TREE_CODE (expression) == STATIC_CAST_EXPR
11376 || TREE_CODE (expression) == CONST_CAST_EXPR
11377 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11378 || TREE_CODE (expression) == CAST_EXPR)
11379 && dependent_type_p (TREE_TYPE (expression)))
11380 return true;
11381 /* A `sizeof' expression where the sizeof operand is a type is
11382 value-dependent if the type is dependent. If the type was not
11383 dependent, we would no longer have a SIZEOF_EXPR, so any
11384 SIZEOF_EXPR is dependent. */
11385 if (TREE_CODE (expression) == SIZEOF_EXPR)
11386 return true;
11387 /* A constant expression is value-dependent if any subexpression is
11388 value-dependent. */
11389 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
11391 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11393 case '1':
11394 return (value_dependent_expression_p
11395 (TREE_OPERAND (expression, 0)));
11396 case '<':
11397 case '2':
11398 return ((value_dependent_expression_p
11399 (TREE_OPERAND (expression, 0)))
11400 || (value_dependent_expression_p
11401 (TREE_OPERAND (expression, 1))));
11402 case 'e':
11404 int i;
11405 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11406 /* In some cases, some of the operands may be missing.
11407 (For example, in the case of PREDECREMENT_EXPR, the
11408 amount to increment by may be missing.) That doesn't
11409 make the expression dependent. */
11410 if (TREE_OPERAND (expression, i)
11411 && (value_dependent_expression_p
11412 (TREE_OPERAND (expression, i))))
11413 return true;
11414 return false;
11419 /* The expression is not value-dependent. */
11420 return false;
11423 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11424 [temp.dep.expr]. */
11426 bool
11427 type_dependent_expression_p (expression)
11428 tree expression;
11430 if (!processing_template_decl)
11431 return false;
11433 /* Some expression forms are never type-dependent. */
11434 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11435 || TREE_CODE (expression) == SIZEOF_EXPR
11436 || TREE_CODE (expression) == ALIGNOF_EXPR
11437 || TREE_CODE (expression) == TYPEID_EXPR
11438 || TREE_CODE (expression) == DELETE_EXPR
11439 || TREE_CODE (expression) == VEC_DELETE_EXPR
11440 || TREE_CODE (expression) == THROW_EXPR)
11441 return false;
11443 /* The types of these expressions depends only on the type to which
11444 the cast occurs. */
11445 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11446 || TREE_CODE (expression) == STATIC_CAST_EXPR
11447 || TREE_CODE (expression) == CONST_CAST_EXPR
11448 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11449 || TREE_CODE (expression) == CAST_EXPR)
11450 return dependent_type_p (TREE_TYPE (expression));
11451 /* The types of these expressions depends only on the type created
11452 by the expression. */
11453 else if (TREE_CODE (expression) == NEW_EXPR
11454 || TREE_CODE (expression) == VEC_NEW_EXPR)
11455 return dependent_type_p (TREE_OPERAND (expression, 1));
11457 if (TREE_CODE (expression) == FUNCTION_DECL
11458 && DECL_LANG_SPECIFIC (expression)
11459 && DECL_TEMPLATE_INFO (expression)
11460 && (dependent_template_id_p
11461 (DECL_TI_TEMPLATE (expression),
11462 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11463 return true;
11465 return (dependent_type_p (TREE_TYPE (expression)));
11468 /* Returns TRUE if the ARG (a template argument) is dependent. */
11470 bool
11471 dependent_template_arg_p (tree arg)
11473 if (!processing_template_decl)
11474 return false;
11476 if (TREE_CODE (arg) == TEMPLATE_DECL
11477 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11478 return dependent_template_p (arg);
11479 else if (TYPE_P (arg))
11480 return dependent_type_p (arg);
11481 else
11482 return (type_dependent_expression_p (arg)
11483 || value_dependent_expression_p (arg));
11486 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11488 static bool
11489 dependent_template_id_p (tree tmpl, tree args)
11491 int i;
11493 if (dependent_template_p (tmpl))
11494 return true;
11495 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
11496 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
11497 return true;
11498 return false;
11501 /* Returns TRUE if the template TMPL is dependent. */
11503 bool
11504 dependent_template_p (tree tmpl)
11506 /* Template template parameters are dependent. */
11507 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
11508 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
11509 return true;
11510 /* So are member templates of dependent classes. */
11511 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
11512 return dependent_type_p (DECL_CONTEXT (tmpl));
11513 return false;
11516 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11517 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11518 can be found. Note that this function peers inside uninstantiated
11519 templates and therefore should be used only in extremely limited
11520 situations. */
11522 tree
11523 resolve_typename_type (tree type, bool only_current_p)
11525 tree scope;
11526 tree name;
11527 tree decl;
11528 int quals;
11530 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
11531 20010702);
11533 scope = TYPE_CONTEXT (type);
11534 name = TYPE_IDENTIFIER (type);
11536 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11537 it first before we can figure out what NAME refers to. */
11538 if (TREE_CODE (scope) == TYPENAME_TYPE)
11539 scope = resolve_typename_type (scope, only_current_p);
11540 /* If we don't know what SCOPE refers to, then we cannot resolve the
11541 TYPENAME_TYPE. */
11542 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
11543 return error_mark_node;
11544 /* If the SCOPE is a template type parameter, we have no way of
11545 resolving the name. */
11546 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
11547 return type;
11548 /* If the SCOPE is not the current instantiation, there's no reason
11549 to look inside it. */
11550 if (only_current_p && !currently_open_class (scope))
11551 return error_mark_node;
11552 /* If SCOPE is a partial instantiation, it will not have a valid
11553 TYPE_FIELDS list, so use the original template. */
11554 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
11555 /* Enter the SCOPE so that name lookup will be resolved as if we
11556 were in the class definition. In particular, SCOPE will no
11557 longer be considered a dependent type. */
11558 push_scope (scope);
11559 /* Look up the declaration. */
11560 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
11561 /* Obtain the set of qualifiers applied to the TYPE. */
11562 quals = cp_type_quals (type);
11563 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11564 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11565 if (!decl)
11566 type = error_mark_node;
11567 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
11568 && TREE_CODE (decl) == TYPE_DECL)
11569 type = TREE_TYPE (decl);
11570 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
11571 && DECL_CLASS_TEMPLATE_P (decl))
11573 tree tmpl;
11574 tree args;
11575 /* Obtain the template and the arguments. */
11576 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
11577 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
11578 /* Instantiate the template. */
11579 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
11580 /*entering_scope=*/0,
11581 tf_error);
11583 else
11584 type = error_mark_node;
11585 /* Qualify the resulting type. */
11586 if (type != error_mark_node && quals)
11587 type = cp_build_qualified_type (type, quals);
11588 /* Leave the SCOPE. */
11589 pop_scope (scope);
11591 return type;
11594 tree
11595 resolve_typename_type_in_current_instantiation (tree type)
11597 tree t;
11599 t = resolve_typename_type (type, /*only_current_p=*/true);
11600 return (t != error_mark_node) ? t : type;
11603 #include "gt-cp-pt.h"