Daily bump.
[official-gcc.git] / gcc / cp / pt.c
bloba21431796837bc5447c3c4eb45d4f65f6f7db2aa
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
28 #include "config.h"
29 #include "system.h"
30 #include "obstack.h"
32 #include "tree.h"
33 #include "flags.h"
34 #include "cp-tree.h"
35 #include "decl.h"
36 #include "parse.h"
37 #include "lex.h"
38 #include "output.h"
39 #include "defaults.h"
40 #include "except.h"
41 #include "toplev.h"
42 #include "rtl.h"
43 #include "defaults.h"
44 #include "ggc.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) PROTO((tree, void*));
50 extern struct obstack permanent_obstack;
52 extern int lineno;
53 extern char *input_filename;
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the
58 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
59 the instantiate request occurred; the TREE_VALUE is a either a DECL
60 (for a function or static data member), or a TYPE (for a class)
61 indicating what we are hoping to instantiate. */
62 static tree pending_templates;
63 static tree *template_tail = &pending_templates;
65 static tree maybe_templates;
66 static tree *maybe_template_tail = &maybe_templates;
68 int processing_template_parmlist;
69 static int template_header_count;
71 static tree saved_trees;
72 static varray_type inline_parm_levels;
73 static size_t inline_parm_levels_used;
75 #define obstack_chunk_alloc xmalloc
76 #define obstack_chunk_free free
78 #define UNIFY_ALLOW_NONE 0
79 #define UNIFY_ALLOW_MORE_CV_QUAL 1
80 #define UNIFY_ALLOW_LESS_CV_QUAL 2
81 #define UNIFY_ALLOW_DERIVED 4
82 #define UNIFY_ALLOW_INTEGER 8
84 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
85 virtual, or a base class of a virtual
86 base. */
87 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
88 type with the desired type. */
90 static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
91 unification_kind_t, int));
92 static int try_one_overload PROTO((tree, tree, tree, tree, tree,
93 unification_kind_t, int));
94 static int unify PROTO((tree, tree, tree, tree, int));
95 static void add_pending_template PROTO((tree));
96 static int push_tinst_level PROTO((tree));
97 static tree classtype_mangled_name PROTO((tree));
98 static char *mangle_class_name_for_template PROTO((char *, tree, tree));
99 static tree tsubst_expr_values PROTO((tree, tree));
100 static int list_eq PROTO((tree, tree));
101 static tree get_class_bindings PROTO((tree, tree, tree));
102 static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
103 static void tsubst_enum PROTO((tree, tree, tree));
104 static tree add_to_template_args PROTO((tree, tree));
105 static tree add_outermost_template_args PROTO((tree, tree));
106 static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
107 tree*));
108 static int type_unification_real PROTO((tree, tree, tree, tree,
109 int, unification_kind_t, int));
110 static void note_template_header PROTO((int));
111 static tree maybe_fold_nontype_arg PROTO((tree));
112 static tree convert_nontype_argument PROTO((tree, tree));
113 static tree convert_template_argument PROTO ((tree, tree, tree, int,
114 int , tree));
115 static tree get_bindings_overload PROTO((tree, tree, tree));
116 static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
117 static tree build_template_parm_index PROTO((int, int, int, tree, tree));
118 static int inline_needs_template_parms PROTO((tree));
119 static void push_inline_template_parms_recursive PROTO((tree, int));
120 static tree retrieve_specialization PROTO((tree, tree));
121 static tree retrieve_local_specialization PROTO((tree, tree));
122 static tree register_specialization PROTO((tree, tree, tree));
123 static tree register_local_specialization PROTO((tree, tree, tree));
124 static int unregister_specialization PROTO((tree, tree));
125 static tree reduce_template_parm_level PROTO((tree, tree, int));
126 static tree build_template_decl PROTO((tree, tree));
127 static int mark_template_parm PROTO((tree, void *));
128 static tree tsubst_friend_function PROTO((tree, tree));
129 static tree tsubst_friend_class PROTO((tree, tree));
130 static tree get_bindings_real PROTO((tree, tree, tree, int));
131 static int template_decl_level PROTO((tree));
132 static tree maybe_get_template_decl_from_type_decl PROTO((tree));
133 static int check_cv_quals_for_unify PROTO((int, tree, tree));
134 static tree tsubst_template_arg_vector PROTO((tree, tree, int));
135 static tree tsubst_template_parms PROTO((tree, tree, int));
136 static void regenerate_decl_from_template PROTO((tree, tree));
137 static tree most_specialized PROTO((tree, tree, tree));
138 static tree most_specialized_class PROTO((tree, tree));
139 static tree most_general_template PROTO((tree));
140 static void set_mangled_name_for_template_decl PROTO((tree));
141 static int template_class_depth_real PROTO((tree, int));
142 static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int));
143 static tree tsubst_decl PROTO((tree, tree, tree, tree));
144 static tree tsubst_arg_types PROTO((tree, tree, int, tree));
145 static tree tsubst_function_type PROTO((tree, tree, int, tree));
146 static void check_specialization_scope PROTO((void));
147 static tree process_partial_specialization PROTO((tree));
148 static void set_current_access_from_decl PROTO((tree));
149 static void check_default_tmpl_args PROTO((tree, tree, int, int));
150 static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree));
151 static tree get_template_base_recursive PROTO((tree, tree,
152 tree, tree, tree, int));
153 static tree get_template_base PROTO((tree, tree, tree, tree));
154 static tree try_class_unification PROTO((tree, tree, tree, tree));
155 static int coerce_template_template_parms PROTO((tree, tree, int,
156 tree, tree));
157 static tree determine_specialization PROTO((tree, tree, tree *, int));
158 static int template_args_equal PROTO((tree, tree));
159 static void print_template_context PROTO((int));
160 static void tsubst_default_arguments PROTO((tree));
162 /* We use TREE_VECs to hold template arguments. If there is only one
163 level of template arguments, then the TREE_VEC contains the
164 arguments directly. If there is more than one level of template
165 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
166 containing the template arguments for a single level. The first
167 entry in the outer TREE_VEC is the outermost level of template
168 parameters; the last is the innermost.
170 It is incorrect to ever form a template argument vector containing
171 only one level of arguments, but which is a TREE_VEC containing as
172 its only entry the TREE_VEC for that level. */
174 /* Non-zero if the template arguments is actually a vector of vectors,
175 rather than just a vector. */
176 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
177 (NODE != NULL_TREE \
178 && TREE_CODE (NODE) == TREE_VEC \
179 && TREE_VEC_LENGTH (NODE) > 0 \
180 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
181 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
183 /* The depth of a template argument vector. When called directly by
184 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
185 template arguments. In fact, we may even see NULL_TREE if there
186 are no template arguments. In both of those cases, there is only
187 one level of template arguments. */
188 #define TMPL_ARGS_DEPTH(NODE) \
189 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
191 /* The LEVELth level of the template ARGS. Note that template
192 parameter levels are indexed from 1, not from 0. */
193 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
194 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
195 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
197 /* Set the LEVELth level of the template ARGS to VAL. This macro does
198 not work with single-level argument vectors. */
199 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
200 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
202 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
203 #define TMPL_ARG(ARGS, LEVEL, IDX) \
204 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
206 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
207 macro does not work with single-level argument vectors. */
208 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
209 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
211 /* Given a single level of template arguments in NODE, return the
212 number of arguments. */
213 #define NUM_TMPL_ARGS(NODE) \
214 ((NODE) == NULL_TREE ? 0 \
215 : (TREE_CODE (NODE) == TREE_VEC \
216 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
218 /* The number of levels of template parameters given by NODE. */
219 #define TMPL_PARMS_DEPTH(NODE) \
220 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
222 /* Called once to initialize pt.c. */
224 void
225 init_pt ()
227 ggc_add_tree_root (&pending_templates, 1);
228 ggc_add_tree_root (&maybe_templates, 1);
229 ggc_add_tree_root (&saved_trees, 1);
232 /* Do any processing required when DECL (a member template declaration
233 using TEMPLATE_PARAMETERS as its innermost parameter list) is
234 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
235 it is a specialization, in which case the DECL itself is returned. */
237 tree
238 finish_member_template_decl (decl)
239 tree decl;
241 if (decl == NULL_TREE || decl == void_type_node)
242 return NULL_TREE;
243 else if (decl == error_mark_node)
244 /* By returning NULL_TREE, the parser will just ignore this
245 declaration. We have already issued the error. */
246 return NULL_TREE;
247 else if (TREE_CODE (decl) == TREE_LIST)
249 /* Assume that the class is the only declspec. */
250 decl = TREE_VALUE (decl);
251 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
252 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
254 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
255 check_member_template (tmpl);
256 return tmpl;
258 return NULL_TREE;
260 else if (DECL_TEMPLATE_INFO (decl))
262 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
264 check_member_template (DECL_TI_TEMPLATE (decl));
265 return DECL_TI_TEMPLATE (decl);
267 else
268 return decl;
270 else
271 cp_error ("invalid member template declaration `%D'", decl);
273 return error_mark_node;
276 /* Returns the template nesting level of the indicated class TYPE.
278 For example, in:
279 template <class T>
280 struct A
282 template <class U>
283 struct B {};
286 A<T>::B<U> has depth two, while A<T> has depth one.
287 Both A<T>::B<int> and A<int>::B<U> have depth one, if
288 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
289 specializations.
291 This function is guaranteed to return 0 if passed NULL_TREE so
292 that, for example, `template_class_depth (current_class_type)' is
293 always safe. */
295 static int
296 template_class_depth_real (type, count_specializations)
297 tree type;
298 int count_specializations;
300 int depth;
302 for (depth = 0;
303 type && TREE_CODE (type) != NAMESPACE_DECL;
304 type = (TREE_CODE (type) == FUNCTION_DECL)
305 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
307 if (TREE_CODE (type) != FUNCTION_DECL)
309 if (CLASSTYPE_TEMPLATE_INFO (type)
310 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
311 && ((count_specializations
312 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
313 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
314 ++depth;
316 else
318 if (DECL_TEMPLATE_INFO (type)
319 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
320 && ((count_specializations
321 && DECL_TEMPLATE_SPECIALIZATION (type))
322 || uses_template_parms (DECL_TI_ARGS (type))))
323 ++depth;
327 return depth;
330 /* Returns the template nesting level of the indicated class TYPE.
331 Like template_class_depth_real, but instantiations do not count in
332 the depth. */
334 int
335 template_class_depth (type)
336 tree type;
338 return template_class_depth_real (type, /*count_specializations=*/0);
341 /* Returns 1 if processing DECL as part of do_pending_inlines
342 needs us to push template parms. */
344 static int
345 inline_needs_template_parms (decl)
346 tree decl;
348 if (! DECL_TEMPLATE_INFO (decl))
349 return 0;
351 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
352 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
355 /* Subroutine of maybe_begin_member_template_processing.
356 Push the template parms in PARMS, starting from LEVELS steps into the
357 chain, and ending at the beginning, since template parms are listed
358 innermost first. */
360 static void
361 push_inline_template_parms_recursive (parmlist, levels)
362 tree parmlist;
363 int levels;
365 tree parms = TREE_VALUE (parmlist);
366 int i;
368 if (levels > 1)
369 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
371 ++processing_template_decl;
372 current_template_parms
373 = tree_cons (build_int_2 (0, processing_template_decl),
374 parms, current_template_parms);
375 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
377 pushlevel (0);
378 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
380 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
381 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
383 switch (TREE_CODE (parm))
385 case TYPE_DECL:
386 case TEMPLATE_DECL:
387 pushdecl (parm);
388 break;
390 case PARM_DECL:
392 /* Make a CONST_DECL as is done in process_template_parm.
393 It is ugly that we recreate this here; the original
394 version built in process_template_parm is no longer
395 available. */
396 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
397 TREE_TYPE (parm));
398 SET_DECL_ARTIFICIAL (decl);
399 DECL_INITIAL (decl) = DECL_INITIAL (parm);
400 DECL_TEMPLATE_PARM_P (decl) = 1;
401 pushdecl (decl);
403 break;
405 default:
406 my_friendly_abort (0);
411 /* Restore the template parameter context for a member template or
412 a friend template defined in a class definition. */
414 void
415 maybe_begin_member_template_processing (decl)
416 tree decl;
418 tree parms;
419 int levels = 0;
421 if (inline_needs_template_parms (decl))
423 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
424 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
426 if (DECL_TEMPLATE_SPECIALIZATION (decl))
428 --levels;
429 parms = TREE_CHAIN (parms);
432 push_inline_template_parms_recursive (parms, levels);
435 /* Remember how many levels of template parameters we pushed so that
436 we can pop them later. */
437 if (!inline_parm_levels)
438 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
439 if (inline_parm_levels_used == inline_parm_levels->num_elements)
440 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
441 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
442 ++inline_parm_levels_used;
445 /* Undo the effects of begin_member_template_processing. */
447 void
448 maybe_end_member_template_processing ()
450 int i;
452 if (!inline_parm_levels_used)
453 return;
455 --inline_parm_levels_used;
456 for (i = 0;
457 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
458 ++i)
460 --processing_template_decl;
461 current_template_parms = TREE_CHAIN (current_template_parms);
462 poplevel (0, 0, 0);
466 /* Returns non-zero iff T is a member template function. We must be
467 careful as in
469 template <class T> class C { void f(); }
471 Here, f is a template function, and a member, but not a member
472 template. This function does not concern itself with the origin of
473 T, only its present state. So if we have
475 template <class T> class C { template <class U> void f(U); }
477 then neither C<int>::f<char> nor C<T>::f<double> is considered
478 to be a member template. But, `template <class U> void
479 C<int>::f(U)' is considered a member template. */
482 is_member_template (t)
483 tree t;
485 if (!DECL_FUNCTION_TEMPLATE_P (t))
486 /* Anything that isn't a function or a template function is
487 certainly not a member template. */
488 return 0;
490 /* A local class can't have member templates. */
491 if (hack_decl_function_context (t))
492 return 0;
494 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
495 /* If there are more levels of template parameters than
496 there are template classes surrounding the declaration,
497 then we have a member template. */
498 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
499 template_class_depth (DECL_CLASS_CONTEXT (t))));
502 #if 0 /* UNUSED */
503 /* Returns non-zero iff T is a member template class. See
504 is_member_template for a description of what precisely constitutes
505 a member template. */
508 is_member_template_class (t)
509 tree t;
511 if (!DECL_CLASS_TEMPLATE_P (t))
512 /* Anything that isn't a class template, is certainly not a member
513 template. */
514 return 0;
516 if (!DECL_CLASS_SCOPE_P (t))
517 /* Anything whose context isn't a class type is surely not a
518 member template. */
519 return 0;
521 /* If there are more levels of template parameters than there are
522 template classes surrounding the declaration, then we have a
523 member template. */
524 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
525 template_class_depth (DECL_CONTEXT (t)));
527 #endif
529 /* Return a new template argument vector which contains all of ARGS,
530 but has as its innermost set of arguments the EXTRA_ARGS. The
531 resulting vector will be built on a temporary obstack, and so must
532 be explicitly copied to the permanent obstack, if required. */
534 static tree
535 add_to_template_args (args, extra_args)
536 tree args;
537 tree extra_args;
539 tree new_args;
540 int extra_depth;
541 int i;
542 int j;
544 extra_depth = TMPL_ARGS_DEPTH (extra_args);
545 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
547 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
548 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
550 for (j = 1; j <= extra_depth; ++j, ++i)
551 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
553 return new_args;
556 /* Like add_to_template_args, but only the outermost ARGS are added to
557 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
558 (EXTRA_ARGS) levels are added. This function is used to combine
559 the template arguments from a partial instantiation with the
560 template arguments used to attain the full instantiation from the
561 partial instantiation. */
563 static tree
564 add_outermost_template_args (args, extra_args)
565 tree args;
566 tree extra_args;
568 tree new_args;
570 /* If there are more levels of EXTRA_ARGS than there are ARGS,
571 something very fishy is going on. */
572 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
575 /* If *all* the new arguments will be the EXTRA_ARGS, just return
576 them. */
577 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
578 return extra_args;
580 /* For the moment, we make ARGS look like it contains fewer levels. */
581 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
583 new_args = add_to_template_args (args, extra_args);
585 /* Now, we restore ARGS to its full dimensions. */
586 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
588 return new_args;
591 /* We've got a template header coming up; push to a new level for storing
592 the parms. */
594 void
595 begin_template_parm_list ()
597 /* We use a non-tag-transparent scope here, which causes pushtag to
598 put tags in this scope, rather than in the enclosing class or
599 namespace scope. This is the right thing, since we want
600 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
601 global template class, push_template_decl handles putting the
602 TEMPLATE_DECL into top-level scope. For a nested template class,
603 e.g.:
605 template <class T> struct S1 {
606 template <class T> struct S2 {};
609 pushtag contains special code to call pushdecl_with_scope on the
610 TEMPLATE_DECL for S2. */
611 pushlevel (0);
612 declare_pseudo_global_level ();
613 ++processing_template_decl;
614 ++processing_template_parmlist;
615 note_template_header (0);
618 /* This routine is called when a specialization is declared. If it is
619 illegal to declare a specialization here, an error is reported. */
621 static void
622 check_specialization_scope ()
624 tree scope = current_scope ();
626 /* [temp.expl.spec]
628 An explicit specialization shall be declared in the namespace of
629 which the template is a member, or, for member templates, in the
630 namespace of which the enclosing class or enclosing class
631 template is a member. An explicit specialization of a member
632 function, member class or static data member of a class template
633 shall be declared in the namespace of which the class template
634 is a member. */
635 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
636 cp_error ("explicit specialization in non-namespace scope `%D'",
637 scope);
639 /* [temp.expl.spec]
641 In an explicit specialization declaration for a member of a class
642 template or a member template that appears in namespace scope,
643 the member template and some of its enclosing class templates may
644 remain unspecialized, except that the declaration shall not
645 explicitly specialize a class member template if its enclosing
646 class templates are not explicitly specialized as well. */
647 if (current_template_parms)
648 cp_error ("enclosing class templates are not explicitly specialized");
651 /* We've just seen template <>. */
653 void
654 begin_specialization ()
656 note_template_header (1);
657 check_specialization_scope ();
660 /* Called at then end of processing a declaration preceeded by
661 template<>. */
663 void
664 end_specialization ()
666 reset_specialization ();
669 /* Any template <>'s that we have seen thus far are not referring to a
670 function specialization. */
672 void
673 reset_specialization ()
675 processing_specialization = 0;
676 template_header_count = 0;
679 /* We've just seen a template header. If SPECIALIZATION is non-zero,
680 it was of the form template <>. */
682 static void
683 note_template_header (specialization)
684 int specialization;
686 processing_specialization = specialization;
687 template_header_count++;
690 /* We're beginning an explicit instantiation. */
692 void
693 begin_explicit_instantiation ()
695 ++processing_explicit_instantiation;
699 void
700 end_explicit_instantiation ()
702 my_friendly_assert(processing_explicit_instantiation > 0, 0);
703 --processing_explicit_instantiation;
706 /* The TYPE is being declared. If it is a template type, that means it
707 is a partial specialization. Do appropriate error-checking. */
709 void
710 maybe_process_partial_specialization (type)
711 tree type;
713 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
716 && TYPE_SIZE (type) == NULL_TREE)
718 if (current_namespace
719 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
721 cp_pedwarn ("specializing `%#T' in different namespace", type);
722 cp_pedwarn_at (" from definition of `%#D'",
723 CLASSTYPE_TI_TEMPLATE (type));
725 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
726 if (processing_template_decl)
727 push_template_decl (TYPE_MAIN_DECL (type));
729 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
730 cp_error ("specialization of `%T' after instantiation", type);
732 else if (processing_specialization)
733 cp_error ("explicit specialization of non-template `%T'", type);
736 /* Retrieve the specialization (in the sense of [temp.spec] - a
737 specialization is either an instantiation or an explicit
738 specialization) of TMPL for the given template ARGS. If there is
739 no such specialization, return NULL_TREE. The ARGS are a vector of
740 arguments, or a vector of vectors of arguments, in the case of
741 templates with more than one level of parameters. */
743 static tree
744 retrieve_specialization (tmpl, args)
745 tree tmpl;
746 tree args;
748 tree s;
750 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
752 /* There should be as many levels of arguments as there are
753 levels of parameters. */
754 my_friendly_assert (TMPL_ARGS_DEPTH (args)
755 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
758 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
759 s != NULL_TREE;
760 s = TREE_CHAIN (s))
761 if (comp_template_args (TREE_PURPOSE (s), args))
762 return TREE_VALUE (s);
764 return NULL_TREE;
767 /* Like retrieve_speciailization, but for local declarations. FN is
768 the function in which we are looking for an instantiation. */
770 static tree
771 retrieve_local_specialization (tmpl, fn)
772 tree tmpl;
773 tree fn;
775 tree s = purpose_member (fn, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
776 return s ? TREE_VALUE (s) : NULL_TREE;
779 /* Returns non-zero iff DECL is a specialization of TMPL. */
782 is_specialization_of (decl, tmpl)
783 tree decl;
784 tree tmpl;
786 tree t;
788 if (TREE_CODE (decl) == FUNCTION_DECL)
790 for (t = decl;
791 t != NULL_TREE;
792 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
793 if (t == tmpl)
794 return 1;
796 else
798 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
800 for (t = TREE_TYPE (decl);
801 t != NULL_TREE;
802 t = CLASSTYPE_USE_TEMPLATE (t)
803 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
804 if (same_type_p (TYPE_MAIN_VARIANT (t),
805 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
806 return 1;
809 return 0;
812 /* Register the specialization SPEC as a specialization of TMPL with
813 the indicated ARGS. Returns SPEC, or an equivalent prior
814 declaration, if available. */
816 static tree
817 register_specialization (spec, tmpl, args)
818 tree spec;
819 tree tmpl;
820 tree args;
822 tree s;
824 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
826 if (TREE_CODE (spec) == FUNCTION_DECL
827 && uses_template_parms (DECL_TI_ARGS (spec)))
828 /* This is the FUNCTION_DECL for a partial instantiation. Don't
829 register it; we want the corresponding TEMPLATE_DECL instead.
830 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
831 the more obvious `uses_template_parms (spec)' to avoid problems
832 with default function arguments. In particular, given
833 something like this:
835 template <class T> void f(T t1, T t = T())
837 the default argument expression is not substituted for in an
838 instantiation unless and until it is actually needed. */
839 return spec;
841 /* There should be as many levels of arguments as there are
842 levels of parameters. */
843 my_friendly_assert (TMPL_ARGS_DEPTH (args)
844 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
847 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
848 s != NULL_TREE;
849 s = TREE_CHAIN (s))
851 tree fn = TREE_VALUE (s);
853 /* We can sometimes try to re-register a specialization that we've
854 already got. In particular, regenerate_decl_from_template
855 calls duplicate_decls which will update the specialization
856 list. But, we'll still get called again here anyhow. It's
857 more convenient to simply allow this than to try to prevent it. */
858 if (fn == spec)
859 return spec;
860 else if (comp_template_args (TREE_PURPOSE (s), args))
862 if (DECL_TEMPLATE_SPECIALIZATION (spec))
864 if (DECL_TEMPLATE_INSTANTIATION (fn))
866 if (TREE_USED (fn)
867 || DECL_EXPLICIT_INSTANTIATION (fn))
869 cp_error ("specialization of %D after instantiation",
870 fn);
871 return spec;
873 else
875 /* This situation should occur only if the first
876 specialization is an implicit instantiation,
877 the second is an explicit specialization, and
878 the implicit instantiation has not yet been
879 used. That situation can occur if we have
880 implicitly instantiated a member function and
881 then specialized it later.
883 We can also wind up here if a friend
884 declaration that looked like an instantiation
885 turns out to be a specialization:
887 template <class T> void foo(T);
888 class S { friend void foo<>(int) };
889 template <> void foo(int);
891 We transform the existing DECL in place so that
892 any pointers to it become pointers to the
893 updated declaration.
895 If there was a definition for the template, but
896 not for the specialization, we want this to
897 look as if there is no definition, and vice
898 versa. */
899 DECL_INITIAL (fn) = NULL_TREE;
900 duplicate_decls (spec, fn);
902 return fn;
905 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
907 duplicate_decls (spec, fn);
908 return fn;
914 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
915 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
917 return spec;
920 /* Unregister the specialization SPEC as a specialization of TMPL.
921 Returns nonzero if the SPEC was listed as a specialization of
922 TMPL. */
924 static int
925 unregister_specialization (spec, tmpl)
926 tree spec;
927 tree tmpl;
929 tree* s;
931 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
932 *s != NULL_TREE;
933 s = &TREE_CHAIN (*s))
934 if (TREE_VALUE (*s) == spec)
936 *s = TREE_CHAIN (*s);
937 return 1;
940 return 0;
943 /* Like register_specialization, but for local declarations. FN is
944 the function in which we are registering SPEC, an instantiation of
945 TMPL. */
947 static tree
948 register_local_specialization (spec, tmpl, fn)
949 tree spec;
950 tree tmpl;
951 tree fn;
953 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
954 = tree_cons (fn, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
956 return spec;
959 /* Print the list of candidate FNS in an error message. */
961 void
962 print_candidates (fns)
963 tree fns;
965 tree fn;
967 const char *str = "candidates are:";
969 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
971 tree f;
973 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
974 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
975 str = " ";
979 /* Returns the template (one of the functions given by TEMPLATE_ID)
980 which can be specialized to match the indicated DECL with the
981 explicit template args given in TEMPLATE_ID. The DECL may be
982 NULL_TREE if none is available. In that case, the functions in
983 TEMPLATE_ID are non-members.
985 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
986 specialization of a member template.
988 The template args (those explicitly specified and those deduced)
989 are output in a newly created vector *TARGS_OUT.
991 If it is impossible to determine the result, an error message is
992 issued. The error_mark_node is returned to indicate failure. */
994 static tree
995 determine_specialization (template_id, decl, targs_out,
996 need_member_template)
997 tree template_id;
998 tree decl;
999 tree* targs_out;
1000 int need_member_template;
1002 tree fns;
1003 tree targs;
1004 tree explicit_targs;
1005 tree candidates = NULL_TREE;
1006 tree templates = NULL_TREE;
1008 *targs_out = NULL_TREE;
1010 if (template_id == error_mark_node)
1011 return error_mark_node;
1013 fns = TREE_OPERAND (template_id, 0);
1014 explicit_targs = TREE_OPERAND (template_id, 1);
1016 if (fns == error_mark_node)
1017 return error_mark_node;
1019 /* Check for baselinks. */
1020 if (BASELINK_P (fns))
1021 fns = TREE_VALUE (fns);
1023 if (!is_overloaded_fn (fns))
1025 cp_error ("`%D' is not a function template", fns);
1026 return error_mark_node;
1029 for (; fns; fns = OVL_NEXT (fns))
1031 tree tmpl;
1033 tree fn = OVL_CURRENT (fns);
1035 if (TREE_CODE (fn) == TEMPLATE_DECL)
1036 /* DECL might be a specialization of FN. */
1037 tmpl = fn;
1038 else if (need_member_template)
1039 /* FN is an ordinary member function, and we need a
1040 specialization of a member template. */
1041 continue;
1042 else if (TREE_CODE (fn) != FUNCTION_DECL)
1043 /* We can get IDENTIFIER_NODEs here in certain erroneous
1044 cases. */
1045 continue;
1046 else if (!DECL_FUNCTION_MEMBER_P (fn))
1047 /* This is just an ordinary non-member function. Nothing can
1048 be a specialization of that. */
1049 continue;
1050 else
1052 tree decl_arg_types;
1054 /* This is an ordinary member function. However, since
1055 we're here, we can assume it's enclosing class is a
1056 template class. For example,
1058 template <typename T> struct S { void f(); };
1059 template <> void S<int>::f() {}
1061 Here, S<int>::f is a non-template, but S<int> is a
1062 template class. If FN has the same type as DECL, we
1063 might be in business. */
1064 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1065 TREE_TYPE (TREE_TYPE (fn))))
1066 /* The return types differ. */
1067 continue;
1069 /* Adjust the type of DECL in case FN is a static member. */
1070 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1071 if (DECL_STATIC_FUNCTION_P (fn)
1072 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1073 decl_arg_types = TREE_CHAIN (decl_arg_types);
1075 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1076 decl_arg_types))
1077 /* They match! */
1078 candidates = tree_cons (NULL_TREE, fn, candidates);
1080 continue;
1083 /* See whether this function might be a specialization of this
1084 template. */
1085 targs = get_bindings (tmpl, decl, explicit_targs);
1087 if (!targs)
1088 /* We cannot deduce template arguments that when used to
1089 specialize TMPL will produce DECL. */
1090 continue;
1092 /* Save this template, and the arguments deduced. */
1093 templates = tree_cons (targs, tmpl, templates);
1096 if (templates && TREE_CHAIN (templates))
1098 /* We have:
1100 [temp.expl.spec]
1102 It is possible for a specialization with a given function
1103 signature to be instantiated from more than one function
1104 template. In such cases, explicit specification of the
1105 template arguments must be used to uniquely identify the
1106 function template specialization being specialized.
1108 Note that here, there's no suggestion that we're supposed to
1109 determine which of the candidate templates is most
1110 specialized. However, we, also have:
1112 [temp.func.order]
1114 Partial ordering of overloaded function template
1115 declarations is used in the following contexts to select
1116 the function template to which a function template
1117 specialization refers:
1119 -- when an explicit specialization refers to a function
1120 template.
1122 So, we do use the partial ordering rules, at least for now.
1123 This extension can only serve to make illegal programs legal,
1124 so it's safe. And, there is strong anecdotal evidence that
1125 the committee intended the partial ordering rules to apply;
1126 the EDG front-end has that behavior, and John Spicer claims
1127 that the committee simply forgot to delete the wording in
1128 [temp.expl.spec]. */
1129 tree tmpl = most_specialized (templates, decl, explicit_targs);
1130 if (tmpl && tmpl != error_mark_node)
1132 targs = get_bindings (tmpl, decl, explicit_targs);
1133 templates = tree_cons (targs, tmpl, NULL_TREE);
1137 if (templates == NULL_TREE && candidates == NULL_TREE)
1139 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1140 template_id, decl);
1141 return error_mark_node;
1143 else if ((templates && TREE_CHAIN (templates))
1144 || (candidates && TREE_CHAIN (candidates))
1145 || (templates && candidates))
1147 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1148 template_id, decl);
1149 chainon (candidates, templates);
1150 print_candidates (candidates);
1151 return error_mark_node;
1154 /* We have one, and exactly one, match. */
1155 if (candidates)
1157 /* It was a specialization of an ordinary member function in a
1158 template class. */
1159 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1160 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1163 /* It was a specialization of a template. */
1164 targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates)));
1165 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1167 *targs_out = copy_node (targs);
1168 SET_TMPL_ARGS_LEVEL (*targs_out,
1169 TMPL_ARGS_DEPTH (*targs_out),
1170 TREE_PURPOSE (templates));
1172 else
1173 *targs_out = TREE_PURPOSE (templates);
1174 return TREE_VALUE (templates);
1177 /* Check to see if the function just declared, as indicated in
1178 DECLARATOR, and in DECL, is a specialization of a function
1179 template. We may also discover that the declaration is an explicit
1180 instantiation at this point.
1182 Returns DECL, or an equivalent declaration that should be used
1183 instead if all goes well. Issues an error message if something is
1184 amiss. Returns error_mark_node if the error is not easily
1185 recoverable.
1187 FLAGS is a bitmask consisting of the following flags:
1189 2: The function has a definition.
1190 4: The function is a friend.
1192 The TEMPLATE_COUNT is the number of references to qualifying
1193 template classes that appeared in the name of the function. For
1194 example, in
1196 template <class T> struct S { void f(); };
1197 void S<int>::f();
1199 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1200 classes are not counted in the TEMPLATE_COUNT, so that in
1202 template <class T> struct S {};
1203 template <> struct S<int> { void f(); }
1204 template <> void S<int>::f();
1206 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1207 illegal; there should be no template <>.)
1209 If the function is a specialization, it is marked as such via
1210 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1211 is set up correctly, and it is added to the list of specializations
1212 for that template. */
1214 tree
1215 check_explicit_specialization (declarator, decl, template_count, flags)
1216 tree declarator;
1217 tree decl;
1218 int template_count;
1219 int flags;
1221 int have_def = flags & 2;
1222 int is_friend = flags & 4;
1223 int specialization = 0;
1224 int explicit_instantiation = 0;
1225 int member_specialization = 0;
1227 tree ctype = DECL_CLASS_CONTEXT (decl);
1228 tree dname = DECL_NAME (decl);
1230 if (processing_specialization)
1232 /* The last template header was of the form template <>. */
1234 if (template_header_count > template_count)
1236 /* There were more template headers than qualifying template
1237 classes. */
1238 if (template_header_count - template_count > 1)
1239 /* There shouldn't be that many template parameter lists.
1240 There can be at most one parameter list for every
1241 qualifying class, plus one for the function itself. */
1242 cp_error ("too many template parameter lists in declaration of `%D'", decl);
1244 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1245 if (ctype)
1246 member_specialization = 1;
1247 else
1248 specialization = 1;
1250 else if (template_header_count == template_count)
1252 /* The counts are equal. So, this might be a
1253 specialization, but it is not a specialization of a
1254 member template. It might be something like
1256 template <class T> struct S {
1257 void f(int i);
1259 template <>
1260 void S<int>::f(int i) {} */
1261 specialization = 1;
1262 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1264 else
1266 /* This cannot be an explicit specialization. There are not
1267 enough headers for all of the qualifying classes. For
1268 example, we might have:
1270 template <>
1271 void S<int>::T<char>::f();
1273 But, we're missing another template <>. */
1274 cp_error("too few template parameter lists in declaration of `%D'", decl);
1275 return decl;
1278 else if (processing_explicit_instantiation)
1280 if (template_header_count)
1281 cp_error ("template parameter list used in explicit instantiation");
1283 if (have_def)
1284 cp_error ("definition provided for explicit instantiation");
1286 explicit_instantiation = 1;
1288 else if (ctype != NULL_TREE
1289 && !TYPE_BEING_DEFINED (ctype)
1290 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1291 && !is_friend)
1293 /* This case catches outdated code that looks like this:
1295 template <class T> struct S { void f(); };
1296 void S<int>::f() {} // Missing template <>
1298 We disable this check when the type is being defined to
1299 avoid complaining about default compiler-generated
1300 constructors, destructors, and assignment operators.
1301 Since the type is an instantiation, not a specialization,
1302 these are the only functions that can be defined before
1303 the class is complete. */
1305 /* If they said
1306 template <class T> void S<int>::f() {}
1307 that's bogus. */
1308 if (template_header_count)
1310 cp_error ("template parameters specified in specialization");
1311 return decl;
1314 if (pedantic)
1315 cp_pedwarn
1316 ("explicit specialization not preceded by `template <>'");
1317 specialization = 1;
1318 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1320 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1322 if (is_friend)
1323 /* This could be something like:
1325 template <class T> void f(T);
1326 class S { friend void f<>(int); } */
1327 specialization = 1;
1328 else
1330 /* This case handles bogus declarations like template <>
1331 template <class T> void f<int>(); */
1333 cp_error ("template-id `%D' in declaration of primary template",
1334 declarator);
1335 return decl;
1339 if (specialization || member_specialization)
1341 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1342 for (; t; t = TREE_CHAIN (t))
1343 if (TREE_PURPOSE (t))
1345 cp_pedwarn
1346 ("default argument specified in explicit specialization");
1347 break;
1349 if (current_lang_name == lang_name_c)
1350 cp_error ("template specialization with C linkage");
1353 if (specialization || member_specialization || explicit_instantiation)
1355 tree tmpl = NULL_TREE;
1356 tree targs = NULL_TREE;
1358 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1359 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1361 tree fns;
1363 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1365 if (!ctype)
1366 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1367 else
1368 fns = dname;
1370 declarator =
1371 lookup_template_function (fns, NULL_TREE);
1374 if (declarator == error_mark_node)
1375 return error_mark_node;
1377 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1379 if (!explicit_instantiation)
1380 /* A specialization in class scope. This is illegal,
1381 but the error will already have been flagged by
1382 check_specialization_scope. */
1383 return error_mark_node;
1384 else
1386 /* It's not legal to write an explicit instantiation in
1387 class scope, e.g.:
1389 class C { template void f(); }
1391 This case is caught by the parser. However, on
1392 something like:
1394 template class C { void f(); };
1396 (which is illegal) we can get here. The error will be
1397 issued later. */
1401 return decl;
1403 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1405 /* A friend declaration. We can't do much, because we don't
1406 know what this resolves to, yet. */
1407 my_friendly_assert (is_friend != 0, 0);
1408 my_friendly_assert (!explicit_instantiation, 0);
1409 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1410 return decl;
1412 else if (ctype != NULL_TREE
1413 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1414 IDENTIFIER_NODE))
1416 /* Find the list of functions in ctype that have the same
1417 name as the declared function. */
1418 tree name = TREE_OPERAND (declarator, 0);
1419 tree fns = NULL_TREE;
1420 int idx;
1422 if (name == constructor_name (ctype)
1423 || name == constructor_name_full (ctype))
1425 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1427 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1428 : !TYPE_HAS_DESTRUCTOR (ctype))
1430 /* From [temp.expl.spec]:
1432 If such an explicit specialization for the member
1433 of a class template names an implicitly-declared
1434 special member function (clause _special_), the
1435 program is ill-formed.
1437 Similar language is found in [temp.explicit]. */
1438 cp_error ("specialization of implicitly-declared special member function");
1439 return error_mark_node;
1442 name = is_constructor ? ctor_identifier : dtor_identifier;
1445 if (!IDENTIFIER_TYPENAME_P (name))
1447 idx = lookup_fnfields_1 (ctype, name);
1448 if (idx >= 0)
1449 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1451 else
1453 tree methods;
1455 /* For a type-conversion operator, we cannot do a
1456 name-based lookup. We might be looking for `operator
1457 int' which will be a specialization of `operator T'.
1458 So, we find *all* the conversion operators, and then
1459 select from them. */
1460 fns = NULL_TREE;
1462 methods = CLASSTYPE_METHOD_VEC (ctype);
1463 if (methods)
1464 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1466 tree ovl = TREE_VEC_ELT (methods, idx);
1468 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1469 /* There are no more conversion functions. */
1470 break;
1472 /* Glue all these conversion functions together
1473 with those we already have. */
1474 for (; ovl; ovl = OVL_NEXT (ovl))
1475 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1479 if (fns == NULL_TREE)
1481 cp_error ("no member function `%D' declared in `%T'",
1482 name, ctype);
1483 return error_mark_node;
1485 else
1486 TREE_OPERAND (declarator, 0) = fns;
1489 /* Figure out what exactly is being specialized at this point.
1490 Note that for an explicit instantiation, even one for a
1491 member function, we cannot tell apriori whether the
1492 instantiation is for a member template, or just a member
1493 function of a template class. Even if a member template is
1494 being instantiated, the member template arguments may be
1495 elided if they can be deduced from the rest of the
1496 declaration. */
1497 tmpl = determine_specialization (declarator, decl,
1498 &targs,
1499 member_specialization);
1501 if (!tmpl || tmpl == error_mark_node)
1502 /* We couldn't figure out what this declaration was
1503 specializing. */
1504 return error_mark_node;
1505 else
1507 tree gen_tmpl = most_general_template (tmpl);
1509 if (explicit_instantiation)
1511 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1512 is done by do_decl_instantiation later. */
1514 int arg_depth = TMPL_ARGS_DEPTH (targs);
1515 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1517 if (arg_depth > parm_depth)
1519 /* If TMPL is not the most general template (for
1520 example, if TMPL is a friend template that is
1521 injected into namespace scope), then there will
1522 be too many levels fo TARGS. Remove some of them
1523 here. */
1524 int i;
1525 tree new_targs;
1527 new_targs = make_temp_vec (parm_depth);
1528 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1529 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1530 = TREE_VEC_ELT (targs, i);
1531 targs = new_targs;
1534 decl = instantiate_template (tmpl, targs);
1535 return decl;
1538 /* If we though that the DECL was a member function, but it
1539 turns out to be specializing a static member function,
1540 make DECL a static member function as well. */
1541 if (DECL_STATIC_FUNCTION_P (tmpl)
1542 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1544 revert_static_member_fn (&decl, 0, 0);
1545 last_function_parms = TREE_CHAIN (last_function_parms);
1548 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1549 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1551 /* Mangle the function name appropriately. Note that we do
1552 not mangle specializations of non-template member
1553 functions of template classes, e.g. with
1555 template <class T> struct S { void f(); }
1557 and given the specialization
1559 template <> void S<int>::f() {}
1561 we do not mangle S<int>::f() here. That's because it's
1562 just an ordinary member function and doesn't need special
1563 treatment. We do this here so that the ordinary,
1564 non-template, name-mangling algorith will not be used
1565 later. */
1566 if ((is_member_template (tmpl) || ctype == NULL_TREE)
1567 && name_mangling_version >= 1)
1568 set_mangled_name_for_template_decl (decl);
1570 if (is_friend && !have_def)
1571 /* This is not really a declaration of a specialization.
1572 It's just the name of an instantiation. But, it's not
1573 a request for an instantiation, either. */
1574 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1576 /* Register this specialization so that we can find it
1577 again. */
1578 decl = register_specialization (decl, gen_tmpl, targs);
1582 return decl;
1585 /* TYPE is being declared. Verify that the use of template headers
1586 and such is reasonable. Issue error messages if not. */
1588 void
1589 maybe_check_template_type (type)
1590 tree type;
1592 if (template_header_count)
1594 /* We are in the scope of some `template <...>' header. */
1596 int context_depth
1597 = template_class_depth_real (TYPE_CONTEXT (type),
1598 /*count_specializations=*/1);
1600 if (template_header_count <= context_depth)
1601 /* This is OK; the template headers are for the context. We
1602 are actually too lenient here; like
1603 check_explicit_specialization we should consider the number
1604 of template types included in the actual declaration. For
1605 example,
1607 template <class T> struct S {
1608 template <class U> template <class V>
1609 struct I {};
1612 is illegal, but:
1614 template <class T> struct S {
1615 template <class U> struct I;
1618 template <class T> template <class U.
1619 struct S<T>::I {};
1621 is not. */
1623 else if (template_header_count > context_depth + 1)
1624 /* There are two many template parameter lists. */
1625 cp_error ("too many template parameter lists in declaration of `%T'", type);
1629 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1630 parameters. These are represented in the same format used for
1631 DECL_TEMPLATE_PARMS. */
1633 int comp_template_parms (parms1, parms2)
1634 tree parms1;
1635 tree parms2;
1637 tree p1;
1638 tree p2;
1640 if (parms1 == parms2)
1641 return 1;
1643 for (p1 = parms1, p2 = parms2;
1644 p1 != NULL_TREE && p2 != NULL_TREE;
1645 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1647 tree t1 = TREE_VALUE (p1);
1648 tree t2 = TREE_VALUE (p2);
1649 int i;
1651 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1652 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1654 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1655 return 0;
1657 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1659 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1660 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1662 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1663 return 0;
1665 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1666 continue;
1667 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1668 return 0;
1672 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1673 /* One set of parameters has more parameters lists than the
1674 other. */
1675 return 0;
1677 return 1;
1680 /* Complain if DECL shadows a template parameter.
1682 [temp.local]: A template-parameter shall not be redeclared within its
1683 scope (including nested scopes). */
1685 void
1686 check_template_shadow (decl)
1687 tree decl;
1689 tree olddecl;
1691 /* If we're not in a template, we can't possibly shadow a template
1692 parameter. */
1693 if (!current_template_parms)
1694 return;
1696 /* Figure out what we're shadowing. */
1697 if (TREE_CODE (decl) == OVERLOAD)
1698 decl = OVL_CURRENT (decl);
1699 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1701 /* If there's no previous binding for this name, we're not shadowing
1702 anything, let alone a template parameter. */
1703 if (!olddecl)
1704 return;
1706 /* If we're not shadowing a template parameter, we're done. Note
1707 that OLDDECL might be an OVERLOAD (or perhaps even an
1708 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1709 node. */
1710 if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd'
1711 || !DECL_TEMPLATE_PARM_P (olddecl))
1712 return;
1714 /* We check for decl != olddecl to avoid bogus errors for using a
1715 name inside a class. We check TPFI to avoid duplicate errors for
1716 inline member templates. */
1717 if (decl == olddecl
1718 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1719 return;
1721 cp_error_at ("declaration of `%#D'", decl);
1722 cp_error_at (" shadows template parm `%#D'", olddecl);
1725 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1726 ORIG_LEVEL, DECL, and TYPE. */
1728 static tree
1729 build_template_parm_index (index, level, orig_level, decl, type)
1730 int index;
1731 int level;
1732 int orig_level;
1733 tree decl;
1734 tree type;
1736 tree t = make_node (TEMPLATE_PARM_INDEX);
1737 TEMPLATE_PARM_IDX (t) = index;
1738 TEMPLATE_PARM_LEVEL (t) = level;
1739 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1740 TEMPLATE_PARM_DECL (t) = decl;
1741 TREE_TYPE (t) = type;
1743 return t;
1746 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1747 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1748 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1749 new one is created. */
1751 static tree
1752 reduce_template_parm_level (index, type, levels)
1753 tree index;
1754 tree type;
1755 int levels;
1757 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1758 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1759 != TEMPLATE_PARM_LEVEL (index) - levels))
1761 tree decl
1762 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1763 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1764 type);
1765 tree t
1766 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1767 TEMPLATE_PARM_LEVEL (index) - levels,
1768 TEMPLATE_PARM_ORIG_LEVEL (index),
1769 decl, type);
1770 TEMPLATE_PARM_DESCENDANTS (index) = t;
1772 /* Template template parameters need this. */
1773 DECL_TEMPLATE_PARMS (decl)
1774 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1777 return TEMPLATE_PARM_DESCENDANTS (index);
1780 /* Process information from new template parameter NEXT and append it to the
1781 LIST being built. */
1783 tree
1784 process_template_parm (list, next)
1785 tree list, next;
1787 tree parm;
1788 tree decl = 0;
1789 tree defval;
1790 int is_type, idx;
1792 parm = next;
1793 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1794 defval = TREE_PURPOSE (parm);
1795 parm = TREE_VALUE (parm);
1796 is_type = TREE_PURPOSE (parm) == class_type_node;
1798 if (list)
1800 tree p = TREE_VALUE (tree_last (list));
1802 if (TREE_CODE (p) == TYPE_DECL)
1803 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1804 else if (TREE_CODE (p) == TEMPLATE_DECL)
1805 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
1806 else
1807 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1808 ++idx;
1810 else
1811 idx = 0;
1813 if (!is_type)
1815 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1816 /* is a const-param */
1817 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1818 PARM, 0, NULL_TREE);
1820 /* [temp.param]
1822 The top-level cv-qualifiers on the template-parameter are
1823 ignored when determining its type. */
1824 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1826 /* A template parameter is not modifiable. */
1827 TREE_READONLY (parm) = 1;
1828 if (IS_AGGR_TYPE (TREE_TYPE (parm))
1829 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1830 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
1832 cp_error ("`%#T' is not a valid type for a template constant parameter",
1833 TREE_TYPE (parm));
1834 if (DECL_NAME (parm) == NULL_TREE)
1835 error (" a template type parameter must begin with `class' or `typename'");
1836 TREE_TYPE (parm) = void_type_node;
1838 else if (pedantic
1839 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1840 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
1841 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1842 TREE_TYPE (parm));
1843 if (TREE_PERMANENT (parm) == 0)
1845 parm = copy_node (parm);
1846 TREE_PERMANENT (parm) = 1;
1848 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1849 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1850 = build_template_parm_index (idx, processing_template_decl,
1851 processing_template_decl,
1852 decl, TREE_TYPE (parm));
1854 else
1856 tree t;
1857 parm = TREE_VALUE (parm);
1859 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1861 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1862 /* This is for distinguishing between real templates and template
1863 template parameters */
1864 TREE_TYPE (parm) = t;
1865 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1866 decl = parm;
1868 else
1870 t = make_lang_type (TEMPLATE_TYPE_PARM);
1871 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1872 decl = build_decl (TYPE_DECL, parm, t);
1875 TYPE_NAME (t) = decl;
1876 TYPE_STUB_DECL (t) = decl;
1877 parm = decl;
1878 TEMPLATE_TYPE_PARM_INDEX (t)
1879 = build_template_parm_index (idx, processing_template_decl,
1880 processing_template_decl,
1881 decl, TREE_TYPE (parm));
1883 SET_DECL_ARTIFICIAL (decl);
1884 DECL_TEMPLATE_PARM_P (decl) = 1;
1885 pushdecl (decl);
1886 parm = build_tree_list (defval, parm);
1887 return chainon (list, parm);
1890 /* The end of a template parameter list has been reached. Process the
1891 tree list into a parameter vector, converting each parameter into a more
1892 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1893 as PARM_DECLs. */
1895 tree
1896 end_template_parm_list (parms)
1897 tree parms;
1899 int nparms;
1900 tree parm;
1901 tree saved_parmlist = make_tree_vec (list_length (parms));
1903 current_template_parms
1904 = tree_cons (build_int_2 (0, processing_template_decl),
1905 saved_parmlist, current_template_parms);
1907 for (parm = parms, nparms = 0;
1908 parm;
1909 parm = TREE_CHAIN (parm), nparms++)
1910 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
1912 --processing_template_parmlist;
1914 return saved_parmlist;
1917 /* end_template_decl is called after a template declaration is seen. */
1919 void
1920 end_template_decl ()
1922 reset_specialization ();
1924 if (! processing_template_decl)
1925 return;
1927 /* This matches the pushlevel in begin_template_parm_list. */
1928 poplevel (0, 0, 0);
1930 --processing_template_decl;
1931 current_template_parms = TREE_CHAIN (current_template_parms);
1932 (void) get_pending_sizes (); /* Why? */
1935 /* Given a template argument vector containing the template PARMS.
1936 The innermost PARMS are given first. */
1938 tree
1939 current_template_args ()
1941 tree header;
1942 tree args = NULL_TREE;
1943 int length = TMPL_PARMS_DEPTH (current_template_parms);
1944 int l = length;
1946 /* If there is only one level of template parameters, we do not
1947 create a TREE_VEC of TREE_VECs. Instead, we return a single
1948 TREE_VEC containing the arguments. */
1949 if (length > 1)
1950 args = make_tree_vec (length);
1952 for (header = current_template_parms; header; header = TREE_CHAIN (header))
1954 tree a = copy_node (TREE_VALUE (header));
1955 int i;
1957 TREE_TYPE (a) = NULL_TREE;
1958 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
1960 tree t = TREE_VEC_ELT (a, i);
1962 /* T will be a list if we are called from within a
1963 begin/end_template_parm_list pair, but a vector directly
1964 if within a begin/end_member_template_processing pair. */
1965 if (TREE_CODE (t) == TREE_LIST)
1967 t = TREE_VALUE (t);
1969 if (TREE_CODE (t) == TYPE_DECL
1970 || TREE_CODE (t) == TEMPLATE_DECL)
1971 t = TREE_TYPE (t);
1972 else
1973 t = DECL_INITIAL (t);
1974 TREE_VEC_ELT (a, i) = t;
1978 if (length > 1)
1979 TREE_VEC_ELT (args, --l) = a;
1980 else
1981 args = a;
1984 return args;
1987 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1988 template PARMS. Used by push_template_decl below. */
1990 static tree
1991 build_template_decl (decl, parms)
1992 tree decl;
1993 tree parms;
1995 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1996 DECL_TEMPLATE_PARMS (tmpl) = parms;
1997 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1998 if (DECL_LANG_SPECIFIC (decl))
2000 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
2001 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2002 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2003 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2006 return tmpl;
2009 struct template_parm_data
2011 /* The level of the template parameters we are currently
2012 processing. */
2013 int level;
2015 /* The index of the specialization argument we are currently
2016 processing. */
2017 int current_arg;
2019 /* An array whose size is the number of template parameters. The
2020 elements are non-zero if the parameter has been used in any one
2021 of the arguments processed so far. */
2022 int* parms;
2024 /* An array whose size is the number of template arguments. The
2025 elements are non-zero if the argument makes use of template
2026 parameters of this level. */
2027 int* arg_uses_template_parms;
2030 /* Subroutine of push_template_decl used to see if each template
2031 parameter in a partial specialization is used in the explicit
2032 argument list. If T is of the LEVEL given in DATA (which is
2033 treated as a template_parm_data*), then DATA->PARMS is marked
2034 appropriately. */
2036 static int
2037 mark_template_parm (t, data)
2038 tree t;
2039 void* data;
2041 int level;
2042 int idx;
2043 struct template_parm_data* tpd = (struct template_parm_data*) data;
2045 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2047 level = TEMPLATE_PARM_LEVEL (t);
2048 idx = TEMPLATE_PARM_IDX (t);
2050 else
2052 level = TEMPLATE_TYPE_LEVEL (t);
2053 idx = TEMPLATE_TYPE_IDX (t);
2056 if (level == tpd->level)
2058 tpd->parms[idx] = 1;
2059 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2062 /* Return zero so that for_each_template_parm will continue the
2063 traversal of the tree; we want to mark *every* template parm. */
2064 return 0;
2067 /* Process the partial specialization DECL. */
2069 static tree
2070 process_partial_specialization (decl)
2071 tree decl;
2073 tree type = TREE_TYPE (decl);
2074 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2075 tree specargs = CLASSTYPE_TI_ARGS (type);
2076 tree inner_args = innermost_args (specargs);
2077 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2078 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2079 int nargs = TREE_VEC_LENGTH (inner_args);
2080 int ntparms = TREE_VEC_LENGTH (inner_parms);
2081 int i;
2082 int did_error_intro = 0;
2083 struct template_parm_data tpd;
2084 struct template_parm_data tpd2;
2086 /* We check that each of the template parameters given in the
2087 partial specialization is used in the argument list to the
2088 specialization. For example:
2090 template <class T> struct S;
2091 template <class T> struct S<T*>;
2093 The second declaration is OK because `T*' uses the template
2094 parameter T, whereas
2096 template <class T> struct S<int>;
2098 is no good. Even trickier is:
2100 template <class T>
2101 struct S1
2103 template <class U>
2104 struct S2;
2105 template <class U>
2106 struct S2<T>;
2109 The S2<T> declaration is actually illegal; it is a
2110 full-specialization. Of course,
2112 template <class U>
2113 struct S2<T (*)(U)>;
2115 or some such would have been OK. */
2116 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2117 tpd.parms = alloca (sizeof (int) * ntparms);
2118 bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
2120 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2121 bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs);
2122 for (i = 0; i < nargs; ++i)
2124 tpd.current_arg = i;
2125 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2126 &mark_template_parm,
2127 &tpd);
2129 for (i = 0; i < ntparms; ++i)
2130 if (tpd.parms[i] == 0)
2132 /* One of the template parms was not used in the
2133 specialization. */
2134 if (!did_error_intro)
2136 cp_error ("template parameters not used in partial specialization:");
2137 did_error_intro = 1;
2140 cp_error (" `%D'",
2141 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2144 /* [temp.class.spec]
2146 The argument list of the specialization shall not be identical to
2147 the implicit argument list of the primary template. */
2148 if (comp_template_args (inner_args,
2149 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2150 (maintmpl)))))
2151 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
2153 /* [temp.class.spec]
2155 A partially specialized non-type argument expression shall not
2156 involve template parameters of the partial specialization except
2157 when the argument expression is a simple identifier.
2159 The type of a template parameter corresponding to a specialized
2160 non-type argument shall not be dependent on a parameter of the
2161 specialization. */
2162 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2163 tpd2.parms = 0;
2164 for (i = 0; i < nargs; ++i)
2166 tree arg = TREE_VEC_ELT (inner_args, i);
2167 if (/* These first two lines are the `non-type' bit. */
2168 TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
2169 && TREE_CODE (arg) != TEMPLATE_DECL
2170 /* This next line is the `argument expression is not just a
2171 simple identifier' condition and also the `specialized
2172 non-type argument' bit. */
2173 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2175 if (tpd.arg_uses_template_parms[i])
2176 cp_error ("template argument `%E' involves template parameter(s)", arg);
2177 else
2179 /* Look at the corresponding template parameter,
2180 marking which template parameters its type depends
2181 upon. */
2182 tree type =
2183 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2184 i)));
2186 if (!tpd2.parms)
2188 /* We haven't yet initialized TPD2. Do so now. */
2189 tpd2.arg_uses_template_parms
2190 = (int*) alloca (sizeof (int) * nargs);
2191 /* The number of parameters here is the number in the
2192 main template, which, as checked in the assertion
2193 above, is NARGS. */
2194 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2195 tpd2.level =
2196 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2199 /* Mark the template parameters. But this time, we're
2200 looking for the template parameters of the main
2201 template, not in the specialization. */
2202 tpd2.current_arg = i;
2203 tpd2.arg_uses_template_parms[i] = 0;
2204 bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
2205 for_each_template_parm (type,
2206 &mark_template_parm,
2207 &tpd2);
2209 if (tpd2.arg_uses_template_parms [i])
2211 /* The type depended on some template parameters.
2212 If they are fully specialized in the
2213 specialization, that's OK. */
2214 int j;
2215 for (j = 0; j < nargs; ++j)
2216 if (tpd2.parms[j] != 0
2217 && tpd.arg_uses_template_parms [j])
2219 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2220 type,
2221 arg);
2222 break;
2229 if (retrieve_specialization (maintmpl, specargs))
2230 /* We've already got this specialization. */
2231 return decl;
2233 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
2234 = tree_cons (inner_args, inner_parms,
2235 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2236 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2237 return decl;
2240 /* Check that a template declaration's use of default arguments is not
2241 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2242 non-zero if DECL is the thing declared by a primary template.
2243 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2245 static void
2246 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2247 tree decl;
2248 tree parms;
2249 int is_primary;
2250 int is_partial;
2252 const char *msg;
2253 int last_level_to_check;
2254 tree parm_level;
2256 /* [temp.param]
2258 A default template-argument shall not be specified in a
2259 function template declaration or a function template definition, nor
2260 in the template-parameter-list of the definition of a member of a
2261 class template. */
2263 if (TREE_CODE (DECL_REAL_CONTEXT (decl)) == FUNCTION_DECL)
2264 /* You can't have a function template declaration in a local
2265 scope, nor you can you define a member of a class template in a
2266 local scope. */
2267 return;
2269 if (current_class_type
2270 && !TYPE_BEING_DEFINED (current_class_type)
2271 && DECL_LANG_SPECIFIC (decl)
2272 /* If this is either a friend defined in the scope of the class
2273 or a member function. */
2274 && DECL_CLASS_CONTEXT (decl) == current_class_type
2275 /* And, if it was a member function, it really was defined in
2276 the scope of the class. */
2277 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl)))
2278 /* We already checked these parameters when the template was
2279 declared, so there's no need to do it again now. This function
2280 was defined in class scope, but we're processing it's body now
2281 that the class is complete. */
2282 return;
2284 /* [temp.param]
2286 If a template-parameter has a default template-argument, all
2287 subsequent template-parameters shall have a default
2288 template-argument supplied. */
2289 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2291 tree inner_parms = TREE_VALUE (parm_level);
2292 int ntparms = TREE_VEC_LENGTH (inner_parms);
2293 int seen_def_arg_p = 0;
2294 int i;
2296 for (i = 0; i < ntparms; ++i)
2298 tree parm = TREE_VEC_ELT (inner_parms, i);
2299 if (TREE_PURPOSE (parm))
2300 seen_def_arg_p = 1;
2301 else if (seen_def_arg_p)
2303 cp_error ("no default argument for `%D'", TREE_VALUE (parm));
2304 /* For better subsequent error-recovery, we indicate that
2305 there should have been a default argument. */
2306 TREE_PURPOSE (parm) = error_mark_node;
2311 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2312 /* For an ordinary class template, default template arguments are
2313 allowed at the innermost level, e.g.:
2314 template <class T = int>
2315 struct S {};
2316 but, in a partial specialization, they're not allowed even
2317 there, as we have in [temp.class.spec]:
2319 The template parameter list of a specialization shall not
2320 contain default template argument values.
2322 So, for a partial specialization, or for a function template,
2323 we look at all of them. */
2325 else
2326 /* But, for a primary class template that is not a partial
2327 specialization we look at all template parameters except the
2328 innermost ones. */
2329 parms = TREE_CHAIN (parms);
2331 /* Figure out what error message to issue. */
2332 if (TREE_CODE (decl) == FUNCTION_DECL)
2333 msg = "default argument for template parameter in function template `%D'";
2334 else if (is_partial)
2335 msg = "default argument in partial specialization `%D'";
2336 else
2337 msg = "default argument for template parameter for class enclosing `%D'";
2339 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2340 /* If we're inside a class definition, there's no need to
2341 examine the parameters to the class itself. On the one
2342 hand, they will be checked when the class is defined, and,
2343 on the other, default arguments are legal in things like:
2344 template <class T = double>
2345 struct S { template <class U> void f(U); };
2346 Here the default argument for `S' has no bearing on the
2347 declaration of `f'. */
2348 last_level_to_check = template_class_depth (current_class_type) + 1;
2349 else
2350 /* Check everything. */
2351 last_level_to_check = 0;
2353 for (parm_level = parms;
2354 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2355 parm_level = TREE_CHAIN (parm_level))
2357 tree inner_parms = TREE_VALUE (parm_level);
2358 int i;
2359 int ntparms;
2361 ntparms = TREE_VEC_LENGTH (inner_parms);
2362 for (i = 0; i < ntparms; ++i)
2363 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2365 if (msg)
2367 cp_error (msg, decl);
2368 msg = 0;
2371 /* Clear out the default argument so that we are not
2372 confused later. */
2373 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2376 /* At this point, if we're still interested in issuing messages,
2377 they must apply to classes surrounding the object declared. */
2378 if (msg)
2379 msg = "default argument for template parameter for class enclosing `%D'";
2383 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2384 parameters given by current_template_args, or reuses a
2385 previously existing one, if appropriate. Returns the DECL, or an
2386 equivalent one, if it is replaced via a call to duplicate_decls.
2388 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2390 tree
2391 push_template_decl_real (decl, is_friend)
2392 tree decl;
2393 int is_friend;
2395 tree tmpl;
2396 tree args;
2397 tree info;
2398 tree ctx;
2399 int primary;
2400 int is_partial;
2401 int new_template_p = 0;
2403 /* See if this is a partial specialization. */
2404 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2405 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2406 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2408 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2410 if (is_friend)
2411 /* For a friend, we want the context of the friend function, not
2412 the type of which it is a friend. */
2413 ctx = DECL_CONTEXT (decl);
2414 else if (DECL_REAL_CONTEXT (decl)
2415 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
2416 /* In the case of a virtual function, we want the class in which
2417 it is defined. */
2418 ctx = DECL_REAL_CONTEXT (decl);
2419 else
2420 /* Otherwise, if we're currently definining some class, the DECL
2421 is assumed to be a member of the class. */
2422 ctx = current_scope ();
2424 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2425 ctx = NULL_TREE;
2427 if (!DECL_CONTEXT (decl))
2428 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2430 /* See if this is a primary template. */
2431 primary = pseudo_global_level_p ();
2433 if (primary)
2435 if (current_lang_name == lang_name_c)
2436 cp_error ("template with C linkage");
2437 else if (TREE_CODE (decl) == TYPE_DECL
2438 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2439 cp_error ("template class without a name");
2440 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2441 && CLASS_TYPE_P (TREE_TYPE (decl)))
2442 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2443 || TREE_CODE (decl) == FUNCTION_DECL)
2444 /* OK */;
2445 else
2446 cp_error ("template declaration of `%#D'", decl);
2449 /* Check to see that the rules regarding the use of default
2450 arguments are not being violated. */
2451 check_default_tmpl_args (decl, current_template_parms,
2452 primary, is_partial);
2454 if (is_partial)
2455 return process_partial_specialization (decl);
2457 args = current_template_args ();
2459 if (!ctx
2460 || TREE_CODE (ctx) == FUNCTION_DECL
2461 || TYPE_BEING_DEFINED (ctx)
2462 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2464 if (DECL_LANG_SPECIFIC (decl)
2465 && DECL_TEMPLATE_INFO (decl)
2466 && DECL_TI_TEMPLATE (decl))
2467 tmpl = DECL_TI_TEMPLATE (decl);
2468 /* If DECL is a TYPE_DECL for a class-template, then there won't
2469 be DECL_LANG_SPECIFIC. The information equivalent to
2470 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2471 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2472 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2473 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2475 /* Since a template declaration already existed for this
2476 class-type, we must be redeclaring it here. Make sure
2477 that the redeclaration is legal. */
2478 redeclare_class_template (TREE_TYPE (decl),
2479 current_template_parms);
2480 /* We don't need to create a new TEMPLATE_DECL; just use the
2481 one we already had. */
2482 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2484 else
2486 tmpl = build_template_decl (decl, current_template_parms);
2487 new_template_p = 1;
2489 if (DECL_LANG_SPECIFIC (decl)
2490 && DECL_TEMPLATE_SPECIALIZATION (decl))
2492 /* A specialization of a member template of a template
2493 class. */
2494 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2495 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2496 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2500 else
2502 tree a, t, current, parms;
2503 int i;
2505 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2506 cp_error ("must specialize `%#T' before defining member `%#D'",
2507 ctx, decl);
2508 if (TREE_CODE (decl) == TYPE_DECL)
2510 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2511 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2512 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2513 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2514 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2515 else
2517 cp_error ("`%D' does not declare a template type", decl);
2518 return decl;
2521 else if (! DECL_TEMPLATE_INFO (decl))
2523 cp_error ("template definition of non-template `%#D'", decl);
2524 return decl;
2526 else
2527 tmpl = DECL_TI_TEMPLATE (decl);
2529 if (is_member_template (tmpl)
2530 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2531 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2532 && DECL_TEMPLATE_SPECIALIZATION (decl))
2534 tree new_tmpl;
2536 /* The declaration is a specialization of a member
2537 template, declared outside the class. Therefore, the
2538 innermost template arguments will be NULL, so we
2539 replace them with the arguments determined by the
2540 earlier call to check_explicit_specialization. */
2541 args = DECL_TI_ARGS (decl);
2543 new_tmpl
2544 = build_template_decl (decl, current_template_parms);
2545 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2546 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2547 DECL_TI_TEMPLATE (decl) = new_tmpl;
2548 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2549 DECL_TEMPLATE_INFO (new_tmpl)
2550 = tree_cons (tmpl, args, NULL_TREE);
2552 register_specialization (new_tmpl, tmpl, args);
2553 return decl;
2556 /* Make sure the template headers we got make sense. */
2558 parms = DECL_TEMPLATE_PARMS (tmpl);
2559 i = TMPL_PARMS_DEPTH (parms);
2560 if (TMPL_ARGS_DEPTH (args) != i)
2562 cp_error ("expected %d levels of template parms for `%#D', got %d",
2563 i, decl, TMPL_ARGS_DEPTH (args));
2565 else
2566 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2568 a = TMPL_ARGS_LEVEL (args, i);
2569 t = INNERMOST_TEMPLATE_PARMS (parms);
2571 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2573 if (current == decl)
2574 cp_error ("got %d template parameters for `%#D'",
2575 TREE_VEC_LENGTH (a), decl);
2576 else
2577 cp_error ("got %d template parameters for `%#T'",
2578 TREE_VEC_LENGTH (a), current);
2579 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2582 /* Perhaps we should also check that the parms are used in the
2583 appropriate qualifying scopes in the declarator? */
2585 if (current == decl)
2586 current = ctx;
2587 else
2588 current = TYPE_CONTEXT (current);
2592 DECL_TEMPLATE_RESULT (tmpl) = decl;
2593 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2595 /* Push template declarations for global functions and types. Note
2596 that we do not try to push a global template friend declared in a
2597 template class; such a thing may well depend on the template
2598 parameters of the class. */
2599 if (new_template_p && !ctx
2600 && !(is_friend && template_class_depth (current_class_type) > 0))
2601 tmpl = pushdecl_namespace_level (tmpl);
2603 if (primary)
2604 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2606 info = tree_cons (tmpl, args, NULL_TREE);
2608 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2610 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2611 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2612 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
2613 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2615 else if (DECL_LANG_SPECIFIC (decl))
2616 DECL_TEMPLATE_INFO (decl) = info;
2618 return DECL_TEMPLATE_RESULT (tmpl);
2621 tree
2622 push_template_decl (decl)
2623 tree decl;
2625 return push_template_decl_real (decl, 0);
2628 /* Called when a class template TYPE is redeclared with the indicated
2629 template PARMS, e.g.:
2631 template <class T> struct S;
2632 template <class T> struct S {}; */
2634 void
2635 redeclare_class_template (type, parms)
2636 tree type;
2637 tree parms;
2639 tree tmpl;
2640 tree tmpl_parms;
2641 int i;
2643 if (!TYPE_TEMPLATE_INFO (type))
2645 cp_error ("`%T' is not a template type", type);
2646 return;
2649 tmpl = TYPE_TI_TEMPLATE (type);
2650 if (!PRIMARY_TEMPLATE_P (tmpl))
2651 /* The type is nested in some template class. Nothing to worry
2652 about here; there are no new template parameters for the nested
2653 type. */
2654 return;
2656 parms = INNERMOST_TEMPLATE_PARMS (parms);
2657 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2659 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2661 cp_error_at ("previous declaration `%D'", tmpl);
2662 cp_error ("used %d template parameter%s instead of %d",
2663 TREE_VEC_LENGTH (tmpl_parms),
2664 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2665 TREE_VEC_LENGTH (parms));
2666 return;
2669 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2671 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2672 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2673 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2674 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2676 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2678 cp_error_at ("template parameter `%#D'", tmpl_parm);
2679 cp_error ("redeclared here as `%#D'", parm);
2680 return;
2683 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2685 /* We have in [temp.param]:
2687 A template-parameter may not be given default arguments
2688 by two different declarations in the same scope. */
2689 cp_error ("redefinition of default argument for `%#D'", parm);
2690 cp_error_at (" original definition appeared here", tmpl_parm);
2691 return;
2694 if (parm_default != NULL_TREE)
2695 /* Update the previous template parameters (which are the ones
2696 that will really count) with the new default value. */
2697 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2698 else if (tmpl_default != NULL_TREE)
2699 /* Update the new parameters, too; they'll be used as the
2700 parameters for any members. */
2701 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2705 /* Attempt to convert the non-type template parameter EXPR to the
2706 indicated TYPE. If the conversion is successful, return the
2707 converted value. If the conversion is unsuccesful, return
2708 NULL_TREE if we issued an error message, or error_mark_node if we
2709 did not. We issue error messages for out-and-out bad template
2710 parameters, but not simply because the conversion failed, since we
2711 might be just trying to do argument deduction. By the time this
2712 function is called, neither TYPE nor EXPR may make use of template
2713 parameters. */
2715 static tree
2716 convert_nontype_argument (type, expr)
2717 tree type;
2718 tree expr;
2720 tree expr_type = TREE_TYPE (expr);
2722 /* A template-argument for a non-type, non-template
2723 template-parameter shall be one of:
2725 --an integral constant-expression of integral or enumeration
2726 type; or
2728 --the name of a non-type template-parameter; or
2730 --the name of an object or function with external linkage,
2731 including function templates and function template-ids but
2732 excluding non-static class members, expressed as id-expression;
2735 --the address of an object or function with external linkage,
2736 including function templates and function template-ids but
2737 excluding non-static class members, expressed as & id-expression
2738 where the & is optional if the name refers to a function or
2739 array; or
2741 --a pointer to member expressed as described in _expr.unary.op_. */
2743 /* An integral constant-expression can include const variables or
2744 enumerators. Simplify things by folding them to their values,
2745 unless we're about to bind the declaration to a reference
2746 parameter. */
2747 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)
2748 && TREE_CODE (type) != REFERENCE_TYPE)
2749 expr = decl_constant_value (expr);
2751 if (is_overloaded_fn (expr))
2752 /* OK for now. We'll check that it has external linkage later.
2753 Check this first since if expr_type is the unknown_type_node
2754 we would otherwise complain below. */
2756 else if (TYPE_PTRMEM_P (expr_type)
2757 || TYPE_PTRMEMFUNC_P (expr_type))
2759 if (TREE_CODE (expr) != PTRMEM_CST)
2760 goto bad_argument;
2762 else if (TYPE_PTR_P (expr_type)
2763 || TYPE_PTRMEM_P (expr_type)
2764 || TREE_CODE (expr_type) == ARRAY_TYPE
2765 || TREE_CODE (type) == REFERENCE_TYPE
2766 /* If expr is the address of an overloaded function, we
2767 will get the unknown_type_node at this point. */
2768 || expr_type == unknown_type_node)
2770 tree referent;
2771 tree e = expr;
2772 STRIP_NOPS (e);
2774 if (TREE_CODE (type) == REFERENCE_TYPE
2775 || TREE_CODE (expr_type) == ARRAY_TYPE)
2776 referent = e;
2777 else
2779 if (TREE_CODE (e) != ADDR_EXPR)
2781 bad_argument:
2782 cp_error ("`%E' is not a valid template argument", expr);
2783 if (TYPE_PTR_P (expr_type))
2785 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2786 cp_error ("it must be the address of a function with external linkage");
2787 else
2788 cp_error ("it must be the address of an object with external linkage");
2790 else if (TYPE_PTRMEM_P (expr_type)
2791 || TYPE_PTRMEMFUNC_P (expr_type))
2792 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2794 return NULL_TREE;
2797 referent = TREE_OPERAND (e, 0);
2798 STRIP_NOPS (referent);
2801 if (TREE_CODE (referent) == STRING_CST)
2803 cp_error ("string literal %E is not a valid template argument",
2804 referent);
2805 error ("because it is the address of an object with static linkage");
2806 return NULL_TREE;
2809 if (is_overloaded_fn (referent))
2810 /* We'll check that it has external linkage later. */
2812 else if (TREE_CODE (referent) != VAR_DECL)
2813 goto bad_argument;
2814 else if (!TREE_PUBLIC (referent))
2816 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
2817 return error_mark_node;
2820 else if (INTEGRAL_TYPE_P (expr_type)
2821 || TYPE_PTRMEM_P (expr_type)
2822 || TYPE_PTRMEMFUNC_P (expr_type)
2823 /* The next two are g++ extensions. */
2824 || TREE_CODE (expr_type) == REAL_TYPE
2825 || TREE_CODE (expr_type) == COMPLEX_TYPE)
2827 if (! TREE_CONSTANT (expr))
2829 non_constant:
2830 cp_error ("non-constant `%E' cannot be used as template argument",
2831 expr);
2832 return NULL_TREE;
2835 else
2837 cp_error ("object `%E' cannot be used as template argument", expr);
2838 return NULL_TREE;
2841 switch (TREE_CODE (type))
2843 case INTEGER_TYPE:
2844 case BOOLEAN_TYPE:
2845 case ENUMERAL_TYPE:
2846 /* For a non-type template-parameter of integral or enumeration
2847 type, integral promotions (_conv.prom_) and integral
2848 conversions (_conv.integral_) are applied. */
2849 if (!INTEGRAL_TYPE_P (expr_type))
2850 return error_mark_node;
2852 /* It's safe to call digest_init in this case; we know we're
2853 just converting one integral constant expression to another. */
2854 expr = digest_init (type, expr, (tree*) 0);
2856 if (TREE_CODE (expr) != INTEGER_CST)
2857 /* Curiously, some TREE_CONSTANT integral expressions do not
2858 simplify to integer constants. For example, `3 % 0',
2859 remains a TRUNC_MOD_EXPR. */
2860 goto non_constant;
2862 return expr;
2864 case REAL_TYPE:
2865 case COMPLEX_TYPE:
2866 /* These are g++ extensions. */
2867 if (TREE_CODE (expr_type) != TREE_CODE (type))
2868 return error_mark_node;
2870 expr = digest_init (type, expr, (tree*) 0);
2872 if (TREE_CODE (expr) != REAL_CST)
2873 goto non_constant;
2875 return expr;
2877 case POINTER_TYPE:
2879 tree type_pointed_to = TREE_TYPE (type);
2881 if (TYPE_PTRMEM_P (type))
2883 tree e;
2885 /* For a non-type template-parameter of type pointer to data
2886 member, qualification conversions (_conv.qual_) are
2887 applied. */
2888 e = perform_qualification_conversions (type, expr);
2889 if (TREE_CODE (e) == NOP_EXPR)
2890 /* The call to perform_qualification_conversions will
2891 insert a NOP_EXPR over EXPR to do express conversion,
2892 if necessary. But, that will confuse us if we use
2893 this (converted) template parameter to instantiate
2894 another template; then the thing will not look like a
2895 valid template argument. So, just make a new
2896 constant, of the appropriate type. */
2897 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
2898 return e;
2900 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2902 /* For a non-type template-parameter of type pointer to
2903 function, only the function-to-pointer conversion
2904 (_conv.func_) is applied. If the template-argument
2905 represents a set of overloaded functions (or a pointer to
2906 such), the matching function is selected from the set
2907 (_over.over_). */
2908 tree fns;
2909 tree fn;
2911 if (TREE_CODE (expr) == ADDR_EXPR)
2912 fns = TREE_OPERAND (expr, 0);
2913 else
2914 fns = expr;
2916 fn = instantiate_type (type_pointed_to, fns, 0);
2918 if (fn == error_mark_node)
2919 return error_mark_node;
2921 if (!TREE_PUBLIC (fn))
2923 if (really_overloaded_fn (fns))
2924 return error_mark_node;
2925 else
2926 goto bad_argument;
2929 expr = build_unary_op (ADDR_EXPR, fn, 0);
2931 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
2933 return expr;
2935 else
2937 /* For a non-type template-parameter of type pointer to
2938 object, qualification conversions (_conv.qual_) and the
2939 array-to-pointer conversion (_conv.array_) are applied.
2940 [Note: In particular, neither the null pointer conversion
2941 (_conv.ptr_) nor the derived-to-base conversion
2942 (_conv.ptr_) are applied. Although 0 is a valid
2943 template-argument for a non-type template-parameter of
2944 integral type, it is not a valid template-argument for a
2945 non-type template-parameter of pointer type.]
2947 The call to decay_conversion performs the
2948 array-to-pointer conversion, if appropriate. */
2949 expr = decay_conversion (expr);
2951 if (expr == error_mark_node)
2952 return error_mark_node;
2953 else
2954 return perform_qualification_conversions (type, expr);
2957 break;
2959 case REFERENCE_TYPE:
2961 tree type_referred_to = TREE_TYPE (type);
2963 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2965 /* For a non-type template-parameter of type reference to
2966 function, no conversions apply. If the
2967 template-argument represents a set of overloaded
2968 functions, the matching function is selected from the
2969 set (_over.over_). */
2970 tree fns = expr;
2971 tree fn;
2973 fn = instantiate_type (type_referred_to, fns, 0);
2975 if (fn == error_mark_node)
2976 return error_mark_node;
2978 if (!TREE_PUBLIC (fn))
2980 if (really_overloaded_fn (fns))
2981 /* Don't issue an error here; we might get a different
2982 function if the overloading had worked out
2983 differently. */
2984 return error_mark_node;
2985 else
2986 goto bad_argument;
2989 my_friendly_assert (same_type_p (type_referred_to,
2990 TREE_TYPE (fn)),
2993 return fn;
2995 else
2997 /* For a non-type template-parameter of type reference to
2998 object, no conversions apply. The type referred to by the
2999 reference may be more cv-qualified than the (otherwise
3000 identical) type of the template-argument. The
3001 template-parameter is bound directly to the
3002 template-argument, which must be an lvalue. */
3003 if ((TYPE_MAIN_VARIANT (expr_type)
3004 != TYPE_MAIN_VARIANT (type_referred_to))
3005 || !at_least_as_qualified_p (type_referred_to,
3006 expr_type)
3007 || !real_lvalue_p (expr))
3008 return error_mark_node;
3009 else
3010 return expr;
3013 break;
3015 case RECORD_TYPE:
3017 if (!TYPE_PTRMEMFUNC_P (type))
3018 /* This handles templates like
3019 template<class T, T t> void f();
3020 when T is substituted with any class. The second template
3021 parameter becomes invalid and the template candidate is
3022 rejected. */
3023 return error_mark_node;
3025 /* For a non-type template-parameter of type pointer to member
3026 function, no conversions apply. If the template-argument
3027 represents a set of overloaded member functions, the
3028 matching member function is selected from the set
3029 (_over.over_). */
3031 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3032 expr_type != unknown_type_node)
3033 return error_mark_node;
3035 if (TREE_CODE (expr) == PTRMEM_CST)
3037 /* A ptr-to-member constant. */
3038 if (!same_type_p (type, expr_type))
3039 return error_mark_node;
3040 else
3041 return expr;
3044 if (TREE_CODE (expr) != ADDR_EXPR)
3045 return error_mark_node;
3047 expr = instantiate_type (type, expr, 0);
3049 if (expr == error_mark_node)
3050 return error_mark_node;
3052 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3054 return expr;
3056 break;
3058 default:
3059 /* All non-type parameters must have one of these types. */
3060 my_friendly_abort (0);
3061 break;
3064 return error_mark_node;
3067 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3068 template template parameters. Both PARM_PARMS and ARG_PARMS are
3069 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3070 or PARM_DECL.
3072 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3073 the case, then extra parameters must have default arguments.
3075 Consider the example:
3076 template <class T, class Allocator = allocator> class vector;
3077 template<template <class U> class TT> class C;
3079 C<vector> is a valid instantiation. PARM_PARMS for the above code
3080 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3081 T and Allocator) and OUTER_ARGS contains the argument that is used to
3082 substitute the TT parameter. */
3084 static int
3085 coerce_template_template_parms (parm_parms, arg_parms, complain,
3086 in_decl, outer_args)
3087 tree parm_parms, arg_parms;
3088 int complain;
3089 tree in_decl, outer_args;
3091 int nparms, nargs, i;
3092 tree parm, arg;
3094 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3095 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3097 nparms = TREE_VEC_LENGTH (parm_parms);
3098 nargs = TREE_VEC_LENGTH (arg_parms);
3100 /* The rule here is opposite of coerce_template_parms. */
3101 if (nargs < nparms
3102 || (nargs > nparms
3103 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3104 return 0;
3106 for (i = 0; i < nparms; ++i)
3108 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3109 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3111 if (arg == NULL_TREE || arg == error_mark_node
3112 || parm == NULL_TREE || parm == error_mark_node)
3113 return 0;
3115 if (TREE_CODE (arg) != TREE_CODE (parm))
3116 return 0;
3118 switch (TREE_CODE (parm))
3120 case TYPE_DECL:
3121 break;
3123 case TEMPLATE_DECL:
3124 /* We encounter instantiations of templates like
3125 template <template <template <class> class> class TT>
3126 class C; */
3128 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3129 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3131 if (!coerce_template_template_parms (parmparm, argparm,
3132 complain, in_decl,
3133 outer_args))
3134 return 0;
3136 break;
3138 case PARM_DECL:
3139 /* The tsubst call is used to handle cases such as
3140 template <class T, template <T> class TT> class D;
3141 i.e. the parameter list of TT depends on earlier parameters. */
3142 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3143 complain, in_decl),
3144 TREE_TYPE (arg)))
3145 return 0;
3146 break;
3148 default:
3149 my_friendly_abort (0);
3152 return 1;
3155 /* Convert the indicated template ARG as necessary to match the
3156 indicated template PARM. Returns the converted ARG, or
3157 error_mark_node if the conversion was unsuccessful. Error messages
3158 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3159 parameter in the parameter list. ARGS is the full set of template
3160 arguments deduced so far. */
3162 static tree
3163 convert_template_argument (parm, arg, args, complain, i, in_decl)
3164 tree parm;
3165 tree arg;
3166 tree args;
3167 int complain;
3168 int i;
3169 tree in_decl;
3171 tree val;
3172 tree inner_args;
3173 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3175 inner_args = innermost_args (args);
3177 if (TREE_CODE (arg) == TREE_LIST
3178 && TREE_TYPE (arg) != NULL_TREE
3179 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3181 /* The template argument was the name of some
3182 member function. That's usually
3183 illegal, but static members are OK. In any
3184 case, grab the underlying fields/functions
3185 and issue an error later if required. */
3186 arg = TREE_VALUE (arg);
3187 TREE_TYPE (arg) = unknown_type_node;
3190 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3191 requires_type = (TREE_CODE (parm) == TYPE_DECL
3192 || requires_tmpl_type);
3194 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3195 we also accept implicitly created TYPE_DECL as a valid argument.
3196 This is necessary to handle the case where we pass a template name
3197 to a template template parameter in a scope where we've derived from
3198 in instantiation of that template, so the template name refers to that
3199 instantiation. We really ought to handle this better. */
3200 is_tmpl_type
3201 = ((TREE_CODE (arg) == TEMPLATE_DECL
3202 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3203 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3204 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg))
3205 || (TREE_CODE (arg) == RECORD_TYPE
3206 && CLASSTYPE_TEMPLATE_INFO (arg)
3207 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
3208 && DECL_ARTIFICIAL (TYPE_NAME (arg))
3209 && requires_tmpl_type
3210 && is_base_of_enclosing_class (arg, current_class_type)));
3211 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
3212 arg = TYPE_STUB_DECL (arg);
3213 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3214 arg = CLASSTYPE_TI_TEMPLATE (arg);
3216 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
3218 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3219 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3221 cp_pedwarn ("to refer to a type member of a template parameter,");
3222 cp_pedwarn (" use `typename %E'", arg);
3224 arg = make_typename_type (TREE_OPERAND (arg, 0),
3225 TREE_OPERAND (arg, 1));
3226 is_type = 1;
3228 if (is_type != requires_type)
3230 if (in_decl)
3232 if (complain)
3234 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3235 i + 1, in_decl);
3236 if (is_type)
3237 cp_error (" expected a constant of type `%T', got `%T'",
3238 TREE_TYPE (parm),
3239 (is_tmpl_type ? DECL_NAME (arg) : arg));
3240 else
3241 cp_error (" expected a type, got `%E'", arg);
3244 return error_mark_node;
3246 if (is_tmpl_type ^ requires_tmpl_type)
3248 if (in_decl && complain)
3250 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3251 i + 1, in_decl);
3252 if (is_tmpl_type)
3253 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
3254 else
3255 cp_error (" expected a class template, got `%T'", arg);
3257 return error_mark_node;
3260 if (is_type)
3262 if (requires_tmpl_type)
3264 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3265 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3267 if (coerce_template_template_parms (parmparm, argparm, complain,
3268 in_decl, inner_args))
3270 val = arg;
3272 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3273 TEMPLATE_DECL. */
3274 if (val != error_mark_node
3275 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3276 val = TREE_TYPE (val);
3278 else
3280 if (in_decl && complain)
3282 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3283 i + 1, in_decl);
3284 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
3287 val = error_mark_node;
3290 else
3292 val = groktypename (arg);
3293 if (! processing_template_decl)
3295 /* [basic.link]: A name with no linkage (notably, the
3296 name of a class or enumeration declared in a local
3297 scope) shall not be used to declare an entity with
3298 linkage. This implies that names with no linkage
3299 cannot be used as template arguments. */
3300 tree t = no_linkage_check (val);
3301 if (t)
3303 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3304 cp_pedwarn
3305 ("template-argument `%T' uses anonymous type", val);
3306 else
3307 cp_error
3308 ("template-argument `%T' uses local type `%T'",
3309 val, t);
3310 return error_mark_node;
3315 else
3317 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3319 if (processing_template_decl)
3320 arg = maybe_fold_nontype_arg (arg);
3322 if (!uses_template_parms (arg) && !uses_template_parms (t))
3323 /* We used to call digest_init here. However, digest_init
3324 will report errors, which we don't want when complain
3325 is zero. More importantly, digest_init will try too
3326 hard to convert things: for example, `0' should not be
3327 converted to pointer type at this point according to
3328 the standard. Accepting this is not merely an
3329 extension, since deciding whether or not these
3330 conversions can occur is part of determining which
3331 function template to call, or whether a given epxlicit
3332 argument specification is legal. */
3333 val = convert_nontype_argument (t, arg);
3334 else
3335 val = arg;
3337 if (val == NULL_TREE)
3338 val = error_mark_node;
3339 else if (val == error_mark_node && complain)
3340 cp_error ("could not convert template argument `%E' to `%T'",
3341 arg, t);
3344 return val;
3347 /* Convert all template arguments to their appropriate types, and
3348 return a vector containing the innermost resulting template
3349 arguments. If any error occurs, return error_mark_node, and, if
3350 COMPLAIN is non-zero, issue an error message. Some error messages
3351 are issued even if COMPLAIN is zero; for instance, if a template
3352 argument is composed from a local class.
3354 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3355 provided in ARGLIST, or else trailing parameters must have default
3356 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3357 deduction for any unspecified trailing arguments.
3359 The resulting TREE_VEC is allocated on a temporary obstack, and
3360 must be explicitly copied if it will be permanent. */
3362 static tree
3363 coerce_template_parms (parms, args, in_decl,
3364 complain,
3365 require_all_arguments)
3366 tree parms, args;
3367 tree in_decl;
3368 int complain;
3369 int require_all_arguments;
3371 int nparms, nargs, i, lost = 0;
3372 tree inner_args;
3373 tree new_args;
3374 tree new_inner_args;
3376 inner_args = innermost_args (args);
3377 nargs = NUM_TMPL_ARGS (inner_args);
3378 nparms = TREE_VEC_LENGTH (parms);
3380 if (nargs > nparms
3381 || (nargs < nparms
3382 && require_all_arguments
3383 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3385 if (complain)
3387 cp_error ("wrong number of template arguments (%d, should be %d)",
3388 nargs, nparms);
3390 if (in_decl)
3391 cp_error_at ("provided for `%D'", in_decl);
3394 return error_mark_node;
3397 new_inner_args = make_temp_vec (nparms);
3398 new_args = add_outermost_template_args (args, new_inner_args);
3399 for (i = 0; i < nparms; i++)
3401 tree arg;
3402 tree parm;
3404 /* Get the Ith template parameter. */
3405 parm = TREE_VEC_ELT (parms, i);
3407 /* Calculate the Ith argument. */
3408 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3410 arg = TREE_VALUE (inner_args);
3411 inner_args = TREE_CHAIN (inner_args);
3413 else if (i < nargs)
3414 arg = TREE_VEC_ELT (inner_args, i);
3415 /* If no template argument was supplied, look for a default
3416 value. */
3417 else if (TREE_PURPOSE (parm) == NULL_TREE)
3419 /* There was no default value. */
3420 my_friendly_assert (!require_all_arguments, 0);
3421 break;
3423 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3424 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3425 else
3426 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3427 in_decl);
3429 /* Now, convert the Ith argument, as necessary. */
3430 if (arg == NULL_TREE)
3431 /* We're out of arguments. */
3433 my_friendly_assert (!require_all_arguments, 0);
3434 break;
3436 else if (arg == error_mark_node)
3438 cp_error ("template argument %d is invalid", i + 1);
3439 arg = error_mark_node;
3441 else
3442 arg = convert_template_argument (TREE_VALUE (parm),
3443 arg, new_args, complain, i,
3444 in_decl);
3446 if (arg == error_mark_node)
3447 lost++;
3448 TREE_VEC_ELT (new_inner_args, i) = arg;
3451 if (lost)
3452 return error_mark_node;
3454 return new_inner_args;
3457 /* Returns 1 if template args OT and NT are equivalent. */
3459 static int
3460 template_args_equal (ot, nt)
3461 tree ot, nt;
3463 if (nt == ot)
3464 return 1;
3465 if (TREE_CODE (nt) != TREE_CODE (ot))
3466 return 0;
3467 if (TREE_CODE (nt) == TREE_VEC)
3468 /* For member templates */
3469 return comp_template_args (ot, nt);
3470 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
3471 return same_type_p (ot, nt);
3472 else
3473 return (cp_tree_equal (ot, nt) > 0);
3476 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3477 of template arguments. Returns 0 otherwise. */
3480 comp_template_args (oldargs, newargs)
3481 tree oldargs, newargs;
3483 int i;
3485 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3486 return 0;
3488 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3490 tree nt = TREE_VEC_ELT (newargs, i);
3491 tree ot = TREE_VEC_ELT (oldargs, i);
3493 if (! template_args_equal (ot, nt))
3494 return 0;
3496 return 1;
3499 /* Given class template name and parameter list, produce a user-friendly name
3500 for the instantiation. */
3502 static char *
3503 mangle_class_name_for_template (name, parms, arglist)
3504 char *name;
3505 tree parms, arglist;
3507 static struct obstack scratch_obstack;
3508 static char *scratch_firstobj;
3509 int i, nparms;
3511 if (!scratch_firstobj)
3512 gcc_obstack_init (&scratch_obstack);
3513 else
3514 obstack_free (&scratch_obstack, scratch_firstobj);
3515 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3517 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3518 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3520 cat (name);
3521 ccat ('<');
3522 nparms = TREE_VEC_LENGTH (parms);
3523 arglist = innermost_args (arglist);
3524 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3525 for (i = 0; i < nparms; i++)
3527 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3528 tree arg = TREE_VEC_ELT (arglist, i);
3530 if (i)
3531 ccat (',');
3533 if (TREE_CODE (parm) == TYPE_DECL)
3535 cat (type_as_string_real (arg, 0, 1));
3536 continue;
3538 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3540 if (TREE_CODE (arg) == TEMPLATE_DECL)
3542 /* Already substituted with real template. Just output
3543 the template name here */
3544 tree context = DECL_CONTEXT (arg);
3545 if (context)
3547 /* The template may be defined in a namespace, or
3548 may be a member template. */
3549 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3550 || CLASS_TYPE_P (context),
3551 980422);
3552 cat(decl_as_string (DECL_CONTEXT (arg), 0));
3553 cat("::");
3555 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3557 else
3558 /* Output the parameter declaration */
3559 cat (type_as_string_real (arg, 0, 1));
3560 continue;
3562 else
3563 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3565 if (TREE_CODE (arg) == TREE_LIST)
3567 /* New list cell was built because old chain link was in
3568 use. */
3569 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3570 arg = TREE_VALUE (arg);
3572 /* No need to check arglist against parmlist here; we did that
3573 in coerce_template_parms, called from lookup_template_class. */
3574 cat (expr_as_string (arg, 0));
3577 char *bufp = obstack_next_free (&scratch_obstack);
3578 int offset = 0;
3579 while (bufp[offset - 1] == ' ')
3580 offset--;
3581 obstack_blank_fast (&scratch_obstack, offset);
3583 /* B<C<char> >, not B<C<char>> */
3584 if (bufp[offset - 1] == '>')
3585 ccat (' ');
3587 ccat ('>');
3588 ccat ('\0');
3589 return (char *) obstack_base (&scratch_obstack);
3592 static tree
3593 classtype_mangled_name (t)
3594 tree t;
3596 if (CLASSTYPE_TEMPLATE_INFO (t)
3597 /* Specializations have already had their names set up in
3598 lookup_template_class. */
3599 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3601 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3603 /* For non-primary templates, the template parameters are
3604 implicit from their surrounding context. */
3605 if (PRIMARY_TEMPLATE_P (tmpl))
3607 tree name = DECL_NAME (tmpl);
3608 char *mangled_name = mangle_class_name_for_template
3609 (IDENTIFIER_POINTER (name),
3610 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3611 CLASSTYPE_TI_ARGS (t));
3612 tree id = get_identifier (mangled_name);
3613 IDENTIFIER_TEMPLATE (id) = name;
3614 return id;
3618 return TYPE_IDENTIFIER (t);
3621 static void
3622 add_pending_template (d)
3623 tree d;
3625 tree ti;
3627 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3628 ti = CLASSTYPE_TEMPLATE_INFO (d);
3629 else
3630 ti = DECL_TEMPLATE_INFO (d);
3632 if (TI_PENDING_TEMPLATE_FLAG (ti))
3633 return;
3635 *template_tail = tree_cons (build_srcloc_here (), d, NULL_TREE);
3636 template_tail = &TREE_CHAIN (*template_tail);
3637 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3641 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3642 may be either a _DECL or an overloaded function or an
3643 IDENTIFIER_NODE), and ARGLIST. */
3645 tree
3646 lookup_template_function (fns, arglist)
3647 tree fns, arglist;
3649 tree type;
3651 if (fns == NULL_TREE)
3653 cp_error ("non-template used as template");
3654 return error_mark_node;
3657 type = TREE_TYPE (fns);
3658 if (TREE_CODE (fns) == OVERLOAD || !type)
3659 type = unknown_type_node;
3661 if (processing_template_decl)
3662 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3663 else
3664 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3667 /* Within the scope of a template class S<T>, the name S gets bound
3668 (in build_self_reference) to a TYPE_DECL for the class, not a
3669 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3670 or one of its enclosing classes, and that type is a template,
3671 return the associated TEMPLATE_DECL. Otherwise, the original
3672 DECL is returned. */
3674 static tree
3675 maybe_get_template_decl_from_type_decl (decl)
3676 tree decl;
3678 return (decl != NULL_TREE
3679 && TREE_CODE (decl) == TYPE_DECL
3680 && DECL_ARTIFICIAL (decl)
3681 && CLASS_TYPE_P (TREE_TYPE (decl))
3682 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3683 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3686 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3687 parameters, find the desired type.
3689 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3690 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3691 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3692 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3693 copy it here to keep it from being reclaimed when the decl storage
3694 is reclaimed.
3696 IN_DECL, if non-NULL, is the template declaration we are trying to
3697 instantiate.
3699 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3700 the class we are looking up.
3702 If the template class is really a local class in a template
3703 function, then the FUNCTION_CONTEXT is the function in which it is
3704 being instantiated. */
3706 tree
3707 lookup_template_class (d1, arglist, in_decl, context, entering_scope)
3708 tree d1, arglist;
3709 tree in_decl;
3710 tree context;
3711 int entering_scope;
3713 tree template = NULL_TREE, parmlist;
3714 tree t;
3716 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3718 if (IDENTIFIER_VALUE (d1)
3719 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3720 template = IDENTIFIER_VALUE (d1);
3721 else
3723 if (context)
3724 push_decl_namespace (context);
3725 template = lookup_name (d1, /*prefer_type=*/0);
3726 template = maybe_get_template_decl_from_type_decl (template);
3727 if (context)
3728 pop_decl_namespace ();
3730 if (template)
3731 context = DECL_CONTEXT (template);
3733 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3735 tree type = TREE_TYPE (d1);
3737 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3738 an implicit typename for the second A. Deal with it. */
3739 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3740 type = TREE_TYPE (type);
3742 if (CLASSTYPE_TEMPLATE_INFO (type))
3744 template = CLASSTYPE_TI_TEMPLATE (type);
3745 d1 = DECL_NAME (template);
3748 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3749 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3750 && IS_AGGR_TYPE (d1)))
3752 template = TYPE_TI_TEMPLATE (d1);
3753 d1 = DECL_NAME (template);
3755 else if (TREE_CODE (d1) == TEMPLATE_DECL
3756 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3758 template = d1;
3759 d1 = DECL_NAME (template);
3760 context = DECL_CONTEXT (template);
3762 else
3763 my_friendly_abort (272);
3765 /* With something like `template <class T> class X class X { ... };'
3766 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3767 We don't want to do that, but we have to deal with the situation,
3768 so let's give them some syntax errors to chew on instead of a
3769 crash. */
3770 if (! template)
3772 cp_error ("`%T' is not a template", d1);
3773 return error_mark_node;
3776 if (context == NULL_TREE)
3777 context = global_namespace;
3779 if (TREE_CODE (template) != TEMPLATE_DECL)
3781 cp_error ("non-template type `%T' used as a template", d1);
3782 if (in_decl)
3783 cp_error_at ("for template declaration `%D'", in_decl);
3784 return error_mark_node;
3787 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3789 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3790 template arguments */
3792 tree parm = copy_template_template_parm (TREE_TYPE (template));
3793 tree template2 = TYPE_STUB_DECL (parm);
3794 tree arglist2;
3796 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3798 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
3799 if (arglist2 == error_mark_node)
3800 return error_mark_node;
3802 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
3803 = tree_cons (template2, arglist2, NULL_TREE);
3804 TYPE_SIZE (parm) = 0;
3805 return parm;
3807 else
3809 tree template_type = TREE_TYPE (template);
3810 tree gen_tmpl;
3811 tree type_decl;
3812 tree found = NULL_TREE;
3813 int arg_depth;
3814 int parm_depth;
3815 int is_partial_instantiation;
3817 gen_tmpl = most_general_template (template);
3818 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
3819 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3820 arg_depth = TMPL_ARGS_DEPTH (arglist);
3822 /* We build up the coerced arguments and such on the
3823 momentary_obstack. */
3824 push_momentary ();
3826 if (arg_depth == 1 && parm_depth > 1)
3828 /* We've been given an incomplete set of template arguments.
3829 For example, given:
3831 template <class T> struct S1 {
3832 template <class U> struct S2 {};
3833 template <class U> struct S2<U*> {};
3836 we will be called with an ARGLIST of `U*', but the
3837 TEMPLATE will be `template <class T> template
3838 <class U> struct S1<T>::S2'. We must fill in the missing
3839 arguments. */
3840 arglist
3841 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3842 arglist);
3843 arg_depth = TMPL_ARGS_DEPTH (arglist);
3846 /* Now we should enough arguments. */
3847 my_friendly_assert (parm_depth == arg_depth, 0);
3849 /* From here on, we're only interested in the most general
3850 template. */
3851 template = gen_tmpl;
3853 /* Calculate the BOUND_ARGS. These will be the args that are
3854 actually tsubst'd into the definition to create the
3855 instantiation. */
3856 if (parm_depth > 1)
3858 /* We have multiple levels of arguments to coerce, at once. */
3859 int i;
3860 int saved_depth = TMPL_ARGS_DEPTH (arglist);
3862 tree bound_args = make_temp_vec (parm_depth);
3864 for (i = saved_depth,
3865 t = DECL_TEMPLATE_PARMS (template);
3866 i > 0 && t != NULL_TREE;
3867 --i, t = TREE_CHAIN (t))
3869 tree a = coerce_template_parms (TREE_VALUE (t),
3870 arglist, template, 1, 1);
3871 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3873 /* We temporarily reduce the length of the ARGLIST so
3874 that coerce_template_parms will see only the arguments
3875 corresponding to the template parameters it is
3876 examining. */
3877 TREE_VEC_LENGTH (arglist)--;
3880 /* Restore the ARGLIST to its full size. */
3881 TREE_VEC_LENGTH (arglist) = saved_depth;
3883 arglist = bound_args;
3885 else
3886 arglist
3887 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3888 innermost_args (arglist),
3889 template, 1, 1);
3891 if (arglist == error_mark_node)
3892 /* We were unable to bind the arguments. */
3893 return error_mark_node;
3895 /* In the scope of a template class, explicit references to the
3896 template class refer to the type of the template, not any
3897 instantiation of it. For example, in:
3899 template <class T> class C { void f(C<T>); }
3901 the `C<T>' is just the same as `C'. Outside of the
3902 class, however, such a reference is an instantiation. */
3903 if (comp_template_args (TYPE_TI_ARGS (template_type),
3904 arglist))
3906 found = template_type;
3908 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
3910 tree ctx;
3912 /* Note that we use DECL_CONTEXT, rather than
3913 CP_DECL_CONTEXT, so that the termination test is
3914 always just `ctx'. We're not interested in namepace
3915 scopes. */
3916 for (ctx = current_class_type;
3917 ctx;
3918 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3919 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3920 if (same_type_p (ctx, template_type))
3921 break;
3923 if (!ctx)
3924 /* We're not in the scope of the class, so the
3925 TEMPLATE_TYPE is not the type we want after
3926 all. */
3927 found = NULL_TREE;
3931 if (!found)
3933 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3934 found; found = TREE_CHAIN (found))
3935 if (comp_template_args (TREE_PURPOSE (found), arglist))
3936 break;
3938 if (found)
3939 found = TREE_VALUE (found);
3942 if (found)
3944 pop_momentary ();
3945 return found;
3948 /* This type is a "partial instantiation" if any of the template
3949 arguments still inolve template parameters. Note that we set
3950 IS_PARTIAL_INSTANTIATION for partial specializations as
3951 well. */
3952 is_partial_instantiation = uses_template_parms (arglist);
3954 if (!is_partial_instantiation
3955 && !PRIMARY_TEMPLATE_P (template)
3956 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
3958 pop_momentary ();
3959 found = xref_tag_from_type (TREE_TYPE (template),
3960 DECL_NAME (template),
3961 /*globalize=*/1);
3962 return found;
3965 /* Since we didn't find the type, we'll have to create it.
3966 Since we'll be saving this type on the
3967 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3968 push_obstacks (&permanent_obstack, &permanent_obstack);
3970 /* Create the type. */
3971 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3973 if (!is_partial_instantiation)
3974 t = start_enum (TYPE_IDENTIFIER (template_type));
3975 else
3976 /* We don't want to call start_enum for this type, since
3977 the values for the enumeration constants may involve
3978 template parameters. And, no one should be interested
3979 in the enumeration constants for such a type. */
3980 t = make_node (ENUMERAL_TYPE);
3982 else
3984 t = make_lang_type (TREE_CODE (template_type));
3985 CLASSTYPE_DECLARED_CLASS (t)
3986 = CLASSTYPE_DECLARED_CLASS (template_type);
3987 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3988 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
3989 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
3992 /* If we called start_enum above, this information will already
3993 be set up. */
3994 if (!TYPE_NAME (t))
3996 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3998 type_decl = create_implicit_typedef (DECL_NAME (template), t);
3999 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4000 TYPE_STUB_DECL (t) = type_decl;
4001 DECL_SOURCE_FILE (type_decl)
4002 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4003 DECL_SOURCE_LINE (type_decl)
4004 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
4006 else
4007 type_decl = TYPE_NAME (t);
4009 /* Set up the template information. We have to figure out which
4010 template is the immediate parent if this is a full
4011 instantiation. */
4012 if (parm_depth == 1 || is_partial_instantiation
4013 || !PRIMARY_TEMPLATE_P (template))
4014 /* This case is easy; there are no member templates involved. */
4015 found = template;
4016 else
4018 /* This is a full instantiation of a member template. There
4019 should be some partial instantiation of which this is an
4020 instance. */
4022 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4023 found; found = TREE_CHAIN (found))
4025 int success;
4026 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4028 /* We only want partial instantiations, here, not
4029 specializations or full instantiations. */
4030 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4031 || !uses_template_parms (TREE_VALUE (found)))
4032 continue;
4034 /* Temporarily reduce by one the number of levels in the
4035 ARGLIST and in FOUND so as to avoid comparing the
4036 last set of arguments. */
4037 TREE_VEC_LENGTH (arglist)--;
4038 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4040 /* See if the arguments match. If they do, then TMPL is
4041 the partial instantiation we want. */
4042 success = comp_template_args (TREE_PURPOSE (found), arglist);
4044 /* Restore the argument vectors to their full size. */
4045 TREE_VEC_LENGTH (arglist)++;
4046 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4048 if (success)
4050 found = tmpl;
4051 break;
4055 if (!found)
4056 my_friendly_abort (0);
4059 SET_TYPE_TEMPLATE_INFO (t,
4060 tree_cons (found, arglist, NULL_TREE));
4061 DECL_TEMPLATE_INSTANTIATIONS (template)
4062 = tree_cons (arglist, t,
4063 DECL_TEMPLATE_INSTANTIATIONS (template));
4065 if (TREE_CODE (t) == ENUMERAL_TYPE
4066 && !is_partial_instantiation)
4067 /* Now that the type has been registered on the instantiations
4068 list, we set up the enumerators. Because the enumeration
4069 constants may involve the enumeration type itself, we make
4070 sure to register the type first, and then create the
4071 constants. That way, doing tsubst_expr for the enumeration
4072 constants won't result in recursive calls here; we'll find
4073 the instantiation and exit above. */
4074 tsubst_enum (template_type, t, arglist);
4076 /* We're done with the permanent obstack, now. */
4077 pop_obstacks ();
4078 /* We're also done with the momentary allocation we started
4079 above. */
4080 pop_momentary ();
4082 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4083 is set up. */
4084 if (TREE_CODE (t) != ENUMERAL_TYPE)
4085 DECL_NAME (type_decl) = classtype_mangled_name (t);
4086 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
4087 if (!is_partial_instantiation)
4089 DECL_ASSEMBLER_NAME (type_decl)
4090 = get_identifier (build_overload_name (t, 1, 1));
4092 /* For backwards compatibility; code that uses
4093 -fexternal-templates expects looking up a template to
4094 instantiate it. I think DDD still relies on this.
4095 (jason 8/20/1998) */
4096 if (TREE_CODE (t) != ENUMERAL_TYPE
4097 && flag_external_templates
4098 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4099 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4100 add_pending_template (t);
4102 else
4103 /* If the type makes use of template parameters, the
4104 code that generates debugging information will crash. */
4105 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4107 return t;
4111 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
4112 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
4113 If FN returns non-zero, the iteration is terminated, and
4114 for_each_template_parm returns 1. Otherwise, the iteration
4115 continues. If FN never returns a non-zero value, the value
4116 returned by for_each_template_parm is 0. If FN is NULL, it is
4117 considered to be the function which always returns 1. */
4119 static int
4120 for_each_template_parm (t, fn, data)
4121 tree t;
4122 tree_fn_t fn;
4123 void* data;
4125 if (!t)
4126 return 0;
4128 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
4129 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4130 return 1;
4132 switch (TREE_CODE (t))
4134 case ARRAY_REF:
4135 case OFFSET_REF:
4136 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4137 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4139 case IDENTIFIER_NODE:
4140 if (!IDENTIFIER_TEMPLATE (t))
4141 return 0;
4142 my_friendly_abort (42);
4144 /* aggregates of tree nodes */
4145 case TREE_VEC:
4147 int i = TREE_VEC_LENGTH (t);
4148 while (i--)
4149 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
4150 return 1;
4151 return 0;
4153 case TREE_LIST:
4154 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
4155 || for_each_template_parm (TREE_VALUE (t), fn, data))
4156 return 1;
4157 return for_each_template_parm (TREE_CHAIN (t), fn, data);
4159 case OVERLOAD:
4160 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
4161 return 1;
4162 return for_each_template_parm (OVL_CHAIN (t), fn, data);
4164 /* constructed type nodes */
4165 case POINTER_TYPE:
4166 case REFERENCE_TYPE:
4167 return for_each_template_parm (TREE_TYPE (t), fn, data);
4169 case RECORD_TYPE:
4170 if (TYPE_PTRMEMFUNC_FLAG (t))
4171 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
4172 fn, data);
4173 /* Fall through. */
4175 case UNION_TYPE:
4176 case ENUMERAL_TYPE:
4177 if (! TYPE_TEMPLATE_INFO (t))
4178 return 0;
4179 return for_each_template_parm (TREE_VALUE
4180 (TYPE_TEMPLATE_INFO (t)),
4181 fn, data);
4182 case METHOD_TYPE:
4183 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
4184 return 1;
4185 /* Fall through. */
4187 case FUNCTION_TYPE:
4188 /* Check the parameter types. Since default arguments are not
4189 instantiated until they are needed, the TYPE_ARG_TYPES may
4190 contain expressions that involve template parameters. But,
4191 no-one should be looking at them yet. And, once they're
4192 instantiated, they don't contain template parameters, so
4193 there's no point in looking at them then, either. */
4195 tree parm;
4197 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4198 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4199 return 1;
4202 /* Check the return type, too. */
4203 return for_each_template_parm (TREE_TYPE (t), fn, data);
4205 case ARRAY_TYPE:
4206 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
4207 return 1;
4208 return for_each_template_parm (TREE_TYPE (t), fn, data);
4209 case OFFSET_TYPE:
4210 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
4211 return 1;
4212 return for_each_template_parm (TREE_TYPE (t), fn, data);
4214 /* decl nodes */
4215 case TYPE_DECL:
4216 return for_each_template_parm (TREE_TYPE (t), fn, data);
4218 case TEMPLATE_DECL:
4219 /* A template template parameter is encountered */
4220 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
4221 return for_each_template_parm (TREE_TYPE (t), fn, data);
4222 /* Already substituted template template parameter */
4223 return 0;
4225 case CONST_DECL:
4226 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
4227 return 1;
4228 goto check_type_and_context;
4230 case FUNCTION_DECL:
4231 case VAR_DECL:
4232 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4233 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
4234 return 1;
4235 /* fall through */
4236 case PARM_DECL:
4237 check_type_and_context:
4238 if (for_each_template_parm (TREE_TYPE (t), fn, data))
4239 return 1;
4240 if (DECL_CONTEXT (t)
4241 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
4242 return 1;
4243 return 0;
4245 case CALL_EXPR:
4246 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4247 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4249 case ADDR_EXPR:
4250 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4252 /* template parm nodes */
4253 case TEMPLATE_TEMPLATE_PARM:
4254 /* Record template parameters such as `T' inside `TT<T>'. */
4255 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
4256 && for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
4257 return 1;
4258 case TEMPLATE_TYPE_PARM:
4259 case TEMPLATE_PARM_INDEX:
4260 if (fn)
4261 return (*fn)(t, data);
4262 else
4263 return 1;
4265 /* simple type nodes */
4266 case INTEGER_TYPE:
4267 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
4268 return 1;
4269 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
4271 case REAL_TYPE:
4272 case COMPLEX_TYPE:
4273 case VOID_TYPE:
4274 case BOOLEAN_TYPE:
4275 case NAMESPACE_DECL:
4276 case FIELD_DECL:
4277 return 0;
4279 /* constants */
4280 case INTEGER_CST:
4281 case REAL_CST:
4282 case STRING_CST:
4283 return 0;
4285 case ERROR_MARK:
4286 /* Non-error_mark_node ERROR_MARKs are bad things. */
4287 my_friendly_assert (t == error_mark_node, 274);
4288 /* NOTREACHED */
4289 return 0;
4291 case PTRMEM_CST:
4292 return for_each_template_parm (TREE_TYPE (t), fn, data);
4294 case SCOPE_REF:
4295 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4297 case CONSTRUCTOR:
4298 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4299 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4300 (TREE_TYPE (t)), fn, data);
4301 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
4303 case SIZEOF_EXPR:
4304 case ALIGNOF_EXPR:
4305 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4307 case TYPENAME_TYPE:
4308 if (!fn)
4309 return 1;
4310 return (for_each_template_parm (TYPE_CONTEXT (t), fn, data)
4311 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t),
4312 fn, data));
4314 case INDIRECT_REF:
4315 case COMPONENT_REF:
4316 /* If there's no type, then this thing must be some expression
4317 involving template parameters. */
4318 if (!fn && !TREE_TYPE (t))
4319 return 1;
4320 if (TREE_CODE (t) == COMPONENT_REF)
4321 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4322 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4323 else
4324 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4326 case MODOP_EXPR:
4327 case CAST_EXPR:
4328 case REINTERPRET_CAST_EXPR:
4329 case CONST_CAST_EXPR:
4330 case STATIC_CAST_EXPR:
4331 case DYNAMIC_CAST_EXPR:
4332 case ARROW_EXPR:
4333 case DOTSTAR_EXPR:
4334 case TYPEID_EXPR:
4335 case LOOKUP_EXPR:
4336 case PSEUDO_DTOR_EXPR:
4337 if (!fn)
4338 return 1;
4339 /* Fall through. */
4341 default:
4342 switch (TREE_CODE_CLASS (TREE_CODE (t)))
4344 case '1':
4345 case '2':
4346 case 'e':
4347 case '<':
4349 int i;
4350 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
4351 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
4352 return 1;
4353 return 0;
4355 default:
4356 break;
4358 sorry ("testing %s for template parms",
4359 tree_code_name [(int) TREE_CODE (t)]);
4360 my_friendly_abort (82);
4361 /* NOTREACHED */
4362 return 0;
4367 uses_template_parms (t)
4368 tree t;
4370 return for_each_template_parm (t, 0, 0);
4373 static struct tinst_level *current_tinst_level;
4374 static struct tinst_level *free_tinst_level;
4375 static int tinst_depth;
4376 extern int max_tinst_depth;
4377 #ifdef GATHER_STATISTICS
4378 int depth_reached;
4379 #endif
4380 int tinst_level_tick;
4381 int last_template_error_tick;
4383 /* Print out all the template instantiations that we are currently
4384 working on. If ERR, we are being called from cp_thing, so do
4385 the right thing for an error message. */
4387 static void
4388 print_template_context (err)
4389 int err;
4391 struct tinst_level *p = current_tinst_level;
4392 int line = lineno;
4393 char *file = input_filename;
4395 if (err && p)
4397 if (current_function_decl != p->decl
4398 && current_function_decl != NULL_TREE)
4399 /* We can get here during the processing of some synthesized
4400 method. Then, p->decl will be the function that's causing
4401 the synthesis. */
4403 else
4405 if (current_function_decl == p->decl)
4406 /* Avoid redundancy with the the "In function" line. */;
4407 else
4408 fprintf (stderr, "%s: In instantiation of `%s':\n",
4409 file, decl_as_string (p->decl, 0));
4411 line = p->line;
4412 file = p->file;
4413 p = p->next;
4417 for (; p; p = p->next)
4419 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
4420 decl_as_string (p->decl, 0));
4421 line = p->line;
4422 file = p->file;
4424 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
4427 /* Called from cp_thing to print the template context for an error. */
4429 void
4430 maybe_print_template_context ()
4432 if (last_template_error_tick == tinst_level_tick
4433 || current_tinst_level == 0)
4434 return;
4436 last_template_error_tick = tinst_level_tick;
4437 print_template_context (1);
4440 static int
4441 push_tinst_level (d)
4442 tree d;
4444 struct tinst_level *new;
4446 if (tinst_depth >= max_tinst_depth)
4448 /* If the instantiation in question still has unbound template parms,
4449 we don't really care if we can't instantiate it, so just return.
4450 This happens with base instantiation for implicit `typename'. */
4451 if (uses_template_parms (d))
4452 return 0;
4454 last_template_error_tick = tinst_level_tick;
4455 error ("template instantiation depth exceeds maximum of %d",
4456 max_tinst_depth);
4457 error (" (use -ftemplate-depth-NN to increase the maximum)");
4458 cp_error (" instantiating `%D'", d);
4460 print_template_context (0);
4462 return 0;
4465 if (free_tinst_level)
4467 new = free_tinst_level;
4468 free_tinst_level = new->next;
4470 else
4471 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
4473 new->decl = d;
4474 new->line = lineno;
4475 new->file = input_filename;
4476 new->next = current_tinst_level;
4477 current_tinst_level = new;
4479 ++tinst_depth;
4480 #ifdef GATHER_STATISTICS
4481 if (tinst_depth > depth_reached)
4482 depth_reached = tinst_depth;
4483 #endif
4485 ++tinst_level_tick;
4486 return 1;
4489 void
4490 pop_tinst_level ()
4492 struct tinst_level *old = current_tinst_level;
4494 /* Restore the filename and line number stashed away when we started
4495 this instantiation. */
4496 lineno = old->line;
4497 input_filename = old->file;
4498 extract_interface_info ();
4500 current_tinst_level = old->next;
4501 old->next = free_tinst_level;
4502 free_tinst_level = old;
4503 --tinst_depth;
4504 ++tinst_level_tick;
4507 struct tinst_level *
4508 tinst_for_decl ()
4510 struct tinst_level *p = current_tinst_level;
4512 if (p)
4513 for (; p->next ; p = p->next )
4515 return p;
4518 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4519 vector of template arguments, as for tsubst.
4521 Returns an appropriate tsbust'd friend declaration. */
4523 static tree
4524 tsubst_friend_function (decl, args)
4525 tree decl;
4526 tree args;
4528 tree new_friend;
4529 int line = lineno;
4530 char *file = input_filename;
4532 lineno = DECL_SOURCE_LINE (decl);
4533 input_filename = DECL_SOURCE_FILE (decl);
4535 if (TREE_CODE (decl) == FUNCTION_DECL
4536 && DECL_TEMPLATE_INSTANTIATION (decl)
4537 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4538 /* This was a friend declared with an explicit template
4539 argument list, e.g.:
4541 friend void f<>(T);
4543 to indicate that f was a template instantiation, not a new
4544 function declaration. Now, we have to figure out what
4545 instantiation of what template. */
4547 tree template_id;
4548 tree new_args;
4549 tree tmpl;
4551 template_id
4552 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
4553 args, /*complain=*/1,
4554 NULL_TREE),
4555 tsubst (DECL_TI_ARGS (decl),
4556 args, /*complain=*/1,
4557 NULL_TREE));
4558 /* FIXME: The decl we create via the next tsubst could be
4559 created on a temporary obstack. */
4560 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4561 tmpl = determine_specialization (template_id, new_friend,
4562 &new_args,
4563 /*need_member_template=*/0);
4564 new_friend = instantiate_template (tmpl, new_args);
4565 goto done;
4568 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4570 /* The NEW_FRIEND will look like an instantiation, to the
4571 compiler, but is not an instantiation from the point of view of
4572 the language. For example, we might have had:
4574 template <class T> struct S {
4575 template <class U> friend void f(T, U);
4578 Then, in S<int>, template <class U> void f(int, U) is not an
4579 instantiation of anything. */
4580 DECL_USE_TEMPLATE (new_friend) = 0;
4581 if (TREE_CODE (decl) == TEMPLATE_DECL)
4582 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4584 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4585 tsubst will have resulted in a call to
4586 set_mangled_name_for_template_decl. But, the function is not a
4587 template instantiation and should not be mangled like one.
4588 Therefore, we remangle the function name. We don't have to do
4589 this if the NEW_FRIEND is a template since
4590 set_mangled_name_for_template_decl doesn't do anything if the
4591 function declaration still uses template arguments. */
4592 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4594 set_mangled_name_for_decl (new_friend);
4595 DECL_RTL (new_friend) = 0;
4596 make_decl_rtl (new_friend, NULL_PTR, 1);
4599 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4601 tree old_decl;
4602 tree new_friend_template_info;
4603 tree new_friend_result_template_info;
4604 tree ns;
4605 int new_friend_is_defn;
4607 /* We must save some information from NEW_FRIEND before calling
4608 duplicate decls since that function will free NEW_FRIEND if
4609 possible. */
4610 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4611 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4613 /* This declaration is a `primary' template. */
4614 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4616 new_friend_is_defn
4617 = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
4618 new_friend_result_template_info
4619 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
4621 else
4623 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4624 new_friend_result_template_info = NULL_TREE;
4627 /* Inside pushdecl_namespace_level, we will push into the
4628 current namespace. However, the friend function should
4629 tyically go into the namespace of the template. */
4630 ns = decl_namespace_context (new_friend);
4631 push_nested_namespace (ns);
4632 old_decl = pushdecl_namespace_level (new_friend);
4633 pop_nested_namespace (ns);
4635 if (old_decl != new_friend)
4637 /* This new friend declaration matched an existing
4638 declaration. For example, given:
4640 template <class T> void f(T);
4641 template <class U> class C {
4642 template <class T> friend void f(T) {}
4645 the friend declaration actually provides the definition
4646 of `f', once C has been instantiated for some type. So,
4647 old_decl will be the out-of-class template declaration,
4648 while new_friend is the in-class definition.
4650 But, if `f' was called before this point, the
4651 instantiation of `f' will have DECL_TI_ARGS corresponding
4652 to `T' but not to `U', references to which might appear
4653 in the definition of `f'. Previously, the most general
4654 template for an instantiation of `f' was the out-of-class
4655 version; now it is the in-class version. Therefore, we
4656 run through all specialization of `f', adding to their
4657 DECL_TI_ARGS appropriately. In particular, they need a
4658 new set of outer arguments, corresponding to the
4659 arguments for this class instantiation.
4661 The same situation can arise with something like this:
4663 friend void f(int);
4664 template <class T> class C {
4665 friend void f(T) {}
4668 when `C<int>' is instantiated. Now, `f(int)' is defined
4669 in the class. */
4671 if (!new_friend_is_defn)
4672 /* On the other hand, if the in-class declaration does
4673 *not* provide a definition, then we don't want to alter
4674 existing definitions. We can just leave everything
4675 alone. */
4677 else
4679 /* Overwrite whatever template info was there before, if
4680 any, with the new template information pertaining to
4681 the declaration. */
4682 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4684 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4685 /* duplicate_decls will take care of this case. */
4687 else
4689 tree t;
4690 tree new_friend_args;
4692 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
4693 = new_friend_result_template_info;
4695 new_friend_args = TI_ARGS (new_friend_template_info);
4696 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4697 t != NULL_TREE;
4698 t = TREE_CHAIN (t))
4700 tree spec = TREE_VALUE (t);
4702 DECL_TI_ARGS (spec)
4703 = add_outermost_template_args (new_friend_args,
4704 DECL_TI_ARGS (spec));
4707 /* Now, since specializations are always supposed to
4708 hang off of the most general template, we must move
4709 them. */
4710 t = most_general_template (old_decl);
4711 if (t != old_decl)
4713 DECL_TEMPLATE_SPECIALIZATIONS (t)
4714 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4715 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4716 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4721 /* The information from NEW_FRIEND has been merged into OLD_DECL
4722 by duplicate_decls. */
4723 new_friend = old_decl;
4726 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4728 /* Check to see that the declaration is really present, and,
4729 possibly obtain an improved declaration. */
4730 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4731 new_friend);
4733 if (fn)
4734 new_friend = fn;
4737 done:
4738 lineno = line;
4739 input_filename = file;
4740 return new_friend;
4743 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4744 template arguments, as for tsubst.
4746 Returns an appropriate tsbust'd friend type. */
4748 static tree
4749 tsubst_friend_class (friend_tmpl, args)
4750 tree friend_tmpl;
4751 tree args;
4753 tree friend_type;
4754 tree tmpl;
4756 /* First, we look for a class template. */
4757 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4759 /* But, if we don't find one, it might be because we're in a
4760 situation like this:
4762 template <class T>
4763 struct S {
4764 template <class U>
4765 friend struct S;
4768 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4769 for `S<int>', not the TEMPLATE_DECL. */
4770 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4772 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4773 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4776 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
4778 /* The friend template has already been declared. Just
4779 check to see that the declarations match, and install any new
4780 default parameters. We must tsubst the default parameters,
4781 of course. We only need the innermost template parameters
4782 because that is all that redeclare_class_template will look
4783 at. */
4784 tree parms
4785 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4786 args, /*complain=*/1);
4787 redeclare_class_template (TREE_TYPE (tmpl), parms);
4788 friend_type = TREE_TYPE (tmpl);
4790 else
4792 /* The friend template has not already been declared. In this
4793 case, the instantiation of the template class will cause the
4794 injection of this template into the global scope. */
4795 tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE);
4797 /* The new TMPL is not an instantiation of anything, so we
4798 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4799 the new type because that is supposed to be the corresponding
4800 template decl, i.e., TMPL. */
4801 DECL_USE_TEMPLATE (tmpl) = 0;
4802 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4803 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4805 /* Inject this template into the global scope. */
4806 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4809 return friend_type;
4812 tree
4813 instantiate_class_template (type)
4814 tree type;
4816 tree template, args, pattern, t;
4817 tree typedecl;
4819 if (type == error_mark_node)
4820 return error_mark_node;
4822 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4823 return type;
4825 /* We want to allocate temporary vectors of template arguments and
4826 template argument expressions on the momentary obstack, not on
4827 the expression obstack. Otherwise, all the space allocated in
4828 argument coercion and such is simply lost. */
4829 push_momentary ();
4831 /* Figure out which template is being instantiated. */
4832 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4833 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4835 /* Figure out which arguments are being used to do the
4836 instantiation. */
4837 args = CLASSTYPE_TI_ARGS (type);
4838 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
4840 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4841 /* If this is a partial instantiation, then we can't instantiate
4842 the type; there's no telling whether or not one of the
4843 template parameters might eventually be instantiated to some
4844 value that results in a specialization being used. For
4845 example, consider:
4847 template <class T>
4848 struct S {};
4850 template <class U>
4851 void f(S<U>);
4853 template <>
4854 struct S<int> {};
4856 Now, the `S<U>' in `f<int>' is the specialization, not an
4857 instantiation of the original template. */
4858 goto end;
4860 /* Determine what specialization of the original template to
4861 instantiate. */
4862 if (PARTIAL_INSTANTIATION_P (type))
4863 /* There's no telling which specialization is appropriate at this
4864 point. Since all peeking at the innards of this partial
4865 instantiation are extensions (like the "implicit typename"
4866 extension, which allows users to omit the keyword `typename' on
4867 names that are declared as types in template base classes), we
4868 are free to do what we please.
4870 Trying to figure out which partial instantiation to use can
4871 cause a crash. (Some of the template arguments don't even have
4872 types.) So, we just use the most general version. */
4873 t = NULL_TREE;
4874 else
4876 t = most_specialized_class (template, args);
4878 if (t == error_mark_node)
4880 const char *str = "candidates are:";
4881 cp_error ("ambiguous class template instantiation for `%#T'", type);
4882 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4883 t = TREE_CHAIN (t))
4885 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4886 args))
4888 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4889 str = " ";
4892 TYPE_BEING_DEFINED (type) = 1;
4893 type = error_mark_node;
4894 goto end;
4898 if (t)
4899 pattern = TREE_TYPE (t);
4900 else
4901 pattern = TREE_TYPE (template);
4903 /* If the template we're instantiating is incomplete, then clearly
4904 there's nothing we can do. */
4905 if (TYPE_SIZE (pattern) == NULL_TREE)
4906 goto end;
4908 /* If this is a partial instantiation, don't tsubst anything. We will
4909 only use this type for implicit typename, so the actual contents don't
4910 matter. All that matters is whether a particular name is a type. */
4911 if (PARTIAL_INSTANTIATION_P (type))
4913 /* The fields set here must be kept in sync with those cleared
4914 in begin_class_definition. */
4915 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4916 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4917 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4918 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4919 /* Pretend that the type is complete, so that we will look
4920 inside it during name lookup and such. */
4921 TYPE_SIZE (type) = integer_zero_node;
4922 goto end;
4925 /* If we've recursively instantiated too many templates, stop. */
4926 if (! push_tinst_level (type))
4927 goto end;
4929 /* Now we're really doing the instantiation. Mark the type as in
4930 the process of being defined. */
4931 TYPE_BEING_DEFINED (type) = 1;
4933 maybe_push_to_top_level (uses_template_parms (type));
4935 if (t)
4937 /* This TYPE is actually a instantiation of of a partial
4938 specialization. We replace the innermost set of ARGS with
4939 the arguments appropriate for substitution. For example,
4940 given:
4942 template <class T> struct S {};
4943 template <class T> struct S<T*> {};
4945 and supposing that we are instantiating S<int*>, ARGS will
4946 present be {int*} but we need {int}. */
4947 tree inner_args
4948 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4949 args);
4951 /* If there were multiple levels in ARGS, replacing the
4952 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4953 want, so we make a copy first. */
4954 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4956 args = copy_node (args);
4957 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4959 else
4960 args = inner_args;
4963 if (flag_external_templates)
4965 if (flag_alt_external_templates)
4967 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4968 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4969 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4970 = (! CLASSTYPE_INTERFACE_ONLY (type)
4971 && CLASSTYPE_INTERFACE_KNOWN (type));
4973 else
4975 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4976 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4977 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4978 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4979 = (! CLASSTYPE_INTERFACE_ONLY (type)
4980 && CLASSTYPE_INTERFACE_KNOWN (type));
4983 else
4985 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4986 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
4989 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4990 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4991 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4992 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4993 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4994 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4995 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4996 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4997 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
4998 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
4999 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5000 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5001 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5002 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5003 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5004 TYPE_USES_COMPLEX_INHERITANCE (type)
5005 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
5006 TYPE_USES_MULTIPLE_INHERITANCE (type)
5007 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5008 TYPE_USES_VIRTUAL_BASECLASSES (type)
5009 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5010 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5011 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5012 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5013 if (ANON_AGGR_TYPE_P (pattern))
5014 SET_ANON_AGGR_TYPE_P (type);
5016 if (TYPE_BINFO_BASETYPES (pattern))
5018 tree base_list = NULL_TREE;
5019 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5020 int i;
5022 /* Substitute into each of the bases to determine the actual
5023 basetypes. */
5024 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5026 tree base;
5027 tree access;
5028 tree pbase;
5030 pbase = TREE_VEC_ELT (pbases, i);
5032 /* Substitue to figure out the base class. */
5033 base = tsubst (BINFO_TYPE (pbase), args,
5034 /*complain=*/1, NULL_TREE);
5035 if (base == error_mark_node)
5036 continue;
5038 /* Calculate the correct access node. */
5039 if (TREE_VIA_VIRTUAL (pbase))
5041 if (TREE_VIA_PUBLIC (pbase))
5042 access = access_public_virtual_node;
5043 else if (TREE_VIA_PROTECTED (pbase))
5044 access = access_protected_virtual_node;
5045 else
5046 access = access_private_virtual_node;
5048 else
5050 if (TREE_VIA_PUBLIC (pbase))
5051 access = access_public_node;
5052 else if (TREE_VIA_PROTECTED (pbase))
5053 access = access_protected_node;
5054 else
5055 access = access_private_node;
5058 base_list = tree_cons (access, base, base_list);
5061 /* The list is now in reverse order; correct that. */
5062 base_list = nreverse (base_list);
5064 /* Now call xref_basetypes to set up all the base-class
5065 information. */
5066 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5067 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5068 ? class_type_node : record_type_node)
5069 : union_type_node,
5070 DECL_NAME (TYPE_NAME (pattern)),
5071 type,
5072 base_list);
5075 /* Now that our base classes are set up, enter the scope of the
5076 class, so that name lookups into base classes, etc. will work
5077 corectly. This is precisely analagous to what we do in
5078 begin_class_definition when defining an ordinary non-template
5079 class. */
5080 pushclass (type, 1);
5082 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
5084 tree tag = TREE_VALUE (t);
5085 tree name = TYPE_IDENTIFIER (tag);
5086 tree newtag;
5088 newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE);
5089 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5091 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5092 /* Unfortunately, lookup_template_class sets
5093 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5094 instantiation (i.e., for the type of a member template
5095 class nested within a template class.) This behavior is
5096 required for maybe_process_partial_specialization to work
5097 correctly, but is not accurate in this case; the TAG is not
5098 an instantiation of anything. (The corresponding
5099 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5100 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5102 /* Now, we call pushtag to put this NEWTAG into the scope of
5103 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5104 pushtag calling push_template_decl. We don't have to do
5105 this for enums because it will already have been done in
5106 tsubst_enum. */
5107 if (name)
5108 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5109 pushtag (name, newtag, /*globalize=*/0);
5113 /* Don't replace enum constants here. */
5114 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
5115 if (TREE_CODE (t) != CONST_DECL)
5117 tree r;
5119 /* The the file and line for this declaration, to assist in
5120 error message reporting. Since we called push_tinst_level
5121 above, we don't need to restore these. */
5122 lineno = DECL_SOURCE_LINE (t);
5123 input_filename = DECL_SOURCE_FILE (t);
5125 r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5126 if (TREE_CODE (r) == VAR_DECL)
5128 tree init;
5130 if (DECL_DEFINED_IN_CLASS_P (r))
5131 init = tsubst_expr (DECL_INITIAL (t), args,
5132 /*complain=*/1, NULL_TREE);
5133 else
5134 init = NULL_TREE;
5136 finish_static_data_member_decl (r, init,
5137 /*asmspec_tree=*/NULL_TREE,
5138 /*need_pop=*/0,
5139 /*flags=*/0);
5141 if (DECL_DEFINED_IN_CLASS_P (r))
5142 check_static_variable_definition (r, TREE_TYPE (r));
5145 /* R will have a TREE_CHAIN if and only if it has already been
5146 processed by finish_member_declaration. This can happen
5147 if, for example, it is a TYPE_DECL for a class-scoped
5148 ENUMERAL_TYPE; such a thing will already have been added to
5149 the field list by tsubst_enum above. */
5150 if (!TREE_CHAIN (r))
5152 set_current_access_from_decl (r);
5153 finish_member_declaration (r);
5157 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5158 for this instantiation. */
5159 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5161 tree r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5162 set_current_access_from_decl (r);
5163 finish_member_declaration (r);
5166 /* Construct the DECL_FRIENDLIST for the new class type. */
5167 typedecl = TYPE_MAIN_DECL (type);
5168 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5169 t != NULL_TREE;
5170 t = TREE_CHAIN (t))
5172 tree friends;
5174 for (friends = TREE_VALUE (t);
5175 friends != NULL_TREE;
5176 friends = TREE_CHAIN (friends))
5177 if (TREE_PURPOSE (friends) == error_mark_node)
5178 add_friend (type,
5179 tsubst_friend_function (TREE_VALUE (friends),
5180 args));
5181 else
5182 add_friends (type,
5183 tsubst_copy (TREE_PURPOSE (t), args,
5184 /*complain=*/1, NULL_TREE),
5185 tsubst (TREE_PURPOSE (friends), args,
5186 /*complain=*/1, NULL_TREE));
5189 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5190 t != NULL_TREE;
5191 t = TREE_CHAIN (t))
5193 tree friend_type = TREE_VALUE (t);
5194 tree new_friend_type;
5196 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5197 new_friend_type = tsubst_friend_class (friend_type, args);
5198 else if (uses_template_parms (friend_type))
5199 new_friend_type = tsubst (friend_type, args, /*complain=*/1,
5200 NULL_TREE);
5201 else
5203 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5205 /* The call to xref_tag_from_type does injection for friend
5206 classes. */
5207 push_nested_namespace (ns);
5208 new_friend_type =
5209 xref_tag_from_type (friend_type, NULL_TREE, 1);
5210 pop_nested_namespace (ns);
5213 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5214 /* Trick make_friend_class into realizing that the friend
5215 we're adding is a template, not an ordinary class. It's
5216 important that we use make_friend_class since it will
5217 perform some error-checking and output cross-reference
5218 information. */
5219 ++processing_template_decl;
5221 make_friend_class (type, new_friend_type);
5223 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5224 --processing_template_decl;
5227 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5228 if (TREE_CODE (t) == FIELD_DECL)
5230 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5231 require_complete_type (t);
5234 /* Set the file and line number information to whatever is given for
5235 the class itself. This puts error messages involving generated
5236 implicit functions at a predictable point, and the same point
5237 that would be used for non-template classes. */
5238 lineno = DECL_SOURCE_LINE (typedecl);
5239 input_filename = DECL_SOURCE_FILE (typedecl);
5241 unreverse_member_declarations (type);
5242 finish_struct_1 (type);
5243 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5245 /* Clear this now so repo_template_used is happy. */
5246 TYPE_BEING_DEFINED (type) = 0;
5247 repo_template_used (type);
5249 /* Now that the class is complete, instantiate default arguments for
5250 any member functions. We don't do this earlier because the
5251 default arguments may reference members of the class. */
5252 if (!PRIMARY_TEMPLATE_P (template))
5253 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5254 if (TREE_CODE (t) == FUNCTION_DECL
5255 /* Implicitly generated member functions will not have tmplate
5256 information; they are not instantiations, but instead are
5257 created "fresh" for each instantiation. */
5258 && DECL_TEMPLATE_INFO (t))
5259 tsubst_default_arguments (t);
5261 popclass ();
5262 pop_from_top_level ();
5263 pop_tinst_level ();
5265 end:
5266 pop_momentary ();
5268 return type;
5271 static int
5272 list_eq (t1, t2)
5273 tree t1, t2;
5275 if (t1 == NULL_TREE)
5276 return t2 == NULL_TREE;
5277 if (t2 == NULL_TREE)
5278 return 0;
5279 /* Don't care if one declares its arg const and the other doesn't -- the
5280 main variant of the arg type is all that matters. */
5281 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5282 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5283 return 0;
5284 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5287 /* If arg is a non-type template parameter that does not depend on template
5288 arguments, fold it like we weren't in the body of a template. */
5290 static tree
5291 maybe_fold_nontype_arg (arg)
5292 tree arg;
5294 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
5295 && !uses_template_parms (arg))
5297 /* Sometimes, one of the args was an expression involving a
5298 template constant parameter, like N - 1. Now that we've
5299 tsubst'd, we might have something like 2 - 1. This will
5300 confuse lookup_template_class, so we do constant folding
5301 here. We have to unset processing_template_decl, to
5302 fool build_expr_from_tree() into building an actual
5303 tree. */
5305 int saved_processing_template_decl = processing_template_decl;
5306 processing_template_decl = 0;
5307 arg = fold (build_expr_from_tree (arg));
5308 processing_template_decl = saved_processing_template_decl;
5310 return arg;
5313 /* Return the TREE_VEC with the arguments for the innermost template header,
5314 where ARGS is either that or the VEC of VECs for all the
5315 arguments. */
5317 tree
5318 innermost_args (args)
5319 tree args;
5321 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
5324 /* Substitute ARGS into the vector of template arguments T. */
5326 static tree
5327 tsubst_template_arg_vector (t, args, complain)
5328 tree t;
5329 tree args;
5330 int complain;
5332 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5333 tree *elts = (tree *) alloca (len * sizeof (tree));
5335 bzero ((char *) elts, len * sizeof (tree));
5337 for (i = 0; i < len; i++)
5339 if (TREE_VEC_ELT (t, i) != NULL_TREE
5340 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5341 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5342 args, complain);
5343 else
5344 elts[i] = maybe_fold_nontype_arg
5345 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5346 NULL_TREE));
5348 if (elts[i] != TREE_VEC_ELT (t, i))
5349 need_new = 1;
5352 if (!need_new)
5353 return t;
5355 t = make_temp_vec (len);
5356 for (i = 0; i < len; i++)
5357 TREE_VEC_ELT (t, i) = elts[i];
5359 return t;
5362 /* Return the result of substituting ARGS into the template parameters
5363 given by PARMS. If there are m levels of ARGS and m + n levels of
5364 PARMS, then the result will contain n levels of PARMS. For
5365 example, if PARMS is `template <class T> template <class U>
5366 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5367 result will be `template <int*, double, class V>'. */
5369 static tree
5370 tsubst_template_parms (parms, args, complain)
5371 tree parms;
5372 tree args;
5373 int complain;
5375 tree r = NULL_TREE;
5376 tree* new_parms;
5378 for (new_parms = &r;
5379 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5380 new_parms = &(TREE_CHAIN (*new_parms)),
5381 parms = TREE_CHAIN (parms))
5383 tree new_vec =
5384 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5385 int i;
5387 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5389 tree default_value =
5390 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5391 tree parm_decl =
5392 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5394 TREE_VEC_ELT (new_vec, i)
5395 = build_tree_list (tsubst (default_value, args, complain,
5396 NULL_TREE),
5397 tsubst (parm_decl, args, complain,
5398 NULL_TREE));
5401 *new_parms =
5402 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
5403 - TMPL_ARGS_DEPTH (args))),
5404 new_vec, NULL_TREE);
5407 return r;
5410 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5411 type T. If T is not an aggregate or enumeration type, it is
5412 handled as if by tsubst. IN_DECL is as for tsubst. If
5413 ENTERING_SCOPE is non-zero, T is the context for a template which
5414 we are presently tsubst'ing. Return the subsituted value. */
5416 static tree
5417 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5418 tree t;
5419 tree args;
5420 int complain;
5421 tree in_decl;
5422 int entering_scope;
5424 if (t == NULL_TREE)
5425 return NULL_TREE;
5427 switch (TREE_CODE (t))
5429 case RECORD_TYPE:
5430 if (TYPE_PTRMEMFUNC_P (t))
5432 tree r = build_ptrmemfunc_type
5433 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl));
5434 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5435 complain);
5438 /* else fall through */
5439 case ENUMERAL_TYPE:
5440 case UNION_TYPE:
5441 if (TYPE_TEMPLATE_INFO (t))
5443 tree argvec;
5444 tree context;
5445 tree r;
5447 /* First, determine the context for the type we are looking
5448 up. */
5449 if (TYPE_CONTEXT (t) != NULL_TREE)
5450 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5451 complain,
5452 in_decl, /*entering_scope=*/1);
5453 else
5454 context = NULL_TREE;
5456 /* Then, figure out what arguments are appropriate for the
5457 type we are trying to find. For example, given:
5459 template <class T> struct S;
5460 template <class T, class U> void f(T, U) { S<U> su; }
5462 and supposing that we are instantiating f<int, double>,
5463 then our ARGS will be {int, double}, but, when looking up
5464 S we only want {double}. */
5465 push_momentary ();
5466 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5467 complain);
5469 r = lookup_template_class (t, argvec, in_decl, context,
5470 entering_scope);
5471 pop_momentary ();
5473 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5474 complain);
5476 else
5477 /* This is not a template type, so there's nothing to do. */
5478 return t;
5480 default:
5481 return tsubst (t, args, complain, in_decl);
5485 /* Substitute into the default argument ARG (a default argument for
5486 FN), which has the indicated TYPE. */
5488 tree
5489 tsubst_default_argument (fn, type, arg)
5490 tree fn;
5491 tree type;
5492 tree arg;
5494 /* This default argument came from a template. Instantiate the
5495 default argument here, not in tsubst. In the case of
5496 something like:
5498 template <class T>
5499 struct S {
5500 static T t();
5501 void f(T = t());
5504 we must be careful to do name lookup in the scope of S<T>,
5505 rather than in the current class. */
5506 if (DECL_CLASS_SCOPE_P (fn))
5507 pushclass (DECL_REAL_CONTEXT (fn), 2);
5509 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
5511 if (DECL_CLASS_SCOPE_P (fn))
5512 popclass ();
5514 /* Make sure the default argument is reasonable. */
5515 arg = check_default_argument (type, arg);
5517 return arg;
5520 /* Substitute into all the default arguments for FN. */
5522 static void
5523 tsubst_default_arguments (fn)
5524 tree fn;
5526 tree arg;
5527 tree tmpl_args;
5529 tmpl_args = DECL_TI_ARGS (fn);
5531 /* If this function is not yet instantiated, we certainly don't need
5532 its default arguments. */
5533 if (uses_template_parms (tmpl_args))
5534 return;
5536 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5537 arg;
5538 arg = TREE_CHAIN (arg))
5539 if (TREE_PURPOSE (arg))
5540 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5541 TREE_VALUE (arg),
5542 TREE_PURPOSE (arg));
5545 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5546 (already computed) substitution of ARGS into TREE_TYPE (T), if
5547 appropriate. Return the result of the substitution. IN_DECL is as
5548 for tsubst. */
5550 static tree
5551 tsubst_decl (t, args, type, in_decl)
5552 tree t;
5553 tree args;
5554 tree type;
5555 tree in_decl;
5557 int saved_lineno;
5558 char* saved_filename;
5559 tree r = NULL_TREE;
5561 /* Set the filename and linenumber to improve error-reporting. */
5562 saved_lineno = lineno;
5563 saved_filename = input_filename;
5564 lineno = DECL_SOURCE_LINE (t);
5565 input_filename = DECL_SOURCE_FILE (t);
5567 switch (TREE_CODE (t))
5569 case TEMPLATE_DECL:
5571 /* We can get here when processing a member template function
5572 of a template class. */
5573 tree decl = DECL_TEMPLATE_RESULT (t);
5574 tree spec;
5575 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5577 if (!is_template_template_parm)
5579 /* We might already have an instance of this template.
5580 The ARGS are for the surrounding class type, so the
5581 full args contain the tsubst'd args for the context,
5582 plus the innermost args from the template decl. */
5583 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5584 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5585 : DECL_TI_ARGS (DECL_RESULT (t));
5586 tree full_args;
5588 push_momentary ();
5589 full_args = tsubst_template_arg_vector (tmpl_args, args,
5590 /*complain=*/1);
5592 /* tsubst_template_arg_vector doesn't copy the vector if
5593 nothing changed. But, *something* should have
5594 changed. */
5595 my_friendly_assert (full_args != tmpl_args, 0);
5597 spec = retrieve_specialization (t, full_args);
5598 pop_momentary ();
5599 if (spec != NULL_TREE)
5601 r = spec;
5602 break;
5606 /* Make a new template decl. It will be similar to the
5607 original, but will record the current template arguments.
5608 We also create a new function declaration, which is just
5609 like the old one, but points to this new template, rather
5610 than the old one. */
5611 r = copy_node (t);
5612 copy_lang_decl (r);
5613 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5614 TREE_CHAIN (r) = NULL_TREE;
5616 if (is_template_template_parm)
5618 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5619 DECL_RESULT (r) = new_decl;
5620 TREE_TYPE (r) = TREE_TYPE (new_decl);
5621 break;
5624 DECL_CONTEXT (r)
5625 = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1,
5626 in_decl, /*entering_scope=*/1);
5627 DECL_CLASS_CONTEXT (r)
5628 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
5629 /*complain=*/1, in_decl,
5630 /*entering_scope=*/1);
5631 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5633 if (TREE_CODE (decl) == TYPE_DECL)
5635 tree new_type = tsubst (TREE_TYPE (t), args,
5636 /*complain=*/1, in_decl);
5637 TREE_TYPE (r) = new_type;
5638 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5639 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
5640 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5642 else
5644 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5645 DECL_RESULT (r) = new_decl;
5646 DECL_TI_TEMPLATE (new_decl) = r;
5647 TREE_TYPE (r) = TREE_TYPE (new_decl);
5648 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5651 SET_DECL_IMPLICIT_INSTANTIATION (r);
5652 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5653 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5655 /* The template parameters for this new template are all the
5656 template parameters for the old template, except the
5657 outermost level of parameters. */
5658 DECL_TEMPLATE_PARMS (r)
5659 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5660 /*complain=*/1);
5662 if (PRIMARY_TEMPLATE_P (t))
5663 DECL_PRIMARY_TEMPLATE (r) = r;
5665 /* We don't partially instantiate partial specializations. */
5666 if (TREE_CODE (decl) == TYPE_DECL)
5667 break;
5669 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
5670 spec != NULL_TREE;
5671 spec = TREE_CHAIN (spec))
5673 /* It helps to consider example here. Consider:
5675 template <class T>
5676 struct S {
5677 template <class U>
5678 void f(U u);
5680 template <>
5681 void f(T* t) {}
5684 Now, for example, we are instantiating S<int>::f(U u).
5685 We want to make a template:
5687 template <class U>
5688 void S<int>::f(U);
5690 It will have a specialization, for the case U = int*, of
5691 the form:
5693 template <>
5694 void S<int>::f<int*>(int*);
5696 This specialization will be an instantiation of
5697 the specialization given in the declaration of S, with
5698 argument list int*. */
5700 tree fn = TREE_VALUE (spec);
5701 tree spec_args;
5702 tree new_fn;
5704 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
5705 /* Instantiations are on the same list, but they're of
5706 no concern to us. */
5707 continue;
5709 if (TREE_CODE (fn) != TEMPLATE_DECL)
5710 /* A full specialization. There's no need to record
5711 that here. */
5712 continue;
5714 spec_args = tsubst (DECL_TI_ARGS (fn), args,
5715 /*complain=*/1, in_decl);
5716 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
5717 spec_args, /*complain=*/1, in_decl);
5718 DECL_TI_TEMPLATE (new_fn) = fn;
5719 register_specialization (new_fn, r,
5720 innermost_args (spec_args));
5723 /* Record this partial instantiation. */
5724 register_specialization (r, t,
5725 DECL_TI_ARGS (DECL_RESULT (r)));
5728 break;
5730 case FUNCTION_DECL:
5732 tree ctx;
5733 tree argvec = NULL_TREE;
5734 tree *friends;
5735 tree gen_tmpl;
5736 int member;
5737 int args_depth;
5738 int parms_depth;
5740 /* Nobody should be tsubst'ing into non-template functions. */
5741 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5743 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5745 tree spec;
5747 /* Allocate template arguments on the momentary obstack,
5748 in case we don't need to keep them. */
5749 push_momentary ();
5751 /* Calculate the most general template of which R is a
5752 specialization, and the complete set of arguments used to
5753 specialize R. */
5754 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5755 argvec
5756 = tsubst_template_arg_vector (DECL_TI_ARGS
5757 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5758 args, /*complain=*/1);
5760 /* Check to see if we already have this specialization. */
5761 spec = retrieve_specialization (gen_tmpl, argvec);
5763 if (spec)
5765 r = spec;
5766 pop_momentary ();
5767 break;
5770 pop_momentary ();
5772 /* Here, we deal with the peculiar case:
5774 template <class T> struct S {
5775 template <class U> friend void f();
5777 template <class U> friend void f() {}
5778 template S<int>;
5779 template void f<double>();
5781 Here, the ARGS for the instantiation of will be {int,
5782 double}. But, we only need as many ARGS as there are
5783 levels of template parameters in CODE_PATTERN. We are
5784 careful not to get fooled into reducing the ARGS in
5785 situations like:
5787 template <class T> struct S { template <class U> void f(U); }
5788 template <class T> template <> void S<T>::f(int) {}
5790 which we can spot because the pattern will be a
5791 specialization in this case. */
5792 args_depth = TMPL_ARGS_DEPTH (args);
5793 parms_depth =
5794 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5795 if (args_depth > parms_depth
5796 && !DECL_TEMPLATE_SPECIALIZATION (t))
5798 my_friendly_assert (DECL_FRIEND_P (t), 0);
5800 if (parms_depth > 1)
5802 int i;
5804 args = make_temp_vec (parms_depth);
5805 for (i = 0; i < parms_depth; ++i)
5806 TREE_VEC_ELT (args, i) =
5807 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
5809 else
5810 args = TREE_VEC_ELT (args, args_depth - parms_depth);
5813 else
5815 /* This special case arises when we have something like this:
5817 template <class T> struct S {
5818 friend void f<int>(int, double);
5821 Here, the DECL_TI_TEMPLATE for the friend declaration
5822 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5823 being called from tsubst_friend_function, and we want
5824 only to create a new decl (R) with appropriate types so
5825 that we can call determine_specialization. */
5826 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5827 == LOOKUP_EXPR)
5828 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5829 == IDENTIFIER_NODE), 0);
5830 gen_tmpl = NULL_TREE;
5833 if (DECL_CLASS_SCOPE_P (t))
5835 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5836 member = 2;
5837 else
5838 member = 1;
5839 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
5840 /*complain=*/1, t,
5841 /*entering_scope=*/1);
5843 else
5845 member = 0;
5846 ctx = NULL_TREE;
5848 type = tsubst (type, args, /*complain=*/1, in_decl);
5850 /* We do NOT check for matching decls pushed separately at this
5851 point, as they may not represent instantiations of this
5852 template, and in any case are considered separate under the
5853 discrete model. Instead, see add_maybe_template. */
5855 r = copy_node (t);
5856 copy_lang_decl (r);
5857 DECL_USE_TEMPLATE (r) = 0;
5858 TREE_TYPE (r) = type;
5860 DECL_CONTEXT (r)
5861 = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t,
5862 /*entering_scope=*/1);
5863 DECL_CLASS_CONTEXT (r) = ctx;
5865 if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r)))
5866 /* Type-conversion operator. Reconstruct the name, in
5867 case it's the name of one of the template's parameters. */
5868 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
5870 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5871 /*complain=*/1, t);
5872 DECL_MAIN_VARIANT (r) = r;
5873 DECL_RESULT (r) = NULL_TREE;
5875 TREE_STATIC (r) = 0;
5876 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5877 DECL_EXTERNAL (r) = 1;
5878 DECL_INTERFACE_KNOWN (r) = 0;
5879 DECL_DEFER_OUTPUT (r) = 0;
5880 TREE_CHAIN (r) = NULL_TREE;
5881 DECL_PENDING_INLINE_INFO (r) = 0;
5882 TREE_USED (r) = 0;
5884 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5885 name. There's no need to do this in the special friend
5886 case mentioned above where GEN_TMPL is NULL. */
5887 if (gen_tmpl)
5889 DECL_TEMPLATE_INFO (r)
5890 = tree_cons (gen_tmpl, argvec, NULL_TREE);
5891 SET_DECL_IMPLICIT_INSTANTIATION (r);
5892 register_specialization (r, gen_tmpl, argvec);
5894 /* Set the mangled name for R. */
5895 if (DECL_DESTRUCTOR_P (t))
5896 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
5897 else
5899 /* Instantiations of template functions must be mangled
5900 specially, in order to conform to 14.5.5.1
5901 [temp.over.link]. */
5902 tree tmpl = DECL_TI_TEMPLATE (t);
5904 /* TMPL will be NULL if this is a specialization of a
5905 member function of a template class. */
5906 if (name_mangling_version < 1
5907 || tmpl == NULL_TREE
5908 || (member && !is_member_template (tmpl)
5909 && !DECL_TEMPLATE_INFO (tmpl)))
5910 set_mangled_name_for_decl (r);
5911 else
5912 set_mangled_name_for_template_decl (r);
5915 DECL_RTL (r) = 0;
5916 make_decl_rtl (r, NULL_PTR, 1);
5918 /* Like grokfndecl. If we don't do this, pushdecl will
5919 mess up our TREE_CHAIN because it doesn't find a
5920 previous decl. Sigh. */
5921 if (member
5922 && ! uses_template_parms (r)
5923 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
5924 == NULL_TREE))
5925 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
5927 /* We're not supposed to instantiate default arguments
5928 until they are called, for a template. But, for a
5929 declaration like:
5931 template <class T> void f ()
5932 { extern void g(int i = T()); }
5934 we should do the substitution when the template is
5935 instantiated. We handle the member function case in
5936 instantiate_class_template since the default arguments
5937 might refer to other members of the class. */
5938 if (!member
5939 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5940 && !uses_template_parms (argvec))
5941 tsubst_default_arguments (r);
5944 /* Copy the list of befriending classes. */
5945 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5946 *friends;
5947 friends = &TREE_CHAIN (*friends))
5949 *friends = copy_node (*friends);
5950 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5951 args, /*complain=*/1,
5952 in_decl);
5955 if (DECL_CONSTRUCTOR_P (r))
5957 maybe_retrofit_in_chrg (r);
5958 grok_ctor_properties (ctx, r);
5960 else if (DECL_OVERLOADED_OPERATOR_P (r))
5961 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
5963 break;
5965 case PARM_DECL:
5967 r = copy_node (t);
5968 TREE_TYPE (r) = type;
5969 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5971 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5972 DECL_INITIAL (r) = TREE_TYPE (r);
5973 else
5974 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5975 /*complain=*/1, in_decl);
5977 DECL_CONTEXT (r) = NULL_TREE;
5978 if (PROMOTE_PROTOTYPES
5979 && (TREE_CODE (type) == INTEGER_TYPE
5980 || TREE_CODE (type) == ENUMERAL_TYPE)
5981 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5982 DECL_ARG_TYPE (r) = integer_type_node;
5983 if (TREE_CHAIN (t))
5984 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5985 /*complain=*/1, TREE_CHAIN (t));
5987 break;
5989 case FIELD_DECL:
5991 r = copy_node (t);
5992 copy_lang_decl (r);
5993 TREE_TYPE (r) = type;
5994 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5996 /* We don't have to set DECL_CONTEXT here; it is set by
5997 finish_member_declaration. */
5998 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
5999 /*complain=*/1, in_decl);
6000 TREE_CHAIN (r) = NULL_TREE;
6001 if (TREE_CODE (type) == VOID_TYPE)
6002 cp_error_at ("instantiation of `%D' as type void", r);
6004 break;
6006 case USING_DECL:
6008 r = copy_node (t);
6009 DECL_INITIAL (r)
6010 = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl);
6011 TREE_CHAIN (r) = NULL_TREE;
6013 break;
6015 case TYPE_DECL:
6016 if (DECL_IMPLICIT_TYPEDEF_P (t))
6018 /* For an implicit typedef, we just want the implicit
6019 typedef for the tsubst'd type. We've already got the
6020 tsubst'd type, as TYPE, so we just need it's associated
6021 declaration. */
6022 r = TYPE_NAME (type);
6023 break;
6025 else if (!DECL_LANG_SPECIFIC (t))
6027 /* For a template type parameter, we don't have to do
6028 anything special. */
6029 r= TYPE_NAME (type);
6030 break;
6033 /* Fall through. */
6035 case VAR_DECL:
6037 tree argvec;
6038 tree gen_tmpl;
6039 tree spec;
6040 tree tmpl;
6041 tree ctx;
6043 /* Nobody should be tsubst'ing into non-template variables. */
6044 my_friendly_assert (DECL_LANG_SPECIFIC (t)
6045 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
6047 if (TYPE_P (DECL_CONTEXT (t)))
6048 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6049 /*complain=*/1,
6050 in_decl, /*entering_scope=*/1);
6051 else
6052 /* Subsequent calls to pushdecl will fill this in. */
6053 ctx = NULL_TREE;
6055 /* Check to see if we already have this specialization. */
6056 tmpl = DECL_TI_TEMPLATE (t);
6057 gen_tmpl = most_general_template (tmpl);
6058 argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl);
6059 if (ctx)
6060 spec = retrieve_specialization (gen_tmpl, argvec);
6061 else
6062 spec = retrieve_local_specialization (gen_tmpl,
6063 current_function_decl);
6065 if (spec)
6067 r = spec;
6068 break;
6071 /* This declaration is going to have to be around for a while,
6072 so me make sure it is on a saveable obstack. */
6073 push_obstacks_nochange ();
6074 saveable_allocation ();
6075 r = copy_node (t);
6076 pop_obstacks ();
6078 TREE_TYPE (r) = type;
6079 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
6080 DECL_CONTEXT (r) = ctx;
6082 /* Don't try to expand the initializer until someone tries to use
6083 this variable; otherwise we run into circular dependencies. */
6084 DECL_INITIAL (r) = NULL_TREE;
6085 DECL_RTL (r) = 0;
6086 DECL_SIZE (r) = 0;
6087 copy_lang_decl (r);
6088 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
6090 /* Even if the original location is out of scope, the newly
6091 substituted one is not. */
6092 if (TREE_CODE (r) == VAR_DECL)
6093 DECL_DEAD_FOR_LOCAL (r) = 0;
6095 /* A static data member declaration is always marked external
6096 when it is declared in-class, even if an initializer is
6097 present. We mimic the non-template processing here. */
6098 if (ctx)
6099 DECL_EXTERNAL (r) = 1;
6101 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6102 SET_DECL_IMPLICIT_INSTANTIATION (r);
6103 if (ctx)
6104 register_specialization (r, gen_tmpl, argvec);
6105 else
6106 register_local_specialization (r, gen_tmpl,
6107 current_function_decl);
6109 TREE_CHAIN (r) = NULL_TREE;
6110 if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE)
6111 cp_error_at ("instantiation of `%D' as type void", r);
6113 break;
6115 default:
6116 my_friendly_abort (0);
6119 /* Restore the file and line information. */
6120 lineno = saved_lineno;
6121 input_filename = saved_filename;
6123 return r;
6126 /* Substitue into the ARG_TYPES of a function type. */
6128 static tree
6129 tsubst_arg_types (arg_types, args, complain, in_decl)
6130 tree arg_types;
6131 tree args;
6132 int complain;
6133 tree in_decl;
6135 tree remaining_arg_types;
6136 tree type;
6138 if (!arg_types || arg_types == void_list_node)
6139 return arg_types;
6141 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6142 args, complain, in_decl);
6143 if (remaining_arg_types == error_mark_node)
6144 return error_mark_node;
6146 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6147 if (type == error_mark_node)
6148 return error_mark_node;
6150 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6151 top-level qualifiers as required. */
6152 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6154 /* Note that we do not substitute into default arguments here. The
6155 standard mandates that they be instantiated only when needed,
6156 which is done in build_over_call. */
6157 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6158 remaining_arg_types);
6162 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6163 *not* handle the exception-specification for FNTYPE, because the
6164 initial substitution of explicitly provided template parameters
6165 during argument deduction forbids substitution into the
6166 exception-specification:
6168 [temp.deduct]
6170 All references in the function type of the function template to the
6171 corresponding template parameters are replaced by the specified tem-
6172 plate argument values. If a substitution in a template parameter or
6173 in the function type of the function template results in an invalid
6174 type, type deduction fails. [Note: The equivalent substitution in
6175 exception specifications is done only when the function is instanti-
6176 ated, at which point a program is ill-formed if the substitution
6177 results in an invalid type.] */
6179 static tree
6180 tsubst_function_type (t, args, complain, in_decl)
6181 tree t;
6182 tree args;
6183 int complain;
6184 tree in_decl;
6186 tree return_type;
6187 tree arg_types;
6188 tree fntype;
6190 /* The TYPE_CONTEXT is not used for function/method types. */
6191 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6193 /* Substitue the return type. */
6194 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6195 if (return_type == error_mark_node)
6196 return error_mark_node;
6198 /* Substitue the argument types. */
6199 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6200 complain, in_decl);
6201 if (arg_types == error_mark_node)
6202 return error_mark_node;
6204 /* Construct a new type node and return it. */
6205 if (TREE_CODE (t) == FUNCTION_TYPE)
6206 fntype = build_function_type (return_type, arg_types);
6207 else
6209 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6210 if (! IS_AGGR_TYPE (r))
6212 /* [temp.deduct]
6214 Type deduction may fail for any of the following
6215 reasons:
6217 -- Attempting to create "pointer to member of T" when T
6218 is not a class type. */
6219 if (complain)
6220 cp_error ("creating pointer to member function of non-class type `%T'",
6222 return error_mark_node;
6225 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6226 (arg_types));
6228 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
6229 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6231 return fntype;
6234 /* Substitute into the PARMS of a call-declarator. */
6236 static tree
6237 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6238 tree parms;
6239 tree args;
6240 int complain;
6241 tree in_decl;
6243 tree new_parms;
6244 tree type;
6245 tree defarg;
6247 if (!parms || parms == void_list_node)
6248 return parms;
6250 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6251 args, complain, in_decl);
6253 /* Figure out the type of this parameter. */
6254 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6256 /* Figure out the default argument as well. Note that we use
6257 tsubst_expr since the default argument is really an expression. */
6258 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6260 /* Chain this parameter on to the front of those we have already
6261 processed. We don't use hash_tree_cons because that function
6262 doesn't check TREE_PARMLIST. */
6263 new_parms = tree_cons (defarg, type, new_parms);
6265 /* And note that these are parameters. */
6266 TREE_PARMLIST (new_parms) = 1;
6268 return new_parms;
6271 /* Take the tree structure T and replace template parameters used
6272 therein with the argument vector ARGS. IN_DECL is an associated
6273 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6274 An appropriate error message is issued only if COMPLAIN is
6275 non-zero. Note that we must be relatively non-tolerant of
6276 extensions here, in order to preserve conformance; if we allow
6277 substitutions that should not be allowed, we may allow argument
6278 deductions that should not succeed, and therefore report ambiguous
6279 overload situations where there are none. In theory, we could
6280 allow the substitution, but indicate that it should have failed,
6281 and allow our caller to make sure that the right thing happens, but
6282 we don't try to do this yet.
6284 This function is used for dealing with types, decls and the like;
6285 for expressions, use tsubst_expr or tsubst_copy. */
6287 tree
6288 tsubst (t, args, complain, in_decl)
6289 tree t, args;
6290 int complain;
6291 tree in_decl;
6293 tree type, r;
6295 if (t == NULL_TREE || t == error_mark_node
6296 || t == integer_type_node
6297 || t == void_type_node
6298 || t == char_type_node
6299 || TREE_CODE (t) == NAMESPACE_DECL)
6300 return t;
6302 if (TREE_CODE (t) == IDENTIFIER_NODE)
6303 type = IDENTIFIER_TYPE_VALUE (t);
6304 else
6305 type = TREE_TYPE (t);
6306 if (type == unknown_type_node)
6307 my_friendly_abort (42);
6309 if (type && TREE_CODE (t) != FUNCTION_DECL
6310 && TREE_CODE (t) != TYPENAME_TYPE
6311 && TREE_CODE (t) != TEMPLATE_DECL
6312 && TREE_CODE (t) != IDENTIFIER_NODE
6313 && TREE_CODE (t) != FUNCTION_TYPE
6314 && TREE_CODE (t) != METHOD_TYPE)
6315 type = tsubst (type, args, complain, in_decl);
6316 if (type == error_mark_node)
6317 return error_mark_node;
6319 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
6320 return tsubst_decl (t, args, type, in_decl);
6322 switch (TREE_CODE (t))
6324 case RECORD_TYPE:
6325 case UNION_TYPE:
6326 case ENUMERAL_TYPE:
6327 return tsubst_aggr_type (t, args, complain, in_decl,
6328 /*entering_scope=*/0);
6330 case ERROR_MARK:
6331 case IDENTIFIER_NODE:
6332 case OP_IDENTIFIER:
6333 case VOID_TYPE:
6334 case REAL_TYPE:
6335 case COMPLEX_TYPE:
6336 case BOOLEAN_TYPE:
6337 case INTEGER_CST:
6338 case REAL_CST:
6339 case STRING_CST:
6340 return t;
6342 case INTEGER_TYPE:
6343 if (t == integer_type_node)
6344 return t;
6346 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6347 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6348 return t;
6351 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6353 max = tsubst_expr (omax, args, complain, in_decl);
6354 if (max == error_mark_node)
6355 return error_mark_node;
6357 /* See if we can reduce this expression to something simpler. */
6358 max = maybe_fold_nontype_arg (max);
6359 if (!processing_template_decl && TREE_READONLY_DECL_P (max))
6360 max = decl_constant_value (max);
6362 if (processing_template_decl
6363 /* When providing explicit arguments to a template
6364 function, but leaving some arguments for subsequent
6365 deduction, MAX may be template-dependent even if we're
6366 not PROCESSING_TEMPLATE_DECL. */
6367 || TREE_CODE (max) != INTEGER_CST)
6369 tree itype = make_node (INTEGER_TYPE);
6370 TYPE_MIN_VALUE (itype) = size_zero_node;
6371 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6372 integer_one_node);
6373 return itype;
6376 if (integer_zerop (omax))
6378 /* Still allow an explicit array of size zero. */
6379 if (pedantic)
6380 pedwarn ("creating array with size zero");
6382 else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node))
6384 /* [temp.deduct]
6386 Type deduction may fail for any of the following
6387 reasons:
6389 Attempting to create an array with a size that is
6390 zero or negative. */
6391 if (complain)
6392 cp_error ("creating array with size `%E'", max);
6394 return error_mark_node;
6397 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node));
6398 return build_index_type (max);
6401 case TEMPLATE_TYPE_PARM:
6402 case TEMPLATE_TEMPLATE_PARM:
6403 case TEMPLATE_PARM_INDEX:
6405 int idx;
6406 int level;
6407 int levels;
6409 r = NULL_TREE;
6411 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6412 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
6414 idx = TEMPLATE_TYPE_IDX (t);
6415 level = TEMPLATE_TYPE_LEVEL (t);
6417 else
6419 idx = TEMPLATE_PARM_IDX (t);
6420 level = TEMPLATE_PARM_LEVEL (t);
6423 if (TREE_VEC_LENGTH (args) > 0)
6425 tree arg = NULL_TREE;
6427 levels = TMPL_ARGS_DEPTH (args);
6428 if (level <= levels)
6429 arg = TMPL_ARG (args, level, idx);
6431 if (arg == error_mark_node)
6432 return error_mark_node;
6433 else if (arg != NULL_TREE)
6435 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6437 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
6438 == 't', 0);
6439 return cp_build_qualified_type_real
6440 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t),
6441 complain);
6443 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
6445 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
6447 /* We are processing a type constructed from
6448 a template template parameter */
6449 tree argvec = tsubst (TYPE_TI_ARGS (t),
6450 args, complain, in_decl);
6451 if (argvec == error_mark_node)
6452 return error_mark_node;
6454 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6455 we are resolving nested-types in the signature of
6456 a member function templates.
6457 Otherwise ARG is a TEMPLATE_DECL and is the real
6458 template to be instantiated. */
6459 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6460 arg = TYPE_NAME (arg);
6462 r = lookup_template_class (DECL_NAME (arg),
6463 argvec, in_decl,
6464 DECL_CONTEXT (arg),
6465 /*entering_scope=*/0);
6466 return cp_build_qualified_type_real (r,
6467 TYPE_QUALS (t),
6468 complain);
6470 else
6471 /* We are processing a template argument list. */
6472 return arg;
6474 else
6475 return arg;
6478 else
6479 my_friendly_abort (981018);
6481 if (level == 1)
6482 /* This can happen during the attempted tsubst'ing in
6483 unify. This means that we don't yet have any information
6484 about the template parameter in question. */
6485 return t;
6487 /* If we get here, we must have been looking at a parm for a
6488 more deeply nested template. Make a new version of this
6489 template parameter, but with a lower level. */
6490 switch (TREE_CODE (t))
6492 case TEMPLATE_TYPE_PARM:
6493 case TEMPLATE_TEMPLATE_PARM:
6494 r = copy_node (t);
6495 TEMPLATE_TYPE_PARM_INDEX (r)
6496 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6497 r, levels);
6498 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6499 TYPE_MAIN_VARIANT (r) = r;
6500 TYPE_POINTER_TO (r) = NULL_TREE;
6501 TYPE_REFERENCE_TO (r) = NULL_TREE;
6503 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6504 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
6506 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6507 complain, in_decl);
6508 if (argvec == error_mark_node)
6509 return error_mark_node;
6511 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6512 = tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
6514 break;
6516 case TEMPLATE_PARM_INDEX:
6517 r = reduce_template_parm_level (t, type, levels);
6518 break;
6520 default:
6521 my_friendly_abort (0);
6524 return r;
6527 case TREE_LIST:
6529 tree purpose, value, chain, result;
6531 if (t == void_list_node)
6532 return t;
6534 purpose = TREE_PURPOSE (t);
6535 if (purpose)
6537 purpose = tsubst (purpose, args, complain, in_decl);
6538 if (purpose == error_mark_node)
6539 return error_mark_node;
6541 value = TREE_VALUE (t);
6542 if (value)
6544 value = tsubst (value, args, complain, in_decl);
6545 if (value == error_mark_node)
6546 return error_mark_node;
6548 chain = TREE_CHAIN (t);
6549 if (chain && chain != void_type_node)
6551 chain = tsubst (chain, args, complain, in_decl);
6552 if (chain == error_mark_node)
6553 return error_mark_node;
6555 if (purpose == TREE_PURPOSE (t)
6556 && value == TREE_VALUE (t)
6557 && chain == TREE_CHAIN (t))
6558 return t;
6559 result = hash_tree_cons (purpose, value, chain);
6560 TREE_PARMLIST (result) = TREE_PARMLIST (t);
6561 return result;
6563 case TREE_VEC:
6564 if (type != NULL_TREE)
6566 /* A binfo node. We always need to make a copy, of the node
6567 itself and of its BINFO_BASETYPES. */
6569 t = copy_node (t);
6571 /* Make sure type isn't a typedef copy. */
6572 type = BINFO_TYPE (TYPE_BINFO (type));
6574 TREE_TYPE (t) = complete_type (type);
6575 if (IS_AGGR_TYPE (type))
6577 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6578 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6579 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6580 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6582 return t;
6585 /* Otherwise, a vector of template arguments. */
6586 return tsubst_template_arg_vector (t, args, complain);
6588 case POINTER_TYPE:
6589 case REFERENCE_TYPE:
6591 enum tree_code code;
6593 if (type == TREE_TYPE (t))
6594 return t;
6596 code = TREE_CODE (t);
6599 /* [temp.deduct]
6601 Type deduction may fail for any of the following
6602 reasons:
6604 -- Attempting to create a pointer to reference type.
6605 -- Attempting to create a reference to a reference type or
6606 a reference to void. */
6607 if (TREE_CODE (type) == REFERENCE_TYPE
6608 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6610 static int last_line = 0;
6611 static char* last_file = 0;
6613 /* We keep track of the last time we issued this error
6614 message to avoid spewing a ton of messages during a
6615 single bad template instantiation. */
6616 if (complain && (last_line != lineno ||
6617 last_file != input_filename))
6619 if (TREE_CODE (type) == VOID_TYPE)
6620 cp_error ("forming reference to void");
6621 else
6622 cp_error ("forming %s to reference type `%T'",
6623 (code == POINTER_TYPE) ? "pointer" : "reference",
6624 type);
6625 last_line = lineno;
6626 last_file = input_filename;
6629 return error_mark_node;
6631 else if (code == POINTER_TYPE)
6632 r = build_pointer_type (type);
6633 else
6634 r = build_reference_type (type);
6635 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6637 /* Will this ever be needed for TYPE_..._TO values? */
6638 layout_type (r);
6639 return r;
6641 case OFFSET_TYPE:
6643 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6644 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6646 /* [temp.deduct]
6648 Type deduction may fail for any of the following
6649 reasons:
6651 -- Attempting to create "pointer to member of T" when T
6652 is not a class type. */
6653 if (complain)
6654 cp_error ("creating pointer to member of non-class type `%T'",
6656 return error_mark_node;
6658 return build_offset_type (r, type);
6660 case FUNCTION_TYPE:
6661 case METHOD_TYPE:
6663 tree fntype;
6664 tree raises;
6666 fntype = tsubst_function_type (t, args, complain, in_decl);
6667 if (fntype == error_mark_node)
6668 return error_mark_node;
6670 /* Substitue the exception specification. */
6671 raises = TYPE_RAISES_EXCEPTIONS (t);
6672 if (raises)
6674 tree list = NULL_TREE;
6676 if (! TREE_VALUE (raises))
6677 list = raises;
6678 else
6679 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6681 tree spec = TREE_VALUE (raises);
6683 spec = tsubst (spec, args, complain, in_decl);
6684 if (spec == error_mark_node)
6685 return spec;
6686 list = add_exception_specifier (list, spec, complain);
6688 fntype = build_exception_variant (fntype, list);
6690 return fntype;
6692 case ARRAY_TYPE:
6694 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6695 if (domain == error_mark_node)
6696 return error_mark_node;
6698 /* As an optimization, we avoid regenerating the array type if
6699 it will obviously be the same as T. */
6700 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6701 return t;
6703 /* These checks should match the ones in grokdeclarator.
6705 [temp.deduct]
6707 The deduction may fail for any of the following reasons:
6709 -- Attempting to create an array with an element type that
6710 is void, a function type, or a reference type. */
6711 if (TREE_CODE (type) == VOID_TYPE
6712 || TREE_CODE (type) == FUNCTION_TYPE
6713 || TREE_CODE (type) == REFERENCE_TYPE)
6715 if (complain)
6716 cp_error ("creating array of `%T'", type);
6717 return error_mark_node;
6720 r = build_cplus_array_type (type, domain);
6721 return r;
6724 case PLUS_EXPR:
6725 case MINUS_EXPR:
6727 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6728 in_decl);
6729 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain,
6730 in_decl);
6732 if (e1 == error_mark_node || e2 == error_mark_node)
6733 return error_mark_node;
6735 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6738 case NEGATE_EXPR:
6739 case NOP_EXPR:
6741 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6742 in_decl);
6743 if (e == error_mark_node)
6744 return error_mark_node;
6746 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6749 case TYPENAME_TYPE:
6751 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6752 in_decl, /*entering_scope=*/1);
6753 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6754 complain, in_decl);
6756 if (ctx == error_mark_node || f == error_mark_node)
6757 return error_mark_node;
6759 if (!IS_AGGR_TYPE (ctx))
6761 if (complain)
6762 cp_error ("`%T' is not a class, struct, or union type",
6763 ctx);
6764 return error_mark_node;
6766 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6768 /* Normally, make_typename_type does not require that the CTX
6769 have complete type in order to allow things like:
6771 template <class T> struct S { typename S<T>::X Y; };
6773 But, such constructs have already been resolved by this
6774 point, so here CTX really should have complete type, unless
6775 it's a partial instantiation. */
6776 ctx = complete_type (ctx);
6777 if (!TYPE_SIZE (ctx))
6779 if (complain)
6780 incomplete_type_error (NULL_TREE, ctx);
6781 return error_mark_node;
6785 f = make_typename_type (ctx, f);
6786 if (f == error_mark_node)
6787 return f;
6788 return cp_build_qualified_type_real (f,
6789 CP_TYPE_QUALS (f)
6790 | CP_TYPE_QUALS (t),
6791 complain);
6794 case INDIRECT_REF:
6796 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6797 in_decl);
6798 if (e == error_mark_node)
6799 return error_mark_node;
6800 return make_pointer_declarator (type, e);
6803 case ADDR_EXPR:
6805 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6806 in_decl);
6807 if (e == error_mark_node)
6808 return error_mark_node;
6809 return make_reference_declarator (type, e);
6812 case ARRAY_REF:
6814 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6815 in_decl);
6816 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain,
6817 in_decl);
6818 if (e1 == error_mark_node || e2 == error_mark_node)
6819 return error_mark_node;
6821 return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr);
6824 case CALL_EXPR:
6826 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6827 in_decl);
6828 tree e2 = (tsubst_call_declarator_parms
6829 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6830 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6831 complain, in_decl);
6833 if (e1 == error_mark_node || e2 == error_mark_node
6834 || e3 == error_mark_node)
6835 return error_mark_node;
6837 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
6840 case SCOPE_REF:
6842 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6843 in_decl);
6844 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6845 if (e1 == error_mark_node || e2 == error_mark_node)
6846 return error_mark_node;
6848 return build_parse_node (TREE_CODE (t), e1, e2);
6851 case TYPEOF_TYPE:
6853 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain,
6854 in_decl);
6855 if (e1 == error_mark_node)
6856 return error_mark_node;
6858 return TREE_TYPE (e1);
6861 default:
6862 sorry ("use of `%s' in template",
6863 tree_code_name [(int) TREE_CODE (t)]);
6864 return error_mark_node;
6868 /* Like tsubst, but deals with expressions. This function just replaces
6869 template parms; to finish processing the resultant expression, use
6870 tsubst_expr. */
6872 tree
6873 tsubst_copy (t, args, complain, in_decl)
6874 tree t, args;
6875 int complain;
6876 tree in_decl;
6878 enum tree_code code;
6879 tree r;
6881 if (t == NULL_TREE || t == error_mark_node)
6882 return t;
6884 code = TREE_CODE (t);
6886 switch (code)
6888 case PARM_DECL:
6889 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6891 case CONST_DECL:
6893 tree enum_type;
6894 tree v;
6896 if (!DECL_CONTEXT (t))
6897 /* This is a global enumeration constant. */
6898 return t;
6900 /* Unfortunately, we cannot just call lookup_name here.
6901 Consider:
6903 template <int I> int f() {
6904 enum E { a = I };
6905 struct S { void g() { E e = a; } };
6908 When we instantiate f<7>::S::g(), say, lookup_name is not
6909 clever enough to find f<7>::a. */
6910 enum_type
6911 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
6912 /*entering_scope=*/0);
6914 for (v = TYPE_VALUES (enum_type);
6915 v != NULL_TREE;
6916 v = TREE_CHAIN (v))
6917 if (TREE_PURPOSE (v) == DECL_NAME (t))
6918 return TREE_VALUE (v);
6920 /* We didn't find the name. That should never happen; if
6921 name-lookup found it during preliminary parsing, we
6922 should find it again here during instantiation. */
6923 my_friendly_abort (0);
6925 return t;
6927 case FIELD_DECL:
6928 if (DECL_CONTEXT (t))
6930 tree ctx;
6932 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
6933 /*entering_scope=*/1);
6934 if (ctx != DECL_CONTEXT (t))
6935 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6937 return t;
6939 case VAR_DECL:
6940 case FUNCTION_DECL:
6941 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
6942 t = tsubst (t, args, complain, in_decl);
6943 mark_used (t);
6944 return t;
6946 case TEMPLATE_DECL:
6947 if (is_member_template (t))
6948 return tsubst (t, args, complain, in_decl);
6949 else
6950 return t;
6952 case LOOKUP_EXPR:
6954 /* We must tsbust into a LOOKUP_EXPR in case the names to
6955 which it refers is a conversion operator; in that case the
6956 name will change. We avoid making unnecessary copies,
6957 however. */
6959 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
6961 if (id != TREE_OPERAND (t, 0))
6963 r = build_nt (LOOKUP_EXPR, id);
6964 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6965 t = r;
6968 return t;
6971 case CAST_EXPR:
6972 case REINTERPRET_CAST_EXPR:
6973 case CONST_CAST_EXPR:
6974 case STATIC_CAST_EXPR:
6975 case DYNAMIC_CAST_EXPR:
6976 case NOP_EXPR:
6977 return build1
6978 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6979 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
6981 case INDIRECT_REF:
6982 case PREDECREMENT_EXPR:
6983 case PREINCREMENT_EXPR:
6984 case POSTDECREMENT_EXPR:
6985 case POSTINCREMENT_EXPR:
6986 case NEGATE_EXPR:
6987 case TRUTH_NOT_EXPR:
6988 case BIT_NOT_EXPR:
6989 case ADDR_EXPR:
6990 case CONVERT_EXPR: /* Unary + */
6991 case SIZEOF_EXPR:
6992 case ALIGNOF_EXPR:
6993 case ARROW_EXPR:
6994 case THROW_EXPR:
6995 case TYPEID_EXPR:
6996 return build1
6997 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6998 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7000 case PLUS_EXPR:
7001 case MINUS_EXPR:
7002 case MULT_EXPR:
7003 case TRUNC_DIV_EXPR:
7004 case CEIL_DIV_EXPR:
7005 case FLOOR_DIV_EXPR:
7006 case ROUND_DIV_EXPR:
7007 case EXACT_DIV_EXPR:
7008 case BIT_AND_EXPR:
7009 case BIT_ANDTC_EXPR:
7010 case BIT_IOR_EXPR:
7011 case BIT_XOR_EXPR:
7012 case TRUNC_MOD_EXPR:
7013 case FLOOR_MOD_EXPR:
7014 case TRUTH_ANDIF_EXPR:
7015 case TRUTH_ORIF_EXPR:
7016 case TRUTH_AND_EXPR:
7017 case TRUTH_OR_EXPR:
7018 case RSHIFT_EXPR:
7019 case LSHIFT_EXPR:
7020 case RROTATE_EXPR:
7021 case LROTATE_EXPR:
7022 case EQ_EXPR:
7023 case NE_EXPR:
7024 case MAX_EXPR:
7025 case MIN_EXPR:
7026 case LE_EXPR:
7027 case GE_EXPR:
7028 case LT_EXPR:
7029 case GT_EXPR:
7030 case COMPONENT_REF:
7031 case ARRAY_REF:
7032 case COMPOUND_EXPR:
7033 case SCOPE_REF:
7034 case DOTSTAR_EXPR:
7035 case MEMBER_REF:
7036 return build_nt
7037 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7038 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7040 case CALL_EXPR:
7042 tree fn = TREE_OPERAND (t, 0);
7043 if (is_overloaded_fn (fn))
7044 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
7045 else
7046 /* Sometimes FN is a LOOKUP_EXPR. */
7047 fn = tsubst_copy (fn, args, complain, in_decl);
7048 return build_nt
7049 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7050 in_decl),
7051 NULL_TREE);
7054 case METHOD_CALL_EXPR:
7056 tree name = TREE_OPERAND (t, 0);
7057 if (TREE_CODE (name) == BIT_NOT_EXPR)
7059 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7060 complain, in_decl);
7061 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7063 else if (TREE_CODE (name) == SCOPE_REF
7064 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7066 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7067 complain, in_decl);
7068 name = TREE_OPERAND (name, 1);
7069 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7070 complain, in_decl);
7071 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7072 name = build_nt (SCOPE_REF, base, name);
7074 else
7075 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7076 return build_nt
7077 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7078 complain, in_decl),
7079 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7080 NULL_TREE);
7083 case STMT_EXPR:
7084 /* This processing should really occur in tsubst_expr, However,
7085 tsubst_expr does not recurse into expressions, since it
7086 assumes that there aren't any statements inside them.
7087 Instead, it simply calls build_expr_from_tree. So, we need
7088 to expand the STMT_EXPR here. */
7089 if (!processing_template_decl)
7091 tree rtl_expr = begin_stmt_expr ();
7092 tree block = tsubst_expr (STMT_EXPR_STMT (t), args,
7093 complain, in_decl);
7094 return finish_stmt_expr (rtl_expr, block);
7097 return t;
7099 case COND_EXPR:
7100 case MODOP_EXPR:
7101 case PSEUDO_DTOR_EXPR:
7103 r = build_nt
7104 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7105 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7106 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7107 return r;
7110 case NEW_EXPR:
7112 r = build_nt
7113 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7114 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7115 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7116 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7117 return r;
7120 case DELETE_EXPR:
7122 r = build_nt
7123 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7124 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7125 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7126 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7127 return r;
7130 case TEMPLATE_ID_EXPR:
7132 /* Substituted template arguments */
7133 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7134 in_decl);
7136 if (targs && TREE_CODE (targs) == TREE_LIST)
7138 tree chain;
7139 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7140 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7142 else if (targs)
7144 int i;
7145 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7146 TREE_VEC_ELT (targs, i)
7147 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7150 return lookup_template_function
7151 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7154 case TREE_LIST:
7156 tree purpose, value, chain;
7158 if (t == void_list_node)
7159 return t;
7161 purpose = TREE_PURPOSE (t);
7162 if (purpose)
7163 purpose = tsubst_copy (purpose, args, complain, in_decl);
7164 value = TREE_VALUE (t);
7165 if (value)
7166 value = tsubst_copy (value, args, complain, in_decl);
7167 chain = TREE_CHAIN (t);
7168 if (chain && chain != void_type_node)
7169 chain = tsubst_copy (chain, args, complain, in_decl);
7170 if (purpose == TREE_PURPOSE (t)
7171 && value == TREE_VALUE (t)
7172 && chain == TREE_CHAIN (t))
7173 return t;
7174 return tree_cons (purpose, value, chain);
7177 case RECORD_TYPE:
7178 case UNION_TYPE:
7179 case ENUMERAL_TYPE:
7180 case INTEGER_TYPE:
7181 case TEMPLATE_TYPE_PARM:
7182 case TEMPLATE_TEMPLATE_PARM:
7183 case TEMPLATE_PARM_INDEX:
7184 case POINTER_TYPE:
7185 case REFERENCE_TYPE:
7186 case OFFSET_TYPE:
7187 case FUNCTION_TYPE:
7188 case METHOD_TYPE:
7189 case ARRAY_TYPE:
7190 case TYPENAME_TYPE:
7191 case TYPE_DECL:
7192 return tsubst (t, args, complain, in_decl);
7194 case IDENTIFIER_NODE:
7195 if (IDENTIFIER_TYPENAME_P (t)
7196 /* Make sure it's not just a variable named `__opr', for instance,
7197 which can occur in some existing code. */
7198 && TREE_TYPE (t))
7199 return build_typename_overload
7200 (tsubst (TREE_TYPE (t), args, complain, in_decl));
7201 else
7202 return t;
7204 case CONSTRUCTOR:
7206 r = build
7207 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7208 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7209 complain, in_decl));
7210 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7211 return r;
7214 default:
7215 return t;
7219 /* Like tsubst_copy, but also does semantic processing. */
7221 tree
7222 tsubst_expr (t, args, complain, in_decl)
7223 tree t, args;
7224 int complain;
7225 tree in_decl;
7227 tree stmt;
7229 if (t == NULL_TREE || t == error_mark_node)
7230 return t;
7232 if (processing_template_decl)
7233 return tsubst_copy (t, args, complain, in_decl);
7235 switch (TREE_CODE (t))
7237 case RETURN_INIT:
7238 prep_stmt (t);
7239 finish_named_return_value
7240 (TREE_OPERAND (t, 0),
7241 tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl));
7242 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7243 break;
7245 case CTOR_INITIALIZER:
7246 prep_stmt (t);
7247 current_member_init_list
7248 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
7249 current_base_init_list
7250 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
7251 setup_vtbl_ptr ();
7252 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7253 break;
7255 case RETURN_STMT:
7256 prep_stmt (t);
7257 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
7258 args, complain, in_decl));
7259 break;
7261 case EXPR_STMT:
7262 prep_stmt (t);
7263 /* If we're doing tsubst'ing, then we should not yet have done
7264 semantic analysisy, so we should not know that this statement
7265 assigns to the `this' pointer. */
7266 my_friendly_assert (EXPR_STMT_ASSIGNS_THIS (t) == 0,
7267 19990831);
7268 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7269 args, complain, in_decl));
7270 break;
7272 case DECL_STMT:
7274 int i = suspend_momentary ();
7275 tree decl;
7276 tree init;
7278 prep_stmt (t);
7279 decl = DECL_STMT_DECL (t);
7280 if (TREE_CODE (decl) == LABEL_DECL)
7281 finish_label_decl (DECL_NAME (decl));
7282 else
7284 init = DECL_INITIAL (decl);
7285 decl = tsubst (decl, args, complain, in_decl);
7286 init = tsubst_expr (init, args, complain, in_decl);
7287 DECL_INITIAL (decl) = init;
7288 /* By marking the declaration as instantiated, we avoid
7289 trying to instantiate it. Since instantiate_decl can't
7290 handle local variables, and since we've already done
7291 all that needs to be done, that's the right thing to
7292 do. */
7293 if (TREE_CODE (decl) == VAR_DECL)
7294 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7295 maybe_push_decl (decl);
7296 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
7297 add_decl_stmt (decl);
7299 resume_momentary (i);
7300 return decl;
7303 case FOR_STMT:
7305 tree tmp;
7306 prep_stmt (t);
7308 stmt = begin_for_stmt ();
7309 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
7310 tsubst_expr (tmp, args, complain, in_decl);
7311 finish_for_init_stmt (stmt);
7312 finish_for_cond (tsubst_expr (FOR_COND (t), args,
7313 complain, in_decl),
7314 stmt);
7315 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7316 finish_for_expr (tmp, stmt);
7317 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7318 finish_for_stmt (tmp, stmt);
7320 break;
7322 case WHILE_STMT:
7324 prep_stmt (t);
7325 stmt = begin_while_stmt ();
7326 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7327 args, complain, in_decl),
7328 stmt);
7329 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7330 finish_while_stmt (stmt);
7332 break;
7334 case DO_STMT:
7336 prep_stmt (t);
7337 stmt = begin_do_stmt ();
7338 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7339 finish_do_body (stmt);
7340 finish_do_stmt (tsubst_expr (DO_COND (t), args,
7341 complain, in_decl),
7342 stmt);
7344 break;
7346 case IF_STMT:
7348 tree tmp;
7350 prep_stmt (t);
7351 stmt = begin_if_stmt ();
7352 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7353 args, complain, in_decl),
7354 stmt);
7356 if (tmp = THEN_CLAUSE (t), tmp)
7358 tsubst_expr (tmp, args, complain, in_decl);
7359 finish_then_clause (stmt);
7362 if (tmp = ELSE_CLAUSE (t), tmp)
7364 begin_else_clause ();
7365 tsubst_expr (tmp, args, complain, in_decl);
7366 finish_else_clause (stmt);
7369 finish_if_stmt ();
7371 break;
7373 case COMPOUND_STMT:
7375 tree substmt;
7377 prep_stmt (t);
7378 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7379 for (substmt = COMPOUND_BODY (t);
7380 substmt != NULL_TREE;
7381 substmt = TREE_CHAIN (substmt))
7382 tsubst_expr (substmt, args, complain, in_decl);
7383 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7385 break;
7387 case BREAK_STMT:
7388 prep_stmt (t);
7389 finish_break_stmt ();
7390 break;
7392 case CONTINUE_STMT:
7393 prep_stmt (t);
7394 finish_continue_stmt ();
7395 break;
7397 case SWITCH_STMT:
7399 tree val;
7401 prep_stmt (t);
7402 stmt = begin_switch_stmt ();
7403 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7404 finish_switch_cond (val, stmt);
7405 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7406 finish_switch_stmt (val, stmt);
7408 break;
7410 case CASE_LABEL:
7411 prep_stmt (t);
7412 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7413 tsubst_expr (CASE_HIGH (t), args, complain, in_decl));
7414 break;
7416 case LABEL_STMT:
7417 lineno = STMT_LINENO (t);
7418 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7419 break;
7421 case GOTO_STMT:
7422 prep_stmt (t);
7423 t = GOTO_DESTINATION (t);
7424 if (TREE_CODE (t) != LABEL_DECL)
7425 /* Computed goto's must be tsubst'd into. On the other hand,
7426 non-computed gotos must not be; the identifier in question
7427 will have no binding. */
7428 t = tsubst_expr (t, args, complain, in_decl);
7429 else
7430 t = DECL_NAME (t);
7431 finish_goto_stmt (t);
7432 break;
7434 case ASM_STMT:
7435 prep_stmt (t);
7436 finish_asm_stmt (ASM_CV_QUAL (t),
7437 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7438 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7439 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7440 tsubst_expr (ASM_CLOBBERS (t), args, complain,
7441 in_decl));
7442 break;
7444 case TRY_BLOCK:
7445 prep_stmt (t);
7446 if (CLEANUP_P (t))
7448 begin_try_block ();
7449 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7450 finish_cleanup_try_block (stmt);
7451 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7452 complain, in_decl),
7453 stmt);
7455 else
7457 tree handler;
7459 if (FN_TRY_BLOCK_P (t))
7460 stmt = begin_function_try_block ();
7461 else
7462 stmt = begin_try_block ();
7464 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7466 if (FN_TRY_BLOCK_P (t))
7467 finish_function_try_block (stmt);
7468 else
7469 finish_try_block (stmt);
7471 handler = TRY_HANDLERS (t);
7472 for (; handler; handler = TREE_CHAIN (handler))
7473 tsubst_expr (handler, args, complain, in_decl);
7474 finish_handler_sequence (stmt);
7476 break;
7478 case HANDLER:
7479 prep_stmt (t);
7480 stmt = begin_handler ();
7481 if (HANDLER_PARMS (t))
7482 expand_start_catch_block
7483 (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t)),
7484 args, complain, in_decl));
7485 else
7486 expand_start_catch_block (NULL_TREE);
7487 finish_handler_parms (stmt);
7488 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7489 finish_handler (stmt);
7490 break;
7492 case TAG_DEFN:
7493 prep_stmt (t);
7494 t = TREE_TYPE (t);
7495 if (TREE_CODE (t) == ENUMERAL_TYPE)
7496 tsubst (t, args, complain, NULL_TREE);
7497 break;
7499 default:
7500 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7502 return NULL_TREE;
7505 /* Instantiate the indicated variable or function template TMPL with
7506 the template arguments in TARG_PTR. */
7508 tree
7509 instantiate_template (tmpl, targ_ptr)
7510 tree tmpl, targ_ptr;
7512 tree fndecl;
7513 tree gen_tmpl;
7514 tree spec;
7515 int i, len;
7516 struct obstack *old_fmp_obstack;
7517 extern struct obstack *function_maybepermanent_obstack;
7518 tree inner_args;
7520 if (tmpl == error_mark_node)
7521 return error_mark_node;
7523 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7525 /* Check to see if we already have this specialization. */
7526 spec = retrieve_specialization (tmpl, targ_ptr);
7527 if (spec != NULL_TREE)
7528 return spec;
7530 if (DECL_TEMPLATE_INFO (tmpl))
7532 /* The TMPL is a partial instantiation. To get a full set of
7533 arguments we must add the arguments used to perform the
7534 partial instantiation. */
7535 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7536 targ_ptr);
7537 gen_tmpl = most_general_template (tmpl);
7539 /* Check to see if we already have this specialization. */
7540 spec = retrieve_specialization (gen_tmpl, targ_ptr);
7541 if (spec != NULL_TREE)
7542 return spec;
7544 else
7545 gen_tmpl = tmpl;
7547 push_obstacks (&permanent_obstack, &permanent_obstack);
7548 old_fmp_obstack = function_maybepermanent_obstack;
7549 function_maybepermanent_obstack = &permanent_obstack;
7551 len = DECL_NTPARMS (gen_tmpl);
7552 inner_args = innermost_args (targ_ptr);
7553 i = len;
7554 while (i--)
7556 tree t = TREE_VEC_ELT (inner_args, i);
7557 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
7559 tree nt = target_type (t);
7560 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
7562 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
7563 cp_error (" trying to instantiate `%D'", gen_tmpl);
7564 fndecl = error_mark_node;
7565 goto out;
7570 /* substitute template parameters */
7571 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl);
7572 /* The DECL_TI_TEMPLATE should always be the immediate parent
7573 template, not the most general template. */
7574 DECL_TI_TEMPLATE (fndecl) = tmpl;
7576 if (flag_external_templates)
7577 add_pending_template (fndecl);
7579 out:
7580 function_maybepermanent_obstack = old_fmp_obstack;
7581 pop_obstacks ();
7583 return fndecl;
7586 /* Push the name of the class template into the scope of the instantiation. */
7588 void
7589 overload_template_name (type)
7590 tree type;
7592 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
7593 tree decl;
7595 if (IDENTIFIER_CLASS_VALUE (id)
7596 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
7597 return;
7599 decl = build_decl (TYPE_DECL, id, type);
7600 SET_DECL_ARTIFICIAL (decl);
7601 pushdecl_class_level (decl);
7604 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7605 arguments that are being used when calling it. TARGS is a vector
7606 into which the deduced template arguments are placed.
7608 Return zero for success, 2 for an incomplete match that doesn't resolve
7609 all the types, and 1 for complete failure. An error message will be
7610 printed only for an incomplete match.
7612 If FN is a conversion operator, RETURN_TYPE is the type desired as
7613 the result of the conversion operator.
7615 TPARMS is a vector of template parameters.
7617 The EXPLICIT_TARGS are explicit template arguments provided via a
7618 template-id.
7620 The parameter STRICT is one of:
7622 DEDUCE_CALL:
7623 We are deducing arguments for a function call, as in
7624 [temp.deduct.call].
7626 DEDUCE_CONV:
7627 We are deducing arguments for a conversion function, as in
7628 [temp.deduct.conv].
7630 DEDUCE_EXACT:
7631 We are deducing arguments when calculating the partial
7632 ordering between specializations of function or class
7633 templates, as in [temp.func.order] and [temp.class.order],
7634 when doing an explicit instantiation as in [temp.explicit],
7635 when determining an explicit specialization as in
7636 [temp.expl.spec], or when taking the address of a function
7637 template, as in [temp.deduct.funcaddr].
7639 The other arguments are as for type_unification. */
7642 fn_type_unification (fn, explicit_targs, targs, args, return_type,
7643 strict)
7644 tree fn, explicit_targs, targs, args, return_type;
7645 unification_kind_t strict;
7647 tree parms;
7648 tree fntype;
7649 int result;
7651 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7653 fntype = TREE_TYPE (fn);
7654 if (explicit_targs)
7656 /* [temp.deduct]
7658 The specified template arguments must match the template
7659 parameters in kind (i.e., type, nontype, template), and there
7660 must not be more arguments than there are parameters;
7661 otherwise type deduction fails.
7663 Nontype arguments must match the types of the corresponding
7664 nontype template parameters, or must be convertible to the
7665 types of the corresponding nontype parameters as specified in
7666 _temp.arg.nontype_, otherwise type deduction fails.
7668 All references in the function type of the function template
7669 to the corresponding template parameters are replaced by the
7670 specified template argument values. If a substitution in a
7671 template parameter or in the function type of the function
7672 template results in an invalid type, type deduction fails. */
7673 int i;
7674 tree converted_args;
7676 converted_args
7677 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7678 explicit_targs, NULL_TREE, /*complain=*/0,
7679 /*require_all_arguments=*/0));
7680 if (converted_args == error_mark_node)
7681 return 1;
7683 fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE);
7684 if (fntype == error_mark_node)
7685 return 1;
7687 /* Place the explicitly specified arguments in TARGS. */
7688 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7689 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
7692 parms = TYPE_ARG_TYPES (fntype);
7694 if (DECL_CONV_FN_P (fn))
7696 /* This is a template conversion operator. Use the return types
7697 as well as the argument types. We use it instead of 'this', since
7698 we could be comparing conversions from different classes. */
7699 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype),
7700 TREE_CHAIN (parms));
7701 args = tree_cons (NULL_TREE, return_type, TREE_CHAIN (args));
7704 /* We allow incomplete unification without an error message here
7705 because the standard doesn't seem to explicitly prohibit it. Our
7706 callers must be ready to deal with unification failures in any
7707 event. */
7708 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7709 targs, parms, args, /*subr=*/0,
7710 strict, /*allow_incomplete*/1);
7712 if (result == 0)
7713 /* All is well so far. Now, check:
7715 [temp.deduct]
7717 When all template arguments have been deduced, all uses of
7718 template parameters in nondeduced contexts are replaced with
7719 the corresponding deduced argument values. If the
7720 substitution results in an invalid type, as described above,
7721 type deduction fails. */
7722 if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE)
7723 == error_mark_node)
7724 return 1;
7726 return result;
7729 /* Adjust types before performing type deduction, as described in
7730 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7731 sections are symmetric. PARM is the type of a function parameter
7732 or the return type of the conversion function. ARG is the type of
7733 the argument passed to the call, or the type of the value
7734 intialized with the result of the conversion function. */
7736 static void
7737 maybe_adjust_types_for_deduction (strict, parm, arg)
7738 unification_kind_t strict;
7739 tree* parm;
7740 tree* arg;
7742 switch (strict)
7744 case DEDUCE_CALL:
7745 break;
7747 case DEDUCE_CONV:
7749 /* Swap PARM and ARG throughout the remainder of this
7750 function; the handling is precisely symmetric since PARM
7751 will initialize ARG rather than vice versa. */
7752 tree* temp = parm;
7753 parm = arg;
7754 arg = temp;
7755 break;
7758 case DEDUCE_EXACT:
7759 /* There is nothing to do in this case. */
7760 return;
7762 default:
7763 my_friendly_abort (0);
7766 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7768 /* [temp.deduct.call]
7770 If P is not a reference type:
7772 --If A is an array type, the pointer type produced by the
7773 array-to-pointer standard conversion (_conv.array_) is
7774 used in place of A for type deduction; otherwise,
7776 --If A is a function type, the pointer type produced by
7777 the function-to-pointer standard conversion
7778 (_conv.func_) is used in place of A for type deduction;
7779 otherwise,
7781 --If A is a cv-qualified type, the top level
7782 cv-qualifiers of A's type are ignored for type
7783 deduction. */
7784 if (TREE_CODE (*arg) == ARRAY_TYPE)
7785 *arg = build_pointer_type (TREE_TYPE (*arg));
7786 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
7787 *arg = build_pointer_type (*arg);
7788 else
7789 *arg = TYPE_MAIN_VARIANT (*arg);
7792 /* [temp.deduct.call]
7794 If P is a cv-qualified type, the top level cv-qualifiers
7795 of P's type are ignored for type deduction. If P is a
7796 reference type, the type referred to by P is used for
7797 type deduction. */
7798 *parm = TYPE_MAIN_VARIANT (*parm);
7799 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7800 *parm = TREE_TYPE (*parm);
7803 /* Like type_unfication.
7805 If SUBR is 1, we're being called recursively (to unify the
7806 arguments of a function or method parameter of a function
7807 template). */
7809 static int
7810 type_unification_real (tparms, targs, parms, args, subr,
7811 strict, allow_incomplete)
7812 tree tparms, targs, parms, args;
7813 int subr;
7814 unification_kind_t strict;
7815 int allow_incomplete;
7817 tree parm, arg;
7818 int i;
7819 int ntparms = TREE_VEC_LENGTH (tparms);
7820 int sub_strict;
7822 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7823 my_friendly_assert (parms == NULL_TREE
7824 || TREE_CODE (parms) == TREE_LIST, 290);
7825 /* ARGS could be NULL (via a call from parse.y to
7826 build_x_function_call). */
7827 if (args)
7828 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
7829 my_friendly_assert (ntparms > 0, 292);
7831 switch (strict)
7833 case DEDUCE_CALL:
7834 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
7835 break;
7837 case DEDUCE_CONV:
7838 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7839 break;
7841 case DEDUCE_EXACT:
7842 sub_strict = UNIFY_ALLOW_NONE;
7843 break;
7845 default:
7846 my_friendly_abort (0);
7849 while (parms
7850 && parms != void_list_node
7851 && args
7852 && args != void_list_node)
7854 parm = TREE_VALUE (parms);
7855 parms = TREE_CHAIN (parms);
7856 arg = TREE_VALUE (args);
7857 args = TREE_CHAIN (args);
7859 if (arg == error_mark_node)
7860 return 1;
7861 if (arg == unknown_type_node)
7862 /* We can't deduce anything from this, but we might get all the
7863 template args from other function args. */
7864 continue;
7866 /* Conversions will be performed on a function argument that
7867 corresponds with a function parameter that contains only
7868 non-deducible template parameters and explicitly specified
7869 template parameters. */
7870 if (! uses_template_parms (parm))
7872 tree type;
7874 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7875 type = TREE_TYPE (arg);
7876 else
7878 type = arg;
7879 arg = NULL_TREE;
7882 if (strict == DEDUCE_EXACT)
7884 if (same_type_p (parm, type))
7885 continue;
7887 else
7888 /* It might work; we shouldn't check now, because we might
7889 get into infinite recursion. Overload resolution will
7890 handle it. */
7891 continue;
7893 return 1;
7896 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7898 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
7899 if (type_unknown_p (arg))
7901 /* [temp.deduct.type] A template-argument can be deduced from
7902 a pointer to function or pointer to member function
7903 argument if the set of overloaded functions does not
7904 contain function templates and at most one of a set of
7905 overloaded functions provides a unique match. */
7907 if (resolve_overloaded_unification
7908 (tparms, targs, parm, arg, strict, sub_strict)
7909 != 0)
7910 return 1;
7911 continue;
7913 arg = TREE_TYPE (arg);
7916 if (!subr)
7917 maybe_adjust_types_for_deduction (strict, &parm, &arg);
7919 switch (unify (tparms, targs, parm, arg, sub_strict))
7921 case 0:
7922 break;
7923 case 1:
7924 return 1;
7927 /* Fail if we've reached the end of the parm list, and more args
7928 are present, and the parm list isn't variadic. */
7929 if (args && args != void_list_node && parms == void_list_node)
7930 return 1;
7931 /* Fail if parms are left and they don't have default values. */
7932 if (parms
7933 && parms != void_list_node
7934 && TREE_PURPOSE (parms) == NULL_TREE)
7935 return 1;
7936 if (!subr)
7937 for (i = 0; i < ntparms; i++)
7938 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
7940 if (!allow_incomplete)
7941 error ("incomplete type unification");
7942 return 2;
7944 return 0;
7947 /* Subroutine of type_unification_real. Args are like the variables at the
7948 call site. ARG is an overloaded function (or template-id); we try
7949 deducing template args from each of the overloads, and if only one
7950 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7952 static int
7953 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
7954 sub_strict)
7955 tree tparms, targs, parm, arg;
7956 unification_kind_t strict;
7957 int sub_strict;
7959 tree tempargs = copy_node (targs);
7960 int good = 0;
7962 if (TREE_CODE (arg) == ADDR_EXPR)
7963 arg = TREE_OPERAND (arg, 0);
7965 if (TREE_CODE (arg) == COMPONENT_REF)
7966 /* Handle `&x' where `x' is some static or non-static member
7967 function name. */
7968 arg = TREE_OPERAND (arg, 1);
7970 if (TREE_CODE (arg) == OFFSET_REF)
7971 arg = TREE_OPERAND (arg, 1);
7973 /* Strip baselink information. */
7974 while (TREE_CODE (arg) == TREE_LIST)
7975 arg = TREE_VALUE (arg);
7977 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
7979 /* If we got some explicit template args, we need to plug them into
7980 the affected templates before we try to unify, in case the
7981 explicit args will completely resolve the templates in question. */
7983 tree expl_subargs = TREE_OPERAND (arg, 1);
7984 arg = TREE_OPERAND (arg, 0);
7986 for (; arg; arg = OVL_NEXT (arg))
7988 tree fn = OVL_CURRENT (arg);
7989 tree subargs, elem;
7991 if (TREE_CODE (fn) != TEMPLATE_DECL)
7992 continue;
7994 subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
7995 if (subargs)
7997 elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0,
7998 NULL_TREE);
7999 if (TREE_CODE (elem) == METHOD_TYPE)
8000 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8001 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8002 strict, sub_strict);
8006 else if (TREE_CODE (arg) == OVERLOAD)
8008 for (; arg; arg = OVL_NEXT (arg))
8010 tree type = TREE_TYPE (OVL_CURRENT (arg));
8011 if (TREE_CODE (type) == METHOD_TYPE)
8012 type = build_ptrmemfunc_type (build_pointer_type (type));
8013 good += try_one_overload (tparms, targs, tempargs, parm,
8014 type,
8015 strict, sub_strict);
8018 else
8019 my_friendly_abort (981006);
8021 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8022 to function or pointer to member function argument if the set of
8023 overloaded functions does not contain function templates and at most
8024 one of a set of overloaded functions provides a unique match.
8026 So if we found multiple possibilities, we return success but don't
8027 deduce anything. */
8029 if (good == 1)
8031 int i = TREE_VEC_LENGTH (targs);
8032 for (; i--; )
8033 if (TREE_VEC_ELT (tempargs, i))
8034 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8036 if (good)
8037 return 0;
8039 return 1;
8042 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8043 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8044 different overloads deduce different arguments for a given parm.
8045 Returns 1 on success. */
8047 static int
8048 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8049 sub_strict)
8050 tree tparms, orig_targs, targs, parm, arg;
8051 unification_kind_t strict;
8052 int sub_strict;
8054 int nargs;
8055 tree tempargs;
8056 int i;
8058 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8059 to function or pointer to member function argument if the set of
8060 overloaded functions does not contain function templates and at most
8061 one of a set of overloaded functions provides a unique match.
8063 So if this is a template, just return success. */
8065 if (uses_template_parms (arg))
8066 return 1;
8068 maybe_adjust_types_for_deduction (strict, &parm, &arg);
8070 /* We don't copy orig_targs for this because if we have already deduced
8071 some template args from previous args, unify would complain when we
8072 try to deduce a template parameter for the same argument, even though
8073 there isn't really a conflict. */
8074 nargs = TREE_VEC_LENGTH (targs);
8075 tempargs = make_scratch_vec (nargs);
8077 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8078 return 0;
8080 /* First make sure we didn't deduce anything that conflicts with
8081 explicitly specified args. */
8082 for (i = nargs; i--; )
8084 tree elt = TREE_VEC_ELT (tempargs, i);
8085 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8087 if (elt == NULL_TREE)
8088 continue;
8089 else if (uses_template_parms (elt))
8091 /* Since we're unifying against ourselves, we will fill in template
8092 args used in the function parm list with our own template parms.
8093 Discard them. */
8094 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8095 continue;
8097 else if (oldelt && ! template_args_equal (oldelt, elt))
8098 return 0;
8101 for (i = nargs; i--; )
8103 tree elt = TREE_VEC_ELT (tempargs, i);
8105 if (elt)
8106 TREE_VEC_ELT (targs, i) = elt;
8109 return 1;
8112 /* PARM is a template class (perhaps with unbound template
8113 parameters). ARG is a fully instantiated type. If ARG can be
8114 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8115 TARGS are as for unify. */
8117 static tree
8118 try_class_unification (tparms, targs, parm, arg)
8119 tree tparms;
8120 tree targs;
8121 tree parm;
8122 tree arg;
8124 int i;
8125 tree copy_of_targs;
8127 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8128 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8129 return NULL_TREE;
8131 /* We need to make a new template argument vector for the call to
8132 unify. If we used TARGS, we'd clutter it up with the result of
8133 the attempted unification, even if this class didn't work out.
8134 We also don't want to commit ourselves to all the unifications
8135 we've already done, since unification is supposed to be done on
8136 an argument-by-argument basis. In other words, consider the
8137 following pathological case:
8139 template <int I, int J, int K>
8140 struct S {};
8142 template <int I, int J>
8143 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8145 template <int I, int J, int K>
8146 void f(S<I, J, K>, S<I, I, I>);
8148 void g() {
8149 S<0, 0, 0> s0;
8150 S<0, 1, 2> s2;
8152 f(s0, s2);
8155 Now, by the time we consider the unification involving `s2', we
8156 already know that we must have `f<0, 0, 0>'. But, even though
8157 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8158 because there are two ways to unify base classes of S<0, 1, 2>
8159 with S<I, I, I>. If we kept the already deduced knowledge, we
8160 would reject the possibility I=1. */
8161 push_momentary ();
8162 copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs));
8163 i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8164 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE);
8165 pop_momentary ();
8167 /* If unification failed, we're done. */
8168 if (i != 0)
8169 return NULL_TREE;
8170 else
8171 return arg;
8174 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8175 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
8176 for the base class of ARG that we are currently examining. */
8178 static tree
8179 get_template_base_recursive (tparms, targs, parm,
8180 arg_binfo, rval, flags)
8181 tree tparms;
8182 tree targs;
8183 tree arg_binfo;
8184 tree rval;
8185 tree parm;
8186 int flags;
8188 tree binfos;
8189 int i, n_baselinks;
8190 tree arg = BINFO_TYPE (arg_binfo);
8192 if (!(flags & GTB_IGNORE_TYPE))
8194 tree r = try_class_unification (tparms, targs,
8195 parm, arg);
8197 /* If there is more than one satisfactory baseclass, then:
8199 [temp.deduct.call]
8201 If they yield more than one possible deduced A, the type
8202 deduction fails.
8204 applies. */
8205 if (r && rval && !same_type_p (r, rval))
8206 return error_mark_node;
8207 else if (r)
8208 rval = r;
8211 binfos = BINFO_BASETYPES (arg_binfo);
8212 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8214 /* Process base types. */
8215 for (i = 0; i < n_baselinks; i++)
8217 tree base_binfo = TREE_VEC_ELT (binfos, i);
8218 int this_virtual;
8220 /* Skip this base, if we've already seen it. */
8221 if (BINFO_MARKED (base_binfo))
8222 continue;
8224 this_virtual =
8225 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8227 /* When searching for a non-virtual, we cannot mark virtually
8228 found binfos. */
8229 if (! this_virtual)
8230 SET_BINFO_MARKED (base_binfo);
8232 rval = get_template_base_recursive (tparms, targs,
8233 parm,
8234 base_binfo,
8235 rval,
8236 GTB_VIA_VIRTUAL * this_virtual);
8238 /* If we discovered more than one matching base class, we can
8239 stop now. */
8240 if (rval == error_mark_node)
8241 return error_mark_node;
8244 return rval;
8247 /* Given a template type PARM and a class type ARG, find the unique
8248 base type in ARG that is an instance of PARM. We do not examine
8249 ARG itself; only its base-classes. If there is no appropriate base
8250 class, return NULL_TREE. If there is more than one, return
8251 error_mark_node. PARM may be the type of a partial specialization,
8252 as well as a plain template type. Used by unify. */
8254 static tree
8255 get_template_base (tparms, targs, parm, arg)
8256 tree tparms;
8257 tree targs;
8258 tree parm;
8259 tree arg;
8261 tree rval;
8262 tree arg_binfo;
8264 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8266 arg_binfo = TYPE_BINFO (complete_type (arg));
8267 rval = get_template_base_recursive (tparms, targs,
8268 parm, arg_binfo,
8269 NULL_TREE,
8270 GTB_IGNORE_TYPE);
8272 /* Since get_template_base_recursive marks the bases classes, we
8273 must unmark them here. */
8274 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
8276 return rval;
8279 /* Returns the level of DECL, which declares a template parameter. */
8281 static int
8282 template_decl_level (decl)
8283 tree decl;
8285 switch (TREE_CODE (decl))
8287 case TYPE_DECL:
8288 case TEMPLATE_DECL:
8289 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8291 case PARM_DECL:
8292 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8294 default:
8295 my_friendly_abort (0);
8296 return 0;
8300 /* Decide whether ARG can be unified with PARM, considering only the
8301 cv-qualifiers of each type, given STRICT as documented for unify.
8302 Returns non-zero iff the unification is OK on that basis.*/
8304 static int
8305 check_cv_quals_for_unify (strict, arg, parm)
8306 int strict;
8307 tree arg;
8308 tree parm;
8310 if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
8311 && !at_least_as_qualified_p (arg, parm))
8312 return 0;
8314 if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
8315 && !at_least_as_qualified_p (parm, arg))
8316 return 0;
8318 return 1;
8321 /* Takes parameters as for type_unification. Returns 0 if the
8322 type deduction suceeds, 1 otherwise. The parameter STRICT is a
8323 bitwise or of the following flags:
8325 UNIFY_ALLOW_NONE:
8326 Require an exact match between PARM and ARG.
8327 UNIFY_ALLOW_MORE_CV_QUAL:
8328 Allow the deduced ARG to be more cv-qualified than ARG.
8329 UNIFY_ALLOW_LESS_CV_QUAL:
8330 Allow the deduced ARG to be less cv-qualified than ARG.
8331 UNIFY_ALLOW_DERIVED:
8332 Allow the deduced ARG to be a template base class of ARG,
8333 or a pointer to a template base class of the type pointed to by
8334 ARG.
8335 UNIFY_ALLOW_INTEGER:
8336 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8337 case for more information. */
8339 static int
8340 unify (tparms, targs, parm, arg, strict)
8341 tree tparms, targs, parm, arg;
8342 int strict;
8344 int idx;
8345 tree targ;
8346 tree tparm;
8348 /* I don't think this will do the right thing with respect to types.
8349 But the only case I've seen it in so far has been array bounds, where
8350 signedness is the only information lost, and I think that will be
8351 okay. */
8352 while (TREE_CODE (parm) == NOP_EXPR)
8353 parm = TREE_OPERAND (parm, 0);
8355 if (arg == error_mark_node)
8356 return 1;
8357 if (arg == unknown_type_node)
8358 /* We can't deduce anything from this, but we might get all the
8359 template args from other function args. */
8360 return 0;
8362 /* If PARM uses template parameters, then we can't bail out here,
8363 even if ARG == PARM, since we won't record unifications for the
8364 template parameters. We might need them if we're trying to
8365 figure out which of two things is more specialized. */
8366 if (arg == parm && !uses_template_parms (parm))
8367 return 0;
8369 /* Immediately reject some pairs that won't unify because of
8370 cv-qualification mismatches. */
8371 if (TREE_CODE (arg) == TREE_CODE (parm)
8372 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
8373 /* We check the cv-qualifiers when unifying with template type
8374 parameters below. We want to allow ARG `const T' to unify with
8375 PARM `T' for example, when computing which of two templates
8376 is more specialized, for example. */
8377 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8378 && !check_cv_quals_for_unify (strict, arg, parm))
8379 return 1;
8381 switch (TREE_CODE (parm))
8383 case TYPENAME_TYPE:
8384 /* In a type which contains a nested-name-specifier, template
8385 argument values cannot be deduced for template parameters used
8386 within the nested-name-specifier. */
8387 return 0;
8389 case TEMPLATE_TYPE_PARM:
8390 case TEMPLATE_TEMPLATE_PARM:
8391 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8393 if (TEMPLATE_TYPE_LEVEL (parm)
8394 != template_decl_level (tparm))
8395 /* The PARM is not one we're trying to unify. Just check
8396 to see if it matches ARG. */
8397 return (TREE_CODE (arg) == TREE_CODE (parm)
8398 && same_type_p (parm, arg)) ? 0 : 1;
8399 idx = TEMPLATE_TYPE_IDX (parm);
8400 targ = TREE_VEC_ELT (targs, idx);
8401 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
8403 /* Check for mixed types and values. */
8404 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8405 && TREE_CODE (tparm) != TYPE_DECL)
8406 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8407 && TREE_CODE (tparm) != TEMPLATE_DECL))
8408 return 1;
8410 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
8412 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
8414 /* We arrive here when PARM does not involve template
8415 specialization. */
8417 /* ARG must be constructed from a template class. */
8418 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
8419 return 1;
8422 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8423 tree parmvec = TYPE_TI_ARGS (parm);
8424 tree argvec = CLASSTYPE_TI_ARGS (arg);
8425 tree argtmplvec
8426 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
8427 int i;
8429 /* The parameter and argument roles have to be switched here
8430 in order to handle default arguments properly. For example,
8431 template<template <class> class TT> void f(TT<int>)
8432 should be able to accept vector<int> which comes from
8433 template <class T, class Allocator = allocator>
8434 class vector. */
8436 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8437 == error_mark_node)
8438 return 1;
8440 /* Deduce arguments T, i from TT<T> or TT<i>.
8441 We check each element of PARMVEC and ARGVEC individually
8442 rather than the whole TREE_VEC since they can have
8443 different number of elements. */
8445 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8447 tree t = TREE_VEC_ELT (parmvec, i);
8449 if (unify (tparms, targs, t,
8450 TREE_VEC_ELT (argvec, i),
8451 UNIFY_ALLOW_NONE))
8452 return 1;
8455 arg = CLASSTYPE_TI_TEMPLATE (arg);
8458 else
8460 /* If PARM is `const T' and ARG is only `int', we don't have
8461 a match unless we are allowing additional qualification.
8462 If ARG is `const int' and PARM is just `T' that's OK;
8463 that binds `const int' to `T'. */
8464 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
8465 arg, parm))
8466 return 1;
8468 /* Consider the case where ARG is `const volatile int' and
8469 PARM is `const T'. Then, T should be `volatile int'. */
8470 arg =
8471 cp_build_qualified_type_real (arg,
8472 CP_TYPE_QUALS (arg)
8473 & ~CP_TYPE_QUALS (parm),
8474 /*complain=*/0);
8475 if (arg == error_mark_node)
8476 return 1;
8479 /* Simple cases: Value already set, does match or doesn't. */
8480 if (targ != NULL_TREE && same_type_p (targ, arg))
8481 return 0;
8482 else if (targ)
8483 return 1;
8485 /* Make sure that ARG is not a variable-sized array. (Note that
8486 were talking about variable-sized arrays (like `int[n]'),
8487 rather than arrays of unknown size (like `int[]').) We'll
8488 get very confused by such a type since the bound of the array
8489 will not be computable in an instantiation. Besides, such
8490 types are not allowed in ISO C++, so we can do as we please
8491 here. */
8492 if (TREE_CODE (arg) == ARRAY_TYPE
8493 && !uses_template_parms (arg)
8494 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8495 != INTEGER_CST))
8496 return 1;
8498 TREE_VEC_ELT (targs, idx) = arg;
8499 return 0;
8501 case TEMPLATE_PARM_INDEX:
8502 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8504 if (TEMPLATE_PARM_LEVEL (parm)
8505 != template_decl_level (tparm))
8506 /* The PARM is not one we're trying to unify. Just check
8507 to see if it matches ARG. */
8508 return (TREE_CODE (arg) == TREE_CODE (parm)
8509 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
8511 idx = TEMPLATE_PARM_IDX (parm);
8512 targ = TREE_VEC_ELT (targs, idx);
8514 if (targ)
8516 int i = (cp_tree_equal (targ, arg) > 0);
8517 if (i == 1)
8518 return 0;
8519 else if (i == 0)
8520 return 1;
8521 else
8522 my_friendly_abort (42);
8525 /* [temp.deduct.type] If, in the declaration of a function template
8526 with a non-type template-parameter, the non-type
8527 template-parameter is used in an expression in the function
8528 parameter-list and, if the corresponding template-argument is
8529 deduced, the template-argument type shall match the type of the
8530 template-parameter exactly, except that a template-argument
8531 deduced from an array bound may be of any integral type. */
8532 if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
8533 /* OK */;
8534 else if ((strict & UNIFY_ALLOW_INTEGER)
8535 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
8536 || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
8537 /* OK */;
8538 else
8539 return 1;
8541 TREE_VEC_ELT (targs, idx) = arg;
8542 return 0;
8544 case POINTER_TYPE:
8546 int sub_strict;
8548 if (TREE_CODE (arg) != POINTER_TYPE)
8549 return 1;
8551 /* [temp.deduct.call]
8553 A can be another pointer or pointer to member type that can
8554 be converted to the deduced A via a qualification
8555 conversion (_conv.qual_).
8557 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8558 This will allow for additional cv-qualification of the
8559 pointed-to types if appropriate. In general, this is a bit
8560 too generous; we are only supposed to allow qualification
8561 conversions and this method will allow an ARG of char** and
8562 a deduced ARG of const char**. However, overload
8563 resolution will subsequently invalidate the candidate, so
8564 this is probably OK. */
8565 sub_strict = strict;
8567 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE)
8568 /* The derived-to-base conversion only persists through one
8569 level of pointers. */
8570 sub_strict &= ~UNIFY_ALLOW_DERIVED;
8572 return unify (tparms, targs, TREE_TYPE (parm),
8573 TREE_TYPE (arg), sub_strict);
8576 case REFERENCE_TYPE:
8577 if (TREE_CODE (arg) != REFERENCE_TYPE)
8578 return 1;
8579 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8580 UNIFY_ALLOW_NONE);
8582 case ARRAY_TYPE:
8583 if (TREE_CODE (arg) != ARRAY_TYPE)
8584 return 1;
8585 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8586 != (TYPE_DOMAIN (arg) == NULL_TREE))
8587 return 1;
8588 if (TYPE_DOMAIN (parm) != NULL_TREE
8589 && unify (tparms, targs, TYPE_DOMAIN (parm),
8590 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8591 return 1;
8592 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8593 UNIFY_ALLOW_NONE);
8595 case REAL_TYPE:
8596 case COMPLEX_TYPE:
8597 case INTEGER_TYPE:
8598 case BOOLEAN_TYPE:
8599 case VOID_TYPE:
8600 if (TREE_CODE (arg) != TREE_CODE (parm))
8601 return 1;
8603 if (TREE_CODE (parm) == INTEGER_TYPE
8604 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8606 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
8607 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
8608 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8609 return 1;
8610 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
8611 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
8612 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER))
8613 return 1;
8615 /* We use the TYPE_MAIN_VARIANT since we have already
8616 checked cv-qualification at the top of the
8617 function. */
8618 else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
8619 TYPE_MAIN_VARIANT (parm)))
8620 return 1;
8622 /* As far as unification is concerned, this wins. Later checks
8623 will invalidate it if necessary. */
8624 return 0;
8626 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8627 /* Type INTEGER_CST can come from ordinary constant template args. */
8628 case INTEGER_CST:
8629 while (TREE_CODE (arg) == NOP_EXPR)
8630 arg = TREE_OPERAND (arg, 0);
8632 if (TREE_CODE (arg) != INTEGER_CST)
8633 return 1;
8634 return !tree_int_cst_equal (parm, arg);
8636 case TREE_VEC:
8638 int i;
8639 if (TREE_CODE (arg) != TREE_VEC)
8640 return 1;
8641 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8642 return 1;
8643 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
8644 if (unify (tparms, targs,
8645 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
8646 UNIFY_ALLOW_NONE))
8647 return 1;
8648 return 0;
8651 case RECORD_TYPE:
8652 case UNION_TYPE:
8653 if (TREE_CODE (arg) != TREE_CODE (parm))
8654 return 1;
8656 if (TYPE_PTRMEMFUNC_P (parm))
8658 if (!TYPE_PTRMEMFUNC_P (arg))
8659 return 1;
8661 return unify (tparms, targs,
8662 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8663 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8664 strict);
8667 if (CLASSTYPE_TEMPLATE_INFO (parm))
8669 tree t = NULL_TREE;
8671 if (strict & UNIFY_ALLOW_DERIVED)
8673 /* First, we try to unify the PARM and ARG directly. */
8674 t = try_class_unification (tparms, targs,
8675 parm, arg);
8677 if (!t)
8679 /* Fallback to the special case allowed in
8680 [temp.deduct.call]:
8682 If P is a class, and P has the form
8683 template-id, then A can be a derived class of
8684 the deduced A. Likewise, if P is a pointer to
8685 a class of the form template-id, A can be a
8686 pointer to a derived class pointed to by the
8687 deduced A. */
8688 t = get_template_base (tparms, targs,
8689 parm, arg);
8691 if (! t || t == error_mark_node)
8692 return 1;
8695 else if (CLASSTYPE_TEMPLATE_INFO (arg)
8696 && (CLASSTYPE_TI_TEMPLATE (parm)
8697 == CLASSTYPE_TI_TEMPLATE (arg)))
8698 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8699 Then, we should unify `int' and `U'. */
8700 t = arg;
8701 else
8702 /* There's no chance of unication succeeding. */
8703 return 1;
8705 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
8706 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
8708 else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
8709 TYPE_MAIN_VARIANT (arg)))
8710 return 1;
8711 return 0;
8713 case METHOD_TYPE:
8714 case FUNCTION_TYPE:
8715 if (TREE_CODE (arg) != TREE_CODE (parm))
8716 return 1;
8718 if (unify (tparms, targs, TREE_TYPE (parm),
8719 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
8720 return 1;
8721 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
8722 TYPE_ARG_TYPES (arg), 1,
8723 DEDUCE_EXACT, 0);
8725 case OFFSET_TYPE:
8726 if (TREE_CODE (arg) != OFFSET_TYPE)
8727 return 1;
8728 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
8729 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
8730 return 1;
8731 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8732 strict);
8734 case CONST_DECL:
8735 if (arg != decl_constant_value (parm))
8736 return 1;
8737 return 0;
8739 case TEMPLATE_DECL:
8740 /* Matched cases are handled by the ARG == PARM test above. */
8741 return 1;
8743 case MINUS_EXPR:
8744 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
8746 /* We handle this case specially, since it comes up with
8747 arrays. In particular, something like:
8749 template <int N> void f(int (&x)[N]);
8751 Here, we are trying to unify the range type, which
8752 looks like [0 ... (N - 1)]. */
8753 tree t, t1, t2;
8754 t1 = TREE_OPERAND (parm, 0);
8755 t2 = TREE_OPERAND (parm, 1);
8757 /* Should this be a regular fold? */
8758 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
8759 integer_type_node,
8760 arg, t2));
8762 return unify (tparms, targs, t1, t, strict);
8764 /* else fall through */
8766 default:
8767 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
8768 /* We're looking at an expression. This can happen with
8769 something like:
8771 template <int I>
8772 void foo(S<I>, S<I + 2>);
8774 This is a "nondeduced context":
8776 [deduct.type]
8778 The nondeduced contexts are:
8780 --A type that is a template-id in which one or more of
8781 the template-arguments is an expression that references
8782 a template-parameter.
8784 In these cases, we assume deduction succeeded, but don't
8785 actually infer any unifications. */
8786 return 0;
8787 else
8788 sorry ("use of `%s' in template type unification",
8789 tree_code_name [(int) TREE_CODE (parm)]);
8791 return 1;
8795 /* Called if RESULT is explicitly instantiated, or is a member of an
8796 explicitly instantiated class, or if using -frepo and the
8797 instantiation of RESULT has been assigned to this file. */
8799 void
8800 mark_decl_instantiated (result, extern_p)
8801 tree result;
8802 int extern_p;
8804 if (TREE_CODE (result) != FUNCTION_DECL)
8805 /* The TREE_PUBLIC flag for function declarations will have been
8806 set correctly by tsubst. */
8807 TREE_PUBLIC (result) = 1;
8809 if (! extern_p)
8811 DECL_INTERFACE_KNOWN (result) = 1;
8812 DECL_NOT_REALLY_EXTERN (result) = 1;
8814 /* Always make artificials weak. */
8815 if (DECL_ARTIFICIAL (result) && flag_weak)
8816 comdat_linkage (result);
8817 /* For WIN32 we also want to put explicit instantiations in
8818 linkonce sections. */
8819 else if (TREE_PUBLIC (result))
8820 maybe_make_one_only (result);
8822 else if (TREE_CODE (result) == FUNCTION_DECL)
8823 mark_inline_for_output (result);
8826 /* Given two function templates PAT1 and PAT2, and explicit template
8827 arguments EXPLICIT_ARGS return:
8829 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8830 -1 if PAT2 is more specialized than PAT1.
8831 0 if neither is more specialized. */
8834 more_specialized (pat1, pat2, explicit_args)
8835 tree pat1, pat2, explicit_args;
8837 tree targs;
8838 int winner = 0;
8840 targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args);
8841 if (targs)
8842 --winner;
8844 targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args);
8845 if (targs)
8846 ++winner;
8848 return winner;
8851 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8853 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8854 -1 if PAT2 is more specialized than PAT1.
8855 0 if neither is more specialized. */
8858 more_specialized_class (pat1, pat2)
8859 tree pat1, pat2;
8861 tree targs;
8862 int winner = 0;
8864 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
8865 TREE_PURPOSE (pat2));
8866 if (targs)
8867 --winner;
8869 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
8870 TREE_PURPOSE (pat1));
8871 if (targs)
8872 ++winner;
8874 return winner;
8877 /* Return the template arguments that will produce the function signature
8878 DECL from the function template FN, with the explicit template
8879 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8880 also match. Return NULL_TREE if no satisfactory arguments could be
8881 found. */
8883 static tree
8884 get_bindings_real (fn, decl, explicit_args, check_rettype)
8885 tree fn, decl, explicit_args;
8886 int check_rettype;
8888 int ntparms = DECL_NTPARMS (fn);
8889 tree targs = make_scratch_vec (ntparms);
8890 tree decl_type;
8891 tree decl_arg_types;
8892 int i;
8894 /* Substitute the explicit template arguments into the type of DECL.
8895 The call to fn_type_unification will handle substitution into the
8896 FN. */
8897 decl_type = TREE_TYPE (decl);
8898 if (explicit_args && uses_template_parms (decl_type))
8900 tree tmpl;
8901 tree converted_args;
8903 if (DECL_TEMPLATE_INFO (decl))
8904 tmpl = DECL_TI_TEMPLATE (decl);
8905 else
8906 /* We can get here for some illegal specializations. */
8907 return NULL_TREE;
8909 converted_args
8910 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
8911 explicit_args, NULL_TREE,
8912 /*complain=*/0,
8913 /*require_all_arguments=*/0));
8914 if (converted_args == error_mark_node)
8915 return NULL_TREE;
8917 decl_type = tsubst (decl_type, converted_args, /*complain=*/0,
8918 NULL_TREE);
8919 if (decl_type == error_mark_node)
8920 return NULL_TREE;
8923 /* If FN is a static member function, adjust the type of DECL
8924 appropriately. */
8925 decl_arg_types = TYPE_ARG_TYPES (decl_type);
8926 if (DECL_STATIC_FUNCTION_P (fn)
8927 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
8928 decl_arg_types = TREE_CHAIN (decl_arg_types);
8930 i = fn_type_unification (fn, explicit_args, targs,
8931 decl_arg_types,
8932 TREE_TYPE (decl_type),
8933 DEDUCE_EXACT);
8935 if (i != 0)
8936 return NULL_TREE;
8938 if (check_rettype)
8940 /* Check to see that the resulting return type is also OK. */
8941 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
8942 /*complain=*/0, NULL_TREE);
8944 if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
8945 return NULL_TREE;
8948 return targs;
8951 /* For most uses, we want to check the return type. */
8953 tree
8954 get_bindings (fn, decl, explicit_args)
8955 tree fn, decl, explicit_args;
8957 return get_bindings_real (fn, decl, explicit_args, 1);
8960 /* But for more_specialized, we only care about the parameter types. */
8962 static tree
8963 get_bindings_overload (fn, decl, explicit_args)
8964 tree fn, decl, explicit_args;
8966 return get_bindings_real (fn, decl, explicit_args, 0);
8969 /* Return the innermost template arguments that, when applied to a
8970 template specialization whose innermost template parameters are
8971 TPARMS, and whose specialization arguments are ARGS, yield the
8972 ARGS.
8974 For example, suppose we have:
8976 template <class T, class U> struct S {};
8977 template <class T> struct S<T*, int> {};
8979 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8980 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8981 int}. The resulting vector will be {double}, indicating that `T'
8982 is bound to `double'. */
8984 static tree
8985 get_class_bindings (tparms, parms, args)
8986 tree tparms, parms, args;
8988 int i, ntparms = TREE_VEC_LENGTH (tparms);
8989 tree vec = make_temp_vec (ntparms);
8991 args = innermost_args (args);
8993 if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE))
8994 return NULL_TREE;
8996 for (i = 0; i < ntparms; ++i)
8997 if (! TREE_VEC_ELT (vec, i))
8998 return NULL_TREE;
9000 return vec;
9003 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9004 Pick the most specialized template, and return the corresponding
9005 instantiation, or if there is no corresponding instantiation, the
9006 template itself. EXPLICIT_ARGS is any template arguments explicity
9007 mentioned in a template-id. If there is no most specialized
9008 tempalte, error_mark_node is returned. If there are no templates
9009 at all, NULL_TREE is returned. */
9011 tree
9012 most_specialized_instantiation (instantiations, explicit_args)
9013 tree instantiations;
9014 tree explicit_args;
9016 tree fn, champ;
9017 int fate;
9019 if (!instantiations)
9020 return NULL_TREE;
9022 champ = instantiations;
9023 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
9025 fate = more_specialized (TREE_VALUE (champ),
9026 TREE_VALUE (fn), explicit_args);
9027 if (fate == 1)
9029 else
9031 if (fate == 0)
9033 fn = TREE_CHAIN (fn);
9034 if (! fn)
9035 return error_mark_node;
9037 champ = fn;
9041 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
9043 fate = more_specialized (TREE_VALUE (champ),
9044 TREE_VALUE (fn), explicit_args);
9045 if (fate != 1)
9046 return error_mark_node;
9049 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9052 /* Return the most specialized of the list of templates in FNS that can
9053 produce an instantiation matching DECL, given the explicit template
9054 arguments EXPLICIT_ARGS. */
9056 static tree
9057 most_specialized (fns, decl, explicit_args)
9058 tree fns, decl, explicit_args;
9060 tree candidates = NULL_TREE;
9061 tree fn, args;
9063 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9065 tree candidate = TREE_VALUE (fn);
9067 args = get_bindings (candidate, decl, explicit_args);
9068 if (args)
9069 candidates = tree_cons (NULL_TREE, candidate, candidates);
9072 return most_specialized_instantiation (candidates, explicit_args);
9075 /* If DECL is a specialization of some template, return the most
9076 general such template. For example, given:
9078 template <class T> struct S { template <class U> void f(U); };
9080 if TMPL is `template <class U> void S<int>::f(U)' this will return
9081 the full template. This function will not trace past partial
9082 specializations, however. For example, given in addition:
9084 template <class T> struct S<T*> { template <class U> void f(U); };
9086 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9087 `template <class T> template <class U> S<T*>::f(U)'. */
9089 static tree
9090 most_general_template (decl)
9091 tree decl;
9093 while (DECL_TEMPLATE_INFO (decl))
9094 decl = DECL_TI_TEMPLATE (decl);
9096 return decl;
9099 /* Return the most specialized of the class template specializations
9100 of TMPL which can produce an instantiation matching ARGS, or
9101 error_mark_node if the choice is ambiguous. */
9103 static tree
9104 most_specialized_class (tmpl, args)
9105 tree tmpl;
9106 tree args;
9108 tree list = NULL_TREE;
9109 tree t;
9110 tree champ;
9111 int fate;
9113 tmpl = most_general_template (tmpl);
9114 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
9116 tree spec_args
9117 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9118 if (spec_args)
9120 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9121 TREE_TYPE (list) = TREE_TYPE (t);
9125 if (! list)
9126 return NULL_TREE;
9128 t = list;
9129 champ = t;
9130 t = TREE_CHAIN (t);
9131 for (; t; t = TREE_CHAIN (t))
9133 fate = more_specialized_class (champ, t);
9134 if (fate == 1)
9136 else
9138 if (fate == 0)
9140 t = TREE_CHAIN (t);
9141 if (! t)
9142 return error_mark_node;
9144 champ = t;
9148 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9150 fate = more_specialized_class (champ, t);
9151 if (fate != 1)
9152 return error_mark_node;
9155 return champ;
9158 /* called from the parser. */
9160 void
9161 do_decl_instantiation (declspecs, declarator, storage)
9162 tree declspecs, declarator, storage;
9164 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
9165 tree result = NULL_TREE;
9166 int extern_p = 0;
9168 if (!decl)
9169 /* An error ocurred, for which grokdeclarator has already issued
9170 an appropriate message. */
9171 return;
9172 else if (! DECL_LANG_SPECIFIC (decl))
9174 cp_error ("explicit instantiation of non-template `%#D'", decl);
9175 return;
9177 else if (TREE_CODE (decl) == VAR_DECL)
9179 /* There is an asymmetry here in the way VAR_DECLs and
9180 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9181 the latter, the DECL we get back will be marked as a
9182 template instantiation, and the appropriate
9183 DECL_TEMPLATE_INFO will be set up. This does not happen for
9184 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9185 should handle VAR_DECLs as it currently handles
9186 FUNCTION_DECLs. */
9187 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9188 if (result && TREE_CODE (result) != VAR_DECL)
9190 cp_error ("no matching template for `%D' found", result);
9191 return;
9194 else if (TREE_CODE (decl) != FUNCTION_DECL)
9196 cp_error ("explicit instantiation of `%#D'", decl);
9197 return;
9199 else
9200 result = decl;
9202 /* Check for various error cases. Note that if the explicit
9203 instantiation is legal the RESULT will currently be marked as an
9204 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9205 until we get here. */
9207 if (DECL_TEMPLATE_SPECIALIZATION (result))
9209 /* [temp.spec]
9211 No program shall both explicitly instantiate and explicitly
9212 specialize a template. */
9213 cp_pedwarn ("explicit instantiation of `%#D' after", result);
9214 cp_pedwarn_at ("explicit specialization here", result);
9215 return;
9217 else if (DECL_EXPLICIT_INSTANTIATION (result))
9219 /* [temp.spec]
9221 No program shall explicitly instantiate any template more
9222 than once.
9224 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9225 instantiation was `extern' and the second is not, and EXTERN_P for
9226 the opposite case. If -frepo, chances are we already got marked
9227 as an explicit instantion because of the repo file. */
9228 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
9229 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result);
9231 /* If we've already instantiated the template, just return now. */
9232 if (DECL_INTERFACE_KNOWN (result))
9233 return;
9235 else if (!DECL_IMPLICIT_INSTANTIATION (result))
9237 cp_error ("no matching template for `%D' found", result);
9238 return;
9240 else if (!DECL_TEMPLATE_INFO (result))
9242 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
9243 return;
9246 if (flag_external_templates)
9247 return;
9249 if (storage == NULL_TREE)
9251 else if (storage == ridpointers[(int) RID_EXTERN])
9253 if (pedantic)
9254 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
9255 extern_p = 1;
9257 else
9258 cp_error ("storage class `%D' applied to template instantiation",
9259 storage);
9261 SET_DECL_EXPLICIT_INSTANTIATION (result);
9262 mark_decl_instantiated (result, extern_p);
9263 repo_template_instantiated (result, extern_p);
9264 if (! extern_p)
9265 instantiate_decl (result);
9268 void
9269 mark_class_instantiated (t, extern_p)
9270 tree t;
9271 int extern_p;
9273 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
9274 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9275 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
9276 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
9277 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9278 if (! extern_p)
9280 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9281 rest_of_type_compilation (t, 1);
9285 void
9286 do_type_instantiation (t, storage)
9287 tree t, storage;
9289 int extern_p = 0;
9290 int nomem_p = 0;
9291 int static_p = 0;
9293 if (TREE_CODE (t) == TYPE_DECL)
9294 t = TREE_TYPE (t);
9296 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
9298 cp_error ("explicit instantiation of non-template type `%T'", t);
9299 return;
9302 complete_type (t);
9304 /* With -fexternal-templates, explicit instantiations are treated the same
9305 as implicit ones. */
9306 if (flag_external_templates)
9307 return;
9309 if (TYPE_SIZE (t) == NULL_TREE)
9311 cp_error ("explicit instantiation of `%#T' before definition of template",
9313 return;
9316 if (storage != NULL_TREE)
9318 if (pedantic)
9319 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
9320 IDENTIFIER_POINTER (storage));
9322 if (storage == ridpointers[(int) RID_INLINE])
9323 nomem_p = 1;
9324 else if (storage == ridpointers[(int) RID_EXTERN])
9325 extern_p = 1;
9326 else if (storage == ridpointers[(int) RID_STATIC])
9327 static_p = 1;
9328 else
9330 cp_error ("storage class `%D' applied to template instantiation",
9331 storage);
9332 extern_p = 0;
9336 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9338 /* [temp.spec]
9340 No program shall both explicitly instantiate and explicitly
9341 specialize a template. */
9342 cp_error ("explicit instantiation of `%#T' after", t);
9343 cp_error_at ("explicit specialization here", t);
9344 return;
9346 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
9348 /* [temp.spec]
9350 No program shall explicitly instantiate any template more
9351 than once.
9353 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9354 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9355 are we already got marked as an explicit instantion because of the
9356 repo file. All these cases are OK. */
9357 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository)
9358 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t);
9360 /* If we've already instantiated the template, just return now. */
9361 if (!CLASSTYPE_INTERFACE_ONLY (t))
9362 return;
9365 mark_class_instantiated (t, extern_p);
9366 repo_template_instantiated (t, extern_p);
9368 if (nomem_p)
9369 return;
9372 tree tmp;
9374 /* In contrast to implicit instantiation, where only the
9375 declarations, and not the definitions, of members are
9376 instantiated, we have here:
9378 [temp.explicit]
9380 The explicit instantiation of a class template specialization
9381 implies the instantiation of all of its members not
9382 previously explicitly specialized in the translation unit
9383 containing the explicit instantiation.
9385 Of course, we can't instantiate member template classes, since
9386 we don't have any arguments for them. Note that the standard
9387 is unclear on whether the instatiation of the members are
9388 *explicit* instantiations or not. We choose to be generous,
9389 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9390 the explicit instantiation of a class where some of the members
9391 have no definition in the current translation unit. */
9393 if (! static_p)
9394 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
9395 if (TREE_CODE (tmp) == FUNCTION_DECL
9396 && DECL_TEMPLATE_INSTANTIATION (tmp))
9398 mark_decl_instantiated (tmp, extern_p);
9399 repo_template_instantiated (tmp, extern_p);
9400 if (! extern_p)
9401 instantiate_decl (tmp);
9404 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9405 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
9407 mark_decl_instantiated (tmp, extern_p);
9408 repo_template_instantiated (tmp, extern_p);
9409 if (! extern_p)
9410 instantiate_decl (tmp);
9413 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
9414 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9415 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
9416 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
9420 /* Given a function DECL, which is a specialization of TMPL, modify
9421 DECL to be a re-instantiation of TMPL with the same template
9422 arguments. TMPL should be the template into which tsubst'ing
9423 should occur for DECL, not the most general template.
9425 One reason for doing this is a scenario like this:
9427 template <class T>
9428 void f(const T&, int i);
9430 void g() { f(3, 7); }
9432 template <class T>
9433 void f(const T& t, const int i) { }
9435 Note that when the template is first instantiated, with
9436 instantiate_template, the resulting DECL will have no name for the
9437 first parameter, and the wrong type for the second. So, when we go
9438 to instantiate the DECL, we regenerate it. */
9440 static void
9441 regenerate_decl_from_template (decl, tmpl)
9442 tree decl;
9443 tree tmpl;
9445 tree args;
9446 tree code_pattern;
9447 tree new_decl;
9448 tree gen_tmpl;
9449 int unregistered;
9451 args = DECL_TI_ARGS (decl);
9452 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9454 /* Unregister the specialization so that when we tsubst we will not
9455 just return DECL. We don't have to unregister DECL from TMPL
9456 because if would only be registered there if it were a partial
9457 instantiation of a specialization, which it isn't: it's a full
9458 instantiation. */
9459 gen_tmpl = most_general_template (tmpl);
9460 unregistered = unregister_specialization (decl, gen_tmpl);
9462 /* If the DECL was not unregistered then something peculiar is
9463 happening: we created a specialization but did not call
9464 register_specialization for it. */
9465 my_friendly_assert (unregistered, 0);
9467 if (TREE_CODE (decl) == VAR_DECL)
9468 /* Make sure that we can see identifiers, and compute access
9469 correctly, for the class members used in the declaration of
9470 this static variable. */
9471 pushclass (DECL_CONTEXT (decl), 2);
9473 /* Do the substitution to get the new declaration. */
9474 new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE);
9476 if (TREE_CODE (decl) == VAR_DECL)
9478 /* Set up DECL_INITIAL, since tsubst doesn't. */
9479 DECL_INITIAL (new_decl) =
9480 tsubst_expr (DECL_INITIAL (code_pattern), args,
9481 /*complain=*/1, DECL_TI_TEMPLATE (decl));
9482 /* Pop the class context we pushed above. */
9483 popclass ();
9485 else if (TREE_CODE (decl) == FUNCTION_DECL)
9487 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9488 new decl. */
9489 DECL_INITIAL (new_decl) = error_mark_node;
9490 /* And don't complain about a duplicate definition. */
9491 DECL_INITIAL (decl) = NULL_TREE;
9494 /* The immediate parent of the new template is still whatever it was
9495 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9496 general template. We also reset the DECL_ASSEMBLER_NAME since
9497 tsubst always calculates the name as if the function in question
9498 were really a template instance, and sometimes, with friend
9499 functions, this is not so. See tsubst_friend_function for
9500 details. */
9501 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9502 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
9503 DECL_RTL (new_decl) = DECL_RTL (decl);
9505 /* Call duplicate decls to merge the old and new declarations. */
9506 duplicate_decls (new_decl, decl);
9508 /* Now, re-register the specialization. */
9509 register_specialization (decl, gen_tmpl, args);
9512 /* Produce the definition of D, a _DECL generated from a template. */
9514 tree
9515 instantiate_decl (d)
9516 tree d;
9518 tree tmpl = DECL_TI_TEMPLATE (d);
9519 tree args = DECL_TI_ARGS (d);
9520 tree td;
9521 tree code_pattern;
9522 tree spec;
9523 tree gen_tmpl;
9524 int nested = in_function_p ();
9525 int pattern_defined;
9526 int line = lineno;
9527 char *file = input_filename;
9529 /* This function should only be used to instantiate templates for
9530 functions and static member variables. */
9531 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9532 || TREE_CODE (d) == VAR_DECL, 0);
9534 if (DECL_TEMPLATE_INSTANTIATED (d))
9535 /* D has already been instantiated. It might seem reasonable to
9536 check whether or not D is an explict instantiation, and, if so,
9537 stop here. But when an explicit instantiation is deferred
9538 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9539 is set, even though we still need to do the instantiation. */
9540 return d;
9542 /* If we already have a specialization of this declaration, then
9543 there's no reason to instantiate it. Note that
9544 retrieve_specialization gives us both instantiations and
9545 specializations, so we must explicitly check
9546 DECL_TEMPLATE_SPECIALIZATION. */
9547 gen_tmpl = most_general_template (tmpl);
9548 spec = retrieve_specialization (gen_tmpl, args);
9549 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9550 return spec;
9552 /* This needs to happen before any tsubsting. */
9553 if (! push_tinst_level (d))
9554 return d;
9556 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9557 for the instantiation. This is not always the most general
9558 template. Consider, for example:
9560 template <class T>
9561 struct S { template <class U> void f();
9562 template <> void f<int>(); };
9564 and an instantiation of S<double>::f<int>. We want TD to be the
9565 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9566 td = tmpl;
9567 for (td = tmpl;
9568 /* An instantiation cannot have a definition, so we need a
9569 more general template. */
9570 DECL_TEMPLATE_INSTANTIATION (td)
9571 /* We must also deal with friend templates. Given:
9573 template <class T> struct S {
9574 template <class U> friend void f() {};
9577 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9578 so far as the language is concerned, but that's still
9579 where we get the pattern for the instantiation from. On
9580 ther hand, if the definition comes outside the class, say:
9582 template <class T> struct S {
9583 template <class U> friend void f();
9585 template <class U> friend void f() {}
9587 we don't need to look any further. That's what the check for
9588 DECL_INITIAL is for. */
9589 || (TREE_CODE (d) == FUNCTION_DECL
9590 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9591 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
9594 /* The present template, TD, should not be a definition. If it
9595 were a definition, we should be using it! Note that we
9596 cannot restructure the loop to just keep going until we find
9597 a template with a definition, since that might go too far if
9598 a specialization was declared, but not defined. */
9599 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9600 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9601 0);
9603 /* Fetch the more general template. */
9604 td = DECL_TI_TEMPLATE (td);
9607 code_pattern = DECL_TEMPLATE_RESULT (td);
9609 if (TREE_CODE (d) == FUNCTION_DECL)
9610 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
9611 else
9612 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
9614 push_to_top_level ();
9615 lineno = DECL_SOURCE_LINE (d);
9616 input_filename = DECL_SOURCE_FILE (d);
9618 if (pattern_defined)
9620 repo_template_used (d);
9622 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
9624 if (flag_alt_external_templates)
9626 if (interface_unknown)
9627 warn_if_unknown_interface (d);
9629 else if (DECL_INTERFACE_KNOWN (code_pattern))
9631 DECL_INTERFACE_KNOWN (d) = 1;
9632 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
9634 else
9635 warn_if_unknown_interface (code_pattern);
9638 if (at_eof)
9639 import_export_decl (d);
9642 /* Reject all external templates except inline functions. */
9643 if (DECL_INTERFACE_KNOWN (d)
9644 && ! DECL_NOT_REALLY_EXTERN (d)
9645 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
9646 goto out;
9648 if (TREE_CODE (d) == VAR_DECL
9649 && TREE_READONLY (d)
9650 && DECL_INITIAL (d) == NULL_TREE
9651 && DECL_INITIAL (code_pattern) != NULL_TREE)
9652 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9653 the variable is a static const initialized in the class body. */;
9654 else if (! pattern_defined
9655 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
9656 && ! at_eof))
9658 /* Defer all templates except inline functions used in another
9659 function. We restore the source position here because it's used
9660 by add_pending_template. */
9661 lineno = line;
9662 input_filename = file;
9664 if (at_eof && !pattern_defined
9665 && DECL_EXPLICIT_INSTANTIATION (d))
9666 /* [temp.explicit]
9668 The definition of a non-exported function template, a
9669 non-exported member function template, or a non-exported
9670 member function or static data member of a class template
9671 shall be present in every translation unit in which it is
9672 explicitly instantiated. */
9673 cp_error ("explicit instantiation of `%D' but no definition available",
9676 add_pending_template (d);
9677 goto out;
9680 /* We're now committed to instantiating this template. Mark it as
9681 instantiated so that recursive calls to instantiate_decl do not
9682 try to instantiate it again. */
9683 DECL_TEMPLATE_INSTANTIATED (d) = 1;
9685 /* Regenerate the declaration in case the template has been modified
9686 by a subsequent redeclaration. */
9687 regenerate_decl_from_template (d, td);
9689 /* We already set the file and line above. Reset them now in case
9690 they changed as a result of calling regenerate_decl_from_template. */
9691 lineno = DECL_SOURCE_LINE (d);
9692 input_filename = DECL_SOURCE_FILE (d);
9694 if (TREE_CODE (d) == VAR_DECL)
9696 DECL_IN_AGGR_P (d) = 0;
9697 if (DECL_INTERFACE_KNOWN (d))
9698 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
9699 else
9701 DECL_EXTERNAL (d) = 1;
9702 DECL_NOT_REALLY_EXTERN (d) = 1;
9704 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
9706 else if (TREE_CODE (d) == FUNCTION_DECL)
9708 /* Set up context. */
9709 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
9710 store_parm_decls ();
9712 /* Substitute into the body of the function. */
9713 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
9714 /*complain=*/1, tmpl);
9716 /* Finish the function. */
9717 expand_body (finish_function (lineno, 0));
9720 out:
9721 lineno = line;
9722 input_filename = file;
9724 pop_from_top_level ();
9725 pop_tinst_level ();
9727 return d;
9730 /* Run through the list of templates that we wish we could
9731 instantiate, and instantiate any we can. */
9734 instantiate_pending_templates ()
9736 tree *t;
9737 int instantiated_something = 0;
9738 int reconsider;
9742 reconsider = 0;
9744 t = &pending_templates;
9745 while (*t)
9747 tree srcloc = TREE_PURPOSE (*t);
9748 tree instantiation = TREE_VALUE (*t);
9750 input_filename = SRCLOC_FILE (srcloc);
9751 lineno = SRCLOC_LINE (srcloc);
9753 if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't')
9755 tree fn;
9757 if (!TYPE_SIZE (instantiation))
9759 instantiate_class_template (instantiation);
9760 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
9761 for (fn = TYPE_METHODS (instantiation);
9763 fn = TREE_CHAIN (fn))
9764 if (! DECL_ARTIFICIAL (fn))
9765 instantiate_decl (fn);
9766 if (TYPE_SIZE (instantiation))
9768 instantiated_something = 1;
9769 reconsider = 1;
9773 if (TYPE_SIZE (instantiation))
9774 /* If INSTANTIATION has been instantiated, then we don't
9775 need to consider it again in the future. */
9776 *t = TREE_CHAIN (*t);
9777 else
9778 t = &TREE_CHAIN (*t);
9780 else
9782 if (DECL_TEMPLATE_INSTANTIATION (instantiation)
9783 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
9785 instantiation = instantiate_decl (instantiation);
9786 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
9788 instantiated_something = 1;
9789 reconsider = 1;
9793 if (!DECL_TEMPLATE_INSTANTIATION (instantiation)
9794 || DECL_TEMPLATE_INSTANTIATED (instantiation))
9795 /* If INSTANTIATION has been instantiated, then we don't
9796 need to consider it again in the future. */
9797 *t = TREE_CHAIN (*t);
9798 else
9799 t = &TREE_CHAIN (*t);
9802 template_tail = t;
9804 /* Go through the things that are template instantiations if we are
9805 using guiding declarations. */
9806 t = &maybe_templates;
9807 while (*t)
9809 tree template;
9810 tree fn;
9811 tree args;
9813 fn = TREE_VALUE (*t);
9815 if (DECL_INITIAL (fn))
9816 /* If the FN is already defined, then it was either already
9817 instantiated or, even though guiding declarations were
9818 allowed, a non-template definition was provided. */
9820 else
9822 template = TREE_PURPOSE (*t);
9823 args = get_bindings (template, fn, NULL_TREE);
9824 fn = instantiate_template (template, args);
9825 instantiate_decl (fn);
9826 reconsider = 1;
9829 /* Remove this entry from the chain. */
9830 *t = TREE_CHAIN (*t);
9832 maybe_template_tail = t;
9834 while (reconsider);
9836 return instantiated_something;
9839 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9840 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9841 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9843 static tree
9844 tsubst_expr_values (t, argvec)
9845 tree t, argvec;
9847 tree first = NULL_TREE;
9848 tree *p = &first;
9850 for (; t; t = TREE_CHAIN (t))
9852 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
9853 /*complain=*/1, NULL_TREE);
9854 tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1,
9855 NULL_TREE);
9856 *p = build_tree_list (pur, val);
9857 p = &TREE_CHAIN (*p);
9859 return first;
9862 void
9863 begin_tree ()
9865 if (current_function)
9867 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
9868 last_tree = NULL_TREE;
9870 else
9871 saved_trees = tree_cons (NULL_TREE, NULL_TREE, saved_trees);
9875 void
9876 end_tree ()
9878 my_friendly_assert (saved_trees != NULL_TREE, 0);
9880 if (current_function)
9881 last_tree = TREE_VALUE (saved_trees);
9882 saved_trees = TREE_CHAIN (saved_trees);
9885 /* D is an undefined function declaration in the presence of templates with
9886 the same name, listed in FNS. If one of them can produce D as an
9887 instantiation, remember this so we can instantiate it at EOF if D has
9888 not been defined by that time. */
9890 void
9891 add_maybe_template (d, fns)
9892 tree d, fns;
9894 tree t;
9896 if (DECL_MAYBE_TEMPLATE (d))
9897 return;
9899 t = most_specialized (fns, d, NULL_TREE);
9900 if (! t)
9901 return;
9902 if (t == error_mark_node)
9904 cp_error ("ambiguous template instantiation for `%D'", d);
9905 return;
9908 *maybe_template_tail = tree_cons (t, d, NULL_TREE);
9909 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
9910 DECL_MAYBE_TEMPLATE (d) = 1;
9913 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9915 static void
9916 set_current_access_from_decl (decl)
9917 tree decl;
9919 if (TREE_PRIVATE (decl))
9920 current_access_specifier = access_private_node;
9921 else if (TREE_PROTECTED (decl))
9922 current_access_specifier = access_protected_node;
9923 else
9924 current_access_specifier = access_public_node;
9927 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9928 is the instantiation (which should have been created with
9929 start_enum) and ARGS are the template arguments to use. */
9931 static void
9932 tsubst_enum (tag, newtag, args)
9933 tree tag;
9934 tree newtag;
9935 tree args;
9937 tree e;
9939 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
9941 tree value;
9942 tree elt;
9944 /* Note that in a template enum, the TREE_VALUE is the
9945 CONST_DECL, not the corresponding INTEGER_CST. */
9946 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
9947 args, /*complain=*/1,
9948 NULL_TREE);
9950 /* Give this enumeration constant the correct access. */
9951 set_current_access_from_decl (TREE_VALUE (e));
9953 /* Actually build the enumerator itself. */
9954 elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
9956 /* We save the enumerators we have built so far in the
9957 TYPE_VALUES so that if the enumeration constants for
9958 subsequent enumerators involve those for previous ones,
9959 tsubst_copy will be able to find them. */
9960 TREE_CHAIN (elt) = TYPE_VALUES (newtag);
9961 TYPE_VALUES (newtag) = elt;
9964 finish_enum (newtag);
9967 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9968 is either an instantiation or specialization of a template
9969 function. */
9971 static void
9972 set_mangled_name_for_template_decl (decl)
9973 tree decl;
9975 tree saved_namespace;
9976 tree context = NULL_TREE;
9977 tree fn_type;
9978 tree ret_type;
9979 tree parm_types;
9980 tree tparms;
9981 tree targs;
9982 tree tmpl;
9983 int parm_depth;
9985 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
9986 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
9988 /* The names of template functions must be mangled so as to indicate
9989 what template is being specialized with what template arguments.
9990 For example, each of the following three functions must get
9991 different mangled names:
9993 void f(int);
9994 template <> void f<7>(int);
9995 template <> void f<8>(int); */
9997 targs = DECL_TI_ARGS (decl);
9998 if (uses_template_parms (targs))
9999 /* This DECL is for a partial instantiation. There's no need to
10000 mangle the name of such an entity. */
10001 return;
10003 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10004 tparms = DECL_TEMPLATE_PARMS (tmpl);
10005 parm_depth = TMPL_PARMS_DEPTH (tparms);
10007 /* There should be as many levels of arguments as there are levels
10008 of parameters. */
10009 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10011 /* We now compute the PARMS and RET_TYPE to give to
10012 build_decl_overload_real. The PARMS and RET_TYPE are the
10013 parameter and return types of the template, after all but the
10014 innermost template arguments have been substituted, not the
10015 parameter and return types of the function DECL. For example,
10016 given:
10018 template <class T> T f(T);
10020 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10021 A more subtle example is:
10023 template <class T> struct S { template <class U> void f(T, U); }
10025 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10026 {int, U}. Thus, the args that we want to subsitute into the
10027 return and parameter type for the function are those in TARGS,
10028 with the innermost level omitted. */
10029 fn_type = TREE_TYPE (tmpl);
10030 if (DECL_STATIC_FUNCTION_P (decl))
10031 context = DECL_CLASS_CONTEXT (decl);
10033 if (parm_depth == 1)
10034 /* No substitution is necessary. */
10036 else
10038 int i;
10039 tree partial_args;
10041 /* Replace the innermost level of the TARGS with NULL_TREEs to
10042 let tsubst know not to subsitute for those parameters. */
10043 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
10044 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10045 SET_TMPL_ARGS_LEVEL (partial_args, i,
10046 TMPL_ARGS_LEVEL (targs, i));
10047 SET_TMPL_ARGS_LEVEL (partial_args,
10048 TMPL_ARGS_DEPTH (targs),
10049 make_temp_vec (DECL_NTPARMS (tmpl)));
10051 /* Now, do the (partial) substitution to figure out the
10052 appropriate function type. */
10053 fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE);
10054 if (DECL_STATIC_FUNCTION_P (decl))
10055 context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE);
10057 /* Substitute into the template parameters to obtain the real
10058 innermost set of parameters. This step is important if the
10059 innermost set of template parameters contains value
10060 parameters whose types depend on outer template parameters. */
10061 TREE_VEC_LENGTH (partial_args)--;
10062 tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1);
10065 /* Now, get the innermost parameters and arguments, and figure out
10066 the parameter and return types. */
10067 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
10068 targs = innermost_args (targs);
10069 ret_type = TREE_TYPE (fn_type);
10070 parm_types = TYPE_ARG_TYPES (fn_type);
10072 /* For a static member function, we generate a fake `this' pointer,
10073 for the purposes of mangling. This indicates of which class the
10074 function is a member. Because of:
10076 [class.static]
10078 There shall not be a static and a nonstatic member function
10079 with the same name and the same parameter types
10081 we don't have to worry that this will result in a clash with a
10082 non-static member function. */
10083 if (DECL_STATIC_FUNCTION_P (decl))
10084 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
10086 /* There should be the same number of template parameters as
10087 template arguments. */
10088 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
10091 /* If the template is in a namespace, we need to put that into the
10092 mangled name. Unfortunately, build_decl_overload_real does not
10093 get the decl to mangle, so it relies on the current
10094 namespace. Therefore, we set that here temporarily. */
10095 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
10096 saved_namespace = current_namespace;
10097 current_namespace = CP_DECL_CONTEXT (decl);
10099 /* Actually set the DCL_ASSEMBLER_NAME. */
10100 DECL_ASSEMBLER_NAME (decl)
10101 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
10102 tparms, targs,
10103 DECL_FUNCTION_MEMBER_P (decl)
10104 + DECL_CONSTRUCTOR_P (decl));
10106 /* Restore the previously active namespace. */
10107 current_namespace = saved_namespace;