2002-02-19 Philip Blundell <philb@gnu.org>
[official-gcc.git] / gcc / cp / pt.c
blob4a57dd39d7a7590d9b38f4c05d62882127ea80c9
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 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"
32 #include "tree.h"
33 #include "flags.h"
34 #include "cp-tree.h"
35 #include "tree-inline.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 last_pending_template;
62 int processing_template_parmlist;
63 static int template_header_count;
65 static tree saved_trees;
66 static varray_type inline_parm_levels;
67 static size_t inline_parm_levels_used;
69 static tree current_tinst_level;
71 /* A map from local variable declarations in the body of the template
72 presently being instantiated to the corresponding instantiated
73 local variables. */
74 static htab_t local_specializations;
76 #define obstack_chunk_alloc xmalloc
77 #define obstack_chunk_free free
79 #define UNIFY_ALLOW_NONE 0
80 #define UNIFY_ALLOW_MORE_CV_QUAL 1
81 #define UNIFY_ALLOW_LESS_CV_QUAL 2
82 #define UNIFY_ALLOW_DERIVED 4
83 #define UNIFY_ALLOW_INTEGER 8
84 #define UNIFY_ALLOW_OUTER_LEVEL 16
85 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
86 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
87 #define UNIFY_ALLOW_MAX_CORRECTION 128
89 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
90 virtual, or a base class of a virtual
91 base. */
92 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
93 type with the desired type. */
95 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
96 unification_kind_t, int));
97 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
98 unification_kind_t, int));
99 static int unify PARAMS ((tree, tree, tree, tree, int));
100 static void add_pending_template PARAMS ((tree));
101 static void reopen_tinst_level PARAMS ((tree));
102 static tree classtype_mangled_name PARAMS ((tree));
103 static char *mangle_class_name_for_template PARAMS ((const char *,
104 tree, tree));
105 static tree tsubst_initializer_list PARAMS ((tree, tree));
106 static int list_eq PARAMS ((tree, tree));
107 static tree get_class_bindings PARAMS ((tree, tree, tree));
108 static tree coerce_template_parms PARAMS ((tree, tree, tree,
109 tsubst_flags_t, int));
110 static void tsubst_enum PARAMS ((tree, tree, tree));
111 static tree add_to_template_args PARAMS ((tree, tree));
112 static tree add_outermost_template_args PARAMS ((tree, tree));
113 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
114 tree*));
115 static int type_unification_real PARAMS ((tree, tree, tree, tree,
116 int, unification_kind_t, int, int));
117 static void note_template_header PARAMS ((int));
118 static tree maybe_fold_nontype_arg PARAMS ((tree));
119 static tree convert_nontype_argument PARAMS ((tree, tree));
120 static tree convert_template_argument PARAMS ((tree, tree, tree,
121 tsubst_flags_t, int, tree));
122 static tree get_bindings_overload PARAMS ((tree, tree, tree));
123 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*));
124 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
125 static int inline_needs_template_parms PARAMS ((tree));
126 static void push_inline_template_parms_recursive PARAMS ((tree, int));
127 static tree retrieve_specialization PARAMS ((tree, tree));
128 static tree retrieve_local_specialization PARAMS ((tree));
129 static tree register_specialization PARAMS ((tree, tree, tree));
130 static void register_local_specialization PARAMS ((tree, tree));
131 static int unregister_specialization PARAMS ((tree, tree));
132 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
133 static tree build_template_decl PARAMS ((tree, tree));
134 static int mark_template_parm PARAMS ((tree, void *));
135 static tree tsubst_friend_function PARAMS ((tree, tree));
136 static tree tsubst_friend_class PARAMS ((tree, tree));
137 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
138 static int template_decl_level PARAMS ((tree));
139 static tree maybe_get_template_decl_from_type_decl PARAMS ((tree));
140 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
141 static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t));
142 static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t));
143 static void regenerate_decl_from_template PARAMS ((tree, tree));
144 static tree most_specialized PARAMS ((tree, tree, tree));
145 static tree most_specialized_class PARAMS ((tree, tree));
146 static int template_class_depth_real PARAMS ((tree, int));
147 static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int));
148 static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t));
149 static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree));
150 static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree));
151 static void check_specialization_scope PARAMS ((void));
152 static tree process_partial_specialization PARAMS ((tree));
153 static void set_current_access_from_decl PARAMS ((tree));
154 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
155 static tree tsubst_call_declarator_parms PARAMS ((tree, tree,
156 tsubst_flags_t, 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, tsubst_flags_t,
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 copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
169 static void copy_default_args_to_explicit_spec PARAMS ((tree));
170 static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t));
172 /* Called once to initialize pt.c. */
174 void
175 init_pt ()
177 ggc_add_tree_root (&pending_templates, 1);
178 ggc_add_tree_root (&saved_trees, 1);
179 ggc_add_tree_root (&current_tinst_level, 1);
182 /* Do any processing required when DECL (a member template declaration
183 using TEMPLATE_PARAMETERS as its innermost parameter list) is
184 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
185 it is a specialization, in which case the DECL itself is returned. */
187 tree
188 finish_member_template_decl (decl)
189 tree decl;
191 if (decl == NULL_TREE || decl == void_type_node)
192 return NULL_TREE;
193 else if (decl == error_mark_node)
194 /* By returning NULL_TREE, the parser will just ignore this
195 declaration. We have already issued the error. */
196 return NULL_TREE;
197 else if (TREE_CODE (decl) == TREE_LIST)
199 /* Assume that the class is the only declspec. */
200 decl = TREE_VALUE (decl);
201 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
202 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
204 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
205 check_member_template (tmpl);
206 return tmpl;
208 return NULL_TREE;
210 else if (TREE_CODE (decl) == FIELD_DECL)
211 error ("data member `%D' cannot be a member template", decl);
212 else if (DECL_TEMPLATE_INFO (decl))
214 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
216 check_member_template (DECL_TI_TEMPLATE (decl));
217 return DECL_TI_TEMPLATE (decl);
219 else
220 return decl;
222 else
223 error ("invalid member template declaration `%D'", decl);
225 return error_mark_node;
228 /* Returns the template nesting level of the indicated class TYPE.
230 For example, in:
231 template <class T>
232 struct A
234 template <class U>
235 struct B {};
238 A<T>::B<U> has depth two, while A<T> has depth one.
239 Both A<T>::B<int> and A<int>::B<U> have depth one, if
240 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
241 specializations.
243 This function is guaranteed to return 0 if passed NULL_TREE so
244 that, for example, `template_class_depth (current_class_type)' is
245 always safe. */
247 static int
248 template_class_depth_real (type, count_specializations)
249 tree type;
250 int count_specializations;
252 int depth;
254 for (depth = 0;
255 type && TREE_CODE (type) != NAMESPACE_DECL;
256 type = (TREE_CODE (type) == FUNCTION_DECL)
257 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
259 if (TREE_CODE (type) != FUNCTION_DECL)
261 if (CLASSTYPE_TEMPLATE_INFO (type)
262 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
263 && ((count_specializations
264 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
265 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
266 ++depth;
268 else
270 if (DECL_TEMPLATE_INFO (type)
271 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
272 && ((count_specializations
273 && DECL_TEMPLATE_SPECIALIZATION (type))
274 || uses_template_parms (DECL_TI_ARGS (type))))
275 ++depth;
279 return depth;
282 /* Returns the template nesting level of the indicated class TYPE.
283 Like template_class_depth_real, but instantiations do not count in
284 the depth. */
286 int
287 template_class_depth (type)
288 tree type;
290 return template_class_depth_real (type, /*count_specializations=*/0);
293 /* Returns 1 if processing DECL as part of do_pending_inlines
294 needs us to push template parms. */
296 static int
297 inline_needs_template_parms (decl)
298 tree decl;
300 if (! DECL_TEMPLATE_INFO (decl))
301 return 0;
303 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
304 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
307 /* Subroutine of maybe_begin_member_template_processing.
308 Push the template parms in PARMS, starting from LEVELS steps into the
309 chain, and ending at the beginning, since template parms are listed
310 innermost first. */
312 static void
313 push_inline_template_parms_recursive (parmlist, levels)
314 tree parmlist;
315 int levels;
317 tree parms = TREE_VALUE (parmlist);
318 int i;
320 if (levels > 1)
321 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
323 ++processing_template_decl;
324 current_template_parms
325 = tree_cons (size_int (processing_template_decl),
326 parms, current_template_parms);
327 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
329 pushlevel (0);
330 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
332 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
333 my_friendly_assert (DECL_P (parm), 0);
335 switch (TREE_CODE (parm))
337 case TYPE_DECL:
338 case TEMPLATE_DECL:
339 pushdecl (parm);
340 break;
342 case PARM_DECL:
344 /* Make a CONST_DECL as is done in process_template_parm.
345 It is ugly that we recreate this here; the original
346 version built in process_template_parm is no longer
347 available. */
348 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
349 TREE_TYPE (parm));
350 DECL_ARTIFICIAL (decl) = 1;
351 DECL_INITIAL (decl) = DECL_INITIAL (parm);
352 SET_DECL_TEMPLATE_PARM_P (decl);
353 pushdecl (decl);
355 break;
357 default:
358 abort ();
363 /* Restore the template parameter context for a member template or
364 a friend template defined in a class definition. */
366 void
367 maybe_begin_member_template_processing (decl)
368 tree decl;
370 tree parms;
371 int levels = 0;
373 if (inline_needs_template_parms (decl))
375 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
376 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
378 if (DECL_TEMPLATE_SPECIALIZATION (decl))
380 --levels;
381 parms = TREE_CHAIN (parms);
384 push_inline_template_parms_recursive (parms, levels);
387 /* Remember how many levels of template parameters we pushed so that
388 we can pop them later. */
389 if (!inline_parm_levels)
390 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
391 if (inline_parm_levels_used == inline_parm_levels->num_elements)
392 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
393 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
394 ++inline_parm_levels_used;
397 /* Undo the effects of begin_member_template_processing. */
399 void
400 maybe_end_member_template_processing ()
402 int i;
404 if (!inline_parm_levels_used)
405 return;
407 --inline_parm_levels_used;
408 for (i = 0;
409 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
410 ++i)
412 --processing_template_decl;
413 current_template_parms = TREE_CHAIN (current_template_parms);
414 poplevel (0, 0, 0);
418 /* Returns non-zero iff T is a member template function. We must be
419 careful as in
421 template <class T> class C { void f(); }
423 Here, f is a template function, and a member, but not a member
424 template. This function does not concern itself with the origin of
425 T, only its present state. So if we have
427 template <class T> class C { template <class U> void f(U); }
429 then neither C<int>::f<char> nor C<T>::f<double> is considered
430 to be a member template. But, `template <class U> void
431 C<int>::f(U)' is considered a member template. */
434 is_member_template (t)
435 tree t;
437 if (!DECL_FUNCTION_TEMPLATE_P (t))
438 /* Anything that isn't a function or a template function is
439 certainly not a member template. */
440 return 0;
442 /* A local class can't have member templates. */
443 if (decl_function_context (t))
444 return 0;
446 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
447 /* If there are more levels of template parameters than
448 there are template classes surrounding the declaration,
449 then we have a member template. */
450 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
451 template_class_depth (DECL_CONTEXT (t))));
454 #if 0 /* UNUSED */
455 /* Returns non-zero iff T is a member template class. See
456 is_member_template for a description of what precisely constitutes
457 a member template. */
460 is_member_template_class (t)
461 tree t;
463 if (!DECL_CLASS_TEMPLATE_P (t))
464 /* Anything that isn't a class template, is certainly not a member
465 template. */
466 return 0;
468 if (!DECL_CLASS_SCOPE_P (t))
469 /* Anything whose context isn't a class type is surely not a
470 member template. */
471 return 0;
473 /* If there are more levels of template parameters than there are
474 template classes surrounding the declaration, then we have a
475 member template. */
476 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
477 template_class_depth (DECL_CONTEXT (t)));
479 #endif
481 /* Return a new template argument vector which contains all of ARGS,
482 but has as its innermost set of arguments the EXTRA_ARGS. */
484 static tree
485 add_to_template_args (args, extra_args)
486 tree args;
487 tree extra_args;
489 tree new_args;
490 int extra_depth;
491 int i;
492 int j;
494 extra_depth = TMPL_ARGS_DEPTH (extra_args);
495 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
497 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
498 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
500 for (j = 1; j <= extra_depth; ++j, ++i)
501 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
503 return new_args;
506 /* Like add_to_template_args, but only the outermost ARGS are added to
507 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
508 (EXTRA_ARGS) levels are added. This function is used to combine
509 the template arguments from a partial instantiation with the
510 template arguments used to attain the full instantiation from the
511 partial instantiation. */
513 static tree
514 add_outermost_template_args (args, extra_args)
515 tree args;
516 tree extra_args;
518 tree new_args;
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
525 /* If *all* the new arguments will be the EXTRA_ARGS, just return
526 them. */
527 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
528 return extra_args;
530 /* For the moment, we make ARGS look like it contains fewer levels. */
531 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
533 new_args = add_to_template_args (args, extra_args);
535 /* Now, we restore ARGS to its full dimensions. */
536 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
538 return new_args;
541 /* Return the N levels of innermost template arguments from the ARGS. */
543 tree
544 get_innermost_template_args (args, n)
545 tree args;
546 int n;
548 tree new_args;
549 int extra_levels;
550 int i;
552 my_friendly_assert (n >= 0, 20000603);
554 /* If N is 1, just return the innermost set of template arguments. */
555 if (n == 1)
556 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
558 /* If we're not removing anything, just return the arguments we were
559 given. */
560 extra_levels = TMPL_ARGS_DEPTH (args) - n;
561 my_friendly_assert (extra_levels >= 0, 20000603);
562 if (extra_levels == 0)
563 return args;
565 /* Make a new set of arguments, not containing the outer arguments. */
566 new_args = make_tree_vec (n);
567 for (i = 1; i <= n; ++i)
568 SET_TMPL_ARGS_LEVEL (new_args, i,
569 TMPL_ARGS_LEVEL (args, i + extra_levels));
571 return new_args;
574 /* We've got a template header coming up; push to a new level for storing
575 the parms. */
577 void
578 begin_template_parm_list ()
580 /* We use a non-tag-transparent scope here, which causes pushtag to
581 put tags in this scope, rather than in the enclosing class or
582 namespace scope. This is the right thing, since we want
583 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
584 global template class, push_template_decl handles putting the
585 TEMPLATE_DECL into top-level scope. For a nested template class,
586 e.g.:
588 template <class T> struct S1 {
589 template <class T> struct S2 {};
592 pushtag contains special code to call pushdecl_with_scope on the
593 TEMPLATE_DECL for S2. */
594 begin_scope (sk_template_parms);
595 ++processing_template_decl;
596 ++processing_template_parmlist;
597 note_template_header (0);
600 /* This routine is called when a specialization is declared. If it is
601 illegal to declare a specialization here, an error is reported. */
603 static void
604 check_specialization_scope ()
606 tree scope = current_scope ();
608 /* [temp.expl.spec]
610 An explicit specialization shall be declared in the namespace of
611 which the template is a member, or, for member templates, in the
612 namespace of which the enclosing class or enclosing class
613 template is a member. An explicit specialization of a member
614 function, member class or static data member of a class template
615 shall be declared in the namespace of which the class template
616 is a member. */
617 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
618 error ("explicit specialization in non-namespace scope `%D'",
619 scope);
621 /* [temp.expl.spec]
623 In an explicit specialization declaration for a member of a class
624 template or a member template that appears in namespace scope,
625 the member template and some of its enclosing class templates may
626 remain unspecialized, except that the declaration shall not
627 explicitly specialize a class member template if its enclosing
628 class templates are not explicitly specialized as well. */
629 if (current_template_parms)
630 error ("enclosing class templates are not explicitly specialized");
633 /* We've just seen template <>. */
635 void
636 begin_specialization ()
638 begin_scope (sk_template_spec);
639 note_template_header (1);
640 check_specialization_scope ();
643 /* Called at then end of processing a declaration preceded by
644 template<>. */
646 void
647 end_specialization ()
649 finish_scope ();
650 reset_specialization ();
653 /* Any template <>'s that we have seen thus far are not referring to a
654 function specialization. */
656 void
657 reset_specialization ()
659 processing_specialization = 0;
660 template_header_count = 0;
663 /* We've just seen a template header. If SPECIALIZATION is non-zero,
664 it was of the form template <>. */
666 static void
667 note_template_header (specialization)
668 int specialization;
670 processing_specialization = specialization;
671 template_header_count++;
674 /* We're beginning an explicit instantiation. */
676 void
677 begin_explicit_instantiation ()
679 ++processing_explicit_instantiation;
683 void
684 end_explicit_instantiation ()
686 my_friendly_assert(processing_explicit_instantiation > 0, 0);
687 --processing_explicit_instantiation;
690 /* The TYPE is being declared. If it is a template type, that means it
691 is a partial specialization. Do appropriate error-checking. */
693 void
694 maybe_process_partial_specialization (type)
695 tree type;
697 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
699 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
700 && !COMPLETE_TYPE_P (type))
702 if (current_namespace
703 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
705 pedwarn ("specializing `%#T' in different namespace", type);
706 cp_pedwarn_at (" from definition of `%#D'",
707 CLASSTYPE_TI_TEMPLATE (type));
709 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
710 if (processing_template_decl)
711 push_template_decl (TYPE_MAIN_DECL (type));
713 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
714 error ("specialization of `%T' after instantiation", type);
716 else if (processing_specialization)
717 error ("explicit specialization of non-template `%T'", type);
720 /* Retrieve the specialization (in the sense of [temp.spec] - a
721 specialization is either an instantiation or an explicit
722 specialization) of TMPL for the given template ARGS. If there is
723 no such specialization, return NULL_TREE. The ARGS are a vector of
724 arguments, or a vector of vectors of arguments, in the case of
725 templates with more than one level of parameters. */
727 static tree
728 retrieve_specialization (tmpl, args)
729 tree tmpl;
730 tree args;
732 tree s;
734 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
736 /* There should be as many levels of arguments as there are
737 levels of parameters. */
738 my_friendly_assert (TMPL_ARGS_DEPTH (args)
739 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
742 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
743 s != NULL_TREE;
744 s = TREE_CHAIN (s))
745 if (comp_template_args (TREE_PURPOSE (s), args))
746 return TREE_VALUE (s);
748 return NULL_TREE;
751 /* Like retrieve_specialization, but for local declarations. */
753 static tree
754 retrieve_local_specialization (tmpl)
755 tree tmpl;
757 return (tree) htab_find (local_specializations, tmpl);
760 /* Returns non-zero iff DECL is a specialization of TMPL. */
763 is_specialization_of (decl, tmpl)
764 tree decl;
765 tree tmpl;
767 tree t;
769 if (TREE_CODE (decl) == FUNCTION_DECL)
771 for (t = decl;
772 t != NULL_TREE;
773 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
774 if (t == tmpl)
775 return 1;
777 else
779 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
781 for (t = TREE_TYPE (decl);
782 t != NULL_TREE;
783 t = CLASSTYPE_USE_TEMPLATE (t)
784 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
785 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
786 return 1;
789 return 0;
792 /* Register the specialization SPEC as a specialization of TMPL with
793 the indicated ARGS. Returns SPEC, or an equivalent prior
794 declaration, if available. */
796 static tree
797 register_specialization (spec, tmpl, args)
798 tree spec;
799 tree tmpl;
800 tree args;
802 tree s;
804 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
806 if (TREE_CODE (spec) == FUNCTION_DECL
807 && uses_template_parms (DECL_TI_ARGS (spec)))
808 /* This is the FUNCTION_DECL for a partial instantiation. Don't
809 register it; we want the corresponding TEMPLATE_DECL instead.
810 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
811 the more obvious `uses_template_parms (spec)' to avoid problems
812 with default function arguments. In particular, given
813 something like this:
815 template <class T> void f(T t1, T t = T())
817 the default argument expression is not substituted for in an
818 instantiation unless and until it is actually needed. */
819 return spec;
821 /* There should be as many levels of arguments as there are
822 levels of parameters. */
823 my_friendly_assert (TMPL_ARGS_DEPTH (args)
824 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
827 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
828 s != NULL_TREE;
829 s = TREE_CHAIN (s))
831 tree fn = TREE_VALUE (s);
833 /* We can sometimes try to re-register a specialization that we've
834 already got. In particular, regenerate_decl_from_template
835 calls duplicate_decls which will update the specialization
836 list. But, we'll still get called again here anyhow. It's
837 more convenient to simply allow this than to try to prevent it. */
838 if (fn == spec)
839 return spec;
840 else if (comp_template_args (TREE_PURPOSE (s), args))
842 if (DECL_TEMPLATE_SPECIALIZATION (spec))
844 if (DECL_TEMPLATE_INSTANTIATION (fn))
846 if (TREE_USED (fn)
847 || DECL_EXPLICIT_INSTANTIATION (fn))
849 error ("specialization of %D after instantiation",
850 fn);
851 return spec;
853 else
855 /* This situation should occur only if the first
856 specialization is an implicit instantiation,
857 the second is an explicit specialization, and
858 the implicit instantiation has not yet been
859 used. That situation can occur if we have
860 implicitly instantiated a member function and
861 then specialized it later.
863 We can also wind up here if a friend
864 declaration that looked like an instantiation
865 turns out to be a specialization:
867 template <class T> void foo(T);
868 class S { friend void foo<>(int) };
869 template <> void foo(int);
871 We transform the existing DECL in place so that
872 any pointers to it become pointers to the
873 updated declaration.
875 If there was a definition for the template, but
876 not for the specialization, we want this to
877 look as if there is no definition, and vice
878 versa. */
879 DECL_INITIAL (fn) = NULL_TREE;
880 duplicate_decls (spec, fn);
882 return fn;
885 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
887 duplicate_decls (spec, fn);
888 return fn;
894 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
895 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
897 return spec;
900 /* Unregister the specialization SPEC as a specialization of TMPL.
901 Returns nonzero if the SPEC was listed as a specialization of
902 TMPL. */
904 static int
905 unregister_specialization (spec, tmpl)
906 tree spec;
907 tree tmpl;
909 tree* s;
911 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
912 *s != NULL_TREE;
913 s = &TREE_CHAIN (*s))
914 if (TREE_VALUE (*s) == spec)
916 *s = TREE_CHAIN (*s);
917 return 1;
920 return 0;
923 /* Like register_specialization, but for local declarations. We are
924 registering SPEC, an instantiation of TMPL. */
926 static void
927 register_local_specialization (spec, tmpl)
928 tree spec;
929 tree tmpl;
931 void **slot;
933 slot = htab_find_slot (local_specializations, tmpl, INSERT);
934 *slot = spec;
937 /* Print the list of candidate FNS in an error message. */
939 void
940 print_candidates (fns)
941 tree fns;
943 tree fn;
945 const char *str = "candidates are:";
947 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
949 tree f;
951 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
952 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
953 str = " ";
957 /* Returns the template (one of the functions given by TEMPLATE_ID)
958 which can be specialized to match the indicated DECL with the
959 explicit template args given in TEMPLATE_ID. The DECL may be
960 NULL_TREE if none is available. In that case, the functions in
961 TEMPLATE_ID are non-members.
963 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
964 specialization of a member template.
966 The template args (those explicitly specified and those deduced)
967 are output in a newly created vector *TARGS_OUT.
969 If it is impossible to determine the result, an error message is
970 issued. The error_mark_node is returned to indicate failure. */
972 static tree
973 determine_specialization (template_id, decl, targs_out,
974 need_member_template)
975 tree template_id;
976 tree decl;
977 tree* targs_out;
978 int need_member_template;
980 tree fns;
981 tree targs;
982 tree explicit_targs;
983 tree candidates = NULL_TREE;
984 tree templates = NULL_TREE;
986 *targs_out = NULL_TREE;
988 if (template_id == error_mark_node)
989 return error_mark_node;
991 fns = TREE_OPERAND (template_id, 0);
992 explicit_targs = TREE_OPERAND (template_id, 1);
994 if (fns == error_mark_node)
995 return error_mark_node;
997 /* Check for baselinks. */
998 if (BASELINK_P (fns))
999 fns = TREE_VALUE (fns);
1001 if (!is_overloaded_fn (fns))
1003 error ("`%D' is not a function template", fns);
1004 return error_mark_node;
1007 for (; fns; fns = OVL_NEXT (fns))
1009 tree tmpl;
1011 tree fn = OVL_CURRENT (fns);
1013 if (TREE_CODE (fn) == TEMPLATE_DECL)
1014 /* DECL might be a specialization of FN. */
1015 tmpl = fn;
1016 else if (need_member_template)
1017 /* FN is an ordinary member function, and we need a
1018 specialization of a member template. */
1019 continue;
1020 else if (TREE_CODE (fn) != FUNCTION_DECL)
1021 /* We can get IDENTIFIER_NODEs here in certain erroneous
1022 cases. */
1023 continue;
1024 else if (!DECL_FUNCTION_MEMBER_P (fn))
1025 /* This is just an ordinary non-member function. Nothing can
1026 be a specialization of that. */
1027 continue;
1028 else if (DECL_ARTIFICIAL (fn))
1029 /* Cannot specialize functions that are created implicitly. */
1030 continue;
1031 else
1033 tree decl_arg_types;
1035 /* This is an ordinary member function. However, since
1036 we're here, we can assume it's enclosing class is a
1037 template class. For example,
1039 template <typename T> struct S { void f(); };
1040 template <> void S<int>::f() {}
1042 Here, S<int>::f is a non-template, but S<int> is a
1043 template class. If FN has the same type as DECL, we
1044 might be in business. */
1046 if (!DECL_TEMPLATE_INFO (fn))
1047 /* Its enclosing class is an explicit specialization
1048 of a template class. This is not a candidate. */
1049 continue;
1051 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1052 TREE_TYPE (TREE_TYPE (fn))))
1053 /* The return types differ. */
1054 continue;
1056 /* Adjust the type of DECL in case FN is a static member. */
1057 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1058 if (DECL_STATIC_FUNCTION_P (fn)
1059 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1060 decl_arg_types = TREE_CHAIN (decl_arg_types);
1062 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1063 decl_arg_types))
1064 /* They match! */
1065 candidates = tree_cons (NULL_TREE, fn, candidates);
1067 continue;
1070 /* See whether this function might be a specialization of this
1071 template. */
1072 targs = get_bindings (tmpl, decl, explicit_targs);
1074 if (!targs)
1075 /* We cannot deduce template arguments that when used to
1076 specialize TMPL will produce DECL. */
1077 continue;
1079 /* Save this template, and the arguments deduced. */
1080 templates = tree_cons (targs, tmpl, templates);
1083 if (templates && TREE_CHAIN (templates))
1085 /* We have:
1087 [temp.expl.spec]
1089 It is possible for a specialization with a given function
1090 signature to be instantiated from more than one function
1091 template. In such cases, explicit specification of the
1092 template arguments must be used to uniquely identify the
1093 function template specialization being specialized.
1095 Note that here, there's no suggestion that we're supposed to
1096 determine which of the candidate templates is most
1097 specialized. However, we, also have:
1099 [temp.func.order]
1101 Partial ordering of overloaded function template
1102 declarations is used in the following contexts to select
1103 the function template to which a function template
1104 specialization refers:
1106 -- when an explicit specialization refers to a function
1107 template.
1109 So, we do use the partial ordering rules, at least for now.
1110 This extension can only serve to make illegal programs legal,
1111 so it's safe. And, there is strong anecdotal evidence that
1112 the committee intended the partial ordering rules to apply;
1113 the EDG front-end has that behavior, and John Spicer claims
1114 that the committee simply forgot to delete the wording in
1115 [temp.expl.spec]. */
1116 tree tmpl = most_specialized (templates, decl, explicit_targs);
1117 if (tmpl && tmpl != error_mark_node)
1119 targs = get_bindings (tmpl, decl, explicit_targs);
1120 templates = tree_cons (targs, tmpl, NULL_TREE);
1124 if (templates == NULL_TREE && candidates == NULL_TREE)
1126 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1127 template_id, decl);
1128 return error_mark_node;
1130 else if ((templates && TREE_CHAIN (templates))
1131 || (candidates && TREE_CHAIN (candidates))
1132 || (templates && candidates))
1134 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1135 template_id, decl);
1136 chainon (candidates, templates);
1137 print_candidates (candidates);
1138 return error_mark_node;
1141 /* We have one, and exactly one, match. */
1142 if (candidates)
1144 /* It was a specialization of an ordinary member function in a
1145 template class. */
1146 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1147 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1150 /* It was a specialization of a template. */
1151 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1152 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1154 *targs_out = copy_node (targs);
1155 SET_TMPL_ARGS_LEVEL (*targs_out,
1156 TMPL_ARGS_DEPTH (*targs_out),
1157 TREE_PURPOSE (templates));
1159 else
1160 *targs_out = TREE_PURPOSE (templates);
1161 return TREE_VALUE (templates);
1164 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1165 but with the default argument values filled in from those in the
1166 TMPL_TYPES. */
1168 static tree
1169 copy_default_args_to_explicit_spec_1 (spec_types,
1170 tmpl_types)
1171 tree spec_types;
1172 tree tmpl_types;
1174 tree new_spec_types;
1176 if (!spec_types)
1177 return NULL_TREE;
1179 if (spec_types == void_list_node)
1180 return void_list_node;
1182 /* Substitute into the rest of the list. */
1183 new_spec_types =
1184 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1185 TREE_CHAIN (tmpl_types));
1187 /* Add the default argument for this parameter. */
1188 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1189 TREE_VALUE (spec_types),
1190 new_spec_types);
1193 /* DECL is an explicit specialization. Replicate default arguments
1194 from the template it specializes. (That way, code like:
1196 template <class T> void f(T = 3);
1197 template <> void f(double);
1198 void g () { f (); }
1200 works, as required.) An alternative approach would be to look up
1201 the correct default arguments at the call-site, but this approach
1202 is consistent with how implicit instantiations are handled. */
1204 static void
1205 copy_default_args_to_explicit_spec (decl)
1206 tree decl;
1208 tree tmpl;
1209 tree spec_types;
1210 tree tmpl_types;
1211 tree new_spec_types;
1212 tree old_type;
1213 tree new_type;
1214 tree t;
1215 tree object_type = NULL_TREE;
1216 tree in_charge = NULL_TREE;
1217 tree vtt = NULL_TREE;
1219 /* See if there's anything we need to do. */
1220 tmpl = DECL_TI_TEMPLATE (decl);
1221 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1222 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1223 if (TREE_PURPOSE (t))
1224 break;
1225 if (!t)
1226 return;
1228 old_type = TREE_TYPE (decl);
1229 spec_types = TYPE_ARG_TYPES (old_type);
1231 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1233 /* Remove the this pointer, but remember the object's type for
1234 CV quals. */
1235 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1236 spec_types = TREE_CHAIN (spec_types);
1237 tmpl_types = TREE_CHAIN (tmpl_types);
1239 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1241 /* DECL may contain more parameters than TMPL due to the extra
1242 in-charge parameter in constructors and destructors. */
1243 in_charge = spec_types;
1244 spec_types = TREE_CHAIN (spec_types);
1246 if (DECL_HAS_VTT_PARM_P (decl))
1248 vtt = spec_types;
1249 spec_types = TREE_CHAIN (spec_types);
1253 /* Compute the merged default arguments. */
1254 new_spec_types =
1255 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1257 /* Compute the new FUNCTION_TYPE. */
1258 if (object_type)
1260 if (vtt)
1261 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1262 TREE_VALUE (vtt),
1263 new_spec_types);
1265 if (in_charge)
1266 /* Put the in-charge parameter back. */
1267 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1268 TREE_VALUE (in_charge),
1269 new_spec_types);
1271 new_type = build_cplus_method_type (object_type,
1272 TREE_TYPE (old_type),
1273 new_spec_types);
1275 else
1276 new_type = build_function_type (TREE_TYPE (old_type),
1277 new_spec_types);
1278 new_type = build_type_attribute_variant (new_type,
1279 TYPE_ATTRIBUTES (old_type));
1280 new_type = build_exception_variant (new_type,
1281 TYPE_RAISES_EXCEPTIONS (old_type));
1282 TREE_TYPE (decl) = new_type;
1285 /* Check to see if the function just declared, as indicated in
1286 DECLARATOR, and in DECL, is a specialization of a function
1287 template. We may also discover that the declaration is an explicit
1288 instantiation at this point.
1290 Returns DECL, or an equivalent declaration that should be used
1291 instead if all goes well. Issues an error message if something is
1292 amiss. Returns error_mark_node if the error is not easily
1293 recoverable.
1295 FLAGS is a bitmask consisting of the following flags:
1297 2: The function has a definition.
1298 4: The function is a friend.
1300 The TEMPLATE_COUNT is the number of references to qualifying
1301 template classes that appeared in the name of the function. For
1302 example, in
1304 template <class T> struct S { void f(); };
1305 void S<int>::f();
1307 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1308 classes are not counted in the TEMPLATE_COUNT, so that in
1310 template <class T> struct S {};
1311 template <> struct S<int> { void f(); }
1312 template <> void S<int>::f();
1314 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1315 illegal; there should be no template <>.)
1317 If the function is a specialization, it is marked as such via
1318 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1319 is set up correctly, and it is added to the list of specializations
1320 for that template. */
1322 tree
1323 check_explicit_specialization (declarator, decl, template_count, flags)
1324 tree declarator;
1325 tree decl;
1326 int template_count;
1327 int flags;
1329 int have_def = flags & 2;
1330 int is_friend = flags & 4;
1331 int specialization = 0;
1332 int explicit_instantiation = 0;
1333 int member_specialization = 0;
1334 tree ctype = DECL_CLASS_CONTEXT (decl);
1335 tree dname = DECL_NAME (decl);
1336 tmpl_spec_kind tsk;
1338 tsk = current_tmpl_spec_kind (template_count);
1340 switch (tsk)
1342 case tsk_none:
1343 if (processing_specialization)
1345 specialization = 1;
1346 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1348 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1350 if (is_friend)
1351 /* This could be something like:
1353 template <class T> void f(T);
1354 class S { friend void f<>(int); } */
1355 specialization = 1;
1356 else
1358 /* This case handles bogus declarations like template <>
1359 template <class T> void f<int>(); */
1361 error ("template-id `%D' in declaration of primary template",
1362 declarator);
1363 return decl;
1366 break;
1368 case tsk_invalid_member_spec:
1369 /* The error has already been reported in
1370 check_specialization_scope. */
1371 return error_mark_node;
1373 case tsk_invalid_expl_inst:
1374 error ("template parameter list used in explicit instantiation");
1376 /* Fall through. */
1378 case tsk_expl_inst:
1379 if (have_def)
1380 error ("definition provided for explicit instantiation");
1382 explicit_instantiation = 1;
1383 break;
1385 case tsk_excessive_parms:
1386 error ("too many template parameter lists in declaration of `%D'",
1387 decl);
1388 return error_mark_node;
1390 /* Fall through. */
1391 case tsk_expl_spec:
1392 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1393 if (ctype)
1394 member_specialization = 1;
1395 else
1396 specialization = 1;
1397 break;
1399 case tsk_insufficient_parms:
1400 if (template_header_count)
1402 error("too few template parameter lists in declaration of `%D'",
1403 decl);
1404 return decl;
1406 else if (ctype != NULL_TREE
1407 && !TYPE_BEING_DEFINED (ctype)
1408 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1409 && !is_friend)
1411 /* For backwards compatibility, we accept:
1413 template <class T> struct S { void f(); };
1414 void S<int>::f() {} // Missing template <>
1416 That used to be legal C++. */
1417 if (pedantic)
1418 pedwarn
1419 ("explicit specialization not preceded by `template <>'");
1420 specialization = 1;
1421 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1423 break;
1425 case tsk_template:
1426 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1428 /* This case handles bogus declarations like template <>
1429 template <class T> void f<int>(); */
1431 if (uses_template_parms (declarator))
1432 error ("partial specialization `%D' of function template",
1433 declarator);
1434 else
1435 error ("template-id `%D' in declaration of primary template",
1436 declarator);
1437 return decl;
1440 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1441 /* This is a specialization of a member template, without
1442 specialization the containing class. Something like:
1444 template <class T> struct S {
1445 template <class U> void f (U);
1447 template <> template <class U> void S<int>::f(U) {}
1449 That's a specialization -- but of the entire template. */
1450 specialization = 1;
1451 break;
1453 default:
1454 abort ();
1457 if (specialization || member_specialization)
1459 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1460 for (; t; t = TREE_CHAIN (t))
1461 if (TREE_PURPOSE (t))
1463 pedwarn
1464 ("default argument specified in explicit specialization");
1465 break;
1467 if (current_lang_name == lang_name_c)
1468 error ("template specialization with C linkage");
1471 if (specialization || member_specialization || explicit_instantiation)
1473 tree tmpl = NULL_TREE;
1474 tree targs = NULL_TREE;
1476 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1477 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1479 tree fns;
1481 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1483 if (!ctype)
1484 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1485 else
1486 fns = dname;
1488 declarator =
1489 lookup_template_function (fns, NULL_TREE);
1492 if (declarator == error_mark_node)
1493 return error_mark_node;
1495 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1497 if (!explicit_instantiation)
1498 /* A specialization in class scope. This is illegal,
1499 but the error will already have been flagged by
1500 check_specialization_scope. */
1501 return error_mark_node;
1502 else
1504 /* It's not legal to write an explicit instantiation in
1505 class scope, e.g.:
1507 class C { template void f(); }
1509 This case is caught by the parser. However, on
1510 something like:
1512 template class C { void f(); };
1514 (which is illegal) we can get here. The error will be
1515 issued later. */
1519 return decl;
1521 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1523 /* A friend declaration. We can't do much, because we don't
1524 know what this resolves to, yet. */
1525 my_friendly_assert (is_friend != 0, 0);
1526 my_friendly_assert (!explicit_instantiation, 0);
1527 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1528 return decl;
1530 else if (ctype != NULL_TREE
1531 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1532 IDENTIFIER_NODE))
1534 /* Find the list of functions in ctype that have the same
1535 name as the declared function. */
1536 tree name = TREE_OPERAND (declarator, 0);
1537 tree fns = NULL_TREE;
1538 int idx;
1540 if (name == constructor_name (ctype)
1541 || name == constructor_name_full (ctype))
1543 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1545 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1546 : !TYPE_HAS_DESTRUCTOR (ctype))
1548 /* From [temp.expl.spec]:
1550 If such an explicit specialization for the member
1551 of a class template names an implicitly-declared
1552 special member function (clause _special_), the
1553 program is ill-formed.
1555 Similar language is found in [temp.explicit]. */
1556 error ("specialization of implicitly-declared special member function");
1557 return error_mark_node;
1560 name = is_constructor ? ctor_identifier : dtor_identifier;
1563 if (!DECL_CONV_FN_P (decl))
1565 idx = lookup_fnfields_1 (ctype, name);
1566 if (idx >= 0)
1567 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1569 else
1571 tree methods;
1573 /* For a type-conversion operator, we cannot do a
1574 name-based lookup. We might be looking for `operator
1575 int' which will be a specialization of `operator T'.
1576 So, we find *all* the conversion operators, and then
1577 select from them. */
1578 fns = NULL_TREE;
1580 methods = CLASSTYPE_METHOD_VEC (ctype);
1581 if (methods)
1582 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1584 tree ovl = TREE_VEC_ELT (methods, idx);
1586 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1587 /* There are no more conversion functions. */
1588 break;
1590 /* Glue all these conversion functions together
1591 with those we already have. */
1592 for (; ovl; ovl = OVL_NEXT (ovl))
1593 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1597 if (fns == NULL_TREE)
1599 error ("no member function `%D' declared in `%T'",
1600 name, ctype);
1601 return error_mark_node;
1603 else
1604 TREE_OPERAND (declarator, 0) = fns;
1607 /* Figure out what exactly is being specialized at this point.
1608 Note that for an explicit instantiation, even one for a
1609 member function, we cannot tell apriori whether the
1610 instantiation is for a member template, or just a member
1611 function of a template class. Even if a member template is
1612 being instantiated, the member template arguments may be
1613 elided if they can be deduced from the rest of the
1614 declaration. */
1615 tmpl = determine_specialization (declarator, decl,
1616 &targs,
1617 member_specialization);
1619 if (!tmpl || tmpl == error_mark_node)
1620 /* We couldn't figure out what this declaration was
1621 specializing. */
1622 return error_mark_node;
1623 else
1625 tree gen_tmpl = most_general_template (tmpl);
1627 if (explicit_instantiation)
1629 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1630 is done by do_decl_instantiation later. */
1632 int arg_depth = TMPL_ARGS_DEPTH (targs);
1633 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1635 if (arg_depth > parm_depth)
1637 /* If TMPL is not the most general template (for
1638 example, if TMPL is a friend template that is
1639 injected into namespace scope), then there will
1640 be too many levels of TARGS. Remove some of them
1641 here. */
1642 int i;
1643 tree new_targs;
1645 new_targs = make_tree_vec (parm_depth);
1646 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1647 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1648 = TREE_VEC_ELT (targs, i);
1649 targs = new_targs;
1652 return instantiate_template (tmpl, targs);
1655 /* If this is a specialization of a member template of a
1656 template class. In we want to return the TEMPLATE_DECL,
1657 not the specialization of it. */
1658 if (tsk == tsk_template)
1660 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1661 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1662 return tmpl;
1665 /* If we thought that the DECL was a member function, but it
1666 turns out to be specializing a static member function,
1667 make DECL a static member function as well. */
1668 if (DECL_STATIC_FUNCTION_P (tmpl)
1669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1671 revert_static_member_fn (decl);
1672 last_function_parms = TREE_CHAIN (last_function_parms);
1675 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1676 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1678 /* Inherit default function arguments from the template
1679 DECL is specializing. */
1680 copy_default_args_to_explicit_spec (decl);
1682 /* This specialization has the same protection as the
1683 template it specializes. */
1684 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1685 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1687 if (is_friend && !have_def)
1688 /* This is not really a declaration of a specialization.
1689 It's just the name of an instantiation. But, it's not
1690 a request for an instantiation, either. */
1691 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1692 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1693 /* This is indeed a specialization. In case of constructors
1694 and destructors, we need in-charge and not-in-charge
1695 versions in V3 ABI. */
1696 clone_function_decl (decl, /*update_method_vec_p=*/0);
1698 /* Register this specialization so that we can find it
1699 again. */
1700 decl = register_specialization (decl, gen_tmpl, targs);
1704 return decl;
1707 /* TYPE is being declared. Verify that the use of template headers
1708 and such is reasonable. Issue error messages if not. */
1710 void
1711 maybe_check_template_type (type)
1712 tree type;
1714 if (template_header_count)
1716 /* We are in the scope of some `template <...>' header. */
1718 int context_depth
1719 = template_class_depth_real (TYPE_CONTEXT (type),
1720 /*count_specializations=*/1);
1722 if (template_header_count <= context_depth)
1723 /* This is OK; the template headers are for the context. We
1724 are actually too lenient here; like
1725 check_explicit_specialization we should consider the number
1726 of template types included in the actual declaration. For
1727 example,
1729 template <class T> struct S {
1730 template <class U> template <class V>
1731 struct I {};
1734 is illegal, but:
1736 template <class T> struct S {
1737 template <class U> struct I;
1740 template <class T> template <class U.
1741 struct S<T>::I {};
1743 is not. */
1745 else if (template_header_count > context_depth + 1)
1746 /* There are two many template parameter lists. */
1747 error ("too many template parameter lists in declaration of `%T'", type);
1751 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1752 parameters. These are represented in the same format used for
1753 DECL_TEMPLATE_PARMS. */
1755 int comp_template_parms (parms1, parms2)
1756 tree parms1;
1757 tree parms2;
1759 tree p1;
1760 tree p2;
1762 if (parms1 == parms2)
1763 return 1;
1765 for (p1 = parms1, p2 = parms2;
1766 p1 != NULL_TREE && p2 != NULL_TREE;
1767 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1769 tree t1 = TREE_VALUE (p1);
1770 tree t2 = TREE_VALUE (p2);
1771 int i;
1773 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1774 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1776 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1777 return 0;
1779 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1781 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1782 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1784 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1785 return 0;
1787 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1788 continue;
1789 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1790 return 0;
1794 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1795 /* One set of parameters has more parameters lists than the
1796 other. */
1797 return 0;
1799 return 1;
1802 /* Complain if DECL shadows a template parameter.
1804 [temp.local]: A template-parameter shall not be redeclared within its
1805 scope (including nested scopes). */
1807 void
1808 check_template_shadow (decl)
1809 tree decl;
1811 tree olddecl;
1813 /* If we're not in a template, we can't possibly shadow a template
1814 parameter. */
1815 if (!current_template_parms)
1816 return;
1818 /* Figure out what we're shadowing. */
1819 if (TREE_CODE (decl) == OVERLOAD)
1820 decl = OVL_CURRENT (decl);
1821 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1823 /* If there's no previous binding for this name, we're not shadowing
1824 anything, let alone a template parameter. */
1825 if (!olddecl)
1826 return;
1828 /* If we're not shadowing a template parameter, we're done. Note
1829 that OLDDECL might be an OVERLOAD (or perhaps even an
1830 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1831 node. */
1832 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1833 return;
1835 /* We check for decl != olddecl to avoid bogus errors for using a
1836 name inside a class. We check TPFI to avoid duplicate errors for
1837 inline member templates. */
1838 if (decl == olddecl
1839 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1840 return;
1842 cp_error_at ("declaration of `%#D'", decl);
1843 cp_error_at (" shadows template parm `%#D'", olddecl);
1846 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1847 ORIG_LEVEL, DECL, and TYPE. */
1849 static tree
1850 build_template_parm_index (index, level, orig_level, decl, type)
1851 int index;
1852 int level;
1853 int orig_level;
1854 tree decl;
1855 tree type;
1857 tree t = make_node (TEMPLATE_PARM_INDEX);
1858 TEMPLATE_PARM_IDX (t) = index;
1859 TEMPLATE_PARM_LEVEL (t) = level;
1860 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1861 TEMPLATE_PARM_DECL (t) = decl;
1862 TREE_TYPE (t) = type;
1864 return t;
1867 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1868 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1869 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1870 new one is created. */
1872 static tree
1873 reduce_template_parm_level (index, type, levels)
1874 tree index;
1875 tree type;
1876 int levels;
1878 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1879 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1880 != TEMPLATE_PARM_LEVEL (index) - levels))
1882 tree decl
1883 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1884 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1885 type);
1886 tree t
1887 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1888 TEMPLATE_PARM_LEVEL (index) - levels,
1889 TEMPLATE_PARM_ORIG_LEVEL (index),
1890 decl, type);
1891 TEMPLATE_PARM_DESCENDANTS (index) = t;
1893 DECL_ARTIFICIAL (decl) = 1;
1894 SET_DECL_TEMPLATE_PARM_P (decl);
1896 /* Template template parameters need this. */
1897 DECL_TEMPLATE_PARMS (decl)
1898 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1901 return TEMPLATE_PARM_DESCENDANTS (index);
1904 /* Process information from new template parameter NEXT and append it to the
1905 LIST being built. */
1907 tree
1908 process_template_parm (list, next)
1909 tree list, next;
1911 tree parm;
1912 tree decl = 0;
1913 tree defval;
1914 int is_type, idx;
1916 parm = next;
1917 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1918 defval = TREE_PURPOSE (parm);
1919 parm = TREE_VALUE (parm);
1920 is_type = TREE_PURPOSE (parm) == class_type_node;
1922 if (list)
1924 tree p = TREE_VALUE (tree_last (list));
1926 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
1927 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1928 else
1929 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1930 ++idx;
1932 else
1933 idx = 0;
1935 if (!is_type)
1937 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1938 /* is a const-param */
1939 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1940 PARM, 0, NULL);
1941 SET_DECL_TEMPLATE_PARM_P (parm);
1943 /* [temp.param]
1945 The top-level cv-qualifiers on the template-parameter are
1946 ignored when determining its type. */
1947 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1949 /* A template parameter is not modifiable. */
1950 TREE_READONLY (parm) = 1;
1951 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
1952 TREE_TYPE (parm) = void_type_node;
1953 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1954 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1955 = build_template_parm_index (idx, processing_template_decl,
1956 processing_template_decl,
1957 decl, TREE_TYPE (parm));
1959 else
1961 tree t;
1962 parm = TREE_VALUE (parm);
1964 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1966 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1967 /* This is for distinguishing between real templates and template
1968 template parameters */
1969 TREE_TYPE (parm) = t;
1970 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1971 decl = parm;
1973 else
1975 t = make_aggr_type (TEMPLATE_TYPE_PARM);
1976 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1977 decl = build_decl (TYPE_DECL, parm, t);
1980 TYPE_NAME (t) = decl;
1981 TYPE_STUB_DECL (t) = decl;
1982 parm = decl;
1983 TEMPLATE_TYPE_PARM_INDEX (t)
1984 = build_template_parm_index (idx, processing_template_decl,
1985 processing_template_decl,
1986 decl, TREE_TYPE (parm));
1988 DECL_ARTIFICIAL (decl) = 1;
1989 SET_DECL_TEMPLATE_PARM_P (decl);
1990 pushdecl (decl);
1991 parm = build_tree_list (defval, parm);
1992 return chainon (list, parm);
1995 /* The end of a template parameter list has been reached. Process the
1996 tree list into a parameter vector, converting each parameter into a more
1997 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1998 as PARM_DECLs. */
2000 tree
2001 end_template_parm_list (parms)
2002 tree parms;
2004 int nparms;
2005 tree parm, next;
2006 tree saved_parmlist = make_tree_vec (list_length (parms));
2008 current_template_parms
2009 = tree_cons (size_int (processing_template_decl),
2010 saved_parmlist, current_template_parms);
2012 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2014 next = TREE_CHAIN (parm);
2015 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2016 TREE_CHAIN (parm) = NULL_TREE;
2019 --processing_template_parmlist;
2021 return saved_parmlist;
2024 /* end_template_decl is called after a template declaration is seen. */
2026 void
2027 end_template_decl ()
2029 reset_specialization ();
2031 if (! processing_template_decl)
2032 return;
2034 /* This matches the pushlevel in begin_template_parm_list. */
2035 finish_scope ();
2037 --processing_template_decl;
2038 current_template_parms = TREE_CHAIN (current_template_parms);
2041 /* Given a template argument vector containing the template PARMS.
2042 The innermost PARMS are given first. */
2044 tree
2045 current_template_args ()
2047 tree header;
2048 tree args = NULL_TREE;
2049 int length = TMPL_PARMS_DEPTH (current_template_parms);
2050 int l = length;
2052 /* If there is only one level of template parameters, we do not
2053 create a TREE_VEC of TREE_VECs. Instead, we return a single
2054 TREE_VEC containing the arguments. */
2055 if (length > 1)
2056 args = make_tree_vec (length);
2058 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2060 tree a = copy_node (TREE_VALUE (header));
2061 int i;
2063 TREE_TYPE (a) = NULL_TREE;
2064 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2066 tree t = TREE_VEC_ELT (a, i);
2068 /* T will be a list if we are called from within a
2069 begin/end_template_parm_list pair, but a vector directly
2070 if within a begin/end_member_template_processing pair. */
2071 if (TREE_CODE (t) == TREE_LIST)
2073 t = TREE_VALUE (t);
2075 if (TREE_CODE (t) == TYPE_DECL
2076 || TREE_CODE (t) == TEMPLATE_DECL)
2077 t = TREE_TYPE (t);
2078 else
2079 t = DECL_INITIAL (t);
2080 TREE_VEC_ELT (a, i) = t;
2084 if (length > 1)
2085 TREE_VEC_ELT (args, --l) = a;
2086 else
2087 args = a;
2090 return args;
2093 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2094 template PARMS. Used by push_template_decl below. */
2096 static tree
2097 build_template_decl (decl, parms)
2098 tree decl;
2099 tree parms;
2101 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2102 DECL_TEMPLATE_PARMS (tmpl) = parms;
2103 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2104 if (DECL_LANG_SPECIFIC (decl))
2106 if (CAN_HAVE_FULL_LANG_DECL_P (decl))
2107 DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl);
2108 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2109 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2110 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2111 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2112 if (DECL_OVERLOADED_OPERATOR_P (decl))
2113 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2114 DECL_OVERLOADED_OPERATOR_P (decl));
2117 return tmpl;
2120 struct template_parm_data
2122 /* The level of the template parameters we are currently
2123 processing. */
2124 int level;
2126 /* The index of the specialization argument we are currently
2127 processing. */
2128 int current_arg;
2130 /* An array whose size is the number of template parameters. The
2131 elements are non-zero if the parameter has been used in any one
2132 of the arguments processed so far. */
2133 int* parms;
2135 /* An array whose size is the number of template arguments. The
2136 elements are non-zero if the argument makes use of template
2137 parameters of this level. */
2138 int* arg_uses_template_parms;
2141 /* Subroutine of push_template_decl used to see if each template
2142 parameter in a partial specialization is used in the explicit
2143 argument list. If T is of the LEVEL given in DATA (which is
2144 treated as a template_parm_data*), then DATA->PARMS is marked
2145 appropriately. */
2147 static int
2148 mark_template_parm (t, data)
2149 tree t;
2150 void* data;
2152 int level;
2153 int idx;
2154 struct template_parm_data* tpd = (struct template_parm_data*) data;
2156 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2158 level = TEMPLATE_PARM_LEVEL (t);
2159 idx = TEMPLATE_PARM_IDX (t);
2161 else
2163 level = TEMPLATE_TYPE_LEVEL (t);
2164 idx = TEMPLATE_TYPE_IDX (t);
2167 if (level == tpd->level)
2169 tpd->parms[idx] = 1;
2170 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2173 /* Return zero so that for_each_template_parm will continue the
2174 traversal of the tree; we want to mark *every* template parm. */
2175 return 0;
2178 /* Process the partial specialization DECL. */
2180 static tree
2181 process_partial_specialization (decl)
2182 tree decl;
2184 tree type = TREE_TYPE (decl);
2185 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2186 tree specargs = CLASSTYPE_TI_ARGS (type);
2187 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2188 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2189 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2190 int nargs = TREE_VEC_LENGTH (inner_args);
2191 int ntparms = TREE_VEC_LENGTH (inner_parms);
2192 int i;
2193 int did_error_intro = 0;
2194 struct template_parm_data tpd;
2195 struct template_parm_data tpd2;
2197 /* We check that each of the template parameters given in the
2198 partial specialization is used in the argument list to the
2199 specialization. For example:
2201 template <class T> struct S;
2202 template <class T> struct S<T*>;
2204 The second declaration is OK because `T*' uses the template
2205 parameter T, whereas
2207 template <class T> struct S<int>;
2209 is no good. Even trickier is:
2211 template <class T>
2212 struct S1
2214 template <class U>
2215 struct S2;
2216 template <class U>
2217 struct S2<T>;
2220 The S2<T> declaration is actually illegal; it is a
2221 full-specialization. Of course,
2223 template <class U>
2224 struct S2<T (*)(U)>;
2226 or some such would have been OK. */
2227 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2228 tpd.parms = alloca (sizeof (int) * ntparms);
2229 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2231 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2232 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2233 for (i = 0; i < nargs; ++i)
2235 tpd.current_arg = i;
2236 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2237 &mark_template_parm,
2238 &tpd);
2240 for (i = 0; i < ntparms; ++i)
2241 if (tpd.parms[i] == 0)
2243 /* One of the template parms was not used in the
2244 specialization. */
2245 if (!did_error_intro)
2247 error ("template parameters not used in partial specialization:");
2248 did_error_intro = 1;
2251 error (" `%D'",
2252 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2255 /* [temp.class.spec]
2257 The argument list of the specialization shall not be identical to
2258 the implicit argument list of the primary template. */
2259 if (comp_template_args
2260 (inner_args,
2261 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2262 (maintmpl)))))
2263 error ("partial specialization `%T' does not specialize any template arguments", type);
2265 /* [temp.class.spec]
2267 A partially specialized non-type argument expression shall not
2268 involve template parameters of the partial specialization except
2269 when the argument expression is a simple identifier.
2271 The type of a template parameter corresponding to a specialized
2272 non-type argument shall not be dependent on a parameter of the
2273 specialization. */
2274 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2275 tpd2.parms = 0;
2276 for (i = 0; i < nargs; ++i)
2278 tree arg = TREE_VEC_ELT (inner_args, i);
2279 if (/* These first two lines are the `non-type' bit. */
2280 !TYPE_P (arg)
2281 && TREE_CODE (arg) != TEMPLATE_DECL
2282 /* This next line is the `argument expression is not just a
2283 simple identifier' condition and also the `specialized
2284 non-type argument' bit. */
2285 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2287 if (tpd.arg_uses_template_parms[i])
2288 error ("template argument `%E' involves template parameter(s)", arg);
2289 else
2291 /* Look at the corresponding template parameter,
2292 marking which template parameters its type depends
2293 upon. */
2294 tree type =
2295 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2296 i)));
2298 if (!tpd2.parms)
2300 /* We haven't yet initialized TPD2. Do so now. */
2301 tpd2.arg_uses_template_parms
2302 = (int*) alloca (sizeof (int) * nargs);
2303 /* The number of parameters here is the number in the
2304 main template, which, as checked in the assertion
2305 above, is NARGS. */
2306 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2307 tpd2.level =
2308 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2311 /* Mark the template parameters. But this time, we're
2312 looking for the template parameters of the main
2313 template, not in the specialization. */
2314 tpd2.current_arg = i;
2315 tpd2.arg_uses_template_parms[i] = 0;
2316 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2317 for_each_template_parm (type,
2318 &mark_template_parm,
2319 &tpd2);
2321 if (tpd2.arg_uses_template_parms [i])
2323 /* The type depended on some template parameters.
2324 If they are fully specialized in the
2325 specialization, that's OK. */
2326 int j;
2327 for (j = 0; j < nargs; ++j)
2328 if (tpd2.parms[j] != 0
2329 && tpd.arg_uses_template_parms [j])
2331 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2332 type,
2333 arg);
2334 break;
2341 if (retrieve_specialization (maintmpl, specargs))
2342 /* We've already got this specialization. */
2343 return decl;
2345 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2346 = tree_cons (inner_args, inner_parms,
2347 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2348 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2349 return decl;
2352 /* Check that a template declaration's use of default arguments is not
2353 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2354 non-zero if DECL is the thing declared by a primary template.
2355 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2357 static void
2358 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2359 tree decl;
2360 tree parms;
2361 int is_primary;
2362 int is_partial;
2364 const char *msg;
2365 int last_level_to_check;
2366 tree parm_level;
2368 /* [temp.param]
2370 A default template-argument shall not be specified in a
2371 function template declaration or a function template definition, nor
2372 in the template-parameter-list of the definition of a member of a
2373 class template. */
2375 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2376 /* You can't have a function template declaration in a local
2377 scope, nor you can you define a member of a class template in a
2378 local scope. */
2379 return;
2381 if (current_class_type
2382 && !TYPE_BEING_DEFINED (current_class_type)
2383 && DECL_LANG_SPECIFIC (decl)
2384 /* If this is either a friend defined in the scope of the class
2385 or a member function. */
2386 && ((DECL_CONTEXT (decl)
2387 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2388 || (DECL_FRIEND_CONTEXT (decl)
2389 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2390 current_class_type)))
2391 /* And, if it was a member function, it really was defined in
2392 the scope of the class. */
2393 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2394 /* We already checked these parameters when the template was
2395 declared, so there's no need to do it again now. This function
2396 was defined in class scope, but we're processing it's body now
2397 that the class is complete. */
2398 return;
2400 /* [temp.param]
2402 If a template-parameter has a default template-argument, all
2403 subsequent template-parameters shall have a default
2404 template-argument supplied. */
2405 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2407 tree inner_parms = TREE_VALUE (parm_level);
2408 int ntparms = TREE_VEC_LENGTH (inner_parms);
2409 int seen_def_arg_p = 0;
2410 int i;
2412 for (i = 0; i < ntparms; ++i)
2414 tree parm = TREE_VEC_ELT (inner_parms, i);
2415 if (TREE_PURPOSE (parm))
2416 seen_def_arg_p = 1;
2417 else if (seen_def_arg_p)
2419 error ("no default argument for `%D'", TREE_VALUE (parm));
2420 /* For better subsequent error-recovery, we indicate that
2421 there should have been a default argument. */
2422 TREE_PURPOSE (parm) = error_mark_node;
2427 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2428 /* For an ordinary class template, default template arguments are
2429 allowed at the innermost level, e.g.:
2430 template <class T = int>
2431 struct S {};
2432 but, in a partial specialization, they're not allowed even
2433 there, as we have in [temp.class.spec]:
2435 The template parameter list of a specialization shall not
2436 contain default template argument values.
2438 So, for a partial specialization, or for a function template,
2439 we look at all of them. */
2441 else
2442 /* But, for a primary class template that is not a partial
2443 specialization we look at all template parameters except the
2444 innermost ones. */
2445 parms = TREE_CHAIN (parms);
2447 /* Figure out what error message to issue. */
2448 if (TREE_CODE (decl) == FUNCTION_DECL)
2449 msg = "default template arguments may not be used in function templates";
2450 else if (is_partial)
2451 msg = "default template arguments may not be used in partial specializations";
2452 else
2453 msg = "default argument for template parameter for class enclosing `%D'";
2455 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2456 /* If we're inside a class definition, there's no need to
2457 examine the parameters to the class itself. On the one
2458 hand, they will be checked when the class is defined, and,
2459 on the other, default arguments are legal in things like:
2460 template <class T = double>
2461 struct S { template <class U> void f(U); };
2462 Here the default argument for `S' has no bearing on the
2463 declaration of `f'. */
2464 last_level_to_check = template_class_depth (current_class_type) + 1;
2465 else
2466 /* Check everything. */
2467 last_level_to_check = 0;
2469 for (parm_level = parms;
2470 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2471 parm_level = TREE_CHAIN (parm_level))
2473 tree inner_parms = TREE_VALUE (parm_level);
2474 int i;
2475 int ntparms;
2477 ntparms = TREE_VEC_LENGTH (inner_parms);
2478 for (i = 0; i < ntparms; ++i)
2479 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2481 if (msg)
2483 error (msg, decl);
2484 msg = 0;
2487 /* Clear out the default argument so that we are not
2488 confused later. */
2489 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2492 /* At this point, if we're still interested in issuing messages,
2493 they must apply to classes surrounding the object declared. */
2494 if (msg)
2495 msg = "default argument for template parameter for class enclosing `%D'";
2499 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2500 parameters given by current_template_args, or reuses a
2501 previously existing one, if appropriate. Returns the DECL, or an
2502 equivalent one, if it is replaced via a call to duplicate_decls.
2504 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2506 tree
2507 push_template_decl_real (decl, is_friend)
2508 tree decl;
2509 int is_friend;
2511 tree tmpl;
2512 tree args;
2513 tree info;
2514 tree ctx;
2515 int primary;
2516 int is_partial;
2517 int new_template_p = 0;
2519 /* See if this is a partial specialization. */
2520 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2521 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2522 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2524 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2526 if (is_friend)
2527 /* For a friend, we want the context of the friend function, not
2528 the type of which it is a friend. */
2529 ctx = DECL_CONTEXT (decl);
2530 else if (CP_DECL_CONTEXT (decl)
2531 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2532 /* In the case of a virtual function, we want the class in which
2533 it is defined. */
2534 ctx = CP_DECL_CONTEXT (decl);
2535 else
2536 /* Otherwise, if we're currently defining some class, the DECL
2537 is assumed to be a member of the class. */
2538 ctx = current_scope ();
2540 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2541 ctx = NULL_TREE;
2543 if (!DECL_CONTEXT (decl))
2544 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2546 /* See if this is a primary template. */
2547 primary = template_parm_scope_p ();
2549 if (primary)
2551 if (current_lang_name == lang_name_c)
2552 error ("template with C linkage");
2553 else if (TREE_CODE (decl) == TYPE_DECL
2554 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2555 error ("template class without a name");
2556 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2557 && CLASS_TYPE_P (TREE_TYPE (decl)))
2558 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2559 || TREE_CODE (decl) == FUNCTION_DECL)
2560 /* OK */;
2561 else
2562 error ("template declaration of `%#D'", decl);
2565 /* Check to see that the rules regarding the use of default
2566 arguments are not being violated. */
2567 check_default_tmpl_args (decl, current_template_parms,
2568 primary, is_partial);
2570 if (is_partial)
2571 return process_partial_specialization (decl);
2573 args = current_template_args ();
2575 if (!ctx
2576 || TREE_CODE (ctx) == FUNCTION_DECL
2577 || (TREE_CODE (ctx) != TEMPLATE_TYPE_PARM
2578 && TREE_CODE (ctx) != BOUND_TEMPLATE_TEMPLATE_PARM
2579 && TYPE_BEING_DEFINED (ctx))
2580 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2582 if (DECL_LANG_SPECIFIC (decl)
2583 && DECL_TEMPLATE_INFO (decl)
2584 && DECL_TI_TEMPLATE (decl))
2585 tmpl = DECL_TI_TEMPLATE (decl);
2586 /* If DECL is a TYPE_DECL for a class-template, then there won't
2587 be DECL_LANG_SPECIFIC. The information equivalent to
2588 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2589 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2590 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2591 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2593 /* Since a template declaration already existed for this
2594 class-type, we must be redeclaring it here. Make sure
2595 that the redeclaration is legal. */
2596 redeclare_class_template (TREE_TYPE (decl),
2597 current_template_parms);
2598 /* We don't need to create a new TEMPLATE_DECL; just use the
2599 one we already had. */
2600 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2602 else
2604 tmpl = build_template_decl (decl, current_template_parms);
2605 new_template_p = 1;
2607 if (DECL_LANG_SPECIFIC (decl)
2608 && DECL_TEMPLATE_SPECIALIZATION (decl))
2610 /* A specialization of a member template of a template
2611 class. */
2612 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2613 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2614 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2618 else
2620 tree a, t, current, parms;
2621 int i;
2623 if (TREE_CODE (decl) == TYPE_DECL)
2625 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2626 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2627 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2628 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2629 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2630 else
2632 error ("`%D' does not declare a template type", decl);
2633 return decl;
2636 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2638 error ("template definition of non-template `%#D'", decl);
2639 return decl;
2641 else
2642 tmpl = DECL_TI_TEMPLATE (decl);
2644 if (is_member_template (tmpl)
2645 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2646 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2647 && DECL_TEMPLATE_SPECIALIZATION (decl))
2649 tree new_tmpl;
2651 /* The declaration is a specialization of a member
2652 template, declared outside the class. Therefore, the
2653 innermost template arguments will be NULL, so we
2654 replace them with the arguments determined by the
2655 earlier call to check_explicit_specialization. */
2656 args = DECL_TI_ARGS (decl);
2658 new_tmpl
2659 = build_template_decl (decl, current_template_parms);
2660 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2661 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2662 DECL_TI_TEMPLATE (decl) = new_tmpl;
2663 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2664 DECL_TEMPLATE_INFO (new_tmpl)
2665 = tree_cons (tmpl, args, NULL_TREE);
2667 register_specialization (new_tmpl,
2668 most_general_template (tmpl),
2669 args);
2670 return decl;
2673 /* Make sure the template headers we got make sense. */
2675 parms = DECL_TEMPLATE_PARMS (tmpl);
2676 i = TMPL_PARMS_DEPTH (parms);
2677 if (TMPL_ARGS_DEPTH (args) != i)
2679 error ("expected %d levels of template parms for `%#D', got %d",
2680 i, decl, TMPL_ARGS_DEPTH (args));
2682 else
2683 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2685 a = TMPL_ARGS_LEVEL (args, i);
2686 t = INNERMOST_TEMPLATE_PARMS (parms);
2688 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2690 if (current == decl)
2691 error ("got %d template parameters for `%#D'",
2692 TREE_VEC_LENGTH (a), decl);
2693 else
2694 error ("got %d template parameters for `%#T'",
2695 TREE_VEC_LENGTH (a), current);
2696 error (" but %d required", TREE_VEC_LENGTH (t));
2699 /* Perhaps we should also check that the parms are used in the
2700 appropriate qualifying scopes in the declarator? */
2702 if (current == decl)
2703 current = ctx;
2704 else
2705 current = TYPE_CONTEXT (current);
2709 DECL_TEMPLATE_RESULT (tmpl) = decl;
2710 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2712 /* Push template declarations for global functions and types. Note
2713 that we do not try to push a global template friend declared in a
2714 template class; such a thing may well depend on the template
2715 parameters of the class. */
2716 if (new_template_p && !ctx
2717 && !(is_friend && template_class_depth (current_class_type) > 0))
2718 tmpl = pushdecl_namespace_level (tmpl);
2720 if (primary)
2721 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2723 info = tree_cons (tmpl, args, NULL_TREE);
2725 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2727 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2728 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2729 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2730 /* Don't change the name if we've already set it up. */
2731 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2732 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2734 else if (DECL_LANG_SPECIFIC (decl))
2735 DECL_TEMPLATE_INFO (decl) = info;
2737 return DECL_TEMPLATE_RESULT (tmpl);
2740 tree
2741 push_template_decl (decl)
2742 tree decl;
2744 return push_template_decl_real (decl, 0);
2747 /* Called when a class template TYPE is redeclared with the indicated
2748 template PARMS, e.g.:
2750 template <class T> struct S;
2751 template <class T> struct S {}; */
2753 void
2754 redeclare_class_template (type, parms)
2755 tree type;
2756 tree parms;
2758 tree tmpl;
2759 tree tmpl_parms;
2760 int i;
2762 if (!TYPE_TEMPLATE_INFO (type))
2764 error ("`%T' is not a template type", type);
2765 return;
2768 tmpl = TYPE_TI_TEMPLATE (type);
2769 if (!PRIMARY_TEMPLATE_P (tmpl))
2770 /* The type is nested in some template class. Nothing to worry
2771 about here; there are no new template parameters for the nested
2772 type. */
2773 return;
2775 parms = INNERMOST_TEMPLATE_PARMS (parms);
2776 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2778 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2780 cp_error_at ("previous declaration `%D'", tmpl);
2781 error ("used %d template parameter%s instead of %d",
2782 TREE_VEC_LENGTH (tmpl_parms),
2783 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2784 TREE_VEC_LENGTH (parms));
2785 return;
2788 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2790 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2791 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2792 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2793 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2795 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2797 cp_error_at ("template parameter `%#D'", tmpl_parm);
2798 error ("redeclared here as `%#D'", parm);
2799 return;
2802 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2804 /* We have in [temp.param]:
2806 A template-parameter may not be given default arguments
2807 by two different declarations in the same scope. */
2808 error ("redefinition of default argument for `%#D'", parm);
2809 cp_error_at (" original definition appeared here", tmpl_parm);
2810 return;
2813 if (parm_default != NULL_TREE)
2814 /* Update the previous template parameters (which are the ones
2815 that will really count) with the new default value. */
2816 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2817 else if (tmpl_default != NULL_TREE)
2818 /* Update the new parameters, too; they'll be used as the
2819 parameters for any members. */
2820 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2824 /* Attempt to convert the non-type template parameter EXPR to the
2825 indicated TYPE. If the conversion is successful, return the
2826 converted value. If the conversion is unsuccessful, return
2827 NULL_TREE if we issued an error message, or error_mark_node if we
2828 did not. We issue error messages for out-and-out bad template
2829 parameters, but not simply because the conversion failed, since we
2830 might be just trying to do argument deduction. By the time this
2831 function is called, neither TYPE nor EXPR may make use of template
2832 parameters. */
2834 static tree
2835 convert_nontype_argument (type, expr)
2836 tree type;
2837 tree expr;
2839 tree expr_type = TREE_TYPE (expr);
2841 /* A template-argument for a non-type, non-template
2842 template-parameter shall be one of:
2844 --an integral constant-expression of integral or enumeration
2845 type; or
2847 --the name of a non-type template-parameter; or
2849 --the name of an object or function with external linkage,
2850 including function templates and function template-ids but
2851 excluding non-static class members, expressed as id-expression;
2854 --the address of an object or function with external linkage,
2855 including function templates and function template-ids but
2856 excluding non-static class members, expressed as & id-expression
2857 where the & is optional if the name refers to a function or
2858 array; or
2860 --a pointer to member expressed as described in _expr.unary.op_. */
2862 /* An integral constant-expression can include const variables or
2863 enumerators. Simplify things by folding them to their values,
2864 unless we're about to bind the declaration to a reference
2865 parameter. */
2866 if (INTEGRAL_TYPE_P (expr_type)
2867 && TREE_CODE (type) != REFERENCE_TYPE)
2868 expr = decl_constant_value (expr);
2870 if (is_overloaded_fn (expr))
2871 /* OK for now. We'll check that it has external linkage later.
2872 Check this first since if expr_type is the unknown_type_node
2873 we would otherwise complain below. */
2875 else if (TYPE_PTRMEM_P (expr_type)
2876 || TYPE_PTRMEMFUNC_P (expr_type))
2878 if (TREE_CODE (expr) != PTRMEM_CST)
2879 goto bad_argument;
2881 else if (TYPE_PTR_P (expr_type)
2882 || TYPE_PTRMEM_P (expr_type)
2883 || TREE_CODE (expr_type) == ARRAY_TYPE
2884 || TREE_CODE (type) == REFERENCE_TYPE
2885 /* If expr is the address of an overloaded function, we
2886 will get the unknown_type_node at this point. */
2887 || expr_type == unknown_type_node)
2889 tree referent;
2890 tree e = expr;
2891 STRIP_NOPS (e);
2893 if (TREE_CODE (expr_type) == ARRAY_TYPE
2894 || (TREE_CODE (type) == REFERENCE_TYPE
2895 && TREE_CODE (e) != ADDR_EXPR))
2896 referent = e;
2897 else
2899 if (TREE_CODE (e) != ADDR_EXPR)
2901 bad_argument:
2902 error ("`%E' is not a valid template argument", expr);
2903 if (TYPE_PTR_P (expr_type))
2905 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2906 error ("it must be the address of a function with external linkage");
2907 else
2908 error ("it must be the address of an object with external linkage");
2910 else if (TYPE_PTRMEM_P (expr_type)
2911 || TYPE_PTRMEMFUNC_P (expr_type))
2912 error ("it must be a pointer-to-member of the form `&X::Y'");
2914 return NULL_TREE;
2917 referent = TREE_OPERAND (e, 0);
2918 STRIP_NOPS (referent);
2921 if (TREE_CODE (referent) == STRING_CST)
2923 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2924 referent);
2925 return NULL_TREE;
2928 if (is_overloaded_fn (referent))
2929 /* We'll check that it has external linkage later. */
2931 else if (TREE_CODE (referent) != VAR_DECL)
2932 goto bad_argument;
2933 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
2935 error ("address of non-extern `%E' cannot be used as template argument", referent);
2936 return error_mark_node;
2939 else if (INTEGRAL_TYPE_P (expr_type)
2940 || TYPE_PTRMEM_P (expr_type)
2941 || TYPE_PTRMEMFUNC_P (expr_type))
2943 if (! TREE_CONSTANT (expr))
2945 non_constant:
2946 error ("non-constant `%E' cannot be used as template argument",
2947 expr);
2948 return NULL_TREE;
2951 else
2953 error ("object `%E' cannot be used as template argument", expr);
2954 return NULL_TREE;
2957 switch (TREE_CODE (type))
2959 case INTEGER_TYPE:
2960 case BOOLEAN_TYPE:
2961 case ENUMERAL_TYPE:
2962 /* For a non-type template-parameter of integral or enumeration
2963 type, integral promotions (_conv.prom_) and integral
2964 conversions (_conv.integral_) are applied. */
2965 if (!INTEGRAL_TYPE_P (expr_type))
2966 return error_mark_node;
2968 /* It's safe to call digest_init in this case; we know we're
2969 just converting one integral constant expression to another. */
2970 expr = digest_init (type, expr, (tree*) 0);
2972 if (TREE_CODE (expr) != INTEGER_CST)
2973 /* Curiously, some TREE_CONSTANT integral expressions do not
2974 simplify to integer constants. For example, `3 % 0',
2975 remains a TRUNC_MOD_EXPR. */
2976 goto non_constant;
2978 return expr;
2980 case POINTER_TYPE:
2982 tree type_pointed_to = TREE_TYPE (type);
2984 if (TYPE_PTRMEM_P (type))
2986 tree e;
2988 /* For a non-type template-parameter of type pointer to data
2989 member, qualification conversions (_conv.qual_) are
2990 applied. */
2991 e = perform_qualification_conversions (type, expr);
2992 if (TREE_CODE (e) == NOP_EXPR)
2993 /* The call to perform_qualification_conversions will
2994 insert a NOP_EXPR over EXPR to do express conversion,
2995 if necessary. But, that will confuse us if we use
2996 this (converted) template parameter to instantiate
2997 another template; then the thing will not look like a
2998 valid template argument. So, just make a new
2999 constant, of the appropriate type. */
3000 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3001 return e;
3003 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3005 /* For a non-type template-parameter of type pointer to
3006 function, only the function-to-pointer conversion
3007 (_conv.func_) is applied. If the template-argument
3008 represents a set of overloaded functions (or a pointer to
3009 such), the matching function is selected from the set
3010 (_over.over_). */
3011 tree fns;
3012 tree fn;
3014 if (TREE_CODE (expr) == ADDR_EXPR)
3015 fns = TREE_OPERAND (expr, 0);
3016 else
3017 fns = expr;
3019 fn = instantiate_type (type_pointed_to, fns, tf_none);
3021 if (fn == error_mark_node)
3022 return error_mark_node;
3024 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3026 if (really_overloaded_fn (fns))
3027 return error_mark_node;
3028 else
3029 goto bad_argument;
3032 expr = build_unary_op (ADDR_EXPR, fn, 0);
3034 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3036 return expr;
3038 else
3040 /* For a non-type template-parameter of type pointer to
3041 object, qualification conversions (_conv.qual_) and the
3042 array-to-pointer conversion (_conv.array_) are applied.
3043 [Note: In particular, neither the null pointer conversion
3044 (_conv.ptr_) nor the derived-to-base conversion
3045 (_conv.ptr_) are applied. Although 0 is a valid
3046 template-argument for a non-type template-parameter of
3047 integral type, it is not a valid template-argument for a
3048 non-type template-parameter of pointer type.]
3050 The call to decay_conversion performs the
3051 array-to-pointer conversion, if appropriate. */
3052 expr = decay_conversion (expr);
3054 if (expr == error_mark_node)
3055 return error_mark_node;
3056 else
3057 return perform_qualification_conversions (type, expr);
3060 break;
3062 case REFERENCE_TYPE:
3064 tree type_referred_to = TREE_TYPE (type);
3066 /* If this expression already has reference type, get the
3067 underling object. */
3068 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3070 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3071 expr = TREE_OPERAND (expr, 0);
3072 expr_type = TREE_TYPE (expr);
3075 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3077 /* For a non-type template-parameter of type reference to
3078 function, no conversions apply. If the
3079 template-argument represents a set of overloaded
3080 functions, the matching function is selected from the
3081 set (_over.over_). */
3082 tree fn;
3084 fn = instantiate_type (type_referred_to, expr, tf_none);
3086 if (fn == error_mark_node)
3087 return error_mark_node;
3089 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3091 if (really_overloaded_fn (expr))
3092 /* Don't issue an error here; we might get a different
3093 function if the overloading had worked out
3094 differently. */
3095 return error_mark_node;
3096 else
3097 goto bad_argument;
3100 my_friendly_assert (same_type_p (type_referred_to,
3101 TREE_TYPE (fn)),
3104 expr = fn;
3106 else
3108 /* For a non-type template-parameter of type reference to
3109 object, no conversions apply. The type referred to by the
3110 reference may be more cv-qualified than the (otherwise
3111 identical) type of the template-argument. The
3112 template-parameter is bound directly to the
3113 template-argument, which must be an lvalue. */
3114 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3115 TYPE_MAIN_VARIANT (type_referred_to))
3116 || !at_least_as_qualified_p (type_referred_to,
3117 expr_type)
3118 || !real_lvalue_p (expr))
3119 return error_mark_node;
3122 mark_addressable (expr);
3123 return build1 (ADDR_EXPR, type, expr);
3125 break;
3127 case RECORD_TYPE:
3129 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3131 /* For a non-type template-parameter of type pointer to member
3132 function, no conversions apply. If the template-argument
3133 represents a set of overloaded member functions, the
3134 matching member function is selected from the set
3135 (_over.over_). */
3137 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3138 expr_type != unknown_type_node)
3139 return error_mark_node;
3141 if (TREE_CODE (expr) == PTRMEM_CST)
3143 /* A ptr-to-member constant. */
3144 if (!same_type_p (type, expr_type))
3145 return error_mark_node;
3146 else
3147 return expr;
3150 if (TREE_CODE (expr) != ADDR_EXPR)
3151 return error_mark_node;
3153 expr = instantiate_type (type, expr, tf_none);
3155 if (expr == error_mark_node)
3156 return error_mark_node;
3158 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3160 return expr;
3162 break;
3164 default:
3165 /* All non-type parameters must have one of these types. */
3166 abort ();
3167 break;
3170 return error_mark_node;
3173 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3174 template template parameters. Both PARM_PARMS and ARG_PARMS are
3175 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3176 or PARM_DECL.
3178 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3179 the case, then extra parameters must have default arguments.
3181 Consider the example:
3182 template <class T, class Allocator = allocator> class vector;
3183 template<template <class U> class TT> class C;
3185 C<vector> is a valid instantiation. PARM_PARMS for the above code
3186 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3187 T and Allocator) and OUTER_ARGS contains the argument that is used to
3188 substitute the TT parameter. */
3190 static int
3191 coerce_template_template_parms (parm_parms, arg_parms, complain,
3192 in_decl, outer_args)
3193 tree parm_parms, arg_parms;
3194 tsubst_flags_t complain;
3195 tree in_decl, outer_args;
3197 int nparms, nargs, i;
3198 tree parm, arg;
3200 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3201 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3203 nparms = TREE_VEC_LENGTH (parm_parms);
3204 nargs = TREE_VEC_LENGTH (arg_parms);
3206 /* The rule here is opposite of coerce_template_parms. */
3207 if (nargs < nparms
3208 || (nargs > nparms
3209 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3210 return 0;
3212 for (i = 0; i < nparms; ++i)
3214 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3215 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3217 if (arg == NULL_TREE || arg == error_mark_node
3218 || parm == NULL_TREE || parm == error_mark_node)
3219 return 0;
3221 if (TREE_CODE (arg) != TREE_CODE (parm))
3222 return 0;
3224 switch (TREE_CODE (parm))
3226 case TYPE_DECL:
3227 break;
3229 case TEMPLATE_DECL:
3230 /* We encounter instantiations of templates like
3231 template <template <template <class> class> class TT>
3232 class C; */
3234 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3235 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3237 if (!coerce_template_template_parms (parmparm, argparm,
3238 complain, in_decl,
3239 outer_args))
3240 return 0;
3242 break;
3244 case PARM_DECL:
3245 /* The tsubst call is used to handle cases such as
3246 template <class T, template <T> class TT> class D;
3247 i.e. the parameter list of TT depends on earlier parameters. */
3248 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3249 complain, in_decl),
3250 TREE_TYPE (arg)))
3251 return 0;
3252 break;
3254 default:
3255 abort ();
3258 return 1;
3261 /* Convert the indicated template ARG as necessary to match the
3262 indicated template PARM. Returns the converted ARG, or
3263 error_mark_node if the conversion was unsuccessful. Error and
3264 warning messages are issued under control of COMPLAIN. This
3265 conversion is for the Ith parameter in the parameter list. ARGS is
3266 the full set of template arguments deduced so far. */
3268 static tree
3269 convert_template_argument (parm, arg, args, complain, i, in_decl)
3270 tree parm;
3271 tree arg;
3272 tree args;
3273 tsubst_flags_t complain;
3274 int i;
3275 tree in_decl;
3277 tree val;
3278 tree inner_args;
3279 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3281 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3283 if (TREE_CODE (arg) == TREE_LIST
3284 && TREE_TYPE (arg) != NULL_TREE
3285 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3287 /* The template argument was the name of some
3288 member function. That's usually
3289 illegal, but static members are OK. In any
3290 case, grab the underlying fields/functions
3291 and issue an error later if required. */
3292 arg = TREE_VALUE (arg);
3293 TREE_TYPE (arg) = unknown_type_node;
3296 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3297 requires_type = (TREE_CODE (parm) == TYPE_DECL
3298 || requires_tmpl_type);
3300 if (TREE_CODE (arg) != RECORD_TYPE)
3301 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3302 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3303 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3304 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3305 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3306 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3308 if (is_base_of_enclosing_class (arg, current_class_type))
3309 /* This is a template name used within the scope of the
3310 template. It could be the template, or it could be the
3311 instantiation. Choose whichever makes sense. */
3312 is_tmpl_type = requires_tmpl_type;
3313 else
3314 is_tmpl_type = 1;
3316 else
3317 /* It is a non-template class, or a specialization of a template
3318 class, or a non-template member of a template class. */
3319 is_tmpl_type = 0;
3321 if (is_tmpl_type
3322 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3323 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3324 arg = TYPE_STUB_DECL (arg);
3325 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3326 arg = CLASSTYPE_TI_TEMPLATE (arg);
3328 is_type = TYPE_P (arg) || is_tmpl_type;
3330 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3331 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3333 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3335 arg = make_typename_type (TREE_OPERAND (arg, 0),
3336 TREE_OPERAND (arg, 1),
3337 complain & tf_error);
3338 if (TREE_CODE (arg) == TYPE_DECL)
3339 arg = TREE_TYPE (arg);
3340 is_type = 1;
3342 if (is_type != requires_type)
3344 if (in_decl)
3346 if (complain & tf_error)
3348 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3349 i + 1, in_decl);
3350 if (is_type)
3351 error (" expected a constant of type `%T', got `%T'",
3352 TREE_TYPE (parm),
3353 (is_tmpl_type ? DECL_NAME (arg) : arg));
3354 else
3355 error (" expected a type, got `%E'", arg);
3358 return error_mark_node;
3360 if (is_tmpl_type ^ requires_tmpl_type)
3362 if (in_decl && (complain & tf_error))
3364 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3365 i + 1, in_decl);
3366 if (is_tmpl_type)
3367 error (" expected a type, got `%T'", DECL_NAME (arg));
3368 else
3369 error (" expected a class template, got `%T'", arg);
3371 return error_mark_node;
3374 if (is_type)
3376 if (requires_tmpl_type)
3378 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3379 /* The number of argument required is not known yet.
3380 Just accept it for now. */
3381 val = TREE_TYPE (arg);
3382 else
3384 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3385 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3387 if (coerce_template_template_parms (parmparm, argparm,
3388 complain, in_decl,
3389 inner_args))
3391 val = arg;
3393 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3394 TEMPLATE_DECL. */
3395 if (val != error_mark_node
3396 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3397 val = TREE_TYPE (val);
3399 else
3401 if (in_decl && (complain & tf_error))
3403 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3404 i + 1, in_decl);
3405 error (" expected a template of type `%D', got `%D'", parm, arg);
3408 val = error_mark_node;
3412 else
3414 val = groktypename (arg);
3415 if (! processing_template_decl)
3417 /* [basic.link]: A name with no linkage (notably, the
3418 name of a class or enumeration declared in a local
3419 scope) shall not be used to declare an entity with
3420 linkage. This implies that names with no linkage
3421 cannot be used as template arguments. */
3422 tree t = no_linkage_check (val);
3423 if (t)
3425 if (TYPE_ANONYMOUS_P (t))
3426 pedwarn
3427 ("template-argument `%T' uses anonymous type", val);
3428 else
3429 error
3430 ("template-argument `%T' uses local type `%T'",
3431 val, t);
3432 return error_mark_node;
3437 else
3439 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3441 if (invalid_nontype_parm_type_p (t, complain))
3442 return error_mark_node;
3444 if (processing_template_decl)
3445 arg = maybe_fold_nontype_arg (arg);
3447 if (!uses_template_parms (arg) && !uses_template_parms (t))
3448 /* We used to call digest_init here. However, digest_init
3449 will report errors, which we don't want when complain
3450 is zero. More importantly, digest_init will try too
3451 hard to convert things: for example, `0' should not be
3452 converted to pointer type at this point according to
3453 the standard. Accepting this is not merely an
3454 extension, since deciding whether or not these
3455 conversions can occur is part of determining which
3456 function template to call, or whether a given explicit
3457 argument specification is legal. */
3458 val = convert_nontype_argument (t, arg);
3459 else
3460 val = arg;
3462 if (val == NULL_TREE)
3463 val = error_mark_node;
3464 else if (val == error_mark_node && (complain & tf_error))
3465 error ("could not convert template argument `%E' to `%T'",
3466 arg, t);
3469 return val;
3472 /* Convert all template arguments to their appropriate types, and
3473 return a vector containing the innermost resulting template
3474 arguments. If any error occurs, return error_mark_node. Error and
3475 warning messages are issued under control of COMPLAIN. Some error
3476 messages are issued even if COMPLAIN is zero; for instance, if a
3477 template argument is composed from a local class.
3479 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3480 provided in ARGLIST, or else trailing parameters must have default
3481 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3482 deduction for any unspecified trailing arguments. */
3484 static tree
3485 coerce_template_parms (parms, args, in_decl,
3486 complain,
3487 require_all_arguments)
3488 tree parms, args;
3489 tree in_decl;
3490 tsubst_flags_t complain;
3491 int require_all_arguments;
3493 int nparms, nargs, i, lost = 0;
3494 tree inner_args;
3495 tree new_args;
3496 tree new_inner_args;
3498 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3499 nargs = NUM_TMPL_ARGS (inner_args);
3500 nparms = TREE_VEC_LENGTH (parms);
3502 if (nargs > nparms
3503 || (nargs < nparms
3504 && require_all_arguments
3505 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3507 if (complain & tf_error)
3509 error ("wrong number of template arguments (%d, should be %d)",
3510 nargs, nparms);
3512 if (in_decl)
3513 cp_error_at ("provided for `%D'", in_decl);
3516 return error_mark_node;
3519 new_inner_args = make_tree_vec (nparms);
3520 new_args = add_outermost_template_args (args, new_inner_args);
3521 for (i = 0; i < nparms; i++)
3523 tree arg;
3524 tree parm;
3526 /* Get the Ith template parameter. */
3527 parm = TREE_VEC_ELT (parms, i);
3529 /* Calculate the Ith argument. */
3530 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3532 arg = TREE_VALUE (inner_args);
3533 inner_args = TREE_CHAIN (inner_args);
3535 else if (i < nargs)
3536 arg = TREE_VEC_ELT (inner_args, i);
3537 /* If no template argument was supplied, look for a default
3538 value. */
3539 else if (TREE_PURPOSE (parm) == NULL_TREE)
3541 /* There was no default value. */
3542 my_friendly_assert (!require_all_arguments, 0);
3543 break;
3545 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3546 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3547 else
3548 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3549 in_decl);
3551 /* Now, convert the Ith argument, as necessary. */
3552 if (arg == NULL_TREE)
3553 /* We're out of arguments. */
3555 my_friendly_assert (!require_all_arguments, 0);
3556 break;
3558 else if (arg == error_mark_node)
3560 error ("template argument %d is invalid", i + 1);
3561 arg = error_mark_node;
3563 else
3564 arg = convert_template_argument (TREE_VALUE (parm),
3565 arg, new_args, complain, i,
3566 in_decl);
3568 if (arg == error_mark_node)
3569 lost++;
3570 TREE_VEC_ELT (new_inner_args, i) = arg;
3573 if (lost)
3574 return error_mark_node;
3576 return new_inner_args;
3579 /* Returns 1 if template args OT and NT are equivalent. */
3581 static int
3582 template_args_equal (ot, nt)
3583 tree ot, nt;
3585 if (nt == ot)
3586 return 1;
3588 if (TREE_CODE (nt) == TREE_VEC)
3589 /* For member templates */
3590 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3591 else if (TYPE_P (nt))
3592 return TYPE_P (ot) && same_type_p (ot, nt);
3593 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3594 return 0;
3595 else
3596 return (cp_tree_equal (ot, nt) > 0);
3599 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3600 of template arguments. Returns 0 otherwise. */
3603 comp_template_args (oldargs, newargs)
3604 tree oldargs, newargs;
3606 int i;
3608 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3609 return 0;
3611 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3613 tree nt = TREE_VEC_ELT (newargs, i);
3614 tree ot = TREE_VEC_ELT (oldargs, i);
3616 if (! template_args_equal (ot, nt))
3617 return 0;
3619 return 1;
3622 /* Given class template name and parameter list, produce a user-friendly name
3623 for the instantiation. */
3625 static char *
3626 mangle_class_name_for_template (name, parms, arglist)
3627 const char *name;
3628 tree parms, arglist;
3630 static struct obstack scratch_obstack;
3631 static char *scratch_firstobj;
3632 int i, nparms;
3634 if (!scratch_firstobj)
3635 gcc_obstack_init (&scratch_obstack);
3636 else
3637 obstack_free (&scratch_obstack, scratch_firstobj);
3638 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3640 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3641 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3643 cat (name);
3644 ccat ('<');
3645 nparms = TREE_VEC_LENGTH (parms);
3646 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3647 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3648 for (i = 0; i < nparms; i++)
3650 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3651 tree arg = TREE_VEC_ELT (arglist, i);
3653 if (i)
3654 ccat (',');
3656 if (TREE_CODE (parm) == TYPE_DECL)
3658 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3659 continue;
3661 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3663 if (TREE_CODE (arg) == TEMPLATE_DECL)
3665 /* Already substituted with real template. Just output
3666 the template name here */
3667 tree context = DECL_CONTEXT (arg);
3668 if (context)
3670 /* The template may be defined in a namespace, or
3671 may be a member template. */
3672 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3673 || CLASS_TYPE_P (context),
3674 980422);
3675 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3676 cat("::");
3678 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3680 else
3681 /* Output the parameter declaration */
3682 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3683 continue;
3685 else
3686 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3688 if (TREE_CODE (arg) == TREE_LIST)
3690 /* New list cell was built because old chain link was in
3691 use. */
3692 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3693 arg = TREE_VALUE (arg);
3695 /* No need to check arglist against parmlist here; we did that
3696 in coerce_template_parms, called from lookup_template_class. */
3697 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3700 char *bufp = obstack_next_free (&scratch_obstack);
3701 int offset = 0;
3702 while (bufp[offset - 1] == ' ')
3703 offset--;
3704 obstack_blank_fast (&scratch_obstack, offset);
3706 /* B<C<char> >, not B<C<char>> */
3707 if (bufp[offset - 1] == '>')
3708 ccat (' ');
3710 ccat ('>');
3711 ccat ('\0');
3712 return (char *) obstack_base (&scratch_obstack);
3715 static tree
3716 classtype_mangled_name (t)
3717 tree t;
3719 if (CLASSTYPE_TEMPLATE_INFO (t)
3720 /* Specializations have already had their names set up in
3721 lookup_template_class. */
3722 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3724 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3726 /* For non-primary templates, the template parameters are
3727 implicit from their surrounding context. */
3728 if (PRIMARY_TEMPLATE_P (tmpl))
3730 tree name = DECL_NAME (tmpl);
3731 char *mangled_name = mangle_class_name_for_template
3732 (IDENTIFIER_POINTER (name),
3733 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3734 CLASSTYPE_TI_ARGS (t));
3735 tree id = get_identifier (mangled_name);
3736 IDENTIFIER_TEMPLATE (id) = name;
3737 return id;
3741 return TYPE_IDENTIFIER (t);
3744 static void
3745 add_pending_template (d)
3746 tree d;
3748 tree ti = (TYPE_P (d)
3749 ? CLASSTYPE_TEMPLATE_INFO (d)
3750 : DECL_TEMPLATE_INFO (d));
3751 tree pt;
3752 int level;
3754 if (TI_PENDING_TEMPLATE_FLAG (ti))
3755 return;
3757 /* We are called both from instantiate_decl, where we've already had a
3758 tinst_level pushed, and instantiate_template, where we haven't.
3759 Compensate. */
3760 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3762 if (level)
3763 push_tinst_level (d);
3765 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3766 if (last_pending_template)
3767 TREE_CHAIN (last_pending_template) = pt;
3768 else
3769 pending_templates = pt;
3771 last_pending_template = pt;
3773 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3775 if (level)
3776 pop_tinst_level ();
3780 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3781 may be either a _DECL or an overloaded function or an
3782 IDENTIFIER_NODE), and ARGLIST. */
3784 tree
3785 lookup_template_function (fns, arglist)
3786 tree fns, arglist;
3788 tree type;
3790 if (fns == NULL_TREE)
3792 error ("non-template used as template");
3793 return error_mark_node;
3796 type = TREE_TYPE (fns);
3797 if (TREE_CODE (fns) == OVERLOAD || !type)
3798 type = unknown_type_node;
3800 if (processing_template_decl)
3801 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3802 else
3803 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3806 /* Within the scope of a template class S<T>, the name S gets bound
3807 (in build_self_reference) to a TYPE_DECL for the class, not a
3808 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3809 or one of its enclosing classes, and that type is a template,
3810 return the associated TEMPLATE_DECL. Otherwise, the original
3811 DECL is returned. */
3813 static tree
3814 maybe_get_template_decl_from_type_decl (decl)
3815 tree decl;
3817 return (decl != NULL_TREE
3818 && TREE_CODE (decl) == TYPE_DECL
3819 && DECL_ARTIFICIAL (decl)
3820 && CLASS_TYPE_P (TREE_TYPE (decl))
3821 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3822 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3825 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3826 parameters, find the desired type.
3828 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3829 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3830 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3831 otherwise.)
3833 IN_DECL, if non-NULL, is the template declaration we are trying to
3834 instantiate.
3836 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3837 the class we are looking up.
3839 Issue error and warning messages under control of COMPLAIN.
3841 If the template class is really a local class in a template
3842 function, then the FUNCTION_CONTEXT is the function in which it is
3843 being instantiated. */
3845 tree
3846 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
3847 tree d1, arglist;
3848 tree in_decl;
3849 tree context;
3850 int entering_scope;
3851 tsubst_flags_t complain;
3853 tree template = NULL_TREE, parmlist;
3854 tree t;
3856 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3858 if (IDENTIFIER_VALUE (d1)
3859 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3860 template = IDENTIFIER_VALUE (d1);
3861 else
3863 if (context)
3864 push_decl_namespace (context);
3865 template = lookup_name (d1, /*prefer_type=*/0);
3866 template = maybe_get_template_decl_from_type_decl (template);
3867 if (context)
3868 pop_decl_namespace ();
3870 if (template)
3871 context = DECL_CONTEXT (template);
3873 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3875 tree type = TREE_TYPE (d1);
3877 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3878 an implicit typename for the second A. Deal with it. */
3879 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3880 type = TREE_TYPE (type);
3882 if (CLASSTYPE_TEMPLATE_INFO (type))
3884 template = CLASSTYPE_TI_TEMPLATE (type);
3885 d1 = DECL_NAME (template);
3888 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3889 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
3891 template = TYPE_TI_TEMPLATE (d1);
3892 d1 = DECL_NAME (template);
3894 else if (TREE_CODE (d1) == TEMPLATE_DECL
3895 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
3897 template = d1;
3898 d1 = DECL_NAME (template);
3899 context = DECL_CONTEXT (template);
3902 /* With something like `template <class T> class X class X { ... };'
3903 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3904 We don't want to do that, but we have to deal with the situation,
3905 so let's give them some syntax errors to chew on instead of a
3906 crash. Alternatively D1 might not be a template type at all. */
3907 if (! template)
3909 if (complain & tf_error)
3910 error ("`%T' is not a template", d1);
3911 return error_mark_node;
3914 if (TREE_CODE (template) != TEMPLATE_DECL
3915 /* If we're called from the parser, make sure it's a user visible
3916 template. */
3917 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
3918 && !DECL_TEMPLATE_PARM_P (template)
3919 && !PRIMARY_TEMPLATE_P (template)))
3921 if (complain & tf_error)
3923 error ("non-template type `%T' used as a template", d1);
3924 if (in_decl)
3925 cp_error_at ("for template declaration `%D'", in_decl);
3927 return error_mark_node;
3930 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3932 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3933 template arguments */
3935 tree parm;
3936 tree arglist2;
3938 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3940 /* Consider an example where a template template parameter declared as
3942 template <class T, class U = std::allocator<T> > class TT
3944 The template parameter level of T and U are one level larger than
3945 of TT. To proper process the default argument of U, say when an
3946 instantiation `TT<int>' is seen, we need to build the full
3947 arguments containing {int} as the innermost level. Outer levels
3948 can be obtained from `current_template_args ()'. */
3950 if (processing_template_decl)
3951 arglist = add_to_template_args (current_template_args (), arglist);
3953 arglist2 = coerce_template_parms (parmlist, arglist, template,
3954 complain, /*require_all_args=*/1);
3955 if (arglist2 == error_mark_node)
3956 return error_mark_node;
3958 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
3959 return parm;
3961 else
3963 tree template_type = TREE_TYPE (template);
3964 tree gen_tmpl;
3965 tree type_decl;
3966 tree found = NULL_TREE;
3967 tree *tp;
3968 int arg_depth;
3969 int parm_depth;
3970 int is_partial_instantiation;
3972 gen_tmpl = most_general_template (template);
3973 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
3974 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3975 arg_depth = TMPL_ARGS_DEPTH (arglist);
3977 if (arg_depth == 1 && parm_depth > 1)
3979 /* We've been given an incomplete set of template arguments.
3980 For example, given:
3982 template <class T> struct S1 {
3983 template <class U> struct S2 {};
3984 template <class U> struct S2<U*> {};
3987 we will be called with an ARGLIST of `U*', but the
3988 TEMPLATE will be `template <class T> template
3989 <class U> struct S1<T>::S2'. We must fill in the missing
3990 arguments. */
3991 arglist
3992 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3993 arglist);
3994 arg_depth = TMPL_ARGS_DEPTH (arglist);
3997 /* Now we should have enough arguments. */
3998 my_friendly_assert (parm_depth == arg_depth, 0);
4000 /* From here on, we're only interested in the most general
4001 template. */
4002 template = gen_tmpl;
4004 /* Calculate the BOUND_ARGS. These will be the args that are
4005 actually tsubst'd into the definition to create the
4006 instantiation. */
4007 if (parm_depth > 1)
4009 /* We have multiple levels of arguments to coerce, at once. */
4010 int i;
4011 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4013 tree bound_args = make_tree_vec (parm_depth);
4015 for (i = saved_depth,
4016 t = DECL_TEMPLATE_PARMS (template);
4017 i > 0 && t != NULL_TREE;
4018 --i, t = TREE_CHAIN (t))
4020 tree a = coerce_template_parms (TREE_VALUE (t),
4021 arglist, template,
4022 complain, /*require_all_args=*/1);
4023 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4025 /* We temporarily reduce the length of the ARGLIST so
4026 that coerce_template_parms will see only the arguments
4027 corresponding to the template parameters it is
4028 examining. */
4029 TREE_VEC_LENGTH (arglist)--;
4032 /* Restore the ARGLIST to its full size. */
4033 TREE_VEC_LENGTH (arglist) = saved_depth;
4035 arglist = bound_args;
4037 else
4038 arglist
4039 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4040 INNERMOST_TEMPLATE_ARGS (arglist),
4041 template,
4042 complain, /*require_all_args=*/1);
4044 if (arglist == error_mark_node)
4045 /* We were unable to bind the arguments. */
4046 return error_mark_node;
4048 /* In the scope of a template class, explicit references to the
4049 template class refer to the type of the template, not any
4050 instantiation of it. For example, in:
4052 template <class T> class C { void f(C<T>); }
4054 the `C<T>' is just the same as `C'. Outside of the
4055 class, however, such a reference is an instantiation. */
4056 if (comp_template_args (TYPE_TI_ARGS (template_type),
4057 arglist))
4059 found = template_type;
4061 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4063 tree ctx;
4065 /* Note that we use DECL_CONTEXT, rather than
4066 CP_DECL_CONTEXT, so that the termination test is
4067 always just `ctx'. We're not interested in namespace
4068 scopes. */
4069 for (ctx = current_class_type;
4070 ctx;
4071 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4072 if (same_type_p (ctx, template_type))
4073 break;
4075 if (!ctx)
4076 /* We're not in the scope of the class, so the
4077 TEMPLATE_TYPE is not the type we want after
4078 all. */
4079 found = NULL_TREE;
4082 if (found)
4083 return found;
4085 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4086 *tp;
4087 tp = &TREE_CHAIN (*tp))
4088 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4090 found = *tp;
4092 /* Use the move-to-front heuristic to speed up future
4093 searches. */
4094 *tp = TREE_CHAIN (*tp);
4095 TREE_CHAIN (found)
4096 = DECL_TEMPLATE_INSTANTIATIONS (template);
4097 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4099 return TREE_VALUE (found);
4102 /* This type is a "partial instantiation" if any of the template
4103 arguments still involve template parameters. Note that we set
4104 IS_PARTIAL_INSTANTIATION for partial specializations as
4105 well. */
4106 is_partial_instantiation = uses_template_parms (arglist);
4108 if (!is_partial_instantiation
4109 && !PRIMARY_TEMPLATE_P (template)
4110 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4112 found = xref_tag_from_type (TREE_TYPE (template),
4113 DECL_NAME (template),
4114 /*globalize=*/1);
4115 return found;
4118 context = tsubst (DECL_CONTEXT (template), arglist,
4119 complain, in_decl);
4120 if (!context)
4121 context = global_namespace;
4123 /* Create the type. */
4124 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4126 if (!is_partial_instantiation)
4128 set_current_access_from_decl (TYPE_NAME (template_type));
4129 t = start_enum (TYPE_IDENTIFIER (template_type));
4131 else
4132 /* We don't want to call start_enum for this type, since
4133 the values for the enumeration constants may involve
4134 template parameters. And, no one should be interested
4135 in the enumeration constants for such a type. */
4136 t = make_node (ENUMERAL_TYPE);
4138 else
4140 t = make_aggr_type (TREE_CODE (template_type));
4141 CLASSTYPE_DECLARED_CLASS (t)
4142 = CLASSTYPE_DECLARED_CLASS (template_type);
4143 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4144 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4145 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4147 /* A local class. Make sure the decl gets registered properly. */
4148 if (context == current_function_decl)
4149 pushtag (DECL_NAME (template), t, 0);
4152 /* If we called start_enum or pushtag above, this information
4153 will already be set up. */
4154 if (!TYPE_NAME (t))
4156 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4158 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4159 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4160 TYPE_STUB_DECL (t) = type_decl;
4161 DECL_SOURCE_FILE (type_decl)
4162 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4163 DECL_SOURCE_LINE (type_decl)
4164 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
4166 else
4167 type_decl = TYPE_NAME (t);
4169 /* Set up the template information. We have to figure out which
4170 template is the immediate parent if this is a full
4171 instantiation. */
4172 if (parm_depth == 1 || is_partial_instantiation
4173 || !PRIMARY_TEMPLATE_P (template))
4174 /* This case is easy; there are no member templates involved. */
4175 found = template;
4176 else
4178 /* This is a full instantiation of a member template. Look
4179 for a partial instantiation of which this is an instance. */
4181 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4182 found; found = TREE_CHAIN (found))
4184 int success;
4185 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4187 /* We only want partial instantiations, here, not
4188 specializations or full instantiations. */
4189 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4190 || !uses_template_parms (TREE_VALUE (found)))
4191 continue;
4193 /* Temporarily reduce by one the number of levels in the
4194 ARGLIST and in FOUND so as to avoid comparing the
4195 last set of arguments. */
4196 TREE_VEC_LENGTH (arglist)--;
4197 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4199 /* See if the arguments match. If they do, then TMPL is
4200 the partial instantiation we want. */
4201 success = comp_template_args (TREE_PURPOSE (found), arglist);
4203 /* Restore the argument vectors to their full size. */
4204 TREE_VEC_LENGTH (arglist)++;
4205 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4207 if (success)
4209 found = tmpl;
4210 break;
4214 if (!found)
4216 /* There was no partial instantiation. This happens
4217 where C<T> is a member template of A<T> and it's used
4218 in something like
4220 template <typename T> struct B { A<T>::C<int> m; };
4221 B<float>;
4223 Create the partial instantiation.
4225 TREE_VEC_LENGTH (arglist)--;
4226 found = tsubst (template, arglist, complain, NULL_TREE);
4227 TREE_VEC_LENGTH (arglist)++;
4231 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4232 DECL_TEMPLATE_INSTANTIATIONS (template)
4233 = tree_cons (arglist, t,
4234 DECL_TEMPLATE_INSTANTIATIONS (template));
4236 if (TREE_CODE (t) == ENUMERAL_TYPE
4237 && !is_partial_instantiation)
4238 /* Now that the type has been registered on the instantiations
4239 list, we set up the enumerators. Because the enumeration
4240 constants may involve the enumeration type itself, we make
4241 sure to register the type first, and then create the
4242 constants. That way, doing tsubst_expr for the enumeration
4243 constants won't result in recursive calls here; we'll find
4244 the instantiation and exit above. */
4245 tsubst_enum (template_type, t, arglist);
4247 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4248 is set up. */
4249 if (TREE_CODE (t) != ENUMERAL_TYPE)
4250 DECL_NAME (type_decl) = classtype_mangled_name (t);
4251 if (!is_partial_instantiation)
4253 /* For backwards compatibility; code that uses
4254 -fexternal-templates expects looking up a template to
4255 instantiate it. I think DDD still relies on this.
4256 (jason 8/20/1998) */
4257 if (TREE_CODE (t) != ENUMERAL_TYPE
4258 && flag_external_templates
4259 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4260 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4261 add_pending_template (t);
4263 else
4264 /* If the type makes use of template parameters, the
4265 code that generates debugging information will crash. */
4266 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4268 return t;
4272 struct pair_fn_data
4274 tree_fn_t fn;
4275 void *data;
4278 /* Called from for_each_template_parm via walk_tree. */
4280 static tree
4281 for_each_template_parm_r (tp, walk_subtrees, d)
4282 tree *tp;
4283 int *walk_subtrees;
4284 void *d;
4286 tree t = *tp;
4287 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4288 tree_fn_t fn = pfd->fn;
4289 void *data = pfd->data;
4291 if (TYPE_P (t)
4292 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4293 return error_mark_node;
4295 switch (TREE_CODE (t))
4297 case RECORD_TYPE:
4298 if (TYPE_PTRMEMFUNC_P (t))
4299 break;
4300 /* Fall through. */
4302 case UNION_TYPE:
4303 case ENUMERAL_TYPE:
4304 if (!TYPE_TEMPLATE_INFO (t))
4305 *walk_subtrees = 0;
4306 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4307 fn, data))
4308 return error_mark_node;
4309 break;
4311 case METHOD_TYPE:
4312 /* Since we're not going to walk subtrees, we have to do this
4313 explicitly here. */
4314 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
4315 return error_mark_node;
4316 /* Fall through. */
4318 case FUNCTION_TYPE:
4319 /* Check the return type. */
4320 if (for_each_template_parm (TREE_TYPE (t), fn, data))
4321 return error_mark_node;
4323 /* Check the parameter types. Since default arguments are not
4324 instantiated until they are needed, the TYPE_ARG_TYPES may
4325 contain expressions that involve template parameters. But,
4326 no-one should be looking at them yet. And, once they're
4327 instantiated, they don't contain template parameters, so
4328 there's no point in looking at them then, either. */
4330 tree parm;
4332 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4333 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4334 return error_mark_node;
4336 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4337 want walk_tree walking into them itself. */
4338 *walk_subtrees = 0;
4340 break;
4342 case FUNCTION_DECL:
4343 case VAR_DECL:
4344 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4345 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
4346 return error_mark_node;
4347 /* Fall through. */
4349 case CONST_DECL:
4350 case PARM_DECL:
4351 if (DECL_CONTEXT (t)
4352 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
4353 return error_mark_node;
4354 break;
4356 case BOUND_TEMPLATE_TEMPLATE_PARM:
4357 /* Record template parameters such as `T' inside `TT<T>'. */
4358 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
4359 return error_mark_node;
4360 /* Fall through. */
4362 case TEMPLATE_TEMPLATE_PARM:
4363 case TEMPLATE_TYPE_PARM:
4364 case TEMPLATE_PARM_INDEX:
4365 if (fn && (*fn)(t, data))
4366 return error_mark_node;
4367 else if (!fn)
4368 return error_mark_node;
4369 break;
4371 case TEMPLATE_DECL:
4372 /* A template template parameter is encountered */
4373 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4374 && for_each_template_parm (TREE_TYPE (t), fn, data))
4375 return error_mark_node;
4377 /* Already substituted template template parameter */
4378 *walk_subtrees = 0;
4379 break;
4381 case TYPENAME_TYPE:
4382 if (!fn || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, data))
4383 return error_mark_node;
4384 break;
4386 case CONSTRUCTOR:
4387 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4388 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4389 (TREE_TYPE (t)), fn, data))
4390 return error_mark_node;
4391 break;
4393 case INDIRECT_REF:
4394 case COMPONENT_REF:
4395 /* If there's no type, then this thing must be some expression
4396 involving template parameters. */
4397 if (!fn && !TREE_TYPE (t))
4398 return error_mark_node;
4399 break;
4401 case MODOP_EXPR:
4402 case CAST_EXPR:
4403 case REINTERPRET_CAST_EXPR:
4404 case CONST_CAST_EXPR:
4405 case STATIC_CAST_EXPR:
4406 case DYNAMIC_CAST_EXPR:
4407 case ARROW_EXPR:
4408 case DOTSTAR_EXPR:
4409 case TYPEID_EXPR:
4410 case LOOKUP_EXPR:
4411 case PSEUDO_DTOR_EXPR:
4412 if (!fn)
4413 return error_mark_node;
4414 break;
4416 default:
4417 break;
4420 /* We didn't find any template parameters we liked. */
4421 return NULL_TREE;
4424 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4425 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4426 call FN with the parameter and the DATA.
4427 If FN returns non-zero, the iteration is terminated, and
4428 for_each_template_parm returns 1. Otherwise, the iteration
4429 continues. If FN never returns a non-zero value, the value
4430 returned by for_each_template_parm is 0. If FN is NULL, it is
4431 considered to be the function which always returns 1. */
4433 static int
4434 for_each_template_parm (t, fn, data)
4435 tree t;
4436 tree_fn_t fn;
4437 void* data;
4439 struct pair_fn_data pfd;
4441 /* Set up. */
4442 pfd.fn = fn;
4443 pfd.data = data;
4445 /* Walk the tree. (Conceptually, we would like to walk without
4446 duplicates, but for_each_template_parm_r recursively calls
4447 for_each_template_parm, so we would need to reorganize a fair
4448 bit to use walk_tree_without_duplicates.) */
4449 return walk_tree (&t,
4450 for_each_template_parm_r,
4451 &pfd,
4452 NULL) != NULL_TREE;
4456 uses_template_parms (t)
4457 tree t;
4459 return for_each_template_parm (t, 0, 0);
4462 static int tinst_depth;
4463 extern int max_tinst_depth;
4464 #ifdef GATHER_STATISTICS
4465 int depth_reached;
4466 #endif
4467 static int tinst_level_tick;
4468 static int last_template_error_tick;
4470 /* We're starting to instantiate D; record the template instantiation context
4471 for diagnostics and to restore it later. */
4474 push_tinst_level (d)
4475 tree d;
4477 tree new;
4479 if (tinst_depth >= max_tinst_depth)
4481 /* If the instantiation in question still has unbound template parms,
4482 we don't really care if we can't instantiate it, so just return.
4483 This happens with base instantiation for implicit `typename'. */
4484 if (uses_template_parms (d))
4485 return 0;
4487 last_template_error_tick = tinst_level_tick;
4488 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4489 max_tinst_depth, d);
4491 print_instantiation_context ();
4493 return 0;
4496 new = build_expr_wfl (d, input_filename, lineno, 0);
4497 TREE_CHAIN (new) = current_tinst_level;
4498 current_tinst_level = new;
4500 ++tinst_depth;
4501 #ifdef GATHER_STATISTICS
4502 if (tinst_depth > depth_reached)
4503 depth_reached = tinst_depth;
4504 #endif
4506 ++tinst_level_tick;
4507 return 1;
4510 /* We're done instantiating this template; return to the instantiation
4511 context. */
4513 void
4514 pop_tinst_level ()
4516 tree old = current_tinst_level;
4518 /* Restore the filename and line number stashed away when we started
4519 this instantiation. */
4520 lineno = TINST_LINE (old);
4521 input_filename = TINST_FILE (old);
4522 extract_interface_info ();
4524 current_tinst_level = TREE_CHAIN (old);
4525 --tinst_depth;
4526 ++tinst_level_tick;
4529 /* We're instantiating a deferred template; restore the template
4530 instantiation context in which the instantiation was requested, which
4531 is one step out from LEVEL. */
4533 static void
4534 reopen_tinst_level (level)
4535 tree level;
4537 tree t;
4539 tinst_depth = 0;
4540 for (t = level; t; t = TREE_CHAIN (t))
4541 ++tinst_depth;
4543 current_tinst_level = level;
4544 pop_tinst_level ();
4547 /* Return the outermost template instantiation context, for use with
4548 -falt-external-templates. */
4550 tree
4551 tinst_for_decl ()
4553 tree p = current_tinst_level;
4555 if (p)
4556 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4558 return p;
4561 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4562 vector of template arguments, as for tsubst.
4564 Returns an appropriate tsubst'd friend declaration. */
4566 static tree
4567 tsubst_friend_function (decl, args)
4568 tree decl;
4569 tree args;
4571 tree new_friend;
4572 int line = lineno;
4573 const char *file = input_filename;
4575 lineno = DECL_SOURCE_LINE (decl);
4576 input_filename = DECL_SOURCE_FILE (decl);
4578 if (TREE_CODE (decl) == FUNCTION_DECL
4579 && DECL_TEMPLATE_INSTANTIATION (decl)
4580 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4581 /* This was a friend declared with an explicit template
4582 argument list, e.g.:
4584 friend void f<>(T);
4586 to indicate that f was a template instantiation, not a new
4587 function declaration. Now, we have to figure out what
4588 instantiation of what template. */
4590 tree template_id, arglist, fns;
4591 tree new_args;
4592 tree tmpl;
4593 tree ns = CP_DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4595 /* Friend functions are looked up in the containing namespace scope.
4596 We must enter that scope, to avoid finding member functions of the
4597 current cless with same name. */
4598 push_nested_namespace (ns);
4599 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4600 tf_error | tf_warning, NULL_TREE);
4601 pop_nested_namespace (ns);
4602 arglist = tsubst (DECL_TI_ARGS (decl), args,
4603 tf_error | tf_warning, NULL_TREE);
4604 template_id = lookup_template_function (fns, arglist);
4606 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4607 tmpl = determine_specialization (template_id, new_friend,
4608 &new_args,
4609 /*need_member_template=*/0);
4610 new_friend = instantiate_template (tmpl, new_args);
4611 goto done;
4614 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4616 /* The NEW_FRIEND will look like an instantiation, to the
4617 compiler, but is not an instantiation from the point of view of
4618 the language. For example, we might have had:
4620 template <class T> struct S {
4621 template <class U> friend void f(T, U);
4624 Then, in S<int>, template <class U> void f(int, U) is not an
4625 instantiation of anything. */
4626 DECL_USE_TEMPLATE (new_friend) = 0;
4627 if (TREE_CODE (decl) == TEMPLATE_DECL)
4629 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4630 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4631 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4634 /* The mangled name for the NEW_FRIEND is incorrect. The function
4635 is not a template instantiation and should not be mangled like
4636 one. Therefore, we forget the mangling here; we'll recompute it
4637 later if we need it. */
4638 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4640 SET_DECL_RTL (new_friend, NULL_RTX);
4641 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4644 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4646 tree old_decl;
4647 tree new_friend_template_info;
4648 tree new_friend_result_template_info;
4649 tree ns;
4650 int new_friend_is_defn;
4652 /* We must save some information from NEW_FRIEND before calling
4653 duplicate decls since that function will free NEW_FRIEND if
4654 possible. */
4655 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4656 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4658 /* This declaration is a `primary' template. */
4659 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4661 new_friend_is_defn
4662 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4663 new_friend_result_template_info
4664 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4666 else
4668 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4669 new_friend_result_template_info = NULL_TREE;
4672 /* Inside pushdecl_namespace_level, we will push into the
4673 current namespace. However, the friend function should go
4674 into the namespace of the template. */
4675 ns = decl_namespace_context (new_friend);
4676 push_nested_namespace (ns);
4677 old_decl = pushdecl_namespace_level (new_friend);
4678 pop_nested_namespace (ns);
4680 if (old_decl != new_friend)
4682 /* This new friend declaration matched an existing
4683 declaration. For example, given:
4685 template <class T> void f(T);
4686 template <class U> class C {
4687 template <class T> friend void f(T) {}
4690 the friend declaration actually provides the definition
4691 of `f', once C has been instantiated for some type. So,
4692 old_decl will be the out-of-class template declaration,
4693 while new_friend is the in-class definition.
4695 But, if `f' was called before this point, the
4696 instantiation of `f' will have DECL_TI_ARGS corresponding
4697 to `T' but not to `U', references to which might appear
4698 in the definition of `f'. Previously, the most general
4699 template for an instantiation of `f' was the out-of-class
4700 version; now it is the in-class version. Therefore, we
4701 run through all specialization of `f', adding to their
4702 DECL_TI_ARGS appropriately. In particular, they need a
4703 new set of outer arguments, corresponding to the
4704 arguments for this class instantiation.
4706 The same situation can arise with something like this:
4708 friend void f(int);
4709 template <class T> class C {
4710 friend void f(T) {}
4713 when `C<int>' is instantiated. Now, `f(int)' is defined
4714 in the class. */
4716 if (!new_friend_is_defn)
4717 /* On the other hand, if the in-class declaration does
4718 *not* provide a definition, then we don't want to alter
4719 existing definitions. We can just leave everything
4720 alone. */
4722 else
4724 /* Overwrite whatever template info was there before, if
4725 any, with the new template information pertaining to
4726 the declaration. */
4727 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4729 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4730 /* duplicate_decls will take care of this case. */
4732 else
4734 tree t;
4735 tree new_friend_args;
4737 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4738 = new_friend_result_template_info;
4740 new_friend_args = TI_ARGS (new_friend_template_info);
4741 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4742 t != NULL_TREE;
4743 t = TREE_CHAIN (t))
4745 tree spec = TREE_VALUE (t);
4747 DECL_TI_ARGS (spec)
4748 = add_outermost_template_args (new_friend_args,
4749 DECL_TI_ARGS (spec));
4752 /* Now, since specializations are always supposed to
4753 hang off of the most general template, we must move
4754 them. */
4755 t = most_general_template (old_decl);
4756 if (t != old_decl)
4758 DECL_TEMPLATE_SPECIALIZATIONS (t)
4759 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4760 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4761 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4766 /* The information from NEW_FRIEND has been merged into OLD_DECL
4767 by duplicate_decls. */
4768 new_friend = old_decl;
4771 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
4773 /* Check to see that the declaration is really present, and,
4774 possibly obtain an improved declaration. */
4775 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4776 new_friend);
4778 if (fn)
4779 new_friend = fn;
4782 done:
4783 lineno = line;
4784 input_filename = file;
4785 return new_friend;
4788 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4789 template arguments, as for tsubst.
4791 Returns an appropriate tsubst'd friend type or error_mark_node on
4792 failure. */
4794 static tree
4795 tsubst_friend_class (friend_tmpl, args)
4796 tree friend_tmpl;
4797 tree args;
4799 tree friend_type;
4800 tree tmpl;
4802 /* First, we look for a class template. */
4803 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4805 /* But, if we don't find one, it might be because we're in a
4806 situation like this:
4808 template <class T>
4809 struct S {
4810 template <class U>
4811 friend struct S;
4814 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4815 for `S<int>', not the TEMPLATE_DECL. */
4816 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4818 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4819 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4822 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
4824 /* The friend template has already been declared. Just
4825 check to see that the declarations match, and install any new
4826 default parameters. We must tsubst the default parameters,
4827 of course. We only need the innermost template parameters
4828 because that is all that redeclare_class_template will look
4829 at. */
4830 tree parms
4831 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4832 args, tf_error | tf_warning);
4833 if (!parms)
4834 return error_mark_node;
4835 redeclare_class_template (TREE_TYPE (tmpl), parms);
4836 friend_type = TREE_TYPE (tmpl);
4838 else
4840 /* The friend template has not already been declared. In this
4841 case, the instantiation of the template class will cause the
4842 injection of this template into the global scope. */
4843 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
4845 /* The new TMPL is not an instantiation of anything, so we
4846 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4847 the new type because that is supposed to be the corresponding
4848 template decl, i.e., TMPL. */
4849 DECL_USE_TEMPLATE (tmpl) = 0;
4850 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4851 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4853 /* Inject this template into the global scope. */
4854 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4857 return friend_type;
4860 tree
4861 instantiate_class_template (type)
4862 tree type;
4864 tree template, args, pattern, t;
4865 tree typedecl;
4867 if (type == error_mark_node)
4868 return error_mark_node;
4870 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
4871 return type;
4873 /* Figure out which template is being instantiated. */
4874 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4875 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4877 /* Figure out which arguments are being used to do the
4878 instantiation. */
4879 args = CLASSTYPE_TI_ARGS (type);
4880 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
4882 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4883 /* If this is a partial instantiation, then we can't instantiate
4884 the type; there's no telling whether or not one of the
4885 template parameters might eventually be instantiated to some
4886 value that results in a specialization being used. For
4887 example, consider:
4889 template <class T>
4890 struct S {};
4892 template <class U>
4893 void f(S<U>);
4895 template <>
4896 struct S<int> {};
4898 Now, the `S<U>' in `f<int>' is the specialization, not an
4899 instantiation of the original template. */
4900 return type;
4902 /* Determine what specialization of the original template to
4903 instantiate. */
4904 if (PARTIAL_INSTANTIATION_P (type))
4905 /* There's no telling which specialization is appropriate at this
4906 point. Since all peeking at the innards of this partial
4907 instantiation are extensions (like the "implicit typename"
4908 extension, which allows users to omit the keyword `typename' on
4909 names that are declared as types in template base classes), we
4910 are free to do what we please.
4912 Trying to figure out which partial instantiation to use can
4913 cause a crash. (Some of the template arguments don't even have
4914 types.) So, we just use the most general version. */
4915 t = NULL_TREE;
4916 else
4918 t = most_specialized_class (template, args);
4920 if (t == error_mark_node)
4922 const char *str = "candidates are:";
4923 error ("ambiguous class template instantiation for `%#T'", type);
4924 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4925 t = TREE_CHAIN (t))
4927 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4928 args))
4930 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4931 str = " ";
4934 TYPE_BEING_DEFINED (type) = 1;
4935 return error_mark_node;
4939 if (t)
4940 pattern = TREE_TYPE (t);
4941 else
4942 pattern = TREE_TYPE (template);
4944 /* If the template we're instantiating is incomplete, then clearly
4945 there's nothing we can do. */
4946 if (!COMPLETE_TYPE_P (pattern))
4947 return type;
4949 /* If this is a partial instantiation, don't tsubst anything. We will
4950 only use this type for implicit typename, so the actual contents don't
4951 matter. All that matters is whether a particular name is a type. */
4952 if (PARTIAL_INSTANTIATION_P (type))
4954 /* The fields set here must be kept in sync with those cleared
4955 in begin_class_definition. */
4956 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4957 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4958 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4959 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4960 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
4962 /* Pretend that the type is complete, so that we will look
4963 inside it during name lookup and such. */
4964 TYPE_SIZE (type) = bitsize_zero_node;
4965 return type;
4968 /* If we've recursively instantiated too many templates, stop. */
4969 if (! push_tinst_level (type))
4970 return type;
4972 /* Now we're really doing the instantiation. Mark the type as in
4973 the process of being defined. */
4974 TYPE_BEING_DEFINED (type) = 1;
4976 maybe_push_to_top_level (uses_template_parms (type));
4978 if (t)
4980 /* This TYPE is actually a instantiation of of a partial
4981 specialization. We replace the innermost set of ARGS with
4982 the arguments appropriate for substitution. For example,
4983 given:
4985 template <class T> struct S {};
4986 template <class T> struct S<T*> {};
4988 and supposing that we are instantiating S<int*>, ARGS will
4989 present be {int*} but we need {int}. */
4990 tree inner_args
4991 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4992 args);
4994 /* If there were multiple levels in ARGS, replacing the
4995 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4996 want, so we make a copy first. */
4997 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4999 args = copy_node (args);
5000 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5002 else
5003 args = inner_args;
5006 if (flag_external_templates)
5008 if (flag_alt_external_templates)
5010 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5011 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5013 else
5015 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5016 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5017 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5020 else
5022 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5025 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5026 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5027 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5028 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5029 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5030 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5031 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5032 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5033 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5034 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5035 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5036 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5037 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5038 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5039 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5040 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5041 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5042 TYPE_USES_MULTIPLE_INHERITANCE (type)
5043 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5044 TYPE_USES_VIRTUAL_BASECLASSES (type)
5045 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5046 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5047 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5048 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5049 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5050 if (ANON_AGGR_TYPE_P (pattern))
5051 SET_ANON_AGGR_TYPE_P (type);
5053 if (TYPE_BINFO_BASETYPES (pattern))
5055 tree base_list = NULL_TREE;
5056 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5057 int i;
5059 /* Substitute into each of the bases to determine the actual
5060 basetypes. */
5061 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5063 tree base;
5064 tree access;
5065 tree pbase;
5067 pbase = TREE_VEC_ELT (pbases, i);
5069 /* Substitute to figure out the base class. */
5070 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5071 if (base == error_mark_node)
5072 continue;
5074 /* Calculate the correct access node. */
5075 if (TREE_VIA_VIRTUAL (pbase))
5077 if (TREE_VIA_PUBLIC (pbase))
5078 access = access_public_virtual_node;
5079 else if (TREE_VIA_PROTECTED (pbase))
5080 access = access_protected_virtual_node;
5081 else
5082 access = access_private_virtual_node;
5084 else
5086 if (TREE_VIA_PUBLIC (pbase))
5087 access = access_public_node;
5088 else if (TREE_VIA_PROTECTED (pbase))
5089 access = access_protected_node;
5090 else
5091 access = access_private_node;
5094 base_list = tree_cons (access, base, base_list);
5097 /* The list is now in reverse order; correct that. */
5098 base_list = nreverse (base_list);
5100 /* Now call xref_basetypes to set up all the base-class
5101 information. */
5102 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5103 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5104 ? class_type_node : record_type_node)
5105 : union_type_node,
5106 DECL_NAME (TYPE_NAME (pattern)),
5107 type,
5108 base_list);
5111 /* Now that our base classes are set up, enter the scope of the
5112 class, so that name lookups into base classes, etc. will work
5113 correctly. This is precisely analogous to what we do in
5114 begin_class_definition when defining an ordinary non-template
5115 class. */
5116 pushclass (type, 1);
5118 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
5120 tree tag = TREE_VALUE (t);
5121 tree name = TYPE_IDENTIFIER (tag);
5122 tree newtag;
5124 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5125 my_friendly_assert (newtag != error_mark_node, 20010206);
5126 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5128 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5129 /* Unfortunately, lookup_template_class sets
5130 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5131 instantiation (i.e., for the type of a member template
5132 class nested within a template class.) This behavior is
5133 required for maybe_process_partial_specialization to work
5134 correctly, but is not accurate in this case; the TAG is not
5135 an instantiation of anything. (The corresponding
5136 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5137 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5139 /* Now, we call pushtag to put this NEWTAG into the scope of
5140 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5141 pushtag calling push_template_decl. We don't have to do
5142 this for enums because it will already have been done in
5143 tsubst_enum. */
5144 if (name)
5145 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5146 pushtag (name, newtag, /*globalize=*/0);
5150 /* Don't replace enum constants here. */
5151 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
5152 if (TREE_CODE (t) != CONST_DECL)
5154 tree r;
5156 /* The the file and line for this declaration, to assist in
5157 error message reporting. Since we called push_tinst_level
5158 above, we don't need to restore these. */
5159 lineno = DECL_SOURCE_LINE (t);
5160 input_filename = DECL_SOURCE_FILE (t);
5162 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5163 if (TREE_CODE (r) == VAR_DECL)
5165 tree init;
5167 if (DECL_INITIALIZED_IN_CLASS_P (r))
5168 init = tsubst_expr (DECL_INITIAL (t), args,
5169 tf_error | tf_warning, NULL_TREE);
5170 else
5171 init = NULL_TREE;
5173 finish_static_data_member_decl (r, init,
5174 /*asmspec_tree=*/NULL_TREE,
5175 /*flags=*/0);
5177 if (DECL_INITIALIZED_IN_CLASS_P (r))
5178 check_static_variable_definition (r, TREE_TYPE (r));
5181 /* R will have a TREE_CHAIN if and only if it has already been
5182 processed by finish_member_declaration. This can happen
5183 if, for example, it is a TYPE_DECL for a class-scoped
5184 ENUMERAL_TYPE; such a thing will already have been added to
5185 the field list by tsubst_enum above. */
5186 if (!TREE_CHAIN (r))
5188 set_current_access_from_decl (r);
5189 finish_member_declaration (r);
5193 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5194 for this instantiation. */
5195 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5197 tree r = tsubst (t, args, tf_error, NULL_TREE);
5198 set_current_access_from_decl (r);
5199 grok_special_member_properties (r);
5200 finish_member_declaration (r);
5203 /* Construct the DECL_FRIENDLIST for the new class type. */
5204 typedecl = TYPE_MAIN_DECL (type);
5205 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5206 t != NULL_TREE;
5207 t = TREE_CHAIN (t))
5209 tree friends;
5211 for (friends = TREE_VALUE (t);
5212 friends != NULL_TREE;
5213 friends = TREE_CHAIN (friends))
5214 if (TREE_PURPOSE (friends) == error_mark_node)
5215 add_friend (type,
5216 tsubst_friend_function (TREE_VALUE (friends),
5217 args));
5218 else
5219 abort ();
5222 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5223 t != NULL_TREE;
5224 t = TREE_CHAIN (t))
5226 tree friend_type = TREE_VALUE (t);
5227 tree new_friend_type;
5229 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5230 new_friend_type = tsubst_friend_class (friend_type, args);
5231 else if (uses_template_parms (friend_type))
5232 new_friend_type = tsubst (friend_type, args,
5233 tf_error | tf_warning, NULL_TREE);
5234 else
5236 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5238 /* The call to xref_tag_from_type does injection for friend
5239 classes. */
5240 push_nested_namespace (ns);
5241 new_friend_type =
5242 xref_tag_from_type (friend_type, NULL_TREE, 1);
5243 pop_nested_namespace (ns);
5246 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5247 /* Trick make_friend_class into realizing that the friend
5248 we're adding is a template, not an ordinary class. It's
5249 important that we use make_friend_class since it will
5250 perform some error-checking and output cross-reference
5251 information. */
5252 ++processing_template_decl;
5254 if (new_friend_type != error_mark_node)
5255 make_friend_class (type, new_friend_type);
5257 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5258 --processing_template_decl;
5261 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5262 if (TREE_CODE (t) == FIELD_DECL)
5264 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5265 require_complete_type (t);
5268 /* Set the file and line number information to whatever is given for
5269 the class itself. This puts error messages involving generated
5270 implicit functions at a predictable point, and the same point
5271 that would be used for non-template classes. */
5272 lineno = DECL_SOURCE_LINE (typedecl);
5273 input_filename = DECL_SOURCE_FILE (typedecl);
5275 unreverse_member_declarations (type);
5276 finish_struct_1 (type);
5277 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5279 /* Clear this now so repo_template_used is happy. */
5280 TYPE_BEING_DEFINED (type) = 0;
5281 repo_template_used (type);
5283 /* Now that the class is complete, instantiate default arguments for
5284 any member functions. We don't do this earlier because the
5285 default arguments may reference members of the class. */
5286 if (!PRIMARY_TEMPLATE_P (template))
5287 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5288 if (TREE_CODE (t) == FUNCTION_DECL
5289 /* Implicitly generated member functions will not have template
5290 information; they are not instantiations, but instead are
5291 created "fresh" for each instantiation. */
5292 && DECL_TEMPLATE_INFO (t))
5293 tsubst_default_arguments (t);
5295 popclass ();
5296 pop_from_top_level ();
5297 pop_tinst_level ();
5299 return type;
5302 static int
5303 list_eq (t1, t2)
5304 tree t1, t2;
5306 if (t1 == NULL_TREE)
5307 return t2 == NULL_TREE;
5308 if (t2 == NULL_TREE)
5309 return 0;
5310 /* Don't care if one declares its arg const and the other doesn't -- the
5311 main variant of the arg type is all that matters. */
5312 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5313 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5314 return 0;
5315 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5318 /* If arg is a non-type template parameter that does not depend on template
5319 arguments, fold it like we weren't in the body of a template. */
5321 static tree
5322 maybe_fold_nontype_arg (arg)
5323 tree arg;
5325 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5327 /* Sometimes, one of the args was an expression involving a
5328 template constant parameter, like N - 1. Now that we've
5329 tsubst'd, we might have something like 2 - 1. This will
5330 confuse lookup_template_class, so we do constant folding
5331 here. We have to unset processing_template_decl, to
5332 fool build_expr_from_tree() into building an actual
5333 tree. */
5335 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5336 as simple as it's going to get, and trying to reprocess
5337 the trees will break. */
5338 if (!TREE_TYPE (arg))
5340 int saved_processing_template_decl = processing_template_decl;
5341 processing_template_decl = 0;
5342 arg = build_expr_from_tree (arg);
5343 processing_template_decl = saved_processing_template_decl;
5346 arg = fold (arg);
5348 return arg;
5351 /* Substitute ARGS into the vector of template arguments T. */
5353 static tree
5354 tsubst_template_arg_vector (t, args, complain)
5355 tree t;
5356 tree args;
5357 tsubst_flags_t complain;
5359 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5360 tree *elts = (tree *) alloca (len * sizeof (tree));
5362 memset ((char *) elts, 0, len * sizeof (tree));
5364 for (i = 0; i < len; i++)
5366 if (TREE_VEC_ELT (t, i) != NULL_TREE
5367 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5368 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5369 args, complain);
5370 else
5371 elts[i] = maybe_fold_nontype_arg
5372 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5373 NULL_TREE));
5375 if (elts[i] == error_mark_node)
5376 return error_mark_node;
5378 if (elts[i] != TREE_VEC_ELT (t, i))
5379 need_new = 1;
5382 if (!need_new)
5383 return t;
5385 t = make_tree_vec (len);
5386 for (i = 0; i < len; i++)
5387 TREE_VEC_ELT (t, i) = elts[i];
5389 return t;
5392 /* Return the result of substituting ARGS into the template parameters
5393 given by PARMS. If there are m levels of ARGS and m + n levels of
5394 PARMS, then the result will contain n levels of PARMS. For
5395 example, if PARMS is `template <class T> template <class U>
5396 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5397 result will be `template <int*, double, class V>'. */
5399 static tree
5400 tsubst_template_parms (parms, args, complain)
5401 tree parms;
5402 tree args;
5403 tsubst_flags_t complain;
5405 tree r = NULL_TREE;
5406 tree* new_parms;
5408 for (new_parms = &r;
5409 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5410 new_parms = &(TREE_CHAIN (*new_parms)),
5411 parms = TREE_CHAIN (parms))
5413 tree new_vec =
5414 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5415 int i;
5417 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5419 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5420 tree default_value = TREE_PURPOSE (tuple);
5421 tree parm_decl = TREE_VALUE (tuple);
5423 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5424 default_value = tsubst_expr (default_value, args,
5425 complain, NULL_TREE);
5426 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5427 parm_decl);
5428 TREE_VEC_ELT (new_vec, i) = tuple;
5431 *new_parms =
5432 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5433 - TMPL_ARGS_DEPTH (args)),
5434 new_vec, NULL_TREE);
5437 return r;
5440 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5441 type T. If T is not an aggregate or enumeration type, it is
5442 handled as if by tsubst. IN_DECL is as for tsubst. If
5443 ENTERING_SCOPE is non-zero, T is the context for a template which
5444 we are presently tsubst'ing. Return the substituted value. */
5446 static tree
5447 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5448 tree t;
5449 tree args;
5450 tsubst_flags_t complain;
5451 tree in_decl;
5452 int entering_scope;
5454 if (t == NULL_TREE)
5455 return NULL_TREE;
5457 switch (TREE_CODE (t))
5459 case RECORD_TYPE:
5460 if (TYPE_PTRMEMFUNC_P (t))
5461 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5463 /* else fall through */
5464 case ENUMERAL_TYPE:
5465 case UNION_TYPE:
5466 if (TYPE_TEMPLATE_INFO (t))
5468 tree argvec;
5469 tree context;
5470 tree r;
5472 /* First, determine the context for the type we are looking
5473 up. */
5474 if (TYPE_CONTEXT (t) != NULL_TREE)
5475 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5476 complain,
5477 in_decl, /*entering_scope=*/1);
5478 else
5479 context = NULL_TREE;
5481 /* Then, figure out what arguments are appropriate for the
5482 type we are trying to find. For example, given:
5484 template <class T> struct S;
5485 template <class T, class U> void f(T, U) { S<U> su; }
5487 and supposing that we are instantiating f<int, double>,
5488 then our ARGS will be {int, double}, but, when looking up
5489 S we only want {double}. */
5490 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5491 complain);
5492 if (argvec == error_mark_node)
5493 return error_mark_node;
5495 r = lookup_template_class (t, argvec, in_decl, context,
5496 entering_scope, complain);
5498 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5500 else
5501 /* This is not a template type, so there's nothing to do. */
5502 return t;
5504 default:
5505 return tsubst (t, args, complain, in_decl);
5509 /* Substitute into the default argument ARG (a default argument for
5510 FN), which has the indicated TYPE. */
5512 tree
5513 tsubst_default_argument (fn, type, arg)
5514 tree fn;
5515 tree type;
5516 tree arg;
5518 /* This default argument came from a template. Instantiate the
5519 default argument here, not in tsubst. In the case of
5520 something like:
5522 template <class T>
5523 struct S {
5524 static T t();
5525 void f(T = t());
5528 we must be careful to do name lookup in the scope of S<T>,
5529 rather than in the current class. */
5530 if (DECL_CLASS_SCOPE_P (fn))
5531 pushclass (DECL_CONTEXT (fn), 2);
5533 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5534 tf_error | tf_warning, NULL_TREE);
5536 if (DECL_CLASS_SCOPE_P (fn))
5537 popclass ();
5539 /* Make sure the default argument is reasonable. */
5540 arg = check_default_argument (type, arg);
5542 return arg;
5545 /* Substitute into all the default arguments for FN. */
5547 static void
5548 tsubst_default_arguments (fn)
5549 tree fn;
5551 tree arg;
5552 tree tmpl_args;
5554 tmpl_args = DECL_TI_ARGS (fn);
5556 /* If this function is not yet instantiated, we certainly don't need
5557 its default arguments. */
5558 if (uses_template_parms (tmpl_args))
5559 return;
5561 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5562 arg;
5563 arg = TREE_CHAIN (arg))
5564 if (TREE_PURPOSE (arg))
5565 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5566 TREE_VALUE (arg),
5567 TREE_PURPOSE (arg));
5570 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5571 (already computed) substitution of ARGS into TREE_TYPE (T), if
5572 appropriate. Return the result of the substitution. Issue error
5573 and warning messages under control of COMPLAIN. */
5575 static tree
5576 tsubst_decl (t, args, type, complain)
5577 tree t;
5578 tree args;
5579 tree type;
5580 tsubst_flags_t complain;
5582 int saved_lineno;
5583 const char *saved_filename;
5584 tree r = NULL_TREE;
5585 tree in_decl = t;
5587 my_friendly_assert (complain & tf_error, 20011214);
5589 /* Set the filename and linenumber to improve error-reporting. */
5590 saved_lineno = lineno;
5591 saved_filename = input_filename;
5592 lineno = DECL_SOURCE_LINE (t);
5593 input_filename = DECL_SOURCE_FILE (t);
5595 switch (TREE_CODE (t))
5597 case TEMPLATE_DECL:
5599 /* We can get here when processing a member template function
5600 of a template class. */
5601 tree decl = DECL_TEMPLATE_RESULT (t);
5602 tree spec;
5603 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5605 if (!is_template_template_parm)
5607 /* We might already have an instance of this template.
5608 The ARGS are for the surrounding class type, so the
5609 full args contain the tsubst'd args for the context,
5610 plus the innermost args from the template decl. */
5611 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5612 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5613 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5614 tree full_args;
5616 full_args = tsubst_template_arg_vector (tmpl_args, args,
5617 complain);
5619 /* tsubst_template_arg_vector doesn't copy the vector if
5620 nothing changed. But, *something* should have
5621 changed. */
5622 my_friendly_assert (full_args != tmpl_args, 0);
5624 spec = retrieve_specialization (t, full_args);
5625 if (spec != NULL_TREE)
5627 r = spec;
5628 break;
5632 /* Make a new template decl. It will be similar to the
5633 original, but will record the current template arguments.
5634 We also create a new function declaration, which is just
5635 like the old one, but points to this new template, rather
5636 than the old one. */
5637 r = copy_decl (t);
5638 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5639 TREE_CHAIN (r) = NULL_TREE;
5641 if (is_template_template_parm)
5643 tree new_decl = tsubst (decl, args, complain, in_decl);
5644 DECL_TEMPLATE_RESULT (r) = new_decl;
5645 TREE_TYPE (r) = TREE_TYPE (new_decl);
5646 break;
5649 DECL_CONTEXT (r)
5650 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5651 complain, in_decl,
5652 /*entering_scope=*/1);
5653 DECL_VIRTUAL_CONTEXT (r)
5654 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5655 complain, in_decl,
5656 /*entering_scope=*/1);
5657 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5659 if (TREE_CODE (decl) == TYPE_DECL)
5661 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5662 TREE_TYPE (r) = new_type;
5663 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5664 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5665 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5667 else
5669 tree new_decl = tsubst (decl, args, complain, in_decl);
5671 DECL_TEMPLATE_RESULT (r) = new_decl;
5672 DECL_TI_TEMPLATE (new_decl) = r;
5673 TREE_TYPE (r) = TREE_TYPE (new_decl);
5674 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5677 SET_DECL_IMPLICIT_INSTANTIATION (r);
5678 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5679 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5681 /* The template parameters for this new template are all the
5682 template parameters for the old template, except the
5683 outermost level of parameters. */
5684 DECL_TEMPLATE_PARMS (r)
5685 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5686 complain);
5688 if (PRIMARY_TEMPLATE_P (t))
5689 DECL_PRIMARY_TEMPLATE (r) = r;
5691 /* We don't partially instantiate partial specializations. */
5692 if (TREE_CODE (decl) == TYPE_DECL)
5693 break;
5695 /* Record this partial instantiation. */
5696 register_specialization (r, t,
5697 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5700 break;
5702 case FUNCTION_DECL:
5704 tree ctx;
5705 tree argvec = NULL_TREE;
5706 tree *friends;
5707 tree gen_tmpl;
5708 int member;
5709 int args_depth;
5710 int parms_depth;
5712 /* Nobody should be tsubst'ing into non-template functions. */
5713 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5715 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5717 tree spec;
5719 /* Calculate the most general template of which R is a
5720 specialization, and the complete set of arguments used to
5721 specialize R. */
5722 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5723 argvec
5724 = tsubst_template_arg_vector (DECL_TI_ARGS
5725 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5726 args, complain);
5728 /* Check to see if we already have this specialization. */
5729 spec = retrieve_specialization (gen_tmpl, argvec);
5731 if (spec)
5733 r = spec;
5734 break;
5737 /* We can see more levels of arguments than parameters if
5738 there was a specialization of a member template, like
5739 this:
5741 template <class T> struct S { template <class U> void f(); }
5742 template <> template <class U> void S<int>::f(U);
5744 Here, we'll be substituting into the specialization,
5745 because that's where we can find the code we actually
5746 want to generate, but we'll have enough arguments for
5747 the most general template.
5749 We also deal with the peculiar case:
5751 template <class T> struct S {
5752 template <class U> friend void f();
5754 template <class U> void f() {}
5755 template S<int>;
5756 template void f<double>();
5758 Here, the ARGS for the instantiation of will be {int,
5759 double}. But, we only need as many ARGS as there are
5760 levels of template parameters in CODE_PATTERN. We are
5761 careful not to get fooled into reducing the ARGS in
5762 situations like:
5764 template <class T> struct S { template <class U> void f(U); }
5765 template <class T> template <> void S<T>::f(int) {}
5767 which we can spot because the pattern will be a
5768 specialization in this case. */
5769 args_depth = TMPL_ARGS_DEPTH (args);
5770 parms_depth =
5771 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5772 if (args_depth > parms_depth
5773 && !DECL_TEMPLATE_SPECIALIZATION (t))
5774 args = get_innermost_template_args (args, parms_depth);
5776 else
5778 /* This special case arises when we have something like this:
5780 template <class T> struct S {
5781 friend void f<int>(int, double);
5784 Here, the DECL_TI_TEMPLATE for the friend declaration
5785 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5786 being called from tsubst_friend_function, and we want
5787 only to create a new decl (R) with appropriate types so
5788 that we can call determine_specialization. */
5789 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5790 == LOOKUP_EXPR)
5791 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5792 == IDENTIFIER_NODE), 0);
5793 gen_tmpl = NULL_TREE;
5796 if (DECL_CLASS_SCOPE_P (t))
5798 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5799 member = 2;
5800 else
5801 member = 1;
5802 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5803 complain, t,
5804 /*entering_scope=*/1);
5806 else
5808 member = 0;
5809 ctx = DECL_CONTEXT (t);
5811 type = tsubst (type, args, complain, in_decl);
5812 if (type == error_mark_node)
5813 return error_mark_node;
5815 /* We do NOT check for matching decls pushed separately at this
5816 point, as they may not represent instantiations of this
5817 template, and in any case are considered separate under the
5818 discrete model. */
5819 r = copy_decl (t);
5820 DECL_USE_TEMPLATE (r) = 0;
5821 TREE_TYPE (r) = type;
5822 /* Clear out the mangled name and RTL for the instantiation. */
5823 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5824 SET_DECL_RTL (r, NULL_RTX);
5826 DECL_CONTEXT (r) = ctx;
5827 DECL_VIRTUAL_CONTEXT (r)
5828 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5829 complain, t,
5830 /*entering_scope=*/1);
5832 if (member && DECL_CONV_FN_P (r))
5833 /* Type-conversion operator. Reconstruct the name, in
5834 case it's the name of one of the template's parameters. */
5835 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5837 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5838 complain, t);
5839 DECL_RESULT (r) = NULL_TREE;
5841 TREE_STATIC (r) = 0;
5842 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5843 DECL_EXTERNAL (r) = 1;
5844 DECL_INTERFACE_KNOWN (r) = 0;
5845 DECL_DEFER_OUTPUT (r) = 0;
5846 TREE_CHAIN (r) = NULL_TREE;
5847 DECL_PENDING_INLINE_INFO (r) = 0;
5848 DECL_PENDING_INLINE_P (r) = 0;
5849 DECL_SAVED_TREE (r) = NULL_TREE;
5850 TREE_USED (r) = 0;
5851 if (DECL_CLONED_FUNCTION (r))
5853 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
5854 args, complain, t);
5855 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
5856 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
5859 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5860 this in the special friend case mentioned above where
5861 GEN_TMPL is NULL. */
5862 if (gen_tmpl)
5864 DECL_TEMPLATE_INFO (r)
5865 = tree_cons (gen_tmpl, argvec, NULL_TREE);
5866 SET_DECL_IMPLICIT_INSTANTIATION (r);
5867 register_specialization (r, gen_tmpl, argvec);
5869 /* We're not supposed to instantiate default arguments
5870 until they are called, for a template. But, for a
5871 declaration like:
5873 template <class T> void f ()
5874 { extern void g(int i = T()); }
5876 we should do the substitution when the template is
5877 instantiated. We handle the member function case in
5878 instantiate_class_template since the default arguments
5879 might refer to other members of the class. */
5880 if (!member
5881 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5882 && !uses_template_parms (argvec))
5883 tsubst_default_arguments (r);
5886 /* Copy the list of befriending classes. */
5887 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5888 *friends;
5889 friends = &TREE_CHAIN (*friends))
5891 *friends = copy_node (*friends);
5892 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5893 args, complain,
5894 in_decl);
5897 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
5899 maybe_retrofit_in_chrg (r);
5900 if (DECL_CONSTRUCTOR_P (r))
5901 grok_ctor_properties (ctx, r);
5902 /* If this is an instantiation of a member template, clone it.
5903 If it isn't, that'll be handled by
5904 clone_constructors_and_destructors. */
5905 if (PRIMARY_TEMPLATE_P (gen_tmpl))
5906 clone_function_decl (r, /*update_method_vec_p=*/0);
5908 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5909 grok_op_properties (r, DECL_FRIEND_P (r));
5911 break;
5913 case PARM_DECL:
5915 r = copy_node (t);
5916 if (DECL_TEMPLATE_PARM_P (t))
5917 SET_DECL_TEMPLATE_PARM_P (r);
5919 TREE_TYPE (r) = type;
5920 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5922 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5923 DECL_INITIAL (r) = TREE_TYPE (r);
5924 else
5925 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5926 complain, in_decl);
5928 DECL_CONTEXT (r) = NULL_TREE;
5929 if (!DECL_TEMPLATE_PARM_P (r) && PROMOTE_PROTOTYPES
5930 && INTEGRAL_TYPE_P (type)
5931 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5932 DECL_ARG_TYPE (r) = integer_type_node;
5933 if (TREE_CHAIN (t))
5934 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5935 complain, TREE_CHAIN (t));
5937 break;
5939 case FIELD_DECL:
5941 r = copy_decl (t);
5942 TREE_TYPE (r) = type;
5943 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5945 /* We don't have to set DECL_CONTEXT here; it is set by
5946 finish_member_declaration. */
5947 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
5948 complain, in_decl);
5949 TREE_CHAIN (r) = NULL_TREE;
5950 if (VOID_TYPE_P (type))
5951 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
5953 break;
5955 case USING_DECL:
5957 r = copy_node (t);
5958 DECL_INITIAL (r)
5959 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
5960 TREE_CHAIN (r) = NULL_TREE;
5962 break;
5964 case TYPE_DECL:
5965 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5966 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
5968 /* If this is the canonical decl, we don't have to mess with
5969 instantiations, and often we can't (for typename, template
5970 type parms and such). Note that TYPE_NAME is not correct for
5971 the above test if we've copied the type for a typedef. */
5972 r = TYPE_NAME (type);
5973 break;
5976 /* Fall through. */
5978 case VAR_DECL:
5980 tree argvec = NULL_TREE;
5981 tree gen_tmpl = NULL_TREE;
5982 tree spec;
5983 tree tmpl = NULL_TREE;
5984 tree ctx;
5985 int local_p;
5987 /* Assume this is a non-local variable. */
5988 local_p = 0;
5990 if (TYPE_P (CP_DECL_CONTEXT (t)))
5991 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5992 complain,
5993 in_decl, /*entering_scope=*/1);
5994 else if (DECL_NAMESPACE_SCOPE_P (t))
5995 ctx = DECL_CONTEXT (t);
5996 else
5998 /* Subsequent calls to pushdecl will fill this in. */
5999 ctx = NULL_TREE;
6000 local_p = 1;
6003 /* Check to see if we already have this specialization. */
6004 if (!local_p)
6006 tmpl = DECL_TI_TEMPLATE (t);
6007 gen_tmpl = most_general_template (tmpl);
6008 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6009 spec = retrieve_specialization (gen_tmpl, argvec);
6011 else
6012 spec = retrieve_local_specialization (t);
6014 if (spec)
6016 r = spec;
6017 break;
6020 r = copy_decl (t);
6021 TREE_TYPE (r) = type;
6022 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6023 DECL_CONTEXT (r) = ctx;
6024 /* Clear out the mangled name and RTL for the instantiation. */
6025 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6026 SET_DECL_RTL (r, NULL_RTX);
6028 /* Don't try to expand the initializer until someone tries to use
6029 this variable; otherwise we run into circular dependencies. */
6030 DECL_INITIAL (r) = NULL_TREE;
6031 SET_DECL_RTL (r, NULL_RTX);
6032 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6034 /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */
6035 if (DECL_PRETTY_FUNCTION_P (r))
6037 const char *const name = (*decl_printable_name)
6038 (current_function_decl, 2);
6039 DECL_INITIAL (r) = cp_fname_init (name);
6040 TREE_TYPE (r) = TREE_TYPE (DECL_INITIAL (r));
6043 /* Even if the original location is out of scope, the newly
6044 substituted one is not. */
6045 if (TREE_CODE (r) == VAR_DECL)
6046 DECL_DEAD_FOR_LOCAL (r) = 0;
6048 if (!local_p)
6050 /* A static data member declaration is always marked
6051 external when it is declared in-class, even if an
6052 initializer is present. We mimic the non-template
6053 processing here. */
6054 DECL_EXTERNAL (r) = 1;
6056 register_specialization (r, gen_tmpl, argvec);
6057 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6058 SET_DECL_IMPLICIT_INSTANTIATION (r);
6060 else
6061 register_local_specialization (r, t);
6063 TREE_CHAIN (r) = NULL_TREE;
6064 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6065 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6067 break;
6069 default:
6070 abort ();
6073 /* Restore the file and line information. */
6074 lineno = saved_lineno;
6075 input_filename = saved_filename;
6077 return r;
6080 /* Substitue into the ARG_TYPES of a function type. */
6082 static tree
6083 tsubst_arg_types (arg_types, args, complain, in_decl)
6084 tree arg_types;
6085 tree args;
6086 tsubst_flags_t complain;
6087 tree in_decl;
6089 tree remaining_arg_types;
6090 tree type;
6092 if (!arg_types || arg_types == void_list_node)
6093 return arg_types;
6095 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6096 args, complain, in_decl);
6097 if (remaining_arg_types == error_mark_node)
6098 return error_mark_node;
6100 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6101 if (type == error_mark_node)
6102 return error_mark_node;
6103 if (VOID_TYPE_P (type))
6105 if (complain & tf_error)
6107 error ("invalid parameter type `%T'", type);
6108 if (in_decl)
6109 cp_error_at ("in declaration `%D'", in_decl);
6111 return error_mark_node;
6114 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6115 top-level qualifiers as required. */
6116 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6118 /* Note that we do not substitute into default arguments here. The
6119 standard mandates that they be instantiated only when needed,
6120 which is done in build_over_call. */
6121 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6122 remaining_arg_types);
6126 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6127 *not* handle the exception-specification for FNTYPE, because the
6128 initial substitution of explicitly provided template parameters
6129 during argument deduction forbids substitution into the
6130 exception-specification:
6132 [temp.deduct]
6134 All references in the function type of the function template to the
6135 corresponding template parameters are replaced by the specified tem-
6136 plate argument values. If a substitution in a template parameter or
6137 in the function type of the function template results in an invalid
6138 type, type deduction fails. [Note: The equivalent substitution in
6139 exception specifications is done only when the function is instanti-
6140 ated, at which point a program is ill-formed if the substitution
6141 results in an invalid type.] */
6143 static tree
6144 tsubst_function_type (t, args, complain, in_decl)
6145 tree t;
6146 tree args;
6147 tsubst_flags_t complain;
6148 tree in_decl;
6150 tree return_type;
6151 tree arg_types;
6152 tree fntype;
6154 /* The TYPE_CONTEXT is not used for function/method types. */
6155 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6157 /* Substitute the return type. */
6158 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6159 if (return_type == error_mark_node)
6160 return error_mark_node;
6162 /* Substitue the argument types. */
6163 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6164 complain, in_decl);
6165 if (arg_types == error_mark_node)
6166 return error_mark_node;
6168 /* Construct a new type node and return it. */
6169 if (TREE_CODE (t) == FUNCTION_TYPE)
6170 fntype = build_function_type (return_type, arg_types);
6171 else
6173 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6174 if (! IS_AGGR_TYPE (r))
6176 /* [temp.deduct]
6178 Type deduction may fail for any of the following
6179 reasons:
6181 -- Attempting to create "pointer to member of T" when T
6182 is not a class type. */
6183 if (complain & tf_error)
6184 error ("creating pointer to member function of non-class type `%T'",
6186 return error_mark_node;
6189 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6190 (arg_types));
6192 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6193 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6195 return fntype;
6198 /* Substitute into the PARMS of a call-declarator. */
6200 static tree
6201 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6202 tree parms;
6203 tree args;
6204 tsubst_flags_t complain;
6205 tree in_decl;
6207 tree new_parms;
6208 tree type;
6209 tree defarg;
6211 if (!parms || parms == void_list_node)
6212 return parms;
6214 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6215 args, complain, in_decl);
6217 /* Figure out the type of this parameter. */
6218 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6220 /* Figure out the default argument as well. Note that we use
6221 tsubst_expr since the default argument is really an expression. */
6222 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6224 /* Chain this parameter on to the front of those we have already
6225 processed. We don't use hash_tree_cons because that function
6226 doesn't check TREE_PARMLIST. */
6227 new_parms = tree_cons (defarg, type, new_parms);
6229 /* And note that these are parameters. */
6230 TREE_PARMLIST (new_parms) = 1;
6232 return new_parms;
6235 /* Take the tree structure T and replace template parameters used
6236 therein with the argument vector ARGS. IN_DECL is an associated
6237 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6238 Issue error and warning messages under control of COMPLAIN. Note
6239 that we must be relatively non-tolerant of extensions here, in
6240 order to preserve conformance; if we allow substitutions that
6241 should not be allowed, we may allow argument deductions that should
6242 not succeed, and therefore report ambiguous overload situations
6243 where there are none. In theory, we could allow the substitution,
6244 but indicate that it should have failed, and allow our caller to
6245 make sure that the right thing happens, but we don't try to do this
6246 yet.
6248 This function is used for dealing with types, decls and the like;
6249 for expressions, use tsubst_expr or tsubst_copy. */
6251 tree
6252 tsubst (t, args, complain, in_decl)
6253 tree t, args;
6254 tsubst_flags_t complain;
6255 tree in_decl;
6257 tree type, r;
6259 if (t == NULL_TREE || t == error_mark_node
6260 || t == integer_type_node
6261 || t == void_type_node
6262 || t == char_type_node
6263 || TREE_CODE (t) == NAMESPACE_DECL)
6264 return t;
6266 if (TREE_CODE (t) == IDENTIFIER_NODE)
6267 type = IDENTIFIER_TYPE_VALUE (t);
6268 else
6269 type = TREE_TYPE (t);
6270 if (type == unknown_type_node)
6271 abort ();
6273 if (type && TREE_CODE (t) != FUNCTION_DECL
6274 && TREE_CODE (t) != TYPENAME_TYPE
6275 && TREE_CODE (t) != TEMPLATE_DECL
6276 && TREE_CODE (t) != IDENTIFIER_NODE
6277 && TREE_CODE (t) != FUNCTION_TYPE
6278 && TREE_CODE (t) != METHOD_TYPE)
6279 type = tsubst (type, args, complain, in_decl);
6280 if (type == error_mark_node)
6281 return error_mark_node;
6283 if (DECL_P (t))
6284 return tsubst_decl (t, args, type, complain);
6286 switch (TREE_CODE (t))
6288 case RECORD_TYPE:
6289 case UNION_TYPE:
6290 case ENUMERAL_TYPE:
6291 return tsubst_aggr_type (t, args, complain, in_decl,
6292 /*entering_scope=*/0);
6294 case ERROR_MARK:
6295 case IDENTIFIER_NODE:
6296 case VOID_TYPE:
6297 case REAL_TYPE:
6298 case COMPLEX_TYPE:
6299 case VECTOR_TYPE:
6300 case BOOLEAN_TYPE:
6301 case INTEGER_CST:
6302 case REAL_CST:
6303 case STRING_CST:
6304 return t;
6306 case INTEGER_TYPE:
6307 if (t == integer_type_node)
6308 return t;
6310 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6311 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6312 return t;
6315 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6317 max = tsubst_expr (omax, args, complain, in_decl);
6318 if (max == error_mark_node)
6319 return error_mark_node;
6321 /* See if we can reduce this expression to something simpler. */
6322 max = maybe_fold_nontype_arg (max);
6323 if (!processing_template_decl)
6324 max = decl_constant_value (max);
6326 if (processing_template_decl
6327 /* When providing explicit arguments to a template
6328 function, but leaving some arguments for subsequent
6329 deduction, MAX may be template-dependent even if we're
6330 not PROCESSING_TEMPLATE_DECL. We still need to check for
6331 template parms, though; MAX won't be an INTEGER_CST for
6332 dynamic arrays, either. */
6333 || (TREE_CODE (max) != INTEGER_CST
6334 && uses_template_parms (max)))
6336 tree itype = make_node (INTEGER_TYPE);
6337 TYPE_MIN_VALUE (itype) = size_zero_node;
6338 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6339 integer_one_node);
6340 return itype;
6343 if (integer_zerop (omax))
6345 /* Still allow an explicit array of size zero. */
6346 if (pedantic)
6347 pedwarn ("creating array with size zero");
6349 else if (integer_zerop (max)
6350 || (TREE_CODE (max) == INTEGER_CST
6351 && INT_CST_LT (max, integer_zero_node)))
6353 /* [temp.deduct]
6355 Type deduction may fail for any of the following
6356 reasons:
6358 Attempting to create an array with a size that is
6359 zero or negative. */
6360 if (complain & tf_error)
6361 error ("creating array with size zero (`%E')", max);
6363 return error_mark_node;
6366 return compute_array_index_type (NULL_TREE, max);
6369 case TEMPLATE_TYPE_PARM:
6370 case TEMPLATE_TEMPLATE_PARM:
6371 case BOUND_TEMPLATE_TEMPLATE_PARM:
6372 case TEMPLATE_PARM_INDEX:
6374 int idx;
6375 int level;
6376 int levels;
6378 r = NULL_TREE;
6380 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6381 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6382 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6384 idx = TEMPLATE_TYPE_IDX (t);
6385 level = TEMPLATE_TYPE_LEVEL (t);
6387 else
6389 idx = TEMPLATE_PARM_IDX (t);
6390 level = TEMPLATE_PARM_LEVEL (t);
6393 if (TREE_VEC_LENGTH (args) > 0)
6395 tree arg = NULL_TREE;
6397 levels = TMPL_ARGS_DEPTH (args);
6398 if (level <= levels)
6399 arg = TMPL_ARG (args, level, idx);
6401 if (arg == error_mark_node)
6402 return error_mark_node;
6403 else if (arg != NULL_TREE)
6405 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6407 my_friendly_assert (TYPE_P (arg), 0);
6408 return cp_build_qualified_type_real
6409 (arg, cp_type_quals (arg) | cp_type_quals (t),
6410 complain);
6412 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6414 /* We are processing a type constructed from
6415 a template template parameter */
6416 tree argvec = tsubst (TYPE_TI_ARGS (t),
6417 args, complain, in_decl);
6418 if (argvec == error_mark_node)
6419 return error_mark_node;
6421 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6422 we are resolving nested-types in the signature of
6423 a member function templates.
6424 Otherwise ARG is a TEMPLATE_DECL and is the real
6425 template to be instantiated. */
6426 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6427 arg = TYPE_NAME (arg);
6429 r = lookup_template_class (arg,
6430 argvec, in_decl,
6431 DECL_CONTEXT (arg),
6432 /*entering_scope=*/0,
6433 complain);
6434 return cp_build_qualified_type_real
6435 (r, TYPE_QUALS (t), complain);
6437 else
6438 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6439 return arg;
6442 else
6443 abort ();
6445 if (level == 1)
6446 /* This can happen during the attempted tsubst'ing in
6447 unify. This means that we don't yet have any information
6448 about the template parameter in question. */
6449 return t;
6451 /* If we get here, we must have been looking at a parm for a
6452 more deeply nested template. Make a new version of this
6453 template parameter, but with a lower level. */
6454 switch (TREE_CODE (t))
6456 case TEMPLATE_TYPE_PARM:
6457 case TEMPLATE_TEMPLATE_PARM:
6458 case BOUND_TEMPLATE_TEMPLATE_PARM:
6459 if (cp_type_quals (t))
6461 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6462 r = cp_build_qualified_type_real
6463 (r, cp_type_quals (t), complain);
6465 else
6467 r = copy_type (t);
6468 TEMPLATE_TYPE_PARM_INDEX (r)
6469 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6470 r, levels);
6471 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6472 TYPE_MAIN_VARIANT (r) = r;
6473 TYPE_POINTER_TO (r) = NULL_TREE;
6474 TYPE_REFERENCE_TO (r) = NULL_TREE;
6476 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6478 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6479 complain, in_decl);
6480 if (argvec == error_mark_node)
6481 return error_mark_node;
6483 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6484 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6487 break;
6489 case TEMPLATE_PARM_INDEX:
6490 r = reduce_template_parm_level (t, type, levels);
6491 break;
6493 default:
6494 abort ();
6497 return r;
6500 case TREE_LIST:
6502 tree purpose, value, chain, result;
6504 if (t == void_list_node)
6505 return t;
6507 purpose = TREE_PURPOSE (t);
6508 if (purpose)
6510 purpose = tsubst (purpose, args, complain, in_decl);
6511 if (purpose == error_mark_node)
6512 return error_mark_node;
6514 value = TREE_VALUE (t);
6515 if (value)
6517 value = tsubst (value, args, complain, in_decl);
6518 if (value == error_mark_node)
6519 return error_mark_node;
6521 chain = TREE_CHAIN (t);
6522 if (chain && chain != void_type_node)
6524 chain = tsubst (chain, args, complain, in_decl);
6525 if (chain == error_mark_node)
6526 return error_mark_node;
6528 if (purpose == TREE_PURPOSE (t)
6529 && value == TREE_VALUE (t)
6530 && chain == TREE_CHAIN (t))
6531 return t;
6532 if (TREE_PARMLIST (t))
6534 result = tree_cons (purpose, value, chain);
6535 TREE_PARMLIST (result) = 1;
6537 else
6538 result = hash_tree_cons (purpose, value, chain);
6539 return result;
6541 case TREE_VEC:
6542 if (type != NULL_TREE)
6544 /* A binfo node. We always need to make a copy, of the node
6545 itself and of its BINFO_BASETYPES. */
6547 t = copy_node (t);
6549 /* Make sure type isn't a typedef copy. */
6550 type = BINFO_TYPE (TYPE_BINFO (type));
6552 TREE_TYPE (t) = complete_type (type);
6553 if (IS_AGGR_TYPE (type))
6555 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6556 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6557 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6558 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6560 return t;
6563 /* Otherwise, a vector of template arguments. */
6564 return tsubst_template_arg_vector (t, args, complain);
6566 case POINTER_TYPE:
6567 case REFERENCE_TYPE:
6569 enum tree_code code;
6571 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6572 return t;
6574 code = TREE_CODE (t);
6577 /* [temp.deduct]
6579 Type deduction may fail for any of the following
6580 reasons:
6582 -- Attempting to create a pointer to reference type.
6583 -- Attempting to create a reference to a reference type or
6584 a reference to void. */
6585 if (TREE_CODE (type) == REFERENCE_TYPE
6586 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6588 static int last_line = 0;
6589 static const char* last_file = 0;
6591 /* We keep track of the last time we issued this error
6592 message to avoid spewing a ton of messages during a
6593 single bad template instantiation. */
6594 if (complain & tf_error
6595 && (last_line != lineno || last_file != input_filename))
6597 if (TREE_CODE (type) == VOID_TYPE)
6598 error ("forming reference to void");
6599 else
6600 error ("forming %s to reference type `%T'",
6601 (code == POINTER_TYPE) ? "pointer" : "reference",
6602 type);
6603 last_line = lineno;
6604 last_file = input_filename;
6607 return error_mark_node;
6609 else if (code == POINTER_TYPE)
6611 r = build_pointer_type (type);
6612 if (TREE_CODE (type) == METHOD_TYPE)
6613 r = build_ptrmemfunc_type (r);
6615 else
6616 r = build_reference_type (type);
6617 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6619 if (r != error_mark_node)
6620 /* Will this ever be needed for TYPE_..._TO values? */
6621 layout_type (r);
6623 return r;
6625 case OFFSET_TYPE:
6627 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6628 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6630 /* [temp.deduct]
6632 Type deduction may fail for any of the following
6633 reasons:
6635 -- Attempting to create "pointer to member of T" when T
6636 is not a class type. */
6637 if (complain & tf_error)
6638 error ("creating pointer to member of non-class type `%T'", r);
6639 return error_mark_node;
6641 if (TREE_CODE (type) == REFERENCE_TYPE)
6643 if (complain)
6644 error ("creating pointer to member reference type `%T'", type);
6646 return error_mark_node;
6648 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6649 if (TREE_CODE (type) == FUNCTION_TYPE)
6650 /* This is really a method type. The cv qualifiers of the
6651 this pointer should _not_ be determined by the cv
6652 qualifiers of the class type. They should be held
6653 somewhere in the FUNCTION_TYPE, but we don't do that at
6654 the moment. Consider
6655 typedef void (Func) () const;
6657 template <typename T1> void Foo (Func T1::*);
6660 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6661 TREE_TYPE (type),
6662 TYPE_ARG_TYPES (type));
6663 else
6664 return build_offset_type (r, type);
6666 case FUNCTION_TYPE:
6667 case METHOD_TYPE:
6669 tree fntype;
6670 tree raises;
6672 fntype = tsubst_function_type (t, args, complain, in_decl);
6673 if (fntype == error_mark_node)
6674 return error_mark_node;
6676 /* Substitue the exception specification. */
6677 raises = TYPE_RAISES_EXCEPTIONS (t);
6678 if (raises)
6680 tree list = NULL_TREE;
6682 if (! TREE_VALUE (raises))
6683 list = raises;
6684 else
6685 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6687 tree spec = TREE_VALUE (raises);
6689 spec = tsubst (spec, args, complain, in_decl);
6690 if (spec == error_mark_node)
6691 return spec;
6692 list = add_exception_specifier (list, spec, complain);
6694 fntype = build_exception_variant (fntype, list);
6696 return fntype;
6698 case ARRAY_TYPE:
6700 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6701 if (domain == error_mark_node)
6702 return error_mark_node;
6704 /* As an optimization, we avoid regenerating the array type if
6705 it will obviously be the same as T. */
6706 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6707 return t;
6709 /* These checks should match the ones in grokdeclarator.
6711 [temp.deduct]
6713 The deduction may fail for any of the following reasons:
6715 -- Attempting to create an array with an element type that
6716 is void, a function type, or a reference type. */
6717 if (TREE_CODE (type) == VOID_TYPE
6718 || TREE_CODE (type) == FUNCTION_TYPE
6719 || TREE_CODE (type) == REFERENCE_TYPE)
6721 if (complain & tf_error)
6722 error ("creating array of `%T'", type);
6723 return error_mark_node;
6726 r = build_cplus_array_type (type, domain);
6727 return r;
6730 case PLUS_EXPR:
6731 case MINUS_EXPR:
6733 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6734 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6736 if (e1 == error_mark_node || e2 == error_mark_node)
6737 return error_mark_node;
6739 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6742 case NEGATE_EXPR:
6743 case NOP_EXPR:
6745 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6746 if (e == error_mark_node)
6747 return error_mark_node;
6749 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6752 case TYPENAME_TYPE:
6754 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6755 in_decl, /*entering_scope=*/1);
6756 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6757 complain, in_decl);
6759 if (ctx == error_mark_node || f == error_mark_node)
6760 return error_mark_node;
6762 if (!IS_AGGR_TYPE (ctx))
6764 if (complain & tf_error)
6765 error ("`%T' is not a class, struct, or union type",
6766 ctx);
6767 return error_mark_node;
6769 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6771 /* Normally, make_typename_type does not require that the CTX
6772 have complete type in order to allow things like:
6774 template <class T> struct S { typename S<T>::X Y; };
6776 But, such constructs have already been resolved by this
6777 point, so here CTX really should have complete type, unless
6778 it's a partial instantiation. */
6779 ctx = complete_type (ctx);
6780 if (!COMPLETE_TYPE_P (ctx))
6782 if (complain & tf_error)
6783 incomplete_type_error (NULL_TREE, ctx);
6784 return error_mark_node;
6788 f = make_typename_type (ctx, f, complain & tf_error);
6789 if (f == error_mark_node)
6790 return f;
6791 return cp_build_qualified_type_real
6792 (f, cp_type_quals (f) | cp_type_quals (t), complain);
6795 case UNBOUND_CLASS_TEMPLATE:
6797 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6798 in_decl, /*entering_scope=*/1);
6799 tree name = TYPE_IDENTIFIER (t);
6801 if (ctx == error_mark_node || name == error_mark_node)
6802 return error_mark_node;
6804 return make_unbound_class_template (ctx, name, complain);
6807 case INDIRECT_REF:
6809 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6810 if (e == error_mark_node)
6811 return error_mark_node;
6812 return make_pointer_declarator (type, e);
6815 case ADDR_EXPR:
6817 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6818 if (e == error_mark_node)
6819 return error_mark_node;
6820 return make_reference_declarator (type, e);
6823 case ARRAY_REF:
6825 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6826 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
6827 if (e1 == error_mark_node || e2 == error_mark_node)
6828 return error_mark_node;
6830 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
6833 case CALL_EXPR:
6835 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6836 tree e2 = (tsubst_call_declarator_parms
6837 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6838 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6839 complain, in_decl);
6841 if (e1 == error_mark_node || e2 == error_mark_node
6842 || e3 == error_mark_node)
6843 return error_mark_node;
6845 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
6848 case SCOPE_REF:
6850 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6851 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6852 if (e1 == error_mark_node || e2 == error_mark_node)
6853 return error_mark_node;
6855 return build_nt (TREE_CODE (t), e1, e2);
6858 case TYPEOF_TYPE:
6860 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
6861 if (e1 == error_mark_node)
6862 return error_mark_node;
6864 return TREE_TYPE (e1);
6867 default:
6868 sorry ("use of `%s' in template",
6869 tree_code_name [(int) TREE_CODE (t)]);
6870 return error_mark_node;
6874 /* Like tsubst, but deals with expressions. This function just replaces
6875 template parms; to finish processing the resultant expression, use
6876 tsubst_expr. */
6878 tree
6879 tsubst_copy (t, args, complain, in_decl)
6880 tree t, args;
6881 tsubst_flags_t complain;
6882 tree in_decl;
6884 enum tree_code code;
6885 tree r;
6887 if (t == NULL_TREE || t == error_mark_node)
6888 return t;
6890 code = TREE_CODE (t);
6892 switch (code)
6894 case PARM_DECL:
6895 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6897 case CONST_DECL:
6899 tree enum_type;
6900 tree v;
6902 if (!DECL_CONTEXT (t))
6903 /* This is a global enumeration constant. */
6904 return t;
6906 /* Unfortunately, we cannot just call lookup_name here.
6907 Consider:
6909 template <int I> int f() {
6910 enum E { a = I };
6911 struct S { void g() { E e = a; } };
6914 When we instantiate f<7>::S::g(), say, lookup_name is not
6915 clever enough to find f<7>::a. */
6916 enum_type
6917 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
6918 /*entering_scope=*/0);
6920 for (v = TYPE_VALUES (enum_type);
6921 v != NULL_TREE;
6922 v = TREE_CHAIN (v))
6923 if (TREE_PURPOSE (v) == DECL_NAME (t))
6924 return TREE_VALUE (v);
6926 /* We didn't find the name. That should never happen; if
6927 name-lookup found it during preliminary parsing, we
6928 should find it again here during instantiation. */
6929 abort ();
6931 return t;
6933 case FIELD_DECL:
6934 if (DECL_CONTEXT (t))
6936 tree ctx;
6938 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
6939 /*entering_scope=*/1);
6940 if (ctx != DECL_CONTEXT (t))
6941 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6943 return t;
6945 case VAR_DECL:
6946 case FUNCTION_DECL:
6947 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
6948 t = tsubst (t, args, complain, in_decl);
6949 mark_used (t);
6950 return t;
6952 case TEMPLATE_DECL:
6953 if (is_member_template (t))
6954 return tsubst (t, args, complain, in_decl);
6955 else
6956 return t;
6958 case LOOKUP_EXPR:
6960 /* We must tsubst into a LOOKUP_EXPR in case the names to
6961 which it refers is a conversion operator; in that case the
6962 name will change. We avoid making unnecessary copies,
6963 however. */
6965 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
6967 if (id != TREE_OPERAND (t, 0))
6969 r = build_nt (LOOKUP_EXPR, id);
6970 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6971 t = r;
6974 return t;
6977 case CAST_EXPR:
6978 case REINTERPRET_CAST_EXPR:
6979 case CONST_CAST_EXPR:
6980 case STATIC_CAST_EXPR:
6981 case DYNAMIC_CAST_EXPR:
6982 case NOP_EXPR:
6983 return build1
6984 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6985 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
6987 case INDIRECT_REF:
6988 case NEGATE_EXPR:
6989 case TRUTH_NOT_EXPR:
6990 case BIT_NOT_EXPR:
6991 case ADDR_EXPR:
6992 case CONVERT_EXPR: /* Unary + */
6993 case SIZEOF_EXPR:
6994 case ALIGNOF_EXPR:
6995 case ARROW_EXPR:
6996 case THROW_EXPR:
6997 case TYPEID_EXPR:
6998 case REALPART_EXPR:
6999 case IMAGPART_EXPR:
7000 return build1
7001 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7002 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7004 case PLUS_EXPR:
7005 case MINUS_EXPR:
7006 case MULT_EXPR:
7007 case TRUNC_DIV_EXPR:
7008 case CEIL_DIV_EXPR:
7009 case FLOOR_DIV_EXPR:
7010 case ROUND_DIV_EXPR:
7011 case EXACT_DIV_EXPR:
7012 case BIT_AND_EXPR:
7013 case BIT_ANDTC_EXPR:
7014 case BIT_IOR_EXPR:
7015 case BIT_XOR_EXPR:
7016 case TRUNC_MOD_EXPR:
7017 case FLOOR_MOD_EXPR:
7018 case TRUTH_ANDIF_EXPR:
7019 case TRUTH_ORIF_EXPR:
7020 case TRUTH_AND_EXPR:
7021 case TRUTH_OR_EXPR:
7022 case RSHIFT_EXPR:
7023 case LSHIFT_EXPR:
7024 case RROTATE_EXPR:
7025 case LROTATE_EXPR:
7026 case EQ_EXPR:
7027 case NE_EXPR:
7028 case MAX_EXPR:
7029 case MIN_EXPR:
7030 case LE_EXPR:
7031 case GE_EXPR:
7032 case LT_EXPR:
7033 case GT_EXPR:
7034 case COMPONENT_REF:
7035 case ARRAY_REF:
7036 case COMPOUND_EXPR:
7037 case SCOPE_REF:
7038 case DOTSTAR_EXPR:
7039 case MEMBER_REF:
7040 case PREDECREMENT_EXPR:
7041 case PREINCREMENT_EXPR:
7042 case POSTDECREMENT_EXPR:
7043 case POSTINCREMENT_EXPR:
7044 return build_nt
7045 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7046 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7048 case CALL_EXPR:
7050 tree fn = TREE_OPERAND (t, 0);
7051 if (is_overloaded_fn (fn))
7052 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
7053 else
7054 /* Sometimes FN is a LOOKUP_EXPR. */
7055 fn = tsubst_copy (fn, args, complain, in_decl);
7056 return build_nt
7057 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7058 in_decl),
7059 NULL_TREE);
7062 case METHOD_CALL_EXPR:
7064 tree name = TREE_OPERAND (t, 0);
7065 if (TREE_CODE (name) == BIT_NOT_EXPR)
7067 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7068 complain, in_decl);
7069 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7071 else if (TREE_CODE (name) == SCOPE_REF
7072 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7074 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7075 complain, in_decl);
7076 name = TREE_OPERAND (name, 1);
7077 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7078 complain, in_decl);
7079 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7080 name = build_nt (SCOPE_REF, base, name);
7082 else
7083 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7084 return build_nt
7085 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7086 complain, in_decl),
7087 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7088 NULL_TREE);
7091 case STMT_EXPR:
7092 /* This processing should really occur in tsubst_expr, However,
7093 tsubst_expr does not recurse into expressions, since it
7094 assumes that there aren't any statements inside them.
7095 Instead, it simply calls build_expr_from_tree. So, we need
7096 to expand the STMT_EXPR here. */
7097 if (!processing_template_decl)
7099 tree stmt_expr = begin_stmt_expr ();
7100 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7101 return finish_stmt_expr (stmt_expr);
7104 return t;
7106 case COND_EXPR:
7107 case MODOP_EXPR:
7108 case PSEUDO_DTOR_EXPR:
7110 r = build_nt
7111 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7112 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7113 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7114 return r;
7117 case NEW_EXPR:
7119 r = build_nt
7120 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7121 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7122 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7123 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7124 return r;
7127 case DELETE_EXPR:
7129 r = build_nt
7130 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7131 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7132 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7133 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7134 return r;
7137 case TEMPLATE_ID_EXPR:
7139 /* Substituted template arguments */
7140 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7141 in_decl);
7143 if (targs && TREE_CODE (targs) == TREE_LIST)
7145 tree chain;
7146 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7147 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7149 else if (targs)
7151 int i;
7152 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7153 TREE_VEC_ELT (targs, i)
7154 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7157 return lookup_template_function
7158 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7161 case TREE_LIST:
7163 tree purpose, value, chain;
7165 if (t == void_list_node)
7166 return t;
7168 purpose = TREE_PURPOSE (t);
7169 if (purpose)
7170 purpose = tsubst_copy (purpose, args, complain, in_decl);
7171 value = TREE_VALUE (t);
7172 if (value)
7173 value = tsubst_copy (value, args, complain, in_decl);
7174 chain = TREE_CHAIN (t);
7175 if (chain && chain != void_type_node)
7176 chain = tsubst_copy (chain, args, complain, in_decl);
7177 if (purpose == TREE_PURPOSE (t)
7178 && value == TREE_VALUE (t)
7179 && chain == TREE_CHAIN (t))
7180 return t;
7181 return tree_cons (purpose, value, chain);
7184 case RECORD_TYPE:
7185 case UNION_TYPE:
7186 case ENUMERAL_TYPE:
7187 case INTEGER_TYPE:
7188 case TEMPLATE_TYPE_PARM:
7189 case TEMPLATE_TEMPLATE_PARM:
7190 case BOUND_TEMPLATE_TEMPLATE_PARM:
7191 case TEMPLATE_PARM_INDEX:
7192 case POINTER_TYPE:
7193 case REFERENCE_TYPE:
7194 case OFFSET_TYPE:
7195 case FUNCTION_TYPE:
7196 case METHOD_TYPE:
7197 case ARRAY_TYPE:
7198 case TYPENAME_TYPE:
7199 case UNBOUND_CLASS_TEMPLATE:
7200 case TYPEOF_TYPE:
7201 case TYPE_DECL:
7202 return tsubst (t, args, complain, in_decl);
7204 case IDENTIFIER_NODE:
7205 if (IDENTIFIER_TYPENAME_P (t))
7207 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7208 return mangle_conv_op_name_for_type (new_type);
7210 else
7211 return t;
7213 case CONSTRUCTOR:
7215 r = build
7216 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7217 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7218 complain, in_decl));
7219 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7220 return r;
7223 case VA_ARG_EXPR:
7224 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7225 in_decl),
7226 tsubst (TREE_TYPE (t), args, complain, in_decl));
7228 default:
7229 return t;
7233 /* Like tsubst_copy, but also does semantic processing. */
7235 tree
7236 tsubst_expr (t, args, complain, in_decl)
7237 tree t, args;
7238 tsubst_flags_t complain;
7239 tree in_decl;
7241 tree stmt, tmp;
7243 if (t == NULL_TREE || t == error_mark_node)
7244 return t;
7246 if (processing_template_decl)
7247 return tsubst_copy (t, args, complain, in_decl);
7249 if (!statement_code_p (TREE_CODE (t)))
7250 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7252 switch (TREE_CODE (t))
7254 case RETURN_INIT:
7255 prep_stmt (t);
7256 finish_named_return_value
7257 (TREE_OPERAND (t, 0),
7258 tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl));
7259 break;
7261 case CTOR_INITIALIZER:
7263 tree member_init_list;
7264 tree base_init_list;
7266 prep_stmt (t);
7267 member_init_list
7268 = tsubst_initializer_list (TREE_OPERAND (t, 0), args);
7269 base_init_list
7270 = tsubst_initializer_list (TREE_OPERAND (t, 1), args);
7271 emit_base_init (member_init_list, base_init_list);
7272 break;
7275 case RETURN_STMT:
7276 prep_stmt (t);
7277 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
7278 args, complain, in_decl));
7279 break;
7281 case EXPR_STMT:
7282 prep_stmt (t);
7283 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7284 args, complain, in_decl));
7285 break;
7287 case USING_STMT:
7288 prep_stmt (t);
7289 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7290 args, complain, in_decl));
7291 break;
7293 case DECL_STMT:
7295 tree decl;
7296 tree init;
7298 prep_stmt (t);
7299 decl = DECL_STMT_DECL (t);
7300 if (TREE_CODE (decl) == LABEL_DECL)
7301 finish_label_decl (DECL_NAME (decl));
7302 else if (TREE_CODE (decl) == USING_DECL)
7304 tree scope = DECL_INITIAL (decl);
7305 tree name = DECL_NAME (decl);
7307 scope = tsubst_expr (scope, args, complain, in_decl);
7308 do_local_using_decl (build_nt (SCOPE_REF, scope, name));
7310 else
7312 init = DECL_INITIAL (decl);
7313 decl = tsubst (decl, args, complain, in_decl);
7314 if (DECL_PRETTY_FUNCTION_P (decl))
7315 init = DECL_INITIAL (decl);
7316 else
7317 init = tsubst_expr (init, args, complain, in_decl);
7318 if (decl != error_mark_node)
7320 if (TREE_CODE (decl) != TYPE_DECL)
7321 /* Make sure the type is instantiated now. */
7322 complete_type (TREE_TYPE (decl));
7323 if (init)
7324 DECL_INITIAL (decl) = error_mark_node;
7325 /* By marking the declaration as instantiated, we avoid
7326 trying to instantiate it. Since instantiate_decl can't
7327 handle local variables, and since we've already done
7328 all that needs to be done, that's the right thing to
7329 do. */
7330 if (TREE_CODE (decl) == VAR_DECL)
7331 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7332 maybe_push_decl (decl);
7333 cp_finish_decl (decl, init, NULL_TREE, 0);
7337 /* A DECL_STMT can also be used as an expression, in the condition
7338 clause of a if/for/while construct. If we aren't followed by
7339 another statement, return our decl. */
7340 if (TREE_CHAIN (t) == NULL_TREE)
7341 return decl;
7343 break;
7345 case FOR_STMT:
7347 prep_stmt (t);
7349 stmt = begin_for_stmt ();
7350 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7351 finish_for_init_stmt (stmt);
7352 finish_for_cond (tsubst_expr (FOR_COND (t),
7353 args, complain, in_decl),
7354 stmt);
7355 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7356 finish_for_expr (tmp, stmt);
7357 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7358 finish_for_stmt (stmt);
7360 break;
7362 case WHILE_STMT:
7364 prep_stmt (t);
7365 stmt = begin_while_stmt ();
7366 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7367 args, complain, in_decl),
7368 stmt);
7369 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7370 finish_while_stmt (stmt);
7372 break;
7374 case DO_STMT:
7376 prep_stmt (t);
7377 stmt = begin_do_stmt ();
7378 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7379 finish_do_body (stmt);
7380 finish_do_stmt (tsubst_expr (DO_COND (t),
7381 args, complain, in_decl),
7382 stmt);
7384 break;
7386 case IF_STMT:
7388 prep_stmt (t);
7389 stmt = begin_if_stmt ();
7390 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7391 args, complain, in_decl),
7392 stmt);
7394 if (tmp = THEN_CLAUSE (t), tmp)
7396 tsubst_expr (tmp, args, complain, in_decl);
7397 finish_then_clause (stmt);
7400 if (tmp = ELSE_CLAUSE (t), tmp)
7402 begin_else_clause ();
7403 tsubst_expr (tmp, args, complain, in_decl);
7404 finish_else_clause (stmt);
7407 finish_if_stmt ();
7409 break;
7411 case COMPOUND_STMT:
7413 prep_stmt (t);
7414 if (COMPOUND_STMT_BODY_BLOCK (t))
7415 stmt = begin_function_body ();
7416 else
7417 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7419 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7421 if (COMPOUND_STMT_BODY_BLOCK (t))
7422 finish_function_body (stmt);
7423 else
7424 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7426 break;
7428 case BREAK_STMT:
7429 prep_stmt (t);
7430 finish_break_stmt ();
7431 break;
7433 case CONTINUE_STMT:
7434 prep_stmt (t);
7435 finish_continue_stmt ();
7436 break;
7438 case SWITCH_STMT:
7440 tree val;
7442 prep_stmt (t);
7443 stmt = begin_switch_stmt ();
7444 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7445 finish_switch_cond (val, stmt);
7446 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7447 finish_switch_stmt (stmt);
7449 break;
7451 case CASE_LABEL:
7452 prep_stmt (t);
7453 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7454 tsubst_expr (CASE_HIGH (t), args, complain,
7455 in_decl));
7456 break;
7458 case LABEL_STMT:
7459 lineno = STMT_LINENO (t);
7460 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7461 break;
7463 case GOTO_STMT:
7464 prep_stmt (t);
7465 tmp = GOTO_DESTINATION (t);
7466 if (TREE_CODE (tmp) != LABEL_DECL)
7467 /* Computed goto's must be tsubst'd into. On the other hand,
7468 non-computed gotos must not be; the identifier in question
7469 will have no binding. */
7470 tmp = tsubst_expr (tmp, args, complain, in_decl);
7471 else
7472 tmp = DECL_NAME (tmp);
7473 finish_goto_stmt (tmp);
7474 break;
7476 case ASM_STMT:
7477 prep_stmt (t);
7478 finish_asm_stmt (ASM_CV_QUAL (t),
7479 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7480 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7481 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7482 tsubst_expr (ASM_CLOBBERS (t), args, complain,
7483 in_decl));
7484 break;
7486 case TRY_BLOCK:
7487 prep_stmt (t);
7488 if (CLEANUP_P (t))
7490 stmt = begin_try_block ();
7491 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7492 finish_cleanup_try_block (stmt);
7493 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7494 complain, in_decl),
7495 stmt);
7497 else
7499 if (FN_TRY_BLOCK_P (t))
7500 stmt = begin_function_try_block ();
7501 else
7502 stmt = begin_try_block ();
7504 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7506 if (FN_TRY_BLOCK_P (t))
7507 finish_function_try_block (stmt);
7508 else
7509 finish_try_block (stmt);
7511 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7512 if (FN_TRY_BLOCK_P (t))
7513 finish_function_handler_sequence (stmt);
7514 else
7515 finish_handler_sequence (stmt);
7517 break;
7519 case HANDLER:
7521 tree decl;
7523 prep_stmt (t);
7524 stmt = begin_handler ();
7525 if (HANDLER_PARMS (t))
7527 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7528 decl = tsubst (decl, args, complain, in_decl);
7529 /* Prevent instantiate_decl from trying to instantiate
7530 this variable. We've already done all that needs to be
7531 done. */
7532 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7534 else
7535 decl = NULL_TREE;
7536 finish_handler_parms (decl, stmt);
7537 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7538 finish_handler (stmt);
7540 break;
7542 case TAG_DEFN:
7543 prep_stmt (t);
7544 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7545 break;
7547 case CTOR_STMT:
7548 add_stmt (copy_node (t));
7549 break;
7551 default:
7552 abort ();
7555 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7558 /* Instantiate the indicated variable or function template TMPL with
7559 the template arguments in TARG_PTR. */
7561 tree
7562 instantiate_template (tmpl, targ_ptr)
7563 tree tmpl, targ_ptr;
7565 tree fndecl;
7566 tree gen_tmpl;
7567 tree spec;
7568 int i, len;
7569 tree inner_args;
7571 if (tmpl == error_mark_node)
7572 return error_mark_node;
7574 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7576 /* If this function is a clone, handle it specially. */
7577 if (DECL_CLONED_FUNCTION_P (tmpl))
7579 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
7580 tree clone;
7582 /* Look for the clone. */
7583 for (clone = TREE_CHAIN (spec);
7584 clone && DECL_CLONED_FUNCTION_P (clone);
7585 clone = TREE_CHAIN (clone))
7586 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7587 return clone;
7588 /* We should always have found the clone by now. */
7589 abort ();
7590 return NULL_TREE;
7593 /* Check to see if we already have this specialization. */
7594 spec = retrieve_specialization (tmpl, targ_ptr);
7595 if (spec != NULL_TREE)
7596 return spec;
7598 gen_tmpl = most_general_template (tmpl);
7599 if (tmpl != gen_tmpl)
7601 /* The TMPL is a partial instantiation. To get a full set of
7602 arguments we must add the arguments used to perform the
7603 partial instantiation. */
7604 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7605 targ_ptr);
7607 /* Check to see if we already have this specialization. */
7608 spec = retrieve_specialization (gen_tmpl, targ_ptr);
7609 if (spec != NULL_TREE)
7610 return spec;
7613 len = DECL_NTPARMS (gen_tmpl);
7614 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
7615 i = len;
7616 while (i--)
7618 tree t = TREE_VEC_ELT (inner_args, i);
7619 if (TYPE_P (t))
7621 tree nt = target_type (t);
7622 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
7624 error ("type `%T' composed from a local class is not a valid template-argument", t);
7625 error (" trying to instantiate `%D'", gen_tmpl);
7626 return error_mark_node;
7631 /* substitute template parameters */
7632 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
7633 targ_ptr, tf_error, gen_tmpl);
7634 /* The DECL_TI_TEMPLATE should always be the immediate parent
7635 template, not the most general template. */
7636 DECL_TI_TEMPLATE (fndecl) = tmpl;
7638 if (flag_external_templates)
7639 add_pending_template (fndecl);
7641 /* If we've just instantiated the main entry point for a function,
7642 instantiate all the alternate entry points as well. We do this
7643 by cloning the instantiation of the main entry point, not by
7644 instantiating the template clones. */
7645 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
7646 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
7648 return fndecl;
7651 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7652 arguments that are being used when calling it. TARGS is a vector
7653 into which the deduced template arguments are placed.
7655 Return zero for success, 2 for an incomplete match that doesn't resolve
7656 all the types, and 1 for complete failure. An error message will be
7657 printed only for an incomplete match.
7659 If FN is a conversion operator, or we are trying to produce a specific
7660 specialization, RETURN_TYPE is the return type desired.
7662 The EXPLICIT_TARGS are explicit template arguments provided via a
7663 template-id.
7665 The parameter STRICT is one of:
7667 DEDUCE_CALL:
7668 We are deducing arguments for a function call, as in
7669 [temp.deduct.call].
7671 DEDUCE_CONV:
7672 We are deducing arguments for a conversion function, as in
7673 [temp.deduct.conv].
7675 DEDUCE_EXACT:
7676 We are deducing arguments when doing an explicit instantiation
7677 as in [temp.explicit], when determining an explicit specialization
7678 as in [temp.expl.spec], or when taking the address of a function
7679 template, as in [temp.deduct.funcaddr].
7681 DEDUCE_ORDER:
7682 We are deducing arguments when calculating the partial
7683 ordering between specializations of function or class
7684 templates, as in [temp.func.order] and [temp.class.order].
7686 LEN is the number of parms to consider before returning success, or -1
7687 for all. This is used in partial ordering to avoid comparing parms for
7688 which no actual argument was passed, since they are not considered in
7689 overload resolution (and are explicitly excluded from consideration in
7690 partial ordering in [temp.func.order]/6). */
7693 fn_type_unification (fn, explicit_targs, targs, args, return_type,
7694 strict, len)
7695 tree fn, explicit_targs, targs, args, return_type;
7696 unification_kind_t strict;
7697 int len;
7699 tree parms;
7700 tree fntype;
7701 int result;
7703 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7705 fntype = TREE_TYPE (fn);
7706 if (explicit_targs)
7708 /* [temp.deduct]
7710 The specified template arguments must match the template
7711 parameters in kind (i.e., type, nontype, template), and there
7712 must not be more arguments than there are parameters;
7713 otherwise type deduction fails.
7715 Nontype arguments must match the types of the corresponding
7716 nontype template parameters, or must be convertible to the
7717 types of the corresponding nontype parameters as specified in
7718 _temp.arg.nontype_, otherwise type deduction fails.
7720 All references in the function type of the function template
7721 to the corresponding template parameters are replaced by the
7722 specified template argument values. If a substitution in a
7723 template parameter or in the function type of the function
7724 template results in an invalid type, type deduction fails. */
7725 int i;
7726 tree converted_args;
7728 converted_args
7729 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7730 explicit_targs, NULL_TREE, tf_none,
7731 /*require_all_arguments=*/0));
7732 if (converted_args == error_mark_node)
7733 return 1;
7735 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
7736 if (fntype == error_mark_node)
7737 return 1;
7739 /* Place the explicitly specified arguments in TARGS. */
7740 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7741 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
7744 parms = TYPE_ARG_TYPES (fntype);
7745 /* Never do unification on the 'this' parameter. */
7746 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
7747 parms = TREE_CHAIN (parms);
7749 if (return_type)
7751 /* We've been given a return type to match, prepend it. */
7752 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
7753 args = tree_cons (NULL_TREE, return_type, args);
7754 if (len >= 0)
7755 ++len;
7758 /* We allow incomplete unification without an error message here
7759 because the standard doesn't seem to explicitly prohibit it. Our
7760 callers must be ready to deal with unification failures in any
7761 event. */
7762 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7763 targs, parms, args, /*subr=*/0,
7764 strict, /*allow_incomplete*/1, len);
7766 if (result == 0)
7767 /* All is well so far. Now, check:
7769 [temp.deduct]
7771 When all template arguments have been deduced, all uses of
7772 template parameters in nondeduced contexts are replaced with
7773 the corresponding deduced argument values. If the
7774 substitution results in an invalid type, as described above,
7775 type deduction fails. */
7776 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
7777 == error_mark_node)
7778 return 1;
7780 return result;
7783 /* Adjust types before performing type deduction, as described in
7784 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7785 sections are symmetric. PARM is the type of a function parameter
7786 or the return type of the conversion function. ARG is the type of
7787 the argument passed to the call, or the type of the value
7788 initialized with the result of the conversion function. */
7790 static int
7791 maybe_adjust_types_for_deduction (strict, parm, arg)
7792 unification_kind_t strict;
7793 tree* parm;
7794 tree* arg;
7796 int result = 0;
7798 switch (strict)
7800 case DEDUCE_CALL:
7801 break;
7803 case DEDUCE_CONV:
7805 /* Swap PARM and ARG throughout the remainder of this
7806 function; the handling is precisely symmetric since PARM
7807 will initialize ARG rather than vice versa. */
7808 tree* temp = parm;
7809 parm = arg;
7810 arg = temp;
7811 break;
7814 case DEDUCE_EXACT:
7815 /* There is nothing to do in this case. */
7816 return 0;
7818 case DEDUCE_ORDER:
7819 /* DR 214. [temp.func.order] is underspecified, and leads to no
7820 ordering between things like `T *' and `T const &' for `U *'.
7821 The former has T=U and the latter T=U*. The former looks more
7822 specialized and John Spicer considers it well-formed (the EDG
7823 compiler accepts it).
7825 John also confirms that deduction should proceed as in a function
7826 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
7827 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7828 to an actual call can have such a type.
7830 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7831 If only ARG is a REFERENCE_TYPE, we look through that and then
7832 proceed as with DEDUCE_CALL (which could further convert it). */
7833 if (TREE_CODE (*arg) == REFERENCE_TYPE)
7835 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7836 return 0;
7837 *arg = TREE_TYPE (*arg);
7839 break;
7840 default:
7841 abort ();
7844 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7846 /* [temp.deduct.call]
7848 If P is not a reference type:
7850 --If A is an array type, the pointer type produced by the
7851 array-to-pointer standard conversion (_conv.array_) is
7852 used in place of A for type deduction; otherwise,
7854 --If A is a function type, the pointer type produced by
7855 the function-to-pointer standard conversion
7856 (_conv.func_) is used in place of A for type deduction;
7857 otherwise,
7859 --If A is a cv-qualified type, the top level
7860 cv-qualifiers of A's type are ignored for type
7861 deduction. */
7862 if (TREE_CODE (*arg) == ARRAY_TYPE)
7863 *arg = build_pointer_type (TREE_TYPE (*arg));
7864 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
7865 *arg = build_pointer_type (*arg);
7866 else
7867 *arg = TYPE_MAIN_VARIANT (*arg);
7870 /* [temp.deduct.call]
7872 If P is a cv-qualified type, the top level cv-qualifiers
7873 of P's type are ignored for type deduction. If P is a
7874 reference type, the type referred to by P is used for
7875 type deduction. */
7876 *parm = TYPE_MAIN_VARIANT (*parm);
7877 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7879 *parm = TREE_TYPE (*parm);
7880 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
7882 return result;
7885 /* Most parms like fn_type_unification.
7887 If SUBR is 1, we're being called recursively (to unify the
7888 arguments of a function or method parameter of a function
7889 template). */
7891 static int
7892 type_unification_real (tparms, targs, xparms, xargs, subr,
7893 strict, allow_incomplete, xlen)
7894 tree tparms, targs, xparms, xargs;
7895 int subr;
7896 unification_kind_t strict;
7897 int allow_incomplete, xlen;
7899 tree parm, arg;
7900 int i;
7901 int ntparms = TREE_VEC_LENGTH (tparms);
7902 int sub_strict;
7903 int saw_undeduced = 0;
7904 tree parms, args;
7905 int len;
7907 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7908 my_friendly_assert (xparms == NULL_TREE
7909 || TREE_CODE (xparms) == TREE_LIST, 290);
7910 /* ARGS could be NULL (via a call from parse.y to
7911 build_x_function_call). */
7912 if (xargs)
7913 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
7914 my_friendly_assert (ntparms > 0, 292);
7916 switch (strict)
7918 case DEDUCE_CALL:
7919 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
7920 | UNIFY_ALLOW_DERIVED);
7921 break;
7923 case DEDUCE_CONV:
7924 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7925 break;
7927 case DEDUCE_EXACT:
7928 sub_strict = UNIFY_ALLOW_NONE;
7929 break;
7931 case DEDUCE_ORDER:
7932 sub_strict = UNIFY_ALLOW_NONE;
7933 break;
7935 default:
7936 abort ();
7939 if (xlen == 0)
7940 return 0;
7942 again:
7943 parms = xparms;
7944 args = xargs;
7945 len = xlen;
7947 while (parms
7948 && parms != void_list_node
7949 && args
7950 && args != void_list_node)
7952 parm = TREE_VALUE (parms);
7953 parms = TREE_CHAIN (parms);
7954 arg = TREE_VALUE (args);
7955 args = TREE_CHAIN (args);
7957 if (arg == error_mark_node)
7958 return 1;
7959 if (arg == unknown_type_node)
7960 /* We can't deduce anything from this, but we might get all the
7961 template args from other function args. */
7962 continue;
7964 /* Conversions will be performed on a function argument that
7965 corresponds with a function parameter that contains only
7966 non-deducible template parameters and explicitly specified
7967 template parameters. */
7968 if (! uses_template_parms (parm))
7970 tree type;
7972 if (!TYPE_P (arg))
7973 type = TREE_TYPE (arg);
7974 else
7976 type = arg;
7977 arg = NULL_TREE;
7980 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
7982 if (same_type_p (parm, type))
7983 continue;
7985 else
7986 /* It might work; we shouldn't check now, because we might
7987 get into infinite recursion. Overload resolution will
7988 handle it. */
7989 continue;
7991 return 1;
7994 if (!TYPE_P (arg))
7996 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
7997 if (type_unknown_p (arg))
7999 /* [temp.deduct.type] A template-argument can be deduced from
8000 a pointer to function or pointer to member function
8001 argument if the set of overloaded functions does not
8002 contain function templates and at most one of a set of
8003 overloaded functions provides a unique match. */
8005 if (resolve_overloaded_unification
8006 (tparms, targs, parm, arg, strict, sub_strict)
8007 != 0)
8008 return 1;
8009 continue;
8011 arg = TREE_TYPE (arg);
8015 int arg_strict = sub_strict;
8017 if (!subr)
8018 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8020 if (unify (tparms, targs, parm, arg, arg_strict))
8021 return 1;
8024 /* Are we done with the interesting parms? */
8025 if (--len == 0)
8026 goto done;
8028 /* Fail if we've reached the end of the parm list, and more args
8029 are present, and the parm list isn't variadic. */
8030 if (args && args != void_list_node && parms == void_list_node)
8031 return 1;
8032 /* Fail if parms are left and they don't have default values. */
8033 if (parms
8034 && parms != void_list_node
8035 && TREE_PURPOSE (parms) == NULL_TREE)
8036 return 1;
8038 done:
8039 if (!subr)
8040 for (i = 0; i < ntparms; i++)
8041 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8043 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8045 /* If this is an undeduced nontype parameter that depends on
8046 a type parameter, try another pass; its type may have been
8047 deduced from a later argument than the one from which
8048 this parameter can be deduced. */
8049 if (TREE_CODE (tparm) == PARM_DECL
8050 && uses_template_parms (TREE_TYPE (tparm))
8051 && !saw_undeduced++)
8052 goto again;
8054 if (!allow_incomplete)
8055 error ("incomplete type unification");
8056 return 2;
8058 return 0;
8061 /* Subroutine of type_unification_real. Args are like the variables at the
8062 call site. ARG is an overloaded function (or template-id); we try
8063 deducing template args from each of the overloads, and if only one
8064 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8066 static int
8067 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8068 sub_strict)
8069 tree tparms, targs, parm, arg;
8070 unification_kind_t strict;
8071 int sub_strict;
8073 tree tempargs = copy_node (targs);
8074 int good = 0;
8076 if (TREE_CODE (arg) == ADDR_EXPR)
8077 arg = TREE_OPERAND (arg, 0);
8079 if (TREE_CODE (arg) == COMPONENT_REF)
8080 /* Handle `&x' where `x' is some static or non-static member
8081 function name. */
8082 arg = TREE_OPERAND (arg, 1);
8084 if (TREE_CODE (arg) == OFFSET_REF)
8085 arg = TREE_OPERAND (arg, 1);
8087 /* Strip baselink information. */
8088 while (TREE_CODE (arg) == TREE_LIST)
8089 arg = TREE_VALUE (arg);
8091 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8093 /* If we got some explicit template args, we need to plug them into
8094 the affected templates before we try to unify, in case the
8095 explicit args will completely resolve the templates in question. */
8097 tree expl_subargs = TREE_OPERAND (arg, 1);
8098 arg = TREE_OPERAND (arg, 0);
8100 for (; arg; arg = OVL_NEXT (arg))
8102 tree fn = OVL_CURRENT (arg);
8103 tree subargs, elem;
8105 if (TREE_CODE (fn) != TEMPLATE_DECL)
8106 continue;
8108 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8109 expl_subargs);
8110 if (subargs)
8112 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
8113 if (TREE_CODE (elem) == METHOD_TYPE)
8114 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8115 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8116 strict, sub_strict);
8120 else if (TREE_CODE (arg) == OVERLOAD)
8122 for (; arg; arg = OVL_NEXT (arg))
8124 tree type = TREE_TYPE (OVL_CURRENT (arg));
8125 if (TREE_CODE (type) == METHOD_TYPE)
8126 type = build_ptrmemfunc_type (build_pointer_type (type));
8127 good += try_one_overload (tparms, targs, tempargs, parm,
8128 type,
8129 strict, sub_strict);
8132 else
8133 abort ();
8135 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8136 to function or pointer to member function argument if the set of
8137 overloaded functions does not contain function templates and at most
8138 one of a set of overloaded functions provides a unique match.
8140 So if we found multiple possibilities, we return success but don't
8141 deduce anything. */
8143 if (good == 1)
8145 int i = TREE_VEC_LENGTH (targs);
8146 for (; i--; )
8147 if (TREE_VEC_ELT (tempargs, i))
8148 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8150 if (good)
8151 return 0;
8153 return 1;
8156 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8157 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8158 different overloads deduce different arguments for a given parm.
8159 Returns 1 on success. */
8161 static int
8162 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8163 sub_strict)
8164 tree tparms, orig_targs, targs, parm, arg;
8165 unification_kind_t strict;
8166 int sub_strict;
8168 int nargs;
8169 tree tempargs;
8170 int i;
8172 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8173 to function or pointer to member function argument if the set of
8174 overloaded functions does not contain function templates and at most
8175 one of a set of overloaded functions provides a unique match.
8177 So if this is a template, just return success. */
8179 if (uses_template_parms (arg))
8180 return 1;
8182 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8184 /* We don't copy orig_targs for this because if we have already deduced
8185 some template args from previous args, unify would complain when we
8186 try to deduce a template parameter for the same argument, even though
8187 there isn't really a conflict. */
8188 nargs = TREE_VEC_LENGTH (targs);
8189 tempargs = make_tree_vec (nargs);
8191 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8192 return 0;
8194 /* First make sure we didn't deduce anything that conflicts with
8195 explicitly specified args. */
8196 for (i = nargs; i--; )
8198 tree elt = TREE_VEC_ELT (tempargs, i);
8199 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8201 if (elt == NULL_TREE)
8202 continue;
8203 else if (uses_template_parms (elt))
8205 /* Since we're unifying against ourselves, we will fill in template
8206 args used in the function parm list with our own template parms.
8207 Discard them. */
8208 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8209 continue;
8211 else if (oldelt && ! template_args_equal (oldelt, elt))
8212 return 0;
8215 for (i = nargs; i--; )
8217 tree elt = TREE_VEC_ELT (tempargs, i);
8219 if (elt)
8220 TREE_VEC_ELT (targs, i) = elt;
8223 return 1;
8226 /* Verify that nondeduce template argument agrees with the type
8227 obtained from argument deduction. Return nonzero if the
8228 verification fails.
8230 For example:
8232 struct A { typedef int X; };
8233 template <class T, class U> struct C {};
8234 template <class T> struct C<T, typename T::X> {};
8236 Then with the instantiation `C<A, int>', we can deduce that
8237 `T' is `A' but unify () does not check whether `typename T::X'
8238 is `int'. This function ensure that they agree.
8240 TARGS, PARMS are the same as the arguments of unify.
8241 ARGS contains template arguments from all levels. */
8243 static int
8244 verify_class_unification (targs, parms, args)
8245 tree targs, parms, args;
8247 int i;
8248 int nparms = TREE_VEC_LENGTH (parms);
8249 tree new_parms = tsubst (parms, add_outermost_template_args (args, targs),
8250 tf_none, NULL_TREE);
8251 if (new_parms == error_mark_node)
8252 return 1;
8254 args = INNERMOST_TEMPLATE_ARGS (args);
8256 for (i = 0; i < nparms; i++)
8258 tree parm = TREE_VEC_ELT (new_parms, i);
8259 tree arg = TREE_VEC_ELT (args, i);
8261 /* In case we are deducing from a function argument of a function
8262 templates, some parameters may not be deduced yet. So we
8263 make sure that only fully substituted elements of PARM are
8264 compared below. */
8266 if (!uses_template_parms (parm) && !template_args_equal (parm, arg))
8267 return 1;
8269 return 0;
8272 /* PARM is a template class (perhaps with unbound template
8273 parameters). ARG is a fully instantiated type. If ARG can be
8274 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8275 TARGS are as for unify. */
8277 static tree
8278 try_class_unification (tparms, targs, parm, arg)
8279 tree tparms;
8280 tree targs;
8281 tree parm;
8282 tree arg;
8284 tree copy_of_targs;
8286 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8287 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8288 return NULL_TREE;
8290 /* We need to make a new template argument vector for the call to
8291 unify. If we used TARGS, we'd clutter it up with the result of
8292 the attempted unification, even if this class didn't work out.
8293 We also don't want to commit ourselves to all the unifications
8294 we've already done, since unification is supposed to be done on
8295 an argument-by-argument basis. In other words, consider the
8296 following pathological case:
8298 template <int I, int J, int K>
8299 struct S {};
8301 template <int I, int J>
8302 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8304 template <int I, int J, int K>
8305 void f(S<I, J, K>, S<I, I, I>);
8307 void g() {
8308 S<0, 0, 0> s0;
8309 S<0, 1, 2> s2;
8311 f(s0, s2);
8314 Now, by the time we consider the unification involving `s2', we
8315 already know that we must have `f<0, 0, 0>'. But, even though
8316 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8317 because there are two ways to unify base classes of S<0, 1, 2>
8318 with S<I, I, I>. If we kept the already deduced knowledge, we
8319 would reject the possibility I=1. */
8320 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
8322 /* If unification failed, we're done. */
8323 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8324 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
8325 return NULL_TREE;
8327 return arg;
8330 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8331 have already discovered to be satisfactory. ARG_BINFO is the binfo
8332 for the base class of ARG that we are currently examining. */
8334 static tree
8335 get_template_base_recursive (tparms, targs, parm,
8336 arg_binfo, rval, flags)
8337 tree tparms;
8338 tree targs;
8339 tree arg_binfo;
8340 tree rval;
8341 tree parm;
8342 int flags;
8344 tree binfos;
8345 int i, n_baselinks;
8346 tree arg = BINFO_TYPE (arg_binfo);
8348 if (!(flags & GTB_IGNORE_TYPE))
8350 tree r = try_class_unification (tparms, targs,
8351 parm, arg);
8353 /* If there is more than one satisfactory baseclass, then:
8355 [temp.deduct.call]
8357 If they yield more than one possible deduced A, the type
8358 deduction fails.
8360 applies. */
8361 if (r && rval && !same_type_p (r, rval))
8362 return error_mark_node;
8363 else if (r)
8364 rval = r;
8367 binfos = BINFO_BASETYPES (arg_binfo);
8368 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8370 /* Process base types. */
8371 for (i = 0; i < n_baselinks; i++)
8373 tree base_binfo = TREE_VEC_ELT (binfos, i);
8374 int this_virtual;
8376 /* Skip this base, if we've already seen it. */
8377 if (BINFO_MARKED (base_binfo))
8378 continue;
8380 this_virtual =
8381 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8383 /* When searching for a non-virtual, we cannot mark virtually
8384 found binfos. */
8385 if (! this_virtual)
8386 SET_BINFO_MARKED (base_binfo);
8388 rval = get_template_base_recursive (tparms, targs,
8389 parm,
8390 base_binfo,
8391 rval,
8392 GTB_VIA_VIRTUAL * this_virtual);
8394 /* If we discovered more than one matching base class, we can
8395 stop now. */
8396 if (rval == error_mark_node)
8397 return error_mark_node;
8400 return rval;
8403 /* Given a template type PARM and a class type ARG, find the unique
8404 base type in ARG that is an instance of PARM. We do not examine
8405 ARG itself; only its base-classes. If there is no appropriate base
8406 class, return NULL_TREE. If there is more than one, return
8407 error_mark_node. PARM may be the type of a partial specialization,
8408 as well as a plain template type. Used by unify. */
8410 static tree
8411 get_template_base (tparms, targs, parm, arg)
8412 tree tparms;
8413 tree targs;
8414 tree parm;
8415 tree arg;
8417 tree rval;
8418 tree arg_binfo;
8420 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8422 arg_binfo = TYPE_BINFO (complete_type (arg));
8423 rval = get_template_base_recursive (tparms, targs,
8424 parm, arg_binfo,
8425 NULL_TREE,
8426 GTB_IGNORE_TYPE);
8428 /* Since get_template_base_recursive marks the bases classes, we
8429 must unmark them here. */
8430 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
8432 return rval;
8435 /* Returns the level of DECL, which declares a template parameter. */
8437 static int
8438 template_decl_level (decl)
8439 tree decl;
8441 switch (TREE_CODE (decl))
8443 case TYPE_DECL:
8444 case TEMPLATE_DECL:
8445 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8447 case PARM_DECL:
8448 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8450 default:
8451 abort ();
8452 return 0;
8456 /* Decide whether ARG can be unified with PARM, considering only the
8457 cv-qualifiers of each type, given STRICT as documented for unify.
8458 Returns non-zero iff the unification is OK on that basis.*/
8460 static int
8461 check_cv_quals_for_unify (strict, arg, parm)
8462 int strict;
8463 tree arg;
8464 tree parm;
8466 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
8467 && !at_least_as_qualified_p (arg, parm))
8468 return 0;
8470 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
8471 && !at_least_as_qualified_p (parm, arg))
8472 return 0;
8474 return 1;
8477 /* Takes parameters as for type_unification. Returns 0 if the
8478 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8479 bitwise or of the following flags:
8481 UNIFY_ALLOW_NONE:
8482 Require an exact match between PARM and ARG.
8483 UNIFY_ALLOW_MORE_CV_QUAL:
8484 Allow the deduced ARG to be more cv-qualified (by qualification
8485 conversion) than ARG.
8486 UNIFY_ALLOW_LESS_CV_QUAL:
8487 Allow the deduced ARG to be less cv-qualified than ARG.
8488 UNIFY_ALLOW_DERIVED:
8489 Allow the deduced ARG to be a template base class of ARG,
8490 or a pointer to a template base class of the type pointed to by
8491 ARG.
8492 UNIFY_ALLOW_INTEGER:
8493 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8494 case for more information.
8495 UNIFY_ALLOW_OUTER_LEVEL:
8496 This is the outermost level of a deduction. Used to determine validity
8497 of qualification conversions. A valid qualification conversion must
8498 have const qualified pointers leading up to the inner type which
8499 requires additional CV quals, except at the outer level, where const
8500 is not required [conv.qual]. It would be normal to set this flag in
8501 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8502 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8503 This is the outermost level of a deduction, and PARM can be more CV
8504 qualified at this point.
8505 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8506 This is the outermost level of a deduction, and PARM can be less CV
8507 qualified at this point.
8508 UNIFY_ALLOW_MAX_CORRECTION:
8509 This is an INTEGER_TYPE's maximum value. Used if the range may
8510 have been derived from a size specification, such as an array size.
8511 If the size was given by a nontype template parameter N, the maximum
8512 value will have the form N-1. The flag says that we can (and indeed
8513 must) unify N with (ARG + 1), an exception to the normal rules on
8514 folding PARM. */
8516 static int
8517 unify (tparms, targs, parm, arg, strict)
8518 tree tparms, targs, parm, arg;
8519 int strict;
8521 int idx;
8522 tree targ;
8523 tree tparm;
8524 int strict_in = strict;
8526 /* I don't think this will do the right thing with respect to types.
8527 But the only case I've seen it in so far has been array bounds, where
8528 signedness is the only information lost, and I think that will be
8529 okay. */
8530 while (TREE_CODE (parm) == NOP_EXPR)
8531 parm = TREE_OPERAND (parm, 0);
8533 if (arg == error_mark_node)
8534 return 1;
8535 if (arg == unknown_type_node)
8536 /* We can't deduce anything from this, but we might get all the
8537 template args from other function args. */
8538 return 0;
8540 /* If PARM uses template parameters, then we can't bail out here,
8541 even if ARG == PARM, since we won't record unifications for the
8542 template parameters. We might need them if we're trying to
8543 figure out which of two things is more specialized. */
8544 if (arg == parm && !uses_template_parms (parm))
8545 return 0;
8547 /* Immediately reject some pairs that won't unify because of
8548 cv-qualification mismatches. */
8549 if (TREE_CODE (arg) == TREE_CODE (parm)
8550 && TYPE_P (arg)
8551 /* It is the elements of the array which hold the cv quals of an array
8552 type, and the elements might be template type parms. We'll check
8553 when we recurse. */
8554 && TREE_CODE (arg) != ARRAY_TYPE
8555 /* We check the cv-qualifiers when unifying with template type
8556 parameters below. We want to allow ARG `const T' to unify with
8557 PARM `T' for example, when computing which of two templates
8558 is more specialized, for example. */
8559 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8560 && !check_cv_quals_for_unify (strict_in, arg, parm))
8561 return 1;
8563 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
8564 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
8565 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
8566 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
8567 strict &= ~UNIFY_ALLOW_DERIVED;
8568 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8569 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
8570 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
8572 switch (TREE_CODE (parm))
8574 case TYPENAME_TYPE:
8575 case SCOPE_REF:
8576 case UNBOUND_CLASS_TEMPLATE:
8577 /* In a type which contains a nested-name-specifier, template
8578 argument values cannot be deduced for template parameters used
8579 within the nested-name-specifier. */
8580 return 0;
8582 case TEMPLATE_TYPE_PARM:
8583 case TEMPLATE_TEMPLATE_PARM:
8584 case BOUND_TEMPLATE_TEMPLATE_PARM:
8585 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8587 if (TEMPLATE_TYPE_LEVEL (parm)
8588 != template_decl_level (tparm))
8589 /* The PARM is not one we're trying to unify. Just check
8590 to see if it matches ARG. */
8591 return (TREE_CODE (arg) == TREE_CODE (parm)
8592 && same_type_p (parm, arg)) ? 0 : 1;
8593 idx = TEMPLATE_TYPE_IDX (parm);
8594 targ = TREE_VEC_ELT (targs, idx);
8595 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
8597 /* Check for mixed types and values. */
8598 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8599 && TREE_CODE (tparm) != TYPE_DECL)
8600 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8601 && TREE_CODE (tparm) != TEMPLATE_DECL))
8602 return 1;
8604 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8606 /* ARG must be constructed from a template class or a template
8607 template parameter. */
8608 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
8609 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
8610 return 1;
8613 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8614 tree parmvec = TYPE_TI_ARGS (parm);
8615 tree argvec = TYPE_TI_ARGS (arg);
8616 tree argtmplvec
8617 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
8618 int i;
8620 /* The parameter and argument roles have to be switched here
8621 in order to handle default arguments properly. For example,
8622 template<template <class> class TT> void f(TT<int>)
8623 should be able to accept vector<int> which comes from
8624 template <class T, class Allocator = allocator>
8625 class vector. */
8627 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8628 == error_mark_node)
8629 return 1;
8631 /* Deduce arguments T, i from TT<T> or TT<i>.
8632 We check each element of PARMVEC and ARGVEC individually
8633 rather than the whole TREE_VEC since they can have
8634 different number of elements. */
8636 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8638 tree t = TREE_VEC_ELT (parmvec, i);
8640 if (unify (tparms, targs, t,
8641 TREE_VEC_ELT (argvec, i),
8642 UNIFY_ALLOW_NONE))
8643 return 1;
8646 arg = TYPE_TI_TEMPLATE (arg);
8648 /* Fall through to deduce template name. */
8651 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8652 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8654 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8656 /* Simple cases: Value already set, does match or doesn't. */
8657 if (targ != NULL_TREE && template_args_equal (targ, arg))
8658 return 0;
8659 else if (targ)
8660 return 1;
8662 else
8664 /* If PARM is `const T' and ARG is only `int', we don't have
8665 a match unless we are allowing additional qualification.
8666 If ARG is `const int' and PARM is just `T' that's OK;
8667 that binds `const int' to `T'. */
8668 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
8669 arg, parm))
8670 return 1;
8672 /* Consider the case where ARG is `const volatile int' and
8673 PARM is `const T'. Then, T should be `volatile int'. */
8674 arg = cp_build_qualified_type_real
8675 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
8676 if (arg == error_mark_node)
8677 return 1;
8679 /* Simple cases: Value already set, does match or doesn't. */
8680 if (targ != NULL_TREE && same_type_p (targ, arg))
8681 return 0;
8682 else if (targ)
8683 return 1;
8686 /* Make sure that ARG is not a variable-sized array. (Note that
8687 were talking about variable-sized arrays (like `int[n]'),
8688 rather than arrays of unknown size (like `int[]').) We'll
8689 get very confused by such a type since the bound of the array
8690 will not be computable in an instantiation. Besides, such
8691 types are not allowed in ISO C++, so we can do as we please
8692 here. */
8693 if (TREE_CODE (arg) == ARRAY_TYPE
8694 && !uses_template_parms (arg)
8695 && TYPE_DOMAIN (arg)
8696 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8697 != INTEGER_CST))
8698 return 1;
8700 TREE_VEC_ELT (targs, idx) = arg;
8701 return 0;
8703 case TEMPLATE_PARM_INDEX:
8704 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8706 if (TEMPLATE_PARM_LEVEL (parm)
8707 != template_decl_level (tparm))
8708 /* The PARM is not one we're trying to unify. Just check
8709 to see if it matches ARG. */
8710 return (TREE_CODE (arg) == TREE_CODE (parm)
8711 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
8713 idx = TEMPLATE_PARM_IDX (parm);
8714 targ = TREE_VEC_ELT (targs, idx);
8716 if (targ)
8718 int i = (cp_tree_equal (targ, arg) > 0);
8719 if (i == 1)
8720 return 0;
8721 else if (i == 0)
8722 return 1;
8723 else
8724 abort ();
8727 /* [temp.deduct.type] If, in the declaration of a function template
8728 with a non-type template-parameter, the non-type
8729 template-parameter is used in an expression in the function
8730 parameter-list and, if the corresponding template-argument is
8731 deduced, the template-argument type shall match the type of the
8732 template-parameter exactly, except that a template-argument
8733 deduced from an array bound may be of any integral type.
8734 The non-type parameter might use already deduced type parameters. */
8735 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
8736 if (same_type_p (TREE_TYPE (arg), tparm))
8737 /* OK */;
8738 else if ((strict & UNIFY_ALLOW_INTEGER)
8739 && (TREE_CODE (tparm) == INTEGER_TYPE
8740 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8741 /* OK */;
8742 else if (uses_template_parms (tparm))
8743 /* We haven't deduced the type of this parameter yet. Try again
8744 later. */
8745 return 0;
8746 else
8747 return 1;
8749 TREE_VEC_ELT (targs, idx) = arg;
8750 return 0;
8752 case POINTER_TYPE:
8754 if (TREE_CODE (arg) != POINTER_TYPE)
8755 return 1;
8757 /* [temp.deduct.call]
8759 A can be another pointer or pointer to member type that can
8760 be converted to the deduced A via a qualification
8761 conversion (_conv.qual_).
8763 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8764 This will allow for additional cv-qualification of the
8765 pointed-to types if appropriate. */
8767 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
8768 /* The derived-to-base conversion only persists through one
8769 level of pointers. */
8770 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
8772 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
8773 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
8775 /* Avoid getting confused about cv-quals; don't recurse here.
8776 Pointers to members should really be just OFFSET_TYPE, not
8777 this two-level nonsense... */
8779 parm = TREE_TYPE (parm);
8780 arg = TREE_TYPE (arg);
8781 goto offset;
8784 return unify (tparms, targs, TREE_TYPE (parm),
8785 TREE_TYPE (arg), strict);
8788 case REFERENCE_TYPE:
8789 if (TREE_CODE (arg) != REFERENCE_TYPE)
8790 return 1;
8791 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8792 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8794 case ARRAY_TYPE:
8795 if (TREE_CODE (arg) != ARRAY_TYPE)
8796 return 1;
8797 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8798 != (TYPE_DOMAIN (arg) == NULL_TREE))
8799 return 1;
8800 if (TYPE_DOMAIN (parm) != NULL_TREE
8801 && unify (tparms, targs, TYPE_DOMAIN (parm),
8802 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8803 return 1;
8804 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8805 UNIFY_ALLOW_NONE);
8807 case REAL_TYPE:
8808 case COMPLEX_TYPE:
8809 case VECTOR_TYPE:
8810 case INTEGER_TYPE:
8811 case BOOLEAN_TYPE:
8812 case VOID_TYPE:
8813 if (TREE_CODE (arg) != TREE_CODE (parm))
8814 return 1;
8816 if (TREE_CODE (parm) == INTEGER_TYPE
8817 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8819 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
8820 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
8821 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8822 return 1;
8823 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
8824 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
8825 TYPE_MAX_VALUE (arg),
8826 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
8827 return 1;
8829 /* We have already checked cv-qualification at the top of the
8830 function. */
8831 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
8832 return 1;
8834 /* As far as unification is concerned, this wins. Later checks
8835 will invalidate it if necessary. */
8836 return 0;
8838 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8839 /* Type INTEGER_CST can come from ordinary constant template args. */
8840 case INTEGER_CST:
8841 while (TREE_CODE (arg) == NOP_EXPR)
8842 arg = TREE_OPERAND (arg, 0);
8844 if (TREE_CODE (arg) != INTEGER_CST)
8845 return 1;
8846 return !tree_int_cst_equal (parm, arg);
8848 case TREE_VEC:
8850 int i;
8851 if (TREE_CODE (arg) != TREE_VEC)
8852 return 1;
8853 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8854 return 1;
8855 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
8856 if (unify (tparms, targs,
8857 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
8858 UNIFY_ALLOW_NONE))
8859 return 1;
8860 return 0;
8863 case RECORD_TYPE:
8864 case UNION_TYPE:
8865 if (TREE_CODE (arg) != TREE_CODE (parm))
8866 return 1;
8868 if (TYPE_PTRMEMFUNC_P (parm))
8870 if (!TYPE_PTRMEMFUNC_P (arg))
8871 return 1;
8873 return unify (tparms, targs,
8874 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8875 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8876 strict);
8879 if (CLASSTYPE_TEMPLATE_INFO (parm))
8881 tree t = NULL_TREE;
8883 if (strict_in & UNIFY_ALLOW_DERIVED)
8885 /* First, we try to unify the PARM and ARG directly. */
8886 t = try_class_unification (tparms, targs,
8887 parm, arg);
8889 if (!t)
8891 /* Fallback to the special case allowed in
8892 [temp.deduct.call]:
8894 If P is a class, and P has the form
8895 template-id, then A can be a derived class of
8896 the deduced A. Likewise, if P is a pointer to
8897 a class of the form template-id, A can be a
8898 pointer to a derived class pointed to by the
8899 deduced A. */
8900 t = get_template_base (tparms, targs,
8901 parm, arg);
8903 if (! t || t == error_mark_node)
8904 return 1;
8907 else if (CLASSTYPE_TEMPLATE_INFO (arg)
8908 && (CLASSTYPE_TI_TEMPLATE (parm)
8909 == CLASSTYPE_TI_TEMPLATE (arg)))
8910 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8911 Then, we should unify `int' and `U'. */
8912 t = arg;
8913 else
8914 /* There's no chance of unification succeeding. */
8915 return 1;
8917 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
8918 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
8920 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
8921 return 1;
8922 return 0;
8924 case METHOD_TYPE:
8925 case FUNCTION_TYPE:
8926 if (TREE_CODE (arg) != TREE_CODE (parm))
8927 return 1;
8929 if (unify (tparms, targs, TREE_TYPE (parm),
8930 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
8931 return 1;
8932 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
8933 TYPE_ARG_TYPES (arg), 1,
8934 DEDUCE_EXACT, 0, -1);
8936 case OFFSET_TYPE:
8937 offset:
8938 if (TREE_CODE (arg) != OFFSET_TYPE)
8939 return 1;
8940 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
8941 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
8942 return 1;
8943 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8944 strict);
8946 case CONST_DECL:
8947 if (arg != decl_constant_value (parm))
8948 return 1;
8949 return 0;
8951 case TEMPLATE_DECL:
8952 /* Matched cases are handled by the ARG == PARM test above. */
8953 return 1;
8955 case MINUS_EXPR:
8956 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
8957 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
8959 /* We handle this case specially, since it comes up with
8960 arrays. In particular, something like:
8962 template <int N> void f(int (&x)[N]);
8964 Here, we are trying to unify the range type, which
8965 looks like [0 ... (N - 1)]. */
8966 tree t, t1, t2;
8967 t1 = TREE_OPERAND (parm, 0);
8968 t2 = TREE_OPERAND (parm, 1);
8970 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
8972 return unify (tparms, targs, t1, t, strict);
8974 /* else fall through */
8976 default:
8977 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
8980 /* We're looking at an expression. This can happen with
8981 something like:
8983 template <int I>
8984 void foo(S<I>, S<I + 2>);
8986 This is a "nondeduced context":
8988 [deduct.type]
8990 The nondeduced contexts are:
8992 --A type that is a template-id in which one or more of
8993 the template-arguments is an expression that references
8994 a template-parameter.
8996 In these cases, we assume deduction succeeded, but don't
8997 actually infer any unifications. */
8999 if (!uses_template_parms (parm)
9000 && !template_args_equal (parm, arg))
9001 return 1;
9002 else
9003 return 0;
9005 else
9006 sorry ("use of `%s' in template type unification",
9007 tree_code_name [(int) TREE_CODE (parm)]);
9009 return 1;
9013 /* Called if RESULT is explicitly instantiated, or is a member of an
9014 explicitly instantiated class, or if using -frepo and the
9015 instantiation of RESULT has been assigned to this file. */
9017 void
9018 mark_decl_instantiated (result, extern_p)
9019 tree result;
9020 int extern_p;
9022 if (TREE_CODE (result) != FUNCTION_DECL)
9023 /* The TREE_PUBLIC flag for function declarations will have been
9024 set correctly by tsubst. */
9025 TREE_PUBLIC (result) = 1;
9027 /* We used to set this unconditionally; we moved that to
9028 do_decl_instantiation so it wouldn't get set on members of
9029 explicit class template instantiations. But we still need to set
9030 it here for the 'extern template' case in order to suppress
9031 implicit instantiations. */
9032 if (extern_p)
9033 SET_DECL_EXPLICIT_INSTANTIATION (result);
9035 if (! extern_p)
9037 DECL_INTERFACE_KNOWN (result) = 1;
9038 DECL_NOT_REALLY_EXTERN (result) = 1;
9040 /* Always make artificials weak. */
9041 if (DECL_ARTIFICIAL (result) && flag_weak)
9042 comdat_linkage (result);
9043 /* For WIN32 we also want to put explicit instantiations in
9044 linkonce sections. */
9045 else if (TREE_PUBLIC (result))
9046 maybe_make_one_only (result);
9048 else if (TREE_CODE (result) == FUNCTION_DECL)
9049 defer_fn (result);
9052 /* Given two function templates PAT1 and PAT2, return:
9054 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9056 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9057 -1 if PAT2 is more specialized than PAT1.
9058 0 if neither is more specialized.
9060 LEN is passed through to fn_type_unification. */
9063 more_specialized (pat1, pat2, deduce, len)
9064 tree pat1, pat2;
9065 int deduce;
9066 int len;
9068 tree targs;
9069 int winner = 0;
9071 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9072 NULL_TREE, 0, deduce, len);
9073 if (targs)
9074 --winner;
9076 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9077 NULL_TREE, 0, deduce, len);
9078 if (targs)
9079 ++winner;
9081 return winner;
9084 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9086 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9087 -1 if PAT2 is more specialized than PAT1.
9088 0 if neither is more specialized. */
9091 more_specialized_class (pat1, pat2)
9092 tree pat1, pat2;
9094 tree targs;
9095 int winner = 0;
9097 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9098 TREE_PURPOSE (pat2));
9099 if (targs)
9100 --winner;
9102 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9103 TREE_PURPOSE (pat1));
9104 if (targs)
9105 ++winner;
9107 return winner;
9110 /* Return the template arguments that will produce the function signature
9111 DECL from the function template FN, with the explicit template
9112 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9113 also match. Return NULL_TREE if no satisfactory arguments could be
9114 found. DEDUCE and LEN are passed through to fn_type_unification. */
9116 static tree
9117 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9118 tree fn, decl, explicit_args;
9119 int check_rettype, deduce, len;
9121 int ntparms = DECL_NTPARMS (fn);
9122 tree targs = make_tree_vec (ntparms);
9123 tree decl_type;
9124 tree decl_arg_types;
9125 int i;
9127 /* Substitute the explicit template arguments into the type of DECL.
9128 The call to fn_type_unification will handle substitution into the
9129 FN. */
9130 decl_type = TREE_TYPE (decl);
9131 if (explicit_args && uses_template_parms (decl_type))
9133 tree tmpl;
9134 tree converted_args;
9136 if (DECL_TEMPLATE_INFO (decl))
9137 tmpl = DECL_TI_TEMPLATE (decl);
9138 else
9139 /* We can get here for some illegal specializations. */
9140 return NULL_TREE;
9142 converted_args
9143 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9144 explicit_args, NULL_TREE,
9145 tf_none, /*require_all_arguments=*/0));
9146 if (converted_args == error_mark_node)
9147 return NULL_TREE;
9149 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
9150 if (decl_type == error_mark_node)
9151 return NULL_TREE;
9154 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9155 /* Never do unification on the 'this' parameter. */
9156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9157 decl_arg_types = TREE_CHAIN (decl_arg_types);
9159 i = fn_type_unification (fn, explicit_args, targs,
9160 decl_arg_types,
9161 (check_rettype || DECL_CONV_FN_P (fn)
9162 ? TREE_TYPE (decl_type) : NULL_TREE),
9163 deduce, len);
9165 if (i != 0)
9166 return NULL_TREE;
9168 return targs;
9171 /* For most uses, we want to check the return type. */
9173 tree
9174 get_bindings (fn, decl, explicit_args)
9175 tree fn, decl, explicit_args;
9177 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9180 /* But for resolve_overloaded_unification, we only care about the parameter
9181 types. */
9183 static tree
9184 get_bindings_overload (fn, decl, explicit_args)
9185 tree fn, decl, explicit_args;
9187 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9190 /* Return the innermost template arguments that, when applied to a
9191 template specialization whose innermost template parameters are
9192 TPARMS, and whose specialization arguments are PARMS, yield the
9193 ARGS.
9195 For example, suppose we have:
9197 template <class T, class U> struct S {};
9198 template <class T> struct S<T*, int> {};
9200 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9201 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9202 int}. The resulting vector will be {double}, indicating that `T'
9203 is bound to `double'. */
9205 static tree
9206 get_class_bindings (tparms, parms, args)
9207 tree tparms, parms, args;
9209 int i, ntparms = TREE_VEC_LENGTH (tparms);
9210 tree vec = make_tree_vec (ntparms);
9212 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9213 UNIFY_ALLOW_NONE))
9214 return NULL_TREE;
9216 for (i = 0; i < ntparms; ++i)
9217 if (! TREE_VEC_ELT (vec, i))
9218 return NULL_TREE;
9220 if (verify_class_unification (vec, parms, args))
9221 return NULL_TREE;
9223 return vec;
9226 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9227 Pick the most specialized template, and return the corresponding
9228 instantiation, or if there is no corresponding instantiation, the
9229 template itself. If there is no most specialized template,
9230 error_mark_node is returned. If there are no templates at all,
9231 NULL_TREE is returned. */
9233 tree
9234 most_specialized_instantiation (instantiations)
9235 tree instantiations;
9237 tree fn, champ;
9238 int fate;
9240 if (!instantiations)
9241 return NULL_TREE;
9243 champ = instantiations;
9244 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
9246 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9247 DEDUCE_EXACT, -1);
9248 if (fate == 1)
9250 else
9252 if (fate == 0)
9254 fn = TREE_CHAIN (fn);
9255 if (! fn)
9256 return error_mark_node;
9258 champ = fn;
9262 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
9264 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9265 DEDUCE_EXACT, -1);
9266 if (fate != 1)
9267 return error_mark_node;
9270 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9273 /* Return the most specialized of the list of templates in FNS that can
9274 produce an instantiation matching DECL, given the explicit template
9275 arguments EXPLICIT_ARGS. */
9277 static tree
9278 most_specialized (fns, decl, explicit_args)
9279 tree fns, decl, explicit_args;
9281 tree candidates = NULL_TREE;
9282 tree fn, args;
9284 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9286 tree candidate = TREE_VALUE (fn);
9288 args = get_bindings (candidate, decl, explicit_args);
9289 if (args)
9290 candidates = tree_cons (NULL_TREE, candidate, candidates);
9293 return most_specialized_instantiation (candidates);
9296 /* If DECL is a specialization of some template, return the most
9297 general such template. Otherwise, returns NULL_TREE.
9299 For example, given:
9301 template <class T> struct S { template <class U> void f(U); };
9303 if TMPL is `template <class U> void S<int>::f(U)' this will return
9304 the full template. This function will not trace past partial
9305 specializations, however. For example, given in addition:
9307 template <class T> struct S<T*> { template <class U> void f(U); };
9309 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9310 `template <class T> template <class U> S<T*>::f(U)'. */
9312 tree
9313 most_general_template (decl)
9314 tree decl;
9316 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9317 an immediate specialization. */
9318 if (TREE_CODE (decl) == FUNCTION_DECL)
9320 if (DECL_TEMPLATE_INFO (decl)) {
9321 decl = DECL_TI_TEMPLATE (decl);
9323 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9324 template friend. */
9325 if (TREE_CODE (decl) != TEMPLATE_DECL)
9326 return NULL_TREE;
9327 } else
9328 return NULL_TREE;
9331 /* Look for more and more general templates. */
9332 while (DECL_TEMPLATE_INFO (decl))
9334 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9335 in some cases. (See cp-tree.h for details.) */
9336 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9337 break;
9339 /* Stop if we run into an explicitly specialized class template. */
9340 if (!DECL_NAMESPACE_SCOPE_P (decl)
9341 && DECL_CONTEXT (decl)
9342 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
9343 break;
9345 decl = DECL_TI_TEMPLATE (decl);
9348 return decl;
9351 /* Return the most specialized of the class template specializations
9352 of TMPL which can produce an instantiation matching ARGS, or
9353 error_mark_node if the choice is ambiguous. */
9355 static tree
9356 most_specialized_class (tmpl, args)
9357 tree tmpl;
9358 tree args;
9360 tree list = NULL_TREE;
9361 tree t;
9362 tree champ;
9363 int fate;
9365 tmpl = most_general_template (tmpl);
9366 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
9368 tree spec_args
9369 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9370 if (spec_args)
9372 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9373 TREE_TYPE (list) = TREE_TYPE (t);
9377 if (! list)
9378 return NULL_TREE;
9380 t = list;
9381 champ = t;
9382 t = TREE_CHAIN (t);
9383 for (; t; t = TREE_CHAIN (t))
9385 fate = more_specialized_class (champ, t);
9386 if (fate == 1)
9388 else
9390 if (fate == 0)
9392 t = TREE_CHAIN (t);
9393 if (! t)
9394 return error_mark_node;
9396 champ = t;
9400 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9402 fate = more_specialized_class (champ, t);
9403 if (fate != 1)
9404 return error_mark_node;
9407 return champ;
9410 /* called from the parser. */
9412 void
9413 do_decl_instantiation (declspecs, declarator, storage)
9414 tree declspecs, declarator, storage;
9416 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL);
9417 tree result = NULL_TREE;
9418 int extern_p = 0;
9420 if (!decl)
9421 /* An error occurred, for which grokdeclarator has already issued
9422 an appropriate message. */
9423 return;
9424 else if (! DECL_LANG_SPECIFIC (decl))
9426 error ("explicit instantiation of non-template `%#D'", decl);
9427 return;
9429 else if (TREE_CODE (decl) == VAR_DECL)
9431 /* There is an asymmetry here in the way VAR_DECLs and
9432 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9433 the latter, the DECL we get back will be marked as a
9434 template instantiation, and the appropriate
9435 DECL_TEMPLATE_INFO will be set up. This does not happen for
9436 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9437 should handle VAR_DECLs as it currently handles
9438 FUNCTION_DECLs. */
9439 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9440 if (result && TREE_CODE (result) != VAR_DECL)
9442 error ("no matching template for `%D' found", result);
9443 return;
9446 else if (TREE_CODE (decl) != FUNCTION_DECL)
9448 error ("explicit instantiation of `%#D'", decl);
9449 return;
9451 else
9452 result = decl;
9454 /* Check for various error cases. Note that if the explicit
9455 instantiation is legal the RESULT will currently be marked as an
9456 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9457 until we get here. */
9459 if (DECL_TEMPLATE_SPECIALIZATION (result))
9461 /* [temp.spec]
9463 No program shall both explicitly instantiate and explicitly
9464 specialize a template. */
9465 pedwarn ("explicit instantiation of `%#D' after", result);
9466 cp_pedwarn_at ("explicit specialization here", result);
9467 return;
9469 else if (DECL_EXPLICIT_INSTANTIATION (result))
9471 /* [temp.spec]
9473 No program shall explicitly instantiate any template more
9474 than once.
9476 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9477 instantiation was `extern' and the second is not, and EXTERN_P for
9478 the opposite case. If -frepo, chances are we already got marked
9479 as an explicit instantiation because of the repo file. */
9480 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
9481 pedwarn ("duplicate explicit instantiation of `%#D'", result);
9483 /* If we've already instantiated the template, just return now. */
9484 if (DECL_INTERFACE_KNOWN (result))
9485 return;
9487 else if (!DECL_IMPLICIT_INSTANTIATION (result))
9489 error ("no matching template for `%D' found", result);
9490 return;
9492 else if (!DECL_TEMPLATE_INFO (result))
9494 pedwarn ("explicit instantiation of non-template `%#D'", result);
9495 return;
9498 if (flag_external_templates)
9499 return;
9501 if (storage == NULL_TREE)
9503 else if (storage == ridpointers[(int) RID_EXTERN])
9505 if (pedantic && !in_system_header)
9506 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9507 extern_p = 1;
9509 else
9510 error ("storage class `%D' applied to template instantiation",
9511 storage);
9513 SET_DECL_EXPLICIT_INSTANTIATION (result);
9514 mark_decl_instantiated (result, extern_p);
9515 repo_template_instantiated (result, extern_p);
9516 if (! extern_p)
9517 instantiate_decl (result, /*defer_ok=*/1);
9520 void
9521 mark_class_instantiated (t, extern_p)
9522 tree t;
9523 int extern_p;
9525 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
9526 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9527 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
9528 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9529 if (! extern_p)
9531 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9532 rest_of_type_compilation (t, 1);
9536 /* Perform an explicit instantiation of template class T. STORAGE, if
9537 non-null, is the RID for extern, inline or static. COMPLAIN is
9538 non-zero if this is called from the parser, zero if called recursively,
9539 since the standard is unclear (as detailed below). */
9541 void
9542 do_type_instantiation (t, storage, complain)
9543 tree t, storage;
9544 tsubst_flags_t complain;
9546 int extern_p = 0;
9547 int nomem_p = 0;
9548 int static_p = 0;
9550 if (TREE_CODE (t) == TYPE_DECL)
9551 t = TREE_TYPE (t);
9553 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
9555 error ("explicit instantiation of non-template type `%T'", t);
9556 return;
9559 complete_type (t);
9561 /* With -fexternal-templates, explicit instantiations are treated the same
9562 as implicit ones. */
9563 if (flag_external_templates)
9564 return;
9566 if (!COMPLETE_TYPE_P (t))
9568 if (complain & tf_error)
9569 error ("explicit instantiation of `%#T' before definition of template",
9571 return;
9574 if (storage != NULL_TREE)
9576 if (pedantic && !in_system_header)
9577 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9578 IDENTIFIER_POINTER (storage));
9580 if (storage == ridpointers[(int) RID_INLINE])
9581 nomem_p = 1;
9582 else if (storage == ridpointers[(int) RID_EXTERN])
9583 extern_p = 1;
9584 else if (storage == ridpointers[(int) RID_STATIC])
9585 static_p = 1;
9586 else
9588 error ("storage class `%D' applied to template instantiation",
9589 storage);
9590 extern_p = 0;
9594 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9596 /* [temp.spec]
9598 No program shall both explicitly instantiate and explicitly
9599 specialize a template. */
9600 if (complain & tf_error)
9602 error ("explicit instantiation of `%#T' after", t);
9603 cp_error_at ("explicit specialization here", t);
9605 return;
9607 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
9609 /* [temp.spec]
9611 No program shall explicitly instantiate any template more
9612 than once.
9614 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9615 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9616 are we already got marked as an explicit instantiation because of the
9617 repo file. All these cases are OK. */
9618 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
9619 && (complain & tf_error))
9620 pedwarn ("duplicate explicit instantiation of `%#T'", t);
9622 /* If we've already instantiated the template, just return now. */
9623 if (!CLASSTYPE_INTERFACE_ONLY (t))
9624 return;
9627 mark_class_instantiated (t, extern_p);
9628 repo_template_instantiated (t, extern_p);
9630 if (nomem_p)
9631 return;
9634 tree tmp;
9636 /* In contrast to implicit instantiation, where only the
9637 declarations, and not the definitions, of members are
9638 instantiated, we have here:
9640 [temp.explicit]
9642 The explicit instantiation of a class template specialization
9643 implies the instantiation of all of its members not
9644 previously explicitly specialized in the translation unit
9645 containing the explicit instantiation.
9647 Of course, we can't instantiate member template classes, since
9648 we don't have any arguments for them. Note that the standard
9649 is unclear on whether the instantiation of the members are
9650 *explicit* instantiations or not. We choose to be generous,
9651 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9652 the explicit instantiation of a class where some of the members
9653 have no definition in the current translation unit. */
9655 if (! static_p)
9656 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
9657 if (TREE_CODE (tmp) == FUNCTION_DECL
9658 && DECL_TEMPLATE_INSTANTIATION (tmp))
9660 mark_decl_instantiated (tmp, extern_p);
9661 repo_template_instantiated (tmp, extern_p);
9662 if (! extern_p)
9663 instantiate_decl (tmp, /*defer_ok=*/1);
9666 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9667 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
9669 mark_decl_instantiated (tmp, extern_p);
9670 repo_template_instantiated (tmp, extern_p);
9671 if (! extern_p)
9672 instantiate_decl (tmp, /*defer_ok=*/1);
9675 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
9676 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9677 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
9678 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
9682 /* Given a function DECL, which is a specialization of TMPL, modify
9683 DECL to be a re-instantiation of TMPL with the same template
9684 arguments. TMPL should be the template into which tsubst'ing
9685 should occur for DECL, not the most general template.
9687 One reason for doing this is a scenario like this:
9689 template <class T>
9690 void f(const T&, int i);
9692 void g() { f(3, 7); }
9694 template <class T>
9695 void f(const T& t, const int i) { }
9697 Note that when the template is first instantiated, with
9698 instantiate_template, the resulting DECL will have no name for the
9699 first parameter, and the wrong type for the second. So, when we go
9700 to instantiate the DECL, we regenerate it. */
9702 static void
9703 regenerate_decl_from_template (decl, tmpl)
9704 tree decl;
9705 tree tmpl;
9707 /* The most general version of TMPL. */
9708 tree gen_tmpl;
9709 /* The arguments used to instantiate DECL, from the most general
9710 template. */
9711 tree args;
9712 tree code_pattern;
9713 tree new_decl;
9714 int unregistered;
9716 args = DECL_TI_ARGS (decl);
9717 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9719 /* Unregister the specialization so that when we tsubst we will not
9720 just return DECL. We don't have to unregister DECL from TMPL
9721 because if would only be registered there if it were a partial
9722 instantiation of a specialization, which it isn't: it's a full
9723 instantiation. */
9724 gen_tmpl = most_general_template (tmpl);
9725 unregistered = unregister_specialization (decl, gen_tmpl);
9727 /* If the DECL was not unregistered then something peculiar is
9728 happening: we created a specialization but did not call
9729 register_specialization for it. */
9730 my_friendly_assert (unregistered, 0);
9732 if (TREE_CODE (decl) == VAR_DECL)
9733 /* Make sure that we can see identifiers, and compute access
9734 correctly, for the class members used in the declaration of
9735 this static variable. */
9736 pushclass (DECL_CONTEXT (decl), 2);
9738 /* Do the substitution to get the new declaration. */
9739 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
9741 if (TREE_CODE (decl) == VAR_DECL)
9743 /* Set up DECL_INITIAL, since tsubst doesn't. */
9744 DECL_INITIAL (new_decl) =
9745 tsubst_expr (DECL_INITIAL (code_pattern), args,
9746 tf_error, DECL_TI_TEMPLATE (decl));
9747 /* Pop the class context we pushed above. */
9748 popclass ();
9750 else if (TREE_CODE (decl) == FUNCTION_DECL)
9752 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9753 new decl. */
9754 DECL_INITIAL (new_decl) = error_mark_node;
9755 /* And don't complain about a duplicate definition. */
9756 DECL_INITIAL (decl) = NULL_TREE;
9759 /* The immediate parent of the new template is still whatever it was
9760 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9761 general template. We also reset the DECL_ASSEMBLER_NAME since
9762 tsubst always calculates the name as if the function in question
9763 were really a template instance, and sometimes, with friend
9764 functions, this is not so. See tsubst_friend_function for
9765 details. */
9766 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9767 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
9768 COPY_DECL_RTL (decl, new_decl);
9769 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
9771 /* Call duplicate decls to merge the old and new declarations. */
9772 duplicate_decls (new_decl, decl);
9774 /* Now, re-register the specialization. */
9775 register_specialization (decl, gen_tmpl, args);
9778 /* Produce the definition of D, a _DECL generated from a template. If
9779 DEFER_OK is non-zero, then we don't have to actually do the
9780 instantiation now; we just have to do it sometime. */
9782 tree
9783 instantiate_decl (d, defer_ok)
9784 tree d;
9785 int defer_ok;
9787 tree tmpl = DECL_TI_TEMPLATE (d);
9788 tree args = DECL_TI_ARGS (d);
9789 tree td;
9790 tree code_pattern;
9791 tree spec;
9792 tree gen_tmpl;
9793 int pattern_defined;
9794 int line = lineno;
9795 int need_push;
9796 const char *file = input_filename;
9798 /* This function should only be used to instantiate templates for
9799 functions and static member variables. */
9800 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9801 || TREE_CODE (d) == VAR_DECL, 0);
9803 /* Don't instantiate cloned functions. Instead, instantiate the
9804 functions they cloned. */
9805 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
9806 d = DECL_CLONED_FUNCTION (d);
9808 if (DECL_TEMPLATE_INSTANTIATED (d))
9809 /* D has already been instantiated. It might seem reasonable to
9810 check whether or not D is an explicit instantiation, and, if so,
9811 stop here. But when an explicit instantiation is deferred
9812 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9813 is set, even though we still need to do the instantiation. */
9814 return d;
9816 /* If we already have a specialization of this declaration, then
9817 there's no reason to instantiate it. Note that
9818 retrieve_specialization gives us both instantiations and
9819 specializations, so we must explicitly check
9820 DECL_TEMPLATE_SPECIALIZATION. */
9821 gen_tmpl = most_general_template (tmpl);
9822 spec = retrieve_specialization (gen_tmpl, args);
9823 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9824 return spec;
9826 /* This needs to happen before any tsubsting. */
9827 if (! push_tinst_level (d))
9828 return d;
9830 timevar_push (TV_PARSE);
9832 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9833 for the instantiation. This is not always the most general
9834 template. Consider, for example:
9836 template <class T>
9837 struct S { template <class U> void f();
9838 template <> void f<int>(); };
9840 and an instantiation of S<double>::f<int>. We want TD to be the
9841 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9842 td = tmpl;
9843 while (/* An instantiation cannot have a definition, so we need a
9844 more general template. */
9845 DECL_TEMPLATE_INSTANTIATION (td)
9846 /* We must also deal with friend templates. Given:
9848 template <class T> struct S {
9849 template <class U> friend void f() {};
9852 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9853 so far as the language is concerned, but that's still
9854 where we get the pattern for the instantiation from. On
9855 other hand, if the definition comes outside the class, say:
9857 template <class T> struct S {
9858 template <class U> friend void f();
9860 template <class U> friend void f() {}
9862 we don't need to look any further. That's what the check for
9863 DECL_INITIAL is for. */
9864 || (TREE_CODE (d) == FUNCTION_DECL
9865 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9866 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))))
9868 /* The present template, TD, should not be a definition. If it
9869 were a definition, we should be using it! Note that we
9870 cannot restructure the loop to just keep going until we find
9871 a template with a definition, since that might go too far if
9872 a specialization was declared, but not defined. */
9873 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9874 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9875 0);
9877 /* Fetch the more general template. */
9878 td = DECL_TI_TEMPLATE (td);
9881 code_pattern = DECL_TEMPLATE_RESULT (td);
9883 if (TREE_CODE (d) == FUNCTION_DECL)
9884 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
9885 else
9886 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
9888 lineno = DECL_SOURCE_LINE (d);
9889 input_filename = DECL_SOURCE_FILE (d);
9891 if (pattern_defined)
9893 /* Let the repository code that this template definition is
9894 available.
9896 The repository doesn't need to know about cloned functions
9897 because they never actually show up in the object file. It
9898 does need to know about the clones; those are the symbols
9899 that the linker will be emitting error messages about. */
9900 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
9901 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
9903 tree t;
9905 for (t = TREE_CHAIN (d);
9906 t && DECL_CLONED_FUNCTION_P (t);
9907 t = TREE_CHAIN (t))
9908 repo_template_used (t);
9910 else
9911 repo_template_used (d);
9913 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
9915 if (flag_alt_external_templates)
9917 if (interface_unknown)
9918 warn_if_unknown_interface (d);
9920 else if (DECL_INTERFACE_KNOWN (code_pattern))
9922 DECL_INTERFACE_KNOWN (d) = 1;
9923 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
9925 else
9926 warn_if_unknown_interface (code_pattern);
9929 if (at_eof)
9930 import_export_decl (d);
9933 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
9934 && DECL_INITIAL (d) == NULL_TREE)
9935 /* We should have set up DECL_INITIAL in instantiate_class_template. */
9936 abort ();
9937 /* Reject all external templates except inline functions. */
9938 else if (DECL_INTERFACE_KNOWN (d)
9939 && ! DECL_NOT_REALLY_EXTERN (d)
9940 && ! (TREE_CODE (d) == FUNCTION_DECL
9941 && DECL_INLINE (d)))
9942 goto out;
9943 /* Defer all other templates, unless we have been explicitly
9944 forbidden from doing so. We restore the source position here
9945 because it's used by add_pending_template. */
9946 else if (! pattern_defined || defer_ok)
9948 lineno = line;
9949 input_filename = file;
9951 if (at_eof && !pattern_defined
9952 && DECL_EXPLICIT_INSTANTIATION (d))
9953 /* [temp.explicit]
9955 The definition of a non-exported function template, a
9956 non-exported member function template, or a non-exported
9957 member function or static data member of a class template
9958 shall be present in every translation unit in which it is
9959 explicitly instantiated. */
9960 pedwarn
9961 ("explicit instantiation of `%D' but no definition available", d);
9963 add_pending_template (d);
9964 goto out;
9967 need_push = !global_bindings_p ();
9968 if (need_push)
9969 push_to_top_level ();
9971 /* We're now committed to instantiating this template. Mark it as
9972 instantiated so that recursive calls to instantiate_decl do not
9973 try to instantiate it again. */
9974 DECL_TEMPLATE_INSTANTIATED (d) = 1;
9976 /* Regenerate the declaration in case the template has been modified
9977 by a subsequent redeclaration. */
9978 regenerate_decl_from_template (d, td);
9980 /* We already set the file and line above. Reset them now in case
9981 they changed as a result of calling regenerate_decl_from_template. */
9982 lineno = DECL_SOURCE_LINE (d);
9983 input_filename = DECL_SOURCE_FILE (d);
9985 if (TREE_CODE (d) == VAR_DECL)
9987 DECL_IN_AGGR_P (d) = 0;
9988 if (DECL_INTERFACE_KNOWN (d))
9989 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
9990 else
9992 DECL_EXTERNAL (d) = 1;
9993 DECL_NOT_REALLY_EXTERN (d) = 1;
9995 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
9997 else if (TREE_CODE (d) == FUNCTION_DECL)
9999 htab_t saved_local_specializations;
10001 /* Save away the current list, in case we are instantiating one
10002 template from within the body of another. */
10003 saved_local_specializations = local_specializations;
10005 /* Set up the list of local specializations. */
10006 local_specializations = htab_create (37,
10007 htab_hash_pointer,
10008 htab_eq_pointer,
10009 NULL);
10011 /* Set up context. */
10012 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10014 /* Substitute into the body of the function. */
10015 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10016 tf_error | tf_warning, tmpl);
10018 /* We don't need the local specializations any more. */
10019 htab_delete (local_specializations);
10020 local_specializations = saved_local_specializations;
10022 /* Finish the function. */
10023 expand_body (finish_function (0));
10026 /* We're not deferring instantiation any more. */
10027 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10029 if (need_push)
10030 pop_from_top_level ();
10032 out:
10033 lineno = line;
10034 input_filename = file;
10036 pop_tinst_level ();
10038 timevar_pop (TV_PARSE);
10040 return d;
10043 /* Run through the list of templates that we wish we could
10044 instantiate, and instantiate any we can. */
10047 instantiate_pending_templates ()
10049 tree *t;
10050 tree last = NULL_TREE;
10051 int instantiated_something = 0;
10052 int reconsider;
10056 reconsider = 0;
10058 t = &pending_templates;
10059 while (*t)
10061 tree instantiation = TREE_VALUE (*t);
10063 reopen_tinst_level (TREE_PURPOSE (*t));
10065 if (TYPE_P (instantiation))
10067 tree fn;
10069 if (!COMPLETE_TYPE_P (instantiation))
10071 instantiate_class_template (instantiation);
10072 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10073 for (fn = TYPE_METHODS (instantiation);
10075 fn = TREE_CHAIN (fn))
10076 if (! DECL_ARTIFICIAL (fn))
10077 instantiate_decl (fn, /*defer_ok=*/0);
10078 if (COMPLETE_TYPE_P (instantiation))
10080 instantiated_something = 1;
10081 reconsider = 1;
10085 if (COMPLETE_TYPE_P (instantiation))
10086 /* If INSTANTIATION has been instantiated, then we don't
10087 need to consider it again in the future. */
10088 *t = TREE_CHAIN (*t);
10089 else
10091 last = *t;
10092 t = &TREE_CHAIN (*t);
10095 else
10097 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10098 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10100 instantiation = instantiate_decl (instantiation,
10101 /*defer_ok=*/0);
10102 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10104 instantiated_something = 1;
10105 reconsider = 1;
10109 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10110 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10111 /* If INSTANTIATION has been instantiated, then we don't
10112 need to consider it again in the future. */
10113 *t = TREE_CHAIN (*t);
10114 else
10116 last = *t;
10117 t = &TREE_CHAIN (*t);
10120 tinst_depth = 0;
10121 current_tinst_level = NULL_TREE;
10123 last_pending_template = last;
10125 while (reconsider);
10127 return instantiated_something;
10130 /* Substitute ARGVEC into T, which is a list of initializers for
10131 either base class or a non-static data member. The TREE_PURPOSEs
10132 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10133 instantiate_decl. */
10135 static tree
10136 tsubst_initializer_list (t, argvec)
10137 tree t, argvec;
10139 tree first = NULL_TREE;
10140 tree *p = &first;
10142 for (; t; t = TREE_CHAIN (t))
10144 tree decl;
10145 tree init;
10146 tree val;
10148 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
10149 NULL_TREE);
10150 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
10151 NULL_TREE);
10153 if (!init)
10155 else if (TREE_CODE (init) == TREE_LIST)
10156 for (val = init; val; val = TREE_CHAIN (val))
10157 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
10158 else
10159 init = convert_from_reference (init);
10161 *p = build_tree_list (decl, init);
10162 p = &TREE_CHAIN (*p);
10164 return first;
10167 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10169 static void
10170 set_current_access_from_decl (decl)
10171 tree decl;
10173 if (TREE_PRIVATE (decl))
10174 current_access_specifier = access_private_node;
10175 else if (TREE_PROTECTED (decl))
10176 current_access_specifier = access_protected_node;
10177 else
10178 current_access_specifier = access_public_node;
10181 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10182 is the instantiation (which should have been created with
10183 start_enum) and ARGS are the template arguments to use. */
10185 static void
10186 tsubst_enum (tag, newtag, args)
10187 tree tag;
10188 tree newtag;
10189 tree args;
10191 tree e;
10193 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
10195 tree value;
10197 /* Note that in a template enum, the TREE_VALUE is the
10198 CONST_DECL, not the corresponding INTEGER_CST. */
10199 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
10200 args, tf_error | tf_warning,
10201 NULL_TREE);
10203 /* Give this enumeration constant the correct access. */
10204 set_current_access_from_decl (TREE_VALUE (e));
10206 /* Actually build the enumerator itself. */
10207 build_enumerator (TREE_PURPOSE (e), value, newtag);
10210 finish_enum (newtag);
10211 DECL_SOURCE_LINE (TYPE_NAME (newtag)) = DECL_SOURCE_LINE (TYPE_NAME (tag));
10212 DECL_SOURCE_FILE (TYPE_NAME (newtag)) = DECL_SOURCE_FILE (TYPE_NAME (tag));
10215 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10216 its type -- but without substituting the innermost set of template
10217 arguments. So, innermost set of template parameters will appear in
10218 the type. If CONTEXTP is non-NULL, then the partially substituted
10219 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10220 will be filled in with the substituted template parameters, if it
10221 is non-NULL. */
10223 tree
10224 get_mostly_instantiated_function_type (decl, contextp, tparmsp)
10225 tree decl;
10226 tree *contextp;
10227 tree *tparmsp;
10229 tree context = NULL_TREE;
10230 tree fn_type;
10231 tree tmpl;
10232 tree targs;
10233 tree tparms;
10234 int parm_depth;
10236 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10237 targs = DECL_TI_ARGS (decl);
10238 tparms = DECL_TEMPLATE_PARMS (tmpl);
10239 parm_depth = TMPL_PARMS_DEPTH (tparms);
10241 /* There should be as many levels of arguments as there are levels
10242 of parameters. */
10243 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10245 fn_type = TREE_TYPE (tmpl);
10246 if (DECL_STATIC_FUNCTION_P (decl))
10247 context = DECL_CONTEXT (decl);
10249 if (parm_depth == 1)
10250 /* No substitution is necessary. */
10252 else
10254 int i;
10255 tree partial_args;
10257 /* Replace the innermost level of the TARGS with NULL_TREEs to
10258 let tsubst know not to substitute for those parameters. */
10259 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
10260 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10261 SET_TMPL_ARGS_LEVEL (partial_args, i,
10262 TMPL_ARGS_LEVEL (targs, i));
10263 SET_TMPL_ARGS_LEVEL (partial_args,
10264 TMPL_ARGS_DEPTH (targs),
10265 make_tree_vec (DECL_NTPARMS (tmpl)));
10267 /* Now, do the (partial) substitution to figure out the
10268 appropriate function type. */
10269 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
10270 if (DECL_STATIC_FUNCTION_P (decl))
10271 context = tsubst (context, partial_args, tf_error, NULL_TREE);
10273 /* Substitute into the template parameters to obtain the real
10274 innermost set of parameters. This step is important if the
10275 innermost set of template parameters contains value
10276 parameters whose types depend on outer template parameters. */
10277 TREE_VEC_LENGTH (partial_args)--;
10278 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
10281 if (contextp)
10282 *contextp = context;
10283 if (tparmsp)
10284 *tparmsp = tparms;
10286 return fn_type;
10289 /* Return truthvalue if we're processing a template different from
10290 the last one involved in diagnostics. */
10292 problematic_instantiation_changed ()
10294 return last_template_error_tick != tinst_level_tick;
10297 /* Remember current template involved in diagnostics. */
10298 void
10299 record_last_problematic_instantiation ()
10301 last_template_error_tick = tinst_level_tick;
10304 tree
10305 current_instantiation ()
10307 return current_tinst_level;
10310 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10311 type. Return zero for ok, non-zero for disallowed. Issue error and
10312 warning messages under control of COMPLAIN. */
10314 static int
10315 invalid_nontype_parm_type_p (type, complain)
10316 tree type;
10317 tsubst_flags_t complain;
10319 if (INTEGRAL_TYPE_P (type))
10320 return 0;
10321 else if (POINTER_TYPE_P (type))
10322 return 0;
10323 else if (TYPE_PTRMEM_P (type))
10324 return 0;
10325 else if (TYPE_PTRMEMFUNC_P (type))
10326 return 0;
10327 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10328 return 0;
10329 else if (TREE_CODE (type) == TYPENAME_TYPE)
10330 return 0;
10332 if (complain & tf_error)
10333 error ("`%#T' is not a valid type for a template constant parameter",
10334 type);
10335 return 1;