* pt.c (tsubst_decl): Set DECL_CONTEXT for namespace-scope
[official-gcc.git] / gcc / cp / pt.c
blob1b24233af2d8ed2d70855e8106ce39e610097086
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001 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 GNU CC.
9 GNU CC 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 GNU CC 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 GNU CC; 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 "obstack.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "parse.h"
38 #include "lex.h"
39 #include "output.h"
40 #include "except.h"
41 #include "toplev.h"
42 #include "rtl.h"
43 #include "ggc.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 extern struct obstack permanent_obstack;
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the
55 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
56 the instantiate request occurred; the TREE_VALUE is a either a DECL
57 (for a function or static data member), or a TYPE (for a class)
58 indicating what we are hoping to instantiate. */
59 static tree pending_templates;
60 static tree *template_tail = &pending_templates;
62 static tree maybe_templates;
63 static tree *maybe_template_tail = &maybe_templates;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static tree saved_trees;
69 static varray_type inline_parm_levels;
70 static size_t inline_parm_levels_used;
72 static tree current_tinst_level;
74 /* A map from local variable declarations in the body of the template
75 presently being instantiated to the corresponding instantiated
76 local variables. */
77 static htab_t local_specializations;
79 #define obstack_chunk_alloc xmalloc
80 #define obstack_chunk_free free
82 #define UNIFY_ALLOW_NONE 0
83 #define UNIFY_ALLOW_MORE_CV_QUAL 1
84 #define UNIFY_ALLOW_LESS_CV_QUAL 2
85 #define UNIFY_ALLOW_DERIVED 4
86 #define UNIFY_ALLOW_INTEGER 8
87 #define UNIFY_ALLOW_OUTER_LEVEL 16
88 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
89 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
91 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
92 virtual, or a base class of a virtual
93 base. */
94 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
95 type with the desired type. */
97 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
98 unification_kind_t, int));
99 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
100 unification_kind_t, int));
101 static int unify PARAMS ((tree, tree, tree, tree, int));
102 static void add_pending_template PARAMS ((tree));
103 static int push_tinst_level PARAMS ((tree));
104 static void reopen_tinst_level PARAMS ((tree));
105 static tree classtype_mangled_name PARAMS ((tree));
106 static char *mangle_class_name_for_template PARAMS ((const char *, tree, tree));
107 static tree tsubst_initializer_list PARAMS ((tree, tree));
108 static int list_eq PARAMS ((tree, tree));
109 static tree get_class_bindings PARAMS ((tree, tree, tree));
110 static tree coerce_template_parms PARAMS ((tree, tree, tree, int, int));
111 static void tsubst_enum PARAMS ((tree, tree, tree));
112 static tree add_to_template_args PARAMS ((tree, tree));
113 static tree add_outermost_template_args PARAMS ((tree, tree));
114 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
115 tree*));
116 static int type_unification_real PARAMS ((tree, tree, tree, tree,
117 int, unification_kind_t, int, int));
118 static void note_template_header PARAMS ((int));
119 static tree maybe_fold_nontype_arg PARAMS ((tree));
120 static tree convert_nontype_argument PARAMS ((tree, tree));
121 static tree convert_template_argument PARAMS ((tree, tree, tree, int,
122 int , tree));
123 static tree get_bindings_overload PARAMS ((tree, tree, tree));
124 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*));
125 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
126 static int inline_needs_template_parms PARAMS ((tree));
127 static void push_inline_template_parms_recursive PARAMS ((tree, int));
128 static tree retrieve_specialization PARAMS ((tree, tree));
129 static tree retrieve_local_specialization PARAMS ((tree));
130 static tree register_specialization PARAMS ((tree, tree, tree));
131 static void register_local_specialization PARAMS ((tree, tree));
132 static int unregister_specialization PARAMS ((tree, tree));
133 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
134 static tree build_template_decl PARAMS ((tree, tree));
135 static int mark_template_parm PARAMS ((tree, void *));
136 static tree tsubst_friend_function PARAMS ((tree, tree));
137 static tree tsubst_friend_class PARAMS ((tree, tree));
138 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
139 static int template_decl_level PARAMS ((tree));
140 static tree maybe_get_template_decl_from_type_decl PARAMS ((tree));
141 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
142 static tree tsubst_template_arg_vector PARAMS ((tree, tree, int));
143 static tree tsubst_template_parms PARAMS ((tree, tree, int));
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, int, tree, int));
149 static tree tsubst_decl PARAMS ((tree, tree, tree));
150 static tree tsubst_arg_types PARAMS ((tree, tree, int, tree));
151 static tree tsubst_function_type PARAMS ((tree, tree, int, 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, int, tree));
157 static tree get_template_base_recursive PARAMS ((tree, tree,
158 tree, tree, tree, int));
159 static tree get_template_base PARAMS ((tree, tree, tree, tree));
160 static int verify_class_unification PARAMS ((tree, tree, tree));
161 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
162 static int coerce_template_template_parms PARAMS ((tree, tree, int,
163 tree, tree));
164 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
165 static int template_args_equal PARAMS ((tree, tree));
166 static void tsubst_default_arguments PARAMS ((tree));
167 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
168 static tree instantiate_clone PARAMS ((tree, tree));
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, int));
173 /* Called once to initialize pt.c. */
175 void
176 init_pt ()
178 ggc_add_tree_root (&pending_templates, 1);
179 ggc_add_tree_root (&maybe_templates, 1);
180 ggc_add_tree_root (&saved_trees, 1);
181 ggc_add_tree_root (&current_tinst_level, 1);
184 /* Do any processing required when DECL (a member template declaration
185 using TEMPLATE_PARAMETERS as its innermost parameter list) is
186 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
187 it is a specialization, in which case the DECL itself is returned. */
189 tree
190 finish_member_template_decl (decl)
191 tree decl;
193 if (decl == NULL_TREE || decl == void_type_node)
194 return NULL_TREE;
195 else if (decl == error_mark_node)
196 /* By returning NULL_TREE, the parser will just ignore this
197 declaration. We have already issued the error. */
198 return NULL_TREE;
199 else if (TREE_CODE (decl) == TREE_LIST)
201 /* Assume that the class is the only declspec. */
202 decl = TREE_VALUE (decl);
203 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
204 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
206 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
207 check_member_template (tmpl);
208 return tmpl;
210 return NULL_TREE;
212 else if (TREE_CODE (decl) == FIELD_DECL)
213 cp_error ("data member `%D' cannot be a member template", decl);
214 else if (DECL_TEMPLATE_INFO (decl))
216 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
218 check_member_template (DECL_TI_TEMPLATE (decl));
219 return DECL_TI_TEMPLATE (decl);
221 else
222 return decl;
224 else
225 cp_error ("invalid member template declaration `%D'", decl);
227 return error_mark_node;
230 /* Returns the template nesting level of the indicated class TYPE.
232 For example, in:
233 template <class T>
234 struct A
236 template <class U>
237 struct B {};
240 A<T>::B<U> has depth two, while A<T> has depth one.
241 Both A<T>::B<int> and A<int>::B<U> have depth one, if
242 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
243 specializations.
245 This function is guaranteed to return 0 if passed NULL_TREE so
246 that, for example, `template_class_depth (current_class_type)' is
247 always safe. */
249 static int
250 template_class_depth_real (type, count_specializations)
251 tree type;
252 int count_specializations;
254 int depth;
256 for (depth = 0;
257 type && TREE_CODE (type) != NAMESPACE_DECL;
258 type = (TREE_CODE (type) == FUNCTION_DECL)
259 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
261 if (TREE_CODE (type) != FUNCTION_DECL)
263 if (CLASSTYPE_TEMPLATE_INFO (type)
264 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
265 && ((count_specializations
266 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
267 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
268 ++depth;
270 else
272 if (DECL_TEMPLATE_INFO (type)
273 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
274 && ((count_specializations
275 && DECL_TEMPLATE_SPECIALIZATION (type))
276 || uses_template_parms (DECL_TI_ARGS (type))))
277 ++depth;
281 return depth;
284 /* Returns the template nesting level of the indicated class TYPE.
285 Like template_class_depth_real, but instantiations do not count in
286 the depth. */
288 int
289 template_class_depth (type)
290 tree type;
292 return template_class_depth_real (type, /*count_specializations=*/0);
295 /* Returns 1 if processing DECL as part of do_pending_inlines
296 needs us to push template parms. */
298 static int
299 inline_needs_template_parms (decl)
300 tree decl;
302 if (! DECL_TEMPLATE_INFO (decl))
303 return 0;
305 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
306 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
309 /* Subroutine of maybe_begin_member_template_processing.
310 Push the template parms in PARMS, starting from LEVELS steps into the
311 chain, and ending at the beginning, since template parms are listed
312 innermost first. */
314 static void
315 push_inline_template_parms_recursive (parmlist, levels)
316 tree parmlist;
317 int levels;
319 tree parms = TREE_VALUE (parmlist);
320 int i;
322 if (levels > 1)
323 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
325 ++processing_template_decl;
326 current_template_parms
327 = tree_cons (size_int (processing_template_decl),
328 parms, current_template_parms);
329 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
331 pushlevel (0);
332 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
334 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
335 my_friendly_assert (DECL_P (parm), 0);
337 switch (TREE_CODE (parm))
339 case TYPE_DECL:
340 case TEMPLATE_DECL:
341 pushdecl (parm);
342 break;
344 case PARM_DECL:
346 /* Make a CONST_DECL as is done in process_template_parm.
347 It is ugly that we recreate this here; the original
348 version built in process_template_parm is no longer
349 available. */
350 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
351 TREE_TYPE (parm));
352 DECL_ARTIFICIAL (decl) = 1;
353 DECL_INITIAL (decl) = DECL_INITIAL (parm);
354 SET_DECL_TEMPLATE_PARM_P (decl);
355 pushdecl (decl);
357 break;
359 default:
360 my_friendly_abort (0);
365 /* Restore the template parameter context for a member template or
366 a friend template defined in a class definition. */
368 void
369 maybe_begin_member_template_processing (decl)
370 tree decl;
372 tree parms;
373 int levels = 0;
375 if (inline_needs_template_parms (decl))
377 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
378 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
380 if (DECL_TEMPLATE_SPECIALIZATION (decl))
382 --levels;
383 parms = TREE_CHAIN (parms);
386 push_inline_template_parms_recursive (parms, levels);
389 /* Remember how many levels of template parameters we pushed so that
390 we can pop them later. */
391 if (!inline_parm_levels)
392 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
393 if (inline_parm_levels_used == inline_parm_levels->num_elements)
394 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
395 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
396 ++inline_parm_levels_used;
399 /* Undo the effects of begin_member_template_processing. */
401 void
402 maybe_end_member_template_processing ()
404 int i;
406 if (!inline_parm_levels_used)
407 return;
409 --inline_parm_levels_used;
410 for (i = 0;
411 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
412 ++i)
414 --processing_template_decl;
415 current_template_parms = TREE_CHAIN (current_template_parms);
416 poplevel (0, 0, 0);
420 /* Returns non-zero iff T is a member template function. We must be
421 careful as in
423 template <class T> class C { void f(); }
425 Here, f is a template function, and a member, but not a member
426 template. This function does not concern itself with the origin of
427 T, only its present state. So if we have
429 template <class T> class C { template <class U> void f(U); }
431 then neither C<int>::f<char> nor C<T>::f<double> is considered
432 to be a member template. But, `template <class U> void
433 C<int>::f(U)' is considered a member template. */
436 is_member_template (t)
437 tree t;
439 if (!DECL_FUNCTION_TEMPLATE_P (t))
440 /* Anything that isn't a function or a template function is
441 certainly not a member template. */
442 return 0;
444 /* A local class can't have member templates. */
445 if (decl_function_context (t))
446 return 0;
448 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
449 /* If there are more levels of template parameters than
450 there are template classes surrounding the declaration,
451 then we have a member template. */
452 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
453 template_class_depth (DECL_CONTEXT (t))));
456 #if 0 /* UNUSED */
457 /* Returns non-zero iff T is a member template class. See
458 is_member_template for a description of what precisely constitutes
459 a member template. */
462 is_member_template_class (t)
463 tree t;
465 if (!DECL_CLASS_TEMPLATE_P (t))
466 /* Anything that isn't a class template, is certainly not a member
467 template. */
468 return 0;
470 if (!DECL_CLASS_SCOPE_P (t))
471 /* Anything whose context isn't a class type is surely not a
472 member template. */
473 return 0;
475 /* If there are more levels of template parameters than there are
476 template classes surrounding the declaration, then we have a
477 member template. */
478 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
479 template_class_depth (DECL_CONTEXT (t)));
481 #endif
483 /* Return a new template argument vector which contains all of ARGS,
484 but has as its innermost set of arguments the EXTRA_ARGS. */
486 static tree
487 add_to_template_args (args, extra_args)
488 tree args;
489 tree extra_args;
491 tree new_args;
492 int extra_depth;
493 int i;
494 int j;
496 extra_depth = TMPL_ARGS_DEPTH (extra_args);
497 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
499 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
500 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
502 for (j = 1; j <= extra_depth; ++j, ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
505 return new_args;
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
515 static tree
516 add_outermost_template_args (args, extra_args)
517 tree args;
518 tree extra_args;
520 tree new_args;
522 /* If there are more levels of EXTRA_ARGS than there are ARGS,
523 something very fishy is going on. */
524 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
527 /* If *all* the new arguments will be the EXTRA_ARGS, just return
528 them. */
529 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
530 return extra_args;
532 /* For the moment, we make ARGS look like it contains fewer levels. */
533 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
535 new_args = add_to_template_args (args, extra_args);
537 /* Now, we restore ARGS to its full dimensions. */
538 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
540 return new_args;
543 /* Return the N levels of innermost template arguments from the ARGS. */
545 tree
546 get_innermost_template_args (args, n)
547 tree args;
548 int n;
550 tree new_args;
551 int extra_levels;
552 int i;
554 my_friendly_assert (n >= 0, 20000603);
556 /* If N is 1, just return the innermost set of template arguments. */
557 if (n == 1)
558 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
560 /* If we're not removing anything, just return the arguments we were
561 given. */
562 extra_levels = TMPL_ARGS_DEPTH (args) - n;
563 my_friendly_assert (extra_levels >= 0, 20000603);
564 if (extra_levels == 0)
565 return args;
567 /* Make a new set of arguments, not containing the outer arguments. */
568 new_args = make_tree_vec (n);
569 for (i = 1; i <= n; ++i)
570 SET_TMPL_ARGS_LEVEL (new_args, i,
571 TMPL_ARGS_LEVEL (args, i + extra_levels));
573 return new_args;
576 /* We've got a template header coming up; push to a new level for storing
577 the parms. */
579 void
580 begin_template_parm_list ()
582 /* We use a non-tag-transparent scope here, which causes pushtag to
583 put tags in this scope, rather than in the enclosing class or
584 namespace scope. This is the right thing, since we want
585 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
586 global template class, push_template_decl handles putting the
587 TEMPLATE_DECL into top-level scope. For a nested template class,
588 e.g.:
590 template <class T> struct S1 {
591 template <class T> struct S2 {};
594 pushtag contains special code to call pushdecl_with_scope on the
595 TEMPLATE_DECL for S2. */
596 begin_scope (sk_template_parms);
597 ++processing_template_decl;
598 ++processing_template_parmlist;
599 note_template_header (0);
602 /* This routine is called when a specialization is declared. If it is
603 illegal to declare a specialization here, an error is reported. */
605 static void
606 check_specialization_scope ()
608 tree scope = current_scope ();
610 /* [temp.expl.spec]
612 An explicit specialization shall be declared in the namespace of
613 which the template is a member, or, for member templates, in the
614 namespace of which the enclosing class or enclosing class
615 template is a member. An explicit specialization of a member
616 function, member class or static data member of a class template
617 shall be declared in the namespace of which the class template
618 is a member. */
619 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
620 cp_error ("explicit specialization in non-namespace scope `%D'",
621 scope);
623 /* [temp.expl.spec]
625 In an explicit specialization declaration for a member of a class
626 template or a member template that appears in namespace scope,
627 the member template and some of its enclosing class templates may
628 remain unspecialized, except that the declaration shall not
629 explicitly specialize a class member template if its enclosing
630 class templates are not explicitly specialized as well. */
631 if (current_template_parms)
632 cp_error ("enclosing class templates are not explicitly specialized");
635 /* We've just seen template <>. */
637 void
638 begin_specialization ()
640 begin_scope (sk_template_spec);
641 note_template_header (1);
642 check_specialization_scope ();
645 /* Called at then end of processing a declaration preceded by
646 template<>. */
648 void
649 end_specialization ()
651 finish_scope ();
652 reset_specialization ();
655 /* Any template <>'s that we have seen thus far are not referring to a
656 function specialization. */
658 void
659 reset_specialization ()
661 processing_specialization = 0;
662 template_header_count = 0;
665 /* We've just seen a template header. If SPECIALIZATION is non-zero,
666 it was of the form template <>. */
668 static void
669 note_template_header (specialization)
670 int specialization;
672 processing_specialization = specialization;
673 template_header_count++;
676 /* We're beginning an explicit instantiation. */
678 void
679 begin_explicit_instantiation ()
681 ++processing_explicit_instantiation;
685 void
686 end_explicit_instantiation ()
688 my_friendly_assert(processing_explicit_instantiation > 0, 0);
689 --processing_explicit_instantiation;
692 /* The TYPE is being declared. If it is a template type, that means it
693 is a partial specialization. Do appropriate error-checking. */
695 void
696 maybe_process_partial_specialization (type)
697 tree type;
699 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
701 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
702 && !COMPLETE_TYPE_P (type))
704 if (current_namespace
705 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
707 cp_pedwarn ("specializing `%#T' in different namespace", type);
708 cp_pedwarn_at (" from definition of `%#D'",
709 CLASSTYPE_TI_TEMPLATE (type));
711 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
712 if (processing_template_decl)
713 push_template_decl (TYPE_MAIN_DECL (type));
715 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
716 cp_error ("specialization of `%T' after instantiation", type);
718 else if (processing_specialization)
719 cp_error ("explicit specialization of non-template `%T'", type);
722 /* Retrieve the specialization (in the sense of [temp.spec] - a
723 specialization is either an instantiation or an explicit
724 specialization) of TMPL for the given template ARGS. If there is
725 no such specialization, return NULL_TREE. The ARGS are a vector of
726 arguments, or a vector of vectors of arguments, in the case of
727 templates with more than one level of parameters. */
729 static tree
730 retrieve_specialization (tmpl, args)
731 tree tmpl;
732 tree args;
734 tree s;
736 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
738 /* There should be as many levels of arguments as there are
739 levels of parameters. */
740 my_friendly_assert (TMPL_ARGS_DEPTH (args)
741 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
744 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
745 s != NULL_TREE;
746 s = TREE_CHAIN (s))
747 if (comp_template_args (TREE_PURPOSE (s), args))
748 return TREE_VALUE (s);
750 return NULL_TREE;
753 /* Like retrieve_specialization, but for local declarations. */
755 static tree
756 retrieve_local_specialization (tmpl)
757 tree tmpl;
759 return (tree) htab_find (local_specializations, tmpl);
762 /* Returns non-zero iff DECL is a specialization of TMPL. */
765 is_specialization_of (decl, tmpl)
766 tree decl;
767 tree tmpl;
769 tree t;
771 if (TREE_CODE (decl) == FUNCTION_DECL)
773 for (t = decl;
774 t != NULL_TREE;
775 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
776 if (t == tmpl)
777 return 1;
779 else
781 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
783 for (t = TREE_TYPE (decl);
784 t != NULL_TREE;
785 t = CLASSTYPE_USE_TEMPLATE (t)
786 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
787 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
788 return 1;
791 return 0;
794 /* Register the specialization SPEC as a specialization of TMPL with
795 the indicated ARGS. Returns SPEC, or an equivalent prior
796 declaration, if available. */
798 static tree
799 register_specialization (spec, tmpl, args)
800 tree spec;
801 tree tmpl;
802 tree args;
804 tree s;
806 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
808 if (TREE_CODE (spec) == FUNCTION_DECL
809 && uses_template_parms (DECL_TI_ARGS (spec)))
810 /* This is the FUNCTION_DECL for a partial instantiation. Don't
811 register it; we want the corresponding TEMPLATE_DECL instead.
812 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
813 the more obvious `uses_template_parms (spec)' to avoid problems
814 with default function arguments. In particular, given
815 something like this:
817 template <class T> void f(T t1, T t = T())
819 the default argument expression is not substituted for in an
820 instantiation unless and until it is actually needed. */
821 return spec;
823 /* There should be as many levels of arguments as there are
824 levels of parameters. */
825 my_friendly_assert (TMPL_ARGS_DEPTH (args)
826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
829 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
830 s != NULL_TREE;
831 s = TREE_CHAIN (s))
833 tree fn = TREE_VALUE (s);
835 /* We can sometimes try to re-register a specialization that we've
836 already got. In particular, regenerate_decl_from_template
837 calls duplicate_decls which will update the specialization
838 list. But, we'll still get called again here anyhow. It's
839 more convenient to simply allow this than to try to prevent it. */
840 if (fn == spec)
841 return spec;
842 else if (comp_template_args (TREE_PURPOSE (s), args))
844 if (DECL_TEMPLATE_SPECIALIZATION (spec))
846 if (DECL_TEMPLATE_INSTANTIATION (fn))
848 if (TREE_USED (fn)
849 || DECL_EXPLICIT_INSTANTIATION (fn))
851 cp_error ("specialization of %D after instantiation",
852 fn);
853 return spec;
855 else
857 /* This situation should occur only if the first
858 specialization is an implicit instantiation,
859 the second is an explicit specialization, and
860 the implicit instantiation has not yet been
861 used. That situation can occur if we have
862 implicitly instantiated a member function and
863 then specialized it later.
865 We can also wind up here if a friend
866 declaration that looked like an instantiation
867 turns out to be a specialization:
869 template <class T> void foo(T);
870 class S { friend void foo<>(int) };
871 template <> void foo(int);
873 We transform the existing DECL in place so that
874 any pointers to it become pointers to the
875 updated declaration.
877 If there was a definition for the template, but
878 not for the specialization, we want this to
879 look as if there is no definition, and vice
880 versa. */
881 DECL_INITIAL (fn) = NULL_TREE;
882 duplicate_decls (spec, fn);
884 return fn;
887 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
889 duplicate_decls (spec, fn);
890 return fn;
896 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
897 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
899 return spec;
902 /* Unregister the specialization SPEC as a specialization of TMPL.
903 Returns nonzero if the SPEC was listed as a specialization of
904 TMPL. */
906 static int
907 unregister_specialization (spec, tmpl)
908 tree spec;
909 tree tmpl;
911 tree* s;
913 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
914 *s != NULL_TREE;
915 s = &TREE_CHAIN (*s))
916 if (TREE_VALUE (*s) == spec)
918 *s = TREE_CHAIN (*s);
919 return 1;
922 return 0;
925 /* Like register_specialization, but for local declarations. We are
926 registering SPEC, an instantiation of TMPL. */
928 static void
929 register_local_specialization (spec, tmpl)
930 tree spec;
931 tree tmpl;
933 void **slot;
935 slot = htab_find_slot (local_specializations, tmpl, INSERT);
936 *slot = spec;
939 /* Print the list of candidate FNS in an error message. */
941 void
942 print_candidates (fns)
943 tree fns;
945 tree fn;
947 const char *str = "candidates are:";
949 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
951 tree f;
953 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
954 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
955 str = " ";
959 /* Returns the template (one of the functions given by TEMPLATE_ID)
960 which can be specialized to match the indicated DECL with the
961 explicit template args given in TEMPLATE_ID. The DECL may be
962 NULL_TREE if none is available. In that case, the functions in
963 TEMPLATE_ID are non-members.
965 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
966 specialization of a member template.
968 The template args (those explicitly specified and those deduced)
969 are output in a newly created vector *TARGS_OUT.
971 If it is impossible to determine the result, an error message is
972 issued. The error_mark_node is returned to indicate failure. */
974 static tree
975 determine_specialization (template_id, decl, targs_out,
976 need_member_template)
977 tree template_id;
978 tree decl;
979 tree* targs_out;
980 int need_member_template;
982 tree fns;
983 tree targs;
984 tree explicit_targs;
985 tree candidates = NULL_TREE;
986 tree templates = NULL_TREE;
988 *targs_out = NULL_TREE;
990 if (template_id == error_mark_node)
991 return error_mark_node;
993 fns = TREE_OPERAND (template_id, 0);
994 explicit_targs = TREE_OPERAND (template_id, 1);
996 if (fns == error_mark_node)
997 return error_mark_node;
999 /* Check for baselinks. */
1000 if (BASELINK_P (fns))
1001 fns = TREE_VALUE (fns);
1003 if (!is_overloaded_fn (fns))
1005 cp_error ("`%D' is not a function template", fns);
1006 return error_mark_node;
1009 for (; fns; fns = OVL_NEXT (fns))
1011 tree tmpl;
1013 tree fn = OVL_CURRENT (fns);
1015 if (TREE_CODE (fn) == TEMPLATE_DECL)
1016 /* DECL might be a specialization of FN. */
1017 tmpl = fn;
1018 else if (need_member_template)
1019 /* FN is an ordinary member function, and we need a
1020 specialization of a member template. */
1021 continue;
1022 else if (TREE_CODE (fn) != FUNCTION_DECL)
1023 /* We can get IDENTIFIER_NODEs here in certain erroneous
1024 cases. */
1025 continue;
1026 else if (!DECL_FUNCTION_MEMBER_P (fn))
1027 /* This is just an ordinary non-member function. Nothing can
1028 be a specialization of that. */
1029 continue;
1030 else
1032 tree decl_arg_types;
1034 /* This is an ordinary member function. However, since
1035 we're here, we can assume it's enclosing class is a
1036 template class. For example,
1038 template <typename T> struct S { void f(); };
1039 template <> void S<int>::f() {}
1041 Here, S<int>::f is a non-template, but S<int> is a
1042 template class. If FN has the same type as DECL, we
1043 might be in business. */
1044 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1045 TREE_TYPE (TREE_TYPE (fn))))
1046 /* The return types differ. */
1047 continue;
1049 /* Adjust the type of DECL in case FN is a static member. */
1050 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1051 if (DECL_STATIC_FUNCTION_P (fn)
1052 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1053 decl_arg_types = TREE_CHAIN (decl_arg_types);
1055 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1056 decl_arg_types))
1057 /* They match! */
1058 candidates = tree_cons (NULL_TREE, fn, candidates);
1060 continue;
1063 /* See whether this function might be a specialization of this
1064 template. */
1065 targs = get_bindings (tmpl, decl, explicit_targs);
1067 if (!targs)
1068 /* We cannot deduce template arguments that when used to
1069 specialize TMPL will produce DECL. */
1070 continue;
1072 /* Save this template, and the arguments deduced. */
1073 templates = tree_cons (targs, tmpl, templates);
1076 if (templates && TREE_CHAIN (templates))
1078 /* We have:
1080 [temp.expl.spec]
1082 It is possible for a specialization with a given function
1083 signature to be instantiated from more than one function
1084 template. In such cases, explicit specification of the
1085 template arguments must be used to uniquely identify the
1086 function template specialization being specialized.
1088 Note that here, there's no suggestion that we're supposed to
1089 determine which of the candidate templates is most
1090 specialized. However, we, also have:
1092 [temp.func.order]
1094 Partial ordering of overloaded function template
1095 declarations is used in the following contexts to select
1096 the function template to which a function template
1097 specialization refers:
1099 -- when an explicit specialization refers to a function
1100 template.
1102 So, we do use the partial ordering rules, at least for now.
1103 This extension can only serve to make illegal programs legal,
1104 so it's safe. And, there is strong anecdotal evidence that
1105 the committee intended the partial ordering rules to apply;
1106 the EDG front-end has that behavior, and John Spicer claims
1107 that the committee simply forgot to delete the wording in
1108 [temp.expl.spec]. */
1109 tree tmpl = most_specialized (templates, decl, explicit_targs);
1110 if (tmpl && tmpl != error_mark_node)
1112 targs = get_bindings (tmpl, decl, explicit_targs);
1113 templates = tree_cons (targs, tmpl, NULL_TREE);
1117 if (templates == NULL_TREE && candidates == NULL_TREE)
1119 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1120 template_id, decl);
1121 return error_mark_node;
1123 else if ((templates && TREE_CHAIN (templates))
1124 || (candidates && TREE_CHAIN (candidates))
1125 || (templates && candidates))
1127 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1128 template_id, decl);
1129 chainon (candidates, templates);
1130 print_candidates (candidates);
1131 return error_mark_node;
1134 /* We have one, and exactly one, match. */
1135 if (candidates)
1137 /* It was a specialization of an ordinary member function in a
1138 template class. */
1139 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1140 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1143 /* It was a specialization of a template. */
1144 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1145 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1147 *targs_out = copy_node (targs);
1148 SET_TMPL_ARGS_LEVEL (*targs_out,
1149 TMPL_ARGS_DEPTH (*targs_out),
1150 TREE_PURPOSE (templates));
1152 else
1153 *targs_out = TREE_PURPOSE (templates);
1154 return TREE_VALUE (templates);
1157 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1158 but with the default argument values filled in from those in the
1159 TMPL_TYPES. */
1161 static tree
1162 copy_default_args_to_explicit_spec_1 (spec_types,
1163 tmpl_types)
1164 tree spec_types;
1165 tree tmpl_types;
1167 tree new_spec_types;
1169 if (!spec_types)
1170 return NULL_TREE;
1172 if (spec_types == void_list_node)
1173 return void_list_node;
1175 /* Substitute into the rest of the list. */
1176 new_spec_types =
1177 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1178 TREE_CHAIN (tmpl_types));
1180 /* Add the default argument for this parameter. */
1181 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1182 TREE_VALUE (spec_types),
1183 new_spec_types);
1186 /* DECL is an explicit specialization. Replicate default arguments
1187 from the template it specializes. (That way, code like:
1189 template <class T> void f(T = 3);
1190 template <> void f(double);
1191 void g () { f (); }
1193 works, as required.) An alternative approach would be to look up
1194 the correct default arguments at the call-site, but this approach
1195 is consistent with how implicit instantiations are handled. */
1197 static void
1198 copy_default_args_to_explicit_spec (decl)
1199 tree decl;
1201 tree tmpl;
1202 tree spec_types;
1203 tree tmpl_types;
1204 tree new_spec_types;
1205 tree old_type;
1206 tree new_type;
1207 tree t;
1208 tree object_type = NULL_TREE;
1209 tree in_charge = NULL_TREE;
1210 tree vtt = NULL_TREE;
1212 /* See if there's anything we need to do. */
1213 tmpl = DECL_TI_TEMPLATE (decl);
1214 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1215 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1216 if (TREE_PURPOSE (t))
1217 break;
1218 if (!t)
1219 return;
1221 old_type = TREE_TYPE (decl);
1222 spec_types = TYPE_ARG_TYPES (old_type);
1224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1226 /* Remove the this pointer, but remember the object's type for
1227 CV quals. */
1228 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1229 spec_types = TREE_CHAIN (spec_types);
1230 tmpl_types = TREE_CHAIN (tmpl_types);
1232 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1234 /* DECL may contain more parameters than TMPL due to the extra
1235 in-charge parameter in constructors and destructors. */
1236 in_charge = spec_types;
1237 spec_types = TREE_CHAIN (spec_types);
1239 if (DECL_HAS_VTT_PARM_P (decl))
1241 vtt = spec_types;
1242 spec_types = TREE_CHAIN (spec_types);
1246 /* Compute the merged default arguments. */
1247 new_spec_types =
1248 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1250 /* Compute the new FUNCTION_TYPE. */
1251 if (object_type)
1253 if (vtt)
1254 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1255 TREE_VALUE (vtt),
1256 new_spec_types);
1258 if (in_charge)
1259 /* Put the in-charge parameter back. */
1260 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1261 TREE_VALUE (in_charge),
1262 new_spec_types);
1264 new_type = build_cplus_method_type (object_type,
1265 TREE_TYPE (old_type),
1266 new_spec_types);
1268 else
1269 new_type = build_function_type (TREE_TYPE (old_type),
1270 new_spec_types);
1271 new_type = build_type_attribute_variant (new_type,
1272 TYPE_ATTRIBUTES (old_type));
1273 new_type = build_exception_variant (new_type,
1274 TYPE_RAISES_EXCEPTIONS (old_type));
1275 TREE_TYPE (decl) = new_type;
1278 /* Check to see if the function just declared, as indicated in
1279 DECLARATOR, and in DECL, is a specialization of a function
1280 template. We may also discover that the declaration is an explicit
1281 instantiation at this point.
1283 Returns DECL, or an equivalent declaration that should be used
1284 instead if all goes well. Issues an error message if something is
1285 amiss. Returns error_mark_node if the error is not easily
1286 recoverable.
1288 FLAGS is a bitmask consisting of the following flags:
1290 2: The function has a definition.
1291 4: The function is a friend.
1293 The TEMPLATE_COUNT is the number of references to qualifying
1294 template classes that appeared in the name of the function. For
1295 example, in
1297 template <class T> struct S { void f(); };
1298 void S<int>::f();
1300 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1301 classes are not counted in the TEMPLATE_COUNT, so that in
1303 template <class T> struct S {};
1304 template <> struct S<int> { void f(); }
1305 template <> void S<int>::f();
1307 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1308 illegal; there should be no template <>.)
1310 If the function is a specialization, it is marked as such via
1311 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1312 is set up correctly, and it is added to the list of specializations
1313 for that template. */
1315 tree
1316 check_explicit_specialization (declarator, decl, template_count, flags)
1317 tree declarator;
1318 tree decl;
1319 int template_count;
1320 int flags;
1322 int have_def = flags & 2;
1323 int is_friend = flags & 4;
1324 int specialization = 0;
1325 int explicit_instantiation = 0;
1326 int member_specialization = 0;
1327 tree ctype = DECL_CLASS_CONTEXT (decl);
1328 tree dname = DECL_NAME (decl);
1329 tmpl_spec_kind tsk;
1331 tsk = current_tmpl_spec_kind (template_count);
1333 switch (tsk)
1335 case tsk_none:
1336 if (processing_specialization)
1338 specialization = 1;
1339 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1341 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1343 if (is_friend)
1344 /* This could be something like:
1346 template <class T> void f(T);
1347 class S { friend void f<>(int); } */
1348 specialization = 1;
1349 else
1351 /* This case handles bogus declarations like template <>
1352 template <class T> void f<int>(); */
1354 cp_error ("template-id `%D' in declaration of primary template",
1355 declarator);
1356 return decl;
1359 break;
1361 case tsk_invalid_member_spec:
1362 /* The error has already been reported in
1363 check_specialization_scope. */
1364 return error_mark_node;
1366 case tsk_invalid_expl_inst:
1367 cp_error ("template parameter list used in explicit instantiation");
1369 /* Fall through. */
1371 case tsk_expl_inst:
1372 if (have_def)
1373 cp_error ("definition provided for explicit instantiation");
1375 explicit_instantiation = 1;
1376 break;
1378 case tsk_excessive_parms:
1379 cp_error ("too many template parameter lists in declaration of `%D'",
1380 decl);
1381 return error_mark_node;
1383 /* Fall through. */
1384 case tsk_expl_spec:
1385 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1386 if (ctype)
1387 member_specialization = 1;
1388 else
1389 specialization = 1;
1390 break;
1392 case tsk_insufficient_parms:
1393 if (template_header_count)
1395 cp_error("too few template parameter lists in declaration of `%D'",
1396 decl);
1397 return decl;
1399 else if (ctype != NULL_TREE
1400 && !TYPE_BEING_DEFINED (ctype)
1401 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1402 && !is_friend)
1404 /* For backwards compatibility, we accept:
1406 template <class T> struct S { void f(); };
1407 void S<int>::f() {} // Missing template <>
1409 That used to be legal C++. */
1410 if (pedantic)
1411 cp_pedwarn
1412 ("explicit specialization not preceded by `template <>'");
1413 specialization = 1;
1414 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1416 break;
1418 case tsk_template:
1419 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1421 /* This case handles bogus declarations like template <>
1422 template <class T> void f<int>(); */
1424 if (uses_template_parms (declarator))
1425 cp_error ("partial specialization `%D' of function template",
1426 declarator);
1427 else
1428 cp_error ("template-id `%D' in declaration of primary template",
1429 declarator);
1430 return decl;
1433 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1434 /* This is a specialization of a member template, without
1435 specialization the containing class. Something like:
1437 template <class T> struct S {
1438 template <class U> void f (U);
1440 template <> template <class U> void S<int>::f(U) {}
1442 That's a specialization -- but of the entire template. */
1443 specialization = 1;
1444 break;
1446 default:
1447 my_friendly_abort (20000309);
1450 if (specialization || member_specialization)
1452 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1453 for (; t; t = TREE_CHAIN (t))
1454 if (TREE_PURPOSE (t))
1456 cp_pedwarn
1457 ("default argument specified in explicit specialization");
1458 break;
1460 if (current_lang_name == lang_name_c)
1461 cp_error ("template specialization with C linkage");
1464 if (specialization || member_specialization || explicit_instantiation)
1466 tree tmpl = NULL_TREE;
1467 tree targs = NULL_TREE;
1469 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1470 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1472 tree fns;
1474 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1476 if (!ctype)
1477 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1478 else
1479 fns = dname;
1481 declarator =
1482 lookup_template_function (fns, NULL_TREE);
1485 if (declarator == error_mark_node)
1486 return error_mark_node;
1488 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1490 if (!explicit_instantiation)
1491 /* A specialization in class scope. This is illegal,
1492 but the error will already have been flagged by
1493 check_specialization_scope. */
1494 return error_mark_node;
1495 else
1497 /* It's not legal to write an explicit instantiation in
1498 class scope, e.g.:
1500 class C { template void f(); }
1502 This case is caught by the parser. However, on
1503 something like:
1505 template class C { void f(); };
1507 (which is illegal) we can get here. The error will be
1508 issued later. */
1512 return decl;
1514 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1516 /* A friend declaration. We can't do much, because we don't
1517 know what this resolves to, yet. */
1518 my_friendly_assert (is_friend != 0, 0);
1519 my_friendly_assert (!explicit_instantiation, 0);
1520 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1521 return decl;
1523 else if (ctype != NULL_TREE
1524 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1525 IDENTIFIER_NODE))
1527 /* Find the list of functions in ctype that have the same
1528 name as the declared function. */
1529 tree name = TREE_OPERAND (declarator, 0);
1530 tree fns = NULL_TREE;
1531 int idx;
1533 if (name == constructor_name (ctype)
1534 || name == constructor_name_full (ctype))
1536 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1538 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1539 : !TYPE_HAS_DESTRUCTOR (ctype))
1541 /* From [temp.expl.spec]:
1543 If such an explicit specialization for the member
1544 of a class template names an implicitly-declared
1545 special member function (clause _special_), the
1546 program is ill-formed.
1548 Similar language is found in [temp.explicit]. */
1549 cp_error ("specialization of implicitly-declared special member function");
1550 return error_mark_node;
1553 name = is_constructor ? ctor_identifier : dtor_identifier;
1556 if (!DECL_CONV_FN_P (decl))
1558 idx = lookup_fnfields_1 (ctype, name);
1559 if (idx >= 0)
1560 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1562 else
1564 tree methods;
1566 /* For a type-conversion operator, we cannot do a
1567 name-based lookup. We might be looking for `operator
1568 int' which will be a specialization of `operator T'.
1569 So, we find *all* the conversion operators, and then
1570 select from them. */
1571 fns = NULL_TREE;
1573 methods = CLASSTYPE_METHOD_VEC (ctype);
1574 if (methods)
1575 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1577 tree ovl = TREE_VEC_ELT (methods, idx);
1579 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1580 /* There are no more conversion functions. */
1581 break;
1583 /* Glue all these conversion functions together
1584 with those we already have. */
1585 for (; ovl; ovl = OVL_NEXT (ovl))
1586 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1590 if (fns == NULL_TREE)
1592 cp_error ("no member function `%D' declared in `%T'",
1593 name, ctype);
1594 return error_mark_node;
1596 else
1597 TREE_OPERAND (declarator, 0) = fns;
1600 /* Figure out what exactly is being specialized at this point.
1601 Note that for an explicit instantiation, even one for a
1602 member function, we cannot tell apriori whether the
1603 instantiation is for a member template, or just a member
1604 function of a template class. Even if a member template is
1605 being instantiated, the member template arguments may be
1606 elided if they can be deduced from the rest of the
1607 declaration. */
1608 tmpl = determine_specialization (declarator, decl,
1609 &targs,
1610 member_specialization);
1612 if (!tmpl || tmpl == error_mark_node)
1613 /* We couldn't figure out what this declaration was
1614 specializing. */
1615 return error_mark_node;
1616 else
1618 tree gen_tmpl = most_general_template (tmpl);
1620 if (explicit_instantiation)
1622 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1623 is done by do_decl_instantiation later. */
1625 int arg_depth = TMPL_ARGS_DEPTH (targs);
1626 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1628 if (arg_depth > parm_depth)
1630 /* If TMPL is not the most general template (for
1631 example, if TMPL is a friend template that is
1632 injected into namespace scope), then there will
1633 be too many levels of TARGS. Remove some of them
1634 here. */
1635 int i;
1636 tree new_targs;
1638 new_targs = make_tree_vec (parm_depth);
1639 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1640 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1641 = TREE_VEC_ELT (targs, i);
1642 targs = new_targs;
1645 return instantiate_template (tmpl, targs);
1648 /* If this is a specialization of a member template of a
1649 template class. In we want to return the TEMPLATE_DECL,
1650 not the specialization of it. */
1651 if (tsk == tsk_template)
1653 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1654 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1655 return tmpl;
1658 /* If we thought that the DECL was a member function, but it
1659 turns out to be specializing a static member function,
1660 make DECL a static member function as well. */
1661 if (DECL_STATIC_FUNCTION_P (tmpl)
1662 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1664 revert_static_member_fn (decl);
1665 last_function_parms = TREE_CHAIN (last_function_parms);
1668 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1669 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1671 /* Inherit default function arguments from the template
1672 DECL is specializing. */
1673 copy_default_args_to_explicit_spec (decl);
1675 if (is_friend && !have_def)
1676 /* This is not really a declaration of a specialization.
1677 It's just the name of an instantiation. But, it's not
1678 a request for an instantiation, either. */
1679 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1680 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1681 /* This is indeed a specialization. In case of constructors
1682 and destructors, we need in-charge and not-in-charge
1683 versions in V3 ABI. */
1684 clone_function_decl (decl, /*update_method_vec_p=*/0);
1686 /* Register this specialization so that we can find it
1687 again. */
1688 decl = register_specialization (decl, gen_tmpl, targs);
1692 return decl;
1695 /* TYPE is being declared. Verify that the use of template headers
1696 and such is reasonable. Issue error messages if not. */
1698 void
1699 maybe_check_template_type (type)
1700 tree type;
1702 if (template_header_count)
1704 /* We are in the scope of some `template <...>' header. */
1706 int context_depth
1707 = template_class_depth_real (TYPE_CONTEXT (type),
1708 /*count_specializations=*/1);
1710 if (template_header_count <= context_depth)
1711 /* This is OK; the template headers are for the context. We
1712 are actually too lenient here; like
1713 check_explicit_specialization we should consider the number
1714 of template types included in the actual declaration. For
1715 example,
1717 template <class T> struct S {
1718 template <class U> template <class V>
1719 struct I {};
1722 is illegal, but:
1724 template <class T> struct S {
1725 template <class U> struct I;
1728 template <class T> template <class U.
1729 struct S<T>::I {};
1731 is not. */
1733 else if (template_header_count > context_depth + 1)
1734 /* There are two many template parameter lists. */
1735 cp_error ("too many template parameter lists in declaration of `%T'", type);
1739 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1740 parameters. These are represented in the same format used for
1741 DECL_TEMPLATE_PARMS. */
1743 int comp_template_parms (parms1, parms2)
1744 tree parms1;
1745 tree parms2;
1747 tree p1;
1748 tree p2;
1750 if (parms1 == parms2)
1751 return 1;
1753 for (p1 = parms1, p2 = parms2;
1754 p1 != NULL_TREE && p2 != NULL_TREE;
1755 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1757 tree t1 = TREE_VALUE (p1);
1758 tree t2 = TREE_VALUE (p2);
1759 int i;
1761 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1762 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1764 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1765 return 0;
1767 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1769 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1770 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1772 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1773 return 0;
1775 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1776 continue;
1777 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1778 return 0;
1782 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1783 /* One set of parameters has more parameters lists than the
1784 other. */
1785 return 0;
1787 return 1;
1790 /* Complain if DECL shadows a template parameter.
1792 [temp.local]: A template-parameter shall not be redeclared within its
1793 scope (including nested scopes). */
1795 void
1796 check_template_shadow (decl)
1797 tree decl;
1799 tree olddecl;
1801 /* If we're not in a template, we can't possibly shadow a template
1802 parameter. */
1803 if (!current_template_parms)
1804 return;
1806 /* Figure out what we're shadowing. */
1807 if (TREE_CODE (decl) == OVERLOAD)
1808 decl = OVL_CURRENT (decl);
1809 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1811 /* If there's no previous binding for this name, we're not shadowing
1812 anything, let alone a template parameter. */
1813 if (!olddecl)
1814 return;
1816 /* If we're not shadowing a template parameter, we're done. Note
1817 that OLDDECL might be an OVERLOAD (or perhaps even an
1818 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1819 node. */
1820 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1821 return;
1823 /* We check for decl != olddecl to avoid bogus errors for using a
1824 name inside a class. We check TPFI to avoid duplicate errors for
1825 inline member templates. */
1826 if (decl == olddecl
1827 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1828 return;
1830 cp_error_at ("declaration of `%#D'", decl);
1831 cp_error_at (" shadows template parm `%#D'", olddecl);
1834 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1835 ORIG_LEVEL, DECL, and TYPE. */
1837 static tree
1838 build_template_parm_index (index, level, orig_level, decl, type)
1839 int index;
1840 int level;
1841 int orig_level;
1842 tree decl;
1843 tree type;
1845 tree t = make_node (TEMPLATE_PARM_INDEX);
1846 TEMPLATE_PARM_IDX (t) = index;
1847 TEMPLATE_PARM_LEVEL (t) = level;
1848 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1849 TEMPLATE_PARM_DECL (t) = decl;
1850 TREE_TYPE (t) = type;
1852 return t;
1855 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1856 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1857 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1858 new one is created. */
1860 static tree
1861 reduce_template_parm_level (index, type, levels)
1862 tree index;
1863 tree type;
1864 int levels;
1866 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1867 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1868 != TEMPLATE_PARM_LEVEL (index) - levels))
1870 tree decl
1871 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1872 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1873 type);
1874 tree t
1875 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1876 TEMPLATE_PARM_LEVEL (index) - levels,
1877 TEMPLATE_PARM_ORIG_LEVEL (index),
1878 decl, type);
1879 TEMPLATE_PARM_DESCENDANTS (index) = t;
1881 DECL_ARTIFICIAL (decl) = 1;
1882 SET_DECL_TEMPLATE_PARM_P (decl);
1884 /* Template template parameters need this. */
1885 DECL_TEMPLATE_PARMS (decl)
1886 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1889 return TEMPLATE_PARM_DESCENDANTS (index);
1892 /* Process information from new template parameter NEXT and append it to the
1893 LIST being built. */
1895 tree
1896 process_template_parm (list, next)
1897 tree list, next;
1899 tree parm;
1900 tree decl = 0;
1901 tree defval;
1902 int is_type, idx;
1904 parm = next;
1905 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1906 defval = TREE_PURPOSE (parm);
1907 parm = TREE_VALUE (parm);
1908 is_type = TREE_PURPOSE (parm) == class_type_node;
1910 if (list)
1912 tree p = TREE_VALUE (tree_last (list));
1914 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
1915 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1916 else
1917 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1918 ++idx;
1920 else
1921 idx = 0;
1923 if (!is_type)
1925 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1926 /* is a const-param */
1927 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1928 PARM, 0, NULL_TREE);
1930 /* [temp.param]
1932 The top-level cv-qualifiers on the template-parameter are
1933 ignored when determining its type. */
1934 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1936 /* A template parameter is not modifiable. */
1937 TREE_READONLY (parm) = 1;
1938 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
1939 TREE_TYPE (parm) = void_type_node;
1940 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1941 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1942 = build_template_parm_index (idx, processing_template_decl,
1943 processing_template_decl,
1944 decl, TREE_TYPE (parm));
1946 else
1948 tree t;
1949 parm = TREE_VALUE (parm);
1951 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1953 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1954 /* This is for distinguishing between real templates and template
1955 template parameters */
1956 TREE_TYPE (parm) = t;
1957 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1958 decl = parm;
1960 else
1962 t = make_aggr_type (TEMPLATE_TYPE_PARM);
1963 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1964 decl = build_decl (TYPE_DECL, parm, t);
1967 TYPE_NAME (t) = decl;
1968 TYPE_STUB_DECL (t) = decl;
1969 parm = decl;
1970 TEMPLATE_TYPE_PARM_INDEX (t)
1971 = build_template_parm_index (idx, processing_template_decl,
1972 processing_template_decl,
1973 decl, TREE_TYPE (parm));
1975 DECL_ARTIFICIAL (decl) = 1;
1976 SET_DECL_TEMPLATE_PARM_P (decl);
1977 pushdecl (decl);
1978 parm = build_tree_list (defval, parm);
1979 return chainon (list, parm);
1982 /* The end of a template parameter list has been reached. Process the
1983 tree list into a parameter vector, converting each parameter into a more
1984 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1985 as PARM_DECLs. */
1987 tree
1988 end_template_parm_list (parms)
1989 tree parms;
1991 int nparms;
1992 tree parm;
1993 tree saved_parmlist = make_tree_vec (list_length (parms));
1995 current_template_parms
1996 = tree_cons (size_int (processing_template_decl),
1997 saved_parmlist, current_template_parms);
1999 for (parm = parms, nparms = 0;
2000 parm;
2001 parm = TREE_CHAIN (parm), nparms++)
2002 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2004 --processing_template_parmlist;
2006 return saved_parmlist;
2009 /* end_template_decl is called after a template declaration is seen. */
2011 void
2012 end_template_decl ()
2014 reset_specialization ();
2016 if (! processing_template_decl)
2017 return;
2019 /* This matches the pushlevel in begin_template_parm_list. */
2020 finish_scope ();
2022 --processing_template_decl;
2023 current_template_parms = TREE_CHAIN (current_template_parms);
2026 /* Given a template argument vector containing the template PARMS.
2027 The innermost PARMS are given first. */
2029 tree
2030 current_template_args ()
2032 tree header;
2033 tree args = NULL_TREE;
2034 int length = TMPL_PARMS_DEPTH (current_template_parms);
2035 int l = length;
2037 /* If there is only one level of template parameters, we do not
2038 create a TREE_VEC of TREE_VECs. Instead, we return a single
2039 TREE_VEC containing the arguments. */
2040 if (length > 1)
2041 args = make_tree_vec (length);
2043 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2045 tree a = copy_node (TREE_VALUE (header));
2046 int i;
2048 TREE_TYPE (a) = NULL_TREE;
2049 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2051 tree t = TREE_VEC_ELT (a, i);
2053 /* T will be a list if we are called from within a
2054 begin/end_template_parm_list pair, but a vector directly
2055 if within a begin/end_member_template_processing pair. */
2056 if (TREE_CODE (t) == TREE_LIST)
2058 t = TREE_VALUE (t);
2060 if (TREE_CODE (t) == TYPE_DECL
2061 || TREE_CODE (t) == TEMPLATE_DECL)
2062 t = TREE_TYPE (t);
2063 else
2064 t = DECL_INITIAL (t);
2065 TREE_VEC_ELT (a, i) = t;
2069 if (length > 1)
2070 TREE_VEC_ELT (args, --l) = a;
2071 else
2072 args = a;
2075 return args;
2078 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2079 template PARMS. Used by push_template_decl below. */
2081 static tree
2082 build_template_decl (decl, parms)
2083 tree decl;
2084 tree parms;
2086 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2087 DECL_TEMPLATE_PARMS (tmpl) = parms;
2088 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2089 if (DECL_LANG_SPECIFIC (decl))
2091 if (CAN_HAVE_FULL_LANG_DECL_P (decl))
2092 DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl);
2093 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2094 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2095 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2096 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2097 if (DECL_OVERLOADED_OPERATOR_P (decl))
2098 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2099 DECL_OVERLOADED_OPERATOR_P (decl));
2102 return tmpl;
2105 struct template_parm_data
2107 /* The level of the template parameters we are currently
2108 processing. */
2109 int level;
2111 /* The index of the specialization argument we are currently
2112 processing. */
2113 int current_arg;
2115 /* An array whose size is the number of template parameters. The
2116 elements are non-zero if the parameter has been used in any one
2117 of the arguments processed so far. */
2118 int* parms;
2120 /* An array whose size is the number of template arguments. The
2121 elements are non-zero if the argument makes use of template
2122 parameters of this level. */
2123 int* arg_uses_template_parms;
2126 /* Subroutine of push_template_decl used to see if each template
2127 parameter in a partial specialization is used in the explicit
2128 argument list. If T is of the LEVEL given in DATA (which is
2129 treated as a template_parm_data*), then DATA->PARMS is marked
2130 appropriately. */
2132 static int
2133 mark_template_parm (t, data)
2134 tree t;
2135 void* data;
2137 int level;
2138 int idx;
2139 struct template_parm_data* tpd = (struct template_parm_data*) data;
2141 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2143 level = TEMPLATE_PARM_LEVEL (t);
2144 idx = TEMPLATE_PARM_IDX (t);
2146 else
2148 level = TEMPLATE_TYPE_LEVEL (t);
2149 idx = TEMPLATE_TYPE_IDX (t);
2152 if (level == tpd->level)
2154 tpd->parms[idx] = 1;
2155 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2158 /* Return zero so that for_each_template_parm will continue the
2159 traversal of the tree; we want to mark *every* template parm. */
2160 return 0;
2163 /* Process the partial specialization DECL. */
2165 static tree
2166 process_partial_specialization (decl)
2167 tree decl;
2169 tree type = TREE_TYPE (decl);
2170 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2171 tree specargs = CLASSTYPE_TI_ARGS (type);
2172 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2173 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2174 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2175 int nargs = TREE_VEC_LENGTH (inner_args);
2176 int ntparms = TREE_VEC_LENGTH (inner_parms);
2177 int i;
2178 int did_error_intro = 0;
2179 struct template_parm_data tpd;
2180 struct template_parm_data tpd2;
2182 /* We check that each of the template parameters given in the
2183 partial specialization is used in the argument list to the
2184 specialization. For example:
2186 template <class T> struct S;
2187 template <class T> struct S<T*>;
2189 The second declaration is OK because `T*' uses the template
2190 parameter T, whereas
2192 template <class T> struct S<int>;
2194 is no good. Even trickier is:
2196 template <class T>
2197 struct S1
2199 template <class U>
2200 struct S2;
2201 template <class U>
2202 struct S2<T>;
2205 The S2<T> declaration is actually illegal; it is a
2206 full-specialization. Of course,
2208 template <class U>
2209 struct S2<T (*)(U)>;
2211 or some such would have been OK. */
2212 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2213 tpd.parms = alloca (sizeof (int) * ntparms);
2214 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2216 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2217 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2218 for (i = 0; i < nargs; ++i)
2220 tpd.current_arg = i;
2221 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2222 &mark_template_parm,
2223 &tpd);
2225 for (i = 0; i < ntparms; ++i)
2226 if (tpd.parms[i] == 0)
2228 /* One of the template parms was not used in the
2229 specialization. */
2230 if (!did_error_intro)
2232 cp_error ("template parameters not used in partial specialization:");
2233 did_error_intro = 1;
2236 cp_error (" `%D'",
2237 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2240 /* [temp.class.spec]
2242 The argument list of the specialization shall not be identical to
2243 the implicit argument list of the primary template. */
2244 if (comp_template_args
2245 (inner_args,
2246 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2247 (maintmpl)))))
2248 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
2250 /* [temp.class.spec]
2252 A partially specialized non-type argument expression shall not
2253 involve template parameters of the partial specialization except
2254 when the argument expression is a simple identifier.
2256 The type of a template parameter corresponding to a specialized
2257 non-type argument shall not be dependent on a parameter of the
2258 specialization. */
2259 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2260 tpd2.parms = 0;
2261 for (i = 0; i < nargs; ++i)
2263 tree arg = TREE_VEC_ELT (inner_args, i);
2264 if (/* These first two lines are the `non-type' bit. */
2265 !TYPE_P (arg)
2266 && TREE_CODE (arg) != TEMPLATE_DECL
2267 /* This next line is the `argument expression is not just a
2268 simple identifier' condition and also the `specialized
2269 non-type argument' bit. */
2270 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2272 if (tpd.arg_uses_template_parms[i])
2273 cp_error ("template argument `%E' involves template parameter(s)", arg);
2274 else
2276 /* Look at the corresponding template parameter,
2277 marking which template parameters its type depends
2278 upon. */
2279 tree type =
2280 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2281 i)));
2283 if (!tpd2.parms)
2285 /* We haven't yet initialized TPD2. Do so now. */
2286 tpd2.arg_uses_template_parms
2287 = (int*) alloca (sizeof (int) * nargs);
2288 /* The number of parameters here is the number in the
2289 main template, which, as checked in the assertion
2290 above, is NARGS. */
2291 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2292 tpd2.level =
2293 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2296 /* Mark the template parameters. But this time, we're
2297 looking for the template parameters of the main
2298 template, not in the specialization. */
2299 tpd2.current_arg = i;
2300 tpd2.arg_uses_template_parms[i] = 0;
2301 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2302 for_each_template_parm (type,
2303 &mark_template_parm,
2304 &tpd2);
2306 if (tpd2.arg_uses_template_parms [i])
2308 /* The type depended on some template parameters.
2309 If they are fully specialized in the
2310 specialization, that's OK. */
2311 int j;
2312 for (j = 0; j < nargs; ++j)
2313 if (tpd2.parms[j] != 0
2314 && tpd.arg_uses_template_parms [j])
2316 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2317 type,
2318 arg);
2319 break;
2326 if (retrieve_specialization (maintmpl, specargs))
2327 /* We've already got this specialization. */
2328 return decl;
2330 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2331 = tree_cons (inner_args, inner_parms,
2332 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2333 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2334 return decl;
2337 /* Check that a template declaration's use of default arguments is not
2338 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2339 non-zero if DECL is the thing declared by a primary template.
2340 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2342 static void
2343 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2344 tree decl;
2345 tree parms;
2346 int is_primary;
2347 int is_partial;
2349 const char *msg;
2350 int last_level_to_check;
2351 tree parm_level;
2353 /* [temp.param]
2355 A default template-argument shall not be specified in a
2356 function template declaration or a function template definition, nor
2357 in the template-parameter-list of the definition of a member of a
2358 class template. */
2360 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2361 /* You can't have a function template declaration in a local
2362 scope, nor you can you define a member of a class template in a
2363 local scope. */
2364 return;
2366 if (current_class_type
2367 && !TYPE_BEING_DEFINED (current_class_type)
2368 && DECL_LANG_SPECIFIC (decl)
2369 /* If this is either a friend defined in the scope of the class
2370 or a member function. */
2371 && ((DECL_CONTEXT (decl)
2372 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2373 || (DECL_FRIEND_CONTEXT (decl)
2374 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2375 current_class_type)))
2376 /* And, if it was a member function, it really was defined in
2377 the scope of the class. */
2378 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2379 /* We already checked these parameters when the template was
2380 declared, so there's no need to do it again now. This function
2381 was defined in class scope, but we're processing it's body now
2382 that the class is complete. */
2383 return;
2385 /* [temp.param]
2387 If a template-parameter has a default template-argument, all
2388 subsequent template-parameters shall have a default
2389 template-argument supplied. */
2390 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2392 tree inner_parms = TREE_VALUE (parm_level);
2393 int ntparms = TREE_VEC_LENGTH (inner_parms);
2394 int seen_def_arg_p = 0;
2395 int i;
2397 for (i = 0; i < ntparms; ++i)
2399 tree parm = TREE_VEC_ELT (inner_parms, i);
2400 if (TREE_PURPOSE (parm))
2401 seen_def_arg_p = 1;
2402 else if (seen_def_arg_p)
2404 cp_error ("no default argument for `%D'", TREE_VALUE (parm));
2405 /* For better subsequent error-recovery, we indicate that
2406 there should have been a default argument. */
2407 TREE_PURPOSE (parm) = error_mark_node;
2412 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2413 /* For an ordinary class template, default template arguments are
2414 allowed at the innermost level, e.g.:
2415 template <class T = int>
2416 struct S {};
2417 but, in a partial specialization, they're not allowed even
2418 there, as we have in [temp.class.spec]:
2420 The template parameter list of a specialization shall not
2421 contain default template argument values.
2423 So, for a partial specialization, or for a function template,
2424 we look at all of them. */
2426 else
2427 /* But, for a primary class template that is not a partial
2428 specialization we look at all template parameters except the
2429 innermost ones. */
2430 parms = TREE_CHAIN (parms);
2432 /* Figure out what error message to issue. */
2433 if (TREE_CODE (decl) == FUNCTION_DECL)
2434 msg = "default argument for template parameter in function template `%D'";
2435 else if (is_partial)
2436 msg = "default argument in partial specialization `%D'";
2437 else
2438 msg = "default argument for template parameter for class enclosing `%D'";
2440 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2441 /* If we're inside a class definition, there's no need to
2442 examine the parameters to the class itself. On the one
2443 hand, they will be checked when the class is defined, and,
2444 on the other, default arguments are legal in things like:
2445 template <class T = double>
2446 struct S { template <class U> void f(U); };
2447 Here the default argument for `S' has no bearing on the
2448 declaration of `f'. */
2449 last_level_to_check = template_class_depth (current_class_type) + 1;
2450 else
2451 /* Check everything. */
2452 last_level_to_check = 0;
2454 for (parm_level = parms;
2455 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2456 parm_level = TREE_CHAIN (parm_level))
2458 tree inner_parms = TREE_VALUE (parm_level);
2459 int i;
2460 int ntparms;
2462 ntparms = TREE_VEC_LENGTH (inner_parms);
2463 for (i = 0; i < ntparms; ++i)
2464 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2466 if (msg)
2468 cp_error (msg, decl);
2469 msg = 0;
2472 /* Clear out the default argument so that we are not
2473 confused later. */
2474 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2477 /* At this point, if we're still interested in issuing messages,
2478 they must apply to classes surrounding the object declared. */
2479 if (msg)
2480 msg = "default argument for template parameter for class enclosing `%D'";
2484 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2485 parameters given by current_template_args, or reuses a
2486 previously existing one, if appropriate. Returns the DECL, or an
2487 equivalent one, if it is replaced via a call to duplicate_decls.
2489 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2491 tree
2492 push_template_decl_real (decl, is_friend)
2493 tree decl;
2494 int is_friend;
2496 tree tmpl;
2497 tree args;
2498 tree info;
2499 tree ctx;
2500 int primary;
2501 int is_partial;
2502 int new_template_p = 0;
2504 /* See if this is a partial specialization. */
2505 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2506 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2507 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2509 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2511 if (is_friend)
2512 /* For a friend, we want the context of the friend function, not
2513 the type of which it is a friend. */
2514 ctx = DECL_CONTEXT (decl);
2515 else if (CP_DECL_CONTEXT (decl)
2516 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2517 /* In the case of a virtual function, we want the class in which
2518 it is defined. */
2519 ctx = CP_DECL_CONTEXT (decl);
2520 else
2521 /* Otherwise, if we're currently defining some class, the DECL
2522 is assumed to be a member of the class. */
2523 ctx = current_scope ();
2525 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2526 ctx = NULL_TREE;
2528 if (!DECL_CONTEXT (decl))
2529 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2531 /* See if this is a primary template. */
2532 primary = template_parm_scope_p ();
2534 if (primary)
2536 if (current_lang_name == lang_name_c)
2537 cp_error ("template with C linkage");
2538 else if (TREE_CODE (decl) == TYPE_DECL
2539 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2540 cp_error ("template class without a name");
2541 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2542 && CLASS_TYPE_P (TREE_TYPE (decl)))
2543 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2544 || TREE_CODE (decl) == FUNCTION_DECL)
2545 /* OK */;
2546 else
2547 cp_error ("template declaration of `%#D'", decl);
2550 /* Check to see that the rules regarding the use of default
2551 arguments are not being violated. */
2552 check_default_tmpl_args (decl, current_template_parms,
2553 primary, is_partial);
2555 if (is_partial)
2556 return process_partial_specialization (decl);
2558 args = current_template_args ();
2560 if (!ctx
2561 || TREE_CODE (ctx) == FUNCTION_DECL
2562 || TYPE_BEING_DEFINED (ctx)
2563 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2565 if (DECL_LANG_SPECIFIC (decl)
2566 && DECL_TEMPLATE_INFO (decl)
2567 && DECL_TI_TEMPLATE (decl))
2568 tmpl = DECL_TI_TEMPLATE (decl);
2569 /* If DECL is a TYPE_DECL for a class-template, then there won't
2570 be DECL_LANG_SPECIFIC. The information equivalent to
2571 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2572 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2573 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2574 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2576 /* Since a template declaration already existed for this
2577 class-type, we must be redeclaring it here. Make sure
2578 that the redeclaration is legal. */
2579 redeclare_class_template (TREE_TYPE (decl),
2580 current_template_parms);
2581 /* We don't need to create a new TEMPLATE_DECL; just use the
2582 one we already had. */
2583 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2585 else
2587 tmpl = build_template_decl (decl, current_template_parms);
2588 new_template_p = 1;
2590 if (DECL_LANG_SPECIFIC (decl)
2591 && DECL_TEMPLATE_SPECIALIZATION (decl))
2593 /* A specialization of a member template of a template
2594 class. */
2595 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2596 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2597 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2601 else
2603 tree a, t, current, parms;
2604 int i;
2606 if (TREE_CODE (decl) == TYPE_DECL)
2608 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2609 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2610 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2611 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2612 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2613 else
2615 cp_error ("`%D' does not declare a template type", decl);
2616 return decl;
2619 else if (! DECL_TEMPLATE_INFO (decl))
2621 cp_error ("template definition of non-template `%#D'", decl);
2622 return decl;
2624 else
2625 tmpl = DECL_TI_TEMPLATE (decl);
2627 if (is_member_template (tmpl)
2628 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2629 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2630 && DECL_TEMPLATE_SPECIALIZATION (decl))
2632 tree new_tmpl;
2634 /* The declaration is a specialization of a member
2635 template, declared outside the class. Therefore, the
2636 innermost template arguments will be NULL, so we
2637 replace them with the arguments determined by the
2638 earlier call to check_explicit_specialization. */
2639 args = DECL_TI_ARGS (decl);
2641 new_tmpl
2642 = build_template_decl (decl, current_template_parms);
2643 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2644 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2645 DECL_TI_TEMPLATE (decl) = new_tmpl;
2646 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2647 DECL_TEMPLATE_INFO (new_tmpl)
2648 = tree_cons (tmpl, args, NULL_TREE);
2650 register_specialization (new_tmpl,
2651 most_general_template (tmpl),
2652 args);
2653 return decl;
2656 /* Make sure the template headers we got make sense. */
2658 parms = DECL_TEMPLATE_PARMS (tmpl);
2659 i = TMPL_PARMS_DEPTH (parms);
2660 if (TMPL_ARGS_DEPTH (args) != i)
2662 cp_error ("expected %d levels of template parms for `%#D', got %d",
2663 i, decl, TMPL_ARGS_DEPTH (args));
2665 else
2666 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2668 a = TMPL_ARGS_LEVEL (args, i);
2669 t = INNERMOST_TEMPLATE_PARMS (parms);
2671 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2673 if (current == decl)
2674 cp_error ("got %d template parameters for `%#D'",
2675 TREE_VEC_LENGTH (a), decl);
2676 else
2677 cp_error ("got %d template parameters for `%#T'",
2678 TREE_VEC_LENGTH (a), current);
2679 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2682 /* Perhaps we should also check that the parms are used in the
2683 appropriate qualifying scopes in the declarator? */
2685 if (current == decl)
2686 current = ctx;
2687 else
2688 current = TYPE_CONTEXT (current);
2692 DECL_TEMPLATE_RESULT (tmpl) = decl;
2693 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2695 /* Push template declarations for global functions and types. Note
2696 that we do not try to push a global template friend declared in a
2697 template class; such a thing may well depend on the template
2698 parameters of the class. */
2699 if (new_template_p && !ctx
2700 && !(is_friend && template_class_depth (current_class_type) > 0))
2701 tmpl = pushdecl_namespace_level (tmpl);
2703 if (primary)
2704 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2706 info = tree_cons (tmpl, args, NULL_TREE);
2708 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2710 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2711 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2712 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2713 /* Don't change the name if we've already set it up. */
2714 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2715 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2717 else if (DECL_LANG_SPECIFIC (decl))
2718 DECL_TEMPLATE_INFO (decl) = info;
2720 return DECL_TEMPLATE_RESULT (tmpl);
2723 tree
2724 push_template_decl (decl)
2725 tree decl;
2727 return push_template_decl_real (decl, 0);
2730 /* Called when a class template TYPE is redeclared with the indicated
2731 template PARMS, e.g.:
2733 template <class T> struct S;
2734 template <class T> struct S {}; */
2736 void
2737 redeclare_class_template (type, parms)
2738 tree type;
2739 tree parms;
2741 tree tmpl;
2742 tree tmpl_parms;
2743 int i;
2745 if (!TYPE_TEMPLATE_INFO (type))
2747 cp_error ("`%T' is not a template type", type);
2748 return;
2751 tmpl = TYPE_TI_TEMPLATE (type);
2752 if (!PRIMARY_TEMPLATE_P (tmpl))
2753 /* The type is nested in some template class. Nothing to worry
2754 about here; there are no new template parameters for the nested
2755 type. */
2756 return;
2758 parms = INNERMOST_TEMPLATE_PARMS (parms);
2759 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2761 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2763 cp_error_at ("previous declaration `%D'", tmpl);
2764 cp_error ("used %d template parameter%s instead of %d",
2765 TREE_VEC_LENGTH (tmpl_parms),
2766 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2767 TREE_VEC_LENGTH (parms));
2768 return;
2771 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2773 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2774 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2775 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2776 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2778 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2780 cp_error_at ("template parameter `%#D'", tmpl_parm);
2781 cp_error ("redeclared here as `%#D'", parm);
2782 return;
2785 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2787 /* We have in [temp.param]:
2789 A template-parameter may not be given default arguments
2790 by two different declarations in the same scope. */
2791 cp_error ("redefinition of default argument for `%#D'", parm);
2792 cp_error_at (" original definition appeared here", tmpl_parm);
2793 return;
2796 if (parm_default != NULL_TREE)
2797 /* Update the previous template parameters (which are the ones
2798 that will really count) with the new default value. */
2799 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2800 else if (tmpl_default != NULL_TREE)
2801 /* Update the new parameters, too; they'll be used as the
2802 parameters for any members. */
2803 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2807 /* Attempt to convert the non-type template parameter EXPR to the
2808 indicated TYPE. If the conversion is successful, return the
2809 converted value. If the conversion is unsuccessful, return
2810 NULL_TREE if we issued an error message, or error_mark_node if we
2811 did not. We issue error messages for out-and-out bad template
2812 parameters, but not simply because the conversion failed, since we
2813 might be just trying to do argument deduction. By the time this
2814 function is called, neither TYPE nor EXPR may make use of template
2815 parameters. */
2817 static tree
2818 convert_nontype_argument (type, expr)
2819 tree type;
2820 tree expr;
2822 tree expr_type = TREE_TYPE (expr);
2824 /* A template-argument for a non-type, non-template
2825 template-parameter shall be one of:
2827 --an integral constant-expression of integral or enumeration
2828 type; or
2830 --the name of a non-type template-parameter; or
2832 --the name of an object or function with external linkage,
2833 including function templates and function template-ids but
2834 excluding non-static class members, expressed as id-expression;
2837 --the address of an object or function with external linkage,
2838 including function templates and function template-ids but
2839 excluding non-static class members, expressed as & id-expression
2840 where the & is optional if the name refers to a function or
2841 array; or
2843 --a pointer to member expressed as described in _expr.unary.op_. */
2845 /* An integral constant-expression can include const variables or
2846 enumerators. Simplify things by folding them to their values,
2847 unless we're about to bind the declaration to a reference
2848 parameter. */
2849 if (INTEGRAL_TYPE_P (expr_type)
2850 && TREE_CODE (type) != REFERENCE_TYPE)
2851 expr = decl_constant_value (expr);
2853 if (is_overloaded_fn (expr))
2854 /* OK for now. We'll check that it has external linkage later.
2855 Check this first since if expr_type is the unknown_type_node
2856 we would otherwise complain below. */
2858 else if (TYPE_PTRMEM_P (expr_type)
2859 || TYPE_PTRMEMFUNC_P (expr_type))
2861 if (TREE_CODE (expr) != PTRMEM_CST)
2862 goto bad_argument;
2864 else if (TYPE_PTR_P (expr_type)
2865 || TYPE_PTRMEM_P (expr_type)
2866 || TREE_CODE (expr_type) == ARRAY_TYPE
2867 || TREE_CODE (type) == REFERENCE_TYPE
2868 /* If expr is the address of an overloaded function, we
2869 will get the unknown_type_node at this point. */
2870 || expr_type == unknown_type_node)
2872 tree referent;
2873 tree e = expr;
2874 STRIP_NOPS (e);
2876 if (TREE_CODE (expr_type) == ARRAY_TYPE
2877 || (TREE_CODE (type) == REFERENCE_TYPE
2878 && TREE_CODE (e) != ADDR_EXPR))
2879 referent = e;
2880 else
2882 if (TREE_CODE (e) != ADDR_EXPR)
2884 bad_argument:
2885 cp_error ("`%E' is not a valid template argument", expr);
2886 if (TYPE_PTR_P (expr_type))
2888 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2889 cp_error ("it must be the address of a function with external linkage");
2890 else
2891 cp_error ("it must be the address of an object with external linkage");
2893 else if (TYPE_PTRMEM_P (expr_type)
2894 || TYPE_PTRMEMFUNC_P (expr_type))
2895 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2897 return NULL_TREE;
2900 referent = TREE_OPERAND (e, 0);
2901 STRIP_NOPS (referent);
2904 if (TREE_CODE (referent) == STRING_CST)
2906 cp_error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2907 referent);
2908 return NULL_TREE;
2911 if (is_overloaded_fn (referent))
2912 /* We'll check that it has external linkage later. */
2914 else if (TREE_CODE (referent) != VAR_DECL)
2915 goto bad_argument;
2916 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
2918 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
2919 return error_mark_node;
2922 else if (INTEGRAL_TYPE_P (expr_type)
2923 || TYPE_PTRMEM_P (expr_type)
2924 || TYPE_PTRMEMFUNC_P (expr_type)
2925 /* The next two are g++ extensions. */
2926 || TREE_CODE (expr_type) == REAL_TYPE
2927 || TREE_CODE (expr_type) == COMPLEX_TYPE)
2929 if (! TREE_CONSTANT (expr))
2931 non_constant:
2932 cp_error ("non-constant `%E' cannot be used as template argument",
2933 expr);
2934 return NULL_TREE;
2937 else
2939 cp_error ("object `%E' cannot be used as template argument", expr);
2940 return NULL_TREE;
2943 switch (TREE_CODE (type))
2945 case INTEGER_TYPE:
2946 case BOOLEAN_TYPE:
2947 case ENUMERAL_TYPE:
2948 /* For a non-type template-parameter of integral or enumeration
2949 type, integral promotions (_conv.prom_) and integral
2950 conversions (_conv.integral_) are applied. */
2951 if (!INTEGRAL_TYPE_P (expr_type))
2952 return error_mark_node;
2954 /* It's safe to call digest_init in this case; we know we're
2955 just converting one integral constant expression to another. */
2956 expr = digest_init (type, expr, (tree*) 0);
2958 if (TREE_CODE (expr) != INTEGER_CST)
2959 /* Curiously, some TREE_CONSTANT integral expressions do not
2960 simplify to integer constants. For example, `3 % 0',
2961 remains a TRUNC_MOD_EXPR. */
2962 goto non_constant;
2964 return expr;
2966 case REAL_TYPE:
2967 case COMPLEX_TYPE:
2968 /* These are g++ extensions. */
2969 if (TREE_CODE (expr_type) != TREE_CODE (type))
2970 return error_mark_node;
2972 expr = digest_init (type, expr, (tree*) 0);
2974 if (TREE_CODE (expr) != REAL_CST)
2975 goto non_constant;
2977 return expr;
2979 case POINTER_TYPE:
2981 tree type_pointed_to = TREE_TYPE (type);
2983 if (TYPE_PTRMEM_P (type))
2985 tree e;
2987 /* For a non-type template-parameter of type pointer to data
2988 member, qualification conversions (_conv.qual_) are
2989 applied. */
2990 e = perform_qualification_conversions (type, expr);
2991 if (TREE_CODE (e) == NOP_EXPR)
2992 /* The call to perform_qualification_conversions will
2993 insert a NOP_EXPR over EXPR to do express conversion,
2994 if necessary. But, that will confuse us if we use
2995 this (converted) template parameter to instantiate
2996 another template; then the thing will not look like a
2997 valid template argument. So, just make a new
2998 constant, of the appropriate type. */
2999 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3000 return e;
3002 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3004 /* For a non-type template-parameter of type pointer to
3005 function, only the function-to-pointer conversion
3006 (_conv.func_) is applied. If the template-argument
3007 represents a set of overloaded functions (or a pointer to
3008 such), the matching function is selected from the set
3009 (_over.over_). */
3010 tree fns;
3011 tree fn;
3013 if (TREE_CODE (expr) == ADDR_EXPR)
3014 fns = TREE_OPERAND (expr, 0);
3015 else
3016 fns = expr;
3018 fn = instantiate_type (type_pointed_to, fns, itf_none);
3020 if (fn == error_mark_node)
3021 return error_mark_node;
3023 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3025 if (really_overloaded_fn (fns))
3026 return error_mark_node;
3027 else
3028 goto bad_argument;
3031 expr = build_unary_op (ADDR_EXPR, fn, 0);
3033 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3035 return expr;
3037 else
3039 /* For a non-type template-parameter of type pointer to
3040 object, qualification conversions (_conv.qual_) and the
3041 array-to-pointer conversion (_conv.array_) are applied.
3042 [Note: In particular, neither the null pointer conversion
3043 (_conv.ptr_) nor the derived-to-base conversion
3044 (_conv.ptr_) are applied. Although 0 is a valid
3045 template-argument for a non-type template-parameter of
3046 integral type, it is not a valid template-argument for a
3047 non-type template-parameter of pointer type.]
3049 The call to decay_conversion performs the
3050 array-to-pointer conversion, if appropriate. */
3051 expr = decay_conversion (expr);
3053 if (expr == error_mark_node)
3054 return error_mark_node;
3055 else
3056 return perform_qualification_conversions (type, expr);
3059 break;
3061 case REFERENCE_TYPE:
3063 tree type_referred_to = TREE_TYPE (type);
3065 /* If this expression already has reference type, get the
3066 underling object. */
3067 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3069 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3070 expr = TREE_OPERAND (expr, 0);
3071 expr_type = TREE_TYPE (expr);
3074 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3076 /* For a non-type template-parameter of type reference to
3077 function, no conversions apply. If the
3078 template-argument represents a set of overloaded
3079 functions, the matching function is selected from the
3080 set (_over.over_). */
3081 tree fn;
3083 fn = instantiate_type (type_referred_to, expr, itf_none);
3085 if (fn == error_mark_node)
3086 return error_mark_node;
3088 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3090 if (really_overloaded_fn (expr))
3091 /* Don't issue an error here; we might get a different
3092 function if the overloading had worked out
3093 differently. */
3094 return error_mark_node;
3095 else
3096 goto bad_argument;
3099 my_friendly_assert (same_type_p (type_referred_to,
3100 TREE_TYPE (fn)),
3103 expr = fn;
3105 else
3107 /* For a non-type template-parameter of type reference to
3108 object, no conversions apply. The type referred to by the
3109 reference may be more cv-qualified than the (otherwise
3110 identical) type of the template-argument. The
3111 template-parameter is bound directly to the
3112 template-argument, which must be an lvalue. */
3113 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3114 TYPE_MAIN_VARIANT (type_referred_to))
3115 || !at_least_as_qualified_p (type_referred_to,
3116 expr_type)
3117 || !real_lvalue_p (expr))
3118 return error_mark_node;
3121 mark_addressable (expr);
3122 return build1 (ADDR_EXPR, type, expr);
3124 break;
3126 case RECORD_TYPE:
3128 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3130 /* For a non-type template-parameter of type pointer to member
3131 function, no conversions apply. If the template-argument
3132 represents a set of overloaded member functions, the
3133 matching member function is selected from the set
3134 (_over.over_). */
3136 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3137 expr_type != unknown_type_node)
3138 return error_mark_node;
3140 if (TREE_CODE (expr) == PTRMEM_CST)
3142 /* A ptr-to-member constant. */
3143 if (!same_type_p (type, expr_type))
3144 return error_mark_node;
3145 else
3146 return expr;
3149 if (TREE_CODE (expr) != ADDR_EXPR)
3150 return error_mark_node;
3152 expr = instantiate_type (type, expr, itf_none);
3154 if (expr == error_mark_node)
3155 return error_mark_node;
3157 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3159 return expr;
3161 break;
3163 default:
3164 /* All non-type parameters must have one of these types. */
3165 my_friendly_abort (0);
3166 break;
3169 return error_mark_node;
3172 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3173 template template parameters. Both PARM_PARMS and ARG_PARMS are
3174 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3175 or PARM_DECL.
3177 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3178 the case, then extra parameters must have default arguments.
3180 Consider the example:
3181 template <class T, class Allocator = allocator> class vector;
3182 template<template <class U> class TT> class C;
3184 C<vector> is a valid instantiation. PARM_PARMS for the above code
3185 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3186 T and Allocator) and OUTER_ARGS contains the argument that is used to
3187 substitute the TT parameter. */
3189 static int
3190 coerce_template_template_parms (parm_parms, arg_parms, complain,
3191 in_decl, outer_args)
3192 tree parm_parms, arg_parms;
3193 int complain;
3194 tree in_decl, outer_args;
3196 int nparms, nargs, i;
3197 tree parm, arg;
3199 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3200 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3202 nparms = TREE_VEC_LENGTH (parm_parms);
3203 nargs = TREE_VEC_LENGTH (arg_parms);
3205 /* The rule here is opposite of coerce_template_parms. */
3206 if (nargs < nparms
3207 || (nargs > nparms
3208 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3209 return 0;
3211 for (i = 0; i < nparms; ++i)
3213 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3214 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3216 if (arg == NULL_TREE || arg == error_mark_node
3217 || parm == NULL_TREE || parm == error_mark_node)
3218 return 0;
3220 if (TREE_CODE (arg) != TREE_CODE (parm))
3221 return 0;
3223 switch (TREE_CODE (parm))
3225 case TYPE_DECL:
3226 break;
3228 case TEMPLATE_DECL:
3229 /* We encounter instantiations of templates like
3230 template <template <template <class> class> class TT>
3231 class C; */
3233 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3234 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3236 if (!coerce_template_template_parms (parmparm, argparm,
3237 complain, in_decl,
3238 outer_args))
3239 return 0;
3241 break;
3243 case PARM_DECL:
3244 /* The tsubst call is used to handle cases such as
3245 template <class T, template <T> class TT> class D;
3246 i.e. the parameter list of TT depends on earlier parameters. */
3247 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3248 complain, in_decl),
3249 TREE_TYPE (arg)))
3250 return 0;
3251 break;
3253 default:
3254 my_friendly_abort (0);
3257 return 1;
3260 /* Convert the indicated template ARG as necessary to match the
3261 indicated template PARM. Returns the converted ARG, or
3262 error_mark_node if the conversion was unsuccessful. Error messages
3263 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3264 parameter in the parameter list. ARGS is the full set of template
3265 arguments deduced so far. */
3267 static tree
3268 convert_template_argument (parm, arg, args, complain, i, in_decl)
3269 tree parm;
3270 tree arg;
3271 tree args;
3272 int complain;
3273 int i;
3274 tree in_decl;
3276 tree val;
3277 tree inner_args;
3278 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3280 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3282 if (TREE_CODE (arg) == TREE_LIST
3283 && TREE_TYPE (arg) != NULL_TREE
3284 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3286 /* The template argument was the name of some
3287 member function. That's usually
3288 illegal, but static members are OK. In any
3289 case, grab the underlying fields/functions
3290 and issue an error later if required. */
3291 arg = TREE_VALUE (arg);
3292 TREE_TYPE (arg) = unknown_type_node;
3295 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3296 requires_type = (TREE_CODE (parm) == TYPE_DECL
3297 || requires_tmpl_type);
3299 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3300 we also accept implicitly created TYPE_DECL as a valid argument.
3301 This is necessary to handle the case where we pass a template name
3302 to a template template parameter in a scope where we've derived from
3303 in instantiation of that template, so the template name refers to that
3304 instantiation. We really ought to handle this better. */
3305 is_tmpl_type
3306 = ((TREE_CODE (arg) == TEMPLATE_DECL
3307 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3308 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3309 || (TREE_CODE (arg) == RECORD_TYPE
3310 && CLASSTYPE_TEMPLATE_INFO (arg)
3311 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
3312 && DECL_ARTIFICIAL (TYPE_NAME (arg))
3313 && requires_tmpl_type
3314 && is_base_of_enclosing_class (arg, current_class_type)));
3315 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
3316 arg = TYPE_STUB_DECL (arg);
3317 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3318 arg = CLASSTYPE_TI_TEMPLATE (arg);
3320 is_type = TYPE_P (arg) || is_tmpl_type;
3322 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3323 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3325 cp_pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3327 arg = make_typename_type (TREE_OPERAND (arg, 0),
3328 TREE_OPERAND (arg, 1),
3329 complain);
3330 is_type = 1;
3332 if (is_type != requires_type)
3334 if (in_decl)
3336 if (complain)
3338 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3339 i + 1, in_decl);
3340 if (is_type)
3341 cp_error (" expected a constant of type `%T', got `%T'",
3342 TREE_TYPE (parm),
3343 (is_tmpl_type ? DECL_NAME (arg) : arg));
3344 else
3345 cp_error (" expected a type, got `%E'", arg);
3348 return error_mark_node;
3350 if (is_tmpl_type ^ requires_tmpl_type)
3352 if (in_decl && complain)
3354 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3355 i + 1, in_decl);
3356 if (is_tmpl_type)
3357 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
3358 else
3359 cp_error (" expected a class template, got `%T'", arg);
3361 return error_mark_node;
3364 if (is_type)
3366 if (requires_tmpl_type)
3368 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3369 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3371 if (coerce_template_template_parms (parmparm, argparm, complain,
3372 in_decl, inner_args))
3374 val = arg;
3376 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3377 TEMPLATE_DECL. */
3378 if (val != error_mark_node
3379 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3380 val = TREE_TYPE (val);
3382 else
3384 if (in_decl && complain)
3386 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3387 i + 1, in_decl);
3388 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
3391 val = error_mark_node;
3394 else
3396 val = groktypename (arg);
3397 if (! processing_template_decl)
3399 /* [basic.link]: A name with no linkage (notably, the
3400 name of a class or enumeration declared in a local
3401 scope) shall not be used to declare an entity with
3402 linkage. This implies that names with no linkage
3403 cannot be used as template arguments. */
3404 tree t = no_linkage_check (val);
3405 if (t)
3407 if (TYPE_ANONYMOUS_P (t))
3408 cp_pedwarn
3409 ("template-argument `%T' uses anonymous type", val);
3410 else
3411 cp_error
3412 ("template-argument `%T' uses local type `%T'",
3413 val, t);
3414 return error_mark_node;
3419 else
3421 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3423 if (invalid_nontype_parm_type_p (t, complain))
3424 return error_mark_node;
3426 if (processing_template_decl)
3427 arg = maybe_fold_nontype_arg (arg);
3429 if (!uses_template_parms (arg) && !uses_template_parms (t))
3430 /* We used to call digest_init here. However, digest_init
3431 will report errors, which we don't want when complain
3432 is zero. More importantly, digest_init will try too
3433 hard to convert things: for example, `0' should not be
3434 converted to pointer type at this point according to
3435 the standard. Accepting this is not merely an
3436 extension, since deciding whether or not these
3437 conversions can occur is part of determining which
3438 function template to call, or whether a given explicit
3439 argument specification is legal. */
3440 val = convert_nontype_argument (t, arg);
3441 else
3442 val = arg;
3444 if (val == NULL_TREE)
3445 val = error_mark_node;
3446 else if (val == error_mark_node && complain)
3447 cp_error ("could not convert template argument `%E' to `%T'",
3448 arg, t);
3451 return val;
3454 /* Convert all template arguments to their appropriate types, and
3455 return a vector containing the innermost resulting template
3456 arguments. If any error occurs, return error_mark_node, and, if
3457 COMPLAIN is non-zero, issue an error message. Some error messages
3458 are issued even if COMPLAIN is zero; for instance, if a template
3459 argument is composed from a local class.
3461 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3462 provided in ARGLIST, or else trailing parameters must have default
3463 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3464 deduction for any unspecified trailing arguments. */
3466 static tree
3467 coerce_template_parms (parms, args, in_decl,
3468 complain,
3469 require_all_arguments)
3470 tree parms, args;
3471 tree in_decl;
3472 int complain;
3473 int require_all_arguments;
3475 int nparms, nargs, i, lost = 0;
3476 tree inner_args;
3477 tree new_args;
3478 tree new_inner_args;
3480 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3481 nargs = NUM_TMPL_ARGS (inner_args);
3482 nparms = TREE_VEC_LENGTH (parms);
3484 if (nargs > nparms
3485 || (nargs < nparms
3486 && require_all_arguments
3487 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3489 if (complain)
3491 cp_error ("wrong number of template arguments (%d, should be %d)",
3492 nargs, nparms);
3494 if (in_decl)
3495 cp_error_at ("provided for `%D'", in_decl);
3498 return error_mark_node;
3501 new_inner_args = make_tree_vec (nparms);
3502 new_args = add_outermost_template_args (args, new_inner_args);
3503 for (i = 0; i < nparms; i++)
3505 tree arg;
3506 tree parm;
3508 /* Get the Ith template parameter. */
3509 parm = TREE_VEC_ELT (parms, i);
3511 /* Calculate the Ith argument. */
3512 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3514 arg = TREE_VALUE (inner_args);
3515 inner_args = TREE_CHAIN (inner_args);
3517 else if (i < nargs)
3518 arg = TREE_VEC_ELT (inner_args, i);
3519 /* If no template argument was supplied, look for a default
3520 value. */
3521 else if (TREE_PURPOSE (parm) == NULL_TREE)
3523 /* There was no default value. */
3524 my_friendly_assert (!require_all_arguments, 0);
3525 break;
3527 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3528 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3529 else
3530 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3531 in_decl);
3533 /* Now, convert the Ith argument, as necessary. */
3534 if (arg == NULL_TREE)
3535 /* We're out of arguments. */
3537 my_friendly_assert (!require_all_arguments, 0);
3538 break;
3540 else if (arg == error_mark_node)
3542 cp_error ("template argument %d is invalid", i + 1);
3543 arg = error_mark_node;
3545 else
3546 arg = convert_template_argument (TREE_VALUE (parm),
3547 arg, new_args, complain, i,
3548 in_decl);
3550 if (arg == error_mark_node)
3551 lost++;
3552 TREE_VEC_ELT (new_inner_args, i) = arg;
3555 if (lost)
3556 return error_mark_node;
3558 return new_inner_args;
3561 /* Returns 1 if template args OT and NT are equivalent. */
3563 static int
3564 template_args_equal (ot, nt)
3565 tree ot, nt;
3567 if (nt == ot)
3568 return 1;
3570 if (TREE_CODE (nt) == TREE_VEC)
3571 /* For member templates */
3572 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3573 else if (TYPE_P (nt))
3574 return TYPE_P (ot) && same_type_p (ot, nt);
3575 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3576 return 0;
3577 else
3578 return (cp_tree_equal (ot, nt) > 0);
3581 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3582 of template arguments. Returns 0 otherwise. */
3585 comp_template_args (oldargs, newargs)
3586 tree oldargs, newargs;
3588 int i;
3590 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3591 return 0;
3593 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3595 tree nt = TREE_VEC_ELT (newargs, i);
3596 tree ot = TREE_VEC_ELT (oldargs, i);
3598 if (! template_args_equal (ot, nt))
3599 return 0;
3601 return 1;
3604 /* Given class template name and parameter list, produce a user-friendly name
3605 for the instantiation. */
3607 static char *
3608 mangle_class_name_for_template (name, parms, arglist)
3609 const char *name;
3610 tree parms, arglist;
3612 static struct obstack scratch_obstack;
3613 static char *scratch_firstobj;
3614 int i, nparms;
3616 if (!scratch_firstobj)
3617 gcc_obstack_init (&scratch_obstack);
3618 else
3619 obstack_free (&scratch_obstack, scratch_firstobj);
3620 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3622 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3623 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3625 cat (name);
3626 ccat ('<');
3627 nparms = TREE_VEC_LENGTH (parms);
3628 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3629 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3630 for (i = 0; i < nparms; i++)
3632 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3633 tree arg = TREE_VEC_ELT (arglist, i);
3635 if (i)
3636 ccat (',');
3638 if (TREE_CODE (parm) == TYPE_DECL)
3640 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3641 continue;
3643 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3645 if (TREE_CODE (arg) == TEMPLATE_DECL)
3647 /* Already substituted with real template. Just output
3648 the template name here */
3649 tree context = DECL_CONTEXT (arg);
3650 if (context)
3652 /* The template may be defined in a namespace, or
3653 may be a member template. */
3654 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3655 || CLASS_TYPE_P (context),
3656 980422);
3657 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3658 cat("::");
3660 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3662 else
3663 /* Output the parameter declaration */
3664 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3665 continue;
3667 else
3668 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3670 if (TREE_CODE (arg) == TREE_LIST)
3672 /* New list cell was built because old chain link was in
3673 use. */
3674 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3675 arg = TREE_VALUE (arg);
3677 /* No need to check arglist against parmlist here; we did that
3678 in coerce_template_parms, called from lookup_template_class. */
3679 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3682 char *bufp = obstack_next_free (&scratch_obstack);
3683 int offset = 0;
3684 while (bufp[offset - 1] == ' ')
3685 offset--;
3686 obstack_blank_fast (&scratch_obstack, offset);
3688 /* B<C<char> >, not B<C<char>> */
3689 if (bufp[offset - 1] == '>')
3690 ccat (' ');
3692 ccat ('>');
3693 ccat ('\0');
3694 return (char *) obstack_base (&scratch_obstack);
3697 static tree
3698 classtype_mangled_name (t)
3699 tree t;
3701 if (CLASSTYPE_TEMPLATE_INFO (t)
3702 /* Specializations have already had their names set up in
3703 lookup_template_class. */
3704 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3706 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3708 /* For non-primary templates, the template parameters are
3709 implicit from their surrounding context. */
3710 if (PRIMARY_TEMPLATE_P (tmpl))
3712 tree name = DECL_NAME (tmpl);
3713 char *mangled_name = mangle_class_name_for_template
3714 (IDENTIFIER_POINTER (name),
3715 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3716 CLASSTYPE_TI_ARGS (t));
3717 tree id = get_identifier (mangled_name);
3718 IDENTIFIER_TEMPLATE (id) = name;
3719 return id;
3723 return TYPE_IDENTIFIER (t);
3726 static void
3727 add_pending_template (d)
3728 tree d;
3730 tree ti = (TYPE_P (d)
3731 ? CLASSTYPE_TEMPLATE_INFO (d)
3732 : DECL_TEMPLATE_INFO (d));
3733 int level;
3735 if (TI_PENDING_TEMPLATE_FLAG (ti))
3736 return;
3738 /* We are called both from instantiate_decl, where we've already had a
3739 tinst_level pushed, and instantiate_template, where we haven't.
3740 Compensate. */
3741 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3743 if (level)
3744 push_tinst_level (d);
3746 *template_tail = tree_cons (current_tinst_level, d, NULL_TREE);
3747 template_tail = &TREE_CHAIN (*template_tail);
3748 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3750 if (level)
3751 pop_tinst_level ();
3755 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3756 may be either a _DECL or an overloaded function or an
3757 IDENTIFIER_NODE), and ARGLIST. */
3759 tree
3760 lookup_template_function (fns, arglist)
3761 tree fns, arglist;
3763 tree type;
3765 if (fns == NULL_TREE)
3767 cp_error ("non-template used as template");
3768 return error_mark_node;
3771 type = TREE_TYPE (fns);
3772 if (TREE_CODE (fns) == OVERLOAD || !type)
3773 type = unknown_type_node;
3775 if (processing_template_decl)
3776 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3777 else
3778 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3781 /* Within the scope of a template class S<T>, the name S gets bound
3782 (in build_self_reference) to a TYPE_DECL for the class, not a
3783 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3784 or one of its enclosing classes, and that type is a template,
3785 return the associated TEMPLATE_DECL. Otherwise, the original
3786 DECL is returned. */
3788 static tree
3789 maybe_get_template_decl_from_type_decl (decl)
3790 tree decl;
3792 return (decl != NULL_TREE
3793 && TREE_CODE (decl) == TYPE_DECL
3794 && DECL_ARTIFICIAL (decl)
3795 && CLASS_TYPE_P (TREE_TYPE (decl))
3796 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3797 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3800 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3801 parameters, find the desired type.
3803 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3804 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3805 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3806 otherwise.)
3808 IN_DECL, if non-NULL, is the template declaration we are trying to
3809 instantiate.
3811 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3812 the class we are looking up.
3814 If COMPLAIN is non-zero, issue error messages.
3816 If the template class is really a local class in a template
3817 function, then the FUNCTION_CONTEXT is the function in which it is
3818 being instantiated. */
3820 tree
3821 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
3822 tree d1, arglist;
3823 tree in_decl;
3824 tree context;
3825 int entering_scope;
3826 int complain;
3828 tree template = NULL_TREE, parmlist;
3829 tree t;
3831 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3833 if (IDENTIFIER_VALUE (d1)
3834 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3835 template = IDENTIFIER_VALUE (d1);
3836 else
3838 if (context)
3839 push_decl_namespace (context);
3840 template = lookup_name (d1, /*prefer_type=*/0);
3841 template = maybe_get_template_decl_from_type_decl (template);
3842 if (context)
3843 pop_decl_namespace ();
3845 if (template)
3846 context = DECL_CONTEXT (template);
3848 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3850 tree type = TREE_TYPE (d1);
3852 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3853 an implicit typename for the second A. Deal with it. */
3854 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3855 type = TREE_TYPE (type);
3857 if (CLASSTYPE_TEMPLATE_INFO (type))
3859 template = CLASSTYPE_TI_TEMPLATE (type);
3860 d1 = DECL_NAME (template);
3863 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3864 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
3866 template = TYPE_TI_TEMPLATE (d1);
3867 d1 = DECL_NAME (template);
3869 else if (TREE_CODE (d1) == TEMPLATE_DECL
3870 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
3872 template = d1;
3873 d1 = DECL_NAME (template);
3874 context = DECL_CONTEXT (template);
3877 /* With something like `template <class T> class X class X { ... };'
3878 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3879 We don't want to do that, but we have to deal with the situation,
3880 so let's give them some syntax errors to chew on instead of a
3881 crash. Alternatively D1 might not be a template type at all. */
3882 if (! template)
3884 if (complain)
3885 cp_error ("`%T' is not a template", d1);
3886 return error_mark_node;
3889 if (TREE_CODE (template) != TEMPLATE_DECL
3890 /* If we're called from the parser, make sure it's a user visible
3891 template. */
3892 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
3893 && !DECL_TEMPLATE_PARM_P (template)
3894 && !PRIMARY_TEMPLATE_P (template)))
3896 if (complain)
3898 cp_error ("non-template type `%T' used as a template", d1);
3899 if (in_decl)
3900 cp_error_at ("for template declaration `%D'", in_decl);
3902 return error_mark_node;
3905 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3907 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3908 template arguments */
3910 tree parm;
3911 tree arglist2;
3913 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3915 arglist2 = coerce_template_parms (parmlist, arglist, template,
3916 complain, /*require_all_args=*/1);
3917 if (arglist2 == error_mark_node)
3918 return error_mark_node;
3920 parm = copy_template_template_parm (TREE_TYPE (template), arglist2);
3921 TYPE_SIZE (parm) = 0;
3922 return parm;
3924 else
3926 tree template_type = TREE_TYPE (template);
3927 tree gen_tmpl;
3928 tree type_decl;
3929 tree found = NULL_TREE;
3930 tree *tp;
3931 int arg_depth;
3932 int parm_depth;
3933 int is_partial_instantiation;
3935 gen_tmpl = most_general_template (template);
3936 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
3937 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3938 arg_depth = TMPL_ARGS_DEPTH (arglist);
3940 if (arg_depth == 1 && parm_depth > 1)
3942 /* We've been given an incomplete set of template arguments.
3943 For example, given:
3945 template <class T> struct S1 {
3946 template <class U> struct S2 {};
3947 template <class U> struct S2<U*> {};
3950 we will be called with an ARGLIST of `U*', but the
3951 TEMPLATE will be `template <class T> template
3952 <class U> struct S1<T>::S2'. We must fill in the missing
3953 arguments. */
3954 arglist
3955 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3956 arglist);
3957 arg_depth = TMPL_ARGS_DEPTH (arglist);
3960 /* Now we should enough arguments. */
3961 my_friendly_assert (parm_depth == arg_depth, 0);
3963 /* From here on, we're only interested in the most general
3964 template. */
3965 template = gen_tmpl;
3967 /* Calculate the BOUND_ARGS. These will be the args that are
3968 actually tsubst'd into the definition to create the
3969 instantiation. */
3970 if (parm_depth > 1)
3972 /* We have multiple levels of arguments to coerce, at once. */
3973 int i;
3974 int saved_depth = TMPL_ARGS_DEPTH (arglist);
3976 tree bound_args = make_tree_vec (parm_depth);
3978 for (i = saved_depth,
3979 t = DECL_TEMPLATE_PARMS (template);
3980 i > 0 && t != NULL_TREE;
3981 --i, t = TREE_CHAIN (t))
3983 tree a = coerce_template_parms (TREE_VALUE (t),
3984 arglist, template,
3985 complain, /*require_all_args=*/1);
3986 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3988 /* We temporarily reduce the length of the ARGLIST so
3989 that coerce_template_parms will see only the arguments
3990 corresponding to the template parameters it is
3991 examining. */
3992 TREE_VEC_LENGTH (arglist)--;
3995 /* Restore the ARGLIST to its full size. */
3996 TREE_VEC_LENGTH (arglist) = saved_depth;
3998 arglist = bound_args;
4000 else
4001 arglist
4002 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4003 INNERMOST_TEMPLATE_ARGS (arglist),
4004 template,
4005 complain, /*require_all_args=*/1);
4007 if (arglist == error_mark_node)
4008 /* We were unable to bind the arguments. */
4009 return error_mark_node;
4011 /* In the scope of a template class, explicit references to the
4012 template class refer to the type of the template, not any
4013 instantiation of it. For example, in:
4015 template <class T> class C { void f(C<T>); }
4017 the `C<T>' is just the same as `C'. Outside of the
4018 class, however, such a reference is an instantiation. */
4019 if (comp_template_args (TYPE_TI_ARGS (template_type),
4020 arglist))
4022 found = template_type;
4024 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4026 tree ctx;
4028 /* Note that we use DECL_CONTEXT, rather than
4029 CP_DECL_CONTEXT, so that the termination test is
4030 always just `ctx'. We're not interested in namespace
4031 scopes. */
4032 for (ctx = current_class_type;
4033 ctx;
4034 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4035 if (same_type_p (ctx, template_type))
4036 break;
4038 if (!ctx)
4039 /* We're not in the scope of the class, so the
4040 TEMPLATE_TYPE is not the type we want after
4041 all. */
4042 found = NULL_TREE;
4045 if (found)
4046 return found;
4048 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4049 *tp;
4050 tp = &TREE_CHAIN (*tp))
4051 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4053 found = *tp;
4055 /* Use the move-to-front heuristic to speed up future
4056 searches. */
4057 *tp = TREE_CHAIN (*tp);
4058 TREE_CHAIN (found)
4059 = DECL_TEMPLATE_INSTANTIATIONS (template);
4060 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4062 return TREE_VALUE (found);
4065 /* This type is a "partial instantiation" if any of the template
4066 arguments still involve template parameters. Note that we set
4067 IS_PARTIAL_INSTANTIATION for partial specializations as
4068 well. */
4069 is_partial_instantiation = uses_template_parms (arglist);
4071 if (!is_partial_instantiation
4072 && !PRIMARY_TEMPLATE_P (template)
4073 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4075 found = xref_tag_from_type (TREE_TYPE (template),
4076 DECL_NAME (template),
4077 /*globalize=*/1);
4078 return found;
4081 context = tsubst (DECL_CONTEXT (template), arglist,
4082 /*complain=*/0, in_decl);
4083 if (!context)
4084 context = global_namespace;
4086 /* Create the type. */
4087 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4089 if (!is_partial_instantiation)
4091 set_current_access_from_decl (TYPE_NAME (template_type));
4092 t = start_enum (TYPE_IDENTIFIER (template_type));
4094 else
4095 /* We don't want to call start_enum for this type, since
4096 the values for the enumeration constants may involve
4097 template parameters. And, no one should be interested
4098 in the enumeration constants for such a type. */
4099 t = make_node (ENUMERAL_TYPE);
4101 else
4103 t = make_aggr_type (TREE_CODE (template_type));
4104 CLASSTYPE_DECLARED_CLASS (t)
4105 = CLASSTYPE_DECLARED_CLASS (template_type);
4106 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4107 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4108 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4110 /* A local class. Make sure the decl gets registered properly. */
4111 if (context == current_function_decl)
4112 pushtag (DECL_NAME (template), t, 0);
4115 /* If we called start_enum or pushtag above, this information
4116 will already be set up. */
4117 if (!TYPE_NAME (t))
4119 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4121 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4122 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4123 TYPE_STUB_DECL (t) = type_decl;
4124 DECL_SOURCE_FILE (type_decl)
4125 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4126 DECL_SOURCE_LINE (type_decl)
4127 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
4129 else
4130 type_decl = TYPE_NAME (t);
4132 /* Set up the template information. We have to figure out which
4133 template is the immediate parent if this is a full
4134 instantiation. */
4135 if (parm_depth == 1 || is_partial_instantiation
4136 || !PRIMARY_TEMPLATE_P (template))
4137 /* This case is easy; there are no member templates involved. */
4138 found = template;
4139 else
4141 /* This is a full instantiation of a member template. Look
4142 for a partial instantiation of which this is an instance. */
4144 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4145 found; found = TREE_CHAIN (found))
4147 int success;
4148 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4150 /* We only want partial instantiations, here, not
4151 specializations or full instantiations. */
4152 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4153 || !uses_template_parms (TREE_VALUE (found)))
4154 continue;
4156 /* Temporarily reduce by one the number of levels in the
4157 ARGLIST and in FOUND so as to avoid comparing the
4158 last set of arguments. */
4159 TREE_VEC_LENGTH (arglist)--;
4160 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4162 /* See if the arguments match. If they do, then TMPL is
4163 the partial instantiation we want. */
4164 success = comp_template_args (TREE_PURPOSE (found), arglist);
4166 /* Restore the argument vectors to their full size. */
4167 TREE_VEC_LENGTH (arglist)++;
4168 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4170 if (success)
4172 found = tmpl;
4173 break;
4177 if (!found)
4179 /* There was no partial instantiation. This happens
4180 where C<T> is a member template of A<T> and it's used
4181 in something like
4183 template <typename T> struct B { A<T>::C<int> m; };
4184 B<float>;
4186 Create the partial instantiation.
4188 TREE_VEC_LENGTH (arglist)--;
4189 template = tsubst (template, arglist, /*complain=*/0, NULL_TREE);
4190 TREE_VEC_LENGTH (arglist)++;
4191 found = template;
4195 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4196 DECL_TEMPLATE_INSTANTIATIONS (template)
4197 = tree_cons (arglist, t,
4198 DECL_TEMPLATE_INSTANTIATIONS (template));
4200 if (TREE_CODE (t) == ENUMERAL_TYPE
4201 && !is_partial_instantiation)
4202 /* Now that the type has been registered on the instantiations
4203 list, we set up the enumerators. Because the enumeration
4204 constants may involve the enumeration type itself, we make
4205 sure to register the type first, and then create the
4206 constants. That way, doing tsubst_expr for the enumeration
4207 constants won't result in recursive calls here; we'll find
4208 the instantiation and exit above. */
4209 tsubst_enum (template_type, t, arglist);
4211 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4212 is set up. */
4213 if (TREE_CODE (t) != ENUMERAL_TYPE)
4214 DECL_NAME (type_decl) = classtype_mangled_name (t);
4215 if (!is_partial_instantiation)
4217 /* For backwards compatibility; code that uses
4218 -fexternal-templates expects looking up a template to
4219 instantiate it. I think DDD still relies on this.
4220 (jason 8/20/1998) */
4221 if (TREE_CODE (t) != ENUMERAL_TYPE
4222 && flag_external_templates
4223 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4224 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4225 add_pending_template (t);
4227 else
4228 /* If the type makes use of template parameters, the
4229 code that generates debugging information will crash. */
4230 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4232 return t;
4236 struct pair_fn_data
4238 tree_fn_t fn;
4239 void *data;
4242 /* Called from for_each_template_parm via walk_tree. */
4244 static tree
4245 for_each_template_parm_r (tp, walk_subtrees, d)
4246 tree *tp;
4247 int *walk_subtrees;
4248 void *d;
4250 tree t = *tp;
4251 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4252 tree_fn_t fn = pfd->fn;
4253 void *data = pfd->data;
4255 if (TYPE_P (t)
4256 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4257 return error_mark_node;
4259 switch (TREE_CODE (t))
4261 case RECORD_TYPE:
4262 if (TYPE_PTRMEMFUNC_P (t))
4263 break;
4264 /* Fall through. */
4266 case UNION_TYPE:
4267 case ENUMERAL_TYPE:
4268 if (!TYPE_TEMPLATE_INFO (t))
4269 *walk_subtrees = 0;
4270 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4271 fn, data))
4272 return error_mark_node;
4273 break;
4275 case METHOD_TYPE:
4276 /* Since we're not going to walk subtrees, we have to do this
4277 explicitly here. */
4278 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
4279 return error_mark_node;
4280 /* Fall through. */
4282 case FUNCTION_TYPE:
4283 /* Check the return type. */
4284 if (for_each_template_parm (TREE_TYPE (t), fn, data))
4285 return error_mark_node;
4287 /* Check the parameter types. Since default arguments are not
4288 instantiated until they are needed, the TYPE_ARG_TYPES may
4289 contain expressions that involve template parameters. But,
4290 no-one should be looking at them yet. And, once they're
4291 instantiated, they don't contain template parameters, so
4292 there's no point in looking at them then, either. */
4294 tree parm;
4296 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4297 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4298 return error_mark_node;
4300 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4301 want walk_tree walking into them itself. */
4302 *walk_subtrees = 0;
4304 break;
4306 case FUNCTION_DECL:
4307 case VAR_DECL:
4308 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4309 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
4310 return error_mark_node;
4311 /* Fall through. */
4313 case CONST_DECL:
4314 case PARM_DECL:
4315 if (DECL_CONTEXT (t)
4316 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
4317 return error_mark_node;
4318 break;
4320 case BOUND_TEMPLATE_TEMPLATE_PARM:
4321 /* Record template parameters such as `T' inside `TT<T>'. */
4322 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
4323 return error_mark_node;
4324 /* Fall through. */
4326 case TEMPLATE_TEMPLATE_PARM:
4327 case TEMPLATE_TYPE_PARM:
4328 case TEMPLATE_PARM_INDEX:
4329 if (fn && (*fn)(t, data))
4330 return error_mark_node;
4331 else if (!fn)
4332 return error_mark_node;
4333 break;
4335 case TEMPLATE_DECL:
4336 /* A template template parameter is encountered */
4337 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4338 && for_each_template_parm (TREE_TYPE (t), fn, data))
4339 return error_mark_node;
4341 /* Already substituted template template parameter */
4342 *walk_subtrees = 0;
4343 break;
4345 case TYPENAME_TYPE:
4346 if (!fn || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, data))
4347 return error_mark_node;
4348 break;
4350 case CONSTRUCTOR:
4351 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4352 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4353 (TREE_TYPE (t)), fn, data))
4354 return error_mark_node;
4355 break;
4357 case INDIRECT_REF:
4358 case COMPONENT_REF:
4359 /* If there's no type, then this thing must be some expression
4360 involving template parameters. */
4361 if (!fn && !TREE_TYPE (t))
4362 return error_mark_node;
4363 break;
4365 case MODOP_EXPR:
4366 case CAST_EXPR:
4367 case REINTERPRET_CAST_EXPR:
4368 case CONST_CAST_EXPR:
4369 case STATIC_CAST_EXPR:
4370 case DYNAMIC_CAST_EXPR:
4371 case ARROW_EXPR:
4372 case DOTSTAR_EXPR:
4373 case TYPEID_EXPR:
4374 case LOOKUP_EXPR:
4375 case PSEUDO_DTOR_EXPR:
4376 if (!fn)
4377 return error_mark_node;
4378 break;
4380 default:
4381 break;
4384 /* We didn't find any template parameters we liked. */
4385 return NULL_TREE;
4388 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4389 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4390 call FN with the parameter and the DATA.
4391 If FN returns non-zero, the iteration is terminated, and
4392 for_each_template_parm returns 1. Otherwise, the iteration
4393 continues. If FN never returns a non-zero value, the value
4394 returned by for_each_template_parm is 0. If FN is NULL, it is
4395 considered to be the function which always returns 1. */
4397 static int
4398 for_each_template_parm (t, fn, data)
4399 tree t;
4400 tree_fn_t fn;
4401 void* data;
4403 struct pair_fn_data pfd;
4405 /* Set up. */
4406 pfd.fn = fn;
4407 pfd.data = data;
4409 /* Walk the tree. (Conceptually, we would like to walk without
4410 duplicates, but for_each_template_parm_r recursively calls
4411 for_each_template_parm, so we would need to reorganize a fair
4412 bit to use walk_tree_without_duplicates.) */
4413 return walk_tree (&t,
4414 for_each_template_parm_r,
4415 &pfd,
4416 NULL) != NULL_TREE;
4420 uses_template_parms (t)
4421 tree t;
4423 return for_each_template_parm (t, 0, 0);
4426 static int tinst_depth;
4427 extern int max_tinst_depth;
4428 #ifdef GATHER_STATISTICS
4429 int depth_reached;
4430 #endif
4431 static int tinst_level_tick;
4432 static int last_template_error_tick;
4434 /* We're starting to instantiate D; record the template instantiation context
4435 for diagnostics and to restore it later. */
4437 static int
4438 push_tinst_level (d)
4439 tree d;
4441 tree new;
4443 if (tinst_depth >= max_tinst_depth)
4445 /* If the instantiation in question still has unbound template parms,
4446 we don't really care if we can't instantiate it, so just return.
4447 This happens with base instantiation for implicit `typename'. */
4448 if (uses_template_parms (d))
4449 return 0;
4451 last_template_error_tick = tinst_level_tick;
4452 cp_error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4453 max_tinst_depth, d);
4455 print_instantiation_context ();
4457 return 0;
4460 new = build_expr_wfl (d, input_filename, lineno, 0);
4461 TREE_CHAIN (new) = current_tinst_level;
4462 current_tinst_level = new;
4464 ++tinst_depth;
4465 #ifdef GATHER_STATISTICS
4466 if (tinst_depth > depth_reached)
4467 depth_reached = tinst_depth;
4468 #endif
4470 ++tinst_level_tick;
4471 return 1;
4474 /* We're done instantiating this template; return to the instantiation
4475 context. */
4477 void
4478 pop_tinst_level ()
4480 tree old = current_tinst_level;
4482 /* Restore the filename and line number stashed away when we started
4483 this instantiation. */
4484 lineno = TINST_LINE (old);
4485 input_filename = TINST_FILE (old);
4486 extract_interface_info ();
4488 current_tinst_level = TREE_CHAIN (old);
4489 --tinst_depth;
4490 ++tinst_level_tick;
4493 /* We're instantiating a deferred template; restore the template
4494 instantiation context in which the instantiation was requested, which
4495 is one step out from LEVEL. */
4497 static void
4498 reopen_tinst_level (level)
4499 tree level;
4501 tree t;
4503 tinst_depth = 0;
4504 for (t = level; t; t = TREE_CHAIN (t))
4505 ++tinst_depth;
4507 current_tinst_level = level;
4508 pop_tinst_level ();
4511 /* Return the outermost template instantiation context, for use with
4512 -falt-external-templates. */
4514 tree
4515 tinst_for_decl ()
4517 tree p = current_tinst_level;
4519 if (p)
4520 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4522 return p;
4525 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4526 vector of template arguments, as for tsubst.
4528 Returns an appropriate tsubst'd friend declaration. */
4530 static tree
4531 tsubst_friend_function (decl, args)
4532 tree decl;
4533 tree args;
4535 tree new_friend;
4536 int line = lineno;
4537 const char *file = input_filename;
4539 lineno = DECL_SOURCE_LINE (decl);
4540 input_filename = DECL_SOURCE_FILE (decl);
4542 if (TREE_CODE (decl) == FUNCTION_DECL
4543 && DECL_TEMPLATE_INSTANTIATION (decl)
4544 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4545 /* This was a friend declared with an explicit template
4546 argument list, e.g.:
4548 friend void f<>(T);
4550 to indicate that f was a template instantiation, not a new
4551 function declaration. Now, we have to figure out what
4552 instantiation of what template. */
4554 tree template_id, arglist, fns;
4555 tree new_args;
4556 tree tmpl;
4557 tree ns = CP_DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4559 /* Friend functions are looked up in the containing namespace scope.
4560 We must enter that scope, to avoid finding member functions of the
4561 current cless with same name. */
4562 push_nested_namespace (ns);
4563 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4564 /*complain=*/1, NULL_TREE);
4565 pop_nested_namespace (ns);
4566 arglist = tsubst (DECL_TI_ARGS (decl), args,
4567 /*complain=*/1, NULL_TREE);
4568 template_id = lookup_template_function (fns, arglist);
4570 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4571 tmpl = determine_specialization (template_id, new_friend,
4572 &new_args,
4573 /*need_member_template=*/0);
4574 new_friend = instantiate_template (tmpl, new_args);
4575 goto done;
4578 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4580 /* The NEW_FRIEND will look like an instantiation, to the
4581 compiler, but is not an instantiation from the point of view of
4582 the language. For example, we might have had:
4584 template <class T> struct S {
4585 template <class U> friend void f(T, U);
4588 Then, in S<int>, template <class U> void f(int, U) is not an
4589 instantiation of anything. */
4590 DECL_USE_TEMPLATE (new_friend) = 0;
4591 if (TREE_CODE (decl) == TEMPLATE_DECL)
4593 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4594 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4595 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4598 /* The mangled name for the NEW_FRIEND is incorrect. The function
4599 is not a template instantiation and should not be mangled like
4600 one. Therefore, we forget the mangling here; we'll recompute it
4601 later if we need it. */
4602 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4604 SET_DECL_RTL (new_friend, NULL_RTX);
4605 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4608 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4610 tree old_decl;
4611 tree new_friend_template_info;
4612 tree new_friend_result_template_info;
4613 tree ns;
4614 int new_friend_is_defn;
4616 /* We must save some information from NEW_FRIEND before calling
4617 duplicate decls since that function will free NEW_FRIEND if
4618 possible. */
4619 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4620 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4622 /* This declaration is a `primary' template. */
4623 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4625 new_friend_is_defn
4626 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4627 new_friend_result_template_info
4628 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4630 else
4632 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4633 new_friend_result_template_info = NULL_TREE;
4636 /* Inside pushdecl_namespace_level, we will push into the
4637 current namespace. However, the friend function should go
4638 into the namespace of the template. */
4639 ns = decl_namespace_context (new_friend);
4640 push_nested_namespace (ns);
4641 old_decl = pushdecl_namespace_level (new_friend);
4642 pop_nested_namespace (ns);
4644 if (old_decl != new_friend)
4646 /* This new friend declaration matched an existing
4647 declaration. For example, given:
4649 template <class T> void f(T);
4650 template <class U> class C {
4651 template <class T> friend void f(T) {}
4654 the friend declaration actually provides the definition
4655 of `f', once C has been instantiated for some type. So,
4656 old_decl will be the out-of-class template declaration,
4657 while new_friend is the in-class definition.
4659 But, if `f' was called before this point, the
4660 instantiation of `f' will have DECL_TI_ARGS corresponding
4661 to `T' but not to `U', references to which might appear
4662 in the definition of `f'. Previously, the most general
4663 template for an instantiation of `f' was the out-of-class
4664 version; now it is the in-class version. Therefore, we
4665 run through all specialization of `f', adding to their
4666 DECL_TI_ARGS appropriately. In particular, they need a
4667 new set of outer arguments, corresponding to the
4668 arguments for this class instantiation.
4670 The same situation can arise with something like this:
4672 friend void f(int);
4673 template <class T> class C {
4674 friend void f(T) {}
4677 when `C<int>' is instantiated. Now, `f(int)' is defined
4678 in the class. */
4680 if (!new_friend_is_defn)
4681 /* On the other hand, if the in-class declaration does
4682 *not* provide a definition, then we don't want to alter
4683 existing definitions. We can just leave everything
4684 alone. */
4686 else
4688 /* Overwrite whatever template info was there before, if
4689 any, with the new template information pertaining to
4690 the declaration. */
4691 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4693 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4694 /* duplicate_decls will take care of this case. */
4696 else
4698 tree t;
4699 tree new_friend_args;
4701 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4702 = new_friend_result_template_info;
4704 new_friend_args = TI_ARGS (new_friend_template_info);
4705 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4706 t != NULL_TREE;
4707 t = TREE_CHAIN (t))
4709 tree spec = TREE_VALUE (t);
4711 DECL_TI_ARGS (spec)
4712 = add_outermost_template_args (new_friend_args,
4713 DECL_TI_ARGS (spec));
4716 /* Now, since specializations are always supposed to
4717 hang off of the most general template, we must move
4718 them. */
4719 t = most_general_template (old_decl);
4720 if (t != old_decl)
4722 DECL_TEMPLATE_SPECIALIZATIONS (t)
4723 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4724 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4725 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4730 /* The information from NEW_FRIEND has been merged into OLD_DECL
4731 by duplicate_decls. */
4732 new_friend = old_decl;
4735 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
4737 /* Check to see that the declaration is really present, and,
4738 possibly obtain an improved declaration. */
4739 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4740 new_friend);
4742 if (fn)
4743 new_friend = fn;
4746 done:
4747 lineno = line;
4748 input_filename = file;
4749 return new_friend;
4752 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4753 template arguments, as for tsubst.
4755 Returns an appropriate tsubst'd friend type or error_mark_node on
4756 failure. */
4758 static tree
4759 tsubst_friend_class (friend_tmpl, args)
4760 tree friend_tmpl;
4761 tree args;
4763 tree friend_type;
4764 tree tmpl;
4766 /* First, we look for a class template. */
4767 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4769 /* But, if we don't find one, it might be because we're in a
4770 situation like this:
4772 template <class T>
4773 struct S {
4774 template <class U>
4775 friend struct S;
4778 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4779 for `S<int>', not the TEMPLATE_DECL. */
4780 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4782 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4783 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4786 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
4788 /* The friend template has already been declared. Just
4789 check to see that the declarations match, and install any new
4790 default parameters. We must tsubst the default parameters,
4791 of course. We only need the innermost template parameters
4792 because that is all that redeclare_class_template will look
4793 at. */
4794 tree parms
4795 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4796 args, /*complain=*/1);
4797 if (!parms)
4798 return error_mark_node;
4799 redeclare_class_template (TREE_TYPE (tmpl), parms);
4800 friend_type = TREE_TYPE (tmpl);
4802 else
4804 /* The friend template has not already been declared. In this
4805 case, the instantiation of the template class will cause the
4806 injection of this template into the global scope. */
4807 tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE);
4809 /* The new TMPL is not an instantiation of anything, so we
4810 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4811 the new type because that is supposed to be the corresponding
4812 template decl, i.e., TMPL. */
4813 DECL_USE_TEMPLATE (tmpl) = 0;
4814 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4815 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4817 /* Inject this template into the global scope. */
4818 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4821 return friend_type;
4824 tree
4825 instantiate_class_template (type)
4826 tree type;
4828 tree template, args, pattern, t;
4829 tree typedecl;
4831 if (type == error_mark_node)
4832 return error_mark_node;
4834 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
4835 return type;
4837 /* Figure out which template is being instantiated. */
4838 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4839 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4841 /* Figure out which arguments are being used to do the
4842 instantiation. */
4843 args = CLASSTYPE_TI_ARGS (type);
4844 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
4846 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4847 /* If this is a partial instantiation, then we can't instantiate
4848 the type; there's no telling whether or not one of the
4849 template parameters might eventually be instantiated to some
4850 value that results in a specialization being used. For
4851 example, consider:
4853 template <class T>
4854 struct S {};
4856 template <class U>
4857 void f(S<U>);
4859 template <>
4860 struct S<int> {};
4862 Now, the `S<U>' in `f<int>' is the specialization, not an
4863 instantiation of the original template. */
4864 return type;
4866 /* Determine what specialization of the original template to
4867 instantiate. */
4868 if (PARTIAL_INSTANTIATION_P (type))
4869 /* There's no telling which specialization is appropriate at this
4870 point. Since all peeking at the innards of this partial
4871 instantiation are extensions (like the "implicit typename"
4872 extension, which allows users to omit the keyword `typename' on
4873 names that are declared as types in template base classes), we
4874 are free to do what we please.
4876 Trying to figure out which partial instantiation to use can
4877 cause a crash. (Some of the template arguments don't even have
4878 types.) So, we just use the most general version. */
4879 t = NULL_TREE;
4880 else
4882 t = most_specialized_class (template, args);
4884 if (t == error_mark_node)
4886 const char *str = "candidates are:";
4887 cp_error ("ambiguous class template instantiation for `%#T'", type);
4888 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4889 t = TREE_CHAIN (t))
4891 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4892 args))
4894 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4895 str = " ";
4898 TYPE_BEING_DEFINED (type) = 1;
4899 return error_mark_node;
4903 if (t)
4904 pattern = TREE_TYPE (t);
4905 else
4906 pattern = TREE_TYPE (template);
4908 /* If the template we're instantiating is incomplete, then clearly
4909 there's nothing we can do. */
4910 if (!COMPLETE_TYPE_P (pattern))
4911 return type;
4913 /* If this is a partial instantiation, don't tsubst anything. We will
4914 only use this type for implicit typename, so the actual contents don't
4915 matter. All that matters is whether a particular name is a type. */
4916 if (PARTIAL_INSTANTIATION_P (type))
4918 /* The fields set here must be kept in sync with those cleared
4919 in begin_class_definition. */
4920 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4921 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4922 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4923 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4924 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
4926 /* Pretend that the type is complete, so that we will look
4927 inside it during name lookup and such. */
4928 TYPE_SIZE (type) = bitsize_zero_node;
4929 return type;
4932 /* If we've recursively instantiated too many templates, stop. */
4933 if (! push_tinst_level (type))
4934 return type;
4936 /* Now we're really doing the instantiation. Mark the type as in
4937 the process of being defined. */
4938 TYPE_BEING_DEFINED (type) = 1;
4940 maybe_push_to_top_level (uses_template_parms (type));
4942 if (t)
4944 /* This TYPE is actually a instantiation of of a partial
4945 specialization. We replace the innermost set of ARGS with
4946 the arguments appropriate for substitution. For example,
4947 given:
4949 template <class T> struct S {};
4950 template <class T> struct S<T*> {};
4952 and supposing that we are instantiating S<int*>, ARGS will
4953 present be {int*} but we need {int}. */
4954 tree inner_args
4955 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4956 args);
4958 /* If there were multiple levels in ARGS, replacing the
4959 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4960 want, so we make a copy first. */
4961 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4963 args = copy_node (args);
4964 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4966 else
4967 args = inner_args;
4970 if (flag_external_templates)
4972 if (flag_alt_external_templates)
4974 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4975 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4976 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4977 = (! CLASSTYPE_INTERFACE_ONLY (type)
4978 && CLASSTYPE_INTERFACE_KNOWN (type));
4980 else
4982 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4983 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4984 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4985 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4986 = (! CLASSTYPE_INTERFACE_ONLY (type)
4987 && CLASSTYPE_INTERFACE_KNOWN (type));
4990 else
4992 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4993 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
4996 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4997 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4998 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4999 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5000 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5001 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5002 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5003 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5004 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
5005 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5006 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5007 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5008 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5009 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5010 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5011 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5012 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5013 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5014 TYPE_USES_MULTIPLE_INHERITANCE (type)
5015 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5016 TYPE_USES_VIRTUAL_BASECLASSES (type)
5017 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5018 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5019 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5020 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5021 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5022 if (ANON_AGGR_TYPE_P (pattern))
5023 SET_ANON_AGGR_TYPE_P (type);
5025 if (TYPE_BINFO_BASETYPES (pattern))
5027 tree base_list = NULL_TREE;
5028 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5029 int i;
5031 /* Substitute into each of the bases to determine the actual
5032 basetypes. */
5033 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5035 tree base;
5036 tree access;
5037 tree pbase;
5039 pbase = TREE_VEC_ELT (pbases, i);
5041 /* Substitute to figure out the base class. */
5042 base = tsubst (BINFO_TYPE (pbase), args,
5043 /*complain=*/1, NULL_TREE);
5044 if (base == error_mark_node)
5045 continue;
5047 /* Calculate the correct access node. */
5048 if (TREE_VIA_VIRTUAL (pbase))
5050 if (TREE_VIA_PUBLIC (pbase))
5051 access = access_public_virtual_node;
5052 else if (TREE_VIA_PROTECTED (pbase))
5053 access = access_protected_virtual_node;
5054 else
5055 access = access_private_virtual_node;
5057 else
5059 if (TREE_VIA_PUBLIC (pbase))
5060 access = access_public_node;
5061 else if (TREE_VIA_PROTECTED (pbase))
5062 access = access_protected_node;
5063 else
5064 access = access_private_node;
5067 base_list = tree_cons (access, base, base_list);
5070 /* The list is now in reverse order; correct that. */
5071 base_list = nreverse (base_list);
5073 /* Now call xref_basetypes to set up all the base-class
5074 information. */
5075 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5076 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5077 ? class_type_node : record_type_node)
5078 : union_type_node,
5079 DECL_NAME (TYPE_NAME (pattern)),
5080 type,
5081 base_list);
5084 /* Now that our base classes are set up, enter the scope of the
5085 class, so that name lookups into base classes, etc. will work
5086 correctly. This is precisely analogous to what we do in
5087 begin_class_definition when defining an ordinary non-template
5088 class. */
5089 pushclass (type, 1);
5091 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
5093 tree tag = TREE_VALUE (t);
5094 tree name = TYPE_IDENTIFIER (tag);
5095 tree newtag;
5097 newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE);
5098 my_friendly_assert (newtag != error_mark_node, 20010206);
5099 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5101 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5102 /* Unfortunately, lookup_template_class sets
5103 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5104 instantiation (i.e., for the type of a member template
5105 class nested within a template class.) This behavior is
5106 required for maybe_process_partial_specialization to work
5107 correctly, but is not accurate in this case; the TAG is not
5108 an instantiation of anything. (The corresponding
5109 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5110 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5112 /* Now, we call pushtag to put this NEWTAG into the scope of
5113 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5114 pushtag calling push_template_decl. We don't have to do
5115 this for enums because it will already have been done in
5116 tsubst_enum. */
5117 if (name)
5118 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5119 pushtag (name, newtag, /*globalize=*/0);
5123 /* Don't replace enum constants here. */
5124 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
5125 if (TREE_CODE (t) != CONST_DECL)
5127 tree r;
5129 /* The the file and line for this declaration, to assist in
5130 error message reporting. Since we called push_tinst_level
5131 above, we don't need to restore these. */
5132 lineno = DECL_SOURCE_LINE (t);
5133 input_filename = DECL_SOURCE_FILE (t);
5135 r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5136 if (TREE_CODE (r) == VAR_DECL)
5138 tree init;
5140 if (DECL_INITIALIZED_IN_CLASS_P (r))
5141 init = tsubst_expr (DECL_INITIAL (t), args,
5142 /*complain=*/1, NULL_TREE);
5143 else
5144 init = NULL_TREE;
5146 finish_static_data_member_decl (r, init,
5147 /*asmspec_tree=*/NULL_TREE,
5148 /*flags=*/0);
5150 if (DECL_INITIALIZED_IN_CLASS_P (r))
5151 check_static_variable_definition (r, TREE_TYPE (r));
5154 /* R will have a TREE_CHAIN if and only if it has already been
5155 processed by finish_member_declaration. This can happen
5156 if, for example, it is a TYPE_DECL for a class-scoped
5157 ENUMERAL_TYPE; such a thing will already have been added to
5158 the field list by tsubst_enum above. */
5159 if (!TREE_CHAIN (r))
5161 set_current_access_from_decl (r);
5162 finish_member_declaration (r);
5166 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5167 for this instantiation. */
5168 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5170 tree r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5171 set_current_access_from_decl (r);
5172 finish_member_declaration (r);
5175 /* Construct the DECL_FRIENDLIST for the new class type. */
5176 typedecl = TYPE_MAIN_DECL (type);
5177 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5178 t != NULL_TREE;
5179 t = TREE_CHAIN (t))
5181 tree friends;
5183 for (friends = TREE_VALUE (t);
5184 friends != NULL_TREE;
5185 friends = TREE_CHAIN (friends))
5186 if (TREE_PURPOSE (friends) == error_mark_node)
5187 add_friend (type,
5188 tsubst_friend_function (TREE_VALUE (friends),
5189 args));
5190 else
5191 my_friendly_abort (20000216);
5194 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5195 t != NULL_TREE;
5196 t = TREE_CHAIN (t))
5198 tree friend_type = TREE_VALUE (t);
5199 tree new_friend_type;
5201 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5202 new_friend_type = tsubst_friend_class (friend_type, args);
5203 else if (uses_template_parms (friend_type))
5204 new_friend_type = tsubst (friend_type, args, /*complain=*/1,
5205 NULL_TREE);
5206 else
5208 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5210 /* The call to xref_tag_from_type does injection for friend
5211 classes. */
5212 push_nested_namespace (ns);
5213 new_friend_type =
5214 xref_tag_from_type (friend_type, NULL_TREE, 1);
5215 pop_nested_namespace (ns);
5218 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5219 /* Trick make_friend_class into realizing that the friend
5220 we're adding is a template, not an ordinary class. It's
5221 important that we use make_friend_class since it will
5222 perform some error-checking and output cross-reference
5223 information. */
5224 ++processing_template_decl;
5226 if (new_friend_type != error_mark_node)
5227 make_friend_class (type, new_friend_type);
5229 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5230 --processing_template_decl;
5233 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5234 if (TREE_CODE (t) == FIELD_DECL)
5236 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5237 require_complete_type (t);
5240 /* Set the file and line number information to whatever is given for
5241 the class itself. This puts error messages involving generated
5242 implicit functions at a predictable point, and the same point
5243 that would be used for non-template classes. */
5244 lineno = DECL_SOURCE_LINE (typedecl);
5245 input_filename = DECL_SOURCE_FILE (typedecl);
5247 unreverse_member_declarations (type);
5248 finish_struct_1 (type);
5249 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5251 /* Clear this now so repo_template_used is happy. */
5252 TYPE_BEING_DEFINED (type) = 0;
5253 repo_template_used (type);
5255 /* Now that the class is complete, instantiate default arguments for
5256 any member functions. We don't do this earlier because the
5257 default arguments may reference members of the class. */
5258 if (!PRIMARY_TEMPLATE_P (template))
5259 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5260 if (TREE_CODE (t) == FUNCTION_DECL
5261 /* Implicitly generated member functions will not have template
5262 information; they are not instantiations, but instead are
5263 created "fresh" for each instantiation. */
5264 && DECL_TEMPLATE_INFO (t))
5265 tsubst_default_arguments (t);
5267 popclass ();
5268 pop_from_top_level ();
5269 pop_tinst_level ();
5271 return type;
5274 static int
5275 list_eq (t1, t2)
5276 tree t1, t2;
5278 if (t1 == NULL_TREE)
5279 return t2 == NULL_TREE;
5280 if (t2 == NULL_TREE)
5281 return 0;
5282 /* Don't care if one declares its arg const and the other doesn't -- the
5283 main variant of the arg type is all that matters. */
5284 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5285 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5286 return 0;
5287 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5290 /* If arg is a non-type template parameter that does not depend on template
5291 arguments, fold it like we weren't in the body of a template. */
5293 static tree
5294 maybe_fold_nontype_arg (arg)
5295 tree arg;
5297 /* If we're not in a template, ARG is already as simple as it's going to
5298 get, and trying to reprocess the trees will break. */
5299 if (! processing_template_decl)
5300 return arg;
5302 if (!TYPE_P (arg) && !uses_template_parms (arg))
5304 /* Sometimes, one of the args was an expression involving a
5305 template constant parameter, like N - 1. Now that we've
5306 tsubst'd, we might have something like 2 - 1. This will
5307 confuse lookup_template_class, so we do constant folding
5308 here. We have to unset processing_template_decl, to
5309 fool build_expr_from_tree() into building an actual
5310 tree. */
5312 int saved_processing_template_decl = processing_template_decl;
5313 processing_template_decl = 0;
5314 arg = fold (build_expr_from_tree (arg));
5315 processing_template_decl = saved_processing_template_decl;
5317 return arg;
5320 /* Substitute ARGS into the vector of template arguments T. */
5322 static tree
5323 tsubst_template_arg_vector (t, args, complain)
5324 tree t;
5325 tree args;
5326 int complain;
5328 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5329 tree *elts = (tree *) alloca (len * sizeof (tree));
5331 memset ((char *) elts, 0, len * sizeof (tree));
5333 for (i = 0; i < len; i++)
5335 if (TREE_VEC_ELT (t, i) != NULL_TREE
5336 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5337 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5338 args, complain);
5339 else
5340 elts[i] = maybe_fold_nontype_arg
5341 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5342 NULL_TREE));
5344 if (elts[i] == error_mark_node)
5345 return error_mark_node;
5347 if (elts[i] != TREE_VEC_ELT (t, i))
5348 need_new = 1;
5351 if (!need_new)
5352 return t;
5354 t = make_tree_vec (len);
5355 for (i = 0; i < len; i++)
5356 TREE_VEC_ELT (t, i) = elts[i];
5358 return t;
5361 /* Return the result of substituting ARGS into the template parameters
5362 given by PARMS. If there are m levels of ARGS and m + n levels of
5363 PARMS, then the result will contain n levels of PARMS. For
5364 example, if PARMS is `template <class T> template <class U>
5365 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5366 result will be `template <int*, double, class V>'. */
5368 static tree
5369 tsubst_template_parms (parms, args, complain)
5370 tree parms;
5371 tree args;
5372 int complain;
5374 tree r = NULL_TREE;
5375 tree* new_parms;
5377 for (new_parms = &r;
5378 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5379 new_parms = &(TREE_CHAIN (*new_parms)),
5380 parms = TREE_CHAIN (parms))
5382 tree new_vec =
5383 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5384 int i;
5386 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5388 tree default_value =
5389 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5390 tree parm_decl =
5391 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5393 TREE_VEC_ELT (new_vec, i)
5394 = build_tree_list (tsubst (default_value, args, complain,
5395 NULL_TREE),
5396 tsubst (parm_decl, args, complain,
5397 NULL_TREE));
5400 *new_parms =
5401 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5402 - TMPL_ARGS_DEPTH (args)),
5403 new_vec, NULL_TREE);
5406 return r;
5409 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5410 type T. If T is not an aggregate or enumeration type, it is
5411 handled as if by tsubst. IN_DECL is as for tsubst. If
5412 ENTERING_SCOPE is non-zero, T is the context for a template which
5413 we are presently tsubst'ing. Return the substituted value. */
5415 static tree
5416 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5417 tree t;
5418 tree args;
5419 int complain;
5420 tree in_decl;
5421 int entering_scope;
5423 if (t == NULL_TREE)
5424 return NULL_TREE;
5426 switch (TREE_CODE (t))
5428 case RECORD_TYPE:
5429 if (TYPE_PTRMEMFUNC_P (t))
5431 tree r = build_ptrmemfunc_type
5432 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl));
5433 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5434 complain);
5437 /* else fall through */
5438 case ENUMERAL_TYPE:
5439 case UNION_TYPE:
5440 if (TYPE_TEMPLATE_INFO (t))
5442 tree argvec;
5443 tree context;
5444 tree r;
5446 /* First, determine the context for the type we are looking
5447 up. */
5448 if (TYPE_CONTEXT (t) != NULL_TREE)
5449 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5450 complain,
5451 in_decl, /*entering_scope=*/1);
5452 else
5453 context = NULL_TREE;
5455 /* Then, figure out what arguments are appropriate for the
5456 type we are trying to find. For example, given:
5458 template <class T> struct S;
5459 template <class T, class U> void f(T, U) { S<U> su; }
5461 and supposing that we are instantiating f<int, double>,
5462 then our ARGS will be {int, double}, but, when looking up
5463 S we only want {double}. */
5464 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5465 complain);
5466 if (argvec == error_mark_node)
5467 return error_mark_node;
5469 r = lookup_template_class (t, argvec, in_decl, context,
5470 entering_scope, complain);
5472 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5473 complain);
5475 else
5476 /* This is not a template type, so there's nothing to do. */
5477 return t;
5479 default:
5480 return tsubst (t, args, complain, in_decl);
5484 /* Substitute into the default argument ARG (a default argument for
5485 FN), which has the indicated TYPE. */
5487 tree
5488 tsubst_default_argument (fn, type, arg)
5489 tree fn;
5490 tree type;
5491 tree arg;
5493 /* This default argument came from a template. Instantiate the
5494 default argument here, not in tsubst. In the case of
5495 something like:
5497 template <class T>
5498 struct S {
5499 static T t();
5500 void f(T = t());
5503 we must be careful to do name lookup in the scope of S<T>,
5504 rather than in the current class. */
5505 if (DECL_CLASS_SCOPE_P (fn))
5506 pushclass (DECL_CONTEXT (fn), 2);
5508 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
5510 if (DECL_CLASS_SCOPE_P (fn))
5511 popclass ();
5513 /* Make sure the default argument is reasonable. */
5514 arg = check_default_argument (type, arg);
5516 return arg;
5519 /* Substitute into all the default arguments for FN. */
5521 static void
5522 tsubst_default_arguments (fn)
5523 tree fn;
5525 tree arg;
5526 tree tmpl_args;
5528 tmpl_args = DECL_TI_ARGS (fn);
5530 /* If this function is not yet instantiated, we certainly don't need
5531 its default arguments. */
5532 if (uses_template_parms (tmpl_args))
5533 return;
5535 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5536 arg;
5537 arg = TREE_CHAIN (arg))
5538 if (TREE_PURPOSE (arg))
5539 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5540 TREE_VALUE (arg),
5541 TREE_PURPOSE (arg));
5544 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5545 (already computed) substitution of ARGS into TREE_TYPE (T), if
5546 appropriate. Return the result of the substitution. */
5548 static tree
5549 tsubst_decl (t, args, type)
5550 tree t;
5551 tree args;
5552 tree type;
5554 int saved_lineno;
5555 const char *saved_filename;
5556 tree r = NULL_TREE;
5557 tree in_decl = t;
5559 /* Set the filename and linenumber to improve error-reporting. */
5560 saved_lineno = lineno;
5561 saved_filename = input_filename;
5562 lineno = DECL_SOURCE_LINE (t);
5563 input_filename = DECL_SOURCE_FILE (t);
5565 switch (TREE_CODE (t))
5567 case TEMPLATE_DECL:
5569 /* We can get here when processing a member template function
5570 of a template class. */
5571 tree decl = DECL_TEMPLATE_RESULT (t);
5572 tree spec;
5573 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5575 if (!is_template_template_parm)
5577 /* We might already have an instance of this template.
5578 The ARGS are for the surrounding class type, so the
5579 full args contain the tsubst'd args for the context,
5580 plus the innermost args from the template decl. */
5581 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5582 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5583 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5584 tree full_args;
5586 full_args = tsubst_template_arg_vector (tmpl_args, args,
5587 /*complain=*/1);
5589 /* tsubst_template_arg_vector doesn't copy the vector if
5590 nothing changed. But, *something* should have
5591 changed. */
5592 my_friendly_assert (full_args != tmpl_args, 0);
5594 spec = retrieve_specialization (t, full_args);
5595 if (spec != NULL_TREE)
5597 r = spec;
5598 break;
5602 /* Make a new template decl. It will be similar to the
5603 original, but will record the current template arguments.
5604 We also create a new function declaration, which is just
5605 like the old one, but points to this new template, rather
5606 than the old one. */
5607 r = copy_decl (t);
5608 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5609 TREE_CHAIN (r) = NULL_TREE;
5611 if (is_template_template_parm)
5613 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5614 DECL_TEMPLATE_RESULT (r) = new_decl;
5615 TREE_TYPE (r) = TREE_TYPE (new_decl);
5616 break;
5619 DECL_CONTEXT (r)
5620 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5621 /*complain=*/1, in_decl,
5622 /*entering_scope=*/1);
5623 DECL_VIRTUAL_CONTEXT (r)
5624 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5625 /*complain=*/1, in_decl,
5626 /*entering_scope=*/1);
5627 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5629 if (TREE_CODE (decl) == TYPE_DECL)
5631 tree new_type = tsubst (TREE_TYPE (t), args,
5632 /*complain=*/1, in_decl);
5633 TREE_TYPE (r) = new_type;
5634 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5635 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5636 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5638 else
5640 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5642 DECL_TEMPLATE_RESULT (r) = new_decl;
5643 DECL_TI_TEMPLATE (new_decl) = r;
5644 TREE_TYPE (r) = TREE_TYPE (new_decl);
5645 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5648 SET_DECL_IMPLICIT_INSTANTIATION (r);
5649 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5650 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5652 /* The template parameters for this new template are all the
5653 template parameters for the old template, except the
5654 outermost level of parameters. */
5655 DECL_TEMPLATE_PARMS (r)
5656 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5657 /*complain=*/1);
5659 if (PRIMARY_TEMPLATE_P (t))
5660 DECL_PRIMARY_TEMPLATE (r) = r;
5662 /* We don't partially instantiate partial specializations. */
5663 if (TREE_CODE (decl) == TYPE_DECL)
5664 break;
5666 /* Record this partial instantiation. */
5667 register_specialization (r, t,
5668 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5671 break;
5673 case FUNCTION_DECL:
5675 tree ctx;
5676 tree argvec = NULL_TREE;
5677 tree *friends;
5678 tree gen_tmpl;
5679 int member;
5680 int args_depth;
5681 int parms_depth;
5683 /* Nobody should be tsubst'ing into non-template functions. */
5684 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5686 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5688 tree spec;
5690 /* Calculate the most general template of which R is a
5691 specialization, and the complete set of arguments used to
5692 specialize R. */
5693 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5694 argvec
5695 = tsubst_template_arg_vector (DECL_TI_ARGS
5696 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5697 args, /*complain=*/1);
5699 /* Check to see if we already have this specialization. */
5700 spec = retrieve_specialization (gen_tmpl, argvec);
5702 if (spec)
5704 r = spec;
5705 break;
5708 /* We can see more levels of arguments than parameters if
5709 there was a specialization of a member template, like
5710 this:
5712 template <class T> struct S { template <class U> void f(); }
5713 template <> template <class U> void S<int>::f(U);
5715 Here, we'll be substituting into the specialization,
5716 because that's where we can find the code we actually
5717 want to generate, but we'll have enough arguments for
5718 the most general template.
5720 We also deal with the peculiar case:
5722 template <class T> struct S {
5723 template <class U> friend void f();
5725 template <class U> void f() {}
5726 template S<int>;
5727 template void f<double>();
5729 Here, the ARGS for the instantiation of will be {int,
5730 double}. But, we only need as many ARGS as there are
5731 levels of template parameters in CODE_PATTERN. We are
5732 careful not to get fooled into reducing the ARGS in
5733 situations like:
5735 template <class T> struct S { template <class U> void f(U); }
5736 template <class T> template <> void S<T>::f(int) {}
5738 which we can spot because the pattern will be a
5739 specialization in this case. */
5740 args_depth = TMPL_ARGS_DEPTH (args);
5741 parms_depth =
5742 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5743 if (args_depth > parms_depth
5744 && !DECL_TEMPLATE_SPECIALIZATION (t))
5745 args = get_innermost_template_args (args, parms_depth);
5747 else
5749 /* This special case arises when we have something like this:
5751 template <class T> struct S {
5752 friend void f<int>(int, double);
5755 Here, the DECL_TI_TEMPLATE for the friend declaration
5756 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5757 being called from tsubst_friend_function, and we want
5758 only to create a new decl (R) with appropriate types so
5759 that we can call determine_specialization. */
5760 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5761 == LOOKUP_EXPR)
5762 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5763 == IDENTIFIER_NODE), 0);
5764 gen_tmpl = NULL_TREE;
5767 if (DECL_CLASS_SCOPE_P (t))
5769 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5770 member = 2;
5771 else
5772 member = 1;
5773 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5774 /*complain=*/1, t,
5775 /*entering_scope=*/1);
5777 else
5779 member = 0;
5780 ctx = DECL_CONTEXT (t);
5782 type = tsubst (type, args, /*complain=*/1, in_decl);
5783 if (type == error_mark_node)
5784 return error_mark_node;
5786 /* We do NOT check for matching decls pushed separately at this
5787 point, as they may not represent instantiations of this
5788 template, and in any case are considered separate under the
5789 discrete model. */
5790 r = copy_decl (t);
5791 DECL_USE_TEMPLATE (r) = 0;
5792 TREE_TYPE (r) = type;
5793 /* Clear out the mangled name and RTL for the instantiation. */
5794 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5795 SET_DECL_RTL (r, NULL_RTX);
5797 DECL_CONTEXT (r) = ctx;
5798 DECL_VIRTUAL_CONTEXT (r)
5799 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5800 /*complain=*/1, t,
5801 /*entering_scope=*/1);
5803 if (member && DECL_CONV_FN_P (r))
5804 /* Type-conversion operator. Reconstruct the name, in
5805 case it's the name of one of the template's parameters. */
5806 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5808 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5809 /*complain=*/1, t);
5810 DECL_RESULT (r) = NULL_TREE;
5812 TREE_STATIC (r) = 0;
5813 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5814 DECL_EXTERNAL (r) = 1;
5815 DECL_INTERFACE_KNOWN (r) = 0;
5816 DECL_DEFER_OUTPUT (r) = 0;
5817 TREE_CHAIN (r) = NULL_TREE;
5818 DECL_PENDING_INLINE_INFO (r) = 0;
5819 DECL_PENDING_INLINE_P (r) = 0;
5820 DECL_SAVED_TREE (r) = NULL_TREE;
5821 TREE_USED (r) = 0;
5822 if (DECL_CLONED_FUNCTION (r))
5824 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
5825 args, /*complain=*/1, t);
5826 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
5827 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
5830 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5831 this in the special friend case mentioned above where
5832 GEN_TMPL is NULL. */
5833 if (gen_tmpl)
5835 DECL_TEMPLATE_INFO (r)
5836 = tree_cons (gen_tmpl, argvec, NULL_TREE);
5837 SET_DECL_IMPLICIT_INSTANTIATION (r);
5838 register_specialization (r, gen_tmpl, argvec);
5840 /* We're not supposed to instantiate default arguments
5841 until they are called, for a template. But, for a
5842 declaration like:
5844 template <class T> void f ()
5845 { extern void g(int i = T()); }
5847 we should do the substitution when the template is
5848 instantiated. We handle the member function case in
5849 instantiate_class_template since the default arguments
5850 might refer to other members of the class. */
5851 if (!member
5852 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5853 && !uses_template_parms (argvec))
5854 tsubst_default_arguments (r);
5857 /* Copy the list of befriending classes. */
5858 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5859 *friends;
5860 friends = &TREE_CHAIN (*friends))
5862 *friends = copy_node (*friends);
5863 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5864 args, /*complain=*/1,
5865 in_decl);
5868 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
5870 maybe_retrofit_in_chrg (r);
5871 if (DECL_CONSTRUCTOR_P (r))
5872 grok_ctor_properties (ctx, r);
5873 /* If this is an instantiation of a member template, clone it.
5874 If it isn't, that'll be handled by
5875 clone_constructors_and_destructors. */
5876 if (PRIMARY_TEMPLATE_P (gen_tmpl))
5877 clone_function_decl(r, /*update_method_vec_p=*/0);
5879 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5880 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
5882 break;
5884 case PARM_DECL:
5886 r = copy_node (t);
5887 TREE_TYPE (r) = type;
5888 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5890 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5891 DECL_INITIAL (r) = TREE_TYPE (r);
5892 else
5893 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5894 /*complain=*/1, in_decl);
5896 DECL_CONTEXT (r) = NULL_TREE;
5897 if (PROMOTE_PROTOTYPES
5898 && INTEGRAL_TYPE_P (type)
5899 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5900 DECL_ARG_TYPE (r) = integer_type_node;
5901 if (TREE_CHAIN (t))
5902 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5903 /*complain=*/1, TREE_CHAIN (t));
5905 break;
5907 case FIELD_DECL:
5909 r = copy_decl (t);
5910 TREE_TYPE (r) = type;
5911 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5913 /* We don't have to set DECL_CONTEXT here; it is set by
5914 finish_member_declaration. */
5915 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
5916 /*complain=*/1, in_decl);
5917 TREE_CHAIN (r) = NULL_TREE;
5918 if (TREE_CODE (type) == VOID_TYPE)
5919 cp_error_at ("instantiation of `%D' as type void", r);
5921 break;
5923 case USING_DECL:
5925 r = copy_node (t);
5926 DECL_INITIAL (r)
5927 = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl);
5928 TREE_CHAIN (r) = NULL_TREE;
5930 break;
5932 case TYPE_DECL:
5933 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5934 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
5936 /* If this is the canonical decl, we don't have to mess with
5937 instantiations, and often we can't (for typename, template
5938 type parms and such). Note that TYPE_NAME is not correct for
5939 the above test if we've copied the type for a typedef. */
5940 r = TYPE_NAME (type);
5941 break;
5944 /* Fall through. */
5946 case VAR_DECL:
5948 tree argvec = NULL_TREE;
5949 tree gen_tmpl = NULL_TREE;
5950 tree spec;
5951 tree tmpl = NULL_TREE;
5952 tree ctx;
5953 int local_p;
5955 /* Assume this is a non-local variable. */
5956 local_p = 0;
5958 if (TYPE_P (CP_DECL_CONTEXT (t)))
5959 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5960 /*complain=*/1,
5961 in_decl, /*entering_scope=*/1);
5962 else if (DECL_NAMESPACE_SCOPE_P (t))
5963 ctx = DECL_CONTEXT (t);
5964 else
5966 /* Subsequent calls to pushdecl will fill this in. */
5967 ctx = NULL_TREE;
5968 local_p = 1;
5971 /* Check to see if we already have this specialization. */
5972 if (!local_p)
5974 tmpl = DECL_TI_TEMPLATE (t);
5975 gen_tmpl = most_general_template (tmpl);
5976 argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl);
5977 spec = retrieve_specialization (gen_tmpl, argvec);
5979 else
5980 spec = retrieve_local_specialization (t);
5982 if (spec)
5984 r = spec;
5985 break;
5988 r = copy_decl (t);
5989 TREE_TYPE (r) = type;
5990 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5991 DECL_CONTEXT (r) = ctx;
5992 /* Clear out the mangled name and RTL for the instantiation. */
5993 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5994 SET_DECL_RTL (r, NULL_RTX);
5996 /* Don't try to expand the initializer until someone tries to use
5997 this variable; otherwise we run into circular dependencies. */
5998 DECL_INITIAL (r) = NULL_TREE;
5999 SET_DECL_RTL (r, NULL_RTX);
6000 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6002 /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */
6003 if (DECL_PRETTY_FUNCTION_P (r))
6005 DECL_INITIAL (r) = tsubst (DECL_INITIAL (t),
6006 args,
6007 /*complain=*/1,
6008 NULL_TREE);
6009 TREE_TYPE (r) = TREE_TYPE (DECL_INITIAL (r));
6012 /* Even if the original location is out of scope, the newly
6013 substituted one is not. */
6014 if (TREE_CODE (r) == VAR_DECL)
6015 DECL_DEAD_FOR_LOCAL (r) = 0;
6017 if (!local_p)
6019 /* A static data member declaration is always marked
6020 external when it is declared in-class, even if an
6021 initializer is present. We mimic the non-template
6022 processing here. */
6023 DECL_EXTERNAL (r) = 1;
6025 register_specialization (r, gen_tmpl, argvec);
6026 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6027 SET_DECL_IMPLICIT_INSTANTIATION (r);
6029 else
6030 register_local_specialization (r, t);
6032 TREE_CHAIN (r) = NULL_TREE;
6033 if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE)
6034 cp_error_at ("instantiation of `%D' as type void", r);
6036 break;
6038 default:
6039 my_friendly_abort (0);
6042 /* Restore the file and line information. */
6043 lineno = saved_lineno;
6044 input_filename = saved_filename;
6046 return r;
6049 /* Substitue into the ARG_TYPES of a function type. */
6051 static tree
6052 tsubst_arg_types (arg_types, args, complain, in_decl)
6053 tree arg_types;
6054 tree args;
6055 int complain;
6056 tree in_decl;
6058 tree remaining_arg_types;
6059 tree type;
6061 if (!arg_types || arg_types == void_list_node)
6062 return arg_types;
6064 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6065 args, complain, in_decl);
6066 if (remaining_arg_types == error_mark_node)
6067 return error_mark_node;
6069 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6070 if (type == error_mark_node)
6071 return error_mark_node;
6072 if (VOID_TYPE_P (type))
6074 if (complain)
6076 cp_error ("invalid parameter type `%T'", type);
6077 if (in_decl)
6078 cp_error_at ("in declaration `%D'", in_decl);
6080 return error_mark_node;
6083 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6084 top-level qualifiers as required. */
6085 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6087 /* Note that we do not substitute into default arguments here. The
6088 standard mandates that they be instantiated only when needed,
6089 which is done in build_over_call. */
6090 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6091 remaining_arg_types);
6095 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6096 *not* handle the exception-specification for FNTYPE, because the
6097 initial substitution of explicitly provided template parameters
6098 during argument deduction forbids substitution into the
6099 exception-specification:
6101 [temp.deduct]
6103 All references in the function type of the function template to the
6104 corresponding template parameters are replaced by the specified tem-
6105 plate argument values. If a substitution in a template parameter or
6106 in the function type of the function template results in an invalid
6107 type, type deduction fails. [Note: The equivalent substitution in
6108 exception specifications is done only when the function is instanti-
6109 ated, at which point a program is ill-formed if the substitution
6110 results in an invalid type.] */
6112 static tree
6113 tsubst_function_type (t, args, complain, in_decl)
6114 tree t;
6115 tree args;
6116 int complain;
6117 tree in_decl;
6119 tree return_type;
6120 tree arg_types;
6121 tree fntype;
6123 /* The TYPE_CONTEXT is not used for function/method types. */
6124 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6126 /* Substitue the return type. */
6127 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6128 if (return_type == error_mark_node)
6129 return error_mark_node;
6131 /* Substitue the argument types. */
6132 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6133 complain, in_decl);
6134 if (arg_types == error_mark_node)
6135 return error_mark_node;
6137 /* Construct a new type node and return it. */
6138 if (TREE_CODE (t) == FUNCTION_TYPE)
6139 fntype = build_function_type (return_type, arg_types);
6140 else
6142 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6143 if (! IS_AGGR_TYPE (r))
6145 /* [temp.deduct]
6147 Type deduction may fail for any of the following
6148 reasons:
6150 -- Attempting to create "pointer to member of T" when T
6151 is not a class type. */
6152 if (complain)
6153 cp_error ("creating pointer to member function of non-class type `%T'",
6155 return error_mark_node;
6158 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6159 (arg_types));
6161 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
6162 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6164 return fntype;
6167 /* Substitute into the PARMS of a call-declarator. */
6169 static tree
6170 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6171 tree parms;
6172 tree args;
6173 int complain;
6174 tree in_decl;
6176 tree new_parms;
6177 tree type;
6178 tree defarg;
6180 if (!parms || parms == void_list_node)
6181 return parms;
6183 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6184 args, complain, in_decl);
6186 /* Figure out the type of this parameter. */
6187 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6189 /* Figure out the default argument as well. Note that we use
6190 tsubst_expr since the default argument is really an expression. */
6191 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6193 /* Chain this parameter on to the front of those we have already
6194 processed. We don't use hash_tree_cons because that function
6195 doesn't check TREE_PARMLIST. */
6196 new_parms = tree_cons (defarg, type, new_parms);
6198 /* And note that these are parameters. */
6199 TREE_PARMLIST (new_parms) = 1;
6201 return new_parms;
6204 /* Take the tree structure T and replace template parameters used
6205 therein with the argument vector ARGS. IN_DECL is an associated
6206 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6207 An appropriate error message is issued only if COMPLAIN is
6208 non-zero. Note that we must be relatively non-tolerant of
6209 extensions here, in order to preserve conformance; if we allow
6210 substitutions that should not be allowed, we may allow argument
6211 deductions that should not succeed, and therefore report ambiguous
6212 overload situations where there are none. In theory, we could
6213 allow the substitution, but indicate that it should have failed,
6214 and allow our caller to make sure that the right thing happens, but
6215 we don't try to do this yet.
6217 This function is used for dealing with types, decls and the like;
6218 for expressions, use tsubst_expr or tsubst_copy. */
6220 tree
6221 tsubst (t, args, complain, in_decl)
6222 tree t, args;
6223 int complain;
6224 tree in_decl;
6226 tree type, r;
6228 if (t == NULL_TREE || t == error_mark_node
6229 || t == integer_type_node
6230 || t == void_type_node
6231 || t == char_type_node
6232 || TREE_CODE (t) == NAMESPACE_DECL)
6233 return t;
6235 if (TREE_CODE (t) == IDENTIFIER_NODE)
6236 type = IDENTIFIER_TYPE_VALUE (t);
6237 else
6238 type = TREE_TYPE (t);
6239 if (type == unknown_type_node)
6240 my_friendly_abort (42);
6242 if (type && TREE_CODE (t) != FUNCTION_DECL
6243 && TREE_CODE (t) != TYPENAME_TYPE
6244 && TREE_CODE (t) != TEMPLATE_DECL
6245 && TREE_CODE (t) != IDENTIFIER_NODE
6246 && TREE_CODE (t) != FUNCTION_TYPE
6247 && TREE_CODE (t) != METHOD_TYPE)
6248 type = tsubst (type, args, complain, in_decl);
6249 if (type == error_mark_node)
6250 return error_mark_node;
6252 if (DECL_P (t))
6253 return tsubst_decl (t, args, type);
6255 switch (TREE_CODE (t))
6257 case RECORD_TYPE:
6258 case UNION_TYPE:
6259 case ENUMERAL_TYPE:
6260 return tsubst_aggr_type (t, args, complain, in_decl,
6261 /*entering_scope=*/0);
6263 case ERROR_MARK:
6264 case IDENTIFIER_NODE:
6265 case OP_IDENTIFIER:
6266 case VOID_TYPE:
6267 case REAL_TYPE:
6268 case COMPLEX_TYPE:
6269 case VECTOR_TYPE:
6270 case BOOLEAN_TYPE:
6271 case INTEGER_CST:
6272 case REAL_CST:
6273 case STRING_CST:
6274 return t;
6276 case INTEGER_TYPE:
6277 if (t == integer_type_node)
6278 return t;
6280 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6281 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6282 return t;
6285 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6287 max = tsubst_expr (omax, args, complain, in_decl);
6288 if (max == error_mark_node)
6289 return error_mark_node;
6291 /* See if we can reduce this expression to something simpler. */
6292 max = maybe_fold_nontype_arg (max);
6293 if (!processing_template_decl)
6294 max = decl_constant_value (max);
6296 if (processing_template_decl
6297 /* When providing explicit arguments to a template
6298 function, but leaving some arguments for subsequent
6299 deduction, MAX may be template-dependent even if we're
6300 not PROCESSING_TEMPLATE_DECL. We still need to check for
6301 template parms, though; MAX won't be an INTEGER_CST for
6302 dynamic arrays, either. */
6303 || (TREE_CODE (max) != INTEGER_CST
6304 && uses_template_parms (max)))
6306 tree itype = make_node (INTEGER_TYPE);
6307 TYPE_MIN_VALUE (itype) = size_zero_node;
6308 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6309 integer_one_node);
6310 return itype;
6313 if (integer_zerop (omax))
6315 /* Still allow an explicit array of size zero. */
6316 if (pedantic)
6317 pedwarn ("creating array with size zero");
6319 else if (integer_zerop (max)
6320 || (TREE_CODE (max) == INTEGER_CST
6321 && INT_CST_LT (max, integer_zero_node)))
6323 /* [temp.deduct]
6325 Type deduction may fail for any of the following
6326 reasons:
6328 Attempting to create an array with a size that is
6329 zero or negative. */
6330 if (complain)
6331 cp_error ("creating array with size zero (`%E')", max);
6333 return error_mark_node;
6336 return compute_array_index_type (NULL_TREE, max);
6339 case TEMPLATE_TYPE_PARM:
6340 case TEMPLATE_TEMPLATE_PARM:
6341 case BOUND_TEMPLATE_TEMPLATE_PARM:
6342 case TEMPLATE_PARM_INDEX:
6344 int idx;
6345 int level;
6346 int levels;
6348 r = NULL_TREE;
6350 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6351 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6352 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6354 idx = TEMPLATE_TYPE_IDX (t);
6355 level = TEMPLATE_TYPE_LEVEL (t);
6357 else
6359 idx = TEMPLATE_PARM_IDX (t);
6360 level = TEMPLATE_PARM_LEVEL (t);
6363 if (TREE_VEC_LENGTH (args) > 0)
6365 tree arg = NULL_TREE;
6367 levels = TMPL_ARGS_DEPTH (args);
6368 if (level <= levels)
6369 arg = TMPL_ARG (args, level, idx);
6371 if (arg == error_mark_node)
6372 return error_mark_node;
6373 else if (arg != NULL_TREE)
6375 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6377 my_friendly_assert (TYPE_P (arg), 0);
6378 return cp_build_qualified_type_real
6379 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t),
6380 complain);
6382 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6384 /* We are processing a type constructed from
6385 a template template parameter */
6386 tree argvec = tsubst (TYPE_TI_ARGS (t),
6387 args, complain, in_decl);
6388 if (argvec == error_mark_node)
6389 return error_mark_node;
6391 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6392 we are resolving nested-types in the signature of
6393 a member function templates.
6394 Otherwise ARG is a TEMPLATE_DECL and is the real
6395 template to be instantiated. */
6396 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6397 arg = TYPE_NAME (arg);
6399 r = lookup_template_class (arg,
6400 argvec, in_decl,
6401 DECL_CONTEXT (arg),
6402 /*entering_scope=*/0,
6403 complain);
6404 return cp_build_qualified_type_real (r,
6405 TYPE_QUALS (t),
6406 complain);
6408 else
6409 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6410 return arg;
6413 else
6414 my_friendly_abort (981018);
6416 if (level == 1)
6417 /* This can happen during the attempted tsubst'ing in
6418 unify. This means that we don't yet have any information
6419 about the template parameter in question. */
6420 return t;
6422 /* If we get here, we must have been looking at a parm for a
6423 more deeply nested template. Make a new version of this
6424 template parameter, but with a lower level. */
6425 switch (TREE_CODE (t))
6427 case TEMPLATE_TYPE_PARM:
6428 case TEMPLATE_TEMPLATE_PARM:
6429 case BOUND_TEMPLATE_TEMPLATE_PARM:
6430 if (CP_TYPE_QUALS (t))
6432 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6433 r = cp_build_qualified_type_real (r, CP_TYPE_QUALS (t),
6434 complain);
6436 else
6438 r = copy_node (t);
6439 TEMPLATE_TYPE_PARM_INDEX (r)
6440 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6441 r, levels);
6442 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6443 TYPE_MAIN_VARIANT (r) = r;
6444 TYPE_POINTER_TO (r) = NULL_TREE;
6445 TYPE_REFERENCE_TO (r) = NULL_TREE;
6447 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6449 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6450 complain, in_decl);
6451 if (argvec == error_mark_node)
6452 return error_mark_node;
6454 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6455 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6458 break;
6460 case TEMPLATE_PARM_INDEX:
6461 r = reduce_template_parm_level (t, type, levels);
6462 break;
6464 default:
6465 my_friendly_abort (0);
6468 return r;
6471 case TREE_LIST:
6473 tree purpose, value, chain, result;
6475 if (t == void_list_node)
6476 return t;
6478 purpose = TREE_PURPOSE (t);
6479 if (purpose)
6481 purpose = tsubst (purpose, args, complain, in_decl);
6482 if (purpose == error_mark_node)
6483 return error_mark_node;
6485 value = TREE_VALUE (t);
6486 if (value)
6488 value = tsubst (value, args, complain, in_decl);
6489 if (value == error_mark_node)
6490 return error_mark_node;
6492 chain = TREE_CHAIN (t);
6493 if (chain && chain != void_type_node)
6495 chain = tsubst (chain, args, complain, in_decl);
6496 if (chain == error_mark_node)
6497 return error_mark_node;
6499 if (purpose == TREE_PURPOSE (t)
6500 && value == TREE_VALUE (t)
6501 && chain == TREE_CHAIN (t))
6502 return t;
6503 if (TREE_PARMLIST (t))
6505 result = tree_cons (purpose, value, chain);
6506 TREE_PARMLIST (result) = 1;
6508 else
6509 result = hash_tree_cons (purpose, value, chain);
6510 return result;
6512 case TREE_VEC:
6513 if (type != NULL_TREE)
6515 /* A binfo node. We always need to make a copy, of the node
6516 itself and of its BINFO_BASETYPES. */
6518 t = copy_node (t);
6520 /* Make sure type isn't a typedef copy. */
6521 type = BINFO_TYPE (TYPE_BINFO (type));
6523 TREE_TYPE (t) = complete_type (type);
6524 if (IS_AGGR_TYPE (type))
6526 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6527 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6528 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6529 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6531 return t;
6534 /* Otherwise, a vector of template arguments. */
6535 return tsubst_template_arg_vector (t, args, complain);
6537 case POINTER_TYPE:
6538 case REFERENCE_TYPE:
6540 enum tree_code code;
6542 if (type == TREE_TYPE (t))
6543 return t;
6545 code = TREE_CODE (t);
6548 /* [temp.deduct]
6550 Type deduction may fail for any of the following
6551 reasons:
6553 -- Attempting to create a pointer to reference type.
6554 -- Attempting to create a reference to a reference type or
6555 a reference to void. */
6556 if (TREE_CODE (type) == REFERENCE_TYPE
6557 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6559 static int last_line = 0;
6560 static const char* last_file = 0;
6562 /* We keep track of the last time we issued this error
6563 message to avoid spewing a ton of messages during a
6564 single bad template instantiation. */
6565 if (complain && (last_line != lineno ||
6566 last_file != input_filename))
6568 if (TREE_CODE (type) == VOID_TYPE)
6569 cp_error ("forming reference to void");
6570 else
6571 cp_error ("forming %s to reference type `%T'",
6572 (code == POINTER_TYPE) ? "pointer" : "reference",
6573 type);
6574 last_line = lineno;
6575 last_file = input_filename;
6578 return error_mark_node;
6580 else if (code == POINTER_TYPE)
6581 r = build_pointer_type (type);
6582 else
6583 r = build_reference_type (type);
6584 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6586 if (r != error_mark_node)
6587 /* Will this ever be needed for TYPE_..._TO values? */
6588 layout_type (r);
6590 return r;
6592 case OFFSET_TYPE:
6594 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6595 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6597 /* [temp.deduct]
6599 Type deduction may fail for any of the following
6600 reasons:
6602 -- Attempting to create "pointer to member of T" when T
6603 is not a class type. */
6604 if (complain)
6605 cp_error ("creating pointer to member of non-class type `%T'",
6607 return error_mark_node;
6609 return build_offset_type (r, type);
6611 case FUNCTION_TYPE:
6612 case METHOD_TYPE:
6614 tree fntype;
6615 tree raises;
6617 fntype = tsubst_function_type (t, args, complain, in_decl);
6618 if (fntype == error_mark_node)
6619 return error_mark_node;
6621 /* Substitue the exception specification. */
6622 raises = TYPE_RAISES_EXCEPTIONS (t);
6623 if (raises)
6625 tree list = NULL_TREE;
6627 if (! TREE_VALUE (raises))
6628 list = raises;
6629 else
6630 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6632 tree spec = TREE_VALUE (raises);
6634 spec = tsubst (spec, args, complain, in_decl);
6635 if (spec == error_mark_node)
6636 return spec;
6637 list = add_exception_specifier (list, spec, complain);
6639 fntype = build_exception_variant (fntype, list);
6641 return fntype;
6643 case ARRAY_TYPE:
6645 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6646 if (domain == error_mark_node)
6647 return error_mark_node;
6649 /* As an optimization, we avoid regenerating the array type if
6650 it will obviously be the same as T. */
6651 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6652 return t;
6654 /* These checks should match the ones in grokdeclarator.
6656 [temp.deduct]
6658 The deduction may fail for any of the following reasons:
6660 -- Attempting to create an array with an element type that
6661 is void, a function type, or a reference type. */
6662 if (TREE_CODE (type) == VOID_TYPE
6663 || TREE_CODE (type) == FUNCTION_TYPE
6664 || TREE_CODE (type) == REFERENCE_TYPE)
6666 if (complain)
6667 cp_error ("creating array of `%T'", type);
6668 return error_mark_node;
6671 r = build_cplus_array_type (type, domain);
6672 return r;
6675 case PLUS_EXPR:
6676 case MINUS_EXPR:
6678 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6679 in_decl);
6680 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain,
6681 in_decl);
6683 if (e1 == error_mark_node || e2 == error_mark_node)
6684 return error_mark_node;
6686 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6689 case NEGATE_EXPR:
6690 case NOP_EXPR:
6692 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6693 in_decl);
6694 if (e == error_mark_node)
6695 return error_mark_node;
6697 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6700 case TYPENAME_TYPE:
6702 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6703 in_decl, /*entering_scope=*/1);
6704 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6705 complain, in_decl);
6707 if (ctx == error_mark_node || f == error_mark_node)
6708 return error_mark_node;
6710 if (!IS_AGGR_TYPE (ctx))
6712 if (complain)
6713 cp_error ("`%T' is not a class, struct, or union type",
6714 ctx);
6715 return error_mark_node;
6717 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6719 /* Normally, make_typename_type does not require that the CTX
6720 have complete type in order to allow things like:
6722 template <class T> struct S { typename S<T>::X Y; };
6724 But, such constructs have already been resolved by this
6725 point, so here CTX really should have complete type, unless
6726 it's a partial instantiation. */
6727 ctx = complete_type (ctx);
6728 if (!COMPLETE_TYPE_P (ctx))
6730 if (complain)
6731 incomplete_type_error (NULL_TREE, ctx);
6732 return error_mark_node;
6736 f = make_typename_type (ctx, f, complain);
6737 if (f == error_mark_node)
6738 return f;
6739 return cp_build_qualified_type_real (f,
6740 CP_TYPE_QUALS (f)
6741 | CP_TYPE_QUALS (t),
6742 complain);
6745 case INDIRECT_REF:
6747 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6748 in_decl);
6749 if (e == error_mark_node)
6750 return error_mark_node;
6751 return make_pointer_declarator (type, e);
6754 case ADDR_EXPR:
6756 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6757 in_decl);
6758 if (e == error_mark_node)
6759 return error_mark_node;
6760 return make_reference_declarator (type, e);
6763 case ARRAY_REF:
6765 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6766 in_decl);
6767 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain,
6768 in_decl);
6769 if (e1 == error_mark_node || e2 == error_mark_node)
6770 return error_mark_node;
6772 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
6775 case CALL_EXPR:
6777 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6778 in_decl);
6779 tree e2 = (tsubst_call_declarator_parms
6780 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6781 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6782 complain, in_decl);
6784 if (e1 == error_mark_node || e2 == error_mark_node
6785 || e3 == error_mark_node)
6786 return error_mark_node;
6788 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
6791 case SCOPE_REF:
6793 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6794 in_decl);
6795 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6796 if (e1 == error_mark_node || e2 == error_mark_node)
6797 return error_mark_node;
6799 return build_nt (TREE_CODE (t), e1, e2);
6802 case TYPEOF_TYPE:
6804 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain,
6805 in_decl);
6806 if (e1 == error_mark_node)
6807 return error_mark_node;
6809 return TREE_TYPE (e1);
6812 case FUNCTION_NAME:
6814 const char *name;
6815 int len;
6816 tree type;
6817 tree str;
6819 /* This code should match declare_hidden_char_array in
6820 c-common.c. */
6821 name = (*decl_printable_name) (current_function_decl, 2);
6822 len = strlen (name) + 1;
6823 type = build_array_type (char_type_node,
6824 build_index_type (size_int (len)));
6825 str = build_string (len, name);
6826 TREE_TYPE (str) = type;
6827 return str;
6830 default:
6831 sorry ("use of `%s' in template",
6832 tree_code_name [(int) TREE_CODE (t)]);
6833 return error_mark_node;
6837 /* Like tsubst, but deals with expressions. This function just replaces
6838 template parms; to finish processing the resultant expression, use
6839 tsubst_expr. */
6841 tree
6842 tsubst_copy (t, args, complain, in_decl)
6843 tree t, args;
6844 int complain;
6845 tree in_decl;
6847 enum tree_code code;
6848 tree r;
6850 if (t == NULL_TREE || t == error_mark_node)
6851 return t;
6853 code = TREE_CODE (t);
6855 switch (code)
6857 case PARM_DECL:
6858 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6860 case CONST_DECL:
6862 tree enum_type;
6863 tree v;
6865 if (!DECL_CONTEXT (t))
6866 /* This is a global enumeration constant. */
6867 return t;
6869 /* Unfortunately, we cannot just call lookup_name here.
6870 Consider:
6872 template <int I> int f() {
6873 enum E { a = I };
6874 struct S { void g() { E e = a; } };
6877 When we instantiate f<7>::S::g(), say, lookup_name is not
6878 clever enough to find f<7>::a. */
6879 enum_type
6880 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
6881 /*entering_scope=*/0);
6883 for (v = TYPE_VALUES (enum_type);
6884 v != NULL_TREE;
6885 v = TREE_CHAIN (v))
6886 if (TREE_PURPOSE (v) == DECL_NAME (t))
6887 return TREE_VALUE (v);
6889 /* We didn't find the name. That should never happen; if
6890 name-lookup found it during preliminary parsing, we
6891 should find it again here during instantiation. */
6892 my_friendly_abort (0);
6894 return t;
6896 case FIELD_DECL:
6897 if (DECL_CONTEXT (t))
6899 tree ctx;
6901 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
6902 /*entering_scope=*/1);
6903 if (ctx != DECL_CONTEXT (t))
6904 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6906 return t;
6908 case VAR_DECL:
6909 case FUNCTION_DECL:
6910 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
6911 t = tsubst (t, args, complain, in_decl);
6912 mark_used (t);
6913 return t;
6915 case TEMPLATE_DECL:
6916 if (is_member_template (t))
6917 return tsubst (t, args, complain, in_decl);
6918 else
6919 return t;
6921 case LOOKUP_EXPR:
6923 /* We must tsubst into a LOOKUP_EXPR in case the names to
6924 which it refers is a conversion operator; in that case the
6925 name will change. We avoid making unnecessary copies,
6926 however. */
6928 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
6930 if (id != TREE_OPERAND (t, 0))
6932 r = build_nt (LOOKUP_EXPR, id);
6933 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6934 t = r;
6937 return t;
6940 case CAST_EXPR:
6941 case REINTERPRET_CAST_EXPR:
6942 case CONST_CAST_EXPR:
6943 case STATIC_CAST_EXPR:
6944 case DYNAMIC_CAST_EXPR:
6945 case NOP_EXPR:
6946 return build1
6947 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6948 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
6950 case INDIRECT_REF:
6951 case NEGATE_EXPR:
6952 case TRUTH_NOT_EXPR:
6953 case BIT_NOT_EXPR:
6954 case ADDR_EXPR:
6955 case CONVERT_EXPR: /* Unary + */
6956 case SIZEOF_EXPR:
6957 case ALIGNOF_EXPR:
6958 case ARROW_EXPR:
6959 case THROW_EXPR:
6960 case TYPEID_EXPR:
6961 case REALPART_EXPR:
6962 case IMAGPART_EXPR:
6963 return build1
6964 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6965 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
6967 case PLUS_EXPR:
6968 case MINUS_EXPR:
6969 case MULT_EXPR:
6970 case TRUNC_DIV_EXPR:
6971 case CEIL_DIV_EXPR:
6972 case FLOOR_DIV_EXPR:
6973 case ROUND_DIV_EXPR:
6974 case EXACT_DIV_EXPR:
6975 case BIT_AND_EXPR:
6976 case BIT_ANDTC_EXPR:
6977 case BIT_IOR_EXPR:
6978 case BIT_XOR_EXPR:
6979 case TRUNC_MOD_EXPR:
6980 case FLOOR_MOD_EXPR:
6981 case TRUTH_ANDIF_EXPR:
6982 case TRUTH_ORIF_EXPR:
6983 case TRUTH_AND_EXPR:
6984 case TRUTH_OR_EXPR:
6985 case RSHIFT_EXPR:
6986 case LSHIFT_EXPR:
6987 case RROTATE_EXPR:
6988 case LROTATE_EXPR:
6989 case EQ_EXPR:
6990 case NE_EXPR:
6991 case MAX_EXPR:
6992 case MIN_EXPR:
6993 case LE_EXPR:
6994 case GE_EXPR:
6995 case LT_EXPR:
6996 case GT_EXPR:
6997 case COMPONENT_REF:
6998 case ARRAY_REF:
6999 case COMPOUND_EXPR:
7000 case SCOPE_REF:
7001 case DOTSTAR_EXPR:
7002 case MEMBER_REF:
7003 case PREDECREMENT_EXPR:
7004 case PREINCREMENT_EXPR:
7005 case POSTDECREMENT_EXPR:
7006 case POSTINCREMENT_EXPR:
7007 return build_nt
7008 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7009 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7011 case CALL_EXPR:
7013 tree fn = TREE_OPERAND (t, 0);
7014 if (is_overloaded_fn (fn))
7015 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
7016 else
7017 /* Sometimes FN is a LOOKUP_EXPR. */
7018 fn = tsubst_copy (fn, args, complain, in_decl);
7019 return build_nt
7020 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7021 in_decl),
7022 NULL_TREE);
7025 case METHOD_CALL_EXPR:
7027 tree name = TREE_OPERAND (t, 0);
7028 if (TREE_CODE (name) == BIT_NOT_EXPR)
7030 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7031 complain, in_decl);
7032 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7034 else if (TREE_CODE (name) == SCOPE_REF
7035 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7037 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7038 complain, in_decl);
7039 name = TREE_OPERAND (name, 1);
7040 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7041 complain, in_decl);
7042 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7043 name = build_nt (SCOPE_REF, base, name);
7045 else
7046 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7047 return build_nt
7048 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7049 complain, in_decl),
7050 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7051 NULL_TREE);
7054 case STMT_EXPR:
7055 /* This processing should really occur in tsubst_expr, However,
7056 tsubst_expr does not recurse into expressions, since it
7057 assumes that there aren't any statements inside them.
7058 Instead, it simply calls build_expr_from_tree. So, we need
7059 to expand the STMT_EXPR here. */
7060 if (!processing_template_decl)
7062 tree stmt_expr = begin_stmt_expr ();
7063 tsubst_expr (STMT_EXPR_STMT (t), args,
7064 complain, in_decl);
7065 return finish_stmt_expr (stmt_expr);
7068 return t;
7070 case COND_EXPR:
7071 case MODOP_EXPR:
7072 case PSEUDO_DTOR_EXPR:
7074 r = build_nt
7075 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7076 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7077 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7078 return r;
7081 case NEW_EXPR:
7083 r = build_nt
7084 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7085 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7086 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7087 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7088 return r;
7091 case DELETE_EXPR:
7093 r = build_nt
7094 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7095 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7096 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7097 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7098 return r;
7101 case TEMPLATE_ID_EXPR:
7103 /* Substituted template arguments */
7104 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7105 in_decl);
7107 if (targs && TREE_CODE (targs) == TREE_LIST)
7109 tree chain;
7110 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7111 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7113 else if (targs)
7115 int i;
7116 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7117 TREE_VEC_ELT (targs, i)
7118 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7121 return lookup_template_function
7122 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7125 case TREE_LIST:
7127 tree purpose, value, chain;
7129 if (t == void_list_node)
7130 return t;
7132 purpose = TREE_PURPOSE (t);
7133 if (purpose)
7134 purpose = tsubst_copy (purpose, args, complain, in_decl);
7135 value = TREE_VALUE (t);
7136 if (value)
7137 value = tsubst_copy (value, args, complain, in_decl);
7138 chain = TREE_CHAIN (t);
7139 if (chain && chain != void_type_node)
7140 chain = tsubst_copy (chain, args, complain, in_decl);
7141 if (purpose == TREE_PURPOSE (t)
7142 && value == TREE_VALUE (t)
7143 && chain == TREE_CHAIN (t))
7144 return t;
7145 return tree_cons (purpose, value, chain);
7148 case RECORD_TYPE:
7149 case UNION_TYPE:
7150 case ENUMERAL_TYPE:
7151 case INTEGER_TYPE:
7152 case TEMPLATE_TYPE_PARM:
7153 case TEMPLATE_TEMPLATE_PARM:
7154 case BOUND_TEMPLATE_TEMPLATE_PARM:
7155 case TEMPLATE_PARM_INDEX:
7156 case POINTER_TYPE:
7157 case REFERENCE_TYPE:
7158 case OFFSET_TYPE:
7159 case FUNCTION_TYPE:
7160 case METHOD_TYPE:
7161 case ARRAY_TYPE:
7162 case TYPENAME_TYPE:
7163 case TYPE_DECL:
7164 return tsubst (t, args, complain, in_decl);
7166 case IDENTIFIER_NODE:
7167 if (IDENTIFIER_TYPENAME_P (t))
7169 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7170 return mangle_conv_op_name_for_type (new_type);
7172 else
7173 return t;
7175 case CONSTRUCTOR:
7177 r = build
7178 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7179 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7180 complain, in_decl));
7181 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7182 return r;
7185 case VA_ARG_EXPR:
7186 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7187 in_decl),
7188 tsubst (TREE_TYPE (t), args, complain, in_decl));
7190 case FUNCTION_NAME:
7191 return tsubst (t, args, complain, in_decl);
7193 default:
7194 return t;
7198 /* Like tsubst_copy, but also does semantic processing. */
7200 tree
7201 tsubst_expr (t, args, complain, in_decl)
7202 tree t, args;
7203 int complain;
7204 tree in_decl;
7206 tree stmt;
7208 if (t == NULL_TREE || t == error_mark_node)
7209 return t;
7211 if (processing_template_decl)
7212 return tsubst_copy (t, args, complain, in_decl);
7214 switch (TREE_CODE (t))
7216 case RETURN_INIT:
7217 prep_stmt (t);
7218 finish_named_return_value
7219 (TREE_OPERAND (t, 0),
7220 tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl));
7221 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7222 break;
7224 case CTOR_INITIALIZER:
7226 tree member_init_list;
7227 tree base_init_list;
7229 prep_stmt (t);
7230 member_init_list
7231 = tsubst_initializer_list (TREE_OPERAND (t, 0), args);
7232 base_init_list
7233 = tsubst_initializer_list (TREE_OPERAND (t, 1), args);
7234 setup_vtbl_ptr (member_init_list, base_init_list);
7235 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7236 break;
7239 case RETURN_STMT:
7240 prep_stmt (t);
7241 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
7242 args, complain, in_decl));
7243 break;
7245 case EXPR_STMT:
7246 prep_stmt (t);
7247 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7248 args, complain, in_decl));
7249 break;
7251 case USING_STMT:
7252 prep_stmt (t);
7253 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7254 args, complain, in_decl));
7255 break;
7257 case DECL_STMT:
7259 tree decl;
7260 tree init;
7262 prep_stmt (t);
7263 decl = DECL_STMT_DECL (t);
7264 if (TREE_CODE (decl) == LABEL_DECL)
7265 finish_label_decl (DECL_NAME (decl));
7266 else if (TREE_CODE (decl) == USING_DECL)
7268 tree scope = DECL_INITIAL (decl);
7269 tree name = DECL_NAME (decl);
7271 scope = tsubst_expr (scope, args, complain, in_decl);
7272 do_local_using_decl (build_nt (SCOPE_REF, scope, name));
7274 else
7276 init = DECL_INITIAL (decl);
7277 decl = tsubst (decl, args, complain, in_decl);
7278 init = tsubst_expr (init, args, complain, in_decl);
7279 if (decl != error_mark_node)
7281 if (TREE_CODE (decl) != TYPE_DECL)
7282 /* Make sure the type is instantiated now. */
7283 complete_type (TREE_TYPE (decl));
7284 if (init)
7285 DECL_INITIAL (decl) = error_mark_node;
7286 /* By marking the declaration as instantiated, we avoid
7287 trying to instantiate it. Since instantiate_decl can't
7288 handle local variables, and since we've already done
7289 all that needs to be done, that's the right thing to
7290 do. */
7291 if (TREE_CODE (decl) == VAR_DECL)
7292 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7293 maybe_push_decl (decl);
7294 cp_finish_decl (decl, init, NULL_TREE, 0);
7297 return decl;
7300 case FOR_STMT:
7302 tree tmp;
7303 prep_stmt (t);
7305 stmt = begin_for_stmt ();
7306 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
7307 tsubst_expr (tmp, args, complain, in_decl);
7308 finish_for_init_stmt (stmt);
7309 finish_for_cond (tsubst_expr (FOR_COND (t), args,
7310 complain, in_decl),
7311 stmt);
7312 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7313 finish_for_expr (tmp, stmt);
7314 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7315 finish_for_stmt (stmt);
7317 break;
7319 case WHILE_STMT:
7321 prep_stmt (t);
7322 stmt = begin_while_stmt ();
7323 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7324 args, complain, in_decl),
7325 stmt);
7326 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7327 finish_while_stmt (stmt);
7329 break;
7331 case DO_STMT:
7333 prep_stmt (t);
7334 stmt = begin_do_stmt ();
7335 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7336 finish_do_body (stmt);
7337 finish_do_stmt (tsubst_expr (DO_COND (t), args,
7338 complain, in_decl),
7339 stmt);
7341 break;
7343 case IF_STMT:
7345 tree tmp;
7347 prep_stmt (t);
7348 stmt = begin_if_stmt ();
7349 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7350 args, complain, in_decl),
7351 stmt);
7353 if (tmp = THEN_CLAUSE (t), tmp)
7355 tsubst_expr (tmp, args, complain, in_decl);
7356 finish_then_clause (stmt);
7359 if (tmp = ELSE_CLAUSE (t), tmp)
7361 begin_else_clause ();
7362 tsubst_expr (tmp, args, complain, in_decl);
7363 finish_else_clause (stmt);
7366 finish_if_stmt ();
7368 break;
7370 case COMPOUND_STMT:
7372 tree substmt;
7374 prep_stmt (t);
7375 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7376 for (substmt = COMPOUND_BODY (t);
7377 substmt != NULL_TREE;
7378 substmt = TREE_CHAIN (substmt))
7379 tsubst_expr (substmt, args, complain, in_decl);
7380 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7382 break;
7384 case BREAK_STMT:
7385 prep_stmt (t);
7386 finish_break_stmt ();
7387 break;
7389 case CONTINUE_STMT:
7390 prep_stmt (t);
7391 finish_continue_stmt ();
7392 break;
7394 case SWITCH_STMT:
7396 tree val;
7398 prep_stmt (t);
7399 stmt = begin_switch_stmt ();
7400 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7401 finish_switch_cond (val, stmt);
7402 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7403 finish_switch_stmt (stmt);
7405 break;
7407 case CASE_LABEL:
7408 prep_stmt (t);
7409 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7410 tsubst_expr (CASE_HIGH (t), args, complain,
7411 in_decl));
7412 break;
7414 case LABEL_STMT:
7415 lineno = STMT_LINENO (t);
7416 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7417 break;
7419 case GOTO_STMT:
7420 prep_stmt (t);
7421 t = GOTO_DESTINATION (t);
7422 if (TREE_CODE (t) != LABEL_DECL)
7423 /* Computed goto's must be tsubst'd into. On the other hand,
7424 non-computed gotos must not be; the identifier in question
7425 will have no binding. */
7426 t = tsubst_expr (t, args, complain, in_decl);
7427 else
7428 t = DECL_NAME (t);
7429 finish_goto_stmt (t);
7430 break;
7432 case ASM_STMT:
7433 prep_stmt (t);
7434 finish_asm_stmt (ASM_CV_QUAL (t),
7435 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7436 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7437 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7438 tsubst_expr (ASM_CLOBBERS (t), args, complain,
7439 in_decl));
7440 break;
7442 case TRY_BLOCK:
7443 prep_stmt (t);
7444 if (CLEANUP_P (t))
7446 stmt = begin_try_block ();
7447 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7448 finish_cleanup_try_block (stmt);
7449 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7450 complain, in_decl),
7451 stmt);
7453 else
7455 tree handler;
7457 if (FN_TRY_BLOCK_P (t))
7458 stmt = begin_function_try_block ();
7459 else
7460 stmt = begin_try_block ();
7462 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7464 if (FN_TRY_BLOCK_P (t))
7465 finish_function_try_block (stmt);
7466 else
7467 finish_try_block (stmt);
7469 handler = TRY_HANDLERS (t);
7470 for (; handler; handler = TREE_CHAIN (handler))
7471 tsubst_expr (handler, args, complain, in_decl);
7472 if (FN_TRY_BLOCK_P (t))
7473 finish_function_handler_sequence (stmt);
7474 else
7475 finish_handler_sequence (stmt);
7477 break;
7479 case HANDLER:
7481 tree decl;
7482 tree blocks;
7484 prep_stmt (t);
7485 stmt = begin_handler ();
7486 if (HANDLER_PARMS (t))
7488 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7489 decl = tsubst (decl, args, complain, in_decl);
7490 /* Prevent instantiate_decl from trying to instantiate
7491 this variable. We've already done all that needs to be
7492 done. */
7493 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7495 else
7496 decl = NULL_TREE;
7497 blocks = finish_handler_parms (decl, stmt);
7498 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7499 finish_handler (blocks, stmt);
7501 break;
7503 case TAG_DEFN:
7504 prep_stmt (t);
7505 t = TREE_TYPE (t);
7506 tsubst (t, args, complain, NULL_TREE);
7507 break;
7509 default:
7510 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7512 return NULL_TREE;
7515 /* TMPL is a TEMPLATE_DECL for a cloned constructor or destructor.
7516 Instantiate it with the ARGS. */
7518 static tree
7519 instantiate_clone (tmpl, args)
7520 tree tmpl;
7521 tree args;
7523 tree spec;
7524 tree clone;
7526 /* Instantiated the cloned function, rather than the clone. */
7527 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), args);
7529 /* Then, see if we've already cloned the instantiation. */
7530 for (clone = TREE_CHAIN (spec);
7531 clone && DECL_CLONED_FUNCTION_P (clone);
7532 clone = TREE_CHAIN (clone))
7533 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7534 return clone;
7536 /* If we haven't, do so know. */
7537 if (!clone)
7538 clone_function_decl (spec, /*update_method_vec_p=*/0);
7540 /* Look again. */
7541 for (clone = TREE_CHAIN (spec);
7542 clone && DECL_CLONED_FUNCTION_P (clone);
7543 clone = TREE_CHAIN (clone))
7544 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7545 return clone;
7547 /* We should always have found the clone by now. */
7548 my_friendly_abort (20000411);
7549 return NULL_TREE;
7552 /* Instantiate the indicated variable or function template TMPL with
7553 the template arguments in TARG_PTR. */
7555 tree
7556 instantiate_template (tmpl, targ_ptr)
7557 tree tmpl, targ_ptr;
7559 tree clone;
7560 tree fndecl;
7561 tree gen_tmpl;
7562 tree spec;
7563 int i, len;
7564 tree inner_args;
7566 if (tmpl == error_mark_node)
7567 return error_mark_node;
7569 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7571 /* If this function is a clone, handle it specially. */
7572 if (DECL_CLONED_FUNCTION_P (tmpl))
7573 return instantiate_clone (tmpl, targ_ptr);
7575 /* Check to see if we already have this specialization. */
7576 spec = retrieve_specialization (tmpl, targ_ptr);
7577 if (spec != NULL_TREE)
7578 return spec;
7580 gen_tmpl = most_general_template (tmpl);
7581 if (tmpl != gen_tmpl)
7583 /* The TMPL is a partial instantiation. To get a full set of
7584 arguments we must add the arguments used to perform the
7585 partial instantiation. */
7586 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7587 targ_ptr);
7589 /* Check to see if we already have this specialization. */
7590 spec = retrieve_specialization (gen_tmpl, targ_ptr);
7591 if (spec != NULL_TREE)
7592 return spec;
7595 len = DECL_NTPARMS (gen_tmpl);
7596 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
7597 i = len;
7598 while (i--)
7600 tree t = TREE_VEC_ELT (inner_args, i);
7601 if (TYPE_P (t))
7603 tree nt = target_type (t);
7604 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
7606 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
7607 cp_error (" trying to instantiate `%D'", gen_tmpl);
7608 return error_mark_node;
7613 /* substitute template parameters */
7614 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
7615 targ_ptr, /*complain=*/1, gen_tmpl);
7616 /* The DECL_TI_TEMPLATE should always be the immediate parent
7617 template, not the most general template. */
7618 DECL_TI_TEMPLATE (fndecl) = tmpl;
7620 if (flag_external_templates)
7621 add_pending_template (fndecl);
7623 /* If we've just instantiated the main entry point for a function,
7624 instantiate all the alternate entry points as well. */
7625 for (clone = TREE_CHAIN (gen_tmpl);
7626 clone && DECL_CLONED_FUNCTION_P (clone);
7627 clone = TREE_CHAIN (clone))
7628 instantiate_template (clone, targ_ptr);
7630 return fndecl;
7633 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7634 arguments that are being used when calling it. TARGS is a vector
7635 into which the deduced template arguments are placed.
7637 Return zero for success, 2 for an incomplete match that doesn't resolve
7638 all the types, and 1 for complete failure. An error message will be
7639 printed only for an incomplete match.
7641 If FN is a conversion operator, or we are trying to produce a specific
7642 specialization, RETURN_TYPE is the return type desired.
7644 The EXPLICIT_TARGS are explicit template arguments provided via a
7645 template-id.
7647 The parameter STRICT is one of:
7649 DEDUCE_CALL:
7650 We are deducing arguments for a function call, as in
7651 [temp.deduct.call].
7653 DEDUCE_CONV:
7654 We are deducing arguments for a conversion function, as in
7655 [temp.deduct.conv].
7657 DEDUCE_EXACT:
7658 We are deducing arguments when doing an explicit instantiation
7659 as in [temp.explicit], when determining an explicit specialization
7660 as in [temp.expl.spec], or when taking the address of a function
7661 template, as in [temp.deduct.funcaddr].
7663 DEDUCE_ORDER:
7664 We are deducing arguments when calculating the partial
7665 ordering between specializations of function or class
7666 templates, as in [temp.func.order] and [temp.class.order].
7668 LEN is the number of parms to consider before returning success, or -1
7669 for all. This is used in partial ordering to avoid comparing parms for
7670 which no actual argument was passed, since they are not considered in
7671 overload resolution (and are explicitly excluded from consideration in
7672 partial ordering in [temp.func.order]/6). */
7675 fn_type_unification (fn, explicit_targs, targs, args, return_type,
7676 strict, len)
7677 tree fn, explicit_targs, targs, args, return_type;
7678 unification_kind_t strict;
7679 int len;
7681 tree parms;
7682 tree fntype;
7683 int result;
7685 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7687 fntype = TREE_TYPE (fn);
7688 if (explicit_targs)
7690 /* [temp.deduct]
7692 The specified template arguments must match the template
7693 parameters in kind (i.e., type, nontype, template), and there
7694 must not be more arguments than there are parameters;
7695 otherwise type deduction fails.
7697 Nontype arguments must match the types of the corresponding
7698 nontype template parameters, or must be convertible to the
7699 types of the corresponding nontype parameters as specified in
7700 _temp.arg.nontype_, otherwise type deduction fails.
7702 All references in the function type of the function template
7703 to the corresponding template parameters are replaced by the
7704 specified template argument values. If a substitution in a
7705 template parameter or in the function type of the function
7706 template results in an invalid type, type deduction fails. */
7707 int i;
7708 tree converted_args;
7710 converted_args
7711 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7712 explicit_targs, NULL_TREE, /*complain=*/0,
7713 /*require_all_arguments=*/0));
7714 if (converted_args == error_mark_node)
7715 return 1;
7717 fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE);
7718 if (fntype == error_mark_node)
7719 return 1;
7721 /* Place the explicitly specified arguments in TARGS. */
7722 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7723 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
7726 parms = TYPE_ARG_TYPES (fntype);
7727 /* Never do unification on the 'this' parameter. */
7728 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
7729 parms = TREE_CHAIN (parms);
7731 if (return_type)
7733 /* We've been given a return type to match, prepend it. */
7734 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
7735 args = tree_cons (NULL_TREE, return_type, args);
7736 if (len >= 0)
7737 ++len;
7740 /* We allow incomplete unification without an error message here
7741 because the standard doesn't seem to explicitly prohibit it. Our
7742 callers must be ready to deal with unification failures in any
7743 event. */
7744 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7745 targs, parms, args, /*subr=*/0,
7746 strict, /*allow_incomplete*/1, len);
7748 if (result == 0)
7749 /* All is well so far. Now, check:
7751 [temp.deduct]
7753 When all template arguments have been deduced, all uses of
7754 template parameters in nondeduced contexts are replaced with
7755 the corresponding deduced argument values. If the
7756 substitution results in an invalid type, as described above,
7757 type deduction fails. */
7758 if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE)
7759 == error_mark_node)
7760 return 1;
7762 return result;
7765 /* Adjust types before performing type deduction, as described in
7766 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7767 sections are symmetric. PARM is the type of a function parameter
7768 or the return type of the conversion function. ARG is the type of
7769 the argument passed to the call, or the type of the value
7770 initialized with the result of the conversion function. */
7772 static int
7773 maybe_adjust_types_for_deduction (strict, parm, arg)
7774 unification_kind_t strict;
7775 tree* parm;
7776 tree* arg;
7778 int result = 0;
7780 switch (strict)
7782 case DEDUCE_CALL:
7783 break;
7785 case DEDUCE_CONV:
7787 /* Swap PARM and ARG throughout the remainder of this
7788 function; the handling is precisely symmetric since PARM
7789 will initialize ARG rather than vice versa. */
7790 tree* temp = parm;
7791 parm = arg;
7792 arg = temp;
7793 break;
7796 case DEDUCE_EXACT:
7797 /* There is nothing to do in this case. */
7798 return 0;
7800 case DEDUCE_ORDER:
7801 /* DR 214. [temp.func.order] is underspecified, and leads to no
7802 ordering between things like `T *' and `T const &' for `U *'.
7803 The former has T=U and the latter T=U*. The former looks more
7804 specialized and John Spicer considers it well-formed (the EDG
7805 compiler accepts it).
7807 John also confirms that deduction should proceed as in a function
7808 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
7809 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7810 to an actual call can have such a type.
7812 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7813 If only ARG is a REFERENCE_TYPE, we look through that and then
7814 proceed as with DEDUCE_CALL (which could further convert it). */
7815 if (TREE_CODE (*arg) == REFERENCE_TYPE)
7817 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7818 return 0;
7819 *arg = TREE_TYPE (*arg);
7821 break;
7822 default:
7823 my_friendly_abort (0);
7826 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7828 /* [temp.deduct.call]
7830 If P is not a reference type:
7832 --If A is an array type, the pointer type produced by the
7833 array-to-pointer standard conversion (_conv.array_) is
7834 used in place of A for type deduction; otherwise,
7836 --If A is a function type, the pointer type produced by
7837 the function-to-pointer standard conversion
7838 (_conv.func_) is used in place of A for type deduction;
7839 otherwise,
7841 --If A is a cv-qualified type, the top level
7842 cv-qualifiers of A's type are ignored for type
7843 deduction. */
7844 if (TREE_CODE (*arg) == ARRAY_TYPE)
7845 *arg = build_pointer_type (TREE_TYPE (*arg));
7846 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
7847 *arg = build_pointer_type (*arg);
7848 else
7849 *arg = TYPE_MAIN_VARIANT (*arg);
7852 /* [temp.deduct.call]
7854 If P is a cv-qualified type, the top level cv-qualifiers
7855 of P's type are ignored for type deduction. If P is a
7856 reference type, the type referred to by P is used for
7857 type deduction. */
7858 *parm = TYPE_MAIN_VARIANT (*parm);
7859 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7861 *parm = TREE_TYPE (*parm);
7862 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
7864 return result;
7867 /* Most parms like fn_type_unification.
7869 If SUBR is 1, we're being called recursively (to unify the
7870 arguments of a function or method parameter of a function
7871 template). */
7873 static int
7874 type_unification_real (tparms, targs, xparms, xargs, subr,
7875 strict, allow_incomplete, xlen)
7876 tree tparms, targs, xparms, xargs;
7877 int subr;
7878 unification_kind_t strict;
7879 int allow_incomplete, xlen;
7881 tree parm, arg;
7882 int i;
7883 int ntparms = TREE_VEC_LENGTH (tparms);
7884 int sub_strict;
7885 int saw_undeduced = 0;
7886 tree parms, args;
7887 int len;
7889 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7890 my_friendly_assert (xparms == NULL_TREE
7891 || TREE_CODE (xparms) == TREE_LIST, 290);
7892 /* ARGS could be NULL (via a call from parse.y to
7893 build_x_function_call). */
7894 if (xargs)
7895 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
7896 my_friendly_assert (ntparms > 0, 292);
7898 switch (strict)
7900 case DEDUCE_CALL:
7901 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
7902 | UNIFY_ALLOW_DERIVED);
7903 break;
7905 case DEDUCE_CONV:
7906 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7907 break;
7909 case DEDUCE_EXACT:
7910 sub_strict = UNIFY_ALLOW_NONE;
7911 break;
7913 case DEDUCE_ORDER:
7914 sub_strict = UNIFY_ALLOW_NONE;
7915 break;
7917 default:
7918 my_friendly_abort (0);
7921 if (xlen == 0)
7922 return 0;
7924 again:
7925 parms = xparms;
7926 args = xargs;
7927 len = xlen;
7929 while (parms
7930 && parms != void_list_node
7931 && args
7932 && args != void_list_node)
7934 parm = TREE_VALUE (parms);
7935 parms = TREE_CHAIN (parms);
7936 arg = TREE_VALUE (args);
7937 args = TREE_CHAIN (args);
7939 if (arg == error_mark_node)
7940 return 1;
7941 if (arg == unknown_type_node)
7942 /* We can't deduce anything from this, but we might get all the
7943 template args from other function args. */
7944 continue;
7946 /* Conversions will be performed on a function argument that
7947 corresponds with a function parameter that contains only
7948 non-deducible template parameters and explicitly specified
7949 template parameters. */
7950 if (! uses_template_parms (parm))
7952 tree type;
7954 if (!TYPE_P (arg))
7955 type = TREE_TYPE (arg);
7956 else
7958 type = arg;
7959 arg = NULL_TREE;
7962 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
7964 if (same_type_p (parm, type))
7965 continue;
7967 else
7968 /* It might work; we shouldn't check now, because we might
7969 get into infinite recursion. Overload resolution will
7970 handle it. */
7971 continue;
7973 return 1;
7976 if (!TYPE_P (arg))
7978 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
7979 if (type_unknown_p (arg))
7981 /* [temp.deduct.type] A template-argument can be deduced from
7982 a pointer to function or pointer to member function
7983 argument if the set of overloaded functions does not
7984 contain function templates and at most one of a set of
7985 overloaded functions provides a unique match. */
7987 if (resolve_overloaded_unification
7988 (tparms, targs, parm, arg, strict, sub_strict)
7989 != 0)
7990 return 1;
7991 continue;
7993 arg = TREE_TYPE (arg);
7997 int arg_strict = sub_strict;
7999 if (!subr)
8000 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8002 if (unify (tparms, targs, parm, arg, arg_strict))
8003 return 1;
8006 /* Are we done with the interesting parms? */
8007 if (--len == 0)
8008 goto done;
8010 /* Fail if we've reached the end of the parm list, and more args
8011 are present, and the parm list isn't variadic. */
8012 if (args && args != void_list_node && parms == void_list_node)
8013 return 1;
8014 /* Fail if parms are left and they don't have default values. */
8015 if (parms
8016 && parms != void_list_node
8017 && TREE_PURPOSE (parms) == NULL_TREE)
8018 return 1;
8020 done:
8021 if (!subr)
8022 for (i = 0; i < ntparms; i++)
8023 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8025 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8027 /* If this is an undeduced nontype parameter that depends on
8028 a type parameter, try another pass; its type may have been
8029 deduced from a later argument than the one from which
8030 this parameter can be deduced. */
8031 if (TREE_CODE (tparm) == PARM_DECL
8032 && uses_template_parms (TREE_TYPE (tparm))
8033 && !saw_undeduced++)
8034 goto again;
8036 if (!allow_incomplete)
8037 error ("incomplete type unification");
8038 return 2;
8040 return 0;
8043 /* Subroutine of type_unification_real. Args are like the variables at the
8044 call site. ARG is an overloaded function (or template-id); we try
8045 deducing template args from each of the overloads, and if only one
8046 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8048 static int
8049 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8050 sub_strict)
8051 tree tparms, targs, parm, arg;
8052 unification_kind_t strict;
8053 int sub_strict;
8055 tree tempargs = copy_node (targs);
8056 int good = 0;
8058 if (TREE_CODE (arg) == ADDR_EXPR)
8059 arg = TREE_OPERAND (arg, 0);
8061 if (TREE_CODE (arg) == COMPONENT_REF)
8062 /* Handle `&x' where `x' is some static or non-static member
8063 function name. */
8064 arg = TREE_OPERAND (arg, 1);
8066 if (TREE_CODE (arg) == OFFSET_REF)
8067 arg = TREE_OPERAND (arg, 1);
8069 /* Strip baselink information. */
8070 while (TREE_CODE (arg) == TREE_LIST)
8071 arg = TREE_VALUE (arg);
8073 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8075 /* If we got some explicit template args, we need to plug them into
8076 the affected templates before we try to unify, in case the
8077 explicit args will completely resolve the templates in question. */
8079 tree expl_subargs = TREE_OPERAND (arg, 1);
8080 arg = TREE_OPERAND (arg, 0);
8082 for (; arg; arg = OVL_NEXT (arg))
8084 tree fn = OVL_CURRENT (arg);
8085 tree subargs, elem;
8087 if (TREE_CODE (fn) != TEMPLATE_DECL)
8088 continue;
8090 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8091 expl_subargs);
8092 if (subargs)
8094 elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0,
8095 NULL_TREE);
8096 if (TREE_CODE (elem) == METHOD_TYPE)
8097 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8098 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8099 strict, sub_strict);
8103 else if (TREE_CODE (arg) == OVERLOAD)
8105 for (; arg; arg = OVL_NEXT (arg))
8107 tree type = TREE_TYPE (OVL_CURRENT (arg));
8108 if (TREE_CODE (type) == METHOD_TYPE)
8109 type = build_ptrmemfunc_type (build_pointer_type (type));
8110 good += try_one_overload (tparms, targs, tempargs, parm,
8111 type,
8112 strict, sub_strict);
8115 else
8116 my_friendly_abort (981006);
8118 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8119 to function or pointer to member function argument if the set of
8120 overloaded functions does not contain function templates and at most
8121 one of a set of overloaded functions provides a unique match.
8123 So if we found multiple possibilities, we return success but don't
8124 deduce anything. */
8126 if (good == 1)
8128 int i = TREE_VEC_LENGTH (targs);
8129 for (; i--; )
8130 if (TREE_VEC_ELT (tempargs, i))
8131 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8133 if (good)
8134 return 0;
8136 return 1;
8139 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8140 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8141 different overloads deduce different arguments for a given parm.
8142 Returns 1 on success. */
8144 static int
8145 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8146 sub_strict)
8147 tree tparms, orig_targs, targs, parm, arg;
8148 unification_kind_t strict;
8149 int sub_strict;
8151 int nargs;
8152 tree tempargs;
8153 int i;
8155 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8156 to function or pointer to member function argument if the set of
8157 overloaded functions does not contain function templates and at most
8158 one of a set of overloaded functions provides a unique match.
8160 So if this is a template, just return success. */
8162 if (uses_template_parms (arg))
8163 return 1;
8165 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8167 /* We don't copy orig_targs for this because if we have already deduced
8168 some template args from previous args, unify would complain when we
8169 try to deduce a template parameter for the same argument, even though
8170 there isn't really a conflict. */
8171 nargs = TREE_VEC_LENGTH (targs);
8172 tempargs = make_tree_vec (nargs);
8174 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8175 return 0;
8177 /* First make sure we didn't deduce anything that conflicts with
8178 explicitly specified args. */
8179 for (i = nargs; i--; )
8181 tree elt = TREE_VEC_ELT (tempargs, i);
8182 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8184 if (elt == NULL_TREE)
8185 continue;
8186 else if (uses_template_parms (elt))
8188 /* Since we're unifying against ourselves, we will fill in template
8189 args used in the function parm list with our own template parms.
8190 Discard them. */
8191 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8192 continue;
8194 else if (oldelt && ! template_args_equal (oldelt, elt))
8195 return 0;
8198 for (i = nargs; i--; )
8200 tree elt = TREE_VEC_ELT (tempargs, i);
8202 if (elt)
8203 TREE_VEC_ELT (targs, i) = elt;
8206 return 1;
8209 /* Verify that nondeduce template argument agrees with the type
8210 obtained from argument deduction. Return nonzero if the
8211 verification fails.
8213 For example:
8215 struct A { typedef int X; };
8216 template <class T, class U> struct C {};
8217 template <class T> struct C<T, typename T::X> {};
8219 Then with the instantiation `C<A, int>', we can deduce that
8220 `T' is `A' but unify () does not check whether `typename T::X'
8221 is `int'. This function ensure that they agree.
8223 TARGS, PARMS are the same as the arguments of unify.
8224 ARGS contains template arguments from all levels. */
8226 static int
8227 verify_class_unification (targs, parms, args)
8228 tree targs, parms, args;
8230 int i;
8231 int nparms = TREE_VEC_LENGTH (parms);
8232 tree new_parms = tsubst (parms, add_outermost_template_args (args, targs),
8233 /*complain=*/0, NULL_TREE);
8234 if (new_parms == error_mark_node)
8235 return 1;
8237 args = INNERMOST_TEMPLATE_ARGS (args);
8239 for (i = 0; i < nparms; i++)
8241 tree parm = TREE_VEC_ELT (new_parms, i);
8242 tree arg = TREE_VEC_ELT (args, i);
8244 /* In case we are deducing from a function argument of a function
8245 templates, some parameters may not be deduced yet. So we
8246 make sure that only fully substituted elements of PARM are
8247 compared below. */
8249 if (!uses_template_parms (parm) && !template_args_equal (parm, arg))
8250 return 1;
8252 return 0;
8255 /* PARM is a template class (perhaps with unbound template
8256 parameters). ARG is a fully instantiated type. If ARG can be
8257 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8258 TARGS are as for unify. */
8260 static tree
8261 try_class_unification (tparms, targs, parm, arg)
8262 tree tparms;
8263 tree targs;
8264 tree parm;
8265 tree arg;
8267 tree copy_of_targs;
8269 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8270 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8271 return NULL_TREE;
8273 /* We need to make a new template argument vector for the call to
8274 unify. If we used TARGS, we'd clutter it up with the result of
8275 the attempted unification, even if this class didn't work out.
8276 We also don't want to commit ourselves to all the unifications
8277 we've already done, since unification is supposed to be done on
8278 an argument-by-argument basis. In other words, consider the
8279 following pathological case:
8281 template <int I, int J, int K>
8282 struct S {};
8284 template <int I, int J>
8285 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8287 template <int I, int J, int K>
8288 void f(S<I, J, K>, S<I, I, I>);
8290 void g() {
8291 S<0, 0, 0> s0;
8292 S<0, 1, 2> s2;
8294 f(s0, s2);
8297 Now, by the time we consider the unification involving `s2', we
8298 already know that we must have `f<0, 0, 0>'. But, even though
8299 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8300 because there are two ways to unify base classes of S<0, 1, 2>
8301 with S<I, I, I>. If we kept the already deduced knowledge, we
8302 would reject the possibility I=1. */
8303 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
8305 /* If unification failed, we're done. */
8306 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8307 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
8308 return NULL_TREE;
8310 return arg;
8313 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8314 have already discovered to be satisfactory. ARG_BINFO is the binfo
8315 for the base class of ARG that we are currently examining. */
8317 static tree
8318 get_template_base_recursive (tparms, targs, parm,
8319 arg_binfo, rval, flags)
8320 tree tparms;
8321 tree targs;
8322 tree arg_binfo;
8323 tree rval;
8324 tree parm;
8325 int flags;
8327 tree binfos;
8328 int i, n_baselinks;
8329 tree arg = BINFO_TYPE (arg_binfo);
8331 if (!(flags & GTB_IGNORE_TYPE))
8333 tree r = try_class_unification (tparms, targs,
8334 parm, arg);
8336 /* If there is more than one satisfactory baseclass, then:
8338 [temp.deduct.call]
8340 If they yield more than one possible deduced A, the type
8341 deduction fails.
8343 applies. */
8344 if (r && rval && !same_type_p (r, rval))
8345 return error_mark_node;
8346 else if (r)
8347 rval = r;
8350 binfos = BINFO_BASETYPES (arg_binfo);
8351 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8353 /* Process base types. */
8354 for (i = 0; i < n_baselinks; i++)
8356 tree base_binfo = TREE_VEC_ELT (binfos, i);
8357 int this_virtual;
8359 /* Skip this base, if we've already seen it. */
8360 if (BINFO_MARKED (base_binfo))
8361 continue;
8363 this_virtual =
8364 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8366 /* When searching for a non-virtual, we cannot mark virtually
8367 found binfos. */
8368 if (! this_virtual)
8369 SET_BINFO_MARKED (base_binfo);
8371 rval = get_template_base_recursive (tparms, targs,
8372 parm,
8373 base_binfo,
8374 rval,
8375 GTB_VIA_VIRTUAL * this_virtual);
8377 /* If we discovered more than one matching base class, we can
8378 stop now. */
8379 if (rval == error_mark_node)
8380 return error_mark_node;
8383 return rval;
8386 /* Given a template type PARM and a class type ARG, find the unique
8387 base type in ARG that is an instance of PARM. We do not examine
8388 ARG itself; only its base-classes. If there is no appropriate base
8389 class, return NULL_TREE. If there is more than one, return
8390 error_mark_node. PARM may be the type of a partial specialization,
8391 as well as a plain template type. Used by unify. */
8393 static tree
8394 get_template_base (tparms, targs, parm, arg)
8395 tree tparms;
8396 tree targs;
8397 tree parm;
8398 tree arg;
8400 tree rval;
8401 tree arg_binfo;
8403 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8405 arg_binfo = TYPE_BINFO (complete_type (arg));
8406 rval = get_template_base_recursive (tparms, targs,
8407 parm, arg_binfo,
8408 NULL_TREE,
8409 GTB_IGNORE_TYPE);
8411 /* Since get_template_base_recursive marks the bases classes, we
8412 must unmark them here. */
8413 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
8415 return rval;
8418 /* Returns the level of DECL, which declares a template parameter. */
8420 static int
8421 template_decl_level (decl)
8422 tree decl;
8424 switch (TREE_CODE (decl))
8426 case TYPE_DECL:
8427 case TEMPLATE_DECL:
8428 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8430 case PARM_DECL:
8431 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8433 default:
8434 my_friendly_abort (0);
8435 return 0;
8439 /* Decide whether ARG can be unified with PARM, considering only the
8440 cv-qualifiers of each type, given STRICT as documented for unify.
8441 Returns non-zero iff the unification is OK on that basis.*/
8443 static int
8444 check_cv_quals_for_unify (strict, arg, parm)
8445 int strict;
8446 tree arg;
8447 tree parm;
8449 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
8450 && !at_least_as_qualified_p (arg, parm))
8451 return 0;
8453 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
8454 && !at_least_as_qualified_p (parm, arg))
8455 return 0;
8457 return 1;
8460 /* Takes parameters as for type_unification. Returns 0 if the
8461 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8462 bitwise or of the following flags:
8464 UNIFY_ALLOW_NONE:
8465 Require an exact match between PARM and ARG.
8466 UNIFY_ALLOW_MORE_CV_QUAL:
8467 Allow the deduced ARG to be more cv-qualified (by qualification
8468 conversion) than ARG.
8469 UNIFY_ALLOW_LESS_CV_QUAL:
8470 Allow the deduced ARG to be less cv-qualified than ARG.
8471 UNIFY_ALLOW_DERIVED:
8472 Allow the deduced ARG to be a template base class of ARG,
8473 or a pointer to a template base class of the type pointed to by
8474 ARG.
8475 UNIFY_ALLOW_INTEGER:
8476 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8477 case for more information.
8478 UNIFY_ALLOW_OUTER_LEVEL:
8479 This is the outermost level of a deduction. Used to determine validity
8480 of qualification conversions. A valid qualification conversion must
8481 have const qualified pointers leading up to the inner type which
8482 requires additional CV quals, except at the outer level, where const
8483 is not required [conv.qual]. It would be normal to set this flag in
8484 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8485 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8486 This is the outermost level of a deduction, and PARM can be more CV
8487 qualified at this point.
8488 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8489 This is the outermost level of a deduction, and PARM can be less CV
8490 qualified at this point. */
8492 static int
8493 unify (tparms, targs, parm, arg, strict)
8494 tree tparms, targs, parm, arg;
8495 int strict;
8497 int idx;
8498 tree targ;
8499 tree tparm;
8500 int strict_in = strict;
8502 /* I don't think this will do the right thing with respect to types.
8503 But the only case I've seen it in so far has been array bounds, where
8504 signedness is the only information lost, and I think that will be
8505 okay. */
8506 while (TREE_CODE (parm) == NOP_EXPR)
8507 parm = TREE_OPERAND (parm, 0);
8509 if (arg == error_mark_node)
8510 return 1;
8511 if (arg == unknown_type_node)
8512 /* We can't deduce anything from this, but we might get all the
8513 template args from other function args. */
8514 return 0;
8516 /* If PARM uses template parameters, then we can't bail out here,
8517 even if ARG == PARM, since we won't record unifications for the
8518 template parameters. We might need them if we're trying to
8519 figure out which of two things is more specialized. */
8520 if (arg == parm && !uses_template_parms (parm))
8521 return 0;
8523 /* Immediately reject some pairs that won't unify because of
8524 cv-qualification mismatches. */
8525 if (TREE_CODE (arg) == TREE_CODE (parm)
8526 && TYPE_P (arg)
8527 /* It is the elements of the array which hold the cv quals of an array
8528 type, and the elements might be template type parms. We'll check
8529 when we recurse. */
8530 && TREE_CODE (arg) != ARRAY_TYPE
8531 /* We check the cv-qualifiers when unifying with template type
8532 parameters below. We want to allow ARG `const T' to unify with
8533 PARM `T' for example, when computing which of two templates
8534 is more specialized, for example. */
8535 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8536 && !check_cv_quals_for_unify (strict_in, arg, parm))
8537 return 1;
8539 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
8540 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
8541 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
8542 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
8543 strict &= ~UNIFY_ALLOW_DERIVED;
8544 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8545 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
8547 switch (TREE_CODE (parm))
8549 case TYPENAME_TYPE:
8550 /* In a type which contains a nested-name-specifier, template
8551 argument values cannot be deduced for template parameters used
8552 within the nested-name-specifier. */
8553 return 0;
8555 case TEMPLATE_TYPE_PARM:
8556 case TEMPLATE_TEMPLATE_PARM:
8557 case BOUND_TEMPLATE_TEMPLATE_PARM:
8558 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8560 if (TEMPLATE_TYPE_LEVEL (parm)
8561 != template_decl_level (tparm))
8562 /* The PARM is not one we're trying to unify. Just check
8563 to see if it matches ARG. */
8564 return (TREE_CODE (arg) == TREE_CODE (parm)
8565 && same_type_p (parm, arg)) ? 0 : 1;
8566 idx = TEMPLATE_TYPE_IDX (parm);
8567 targ = TREE_VEC_ELT (targs, idx);
8568 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
8570 /* Check for mixed types and values. */
8571 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8572 && TREE_CODE (tparm) != TYPE_DECL)
8573 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8574 && TREE_CODE (tparm) != TEMPLATE_DECL))
8575 return 1;
8577 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8579 /* ARG must be constructed from a template class or a template
8580 template parameter. */
8581 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
8582 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
8583 return 1;
8586 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8587 tree parmvec = TYPE_TI_ARGS (parm);
8588 tree argvec = TYPE_TI_ARGS (arg);
8589 tree argtmplvec
8590 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
8591 int i;
8593 /* The parameter and argument roles have to be switched here
8594 in order to handle default arguments properly. For example,
8595 template<template <class> class TT> void f(TT<int>)
8596 should be able to accept vector<int> which comes from
8597 template <class T, class Allocator = allocator>
8598 class vector. */
8600 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8601 == error_mark_node)
8602 return 1;
8604 /* Deduce arguments T, i from TT<T> or TT<i>.
8605 We check each element of PARMVEC and ARGVEC individually
8606 rather than the whole TREE_VEC since they can have
8607 different number of elements. */
8609 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8611 tree t = TREE_VEC_ELT (parmvec, i);
8613 if (unify (tparms, targs, t,
8614 TREE_VEC_ELT (argvec, i),
8615 UNIFY_ALLOW_NONE))
8616 return 1;
8619 arg = TYPE_TI_TEMPLATE (arg);
8621 /* Fall through to deduce template name. */
8624 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8625 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8627 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8629 /* Simple cases: Value already set, does match or doesn't. */
8630 if (targ != NULL_TREE && template_args_equal (targ, arg))
8631 return 0;
8632 else if (targ)
8633 return 1;
8635 else
8637 /* If PARM is `const T' and ARG is only `int', we don't have
8638 a match unless we are allowing additional qualification.
8639 If ARG is `const int' and PARM is just `T' that's OK;
8640 that binds `const int' to `T'. */
8641 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
8642 arg, parm))
8643 return 1;
8645 /* Consider the case where ARG is `const volatile int' and
8646 PARM is `const T'. Then, T should be `volatile int'. */
8647 arg =
8648 cp_build_qualified_type_real (arg,
8649 CP_TYPE_QUALS (arg)
8650 & ~CP_TYPE_QUALS (parm),
8651 /*complain=*/0);
8652 if (arg == error_mark_node)
8653 return 1;
8655 /* Simple cases: Value already set, does match or doesn't. */
8656 if (targ != NULL_TREE && same_type_p (targ, arg))
8657 return 0;
8658 else if (targ)
8659 return 1;
8662 /* Make sure that ARG is not a variable-sized array. (Note that
8663 were talking about variable-sized arrays (like `int[n]'),
8664 rather than arrays of unknown size (like `int[]').) We'll
8665 get very confused by such a type since the bound of the array
8666 will not be computable in an instantiation. Besides, such
8667 types are not allowed in ISO C++, so we can do as we please
8668 here. */
8669 if (TREE_CODE (arg) == ARRAY_TYPE
8670 && !uses_template_parms (arg)
8671 && TYPE_DOMAIN (arg)
8672 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8673 != INTEGER_CST))
8674 return 1;
8676 TREE_VEC_ELT (targs, idx) = arg;
8677 return 0;
8679 case TEMPLATE_PARM_INDEX:
8680 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8682 if (TEMPLATE_PARM_LEVEL (parm)
8683 != template_decl_level (tparm))
8684 /* The PARM is not one we're trying to unify. Just check
8685 to see if it matches ARG. */
8686 return (TREE_CODE (arg) == TREE_CODE (parm)
8687 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
8689 idx = TEMPLATE_PARM_IDX (parm);
8690 targ = TREE_VEC_ELT (targs, idx);
8692 if (targ)
8694 int i = (cp_tree_equal (targ, arg) > 0);
8695 if (i == 1)
8696 return 0;
8697 else if (i == 0)
8698 return 1;
8699 else
8700 my_friendly_abort (42);
8703 /* [temp.deduct.type] If, in the declaration of a function template
8704 with a non-type template-parameter, the non-type
8705 template-parameter is used in an expression in the function
8706 parameter-list and, if the corresponding template-argument is
8707 deduced, the template-argument type shall match the type of the
8708 template-parameter exactly, except that a template-argument
8709 deduced from an array bound may be of any integral type.
8710 The non-type parameter might use already deduced type parameters. */
8711 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
8712 if (same_type_p (TREE_TYPE (arg), tparm))
8713 /* OK */;
8714 else if ((strict & UNIFY_ALLOW_INTEGER)
8715 && (TREE_CODE (tparm) == INTEGER_TYPE
8716 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8717 /* OK */;
8718 else if (uses_template_parms (tparm))
8719 /* We haven't deduced the type of this parameter yet. Try again
8720 later. */
8721 return 0;
8722 else
8723 return 1;
8725 TREE_VEC_ELT (targs, idx) = arg;
8726 return 0;
8728 case POINTER_TYPE:
8730 if (TREE_CODE (arg) != POINTER_TYPE)
8731 return 1;
8733 /* [temp.deduct.call]
8735 A can be another pointer or pointer to member type that can
8736 be converted to the deduced A via a qualification
8737 conversion (_conv.qual_).
8739 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8740 This will allow for additional cv-qualification of the
8741 pointed-to types if appropriate. */
8743 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
8744 /* The derived-to-base conversion only persists through one
8745 level of pointers. */
8746 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
8748 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
8749 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
8751 /* Avoid getting confused about cv-quals; don't recurse here.
8752 Pointers to members should really be just OFFSET_TYPE, not
8753 this two-level nonsense... */
8755 parm = TREE_TYPE (parm);
8756 arg = TREE_TYPE (arg);
8757 goto offset;
8760 return unify (tparms, targs, TREE_TYPE (parm),
8761 TREE_TYPE (arg), strict);
8764 case REFERENCE_TYPE:
8765 if (TREE_CODE (arg) != REFERENCE_TYPE)
8766 return 1;
8767 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8768 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8770 case ARRAY_TYPE:
8771 if (TREE_CODE (arg) != ARRAY_TYPE)
8772 return 1;
8773 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8774 != (TYPE_DOMAIN (arg) == NULL_TREE))
8775 return 1;
8776 if (TYPE_DOMAIN (parm) != NULL_TREE
8777 && unify (tparms, targs, TYPE_DOMAIN (parm),
8778 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8779 return 1;
8780 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8781 UNIFY_ALLOW_NONE);
8783 case REAL_TYPE:
8784 case COMPLEX_TYPE:
8785 case VECTOR_TYPE:
8786 case INTEGER_TYPE:
8787 case BOOLEAN_TYPE:
8788 case VOID_TYPE:
8789 if (TREE_CODE (arg) != TREE_CODE (parm))
8790 return 1;
8792 if (TREE_CODE (parm) == INTEGER_TYPE
8793 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8795 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
8796 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
8797 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8798 return 1;
8799 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
8800 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
8801 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER))
8802 return 1;
8804 /* We have already checked cv-qualification at the top of the
8805 function. */
8806 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
8807 return 1;
8809 /* As far as unification is concerned, this wins. Later checks
8810 will invalidate it if necessary. */
8811 return 0;
8813 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8814 /* Type INTEGER_CST can come from ordinary constant template args. */
8815 case INTEGER_CST:
8816 while (TREE_CODE (arg) == NOP_EXPR)
8817 arg = TREE_OPERAND (arg, 0);
8819 if (TREE_CODE (arg) != INTEGER_CST)
8820 return 1;
8821 return !tree_int_cst_equal (parm, arg);
8823 case TREE_VEC:
8825 int i;
8826 if (TREE_CODE (arg) != TREE_VEC)
8827 return 1;
8828 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8829 return 1;
8830 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
8831 if (unify (tparms, targs,
8832 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
8833 UNIFY_ALLOW_NONE))
8834 return 1;
8835 return 0;
8838 case RECORD_TYPE:
8839 case UNION_TYPE:
8840 if (TREE_CODE (arg) != TREE_CODE (parm))
8841 return 1;
8843 if (TYPE_PTRMEMFUNC_P (parm))
8845 if (!TYPE_PTRMEMFUNC_P (arg))
8846 return 1;
8848 return unify (tparms, targs,
8849 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8850 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8851 strict);
8854 if (CLASSTYPE_TEMPLATE_INFO (parm))
8856 tree t = NULL_TREE;
8858 if (strict_in & UNIFY_ALLOW_DERIVED)
8860 /* First, we try to unify the PARM and ARG directly. */
8861 t = try_class_unification (tparms, targs,
8862 parm, arg);
8864 if (!t)
8866 /* Fallback to the special case allowed in
8867 [temp.deduct.call]:
8869 If P is a class, and P has the form
8870 template-id, then A can be a derived class of
8871 the deduced A. Likewise, if P is a pointer to
8872 a class of the form template-id, A can be a
8873 pointer to a derived class pointed to by the
8874 deduced A. */
8875 t = get_template_base (tparms, targs,
8876 parm, arg);
8878 if (! t || t == error_mark_node)
8879 return 1;
8882 else if (CLASSTYPE_TEMPLATE_INFO (arg)
8883 && (CLASSTYPE_TI_TEMPLATE (parm)
8884 == CLASSTYPE_TI_TEMPLATE (arg)))
8885 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8886 Then, we should unify `int' and `U'. */
8887 t = arg;
8888 else
8889 /* There's no chance of unification succeeding. */
8890 return 1;
8892 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
8893 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
8895 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
8896 return 1;
8897 return 0;
8899 case METHOD_TYPE:
8900 case FUNCTION_TYPE:
8901 if (TREE_CODE (arg) != TREE_CODE (parm))
8902 return 1;
8904 if (unify (tparms, targs, TREE_TYPE (parm),
8905 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
8906 return 1;
8907 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
8908 TYPE_ARG_TYPES (arg), 1,
8909 DEDUCE_EXACT, 0, -1);
8911 case OFFSET_TYPE:
8912 offset:
8913 if (TREE_CODE (arg) != OFFSET_TYPE)
8914 return 1;
8915 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
8916 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
8917 return 1;
8918 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8919 strict);
8921 case CONST_DECL:
8922 if (arg != decl_constant_value (parm))
8923 return 1;
8924 return 0;
8926 case TEMPLATE_DECL:
8927 /* Matched cases are handled by the ARG == PARM test above. */
8928 return 1;
8930 case MINUS_EXPR:
8931 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
8933 /* We handle this case specially, since it comes up with
8934 arrays. In particular, something like:
8936 template <int N> void f(int (&x)[N]);
8938 Here, we are trying to unify the range type, which
8939 looks like [0 ... (N - 1)]. */
8940 tree t, t1, t2;
8941 t1 = TREE_OPERAND (parm, 0);
8942 t2 = TREE_OPERAND (parm, 1);
8944 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
8946 return unify (tparms, targs, t1, t, strict);
8948 /* else fall through */
8950 default:
8951 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
8954 /* We're looking at an expression. This can happen with
8955 something like:
8957 template <int I>
8958 void foo(S<I>, S<I + 2>);
8960 This is a "nondeduced context":
8962 [deduct.type]
8964 The nondeduced contexts are:
8966 --A type that is a template-id in which one or more of
8967 the template-arguments is an expression that references
8968 a template-parameter.
8970 In these cases, we assume deduction succeeded, but don't
8971 actually infer any unifications. */
8973 if (!uses_template_parms (parm)
8974 && !template_args_equal (parm, arg))
8975 return 1;
8976 else
8977 return 0;
8979 else
8980 sorry ("use of `%s' in template type unification",
8981 tree_code_name [(int) TREE_CODE (parm)]);
8983 return 1;
8987 /* Called if RESULT is explicitly instantiated, or is a member of an
8988 explicitly instantiated class, or if using -frepo and the
8989 instantiation of RESULT has been assigned to this file. */
8991 void
8992 mark_decl_instantiated (result, extern_p)
8993 tree result;
8994 int extern_p;
8996 if (TREE_CODE (result) != FUNCTION_DECL)
8997 /* The TREE_PUBLIC flag for function declarations will have been
8998 set correctly by tsubst. */
8999 TREE_PUBLIC (result) = 1;
9001 /* We used to set this unconditionally; we moved that to
9002 do_decl_instantiation so it wouldn't get set on members of
9003 explicit class template instantiations. But we still need to set
9004 it here for the 'extern template' case in order to suppress
9005 implicit instantiations. */
9006 if (extern_p)
9007 SET_DECL_EXPLICIT_INSTANTIATION (result);
9009 if (! extern_p)
9011 DECL_INTERFACE_KNOWN (result) = 1;
9012 DECL_NOT_REALLY_EXTERN (result) = 1;
9014 /* Always make artificials weak. */
9015 if (DECL_ARTIFICIAL (result) && flag_weak)
9016 comdat_linkage (result);
9017 /* For WIN32 we also want to put explicit instantiations in
9018 linkonce sections. */
9019 else if (TREE_PUBLIC (result))
9020 maybe_make_one_only (result);
9022 else if (TREE_CODE (result) == FUNCTION_DECL)
9023 defer_fn (result);
9026 /* Given two function templates PAT1 and PAT2, return:
9028 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9030 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9031 -1 if PAT2 is more specialized than PAT1.
9032 0 if neither is more specialized.
9034 LEN is passed through to fn_type_unification. */
9037 more_specialized (pat1, pat2, deduce, len)
9038 tree pat1, pat2;
9039 int deduce;
9040 int len;
9042 tree targs;
9043 int winner = 0;
9045 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9046 NULL_TREE, 0, deduce, len);
9047 if (targs)
9048 --winner;
9050 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9051 NULL_TREE, 0, deduce, len);
9052 if (targs)
9053 ++winner;
9055 return winner;
9058 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9060 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9061 -1 if PAT2 is more specialized than PAT1.
9062 0 if neither is more specialized. */
9065 more_specialized_class (pat1, pat2)
9066 tree pat1, pat2;
9068 tree targs;
9069 int winner = 0;
9071 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9072 TREE_PURPOSE (pat2));
9073 if (targs)
9074 --winner;
9076 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9077 TREE_PURPOSE (pat1));
9078 if (targs)
9079 ++winner;
9081 return winner;
9084 /* Return the template arguments that will produce the function signature
9085 DECL from the function template FN, with the explicit template
9086 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9087 also match. Return NULL_TREE if no satisfactory arguments could be
9088 found. DEDUCE and LEN are passed through to fn_type_unification. */
9090 static tree
9091 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9092 tree fn, decl, explicit_args;
9093 int check_rettype, deduce, len;
9095 int ntparms = DECL_NTPARMS (fn);
9096 tree targs = make_tree_vec (ntparms);
9097 tree decl_type;
9098 tree decl_arg_types;
9099 int i;
9101 /* Substitute the explicit template arguments into the type of DECL.
9102 The call to fn_type_unification will handle substitution into the
9103 FN. */
9104 decl_type = TREE_TYPE (decl);
9105 if (explicit_args && uses_template_parms (decl_type))
9107 tree tmpl;
9108 tree converted_args;
9110 if (DECL_TEMPLATE_INFO (decl))
9111 tmpl = DECL_TI_TEMPLATE (decl);
9112 else
9113 /* We can get here for some illegal specializations. */
9114 return NULL_TREE;
9116 converted_args
9117 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9118 explicit_args, NULL_TREE,
9119 /*complain=*/0,
9120 /*require_all_arguments=*/0));
9121 if (converted_args == error_mark_node)
9122 return NULL_TREE;
9124 decl_type = tsubst (decl_type, converted_args, /*complain=*/0,
9125 NULL_TREE);
9126 if (decl_type == error_mark_node)
9127 return NULL_TREE;
9130 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9131 /* Never do unification on the 'this' parameter. */
9132 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9133 decl_arg_types = TREE_CHAIN (decl_arg_types);
9135 i = fn_type_unification (fn, explicit_args, targs,
9136 decl_arg_types,
9137 (check_rettype || DECL_CONV_FN_P (fn)
9138 ? TREE_TYPE (decl_type) : NULL_TREE),
9139 deduce, len);
9141 if (i != 0)
9142 return NULL_TREE;
9144 return targs;
9147 /* For most uses, we want to check the return type. */
9149 tree
9150 get_bindings (fn, decl, explicit_args)
9151 tree fn, decl, explicit_args;
9153 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9156 /* But for resolve_overloaded_unification, we only care about the parameter
9157 types. */
9159 static tree
9160 get_bindings_overload (fn, decl, explicit_args)
9161 tree fn, decl, explicit_args;
9163 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9166 /* Return the innermost template arguments that, when applied to a
9167 template specialization whose innermost template parameters are
9168 TPARMS, and whose specialization arguments are ARGS, yield the
9169 ARGS.
9171 For example, suppose we have:
9173 template <class T, class U> struct S {};
9174 template <class T> struct S<T*, int> {};
9176 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9177 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9178 int}. The resulting vector will be {double}, indicating that `T'
9179 is bound to `double'. */
9181 static tree
9182 get_class_bindings (tparms, parms, args)
9183 tree tparms, parms, args;
9185 int i, ntparms = TREE_VEC_LENGTH (tparms);
9186 tree vec = make_tree_vec (ntparms);
9188 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9189 UNIFY_ALLOW_NONE))
9190 return NULL_TREE;
9192 for (i = 0; i < ntparms; ++i)
9193 if (! TREE_VEC_ELT (vec, i))
9194 return NULL_TREE;
9196 if (verify_class_unification (vec, parms, args))
9197 return NULL_TREE;
9199 return vec;
9202 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9203 Pick the most specialized template, and return the corresponding
9204 instantiation, or if there is no corresponding instantiation, the
9205 template itself. If there is no most specialized template,
9206 error_mark_node is returned. If there are no templates at all,
9207 NULL_TREE is returned. */
9209 tree
9210 most_specialized_instantiation (instantiations)
9211 tree instantiations;
9213 tree fn, champ;
9214 int fate;
9216 if (!instantiations)
9217 return NULL_TREE;
9219 champ = instantiations;
9220 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
9222 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9223 DEDUCE_EXACT, -1);
9224 if (fate == 1)
9226 else
9228 if (fate == 0)
9230 fn = TREE_CHAIN (fn);
9231 if (! fn)
9232 return error_mark_node;
9234 champ = fn;
9238 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
9240 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9241 DEDUCE_EXACT, -1);
9242 if (fate != 1)
9243 return error_mark_node;
9246 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9249 /* Return the most specialized of the list of templates in FNS that can
9250 produce an instantiation matching DECL, given the explicit template
9251 arguments EXPLICIT_ARGS. */
9253 static tree
9254 most_specialized (fns, decl, explicit_args)
9255 tree fns, decl, explicit_args;
9257 tree candidates = NULL_TREE;
9258 tree fn, args;
9260 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9262 tree candidate = TREE_VALUE (fn);
9264 args = get_bindings (candidate, decl, explicit_args);
9265 if (args)
9266 candidates = tree_cons (NULL_TREE, candidate, candidates);
9269 return most_specialized_instantiation (candidates);
9272 /* If DECL is a specialization of some template, return the most
9273 general such template. Otherwise, returns NULL_TREE.
9275 For example, given:
9277 template <class T> struct S { template <class U> void f(U); };
9279 if TMPL is `template <class U> void S<int>::f(U)' this will return
9280 the full template. This function will not trace past partial
9281 specializations, however. For example, given in addition:
9283 template <class T> struct S<T*> { template <class U> void f(U); };
9285 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9286 `template <class T> template <class U> S<T*>::f(U)'. */
9288 tree
9289 most_general_template (decl)
9290 tree decl;
9292 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9293 an immediate specialization. */
9294 if (TREE_CODE (decl) == FUNCTION_DECL)
9296 if (DECL_TEMPLATE_INFO (decl)) {
9297 decl = DECL_TI_TEMPLATE (decl);
9299 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9300 template friend. */
9301 if (TREE_CODE (decl) != TEMPLATE_DECL)
9302 return NULL_TREE;
9303 } else
9304 return NULL_TREE;
9307 /* Look for more and more general templates. */
9308 while (DECL_TEMPLATE_INFO (decl))
9310 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9311 in some cases. (See cp-tree.h for details.) */
9312 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9313 break;
9315 /* Stop if we run into an explicitly specialized class template. */
9316 if (!DECL_NAMESPACE_SCOPE_P (decl)
9317 && DECL_CONTEXT (decl)
9318 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
9319 break;
9321 decl = DECL_TI_TEMPLATE (decl);
9324 return decl;
9327 /* Return the most specialized of the class template specializations
9328 of TMPL which can produce an instantiation matching ARGS, or
9329 error_mark_node if the choice is ambiguous. */
9331 static tree
9332 most_specialized_class (tmpl, args)
9333 tree tmpl;
9334 tree args;
9336 tree list = NULL_TREE;
9337 tree t;
9338 tree champ;
9339 int fate;
9341 tmpl = most_general_template (tmpl);
9342 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
9344 tree spec_args
9345 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9346 if (spec_args)
9348 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9349 TREE_TYPE (list) = TREE_TYPE (t);
9353 if (! list)
9354 return NULL_TREE;
9356 t = list;
9357 champ = t;
9358 t = TREE_CHAIN (t);
9359 for (; t; t = TREE_CHAIN (t))
9361 fate = more_specialized_class (champ, t);
9362 if (fate == 1)
9364 else
9366 if (fate == 0)
9368 t = TREE_CHAIN (t);
9369 if (! t)
9370 return error_mark_node;
9372 champ = t;
9376 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9378 fate = more_specialized_class (champ, t);
9379 if (fate != 1)
9380 return error_mark_node;
9383 return champ;
9386 /* called from the parser. */
9388 void
9389 do_decl_instantiation (declspecs, declarator, storage)
9390 tree declspecs, declarator, storage;
9392 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
9393 tree result = NULL_TREE;
9394 int extern_p = 0;
9396 if (!decl)
9397 /* An error occurred, for which grokdeclarator has already issued
9398 an appropriate message. */
9399 return;
9400 else if (! DECL_LANG_SPECIFIC (decl))
9402 cp_error ("explicit instantiation of non-template `%#D'", decl);
9403 return;
9405 else if (TREE_CODE (decl) == VAR_DECL)
9407 /* There is an asymmetry here in the way VAR_DECLs and
9408 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9409 the latter, the DECL we get back will be marked as a
9410 template instantiation, and the appropriate
9411 DECL_TEMPLATE_INFO will be set up. This does not happen for
9412 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9413 should handle VAR_DECLs as it currently handles
9414 FUNCTION_DECLs. */
9415 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9416 if (result && TREE_CODE (result) != VAR_DECL)
9418 cp_error ("no matching template for `%D' found", result);
9419 return;
9422 else if (TREE_CODE (decl) != FUNCTION_DECL)
9424 cp_error ("explicit instantiation of `%#D'", decl);
9425 return;
9427 else
9428 result = decl;
9430 /* Check for various error cases. Note that if the explicit
9431 instantiation is legal the RESULT will currently be marked as an
9432 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9433 until we get here. */
9435 if (DECL_TEMPLATE_SPECIALIZATION (result))
9437 /* [temp.spec]
9439 No program shall both explicitly instantiate and explicitly
9440 specialize a template. */
9441 cp_pedwarn ("explicit instantiation of `%#D' after", result);
9442 cp_pedwarn_at ("explicit specialization here", result);
9443 return;
9445 else if (DECL_EXPLICIT_INSTANTIATION (result))
9447 /* [temp.spec]
9449 No program shall explicitly instantiate any template more
9450 than once.
9452 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9453 instantiation was `extern' and the second is not, and EXTERN_P for
9454 the opposite case. If -frepo, chances are we already got marked
9455 as an explicit instantiation because of the repo file. */
9456 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
9457 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result);
9459 /* If we've already instantiated the template, just return now. */
9460 if (DECL_INTERFACE_KNOWN (result))
9461 return;
9463 else if (!DECL_IMPLICIT_INSTANTIATION (result))
9465 cp_error ("no matching template for `%D' found", result);
9466 return;
9468 else if (!DECL_TEMPLATE_INFO (result))
9470 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
9471 return;
9474 if (flag_external_templates)
9475 return;
9477 if (storage == NULL_TREE)
9479 else if (storage == ridpointers[(int) RID_EXTERN])
9481 if (pedantic)
9482 cp_pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9483 extern_p = 1;
9485 else
9486 cp_error ("storage class `%D' applied to template instantiation",
9487 storage);
9489 SET_DECL_EXPLICIT_INSTANTIATION (result);
9490 mark_decl_instantiated (result, extern_p);
9491 repo_template_instantiated (result, extern_p);
9492 if (! extern_p)
9493 instantiate_decl (result, /*defer_ok=*/1);
9496 void
9497 mark_class_instantiated (t, extern_p)
9498 tree t;
9499 int extern_p;
9501 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
9502 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9503 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
9504 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
9505 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9506 if (! extern_p)
9508 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9509 rest_of_type_compilation (t, 1);
9513 /* Perform an explicit instantiation of template class T. STORAGE, if
9514 non-null, is the RID for extern, inline or static. COMPLAIN is
9515 non-zero if this is called from the parser, zero if called recursively,
9516 since the standard is unclear (as detailed below). */
9518 void
9519 do_type_instantiation (t, storage, complain)
9520 tree t, storage;
9521 int complain;
9523 int extern_p = 0;
9524 int nomem_p = 0;
9525 int static_p = 0;
9527 if (TREE_CODE (t) == TYPE_DECL)
9528 t = TREE_TYPE (t);
9530 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
9532 cp_error ("explicit instantiation of non-template type `%T'", t);
9533 return;
9536 complete_type (t);
9538 /* With -fexternal-templates, explicit instantiations are treated the same
9539 as implicit ones. */
9540 if (flag_external_templates)
9541 return;
9543 if (!COMPLETE_TYPE_P (t))
9545 if (complain)
9546 cp_error ("explicit instantiation of `%#T' before definition of template",
9548 return;
9551 if (storage != NULL_TREE)
9553 if (pedantic)
9554 cp_pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9555 IDENTIFIER_POINTER (storage));
9557 if (storage == ridpointers[(int) RID_INLINE])
9558 nomem_p = 1;
9559 else if (storage == ridpointers[(int) RID_EXTERN])
9560 extern_p = 1;
9561 else if (storage == ridpointers[(int) RID_STATIC])
9562 static_p = 1;
9563 else
9565 cp_error ("storage class `%D' applied to template instantiation",
9566 storage);
9567 extern_p = 0;
9571 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9573 /* [temp.spec]
9575 No program shall both explicitly instantiate and explicitly
9576 specialize a template. */
9577 if (complain)
9579 cp_error ("explicit instantiation of `%#T' after", t);
9580 cp_error_at ("explicit specialization here", t);
9582 return;
9584 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
9586 /* [temp.spec]
9588 No program shall explicitly instantiate any template more
9589 than once.
9591 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9592 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9593 are we already got marked as an explicit instantion because of the
9594 repo file. All these cases are OK. */
9595 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
9596 && complain)
9597 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t);
9599 /* If we've already instantiated the template, just return now. */
9600 if (!CLASSTYPE_INTERFACE_ONLY (t))
9601 return;
9604 mark_class_instantiated (t, extern_p);
9605 repo_template_instantiated (t, extern_p);
9607 if (nomem_p)
9608 return;
9611 tree tmp;
9613 /* In contrast to implicit instantiation, where only the
9614 declarations, and not the definitions, of members are
9615 instantiated, we have here:
9617 [temp.explicit]
9619 The explicit instantiation of a class template specialization
9620 implies the instantiation of all of its members not
9621 previously explicitly specialized in the translation unit
9622 containing the explicit instantiation.
9624 Of course, we can't instantiate member template classes, since
9625 we don't have any arguments for them. Note that the standard
9626 is unclear on whether the instantiation of the members are
9627 *explicit* instantiations or not. We choose to be generous,
9628 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9629 the explicit instantiation of a class where some of the members
9630 have no definition in the current translation unit. */
9632 if (! static_p)
9633 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
9634 if (TREE_CODE (tmp) == FUNCTION_DECL
9635 && DECL_TEMPLATE_INSTANTIATION (tmp))
9637 mark_decl_instantiated (tmp, extern_p);
9638 repo_template_instantiated (tmp, extern_p);
9639 if (! extern_p)
9640 instantiate_decl (tmp, /*defer_ok=*/1);
9643 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9644 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
9646 mark_decl_instantiated (tmp, extern_p);
9647 repo_template_instantiated (tmp, extern_p);
9648 if (! extern_p)
9649 instantiate_decl (tmp, /*defer_ok=*/1);
9652 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
9653 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9654 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
9655 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
9659 /* Given a function DECL, which is a specialization of TMPL, modify
9660 DECL to be a re-instantiation of TMPL with the same template
9661 arguments. TMPL should be the template into which tsubst'ing
9662 should occur for DECL, not the most general template.
9664 One reason for doing this is a scenario like this:
9666 template <class T>
9667 void f(const T&, int i);
9669 void g() { f(3, 7); }
9671 template <class T>
9672 void f(const T& t, const int i) { }
9674 Note that when the template is first instantiated, with
9675 instantiate_template, the resulting DECL will have no name for the
9676 first parameter, and the wrong type for the second. So, when we go
9677 to instantiate the DECL, we regenerate it. */
9679 static void
9680 regenerate_decl_from_template (decl, tmpl)
9681 tree decl;
9682 tree tmpl;
9684 /* The most general version of TMPL. */
9685 tree gen_tmpl;
9686 /* The arguments used to instantiate DECL, from the most general
9687 template. */
9688 tree args;
9689 tree code_pattern;
9690 tree new_decl;
9691 int unregistered;
9693 args = DECL_TI_ARGS (decl);
9694 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9696 /* Unregister the specialization so that when we tsubst we will not
9697 just return DECL. We don't have to unregister DECL from TMPL
9698 because if would only be registered there if it were a partial
9699 instantiation of a specialization, which it isn't: it's a full
9700 instantiation. */
9701 gen_tmpl = most_general_template (tmpl);
9702 unregistered = unregister_specialization (decl, gen_tmpl);
9704 /* If the DECL was not unregistered then something peculiar is
9705 happening: we created a specialization but did not call
9706 register_specialization for it. */
9707 my_friendly_assert (unregistered, 0);
9709 if (TREE_CODE (decl) == VAR_DECL)
9710 /* Make sure that we can see identifiers, and compute access
9711 correctly, for the class members used in the declaration of
9712 this static variable. */
9713 pushclass (DECL_CONTEXT (decl), 2);
9715 /* Do the substitution to get the new declaration. */
9716 new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE);
9718 if (TREE_CODE (decl) == VAR_DECL)
9720 /* Set up DECL_INITIAL, since tsubst doesn't. */
9721 DECL_INITIAL (new_decl) =
9722 tsubst_expr (DECL_INITIAL (code_pattern), args,
9723 /*complain=*/1, DECL_TI_TEMPLATE (decl));
9724 /* Pop the class context we pushed above. */
9725 popclass ();
9727 else if (TREE_CODE (decl) == FUNCTION_DECL)
9729 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9730 new decl. */
9731 DECL_INITIAL (new_decl) = error_mark_node;
9732 /* And don't complain about a duplicate definition. */
9733 DECL_INITIAL (decl) = NULL_TREE;
9736 /* The immediate parent of the new template is still whatever it was
9737 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9738 general template. We also reset the DECL_ASSEMBLER_NAME since
9739 tsubst always calculates the name as if the function in question
9740 were really a template instance, and sometimes, with friend
9741 functions, this is not so. See tsubst_friend_function for
9742 details. */
9743 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9744 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
9745 COPY_DECL_RTL (decl, new_decl);
9746 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
9748 /* Call duplicate decls to merge the old and new declarations. */
9749 duplicate_decls (new_decl, decl);
9751 /* Now, re-register the specialization. */
9752 register_specialization (decl, gen_tmpl, args);
9755 /* Produce the definition of D, a _DECL generated from a template. If
9756 DEFER_OK is non-zero, then we don't have to actually do the
9757 instantiation now; we just have to do it sometime. */
9759 tree
9760 instantiate_decl (d, defer_ok)
9761 tree d;
9762 int defer_ok;
9764 tree tmpl = DECL_TI_TEMPLATE (d);
9765 tree args = DECL_TI_ARGS (d);
9766 tree td;
9767 tree code_pattern;
9768 tree spec;
9769 tree gen_tmpl;
9770 int pattern_defined;
9771 int line = lineno;
9772 int need_push;
9773 const char *file = input_filename;
9775 /* This function should only be used to instantiate templates for
9776 functions and static member variables. */
9777 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9778 || TREE_CODE (d) == VAR_DECL, 0);
9780 /* Don't instantiate cloned functions. Instead, instantiate the
9781 functions they cloned. */
9782 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
9783 d = DECL_CLONED_FUNCTION (d);
9785 if (DECL_TEMPLATE_INSTANTIATED (d))
9786 /* D has already been instantiated. It might seem reasonable to
9787 check whether or not D is an explicit instantiation, and, if so,
9788 stop here. But when an explicit instantiation is deferred
9789 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9790 is set, even though we still need to do the instantiation. */
9791 return d;
9793 /* If we already have a specialization of this declaration, then
9794 there's no reason to instantiate it. Note that
9795 retrieve_specialization gives us both instantiations and
9796 specializations, so we must explicitly check
9797 DECL_TEMPLATE_SPECIALIZATION. */
9798 gen_tmpl = most_general_template (tmpl);
9799 spec = retrieve_specialization (gen_tmpl, args);
9800 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9801 return spec;
9803 /* This needs to happen before any tsubsting. */
9804 if (! push_tinst_level (d))
9805 return d;
9807 timevar_push (TV_PARSE);
9809 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9810 for the instantiation. This is not always the most general
9811 template. Consider, for example:
9813 template <class T>
9814 struct S { template <class U> void f();
9815 template <> void f<int>(); };
9817 and an instantiation of S<double>::f<int>. We want TD to be the
9818 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9819 td = tmpl;
9820 while (/* An instantiation cannot have a definition, so we need a
9821 more general template. */
9822 DECL_TEMPLATE_INSTANTIATION (td)
9823 /* We must also deal with friend templates. Given:
9825 template <class T> struct S {
9826 template <class U> friend void f() {};
9829 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9830 so far as the language is concerned, but that's still
9831 where we get the pattern for the instantiation from. On
9832 other hand, if the definition comes outside the class, say:
9834 template <class T> struct S {
9835 template <class U> friend void f();
9837 template <class U> friend void f() {}
9839 we don't need to look any further. That's what the check for
9840 DECL_INITIAL is for. */
9841 || (TREE_CODE (d) == FUNCTION_DECL
9842 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9843 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))))
9845 /* The present template, TD, should not be a definition. If it
9846 were a definition, we should be using it! Note that we
9847 cannot restructure the loop to just keep going until we find
9848 a template with a definition, since that might go too far if
9849 a specialization was declared, but not defined. */
9850 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9851 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9852 0);
9854 /* Fetch the more general template. */
9855 td = DECL_TI_TEMPLATE (td);
9858 code_pattern = DECL_TEMPLATE_RESULT (td);
9860 if (TREE_CODE (d) == FUNCTION_DECL)
9861 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
9862 else
9863 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
9865 lineno = DECL_SOURCE_LINE (d);
9866 input_filename = DECL_SOURCE_FILE (d);
9868 if (pattern_defined)
9870 /* Let the repository code that this template definition is
9871 available.
9873 The repository doesn't need to know about cloned functions
9874 because they never actually show up in the object file. It
9875 does need to know about the clones; those are the symbols
9876 that the linker will be emitting error messages about. */
9877 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
9878 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
9880 tree t;
9882 for (t = TREE_CHAIN (d);
9883 t && DECL_CLONED_FUNCTION_P (t);
9884 t = TREE_CHAIN (t))
9885 repo_template_used (t);
9887 else
9888 repo_template_used (d);
9890 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
9892 if (flag_alt_external_templates)
9894 if (interface_unknown)
9895 warn_if_unknown_interface (d);
9897 else if (DECL_INTERFACE_KNOWN (code_pattern))
9899 DECL_INTERFACE_KNOWN (d) = 1;
9900 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
9902 else
9903 warn_if_unknown_interface (code_pattern);
9906 if (at_eof)
9907 import_export_decl (d);
9910 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
9911 && DECL_INITIAL (d) == NULL_TREE)
9912 /* We should have set up DECL_INITIAL in instantiate_class_template. */
9913 abort ();
9914 /* Reject all external templates except inline functions. */
9915 else if (DECL_INTERFACE_KNOWN (d)
9916 && ! DECL_NOT_REALLY_EXTERN (d)
9917 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
9918 goto out;
9919 /* Defer all other templates, unless we have been explicitly
9920 forbidden from doing so. We restore the source position here
9921 because it's used by add_pending_template. */
9922 else if (! pattern_defined || defer_ok)
9924 lineno = line;
9925 input_filename = file;
9927 if (at_eof && !pattern_defined
9928 && DECL_EXPLICIT_INSTANTIATION (d))
9929 /* [temp.explicit]
9931 The definition of a non-exported function template, a
9932 non-exported member function template, or a non-exported
9933 member function or static data member of a class template
9934 shall be present in every translation unit in which it is
9935 explicitly instantiated. */
9936 cp_error ("explicit instantiation of `%D' but no definition available",
9939 add_pending_template (d);
9940 goto out;
9943 need_push = !global_bindings_p ();
9944 if (need_push)
9945 push_to_top_level ();
9947 /* We're now committed to instantiating this template. Mark it as
9948 instantiated so that recursive calls to instantiate_decl do not
9949 try to instantiate it again. */
9950 DECL_TEMPLATE_INSTANTIATED (d) = 1;
9952 /* Regenerate the declaration in case the template has been modified
9953 by a subsequent redeclaration. */
9954 regenerate_decl_from_template (d, td);
9956 /* We already set the file and line above. Reset them now in case
9957 they changed as a result of calling regenerate_decl_from_template. */
9958 lineno = DECL_SOURCE_LINE (d);
9959 input_filename = DECL_SOURCE_FILE (d);
9961 if (TREE_CODE (d) == VAR_DECL)
9963 DECL_IN_AGGR_P (d) = 0;
9964 if (DECL_INTERFACE_KNOWN (d))
9965 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
9966 else
9968 DECL_EXTERNAL (d) = 1;
9969 DECL_NOT_REALLY_EXTERN (d) = 1;
9971 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
9973 else if (TREE_CODE (d) == FUNCTION_DECL)
9975 htab_t saved_local_specializations;
9977 /* Save away the current list, in case we are instantiating one
9978 template from within the body of another. */
9979 saved_local_specializations = local_specializations;
9981 /* Set up the list of local specializations. */
9982 local_specializations = htab_create (37,
9983 htab_hash_pointer,
9984 htab_eq_pointer,
9985 NULL);
9987 /* Set up context. */
9988 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
9990 /* We already set up __FUNCTION__, etc., so we don't want to do
9991 it again now. */
9992 function_name_declared_p = 1;
9994 /* Substitute into the body of the function. */
9995 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
9996 /*complain=*/1, tmpl);
9998 /* We don't need the local specializations any more. */
9999 htab_delete (local_specializations);
10000 local_specializations = saved_local_specializations;
10002 /* Finish the function. */
10003 expand_body (finish_function (0));
10006 /* We're not deferring instantiation any more. */
10007 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10009 if (need_push)
10010 pop_from_top_level ();
10012 out:
10013 lineno = line;
10014 input_filename = file;
10016 pop_tinst_level ();
10018 timevar_pop (TV_PARSE);
10020 return d;
10023 /* Run through the list of templates that we wish we could
10024 instantiate, and instantiate any we can. */
10027 instantiate_pending_templates ()
10029 tree *t;
10030 int instantiated_something = 0;
10031 int reconsider;
10035 reconsider = 0;
10037 t = &pending_templates;
10038 while (*t)
10040 tree instantiation = TREE_VALUE (*t);
10042 reopen_tinst_level (TREE_PURPOSE (*t));
10044 if (TYPE_P (instantiation))
10046 tree fn;
10048 if (!COMPLETE_TYPE_P (instantiation))
10050 instantiate_class_template (instantiation);
10051 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10052 for (fn = TYPE_METHODS (instantiation);
10054 fn = TREE_CHAIN (fn))
10055 if (! DECL_ARTIFICIAL (fn))
10056 instantiate_decl (fn, /*defer_ok=*/0);
10057 if (COMPLETE_TYPE_P (instantiation))
10059 instantiated_something = 1;
10060 reconsider = 1;
10064 if (COMPLETE_TYPE_P (instantiation))
10065 /* If INSTANTIATION has been instantiated, then we don't
10066 need to consider it again in the future. */
10067 *t = TREE_CHAIN (*t);
10068 else
10069 t = &TREE_CHAIN (*t);
10071 else
10073 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10074 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10076 instantiation = instantiate_decl (instantiation,
10077 /*defer_ok=*/0);
10078 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10080 instantiated_something = 1;
10081 reconsider = 1;
10085 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10086 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10087 /* If INSTANTIATION has been instantiated, then we don't
10088 need to consider it again in the future. */
10089 *t = TREE_CHAIN (*t);
10090 else
10091 t = &TREE_CHAIN (*t);
10093 tinst_depth = 0;
10094 current_tinst_level = NULL_TREE;
10096 template_tail = t;
10098 /* Go through the things that are template instantiations if we are
10099 using guiding declarations. */
10100 t = &maybe_templates;
10101 while (*t)
10103 tree template;
10104 tree fn;
10105 tree args;
10107 fn = TREE_VALUE (*t);
10109 if (DECL_INITIAL (fn))
10110 /* If the FN is already defined, then it was either already
10111 instantiated or, even though guiding declarations were
10112 allowed, a non-template definition was provided. */
10114 else
10116 template = TREE_PURPOSE (*t);
10117 args = get_bindings (template, fn, NULL_TREE);
10118 fn = instantiate_template (template, args);
10119 instantiate_decl (fn, /*defer_ok=*/0);
10120 reconsider = 1;
10123 /* Remove this entry from the chain. */
10124 *t = TREE_CHAIN (*t);
10126 maybe_template_tail = t;
10128 while (reconsider);
10130 return instantiated_something;
10133 /* Substitute ARGVEC into T, which is a list of initializers for
10134 either base class or a non-static data member. The TREE_PURPOSEs
10135 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10136 instantiate_decl. */
10138 static tree
10139 tsubst_initializer_list (t, argvec)
10140 tree t, argvec;
10142 tree first = NULL_TREE;
10143 tree *p = &first;
10145 for (; t; t = TREE_CHAIN (t))
10147 tree decl;
10148 tree init;
10149 tree val;
10151 decl = tsubst_copy (TREE_PURPOSE (t), argvec, /*complain=*/1,
10152 NULL_TREE);
10153 init = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1,
10154 NULL_TREE);
10156 if (!init)
10158 else if (TREE_CODE (init) == TREE_LIST)
10159 for (val = init; val; val = TREE_CHAIN (val))
10160 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
10161 else
10162 init = convert_from_reference (init);
10164 *p = build_tree_list (decl, init);
10165 p = &TREE_CHAIN (*p);
10167 return first;
10170 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10172 static void
10173 set_current_access_from_decl (decl)
10174 tree decl;
10176 if (TREE_PRIVATE (decl))
10177 current_access_specifier = access_private_node;
10178 else if (TREE_PROTECTED (decl))
10179 current_access_specifier = access_protected_node;
10180 else
10181 current_access_specifier = access_public_node;
10184 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10185 is the instantiation (which should have been created with
10186 start_enum) and ARGS are the template arguments to use. */
10188 static void
10189 tsubst_enum (tag, newtag, args)
10190 tree tag;
10191 tree newtag;
10192 tree args;
10194 tree e;
10196 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
10198 tree value;
10200 /* Note that in a template enum, the TREE_VALUE is the
10201 CONST_DECL, not the corresponding INTEGER_CST. */
10202 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
10203 args, /*complain=*/1,
10204 NULL_TREE);
10206 /* Give this enumeration constant the correct access. */
10207 set_current_access_from_decl (TREE_VALUE (e));
10209 /* Actually build the enumerator itself. */
10210 build_enumerator (TREE_PURPOSE (e), value, newtag);
10213 finish_enum (newtag);
10214 DECL_SOURCE_LINE (TYPE_NAME (newtag)) = DECL_SOURCE_LINE (TYPE_NAME (tag));
10215 DECL_SOURCE_FILE (TYPE_NAME (newtag)) = DECL_SOURCE_FILE (TYPE_NAME (tag));
10218 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10219 its type -- but without substituting the innermost set of template
10220 arguments. So, innermost set of template parameters will appear in
10221 the type. If CONTEXTP is non-NULL, then the partially substituted
10222 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10223 will be filled in with the substituted template parameters, if it
10224 is non-NULL. */
10226 tree
10227 get_mostly_instantiated_function_type (decl, contextp, tparmsp)
10228 tree decl;
10229 tree *contextp;
10230 tree *tparmsp;
10232 tree context = NULL_TREE;
10233 tree fn_type;
10234 tree tmpl;
10235 tree targs;
10236 tree tparms;
10237 int parm_depth;
10239 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10240 targs = DECL_TI_ARGS (decl);
10241 tparms = DECL_TEMPLATE_PARMS (tmpl);
10242 parm_depth = TMPL_PARMS_DEPTH (tparms);
10244 /* There should be as many levels of arguments as there are levels
10245 of parameters. */
10246 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10248 fn_type = TREE_TYPE (tmpl);
10249 if (DECL_STATIC_FUNCTION_P (decl))
10250 context = DECL_CONTEXT (decl);
10252 if (parm_depth == 1)
10253 /* No substitution is necessary. */
10255 else
10257 int i;
10258 tree partial_args;
10260 /* Replace the innermost level of the TARGS with NULL_TREEs to
10261 let tsubst know not to substitute for those parameters. */
10262 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
10263 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10264 SET_TMPL_ARGS_LEVEL (partial_args, i,
10265 TMPL_ARGS_LEVEL (targs, i));
10266 SET_TMPL_ARGS_LEVEL (partial_args,
10267 TMPL_ARGS_DEPTH (targs),
10268 make_tree_vec (DECL_NTPARMS (tmpl)));
10270 /* Now, do the (partial) substitution to figure out the
10271 appropriate function type. */
10272 fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE);
10273 if (DECL_STATIC_FUNCTION_P (decl))
10274 context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE);
10276 /* Substitute into the template parameters to obtain the real
10277 innermost set of parameters. This step is important if the
10278 innermost set of template parameters contains value
10279 parameters whose types depend on outer template parameters. */
10280 TREE_VEC_LENGTH (partial_args)--;
10281 tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1);
10284 if (contextp)
10285 *contextp = context;
10286 if (tparmsp)
10287 *tparmsp = tparms;
10289 return fn_type;
10292 /* Return truthvalue if we're processing a template different from
10293 the last one involved in diagnostics. */
10295 problematic_instantiation_changed ()
10297 return last_template_error_tick != tinst_level_tick;
10300 /* Remember current template involved in diagnostics. */
10301 void
10302 record_last_problematic_instantiation ()
10304 last_template_error_tick = tinst_level_tick;
10307 tree
10308 current_instantiation ()
10310 return current_tinst_level;
10313 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10314 type. Return zero for ok, non-zero for disallowed. If COMPLAIN is
10315 non-zero, then complain. */
10317 static int
10318 invalid_nontype_parm_type_p (type, complain)
10319 tree type;
10320 int complain;
10322 if (INTEGRAL_TYPE_P (type))
10323 return 0;
10324 else if (POINTER_TYPE_P (type))
10325 return 0;
10326 else if (TYPE_PTRMEM_P (type))
10327 return 0;
10328 else if (TYPE_PTRMEMFUNC_P (type))
10329 return 0;
10330 else if (!pedantic && TREE_CODE (type) == REAL_TYPE)
10332 if (complain)
10333 cp_deprecated ("floating point template constant parameter");
10334 return 0; /* GNU extension */
10336 else if (!pedantic && TREE_CODE (type) == COMPLEX_TYPE)
10338 if (complain)
10339 cp_deprecated ("complex template constant parameter");
10340 return 0; /* GNU extension */
10342 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10343 return 0;
10344 else if (TREE_CODE (type) == TYPENAME_TYPE)
10345 return 0;
10347 if (complain)
10348 cp_error ("`%#T' is not a valid type for a template constant parameter",
10349 type);
10350 return 1;