PR c++/7754
[official-gcc.git] / gcc / cp / pt.c
blob8fc9f743e1e033bc41d03b2da24900bf36a106cb
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 int can_complete_type_without_circularity PARAMS ((tree));
138 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
139 static int template_decl_level PARAMS ((tree));
140 static tree maybe_get_template_decl_from_type_decl PARAMS ((tree));
141 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
142 static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t));
143 static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t));
144 static void regenerate_decl_from_template PARAMS ((tree, tree));
145 static tree most_specialized PARAMS ((tree, tree, tree));
146 static tree most_specialized_class PARAMS ((tree, tree));
147 static int template_class_depth_real PARAMS ((tree, int));
148 static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int));
149 static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t));
150 static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree));
151 static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree));
152 static void check_specialization_scope PARAMS ((void));
153 static tree process_partial_specialization PARAMS ((tree));
154 static void set_current_access_from_decl PARAMS ((tree));
155 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
156 static tree tsubst_call_declarator_parms PARAMS ((tree, tree,
157 tsubst_flags_t, tree));
158 static tree get_template_base_recursive PARAMS ((tree, tree,
159 tree, tree, tree, int));
160 static tree get_template_base PARAMS ((tree, tree, tree, tree));
161 static int verify_class_unification PARAMS ((tree, tree, tree));
162 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
163 static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t,
164 tree, tree));
165 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
166 static int template_args_equal PARAMS ((tree, tree));
167 static void tsubst_default_arguments PARAMS ((tree));
168 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
169 static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
170 static void copy_default_args_to_explicit_spec PARAMS ((tree));
171 static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t));
173 /* Called once to initialize pt.c. */
175 void
176 init_pt ()
178 ggc_add_tree_root (&pending_templates, 1);
179 ggc_add_tree_root (&saved_trees, 1);
180 ggc_add_tree_root (&current_tinst_level, 1);
183 /* Do any processing required when DECL (a member template declaration
184 using TEMPLATE_PARAMETERS as its innermost parameter list) is
185 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
186 it is a specialization, in which case the DECL itself is returned. */
188 tree
189 finish_member_template_decl (decl)
190 tree decl;
192 if (decl == NULL_TREE || decl == void_type_node)
193 return NULL_TREE;
194 else if (decl == error_mark_node)
195 /* By returning NULL_TREE, the parser will just ignore this
196 declaration. We have already issued the error. */
197 return NULL_TREE;
198 else if (TREE_CODE (decl) == TREE_LIST)
200 /* Assume that the class is the only declspec. */
201 decl = TREE_VALUE (decl);
202 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
203 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
205 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
206 check_member_template (tmpl);
207 return tmpl;
209 return NULL_TREE;
211 else if (TREE_CODE (decl) == FIELD_DECL)
212 error ("data member `%D' cannot be a member template", decl);
213 else if (DECL_TEMPLATE_INFO (decl))
215 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
217 check_member_template (DECL_TI_TEMPLATE (decl));
218 return DECL_TI_TEMPLATE (decl);
220 else
221 return decl;
223 else
224 error ("invalid member template declaration `%D'", decl);
226 return error_mark_node;
229 /* Returns the template nesting level of the indicated class TYPE.
231 For example, in:
232 template <class T>
233 struct A
235 template <class U>
236 struct B {};
239 A<T>::B<U> has depth two, while A<T> has depth one.
240 Both A<T>::B<int> and A<int>::B<U> have depth one, if
241 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
242 specializations.
244 This function is guaranteed to return 0 if passed NULL_TREE so
245 that, for example, `template_class_depth (current_class_type)' is
246 always safe. */
248 static int
249 template_class_depth_real (type, count_specializations)
250 tree type;
251 int count_specializations;
253 int depth;
255 for (depth = 0;
256 type && TREE_CODE (type) != NAMESPACE_DECL;
257 type = (TREE_CODE (type) == FUNCTION_DECL)
258 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
260 if (TREE_CODE (type) != FUNCTION_DECL)
262 if (CLASSTYPE_TEMPLATE_INFO (type)
263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
264 && ((count_specializations
265 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
266 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
267 ++depth;
269 else
271 if (DECL_TEMPLATE_INFO (type)
272 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
273 && ((count_specializations
274 && DECL_TEMPLATE_SPECIALIZATION (type))
275 || uses_template_parms (DECL_TI_ARGS (type))))
276 ++depth;
280 return depth;
283 /* Returns the template nesting level of the indicated class TYPE.
284 Like template_class_depth_real, but instantiations do not count in
285 the depth. */
287 int
288 template_class_depth (type)
289 tree type;
291 return template_class_depth_real (type, /*count_specializations=*/0);
294 /* Returns 1 if processing DECL as part of do_pending_inlines
295 needs us to push template parms. */
297 static int
298 inline_needs_template_parms (decl)
299 tree decl;
301 if (! DECL_TEMPLATE_INFO (decl))
302 return 0;
304 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
305 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
308 /* Subroutine of maybe_begin_member_template_processing.
309 Push the template parms in PARMS, starting from LEVELS steps into the
310 chain, and ending at the beginning, since template parms are listed
311 innermost first. */
313 static void
314 push_inline_template_parms_recursive (parmlist, levels)
315 tree parmlist;
316 int levels;
318 tree parms = TREE_VALUE (parmlist);
319 int i;
321 if (levels > 1)
322 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
324 ++processing_template_decl;
325 current_template_parms
326 = tree_cons (size_int (processing_template_decl),
327 parms, current_template_parms);
328 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
330 pushlevel (0);
331 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
333 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
334 my_friendly_assert (DECL_P (parm), 0);
336 switch (TREE_CODE (parm))
338 case TYPE_DECL:
339 case TEMPLATE_DECL:
340 pushdecl (parm);
341 break;
343 case PARM_DECL:
345 /* Make a CONST_DECL as is done in process_template_parm.
346 It is ugly that we recreate this here; the original
347 version built in process_template_parm is no longer
348 available. */
349 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
350 TREE_TYPE (parm));
351 DECL_ARTIFICIAL (decl) = 1;
352 DECL_INITIAL (decl) = DECL_INITIAL (parm);
353 SET_DECL_TEMPLATE_PARM_P (decl);
354 pushdecl (decl);
356 break;
358 default:
359 abort ();
364 /* Restore the template parameter context for a member template or
365 a friend template defined in a class definition. */
367 void
368 maybe_begin_member_template_processing (decl)
369 tree decl;
371 tree parms;
372 int levels = 0;
374 if (inline_needs_template_parms (decl))
376 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
377 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
379 if (DECL_TEMPLATE_SPECIALIZATION (decl))
381 --levels;
382 parms = TREE_CHAIN (parms);
385 push_inline_template_parms_recursive (parms, levels);
388 /* Remember how many levels of template parameters we pushed so that
389 we can pop them later. */
390 if (!inline_parm_levels)
391 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
392 if (inline_parm_levels_used == inline_parm_levels->num_elements)
393 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
394 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
395 ++inline_parm_levels_used;
398 /* Undo the effects of begin_member_template_processing. */
400 void
401 maybe_end_member_template_processing ()
403 int i;
405 if (!inline_parm_levels_used)
406 return;
408 --inline_parm_levels_used;
409 for (i = 0;
410 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
411 ++i)
413 --processing_template_decl;
414 current_template_parms = TREE_CHAIN (current_template_parms);
415 poplevel (0, 0, 0);
419 /* Returns non-zero iff T is a member template function. We must be
420 careful as in
422 template <class T> class C { void f(); }
424 Here, f is a template function, and a member, but not a member
425 template. This function does not concern itself with the origin of
426 T, only its present state. So if we have
428 template <class T> class C { template <class U> void f(U); }
430 then neither C<int>::f<char> nor C<T>::f<double> is considered
431 to be a member template. But, `template <class U> void
432 C<int>::f(U)' is considered a member template. */
435 is_member_template (t)
436 tree t;
438 if (!DECL_FUNCTION_TEMPLATE_P (t))
439 /* Anything that isn't a function or a template function is
440 certainly not a member template. */
441 return 0;
443 /* A local class can't have member templates. */
444 if (decl_function_context (t))
445 return 0;
447 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
448 /* If there are more levels of template parameters than
449 there are template classes surrounding the declaration,
450 then we have a member template. */
451 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
452 template_class_depth (DECL_CONTEXT (t))));
455 #if 0 /* UNUSED */
456 /* Returns non-zero iff T is a member template class. See
457 is_member_template for a description of what precisely constitutes
458 a member template. */
461 is_member_template_class (t)
462 tree t;
464 if (!DECL_CLASS_TEMPLATE_P (t))
465 /* Anything that isn't a class template, is certainly not a member
466 template. */
467 return 0;
469 if (!DECL_CLASS_SCOPE_P (t))
470 /* Anything whose context isn't a class type is surely not a
471 member template. */
472 return 0;
474 /* If there are more levels of template parameters than there are
475 template classes surrounding the declaration, then we have a
476 member template. */
477 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
478 template_class_depth (DECL_CONTEXT (t)));
480 #endif
482 /* Return a new template argument vector which contains all of ARGS,
483 but has as its innermost set of arguments the EXTRA_ARGS. */
485 static tree
486 add_to_template_args (args, extra_args)
487 tree args;
488 tree extra_args;
490 tree new_args;
491 int extra_depth;
492 int i;
493 int j;
495 extra_depth = TMPL_ARGS_DEPTH (extra_args);
496 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
498 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
499 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
501 for (j = 1; j <= extra_depth; ++j, ++i)
502 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
504 return new_args;
507 /* Like add_to_template_args, but only the outermost ARGS are added to
508 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
509 (EXTRA_ARGS) levels are added. This function is used to combine
510 the template arguments from a partial instantiation with the
511 template arguments used to attain the full instantiation from the
512 partial instantiation. */
514 static tree
515 add_outermost_template_args (args, extra_args)
516 tree args;
517 tree extra_args;
519 tree new_args;
521 /* If there are more levels of EXTRA_ARGS than there are ARGS,
522 something very fishy is going on. */
523 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
526 /* If *all* the new arguments will be the EXTRA_ARGS, just return
527 them. */
528 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
529 return extra_args;
531 /* For the moment, we make ARGS look like it contains fewer levels. */
532 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
534 new_args = add_to_template_args (args, extra_args);
536 /* Now, we restore ARGS to its full dimensions. */
537 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
539 return new_args;
542 /* Return the N levels of innermost template arguments from the ARGS. */
544 tree
545 get_innermost_template_args (args, n)
546 tree args;
547 int n;
549 tree new_args;
550 int extra_levels;
551 int i;
553 my_friendly_assert (n >= 0, 20000603);
555 /* If N is 1, just return the innermost set of template arguments. */
556 if (n == 1)
557 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
559 /* If we're not removing anything, just return the arguments we were
560 given. */
561 extra_levels = TMPL_ARGS_DEPTH (args) - n;
562 my_friendly_assert (extra_levels >= 0, 20000603);
563 if (extra_levels == 0)
564 return args;
566 /* Make a new set of arguments, not containing the outer arguments. */
567 new_args = make_tree_vec (n);
568 for (i = 1; i <= n; ++i)
569 SET_TMPL_ARGS_LEVEL (new_args, i,
570 TMPL_ARGS_LEVEL (args, i + extra_levels));
572 return new_args;
575 /* We've got a template header coming up; push to a new level for storing
576 the parms. */
578 void
579 begin_template_parm_list ()
581 /* We use a non-tag-transparent scope here, which causes pushtag to
582 put tags in this scope, rather than in the enclosing class or
583 namespace scope. This is the right thing, since we want
584 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
585 global template class, push_template_decl handles putting the
586 TEMPLATE_DECL into top-level scope. For a nested template class,
587 e.g.:
589 template <class T> struct S1 {
590 template <class T> struct S2 {};
593 pushtag contains special code to call pushdecl_with_scope on the
594 TEMPLATE_DECL for S2. */
595 begin_scope (sk_template_parms);
596 ++processing_template_decl;
597 ++processing_template_parmlist;
598 note_template_header (0);
601 /* This routine is called when a specialization is declared. If it is
602 illegal to declare a specialization here, an error is reported. */
604 static void
605 check_specialization_scope ()
607 tree scope = current_scope ();
609 /* [temp.expl.spec]
611 An explicit specialization shall be declared in the namespace of
612 which the template is a member, or, for member templates, in the
613 namespace of which the enclosing class or enclosing class
614 template is a member. An explicit specialization of a member
615 function, member class or static data member of a class template
616 shall be declared in the namespace of which the class template
617 is a member. */
618 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
619 error ("explicit specialization in non-namespace scope `%D'",
620 scope);
622 /* [temp.expl.spec]
624 In an explicit specialization declaration for a member of a class
625 template or a member template that appears in namespace scope,
626 the member template and some of its enclosing class templates may
627 remain unspecialized, except that the declaration shall not
628 explicitly specialize a class member template if its enclosing
629 class templates are not explicitly specialized as well. */
630 if (current_template_parms)
631 error ("enclosing class templates are not explicitly specialized");
634 /* We've just seen template <>. */
636 void
637 begin_specialization ()
639 begin_scope (sk_template_spec);
640 note_template_header (1);
641 check_specialization_scope ();
644 /* Called at then end of processing a declaration preceded by
645 template<>. */
647 void
648 end_specialization ()
650 finish_scope ();
651 reset_specialization ();
654 /* Any template <>'s that we have seen thus far are not referring to a
655 function specialization. */
657 void
658 reset_specialization ()
660 processing_specialization = 0;
661 template_header_count = 0;
664 /* We've just seen a template header. If SPECIALIZATION is non-zero,
665 it was of the form template <>. */
667 static void
668 note_template_header (specialization)
669 int specialization;
671 processing_specialization = specialization;
672 template_header_count++;
675 /* We're beginning an explicit instantiation. */
677 void
678 begin_explicit_instantiation ()
680 ++processing_explicit_instantiation;
684 void
685 end_explicit_instantiation ()
687 my_friendly_assert(processing_explicit_instantiation > 0, 0);
688 --processing_explicit_instantiation;
691 /* The TYPE is being declared. If it is a template type, that means it
692 is a partial specialization. Do appropriate error-checking. */
694 void
695 maybe_process_partial_specialization (type)
696 tree type;
698 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
700 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
701 && !COMPLETE_TYPE_P (type))
703 if (current_namespace
704 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
706 pedwarn ("specializing `%#T' in different namespace", type);
707 cp_pedwarn_at (" from definition of `%#D'",
708 CLASSTYPE_TI_TEMPLATE (type));
710 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
711 if (processing_template_decl)
712 push_template_decl (TYPE_MAIN_DECL (type));
714 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
715 error ("specialization of `%T' after instantiation", type);
717 else if (processing_specialization)
718 error ("explicit specialization of non-template `%T'", type);
721 /* Retrieve the specialization (in the sense of [temp.spec] - a
722 specialization is either an instantiation or an explicit
723 specialization) of TMPL for the given template ARGS. If there is
724 no such specialization, return NULL_TREE. The ARGS are a vector of
725 arguments, or a vector of vectors of arguments, in the case of
726 templates with more than one level of parameters. */
728 static tree
729 retrieve_specialization (tmpl, args)
730 tree tmpl;
731 tree args;
733 tree s;
735 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
737 /* There should be as many levels of arguments as there are
738 levels of parameters. */
739 my_friendly_assert (TMPL_ARGS_DEPTH (args)
740 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
743 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
744 s != NULL_TREE;
745 s = TREE_CHAIN (s))
746 if (comp_template_args (TREE_PURPOSE (s), args))
747 return TREE_VALUE (s);
749 return NULL_TREE;
752 /* Like retrieve_specialization, but for local declarations. */
754 static tree
755 retrieve_local_specialization (tmpl)
756 tree tmpl;
758 return (tree) htab_find (local_specializations, tmpl);
761 /* Returns non-zero iff DECL is a specialization of TMPL. */
764 is_specialization_of (decl, tmpl)
765 tree decl;
766 tree tmpl;
768 tree t;
770 if (TREE_CODE (decl) == FUNCTION_DECL)
772 for (t = decl;
773 t != NULL_TREE;
774 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
775 if (t == tmpl)
776 return 1;
778 else
780 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
782 for (t = TREE_TYPE (decl);
783 t != NULL_TREE;
784 t = CLASSTYPE_USE_TEMPLATE (t)
785 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
786 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
787 return 1;
790 return 0;
793 /* Register the specialization SPEC as a specialization of TMPL with
794 the indicated ARGS. Returns SPEC, or an equivalent prior
795 declaration, if available. */
797 static tree
798 register_specialization (spec, tmpl, args)
799 tree spec;
800 tree tmpl;
801 tree args;
803 tree s;
805 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
807 if (TREE_CODE (spec) == FUNCTION_DECL
808 && uses_template_parms (DECL_TI_ARGS (spec)))
809 /* This is the FUNCTION_DECL for a partial instantiation. Don't
810 register it; we want the corresponding TEMPLATE_DECL instead.
811 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
812 the more obvious `uses_template_parms (spec)' to avoid problems
813 with default function arguments. In particular, given
814 something like this:
816 template <class T> void f(T t1, T t = T())
818 the default argument expression is not substituted for in an
819 instantiation unless and until it is actually needed. */
820 return spec;
822 /* There should be as many levels of arguments as there are
823 levels of parameters. */
824 my_friendly_assert (TMPL_ARGS_DEPTH (args)
825 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
828 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
829 s != NULL_TREE;
830 s = TREE_CHAIN (s))
832 tree fn = TREE_VALUE (s);
834 /* We can sometimes try to re-register a specialization that we've
835 already got. In particular, regenerate_decl_from_template
836 calls duplicate_decls which will update the specialization
837 list. But, we'll still get called again here anyhow. It's
838 more convenient to simply allow this than to try to prevent it. */
839 if (fn == spec)
840 return spec;
841 else if (comp_template_args (TREE_PURPOSE (s), args))
843 if (DECL_TEMPLATE_SPECIALIZATION (spec))
845 if (DECL_TEMPLATE_INSTANTIATION (fn))
847 if (TREE_USED (fn)
848 || DECL_EXPLICIT_INSTANTIATION (fn))
850 error ("specialization of %D after instantiation",
851 fn);
852 return spec;
854 else
856 /* This situation should occur only if the first
857 specialization is an implicit instantiation,
858 the second is an explicit specialization, and
859 the implicit instantiation has not yet been
860 used. That situation can occur if we have
861 implicitly instantiated a member function and
862 then specialized it later.
864 We can also wind up here if a friend
865 declaration that looked like an instantiation
866 turns out to be a specialization:
868 template <class T> void foo(T);
869 class S { friend void foo<>(int) };
870 template <> void foo(int);
872 We transform the existing DECL in place so that
873 any pointers to it become pointers to the
874 updated declaration.
876 If there was a definition for the template, but
877 not for the specialization, we want this to
878 look as if there is no definition, and vice
879 versa. */
880 DECL_INITIAL (fn) = NULL_TREE;
881 duplicate_decls (spec, fn);
883 return fn;
886 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
888 duplicate_decls (spec, fn);
889 return fn;
895 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
896 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
898 return spec;
901 /* Unregister the specialization SPEC as a specialization of TMPL.
902 Returns nonzero if the SPEC was listed as a specialization of
903 TMPL. */
905 static int
906 unregister_specialization (spec, tmpl)
907 tree spec;
908 tree tmpl;
910 tree* s;
912 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
913 *s != NULL_TREE;
914 s = &TREE_CHAIN (*s))
915 if (TREE_VALUE (*s) == spec)
917 *s = TREE_CHAIN (*s);
918 return 1;
921 return 0;
924 /* Like register_specialization, but for local declarations. We are
925 registering SPEC, an instantiation of TMPL. */
927 static void
928 register_local_specialization (spec, tmpl)
929 tree spec;
930 tree tmpl;
932 void **slot;
934 slot = htab_find_slot (local_specializations, tmpl, INSERT);
935 *slot = spec;
938 /* Print the list of candidate FNS in an error message. */
940 void
941 print_candidates (fns)
942 tree fns;
944 tree fn;
946 const char *str = "candidates are:";
948 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
950 tree f;
952 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
953 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
954 str = " ";
958 /* Returns the template (one of the functions given by TEMPLATE_ID)
959 which can be specialized to match the indicated DECL with the
960 explicit template args given in TEMPLATE_ID. The DECL may be
961 NULL_TREE if none is available. In that case, the functions in
962 TEMPLATE_ID are non-members.
964 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
965 specialization of a member template.
967 The template args (those explicitly specified and those deduced)
968 are output in a newly created vector *TARGS_OUT.
970 If it is impossible to determine the result, an error message is
971 issued. The error_mark_node is returned to indicate failure. */
973 static tree
974 determine_specialization (template_id, decl, targs_out,
975 need_member_template)
976 tree template_id;
977 tree decl;
978 tree* targs_out;
979 int need_member_template;
981 tree fns;
982 tree targs;
983 tree explicit_targs;
984 tree candidates = NULL_TREE;
985 tree templates = NULL_TREE;
987 *targs_out = NULL_TREE;
989 if (template_id == error_mark_node)
990 return error_mark_node;
992 fns = TREE_OPERAND (template_id, 0);
993 explicit_targs = TREE_OPERAND (template_id, 1);
995 if (fns == error_mark_node)
996 return error_mark_node;
998 /* Check for baselinks. */
999 if (BASELINK_P (fns))
1000 fns = TREE_VALUE (fns);
1002 if (!is_overloaded_fn (fns))
1004 error ("`%D' is not a function template", fns);
1005 return error_mark_node;
1008 for (; fns; fns = OVL_NEXT (fns))
1010 tree tmpl;
1012 tree fn = OVL_CURRENT (fns);
1014 if (TREE_CODE (fn) == TEMPLATE_DECL)
1015 /* DECL might be a specialization of FN. */
1016 tmpl = fn;
1017 else if (need_member_template)
1018 /* FN is an ordinary member function, and we need a
1019 specialization of a member template. */
1020 continue;
1021 else if (TREE_CODE (fn) != FUNCTION_DECL)
1022 /* We can get IDENTIFIER_NODEs here in certain erroneous
1023 cases. */
1024 continue;
1025 else if (!DECL_FUNCTION_MEMBER_P (fn))
1026 /* This is just an ordinary non-member function. Nothing can
1027 be a specialization of that. */
1028 continue;
1029 else if (DECL_ARTIFICIAL (fn))
1030 /* Cannot specialize functions that are created implicitly. */
1031 continue;
1032 else
1034 tree decl_arg_types;
1036 /* This is an ordinary member function. However, since
1037 we're here, we can assume it's enclosing class is a
1038 template class. For example,
1040 template <typename T> struct S { void f(); };
1041 template <> void S<int>::f() {}
1043 Here, S<int>::f is a non-template, but S<int> is a
1044 template class. If FN has the same type as DECL, we
1045 might be in business. */
1047 if (!DECL_TEMPLATE_INFO (fn))
1048 /* Its enclosing class is an explicit specialization
1049 of a template class. This is not a candidate. */
1050 continue;
1052 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1053 TREE_TYPE (TREE_TYPE (fn))))
1054 /* The return types differ. */
1055 continue;
1057 /* Adjust the type of DECL in case FN is a static member. */
1058 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1059 if (DECL_STATIC_FUNCTION_P (fn)
1060 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1061 decl_arg_types = TREE_CHAIN (decl_arg_types);
1063 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1064 decl_arg_types))
1065 /* They match! */
1066 candidates = tree_cons (NULL_TREE, fn, candidates);
1068 continue;
1071 /* See whether this function might be a specialization of this
1072 template. */
1073 targs = get_bindings (tmpl, decl, explicit_targs);
1075 if (!targs)
1076 /* We cannot deduce template arguments that when used to
1077 specialize TMPL will produce DECL. */
1078 continue;
1080 /* Save this template, and the arguments deduced. */
1081 templates = tree_cons (targs, tmpl, templates);
1084 if (templates && TREE_CHAIN (templates))
1086 /* We have:
1088 [temp.expl.spec]
1090 It is possible for a specialization with a given function
1091 signature to be instantiated from more than one function
1092 template. In such cases, explicit specification of the
1093 template arguments must be used to uniquely identify the
1094 function template specialization being specialized.
1096 Note that here, there's no suggestion that we're supposed to
1097 determine which of the candidate templates is most
1098 specialized. However, we, also have:
1100 [temp.func.order]
1102 Partial ordering of overloaded function template
1103 declarations is used in the following contexts to select
1104 the function template to which a function template
1105 specialization refers:
1107 -- when an explicit specialization refers to a function
1108 template.
1110 So, we do use the partial ordering rules, at least for now.
1111 This extension can only serve to make illegal programs legal,
1112 so it's safe. And, there is strong anecdotal evidence that
1113 the committee intended the partial ordering rules to apply;
1114 the EDG front-end has that behavior, and John Spicer claims
1115 that the committee simply forgot to delete the wording in
1116 [temp.expl.spec]. */
1117 tree tmpl = most_specialized (templates, decl, explicit_targs);
1118 if (tmpl && tmpl != error_mark_node)
1120 targs = get_bindings (tmpl, decl, explicit_targs);
1121 templates = tree_cons (targs, tmpl, NULL_TREE);
1125 if (templates == NULL_TREE && candidates == NULL_TREE)
1127 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1128 template_id, decl);
1129 return error_mark_node;
1131 else if ((templates && TREE_CHAIN (templates))
1132 || (candidates && TREE_CHAIN (candidates))
1133 || (templates && candidates))
1135 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1136 template_id, decl);
1137 chainon (candidates, templates);
1138 print_candidates (candidates);
1139 return error_mark_node;
1142 /* We have one, and exactly one, match. */
1143 if (candidates)
1145 /* It was a specialization of an ordinary member function in a
1146 template class. */
1147 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1148 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1151 /* It was a specialization of a template. */
1152 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1153 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1155 *targs_out = copy_node (targs);
1156 SET_TMPL_ARGS_LEVEL (*targs_out,
1157 TMPL_ARGS_DEPTH (*targs_out),
1158 TREE_PURPOSE (templates));
1160 else
1161 *targs_out = TREE_PURPOSE (templates);
1162 return TREE_VALUE (templates);
1165 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1166 but with the default argument values filled in from those in the
1167 TMPL_TYPES. */
1169 static tree
1170 copy_default_args_to_explicit_spec_1 (spec_types,
1171 tmpl_types)
1172 tree spec_types;
1173 tree tmpl_types;
1175 tree new_spec_types;
1177 if (!spec_types)
1178 return NULL_TREE;
1180 if (spec_types == void_list_node)
1181 return void_list_node;
1183 /* Substitute into the rest of the list. */
1184 new_spec_types =
1185 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1186 TREE_CHAIN (tmpl_types));
1188 /* Add the default argument for this parameter. */
1189 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1190 TREE_VALUE (spec_types),
1191 new_spec_types);
1194 /* DECL is an explicit specialization. Replicate default arguments
1195 from the template it specializes. (That way, code like:
1197 template <class T> void f(T = 3);
1198 template <> void f(double);
1199 void g () { f (); }
1201 works, as required.) An alternative approach would be to look up
1202 the correct default arguments at the call-site, but this approach
1203 is consistent with how implicit instantiations are handled. */
1205 static void
1206 copy_default_args_to_explicit_spec (decl)
1207 tree decl;
1209 tree tmpl;
1210 tree spec_types;
1211 tree tmpl_types;
1212 tree new_spec_types;
1213 tree old_type;
1214 tree new_type;
1215 tree t;
1216 tree object_type = NULL_TREE;
1217 tree in_charge = NULL_TREE;
1218 tree vtt = NULL_TREE;
1220 /* See if there's anything we need to do. */
1221 tmpl = DECL_TI_TEMPLATE (decl);
1222 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1223 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1224 if (TREE_PURPOSE (t))
1225 break;
1226 if (!t)
1227 return;
1229 old_type = TREE_TYPE (decl);
1230 spec_types = TYPE_ARG_TYPES (old_type);
1232 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1234 /* Remove the this pointer, but remember the object's type for
1235 CV quals. */
1236 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1237 spec_types = TREE_CHAIN (spec_types);
1238 tmpl_types = TREE_CHAIN (tmpl_types);
1240 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1242 /* DECL may contain more parameters than TMPL due to the extra
1243 in-charge parameter in constructors and destructors. */
1244 in_charge = spec_types;
1245 spec_types = TREE_CHAIN (spec_types);
1247 if (DECL_HAS_VTT_PARM_P (decl))
1249 vtt = spec_types;
1250 spec_types = TREE_CHAIN (spec_types);
1254 /* Compute the merged default arguments. */
1255 new_spec_types =
1256 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1258 /* Compute the new FUNCTION_TYPE. */
1259 if (object_type)
1261 if (vtt)
1262 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1263 TREE_VALUE (vtt),
1264 new_spec_types);
1266 if (in_charge)
1267 /* Put the in-charge parameter back. */
1268 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1269 TREE_VALUE (in_charge),
1270 new_spec_types);
1272 new_type = build_cplus_method_type (object_type,
1273 TREE_TYPE (old_type),
1274 new_spec_types);
1276 else
1277 new_type = build_function_type (TREE_TYPE (old_type),
1278 new_spec_types);
1279 new_type = build_type_attribute_variant (new_type,
1280 TYPE_ATTRIBUTES (old_type));
1281 new_type = build_exception_variant (new_type,
1282 TYPE_RAISES_EXCEPTIONS (old_type));
1283 TREE_TYPE (decl) = new_type;
1286 /* Check to see if the function just declared, as indicated in
1287 DECLARATOR, and in DECL, is a specialization of a function
1288 template. We may also discover that the declaration is an explicit
1289 instantiation at this point.
1291 Returns DECL, or an equivalent declaration that should be used
1292 instead if all goes well. Issues an error message if something is
1293 amiss. Returns error_mark_node if the error is not easily
1294 recoverable.
1296 FLAGS is a bitmask consisting of the following flags:
1298 2: The function has a definition.
1299 4: The function is a friend.
1301 The TEMPLATE_COUNT is the number of references to qualifying
1302 template classes that appeared in the name of the function. For
1303 example, in
1305 template <class T> struct S { void f(); };
1306 void S<int>::f();
1308 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1309 classes are not counted in the TEMPLATE_COUNT, so that in
1311 template <class T> struct S {};
1312 template <> struct S<int> { void f(); }
1313 template <> void S<int>::f();
1315 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1316 illegal; there should be no template <>.)
1318 If the function is a specialization, it is marked as such via
1319 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1320 is set up correctly, and it is added to the list of specializations
1321 for that template. */
1323 tree
1324 check_explicit_specialization (declarator, decl, template_count, flags)
1325 tree declarator;
1326 tree decl;
1327 int template_count;
1328 int flags;
1330 int have_def = flags & 2;
1331 int is_friend = flags & 4;
1332 int specialization = 0;
1333 int explicit_instantiation = 0;
1334 int member_specialization = 0;
1335 tree ctype = DECL_CLASS_CONTEXT (decl);
1336 tree dname = DECL_NAME (decl);
1337 tmpl_spec_kind tsk;
1339 tsk = current_tmpl_spec_kind (template_count);
1341 switch (tsk)
1343 case tsk_none:
1344 if (processing_specialization)
1346 specialization = 1;
1347 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1349 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1351 if (is_friend)
1352 /* This could be something like:
1354 template <class T> void f(T);
1355 class S { friend void f<>(int); } */
1356 specialization = 1;
1357 else
1359 /* This case handles bogus declarations like template <>
1360 template <class T> void f<int>(); */
1362 error ("template-id `%D' in declaration of primary template",
1363 declarator);
1364 return decl;
1367 break;
1369 case tsk_invalid_member_spec:
1370 /* The error has already been reported in
1371 check_specialization_scope. */
1372 return error_mark_node;
1374 case tsk_invalid_expl_inst:
1375 error ("template parameter list used in explicit instantiation");
1377 /* Fall through. */
1379 case tsk_expl_inst:
1380 if (have_def)
1381 error ("definition provided for explicit instantiation");
1383 explicit_instantiation = 1;
1384 break;
1386 case tsk_excessive_parms:
1387 error ("too many template parameter lists in declaration of `%D'",
1388 decl);
1389 return error_mark_node;
1391 /* Fall through. */
1392 case tsk_expl_spec:
1393 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1394 if (ctype)
1395 member_specialization = 1;
1396 else
1397 specialization = 1;
1398 break;
1400 case tsk_insufficient_parms:
1401 if (template_header_count)
1403 error("too few template parameter lists in declaration of `%D'",
1404 decl);
1405 return decl;
1407 else if (ctype != NULL_TREE
1408 && !TYPE_BEING_DEFINED (ctype)
1409 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1410 && !is_friend)
1412 /* For backwards compatibility, we accept:
1414 template <class T> struct S { void f(); };
1415 void S<int>::f() {} // Missing template <>
1417 That used to be legal C++. */
1418 if (pedantic)
1419 pedwarn
1420 ("explicit specialization not preceded by `template <>'");
1421 specialization = 1;
1422 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1424 break;
1426 case tsk_template:
1427 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1429 /* This case handles bogus declarations like template <>
1430 template <class T> void f<int>(); */
1432 if (uses_template_parms (declarator))
1433 error ("partial specialization `%D' of function template",
1434 declarator);
1435 else
1436 error ("template-id `%D' in declaration of primary template",
1437 declarator);
1438 return decl;
1441 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1442 /* This is a specialization of a member template, without
1443 specialization the containing class. Something like:
1445 template <class T> struct S {
1446 template <class U> void f (U);
1448 template <> template <class U> void S<int>::f(U) {}
1450 That's a specialization -- but of the entire template. */
1451 specialization = 1;
1452 break;
1454 default:
1455 abort ();
1458 if (specialization || member_specialization)
1460 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1461 for (; t; t = TREE_CHAIN (t))
1462 if (TREE_PURPOSE (t))
1464 pedwarn
1465 ("default argument specified in explicit specialization");
1466 break;
1468 if (current_lang_name == lang_name_c)
1469 error ("template specialization with C linkage");
1472 if (specialization || member_specialization || explicit_instantiation)
1474 tree tmpl = NULL_TREE;
1475 tree targs = NULL_TREE;
1477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1478 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1480 tree fns;
1482 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1484 if (!ctype)
1485 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1486 else
1487 fns = dname;
1489 declarator =
1490 lookup_template_function (fns, NULL_TREE);
1493 if (declarator == error_mark_node)
1494 return error_mark_node;
1496 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1498 if (!explicit_instantiation)
1499 /* A specialization in class scope. This is illegal,
1500 but the error will already have been flagged by
1501 check_specialization_scope. */
1502 return error_mark_node;
1503 else
1505 /* It's not legal to write an explicit instantiation in
1506 class scope, e.g.:
1508 class C { template void f(); }
1510 This case is caught by the parser. However, on
1511 something like:
1513 template class C { void f(); };
1515 (which is illegal) we can get here. The error will be
1516 issued later. */
1520 return decl;
1522 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1524 /* A friend declaration. We can't do much, because we don't
1525 know what this resolves to, yet. */
1526 my_friendly_assert (is_friend != 0, 0);
1527 my_friendly_assert (!explicit_instantiation, 0);
1528 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1529 return decl;
1531 else if (ctype != NULL_TREE
1532 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1533 IDENTIFIER_NODE))
1535 /* Find the list of functions in ctype that have the same
1536 name as the declared function. */
1537 tree name = TREE_OPERAND (declarator, 0);
1538 tree fns = NULL_TREE;
1539 int idx;
1541 if (name == constructor_name (ctype)
1542 || name == constructor_name_full (ctype))
1544 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1546 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1547 : !TYPE_HAS_DESTRUCTOR (ctype))
1549 /* From [temp.expl.spec]:
1551 If such an explicit specialization for the member
1552 of a class template names an implicitly-declared
1553 special member function (clause _special_), the
1554 program is ill-formed.
1556 Similar language is found in [temp.explicit]. */
1557 error ("specialization of implicitly-declared special member function");
1558 return error_mark_node;
1561 name = is_constructor ? ctor_identifier : dtor_identifier;
1564 if (!DECL_CONV_FN_P (decl))
1566 idx = lookup_fnfields_1 (ctype, name);
1567 if (idx >= 0)
1568 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1570 else
1572 tree methods;
1574 /* For a type-conversion operator, we cannot do a
1575 name-based lookup. We might be looking for `operator
1576 int' which will be a specialization of `operator T'.
1577 So, we find *all* the conversion operators, and then
1578 select from them. */
1579 fns = NULL_TREE;
1581 methods = CLASSTYPE_METHOD_VEC (ctype);
1582 if (methods)
1583 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1585 tree ovl = TREE_VEC_ELT (methods, idx);
1587 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1588 /* There are no more conversion functions. */
1589 break;
1591 /* Glue all these conversion functions together
1592 with those we already have. */
1593 for (; ovl; ovl = OVL_NEXT (ovl))
1594 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1598 if (fns == NULL_TREE)
1600 error ("no member function `%D' declared in `%T'",
1601 name, ctype);
1602 return error_mark_node;
1604 else
1605 TREE_OPERAND (declarator, 0) = fns;
1608 /* Figure out what exactly is being specialized at this point.
1609 Note that for an explicit instantiation, even one for a
1610 member function, we cannot tell apriori whether the
1611 instantiation is for a member template, or just a member
1612 function of a template class. Even if a member template is
1613 being instantiated, the member template arguments may be
1614 elided if they can be deduced from the rest of the
1615 declaration. */
1616 tmpl = determine_specialization (declarator, decl,
1617 &targs,
1618 member_specialization);
1620 if (!tmpl || tmpl == error_mark_node)
1621 /* We couldn't figure out what this declaration was
1622 specializing. */
1623 return error_mark_node;
1624 else
1626 tree gen_tmpl = most_general_template (tmpl);
1628 if (explicit_instantiation)
1630 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1631 is done by do_decl_instantiation later. */
1633 int arg_depth = TMPL_ARGS_DEPTH (targs);
1634 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1636 if (arg_depth > parm_depth)
1638 /* If TMPL is not the most general template (for
1639 example, if TMPL is a friend template that is
1640 injected into namespace scope), then there will
1641 be too many levels of TARGS. Remove some of them
1642 here. */
1643 int i;
1644 tree new_targs;
1646 new_targs = make_tree_vec (parm_depth);
1647 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1648 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1649 = TREE_VEC_ELT (targs, i);
1650 targs = new_targs;
1653 return instantiate_template (tmpl, targs);
1656 /* If this is a specialization of a member template of a
1657 template class. In we want to return the TEMPLATE_DECL,
1658 not the specialization of it. */
1659 if (tsk == tsk_template)
1661 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1662 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1663 return tmpl;
1666 /* If we thought that the DECL was a member function, but it
1667 turns out to be specializing a static member function,
1668 make DECL a static member function as well. */
1669 if (DECL_STATIC_FUNCTION_P (tmpl)
1670 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1672 revert_static_member_fn (decl);
1673 last_function_parms = TREE_CHAIN (last_function_parms);
1676 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1677 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1679 /* Inherit default function arguments from the template
1680 DECL is specializing. */
1681 copy_default_args_to_explicit_spec (decl);
1683 /* This specialization has the same protection as the
1684 template it specializes. */
1685 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1686 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1688 if (is_friend && !have_def)
1689 /* This is not really a declaration of a specialization.
1690 It's just the name of an instantiation. But, it's not
1691 a request for an instantiation, either. */
1692 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1693 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1694 /* This is indeed a specialization. In case of constructors
1695 and destructors, we need in-charge and not-in-charge
1696 versions in V3 ABI. */
1697 clone_function_decl (decl, /*update_method_vec_p=*/0);
1699 /* Register this specialization so that we can find it
1700 again. */
1701 decl = register_specialization (decl, gen_tmpl, targs);
1705 return decl;
1708 /* TYPE is being declared. Verify that the use of template headers
1709 and such is reasonable. Issue error messages if not. */
1711 void
1712 maybe_check_template_type (type)
1713 tree type;
1715 if (template_header_count)
1717 /* We are in the scope of some `template <...>' header. */
1719 int context_depth
1720 = template_class_depth_real (TYPE_CONTEXT (type),
1721 /*count_specializations=*/1);
1723 if (template_header_count <= context_depth)
1724 /* This is OK; the template headers are for the context. We
1725 are actually too lenient here; like
1726 check_explicit_specialization we should consider the number
1727 of template types included in the actual declaration. For
1728 example,
1730 template <class T> struct S {
1731 template <class U> template <class V>
1732 struct I {};
1735 is illegal, but:
1737 template <class T> struct S {
1738 template <class U> struct I;
1741 template <class T> template <class U.
1742 struct S<T>::I {};
1744 is not. */
1746 else if (template_header_count > context_depth + 1)
1747 /* There are two many template parameter lists. */
1748 error ("too many template parameter lists in declaration of `%T'", type);
1752 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1753 parameters. These are represented in the same format used for
1754 DECL_TEMPLATE_PARMS. */
1756 int comp_template_parms (parms1, parms2)
1757 tree parms1;
1758 tree parms2;
1760 tree p1;
1761 tree p2;
1763 if (parms1 == parms2)
1764 return 1;
1766 for (p1 = parms1, p2 = parms2;
1767 p1 != NULL_TREE && p2 != NULL_TREE;
1768 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1770 tree t1 = TREE_VALUE (p1);
1771 tree t2 = TREE_VALUE (p2);
1772 int i;
1774 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1775 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1777 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1778 return 0;
1780 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1782 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1783 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1785 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1786 return 0;
1788 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1789 continue;
1790 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1791 return 0;
1795 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1796 /* One set of parameters has more parameters lists than the
1797 other. */
1798 return 0;
1800 return 1;
1803 /* Complain if DECL shadows a template parameter.
1805 [temp.local]: A template-parameter shall not be redeclared within its
1806 scope (including nested scopes). */
1808 void
1809 check_template_shadow (decl)
1810 tree decl;
1812 tree olddecl;
1814 /* If we're not in a template, we can't possibly shadow a template
1815 parameter. */
1816 if (!current_template_parms)
1817 return;
1819 /* Figure out what we're shadowing. */
1820 if (TREE_CODE (decl) == OVERLOAD)
1821 decl = OVL_CURRENT (decl);
1822 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1824 /* If there's no previous binding for this name, we're not shadowing
1825 anything, let alone a template parameter. */
1826 if (!olddecl)
1827 return;
1829 /* If we're not shadowing a template parameter, we're done. Note
1830 that OLDDECL might be an OVERLOAD (or perhaps even an
1831 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1832 node. */
1833 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1834 return;
1836 /* We check for decl != olddecl to avoid bogus errors for using a
1837 name inside a class. We check TPFI to avoid duplicate errors for
1838 inline member templates. */
1839 if (decl == olddecl
1840 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1841 return;
1843 cp_error_at ("declaration of `%#D'", decl);
1844 cp_error_at (" shadows template parm `%#D'", olddecl);
1847 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1848 ORIG_LEVEL, DECL, and TYPE. */
1850 static tree
1851 build_template_parm_index (index, level, orig_level, decl, type)
1852 int index;
1853 int level;
1854 int orig_level;
1855 tree decl;
1856 tree type;
1858 tree t = make_node (TEMPLATE_PARM_INDEX);
1859 TEMPLATE_PARM_IDX (t) = index;
1860 TEMPLATE_PARM_LEVEL (t) = level;
1861 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1862 TEMPLATE_PARM_DECL (t) = decl;
1863 TREE_TYPE (t) = type;
1865 return t;
1868 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1869 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1870 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1871 new one is created. */
1873 static tree
1874 reduce_template_parm_level (index, type, levels)
1875 tree index;
1876 tree type;
1877 int levels;
1879 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1880 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1881 != TEMPLATE_PARM_LEVEL (index) - levels))
1883 tree decl
1884 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1885 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1886 type);
1887 tree t
1888 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1889 TEMPLATE_PARM_LEVEL (index) - levels,
1890 TEMPLATE_PARM_ORIG_LEVEL (index),
1891 decl, type);
1892 TEMPLATE_PARM_DESCENDANTS (index) = t;
1894 DECL_ARTIFICIAL (decl) = 1;
1895 SET_DECL_TEMPLATE_PARM_P (decl);
1897 /* Template template parameters need this. */
1898 DECL_TEMPLATE_PARMS (decl)
1899 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1902 return TEMPLATE_PARM_DESCENDANTS (index);
1905 /* Process information from new template parameter NEXT and append it to the
1906 LIST being built. */
1908 tree
1909 process_template_parm (list, next)
1910 tree list, next;
1912 tree parm;
1913 tree decl = 0;
1914 tree defval;
1915 int is_type, idx;
1917 parm = next;
1918 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1919 defval = TREE_PURPOSE (parm);
1920 parm = TREE_VALUE (parm);
1921 is_type = TREE_PURPOSE (parm) == class_type_node;
1923 if (list)
1925 tree p = TREE_VALUE (tree_last (list));
1927 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
1928 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1929 else
1930 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1931 ++idx;
1933 else
1934 idx = 0;
1936 if (!is_type)
1938 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1939 /* is a const-param */
1940 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1941 PARM, 0, NULL);
1942 SET_DECL_TEMPLATE_PARM_P (parm);
1944 /* [temp.param]
1946 The top-level cv-qualifiers on the template-parameter are
1947 ignored when determining its type. */
1948 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1950 /* A template parameter is not modifiable. */
1951 TREE_READONLY (parm) = 1;
1952 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
1953 TREE_TYPE (parm) = void_type_node;
1954 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1955 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1956 = build_template_parm_index (idx, processing_template_decl,
1957 processing_template_decl,
1958 decl, TREE_TYPE (parm));
1960 else
1962 tree t;
1963 parm = TREE_VALUE (parm);
1965 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1967 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1968 /* This is for distinguishing between real templates and template
1969 template parameters */
1970 TREE_TYPE (parm) = t;
1971 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1972 decl = parm;
1974 else
1976 t = make_aggr_type (TEMPLATE_TYPE_PARM);
1977 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1978 decl = build_decl (TYPE_DECL, parm, t);
1981 TYPE_NAME (t) = decl;
1982 TYPE_STUB_DECL (t) = decl;
1983 parm = decl;
1984 TEMPLATE_TYPE_PARM_INDEX (t)
1985 = build_template_parm_index (idx, processing_template_decl,
1986 processing_template_decl,
1987 decl, TREE_TYPE (parm));
1989 DECL_ARTIFICIAL (decl) = 1;
1990 SET_DECL_TEMPLATE_PARM_P (decl);
1991 pushdecl (decl);
1992 parm = build_tree_list (defval, parm);
1993 return chainon (list, parm);
1996 /* The end of a template parameter list has been reached. Process the
1997 tree list into a parameter vector, converting each parameter into a more
1998 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1999 as PARM_DECLs. */
2001 tree
2002 end_template_parm_list (parms)
2003 tree parms;
2005 int nparms;
2006 tree parm, next;
2007 tree saved_parmlist = make_tree_vec (list_length (parms));
2009 current_template_parms
2010 = tree_cons (size_int (processing_template_decl),
2011 saved_parmlist, current_template_parms);
2013 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2015 next = TREE_CHAIN (parm);
2016 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2017 TREE_CHAIN (parm) = NULL_TREE;
2020 --processing_template_parmlist;
2022 return saved_parmlist;
2025 /* end_template_decl is called after a template declaration is seen. */
2027 void
2028 end_template_decl ()
2030 reset_specialization ();
2032 if (! processing_template_decl)
2033 return;
2035 /* This matches the pushlevel in begin_template_parm_list. */
2036 finish_scope ();
2038 --processing_template_decl;
2039 current_template_parms = TREE_CHAIN (current_template_parms);
2042 /* Given a template argument vector containing the template PARMS.
2043 The innermost PARMS are given first. */
2045 tree
2046 current_template_args ()
2048 tree header;
2049 tree args = NULL_TREE;
2050 int length = TMPL_PARMS_DEPTH (current_template_parms);
2051 int l = length;
2053 /* If there is only one level of template parameters, we do not
2054 create a TREE_VEC of TREE_VECs. Instead, we return a single
2055 TREE_VEC containing the arguments. */
2056 if (length > 1)
2057 args = make_tree_vec (length);
2059 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2061 tree a = copy_node (TREE_VALUE (header));
2062 int i;
2064 TREE_TYPE (a) = NULL_TREE;
2065 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2067 tree t = TREE_VEC_ELT (a, i);
2069 /* T will be a list if we are called from within a
2070 begin/end_template_parm_list pair, but a vector directly
2071 if within a begin/end_member_template_processing pair. */
2072 if (TREE_CODE (t) == TREE_LIST)
2074 t = TREE_VALUE (t);
2076 if (TREE_CODE (t) == TYPE_DECL
2077 || TREE_CODE (t) == TEMPLATE_DECL)
2078 t = TREE_TYPE (t);
2079 else
2080 t = DECL_INITIAL (t);
2081 TREE_VEC_ELT (a, i) = t;
2085 if (length > 1)
2086 TREE_VEC_ELT (args, --l) = a;
2087 else
2088 args = a;
2091 return args;
2094 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2095 template PARMS. Used by push_template_decl below. */
2097 static tree
2098 build_template_decl (decl, parms)
2099 tree decl;
2100 tree parms;
2102 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2103 DECL_TEMPLATE_PARMS (tmpl) = parms;
2104 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2105 if (DECL_LANG_SPECIFIC (decl))
2107 if (CAN_HAVE_FULL_LANG_DECL_P (decl))
2108 DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl);
2109 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2110 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2111 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2112 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2113 if (DECL_OVERLOADED_OPERATOR_P (decl))
2114 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2115 DECL_OVERLOADED_OPERATOR_P (decl));
2118 return tmpl;
2121 struct template_parm_data
2123 /* The level of the template parameters we are currently
2124 processing. */
2125 int level;
2127 /* The index of the specialization argument we are currently
2128 processing. */
2129 int current_arg;
2131 /* An array whose size is the number of template parameters. The
2132 elements are non-zero if the parameter has been used in any one
2133 of the arguments processed so far. */
2134 int* parms;
2136 /* An array whose size is the number of template arguments. The
2137 elements are non-zero if the argument makes use of template
2138 parameters of this level. */
2139 int* arg_uses_template_parms;
2142 /* Subroutine of push_template_decl used to see if each template
2143 parameter in a partial specialization is used in the explicit
2144 argument list. If T is of the LEVEL given in DATA (which is
2145 treated as a template_parm_data*), then DATA->PARMS is marked
2146 appropriately. */
2148 static int
2149 mark_template_parm (t, data)
2150 tree t;
2151 void* data;
2153 int level;
2154 int idx;
2155 struct template_parm_data* tpd = (struct template_parm_data*) data;
2157 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2159 level = TEMPLATE_PARM_LEVEL (t);
2160 idx = TEMPLATE_PARM_IDX (t);
2162 else
2164 level = TEMPLATE_TYPE_LEVEL (t);
2165 idx = TEMPLATE_TYPE_IDX (t);
2168 if (level == tpd->level)
2170 tpd->parms[idx] = 1;
2171 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2174 /* Return zero so that for_each_template_parm will continue the
2175 traversal of the tree; we want to mark *every* template parm. */
2176 return 0;
2179 /* Process the partial specialization DECL. */
2181 static tree
2182 process_partial_specialization (decl)
2183 tree decl;
2185 tree type = TREE_TYPE (decl);
2186 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2187 tree specargs = CLASSTYPE_TI_ARGS (type);
2188 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2189 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2190 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2191 int nargs = TREE_VEC_LENGTH (inner_args);
2192 int ntparms = TREE_VEC_LENGTH (inner_parms);
2193 int i;
2194 int did_error_intro = 0;
2195 struct template_parm_data tpd;
2196 struct template_parm_data tpd2;
2198 /* We check that each of the template parameters given in the
2199 partial specialization is used in the argument list to the
2200 specialization. For example:
2202 template <class T> struct S;
2203 template <class T> struct S<T*>;
2205 The second declaration is OK because `T*' uses the template
2206 parameter T, whereas
2208 template <class T> struct S<int>;
2210 is no good. Even trickier is:
2212 template <class T>
2213 struct S1
2215 template <class U>
2216 struct S2;
2217 template <class U>
2218 struct S2<T>;
2221 The S2<T> declaration is actually illegal; it is a
2222 full-specialization. Of course,
2224 template <class U>
2225 struct S2<T (*)(U)>;
2227 or some such would have been OK. */
2228 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2229 tpd.parms = alloca (sizeof (int) * ntparms);
2230 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2232 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2233 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2234 for (i = 0; i < nargs; ++i)
2236 tpd.current_arg = i;
2237 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2238 &mark_template_parm,
2239 &tpd);
2241 for (i = 0; i < ntparms; ++i)
2242 if (tpd.parms[i] == 0)
2244 /* One of the template parms was not used in the
2245 specialization. */
2246 if (!did_error_intro)
2248 error ("template parameters not used in partial specialization:");
2249 did_error_intro = 1;
2252 error (" `%D'",
2253 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2256 /* [temp.class.spec]
2258 The argument list of the specialization shall not be identical to
2259 the implicit argument list of the primary template. */
2260 if (comp_template_args
2261 (inner_args,
2262 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2263 (maintmpl)))))
2264 error ("partial specialization `%T' does not specialize any template arguments", type);
2266 /* [temp.class.spec]
2268 A partially specialized non-type argument expression shall not
2269 involve template parameters of the partial specialization except
2270 when the argument expression is a simple identifier.
2272 The type of a template parameter corresponding to a specialized
2273 non-type argument shall not be dependent on a parameter of the
2274 specialization. */
2275 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2276 tpd2.parms = 0;
2277 for (i = 0; i < nargs; ++i)
2279 tree arg = TREE_VEC_ELT (inner_args, i);
2280 if (/* These first two lines are the `non-type' bit. */
2281 !TYPE_P (arg)
2282 && TREE_CODE (arg) != TEMPLATE_DECL
2283 /* This next line is the `argument expression is not just a
2284 simple identifier' condition and also the `specialized
2285 non-type argument' bit. */
2286 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2288 if (tpd.arg_uses_template_parms[i])
2289 error ("template argument `%E' involves template parameter(s)", arg);
2290 else
2292 /* Look at the corresponding template parameter,
2293 marking which template parameters its type depends
2294 upon. */
2295 tree type =
2296 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2297 i)));
2299 if (!tpd2.parms)
2301 /* We haven't yet initialized TPD2. Do so now. */
2302 tpd2.arg_uses_template_parms
2303 = (int*) alloca (sizeof (int) * nargs);
2304 /* The number of parameters here is the number in the
2305 main template, which, as checked in the assertion
2306 above, is NARGS. */
2307 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2308 tpd2.level =
2309 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2312 /* Mark the template parameters. But this time, we're
2313 looking for the template parameters of the main
2314 template, not in the specialization. */
2315 tpd2.current_arg = i;
2316 tpd2.arg_uses_template_parms[i] = 0;
2317 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2318 for_each_template_parm (type,
2319 &mark_template_parm,
2320 &tpd2);
2322 if (tpd2.arg_uses_template_parms [i])
2324 /* The type depended on some template parameters.
2325 If they are fully specialized in the
2326 specialization, that's OK. */
2327 int j;
2328 for (j = 0; j < nargs; ++j)
2329 if (tpd2.parms[j] != 0
2330 && tpd.arg_uses_template_parms [j])
2332 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2333 type,
2334 arg);
2335 break;
2342 if (retrieve_specialization (maintmpl, specargs))
2343 /* We've already got this specialization. */
2344 return decl;
2346 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2347 = tree_cons (inner_args, inner_parms,
2348 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2349 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2350 return decl;
2353 /* Check that a template declaration's use of default arguments is not
2354 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2355 non-zero if DECL is the thing declared by a primary template.
2356 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2358 static void
2359 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2360 tree decl;
2361 tree parms;
2362 int is_primary;
2363 int is_partial;
2365 const char *msg;
2366 int last_level_to_check;
2367 tree parm_level;
2369 /* [temp.param]
2371 A default template-argument shall not be specified in a
2372 function template declaration or a function template definition, nor
2373 in the template-parameter-list of the definition of a member of a
2374 class template. */
2376 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2377 /* You can't have a function template declaration in a local
2378 scope, nor you can you define a member of a class template in a
2379 local scope. */
2380 return;
2382 if (current_class_type
2383 && !TYPE_BEING_DEFINED (current_class_type)
2384 && DECL_LANG_SPECIFIC (decl)
2385 /* If this is either a friend defined in the scope of the class
2386 or a member function. */
2387 && ((DECL_CONTEXT (decl)
2388 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2389 || (DECL_FRIEND_CONTEXT (decl)
2390 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2391 current_class_type)))
2392 /* And, if it was a member function, it really was defined in
2393 the scope of the class. */
2394 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2395 /* We already checked these parameters when the template was
2396 declared, so there's no need to do it again now. This function
2397 was defined in class scope, but we're processing it's body now
2398 that the class is complete. */
2399 return;
2401 /* [temp.param]
2403 If a template-parameter has a default template-argument, all
2404 subsequent template-parameters shall have a default
2405 template-argument supplied. */
2406 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2408 tree inner_parms = TREE_VALUE (parm_level);
2409 int ntparms = TREE_VEC_LENGTH (inner_parms);
2410 int seen_def_arg_p = 0;
2411 int i;
2413 for (i = 0; i < ntparms; ++i)
2415 tree parm = TREE_VEC_ELT (inner_parms, i);
2416 if (TREE_PURPOSE (parm))
2417 seen_def_arg_p = 1;
2418 else if (seen_def_arg_p)
2420 error ("no default argument for `%D'", TREE_VALUE (parm));
2421 /* For better subsequent error-recovery, we indicate that
2422 there should have been a default argument. */
2423 TREE_PURPOSE (parm) = error_mark_node;
2428 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2429 /* For an ordinary class template, default template arguments are
2430 allowed at the innermost level, e.g.:
2431 template <class T = int>
2432 struct S {};
2433 but, in a partial specialization, they're not allowed even
2434 there, as we have in [temp.class.spec]:
2436 The template parameter list of a specialization shall not
2437 contain default template argument values.
2439 So, for a partial specialization, or for a function template,
2440 we look at all of them. */
2442 else
2443 /* But, for a primary class template that is not a partial
2444 specialization we look at all template parameters except the
2445 innermost ones. */
2446 parms = TREE_CHAIN (parms);
2448 /* Figure out what error message to issue. */
2449 if (TREE_CODE (decl) == FUNCTION_DECL)
2450 msg = "default template arguments may not be used in function templates";
2451 else if (is_partial)
2452 msg = "default template arguments may not be used in partial specializations";
2453 else
2454 msg = "default argument for template parameter for class enclosing `%D'";
2456 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2457 /* If we're inside a class definition, there's no need to
2458 examine the parameters to the class itself. On the one
2459 hand, they will be checked when the class is defined, and,
2460 on the other, default arguments are legal in things like:
2461 template <class T = double>
2462 struct S { template <class U> void f(U); };
2463 Here the default argument for `S' has no bearing on the
2464 declaration of `f'. */
2465 last_level_to_check = template_class_depth (current_class_type) + 1;
2466 else
2467 /* Check everything. */
2468 last_level_to_check = 0;
2470 for (parm_level = parms;
2471 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2472 parm_level = TREE_CHAIN (parm_level))
2474 tree inner_parms = TREE_VALUE (parm_level);
2475 int i;
2476 int ntparms;
2478 ntparms = TREE_VEC_LENGTH (inner_parms);
2479 for (i = 0; i < ntparms; ++i)
2480 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2482 if (msg)
2484 error (msg, decl);
2485 msg = 0;
2488 /* Clear out the default argument so that we are not
2489 confused later. */
2490 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2493 /* At this point, if we're still interested in issuing messages,
2494 they must apply to classes surrounding the object declared. */
2495 if (msg)
2496 msg = "default argument for template parameter for class enclosing `%D'";
2500 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2501 parameters given by current_template_args, or reuses a
2502 previously existing one, if appropriate. Returns the DECL, or an
2503 equivalent one, if it is replaced via a call to duplicate_decls.
2505 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2507 tree
2508 push_template_decl_real (decl, is_friend)
2509 tree decl;
2510 int is_friend;
2512 tree tmpl;
2513 tree args;
2514 tree info;
2515 tree ctx;
2516 int primary;
2517 int is_partial;
2518 int new_template_p = 0;
2520 /* See if this is a partial specialization. */
2521 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2522 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2523 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2525 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2527 if (is_friend)
2528 /* For a friend, we want the context of the friend function, not
2529 the type of which it is a friend. */
2530 ctx = DECL_CONTEXT (decl);
2531 else if (CP_DECL_CONTEXT (decl)
2532 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2533 /* In the case of a virtual function, we want the class in which
2534 it is defined. */
2535 ctx = CP_DECL_CONTEXT (decl);
2536 else
2537 /* Otherwise, if we're currently defining some class, the DECL
2538 is assumed to be a member of the class. */
2539 ctx = current_scope ();
2541 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2542 ctx = NULL_TREE;
2544 if (!DECL_CONTEXT (decl))
2545 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2547 /* See if this is a primary template. */
2548 primary = template_parm_scope_p ();
2550 if (primary)
2552 if (current_lang_name == lang_name_c)
2553 error ("template with C linkage");
2554 else if (TREE_CODE (decl) == TYPE_DECL
2555 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2556 error ("template class without a name");
2557 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2558 && CLASS_TYPE_P (TREE_TYPE (decl)))
2559 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2560 || TREE_CODE (decl) == FUNCTION_DECL)
2561 /* OK */;
2562 else
2563 error ("template declaration of `%#D'", decl);
2566 /* Check to see that the rules regarding the use of default
2567 arguments are not being violated. */
2568 check_default_tmpl_args (decl, current_template_parms,
2569 primary, is_partial);
2571 if (is_partial)
2572 return process_partial_specialization (decl);
2574 args = current_template_args ();
2576 if (!ctx
2577 || TREE_CODE (ctx) == FUNCTION_DECL
2578 || (TREE_CODE (ctx) != TEMPLATE_TYPE_PARM
2579 && TREE_CODE (ctx) != BOUND_TEMPLATE_TEMPLATE_PARM
2580 && TYPE_BEING_DEFINED (ctx))
2581 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2583 if (DECL_LANG_SPECIFIC (decl)
2584 && DECL_TEMPLATE_INFO (decl)
2585 && DECL_TI_TEMPLATE (decl))
2586 tmpl = DECL_TI_TEMPLATE (decl);
2587 /* If DECL is a TYPE_DECL for a class-template, then there won't
2588 be DECL_LANG_SPECIFIC. The information equivalent to
2589 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2590 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2591 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2592 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2594 /* Since a template declaration already existed for this
2595 class-type, we must be redeclaring it here. Make sure
2596 that the redeclaration is legal. */
2597 redeclare_class_template (TREE_TYPE (decl),
2598 current_template_parms);
2599 /* We don't need to create a new TEMPLATE_DECL; just use the
2600 one we already had. */
2601 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2603 else
2605 tmpl = build_template_decl (decl, current_template_parms);
2606 new_template_p = 1;
2608 if (DECL_LANG_SPECIFIC (decl)
2609 && DECL_TEMPLATE_SPECIALIZATION (decl))
2611 /* A specialization of a member template of a template
2612 class. */
2613 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2614 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2615 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2619 else
2621 tree a, t, current, parms;
2622 int i;
2624 if (TREE_CODE (decl) == TYPE_DECL)
2626 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2627 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2628 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2629 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2630 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2631 else
2633 error ("`%D' does not declare a template type", decl);
2634 return decl;
2637 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2639 error ("template definition of non-template `%#D'", decl);
2640 return decl;
2642 else
2643 tmpl = DECL_TI_TEMPLATE (decl);
2645 if (is_member_template (tmpl)
2646 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2647 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2648 && DECL_TEMPLATE_SPECIALIZATION (decl))
2650 tree new_tmpl;
2652 /* The declaration is a specialization of a member
2653 template, declared outside the class. Therefore, the
2654 innermost template arguments will be NULL, so we
2655 replace them with the arguments determined by the
2656 earlier call to check_explicit_specialization. */
2657 args = DECL_TI_ARGS (decl);
2659 new_tmpl
2660 = build_template_decl (decl, current_template_parms);
2661 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2662 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2663 DECL_TI_TEMPLATE (decl) = new_tmpl;
2664 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2665 DECL_TEMPLATE_INFO (new_tmpl)
2666 = tree_cons (tmpl, args, NULL_TREE);
2668 register_specialization (new_tmpl,
2669 most_general_template (tmpl),
2670 args);
2671 return decl;
2674 /* Make sure the template headers we got make sense. */
2676 parms = DECL_TEMPLATE_PARMS (tmpl);
2677 i = TMPL_PARMS_DEPTH (parms);
2678 if (TMPL_ARGS_DEPTH (args) != i)
2680 error ("expected %d levels of template parms for `%#D', got %d",
2681 i, decl, TMPL_ARGS_DEPTH (args));
2683 else
2684 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2686 a = TMPL_ARGS_LEVEL (args, i);
2687 t = INNERMOST_TEMPLATE_PARMS (parms);
2689 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2691 if (current == decl)
2692 error ("got %d template parameters for `%#D'",
2693 TREE_VEC_LENGTH (a), decl);
2694 else
2695 error ("got %d template parameters for `%#T'",
2696 TREE_VEC_LENGTH (a), current);
2697 error (" but %d required", TREE_VEC_LENGTH (t));
2700 /* Perhaps we should also check that the parms are used in the
2701 appropriate qualifying scopes in the declarator? */
2703 if (current == decl)
2704 current = ctx;
2705 else
2706 current = TYPE_CONTEXT (current);
2710 DECL_TEMPLATE_RESULT (tmpl) = decl;
2711 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2713 /* Push template declarations for global functions and types. Note
2714 that we do not try to push a global template friend declared in a
2715 template class; such a thing may well depend on the template
2716 parameters of the class. */
2717 if (new_template_p && !ctx
2718 && !(is_friend && template_class_depth (current_class_type) > 0))
2719 tmpl = pushdecl_namespace_level (tmpl);
2721 if (primary)
2722 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2724 info = tree_cons (tmpl, args, NULL_TREE);
2726 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2728 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2729 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2730 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2731 /* Don't change the name if we've already set it up. */
2732 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2733 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2735 else if (DECL_LANG_SPECIFIC (decl))
2736 DECL_TEMPLATE_INFO (decl) = info;
2738 return DECL_TEMPLATE_RESULT (tmpl);
2741 tree
2742 push_template_decl (decl)
2743 tree decl;
2745 return push_template_decl_real (decl, 0);
2748 /* Called when a class template TYPE is redeclared with the indicated
2749 template PARMS, e.g.:
2751 template <class T> struct S;
2752 template <class T> struct S {}; */
2754 void
2755 redeclare_class_template (type, parms)
2756 tree type;
2757 tree parms;
2759 tree tmpl;
2760 tree tmpl_parms;
2761 int i;
2763 if (!TYPE_TEMPLATE_INFO (type))
2765 error ("`%T' is not a template type", type);
2766 return;
2769 tmpl = TYPE_TI_TEMPLATE (type);
2770 if (!PRIMARY_TEMPLATE_P (tmpl))
2771 /* The type is nested in some template class. Nothing to worry
2772 about here; there are no new template parameters for the nested
2773 type. */
2774 return;
2776 parms = INNERMOST_TEMPLATE_PARMS (parms);
2777 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2779 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2781 cp_error_at ("previous declaration `%D'", tmpl);
2782 error ("used %d template parameter%s instead of %d",
2783 TREE_VEC_LENGTH (tmpl_parms),
2784 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2785 TREE_VEC_LENGTH (parms));
2786 return;
2789 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2791 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2792 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2793 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2794 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2796 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2798 cp_error_at ("template parameter `%#D'", tmpl_parm);
2799 error ("redeclared here as `%#D'", parm);
2800 return;
2803 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2805 /* We have in [temp.param]:
2807 A template-parameter may not be given default arguments
2808 by two different declarations in the same scope. */
2809 error ("redefinition of default argument for `%#D'", parm);
2810 cp_error_at (" original definition appeared here", tmpl_parm);
2811 return;
2814 if (parm_default != NULL_TREE)
2815 /* Update the previous template parameters (which are the ones
2816 that will really count) with the new default value. */
2817 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2818 else if (tmpl_default != NULL_TREE)
2819 /* Update the new parameters, too; they'll be used as the
2820 parameters for any members. */
2821 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2825 /* Attempt to convert the non-type template parameter EXPR to the
2826 indicated TYPE. If the conversion is successful, return the
2827 converted value. If the conversion is unsuccessful, return
2828 NULL_TREE if we issued an error message, or error_mark_node if we
2829 did not. We issue error messages for out-and-out bad template
2830 parameters, but not simply because the conversion failed, since we
2831 might be just trying to do argument deduction. By the time this
2832 function is called, neither TYPE nor EXPR may make use of template
2833 parameters. */
2835 static tree
2836 convert_nontype_argument (type, expr)
2837 tree type;
2838 tree expr;
2840 tree expr_type = TREE_TYPE (expr);
2842 /* A template-argument for a non-type, non-template
2843 template-parameter shall be one of:
2845 --an integral constant-expression of integral or enumeration
2846 type; or
2848 --the name of a non-type template-parameter; or
2850 --the name of an object or function with external linkage,
2851 including function templates and function template-ids but
2852 excluding non-static class members, expressed as id-expression;
2855 --the address of an object or function with external linkage,
2856 including function templates and function template-ids but
2857 excluding non-static class members, expressed as & id-expression
2858 where the & is optional if the name refers to a function or
2859 array; or
2861 --a pointer to member expressed as described in _expr.unary.op_. */
2863 /* An integral constant-expression can include const variables or
2864 enumerators. Simplify things by folding them to their values,
2865 unless we're about to bind the declaration to a reference
2866 parameter. */
2867 if (INTEGRAL_TYPE_P (expr_type)
2868 && TREE_CODE (type) != REFERENCE_TYPE)
2869 expr = decl_constant_value (expr);
2871 if (is_overloaded_fn (expr))
2872 /* OK for now. We'll check that it has external linkage later.
2873 Check this first since if expr_type is the unknown_type_node
2874 we would otherwise complain below. */
2876 else if (TYPE_PTRMEM_P (expr_type)
2877 || TYPE_PTRMEMFUNC_P (expr_type))
2879 if (TREE_CODE (expr) != PTRMEM_CST)
2880 goto bad_argument;
2882 else if (TYPE_PTR_P (expr_type)
2883 || TYPE_PTRMEM_P (expr_type)
2884 || TREE_CODE (expr_type) == ARRAY_TYPE
2885 || TREE_CODE (type) == REFERENCE_TYPE
2886 /* If expr is the address of an overloaded function, we
2887 will get the unknown_type_node at this point. */
2888 || expr_type == unknown_type_node)
2890 tree referent;
2891 tree e = expr;
2892 STRIP_NOPS (e);
2894 if (TREE_CODE (expr_type) == ARRAY_TYPE
2895 || (TREE_CODE (type) == REFERENCE_TYPE
2896 && TREE_CODE (e) != ADDR_EXPR))
2897 referent = e;
2898 else
2900 if (TREE_CODE (e) != ADDR_EXPR)
2902 bad_argument:
2903 error ("`%E' is not a valid template argument", expr);
2904 if (TYPE_PTR_P (expr_type))
2906 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2907 error ("it must be the address of a function with external linkage");
2908 else
2909 error ("it must be the address of an object with external linkage");
2911 else if (TYPE_PTRMEM_P (expr_type)
2912 || TYPE_PTRMEMFUNC_P (expr_type))
2913 error ("it must be a pointer-to-member of the form `&X::Y'");
2915 return NULL_TREE;
2918 referent = TREE_OPERAND (e, 0);
2919 STRIP_NOPS (referent);
2922 if (TREE_CODE (referent) == STRING_CST)
2924 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2925 referent);
2926 return NULL_TREE;
2929 if (is_overloaded_fn (referent))
2930 /* We'll check that it has external linkage later. */
2932 else if (TREE_CODE (referent) != VAR_DECL)
2933 goto bad_argument;
2934 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
2936 error ("address of non-extern `%E' cannot be used as template argument", referent);
2937 return error_mark_node;
2940 else if (INTEGRAL_TYPE_P (expr_type)
2941 || TYPE_PTRMEM_P (expr_type)
2942 || TYPE_PTRMEMFUNC_P (expr_type))
2944 if (! TREE_CONSTANT (expr))
2946 non_constant:
2947 error ("non-constant `%E' cannot be used as template argument",
2948 expr);
2949 return NULL_TREE;
2952 else
2954 error ("object `%E' cannot be used as template argument", expr);
2955 return NULL_TREE;
2958 switch (TREE_CODE (type))
2960 case INTEGER_TYPE:
2961 case BOOLEAN_TYPE:
2962 case ENUMERAL_TYPE:
2963 /* For a non-type template-parameter of integral or enumeration
2964 type, integral promotions (_conv.prom_) and integral
2965 conversions (_conv.integral_) are applied. */
2966 if (!INTEGRAL_TYPE_P (expr_type))
2967 return error_mark_node;
2969 /* It's safe to call digest_init in this case; we know we're
2970 just converting one integral constant expression to another. */
2971 expr = digest_init (type, expr, (tree*) 0);
2973 if (TREE_CODE (expr) != INTEGER_CST)
2974 /* Curiously, some TREE_CONSTANT integral expressions do not
2975 simplify to integer constants. For example, `3 % 0',
2976 remains a TRUNC_MOD_EXPR. */
2977 goto non_constant;
2979 return expr;
2981 case POINTER_TYPE:
2983 tree type_pointed_to = TREE_TYPE (type);
2985 if (TYPE_PTRMEM_P (type))
2987 tree e;
2989 /* For a non-type template-parameter of type pointer to data
2990 member, qualification conversions (_conv.qual_) are
2991 applied. */
2992 e = perform_qualification_conversions (type, expr);
2993 if (TREE_CODE (e) == NOP_EXPR)
2994 /* The call to perform_qualification_conversions will
2995 insert a NOP_EXPR over EXPR to do express conversion,
2996 if necessary. But, that will confuse us if we use
2997 this (converted) template parameter to instantiate
2998 another template; then the thing will not look like a
2999 valid template argument. So, just make a new
3000 constant, of the appropriate type. */
3001 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3002 return e;
3004 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3006 /* For a non-type template-parameter of type pointer to
3007 function, only the function-to-pointer conversion
3008 (_conv.func_) is applied. If the template-argument
3009 represents a set of overloaded functions (or a pointer to
3010 such), the matching function is selected from the set
3011 (_over.over_). */
3012 tree fns;
3013 tree fn;
3015 if (TREE_CODE (expr) == ADDR_EXPR)
3016 fns = TREE_OPERAND (expr, 0);
3017 else
3018 fns = expr;
3020 fn = instantiate_type (type_pointed_to, fns, tf_none);
3022 if (fn == error_mark_node)
3023 return error_mark_node;
3025 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3027 if (really_overloaded_fn (fns))
3028 return error_mark_node;
3029 else
3030 goto bad_argument;
3033 expr = build_unary_op (ADDR_EXPR, fn, 0);
3035 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3037 return expr;
3039 else
3041 /* For a non-type template-parameter of type pointer to
3042 object, qualification conversions (_conv.qual_) and the
3043 array-to-pointer conversion (_conv.array_) are applied.
3044 [Note: In particular, neither the null pointer conversion
3045 (_conv.ptr_) nor the derived-to-base conversion
3046 (_conv.ptr_) are applied. Although 0 is a valid
3047 template-argument for a non-type template-parameter of
3048 integral type, it is not a valid template-argument for a
3049 non-type template-parameter of pointer type.]
3051 The call to decay_conversion performs the
3052 array-to-pointer conversion, if appropriate. */
3053 expr = decay_conversion (expr);
3055 if (expr == error_mark_node)
3056 return error_mark_node;
3057 else
3058 return perform_qualification_conversions (type, expr);
3061 break;
3063 case REFERENCE_TYPE:
3065 tree type_referred_to = TREE_TYPE (type);
3067 /* If this expression already has reference type, get the
3068 underling object. */
3069 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3071 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3072 expr = TREE_OPERAND (expr, 0);
3073 expr_type = TREE_TYPE (expr);
3076 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3078 /* For a non-type template-parameter of type reference to
3079 function, no conversions apply. If the
3080 template-argument represents a set of overloaded
3081 functions, the matching function is selected from the
3082 set (_over.over_). */
3083 tree fn;
3085 fn = instantiate_type (type_referred_to, expr, tf_none);
3087 if (fn == error_mark_node)
3088 return error_mark_node;
3090 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3092 if (really_overloaded_fn (expr))
3093 /* Don't issue an error here; we might get a different
3094 function if the overloading had worked out
3095 differently. */
3096 return error_mark_node;
3097 else
3098 goto bad_argument;
3101 my_friendly_assert (same_type_p (type_referred_to,
3102 TREE_TYPE (fn)),
3105 expr = fn;
3107 else
3109 /* For a non-type template-parameter of type reference to
3110 object, no conversions apply. The type referred to by the
3111 reference may be more cv-qualified than the (otherwise
3112 identical) type of the template-argument. The
3113 template-parameter is bound directly to the
3114 template-argument, which must be an lvalue. */
3115 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3116 TYPE_MAIN_VARIANT (type_referred_to))
3117 || !at_least_as_qualified_p (type_referred_to,
3118 expr_type)
3119 || !real_lvalue_p (expr))
3120 return error_mark_node;
3123 mark_addressable (expr);
3124 return build1 (ADDR_EXPR, type, expr);
3126 break;
3128 case RECORD_TYPE:
3130 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3132 /* For a non-type template-parameter of type pointer to member
3133 function, no conversions apply. If the template-argument
3134 represents a set of overloaded member functions, the
3135 matching member function is selected from the set
3136 (_over.over_). */
3138 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3139 expr_type != unknown_type_node)
3140 return error_mark_node;
3142 if (TREE_CODE (expr) == PTRMEM_CST)
3144 /* A ptr-to-member constant. */
3145 if (!same_type_p (type, expr_type))
3146 return error_mark_node;
3147 else
3148 return expr;
3151 if (TREE_CODE (expr) != ADDR_EXPR)
3152 return error_mark_node;
3154 expr = instantiate_type (type, expr, tf_none);
3156 if (expr == error_mark_node)
3157 return error_mark_node;
3159 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3161 return expr;
3163 break;
3165 default:
3166 /* All non-type parameters must have one of these types. */
3167 abort ();
3168 break;
3171 return error_mark_node;
3174 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3175 template template parameters. Both PARM_PARMS and ARG_PARMS are
3176 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3177 or PARM_DECL.
3179 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3180 the case, then extra parameters must have default arguments.
3182 Consider the example:
3183 template <class T, class Allocator = allocator> class vector;
3184 template<template <class U> class TT> class C;
3186 C<vector> is a valid instantiation. PARM_PARMS for the above code
3187 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3188 T and Allocator) and OUTER_ARGS contains the argument that is used to
3189 substitute the TT parameter. */
3191 static int
3192 coerce_template_template_parms (parm_parms, arg_parms, complain,
3193 in_decl, outer_args)
3194 tree parm_parms, arg_parms;
3195 tsubst_flags_t complain;
3196 tree in_decl, outer_args;
3198 int nparms, nargs, i;
3199 tree parm, arg;
3201 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3202 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3204 nparms = TREE_VEC_LENGTH (parm_parms);
3205 nargs = TREE_VEC_LENGTH (arg_parms);
3207 /* The rule here is opposite of coerce_template_parms. */
3208 if (nargs < nparms
3209 || (nargs > nparms
3210 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3211 return 0;
3213 for (i = 0; i < nparms; ++i)
3215 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3216 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3218 if (arg == NULL_TREE || arg == error_mark_node
3219 || parm == NULL_TREE || parm == error_mark_node)
3220 return 0;
3222 if (TREE_CODE (arg) != TREE_CODE (parm))
3223 return 0;
3225 switch (TREE_CODE (parm))
3227 case TYPE_DECL:
3228 break;
3230 case TEMPLATE_DECL:
3231 /* We encounter instantiations of templates like
3232 template <template <template <class> class> class TT>
3233 class C; */
3235 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3236 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3238 if (!coerce_template_template_parms (parmparm, argparm,
3239 complain, in_decl,
3240 outer_args))
3241 return 0;
3243 break;
3245 case PARM_DECL:
3246 /* The tsubst call is used to handle cases such as
3247 template <class T, template <T> class TT> class D;
3248 i.e. the parameter list of TT depends on earlier parameters. */
3249 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3250 complain, in_decl),
3251 TREE_TYPE (arg)))
3252 return 0;
3253 break;
3255 default:
3256 abort ();
3259 return 1;
3262 /* Convert the indicated template ARG as necessary to match the
3263 indicated template PARM. Returns the converted ARG, or
3264 error_mark_node if the conversion was unsuccessful. Error and
3265 warning messages are issued under control of COMPLAIN. This
3266 conversion is for the Ith parameter in the parameter list. ARGS is
3267 the full set of template arguments deduced so far. */
3269 static tree
3270 convert_template_argument (parm, arg, args, complain, i, in_decl)
3271 tree parm;
3272 tree arg;
3273 tree args;
3274 tsubst_flags_t complain;
3275 int i;
3276 tree in_decl;
3278 tree val;
3279 tree inner_args;
3280 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3282 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3284 if (TREE_CODE (arg) == TREE_LIST
3285 && TREE_TYPE (arg) != NULL_TREE
3286 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3288 /* The template argument was the name of some
3289 member function. That's usually
3290 illegal, but static members are OK. In any
3291 case, grab the underlying fields/functions
3292 and issue an error later if required. */
3293 arg = TREE_VALUE (arg);
3294 TREE_TYPE (arg) = unknown_type_node;
3297 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3298 requires_type = (TREE_CODE (parm) == TYPE_DECL
3299 || requires_tmpl_type);
3301 if (TREE_CODE (arg) != RECORD_TYPE)
3302 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3303 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3304 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3305 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3306 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3309 if (is_base_of_enclosing_class (arg, current_class_type))
3310 /* This is a template name used within the scope of the
3311 template. It could be the template, or it could be the
3312 instantiation. Choose whichever makes sense. */
3313 is_tmpl_type = requires_tmpl_type;
3314 else
3315 is_tmpl_type = 1;
3317 else
3318 /* It is a non-template class, or a specialization of a template
3319 class, or a non-template member of a template class. */
3320 is_tmpl_type = 0;
3322 if (is_tmpl_type
3323 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3324 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3325 arg = TYPE_STUB_DECL (arg);
3326 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3327 arg = CLASSTYPE_TI_TEMPLATE (arg);
3329 is_type = TYPE_P (arg) || is_tmpl_type;
3331 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3332 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3334 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3336 arg = make_typename_type (TREE_OPERAND (arg, 0),
3337 TREE_OPERAND (arg, 1),
3338 complain & tf_error);
3339 is_type = 1;
3341 if (is_type != requires_type)
3343 if (in_decl)
3345 if (complain & tf_error)
3347 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3348 i + 1, in_decl);
3349 if (is_type)
3350 error (" expected a constant of type `%T', got `%T'",
3351 TREE_TYPE (parm),
3352 (is_tmpl_type ? DECL_NAME (arg) : arg));
3353 else
3354 error (" expected a type, got `%E'", arg);
3357 return error_mark_node;
3359 if (is_tmpl_type ^ requires_tmpl_type)
3361 if (in_decl && (complain & tf_error))
3363 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3364 i + 1, in_decl);
3365 if (is_tmpl_type)
3366 error (" expected a type, got `%T'", DECL_NAME (arg));
3367 else
3368 error (" expected a class template, got `%T'", arg);
3370 return error_mark_node;
3373 if (is_type)
3375 if (requires_tmpl_type)
3377 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3378 /* The number of argument required is not known yet.
3379 Just accept it for now. */
3380 val = TREE_TYPE (arg);
3381 else
3383 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3384 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3386 if (coerce_template_template_parms (parmparm, argparm,
3387 complain, in_decl,
3388 inner_args))
3390 val = arg;
3392 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3393 TEMPLATE_DECL. */
3394 if (val != error_mark_node
3395 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3396 val = TREE_TYPE (val);
3398 else
3400 if (in_decl && (complain & tf_error))
3402 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3403 i + 1, in_decl);
3404 error (" expected a template of type `%D', got `%D'", parm, arg);
3407 val = error_mark_node;
3411 else
3413 val = groktypename (arg);
3414 if (! processing_template_decl)
3416 /* [basic.link]: A name with no linkage (notably, the
3417 name of a class or enumeration declared in a local
3418 scope) shall not be used to declare an entity with
3419 linkage. This implies that names with no linkage
3420 cannot be used as template arguments. */
3421 tree t = no_linkage_check (val);
3422 if (t)
3424 if (TYPE_ANONYMOUS_P (t))
3425 pedwarn
3426 ("template-argument `%T' uses anonymous type", val);
3427 else
3428 error
3429 ("template-argument `%T' uses local type `%T'",
3430 val, t);
3431 return error_mark_node;
3436 else
3438 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3440 if (invalid_nontype_parm_type_p (t, complain))
3441 return error_mark_node;
3443 if (processing_template_decl)
3444 arg = maybe_fold_nontype_arg (arg);
3446 if (!uses_template_parms (arg) && !uses_template_parms (t))
3447 /* We used to call digest_init here. However, digest_init
3448 will report errors, which we don't want when complain
3449 is zero. More importantly, digest_init will try too
3450 hard to convert things: for example, `0' should not be
3451 converted to pointer type at this point according to
3452 the standard. Accepting this is not merely an
3453 extension, since deciding whether or not these
3454 conversions can occur is part of determining which
3455 function template to call, or whether a given explicit
3456 argument specification is legal. */
3457 val = convert_nontype_argument (t, arg);
3458 else
3459 val = arg;
3461 if (val == NULL_TREE)
3462 val = error_mark_node;
3463 else if (val == error_mark_node && (complain & tf_error))
3464 error ("could not convert template argument `%E' to `%T'",
3465 arg, t);
3468 return val;
3471 /* Convert all template arguments to their appropriate types, and
3472 return a vector containing the innermost resulting template
3473 arguments. If any error occurs, return error_mark_node. Error and
3474 warning messages are issued under control of COMPLAIN. Some error
3475 messages are issued even if COMPLAIN is zero; for instance, if a
3476 template argument is composed from a local class.
3478 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3479 provided in ARGLIST, or else trailing parameters must have default
3480 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3481 deduction for any unspecified trailing arguments. */
3483 static tree
3484 coerce_template_parms (parms, args, in_decl,
3485 complain,
3486 require_all_arguments)
3487 tree parms, args;
3488 tree in_decl;
3489 tsubst_flags_t complain;
3490 int require_all_arguments;
3492 int nparms, nargs, i, lost = 0;
3493 tree inner_args;
3494 tree new_args;
3495 tree new_inner_args;
3497 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3498 nargs = NUM_TMPL_ARGS (inner_args);
3499 nparms = TREE_VEC_LENGTH (parms);
3501 if (nargs > nparms
3502 || (nargs < nparms
3503 && require_all_arguments
3504 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3506 if (complain & tf_error)
3508 error ("wrong number of template arguments (%d, should be %d)",
3509 nargs, nparms);
3511 if (in_decl)
3512 cp_error_at ("provided for `%D'", in_decl);
3515 return error_mark_node;
3518 new_inner_args = make_tree_vec (nparms);
3519 new_args = add_outermost_template_args (args, new_inner_args);
3520 for (i = 0; i < nparms; i++)
3522 tree arg;
3523 tree parm;
3525 /* Get the Ith template parameter. */
3526 parm = TREE_VEC_ELT (parms, i);
3528 /* Calculate the Ith argument. */
3529 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3531 arg = TREE_VALUE (inner_args);
3532 inner_args = TREE_CHAIN (inner_args);
3534 else if (i < nargs)
3535 arg = TREE_VEC_ELT (inner_args, i);
3536 /* If no template argument was supplied, look for a default
3537 value. */
3538 else if (TREE_PURPOSE (parm) == NULL_TREE)
3540 /* There was no default value. */
3541 my_friendly_assert (!require_all_arguments, 0);
3542 break;
3544 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3545 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3546 else
3547 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3548 in_decl);
3550 /* Now, convert the Ith argument, as necessary. */
3551 if (arg == NULL_TREE)
3552 /* We're out of arguments. */
3554 my_friendly_assert (!require_all_arguments, 0);
3555 break;
3557 else if (arg == error_mark_node)
3559 error ("template argument %d is invalid", i + 1);
3560 arg = error_mark_node;
3562 else
3563 arg = convert_template_argument (TREE_VALUE (parm),
3564 arg, new_args, complain, i,
3565 in_decl);
3567 if (arg == error_mark_node)
3568 lost++;
3569 TREE_VEC_ELT (new_inner_args, i) = arg;
3572 if (lost)
3573 return error_mark_node;
3575 return new_inner_args;
3578 /* Returns 1 if template args OT and NT are equivalent. */
3580 static int
3581 template_args_equal (ot, nt)
3582 tree ot, nt;
3584 if (nt == ot)
3585 return 1;
3587 if (TREE_CODE (nt) == TREE_VEC)
3588 /* For member templates */
3589 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3590 else if (TYPE_P (nt))
3591 return TYPE_P (ot) && same_type_p (ot, nt);
3592 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3593 return 0;
3594 else
3595 return (cp_tree_equal (ot, nt) > 0);
3598 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3599 of template arguments. Returns 0 otherwise. */
3602 comp_template_args (oldargs, newargs)
3603 tree oldargs, newargs;
3605 int i;
3607 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3608 return 0;
3610 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3612 tree nt = TREE_VEC_ELT (newargs, i);
3613 tree ot = TREE_VEC_ELT (oldargs, i);
3615 if (! template_args_equal (ot, nt))
3616 return 0;
3618 return 1;
3621 /* Given class template name and parameter list, produce a user-friendly name
3622 for the instantiation. */
3624 static char *
3625 mangle_class_name_for_template (name, parms, arglist)
3626 const char *name;
3627 tree parms, arglist;
3629 static struct obstack scratch_obstack;
3630 static char *scratch_firstobj;
3631 int i, nparms;
3633 if (!scratch_firstobj)
3634 gcc_obstack_init (&scratch_obstack);
3635 else
3636 obstack_free (&scratch_obstack, scratch_firstobj);
3637 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3639 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3640 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3642 cat (name);
3643 ccat ('<');
3644 nparms = TREE_VEC_LENGTH (parms);
3645 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3646 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3647 for (i = 0; i < nparms; i++)
3649 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3650 tree arg = TREE_VEC_ELT (arglist, i);
3652 if (i)
3653 ccat (',');
3655 if (TREE_CODE (parm) == TYPE_DECL)
3657 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3658 continue;
3660 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3662 if (TREE_CODE (arg) == TEMPLATE_DECL)
3664 /* Already substituted with real template. Just output
3665 the template name here */
3666 tree context = DECL_CONTEXT (arg);
3667 if (context)
3669 /* The template may be defined in a namespace, or
3670 may be a member template. */
3671 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3672 || CLASS_TYPE_P (context),
3673 980422);
3674 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3675 cat("::");
3677 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3679 else
3680 /* Output the parameter declaration */
3681 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3682 continue;
3684 else
3685 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3687 if (TREE_CODE (arg) == TREE_LIST)
3689 /* New list cell was built because old chain link was in
3690 use. */
3691 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3692 arg = TREE_VALUE (arg);
3694 /* No need to check arglist against parmlist here; we did that
3695 in coerce_template_parms, called from lookup_template_class. */
3696 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3699 char *bufp = obstack_next_free (&scratch_obstack);
3700 int offset = 0;
3701 while (bufp[offset - 1] == ' ')
3702 offset--;
3703 obstack_blank_fast (&scratch_obstack, offset);
3705 /* B<C<char> >, not B<C<char>> */
3706 if (bufp[offset - 1] == '>')
3707 ccat (' ');
3709 ccat ('>');
3710 ccat ('\0');
3711 return (char *) obstack_base (&scratch_obstack);
3714 static tree
3715 classtype_mangled_name (t)
3716 tree t;
3718 if (CLASSTYPE_TEMPLATE_INFO (t)
3719 /* Specializations have already had their names set up in
3720 lookup_template_class. */
3721 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3723 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3725 /* For non-primary templates, the template parameters are
3726 implicit from their surrounding context. */
3727 if (PRIMARY_TEMPLATE_P (tmpl))
3729 tree name = DECL_NAME (tmpl);
3730 char *mangled_name = mangle_class_name_for_template
3731 (IDENTIFIER_POINTER (name),
3732 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3733 CLASSTYPE_TI_ARGS (t));
3734 tree id = get_identifier (mangled_name);
3735 IDENTIFIER_TEMPLATE (id) = name;
3736 return id;
3740 return TYPE_IDENTIFIER (t);
3743 static void
3744 add_pending_template (d)
3745 tree d;
3747 tree ti = (TYPE_P (d)
3748 ? CLASSTYPE_TEMPLATE_INFO (d)
3749 : DECL_TEMPLATE_INFO (d));
3750 tree pt;
3751 int level;
3753 if (TI_PENDING_TEMPLATE_FLAG (ti))
3754 return;
3756 /* We are called both from instantiate_decl, where we've already had a
3757 tinst_level pushed, and instantiate_template, where we haven't.
3758 Compensate. */
3759 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3761 if (level)
3762 push_tinst_level (d);
3764 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3765 if (last_pending_template)
3766 TREE_CHAIN (last_pending_template) = pt;
3767 else
3768 pending_templates = pt;
3770 last_pending_template = pt;
3772 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3774 if (level)
3775 pop_tinst_level ();
3779 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3780 may be either a _DECL or an overloaded function or an
3781 IDENTIFIER_NODE), and ARGLIST. */
3783 tree
3784 lookup_template_function (fns, arglist)
3785 tree fns, arglist;
3787 tree type;
3789 if (fns == NULL_TREE)
3791 error ("non-template used as template");
3792 return error_mark_node;
3795 type = TREE_TYPE (fns);
3796 if (TREE_CODE (fns) == OVERLOAD || !type)
3797 type = unknown_type_node;
3799 if (processing_template_decl)
3800 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3801 else
3802 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3805 /* Within the scope of a template class S<T>, the name S gets bound
3806 (in build_self_reference) to a TYPE_DECL for the class, not a
3807 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3808 or one of its enclosing classes, and that type is a template,
3809 return the associated TEMPLATE_DECL. Otherwise, the original
3810 DECL is returned. */
3812 static tree
3813 maybe_get_template_decl_from_type_decl (decl)
3814 tree decl;
3816 return (decl != NULL_TREE
3817 && TREE_CODE (decl) == TYPE_DECL
3818 && DECL_ARTIFICIAL (decl)
3819 && CLASS_TYPE_P (TREE_TYPE (decl))
3820 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3821 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3824 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3825 parameters, find the desired type.
3827 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3828 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3829 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3830 otherwise.)
3832 IN_DECL, if non-NULL, is the template declaration we are trying to
3833 instantiate.
3835 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3836 the class we are looking up.
3838 Issue error and warning messages under control of COMPLAIN.
3840 If the template class is really a local class in a template
3841 function, then the FUNCTION_CONTEXT is the function in which it is
3842 being instantiated. */
3844 tree
3845 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
3846 tree d1, arglist;
3847 tree in_decl;
3848 tree context;
3849 int entering_scope;
3850 tsubst_flags_t complain;
3852 tree template = NULL_TREE, parmlist;
3853 tree t;
3855 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3857 if (IDENTIFIER_VALUE (d1)
3858 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3859 template = IDENTIFIER_VALUE (d1);
3860 else
3862 if (context)
3863 push_decl_namespace (context);
3864 template = lookup_name (d1, /*prefer_type=*/0);
3865 template = maybe_get_template_decl_from_type_decl (template);
3866 if (context)
3867 pop_decl_namespace ();
3869 if (template)
3870 context = DECL_CONTEXT (template);
3872 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3874 tree type = TREE_TYPE (d1);
3876 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3877 an implicit typename for the second A. Deal with it. */
3878 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3879 type = TREE_TYPE (type);
3881 if (CLASSTYPE_TEMPLATE_INFO (type))
3883 template = CLASSTYPE_TI_TEMPLATE (type);
3884 d1 = DECL_NAME (template);
3887 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3888 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
3890 template = TYPE_TI_TEMPLATE (d1);
3891 d1 = DECL_NAME (template);
3893 else if (TREE_CODE (d1) == TEMPLATE_DECL
3894 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
3896 template = d1;
3897 d1 = DECL_NAME (template);
3898 context = DECL_CONTEXT (template);
3901 /* With something like `template <class T> class X class X { ... };'
3902 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3903 We don't want to do that, but we have to deal with the situation,
3904 so let's give them some syntax errors to chew on instead of a
3905 crash. Alternatively D1 might not be a template type at all. */
3906 if (! template)
3908 if (complain & tf_error)
3909 error ("`%T' is not a template", d1);
3910 return error_mark_node;
3913 if (TREE_CODE (template) != TEMPLATE_DECL
3914 /* If we're called from the parser, make sure it's a user visible
3915 template. */
3916 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
3917 && !DECL_TEMPLATE_PARM_P (template)
3918 && !PRIMARY_TEMPLATE_P (template)))
3920 if (complain & tf_error)
3922 error ("non-template type `%T' used as a template", d1);
3923 if (in_decl)
3924 cp_error_at ("for template declaration `%D'", in_decl);
3926 return error_mark_node;
3929 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3931 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3932 template arguments */
3934 tree parm;
3935 tree arglist2;
3937 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3939 /* Consider an example where a template template parameter declared as
3941 template <class T, class U = std::allocator<T> > class TT
3943 The template parameter level of T and U are one level larger than
3944 of TT. To proper process the default argument of U, say when an
3945 instantiation `TT<int>' is seen, we need to build the full
3946 arguments containing {int} as the innermost level. Outer levels,
3947 available when not appearing as default template argument, can be
3948 obtained from `current_template_args ()'.
3950 Suppose that TT is later substituted with std::vector. The above
3951 instantiation is `TT<int, std::allocator<T> >' with TT at
3952 level 1, and T at level 2, while the template arguments at level 1
3953 becomes {std::vector} and the inner level 2 is {int}. */
3955 if (current_template_parms)
3956 arglist = add_to_template_args (current_template_args (), arglist);
3958 arglist2 = coerce_template_parms (parmlist, arglist, template,
3959 complain, /*require_all_args=*/1);
3960 if (arglist2 == error_mark_node)
3961 return error_mark_node;
3963 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
3964 return parm;
3966 else
3968 tree template_type = TREE_TYPE (template);
3969 tree gen_tmpl;
3970 tree type_decl;
3971 tree found = NULL_TREE;
3972 tree *tp;
3973 int arg_depth;
3974 int parm_depth;
3975 int is_partial_instantiation;
3977 gen_tmpl = most_general_template (template);
3978 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
3979 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3980 arg_depth = TMPL_ARGS_DEPTH (arglist);
3982 if (arg_depth == 1 && parm_depth > 1)
3984 /* We've been given an incomplete set of template arguments.
3985 For example, given:
3987 template <class T> struct S1 {
3988 template <class U> struct S2 {};
3989 template <class U> struct S2<U*> {};
3992 we will be called with an ARGLIST of `U*', but the
3993 TEMPLATE will be `template <class T> template
3994 <class U> struct S1<T>::S2'. We must fill in the missing
3995 arguments. */
3996 arglist
3997 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3998 arglist);
3999 arg_depth = TMPL_ARGS_DEPTH (arglist);
4002 /* Now we should have enough arguments. */
4003 my_friendly_assert (parm_depth == arg_depth, 0);
4005 /* From here on, we're only interested in the most general
4006 template. */
4007 template = gen_tmpl;
4009 /* Calculate the BOUND_ARGS. These will be the args that are
4010 actually tsubst'd into the definition to create the
4011 instantiation. */
4012 if (parm_depth > 1)
4014 /* We have multiple levels of arguments to coerce, at once. */
4015 int i;
4016 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4018 tree bound_args = make_tree_vec (parm_depth);
4020 for (i = saved_depth,
4021 t = DECL_TEMPLATE_PARMS (template);
4022 i > 0 && t != NULL_TREE;
4023 --i, t = TREE_CHAIN (t))
4025 tree a = coerce_template_parms (TREE_VALUE (t),
4026 arglist, template,
4027 complain, /*require_all_args=*/1);
4028 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4030 /* We temporarily reduce the length of the ARGLIST so
4031 that coerce_template_parms will see only the arguments
4032 corresponding to the template parameters it is
4033 examining. */
4034 TREE_VEC_LENGTH (arglist)--;
4037 /* Restore the ARGLIST to its full size. */
4038 TREE_VEC_LENGTH (arglist) = saved_depth;
4040 arglist = bound_args;
4042 else
4043 arglist
4044 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4045 INNERMOST_TEMPLATE_ARGS (arglist),
4046 template,
4047 complain, /*require_all_args=*/1);
4049 if (arglist == error_mark_node)
4050 /* We were unable to bind the arguments. */
4051 return error_mark_node;
4053 /* In the scope of a template class, explicit references to the
4054 template class refer to the type of the template, not any
4055 instantiation of it. For example, in:
4057 template <class T> class C { void f(C<T>); }
4059 the `C<T>' is just the same as `C'. Outside of the
4060 class, however, such a reference is an instantiation. */
4061 if (comp_template_args (TYPE_TI_ARGS (template_type),
4062 arglist))
4064 found = template_type;
4066 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4068 tree ctx;
4070 /* Note that we use DECL_CONTEXT, rather than
4071 CP_DECL_CONTEXT, so that the termination test is
4072 always just `ctx'. We're not interested in namespace
4073 scopes. */
4074 for (ctx = current_class_type;
4075 ctx;
4076 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4077 if (same_type_p (ctx, template_type))
4078 break;
4080 if (!ctx)
4081 /* We're not in the scope of the class, so the
4082 TEMPLATE_TYPE is not the type we want after
4083 all. */
4084 found = NULL_TREE;
4087 if (found)
4088 return found;
4090 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4091 *tp;
4092 tp = &TREE_CHAIN (*tp))
4093 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4095 found = *tp;
4097 /* Use the move-to-front heuristic to speed up future
4098 searches. */
4099 *tp = TREE_CHAIN (*tp);
4100 TREE_CHAIN (found)
4101 = DECL_TEMPLATE_INSTANTIATIONS (template);
4102 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4104 return TREE_VALUE (found);
4107 /* This type is a "partial instantiation" if any of the template
4108 arguments still involve template parameters. Note that we set
4109 IS_PARTIAL_INSTANTIATION for partial specializations as
4110 well. */
4111 is_partial_instantiation = uses_template_parms (arglist);
4113 if (!is_partial_instantiation
4114 && !PRIMARY_TEMPLATE_P (template)
4115 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4117 found = xref_tag_from_type (TREE_TYPE (template),
4118 DECL_NAME (template),
4119 /*globalize=*/1);
4120 return found;
4123 context = tsubst (DECL_CONTEXT (template), arglist,
4124 complain, in_decl);
4125 if (!context)
4126 context = global_namespace;
4128 /* Create the type. */
4129 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4131 if (!is_partial_instantiation)
4133 set_current_access_from_decl (TYPE_NAME (template_type));
4134 t = start_enum (TYPE_IDENTIFIER (template_type));
4136 else
4137 /* We don't want to call start_enum for this type, since
4138 the values for the enumeration constants may involve
4139 template parameters. And, no one should be interested
4140 in the enumeration constants for such a type. */
4141 t = make_node (ENUMERAL_TYPE);
4143 else
4145 t = make_aggr_type (TREE_CODE (template_type));
4146 CLASSTYPE_DECLARED_CLASS (t)
4147 = CLASSTYPE_DECLARED_CLASS (template_type);
4148 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4149 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4150 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4152 /* A local class. Make sure the decl gets registered properly. */
4153 if (context == current_function_decl)
4154 pushtag (DECL_NAME (template), t, 0);
4157 /* If we called start_enum or pushtag above, this information
4158 will already be set up. */
4159 if (!TYPE_NAME (t))
4161 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4163 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4164 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4165 TYPE_STUB_DECL (t) = type_decl;
4166 DECL_SOURCE_FILE (type_decl)
4167 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4168 DECL_SOURCE_LINE (type_decl)
4169 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
4171 else
4172 type_decl = TYPE_NAME (t);
4174 /* Set up the template information. We have to figure out which
4175 template is the immediate parent if this is a full
4176 instantiation. */
4177 if (parm_depth == 1 || is_partial_instantiation
4178 || !PRIMARY_TEMPLATE_P (template))
4179 /* This case is easy; there are no member templates involved. */
4180 found = template;
4181 else
4183 /* This is a full instantiation of a member template. Look
4184 for a partial instantiation of which this is an instance. */
4186 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4187 found; found = TREE_CHAIN (found))
4189 int success;
4190 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4192 /* We only want partial instantiations, here, not
4193 specializations or full instantiations. */
4194 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4195 || !uses_template_parms (TREE_VALUE (found)))
4196 continue;
4198 /* Temporarily reduce by one the number of levels in the
4199 ARGLIST and in FOUND so as to avoid comparing the
4200 last set of arguments. */
4201 TREE_VEC_LENGTH (arglist)--;
4202 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4204 /* See if the arguments match. If they do, then TMPL is
4205 the partial instantiation we want. */
4206 success = comp_template_args (TREE_PURPOSE (found), arglist);
4208 /* Restore the argument vectors to their full size. */
4209 TREE_VEC_LENGTH (arglist)++;
4210 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4212 if (success)
4214 found = tmpl;
4215 break;
4219 if (!found)
4221 /* There was no partial instantiation. This happens
4222 where C<T> is a member template of A<T> and it's used
4223 in something like
4225 template <typename T> struct B { A<T>::C<int> m; };
4226 B<float>;
4228 Create the partial instantiation.
4230 TREE_VEC_LENGTH (arglist)--;
4231 found = tsubst (template, arglist, complain, NULL_TREE);
4232 TREE_VEC_LENGTH (arglist)++;
4236 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4237 DECL_TEMPLATE_INSTANTIATIONS (template)
4238 = tree_cons (arglist, t,
4239 DECL_TEMPLATE_INSTANTIATIONS (template));
4241 if (TREE_CODE (t) == ENUMERAL_TYPE
4242 && !is_partial_instantiation)
4243 /* Now that the type has been registered on the instantiations
4244 list, we set up the enumerators. Because the enumeration
4245 constants may involve the enumeration type itself, we make
4246 sure to register the type first, and then create the
4247 constants. That way, doing tsubst_expr for the enumeration
4248 constants won't result in recursive calls here; we'll find
4249 the instantiation and exit above. */
4250 tsubst_enum (template_type, t, arglist);
4252 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4253 is set up. */
4254 if (TREE_CODE (t) != ENUMERAL_TYPE)
4255 DECL_NAME (type_decl) = classtype_mangled_name (t);
4256 if (!is_partial_instantiation)
4258 /* For backwards compatibility; code that uses
4259 -fexternal-templates expects looking up a template to
4260 instantiate it. I think DDD still relies on this.
4261 (jason 8/20/1998) */
4262 if (TREE_CODE (t) != ENUMERAL_TYPE
4263 && flag_external_templates
4264 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4265 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4266 add_pending_template (t);
4268 else
4269 /* If the type makes use of template parameters, the
4270 code that generates debugging information will crash. */
4271 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4273 return t;
4277 struct pair_fn_data
4279 tree_fn_t fn;
4280 void *data;
4283 /* Called from for_each_template_parm via walk_tree. */
4285 static tree
4286 for_each_template_parm_r (tp, walk_subtrees, d)
4287 tree *tp;
4288 int *walk_subtrees;
4289 void *d;
4291 tree t = *tp;
4292 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4293 tree_fn_t fn = pfd->fn;
4294 void *data = pfd->data;
4296 if (TYPE_P (t)
4297 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4298 return error_mark_node;
4300 switch (TREE_CODE (t))
4302 case RECORD_TYPE:
4303 if (TYPE_PTRMEMFUNC_P (t))
4304 break;
4305 /* Fall through. */
4307 case UNION_TYPE:
4308 case ENUMERAL_TYPE:
4309 if (!TYPE_TEMPLATE_INFO (t))
4310 *walk_subtrees = 0;
4311 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4312 fn, data))
4313 return error_mark_node;
4314 break;
4316 case METHOD_TYPE:
4317 /* Since we're not going to walk subtrees, we have to do this
4318 explicitly here. */
4319 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
4320 return error_mark_node;
4321 /* Fall through. */
4323 case FUNCTION_TYPE:
4324 /* Check the return type. */
4325 if (for_each_template_parm (TREE_TYPE (t), fn, data))
4326 return error_mark_node;
4328 /* Check the parameter types. Since default arguments are not
4329 instantiated until they are needed, the TYPE_ARG_TYPES may
4330 contain expressions that involve template parameters. But,
4331 no-one should be looking at them yet. And, once they're
4332 instantiated, they don't contain template parameters, so
4333 there's no point in looking at them then, either. */
4335 tree parm;
4337 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4338 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4339 return error_mark_node;
4341 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4342 want walk_tree walking into them itself. */
4343 *walk_subtrees = 0;
4345 break;
4347 case FUNCTION_DECL:
4348 case VAR_DECL:
4349 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4350 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
4351 return error_mark_node;
4352 /* Fall through. */
4354 case CONST_DECL:
4355 case PARM_DECL:
4356 if (DECL_CONTEXT (t)
4357 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
4358 return error_mark_node;
4359 break;
4361 case BOUND_TEMPLATE_TEMPLATE_PARM:
4362 /* Record template parameters such as `T' inside `TT<T>'. */
4363 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
4364 return error_mark_node;
4365 /* Fall through. */
4367 case TEMPLATE_TEMPLATE_PARM:
4368 case TEMPLATE_TYPE_PARM:
4369 case TEMPLATE_PARM_INDEX:
4370 if (fn && (*fn)(t, data))
4371 return error_mark_node;
4372 else if (!fn)
4373 return error_mark_node;
4374 break;
4376 case TEMPLATE_DECL:
4377 /* A template template parameter is encountered */
4378 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4379 && for_each_template_parm (TREE_TYPE (t), fn, data))
4380 return error_mark_node;
4382 /* Already substituted template template parameter */
4383 *walk_subtrees = 0;
4384 break;
4386 case TYPENAME_TYPE:
4387 if (!fn || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, data))
4388 return error_mark_node;
4389 break;
4391 case CONSTRUCTOR:
4392 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4393 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4394 (TREE_TYPE (t)), fn, data))
4395 return error_mark_node;
4396 break;
4398 case INDIRECT_REF:
4399 case COMPONENT_REF:
4400 /* If there's no type, then this thing must be some expression
4401 involving template parameters. */
4402 if (!fn && !TREE_TYPE (t))
4403 return error_mark_node;
4404 break;
4406 case MODOP_EXPR:
4407 case CAST_EXPR:
4408 case REINTERPRET_CAST_EXPR:
4409 case CONST_CAST_EXPR:
4410 case STATIC_CAST_EXPR:
4411 case DYNAMIC_CAST_EXPR:
4412 case ARROW_EXPR:
4413 case DOTSTAR_EXPR:
4414 case TYPEID_EXPR:
4415 case LOOKUP_EXPR:
4416 case PSEUDO_DTOR_EXPR:
4417 if (!fn)
4418 return error_mark_node;
4419 break;
4421 default:
4422 break;
4425 /* We didn't find any template parameters we liked. */
4426 return NULL_TREE;
4429 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4430 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4431 call FN with the parameter and the DATA.
4432 If FN returns non-zero, the iteration is terminated, and
4433 for_each_template_parm returns 1. Otherwise, the iteration
4434 continues. If FN never returns a non-zero value, the value
4435 returned by for_each_template_parm is 0. If FN is NULL, it is
4436 considered to be the function which always returns 1. */
4438 static int
4439 for_each_template_parm (t, fn, data)
4440 tree t;
4441 tree_fn_t fn;
4442 void* data;
4444 struct pair_fn_data pfd;
4446 /* Set up. */
4447 pfd.fn = fn;
4448 pfd.data = data;
4450 /* Walk the tree. (Conceptually, we would like to walk without
4451 duplicates, but for_each_template_parm_r recursively calls
4452 for_each_template_parm, so we would need to reorganize a fair
4453 bit to use walk_tree_without_duplicates.) */
4454 return walk_tree (&t,
4455 for_each_template_parm_r,
4456 &pfd,
4457 NULL) != NULL_TREE;
4461 uses_template_parms (t)
4462 tree t;
4464 return for_each_template_parm (t, 0, 0);
4467 static int tinst_depth;
4468 extern int max_tinst_depth;
4469 #ifdef GATHER_STATISTICS
4470 int depth_reached;
4471 #endif
4472 static int tinst_level_tick;
4473 static int last_template_error_tick;
4475 /* We're starting to instantiate D; record the template instantiation context
4476 for diagnostics and to restore it later. */
4479 push_tinst_level (d)
4480 tree d;
4482 tree new;
4484 if (tinst_depth >= max_tinst_depth)
4486 /* If the instantiation in question still has unbound template parms,
4487 we don't really care if we can't instantiate it, so just return.
4488 This happens with base instantiation for implicit `typename'. */
4489 if (uses_template_parms (d))
4490 return 0;
4492 last_template_error_tick = tinst_level_tick;
4493 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4494 max_tinst_depth, d);
4496 print_instantiation_context ();
4498 return 0;
4501 new = build_expr_wfl (d, input_filename, lineno, 0);
4502 TREE_CHAIN (new) = current_tinst_level;
4503 current_tinst_level = new;
4505 ++tinst_depth;
4506 #ifdef GATHER_STATISTICS
4507 if (tinst_depth > depth_reached)
4508 depth_reached = tinst_depth;
4509 #endif
4511 ++tinst_level_tick;
4512 return 1;
4515 /* We're done instantiating this template; return to the instantiation
4516 context. */
4518 void
4519 pop_tinst_level ()
4521 tree old = current_tinst_level;
4523 /* Restore the filename and line number stashed away when we started
4524 this instantiation. */
4525 lineno = TINST_LINE (old);
4526 input_filename = TINST_FILE (old);
4527 extract_interface_info ();
4529 current_tinst_level = TREE_CHAIN (old);
4530 --tinst_depth;
4531 ++tinst_level_tick;
4534 /* We're instantiating a deferred template; restore the template
4535 instantiation context in which the instantiation was requested, which
4536 is one step out from LEVEL. */
4538 static void
4539 reopen_tinst_level (level)
4540 tree level;
4542 tree t;
4544 tinst_depth = 0;
4545 for (t = level; t; t = TREE_CHAIN (t))
4546 ++tinst_depth;
4548 current_tinst_level = level;
4549 pop_tinst_level ();
4552 /* Return the outermost template instantiation context, for use with
4553 -falt-external-templates. */
4555 tree
4556 tinst_for_decl ()
4558 tree p = current_tinst_level;
4560 if (p)
4561 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4563 return p;
4566 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4567 vector of template arguments, as for tsubst.
4569 Returns an appropriate tsubst'd friend declaration. */
4571 static tree
4572 tsubst_friend_function (decl, args)
4573 tree decl;
4574 tree args;
4576 tree new_friend;
4577 int line = lineno;
4578 const char *file = input_filename;
4580 lineno = DECL_SOURCE_LINE (decl);
4581 input_filename = DECL_SOURCE_FILE (decl);
4583 if (TREE_CODE (decl) == FUNCTION_DECL
4584 && DECL_TEMPLATE_INSTANTIATION (decl)
4585 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4586 /* This was a friend declared with an explicit template
4587 argument list, e.g.:
4589 friend void f<>(T);
4591 to indicate that f was a template instantiation, not a new
4592 function declaration. Now, we have to figure out what
4593 instantiation of what template. */
4595 tree template_id, arglist, fns;
4596 tree new_args;
4597 tree tmpl;
4598 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
4600 /* Friend functions are looked up in the containing namespace scope.
4601 We must enter that scope, to avoid finding member functions of the
4602 current cless with same name. */
4603 push_nested_namespace (ns);
4604 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4605 tf_error | tf_warning, NULL_TREE);
4606 pop_nested_namespace (ns);
4607 arglist = tsubst (DECL_TI_ARGS (decl), args,
4608 tf_error | tf_warning, NULL_TREE);
4609 template_id = lookup_template_function (fns, arglist);
4611 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4612 tmpl = determine_specialization (template_id, new_friend,
4613 &new_args,
4614 /*need_member_template=*/0);
4615 new_friend = instantiate_template (tmpl, new_args);
4616 goto done;
4619 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4621 /* The NEW_FRIEND will look like an instantiation, to the
4622 compiler, but is not an instantiation from the point of view of
4623 the language. For example, we might have had:
4625 template <class T> struct S {
4626 template <class U> friend void f(T, U);
4629 Then, in S<int>, template <class U> void f(int, U) is not an
4630 instantiation of anything. */
4631 DECL_USE_TEMPLATE (new_friend) = 0;
4632 if (TREE_CODE (decl) == TEMPLATE_DECL)
4634 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4635 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4636 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4639 /* The mangled name for the NEW_FRIEND is incorrect. The function
4640 is not a template instantiation and should not be mangled like
4641 one. Therefore, we forget the mangling here; we'll recompute it
4642 later if we need it. */
4643 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4645 SET_DECL_RTL (new_friend, NULL_RTX);
4646 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4649 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4651 tree old_decl;
4652 tree new_friend_template_info;
4653 tree new_friend_result_template_info;
4654 tree ns;
4655 int new_friend_is_defn;
4657 /* We must save some information from NEW_FRIEND before calling
4658 duplicate decls since that function will free NEW_FRIEND if
4659 possible. */
4660 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4661 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4663 /* This declaration is a `primary' template. */
4664 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4666 new_friend_is_defn
4667 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4668 new_friend_result_template_info
4669 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4671 else
4673 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4674 new_friend_result_template_info = NULL_TREE;
4677 /* Inside pushdecl_namespace_level, we will push into the
4678 current namespace. However, the friend function should go
4679 into the namespace of the template. */
4680 ns = decl_namespace_context (new_friend);
4681 push_nested_namespace (ns);
4682 old_decl = pushdecl_namespace_level (new_friend);
4683 pop_nested_namespace (ns);
4685 if (old_decl != new_friend)
4687 /* This new friend declaration matched an existing
4688 declaration. For example, given:
4690 template <class T> void f(T);
4691 template <class U> class C {
4692 template <class T> friend void f(T) {}
4695 the friend declaration actually provides the definition
4696 of `f', once C has been instantiated for some type. So,
4697 old_decl will be the out-of-class template declaration,
4698 while new_friend is the in-class definition.
4700 But, if `f' was called before this point, the
4701 instantiation of `f' will have DECL_TI_ARGS corresponding
4702 to `T' but not to `U', references to which might appear
4703 in the definition of `f'. Previously, the most general
4704 template for an instantiation of `f' was the out-of-class
4705 version; now it is the in-class version. Therefore, we
4706 run through all specialization of `f', adding to their
4707 DECL_TI_ARGS appropriately. In particular, they need a
4708 new set of outer arguments, corresponding to the
4709 arguments for this class instantiation.
4711 The same situation can arise with something like this:
4713 friend void f(int);
4714 template <class T> class C {
4715 friend void f(T) {}
4718 when `C<int>' is instantiated. Now, `f(int)' is defined
4719 in the class. */
4721 if (!new_friend_is_defn)
4722 /* On the other hand, if the in-class declaration does
4723 *not* provide a definition, then we don't want to alter
4724 existing definitions. We can just leave everything
4725 alone. */
4727 else
4729 /* Overwrite whatever template info was there before, if
4730 any, with the new template information pertaining to
4731 the declaration. */
4732 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4734 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4735 /* duplicate_decls will take care of this case. */
4737 else
4739 tree t;
4740 tree new_friend_args;
4742 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4743 = new_friend_result_template_info;
4745 new_friend_args = TI_ARGS (new_friend_template_info);
4746 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4747 t != NULL_TREE;
4748 t = TREE_CHAIN (t))
4750 tree spec = TREE_VALUE (t);
4752 DECL_TI_ARGS (spec)
4753 = add_outermost_template_args (new_friend_args,
4754 DECL_TI_ARGS (spec));
4757 /* Now, since specializations are always supposed to
4758 hang off of the most general template, we must move
4759 them. */
4760 t = most_general_template (old_decl);
4761 if (t != old_decl)
4763 DECL_TEMPLATE_SPECIALIZATIONS (t)
4764 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4765 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4766 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4771 /* The information from NEW_FRIEND has been merged into OLD_DECL
4772 by duplicate_decls. */
4773 new_friend = old_decl;
4776 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
4778 /* Check to see that the declaration is really present, and,
4779 possibly obtain an improved declaration. */
4780 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4781 new_friend);
4783 if (fn)
4784 new_friend = fn;
4787 done:
4788 lineno = line;
4789 input_filename = file;
4790 return new_friend;
4793 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4794 template arguments, as for tsubst.
4796 Returns an appropriate tsubst'd friend type or error_mark_node on
4797 failure. */
4799 static tree
4800 tsubst_friend_class (friend_tmpl, args)
4801 tree friend_tmpl;
4802 tree args;
4804 tree friend_type;
4805 tree tmpl;
4806 tree context;
4808 context = DECL_CONTEXT (friend_tmpl);
4810 if (context)
4812 if (TREE_CODE (context) == NAMESPACE_DECL)
4813 push_nested_namespace (context);
4814 else
4815 push_nested_class (tsubst (context, args, tf_none, NULL_TREE), 2);
4818 /* First, we look for a class template. */
4819 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4821 /* But, if we don't find one, it might be because we're in a
4822 situation like this:
4824 template <class T>
4825 struct S {
4826 template <class U>
4827 friend struct S;
4830 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4831 for `S<int>', not the TEMPLATE_DECL. */
4832 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4834 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4835 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4838 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
4840 /* The friend template has already been declared. Just
4841 check to see that the declarations match, and install any new
4842 default parameters. We must tsubst the default parameters,
4843 of course. We only need the innermost template parameters
4844 because that is all that redeclare_class_template will look
4845 at. */
4846 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
4847 > TMPL_ARGS_DEPTH (args))
4849 tree parms;
4850 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4851 args, tf_error | tf_warning);
4852 redeclare_class_template (TREE_TYPE (tmpl), parms);
4855 friend_type = TREE_TYPE (tmpl);
4857 else
4859 /* The friend template has not already been declared. In this
4860 case, the instantiation of the template class will cause the
4861 injection of this template into the global scope. */
4862 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
4864 /* The new TMPL is not an instantiation of anything, so we
4865 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4866 the new type because that is supposed to be the corresponding
4867 template decl, i.e., TMPL. */
4868 DECL_USE_TEMPLATE (tmpl) = 0;
4869 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4870 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4872 /* Inject this template into the global scope. */
4873 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4876 if (context)
4878 if (TREE_CODE (context) == NAMESPACE_DECL)
4879 pop_nested_namespace (context);
4880 else
4881 pop_nested_class ();
4884 return friend_type;
4887 /* Returns zero if TYPE cannot be completed later due to circularity.
4888 Otherwise returns one. */
4890 static int
4891 can_complete_type_without_circularity (type)
4892 tree type;
4894 if (type == NULL_TREE || type == error_mark_node)
4895 return 0;
4896 else if (COMPLETE_TYPE_P (type))
4897 return 1;
4898 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
4899 return can_complete_type_without_circularity (TREE_TYPE (type));
4900 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
4901 return 0;
4902 else
4903 return 1;
4906 tree
4907 instantiate_class_template (type)
4908 tree type;
4910 tree template, args, pattern, t;
4911 tree typedecl;
4913 if (type == error_mark_node)
4914 return error_mark_node;
4916 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
4917 return type;
4919 /* Figure out which template is being instantiated. */
4920 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4921 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4923 /* Figure out which arguments are being used to do the
4924 instantiation. */
4925 args = CLASSTYPE_TI_ARGS (type);
4926 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
4928 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4929 /* If this is a partial instantiation, then we can't instantiate
4930 the type; there's no telling whether or not one of the
4931 template parameters might eventually be instantiated to some
4932 value that results in a specialization being used. For
4933 example, consider:
4935 template <class T>
4936 struct S {};
4938 template <class U>
4939 void f(S<U>);
4941 template <>
4942 struct S<int> {};
4944 Now, the `S<U>' in `f<int>' is the specialization, not an
4945 instantiation of the original template. */
4946 return type;
4948 /* Determine what specialization of the original template to
4949 instantiate. */
4950 if (PARTIAL_INSTANTIATION_P (type))
4951 /* There's no telling which specialization is appropriate at this
4952 point. Since all peeking at the innards of this partial
4953 instantiation are extensions (like the "implicit typename"
4954 extension, which allows users to omit the keyword `typename' on
4955 names that are declared as types in template base classes), we
4956 are free to do what we please.
4958 Trying to figure out which partial instantiation to use can
4959 cause a crash. (Some of the template arguments don't even have
4960 types.) So, we just use the most general version. */
4961 t = NULL_TREE;
4962 else
4964 t = most_specialized_class (template, args);
4966 if (t == error_mark_node)
4968 const char *str = "candidates are:";
4969 error ("ambiguous class template instantiation for `%#T'", type);
4970 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4971 t = TREE_CHAIN (t))
4973 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4974 args))
4976 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4977 str = " ";
4980 TYPE_BEING_DEFINED (type) = 1;
4981 return error_mark_node;
4985 if (t)
4986 pattern = TREE_TYPE (t);
4987 else
4988 pattern = TREE_TYPE (template);
4990 /* If the template we're instantiating is incomplete, then clearly
4991 there's nothing we can do. */
4992 if (!COMPLETE_TYPE_P (pattern))
4993 return type;
4995 /* If this is a partial instantiation, don't tsubst anything. We will
4996 only use this type for implicit typename, so the actual contents don't
4997 matter. All that matters is whether a particular name is a type. */
4998 if (PARTIAL_INSTANTIATION_P (type))
5000 /* The fields set here must be kept in sync with those cleared
5001 in begin_class_definition. */
5002 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
5003 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
5004 TYPE_METHODS (type) = TYPE_METHODS (pattern);
5005 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
5006 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
5008 /* Pretend that the type is complete, so that we will look
5009 inside it during name lookup and such. */
5010 TYPE_SIZE (type) = bitsize_zero_node;
5011 return type;
5014 /* If we've recursively instantiated too many templates, stop. */
5015 if (! push_tinst_level (type))
5016 return type;
5018 /* Now we're really doing the instantiation. Mark the type as in
5019 the process of being defined. */
5020 TYPE_BEING_DEFINED (type) = 1;
5022 maybe_push_to_top_level (uses_template_parms (type));
5024 if (t)
5026 /* This TYPE is actually a instantiation of of a partial
5027 specialization. We replace the innermost set of ARGS with
5028 the arguments appropriate for substitution. For example,
5029 given:
5031 template <class T> struct S {};
5032 template <class T> struct S<T*> {};
5034 and supposing that we are instantiating S<int*>, ARGS will
5035 present be {int*} but we need {int}. */
5036 tree inner_args
5037 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5038 args);
5040 /* If there were multiple levels in ARGS, replacing the
5041 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5042 want, so we make a copy first. */
5043 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5045 args = copy_node (args);
5046 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5048 else
5049 args = inner_args;
5052 if (flag_external_templates)
5054 if (flag_alt_external_templates)
5056 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5057 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5059 else
5061 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5062 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5063 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5066 else
5068 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5071 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5072 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5073 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5074 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5075 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5076 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5077 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5078 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5079 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5080 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5081 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5082 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5083 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5084 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5085 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5086 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5087 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5088 TYPE_USES_MULTIPLE_INHERITANCE (type)
5089 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5090 TYPE_USES_VIRTUAL_BASECLASSES (type)
5091 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5092 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5093 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5094 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5095 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5096 if (ANON_AGGR_TYPE_P (pattern))
5097 SET_ANON_AGGR_TYPE_P (type);
5099 if (TYPE_BINFO_BASETYPES (pattern))
5101 tree base_list = NULL_TREE;
5102 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5103 int i;
5105 /* Substitute into each of the bases to determine the actual
5106 basetypes. */
5107 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5109 tree base;
5110 tree access;
5111 tree pbase;
5113 pbase = TREE_VEC_ELT (pbases, i);
5115 /* Substitute to figure out the base class. */
5116 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5117 if (base == error_mark_node)
5118 continue;
5120 /* Calculate the correct access node. */
5121 if (TREE_VIA_VIRTUAL (pbase))
5123 if (TREE_VIA_PUBLIC (pbase))
5124 access = access_public_virtual_node;
5125 else if (TREE_VIA_PROTECTED (pbase))
5126 access = access_protected_virtual_node;
5127 else
5128 access = access_private_virtual_node;
5130 else
5132 if (TREE_VIA_PUBLIC (pbase))
5133 access = access_public_node;
5134 else if (TREE_VIA_PROTECTED (pbase))
5135 access = access_protected_node;
5136 else
5137 access = access_private_node;
5140 base_list = tree_cons (access, base, base_list);
5143 /* The list is now in reverse order; correct that. */
5144 base_list = nreverse (base_list);
5146 /* Now call xref_basetypes to set up all the base-class
5147 information. */
5148 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5149 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5150 ? class_type_node : record_type_node)
5151 : union_type_node,
5152 DECL_NAME (TYPE_NAME (pattern)),
5153 type,
5154 base_list);
5157 /* Now that our base classes are set up, enter the scope of the
5158 class, so that name lookups into base classes, etc. will work
5159 correctly. This is precisely analogous to what we do in
5160 begin_class_definition when defining an ordinary non-template
5161 class. */
5162 pushclass (type, 1);
5164 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
5166 tree tag = TREE_VALUE (t);
5167 tree name = TYPE_IDENTIFIER (tag);
5168 tree newtag;
5170 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5171 my_friendly_assert (newtag != error_mark_node, 20010206);
5172 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5174 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5175 /* Unfortunately, lookup_template_class sets
5176 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5177 instantiation (i.e., for the type of a member template
5178 class nested within a template class.) This behavior is
5179 required for maybe_process_partial_specialization to work
5180 correctly, but is not accurate in this case; the TAG is not
5181 an instantiation of anything. (The corresponding
5182 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5183 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5185 /* Now, we call pushtag to put this NEWTAG into the scope of
5186 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5187 pushtag calling push_template_decl. We don't have to do
5188 this for enums because it will already have been done in
5189 tsubst_enum. */
5190 if (name)
5191 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5192 pushtag (name, newtag, /*globalize=*/0);
5196 /* Don't replace enum constants here. */
5197 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
5198 if (TREE_CODE (t) != CONST_DECL)
5200 tree r;
5202 /* The the file and line for this declaration, to assist in
5203 error message reporting. Since we called push_tinst_level
5204 above, we don't need to restore these. */
5205 lineno = DECL_SOURCE_LINE (t);
5206 input_filename = DECL_SOURCE_FILE (t);
5208 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5209 if (TREE_CODE (r) == VAR_DECL)
5211 tree init;
5213 if (DECL_INITIALIZED_IN_CLASS_P (r))
5214 init = tsubst_expr (DECL_INITIAL (t), args,
5215 tf_error | tf_warning, NULL_TREE);
5216 else
5217 init = NULL_TREE;
5219 finish_static_data_member_decl (r, init,
5220 /*asmspec_tree=*/NULL_TREE,
5221 /*flags=*/0);
5223 if (DECL_INITIALIZED_IN_CLASS_P (r))
5224 check_static_variable_definition (r, TREE_TYPE (r));
5226 else if (TREE_CODE (r) == FIELD_DECL)
5228 /* Determine whether R has a valid type and can be
5229 completed later. If R is invalid, then it is replaced
5230 by error_mark_node so that it will not be added to
5231 TYPE_FIELDS. */
5232 tree rtype = TREE_TYPE (r);
5233 if (!can_complete_type_without_circularity (rtype))
5235 incomplete_type_error (r, rtype);
5236 r = error_mark_node;
5240 /* R will have a TREE_CHAIN if and only if it has already been
5241 processed by finish_member_declaration. This can happen
5242 if, for example, it is a TYPE_DECL for a class-scoped
5243 ENUMERAL_TYPE; such a thing will already have been added to
5244 the field list by tsubst_enum above. */
5245 if (!TREE_CHAIN (r))
5247 set_current_access_from_decl (r);
5248 finish_member_declaration (r);
5252 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5253 for this instantiation. */
5254 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5256 tree r = tsubst (t, args, tf_error, NULL_TREE);
5257 set_current_access_from_decl (r);
5258 grok_special_member_properties (r);
5259 finish_member_declaration (r);
5262 /* Construct the DECL_FRIENDLIST for the new class type. */
5263 typedecl = TYPE_MAIN_DECL (type);
5264 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5265 t != NULL_TREE;
5266 t = TREE_CHAIN (t))
5268 tree friends;
5270 for (friends = TREE_VALUE (t);
5271 friends != NULL_TREE;
5272 friends = TREE_CHAIN (friends))
5273 if (TREE_PURPOSE (friends) == error_mark_node)
5274 add_friend (type,
5275 tsubst_friend_function (TREE_VALUE (friends),
5276 args));
5277 else
5278 abort ();
5281 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5282 t != NULL_TREE;
5283 t = TREE_CHAIN (t))
5285 tree friend_type = TREE_VALUE (t);
5286 tree new_friend_type;
5288 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5289 new_friend_type = tsubst_friend_class (friend_type, args);
5290 else if (uses_template_parms (friend_type))
5291 new_friend_type = tsubst (friend_type, args,
5292 tf_error | tf_warning, NULL_TREE);
5293 else
5295 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5297 /* The call to xref_tag_from_type does injection for friend
5298 classes. */
5299 push_nested_namespace (ns);
5300 new_friend_type =
5301 xref_tag_from_type (friend_type, NULL_TREE, 1);
5302 pop_nested_namespace (ns);
5305 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5306 /* Trick make_friend_class into realizing that the friend
5307 we're adding is a template, not an ordinary class. It's
5308 important that we use make_friend_class since it will
5309 perform some error-checking and output cross-reference
5310 information. */
5311 ++processing_template_decl;
5313 if (new_friend_type != error_mark_node)
5314 make_friend_class (type, new_friend_type);
5316 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5317 --processing_template_decl;
5320 /* Now that TYPE_FIELDS and TYPE_METHODS are set up. We can
5321 instantiate templates used by this class. */
5322 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5323 if (TREE_CODE (t) == FIELD_DECL)
5325 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5326 require_complete_type (t);
5329 /* Set the file and line number information to whatever is given for
5330 the class itself. This puts error messages involving generated
5331 implicit functions at a predictable point, and the same point
5332 that would be used for non-template classes. */
5333 lineno = DECL_SOURCE_LINE (typedecl);
5334 input_filename = DECL_SOURCE_FILE (typedecl);
5336 unreverse_member_declarations (type);
5337 finish_struct_1 (type);
5338 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5340 /* Clear this now so repo_template_used is happy. */
5341 TYPE_BEING_DEFINED (type) = 0;
5342 repo_template_used (type);
5344 /* Now that the class is complete, instantiate default arguments for
5345 any member functions. We don't do this earlier because the
5346 default arguments may reference members of the class. */
5347 if (!PRIMARY_TEMPLATE_P (template))
5348 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5349 if (TREE_CODE (t) == FUNCTION_DECL
5350 /* Implicitly generated member functions will not have template
5351 information; they are not instantiations, but instead are
5352 created "fresh" for each instantiation. */
5353 && DECL_TEMPLATE_INFO (t))
5354 tsubst_default_arguments (t);
5356 popclass ();
5357 pop_from_top_level ();
5358 pop_tinst_level ();
5360 return type;
5363 static int
5364 list_eq (t1, t2)
5365 tree t1, t2;
5367 if (t1 == NULL_TREE)
5368 return t2 == NULL_TREE;
5369 if (t2 == NULL_TREE)
5370 return 0;
5371 /* Don't care if one declares its arg const and the other doesn't -- the
5372 main variant of the arg type is all that matters. */
5373 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5374 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5375 return 0;
5376 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5379 /* If arg is a non-type template parameter that does not depend on template
5380 arguments, fold it like we weren't in the body of a template. */
5382 static tree
5383 maybe_fold_nontype_arg (arg)
5384 tree arg;
5386 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5388 /* Sometimes, one of the args was an expression involving a
5389 template constant parameter, like N - 1. Now that we've
5390 tsubst'd, we might have something like 2 - 1. This will
5391 confuse lookup_template_class, so we do constant folding
5392 here. We have to unset processing_template_decl, to
5393 fool build_expr_from_tree() into building an actual
5394 tree. */
5396 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5397 as simple as it's going to get, and trying to reprocess
5398 the trees will break. */
5399 if (!TREE_TYPE (arg))
5401 int saved_processing_template_decl = processing_template_decl;
5402 processing_template_decl = 0;
5403 arg = build_expr_from_tree (arg);
5404 processing_template_decl = saved_processing_template_decl;
5407 arg = fold (arg);
5409 return arg;
5412 /* Substitute ARGS into the vector of template arguments T. */
5414 static tree
5415 tsubst_template_arg_vector (t, args, complain)
5416 tree t;
5417 tree args;
5418 tsubst_flags_t complain;
5420 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5421 tree *elts = (tree *) alloca (len * sizeof (tree));
5423 memset ((char *) elts, 0, len * sizeof (tree));
5425 for (i = 0; i < len; i++)
5427 if (TREE_VEC_ELT (t, i) != NULL_TREE
5428 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5429 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5430 args, complain);
5431 else
5432 elts[i] = maybe_fold_nontype_arg
5433 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5434 NULL_TREE));
5436 if (elts[i] == error_mark_node)
5437 return error_mark_node;
5439 if (elts[i] != TREE_VEC_ELT (t, i))
5440 need_new = 1;
5443 if (!need_new)
5444 return t;
5446 t = make_tree_vec (len);
5447 for (i = 0; i < len; i++)
5448 TREE_VEC_ELT (t, i) = elts[i];
5450 return t;
5453 /* Return the result of substituting ARGS into the template parameters
5454 given by PARMS. If there are m levels of ARGS and m + n levels of
5455 PARMS, then the result will contain n levels of PARMS. For
5456 example, if PARMS is `template <class T> template <class U>
5457 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5458 result will be `template <int*, double, class V>'. */
5460 static tree
5461 tsubst_template_parms (parms, args, complain)
5462 tree parms;
5463 tree args;
5464 tsubst_flags_t complain;
5466 tree r = NULL_TREE;
5467 tree* new_parms;
5469 for (new_parms = &r;
5470 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5471 new_parms = &(TREE_CHAIN (*new_parms)),
5472 parms = TREE_CHAIN (parms))
5474 tree new_vec =
5475 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5476 int i;
5478 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5480 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5481 tree default_value = TREE_PURPOSE (tuple);
5482 tree parm_decl = TREE_VALUE (tuple);
5484 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5485 default_value = tsubst_expr (default_value, args,
5486 complain, NULL_TREE);
5487 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5488 parm_decl);
5489 TREE_VEC_ELT (new_vec, i) = tuple;
5492 *new_parms =
5493 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5494 - TMPL_ARGS_DEPTH (args)),
5495 new_vec, NULL_TREE);
5498 return r;
5501 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5502 type T. If T is not an aggregate or enumeration type, it is
5503 handled as if by tsubst. IN_DECL is as for tsubst. If
5504 ENTERING_SCOPE is non-zero, T is the context for a template which
5505 we are presently tsubst'ing. Return the substituted value. */
5507 static tree
5508 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5509 tree t;
5510 tree args;
5511 tsubst_flags_t complain;
5512 tree in_decl;
5513 int entering_scope;
5515 if (t == NULL_TREE)
5516 return NULL_TREE;
5518 switch (TREE_CODE (t))
5520 case RECORD_TYPE:
5521 if (TYPE_PTRMEMFUNC_P (t))
5522 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5524 /* else fall through */
5525 case ENUMERAL_TYPE:
5526 case UNION_TYPE:
5527 if (TYPE_TEMPLATE_INFO (t))
5529 tree argvec;
5530 tree context;
5531 tree r;
5533 /* First, determine the context for the type we are looking
5534 up. */
5535 if (TYPE_CONTEXT (t) != NULL_TREE)
5536 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5537 complain,
5538 in_decl, /*entering_scope=*/1);
5539 else
5540 context = NULL_TREE;
5542 /* Then, figure out what arguments are appropriate for the
5543 type we are trying to find. For example, given:
5545 template <class T> struct S;
5546 template <class T, class U> void f(T, U) { S<U> su; }
5548 and supposing that we are instantiating f<int, double>,
5549 then our ARGS will be {int, double}, but, when looking up
5550 S we only want {double}. */
5551 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5552 complain);
5553 if (argvec == error_mark_node)
5554 return error_mark_node;
5556 r = lookup_template_class (t, argvec, in_decl, context,
5557 entering_scope, complain);
5559 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5561 else
5562 /* This is not a template type, so there's nothing to do. */
5563 return t;
5565 default:
5566 return tsubst (t, args, complain, in_decl);
5570 /* Substitute into the default argument ARG (a default argument for
5571 FN), which has the indicated TYPE. */
5573 tree
5574 tsubst_default_argument (fn, type, arg)
5575 tree fn;
5576 tree type;
5577 tree arg;
5579 /* This default argument came from a template. Instantiate the
5580 default argument here, not in tsubst. In the case of
5581 something like:
5583 template <class T>
5584 struct S {
5585 static T t();
5586 void f(T = t());
5589 we must be careful to do name lookup in the scope of S<T>,
5590 rather than in the current class. */
5591 if (DECL_CLASS_SCOPE_P (fn))
5592 pushclass (DECL_CONTEXT (fn), 2);
5594 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5595 tf_error | tf_warning, NULL_TREE);
5597 if (DECL_CLASS_SCOPE_P (fn))
5598 popclass ();
5600 /* Make sure the default argument is reasonable. */
5601 arg = check_default_argument (type, arg);
5603 return arg;
5606 /* Substitute into all the default arguments for FN. */
5608 static void
5609 tsubst_default_arguments (fn)
5610 tree fn;
5612 tree arg;
5613 tree tmpl_args;
5615 tmpl_args = DECL_TI_ARGS (fn);
5617 /* If this function is not yet instantiated, we certainly don't need
5618 its default arguments. */
5619 if (uses_template_parms (tmpl_args))
5620 return;
5622 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5623 arg;
5624 arg = TREE_CHAIN (arg))
5625 if (TREE_PURPOSE (arg))
5626 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5627 TREE_VALUE (arg),
5628 TREE_PURPOSE (arg));
5631 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5632 (already computed) substitution of ARGS into TREE_TYPE (T), if
5633 appropriate. Return the result of the substitution. Issue error
5634 and warning messages under control of COMPLAIN. */
5636 static tree
5637 tsubst_decl (t, args, type, complain)
5638 tree t;
5639 tree args;
5640 tree type;
5641 tsubst_flags_t complain;
5643 int saved_lineno;
5644 const char *saved_filename;
5645 tree r = NULL_TREE;
5646 tree in_decl = t;
5648 my_friendly_assert (complain & tf_error, 20011214);
5650 /* Set the filename and linenumber to improve error-reporting. */
5651 saved_lineno = lineno;
5652 saved_filename = input_filename;
5653 lineno = DECL_SOURCE_LINE (t);
5654 input_filename = DECL_SOURCE_FILE (t);
5656 switch (TREE_CODE (t))
5658 case TEMPLATE_DECL:
5660 /* We can get here when processing a member template function
5661 of a template class. */
5662 tree decl = DECL_TEMPLATE_RESULT (t);
5663 tree spec;
5664 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5666 if (!is_template_template_parm)
5668 /* We might already have an instance of this template.
5669 The ARGS are for the surrounding class type, so the
5670 full args contain the tsubst'd args for the context,
5671 plus the innermost args from the template decl. */
5672 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5673 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5674 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5675 tree full_args;
5677 full_args = tsubst_template_arg_vector (tmpl_args, args,
5678 complain);
5680 /* tsubst_template_arg_vector doesn't copy the vector if
5681 nothing changed. But, *something* should have
5682 changed. */
5683 my_friendly_assert (full_args != tmpl_args, 0);
5685 spec = retrieve_specialization (t, full_args);
5686 if (spec != NULL_TREE)
5688 r = spec;
5689 break;
5693 /* Make a new template decl. It will be similar to the
5694 original, but will record the current template arguments.
5695 We also create a new function declaration, which is just
5696 like the old one, but points to this new template, rather
5697 than the old one. */
5698 r = copy_decl (t);
5699 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5700 TREE_CHAIN (r) = NULL_TREE;
5702 if (is_template_template_parm)
5704 tree new_decl = tsubst (decl, args, complain, in_decl);
5705 DECL_TEMPLATE_RESULT (r) = new_decl;
5706 TREE_TYPE (r) = TREE_TYPE (new_decl);
5707 break;
5710 DECL_CONTEXT (r)
5711 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5712 complain, in_decl,
5713 /*entering_scope=*/1);
5714 DECL_VIRTUAL_CONTEXT (r)
5715 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5716 complain, in_decl,
5717 /*entering_scope=*/1);
5718 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5720 if (TREE_CODE (decl) == TYPE_DECL)
5722 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5723 TREE_TYPE (r) = new_type;
5724 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5725 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5726 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5728 else
5730 tree new_decl = tsubst (decl, args, complain, in_decl);
5732 DECL_TEMPLATE_RESULT (r) = new_decl;
5733 DECL_TI_TEMPLATE (new_decl) = r;
5734 TREE_TYPE (r) = TREE_TYPE (new_decl);
5735 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5738 SET_DECL_IMPLICIT_INSTANTIATION (r);
5739 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5740 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5742 /* The template parameters for this new template are all the
5743 template parameters for the old template, except the
5744 outermost level of parameters. */
5745 DECL_TEMPLATE_PARMS (r)
5746 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5747 complain);
5749 if (PRIMARY_TEMPLATE_P (t))
5750 DECL_PRIMARY_TEMPLATE (r) = r;
5752 /* We don't partially instantiate partial specializations. */
5753 if (TREE_CODE (decl) == TYPE_DECL)
5754 break;
5756 /* Record this partial instantiation. */
5757 register_specialization (r, t,
5758 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5761 break;
5763 case FUNCTION_DECL:
5765 tree ctx;
5766 tree argvec = NULL_TREE;
5767 tree *friends;
5768 tree gen_tmpl;
5769 int member;
5770 int args_depth;
5771 int parms_depth;
5773 /* Nobody should be tsubst'ing into non-template functions. */
5774 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5776 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5778 tree spec;
5780 /* Calculate the most general template of which R is a
5781 specialization, and the complete set of arguments used to
5782 specialize R. */
5783 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5784 argvec
5785 = tsubst_template_arg_vector (DECL_TI_ARGS
5786 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5787 args, complain);
5789 /* Check to see if we already have this specialization. */
5790 spec = retrieve_specialization (gen_tmpl, argvec);
5792 if (spec)
5794 r = spec;
5795 break;
5798 /* We can see more levels of arguments than parameters if
5799 there was a specialization of a member template, like
5800 this:
5802 template <class T> struct S { template <class U> void f(); }
5803 template <> template <class U> void S<int>::f(U);
5805 Here, we'll be substituting into the specialization,
5806 because that's where we can find the code we actually
5807 want to generate, but we'll have enough arguments for
5808 the most general template.
5810 We also deal with the peculiar case:
5812 template <class T> struct S {
5813 template <class U> friend void f();
5815 template <class U> void f() {}
5816 template S<int>;
5817 template void f<double>();
5819 Here, the ARGS for the instantiation of will be {int,
5820 double}. But, we only need as many ARGS as there are
5821 levels of template parameters in CODE_PATTERN. We are
5822 careful not to get fooled into reducing the ARGS in
5823 situations like:
5825 template <class T> struct S { template <class U> void f(U); }
5826 template <class T> template <> void S<T>::f(int) {}
5828 which we can spot because the pattern will be a
5829 specialization in this case. */
5830 args_depth = TMPL_ARGS_DEPTH (args);
5831 parms_depth =
5832 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5833 if (args_depth > parms_depth
5834 && !DECL_TEMPLATE_SPECIALIZATION (t))
5835 args = get_innermost_template_args (args, parms_depth);
5837 else
5839 /* This special case arises when we have something like this:
5841 template <class T> struct S {
5842 friend void f<int>(int, double);
5845 Here, the DECL_TI_TEMPLATE for the friend declaration
5846 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5847 being called from tsubst_friend_function, and we want
5848 only to create a new decl (R) with appropriate types so
5849 that we can call determine_specialization. */
5850 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5851 == LOOKUP_EXPR)
5852 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5853 == IDENTIFIER_NODE), 0);
5854 gen_tmpl = NULL_TREE;
5857 if (DECL_CLASS_SCOPE_P (t))
5859 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5860 member = 2;
5861 else
5862 member = 1;
5863 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5864 complain, t,
5865 /*entering_scope=*/1);
5867 else
5869 member = 0;
5870 ctx = DECL_CONTEXT (t);
5872 type = tsubst (type, args, complain, in_decl);
5873 if (type == error_mark_node)
5874 return error_mark_node;
5876 /* We do NOT check for matching decls pushed separately at this
5877 point, as they may not represent instantiations of this
5878 template, and in any case are considered separate under the
5879 discrete model. */
5880 r = copy_decl (t);
5881 DECL_USE_TEMPLATE (r) = 0;
5882 TREE_TYPE (r) = type;
5883 /* Clear out the mangled name and RTL for the instantiation. */
5884 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5885 SET_DECL_RTL (r, NULL_RTX);
5887 DECL_CONTEXT (r) = ctx;
5888 DECL_VIRTUAL_CONTEXT (r)
5889 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5890 complain, t,
5891 /*entering_scope=*/1);
5893 if (member && DECL_CONV_FN_P (r))
5894 /* Type-conversion operator. Reconstruct the name, in
5895 case it's the name of one of the template's parameters. */
5896 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5898 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5899 complain, t);
5900 DECL_RESULT (r) = NULL_TREE;
5902 TREE_STATIC (r) = 0;
5903 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5904 DECL_EXTERNAL (r) = 1;
5905 DECL_INTERFACE_KNOWN (r) = 0;
5906 DECL_DEFER_OUTPUT (r) = 0;
5907 TREE_CHAIN (r) = NULL_TREE;
5908 DECL_PENDING_INLINE_INFO (r) = 0;
5909 DECL_PENDING_INLINE_P (r) = 0;
5910 DECL_SAVED_TREE (r) = NULL_TREE;
5911 TREE_USED (r) = 0;
5912 if (DECL_CLONED_FUNCTION (r))
5914 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
5915 args, complain, t);
5916 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
5917 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
5920 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5921 this in the special friend case mentioned above where
5922 GEN_TMPL is NULL. */
5923 if (gen_tmpl)
5925 DECL_TEMPLATE_INFO (r)
5926 = tree_cons (gen_tmpl, argvec, NULL_TREE);
5927 SET_DECL_IMPLICIT_INSTANTIATION (r);
5928 register_specialization (r, gen_tmpl, argvec);
5930 /* We're not supposed to instantiate default arguments
5931 until they are called, for a template. But, for a
5932 declaration like:
5934 template <class T> void f ()
5935 { extern void g(int i = T()); }
5937 we should do the substitution when the template is
5938 instantiated. We handle the member function case in
5939 instantiate_class_template since the default arguments
5940 might refer to other members of the class. */
5941 if (!member
5942 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5943 && !uses_template_parms (argvec))
5944 tsubst_default_arguments (r);
5947 /* Copy the list of befriending classes. */
5948 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5949 *friends;
5950 friends = &TREE_CHAIN (*friends))
5952 *friends = copy_node (*friends);
5953 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5954 args, complain,
5955 in_decl);
5958 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
5960 maybe_retrofit_in_chrg (r);
5961 if (DECL_CONSTRUCTOR_P (r))
5962 grok_ctor_properties (ctx, r);
5963 /* If this is an instantiation of a member template, clone it.
5964 If it isn't, that'll be handled by
5965 clone_constructors_and_destructors. */
5966 if (PRIMARY_TEMPLATE_P (gen_tmpl))
5967 clone_function_decl (r, /*update_method_vec_p=*/0);
5969 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5970 grok_op_properties (r, DECL_FRIEND_P (r));
5972 break;
5974 case PARM_DECL:
5976 r = copy_node (t);
5977 if (DECL_TEMPLATE_PARM_P (t))
5978 SET_DECL_TEMPLATE_PARM_P (r);
5980 TREE_TYPE (r) = type;
5981 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5983 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5984 DECL_INITIAL (r) = TREE_TYPE (r);
5985 else
5986 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5987 complain, in_decl);
5989 DECL_CONTEXT (r) = NULL_TREE;
5990 if (!DECL_TEMPLATE_PARM_P (r) && PROMOTE_PROTOTYPES
5991 && INTEGRAL_TYPE_P (type)
5992 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5993 DECL_ARG_TYPE (r) = integer_type_node;
5994 if (TREE_CHAIN (t))
5995 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5996 complain, TREE_CHAIN (t));
5998 break;
6000 case FIELD_DECL:
6002 r = copy_decl (t);
6003 TREE_TYPE (r) = type;
6004 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6006 /* We don't have to set DECL_CONTEXT here; it is set by
6007 finish_member_declaration. */
6008 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6009 complain, in_decl);
6010 TREE_CHAIN (r) = NULL_TREE;
6011 if (VOID_TYPE_P (type))
6012 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6014 break;
6016 case USING_DECL:
6018 r = copy_node (t);
6019 DECL_INITIAL (r)
6020 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6021 TREE_CHAIN (r) = NULL_TREE;
6023 break;
6025 case TYPE_DECL:
6026 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6027 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6029 /* If this is the canonical decl, we don't have to mess with
6030 instantiations, and often we can't (for typename, template
6031 type parms and such). Note that TYPE_NAME is not correct for
6032 the above test if we've copied the type for a typedef. */
6033 r = TYPE_NAME (type);
6034 break;
6037 /* Fall through. */
6039 case VAR_DECL:
6041 tree argvec = NULL_TREE;
6042 tree gen_tmpl = NULL_TREE;
6043 tree spec;
6044 tree tmpl = NULL_TREE;
6045 tree ctx;
6046 int local_p;
6048 /* Assume this is a non-local variable. */
6049 local_p = 0;
6051 if (TYPE_P (CP_DECL_CONTEXT (t)))
6052 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6053 complain,
6054 in_decl, /*entering_scope=*/1);
6055 else if (DECL_NAMESPACE_SCOPE_P (t))
6056 ctx = DECL_CONTEXT (t);
6057 else
6059 /* Subsequent calls to pushdecl will fill this in. */
6060 ctx = NULL_TREE;
6061 local_p = 1;
6064 /* Check to see if we already have this specialization. */
6065 if (!local_p)
6067 tmpl = DECL_TI_TEMPLATE (t);
6068 gen_tmpl = most_general_template (tmpl);
6069 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6070 spec = retrieve_specialization (gen_tmpl, argvec);
6072 else
6073 spec = retrieve_local_specialization (t);
6075 if (spec)
6077 r = spec;
6078 break;
6081 r = copy_decl (t);
6082 if (TREE_CODE (r) == VAR_DECL)
6083 type = complete_type (type);
6084 TREE_TYPE (r) = type;
6085 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6086 DECL_CONTEXT (r) = ctx;
6087 /* Clear out the mangled name and RTL for the instantiation. */
6088 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6089 SET_DECL_RTL (r, NULL_RTX);
6091 /* Don't try to expand the initializer until someone tries to use
6092 this variable; otherwise we run into circular dependencies. */
6093 DECL_INITIAL (r) = NULL_TREE;
6094 SET_DECL_RTL (r, NULL_RTX);
6095 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6097 /* Even if the original location is out of scope, the newly
6098 substituted one is not. */
6099 if (TREE_CODE (r) == VAR_DECL)
6100 DECL_DEAD_FOR_LOCAL (r) = 0;
6102 if (!local_p)
6104 /* A static data member declaration is always marked
6105 external when it is declared in-class, even if an
6106 initializer is present. We mimic the non-template
6107 processing here. */
6108 DECL_EXTERNAL (r) = 1;
6110 register_specialization (r, gen_tmpl, argvec);
6111 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6112 SET_DECL_IMPLICIT_INSTANTIATION (r);
6114 else
6115 register_local_specialization (r, t);
6117 TREE_CHAIN (r) = NULL_TREE;
6118 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6119 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6120 /* Compute the size, alignment, etc. of R. */
6121 layout_decl (r, 0);
6123 break;
6125 default:
6126 abort ();
6129 /* Restore the file and line information. */
6130 lineno = saved_lineno;
6131 input_filename = saved_filename;
6133 return r;
6136 /* Substitue into the ARG_TYPES of a function type. */
6138 static tree
6139 tsubst_arg_types (arg_types, args, complain, in_decl)
6140 tree arg_types;
6141 tree args;
6142 tsubst_flags_t complain;
6143 tree in_decl;
6145 tree remaining_arg_types;
6146 tree type;
6148 if (!arg_types || arg_types == void_list_node)
6149 return arg_types;
6151 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6152 args, complain, in_decl);
6153 if (remaining_arg_types == error_mark_node)
6154 return error_mark_node;
6156 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6157 if (type == error_mark_node)
6158 return error_mark_node;
6159 if (VOID_TYPE_P (type))
6161 if (complain & tf_error)
6163 error ("invalid parameter type `%T'", type);
6164 if (in_decl)
6165 cp_error_at ("in declaration `%D'", in_decl);
6167 return error_mark_node;
6170 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6171 top-level qualifiers as required. */
6172 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6174 /* Note that we do not substitute into default arguments here. The
6175 standard mandates that they be instantiated only when needed,
6176 which is done in build_over_call. */
6177 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6178 remaining_arg_types);
6182 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6183 *not* handle the exception-specification for FNTYPE, because the
6184 initial substitution of explicitly provided template parameters
6185 during argument deduction forbids substitution into the
6186 exception-specification:
6188 [temp.deduct]
6190 All references in the function type of the function template to the
6191 corresponding template parameters are replaced by the specified tem-
6192 plate argument values. If a substitution in a template parameter or
6193 in the function type of the function template results in an invalid
6194 type, type deduction fails. [Note: The equivalent substitution in
6195 exception specifications is done only when the function is instanti-
6196 ated, at which point a program is ill-formed if the substitution
6197 results in an invalid type.] */
6199 static tree
6200 tsubst_function_type (t, args, complain, in_decl)
6201 tree t;
6202 tree args;
6203 tsubst_flags_t complain;
6204 tree in_decl;
6206 tree return_type;
6207 tree arg_types;
6208 tree fntype;
6210 /* The TYPE_CONTEXT is not used for function/method types. */
6211 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6213 /* Substitute the return type. */
6214 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6215 if (return_type == error_mark_node)
6216 return error_mark_node;
6218 /* Substitue the argument types. */
6219 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6220 complain, in_decl);
6221 if (arg_types == error_mark_node)
6222 return error_mark_node;
6224 /* Construct a new type node and return it. */
6225 if (TREE_CODE (t) == FUNCTION_TYPE)
6226 fntype = build_function_type (return_type, arg_types);
6227 else
6229 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6230 if (! IS_AGGR_TYPE (r))
6232 /* [temp.deduct]
6234 Type deduction may fail for any of the following
6235 reasons:
6237 -- Attempting to create "pointer to member of T" when T
6238 is not a class type. */
6239 if (complain & tf_error)
6240 error ("creating pointer to member function of non-class type `%T'",
6242 return error_mark_node;
6245 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6246 (arg_types));
6248 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6249 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6251 return fntype;
6254 /* Substitute into the PARMS of a call-declarator. */
6256 static tree
6257 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6258 tree parms;
6259 tree args;
6260 tsubst_flags_t complain;
6261 tree in_decl;
6263 tree new_parms;
6264 tree type;
6265 tree defarg;
6267 if (!parms || parms == void_list_node)
6268 return parms;
6270 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6271 args, complain, in_decl);
6273 /* Figure out the type of this parameter. */
6274 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6276 /* Figure out the default argument as well. Note that we use
6277 tsubst_expr since the default argument is really an expression. */
6278 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6280 /* Chain this parameter on to the front of those we have already
6281 processed. We don't use hash_tree_cons because that function
6282 doesn't check TREE_PARMLIST. */
6283 new_parms = tree_cons (defarg, type, new_parms);
6285 /* And note that these are parameters. */
6286 TREE_PARMLIST (new_parms) = 1;
6288 return new_parms;
6291 /* Take the tree structure T and replace template parameters used
6292 therein with the argument vector ARGS. IN_DECL is an associated
6293 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6294 Issue error and warning messages under control of COMPLAIN. Note
6295 that we must be relatively non-tolerant of extensions here, in
6296 order to preserve conformance; if we allow substitutions that
6297 should not be allowed, we may allow argument deductions that should
6298 not succeed, and therefore report ambiguous overload situations
6299 where there are none. In theory, we could allow the substitution,
6300 but indicate that it should have failed, and allow our caller to
6301 make sure that the right thing happens, but we don't try to do this
6302 yet.
6304 This function is used for dealing with types, decls and the like;
6305 for expressions, use tsubst_expr or tsubst_copy. */
6307 tree
6308 tsubst (t, args, complain, in_decl)
6309 tree t, args;
6310 tsubst_flags_t complain;
6311 tree in_decl;
6313 tree type, r;
6315 if (t == NULL_TREE || t == error_mark_node
6316 || t == integer_type_node
6317 || t == void_type_node
6318 || t == char_type_node
6319 || TREE_CODE (t) == NAMESPACE_DECL)
6320 return t;
6322 if (TREE_CODE (t) == IDENTIFIER_NODE)
6323 type = IDENTIFIER_TYPE_VALUE (t);
6324 else
6325 type = TREE_TYPE (t);
6326 if (type == unknown_type_node)
6327 abort ();
6329 if (type && TREE_CODE (t) != FUNCTION_DECL
6330 && TREE_CODE (t) != TYPENAME_TYPE
6331 && TREE_CODE (t) != TEMPLATE_DECL
6332 && TREE_CODE (t) != IDENTIFIER_NODE
6333 && TREE_CODE (t) != FUNCTION_TYPE
6334 && TREE_CODE (t) != METHOD_TYPE)
6335 type = tsubst (type, args, complain, in_decl);
6336 if (type == error_mark_node)
6337 return error_mark_node;
6339 if (DECL_P (t))
6340 return tsubst_decl (t, args, type, complain);
6342 switch (TREE_CODE (t))
6344 case RECORD_TYPE:
6345 case UNION_TYPE:
6346 case ENUMERAL_TYPE:
6347 return tsubst_aggr_type (t, args, complain, in_decl,
6348 /*entering_scope=*/0);
6350 case ERROR_MARK:
6351 case IDENTIFIER_NODE:
6352 case VOID_TYPE:
6353 case REAL_TYPE:
6354 case COMPLEX_TYPE:
6355 case VECTOR_TYPE:
6356 case BOOLEAN_TYPE:
6357 case INTEGER_CST:
6358 case REAL_CST:
6359 case STRING_CST:
6360 return t;
6362 case INTEGER_TYPE:
6363 if (t == integer_type_node)
6364 return t;
6366 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6367 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6368 return t;
6371 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6373 max = tsubst_expr (omax, args, complain, in_decl);
6374 if (max == error_mark_node)
6375 return error_mark_node;
6377 /* See if we can reduce this expression to something simpler. */
6378 max = maybe_fold_nontype_arg (max);
6379 if (!processing_template_decl)
6380 max = decl_constant_value (max);
6382 if (processing_template_decl
6383 /* When providing explicit arguments to a template
6384 function, but leaving some arguments for subsequent
6385 deduction, MAX may be template-dependent even if we're
6386 not PROCESSING_TEMPLATE_DECL. We still need to check for
6387 template parms, though; MAX won't be an INTEGER_CST for
6388 dynamic arrays, either. */
6389 || (TREE_CODE (max) != INTEGER_CST
6390 && uses_template_parms (max)))
6392 tree itype = make_node (INTEGER_TYPE);
6393 TYPE_MIN_VALUE (itype) = size_zero_node;
6394 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6395 integer_one_node);
6396 return itype;
6399 if (integer_zerop (omax))
6401 /* Still allow an explicit array of size zero. */
6402 if (pedantic)
6403 pedwarn ("creating array with size zero");
6405 else if (integer_zerop (max)
6406 || (TREE_CODE (max) == INTEGER_CST
6407 && INT_CST_LT (max, integer_zero_node)))
6409 /* [temp.deduct]
6411 Type deduction may fail for any of the following
6412 reasons:
6414 Attempting to create an array with a size that is
6415 zero or negative. */
6416 if (complain & tf_error)
6417 error ("creating array with size zero (`%E')", max);
6419 return error_mark_node;
6422 return compute_array_index_type (NULL_TREE, max);
6425 case TEMPLATE_TYPE_PARM:
6426 case TEMPLATE_TEMPLATE_PARM:
6427 case BOUND_TEMPLATE_TEMPLATE_PARM:
6428 case TEMPLATE_PARM_INDEX:
6430 int idx;
6431 int level;
6432 int levels;
6434 r = NULL_TREE;
6436 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6437 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6438 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6440 idx = TEMPLATE_TYPE_IDX (t);
6441 level = TEMPLATE_TYPE_LEVEL (t);
6443 else
6445 idx = TEMPLATE_PARM_IDX (t);
6446 level = TEMPLATE_PARM_LEVEL (t);
6449 if (TREE_VEC_LENGTH (args) > 0)
6451 tree arg = NULL_TREE;
6453 levels = TMPL_ARGS_DEPTH (args);
6454 if (level <= levels)
6455 arg = TMPL_ARG (args, level, idx);
6457 if (arg == error_mark_node)
6458 return error_mark_node;
6459 else if (arg != NULL_TREE)
6461 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6463 my_friendly_assert (TYPE_P (arg), 0);
6464 return cp_build_qualified_type_real
6465 (arg, cp_type_quals (arg) | cp_type_quals (t),
6466 complain | tf_ignore_bad_quals);
6468 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6470 /* We are processing a type constructed from
6471 a template template parameter */
6472 tree argvec = tsubst (TYPE_TI_ARGS (t),
6473 args, complain, in_decl);
6474 if (argvec == error_mark_node)
6475 return error_mark_node;
6477 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6478 we are resolving nested-types in the signature of
6479 a member function templates.
6480 Otherwise ARG is a TEMPLATE_DECL and is the real
6481 template to be instantiated. */
6482 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6483 arg = TYPE_NAME (arg);
6485 r = lookup_template_class (arg,
6486 argvec, in_decl,
6487 DECL_CONTEXT (arg),
6488 /*entering_scope=*/0,
6489 complain);
6490 return cp_build_qualified_type_real
6491 (r, TYPE_QUALS (t), complain);
6493 else
6494 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6495 return arg;
6498 else
6499 abort ();
6501 if (level == 1)
6502 /* This can happen during the attempted tsubst'ing in
6503 unify. This means that we don't yet have any information
6504 about the template parameter in question. */
6505 return t;
6507 /* If we get here, we must have been looking at a parm for a
6508 more deeply nested template. Make a new version of this
6509 template parameter, but with a lower level. */
6510 switch (TREE_CODE (t))
6512 case TEMPLATE_TYPE_PARM:
6513 case TEMPLATE_TEMPLATE_PARM:
6514 case BOUND_TEMPLATE_TEMPLATE_PARM:
6515 if (cp_type_quals (t))
6517 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6518 r = cp_build_qualified_type_real
6519 (r, cp_type_quals (t),
6520 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6521 ? tf_ignore_bad_quals : 0));
6523 else
6525 r = copy_type (t);
6526 TEMPLATE_TYPE_PARM_INDEX (r)
6527 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6528 r, levels);
6529 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6530 TYPE_MAIN_VARIANT (r) = r;
6531 TYPE_POINTER_TO (r) = NULL_TREE;
6532 TYPE_REFERENCE_TO (r) = NULL_TREE;
6534 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6536 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6537 complain, in_decl);
6538 if (argvec == error_mark_node)
6539 return error_mark_node;
6541 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6542 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6545 break;
6547 case TEMPLATE_PARM_INDEX:
6548 r = reduce_template_parm_level (t, type, levels);
6549 break;
6551 default:
6552 abort ();
6555 return r;
6558 case TREE_LIST:
6560 tree purpose, value, chain, result;
6562 if (t == void_list_node)
6563 return t;
6565 purpose = TREE_PURPOSE (t);
6566 if (purpose)
6568 purpose = tsubst (purpose, args, complain, in_decl);
6569 if (purpose == error_mark_node)
6570 return error_mark_node;
6572 value = TREE_VALUE (t);
6573 if (value)
6575 value = tsubst (value, args, complain, in_decl);
6576 if (value == error_mark_node)
6577 return error_mark_node;
6579 chain = TREE_CHAIN (t);
6580 if (chain && chain != void_type_node)
6582 chain = tsubst (chain, args, complain, in_decl);
6583 if (chain == error_mark_node)
6584 return error_mark_node;
6586 if (purpose == TREE_PURPOSE (t)
6587 && value == TREE_VALUE (t)
6588 && chain == TREE_CHAIN (t))
6589 return t;
6590 if (TREE_PARMLIST (t))
6592 result = tree_cons (purpose, value, chain);
6593 TREE_PARMLIST (result) = 1;
6595 else
6596 result = hash_tree_cons (purpose, value, chain);
6597 return result;
6599 case TREE_VEC:
6600 if (type != NULL_TREE)
6602 /* A binfo node. We always need to make a copy, of the node
6603 itself and of its BINFO_BASETYPES. */
6605 t = copy_node (t);
6607 /* Make sure type isn't a typedef copy. */
6608 type = BINFO_TYPE (TYPE_BINFO (type));
6610 TREE_TYPE (t) = complete_type (type);
6611 if (IS_AGGR_TYPE (type))
6613 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6614 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6615 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6616 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6618 return t;
6621 /* Otherwise, a vector of template arguments. */
6622 return tsubst_template_arg_vector (t, args, complain);
6624 case POINTER_TYPE:
6625 case REFERENCE_TYPE:
6627 enum tree_code code;
6629 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6630 return t;
6632 code = TREE_CODE (t);
6635 /* [temp.deduct]
6637 Type deduction may fail for any of the following
6638 reasons:
6640 -- Attempting to create a pointer to reference type.
6641 -- Attempting to create a reference to a reference type or
6642 a reference to void. */
6643 if (TREE_CODE (type) == REFERENCE_TYPE
6644 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6646 static int last_line = 0;
6647 static const char* last_file = 0;
6649 /* We keep track of the last time we issued this error
6650 message to avoid spewing a ton of messages during a
6651 single bad template instantiation. */
6652 if (complain & tf_error
6653 && (last_line != lineno || last_file != input_filename))
6655 if (TREE_CODE (type) == VOID_TYPE)
6656 error ("forming reference to void");
6657 else
6658 error ("forming %s to reference type `%T'",
6659 (code == POINTER_TYPE) ? "pointer" : "reference",
6660 type);
6661 last_line = lineno;
6662 last_file = input_filename;
6665 return error_mark_node;
6667 else if (code == POINTER_TYPE)
6669 r = build_pointer_type (type);
6670 if (TREE_CODE (type) == METHOD_TYPE)
6671 r = build_ptrmemfunc_type (r);
6673 else
6674 r = build_reference_type (type);
6675 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6677 if (r != error_mark_node)
6678 /* Will this ever be needed for TYPE_..._TO values? */
6679 layout_type (r);
6681 return r;
6683 case OFFSET_TYPE:
6685 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6686 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6688 /* [temp.deduct]
6690 Type deduction may fail for any of the following
6691 reasons:
6693 -- Attempting to create "pointer to member of T" when T
6694 is not a class type. */
6695 if (complain & tf_error)
6696 error ("creating pointer to member of non-class type `%T'", r);
6697 return error_mark_node;
6699 if (TREE_CODE (type) == REFERENCE_TYPE)
6701 if (complain)
6702 error ("creating pointer to member reference type `%T'", type);
6704 return error_mark_node;
6706 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6707 if (TREE_CODE (type) == FUNCTION_TYPE)
6708 /* This is really a method type. The cv qualifiers of the
6709 this pointer should _not_ be determined by the cv
6710 qualifiers of the class type. They should be held
6711 somewhere in the FUNCTION_TYPE, but we don't do that at
6712 the moment. Consider
6713 typedef void (Func) () const;
6715 template <typename T1> void Foo (Func T1::*);
6718 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6719 TREE_TYPE (type),
6720 TYPE_ARG_TYPES (type));
6721 else
6722 return build_offset_type (r, type);
6724 case FUNCTION_TYPE:
6725 case METHOD_TYPE:
6727 tree fntype;
6728 tree raises;
6730 fntype = tsubst_function_type (t, args, complain, in_decl);
6731 if (fntype == error_mark_node)
6732 return error_mark_node;
6734 /* Substitue the exception specification. */
6735 raises = TYPE_RAISES_EXCEPTIONS (t);
6736 if (raises)
6738 tree list = NULL_TREE;
6740 if (! TREE_VALUE (raises))
6741 list = raises;
6742 else
6743 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6745 tree spec = TREE_VALUE (raises);
6747 spec = tsubst (spec, args, complain, in_decl);
6748 if (spec == error_mark_node)
6749 return spec;
6750 list = add_exception_specifier (list, spec, complain);
6752 fntype = build_exception_variant (fntype, list);
6754 return fntype;
6756 case ARRAY_TYPE:
6758 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6759 if (domain == error_mark_node)
6760 return error_mark_node;
6762 /* As an optimization, we avoid regenerating the array type if
6763 it will obviously be the same as T. */
6764 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6765 return t;
6767 /* These checks should match the ones in grokdeclarator.
6769 [temp.deduct]
6771 The deduction may fail for any of the following reasons:
6773 -- Attempting to create an array with an element type that
6774 is void, a function type, or a reference type. */
6775 if (TREE_CODE (type) == VOID_TYPE
6776 || TREE_CODE (type) == FUNCTION_TYPE
6777 || TREE_CODE (type) == REFERENCE_TYPE)
6779 if (complain & tf_error)
6780 error ("creating array of `%T'", type);
6781 return error_mark_node;
6784 r = build_cplus_array_type (type, domain);
6785 return r;
6788 case PLUS_EXPR:
6789 case MINUS_EXPR:
6791 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6792 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6794 if (e1 == error_mark_node || e2 == error_mark_node)
6795 return error_mark_node;
6797 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6800 case NEGATE_EXPR:
6801 case NOP_EXPR:
6803 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6804 if (e == error_mark_node)
6805 return error_mark_node;
6807 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6810 case TYPENAME_TYPE:
6812 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6813 in_decl, /*entering_scope=*/1);
6814 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6815 complain, in_decl);
6817 if (ctx == error_mark_node || f == error_mark_node)
6818 return error_mark_node;
6820 if (!IS_AGGR_TYPE (ctx))
6822 if (complain & tf_error)
6823 error ("`%T' is not a class, struct, or union type",
6824 ctx);
6825 return error_mark_node;
6827 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6829 /* Normally, make_typename_type does not require that the CTX
6830 have complete type in order to allow things like:
6832 template <class T> struct S { typename S<T>::X Y; };
6834 But, such constructs have already been resolved by this
6835 point, so here CTX really should have complete type, unless
6836 it's a partial instantiation. */
6837 ctx = complete_type (ctx);
6838 if (!COMPLETE_TYPE_P (ctx))
6840 if (complain & tf_error)
6841 incomplete_type_error (NULL_TREE, ctx);
6842 return error_mark_node;
6846 f = make_typename_type (ctx, f,
6847 (complain & tf_error) | tf_keep_type_decl);
6848 if (f == error_mark_node)
6849 return f;
6850 if (TREE_CODE (f) == TYPE_DECL)
6852 complain |= tf_ignore_bad_quals;
6853 f = TREE_TYPE (f);
6856 return cp_build_qualified_type_real
6857 (f, cp_type_quals (f) | cp_type_quals (t), complain);
6860 case UNBOUND_CLASS_TEMPLATE:
6862 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6863 in_decl, /*entering_scope=*/1);
6864 tree name = TYPE_IDENTIFIER (t);
6866 if (ctx == error_mark_node || name == error_mark_node)
6867 return error_mark_node;
6869 return make_unbound_class_template (ctx, name, complain);
6872 case INDIRECT_REF:
6874 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6875 if (e == error_mark_node)
6876 return error_mark_node;
6877 return make_pointer_declarator (type, e);
6880 case ADDR_EXPR:
6882 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6883 if (e == error_mark_node)
6884 return error_mark_node;
6885 return make_reference_declarator (type, e);
6888 case ARRAY_REF:
6890 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6891 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
6892 if (e1 == error_mark_node || e2 == error_mark_node)
6893 return error_mark_node;
6895 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
6898 case CALL_EXPR:
6900 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6901 tree e2 = (tsubst_call_declarator_parms
6902 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6903 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6904 complain, in_decl);
6906 if (e1 == error_mark_node || e2 == error_mark_node
6907 || e3 == error_mark_node)
6908 return error_mark_node;
6910 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
6913 case SCOPE_REF:
6915 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6916 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6917 if (e1 == error_mark_node || e2 == error_mark_node)
6918 return error_mark_node;
6920 return build_nt (TREE_CODE (t), e1, e2);
6923 case TYPEOF_TYPE:
6925 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
6926 if (e1 == error_mark_node)
6927 return error_mark_node;
6929 return TREE_TYPE (e1);
6932 default:
6933 sorry ("use of `%s' in template",
6934 tree_code_name [(int) TREE_CODE (t)]);
6935 return error_mark_node;
6939 /* Like tsubst, but deals with expressions. This function just replaces
6940 template parms; to finish processing the resultant expression, use
6941 tsubst_expr. */
6943 tree
6944 tsubst_copy (t, args, complain, in_decl)
6945 tree t, args;
6946 tsubst_flags_t complain;
6947 tree in_decl;
6949 enum tree_code code;
6950 tree r;
6952 if (t == NULL_TREE || t == error_mark_node)
6953 return t;
6955 code = TREE_CODE (t);
6957 switch (code)
6959 case PARM_DECL:
6960 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6962 case CONST_DECL:
6964 tree enum_type;
6965 tree v;
6967 if (!DECL_CONTEXT (t))
6968 /* This is a global enumeration constant. */
6969 return t;
6971 /* Unfortunately, we cannot just call lookup_name here.
6972 Consider:
6974 template <int I> int f() {
6975 enum E { a = I };
6976 struct S { void g() { E e = a; } };
6979 When we instantiate f<7>::S::g(), say, lookup_name is not
6980 clever enough to find f<7>::a. */
6981 enum_type
6982 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
6983 /*entering_scope=*/0);
6985 for (v = TYPE_VALUES (enum_type);
6986 v != NULL_TREE;
6987 v = TREE_CHAIN (v))
6988 if (TREE_PURPOSE (v) == DECL_NAME (t))
6989 return TREE_VALUE (v);
6991 /* We didn't find the name. That should never happen; if
6992 name-lookup found it during preliminary parsing, we
6993 should find it again here during instantiation. */
6994 abort ();
6996 return t;
6998 case FIELD_DECL:
6999 if (DECL_CONTEXT (t))
7001 tree ctx;
7003 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7004 /*entering_scope=*/1);
7005 if (ctx != DECL_CONTEXT (t))
7006 return lookup_field (ctx, DECL_NAME (t), 0, 0);
7008 return t;
7010 case VAR_DECL:
7011 case FUNCTION_DECL:
7012 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7013 t = tsubst (t, args, complain, in_decl);
7014 mark_used (t);
7015 return t;
7017 case TEMPLATE_DECL:
7018 if (is_member_template (t))
7019 return tsubst (t, args, complain, in_decl);
7020 else
7021 return t;
7023 case LOOKUP_EXPR:
7025 /* We must tsubst into a LOOKUP_EXPR in case the names to
7026 which it refers is a conversion operator; in that case the
7027 name will change. We avoid making unnecessary copies,
7028 however. */
7030 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7032 if (id != TREE_OPERAND (t, 0))
7034 r = build_nt (LOOKUP_EXPR, id);
7035 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7036 t = r;
7039 return t;
7042 case CAST_EXPR:
7043 case REINTERPRET_CAST_EXPR:
7044 case CONST_CAST_EXPR:
7045 case STATIC_CAST_EXPR:
7046 case DYNAMIC_CAST_EXPR:
7047 case NOP_EXPR:
7048 return build1
7049 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7050 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7052 case INDIRECT_REF:
7053 case NEGATE_EXPR:
7054 case TRUTH_NOT_EXPR:
7055 case BIT_NOT_EXPR:
7056 case ADDR_EXPR:
7057 case CONVERT_EXPR: /* Unary + */
7058 case SIZEOF_EXPR:
7059 case ALIGNOF_EXPR:
7060 case ARROW_EXPR:
7061 case THROW_EXPR:
7062 case TYPEID_EXPR:
7063 case REALPART_EXPR:
7064 case IMAGPART_EXPR:
7065 return build1
7066 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7067 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7069 case PLUS_EXPR:
7070 case MINUS_EXPR:
7071 case MULT_EXPR:
7072 case TRUNC_DIV_EXPR:
7073 case CEIL_DIV_EXPR:
7074 case FLOOR_DIV_EXPR:
7075 case ROUND_DIV_EXPR:
7076 case EXACT_DIV_EXPR:
7077 case BIT_AND_EXPR:
7078 case BIT_ANDTC_EXPR:
7079 case BIT_IOR_EXPR:
7080 case BIT_XOR_EXPR:
7081 case TRUNC_MOD_EXPR:
7082 case FLOOR_MOD_EXPR:
7083 case TRUTH_ANDIF_EXPR:
7084 case TRUTH_ORIF_EXPR:
7085 case TRUTH_AND_EXPR:
7086 case TRUTH_OR_EXPR:
7087 case RSHIFT_EXPR:
7088 case LSHIFT_EXPR:
7089 case RROTATE_EXPR:
7090 case LROTATE_EXPR:
7091 case EQ_EXPR:
7092 case NE_EXPR:
7093 case MAX_EXPR:
7094 case MIN_EXPR:
7095 case LE_EXPR:
7096 case GE_EXPR:
7097 case LT_EXPR:
7098 case GT_EXPR:
7099 case COMPONENT_REF:
7100 case ARRAY_REF:
7101 case COMPOUND_EXPR:
7102 case SCOPE_REF:
7103 case DOTSTAR_EXPR:
7104 case MEMBER_REF:
7105 case PREDECREMENT_EXPR:
7106 case PREINCREMENT_EXPR:
7107 case POSTDECREMENT_EXPR:
7108 case POSTINCREMENT_EXPR:
7109 return build_nt
7110 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7111 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7113 case CALL_EXPR:
7115 tree fn = TREE_OPERAND (t, 0);
7116 if (is_overloaded_fn (fn))
7117 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
7118 else
7119 /* Sometimes FN is a LOOKUP_EXPR. */
7120 fn = tsubst_copy (fn, args, complain, in_decl);
7121 return build_nt
7122 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7123 in_decl),
7124 NULL_TREE);
7127 case METHOD_CALL_EXPR:
7129 tree name = TREE_OPERAND (t, 0);
7130 if (TREE_CODE (name) == BIT_NOT_EXPR)
7132 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7133 complain, in_decl);
7134 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7136 else if (TREE_CODE (name) == SCOPE_REF
7137 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7139 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7140 complain, in_decl);
7141 name = TREE_OPERAND (name, 1);
7142 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7143 complain, in_decl);
7144 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7145 name = build_nt (SCOPE_REF, base, name);
7147 else
7148 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7149 return build_nt
7150 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7151 complain, in_decl),
7152 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7153 NULL_TREE);
7156 case STMT_EXPR:
7157 /* This processing should really occur in tsubst_expr, However,
7158 tsubst_expr does not recurse into expressions, since it
7159 assumes that there aren't any statements inside them.
7160 Instead, it simply calls build_expr_from_tree. So, we need
7161 to expand the STMT_EXPR here. */
7162 if (!processing_template_decl)
7164 tree stmt_expr = begin_stmt_expr ();
7165 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7166 return finish_stmt_expr (stmt_expr);
7169 return t;
7171 case COND_EXPR:
7172 case MODOP_EXPR:
7173 case PSEUDO_DTOR_EXPR:
7175 r = build_nt
7176 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7177 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7178 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7179 return r;
7182 case NEW_EXPR:
7184 r = build_nt
7185 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7186 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7187 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7188 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7189 return r;
7192 case DELETE_EXPR:
7194 r = build_nt
7195 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7196 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7197 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7198 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7199 return r;
7202 case TEMPLATE_ID_EXPR:
7204 /* Substituted template arguments */
7205 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7206 in_decl);
7208 if (targs && TREE_CODE (targs) == TREE_LIST)
7210 tree chain;
7211 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7212 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7214 else if (targs)
7216 int i;
7217 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7218 TREE_VEC_ELT (targs, i)
7219 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7222 return lookup_template_function
7223 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7226 case TREE_LIST:
7228 tree purpose, value, chain;
7230 if (t == void_list_node)
7231 return t;
7233 purpose = TREE_PURPOSE (t);
7234 if (purpose)
7235 purpose = tsubst_copy (purpose, args, complain, in_decl);
7236 value = TREE_VALUE (t);
7237 if (value)
7238 value = tsubst_copy (value, args, complain, in_decl);
7239 chain = TREE_CHAIN (t);
7240 if (chain && chain != void_type_node)
7241 chain = tsubst_copy (chain, args, complain, in_decl);
7242 if (purpose == TREE_PURPOSE (t)
7243 && value == TREE_VALUE (t)
7244 && chain == TREE_CHAIN (t))
7245 return t;
7246 return tree_cons (purpose, value, chain);
7249 case RECORD_TYPE:
7250 case UNION_TYPE:
7251 case ENUMERAL_TYPE:
7252 case INTEGER_TYPE:
7253 case TEMPLATE_TYPE_PARM:
7254 case TEMPLATE_TEMPLATE_PARM:
7255 case BOUND_TEMPLATE_TEMPLATE_PARM:
7256 case TEMPLATE_PARM_INDEX:
7257 case POINTER_TYPE:
7258 case REFERENCE_TYPE:
7259 case OFFSET_TYPE:
7260 case FUNCTION_TYPE:
7261 case METHOD_TYPE:
7262 case ARRAY_TYPE:
7263 case TYPENAME_TYPE:
7264 case UNBOUND_CLASS_TEMPLATE:
7265 case TYPEOF_TYPE:
7266 case TYPE_DECL:
7267 return tsubst (t, args, complain, in_decl);
7269 case IDENTIFIER_NODE:
7270 if (IDENTIFIER_TYPENAME_P (t))
7272 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7273 return mangle_conv_op_name_for_type (new_type);
7275 else
7276 return t;
7278 case CONSTRUCTOR:
7280 r = build
7281 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7282 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7283 complain, in_decl));
7284 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7285 return r;
7288 case VA_ARG_EXPR:
7289 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7290 in_decl),
7291 tsubst (TREE_TYPE (t), args, complain, in_decl));
7293 default:
7294 return t;
7298 /* Like tsubst_copy, but also does semantic processing. */
7300 tree
7301 tsubst_expr (t, args, complain, in_decl)
7302 tree t, args;
7303 tsubst_flags_t complain;
7304 tree in_decl;
7306 tree stmt, tmp;
7308 if (t == NULL_TREE || t == error_mark_node)
7309 return t;
7311 if (processing_template_decl)
7312 return tsubst_copy (t, args, complain, in_decl);
7314 if (!statement_code_p (TREE_CODE (t)))
7315 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7317 switch (TREE_CODE (t))
7319 case RETURN_INIT:
7320 prep_stmt (t);
7321 finish_named_return_value
7322 (TREE_OPERAND (t, 0),
7323 tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl));
7324 break;
7326 case CTOR_INITIALIZER:
7328 tree member_init_list;
7329 tree base_init_list;
7331 prep_stmt (t);
7332 member_init_list
7333 = tsubst_initializer_list (TREE_OPERAND (t, 0), args);
7334 base_init_list
7335 = tsubst_initializer_list (TREE_OPERAND (t, 1), args);
7336 emit_base_init (member_init_list, base_init_list);
7337 break;
7340 case RETURN_STMT:
7341 prep_stmt (t);
7342 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
7343 args, complain, in_decl));
7344 break;
7346 case EXPR_STMT:
7347 prep_stmt (t);
7348 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7349 args, complain, in_decl));
7350 break;
7352 case USING_STMT:
7353 prep_stmt (t);
7354 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7355 args, complain, in_decl));
7356 break;
7358 case DECL_STMT:
7360 tree decl;
7361 tree init;
7363 prep_stmt (t);
7364 decl = DECL_STMT_DECL (t);
7365 if (TREE_CODE (decl) == LABEL_DECL)
7366 finish_label_decl (DECL_NAME (decl));
7367 else if (TREE_CODE (decl) == USING_DECL)
7369 tree scope = DECL_INITIAL (decl);
7370 tree name = DECL_NAME (decl);
7372 scope = tsubst_expr (scope, args, complain, in_decl);
7373 do_local_using_decl (build_nt (SCOPE_REF, scope, name));
7375 else
7377 init = DECL_INITIAL (decl);
7378 decl = tsubst (decl, args, complain, in_decl);
7379 if (decl != error_mark_node)
7381 if (init)
7382 DECL_INITIAL (decl) = error_mark_node;
7383 /* By marking the declaration as instantiated, we avoid
7384 trying to instantiate it. Since instantiate_decl can't
7385 handle local variables, and since we've already done
7386 all that needs to be done, that's the right thing to
7387 do. */
7388 if (TREE_CODE (decl) == VAR_DECL)
7389 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7390 if (TREE_CODE (decl) == VAR_DECL
7391 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7392 /* Anonymous aggregates are a special case. */
7393 finish_anon_union (decl);
7394 else
7396 maybe_push_decl (decl);
7397 if (DECL_PRETTY_FUNCTION_P (decl))
7399 /* For __PRETTY_FUNCTION__ we have to adjust the
7400 initializer. */
7401 const char *const name
7402 = (*decl_printable_name) (current_function_decl, 2);
7403 init = cp_fname_init (name);
7404 TREE_TYPE (decl) = TREE_TYPE (init);
7406 else
7407 init = tsubst_expr (init, args, complain, in_decl);
7408 cp_finish_decl (decl, init, NULL_TREE, 0);
7413 /* A DECL_STMT can also be used as an expression, in the condition
7414 clause of a if/for/while construct. If we aren't followed by
7415 another statement, return our decl. */
7416 if (TREE_CHAIN (t) == NULL_TREE)
7417 return decl;
7419 break;
7421 case FOR_STMT:
7423 prep_stmt (t);
7425 stmt = begin_for_stmt ();
7426 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7427 finish_for_init_stmt (stmt);
7428 finish_for_cond (tsubst_expr (FOR_COND (t),
7429 args, complain, in_decl),
7430 stmt);
7431 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7432 finish_for_expr (tmp, stmt);
7433 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7434 finish_for_stmt (stmt);
7436 break;
7438 case WHILE_STMT:
7440 prep_stmt (t);
7441 stmt = begin_while_stmt ();
7442 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7443 args, complain, in_decl),
7444 stmt);
7445 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7446 finish_while_stmt (stmt);
7448 break;
7450 case DO_STMT:
7452 prep_stmt (t);
7453 stmt = begin_do_stmt ();
7454 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7455 finish_do_body (stmt);
7456 finish_do_stmt (tsubst_expr (DO_COND (t),
7457 args, complain, in_decl),
7458 stmt);
7460 break;
7462 case IF_STMT:
7464 prep_stmt (t);
7465 stmt = begin_if_stmt ();
7466 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7467 args, complain, in_decl),
7468 stmt);
7470 if (tmp = THEN_CLAUSE (t), tmp)
7472 tsubst_expr (tmp, args, complain, in_decl);
7473 finish_then_clause (stmt);
7476 if (tmp = ELSE_CLAUSE (t), tmp)
7478 begin_else_clause ();
7479 tsubst_expr (tmp, args, complain, in_decl);
7480 finish_else_clause (stmt);
7483 finish_if_stmt ();
7485 break;
7487 case COMPOUND_STMT:
7489 prep_stmt (t);
7490 if (COMPOUND_STMT_BODY_BLOCK (t))
7491 stmt = begin_function_body ();
7492 else
7493 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7495 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7497 if (COMPOUND_STMT_BODY_BLOCK (t))
7498 finish_function_body (stmt);
7499 else
7500 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7502 break;
7504 case BREAK_STMT:
7505 prep_stmt (t);
7506 finish_break_stmt ();
7507 break;
7509 case CONTINUE_STMT:
7510 prep_stmt (t);
7511 finish_continue_stmt ();
7512 break;
7514 case SWITCH_STMT:
7516 tree val;
7518 prep_stmt (t);
7519 stmt = begin_switch_stmt ();
7520 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7521 finish_switch_cond (val, stmt);
7522 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7523 finish_switch_stmt (stmt);
7525 break;
7527 case CASE_LABEL:
7528 prep_stmt (t);
7529 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7530 tsubst_expr (CASE_HIGH (t), args, complain,
7531 in_decl));
7532 break;
7534 case LABEL_STMT:
7535 lineno = STMT_LINENO (t);
7536 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7537 break;
7539 case FILE_STMT:
7540 input_filename = FILE_STMT_FILENAME (t);
7541 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7542 break;
7544 case GOTO_STMT:
7545 prep_stmt (t);
7546 tmp = GOTO_DESTINATION (t);
7547 if (TREE_CODE (tmp) != LABEL_DECL)
7548 /* Computed goto's must be tsubst'd into. On the other hand,
7549 non-computed gotos must not be; the identifier in question
7550 will have no binding. */
7551 tmp = tsubst_expr (tmp, args, complain, in_decl);
7552 else
7553 tmp = DECL_NAME (tmp);
7554 finish_goto_stmt (tmp);
7555 break;
7557 case ASM_STMT:
7558 prep_stmt (t);
7559 tmp = finish_asm_stmt
7560 (ASM_CV_QUAL (t),
7561 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7562 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7563 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7564 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7565 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
7566 break;
7568 case TRY_BLOCK:
7569 prep_stmt (t);
7570 if (CLEANUP_P (t))
7572 stmt = begin_try_block ();
7573 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7574 finish_cleanup_try_block (stmt);
7575 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7576 complain, in_decl),
7577 stmt);
7579 else
7581 if (FN_TRY_BLOCK_P (t))
7582 stmt = begin_function_try_block ();
7583 else
7584 stmt = begin_try_block ();
7586 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7588 if (FN_TRY_BLOCK_P (t))
7589 finish_function_try_block (stmt);
7590 else
7591 finish_try_block (stmt);
7593 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7594 if (FN_TRY_BLOCK_P (t))
7595 finish_function_handler_sequence (stmt);
7596 else
7597 finish_handler_sequence (stmt);
7599 break;
7601 case HANDLER:
7603 tree decl;
7605 prep_stmt (t);
7606 stmt = begin_handler ();
7607 if (HANDLER_PARMS (t))
7609 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7610 decl = tsubst (decl, args, complain, in_decl);
7611 /* Prevent instantiate_decl from trying to instantiate
7612 this variable. We've already done all that needs to be
7613 done. */
7614 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7616 else
7617 decl = NULL_TREE;
7618 finish_handler_parms (decl, stmt);
7619 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7620 finish_handler (stmt);
7622 break;
7624 case TAG_DEFN:
7625 prep_stmt (t);
7626 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7627 break;
7629 case CTOR_STMT:
7630 add_stmt (copy_node (t));
7631 break;
7633 default:
7634 abort ();
7637 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7640 /* Instantiate the indicated variable or function template TMPL with
7641 the template arguments in TARG_PTR. */
7643 tree
7644 instantiate_template (tmpl, targ_ptr)
7645 tree tmpl, targ_ptr;
7647 tree fndecl;
7648 tree gen_tmpl;
7649 tree spec;
7650 int i, len;
7651 tree inner_args;
7653 if (tmpl == error_mark_node)
7654 return error_mark_node;
7656 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7658 /* If this function is a clone, handle it specially. */
7659 if (DECL_CLONED_FUNCTION_P (tmpl))
7661 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
7662 tree clone;
7664 /* Look for the clone. */
7665 for (clone = TREE_CHAIN (spec);
7666 clone && DECL_CLONED_FUNCTION_P (clone);
7667 clone = TREE_CHAIN (clone))
7668 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7669 return clone;
7670 /* We should always have found the clone by now. */
7671 abort ();
7672 return NULL_TREE;
7675 /* Check to see if we already have this specialization. */
7676 spec = retrieve_specialization (tmpl, targ_ptr);
7677 if (spec != NULL_TREE)
7678 return spec;
7680 gen_tmpl = most_general_template (tmpl);
7681 if (tmpl != gen_tmpl)
7683 /* The TMPL is a partial instantiation. To get a full set of
7684 arguments we must add the arguments used to perform the
7685 partial instantiation. */
7686 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7687 targ_ptr);
7689 /* Check to see if we already have this specialization. */
7690 spec = retrieve_specialization (gen_tmpl, targ_ptr);
7691 if (spec != NULL_TREE)
7692 return spec;
7695 len = DECL_NTPARMS (gen_tmpl);
7696 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
7697 i = len;
7698 while (i--)
7700 tree t = TREE_VEC_ELT (inner_args, i);
7701 if (TYPE_P (t))
7703 tree nt = target_type (t);
7704 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
7706 error ("type `%T' composed from a local class is not a valid template-argument", t);
7707 error (" trying to instantiate `%D'", gen_tmpl);
7708 return error_mark_node;
7713 /* substitute template parameters */
7714 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
7715 targ_ptr, tf_error, gen_tmpl);
7716 /* The DECL_TI_TEMPLATE should always be the immediate parent
7717 template, not the most general template. */
7718 DECL_TI_TEMPLATE (fndecl) = tmpl;
7720 if (flag_external_templates)
7721 add_pending_template (fndecl);
7723 /* If we've just instantiated the main entry point for a function,
7724 instantiate all the alternate entry points as well. We do this
7725 by cloning the instantiation of the main entry point, not by
7726 instantiating the template clones. */
7727 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
7728 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
7730 return fndecl;
7733 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7734 arguments that are being used when calling it. TARGS is a vector
7735 into which the deduced template arguments are placed.
7737 Return zero for success, 2 for an incomplete match that doesn't resolve
7738 all the types, and 1 for complete failure. An error message will be
7739 printed only for an incomplete match.
7741 If FN is a conversion operator, or we are trying to produce a specific
7742 specialization, RETURN_TYPE is the return type desired.
7744 The EXPLICIT_TARGS are explicit template arguments provided via a
7745 template-id.
7747 The parameter STRICT is one of:
7749 DEDUCE_CALL:
7750 We are deducing arguments for a function call, as in
7751 [temp.deduct.call].
7753 DEDUCE_CONV:
7754 We are deducing arguments for a conversion function, as in
7755 [temp.deduct.conv].
7757 DEDUCE_EXACT:
7758 We are deducing arguments when doing an explicit instantiation
7759 as in [temp.explicit], when determining an explicit specialization
7760 as in [temp.expl.spec], or when taking the address of a function
7761 template, as in [temp.deduct.funcaddr].
7763 DEDUCE_ORDER:
7764 We are deducing arguments when calculating the partial
7765 ordering between specializations of function or class
7766 templates, as in [temp.func.order] and [temp.class.order].
7768 LEN is the number of parms to consider before returning success, or -1
7769 for all. This is used in partial ordering to avoid comparing parms for
7770 which no actual argument was passed, since they are not considered in
7771 overload resolution (and are explicitly excluded from consideration in
7772 partial ordering in [temp.func.order]/6). */
7775 fn_type_unification (fn, explicit_targs, targs, args, return_type,
7776 strict, len)
7777 tree fn, explicit_targs, targs, args, return_type;
7778 unification_kind_t strict;
7779 int len;
7781 tree parms;
7782 tree fntype;
7783 int result;
7785 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7787 fntype = TREE_TYPE (fn);
7788 if (explicit_targs)
7790 /* [temp.deduct]
7792 The specified template arguments must match the template
7793 parameters in kind (i.e., type, nontype, template), and there
7794 must not be more arguments than there are parameters;
7795 otherwise type deduction fails.
7797 Nontype arguments must match the types of the corresponding
7798 nontype template parameters, or must be convertible to the
7799 types of the corresponding nontype parameters as specified in
7800 _temp.arg.nontype_, otherwise type deduction fails.
7802 All references in the function type of the function template
7803 to the corresponding template parameters are replaced by the
7804 specified template argument values. If a substitution in a
7805 template parameter or in the function type of the function
7806 template results in an invalid type, type deduction fails. */
7807 int i;
7808 tree converted_args;
7810 converted_args
7811 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7812 explicit_targs, NULL_TREE, tf_none,
7813 /*require_all_arguments=*/0));
7814 if (converted_args == error_mark_node)
7815 return 1;
7817 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
7818 if (fntype == error_mark_node)
7819 return 1;
7821 /* Place the explicitly specified arguments in TARGS. */
7822 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7823 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
7826 parms = TYPE_ARG_TYPES (fntype);
7827 /* Never do unification on the 'this' parameter. */
7828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
7829 parms = TREE_CHAIN (parms);
7831 if (return_type)
7833 /* We've been given a return type to match, prepend it. */
7834 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
7835 args = tree_cons (NULL_TREE, return_type, args);
7836 if (len >= 0)
7837 ++len;
7840 /* We allow incomplete unification without an error message here
7841 because the standard doesn't seem to explicitly prohibit it. Our
7842 callers must be ready to deal with unification failures in any
7843 event. */
7844 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7845 targs, parms, args, /*subr=*/0,
7846 strict, /*allow_incomplete*/1, len);
7848 if (result == 0)
7849 /* All is well so far. Now, check:
7851 [temp.deduct]
7853 When all template arguments have been deduced, all uses of
7854 template parameters in nondeduced contexts are replaced with
7855 the corresponding deduced argument values. If the
7856 substitution results in an invalid type, as described above,
7857 type deduction fails. */
7858 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
7859 == error_mark_node)
7860 return 1;
7862 return result;
7865 /* Adjust types before performing type deduction, as described in
7866 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7867 sections are symmetric. PARM is the type of a function parameter
7868 or the return type of the conversion function. ARG is the type of
7869 the argument passed to the call, or the type of the value
7870 initialized with the result of the conversion function. */
7872 static int
7873 maybe_adjust_types_for_deduction (strict, parm, arg)
7874 unification_kind_t strict;
7875 tree* parm;
7876 tree* arg;
7878 int result = 0;
7880 switch (strict)
7882 case DEDUCE_CALL:
7883 break;
7885 case DEDUCE_CONV:
7887 /* Swap PARM and ARG throughout the remainder of this
7888 function; the handling is precisely symmetric since PARM
7889 will initialize ARG rather than vice versa. */
7890 tree* temp = parm;
7891 parm = arg;
7892 arg = temp;
7893 break;
7896 case DEDUCE_EXACT:
7897 /* There is nothing to do in this case. */
7898 return 0;
7900 case DEDUCE_ORDER:
7901 /* DR 214. [temp.func.order] is underspecified, and leads to no
7902 ordering between things like `T *' and `T const &' for `U *'.
7903 The former has T=U and the latter T=U*. The former looks more
7904 specialized and John Spicer considers it well-formed (the EDG
7905 compiler accepts it).
7907 John also confirms that deduction should proceed as in a function
7908 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
7909 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7910 to an actual call can have such a type.
7912 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7913 If only ARG is a REFERENCE_TYPE, we look through that and then
7914 proceed as with DEDUCE_CALL (which could further convert it). */
7915 if (TREE_CODE (*arg) == REFERENCE_TYPE)
7917 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7918 return 0;
7919 *arg = TREE_TYPE (*arg);
7921 break;
7922 default:
7923 abort ();
7926 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7928 /* [temp.deduct.call]
7930 If P is not a reference type:
7932 --If A is an array type, the pointer type produced by the
7933 array-to-pointer standard conversion (_conv.array_) is
7934 used in place of A for type deduction; otherwise,
7936 --If A is a function type, the pointer type produced by
7937 the function-to-pointer standard conversion
7938 (_conv.func_) is used in place of A for type deduction;
7939 otherwise,
7941 --If A is a cv-qualified type, the top level
7942 cv-qualifiers of A's type are ignored for type
7943 deduction. */
7944 if (TREE_CODE (*arg) == ARRAY_TYPE)
7945 *arg = build_pointer_type (TREE_TYPE (*arg));
7946 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
7947 *arg = build_pointer_type (*arg);
7948 else
7949 *arg = TYPE_MAIN_VARIANT (*arg);
7952 /* [temp.deduct.call]
7954 If P is a cv-qualified type, the top level cv-qualifiers
7955 of P's type are ignored for type deduction. If P is a
7956 reference type, the type referred to by P is used for
7957 type deduction. */
7958 *parm = TYPE_MAIN_VARIANT (*parm);
7959 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7961 *parm = TREE_TYPE (*parm);
7962 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
7964 return result;
7967 /* Most parms like fn_type_unification.
7969 If SUBR is 1, we're being called recursively (to unify the
7970 arguments of a function or method parameter of a function
7971 template). */
7973 static int
7974 type_unification_real (tparms, targs, xparms, xargs, subr,
7975 strict, allow_incomplete, xlen)
7976 tree tparms, targs, xparms, xargs;
7977 int subr;
7978 unification_kind_t strict;
7979 int allow_incomplete, xlen;
7981 tree parm, arg;
7982 int i;
7983 int ntparms = TREE_VEC_LENGTH (tparms);
7984 int sub_strict;
7985 int saw_undeduced = 0;
7986 tree parms, args;
7987 int len;
7989 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7990 my_friendly_assert (xparms == NULL_TREE
7991 || TREE_CODE (xparms) == TREE_LIST, 290);
7992 /* ARGS could be NULL (via a call from parse.y to
7993 build_x_function_call). */
7994 if (xargs)
7995 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
7996 my_friendly_assert (ntparms > 0, 292);
7998 switch (strict)
8000 case DEDUCE_CALL:
8001 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8002 | UNIFY_ALLOW_DERIVED);
8003 break;
8005 case DEDUCE_CONV:
8006 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8007 break;
8009 case DEDUCE_EXACT:
8010 sub_strict = UNIFY_ALLOW_NONE;
8011 break;
8013 case DEDUCE_ORDER:
8014 sub_strict = UNIFY_ALLOW_NONE;
8015 break;
8017 default:
8018 abort ();
8021 if (xlen == 0)
8022 return 0;
8024 again:
8025 parms = xparms;
8026 args = xargs;
8027 len = xlen;
8029 while (parms
8030 && parms != void_list_node
8031 && args
8032 && args != void_list_node)
8034 parm = TREE_VALUE (parms);
8035 parms = TREE_CHAIN (parms);
8036 arg = TREE_VALUE (args);
8037 args = TREE_CHAIN (args);
8039 if (arg == error_mark_node)
8040 return 1;
8041 if (arg == unknown_type_node)
8042 /* We can't deduce anything from this, but we might get all the
8043 template args from other function args. */
8044 continue;
8046 /* Conversions will be performed on a function argument that
8047 corresponds with a function parameter that contains only
8048 non-deducible template parameters and explicitly specified
8049 template parameters. */
8050 if (! uses_template_parms (parm))
8052 tree type;
8054 if (!TYPE_P (arg))
8055 type = TREE_TYPE (arg);
8056 else
8058 type = arg;
8059 arg = NULL_TREE;
8062 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
8064 if (same_type_p (parm, type))
8065 continue;
8067 else
8068 /* It might work; we shouldn't check now, because we might
8069 get into infinite recursion. Overload resolution will
8070 handle it. */
8071 continue;
8073 return 1;
8076 if (!TYPE_P (arg))
8078 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8079 if (type_unknown_p (arg))
8081 /* [temp.deduct.type] A template-argument can be deduced from
8082 a pointer to function or pointer to member function
8083 argument if the set of overloaded functions does not
8084 contain function templates and at most one of a set of
8085 overloaded functions provides a unique match. */
8087 if (resolve_overloaded_unification
8088 (tparms, targs, parm, arg, strict, sub_strict)
8089 != 0)
8090 return 1;
8091 continue;
8093 arg = TREE_TYPE (arg);
8097 int arg_strict = sub_strict;
8099 if (!subr)
8100 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8102 if (unify (tparms, targs, parm, arg, arg_strict))
8103 return 1;
8106 /* Are we done with the interesting parms? */
8107 if (--len == 0)
8108 goto done;
8110 /* Fail if we've reached the end of the parm list, and more args
8111 are present, and the parm list isn't variadic. */
8112 if (args && args != void_list_node && parms == void_list_node)
8113 return 1;
8114 /* Fail if parms are left and they don't have default values. */
8115 if (parms
8116 && parms != void_list_node
8117 && TREE_PURPOSE (parms) == NULL_TREE)
8118 return 1;
8120 done:
8121 if (!subr)
8122 for (i = 0; i < ntparms; i++)
8123 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8125 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8127 /* If this is an undeduced nontype parameter that depends on
8128 a type parameter, try another pass; its type may have been
8129 deduced from a later argument than the one from which
8130 this parameter can be deduced. */
8131 if (TREE_CODE (tparm) == PARM_DECL
8132 && uses_template_parms (TREE_TYPE (tparm))
8133 && !saw_undeduced++)
8134 goto again;
8136 if (!allow_incomplete)
8137 error ("incomplete type unification");
8138 return 2;
8140 return 0;
8143 /* Subroutine of type_unification_real. Args are like the variables at the
8144 call site. ARG is an overloaded function (or template-id); we try
8145 deducing template args from each of the overloads, and if only one
8146 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8148 static int
8149 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8150 sub_strict)
8151 tree tparms, targs, parm, arg;
8152 unification_kind_t strict;
8153 int sub_strict;
8155 tree tempargs = copy_node (targs);
8156 int good = 0;
8158 if (TREE_CODE (arg) == ADDR_EXPR)
8159 arg = TREE_OPERAND (arg, 0);
8161 if (TREE_CODE (arg) == COMPONENT_REF)
8162 /* Handle `&x' where `x' is some static or non-static member
8163 function name. */
8164 arg = TREE_OPERAND (arg, 1);
8166 if (TREE_CODE (arg) == OFFSET_REF)
8167 arg = TREE_OPERAND (arg, 1);
8169 /* Strip baselink information. */
8170 while (TREE_CODE (arg) == TREE_LIST)
8171 arg = TREE_VALUE (arg);
8173 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8175 /* If we got some explicit template args, we need to plug them into
8176 the affected templates before we try to unify, in case the
8177 explicit args will completely resolve the templates in question. */
8179 tree expl_subargs = TREE_OPERAND (arg, 1);
8180 arg = TREE_OPERAND (arg, 0);
8182 for (; arg; arg = OVL_NEXT (arg))
8184 tree fn = OVL_CURRENT (arg);
8185 tree subargs, elem;
8187 if (TREE_CODE (fn) != TEMPLATE_DECL)
8188 continue;
8190 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8191 expl_subargs);
8192 if (subargs)
8194 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
8195 if (TREE_CODE (elem) == METHOD_TYPE)
8196 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8197 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8198 strict, sub_strict);
8202 else if (TREE_CODE (arg) == OVERLOAD)
8204 for (; arg; arg = OVL_NEXT (arg))
8206 tree type = TREE_TYPE (OVL_CURRENT (arg));
8207 if (TREE_CODE (type) == METHOD_TYPE)
8208 type = build_ptrmemfunc_type (build_pointer_type (type));
8209 good += try_one_overload (tparms, targs, tempargs, parm,
8210 type,
8211 strict, sub_strict);
8214 else
8215 abort ();
8217 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8218 to function or pointer to member function argument if the set of
8219 overloaded functions does not contain function templates and at most
8220 one of a set of overloaded functions provides a unique match.
8222 So if we found multiple possibilities, we return success but don't
8223 deduce anything. */
8225 if (good == 1)
8227 int i = TREE_VEC_LENGTH (targs);
8228 for (; i--; )
8229 if (TREE_VEC_ELT (tempargs, i))
8230 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8232 if (good)
8233 return 0;
8235 return 1;
8238 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8239 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8240 different overloads deduce different arguments for a given parm.
8241 Returns 1 on success. */
8243 static int
8244 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8245 sub_strict)
8246 tree tparms, orig_targs, targs, parm, arg;
8247 unification_kind_t strict;
8248 int sub_strict;
8250 int nargs;
8251 tree tempargs;
8252 int i;
8254 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8255 to function or pointer to member function argument if the set of
8256 overloaded functions does not contain function templates and at most
8257 one of a set of overloaded functions provides a unique match.
8259 So if this is a template, just return success. */
8261 if (uses_template_parms (arg))
8262 return 1;
8264 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8266 /* We don't copy orig_targs for this because if we have already deduced
8267 some template args from previous args, unify would complain when we
8268 try to deduce a template parameter for the same argument, even though
8269 there isn't really a conflict. */
8270 nargs = TREE_VEC_LENGTH (targs);
8271 tempargs = make_tree_vec (nargs);
8273 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8274 return 0;
8276 /* First make sure we didn't deduce anything that conflicts with
8277 explicitly specified args. */
8278 for (i = nargs; i--; )
8280 tree elt = TREE_VEC_ELT (tempargs, i);
8281 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8283 if (elt == NULL_TREE)
8284 continue;
8285 else if (uses_template_parms (elt))
8287 /* Since we're unifying against ourselves, we will fill in template
8288 args used in the function parm list with our own template parms.
8289 Discard them. */
8290 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8291 continue;
8293 else if (oldelt && ! template_args_equal (oldelt, elt))
8294 return 0;
8297 for (i = nargs; i--; )
8299 tree elt = TREE_VEC_ELT (tempargs, i);
8301 if (elt)
8302 TREE_VEC_ELT (targs, i) = elt;
8305 return 1;
8308 /* Verify that nondeduce template argument agrees with the type
8309 obtained from argument deduction. Return nonzero if the
8310 verification fails.
8312 For example:
8314 struct A { typedef int X; };
8315 template <class T, class U> struct C {};
8316 template <class T> struct C<T, typename T::X> {};
8318 Then with the instantiation `C<A, int>', we can deduce that
8319 `T' is `A' but unify () does not check whether `typename T::X'
8320 is `int'. This function ensure that they agree.
8322 TARGS, PARMS are the same as the arguments of unify.
8323 ARGS contains template arguments from all levels. */
8325 static int
8326 verify_class_unification (targs, parms, args)
8327 tree targs, parms, args;
8329 int i;
8330 int nparms = TREE_VEC_LENGTH (parms);
8331 tree new_parms = tsubst (parms, add_outermost_template_args (args, targs),
8332 tf_none, NULL_TREE);
8333 if (new_parms == error_mark_node)
8334 return 1;
8336 args = INNERMOST_TEMPLATE_ARGS (args);
8338 for (i = 0; i < nparms; i++)
8340 tree parm = TREE_VEC_ELT (new_parms, i);
8341 tree arg = TREE_VEC_ELT (args, i);
8343 /* In case we are deducing from a function argument of a function
8344 templates, some parameters may not be deduced yet. So we
8345 make sure that only fully substituted elements of PARM are
8346 compared below. */
8348 if (!uses_template_parms (parm) && !template_args_equal (parm, arg))
8349 return 1;
8351 return 0;
8354 /* PARM is a template class (perhaps with unbound template
8355 parameters). ARG is a fully instantiated type. If ARG can be
8356 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8357 TARGS are as for unify. */
8359 static tree
8360 try_class_unification (tparms, targs, parm, arg)
8361 tree tparms;
8362 tree targs;
8363 tree parm;
8364 tree arg;
8366 tree copy_of_targs;
8368 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8369 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8370 return NULL_TREE;
8372 /* We need to make a new template argument vector for the call to
8373 unify. If we used TARGS, we'd clutter it up with the result of
8374 the attempted unification, even if this class didn't work out.
8375 We also don't want to commit ourselves to all the unifications
8376 we've already done, since unification is supposed to be done on
8377 an argument-by-argument basis. In other words, consider the
8378 following pathological case:
8380 template <int I, int J, int K>
8381 struct S {};
8383 template <int I, int J>
8384 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8386 template <int I, int J, int K>
8387 void f(S<I, J, K>, S<I, I, I>);
8389 void g() {
8390 S<0, 0, 0> s0;
8391 S<0, 1, 2> s2;
8393 f(s0, s2);
8396 Now, by the time we consider the unification involving `s2', we
8397 already know that we must have `f<0, 0, 0>'. But, even though
8398 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8399 because there are two ways to unify base classes of S<0, 1, 2>
8400 with S<I, I, I>. If we kept the already deduced knowledge, we
8401 would reject the possibility I=1. */
8402 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
8404 /* If unification failed, we're done. */
8405 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8406 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
8407 return NULL_TREE;
8409 return arg;
8412 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8413 have already discovered to be satisfactory. ARG_BINFO is the binfo
8414 for the base class of ARG that we are currently examining. */
8416 static tree
8417 get_template_base_recursive (tparms, targs, parm,
8418 arg_binfo, rval, flags)
8419 tree tparms;
8420 tree targs;
8421 tree arg_binfo;
8422 tree rval;
8423 tree parm;
8424 int flags;
8426 tree binfos;
8427 int i, n_baselinks;
8428 tree arg = BINFO_TYPE (arg_binfo);
8430 if (!(flags & GTB_IGNORE_TYPE))
8432 tree r = try_class_unification (tparms, targs,
8433 parm, arg);
8435 /* If there is more than one satisfactory baseclass, then:
8437 [temp.deduct.call]
8439 If they yield more than one possible deduced A, the type
8440 deduction fails.
8442 applies. */
8443 if (r && rval && !same_type_p (r, rval))
8444 return error_mark_node;
8445 else if (r)
8446 rval = r;
8449 binfos = BINFO_BASETYPES (arg_binfo);
8450 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8452 /* Process base types. */
8453 for (i = 0; i < n_baselinks; i++)
8455 tree base_binfo = TREE_VEC_ELT (binfos, i);
8456 int this_virtual;
8458 /* Skip this base, if we've already seen it. */
8459 if (BINFO_MARKED (base_binfo))
8460 continue;
8462 this_virtual =
8463 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8465 /* When searching for a non-virtual, we cannot mark virtually
8466 found binfos. */
8467 if (! this_virtual)
8468 SET_BINFO_MARKED (base_binfo);
8470 rval = get_template_base_recursive (tparms, targs,
8471 parm,
8472 base_binfo,
8473 rval,
8474 GTB_VIA_VIRTUAL * this_virtual);
8476 /* If we discovered more than one matching base class, we can
8477 stop now. */
8478 if (rval == error_mark_node)
8479 return error_mark_node;
8482 return rval;
8485 /* Given a template type PARM and a class type ARG, find the unique
8486 base type in ARG that is an instance of PARM. We do not examine
8487 ARG itself; only its base-classes. If there is no appropriate base
8488 class, return NULL_TREE. If there is more than one, return
8489 error_mark_node. PARM may be the type of a partial specialization,
8490 as well as a plain template type. Used by unify. */
8492 static tree
8493 get_template_base (tparms, targs, parm, arg)
8494 tree tparms;
8495 tree targs;
8496 tree parm;
8497 tree arg;
8499 tree rval;
8500 tree arg_binfo;
8502 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8504 arg_binfo = TYPE_BINFO (complete_type (arg));
8505 rval = get_template_base_recursive (tparms, targs,
8506 parm, arg_binfo,
8507 NULL_TREE,
8508 GTB_IGNORE_TYPE);
8510 /* Since get_template_base_recursive marks the bases classes, we
8511 must unmark them here. */
8512 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
8514 return rval;
8517 /* Returns the level of DECL, which declares a template parameter. */
8519 static int
8520 template_decl_level (decl)
8521 tree decl;
8523 switch (TREE_CODE (decl))
8525 case TYPE_DECL:
8526 case TEMPLATE_DECL:
8527 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8529 case PARM_DECL:
8530 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8532 default:
8533 abort ();
8534 return 0;
8538 /* Decide whether ARG can be unified with PARM, considering only the
8539 cv-qualifiers of each type, given STRICT as documented for unify.
8540 Returns non-zero iff the unification is OK on that basis.*/
8542 static int
8543 check_cv_quals_for_unify (strict, arg, parm)
8544 int strict;
8545 tree arg;
8546 tree parm;
8548 int arg_quals = cp_type_quals (arg);
8549 int parm_quals = cp_type_quals (parm);
8551 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
8553 /* If the cvr quals of parm will not unify with ARG, they'll be
8554 ignored in instantiation, so we have to do the same here. */
8555 if (TREE_CODE (arg) == REFERENCE_TYPE
8556 || TREE_CODE (arg) == FUNCTION_TYPE
8557 || TREE_CODE (arg) == METHOD_TYPE)
8558 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
8559 if (!POINTER_TYPE_P (arg) &&
8560 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
8561 parm_quals &= ~TYPE_QUAL_RESTRICT;
8564 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
8565 && (arg_quals & parm_quals) != parm_quals)
8566 return 0;
8568 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
8569 && (parm_quals & arg_quals) != arg_quals)
8570 return 0;
8572 return 1;
8575 /* Takes parameters as for type_unification. Returns 0 if the
8576 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8577 bitwise or of the following flags:
8579 UNIFY_ALLOW_NONE:
8580 Require an exact match between PARM and ARG.
8581 UNIFY_ALLOW_MORE_CV_QUAL:
8582 Allow the deduced ARG to be more cv-qualified (by qualification
8583 conversion) than ARG.
8584 UNIFY_ALLOW_LESS_CV_QUAL:
8585 Allow the deduced ARG to be less cv-qualified than ARG.
8586 UNIFY_ALLOW_DERIVED:
8587 Allow the deduced ARG to be a template base class of ARG,
8588 or a pointer to a template base class of the type pointed to by
8589 ARG.
8590 UNIFY_ALLOW_INTEGER:
8591 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8592 case for more information.
8593 UNIFY_ALLOW_OUTER_LEVEL:
8594 This is the outermost level of a deduction. Used to determine validity
8595 of qualification conversions. A valid qualification conversion must
8596 have const qualified pointers leading up to the inner type which
8597 requires additional CV quals, except at the outer level, where const
8598 is not required [conv.qual]. It would be normal to set this flag in
8599 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8600 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8601 This is the outermost level of a deduction, and PARM can be more CV
8602 qualified at this point.
8603 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8604 This is the outermost level of a deduction, and PARM can be less CV
8605 qualified at this point.
8606 UNIFY_ALLOW_MAX_CORRECTION:
8607 This is an INTEGER_TYPE's maximum value. Used if the range may
8608 have been derived from a size specification, such as an array size.
8609 If the size was given by a nontype template parameter N, the maximum
8610 value will have the form N-1. The flag says that we can (and indeed
8611 must) unify N with (ARG + 1), an exception to the normal rules on
8612 folding PARM. */
8614 static int
8615 unify (tparms, targs, parm, arg, strict)
8616 tree tparms, targs, parm, arg;
8617 int strict;
8619 int idx;
8620 tree targ;
8621 tree tparm;
8622 int strict_in = strict;
8624 /* I don't think this will do the right thing with respect to types.
8625 But the only case I've seen it in so far has been array bounds, where
8626 signedness is the only information lost, and I think that will be
8627 okay. */
8628 while (TREE_CODE (parm) == NOP_EXPR)
8629 parm = TREE_OPERAND (parm, 0);
8631 if (arg == error_mark_node)
8632 return 1;
8633 if (arg == unknown_type_node)
8634 /* We can't deduce anything from this, but we might get all the
8635 template args from other function args. */
8636 return 0;
8638 /* If PARM uses template parameters, then we can't bail out here,
8639 even if ARG == PARM, since we won't record unifications for the
8640 template parameters. We might need them if we're trying to
8641 figure out which of two things is more specialized. */
8642 if (arg == parm && !uses_template_parms (parm))
8643 return 0;
8645 /* Immediately reject some pairs that won't unify because of
8646 cv-qualification mismatches. */
8647 if (TREE_CODE (arg) == TREE_CODE (parm)
8648 && TYPE_P (arg)
8649 /* It is the elements of the array which hold the cv quals of an array
8650 type, and the elements might be template type parms. We'll check
8651 when we recurse. */
8652 && TREE_CODE (arg) != ARRAY_TYPE
8653 /* We check the cv-qualifiers when unifying with template type
8654 parameters below. We want to allow ARG `const T' to unify with
8655 PARM `T' for example, when computing which of two templates
8656 is more specialized, for example. */
8657 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8658 && !check_cv_quals_for_unify (strict_in, arg, parm))
8659 return 1;
8661 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
8662 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
8663 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
8664 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
8665 strict &= ~UNIFY_ALLOW_DERIVED;
8666 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8667 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
8668 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
8670 switch (TREE_CODE (parm))
8672 case TYPENAME_TYPE:
8673 case SCOPE_REF:
8674 case UNBOUND_CLASS_TEMPLATE:
8675 /* In a type which contains a nested-name-specifier, template
8676 argument values cannot be deduced for template parameters used
8677 within the nested-name-specifier. */
8678 return 0;
8680 case TEMPLATE_TYPE_PARM:
8681 case TEMPLATE_TEMPLATE_PARM:
8682 case BOUND_TEMPLATE_TEMPLATE_PARM:
8683 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8685 if (TEMPLATE_TYPE_LEVEL (parm)
8686 != template_decl_level (tparm))
8687 /* The PARM is not one we're trying to unify. Just check
8688 to see if it matches ARG. */
8689 return (TREE_CODE (arg) == TREE_CODE (parm)
8690 && same_type_p (parm, arg)) ? 0 : 1;
8691 idx = TEMPLATE_TYPE_IDX (parm);
8692 targ = TREE_VEC_ELT (targs, idx);
8693 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
8695 /* Check for mixed types and values. */
8696 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8697 && TREE_CODE (tparm) != TYPE_DECL)
8698 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8699 && TREE_CODE (tparm) != TEMPLATE_DECL))
8700 return 1;
8702 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8704 /* ARG must be constructed from a template class or a template
8705 template parameter. */
8706 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
8707 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
8708 return 1;
8711 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8712 tree parmvec = TYPE_TI_ARGS (parm);
8713 tree argvec = TYPE_TI_ARGS (arg);
8714 tree argtmplvec
8715 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
8716 int i;
8718 /* The parameter and argument roles have to be switched here
8719 in order to handle default arguments properly. For example,
8720 template<template <class> class TT> void f(TT<int>)
8721 should be able to accept vector<int> which comes from
8722 template <class T, class Allocator = allocator>
8723 class vector. */
8725 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8726 == error_mark_node)
8727 return 1;
8729 /* Deduce arguments T, i from TT<T> or TT<i>.
8730 We check each element of PARMVEC and ARGVEC individually
8731 rather than the whole TREE_VEC since they can have
8732 different number of elements. */
8734 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8736 tree t = TREE_VEC_ELT (parmvec, i);
8738 if (unify (tparms, targs, t,
8739 TREE_VEC_ELT (argvec, i),
8740 UNIFY_ALLOW_NONE))
8741 return 1;
8744 arg = TYPE_TI_TEMPLATE (arg);
8746 /* Fall through to deduce template name. */
8749 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8750 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8752 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8754 /* Simple cases: Value already set, does match or doesn't. */
8755 if (targ != NULL_TREE && template_args_equal (targ, arg))
8756 return 0;
8757 else if (targ)
8758 return 1;
8760 else
8762 /* If PARM is `const T' and ARG is only `int', we don't have
8763 a match unless we are allowing additional qualification.
8764 If ARG is `const int' and PARM is just `T' that's OK;
8765 that binds `const int' to `T'. */
8766 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
8767 arg, parm))
8768 return 1;
8770 /* Consider the case where ARG is `const volatile int' and
8771 PARM is `const T'. Then, T should be `volatile int'. */
8772 arg = cp_build_qualified_type_real
8773 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
8774 if (arg == error_mark_node)
8775 return 1;
8777 /* Simple cases: Value already set, does match or doesn't. */
8778 if (targ != NULL_TREE && same_type_p (targ, arg))
8779 return 0;
8780 else if (targ)
8781 return 1;
8784 /* Make sure that ARG is not a variable-sized array. (Note that
8785 were talking about variable-sized arrays (like `int[n]'),
8786 rather than arrays of unknown size (like `int[]').) We'll
8787 get very confused by such a type since the bound of the array
8788 will not be computable in an instantiation. Besides, such
8789 types are not allowed in ISO C++, so we can do as we please
8790 here. */
8791 if (TREE_CODE (arg) == ARRAY_TYPE
8792 && !uses_template_parms (arg)
8793 && TYPE_DOMAIN (arg)
8794 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8795 != INTEGER_CST))
8796 return 1;
8798 TREE_VEC_ELT (targs, idx) = arg;
8799 return 0;
8801 case TEMPLATE_PARM_INDEX:
8802 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8804 if (TEMPLATE_PARM_LEVEL (parm)
8805 != template_decl_level (tparm))
8806 /* The PARM is not one we're trying to unify. Just check
8807 to see if it matches ARG. */
8808 return (TREE_CODE (arg) == TREE_CODE (parm)
8809 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
8811 idx = TEMPLATE_PARM_IDX (parm);
8812 targ = TREE_VEC_ELT (targs, idx);
8814 if (targ)
8816 int i = (cp_tree_equal (targ, arg) > 0);
8817 if (i == 1)
8818 return 0;
8819 else if (i == 0)
8820 return 1;
8821 else
8822 abort ();
8825 /* [temp.deduct.type] If, in the declaration of a function template
8826 with a non-type template-parameter, the non-type
8827 template-parameter is used in an expression in the function
8828 parameter-list and, if the corresponding template-argument is
8829 deduced, the template-argument type shall match the type of the
8830 template-parameter exactly, except that a template-argument
8831 deduced from an array bound may be of any integral type.
8832 The non-type parameter might use already deduced type parameters. */
8833 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
8834 if (same_type_p (TREE_TYPE (arg), tparm))
8835 /* OK */;
8836 else if ((strict & UNIFY_ALLOW_INTEGER)
8837 && (TREE_CODE (tparm) == INTEGER_TYPE
8838 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8839 /* OK */;
8840 else if (uses_template_parms (tparm))
8841 /* We haven't deduced the type of this parameter yet. Try again
8842 later. */
8843 return 0;
8844 else
8845 return 1;
8847 TREE_VEC_ELT (targs, idx) = arg;
8848 return 0;
8850 case POINTER_TYPE:
8852 if (TREE_CODE (arg) != POINTER_TYPE)
8853 return 1;
8855 /* [temp.deduct.call]
8857 A can be another pointer or pointer to member type that can
8858 be converted to the deduced A via a qualification
8859 conversion (_conv.qual_).
8861 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8862 This will allow for additional cv-qualification of the
8863 pointed-to types if appropriate. */
8865 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
8866 /* The derived-to-base conversion only persists through one
8867 level of pointers. */
8868 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
8870 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
8871 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
8873 /* Avoid getting confused about cv-quals; don't recurse here.
8874 Pointers to members should really be just OFFSET_TYPE, not
8875 this two-level nonsense... */
8877 parm = TREE_TYPE (parm);
8878 arg = TREE_TYPE (arg);
8879 goto offset;
8882 return unify (tparms, targs, TREE_TYPE (parm),
8883 TREE_TYPE (arg), strict);
8886 case REFERENCE_TYPE:
8887 if (TREE_CODE (arg) != REFERENCE_TYPE)
8888 return 1;
8889 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8890 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8892 case ARRAY_TYPE:
8893 if (TREE_CODE (arg) != ARRAY_TYPE)
8894 return 1;
8895 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8896 != (TYPE_DOMAIN (arg) == NULL_TREE))
8897 return 1;
8898 if (TYPE_DOMAIN (parm) != NULL_TREE
8899 && unify (tparms, targs, TYPE_DOMAIN (parm),
8900 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8901 return 1;
8902 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8903 UNIFY_ALLOW_NONE);
8905 case REAL_TYPE:
8906 case COMPLEX_TYPE:
8907 case VECTOR_TYPE:
8908 case INTEGER_TYPE:
8909 case BOOLEAN_TYPE:
8910 case VOID_TYPE:
8911 if (TREE_CODE (arg) != TREE_CODE (parm))
8912 return 1;
8914 if (TREE_CODE (parm) == INTEGER_TYPE
8915 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8917 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
8918 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
8919 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8920 return 1;
8921 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
8922 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
8923 TYPE_MAX_VALUE (arg),
8924 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
8925 return 1;
8927 /* We have already checked cv-qualification at the top of the
8928 function. */
8929 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
8930 return 1;
8932 /* As far as unification is concerned, this wins. Later checks
8933 will invalidate it if necessary. */
8934 return 0;
8936 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8937 /* Type INTEGER_CST can come from ordinary constant template args. */
8938 case INTEGER_CST:
8939 while (TREE_CODE (arg) == NOP_EXPR)
8940 arg = TREE_OPERAND (arg, 0);
8942 if (TREE_CODE (arg) != INTEGER_CST)
8943 return 1;
8944 return !tree_int_cst_equal (parm, arg);
8946 case TREE_VEC:
8948 int i;
8949 if (TREE_CODE (arg) != TREE_VEC)
8950 return 1;
8951 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8952 return 1;
8953 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
8954 if (unify (tparms, targs,
8955 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
8956 UNIFY_ALLOW_NONE))
8957 return 1;
8958 return 0;
8961 case RECORD_TYPE:
8962 case UNION_TYPE:
8963 if (TREE_CODE (arg) != TREE_CODE (parm))
8964 return 1;
8966 if (TYPE_PTRMEMFUNC_P (parm))
8968 if (!TYPE_PTRMEMFUNC_P (arg))
8969 return 1;
8971 return unify (tparms, targs,
8972 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8973 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8974 strict);
8977 if (CLASSTYPE_TEMPLATE_INFO (parm))
8979 tree t = NULL_TREE;
8981 if (strict_in & UNIFY_ALLOW_DERIVED)
8983 /* First, we try to unify the PARM and ARG directly. */
8984 t = try_class_unification (tparms, targs,
8985 parm, arg);
8987 if (!t)
8989 /* Fallback to the special case allowed in
8990 [temp.deduct.call]:
8992 If P is a class, and P has the form
8993 template-id, then A can be a derived class of
8994 the deduced A. Likewise, if P is a pointer to
8995 a class of the form template-id, A can be a
8996 pointer to a derived class pointed to by the
8997 deduced A. */
8998 t = get_template_base (tparms, targs,
8999 parm, arg);
9001 if (! t || t == error_mark_node)
9002 return 1;
9005 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9006 && (CLASSTYPE_TI_TEMPLATE (parm)
9007 == CLASSTYPE_TI_TEMPLATE (arg)))
9008 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9009 Then, we should unify `int' and `U'. */
9010 t = arg;
9011 else
9012 /* There's no chance of unification succeeding. */
9013 return 1;
9015 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
9016 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
9018 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
9019 return 1;
9020 return 0;
9022 case METHOD_TYPE:
9023 case FUNCTION_TYPE:
9024 if (TREE_CODE (arg) != TREE_CODE (parm))
9025 return 1;
9027 if (unify (tparms, targs, TREE_TYPE (parm),
9028 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
9029 return 1;
9030 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
9031 TYPE_ARG_TYPES (arg), 1,
9032 DEDUCE_EXACT, 0, -1);
9034 case OFFSET_TYPE:
9035 offset:
9036 if (TREE_CODE (arg) != OFFSET_TYPE)
9037 return 1;
9038 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
9039 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
9040 return 1;
9041 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9042 strict);
9044 case CONST_DECL:
9045 if (arg != decl_constant_value (parm))
9046 return 1;
9047 return 0;
9049 case TEMPLATE_DECL:
9050 /* Matched cases are handled by the ARG == PARM test above. */
9051 return 1;
9053 case MINUS_EXPR:
9054 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9055 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
9057 /* We handle this case specially, since it comes up with
9058 arrays. In particular, something like:
9060 template <int N> void f(int (&x)[N]);
9062 Here, we are trying to unify the range type, which
9063 looks like [0 ... (N - 1)]. */
9064 tree t, t1, t2;
9065 t1 = TREE_OPERAND (parm, 0);
9066 t2 = TREE_OPERAND (parm, 1);
9068 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
9070 return unify (tparms, targs, t1, t, strict);
9072 /* else fall through */
9074 default:
9075 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
9078 /* We're looking at an expression. This can happen with
9079 something like:
9081 template <int I>
9082 void foo(S<I>, S<I + 2>);
9084 This is a "nondeduced context":
9086 [deduct.type]
9088 The nondeduced contexts are:
9090 --A type that is a template-id in which one or more of
9091 the template-arguments is an expression that references
9092 a template-parameter.
9094 In these cases, we assume deduction succeeded, but don't
9095 actually infer any unifications. */
9097 if (!uses_template_parms (parm)
9098 && !template_args_equal (parm, arg))
9099 return 1;
9100 else
9101 return 0;
9103 else
9104 sorry ("use of `%s' in template type unification",
9105 tree_code_name [(int) TREE_CODE (parm)]);
9107 return 1;
9111 /* Called if RESULT is explicitly instantiated, or is a member of an
9112 explicitly instantiated class, or if using -frepo and the
9113 instantiation of RESULT has been assigned to this file. */
9115 void
9116 mark_decl_instantiated (result, extern_p)
9117 tree result;
9118 int extern_p;
9120 if (TREE_CODE (result) != FUNCTION_DECL)
9121 /* The TREE_PUBLIC flag for function declarations will have been
9122 set correctly by tsubst. */
9123 TREE_PUBLIC (result) = 1;
9125 /* We used to set this unconditionally; we moved that to
9126 do_decl_instantiation so it wouldn't get set on members of
9127 explicit class template instantiations. But we still need to set
9128 it here for the 'extern template' case in order to suppress
9129 implicit instantiations. */
9130 if (extern_p)
9131 SET_DECL_EXPLICIT_INSTANTIATION (result);
9133 if (! extern_p)
9135 DECL_INTERFACE_KNOWN (result) = 1;
9136 DECL_NOT_REALLY_EXTERN (result) = 1;
9138 /* Always make artificials weak. */
9139 if (DECL_ARTIFICIAL (result) && flag_weak)
9140 comdat_linkage (result);
9141 /* For WIN32 we also want to put explicit instantiations in
9142 linkonce sections. */
9143 else if (TREE_PUBLIC (result))
9144 maybe_make_one_only (result);
9146 else if (TREE_CODE (result) == FUNCTION_DECL)
9147 defer_fn (result);
9150 /* Given two function templates PAT1 and PAT2, return:
9152 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9154 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9155 -1 if PAT2 is more specialized than PAT1.
9156 0 if neither is more specialized.
9158 LEN is passed through to fn_type_unification. */
9161 more_specialized (pat1, pat2, deduce, len)
9162 tree pat1, pat2;
9163 int deduce;
9164 int len;
9166 tree targs;
9167 int winner = 0;
9169 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9170 NULL_TREE, 0, deduce, len);
9171 if (targs)
9172 --winner;
9174 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9175 NULL_TREE, 0, deduce, len);
9176 if (targs)
9177 ++winner;
9179 return winner;
9182 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9184 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9185 -1 if PAT2 is more specialized than PAT1.
9186 0 if neither is more specialized. */
9189 more_specialized_class (pat1, pat2)
9190 tree pat1, pat2;
9192 tree targs;
9193 int winner = 0;
9195 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9196 TREE_PURPOSE (pat2));
9197 if (targs)
9198 --winner;
9200 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9201 TREE_PURPOSE (pat1));
9202 if (targs)
9203 ++winner;
9205 return winner;
9208 /* Return the template arguments that will produce the function signature
9209 DECL from the function template FN, with the explicit template
9210 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9211 also match. Return NULL_TREE if no satisfactory arguments could be
9212 found. DEDUCE and LEN are passed through to fn_type_unification. */
9214 static tree
9215 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9216 tree fn, decl, explicit_args;
9217 int check_rettype, deduce, len;
9219 int ntparms = DECL_NTPARMS (fn);
9220 tree targs = make_tree_vec (ntparms);
9221 tree decl_type;
9222 tree decl_arg_types;
9223 int i;
9225 /* Substitute the explicit template arguments into the type of DECL.
9226 The call to fn_type_unification will handle substitution into the
9227 FN. */
9228 decl_type = TREE_TYPE (decl);
9229 if (explicit_args && uses_template_parms (decl_type))
9231 tree tmpl;
9232 tree converted_args;
9234 if (DECL_TEMPLATE_INFO (decl))
9235 tmpl = DECL_TI_TEMPLATE (decl);
9236 else
9237 /* We can get here for some illegal specializations. */
9238 return NULL_TREE;
9240 converted_args
9241 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9242 explicit_args, NULL_TREE,
9243 tf_none, /*require_all_arguments=*/0));
9244 if (converted_args == error_mark_node)
9245 return NULL_TREE;
9247 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
9248 if (decl_type == error_mark_node)
9249 return NULL_TREE;
9252 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9253 /* Never do unification on the 'this' parameter. */
9254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9255 decl_arg_types = TREE_CHAIN (decl_arg_types);
9257 i = fn_type_unification (fn, explicit_args, targs,
9258 decl_arg_types,
9259 (check_rettype || DECL_CONV_FN_P (fn)
9260 ? TREE_TYPE (decl_type) : NULL_TREE),
9261 deduce, len);
9263 if (i != 0)
9264 return NULL_TREE;
9266 return targs;
9269 /* For most uses, we want to check the return type. */
9271 tree
9272 get_bindings (fn, decl, explicit_args)
9273 tree fn, decl, explicit_args;
9275 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9278 /* But for resolve_overloaded_unification, we only care about the parameter
9279 types. */
9281 static tree
9282 get_bindings_overload (fn, decl, explicit_args)
9283 tree fn, decl, explicit_args;
9285 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9288 /* Return the innermost template arguments that, when applied to a
9289 template specialization whose innermost template parameters are
9290 TPARMS, and whose specialization arguments are PARMS, yield the
9291 ARGS.
9293 For example, suppose we have:
9295 template <class T, class U> struct S {};
9296 template <class T> struct S<T*, int> {};
9298 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9299 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9300 int}. The resulting vector will be {double}, indicating that `T'
9301 is bound to `double'. */
9303 static tree
9304 get_class_bindings (tparms, parms, args)
9305 tree tparms, parms, args;
9307 int i, ntparms = TREE_VEC_LENGTH (tparms);
9308 tree vec = make_tree_vec (ntparms);
9310 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9311 UNIFY_ALLOW_NONE))
9312 return NULL_TREE;
9314 for (i = 0; i < ntparms; ++i)
9315 if (! TREE_VEC_ELT (vec, i))
9316 return NULL_TREE;
9318 if (verify_class_unification (vec, parms, args))
9319 return NULL_TREE;
9321 return vec;
9324 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9325 Pick the most specialized template, and return the corresponding
9326 instantiation, or if there is no corresponding instantiation, the
9327 template itself. If there is no most specialized template,
9328 error_mark_node is returned. If there are no templates at all,
9329 NULL_TREE is returned. */
9331 tree
9332 most_specialized_instantiation (instantiations)
9333 tree instantiations;
9335 tree fn, champ;
9336 int fate;
9338 if (!instantiations)
9339 return NULL_TREE;
9341 champ = instantiations;
9342 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
9344 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9345 DEDUCE_EXACT, -1);
9346 if (fate == 1)
9348 else
9350 if (fate == 0)
9352 fn = TREE_CHAIN (fn);
9353 if (! fn)
9354 return error_mark_node;
9356 champ = fn;
9360 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
9362 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9363 DEDUCE_EXACT, -1);
9364 if (fate != 1)
9365 return error_mark_node;
9368 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9371 /* Return the most specialized of the list of templates in FNS that can
9372 produce an instantiation matching DECL, given the explicit template
9373 arguments EXPLICIT_ARGS. */
9375 static tree
9376 most_specialized (fns, decl, explicit_args)
9377 tree fns, decl, explicit_args;
9379 tree candidates = NULL_TREE;
9380 tree fn, args;
9382 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9384 tree candidate = TREE_VALUE (fn);
9386 args = get_bindings (candidate, decl, explicit_args);
9387 if (args)
9388 candidates = tree_cons (NULL_TREE, candidate, candidates);
9391 return most_specialized_instantiation (candidates);
9394 /* If DECL is a specialization of some template, return the most
9395 general such template. Otherwise, returns NULL_TREE.
9397 For example, given:
9399 template <class T> struct S { template <class U> void f(U); };
9401 if TMPL is `template <class U> void S<int>::f(U)' this will return
9402 the full template. This function will not trace past partial
9403 specializations, however. For example, given in addition:
9405 template <class T> struct S<T*> { template <class U> void f(U); };
9407 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9408 `template <class T> template <class U> S<T*>::f(U)'. */
9410 tree
9411 most_general_template (decl)
9412 tree decl;
9414 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9415 an immediate specialization. */
9416 if (TREE_CODE (decl) == FUNCTION_DECL)
9418 if (DECL_TEMPLATE_INFO (decl)) {
9419 decl = DECL_TI_TEMPLATE (decl);
9421 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9422 template friend. */
9423 if (TREE_CODE (decl) != TEMPLATE_DECL)
9424 return NULL_TREE;
9425 } else
9426 return NULL_TREE;
9429 /* Look for more and more general templates. */
9430 while (DECL_TEMPLATE_INFO (decl))
9432 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9433 in some cases. (See cp-tree.h for details.) */
9434 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9435 break;
9437 /* Stop if we run into an explicitly specialized class template. */
9438 if (!DECL_NAMESPACE_SCOPE_P (decl)
9439 && DECL_CONTEXT (decl)
9440 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
9441 break;
9443 decl = DECL_TI_TEMPLATE (decl);
9446 return decl;
9449 /* Return the most specialized of the class template specializations
9450 of TMPL which can produce an instantiation matching ARGS, or
9451 error_mark_node if the choice is ambiguous. */
9453 static tree
9454 most_specialized_class (tmpl, args)
9455 tree tmpl;
9456 tree args;
9458 tree list = NULL_TREE;
9459 tree t;
9460 tree champ;
9461 int fate;
9463 tmpl = most_general_template (tmpl);
9464 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
9466 tree spec_args
9467 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9468 if (spec_args)
9470 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9471 TREE_TYPE (list) = TREE_TYPE (t);
9475 if (! list)
9476 return NULL_TREE;
9478 t = list;
9479 champ = t;
9480 t = TREE_CHAIN (t);
9481 for (; t; t = TREE_CHAIN (t))
9483 fate = more_specialized_class (champ, t);
9484 if (fate == 1)
9486 else
9488 if (fate == 0)
9490 t = TREE_CHAIN (t);
9491 if (! t)
9492 return error_mark_node;
9494 champ = t;
9498 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9500 fate = more_specialized_class (champ, t);
9501 if (fate != 1)
9502 return error_mark_node;
9505 return champ;
9508 /* called from the parser. */
9510 void
9511 do_decl_instantiation (declspecs, declarator, storage)
9512 tree declspecs, declarator, storage;
9514 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL);
9515 tree result = NULL_TREE;
9516 int extern_p = 0;
9518 if (!decl)
9519 /* An error occurred, for which grokdeclarator has already issued
9520 an appropriate message. */
9521 return;
9522 else if (! DECL_LANG_SPECIFIC (decl))
9524 error ("explicit instantiation of non-template `%#D'", decl);
9525 return;
9527 else if (TREE_CODE (decl) == VAR_DECL)
9529 /* There is an asymmetry here in the way VAR_DECLs and
9530 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9531 the latter, the DECL we get back will be marked as a
9532 template instantiation, and the appropriate
9533 DECL_TEMPLATE_INFO will be set up. This does not happen for
9534 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9535 should handle VAR_DECLs as it currently handles
9536 FUNCTION_DECLs. */
9537 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9538 if (result && TREE_CODE (result) != VAR_DECL)
9540 error ("no matching template for `%D' found", result);
9541 return;
9544 else if (TREE_CODE (decl) != FUNCTION_DECL)
9546 error ("explicit instantiation of `%#D'", decl);
9547 return;
9549 else
9550 result = decl;
9552 /* Check for various error cases. Note that if the explicit
9553 instantiation is legal the RESULT will currently be marked as an
9554 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9555 until we get here. */
9557 if (DECL_TEMPLATE_SPECIALIZATION (result))
9559 /* DR 259 [temp.spec].
9561 Both an explicit instantiation and a declaration of an explicit
9562 specialization shall not appear in a program unless the explicit
9563 instantiation follows a declaration of the explicit specialization.
9565 For a given set of template parameters, if an explicit
9566 instantiation of a template appears after a declaration of an
9567 explicit specialization for that template, the explicit
9568 instantiation has no effect. */
9569 return;
9571 else if (DECL_EXPLICIT_INSTANTIATION (result))
9573 /* [temp.spec]
9575 No program shall explicitly instantiate any template more
9576 than once.
9578 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9579 instantiation was `extern' and the second is not, and EXTERN_P for
9580 the opposite case. If -frepo, chances are we already got marked
9581 as an explicit instantiation because of the repo file. */
9582 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
9583 pedwarn ("duplicate explicit instantiation of `%#D'", result);
9585 /* If we've already instantiated the template, just return now. */
9586 if (DECL_INTERFACE_KNOWN (result))
9587 return;
9589 else if (!DECL_IMPLICIT_INSTANTIATION (result))
9591 error ("no matching template for `%D' found", result);
9592 return;
9594 else if (!DECL_TEMPLATE_INFO (result))
9596 pedwarn ("explicit instantiation of non-template `%#D'", result);
9597 return;
9600 if (flag_external_templates)
9601 return;
9603 if (storage == NULL_TREE)
9605 else if (storage == ridpointers[(int) RID_EXTERN])
9607 if (pedantic && !in_system_header)
9608 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9609 extern_p = 1;
9611 else
9612 error ("storage class `%D' applied to template instantiation",
9613 storage);
9615 SET_DECL_EXPLICIT_INSTANTIATION (result);
9616 mark_decl_instantiated (result, extern_p);
9617 repo_template_instantiated (result, extern_p);
9618 if (! extern_p)
9619 instantiate_decl (result, /*defer_ok=*/1);
9622 void
9623 mark_class_instantiated (t, extern_p)
9624 tree t;
9625 int extern_p;
9627 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
9628 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9629 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
9630 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9631 if (! extern_p)
9633 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9634 rest_of_type_compilation (t, 1);
9638 /* Perform an explicit instantiation of template class T. STORAGE, if
9639 non-null, is the RID for extern, inline or static. COMPLAIN is
9640 non-zero if this is called from the parser, zero if called recursively,
9641 since the standard is unclear (as detailed below). */
9643 void
9644 do_type_instantiation (t, storage, complain)
9645 tree t, storage;
9646 tsubst_flags_t complain;
9648 int extern_p = 0;
9649 int nomem_p = 0;
9650 int static_p = 0;
9652 if (TREE_CODE (t) == TYPE_DECL)
9653 t = TREE_TYPE (t);
9655 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
9657 error ("explicit instantiation of non-template type `%T'", t);
9658 return;
9661 complete_type (t);
9663 /* With -fexternal-templates, explicit instantiations are treated the same
9664 as implicit ones. */
9665 if (flag_external_templates)
9666 return;
9668 if (!COMPLETE_TYPE_P (t))
9670 if (complain & tf_error)
9671 error ("explicit instantiation of `%#T' before definition of template",
9673 return;
9676 if (storage != NULL_TREE)
9678 if (pedantic && !in_system_header)
9679 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9680 IDENTIFIER_POINTER (storage));
9682 if (storage == ridpointers[(int) RID_INLINE])
9683 nomem_p = 1;
9684 else if (storage == ridpointers[(int) RID_EXTERN])
9685 extern_p = 1;
9686 else if (storage == ridpointers[(int) RID_STATIC])
9687 static_p = 1;
9688 else
9690 error ("storage class `%D' applied to template instantiation",
9691 storage);
9692 extern_p = 0;
9696 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9698 /* DR 259 [temp.spec].
9700 Both an explicit instantiation and a declaration of an explicit
9701 specialization shall not appear in a program unless the explicit
9702 instantiation follows a declaration of the explicit specialization.
9704 For a given set of template parameters, if an explicit
9705 instantiation of a template appears after a declaration of an
9706 explicit specialization for that template, the explicit
9707 instantiation has no effect. */
9708 return;
9710 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
9712 /* [temp.spec]
9714 No program shall explicitly instantiate any template more
9715 than once.
9717 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9718 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9719 are we already got marked as an explicit instantiation because of the
9720 repo file. All these cases are OK. */
9721 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
9722 && (complain & tf_error))
9723 pedwarn ("duplicate explicit instantiation of `%#T'", t);
9725 /* If we've already instantiated the template, just return now. */
9726 if (!CLASSTYPE_INTERFACE_ONLY (t))
9727 return;
9730 mark_class_instantiated (t, extern_p);
9731 repo_template_instantiated (t, extern_p);
9733 if (nomem_p)
9734 return;
9737 tree tmp;
9739 /* In contrast to implicit instantiation, where only the
9740 declarations, and not the definitions, of members are
9741 instantiated, we have here:
9743 [temp.explicit]
9745 The explicit instantiation of a class template specialization
9746 implies the instantiation of all of its members not
9747 previously explicitly specialized in the translation unit
9748 containing the explicit instantiation.
9750 Of course, we can't instantiate member template classes, since
9751 we don't have any arguments for them. Note that the standard
9752 is unclear on whether the instantiation of the members are
9753 *explicit* instantiations or not. We choose to be generous,
9754 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9755 the explicit instantiation of a class where some of the members
9756 have no definition in the current translation unit. */
9758 if (! static_p)
9759 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
9760 if (TREE_CODE (tmp) == FUNCTION_DECL
9761 && DECL_TEMPLATE_INSTANTIATION (tmp))
9763 mark_decl_instantiated (tmp, extern_p);
9764 repo_template_instantiated (tmp, extern_p);
9765 if (! extern_p)
9766 instantiate_decl (tmp, /*defer_ok=*/1);
9769 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9770 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
9772 mark_decl_instantiated (tmp, extern_p);
9773 repo_template_instantiated (tmp, extern_p);
9774 if (! extern_p)
9775 instantiate_decl (tmp, /*defer_ok=*/1);
9778 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
9779 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9780 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
9781 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
9785 /* Given a function DECL, which is a specialization of TMPL, modify
9786 DECL to be a re-instantiation of TMPL with the same template
9787 arguments. TMPL should be the template into which tsubst'ing
9788 should occur for DECL, not the most general template.
9790 One reason for doing this is a scenario like this:
9792 template <class T>
9793 void f(const T&, int i);
9795 void g() { f(3, 7); }
9797 template <class T>
9798 void f(const T& t, const int i) { }
9800 Note that when the template is first instantiated, with
9801 instantiate_template, the resulting DECL will have no name for the
9802 first parameter, and the wrong type for the second. So, when we go
9803 to instantiate the DECL, we regenerate it. */
9805 static void
9806 regenerate_decl_from_template (decl, tmpl)
9807 tree decl;
9808 tree tmpl;
9810 /* The most general version of TMPL. */
9811 tree gen_tmpl;
9812 /* The arguments used to instantiate DECL, from the most general
9813 template. */
9814 tree args;
9815 tree code_pattern;
9816 tree new_decl;
9817 int unregistered;
9819 args = DECL_TI_ARGS (decl);
9820 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9822 /* Unregister the specialization so that when we tsubst we will not
9823 just return DECL. We don't have to unregister DECL from TMPL
9824 because if would only be registered there if it were a partial
9825 instantiation of a specialization, which it isn't: it's a full
9826 instantiation. */
9827 gen_tmpl = most_general_template (tmpl);
9828 unregistered = unregister_specialization (decl, gen_tmpl);
9830 /* If the DECL was not unregistered then something peculiar is
9831 happening: we created a specialization but did not call
9832 register_specialization for it. */
9833 my_friendly_assert (unregistered, 0);
9835 if (TREE_CODE (decl) == VAR_DECL)
9836 /* Make sure that we can see identifiers, and compute access
9837 correctly, for the class members used in the declaration of
9838 this static variable. */
9839 pushclass (DECL_CONTEXT (decl), 2);
9841 /* Do the substitution to get the new declaration. */
9842 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
9844 if (TREE_CODE (decl) == VAR_DECL)
9846 /* Set up DECL_INITIAL, since tsubst doesn't. */
9847 DECL_INITIAL (new_decl) =
9848 tsubst_expr (DECL_INITIAL (code_pattern), args,
9849 tf_error, DECL_TI_TEMPLATE (decl));
9850 /* Pop the class context we pushed above. */
9851 popclass ();
9853 else if (TREE_CODE (decl) == FUNCTION_DECL)
9855 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9856 new decl. */
9857 DECL_INITIAL (new_decl) = error_mark_node;
9858 /* And don't complain about a duplicate definition. */
9859 DECL_INITIAL (decl) = NULL_TREE;
9862 /* The immediate parent of the new template is still whatever it was
9863 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9864 general template. We also reset the DECL_ASSEMBLER_NAME since
9865 tsubst always calculates the name as if the function in question
9866 were really a template instance, and sometimes, with friend
9867 functions, this is not so. See tsubst_friend_function for
9868 details. */
9869 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9870 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
9871 COPY_DECL_RTL (decl, new_decl);
9872 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
9874 /* Call duplicate decls to merge the old and new declarations. */
9875 duplicate_decls (new_decl, decl);
9877 /* Now, re-register the specialization. */
9878 register_specialization (decl, gen_tmpl, args);
9881 /* Produce the definition of D, a _DECL generated from a template. If
9882 DEFER_OK is non-zero, then we don't have to actually do the
9883 instantiation now; we just have to do it sometime. */
9885 tree
9886 instantiate_decl (d, defer_ok)
9887 tree d;
9888 int defer_ok;
9890 tree tmpl = DECL_TI_TEMPLATE (d);
9891 tree args = DECL_TI_ARGS (d);
9892 tree td;
9893 tree code_pattern;
9894 tree spec;
9895 tree gen_tmpl;
9896 int pattern_defined;
9897 int line = lineno;
9898 int need_push;
9899 const char *file = input_filename;
9901 /* This function should only be used to instantiate templates for
9902 functions and static member variables. */
9903 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9904 || TREE_CODE (d) == VAR_DECL, 0);
9906 /* Don't instantiate cloned functions. Instead, instantiate the
9907 functions they cloned. */
9908 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
9909 d = DECL_CLONED_FUNCTION (d);
9911 if (DECL_TEMPLATE_INSTANTIATED (d))
9912 /* D has already been instantiated. It might seem reasonable to
9913 check whether or not D is an explicit instantiation, and, if so,
9914 stop here. But when an explicit instantiation is deferred
9915 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9916 is set, even though we still need to do the instantiation. */
9917 return d;
9919 /* If we already have a specialization of this declaration, then
9920 there's no reason to instantiate it. Note that
9921 retrieve_specialization gives us both instantiations and
9922 specializations, so we must explicitly check
9923 DECL_TEMPLATE_SPECIALIZATION. */
9924 gen_tmpl = most_general_template (tmpl);
9925 spec = retrieve_specialization (gen_tmpl, args);
9926 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9927 return spec;
9929 /* This needs to happen before any tsubsting. */
9930 if (! push_tinst_level (d))
9931 return d;
9933 timevar_push (TV_PARSE);
9935 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9936 for the instantiation. This is not always the most general
9937 template. Consider, for example:
9939 template <class T>
9940 struct S { template <class U> void f();
9941 template <> void f<int>(); };
9943 and an instantiation of S<double>::f<int>. We want TD to be the
9944 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9945 td = tmpl;
9946 while (/* An instantiation cannot have a definition, so we need a
9947 more general template. */
9948 DECL_TEMPLATE_INSTANTIATION (td)
9949 /* We must also deal with friend templates. Given:
9951 template <class T> struct S {
9952 template <class U> friend void f() {};
9955 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9956 so far as the language is concerned, but that's still
9957 where we get the pattern for the instantiation from. On
9958 other hand, if the definition comes outside the class, say:
9960 template <class T> struct S {
9961 template <class U> friend void f();
9963 template <class U> friend void f() {}
9965 we don't need to look any further. That's what the check for
9966 DECL_INITIAL is for. */
9967 || (TREE_CODE (d) == FUNCTION_DECL
9968 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9969 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))))
9971 /* The present template, TD, should not be a definition. If it
9972 were a definition, we should be using it! Note that we
9973 cannot restructure the loop to just keep going until we find
9974 a template with a definition, since that might go too far if
9975 a specialization was declared, but not defined. */
9976 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9977 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9978 0);
9980 /* Fetch the more general template. */
9981 td = DECL_TI_TEMPLATE (td);
9984 code_pattern = DECL_TEMPLATE_RESULT (td);
9986 if (TREE_CODE (d) == FUNCTION_DECL)
9987 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
9988 else
9989 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
9991 lineno = DECL_SOURCE_LINE (d);
9992 input_filename = DECL_SOURCE_FILE (d);
9994 if (pattern_defined)
9996 /* Let the repository code that this template definition is
9997 available.
9999 The repository doesn't need to know about cloned functions
10000 because they never actually show up in the object file. It
10001 does need to know about the clones; those are the symbols
10002 that the linker will be emitting error messages about. */
10003 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10004 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10006 tree t;
10008 for (t = TREE_CHAIN (d);
10009 t && DECL_CLONED_FUNCTION_P (t);
10010 t = TREE_CHAIN (t))
10011 repo_template_used (t);
10013 else
10014 repo_template_used (d);
10016 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10018 if (flag_alt_external_templates)
10020 if (interface_unknown)
10021 warn_if_unknown_interface (d);
10023 else if (DECL_INTERFACE_KNOWN (code_pattern))
10025 DECL_INTERFACE_KNOWN (d) = 1;
10026 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
10028 else
10029 warn_if_unknown_interface (code_pattern);
10032 if (at_eof)
10033 import_export_decl (d);
10036 if (!defer_ok)
10038 /* Recheck the substitutions to obtain any warning messages
10039 about ignoring cv qualifiers. */
10040 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10041 tree type = TREE_TYPE (gen);
10043 if (TREE_CODE (gen) == FUNCTION_DECL)
10045 tsubst (DECL_ARGUMENTS (gen), args, tf_error | tf_warning, d);
10046 tsubst (TYPE_RAISES_EXCEPTIONS (type), args,
10047 tf_error | tf_warning, d);
10048 /* Don't simply tsubst the function type, as that will give
10049 duplicate warnings about poor parameter qualifications.
10050 The function arguments are the same as the decl_arguments
10051 without the top level cv qualifiers. */
10052 type = TREE_TYPE (type);
10054 tsubst (type, args, tf_error | tf_warning, d);
10057 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10058 && DECL_INITIAL (d) == NULL_TREE)
10059 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10060 abort ();
10061 /* Reject all external templates except inline functions. */
10062 else if (DECL_INTERFACE_KNOWN (d)
10063 && ! DECL_NOT_REALLY_EXTERN (d)
10064 && ! (TREE_CODE (d) == FUNCTION_DECL
10065 && DECL_INLINE (d)))
10066 goto out;
10067 /* Defer all other templates, unless we have been explicitly
10068 forbidden from doing so. We restore the source position here
10069 because it's used by add_pending_template. */
10070 else if (! pattern_defined || defer_ok)
10072 lineno = line;
10073 input_filename = file;
10075 if (at_eof && !pattern_defined
10076 && DECL_EXPLICIT_INSTANTIATION (d))
10077 /* [temp.explicit]
10079 The definition of a non-exported function template, a
10080 non-exported member function template, or a non-exported
10081 member function or static data member of a class template
10082 shall be present in every translation unit in which it is
10083 explicitly instantiated. */
10084 pedwarn
10085 ("explicit instantiation of `%D' but no definition available", d);
10087 add_pending_template (d);
10088 goto out;
10091 need_push = !global_bindings_p ();
10092 if (need_push)
10093 push_to_top_level ();
10095 /* We're now committed to instantiating this template. Mark it as
10096 instantiated so that recursive calls to instantiate_decl do not
10097 try to instantiate it again. */
10098 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10100 /* Regenerate the declaration in case the template has been modified
10101 by a subsequent redeclaration. */
10102 regenerate_decl_from_template (d, td);
10104 /* We already set the file and line above. Reset them now in case
10105 they changed as a result of calling regenerate_decl_from_template. */
10106 lineno = DECL_SOURCE_LINE (d);
10107 input_filename = DECL_SOURCE_FILE (d);
10109 if (TREE_CODE (d) == VAR_DECL)
10111 DECL_IN_AGGR_P (d) = 0;
10112 if (DECL_INTERFACE_KNOWN (d))
10113 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10114 else
10116 DECL_EXTERNAL (d) = 1;
10117 DECL_NOT_REALLY_EXTERN (d) = 1;
10119 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
10121 else if (TREE_CODE (d) == FUNCTION_DECL)
10123 htab_t saved_local_specializations;
10125 /* Save away the current list, in case we are instantiating one
10126 template from within the body of another. */
10127 saved_local_specializations = local_specializations;
10129 /* Set up the list of local specializations. */
10130 local_specializations = htab_create (37,
10131 htab_hash_pointer,
10132 htab_eq_pointer,
10133 NULL);
10135 /* Set up context. */
10136 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10138 /* Substitute into the body of the function. */
10139 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10140 tf_error | tf_warning, tmpl);
10142 /* We don't need the local specializations any more. */
10143 htab_delete (local_specializations);
10144 local_specializations = saved_local_specializations;
10146 /* Finish the function. */
10147 expand_body (finish_function (0));
10150 /* We're not deferring instantiation any more. */
10151 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10153 if (need_push)
10154 pop_from_top_level ();
10156 out:
10157 lineno = line;
10158 input_filename = file;
10160 pop_tinst_level ();
10162 timevar_pop (TV_PARSE);
10164 return d;
10167 /* Run through the list of templates that we wish we could
10168 instantiate, and instantiate any we can. */
10171 instantiate_pending_templates ()
10173 tree *t;
10174 tree last = NULL_TREE;
10175 int instantiated_something = 0;
10176 int reconsider;
10180 reconsider = 0;
10182 t = &pending_templates;
10183 while (*t)
10185 tree instantiation = TREE_VALUE (*t);
10187 reopen_tinst_level (TREE_PURPOSE (*t));
10189 if (TYPE_P (instantiation))
10191 tree fn;
10193 if (!COMPLETE_TYPE_P (instantiation))
10195 instantiate_class_template (instantiation);
10196 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10197 for (fn = TYPE_METHODS (instantiation);
10199 fn = TREE_CHAIN (fn))
10200 if (! DECL_ARTIFICIAL (fn))
10201 instantiate_decl (fn, /*defer_ok=*/0);
10202 if (COMPLETE_TYPE_P (instantiation))
10204 instantiated_something = 1;
10205 reconsider = 1;
10209 if (COMPLETE_TYPE_P (instantiation))
10210 /* If INSTANTIATION has been instantiated, then we don't
10211 need to consider it again in the future. */
10212 *t = TREE_CHAIN (*t);
10213 else
10215 last = *t;
10216 t = &TREE_CHAIN (*t);
10219 else
10221 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10222 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10224 instantiation = instantiate_decl (instantiation,
10225 /*defer_ok=*/0);
10226 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10228 instantiated_something = 1;
10229 reconsider = 1;
10233 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10234 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10235 /* If INSTANTIATION has been instantiated, then we don't
10236 need to consider it again in the future. */
10237 *t = TREE_CHAIN (*t);
10238 else
10240 last = *t;
10241 t = &TREE_CHAIN (*t);
10244 tinst_depth = 0;
10245 current_tinst_level = NULL_TREE;
10247 last_pending_template = last;
10249 while (reconsider);
10251 return instantiated_something;
10254 /* Substitute ARGVEC into T, which is a list of initializers for
10255 either base class or a non-static data member. The TREE_PURPOSEs
10256 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10257 instantiate_decl. */
10259 static tree
10260 tsubst_initializer_list (t, argvec)
10261 tree t, argvec;
10263 tree first = NULL_TREE;
10264 tree *p = &first;
10266 for (; t; t = TREE_CHAIN (t))
10268 tree decl;
10269 tree init;
10270 tree val;
10272 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
10273 NULL_TREE);
10274 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
10275 NULL_TREE);
10277 if (!init)
10279 else if (TREE_CODE (init) == TREE_LIST)
10280 for (val = init; val; val = TREE_CHAIN (val))
10281 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
10282 else
10283 init = convert_from_reference (init);
10285 *p = expand_member_init (current_class_ref, decl,
10286 init ? init : void_type_node);
10287 if (*p)
10288 p = &TREE_CHAIN (*p);
10290 return first;
10293 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10295 static void
10296 set_current_access_from_decl (decl)
10297 tree decl;
10299 if (TREE_PRIVATE (decl))
10300 current_access_specifier = access_private_node;
10301 else if (TREE_PROTECTED (decl))
10302 current_access_specifier = access_protected_node;
10303 else
10304 current_access_specifier = access_public_node;
10307 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10308 is the instantiation (which should have been created with
10309 start_enum) and ARGS are the template arguments to use. */
10311 static void
10312 tsubst_enum (tag, newtag, args)
10313 tree tag;
10314 tree newtag;
10315 tree args;
10317 tree e;
10319 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
10321 tree value;
10323 /* Note that in a template enum, the TREE_VALUE is the
10324 CONST_DECL, not the corresponding INTEGER_CST. */
10325 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
10326 args, tf_error | tf_warning,
10327 NULL_TREE);
10329 /* Give this enumeration constant the correct access. */
10330 set_current_access_from_decl (TREE_VALUE (e));
10332 /* Actually build the enumerator itself. */
10333 build_enumerator (TREE_PURPOSE (e), value, newtag);
10336 finish_enum (newtag);
10337 DECL_SOURCE_LINE (TYPE_NAME (newtag)) = DECL_SOURCE_LINE (TYPE_NAME (tag));
10338 DECL_SOURCE_FILE (TYPE_NAME (newtag)) = DECL_SOURCE_FILE (TYPE_NAME (tag));
10341 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10342 its type -- but without substituting the innermost set of template
10343 arguments. So, innermost set of template parameters will appear in
10344 the type. If CONTEXTP is non-NULL, then the partially substituted
10345 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10346 will be filled in with the substituted template parameters, if it
10347 is non-NULL. */
10349 tree
10350 get_mostly_instantiated_function_type (decl, contextp, tparmsp)
10351 tree decl;
10352 tree *contextp;
10353 tree *tparmsp;
10355 tree context = NULL_TREE;
10356 tree fn_type;
10357 tree tmpl;
10358 tree targs;
10359 tree tparms;
10360 int parm_depth;
10362 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10363 targs = DECL_TI_ARGS (decl);
10364 tparms = DECL_TEMPLATE_PARMS (tmpl);
10365 parm_depth = TMPL_PARMS_DEPTH (tparms);
10367 /* There should be as many levels of arguments as there are levels
10368 of parameters. */
10369 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10371 fn_type = TREE_TYPE (tmpl);
10372 if (DECL_STATIC_FUNCTION_P (decl))
10373 context = DECL_CONTEXT (decl);
10375 if (parm_depth == 1)
10376 /* No substitution is necessary. */
10378 else
10380 int i;
10381 tree partial_args;
10383 /* Replace the innermost level of the TARGS with NULL_TREEs to
10384 let tsubst know not to substitute for those parameters. */
10385 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
10386 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10387 SET_TMPL_ARGS_LEVEL (partial_args, i,
10388 TMPL_ARGS_LEVEL (targs, i));
10389 SET_TMPL_ARGS_LEVEL (partial_args,
10390 TMPL_ARGS_DEPTH (targs),
10391 make_tree_vec (DECL_NTPARMS (tmpl)));
10393 /* Now, do the (partial) substitution to figure out the
10394 appropriate function type. */
10395 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
10396 if (DECL_STATIC_FUNCTION_P (decl))
10397 context = tsubst (context, partial_args, tf_error, NULL_TREE);
10399 /* Substitute into the template parameters to obtain the real
10400 innermost set of parameters. This step is important if the
10401 innermost set of template parameters contains value
10402 parameters whose types depend on outer template parameters. */
10403 TREE_VEC_LENGTH (partial_args)--;
10404 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
10407 if (contextp)
10408 *contextp = context;
10409 if (tparmsp)
10410 *tparmsp = tparms;
10412 return fn_type;
10415 /* Return truthvalue if we're processing a template different from
10416 the last one involved in diagnostics. */
10418 problematic_instantiation_changed ()
10420 return last_template_error_tick != tinst_level_tick;
10423 /* Remember current template involved in diagnostics. */
10424 void
10425 record_last_problematic_instantiation ()
10427 last_template_error_tick = tinst_level_tick;
10430 tree
10431 current_instantiation ()
10433 return current_tinst_level;
10436 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10437 type. Return zero for ok, non-zero for disallowed. Issue error and
10438 warning messages under control of COMPLAIN. */
10440 static int
10441 invalid_nontype_parm_type_p (type, complain)
10442 tree type;
10443 tsubst_flags_t complain;
10445 if (INTEGRAL_TYPE_P (type))
10446 return 0;
10447 else if (POINTER_TYPE_P (type))
10448 return 0;
10449 else if (TYPE_PTRMEM_P (type))
10450 return 0;
10451 else if (TYPE_PTRMEMFUNC_P (type))
10452 return 0;
10453 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10454 return 0;
10455 else if (TREE_CODE (type) == TYPENAME_TYPE)
10456 return 0;
10458 if (complain & tf_error)
10459 error ("`%#T' is not a valid type for a template constant parameter",
10460 type);
10461 return 1;