cp-tree.def (CPLUS_BINDING): Update documentation.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GNU CC.
7
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.
12
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.
17
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. */
22
23 /* Known bugs or deficiencies include:
24
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". */
27
28 #include "config.h"
29 #include "system.h"
30 #include "obstack.h"
31
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
43 /* The type of functions taking a tree, and some additional data, and
44 returning an int. */
45 typedef int (*tree_fn_t) PROTO((tree, void*));
46
47 extern struct obstack permanent_obstack;
48
49 extern int lineno;
50 extern char *input_filename;
51 struct pending_inline *pending_template_expansions;
52
53 tree current_template_parms;
54 HOST_WIDE_INT processing_template_decl;
55
56 tree pending_templates;
57 static tree *template_tail = &pending_templates;
58
59 tree maybe_templates;
60 static tree *maybe_template_tail = &maybe_templates;
61
62 int minimal_parse_mode;
63
64 int processing_specialization;
65 int processing_explicit_instantiation;
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static tree saved_trees;
70
71 #define obstack_chunk_alloc xmalloc
72 #define obstack_chunk_free free
73
74 #define UNIFY_ALLOW_NONE 0
75 #define UNIFY_ALLOW_MORE_CV_QUAL 1
76 #define UNIFY_ALLOW_LESS_CV_QUAL 2
77 #define UNIFY_ALLOW_DERIVED 4
78 #define UNIFY_ALLOW_INTEGER 8
79
80 static int unify PROTO((tree, tree, tree, tree, int, int*));
81 static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
82 unification_kind_t, int,
83 int*));
84 static int try_one_overload PROTO((tree, tree, tree, tree, tree,
85 unification_kind_t, int, int*));
86 static int unify PROTO((tree, tree, tree, tree, int, int*));
87 static void add_pending_template PROTO((tree));
88 static int push_tinst_level PROTO((tree));
89 static tree classtype_mangled_name PROTO((tree));
90 static char *mangle_class_name_for_template PROTO((char *, tree, tree));
91 static tree tsubst_expr_values PROTO((tree, tree));
92 static int list_eq PROTO((tree, tree));
93 static tree get_class_bindings PROTO((tree, tree, tree));
94 static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
95 static void tsubst_enum PROTO((tree, tree, tree));
96 static tree add_to_template_args PROTO((tree, tree));
97 static tree add_outermost_template_args PROTO((tree, tree));
98 static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
99 tree*));
100 static int type_unification_real PROTO((tree, tree, tree, tree,
101 int, unification_kind_t, int, int*));
102 static void note_template_header PROTO((int));
103 static tree maybe_fold_nontype_arg PROTO((tree));
104 static tree convert_nontype_argument PROTO((tree, tree));
105 static tree convert_template_argument PROTO ((tree, tree, tree, int,
106 int , tree));
107 static tree get_bindings_overload PROTO((tree, tree, tree));
108 static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
109 static tree build_template_parm_index PROTO((int, int, int, tree, tree));
110 static int inline_needs_template_parms PROTO((tree));
111 static void push_inline_template_parms_recursive PROTO((tree, int));
112 static tree retrieve_specialization PROTO((tree, tree));
113 static tree register_specialization PROTO((tree, tree, tree));
114 static int unregister_specialization PROTO((tree, tree));
115 static tree reduce_template_parm_level PROTO((tree, tree, int));
116 static tree build_template_decl PROTO((tree, tree));
117 static int mark_template_parm PROTO((tree, void *));
118 static tree tsubst_friend_function PROTO((tree, tree));
119 static tree tsubst_friend_class PROTO((tree, tree));
120 static tree get_bindings_real PROTO((tree, tree, tree, int));
121 static int template_decl_level PROTO((tree));
122 static tree maybe_get_template_decl_from_type_decl PROTO((tree));
123 static int check_cv_quals_for_unify PROTO((int, tree, tree));
124 static tree tsubst_template_arg_vector PROTO((tree, tree));
125 static tree tsubst_template_parms PROTO((tree, tree));
126 static void regenerate_decl_from_template PROTO((tree, tree));
127 static tree most_specialized PROTO((tree, tree, tree));
128 static tree most_specialized_class PROTO((tree, tree));
129 static tree most_general_template PROTO((tree));
130 static void set_mangled_name_for_template_decl PROTO((tree));
131 static int template_class_depth_real PROTO((tree, int));
132 static tree tsubst_aggr_type PROTO((tree, tree, tree, int));
133 static tree tsubst_decl PROTO((tree, tree, tree, tree));
134 static tree tsubst_arg_types PROTO((tree, tree, tree));
135 static void check_specialization_scope PROTO((void));
136 static tree process_partial_specialization PROTO((tree));
137 static void set_current_access_from_decl PROTO((tree));
138 static void check_default_tmpl_args PROTO((tree, tree, int, int));
139
140 /* We use TREE_VECs to hold template arguments. If there is only one
141 level of template arguments, then the TREE_VEC contains the
142 arguments directly. If there is more than one level of template
143 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
144 containing the template arguments for a single level. The first
145 entry in the outer TREE_VEC is the outermost level of template
146 parameters; the last is the innermost.
147
148 It is incorrect to ever form a template argument vector containing
149 only one level of arguments, but which is a TREE_VEC containing as
150 its only entry the TREE_VEC for that level. */
151
152 /* Non-zero if the template arguments is actually a vector of vectors,
153 rather than just a vector. */
154 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
155 (NODE != NULL_TREE \
156 && TREE_CODE (NODE) == TREE_VEC \
157 && TREE_VEC_LENGTH (NODE) > 0 \
158 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
159 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
160
161 /* The depth of a template argument vector. When called directly by
162 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
163 template arguments. In fact, we may even see NULL_TREE if there
164 are no template arguments. In both of those cases, there is only
165 one level of template arguments. */
166 #define TMPL_ARGS_DEPTH(NODE) \
167 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
168
169 /* The LEVELth level of the template ARGS. Note that template
170 parameter levels are indexed from 1, not from 0. */
171 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
172 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
173 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
174
175 /* Set the LEVELth level of the template ARGS to VAL. This macro does
176 not work with single-level argument vectors. */
177 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
178 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
179
180 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
181 #define TMPL_ARG(ARGS, LEVEL, IDX) \
182 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
183
184 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
185 macro does not work with single-level argument vectors. */
186 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
187 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
188
189 /* Given a single level of template arguments in NODE, return the
190 number of arguments. */
191 #define NUM_TMPL_ARGS(NODE) \
192 ((NODE) == NULL_TREE ? 0 \
193 : (TREE_CODE (NODE) == TREE_VEC \
194 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
195
196 /* The number of levels of template parameters given by NODE. */
197 #define TMPL_PARMS_DEPTH(NODE) \
198 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
199
200 /* Do any processing required when DECL (a member template declaration
201 using TEMPLATE_PARAMETERS as its innermost parameter list) is
202 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
203 it is a specialization, in which case the DECL itself is returned. */
204
205 tree
206 finish_member_template_decl (decl)
207 tree decl;
208 {
209 if (decl == NULL_TREE || decl == void_type_node)
210 return NULL_TREE;
211 else if (decl == error_mark_node)
212 /* By returning NULL_TREE, the parser will just ignore this
213 declaration. We have already issued the error. */
214 return NULL_TREE;
215 else if (TREE_CODE (decl) == TREE_LIST)
216 {
217 /* Assume that the class is the only declspec. */
218 decl = TREE_VALUE (decl);
219 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
220 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
221 {
222 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
223 check_member_template (tmpl);
224 return tmpl;
225 }
226 return NULL_TREE;
227 }
228 else if (DECL_TEMPLATE_INFO (decl))
229 {
230 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
231 {
232 check_member_template (DECL_TI_TEMPLATE (decl));
233 return DECL_TI_TEMPLATE (decl);
234 }
235 else
236 return decl;
237 }
238 else
239 cp_error ("invalid member template declaration `%D'", decl);
240
241 return error_mark_node;
242 }
243
244 /* Returns the template nesting level of the indicated class TYPE.
245
246 For example, in:
247 template <class T>
248 struct A
249 {
250 template <class U>
251 struct B {};
252 };
253
254 A<T>::B<U> has depth two, while A<T> has depth one.
255 Both A<T>::B<int> and A<int>::B<U> have depth one, if
256 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
257 specializations.
258
259 This function is guaranteed to return 0 if passed NULL_TREE so
260 that, for example, `template_class_depth (current_class_type)' is
261 always safe. */
262
263 int
264 template_class_depth_real (type, count_specializations)
265 tree type;
266 int count_specializations;
267 {
268 int depth;
269
270 for (depth = 0;
271 type && TREE_CODE (type) != NAMESPACE_DECL;
272 type = (TREE_CODE (type) == FUNCTION_DECL)
273 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
274 {
275 if (TREE_CODE (type) != FUNCTION_DECL)
276 {
277 if (CLASSTYPE_TEMPLATE_INFO (type)
278 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
279 && ((count_specializations
280 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
281 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
282 ++depth;
283 }
284 else
285 {
286 if (DECL_TEMPLATE_INFO (type)
287 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
288 && ((count_specializations
289 && DECL_TEMPLATE_SPECIALIZATION (type))
290 || uses_template_parms (DECL_TI_ARGS (type))))
291 ++depth;
292 }
293 }
294
295 return depth;
296 }
297
298 /* Returns the template nesting level of the indicated class TYPE.
299 Like template_class_depth_real, but instantiations do not count in
300 the depth. */
301
302 int
303 template_class_depth (type)
304 tree type;
305 {
306 return template_class_depth_real (type, /*count_specializations=*/0);
307 }
308
309 /* Returns 1 if processing DECL as part of do_pending_inlines
310 needs us to push template parms. */
311
312 static int
313 inline_needs_template_parms (decl)
314 tree decl;
315 {
316 if (! DECL_TEMPLATE_INFO (decl))
317 return 0;
318
319 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
320 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
321 }
322
323 /* Subroutine of maybe_begin_member_template_processing.
324 Push the template parms in PARMS, starting from LEVELS steps into the
325 chain, and ending at the beginning, since template parms are listed
326 innermost first. */
327
328 static void
329 push_inline_template_parms_recursive (parmlist, levels)
330 tree parmlist;
331 int levels;
332 {
333 tree parms = TREE_VALUE (parmlist);
334 int i;
335
336 if (levels > 1)
337 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
338
339 ++processing_template_decl;
340 current_template_parms
341 = tree_cons (build_int_2 (0, processing_template_decl),
342 parms, current_template_parms);
343 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
344
345 pushlevel (0);
346 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
347 {
348 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
349 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
350
351 switch (TREE_CODE (parm))
352 {
353 case TYPE_DECL:
354 case TEMPLATE_DECL:
355 pushdecl (parm);
356 break;
357
358 case PARM_DECL:
359 {
360 /* Make a CONST_DECL as is done in process_template_parm. */
361 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
362 TREE_TYPE (parm));
363 DECL_INITIAL (decl) = DECL_INITIAL (parm);
364 pushdecl (decl);
365 }
366 break;
367
368 default:
369 my_friendly_abort (0);
370 }
371 }
372 }
373
374 /* Restore the template parameter context for a member template or
375 a friend template defined in a class definition. */
376
377 void
378 maybe_begin_member_template_processing (decl)
379 tree decl;
380 {
381 tree parms;
382 int levels;
383
384 if (! inline_needs_template_parms (decl))
385 return;
386
387 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
388
389 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
390
391 if (DECL_TEMPLATE_SPECIALIZATION (decl))
392 {
393 --levels;
394 parms = TREE_CHAIN (parms);
395 }
396
397 push_inline_template_parms_recursive (parms, levels);
398 }
399
400 /* Undo the effects of begin_member_template_processing. */
401
402 void
403 maybe_end_member_template_processing ()
404 {
405 if (! processing_template_decl)
406 return;
407
408 while (current_template_parms
409 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
410 {
411 --processing_template_decl;
412 current_template_parms = TREE_CHAIN (current_template_parms);
413 poplevel (0, 0, 0);
414 }
415 }
416
417 /* Returns non-zero iff T is a member template function. We must be
418 careful as in
419
420 template <class T> class C { void f(); }
421
422 Here, f is a template function, and a member, but not a member
423 template. This function does not concern itself with the origin of
424 T, only its present state. So if we have
425
426 template <class T> class C { template <class U> void f(U); }
427
428 then neither C<int>::f<char> nor C<T>::f<double> is considered
429 to be a member template. But, `template <class U> void
430 C<int>::f(U)' is considered a member template. */
431
432 int
433 is_member_template (t)
434 tree t;
435 {
436 if (!DECL_FUNCTION_TEMPLATE_P (t))
437 /* Anything that isn't a function or a template function is
438 certainly not a member template. */
439 return 0;
440
441 /* A local class can't have member templates. */
442 if (hack_decl_function_context (t))
443 return 0;
444
445 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
446 /* If there are more levels of template parameters than
447 there are template classes surrounding the declaration,
448 then we have a member template. */
449 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
450 template_class_depth (DECL_CLASS_CONTEXT (t))));
451 }
452
453 #if 0 /* UNUSED */
454 /* Returns non-zero iff T is a member template class. See
455 is_member_template for a description of what precisely constitutes
456 a member template. */
457
458 int
459 is_member_template_class (t)
460 tree t;
461 {
462 if (!DECL_CLASS_TEMPLATE_P (t))
463 /* Anything that isn't a class template, is certainly not a member
464 template. */
465 return 0;
466
467 if (!DECL_CLASS_SCOPE_P (t))
468 /* Anything whose context isn't a class type is surely not a
469 member template. */
470 return 0;
471
472 /* If there are more levels of template parameters than there are
473 template classes surrounding the declaration, then we have a
474 member template. */
475 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
476 template_class_depth (DECL_CONTEXT (t)));
477 }
478 #endif
479
480 /* Return a new template argument vector which contains all of ARGS,
481 but has as its innermost set of arguments the EXTRA_ARGS. The
482 resulting vector will be built on a temporary obstack, and so must
483 be explicitly copied to the permanent obstack, if required. */
484
485 static tree
486 add_to_template_args (args, extra_args)
487 tree args;
488 tree extra_args;
489 {
490 tree new_args;
491 int extra_depth;
492 int i;
493 int j;
494
495 extra_depth = TMPL_ARGS_DEPTH (extra_args);
496 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
497
498 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
499 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
500
501 for (j = 1; j <= extra_depth; ++j, ++i)
502 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
503
504 return new_args;
505 }
506
507 /* Like add_to_template_args, but only the outermost ARGS are added to
508 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
509 (EXTRA_ARGS) levels are added. This function is used to combine
510 the template arguments from a partial instantiation with the
511 template arguments used to attain the full instantiation from the
512 partial instantiation. */
513
514 static tree
515 add_outermost_template_args (args, extra_args)
516 tree args;
517 tree extra_args;
518 {
519 tree new_args;
520
521 /* If there are more levels of EXTRA_ARGS than there are ARGS,
522 something very fishy is going on. */
523 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
524 0);
525
526 /* If *all* the new arguments will be the EXTRA_ARGS, just return
527 them. */
528 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
529 return extra_args;
530
531 /* For the moment, we make ARGS look like it contains fewer levels. */
532 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
533
534 new_args = add_to_template_args (args, extra_args);
535
536 /* Now, we restore ARGS to its full dimensions. */
537 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
538
539 return new_args;
540 }
541
542 /* We've got a template header coming up; push to a new level for storing
543 the parms. */
544
545 void
546 begin_template_parm_list ()
547 {
548 /* We use a non-tag-transparent scope here, which causes pushtag to
549 put tags in this scope, rather than in the enclosing class or
550 namespace scope. This is the right thing, since we want
551 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
552 global template class, push_template_decl handles putting the
553 TEMPLATE_DECL into top-level scope. For a nested template class,
554 e.g.:
555
556 template <class T> struct S1 {
557 template <class T> struct S2 {};
558 };
559
560 pushtag contains special code to call pushdecl_with_scope on the
561 TEMPLATE_DECL for S2. */
562 pushlevel (0);
563 declare_pseudo_global_level ();
564 ++processing_template_decl;
565 ++processing_template_parmlist;
566 note_template_header (0);
567 }
568
569 /* This routine is called when a specialization is declared. If it is
570 illegal to declare a specialization here, an error is reported. */
571
572 void
573 check_specialization_scope ()
574 {
575 tree scope = current_scope ();
576
577 /* [temp.expl.spec]
578
579 An explicit specialization shall be declared in the namespace of
580 which the template is a member, or, for member templates, in the
581 namespace of which the enclosing class or enclosing class
582 template is a member. An explicit specialization of a member
583 function, member class or static data member of a class template
584 shall be declared in the namespace of which the class template
585 is a member. */
586 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
587 cp_error ("explicit specialization in non-namespace scope `%D'",
588 scope);
589
590 /* [temp.expl.spec]
591
592 In an explicit specialization declaration for a member of a class
593 template or a member template that appears in namespace scope,
594 the member template and some of its enclosing class templates may
595 remain unspecialized, except that the declaration shall not
596 explicitly specialize a class member template if its enclosing
597 class templates are not explicitly specialized as well. */
598 if (current_template_parms)
599 cp_error ("enclosing class templates are not explicitly specialized");
600 }
601
602 /* We've just seen template <>. */
603
604 void
605 begin_specialization ()
606 {
607 note_template_header (1);
608 check_specialization_scope ();
609 }
610
611 /* Called at then end of processing a declaration preceeded by
612 template<>. */
613
614 void
615 end_specialization ()
616 {
617 reset_specialization ();
618 }
619
620 /* Any template <>'s that we have seen thus far are not referring to a
621 function specialization. */
622
623 void
624 reset_specialization ()
625 {
626 processing_specialization = 0;
627 template_header_count = 0;
628 }
629
630 /* We've just seen a template header. If SPECIALIZATION is non-zero,
631 it was of the form template <>. */
632
633 static void
634 note_template_header (specialization)
635 int specialization;
636 {
637 processing_specialization = specialization;
638 template_header_count++;
639 }
640
641 /* We're beginning an explicit instantiation. */
642
643 void
644 begin_explicit_instantiation ()
645 {
646 ++processing_explicit_instantiation;
647 }
648
649
650 void
651 end_explicit_instantiation ()
652 {
653 my_friendly_assert(processing_explicit_instantiation > 0, 0);
654 --processing_explicit_instantiation;
655 }
656
657 /* The TYPE is being declared. If it is a template type, that means it
658 is a partial specialization. Do appropriate error-checking. */
659
660 void
661 maybe_process_partial_specialization (type)
662 tree type;
663 {
664 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
665 {
666 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
667 && TYPE_SIZE (type) == NULL_TREE)
668 {
669 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
670 if (processing_template_decl)
671 push_template_decl (TYPE_MAIN_DECL (type));
672 }
673 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
674 cp_error ("specialization of `%T' after instantiation", type);
675 }
676 }
677
678 /* Retrieve the specialization (in the sense of [temp.spec] - a
679 specialization is either an instantiation or an explicit
680 specialization) of TMPL for the given template ARGS. If there is
681 no such specialization, return NULL_TREE. The ARGS are a vector of
682 arguments, or a vector of vectors of arguments, in the case of
683 templates with more than one level of parameters. */
684
685 static tree
686 retrieve_specialization (tmpl, args)
687 tree tmpl;
688 tree args;
689 {
690 tree s;
691
692 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
693
694 /* There should be as many levels of arguments as there are
695 levels of parameters. */
696 my_friendly_assert (TMPL_ARGS_DEPTH (args)
697 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
698 0);
699
700 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
701 s != NULL_TREE;
702 s = TREE_CHAIN (s))
703 if (comp_template_args (TREE_PURPOSE (s), args))
704 return TREE_VALUE (s);
705
706 return NULL_TREE;
707 }
708
709 /* Returns non-zero iff DECL is a specialization of TMPL. */
710
711 int
712 is_specialization_of (decl, tmpl)
713 tree decl;
714 tree tmpl;
715 {
716 tree t;
717
718 if (TREE_CODE (decl) == FUNCTION_DECL)
719 {
720 for (t = decl;
721 t != NULL_TREE;
722 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
723 if (t == tmpl)
724 return 1;
725 }
726 else
727 {
728 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
729
730 for (t = TREE_TYPE (decl);
731 t != NULL_TREE;
732 t = CLASSTYPE_USE_TEMPLATE (t)
733 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
734 if (same_type_p (TYPE_MAIN_VARIANT (t),
735 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
736 return 1;
737 }
738
739 return 0;
740 }
741
742 /* Register the specialization SPEC as a specialization of TMPL with
743 the indicated ARGS. Returns SPEC, or an equivalent prior
744 declaration, if available. */
745
746 static tree
747 register_specialization (spec, tmpl, args)
748 tree spec;
749 tree tmpl;
750 tree args;
751 {
752 tree s;
753
754 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
755
756 if (TREE_CODE (spec) == FUNCTION_DECL
757 && uses_template_parms (DECL_TI_ARGS (spec)))
758 /* This is the FUNCTION_DECL for a partial instantiation. Don't
759 register it; we want the corresponding TEMPLATE_DECL instead.
760 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
761 the more obvious `uses_template_parms (spec)' to avoid problems
762 with default function arguments. In particular, given
763 something like this:
764
765 template <class T> void f(T t1, T t = T())
766
767 the default argument expression is not substituted for in an
768 instantiation unless and until it is actually needed. */
769 return spec;
770
771 /* There should be as many levels of arguments as there are
772 levels of parameters. */
773 my_friendly_assert (TMPL_ARGS_DEPTH (args)
774 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
775 0);
776
777 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
778 s != NULL_TREE;
779 s = TREE_CHAIN (s))
780 if (comp_template_args (TREE_PURPOSE (s), args))
781 {
782 tree fn = TREE_VALUE (s);
783
784 if (DECL_TEMPLATE_SPECIALIZATION (spec))
785 {
786 if (DECL_TEMPLATE_INSTANTIATION (fn))
787 {
788 if (TREE_USED (fn)
789 || DECL_EXPLICIT_INSTANTIATION (fn))
790 {
791 cp_error ("specialization of %D after instantiation",
792 fn);
793 return spec;
794 }
795 else
796 {
797 /* This situation should occur only if the first
798 specialization is an implicit instantiation,
799 the second is an explicit specialization, and
800 the implicit instantiation has not yet been
801 used. That situation can occur if we have
802 implicitly instantiated a member function and
803 then specialized it later.
804
805 We can also wind up here if a friend
806 declaration that looked like an instantiation
807 turns out to be a specialization:
808
809 template <class T> void foo(T);
810 class S { friend void foo<>(int) };
811 template <> void foo(int);
812
813 We transform the existing DECL in place so that
814 any pointers to it become pointers to the
815 updated declaration.
816
817 If there was a definition for the template, but
818 not for the specialization, we want this to
819 look as if there is no definition, and vice
820 versa. */
821 DECL_INITIAL (fn) = NULL_TREE;
822 duplicate_decls (spec, fn);
823
824 return fn;
825 }
826 }
827 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
828 {
829 duplicate_decls (spec, fn);
830 return fn;
831 }
832 }
833 }
834
835 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
836 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
837
838 return spec;
839 }
840
841 /* Unregister the specialization SPEC as a specialization of TMPL.
842 Returns nonzero if the SPEC was listed as a specialization of
843 TMPL. */
844
845 static int
846 unregister_specialization (spec, tmpl)
847 tree spec;
848 tree tmpl;
849 {
850 tree* s;
851
852 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
853 *s != NULL_TREE;
854 s = &TREE_CHAIN (*s))
855 if (TREE_VALUE (*s) == spec)
856 {
857 *s = TREE_CHAIN (*s);
858 return 1;
859 }
860
861 return 0;
862 }
863
864 /* Print the list of candidate FNS in an error message. */
865
866 void
867 print_candidates (fns)
868 tree fns;
869 {
870 tree fn;
871
872 char* str = "candidates are:";
873
874 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
875 {
876 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
877 str = " ";
878 }
879 }
880
881 /* Returns the template (one of the functions given by TEMPLATE_ID)
882 which can be specialized to match the indicated DECL with the
883 explicit template args given in TEMPLATE_ID. If
884 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
885 member template. The template args (those explicitly specified and
886 those deduced) are output in a newly created vector *TARGS_OUT. If
887 it is impossible to determine the result, an error message is
888 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
889 available. */
890
891 tree
892 determine_specialization (template_id, decl, targs_out,
893 need_member_template,
894 complain)
895 tree template_id;
896 tree decl;
897 tree* targs_out;
898 int need_member_template;
899 int complain;
900 {
901 tree fns, targs_in;
902 tree templates = NULL_TREE;
903 tree fn;
904
905 *targs_out = NULL_TREE;
906
907 if (template_id == error_mark_node)
908 return error_mark_node;
909
910 fns = TREE_OPERAND (template_id, 0);
911 targs_in = TREE_OPERAND (template_id, 1);
912
913 if (fns == error_mark_node)
914 return error_mark_node;
915
916 /* Check for baselinks. */
917 if (TREE_CODE (fns) == TREE_LIST)
918 fns = TREE_VALUE (fns);
919
920 for (; fns; fns = OVL_NEXT (fns))
921 {
922 tree tmpl;
923
924 fn = OVL_CURRENT (fns);
925 if (!need_member_template
926 && TREE_CODE (fn) == FUNCTION_DECL
927 && DECL_FUNCTION_MEMBER_P (fn)
928 && DECL_USE_TEMPLATE (fn)
929 && DECL_TI_TEMPLATE (fn))
930 /* We can get here when processing something like:
931 template <class T> class X { void f(); }
932 template <> void X<int>::f() {}
933 We're specializing a member function, but not a member
934 template. */
935 tmpl = DECL_TI_TEMPLATE (fn);
936 else if (TREE_CODE (fn) != TEMPLATE_DECL
937 || (need_member_template && !is_member_template (fn)))
938 continue;
939 else
940 tmpl = fn;
941
942 if (list_length (targs_in) > DECL_NTPARMS (tmpl))
943 continue;
944
945 if (decl == NULL_TREE)
946 {
947 /* Unify against ourselves to make sure that the args we have
948 make sense and there aren't any undeducible parms. It's OK if
949 not all the parms are specified; they might be deduced
950 later. */
951 tree targs = get_bindings_overload (tmpl, DECL_RESULT (tmpl),
952 targs_in);
953
954 if (targs)
955 /* Unification was successful. */
956 templates = scratch_tree_cons (targs, tmpl, templates);
957 }
958 else
959 templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
960 }
961
962 if (decl != NULL_TREE)
963 {
964 tree tmpl = most_specialized (templates, decl, targs_in);
965 tree inner_args;
966 tree tmpl_args;
967
968 if (tmpl == error_mark_node)
969 goto ambiguous;
970 else if (tmpl == NULL_TREE)
971 goto no_match;
972
973 inner_args = get_bindings (tmpl, decl, targs_in);
974 tmpl_args = DECL_TI_ARGS (DECL_RESULT (tmpl));
975 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args))
976 {
977 *targs_out = copy_node (tmpl_args);
978 SET_TMPL_ARGS_LEVEL (*targs_out,
979 TMPL_ARGS_DEPTH (*targs_out),
980 inner_args);
981 }
982 else
983 *targs_out = inner_args;
984
985 return tmpl;
986 }
987
988 if (templates == NULL_TREE)
989 {
990 no_match:
991 if (complain)
992 {
993 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
994 template_id, decl);
995 return error_mark_node;
996 }
997 return NULL_TREE;
998 }
999 else if (TREE_CHAIN (templates) != NULL_TREE
1000 || uses_template_parms (TREE_PURPOSE (templates)))
1001 {
1002 ambiguous:
1003 if (complain)
1004 {
1005 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1006 template_id, decl);
1007 print_candidates (templates);
1008 return error_mark_node;
1009 }
1010 return NULL_TREE;
1011 }
1012
1013 /* We have one, and exactly one, match. */
1014 *targs_out = TREE_PURPOSE (templates);
1015 return TREE_VALUE (templates);
1016 }
1017
1018 /* Check to see if the function just declared, as indicated in
1019 DECLARATOR, and in DECL, is a specialization of a function
1020 template. We may also discover that the declaration is an explicit
1021 instantiation at this point.
1022
1023 Returns DECL, or an equivalent declaration that should be used
1024 instead.
1025
1026 FLAGS is a bitmask consisting of the following flags:
1027
1028 2: The function has a definition.
1029 4: The function is a friend.
1030
1031 The TEMPLATE_COUNT is the number of references to qualifying
1032 template classes that appeared in the name of the function. For
1033 example, in
1034
1035 template <class T> struct S { void f(); };
1036 void S<int>::f();
1037
1038 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1039 classes are not counted in the TEMPLATE_COUNT, so that in
1040
1041 template <class T> struct S {};
1042 template <> struct S<int> { void f(); }
1043 template <> void S<int>::f();
1044
1045 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1046 illegal; there should be no template <>.)
1047
1048 If the function is a specialization, it is marked as such via
1049 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1050 is set up correctly, and it is added to the list of specializations
1051 for that template. */
1052
1053 tree
1054 check_explicit_specialization (declarator, decl, template_count, flags)
1055 tree declarator;
1056 tree decl;
1057 int template_count;
1058 int flags;
1059 {
1060 int have_def = flags & 2;
1061 int is_friend = flags & 4;
1062 int specialization = 0;
1063 int explicit_instantiation = 0;
1064 int member_specialization = 0;
1065
1066 tree ctype = DECL_CLASS_CONTEXT (decl);
1067 tree dname = DECL_NAME (decl);
1068
1069 if (processing_specialization)
1070 {
1071 /* The last template header was of the form template <>. */
1072
1073 if (template_header_count > template_count)
1074 {
1075 /* There were more template headers than qualifying template
1076 classes. */
1077 if (template_header_count - template_count > 1)
1078 /* There shouldn't be that many template parameter
1079 lists. There can be at most one parameter list for
1080 every qualifying class, plus one for the function
1081 itself. */
1082 cp_error ("too many template parameter lists in declaration of `%D'", decl);
1083
1084 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1085 if (ctype)
1086 member_specialization = 1;
1087 else
1088 specialization = 1;
1089 }
1090 else if (template_header_count == template_count)
1091 {
1092 /* The counts are equal. So, this might be a
1093 specialization, but it is not a specialization of a
1094 member template. It might be something like
1095
1096 template <class T> struct S {
1097 void f(int i);
1098 };
1099 template <>
1100 void S<int>::f(int i) {} */
1101 specialization = 1;
1102 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1103 }
1104 else
1105 {
1106 /* This cannot be an explicit specialization. There are not
1107 enough headers for all of the qualifying classes. For
1108 example, we might have:
1109
1110 template <>
1111 void S<int>::T<char>::f();
1112
1113 But, we're missing another template <>. */
1114 cp_error("too few template parameter lists in declaration of `%D'", decl);
1115 return decl;
1116 }
1117 }
1118 else if (processing_explicit_instantiation)
1119 {
1120 if (template_header_count)
1121 cp_error ("template parameter list used in explicit instantiation");
1122
1123 if (have_def)
1124 cp_error ("definition provided for explicit instantiation");
1125
1126 explicit_instantiation = 1;
1127 }
1128 else if (ctype != NULL_TREE
1129 && !TYPE_BEING_DEFINED (ctype)
1130 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1131 && !is_friend)
1132 {
1133 /* This case catches outdated code that looks like this:
1134
1135 template <class T> struct S { void f(); };
1136 void S<int>::f() {} // Missing template <>
1137
1138 We disable this check when the type is being defined to
1139 avoid complaining about default compiler-generated
1140 constructors, destructors, and assignment operators.
1141 Since the type is an instantiation, not a specialization,
1142 these are the only functions that can be defined before
1143 the class is complete. */
1144
1145 /* If they said
1146 template <class T> void S<int>::f() {}
1147 that's bogus. */
1148 if (template_header_count)
1149 {
1150 cp_error ("template parameters specified in specialization");
1151 return decl;
1152 }
1153
1154 if (pedantic)
1155 cp_pedwarn
1156 ("explicit specialization not preceded by `template <>'");
1157 specialization = 1;
1158 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1159 }
1160 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1161 {
1162 if (is_friend)
1163 /* This could be something like:
1164
1165 template <class T> void f(T);
1166 class S { friend void f<>(int); } */
1167 specialization = 1;
1168 else
1169 {
1170 /* This case handles bogus declarations like template <>
1171 template <class T> void f<int>(); */
1172
1173 cp_error ("template-id `%D' in declaration of primary template",
1174 declarator);
1175 return decl;
1176 }
1177 }
1178
1179 if (specialization || member_specialization)
1180 {
1181 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1182 for (; t; t = TREE_CHAIN (t))
1183 if (TREE_PURPOSE (t))
1184 {
1185 cp_pedwarn
1186 ("default argument specified in explicit specialization");
1187 break;
1188 }
1189 if (current_lang_name == lang_name_c)
1190 cp_error ("template specialization with C linkage");
1191 }
1192
1193 if (specialization || member_specialization || explicit_instantiation)
1194 {
1195 tree gen_tmpl;
1196 tree tmpl = NULL_TREE;
1197 tree targs = NULL_TREE;
1198
1199 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1200 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1201 {
1202 tree fns;
1203
1204 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1205 0);
1206 if (!ctype)
1207 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1208 else
1209 fns = dname;
1210
1211 declarator =
1212 lookup_template_function (fns, NULL_TREE);
1213 }
1214
1215 if (declarator == error_mark_node)
1216 return error_mark_node;
1217
1218 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1219 {
1220 /* A friend declaration. We can't do much, because we don't
1221 know what this resolves to, yet. */
1222 my_friendly_assert (is_friend != 0, 0);
1223 my_friendly_assert (!explicit_instantiation, 0);
1224 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1225 return decl;
1226 }
1227
1228 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1229 {
1230 if (!explicit_instantiation)
1231 /* A specialization in class scope. This is illegal,
1232 but the error will already have been flagged by
1233 check_specialization_scope. */
1234 return error_mark_node;
1235 else
1236 {
1237 /* It's not legal to write an explicit instantiation in
1238 class scope, e.g.:
1239
1240 class C { template void f(); }
1241
1242 This case is caught by the parser. However, on
1243 something like:
1244
1245 template class C { void f(); };
1246
1247 (which is illegal) we can get here. The error will be
1248 issued later. */
1249 ;
1250 }
1251
1252 return decl;
1253 }
1254 else if (ctype != NULL_TREE
1255 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1256 IDENTIFIER_NODE))
1257 {
1258 /* Find the list of functions in ctype that have the same
1259 name as the declared function. */
1260 tree name = TREE_OPERAND (declarator, 0);
1261 tree fns;
1262
1263 if (name == constructor_name (ctype)
1264 || name == constructor_name_full (ctype))
1265 {
1266 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1267
1268 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1269 : !TYPE_HAS_DESTRUCTOR (ctype))
1270 {
1271 /* From [temp.expl.spec]:
1272
1273 If such an explicit specialization for the member
1274 of a class template names an implicitly-declared
1275 special member function (clause _special_), the
1276 program is ill-formed.
1277
1278 Similar language is found in [temp.explicit]. */
1279 cp_error ("specialization of implicitly-declared special member function");
1280
1281 return decl;
1282 }
1283
1284 name = is_constructor ? ctor_identifier : dtor_identifier;
1285 }
1286
1287 fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
1288
1289 if (fns == NULL_TREE)
1290 {
1291 cp_error ("no member function `%s' declared in `%T'",
1292 IDENTIFIER_POINTER (name),
1293 ctype);
1294 return decl;
1295 }
1296 else
1297 TREE_OPERAND (declarator, 0) = fns;
1298 }
1299
1300 /* Figure out what exactly is being specialized at this point.
1301 Note that for an explicit instantiation, even one for a
1302 member function, we cannot tell apriori whether the
1303 instantiation is for a member template, or just a member
1304 function of a template class. Even if a member template is
1305 being instantiated, the member template arguments may be
1306 elided if they can be deduced from the rest of the
1307 declaration. */
1308 tmpl = determine_specialization (declarator, decl,
1309 &targs,
1310 member_specialization,
1311 1);
1312
1313 if (tmpl && tmpl != error_mark_node)
1314 {
1315 gen_tmpl = most_general_template (tmpl);
1316
1317 if (explicit_instantiation)
1318 {
1319 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1320 is done by do_decl_instantiation later. */
1321 decl = instantiate_template (tmpl, innermost_args (targs));
1322 return decl;
1323 }
1324
1325 /* If we though that the DECL was a member function, but it
1326 turns out to be specializing a static member function,
1327 make DECL a static member function as well. */
1328 if (DECL_STATIC_FUNCTION_P (tmpl)
1329 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1330 {
1331 revert_static_member_fn (&decl, 0, 0);
1332 last_function_parms = TREE_CHAIN (last_function_parms);
1333 }
1334
1335 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1336 DECL_TEMPLATE_INFO (decl)
1337 = perm_tree_cons (tmpl, targs, NULL_TREE);
1338
1339 /* Mangle the function name appropriately. Note that we do
1340 not mangle specializations of non-template member
1341 functions of template classes, e.g. with
1342
1343 template <class T> struct S { void f(); }
1344
1345 and given the specialization
1346
1347 template <> void S<int>::f() {}
1348
1349 we do not mangle S<int>::f() here. That's because it's
1350 just an ordinary member function and doesn't need special
1351 treatment. We do this here so that the ordinary,
1352 non-template, name-mangling algorith will not be used
1353 later. */
1354 if ((is_member_template (tmpl) || ctype == NULL_TREE)
1355 && name_mangling_version >= 1)
1356 set_mangled_name_for_template_decl (decl);
1357
1358 if (is_friend && !have_def)
1359 /* This is not really a declaration of a specialization.
1360 It's just the name of an instantiation. But, it's not
1361 a request for an instantiation, either. */
1362 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1363
1364 /* Register this specialization so that we can find it
1365 again. */
1366 decl = register_specialization (decl, gen_tmpl, targs);
1367
1368 return decl;
1369 }
1370 }
1371
1372 return decl;
1373 }
1374
1375 /* TYPE is being declared. Verify that the use of template headers
1376 and such is reasonable. Issue error messages if not. */
1377
1378 void
1379 maybe_check_template_type (type)
1380 tree type;
1381 {
1382 if (template_header_count)
1383 {
1384 /* We are in the scope of some `template <...>' header. */
1385
1386 int context_depth
1387 = template_class_depth_real (TYPE_CONTEXT (type),
1388 /*count_specializations=*/1);
1389
1390 if (template_header_count <= context_depth)
1391 /* This is OK; the template headers are for the context. We
1392 are actually too lenient here; like
1393 check_explicit_specialization we should consider the number
1394 of template types included in the actual declaration. For
1395 example,
1396
1397 template <class T> struct S {
1398 template <class U> template <class V>
1399 struct I {};
1400 };
1401
1402 is illegal, but:
1403
1404 template <class T> struct S {
1405 template <class U> struct I;
1406 };
1407
1408 template <class T> template <class U.
1409 struct S<T>::I {};
1410
1411 is not. */
1412 ;
1413 else if (template_header_count > context_depth + 1)
1414 /* There are two many template parameter lists. */
1415 cp_error ("too many template parameter lists in declaration of `%T'", type);
1416 }
1417 }
1418
1419 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1420 parameters. These are represented in the same format used for
1421 DECL_TEMPLATE_PARMS. */
1422
1423 int comp_template_parms (parms1, parms2)
1424 tree parms1;
1425 tree parms2;
1426 {
1427 tree p1;
1428 tree p2;
1429
1430 if (parms1 == parms2)
1431 return 1;
1432
1433 for (p1 = parms1, p2 = parms2;
1434 p1 != NULL_TREE && p2 != NULL_TREE;
1435 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1436 {
1437 tree t1 = TREE_VALUE (p1);
1438 tree t2 = TREE_VALUE (p2);
1439 int i;
1440
1441 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1442 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1443
1444 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1445 return 0;
1446
1447 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1448 {
1449 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1450 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1451
1452 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1453 return 0;
1454
1455 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1456 continue;
1457 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1458 return 0;
1459 }
1460 }
1461
1462 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1463 /* One set of parameters has more parameters lists than the
1464 other. */
1465 return 0;
1466
1467 return 1;
1468 }
1469
1470
1471 /* Returns 1 iff DECL is a template parameter. */
1472
1473 int decl_template_parm_p (decl)
1474 tree decl;
1475 {
1476 /* For template template parms. */
1477 if (TREE_CODE (decl) == TEMPLATE_DECL
1478 && TREE_TYPE (decl)
1479 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
1480 return 1;
1481
1482 /* For template type parms. */
1483 if (TREE_CODE (decl) == TYPE_DECL
1484 && TREE_TYPE (decl)
1485 && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
1486 return 1;
1487
1488 /* For template non-type parms. */
1489 if (TREE_CODE (decl) == CONST_DECL
1490 && DECL_INITIAL (decl)
1491 && TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
1492 return 1;
1493
1494 return 0;
1495 }
1496
1497 /* Complain if DECL shadows a template parameter.
1498
1499 [temp.local]: A template-parameter shall not be redeclared within its
1500 scope (including nested scopes). */
1501
1502 void
1503 check_template_shadow (decl)
1504 tree decl;
1505 {
1506 tree olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1507
1508 if (current_template_parms && olddecl)
1509 {
1510 /* We check for decl != olddecl to avoid bogus errors for using a
1511 name inside a class. We check TPFI to avoid duplicate errors for
1512 inline member templates. */
1513 if (decl != olddecl && decl_template_parm_p (olddecl)
1514 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1515 {
1516 cp_error_at ("declaration of `%#D'", decl);
1517 cp_error_at (" shadows template parm `%#D'", olddecl);
1518 }
1519 }
1520 }
1521
1522 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1523 ORIG_LEVEL, DECL, and TYPE. */
1524
1525 static tree
1526 build_template_parm_index (index, level, orig_level, decl, type)
1527 int index;
1528 int level;
1529 int orig_level;
1530 tree decl;
1531 tree type;
1532 {
1533 tree t = make_node (TEMPLATE_PARM_INDEX);
1534 TEMPLATE_PARM_IDX (t) = index;
1535 TEMPLATE_PARM_LEVEL (t) = level;
1536 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1537 TEMPLATE_PARM_DECL (t) = decl;
1538 TREE_TYPE (t) = type;
1539
1540 return t;
1541 }
1542
1543 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1544 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1545 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1546 new one is created. */
1547
1548 static tree
1549 reduce_template_parm_level (index, type, levels)
1550 tree index;
1551 tree type;
1552 int levels;
1553 {
1554 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1555 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1556 != TEMPLATE_PARM_LEVEL (index) - levels))
1557 {
1558 tree decl
1559 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1560 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1561 type);
1562 tree t
1563 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1564 TEMPLATE_PARM_LEVEL (index) - levels,
1565 TEMPLATE_PARM_ORIG_LEVEL (index),
1566 decl, type);
1567 TEMPLATE_PARM_DESCENDANTS (index) = t;
1568
1569 /* Template template parameters need this. */
1570 DECL_TEMPLATE_PARMS (decl)
1571 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1572 }
1573
1574 return TEMPLATE_PARM_DESCENDANTS (index);
1575 }
1576
1577 /* Process information from new template parameter NEXT and append it to the
1578 LIST being built. */
1579
1580 tree
1581 process_template_parm (list, next)
1582 tree list, next;
1583 {
1584 tree parm;
1585 tree decl = 0;
1586 tree defval;
1587 int is_type, idx;
1588
1589 parm = next;
1590 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1591 defval = TREE_PURPOSE (parm);
1592 parm = TREE_VALUE (parm);
1593 is_type = TREE_PURPOSE (parm) == class_type_node;
1594
1595 if (list)
1596 {
1597 tree p = TREE_VALUE (tree_last (list));
1598
1599 if (TREE_CODE (p) == TYPE_DECL)
1600 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1601 else if (TREE_CODE (p) == TEMPLATE_DECL)
1602 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
1603 else
1604 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1605 ++idx;
1606 }
1607 else
1608 idx = 0;
1609
1610 if (!is_type)
1611 {
1612 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1613 /* is a const-param */
1614 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1615 PARM, 0, NULL_TREE);
1616 /* A template parameter is not modifiable. */
1617 TREE_READONLY (parm) = 1;
1618 if (IS_AGGR_TYPE (TREE_TYPE (parm))
1619 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1620 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
1621 {
1622 cp_error ("`%#T' is not a valid type for a template constant parameter",
1623 TREE_TYPE (parm));
1624 if (DECL_NAME (parm) == NULL_TREE)
1625 error (" a template type parameter must begin with `class' or `typename'");
1626 TREE_TYPE (parm) = void_type_node;
1627 }
1628 else if (pedantic
1629 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1630 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
1631 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1632 TREE_TYPE (parm));
1633 if (TREE_PERMANENT (parm) == 0)
1634 {
1635 parm = copy_node (parm);
1636 TREE_PERMANENT (parm) = 1;
1637 }
1638 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1639 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1640 = build_template_parm_index (idx, processing_template_decl,
1641 processing_template_decl,
1642 decl, TREE_TYPE (parm));
1643 }
1644 else
1645 {
1646 tree t;
1647 parm = TREE_VALUE (parm);
1648
1649 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1650 {
1651 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1652 /* This is for distinguishing between real templates and template
1653 template parameters */
1654 TREE_TYPE (parm) = t;
1655 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1656 decl = parm;
1657 }
1658 else
1659 {
1660 t = make_lang_type (TEMPLATE_TYPE_PARM);
1661 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1662 decl = build_decl (TYPE_DECL, parm, t);
1663 }
1664
1665 TYPE_NAME (t) = decl;
1666 TYPE_STUB_DECL (t) = decl;
1667 parm = decl;
1668 TEMPLATE_TYPE_PARM_INDEX (t)
1669 = build_template_parm_index (idx, processing_template_decl,
1670 processing_template_decl,
1671 decl, TREE_TYPE (parm));
1672 }
1673 SET_DECL_ARTIFICIAL (decl);
1674 pushdecl (decl);
1675 parm = build_tree_list (defval, parm);
1676 return chainon (list, parm);
1677 }
1678
1679 /* The end of a template parameter list has been reached. Process the
1680 tree list into a parameter vector, converting each parameter into a more
1681 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1682 as PARM_DECLs. */
1683
1684 tree
1685 end_template_parm_list (parms)
1686 tree parms;
1687 {
1688 int nparms;
1689 tree parm;
1690 tree saved_parmlist = make_tree_vec (list_length (parms));
1691
1692 current_template_parms
1693 = tree_cons (build_int_2 (0, processing_template_decl),
1694 saved_parmlist, current_template_parms);
1695
1696 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
1697 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
1698
1699 --processing_template_parmlist;
1700
1701 return saved_parmlist;
1702 }
1703
1704 /* end_template_decl is called after a template declaration is seen. */
1705
1706 void
1707 end_template_decl ()
1708 {
1709 reset_specialization ();
1710
1711 if (! processing_template_decl)
1712 return;
1713
1714 /* This matches the pushlevel in begin_template_parm_list. */
1715 poplevel (0, 0, 0);
1716
1717 --processing_template_decl;
1718 current_template_parms = TREE_CHAIN (current_template_parms);
1719 (void) get_pending_sizes (); /* Why? */
1720 }
1721
1722 /* Given a template argument vector containing the template PARMS.
1723 The innermost PARMS are given first. */
1724
1725 tree
1726 current_template_args ()
1727 {
1728 tree header;
1729 tree args = NULL_TREE;
1730 int length = TMPL_PARMS_DEPTH (current_template_parms);
1731 int l = length;
1732
1733 /* If there is only one level of template parameters, we do not
1734 create a TREE_VEC of TREE_VECs. Instead, we return a single
1735 TREE_VEC containing the arguments. */
1736 if (length > 1)
1737 args = make_tree_vec (length);
1738
1739 for (header = current_template_parms; header; header = TREE_CHAIN (header))
1740 {
1741 tree a = copy_node (TREE_VALUE (header));
1742 int i;
1743
1744 TREE_TYPE (a) = NULL_TREE;
1745 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
1746 {
1747 tree t = TREE_VEC_ELT (a, i);
1748
1749 /* T will be a list if we are called from within a
1750 begin/end_template_parm_list pair, but a vector directly
1751 if within a begin/end_member_template_processing pair. */
1752 if (TREE_CODE (t) == TREE_LIST)
1753 {
1754 t = TREE_VALUE (t);
1755
1756 if (TREE_CODE (t) == TYPE_DECL
1757 || TREE_CODE (t) == TEMPLATE_DECL)
1758 t = TREE_TYPE (t);
1759 else
1760 t = DECL_INITIAL (t);
1761 TREE_VEC_ELT (a, i) = t;
1762 }
1763 }
1764
1765 if (length > 1)
1766 TREE_VEC_ELT (args, --l) = a;
1767 else
1768 args = a;
1769 }
1770
1771 return args;
1772 }
1773
1774 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1775 template PARMS. Used by push_template_decl below. */
1776
1777 static tree
1778 build_template_decl (decl, parms)
1779 tree decl;
1780 tree parms;
1781 {
1782 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1783 DECL_TEMPLATE_PARMS (tmpl) = parms;
1784 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1785 if (DECL_LANG_SPECIFIC (decl))
1786 {
1787 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
1788 DECL_STATIC_FUNCTION_P (tmpl) =
1789 DECL_STATIC_FUNCTION_P (decl);
1790 }
1791
1792 return tmpl;
1793 }
1794
1795 struct template_parm_data
1796 {
1797 /* The level of the template parameters we are currently
1798 processing. */
1799 int level;
1800
1801 /* The index of the specialization argument we are currently
1802 processing. */
1803 int current_arg;
1804
1805 /* An array whose size is the number of template parameters. The
1806 elements are non-zero if the parameter has been used in any one
1807 of the arguments processed so far. */
1808 int* parms;
1809
1810 /* An array whose size is the number of template arguments. The
1811 elements are non-zero if the argument makes use of template
1812 parameters of this level. */
1813 int* arg_uses_template_parms;
1814 };
1815
1816 /* Subroutine of push_template_decl used to see if each template
1817 parameter in a partial specialization is used in the explicit
1818 argument list. If T is of the LEVEL given in DATA (which is
1819 treated as a template_parm_data*), then DATA->PARMS is marked
1820 appropriately. */
1821
1822 static int
1823 mark_template_parm (t, data)
1824 tree t;
1825 void* data;
1826 {
1827 int level;
1828 int idx;
1829 struct template_parm_data* tpd = (struct template_parm_data*) data;
1830
1831 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
1832 {
1833 level = TEMPLATE_PARM_LEVEL (t);
1834 idx = TEMPLATE_PARM_IDX (t);
1835 }
1836 else
1837 {
1838 level = TEMPLATE_TYPE_LEVEL (t);
1839 idx = TEMPLATE_TYPE_IDX (t);
1840 }
1841
1842 if (level == tpd->level)
1843 {
1844 tpd->parms[idx] = 1;
1845 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
1846 }
1847
1848 /* Return zero so that for_each_template_parm will continue the
1849 traversal of the tree; we want to mark *every* template parm. */
1850 return 0;
1851 }
1852
1853 /* Process the partial specialization DECL. */
1854
1855 tree
1856 process_partial_specialization (decl)
1857 tree decl;
1858 {
1859 tree type = TREE_TYPE (decl);
1860 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
1861 tree specargs = CLASSTYPE_TI_ARGS (type);
1862 tree inner_args = innermost_args (specargs);
1863 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
1864 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
1865 int nargs = TREE_VEC_LENGTH (inner_args);
1866 int ntparms = TREE_VEC_LENGTH (inner_parms);
1867 int i;
1868 int did_error_intro = 0;
1869 struct template_parm_data tpd;
1870 struct template_parm_data tpd2;
1871
1872 /* We check that each of the template parameters given in the
1873 partial specialization is used in the argument list to the
1874 specialization. For example:
1875
1876 template <class T> struct S;
1877 template <class T> struct S<T*>;
1878
1879 The second declaration is OK because `T*' uses the template
1880 parameter T, whereas
1881
1882 template <class T> struct S<int>;
1883
1884 is no good. Even trickier is:
1885
1886 template <class T>
1887 struct S1
1888 {
1889 template <class U>
1890 struct S2;
1891 template <class U>
1892 struct S2<T>;
1893 };
1894
1895 The S2<T> declaration is actually illegal; it is a
1896 full-specialization. Of course,
1897
1898 template <class U>
1899 struct S2<T (*)(U)>;
1900
1901 or some such would have been OK. */
1902 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
1903 tpd.parms = alloca (sizeof (int) * ntparms);
1904 bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
1905
1906 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
1907 bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs);
1908 for (i = 0; i < nargs; ++i)
1909 {
1910 tpd.current_arg = i;
1911 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
1912 &mark_template_parm,
1913 &tpd);
1914 }
1915 for (i = 0; i < ntparms; ++i)
1916 if (tpd.parms[i] == 0)
1917 {
1918 /* One of the template parms was not used in the
1919 specialization. */
1920 if (!did_error_intro)
1921 {
1922 cp_error ("template parameters not used in partial specialization:");
1923 did_error_intro = 1;
1924 }
1925
1926 cp_error (" `%D'",
1927 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
1928 }
1929
1930 /* [temp.class.spec]
1931
1932 The argument list of the specialization shall not be identical to
1933 the implicit argument list of the primary template. */
1934 if (comp_template_args (inner_args,
1935 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
1936 (maintmpl)))))
1937 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
1938
1939 /* [temp.class.spec]
1940
1941 A partially specialized non-type argument expression shall not
1942 involve template parameters of the partial specialization except
1943 when the argument expression is a simple identifier.
1944
1945 The type of a template parameter corresponding to a specialized
1946 non-type argument shall not be dependent on a parameter of the
1947 specialization. */
1948 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
1949 tpd2.parms = 0;
1950 for (i = 0; i < nargs; ++i)
1951 {
1952 tree arg = TREE_VEC_ELT (inner_args, i);
1953 if (/* These first two lines are the `non-type' bit. */
1954 TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
1955 && TREE_CODE (arg) != TEMPLATE_DECL
1956 /* This next line is the `argument expression is not just a
1957 simple identifier' condition and also the `specialized
1958 non-type argument' bit. */
1959 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
1960 {
1961 if (tpd.arg_uses_template_parms[i])
1962 cp_error ("template argument `%E' involves template parameter(s)", arg);
1963 else
1964 {
1965 /* Look at the corresponding template parameter,
1966 marking which template parameters its type depends
1967 upon. */
1968 tree type =
1969 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
1970 i)));
1971
1972 if (!tpd2.parms)
1973 {
1974 /* We haven't yet initialized TPD2. Do so now. */
1975 tpd2.arg_uses_template_parms
1976 = (int*) alloca (sizeof (int) * nargs);
1977 /* The number of parameters here is the number in the
1978 main template, which, as checked in the assertion
1979 above, is NARGS. */
1980 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
1981 tpd2.level =
1982 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
1983 }
1984
1985 /* Mark the template parameters. But this time, we're
1986 looking for the template parameters of the main
1987 template, not in the specialization. */
1988 tpd2.current_arg = i;
1989 tpd2.arg_uses_template_parms[i] = 0;
1990 bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
1991 for_each_template_parm (type,
1992 &mark_template_parm,
1993 &tpd2);
1994
1995 if (tpd2.arg_uses_template_parms [i])
1996 {
1997 /* The type depended on some template parameters.
1998 If they are fully specialized in the
1999 specialization, that's OK. */
2000 int j;
2001 for (j = 0; j < nargs; ++j)
2002 if (tpd2.parms[j] != 0
2003 && tpd.arg_uses_template_parms [j])
2004 {
2005 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2006 type,
2007 arg);
2008 break;
2009 }
2010 }
2011 }
2012 }
2013 }
2014
2015 if (retrieve_specialization (maintmpl, specargs))
2016 /* We've already got this specialization. */
2017 return decl;
2018
2019 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
2020 = perm_tree_cons (inner_args, inner_parms,
2021 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2022 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2023 return decl;
2024 }
2025
2026 /* Check that a template declaration's use of default arguments is not
2027 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2028 non-zero if DECL is the thing declared by a primary template.
2029 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2030
2031 static void
2032 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2033 tree decl;
2034 tree parms;
2035 int is_primary;
2036 int is_partial;
2037 {
2038 char* msg;
2039 int last_level_to_check;
2040
2041 /* [temp.param]
2042
2043 A default template-argument shall not be specified in a
2044 function template declaration or a function template definition, nor
2045 in the template-parameter-list of the definition of a member of a
2046 class template. */
2047
2048 if (current_class_type
2049 && !TYPE_BEING_DEFINED (current_class_type)
2050 && DECL_REAL_CONTEXT (decl) == current_class_type
2051 && DECL_DEFINED_IN_CLASS_P (decl))
2052 /* We already checked these parameters when the template was
2053 declared, so there's no need to do it again now. This is an
2054 inline member function definition. */
2055 return;
2056
2057 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2058 /* For an ordinary class template, default template arguments are
2059 allowed at the innermost level, e.g.:
2060 template <class T = int>
2061 struct S {};
2062 but, in a partial specialization, they're not allowed even
2063 there, as we have in [temp.class.spec]:
2064
2065 The template parameter list of a specialization shall not
2066 contain default template argument values.
2067
2068 So, for a partial specialization, or for a function template,
2069 we look at all of them. */
2070 ;
2071 else
2072 /* But, for a primary class template that is not a partial
2073 specialization we look at all template parameters except the
2074 innermost ones. */
2075 parms = TREE_CHAIN (parms);
2076
2077 /* Figure out what error message to issue. */
2078 if (TREE_CODE (decl) == FUNCTION_DECL)
2079 msg = "default argument for template parameter in function template `%D'";
2080 else if (is_partial)
2081 msg = "default argument in partial specialization `%D'";
2082 else
2083 msg = "default argument for template parameter for class enclosing `%D'";
2084
2085 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2086 /* If we're inside a class definition, there's no need to
2087 examine the parameters to the class itself. On the one
2088 hand, they will be checked when the class is defined, and,
2089 on the other, default arguments are legal in things like:
2090 template <class T = double>
2091 struct S { template <class U> void f(U); };
2092 Here the default argument for `S' has no bearing on the
2093 declaration of `f'. */
2094 last_level_to_check = template_class_depth (current_class_type) + 1;
2095 else
2096 /* Check everything. */
2097 last_level_to_check = 0;
2098
2099 for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check;
2100 parms = TREE_CHAIN (parms))
2101 {
2102 tree inner_parms = TREE_VALUE (parms);
2103 int i, ntparms;
2104
2105 ntparms = TREE_VEC_LENGTH (inner_parms);
2106 for (i = 0; i < ntparms; ++i)
2107 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2108 {
2109 if (msg)
2110 {
2111 cp_error (msg, decl);
2112 msg = 0;
2113 }
2114
2115 /* Clear out the default argument so that we are not
2116 confused later. */
2117 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2118 }
2119
2120 /* At this point, if we're still interested in issuing messages,
2121 they must apply to classes surrounding the object declared. */
2122 if (msg)
2123 msg = "default argument for template parameter for class enclosing `%D'";
2124 }
2125 }
2126
2127 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2128 parameters given by current_template_args, or reuses a
2129 previously existing one, if appropriate. Returns the DECL, or an
2130 equivalent one, if it is replaced via a call to duplicate_decls.
2131
2132 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2133
2134 tree
2135 push_template_decl_real (decl, is_friend)
2136 tree decl;
2137 int is_friend;
2138 {
2139 tree tmpl;
2140 tree args;
2141 tree info;
2142 tree ctx;
2143 int primary;
2144 int is_partial;
2145
2146 /* See if this is a partial specialization. */
2147 is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
2148 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2149 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2150
2151 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2152
2153 if (is_friend)
2154 /* For a friend, we want the context of the friend function, not
2155 the type of which it is a friend. */
2156 ctx = DECL_CONTEXT (decl);
2157 else if (DECL_REAL_CONTEXT (decl)
2158 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
2159 /* In the case of a virtual function, we want the class in which
2160 it is defined. */
2161 ctx = DECL_REAL_CONTEXT (decl);
2162 else
2163 /* Otherwise, if we're currently definining some class, the DECL
2164 is assumed to be a member of the class. */
2165 ctx = current_class_type;
2166
2167 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2168 ctx = NULL_TREE;
2169
2170 if (!DECL_CONTEXT (decl))
2171 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2172
2173 /* For determining whether this is a primary template or not, we're really
2174 interested in the lexical context, not the true context. */
2175 if (is_friend)
2176 info = current_class_type;
2177 else
2178 info = ctx;
2179
2180 /* See if this is a primary template. */
2181 if (info && TREE_CODE (info) == FUNCTION_DECL)
2182 primary = 0;
2183 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2184 this next line works even when we are at global scope. */
2185 else if (processing_template_decl > template_class_depth (info))
2186 primary = 1;
2187 else
2188 primary = 0;
2189
2190 if (primary)
2191 {
2192 if (current_lang_name == lang_name_c)
2193 cp_error ("template with C linkage");
2194 if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
2195 cp_error ("template class without a name");
2196 if (TREE_CODE (decl) == TYPE_DECL
2197 && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2198 cp_error ("template declaration of `%#T'", TREE_TYPE (decl));
2199 }
2200
2201 /* Check to see that the rules regarding the use of default
2202 arguments are not being violated. */
2203 check_default_tmpl_args (decl, current_template_parms,
2204 primary, is_partial);
2205
2206 if (is_partial)
2207 return process_partial_specialization (decl);
2208
2209 args = current_template_args ();
2210
2211 if (!ctx
2212 || TREE_CODE (ctx) == FUNCTION_DECL
2213 || TYPE_BEING_DEFINED (ctx)
2214 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2215 {
2216 if (DECL_LANG_SPECIFIC (decl)
2217 && DECL_TEMPLATE_INFO (decl)
2218 && DECL_TI_TEMPLATE (decl))
2219 tmpl = DECL_TI_TEMPLATE (decl);
2220 else
2221 {
2222 tmpl = build_template_decl (decl, current_template_parms);
2223
2224 if (DECL_LANG_SPECIFIC (decl)
2225 && DECL_TEMPLATE_SPECIALIZATION (decl))
2226 {
2227 /* A specialization of a member template of a template
2228 class. */
2229 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2230 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2231 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2232 }
2233 }
2234 }
2235 else
2236 {
2237 tree a, t, current, parms;
2238 int i;
2239
2240 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2241 cp_error ("must specialize `%#T' before defining member `%#D'",
2242 ctx, decl);
2243 if (TREE_CODE (decl) == TYPE_DECL)
2244 {
2245 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2246 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2247 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2248 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2249 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2250 else
2251 {
2252 cp_error ("`%D' does not declare a template type", decl);
2253 return decl;
2254 }
2255 }
2256 else if (! DECL_TEMPLATE_INFO (decl))
2257 {
2258 cp_error ("template definition of non-template `%#D'", decl);
2259 return decl;
2260 }
2261 else
2262 tmpl = DECL_TI_TEMPLATE (decl);
2263
2264 if (is_member_template (tmpl)
2265 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2266 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2267 && DECL_TEMPLATE_SPECIALIZATION (decl))
2268 {
2269 tree new_tmpl;
2270
2271 /* The declaration is a specialization of a member
2272 template, declared outside the class. Therefore, the
2273 innermost template arguments will be NULL, so we
2274 replace them with the arguments determined by the
2275 earlier call to check_explicit_specialization. */
2276 args = DECL_TI_ARGS (decl);
2277
2278 new_tmpl
2279 = build_template_decl (decl, current_template_parms);
2280 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2281 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2282 DECL_TI_TEMPLATE (decl) = new_tmpl;
2283 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2284 DECL_TEMPLATE_INFO (new_tmpl) =
2285 perm_tree_cons (tmpl, args, NULL_TREE);
2286
2287 register_specialization (new_tmpl, tmpl, args);
2288 return decl;
2289 }
2290
2291 /* Make sure the template headers we got make sense. */
2292
2293 parms = DECL_TEMPLATE_PARMS (tmpl);
2294 i = TMPL_PARMS_DEPTH (parms);
2295 if (TMPL_ARGS_DEPTH (args) != i)
2296 {
2297 cp_error ("expected %d levels of template parms for `%#D', got %d",
2298 i, decl, TMPL_ARGS_DEPTH (args));
2299 }
2300 else
2301 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2302 {
2303 a = TMPL_ARGS_LEVEL (args, i);
2304 t = INNERMOST_TEMPLATE_PARMS (parms);
2305
2306 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2307 {
2308 if (current == decl)
2309 cp_error ("got %d template parameters for `%#D'",
2310 TREE_VEC_LENGTH (a), decl);
2311 else
2312 cp_error ("got %d template parameters for `%#T'",
2313 TREE_VEC_LENGTH (a), current);
2314 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2315 }
2316
2317 /* Perhaps we should also check that the parms are used in the
2318 appropriate qualifying scopes in the declarator? */
2319
2320 if (current == decl)
2321 current = ctx;
2322 else
2323 current = TYPE_CONTEXT (current);
2324 }
2325 }
2326
2327 DECL_TEMPLATE_RESULT (tmpl) = decl;
2328 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2329
2330 /* Push template declarations for global functions and types. Note
2331 that we do not try to push a global template friend declared in a
2332 template class; such a thing may well depend on the template
2333 parameters of the class. */
2334 if (! ctx
2335 && !(is_friend && template_class_depth (current_class_type) > 0))
2336 tmpl = pushdecl_namespace_level (tmpl);
2337
2338 if (primary)
2339 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2340
2341 info = perm_tree_cons (tmpl, args, NULL_TREE);
2342
2343 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2344 {
2345 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2346 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2347 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
2348 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2349 }
2350 else if (! DECL_LANG_SPECIFIC (decl))
2351 cp_error ("template declaration of `%#D'", decl);
2352 else
2353 DECL_TEMPLATE_INFO (decl) = info;
2354
2355 return DECL_TEMPLATE_RESULT (tmpl);
2356 }
2357
2358 tree
2359 push_template_decl (decl)
2360 tree decl;
2361 {
2362 return push_template_decl_real (decl, 0);
2363 }
2364
2365 /* Called when a class template TYPE is redeclared with the indicated
2366 template PARMS, e.g.:
2367
2368 template <class T> struct S;
2369 template <class T> struct S {}; */
2370
2371 void
2372 redeclare_class_template (type, parms)
2373 tree type;
2374 tree parms;
2375 {
2376 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
2377 tree tmpl_parms;
2378 int i;
2379
2380 if (!PRIMARY_TEMPLATE_P (tmpl))
2381 /* The type is nested in some template class. Nothing to worry
2382 about here; there are no new template parameters for the nested
2383 type. */
2384 return;
2385
2386 parms = INNERMOST_TEMPLATE_PARMS (parms);
2387 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2388
2389 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2390 {
2391 cp_error_at ("previous declaration `%D'", tmpl);
2392 cp_error ("used %d template parameter%s instead of %d",
2393 TREE_VEC_LENGTH (tmpl_parms),
2394 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2395 TREE_VEC_LENGTH (parms));
2396 return;
2397 }
2398
2399 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2400 {
2401 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2402 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2403 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2404 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2405
2406 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2407 {
2408 cp_error_at ("template parameter `%#D'", tmpl_parm);
2409 cp_error ("redeclared here as `%#D'", parm);
2410 return;
2411 }
2412
2413 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2414 {
2415 /* We have in [temp.param]:
2416
2417 A template-parameter may not be given default arguments
2418 by two different declarations in the same scope. */
2419 cp_error ("redefinition of default argument for `%#D'", parm);
2420 cp_error_at (" original definition appeared here", tmpl_parm);
2421 return;
2422 }
2423
2424 if (parm_default != NULL_TREE)
2425 /* Update the previous template parameters (which are the ones
2426 that will really count) with the new default value. */
2427 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2428 }
2429 }
2430
2431 /* Attempt to convert the non-type template parameter EXPR to the
2432 indicated TYPE. If the conversion is successful, return the
2433 converted value. If the conversion is unsuccesful, return
2434 NULL_TREE if we issued an error message, or error_mark_node if we
2435 did not. We issue error messages for out-and-out bad template
2436 parameters, but not simply because the conversion failed, since we
2437 might be just trying to do argument deduction. By the time this
2438 function is called, neither TYPE nor EXPR may make use of template
2439 parameters. */
2440
2441 static tree
2442 convert_nontype_argument (type, expr)
2443 tree type;
2444 tree expr;
2445 {
2446 tree expr_type = TREE_TYPE (expr);
2447
2448 /* A template-argument for a non-type, non-template
2449 template-parameter shall be one of:
2450
2451 --an integral constant-expression of integral or enumeration
2452 type; or
2453
2454 --the name of a non-type template-parameter; or
2455
2456 --the name of an object or function with external linkage,
2457 including function templates and function template-ids but
2458 excluding non-static class members, expressed as id-expression;
2459 or
2460
2461 --the address of an object or function with external linkage,
2462 including function templates and function template-ids but
2463 excluding non-static class members, expressed as & id-expression
2464 where the & is optional if the name refers to a function or
2465 array; or
2466
2467 --a pointer to member expressed as described in _expr.unary.op_. */
2468
2469 /* An integral constant-expression can include const variables
2470 or enumerators. */
2471 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
2472 expr = decl_constant_value (expr);
2473
2474 if (is_overloaded_fn (expr))
2475 /* OK for now. We'll check that it has external linkage later.
2476 Check this first since if expr_type is the unknown_type_node
2477 we would otherwise complain below. */
2478 ;
2479 else if (TYPE_PTR_P (expr_type)
2480 || TREE_CODE (expr_type) == ARRAY_TYPE
2481 || TREE_CODE (type) == REFERENCE_TYPE
2482 /* If expr is the address of an overloaded function, we
2483 will get the unknown_type_node at this point. */
2484 || expr_type == unknown_type_node)
2485 {
2486 tree referent;
2487 tree e = expr;
2488 STRIP_NOPS (e);
2489
2490 if (TREE_CODE (type) == REFERENCE_TYPE
2491 || TREE_CODE (expr_type) == ARRAY_TYPE)
2492 referent = e;
2493 else
2494 {
2495 if (TREE_CODE (e) != ADDR_EXPR)
2496 {
2497 bad_argument:
2498 cp_error ("`%E' is not a valid template argument", expr);
2499 error ("it must be %s%s with external linkage",
2500 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
2501 ? "a pointer to " : "",
2502 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
2503 ? "a function" : "an object");
2504 return NULL_TREE;
2505 }
2506
2507 referent = TREE_OPERAND (e, 0);
2508 STRIP_NOPS (referent);
2509 }
2510
2511 if (TREE_CODE (referent) == STRING_CST)
2512 {
2513 cp_error ("string literal %E is not a valid template argument",
2514 referent);
2515 error ("because it is the address of an object with static linkage");
2516 return NULL_TREE;
2517 }
2518
2519 if (is_overloaded_fn (referent))
2520 /* We'll check that it has external linkage later. */
2521 ;
2522 else if (TREE_CODE (referent) != VAR_DECL)
2523 goto bad_argument;
2524 else if (!TREE_PUBLIC (referent))
2525 {
2526 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
2527 return error_mark_node;
2528 }
2529 }
2530 else if (INTEGRAL_TYPE_P (expr_type)
2531 || TYPE_PTRMEM_P (expr_type)
2532 || TYPE_PTRMEMFUNC_P (expr_type)
2533 /* The next two are g++ extensions. */
2534 || TREE_CODE (expr_type) == REAL_TYPE
2535 || TREE_CODE (expr_type) == COMPLEX_TYPE)
2536 {
2537 if (! TREE_CONSTANT (expr))
2538 {
2539 non_constant:
2540 cp_error ("non-constant `%E' cannot be used as template argument",
2541 expr);
2542 return NULL_TREE;
2543 }
2544 }
2545 else
2546 {
2547 cp_error ("object `%E' cannot be used as template argument", expr);
2548 return NULL_TREE;
2549 }
2550
2551 switch (TREE_CODE (type))
2552 {
2553 case INTEGER_TYPE:
2554 case BOOLEAN_TYPE:
2555 case ENUMERAL_TYPE:
2556 /* For a non-type template-parameter of integral or enumeration
2557 type, integral promotions (_conv.prom_) and integral
2558 conversions (_conv.integral_) are applied. */
2559 if (!INTEGRAL_TYPE_P (expr_type))
2560 return error_mark_node;
2561
2562 /* It's safe to call digest_init in this case; we know we're
2563 just converting one integral constant expression to another. */
2564 expr = digest_init (type, expr, (tree*) 0);
2565
2566 if (TREE_CODE (expr) != INTEGER_CST)
2567 /* Curiously, some TREE_CONSTANT integral expressions do not
2568 simplify to integer constants. For example, `3 % 0',
2569 remains a TRUNC_MOD_EXPR. */
2570 goto non_constant;
2571
2572 return expr;
2573
2574 case REAL_TYPE:
2575 case COMPLEX_TYPE:
2576 /* These are g++ extensions. */
2577 if (TREE_CODE (expr_type) != TREE_CODE (type))
2578 return error_mark_node;
2579
2580 expr = digest_init (type, expr, (tree*) 0);
2581
2582 if (TREE_CODE (expr) != REAL_CST)
2583 goto non_constant;
2584
2585 return expr;
2586
2587 case POINTER_TYPE:
2588 {
2589 tree type_pointed_to = TREE_TYPE (type);
2590
2591 if (TYPE_PTRMEM_P (type))
2592 /* For a non-type template-parameter of type pointer to data
2593 member, qualification conversions (_conv.qual_) are
2594 applied. */
2595 return perform_qualification_conversions (type, expr);
2596 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2597 {
2598 /* For a non-type template-parameter of type pointer to
2599 function, only the function-to-pointer conversion
2600 (_conv.func_) is applied. If the template-argument
2601 represents a set of overloaded functions (or a pointer to
2602 such), the matching function is selected from the set
2603 (_over.over_). */
2604 tree fns;
2605 tree fn;
2606
2607 if (TREE_CODE (expr) == ADDR_EXPR)
2608 fns = TREE_OPERAND (expr, 0);
2609 else
2610 fns = expr;
2611
2612 fn = instantiate_type (type_pointed_to, fns, 0);
2613
2614 if (fn == error_mark_node)
2615 return error_mark_node;
2616
2617 if (!TREE_PUBLIC (fn))
2618 {
2619 if (really_overloaded_fn (fns))
2620 return error_mark_node;
2621 else
2622 goto bad_argument;
2623 }
2624
2625 expr = build_unary_op (ADDR_EXPR, fn, 0);
2626
2627 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
2628 0);
2629 return expr;
2630 }
2631 else
2632 {
2633 /* For a non-type template-parameter of type pointer to
2634 object, qualification conversions (_conv.qual_) and the
2635 array-to-pointer conversion (_conv.array_) are applied.
2636 [Note: In particular, neither the null pointer conversion
2637 (_conv.ptr_) nor the derived-to-base conversion
2638 (_conv.ptr_) are applied. Although 0 is a valid
2639 template-argument for a non-type template-parameter of
2640 integral type, it is not a valid template-argument for a
2641 non-type template-parameter of pointer type.]
2642
2643 The call to decay_conversion performs the
2644 array-to-pointer conversion, if appropriate. */
2645 expr = decay_conversion (expr);
2646
2647 if (expr == error_mark_node)
2648 return error_mark_node;
2649 else
2650 return perform_qualification_conversions (type, expr);
2651 }
2652 }
2653 break;
2654
2655 case REFERENCE_TYPE:
2656 {
2657 tree type_referred_to = TREE_TYPE (type);
2658
2659 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2660 {
2661 /* For a non-type template-parameter of type reference to
2662 function, no conversions apply. If the
2663 template-argument represents a set of overloaded
2664 functions, the matching function is selected from the
2665 set (_over.over_). */
2666 tree fns = expr;
2667 tree fn;
2668
2669 fn = instantiate_type (type_referred_to, fns, 0);
2670
2671 if (fn == error_mark_node)
2672 return error_mark_node;
2673
2674 if (!TREE_PUBLIC (fn))
2675 {
2676 if (really_overloaded_fn (fns))
2677 /* Don't issue an error here; we might get a different
2678 function if the overloading had worked out
2679 differently. */
2680 return error_mark_node;
2681 else
2682 goto bad_argument;
2683 }
2684
2685 my_friendly_assert (same_type_p (type_referred_to,
2686 TREE_TYPE (fn)),
2687 0);
2688
2689 return fn;
2690 }
2691 else
2692 {
2693 /* For a non-type template-parameter of type reference to
2694 object, no conversions apply. The type referred to by the
2695 reference may be more cv-qualified than the (otherwise
2696 identical) type of the template-argument. The
2697 template-parameter is bound directly to the
2698 template-argument, which must be an lvalue. */
2699 if ((TYPE_MAIN_VARIANT (expr_type)
2700 != TYPE_MAIN_VARIANT (type_referred_to))
2701 || !at_least_as_qualified_p (type_referred_to,
2702 expr_type)
2703 || !real_lvalue_p (expr))
2704 return error_mark_node;
2705 else
2706 return expr;
2707 }
2708 }
2709 break;
2710
2711 case RECORD_TYPE:
2712 {
2713 tree fns;
2714 tree fn;
2715
2716 if (!TYPE_PTRMEMFUNC_P (type))
2717 /* This handles templates like
2718 template<class T, T t> void f();
2719 when T is substituted with any class. The second template
2720 parameter becomes invalid and the template candidate is
2721 rejected. */
2722 return error_mark_node;
2723
2724 /* For a non-type template-parameter of type pointer to member
2725 function, no conversions apply. If the template-argument
2726 represents a set of overloaded member functions, the
2727 matching member function is selected from the set
2728 (_over.over_). */
2729
2730 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
2731 expr_type != unknown_type_node)
2732 return error_mark_node;
2733
2734 if (TREE_CODE (expr) == CONSTRUCTOR)
2735 {
2736 /* A ptr-to-member constant. */
2737 if (!same_type_p (type, expr_type))
2738 return error_mark_node;
2739 else
2740 return expr;
2741 }
2742
2743 if (TREE_CODE (expr) != ADDR_EXPR)
2744 return error_mark_node;
2745
2746 fns = TREE_OPERAND (expr, 0);
2747
2748 fn = instantiate_type (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)),
2749 fns, 0);
2750
2751 if (fn == error_mark_node)
2752 return error_mark_node;
2753
2754 expr = build_unary_op (ADDR_EXPR, fn, 0);
2755
2756 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
2757 0);
2758 return expr;
2759 }
2760 break;
2761
2762 default:
2763 /* All non-type parameters must have one of these types. */
2764 my_friendly_abort (0);
2765 break;
2766 }
2767
2768 return error_mark_node;
2769 }
2770
2771 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2772 template template parameters. Both PARM_PARMS and ARG_PARMS are
2773 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2774 or PARM_DECL.
2775
2776 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2777 the case, then extra parameters must have default arguments.
2778
2779 Consider the example:
2780 template <class T, class Allocator = allocator> class vector;
2781 template<template <class U> class TT> class C;
2782
2783 C<vector> is a valid instantiation. PARM_PARMS for the above code
2784 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2785 T and Allocator) and OUTER_ARGS contains the argument that is used to
2786 substitute the TT parameter. */
2787
2788 static int
2789 coerce_template_template_parms (parm_parms, arg_parms, in_decl, outer_args)
2790 tree parm_parms, arg_parms, in_decl, outer_args;
2791 {
2792 int nparms, nargs, i;
2793 tree parm, arg;
2794
2795 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
2796 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
2797
2798 nparms = TREE_VEC_LENGTH (parm_parms);
2799 nargs = TREE_VEC_LENGTH (arg_parms);
2800
2801 /* The rule here is opposite of coerce_template_parms. */
2802 if (nargs < nparms
2803 || (nargs > nparms
2804 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
2805 return 0;
2806
2807 for (i = 0; i < nparms; ++i)
2808 {
2809 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
2810 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
2811
2812 if (arg == NULL_TREE || arg == error_mark_node
2813 || parm == NULL_TREE || parm == error_mark_node)
2814 return 0;
2815
2816 if (TREE_CODE (arg) != TREE_CODE (parm))
2817 return 0;
2818
2819 switch (TREE_CODE (parm))
2820 {
2821 case TYPE_DECL:
2822 break;
2823
2824 case TEMPLATE_DECL:
2825 /* We encounter instantiations of templates like
2826 template <template <template <class> class> class TT>
2827 class C; */
2828 sorry ("nested template template parameter");
2829 return 0;
2830
2831 case PARM_DECL:
2832 /* The tsubst call is used to handle cases such as
2833 template <class T, template <T> class TT> class D;
2834 i.e. the parameter list of TT depends on earlier parameters. */
2835 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, in_decl),
2836 TREE_TYPE (arg)))
2837 return 0;
2838 break;
2839
2840 default:
2841 my_friendly_abort (0);
2842 }
2843 }
2844 return 1;
2845 }
2846
2847 /* Convert the indicated template ARG as necessary to match the
2848 indicated template PARM. Returns the converted ARG, or
2849 error_mark_node if the conversion was unsuccessful. Error messages
2850 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2851 parameter in the parameter list. ARGS is the full set of template
2852 arguments deduced so far. */
2853
2854 static tree
2855 convert_template_argument (parm, arg, args, complain, i, in_decl)
2856 tree parm;
2857 tree arg;
2858 tree args;
2859 int complain;
2860 int i;
2861 tree in_decl;
2862 {
2863 tree val;
2864 tree inner_args;
2865 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
2866
2867 inner_args = innermost_args (args);
2868
2869 if (TREE_CODE (arg) == TREE_LIST
2870 && TREE_TYPE (arg) != NULL_TREE
2871 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
2872 {
2873 /* The template argument was the name of some
2874 member function. That's usually
2875 illegal, but static members are OK. In any
2876 case, grab the underlying fields/functions
2877 and issue an error later if required. */
2878 arg = TREE_VALUE (arg);
2879 TREE_TYPE (arg) = unknown_type_node;
2880 }
2881
2882 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
2883 requires_type = (TREE_CODE (parm) == TYPE_DECL
2884 || requires_tmpl_type);
2885
2886 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2887 we also accept implicitly created TYPE_DECL as a valid argument.
2888 This is necessary to handle the case where we pass a template name
2889 to a template template parameter in a scope where we've derived from
2890 in instantiation of that template, so the template name refers to that
2891 instantiation. We really ought to handle this better. */
2892 is_tmpl_type
2893 = ((TREE_CODE (arg) == TEMPLATE_DECL
2894 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
2895 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
2896 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg))
2897 || (TREE_CODE (arg) == RECORD_TYPE
2898 && CLASSTYPE_TEMPLATE_INFO (arg)
2899 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
2900 && DECL_ARTIFICIAL (TYPE_NAME (arg))
2901 && requires_tmpl_type
2902 && is_base_of_enclosing_class (arg, current_class_type)));
2903 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2904 arg = TYPE_STUB_DECL (arg);
2905 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
2906 arg = CLASSTYPE_TI_TEMPLATE (arg);
2907
2908 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
2909
2910 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
2911 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
2912 {
2913 cp_pedwarn ("to refer to a type member of a template parameter,");
2914 cp_pedwarn (" use `typename %E'", arg);
2915
2916 arg = make_typename_type (TREE_OPERAND (arg, 0),
2917 TREE_OPERAND (arg, 1));
2918 is_type = 1;
2919 }
2920 if (is_type != requires_type)
2921 {
2922 if (in_decl)
2923 {
2924 if (complain)
2925 {
2926 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2927 i + 1, in_decl);
2928 if (is_type)
2929 cp_error (" expected a constant of type `%T', got `%T'",
2930 TREE_TYPE (parm),
2931 (is_tmpl_type ? DECL_NAME (arg) : arg));
2932 else
2933 cp_error (" expected a type, got `%E'", arg);
2934 }
2935 }
2936 return error_mark_node;
2937 }
2938 if (is_tmpl_type ^ requires_tmpl_type)
2939 {
2940 if (in_decl && complain)
2941 {
2942 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2943 i + 1, in_decl);
2944 if (is_tmpl_type)
2945 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
2946 else
2947 cp_error (" expected a class template, got `%T'", arg);
2948 }
2949 return error_mark_node;
2950 }
2951
2952 if (is_type)
2953 {
2954 if (requires_tmpl_type)
2955 {
2956 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2957 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2958
2959 if (coerce_template_template_parms (parmparm, argparm,
2960 in_decl, inner_args))
2961 {
2962 val = arg;
2963
2964 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2965 TEMPLATE_DECL. */
2966 if (val != error_mark_node
2967 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
2968 val = TREE_TYPE (val);
2969 }
2970 else
2971 {
2972 if (in_decl && complain)
2973 {
2974 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2975 i + 1, in_decl);
2976 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
2977 }
2978
2979 val = error_mark_node;
2980 }
2981 }
2982 else
2983 {
2984 val = groktypename (arg);
2985 if (! processing_template_decl)
2986 {
2987 /* [basic.link]: A name with no linkage (notably, the
2988 name of a class or enumeration declared in a local
2989 scope) shall not be used to declare an entity with
2990 linkage. This implies that names with no linkage
2991 cannot be used as template arguments. */
2992 tree t = no_linkage_check (val);
2993 if (t)
2994 {
2995 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
2996 cp_pedwarn
2997 ("template-argument `%T' uses anonymous type", val);
2998 else
2999 cp_error
3000 ("template-argument `%T' uses local type `%T'",
3001 val, t);
3002 return error_mark_node;
3003 }
3004 }
3005 }
3006 }
3007 else
3008 {
3009 tree t = tsubst (TREE_TYPE (parm), args, in_decl);
3010
3011 if (processing_template_decl)
3012 arg = maybe_fold_nontype_arg (arg);
3013
3014 if (!uses_template_parms (arg) && !uses_template_parms (t))
3015 /* We used to call digest_init here. However, digest_init
3016 will report errors, which we don't want when complain
3017 is zero. More importantly, digest_init will try too
3018 hard to convert things: for example, `0' should not be
3019 converted to pointer type at this point according to
3020 the standard. Accepting this is not merely an
3021 extension, since deciding whether or not these
3022 conversions can occur is part of determining which
3023 function template to call, or whether a given epxlicit
3024 argument specification is legal. */
3025 val = convert_nontype_argument (t, arg);
3026 else
3027 val = arg;
3028
3029 if (val == NULL_TREE)
3030 val = error_mark_node;
3031 else if (val == error_mark_node && complain)
3032 cp_error ("could not convert template argument `%E' to `%T'",
3033 arg, t);
3034 }
3035
3036 return val;
3037 }
3038
3039 /* Convert all template arguments to their appropriate types, and
3040 return a vector containing the innermost resulting template
3041 arguments. If any error occurs, return error_mark_node, and, if
3042 COMPLAIN is non-zero, issue an error message. Some error messages
3043 are issued even if COMPLAIN is zero; for instance, if a template
3044 argument is composed from a local class.
3045
3046 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3047 provided in ARGLIST, or else trailing parameters must have default
3048 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3049 deduction for any unspecified trailing arguments.
3050
3051 The resulting TREE_VEC is allocated on a temporary obstack, and
3052 must be explicitly copied if it will be permanent. */
3053
3054 static tree
3055 coerce_template_parms (parms, args, in_decl,
3056 complain,
3057 require_all_arguments)
3058 tree parms, args;
3059 tree in_decl;
3060 int complain;
3061 int require_all_arguments;
3062 {
3063 int nparms, nargs, i, lost = 0;
3064 tree inner_args;
3065 tree new_args;
3066 tree new_inner_args;
3067
3068 inner_args = innermost_args (args);
3069 nargs = NUM_TMPL_ARGS (inner_args);
3070 nparms = TREE_VEC_LENGTH (parms);
3071
3072 if (nargs > nparms
3073 || (nargs < nparms
3074 && require_all_arguments
3075 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3076 {
3077 if (complain)
3078 {
3079 cp_error ("wrong number of template arguments (%d, should be %d)",
3080 nargs, nparms);
3081
3082 if (in_decl)
3083 cp_error_at ("provided for `%D'", in_decl);
3084 }
3085
3086 return error_mark_node;
3087 }
3088
3089 new_inner_args = make_temp_vec (nparms);
3090 new_args = add_outermost_template_args (args, new_inner_args);
3091 for (i = 0; i < nparms; i++)
3092 {
3093 tree arg;
3094 tree parm;
3095
3096 /* Get the Ith template parameter. */
3097 parm = TREE_VEC_ELT (parms, i);
3098
3099 /* Calculate the Ith argument. */
3100 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3101 {
3102 arg = TREE_VALUE (inner_args);
3103 inner_args = TREE_CHAIN (inner_args);
3104 }
3105 else if (i < nargs)
3106 arg = TREE_VEC_ELT (inner_args, i);
3107 /* If no template argument was supplied, look for a default
3108 value. */
3109 else if (TREE_PURPOSE (parm) == NULL_TREE)
3110 {
3111 /* There was no default value. */
3112 my_friendly_assert (!require_all_arguments, 0);
3113 break;
3114 }
3115 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3116 arg = tsubst (TREE_PURPOSE (parm), new_args, in_decl);
3117 else
3118 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, in_decl);
3119
3120 /* Now, convert the Ith argument, as necessary. */
3121 if (arg == NULL_TREE)
3122 /* We're out of arguments. */
3123 {
3124 my_friendly_assert (!require_all_arguments, 0);
3125 break;
3126 }
3127 else if (arg == error_mark_node)
3128 {
3129 cp_error ("template argument %d is invalid", i + 1);
3130 arg = error_mark_node;
3131 }
3132 else
3133 arg = convert_template_argument (TREE_VALUE (parm),
3134 arg, new_args, complain, i,
3135 in_decl);
3136
3137 if (arg == error_mark_node)
3138 lost++;
3139 TREE_VEC_ELT (new_inner_args, i) = arg;
3140 }
3141
3142 if (lost)
3143 return error_mark_node;
3144
3145 return new_inner_args;
3146 }
3147
3148 /* Returns 1 if template args OT and NT are equivalent. */
3149
3150 int
3151 template_args_equal (ot, nt)
3152 tree ot, nt;
3153 {
3154 if (nt == ot)
3155 return 1;
3156 if (TREE_CODE (nt) != TREE_CODE (ot))
3157 return 0;
3158 if (TREE_CODE (nt) == TREE_VEC)
3159 /* For member templates */
3160 return comp_template_args (ot, nt);
3161 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
3162 return same_type_p (ot, nt);
3163 else
3164 return (cp_tree_equal (ot, nt) > 0);
3165 }
3166
3167 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3168 of template arguments. Returns 0 otherwise. */
3169
3170 int
3171 comp_template_args (oldargs, newargs)
3172 tree oldargs, newargs;
3173 {
3174 int i;
3175
3176 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3177 return 0;
3178
3179 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3180 {
3181 tree nt = TREE_VEC_ELT (newargs, i);
3182 tree ot = TREE_VEC_ELT (oldargs, i);
3183
3184 if (! template_args_equal (ot, nt))
3185 return 0;
3186 }
3187 return 1;
3188 }
3189
3190 /* Given class template name and parameter list, produce a user-friendly name
3191 for the instantiation. */
3192
3193 static char *
3194 mangle_class_name_for_template (name, parms, arglist)
3195 char *name;
3196 tree parms, arglist;
3197 {
3198 static struct obstack scratch_obstack;
3199 static char *scratch_firstobj;
3200 int i, nparms;
3201
3202 if (!scratch_firstobj)
3203 gcc_obstack_init (&scratch_obstack);
3204 else
3205 obstack_free (&scratch_obstack, scratch_firstobj);
3206 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3207
3208 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3209 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3210
3211 cat (name);
3212 ccat ('<');
3213 nparms = TREE_VEC_LENGTH (parms);
3214 arglist = innermost_args (arglist);
3215 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3216 for (i = 0; i < nparms; i++)
3217 {
3218 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3219 tree arg = TREE_VEC_ELT (arglist, i);
3220
3221 if (i)
3222 ccat (',');
3223
3224 if (TREE_CODE (parm) == TYPE_DECL)
3225 {
3226 cat (type_as_string_real (arg, 0, 1));
3227 continue;
3228 }
3229 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3230 {
3231 if (TREE_CODE (arg) == TEMPLATE_DECL)
3232 {
3233 /* Already substituted with real template. Just output
3234 the template name here */
3235 tree context = DECL_CONTEXT (arg);
3236 if (context)
3237 {
3238 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
3239 cat(decl_as_string (DECL_CONTEXT (arg), 0));
3240 cat("::");
3241 }
3242 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3243 }
3244 else
3245 /* Output the parameter declaration */
3246 cat (type_as_string_real (arg, 0, 1));
3247 continue;
3248 }
3249 else
3250 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3251
3252 if (TREE_CODE (arg) == TREE_LIST)
3253 {
3254 /* New list cell was built because old chain link was in
3255 use. */
3256 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3257 arg = TREE_VALUE (arg);
3258 }
3259 /* No need to check arglist against parmlist here; we did that
3260 in coerce_template_parms, called from lookup_template_class. */
3261 cat (expr_as_string (arg, 0));
3262 }
3263 {
3264 char *bufp = obstack_next_free (&scratch_obstack);
3265 int offset = 0;
3266 while (bufp[offset - 1] == ' ')
3267 offset--;
3268 obstack_blank_fast (&scratch_obstack, offset);
3269
3270 /* B<C<char> >, not B<C<char>> */
3271 if (bufp[offset - 1] == '>')
3272 ccat (' ');
3273 }
3274 ccat ('>');
3275 ccat ('\0');
3276 return (char *) obstack_base (&scratch_obstack);
3277 }
3278
3279 static tree
3280 classtype_mangled_name (t)
3281 tree t;
3282 {
3283 if (CLASSTYPE_TEMPLATE_INFO (t)
3284 /* Specializations have already had their names set up in
3285 lookup_template_class. */
3286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3287 {
3288 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3289
3290 /* For non-primary templates, the template parameters are
3291 implicit from their surrounding context. */
3292 if (PRIMARY_TEMPLATE_P (tmpl))
3293 {
3294 tree name = DECL_NAME (tmpl);
3295 char *mangled_name = mangle_class_name_for_template
3296 (IDENTIFIER_POINTER (name),
3297 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3298 CLASSTYPE_TI_ARGS (t));
3299 tree id = get_identifier (mangled_name);
3300 IDENTIFIER_TEMPLATE (id) = name;
3301 return id;
3302 }
3303 }
3304
3305 return TYPE_IDENTIFIER (t);
3306 }
3307
3308 static void
3309 add_pending_template (d)
3310 tree d;
3311 {
3312 tree ti;
3313
3314 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3315 ti = CLASSTYPE_TEMPLATE_INFO (d);
3316 else
3317 ti = DECL_TEMPLATE_INFO (d);
3318
3319 if (TI_PENDING_TEMPLATE_FLAG (ti))
3320 return;
3321
3322 *template_tail = perm_tree_cons
3323 (build_srcloc_here (), d, NULL_TREE);
3324 template_tail = &TREE_CHAIN (*template_tail);
3325 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3326 }
3327
3328
3329 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3330 may be either a _DECL or an overloaded function or an
3331 IDENTIFIER_NODE), and ARGLIST. */
3332
3333 tree
3334 lookup_template_function (fns, arglist)
3335 tree fns, arglist;
3336 {
3337 tree type;
3338
3339 if (fns == NULL_TREE)
3340 {
3341 cp_error ("non-template used as template");
3342 return error_mark_node;
3343 }
3344
3345 type = TREE_TYPE (fns);
3346 if (TREE_CODE (fns) == OVERLOAD || !type)
3347 type = unknown_type_node;
3348
3349 if (processing_template_decl)
3350 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3351 else
3352 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3353 }
3354
3355 /* Within the scope of a template class S<T>, the name S gets bound
3356 (in build_self_reference) to a TYPE_DECL for the class, not a
3357 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3358 or one of its enclosing classes, and that type is a template,
3359 return the associated TEMPLATE_DECL. Otherwise, the original
3360 DECL is returned. */
3361
3362 tree
3363 maybe_get_template_decl_from_type_decl (decl)
3364 tree decl;
3365 {
3366 return (decl != NULL_TREE
3367 && TREE_CODE (decl) == TYPE_DECL
3368 && DECL_ARTIFICIAL (decl)
3369 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3370 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3371 }
3372
3373 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3374 parameters, find the desired type.
3375
3376 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3377 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3378 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3379 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3380 copy it here to keep it from being reclaimed when the decl storage
3381 is reclaimed.
3382
3383 IN_DECL, if non-NULL, is the template declaration we are trying to
3384 instantiate.
3385
3386 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3387 the class we are looking up.
3388
3389 If the template class is really a local class in a template
3390 function, then the FUNCTION_CONTEXT is the function in which it is
3391 being instantiated. */
3392
3393 tree
3394 lookup_template_class (d1, arglist, in_decl, context, entering_scope)
3395 tree d1, arglist;
3396 tree in_decl;
3397 tree context;
3398 int entering_scope;
3399 {
3400 tree template = NULL_TREE, parmlist;
3401 tree t;
3402
3403 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3404 {
3405 if (IDENTIFIER_VALUE (d1)
3406 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3407 template = IDENTIFIER_VALUE (d1);
3408 else
3409 {
3410 if (context)
3411 push_decl_namespace (context);
3412 if (current_class_type != NULL_TREE)
3413 template =
3414 maybe_get_template_decl_from_type_decl
3415 (IDENTIFIER_CLASS_VALUE (d1));
3416 if (template == NULL_TREE)
3417 template = lookup_name_nonclass (d1);
3418 if (context)
3419 pop_decl_namespace ();
3420 }
3421 if (template)
3422 context = DECL_CONTEXT (template);
3423 }
3424 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3425 {
3426 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)))
3427 {
3428 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
3429 d1 = DECL_NAME (template);
3430 }
3431 }
3432 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3433 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3434 && IS_AGGR_TYPE (d1)))
3435 {
3436 template = TYPE_TI_TEMPLATE (d1);
3437 d1 = DECL_NAME (template);
3438 }
3439 else if (TREE_CODE (d1) == TEMPLATE_DECL
3440 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3441 {
3442 template = d1;
3443 d1 = DECL_NAME (template);
3444 context = DECL_CONTEXT (template);
3445 }
3446 else
3447 my_friendly_abort (272);
3448
3449 /* With something like `template <class T> class X class X { ... };'
3450 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3451 We don't want to do that, but we have to deal with the situation,
3452 so let's give them some syntax errors to chew on instead of a
3453 crash. */
3454 if (! template)
3455 {
3456 cp_error ("`%T' is not a template", d1);
3457 return error_mark_node;
3458 }
3459
3460 if (context == NULL_TREE)
3461 context = global_namespace;
3462
3463 if (TREE_CODE (template) != TEMPLATE_DECL)
3464 {
3465 cp_error ("non-template type `%T' used as a template", d1);
3466 if (in_decl)
3467 cp_error_at ("for template declaration `%D'", in_decl);
3468 return error_mark_node;
3469 }
3470
3471 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3472 {
3473 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3474 template arguments */
3475
3476 tree parm = copy_template_template_parm (TREE_TYPE (template));
3477 tree template2 = TYPE_STUB_DECL (parm);
3478 tree arglist2;
3479
3480 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3481
3482 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
3483 if (arglist2 == error_mark_node)
3484 return error_mark_node;
3485
3486 arglist2 = copy_to_permanent (arglist2);
3487 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
3488 = perm_tree_cons (template2, arglist2, NULL_TREE);
3489 TYPE_SIZE (parm) = 0;
3490 return parm;
3491 }
3492 else
3493 {
3494 tree template_type = TREE_TYPE (template);
3495 tree type_decl;
3496 tree found = NULL_TREE;
3497 int arg_depth;
3498 int parm_depth;
3499 int is_partial_instantiation;
3500
3501 template = most_general_template (template);
3502 parmlist = DECL_TEMPLATE_PARMS (template);
3503 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3504 arg_depth = TMPL_ARGS_DEPTH (arglist);
3505
3506 /* We build up the coerced arguments and such on the
3507 momentary_obstack. */
3508 push_momentary ();
3509
3510 if (arg_depth == 1 && parm_depth > 1)
3511 {
3512 /* We've been given an incomplete set of template arguments.
3513 For example, given:
3514
3515 template <class T> struct S1 {
3516 template <class U> struct S2 {};
3517 template <class U> struct S2<U*> {};
3518 };
3519
3520 we will be called with an ARGLIST of `U*', but the
3521 TEMPLATE will be `template <class T> template
3522 <class U> struct S1<T>::S2'. We must fill in the missing
3523 arguments. */
3524 my_friendly_assert (context != NULL_TREE, 0);
3525 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context))
3526 && context != global_namespace)
3527 context = DECL_REAL_CONTEXT (context);
3528
3529 if (context == global_namespace)
3530 /* This is bad. We cannot get enough arguments, even from
3531 the surrounding context, to resolve this class. One
3532 case where this might happen is (illegal) code like:
3533
3534 template <class U>
3535 template <class T>
3536 struct S {
3537 A(const A<T>& a) {}
3538 };
3539
3540 We should catch this error sooner (at the opening curly
3541 for `S', but it is better to be safe than sorry here. */
3542 {
3543 cp_error ("invalid use of `%D'", template);
3544 return error_mark_node;
3545 }
3546
3547 arglist = add_to_template_args (TYPE_TI_ARGS (context),
3548 arglist);
3549 arg_depth = TMPL_ARGS_DEPTH (arglist);
3550 }
3551
3552 my_friendly_assert (parm_depth == arg_depth, 0);
3553
3554 /* Calculate the BOUND_ARGS. These will be the args that are
3555 actually tsubst'd into the definition to create the
3556 instantiation. */
3557 if (parm_depth > 1)
3558 {
3559 /* We have multiple levels of arguments to coerce, at once. */
3560 int i;
3561 int saved_depth = TMPL_ARGS_DEPTH (arglist);
3562
3563 tree bound_args = make_temp_vec (parm_depth);
3564
3565 for (i = saved_depth,
3566 t = DECL_TEMPLATE_PARMS (template);
3567 i > 0 && t != NULL_TREE;
3568 --i, t = TREE_CHAIN (t))
3569 {
3570 tree a = coerce_template_parms (TREE_VALUE (t),
3571 arglist, template, 1, 1);
3572 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3573
3574 /* We temporarily reduce the length of the ARGLIST so
3575 that coerce_template_parms will see only the arguments
3576 corresponding to the template parameters it is
3577 examining. */
3578 TREE_VEC_LENGTH (arglist)--;
3579 }
3580
3581 /* Restore the ARGLIST to its full size. */
3582 TREE_VEC_LENGTH (arglist) = saved_depth;
3583
3584 arglist = bound_args;
3585 }
3586 else
3587 arglist
3588 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3589 innermost_args (arglist),
3590 template, 1, 1);
3591
3592 if (arglist == error_mark_node)
3593 /* We were unable to bind the arguments. */
3594 return error_mark_node;
3595
3596 /* In the scope of a template class, explicit references to the
3597 template class refer to the type of the template, not any
3598 instantiation of it. For example, in:
3599
3600 template <class T> class C { void f(C<T>); }
3601
3602 the `C<T>' is just the same as `C'. Outside of the
3603 class, however, such a reference is an instantiation. */
3604 if (comp_template_args (TYPE_TI_ARGS (template_type),
3605 arglist))
3606 {
3607 found = template_type;
3608
3609 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
3610 {
3611 tree ctx;
3612
3613 /* Note that we use DECL_CONTEXT, rather than
3614 CP_DECL_CONTEXT, so that the termination test is
3615 always just `ctx'. We're not interested in namepace
3616 scopes. */
3617 for (ctx = current_class_type;
3618 ctx;
3619 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3620 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3621 if (same_type_p (ctx, template_type))
3622 break;
3623
3624 if (!ctx)
3625 /* We're not in the scope of the class, so the
3626 TEMPLATE_TYPE is not the type we want after
3627 all. */
3628 found = NULL_TREE;
3629 }
3630 }
3631
3632 if (!found)
3633 {
3634 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3635 found; found = TREE_CHAIN (found))
3636 if (comp_template_args (TREE_PURPOSE (found), arglist))
3637 break;
3638
3639 if (found)
3640 found = TREE_VALUE (found);
3641 }
3642
3643 if (found)
3644 {
3645 pop_momentary ();
3646 return found;
3647 }
3648
3649 /* Since we didn't find the type, we'll have to create it.
3650 Since we'll be saving this type on the
3651 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3652 push_obstacks (&permanent_obstack, &permanent_obstack);
3653
3654 /* This type is a "partial instantiation" if any of the template
3655 arguments still inolve template parameters. Note that we set
3656 IS_PARTIAL_INSTANTIATION for partial specializations as
3657 well. */
3658 is_partial_instantiation = uses_template_parms (arglist);
3659
3660 /* Create the type. */
3661 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3662 {
3663 if (!is_partial_instantiation)
3664 t = start_enum (TYPE_IDENTIFIER (template_type));
3665 else
3666 /* We don't want to call start_enum for this type, since
3667 the values for the enumeration constants may involve
3668 template parameters. And, no one should be interested
3669 in the enumeration constants for such a type. */
3670 t = make_node (ENUMERAL_TYPE);
3671 }
3672 else
3673 {
3674 t = make_lang_type (TREE_CODE (template_type));
3675 CLASSTYPE_DECLARED_CLASS (t)
3676 = CLASSTYPE_DECLARED_CLASS (template_type);
3677 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3678 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
3679 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
3680 }
3681
3682 /* If we called start_enum above, this information will already
3683 be set up. */
3684 if (!TYPE_NAME (t))
3685 {
3686 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3687
3688 /* Create a stub TYPE_DECL for it. */
3689 type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t);
3690 SET_DECL_ARTIFICIAL (type_decl);
3691 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
3692 DECL_SOURCE_FILE (type_decl)
3693 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
3694 DECL_SOURCE_LINE (type_decl)
3695 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
3696 TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl;
3697 }
3698 else
3699 type_decl = TYPE_NAME (t);
3700
3701 /* Set up the template information. We have to figure out which
3702 template is the immediate parent if this is a full
3703 instantiation. */
3704 if (parm_depth == 1 || is_partial_instantiation
3705 || !PRIMARY_TEMPLATE_P (template))
3706 /* This case is easy; there are no member templates involved. */
3707 found = template;
3708 else
3709 {
3710 /* This is a full instantiation of a member template. There
3711 should be some partial instantiation of which this is an
3712 instance. */
3713
3714 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3715 found; found = TREE_CHAIN (found))
3716 {
3717 int success;
3718 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
3719
3720 /* We only want partial instantiations, here, not
3721 specializations or full instantiations. */
3722 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
3723 || !uses_template_parms (TREE_VALUE (found)))
3724 continue;
3725
3726 /* Temporarily reduce by one the number of levels in the
3727 ARGLIST and in FOUND so as to avoid comparing the
3728 last set of arguments. */
3729 TREE_VEC_LENGTH (arglist)--;
3730 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
3731
3732 /* See if the arguments match. If they do, then TMPL is
3733 the partial instantiation we want. */
3734 success = comp_template_args (TREE_PURPOSE (found), arglist);
3735
3736 /* Restore the argument vectors to their full size. */
3737 TREE_VEC_LENGTH (arglist)++;
3738 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
3739
3740 if (success)
3741 {
3742 found = tmpl;
3743 break;
3744 }
3745 }
3746
3747 if (!found)
3748 my_friendly_abort (0);
3749 }
3750
3751 arglist = copy_to_permanent (arglist);
3752 SET_TYPE_TEMPLATE_INFO (t,
3753 tree_cons (found, arglist, NULL_TREE));
3754 DECL_TEMPLATE_INSTANTIATIONS (template)
3755 = tree_cons (arglist, t,
3756 DECL_TEMPLATE_INSTANTIATIONS (template));
3757
3758 if (TREE_CODE (t) == ENUMERAL_TYPE
3759 && !is_partial_instantiation)
3760 /* Now that the type has been registered on the instantiations
3761 list, we set up the enumerators. Because the enumeration
3762 constants may involve the enumeration type itself, we make
3763 sure to register the type first, and then create the
3764 constants. That way, doing tsubst_expr for the enumeration
3765 constants won't result in recursive calls here; we'll find
3766 the instantiation and exit above. */
3767 tsubst_enum (template_type, t, arglist);
3768
3769 /* We're done with the permanent obstack, now. */
3770 pop_obstacks ();
3771 /* We're also done with the momentary allocation we started
3772 above. */
3773 pop_momentary ();
3774
3775 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3776 is set up. */
3777 if (TREE_CODE (t) != ENUMERAL_TYPE)
3778 DECL_NAME (type_decl) = classtype_mangled_name (t);
3779 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
3780 if (!is_partial_instantiation)
3781 {
3782 DECL_ASSEMBLER_NAME (type_decl)
3783 = get_identifier (build_overload_name (t, 1, 1));
3784
3785 /* For backwards compatibility; code that uses
3786 -fexternal-templates expects looking up a template to
3787 instantiate it. I think DDD still relies on this.
3788 (jason 8/20/1998) */
3789 if (TREE_CODE (t) != ENUMERAL_TYPE
3790 && flag_external_templates
3791 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3792 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3793 add_pending_template (t);
3794 }
3795 else
3796 /* If the type makes use of template parameters, the
3797 code that generates debugging information will crash. */
3798 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
3799
3800 return t;
3801 }
3802 }
3803 \f
3804 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3805 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3806 If FN returns non-zero, the iteration is terminated, and
3807 for_each_template_parm returns 1. Otherwise, the iteration
3808 continues. If FN never returns a non-zero value, the value
3809 returned by for_each_template_parm is 0. If FN is NULL, it is
3810 considered to be the function which always returns 1. */
3811
3812 int
3813 for_each_template_parm (t, fn, data)
3814 tree t;
3815 tree_fn_t fn;
3816 void* data;
3817 {
3818 if (!t)
3819 return 0;
3820
3821 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
3822 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
3823 return 1;
3824
3825 switch (TREE_CODE (t))
3826 {
3827 case INDIRECT_REF:
3828 case COMPONENT_REF:
3829 /* We assume that the object must be instantiated in order to build
3830 the COMPONENT_REF, so we test only whether the type of the
3831 COMPONENT_REF uses template parms. */
3832 return for_each_template_parm (TREE_TYPE (t), fn, data);
3833
3834 case IDENTIFIER_NODE:
3835 if (!IDENTIFIER_TEMPLATE (t))
3836 return 0;
3837 my_friendly_abort (42);
3838
3839 /* aggregates of tree nodes */
3840 case TREE_VEC:
3841 {
3842 int i = TREE_VEC_LENGTH (t);
3843 while (i--)
3844 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
3845 return 1;
3846 return 0;
3847 }
3848 case TREE_LIST:
3849 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
3850 || for_each_template_parm (TREE_VALUE (t), fn, data))
3851 return 1;
3852 return for_each_template_parm (TREE_CHAIN (t), fn, data);
3853
3854 case OVERLOAD:
3855 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
3856 return 1;
3857 return for_each_template_parm (OVL_CHAIN (t), fn, data);
3858
3859 /* constructed type nodes */
3860 case POINTER_TYPE:
3861 case REFERENCE_TYPE:
3862 return for_each_template_parm (TREE_TYPE (t), fn, data);
3863
3864 case RECORD_TYPE:
3865 if (TYPE_PTRMEMFUNC_FLAG (t))
3866 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
3867 fn, data);
3868 /* Fall through. */
3869
3870 case UNION_TYPE:
3871 case ENUMERAL_TYPE:
3872 if (! TYPE_TEMPLATE_INFO (t))
3873 return 0;
3874 return for_each_template_parm (TREE_VALUE
3875 (TYPE_TEMPLATE_INFO (t)),
3876 fn, data);
3877 case METHOD_TYPE:
3878 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
3879 return 1;
3880 /* Fall through. */
3881
3882 case FUNCTION_TYPE:
3883 /* Check the parameter types. Since default arguments are not
3884 instantiated until they are needed, the TYPE_ARG_TYPES may
3885 contain expressions that involve template parameters. But,
3886 no-one should be looking at them yet. And, once they're
3887 instantiated, they don't contain template parameters, so
3888 there's no point in looking at them then, either. */
3889 {
3890 tree parm;
3891
3892 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
3893 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
3894 return 1;
3895 }
3896
3897 /* Check the return type, too. */
3898 return for_each_template_parm (TREE_TYPE (t), fn, data);
3899
3900 case ARRAY_TYPE:
3901 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
3902 return 1;
3903 return for_each_template_parm (TREE_TYPE (t), fn, data);
3904 case OFFSET_TYPE:
3905 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
3906 return 1;
3907 return for_each_template_parm (TREE_TYPE (t), fn, data);
3908
3909 /* decl nodes */
3910 case TYPE_DECL:
3911 return for_each_template_parm (TREE_TYPE (t), fn, data);
3912
3913 case TEMPLATE_DECL:
3914 /* A template template parameter is encountered */
3915 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3916 return for_each_template_parm (TREE_TYPE (t), fn, data);
3917 /* Already substituted template template parameter */
3918 return 0;
3919
3920 case CONST_DECL:
3921 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3922 return 1;
3923 goto check_type_and_context;
3924
3925 case FUNCTION_DECL:
3926 case VAR_DECL:
3927 /* ??? What about FIELD_DECLs? */
3928 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
3929 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
3930 return 1;
3931 /* fall through */
3932 case PARM_DECL:
3933 check_type_and_context:
3934 if (for_each_template_parm (TREE_TYPE (t), fn, data))
3935 return 1;
3936 if (DECL_CONTEXT (t)
3937 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
3938 return 1;
3939 return 0;
3940
3941 case CALL_EXPR:
3942 return for_each_template_parm (TREE_TYPE (t), fn, data);
3943 case ADDR_EXPR:
3944 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3945
3946 /* template parm nodes */
3947 case TEMPLATE_TEMPLATE_PARM:
3948 /* Record template parameters such as `T' inside `TT<T>'. */
3949 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
3950 && for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
3951 return 1;
3952 case TEMPLATE_TYPE_PARM:
3953 case TEMPLATE_PARM_INDEX:
3954 if (fn)
3955 return (*fn)(t, data);
3956 else
3957 return 1;
3958
3959 /* simple type nodes */
3960 case INTEGER_TYPE:
3961 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
3962 return 1;
3963 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
3964
3965 case REAL_TYPE:
3966 case COMPLEX_TYPE:
3967 case VOID_TYPE:
3968 case BOOLEAN_TYPE:
3969 case NAMESPACE_DECL:
3970 return 0;
3971
3972 /* constants */
3973 case INTEGER_CST:
3974 case REAL_CST:
3975 case STRING_CST:
3976 return 0;
3977
3978 case ERROR_MARK:
3979 /* Non-error_mark_node ERROR_MARKs are bad things. */
3980 my_friendly_assert (t == error_mark_node, 274);
3981 /* NOTREACHED */
3982 return 0;
3983
3984 case LOOKUP_EXPR:
3985 case TYPENAME_TYPE:
3986 return 1;
3987
3988 case PTRMEM_CST:
3989 return for_each_template_parm (TREE_TYPE (t), fn, data);
3990
3991 case SCOPE_REF:
3992 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3993
3994 case CONSTRUCTOR:
3995 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3996 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3997 (TREE_TYPE (t)), fn, data);
3998 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
3999
4000 case MODOP_EXPR:
4001 case CAST_EXPR:
4002 case REINTERPRET_CAST_EXPR:
4003 case CONST_CAST_EXPR:
4004 case STATIC_CAST_EXPR:
4005 case DYNAMIC_CAST_EXPR:
4006 case ARROW_EXPR:
4007 case DOTSTAR_EXPR:
4008 case TYPEID_EXPR:
4009 return 1;
4010
4011 case SIZEOF_EXPR:
4012 case ALIGNOF_EXPR:
4013 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4014
4015 default:
4016 switch (TREE_CODE_CLASS (TREE_CODE (t)))
4017 {
4018 case '1':
4019 case '2':
4020 case 'e':
4021 case '<':
4022 {
4023 int i;
4024 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
4025 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
4026 return 1;
4027 return 0;
4028 }
4029 default:
4030 break;
4031 }
4032 sorry ("testing %s for template parms",
4033 tree_code_name [(int) TREE_CODE (t)]);
4034 my_friendly_abort (82);
4035 /* NOTREACHED */
4036 return 0;
4037 }
4038 }
4039
4040 int
4041 uses_template_parms (t)
4042 tree t;
4043 {
4044 return for_each_template_parm (t, 0, 0);
4045 }
4046
4047 static struct tinst_level *current_tinst_level;
4048 static struct tinst_level *free_tinst_level;
4049 static int tinst_depth;
4050 extern int max_tinst_depth;
4051 #ifdef GATHER_STATISTICS
4052 int depth_reached;
4053 #endif
4054 int tinst_level_tick;
4055 int last_template_error_tick;
4056
4057 /* Print out all the template instantiations that we are currently
4058 working on. If ERR, we are being called from cp_thing, so do
4059 the right thing for an error message. */
4060
4061 static void
4062 print_template_context (err)
4063 int err;
4064 {
4065 struct tinst_level *p = current_tinst_level;
4066 int line = lineno;
4067 char *file = input_filename;
4068
4069 if (err && p)
4070 {
4071 if (current_function_decl != p->decl
4072 && current_function_decl != NULL_TREE)
4073 /* We can get here during the processing of some synthesized
4074 method. Then, p->decl will be the function that's causing
4075 the synthesis. */
4076 ;
4077 else
4078 {
4079 if (current_function_decl == p->decl)
4080 /* Avoid redundancy with the the "In function" line. */;
4081 else
4082 fprintf (stderr, "%s: In instantiation of `%s':\n",
4083 file, decl_as_string (p->decl, 0));
4084
4085 line = p->line;
4086 file = p->file;
4087 p = p->next;
4088 }
4089 }
4090
4091 for (; p; p = p->next)
4092 {
4093 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
4094 decl_as_string (p->decl, 0));
4095 line = p->line;
4096 file = p->file;
4097 }
4098 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
4099 }
4100
4101 /* Called from cp_thing to print the template context for an error. */
4102
4103 void
4104 maybe_print_template_context ()
4105 {
4106 if (last_template_error_tick == tinst_level_tick
4107 || current_tinst_level == 0)
4108 return;
4109
4110 last_template_error_tick = tinst_level_tick;
4111 print_template_context (1);
4112 }
4113
4114 static int
4115 push_tinst_level (d)
4116 tree d;
4117 {
4118 struct tinst_level *new;
4119
4120 if (tinst_depth >= max_tinst_depth)
4121 {
4122 /* If the instantiation in question still has unbound template parms,
4123 we don't really care if we can't instantiate it, so just return.
4124 This happens with base instantiation for implicit `typename'. */
4125 if (uses_template_parms (d))
4126 return 0;
4127
4128 last_template_error_tick = tinst_level_tick;
4129 error ("template instantiation depth exceeds maximum of %d",
4130 max_tinst_depth);
4131 error (" (use -ftemplate-depth-NN to increase the maximum)");
4132 cp_error (" instantiating `%D'", d);
4133
4134 print_template_context (0);
4135
4136 return 0;
4137 }
4138
4139 if (free_tinst_level)
4140 {
4141 new = free_tinst_level;
4142 free_tinst_level = new->next;
4143 }
4144 else
4145 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
4146
4147 new->decl = d;
4148 new->line = lineno;
4149 new->file = input_filename;
4150 new->next = current_tinst_level;
4151 current_tinst_level = new;
4152
4153 ++tinst_depth;
4154 #ifdef GATHER_STATISTICS
4155 if (tinst_depth > depth_reached)
4156 depth_reached = tinst_depth;
4157 #endif
4158
4159 ++tinst_level_tick;
4160 return 1;
4161 }
4162
4163 void
4164 pop_tinst_level ()
4165 {
4166 struct tinst_level *old = current_tinst_level;
4167
4168 /* Restore the filename and line number stashed away when we started
4169 this instantiation. */
4170 lineno = old->line;
4171 input_filename = old->file;
4172 extract_interface_info ();
4173
4174 current_tinst_level = old->next;
4175 old->next = free_tinst_level;
4176 free_tinst_level = old;
4177 --tinst_depth;
4178 ++tinst_level_tick;
4179 }
4180
4181 struct tinst_level *
4182 tinst_for_decl ()
4183 {
4184 struct tinst_level *p = current_tinst_level;
4185
4186 if (p)
4187 for (; p->next ; p = p->next )
4188 ;
4189 return p;
4190 }
4191
4192 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4193 vector of template arguments, as for tsubst.
4194
4195 Returns an appropriate tsbust'd friend declaration. */
4196
4197 static tree
4198 tsubst_friend_function (decl, args)
4199 tree decl;
4200 tree args;
4201 {
4202 tree new_friend;
4203 int line = lineno;
4204 char *file = input_filename;
4205
4206 lineno = DECL_SOURCE_LINE (decl);
4207 input_filename = DECL_SOURCE_FILE (decl);
4208
4209 if (TREE_CODE (decl) == FUNCTION_DECL
4210 && DECL_TEMPLATE_INSTANTIATION (decl)
4211 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4212 /* This was a friend declared with an explicit template
4213 argument list, e.g.:
4214
4215 friend void f<>(T);
4216
4217 to indicate that f was a template instantiation, not a new
4218 function declaration. Now, we have to figure out what
4219 instantiation of what template. */
4220 {
4221 tree template_id;
4222 tree new_args;
4223 tree tmpl;
4224
4225 template_id
4226 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
4227 args, NULL_TREE),
4228 tsubst (DECL_TI_ARGS (decl),
4229 args, NULL_TREE));
4230 /* FIXME: The decl we create via the next tsubst could be
4231 created on a temporary obstack. */
4232 new_friend = tsubst (decl, args, NULL_TREE);
4233 tmpl = determine_specialization (template_id, new_friend,
4234 &new_args,
4235 /*need_member_template=*/0,
4236 /*complain=*/1);
4237 new_friend = instantiate_template (tmpl, new_args);
4238 goto done;
4239 }
4240
4241 new_friend = tsubst (decl, args, NULL_TREE);
4242
4243 /* The NEW_FRIEND will look like an instantiation, to the
4244 compiler, but is not an instantiation from the point of view of
4245 the language. For example, we might have had:
4246
4247 template <class T> struct S {
4248 template <class U> friend void f(T, U);
4249 };
4250
4251 Then, in S<int>, template <class U> void f(int, U) is not an
4252 instantiation of anything. */
4253 DECL_USE_TEMPLATE (new_friend) = 0;
4254 if (TREE_CODE (decl) == TEMPLATE_DECL)
4255 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4256
4257 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4258 tsubst will have resulted in a call to
4259 set_mangled_name_for_template_decl. But, the function is not a
4260 template instantiation and should not be mangled like one.
4261 Therefore, we remangle the function name. We don't have to do
4262 this if the NEW_FRIEND is a template since
4263 set_mangled_name_for_template_decl doesn't do anything if the
4264 function declaration still uses template arguments. */
4265 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4266 {
4267 set_mangled_name_for_decl (new_friend);
4268 DECL_RTL (new_friend) = 0;
4269 make_decl_rtl (new_friend, NULL_PTR, 1);
4270 }
4271
4272 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4273 {
4274 tree old_decl;
4275 tree new_friend_template_info;
4276 tree new_friend_result_template_info;
4277 int new_friend_is_defn;
4278
4279 /* We must save some information from NEW_FRIEND before calling
4280 duplicate decls since that function will free NEW_FRIEND if
4281 possible. */
4282 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4283 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4284 {
4285 /* This declaration is a `primary' template. */
4286 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4287
4288 new_friend_is_defn
4289 = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
4290 new_friend_result_template_info
4291 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
4292 }
4293 else
4294 {
4295 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4296 new_friend_result_template_info = NULL_TREE;
4297 }
4298
4299 old_decl = pushdecl_namespace_level (new_friend);
4300
4301 if (old_decl != new_friend)
4302 {
4303 /* This new friend declaration matched an existing
4304 declaration. For example, given:
4305
4306 template <class T> void f(T);
4307 template <class U> class C {
4308 template <class T> friend void f(T) {}
4309 };
4310
4311 the friend declaration actually provides the definition
4312 of `f', once C has been instantiated for some type. So,
4313 old_decl will be the out-of-class template declaration,
4314 while new_friend is the in-class definition.
4315
4316 But, if `f' was called before this point, the
4317 instantiation of `f' will have DECL_TI_ARGS corresponding
4318 to `T' but not to `U', references to which might appear
4319 in the definition of `f'. Previously, the most general
4320 template for an instantiation of `f' was the out-of-class
4321 version; now it is the in-class version. Therefore, we
4322 run through all specialization of `f', adding to their
4323 DECL_TI_ARGS appropriately. In particular, they need a
4324 new set of outer arguments, corresponding to the
4325 arguments for this class instantiation.
4326
4327 The same situation can arise with something like this:
4328
4329 friend void f(int);
4330 template <class T> class C {
4331 friend void f(T) {}
4332 };
4333
4334 when `C<int>' is instantiated. Now, `f(int)' is defined
4335 in the class. */
4336
4337 if (!new_friend_is_defn)
4338 /* On the other hand, if the in-class declaration does
4339 *not* provide a definition, then we don't want to alter
4340 existing definitions. We can just leave everything
4341 alone. */
4342 ;
4343 else
4344 {
4345 /* Overwrite whatever template info was there before, if
4346 any, with the new template information pertaining to
4347 the declaration. */
4348 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4349
4350 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4351 /* duplicate_decls will take care of this case. */
4352 ;
4353 else
4354 {
4355 tree t;
4356 tree new_friend_args;
4357
4358 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
4359 = new_friend_result_template_info;
4360
4361 new_friend_args = TI_ARGS (new_friend_template_info);
4362 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4363 t != NULL_TREE;
4364 t = TREE_CHAIN (t))
4365 {
4366 tree spec = TREE_VALUE (t);
4367
4368 DECL_TI_ARGS (spec)
4369 = add_outermost_template_args (new_friend_args,
4370 DECL_TI_ARGS (spec));
4371 DECL_TI_ARGS (spec)
4372 = copy_to_permanent (DECL_TI_ARGS (spec));
4373 }
4374
4375 /* Now, since specializations are always supposed to
4376 hang off of the most general template, we must move
4377 them. */
4378 t = most_general_template (old_decl);
4379 if (t != old_decl)
4380 {
4381 DECL_TEMPLATE_SPECIALIZATIONS (t)
4382 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4383 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4384 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4385 }
4386 }
4387 }
4388
4389 /* The information from NEW_FRIEND has been merged into OLD_DECL
4390 by duplicate_decls. */
4391 new_friend = old_decl;
4392 }
4393 }
4394 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4395 {
4396 /* Check to see that the declaration is really present, and,
4397 possibly obtain an improved declaration. */
4398 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4399 new_friend);
4400
4401 if (fn)
4402 new_friend = fn;
4403 }
4404
4405 done:
4406 lineno = line;
4407 input_filename = file;
4408 return new_friend;
4409 }
4410
4411 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4412 template arguments, as for tsubst.
4413
4414 Returns an appropriate tsbust'd friend type. */
4415
4416 static tree
4417 tsubst_friend_class (friend_tmpl, args)
4418 tree friend_tmpl;
4419 tree args;
4420 {
4421 tree friend_type;
4422 tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
4423
4424 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4425
4426 if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl))
4427 {
4428 /* The friend template has already been declared. Just
4429 check to see that the declarations match, and install any new
4430 default parameters. We must tsubst the default parameters,
4431 of course. We only need the innermost template parameters
4432 because that is all that redeclare_class_template will look
4433 at. */
4434 tree parms
4435 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4436 args);
4437 redeclare_class_template (TREE_TYPE (tmpl), parms);
4438 friend_type = TREE_TYPE (tmpl);
4439 }
4440 else
4441 {
4442 /* The friend template has not already been declared. In this
4443 case, the instantiation of the template class will cause the
4444 injection of this template into the global scope. */
4445 tmpl = tsubst (friend_tmpl, args, NULL_TREE);
4446
4447 /* The new TMPL is not an instantiation of anything, so we
4448 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4449 the new type because that is supposed to be the corresponding
4450 template decl, i.e., TMPL. */
4451 DECL_USE_TEMPLATE (tmpl) = 0;
4452 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4453 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4454
4455 /* Inject this template into the global scope. */
4456 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4457 }
4458
4459 return friend_type;
4460 }
4461
4462 tree
4463 instantiate_class_template (type)
4464 tree type;
4465 {
4466 tree template, args, pattern, t;
4467 tree typedecl;
4468 int is_partial_instantiation;
4469
4470 if (type == error_mark_node)
4471 return error_mark_node;
4472
4473 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4474 return type;
4475
4476 /* We want to allocate temporary vectors of template arguments and
4477 template argument expressions on the momentary obstack, not on
4478 the expression obstack. Otherwise, all the space allocated in
4479 argument coercion and such is simply lost. */
4480 push_momentary ();
4481
4482 /* Figure out which template is being instantiated. */
4483 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4484 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4485
4486 /* Figure out which arguments are being used to do the
4487 instantiation. */
4488 args = CLASSTYPE_TI_ARGS (type);
4489 is_partial_instantiation = uses_template_parms (args);
4490
4491 if (is_partial_instantiation)
4492 /* There's no telling which specialization is appropriate at this
4493 point. Since all peeking at the innards of this partial
4494 instantiation are extensions (like the "implicit typename"
4495 extension, which allows users to omit the keyword `typename' on
4496 names that are declared as types in template base classes), we
4497 are free to do what we please.
4498
4499 Trying to figure out which partial instantiation to use can
4500 cause a crash. (Some of the template arguments don't even have
4501 types.) So, we just use the most general version. */
4502 t = NULL_TREE;
4503 else
4504 {
4505 t = most_specialized_class (template, args);
4506
4507 if (t == error_mark_node)
4508 {
4509 char *str = "candidates are:";
4510 cp_error ("ambiguous class template instantiation for `%#T'", type);
4511 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4512 t = TREE_CHAIN (t))
4513 {
4514 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4515 args))
4516 {
4517 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4518 str = " ";
4519 }
4520 }
4521 TYPE_BEING_DEFINED (type) = 1;
4522 type = error_mark_node;
4523 goto end;
4524 }
4525 }
4526
4527 if (t)
4528 pattern = TREE_TYPE (t);
4529 else
4530 pattern = TREE_TYPE (template);
4531
4532 if (TYPE_SIZE (pattern) == NULL_TREE)
4533 goto end;
4534
4535 if (t)
4536 {
4537 /* This TYPE is actually a instantiation of of a partial
4538 specialization. We replace the innermost set of ARGS with
4539 the arguments appropriate for substitution. For example,
4540 given:
4541
4542 template <class T> struct S {};
4543 template <class T> struct S<T*> {};
4544
4545 and supposing that we are instantiating S<int*>, ARGS will
4546 present be {int*} but we need {int}. */
4547 tree inner_args
4548 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4549 args);
4550
4551 /* If there were multiple levels in ARGS, replacing the
4552 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4553 want, so we make a copy first. */
4554 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4555 {
4556 args = copy_node (args);
4557 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4558 }
4559 else
4560 args = inner_args;
4561 }
4562
4563 if (pedantic && is_partial_instantiation)
4564 {
4565 /* If this is a partial instantiation, then we can't instantiate
4566 the type; there's no telling whether or not one of the
4567 template parameters might eventually be instantiated to some
4568 value that results in a specialization being used. We do
4569 mark the type as complete so that, for example, declaring one
4570 of its members to be a friend will not be rejected. */
4571 TYPE_SIZE (type) = integer_zero_node;
4572 goto end;
4573 }
4574
4575 TYPE_BEING_DEFINED (type) = 1;
4576
4577 if (! push_tinst_level (type))
4578 goto end;
4579
4580 maybe_push_to_top_level (uses_template_parms (type));
4581 pushclass (type, 0);
4582
4583 /* We must copy the arguments to the permanent obstack since
4584 during the tsubst'ing below they may wind up in the
4585 DECL_TI_ARGS of some instantiated member template. */
4586 args = copy_to_permanent (args);
4587
4588 if (flag_external_templates)
4589 {
4590 if (flag_alt_external_templates)
4591 {
4592 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4593 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4594 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4595 = (! CLASSTYPE_INTERFACE_ONLY (type)
4596 && CLASSTYPE_INTERFACE_KNOWN (type));
4597 }
4598 else
4599 {
4600 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4601 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4602 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4603 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4604 = (! CLASSTYPE_INTERFACE_ONLY (type)
4605 && CLASSTYPE_INTERFACE_KNOWN (type));
4606 }
4607 }
4608 else
4609 {
4610 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4611 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
4612 }
4613
4614 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4615 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4616 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
4617 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4618 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4619 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4620 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4621 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4622 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4623 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
4624 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
4625 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
4626 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
4627 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
4628 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
4629 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4630 TYPE_USES_COMPLEX_INHERITANCE (type)
4631 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
4632 TYPE_USES_MULTIPLE_INHERITANCE (type)
4633 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
4634 TYPE_USES_VIRTUAL_BASECLASSES (type)
4635 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
4636 TYPE_PACKED (type) = TYPE_PACKED (pattern);
4637 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
4638 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
4639
4640 /* If this is a partial instantiation, don't tsubst anything. We will
4641 only use this type for implicit typename, so the actual contents don't
4642 matter. All that matters is whether a particular name is a type. */
4643 if (is_partial_instantiation)
4644 {
4645 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4646 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4647 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4648 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4649 TYPE_SIZE (type) = integer_zero_node;
4650 goto done_with_instantiation;
4651 }
4652
4653 {
4654 tree binfo = TYPE_BINFO (type);
4655 tree pbases = TYPE_BINFO_BASETYPES (pattern);
4656
4657 if (pbases)
4658 {
4659 tree bases;
4660 int i;
4661 int len = TREE_VEC_LENGTH (pbases);
4662 bases = make_tree_vec (len);
4663 for (i = 0; i < len; ++i)
4664 {
4665 tree elt, basetype;
4666
4667 TREE_VEC_ELT (bases, i) = elt
4668 = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
4669 BINFO_INHERITANCE_CHAIN (elt) = binfo;
4670
4671 basetype = TREE_TYPE (elt);
4672
4673 if (! IS_AGGR_TYPE (basetype))
4674 cp_error
4675 ("base type `%T' of `%T' fails to be a struct or class type",
4676 basetype, type);
4677 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE)
4678 cp_error ("base class `%T' of `%T' has incomplete type",
4679 basetype, type);
4680
4681 /* These are set up in xref_basetypes for normal classes, so
4682 we have to handle them here for template bases. */
4683
4684 unshare_base_binfos (elt);
4685
4686 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4687 {
4688 TYPE_USES_VIRTUAL_BASECLASSES (type) = 1;
4689 TYPE_USES_COMPLEX_INHERITANCE (type) = 1;
4690 }
4691 TYPE_GETS_NEW (type) |= TYPE_GETS_NEW (basetype);
4692 TYPE_GETS_DELETE (type) |= TYPE_GETS_DELETE (basetype);
4693 }
4694 /* Don't initialize this until the vector is filled out, or
4695 lookups will crash. */
4696 BINFO_BASETYPES (binfo) = bases;
4697 }
4698 }
4699
4700 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
4701 {
4702 tree tag = TREE_VALUE (t);
4703 tree name = TYPE_IDENTIFIER (tag);
4704 tree newtag;
4705
4706 newtag = tsubst (tag, args, NULL_TREE);
4707 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
4708 {
4709 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
4710 /* Unfortunately, lookup_template_class sets
4711 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4712 instantiation (i.e., for the type of a member template
4713 class nested within a template class.) This behavior is
4714 required for maybe_process_partial_specialization to work
4715 correctly, but is not accurate in this case; the TAG is not
4716 an instantiation of anything. (The corresponding
4717 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4718 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
4719
4720 /* Now, we call pushtag to put this NEWTAG into the scope of
4721 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4722 pushtag calling push_template_decl. We don't have to do
4723 this for enums because it will already have been done in
4724 tsubst_enum. */
4725 if (name)
4726 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
4727 pushtag (name, newtag, /*globalize=*/0);
4728 }
4729 }
4730
4731 /* Don't replace enum constants here. */
4732 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
4733 if (TREE_CODE (t) != CONST_DECL)
4734 {
4735 tree r;
4736
4737 /* The the file and line for this declaration, to assist in
4738 error message reporting. Since we called push_tinst_level
4739 above, we don't need to restore these. */
4740 lineno = DECL_SOURCE_LINE (t);
4741 input_filename = DECL_SOURCE_FILE (t);
4742
4743 r = tsubst (t, args, NULL_TREE);
4744 if (TREE_CODE (r) == VAR_DECL)
4745 {
4746 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
4747 /* Perhaps we should do more of grokfield here. */
4748 start_decl_1 (r);
4749 DECL_IN_AGGR_P (r) = 1;
4750 DECL_EXTERNAL (r) = 1;
4751 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
4752 if (DECL_DEFINED_IN_CLASS_P (r))
4753 check_static_variable_definition (r, TREE_TYPE (r));
4754 }
4755
4756 /* R will have a TREE_CHAIN if and only if it has already been
4757 processed by finish_member_declaration. This can happen
4758 if, for example, it is a TYPE_DECL for a class-scoped
4759 ENUMERAL_TYPE; such a thing will already have been added to
4760 the field list by tsubst_enum above. */
4761 if (!TREE_CHAIN (r))
4762 {
4763 set_current_access_from_decl (r);
4764 finish_member_declaration (r);
4765 }
4766 }
4767
4768 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4769 for this instantiation. */
4770 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
4771 {
4772 tree r = tsubst (t, args, NULL_TREE);
4773 set_current_access_from_decl (r);
4774 finish_member_declaration (r);
4775 }
4776
4777 /* Construct the DECL_FRIENDLIST for the new class type. */
4778 typedecl = TYPE_MAIN_DECL (type);
4779 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
4780 t != NULL_TREE;
4781 t = TREE_CHAIN (t))
4782 {
4783 tree friends;
4784
4785 DECL_FRIENDLIST (typedecl)
4786 = tree_cons (TREE_PURPOSE (t), NULL_TREE,
4787 DECL_FRIENDLIST (typedecl));
4788
4789 for (friends = TREE_VALUE (t);
4790 friends != NULL_TREE;
4791 friends = TREE_CHAIN (friends))
4792 {
4793 if (TREE_PURPOSE (friends) == error_mark_node)
4794 {
4795 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4796 = tree_cons (error_mark_node,
4797 tsubst_friend_function (TREE_VALUE (friends),
4798 args),
4799 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4800 }
4801 else
4802 {
4803 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4804 = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
4805 NULL_TREE,
4806 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4807
4808 }
4809 }
4810 }
4811
4812 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
4813 t != NULL_TREE;
4814 t = TREE_CHAIN (t))
4815 {
4816 tree friend_type = TREE_VALUE (t);
4817 tree new_friend_type;
4818
4819 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4820 new_friend_type = tsubst_friend_class (friend_type, args);
4821 else if (uses_template_parms (friend_type))
4822 new_friend_type = tsubst (friend_type, args, NULL_TREE);
4823 else
4824 /* The call to xref_tag_from_type does injection for friend
4825 classes. */
4826 new_friend_type =
4827 xref_tag_from_type (friend_type, NULL_TREE, 1);
4828
4829
4830 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4831 /* Trick make_friend_class into realizing that the friend
4832 we're adding is a template, not an ordinary class. It's
4833 important that we use make_friend_class since it will
4834 perform some error-checking and output cross-reference
4835 information. */
4836 ++processing_template_decl;
4837
4838 make_friend_class (type, new_friend_type);
4839
4840 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4841 --processing_template_decl;
4842 }
4843
4844 /* This does injection for friend functions. */
4845 if (!processing_template_decl)
4846 {
4847 t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
4848
4849 for (; t; t = TREE_CHAIN (t))
4850 {
4851 tree d = TREE_VALUE (t);
4852
4853 if (TREE_CODE (d) == TYPE_DECL)
4854 /* Already injected. */;
4855 else
4856 pushdecl (d);
4857 }
4858 }
4859
4860 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
4861 if (TREE_CODE (t) == FIELD_DECL)
4862 {
4863 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
4864 require_complete_type (t);
4865 }
4866
4867 /* Set the file and line number information to whatever is given for
4868 the class itself. This puts error messages involving generated
4869 implicit functions at a predictable point, and the same point
4870 that would be used for non-template classes. */
4871 lineno = DECL_SOURCE_LINE (typedecl);
4872 input_filename = DECL_SOURCE_FILE (typedecl);
4873
4874 unreverse_member_declarations (type);
4875 type = finish_struct_1 (type, 0);
4876 CLASSTYPE_GOT_SEMICOLON (type) = 1;
4877
4878 /* Clear this now so repo_template_used is happy. */
4879 TYPE_BEING_DEFINED (type) = 0;
4880 repo_template_used (type);
4881
4882 done_with_instantiation:
4883 TYPE_BEING_DEFINED (type) = 0;
4884 popclass (0);
4885
4886 pop_from_top_level ();
4887 pop_tinst_level ();
4888
4889 end:
4890 pop_momentary ();
4891
4892 return type;
4893 }
4894
4895 static int
4896 list_eq (t1, t2)
4897 tree t1, t2;
4898 {
4899 if (t1 == NULL_TREE)
4900 return t2 == NULL_TREE;
4901 if (t2 == NULL_TREE)
4902 return 0;
4903 /* Don't care if one declares its arg const and the other doesn't -- the
4904 main variant of the arg type is all that matters. */
4905 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
4906 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
4907 return 0;
4908 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
4909 }
4910
4911 /* If arg is a non-type template parameter that does not depend on template
4912 arguments, fold it like we weren't in the body of a template. */
4913
4914 static tree
4915 maybe_fold_nontype_arg (arg)
4916 tree arg;
4917 {
4918 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
4919 && !uses_template_parms (arg))
4920 {
4921 /* Sometimes, one of the args was an expression involving a
4922 template constant parameter, like N - 1. Now that we've
4923 tsubst'd, we might have something like 2 - 1. This will
4924 confuse lookup_template_class, so we do constant folding
4925 here. We have to unset processing_template_decl, to
4926 fool build_expr_from_tree() into building an actual
4927 tree. */
4928
4929 int saved_processing_template_decl = processing_template_decl;
4930 processing_template_decl = 0;
4931 arg = fold (build_expr_from_tree (arg));
4932 processing_template_decl = saved_processing_template_decl;
4933 }
4934 return arg;
4935 }
4936
4937 /* Return the TREE_VEC with the arguments for the innermost template header,
4938 where ARGS is either that or the VEC of VECs for all the
4939 arguments. */
4940
4941 tree
4942 innermost_args (args)
4943 tree args;
4944 {
4945 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
4946 }
4947
4948 /* Substitute ARGS into the vector of template arguments T. */
4949
4950 tree
4951 tsubst_template_arg_vector (t, args)
4952 tree t;
4953 tree args;
4954 {
4955 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
4956 tree *elts = (tree *) alloca (len * sizeof (tree));
4957
4958 bzero ((char *) elts, len * sizeof (tree));
4959
4960 for (i = 0; i < len; i++)
4961 {
4962 if (TREE_VEC_ELT (t, i) != NULL_TREE
4963 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4964 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args);
4965 else
4966 elts[i] = maybe_fold_nontype_arg
4967 (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE));
4968
4969 if (elts[i] != TREE_VEC_ELT (t, i))
4970 need_new = 1;
4971 }
4972
4973 if (!need_new)
4974 return t;
4975
4976 t = make_temp_vec (len);
4977 for (i = 0; i < len; i++)
4978 TREE_VEC_ELT (t, i) = elts[i];
4979
4980 return t;
4981 }
4982
4983 /* Return the result of substituting ARGS into the template parameters
4984 given by PARMS. If there are m levels of ARGS and m + n levels of
4985 PARMS, then the result will contain n levels of PARMS. For
4986 example, if PARMS is `template <class T> template <class U>
4987 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
4988 result will be `template <int*, double, class V>'. */
4989
4990 tree
4991 tsubst_template_parms (parms, args)
4992 tree parms;
4993 tree args;
4994 {
4995 tree r;
4996 tree* new_parms = &r;
4997
4998 for (new_parms = &r;
4999 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5000 new_parms = &(TREE_CHAIN (*new_parms)),
5001 parms = TREE_CHAIN (parms))
5002 {
5003 tree new_vec =
5004 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5005 int i;
5006
5007 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5008 {
5009 tree default_value =
5010 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5011 tree parm_decl =
5012 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5013
5014 TREE_VEC_ELT (new_vec, i)
5015 = build_tree_list (tsubst (default_value, args, NULL_TREE),
5016 tsubst (parm_decl, args, NULL_TREE));
5017
5018 }
5019
5020 *new_parms =
5021 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
5022 - TMPL_ARGS_DEPTH (args))),
5023 new_vec, NULL_TREE);
5024 }
5025
5026 return r;
5027 }
5028
5029 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5030 type T. If T is not an aggregate or enumeration type, it is
5031 handled as if by tsubst. IN_DECL is as for tsubst. If
5032 ENTERING_SCOPE is non-zero, T is the context for a template which
5033 we are presently tsubst'ing. Return the subsituted value. */
5034
5035 tree
5036 tsubst_aggr_type (t, args, in_decl, entering_scope)
5037 tree t;
5038 tree args;
5039 tree in_decl;
5040 int entering_scope;
5041 {
5042 if (t == NULL_TREE)
5043 return NULL_TREE;
5044
5045 switch (TREE_CODE (t))
5046 {
5047 case RECORD_TYPE:
5048 if (TYPE_PTRMEMFUNC_P (t))
5049 {
5050 tree r = build_ptrmemfunc_type
5051 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
5052 return cp_build_qualified_type (r, TYPE_QUALS (t));
5053 }
5054
5055 /* else fall through */
5056 case ENUMERAL_TYPE:
5057 case UNION_TYPE:
5058 if (uses_template_parms (t))
5059 {
5060 tree argvec;
5061 tree context;
5062 tree r;
5063
5064 /* First, determine the context for the type we are looking
5065 up. */
5066 if (TYPE_CONTEXT (t) != NULL_TREE)
5067 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5068 in_decl, /*entering_scope=*/1);
5069 else
5070 context = NULL_TREE;
5071
5072 /* Then, figure out what arguments are appropriate for the
5073 type we are trying to find. For example, given:
5074
5075 template <class T> struct S;
5076 template <class T, class U> void f(T, U) { S<U> su; }
5077
5078 and supposing that we are instantiating f<int, double>,
5079 then our ARGS will be {int, double}, but, when looking up
5080 S we only want {double}. */
5081 push_momentary ();
5082 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args);
5083
5084 r = lookup_template_class (t, argvec, in_decl, context,
5085 entering_scope);
5086 pop_momentary ();
5087
5088 return cp_build_qualified_type (r, TYPE_QUALS (t));
5089 }
5090 else
5091 /* This is not a template type, so there's nothing to do. */
5092 return t;
5093
5094 default:
5095 return tsubst (t, args, in_decl);
5096 }
5097 }
5098
5099 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5100 (already computed) substitution of ARGS into TREE_TYPE (T), if
5101 appropriate. Return the result of the substitution. IN_DECL is as
5102 for tsubst. */
5103
5104 tree
5105 tsubst_decl (t, args, type, in_decl)
5106 tree t;
5107 tree args;
5108 tree type;
5109 tree in_decl;
5110 {
5111 int saved_lineno;
5112 char* saved_filename;
5113 tree r = NULL_TREE;
5114
5115 /* Set the filename and linenumber to improve error-reporting. */
5116 saved_lineno = lineno;
5117 saved_filename = input_filename;
5118 lineno = DECL_SOURCE_LINE (t);
5119 input_filename = DECL_SOURCE_FILE (t);
5120
5121 switch (TREE_CODE (t))
5122 {
5123 case TEMPLATE_DECL:
5124 {
5125 /* We can get here when processing a member template function
5126 of a template class. */
5127 tree decl = DECL_TEMPLATE_RESULT (t);
5128 tree spec;
5129 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5130
5131 if (!is_template_template_parm)
5132 {
5133 /* We might already have an instance of this template.
5134 The ARGS are for the surrounding class type, so the
5135 full args contain the tsubst'd args for the context,
5136 plus the innermost args from the template decl. */
5137 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5138 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5139 : DECL_TI_ARGS (DECL_RESULT (t));
5140 tree full_args;
5141
5142 push_momentary ();
5143 full_args = tsubst_template_arg_vector (tmpl_args, args);
5144
5145 /* tsubst_template_arg_vector doesn't copy the vector if
5146 nothing changed. But, *something* should have
5147 changed. */
5148 my_friendly_assert (full_args != tmpl_args, 0);
5149
5150 spec = retrieve_specialization (t, full_args);
5151 pop_momentary ();
5152 if (spec != NULL_TREE)
5153 {
5154 r = spec;
5155 break;
5156 }
5157 }
5158
5159 /* Make a new template decl. It will be similar to the
5160 original, but will record the current template arguments.
5161 We also create a new function declaration, which is just
5162 like the old one, but points to this new template, rather
5163 than the old one. */
5164 r = copy_node (t);
5165 copy_lang_decl (r);
5166 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5167 TREE_CHAIN (r) = NULL_TREE;
5168
5169 if (is_template_template_parm)
5170 {
5171 tree new_decl = tsubst (decl, args, in_decl);
5172 DECL_RESULT (r) = new_decl;
5173 TREE_TYPE (r) = TREE_TYPE (new_decl);
5174 break;
5175 }
5176
5177 DECL_CONTEXT (r)
5178 = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5179 /*entering_scope=*/1);
5180 DECL_CLASS_CONTEXT (r)
5181 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, in_decl,
5182 /*entering_scope=*/1);
5183 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5184
5185 if (TREE_CODE (decl) == TYPE_DECL)
5186 {
5187 tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
5188 TREE_TYPE (r) = new_type;
5189 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5190 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
5191 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5192 }
5193 else
5194 {
5195 tree new_decl = tsubst (decl, args, in_decl);
5196 DECL_RESULT (r) = new_decl;
5197 DECL_TI_TEMPLATE (new_decl) = r;
5198 TREE_TYPE (r) = TREE_TYPE (new_decl);
5199 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5200 }
5201
5202 SET_DECL_IMPLICIT_INSTANTIATION (r);
5203 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5204 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5205
5206 /* The template parameters for this new template are all the
5207 template parameters for the old template, except the
5208 outermost level of parameters. */
5209 DECL_TEMPLATE_PARMS (r)
5210 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args);
5211
5212 if (PRIMARY_TEMPLATE_P (t))
5213 DECL_PRIMARY_TEMPLATE (r) = r;
5214
5215 /* We don't partially instantiate partial specializations. */
5216 if (TREE_CODE (decl) == TYPE_DECL)
5217 break;
5218
5219 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
5220 spec != NULL_TREE;
5221 spec = TREE_CHAIN (spec))
5222 {
5223 /* It helps to consider example here. Consider:
5224
5225 template <class T>
5226 struct S {
5227 template <class U>
5228 void f(U u);
5229
5230 template <>
5231 void f(T* t) {}
5232 };
5233
5234 Now, for example, we are instantiating S<int>::f(U u).
5235 We want to make a template:
5236
5237 template <class U>
5238 void S<int>::f(U);
5239
5240 It will have a specialization, for the case U = int*, of
5241 the form:
5242
5243 template <>
5244 void S<int>::f<int*>(int*);
5245
5246 This specialization will be an instantiation of
5247 the specialization given in the declaration of S, with
5248 argument list int*. */
5249
5250 tree fn = TREE_VALUE (spec);
5251 tree spec_args;
5252 tree new_fn;
5253
5254 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
5255 /* Instantiations are on the same list, but they're of
5256 no concern to us. */
5257 continue;
5258
5259 if (TREE_CODE (fn) != TEMPLATE_DECL)
5260 /* A full specialization. There's no need to record
5261 that here. */
5262 continue;
5263
5264 spec_args = tsubst (DECL_TI_ARGS (fn), args, in_decl);
5265 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
5266 spec_args, in_decl);
5267 DECL_TI_TEMPLATE (new_fn) = fn;
5268 register_specialization (new_fn, r,
5269 innermost_args (spec_args));
5270 }
5271
5272 /* Record this partial instantiation. */
5273 register_specialization (r, t,
5274 DECL_TI_ARGS (DECL_RESULT (r)));
5275
5276 }
5277 break;
5278
5279 case FUNCTION_DECL:
5280 {
5281 tree ctx;
5282 tree argvec = NULL_TREE;
5283 tree gen_tmpl;
5284 int member;
5285 int args_depth;
5286 int parms_depth;
5287
5288 /* Nobody should be tsubst'ing into non-template functions. */
5289 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5290
5291 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5292 {
5293 tree spec;
5294
5295 /* Allocate template arguments on the momentary obstack,
5296 in case we don't need to keep them. */
5297 push_momentary ();
5298
5299 /* Calculate the most general template of which R is a
5300 specialization, and the complete set of arguments used to
5301 specialize R. */
5302 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5303 argvec
5304 = tsubst_template_arg_vector (DECL_TI_ARGS
5305 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5306 args);
5307
5308 /* Check to see if we already have this specialization. */
5309 spec = retrieve_specialization (gen_tmpl, argvec);
5310
5311 if (spec)
5312 {
5313 r = spec;
5314 pop_momentary ();
5315 break;
5316 }
5317
5318 /* We're going to need to keep the ARGVEC, so we copy it
5319 here. */
5320 argvec = copy_to_permanent (argvec);
5321 pop_momentary ();
5322
5323 /* Here, we deal with the peculiar case:
5324
5325 template <class T> struct S {
5326 template <class U> friend void f();
5327 };
5328 template <class U> friend void f() {}
5329 template S<int>;
5330 template void f<double>();
5331
5332 Here, the ARGS for the instantiation of will be {int,
5333 double}. But, we only need as many ARGS as there are
5334 levels of template parameters in CODE_PATTERN. We are
5335 careful not to get fooled into reducing the ARGS in
5336 situations like:
5337
5338 template <class T> struct S { template <class U> void f(U); }
5339 template <class T> template <> void S<T>::f(int) {}
5340
5341 which we can spot because the pattern will be a
5342 specialization in this case. */
5343 args_depth = TMPL_ARGS_DEPTH (args);
5344 parms_depth =
5345 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5346 if (args_depth > parms_depth
5347 && !DECL_TEMPLATE_SPECIALIZATION (t))
5348 {
5349 my_friendly_assert (DECL_FRIEND_P (t), 0);
5350
5351 if (parms_depth > 1)
5352 {
5353 int i;
5354
5355 args = make_temp_vec (parms_depth);
5356 for (i = 0; i < parms_depth; ++i)
5357 TREE_VEC_ELT (args, i) =
5358 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
5359 }
5360 else
5361 args = TREE_VEC_ELT (args, args_depth - parms_depth);
5362 }
5363 }
5364 else
5365 {
5366 /* This special case arises when we have something like this:
5367
5368 template <class T> struct S {
5369 friend void f<int>(int, double);
5370 };
5371
5372 Here, the DECL_TI_TEMPLATE for the friend declaration
5373 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5374 being called from tsubst_friend_function, and we want
5375 only to create a new decl (R) with appropriate types so
5376 that we can call determine_specialization. */
5377 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5378 == LOOKUP_EXPR)
5379 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5380 == IDENTIFIER_NODE), 0);
5381 gen_tmpl = NULL_TREE;
5382 }
5383
5384 if (DECL_CLASS_SCOPE_P (t))
5385 {
5386 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5387 member = 2;
5388 else
5389 member = 1;
5390 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, t,
5391 /*entering_scope=*/1);
5392 }
5393 else
5394 {
5395 member = 0;
5396 ctx = NULL_TREE;
5397 }
5398 type = tsubst (type, args, in_decl);
5399
5400 /* We do NOT check for matching decls pushed separately at this
5401 point, as they may not represent instantiations of this
5402 template, and in any case are considered separate under the
5403 discrete model. Instead, see add_maybe_template. */
5404
5405 r = copy_node (t);
5406 copy_lang_decl (r);
5407 DECL_USE_TEMPLATE (r) = 0;
5408 TREE_TYPE (r) = type;
5409
5410 DECL_CONTEXT (r)
5411 = tsubst_aggr_type (DECL_CONTEXT (t), args, t, /*entering_scope=*/1);
5412 DECL_CLASS_CONTEXT (r) = ctx;
5413
5414 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
5415 IDENTIFIER_POINTER (DECL_NAME (r)),
5416 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
5417 /* Type-conversion operator. Reconstruct the name, in
5418 case it's the name of one of the template's parameters. */
5419 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
5420
5421 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
5422 DECL_MAIN_VARIANT (r) = r;
5423 DECL_RESULT (r) = NULL_TREE;
5424
5425 TREE_STATIC (r) = 0;
5426 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5427 DECL_EXTERNAL (r) = 1;
5428 DECL_INTERFACE_KNOWN (r) = 0;
5429 DECL_DEFER_OUTPUT (r) = 0;
5430 TREE_CHAIN (r) = NULL_TREE;
5431 DECL_PENDING_INLINE_INFO (r) = 0;
5432 TREE_USED (r) = 0;
5433
5434 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5435 name. There's no need to do this in the special friend
5436 case mentioned above where GEN_TMPL is NULL. */
5437 if (gen_tmpl)
5438 {
5439 /* The ARGVEC was built on the momentary obstack. Make it
5440 permanent now. */
5441 argvec = copy_to_permanent (argvec);
5442 DECL_TEMPLATE_INFO (r)
5443 = perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
5444 SET_DECL_IMPLICIT_INSTANTIATION (r);
5445 register_specialization (r, gen_tmpl, argvec);
5446
5447 /* Set the mangled name for R. */
5448 if (DECL_DESTRUCTOR_P (t))
5449 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
5450 else
5451 {
5452 /* Instantiations of template functions must be mangled
5453 specially, in order to conform to 14.5.5.1
5454 [temp.over.link]. */
5455 tree tmpl = DECL_TI_TEMPLATE (t);
5456
5457 /* TMPL will be NULL if this is a specialization of a
5458 member function of a template class. */
5459 if (name_mangling_version < 1
5460 || tmpl == NULL_TREE
5461 || (member && !is_member_template (tmpl)
5462 && !DECL_TEMPLATE_INFO (tmpl)))
5463 set_mangled_name_for_decl (r);
5464 else
5465 set_mangled_name_for_template_decl (r);
5466 }
5467
5468 DECL_RTL (r) = 0;
5469 make_decl_rtl (r, NULL_PTR, 1);
5470
5471 /* Like grokfndecl. If we don't do this, pushdecl will
5472 mess up our TREE_CHAIN because it doesn't find a
5473 previous decl. Sigh. */
5474 if (member
5475 && ! uses_template_parms (r)
5476 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
5477 == NULL_TREE))
5478 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
5479 }
5480
5481 if (DECL_CONSTRUCTOR_P (r))
5482 {
5483 maybe_retrofit_in_chrg (r);
5484 grok_ctor_properties (ctx, r);
5485 }
5486 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5487 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
5488 }
5489 break;
5490
5491 case PARM_DECL:
5492 {
5493 r = copy_node (t);
5494 TREE_TYPE (r) = type;
5495 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5496 DECL_INITIAL (r) = TREE_TYPE (r);
5497 else
5498 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);
5499
5500 DECL_CONTEXT (r) = NULL_TREE;
5501 #ifdef PROMOTE_PROTOTYPES
5502 if ((TREE_CODE (type) == INTEGER_TYPE
5503 || TREE_CODE (type) == ENUMERAL_TYPE)
5504 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5505 DECL_ARG_TYPE (r) = integer_type_node;
5506 #endif
5507 if (TREE_CHAIN (t))
5508 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
5509 }
5510 break;
5511
5512 case FIELD_DECL:
5513 {
5514 r = copy_node (t);
5515 TREE_TYPE (r) = type;
5516 copy_lang_decl (r);
5517 #if 0
5518 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
5519 #endif
5520 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
5521 TREE_CHAIN (r) = NULL_TREE;
5522 if (TREE_CODE (type) == VOID_TYPE)
5523 cp_error_at ("instantiation of `%D' as type void", r);
5524 }
5525 break;
5526
5527 case USING_DECL:
5528 {
5529 r = copy_node (t);
5530 DECL_INITIAL (r)
5531 = tsubst_copy (DECL_INITIAL (t), args, in_decl);
5532 TREE_CHAIN (r) = NULL_TREE;
5533 }
5534 break;
5535
5536 case VAR_DECL:
5537 {
5538 tree argvec;
5539 tree gen_tmpl;
5540 tree spec;
5541 tree tmpl;
5542 tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5543 /*entering_scope=*/1);
5544
5545 /* Nobody should be tsubst'ing into non-template variables. */
5546 my_friendly_assert (DECL_LANG_SPECIFIC (t)
5547 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5548
5549 /* Check to see if we already have this specialization. */
5550 tmpl = DECL_TI_TEMPLATE (t);
5551 gen_tmpl = most_general_template (tmpl);
5552 argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
5553 spec = retrieve_specialization (gen_tmpl, argvec);
5554
5555 if (spec)
5556 {
5557 r = spec;
5558 break;
5559 }
5560
5561 r = copy_node (t);
5562 TREE_TYPE (r) = type;
5563 DECL_CONTEXT (r) = ctx;
5564 if (TREE_STATIC (r))
5565 DECL_ASSEMBLER_NAME (r)
5566 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
5567
5568 /* Don't try to expand the initializer until someone tries to use
5569 this variable; otherwise we run into circular dependencies. */
5570 DECL_INITIAL (r) = NULL_TREE;
5571 DECL_RTL (r) = 0;
5572 DECL_SIZE (r) = 0;
5573 copy_lang_decl (r);
5574 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
5575
5576 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
5577 SET_DECL_IMPLICIT_INSTANTIATION (r);
5578 register_specialization (r, gen_tmpl, argvec);
5579
5580 TREE_CHAIN (r) = NULL_TREE;
5581 if (TREE_CODE (type) == VOID_TYPE)
5582 cp_error_at ("instantiation of `%D' as type void", r);
5583 }
5584 break;
5585
5586 case TYPE_DECL:
5587 if (t == TYPE_NAME (TREE_TYPE (t)))
5588 r = TYPE_NAME (type);
5589 else
5590 {
5591 r = copy_node (t);
5592 TREE_TYPE (r) = type;
5593 DECL_CONTEXT (r) = current_class_type;
5594 TREE_CHAIN (r) = NULL_TREE;
5595 }
5596 break;
5597
5598 default:
5599 my_friendly_abort (0);
5600 }
5601
5602 /* Restore the file and line information. */
5603 lineno = saved_lineno;
5604 input_filename = saved_filename;
5605
5606 return r;
5607 }
5608
5609 /* Substitue into the ARG_TYPES of a function type. */
5610
5611 tree
5612 tsubst_arg_types (arg_types, args, in_decl)
5613 tree arg_types;
5614 tree args;
5615 tree in_decl;
5616 {
5617 tree remaining_arg_types;
5618 tree type;
5619
5620 if (!arg_types || arg_types == void_list_node)
5621 return arg_types;
5622
5623 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
5624 args, in_decl);
5625
5626 /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't
5627 matter on function types. */
5628 type = TYPE_MAIN_VARIANT (type_decays_to
5629 (tsubst (TREE_VALUE (arg_types),
5630 args, in_decl)));
5631
5632 /* Note that we do not substitute into default arguments here. The
5633 standard mandates that they be instantiated only when needed,
5634 which is done in build_over_call. */
5635 return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type,
5636 remaining_arg_types);
5637
5638 }
5639
5640 /* Take the tree structure T and replace template parameters used therein
5641 with the argument vector ARGS. IN_DECL is an associated decl for
5642 diagnostics.
5643
5644 tsubst is used for dealing with types, decls and the like; for
5645 expressions, use tsubst_expr or tsubst_copy. */
5646
5647 tree
5648 tsubst (t, args, in_decl)
5649 tree t, args;
5650 tree in_decl;
5651 {
5652 tree type, r;
5653
5654 if (t == NULL_TREE || t == error_mark_node
5655 || t == integer_type_node
5656 || t == void_type_node
5657 || t == char_type_node
5658 || TREE_CODE (t) == NAMESPACE_DECL)
5659 return t;
5660
5661 if (TREE_CODE (t) == IDENTIFIER_NODE)
5662 type = IDENTIFIER_TYPE_VALUE (t);
5663 else
5664 type = TREE_TYPE (t);
5665 if (type == unknown_type_node)
5666 my_friendly_abort (42);
5667
5668 if (type && TREE_CODE (t) != FUNCTION_DECL
5669 && TREE_CODE (t) != TYPENAME_TYPE
5670 && TREE_CODE (t) != TEMPLATE_DECL
5671 && TREE_CODE (t) != IDENTIFIER_NODE)
5672 type = tsubst (type, args, in_decl);
5673
5674 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
5675 return tsubst_decl (t, args, type, in_decl);
5676
5677 switch (TREE_CODE (t))
5678 {
5679 case RECORD_TYPE:
5680 case UNION_TYPE:
5681 case ENUMERAL_TYPE:
5682 return tsubst_aggr_type (t, args, in_decl, /*entering_scope=*/0);
5683
5684 case ERROR_MARK:
5685 case IDENTIFIER_NODE:
5686 case OP_IDENTIFIER:
5687 case VOID_TYPE:
5688 case REAL_TYPE:
5689 case COMPLEX_TYPE:
5690 case BOOLEAN_TYPE:
5691 case INTEGER_CST:
5692 case REAL_CST:
5693 case STRING_CST:
5694 return t;
5695
5696 case INTEGER_TYPE:
5697 if (t == integer_type_node)
5698 return t;
5699
5700 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
5701 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
5702 return t;
5703
5704 {
5705 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
5706
5707 max = tsubst_expr (max, args, in_decl);
5708 if (processing_template_decl)
5709 {
5710 tree itype = make_node (INTEGER_TYPE);
5711 TYPE_MIN_VALUE (itype) = size_zero_node;
5712 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
5713 integer_one_node);
5714 return itype;
5715 }
5716
5717 if (pedantic && integer_zerop (max))
5718 pedwarn ("creating array with size zero");
5719 else if (INT_CST_LT (max, integer_zero_node))
5720 {
5721 cp_error ("creating array with size `%E'", max);
5722 max = integer_one_node;
5723 }
5724
5725 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
5726 if (!TREE_PERMANENT (max) && !allocation_temporary_p ())
5727 max = copy_to_permanent (max);
5728 return build_index_type (max);
5729 }
5730
5731 case TEMPLATE_TYPE_PARM:
5732 case TEMPLATE_TEMPLATE_PARM:
5733 case TEMPLATE_PARM_INDEX:
5734 {
5735 int idx;
5736 int level;
5737 int levels;
5738
5739 r = NULL_TREE;
5740
5741 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
5742 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5743 {
5744 idx = TEMPLATE_TYPE_IDX (t);
5745 level = TEMPLATE_TYPE_LEVEL (t);
5746 }
5747 else
5748 {
5749 idx = TEMPLATE_PARM_IDX (t);
5750 level = TEMPLATE_PARM_LEVEL (t);
5751 }
5752
5753 if (TREE_VEC_LENGTH (args) > 0)
5754 {
5755 tree arg = NULL_TREE;
5756
5757 levels = TMPL_ARGS_DEPTH (args);
5758 if (level <= levels)
5759 arg = TMPL_ARG (args, level, idx);
5760
5761 if (arg == error_mark_node)
5762 return error_mark_node;
5763 else if (arg != NULL_TREE)
5764 {
5765 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5766 {
5767 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
5768 == 't', 0);
5769 return cp_build_qualified_type
5770 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t));
5771 }
5772 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5773 {
5774 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
5775 {
5776 /* We are processing a type constructed from
5777 a template template parameter */
5778 tree argvec = tsubst (TYPE_TI_ARGS (t),
5779 args, in_decl);
5780
5781 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5782 we are resolving nested-types in the signature of
5783 a member function templates.
5784 Otherwise ARG is a TEMPLATE_DECL and is the real
5785 template to be instantiated. */
5786 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
5787 arg = TYPE_NAME (arg);
5788
5789 r = lookup_template_class (DECL_NAME (arg),
5790 argvec, in_decl,
5791 DECL_CONTEXT (arg),
5792 /*entering_scope=*/0);
5793 return cp_build_qualified_type (r, TYPE_QUALS (t));
5794 }
5795 else
5796 /* We are processing a template argument list. */
5797 return arg;
5798 }
5799 else
5800 return arg;
5801 }
5802 }
5803 else
5804 my_friendly_abort (981018);
5805
5806 if (level == 1)
5807 /* This can happen during the attempted tsubst'ing in
5808 unify. This means that we don't yet have any information
5809 about the template parameter in question. */
5810 return t;
5811
5812 /* If we get here, we must have been looking at a parm for a
5813 more deeply nested template. Make a new version of this
5814 template parameter, but with a lower level. */
5815 switch (TREE_CODE (t))
5816 {
5817 case TEMPLATE_TYPE_PARM:
5818 case TEMPLATE_TEMPLATE_PARM:
5819 r = copy_node (t);
5820 TEMPLATE_TYPE_PARM_INDEX (r)
5821 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
5822 r, levels);
5823 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
5824 TYPE_MAIN_VARIANT (r) = r;
5825 TYPE_POINTER_TO (r) = NULL_TREE;
5826 TYPE_REFERENCE_TO (r) = NULL_TREE;
5827
5828 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
5829 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
5830 {
5831 tree argvec = tsubst (TYPE_TI_ARGS (t), args, in_decl);
5832 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
5833 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
5834 }
5835 break;
5836
5837 case TEMPLATE_PARM_INDEX:
5838 r = reduce_template_parm_level (t, type, levels);
5839 break;
5840
5841 default:
5842 my_friendly_abort (0);
5843 }
5844
5845 return r;
5846 }
5847
5848 case TREE_LIST:
5849 {
5850 tree purpose, value, chain, result;
5851 int via_public, via_virtual, via_protected;
5852
5853 if (t == void_list_node)
5854 return t;
5855
5856 via_public = TREE_VIA_PUBLIC (t);
5857 via_protected = TREE_VIA_PROTECTED (t);
5858 via_virtual = TREE_VIA_VIRTUAL (t);
5859
5860 purpose = TREE_PURPOSE (t);
5861 if (purpose)
5862 purpose = tsubst (purpose, args, in_decl);
5863 value = TREE_VALUE (t);
5864 if (value)
5865 value = tsubst (value, args, in_decl);
5866 chain = TREE_CHAIN (t);
5867 if (chain && chain != void_type_node)
5868 chain = tsubst (chain, args, in_decl);
5869 if (purpose == TREE_PURPOSE (t)
5870 && value == TREE_VALUE (t)
5871 && chain == TREE_CHAIN (t))
5872 return t;
5873 result = hash_tree_cons (via_public, via_virtual, via_protected,
5874 purpose, value, chain);
5875 TREE_PARMLIST (result) = TREE_PARMLIST (t);
5876 return result;
5877 }
5878 case TREE_VEC:
5879 if (type != NULL_TREE)
5880 {
5881 /* A binfo node. We always need to make a copy, of the node
5882 itself and of its BINFO_BASETYPES. */
5883
5884 t = copy_node (t);
5885
5886 /* Make sure type isn't a typedef copy. */
5887 type = BINFO_TYPE (TYPE_BINFO (type));
5888
5889 TREE_TYPE (t) = complete_type (type);
5890 if (IS_AGGR_TYPE (type))
5891 {
5892 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
5893 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
5894 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
5895 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
5896 }
5897 return t;
5898 }
5899
5900 /* Otherwise, a vector of template arguments. */
5901 return tsubst_template_arg_vector (t, args);
5902
5903 case POINTER_TYPE:
5904 case REFERENCE_TYPE:
5905 {
5906 enum tree_code code;
5907
5908 if (type == TREE_TYPE (t))
5909 return t;
5910
5911 code = TREE_CODE (t);
5912 if (TREE_CODE (type) == REFERENCE_TYPE
5913 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
5914 {
5915 static int last_line = 0;
5916 static char* last_file = 0;
5917
5918 /* We keep track of the last time we issued this error
5919 message to avoid spewing a ton of messages during a
5920 single bad template instantiation. */
5921 if (last_line != lineno ||
5922 last_file != input_filename)
5923 {
5924 if (TREE_CODE (type) == VOID_TYPE)
5925 cp_error ("forming reference to void");
5926 else
5927 cp_error ("forming %s to reference type `%T'",
5928 (code == POINTER_TYPE) ? "pointer" : "reference",
5929 type);
5930 last_line = lineno;
5931 last_file = input_filename;
5932 }
5933
5934 /* Use the underlying type in an attempt at error
5935 recovery; maybe the user meant vector<int> and wrote
5936 vector<int&>, or some such. */
5937 if (code == REFERENCE_TYPE)
5938 r = type;
5939 else
5940 r = build_pointer_type (TREE_TYPE (type));
5941 }
5942 else if (code == POINTER_TYPE)
5943 r = build_pointer_type (type);
5944 else
5945 r = build_reference_type (type);
5946 r = cp_build_qualified_type (r, TYPE_QUALS (t));
5947
5948 /* Will this ever be needed for TYPE_..._TO values? */
5949 layout_type (r);
5950 return r;
5951 }
5952 case OFFSET_TYPE:
5953 {
5954 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl);
5955 if (! IS_AGGR_TYPE (r))
5956 cp_error ("creating pointer to member of non-class type `%T'", r);
5957 return build_offset_type (r, type);
5958 }
5959 case FUNCTION_TYPE:
5960 case METHOD_TYPE:
5961 {
5962 tree arg_types;
5963 tree raises;
5964 tree fntype;
5965
5966 /* The TYPE_CONTEXT is not used for function/method types. */
5967 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
5968
5969 /* Substitue the argument types. */
5970 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, in_decl);
5971
5972 /* Construct a new type node and return it. */
5973 if (TREE_CODE (t) == FUNCTION_TYPE)
5974 fntype = build_function_type (type, arg_types);
5975 else
5976 {
5977 r = TREE_TYPE (TREE_VALUE (arg_types));
5978 if (! IS_AGGR_TYPE (r))
5979 cp_error ("creating pointer to member function of non-class type `%T'",
5980 r);
5981
5982 fntype = build_cplus_method_type (r, type, TREE_CHAIN (arg_types));
5983 }
5984 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
5985
5986 /* Substitue the exception specification. */
5987 raises = TYPE_RAISES_EXCEPTIONS (t);
5988 if (raises)
5989 {
5990 raises = tsubst (raises, args, in_decl);
5991 fntype = build_exception_variant (fntype, raises);
5992 }
5993 return fntype;
5994 }
5995 case ARRAY_TYPE:
5996 {
5997 tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
5998 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5999 return t;
6000
6001 /* These checks should match the ones in grokdeclarator. */
6002 if (TREE_CODE (type) == VOID_TYPE)
6003 {
6004 cp_error ("creating array of void");
6005 type = build_pointer_type (type);
6006 }
6007 else if (TREE_CODE (type) == FUNCTION_TYPE)
6008 {
6009 cp_error ("creating array of functions `%T'", type);
6010 type = build_pointer_type (type);
6011 }
6012 else if (TREE_CODE (type) == REFERENCE_TYPE)
6013 {
6014 cp_error ("creating array of references `%T'", type);
6015 type = TREE_TYPE (type);
6016 }
6017
6018 r = build_cplus_array_type (type, domain);
6019 return r;
6020 }
6021
6022 case PLUS_EXPR:
6023 case MINUS_EXPR:
6024 return fold (build (TREE_CODE (t), TREE_TYPE (t),
6025 tsubst (TREE_OPERAND (t, 0), args, in_decl),
6026 tsubst (TREE_OPERAND (t, 1), args, in_decl)));
6027
6028 case NEGATE_EXPR:
6029 case NOP_EXPR:
6030 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
6031 tsubst (TREE_OPERAND (t, 0), args, in_decl)));
6032
6033 case TYPENAME_TYPE:
6034 {
6035 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, in_decl,
6036 /*entering_scope=*/1);
6037 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
6038
6039 /* Normally, make_typename_type does not require that the CTX
6040 have complete type in order to allow things like:
6041
6042 template <class T> struct S { typename S<T>::X Y; };
6043
6044 But, such constructs have already been resolved by this
6045 point, so here CTX really should have complete type, unless
6046 it's a partial instantiation. */
6047 if (!uses_template_parms (ctx)
6048 && !complete_type_or_else (ctx))
6049 return error_mark_node;
6050
6051 f = make_typename_type (ctx, f);
6052 return cp_build_qualified_type (f,
6053 CP_TYPE_QUALS (f)
6054 | CP_TYPE_QUALS (t));
6055 }
6056
6057 case INDIRECT_REF:
6058 return make_pointer_declarator
6059 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
6060
6061 case ADDR_EXPR:
6062 return make_reference_declarator
6063 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
6064
6065 case ARRAY_REF:
6066 return build_parse_node
6067 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
6068 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
6069
6070 case CALL_EXPR:
6071 return make_call_declarator
6072 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
6073 tsubst (TREE_OPERAND (t, 1), args, in_decl),
6074 TREE_OPERAND (t, 2),
6075 tsubst (TREE_TYPE (t), args, in_decl));
6076
6077 case SCOPE_REF:
6078 return build_parse_node
6079 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
6080 tsubst (TREE_OPERAND (t, 1), args, in_decl));
6081
6082 case TYPEOF_TYPE:
6083 return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t), args, in_decl));
6084
6085 default:
6086 sorry ("use of `%s' in template",
6087 tree_code_name [(int) TREE_CODE (t)]);
6088 return error_mark_node;
6089 }
6090 }
6091
6092 void
6093 do_pushlevel ()
6094 {
6095 emit_line_note (input_filename, lineno);
6096 pushlevel (0);
6097 clear_last_expr ();
6098 push_momentary ();
6099 expand_start_bindings (0);
6100 }
6101
6102 tree
6103 do_poplevel ()
6104 {
6105 tree t;
6106 int saved_warn_unused = 0;
6107
6108 if (processing_template_decl)
6109 {
6110 saved_warn_unused = warn_unused;
6111 warn_unused = 0;
6112 }
6113 expand_end_bindings (getdecls (), kept_level_p (), 0);
6114 if (processing_template_decl)
6115 warn_unused = saved_warn_unused;
6116 t = poplevel (kept_level_p (), 1, 0);
6117 pop_momentary ();
6118 return t;
6119 }
6120
6121 /* Like tsubst, but deals with expressions. This function just replaces
6122 template parms; to finish processing the resultant expression, use
6123 tsubst_expr. */
6124
6125 tree
6126 tsubst_copy (t, args, in_decl)
6127 tree t, args;
6128 tree in_decl;
6129 {
6130 enum tree_code code;
6131 tree r;
6132
6133 if (t == NULL_TREE || t == error_mark_node)
6134 return t;
6135
6136 code = TREE_CODE (t);
6137
6138 switch (code)
6139 {
6140 case PARM_DECL:
6141 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6142
6143 case CONST_DECL:
6144 {
6145 tree enum_type;
6146 tree v;
6147
6148 if (!DECL_CONTEXT (t))
6149 /* This is a global enumeration constant. */
6150 return t;
6151
6152 /* Unfortunately, we cannot just call lookup_name here.
6153 Consider:
6154
6155 template <int I> int f() {
6156 enum E { a = I };
6157 struct S { void g() { E e = a; } };
6158 };
6159
6160 When we instantiate f<7>::S::g(), say, lookup_name is not
6161 clever enough to find f<7>::a. */
6162 enum_type
6163 = tsubst_aggr_type (TREE_TYPE (t), args, in_decl,
6164 /*entering_scope=*/0);
6165
6166 for (v = TYPE_VALUES (enum_type);
6167 v != NULL_TREE;
6168 v = TREE_CHAIN (v))
6169 if (TREE_PURPOSE (v) == DECL_NAME (t))
6170 return TREE_VALUE (v);
6171
6172 /* We didn't find the name. That should never happen; if
6173 name-lookup found it during preliminary parsing, we
6174 should find it again here during instantiation. */
6175 my_friendly_abort (0);
6176 }
6177 return t;
6178
6179 case FIELD_DECL:
6180 if (DECL_CONTEXT (t))
6181 {
6182 tree ctx;
6183
6184 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
6185 /*entering_scope=*/1);
6186 if (ctx != DECL_CONTEXT (t))
6187 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6188 }
6189 return t;
6190
6191 case VAR_DECL:
6192 case FUNCTION_DECL:
6193 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
6194 t = tsubst (t, args, in_decl);
6195 mark_used (t);
6196 return t;
6197
6198 case TEMPLATE_DECL:
6199 if (is_member_template (t))
6200 return tsubst (t, args, in_decl);
6201 else
6202 return t;
6203
6204 case LOOKUP_EXPR:
6205 {
6206 /* We must tsbust into a LOOKUP_EXPR in case the names to
6207 which it refers is a conversion operator; in that case the
6208 name will change. We avoid making unnecessary copies,
6209 however. */
6210
6211 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
6212
6213 if (id != TREE_OPERAND (t, 0))
6214 {
6215 r = build_nt (LOOKUP_EXPR, id);
6216 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6217 t = r;
6218 }
6219
6220 return t;
6221 }
6222
6223 case CAST_EXPR:
6224 case REINTERPRET_CAST_EXPR:
6225 case CONST_CAST_EXPR:
6226 case STATIC_CAST_EXPR:
6227 case DYNAMIC_CAST_EXPR:
6228 case NOP_EXPR:
6229 return build1
6230 (code, tsubst (TREE_TYPE (t), args, in_decl),
6231 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
6232
6233 case INDIRECT_REF:
6234 case PREDECREMENT_EXPR:
6235 case PREINCREMENT_EXPR:
6236 case POSTDECREMENT_EXPR:
6237 case POSTINCREMENT_EXPR:
6238 case NEGATE_EXPR:
6239 case TRUTH_NOT_EXPR:
6240 case BIT_NOT_EXPR:
6241 case ADDR_EXPR:
6242 case CONVERT_EXPR: /* Unary + */
6243 case SIZEOF_EXPR:
6244 case ALIGNOF_EXPR:
6245 case ARROW_EXPR:
6246 case THROW_EXPR:
6247 case TYPEID_EXPR:
6248 return build1
6249 (code, NULL_TREE,
6250 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
6251
6252 case PLUS_EXPR:
6253 case MINUS_EXPR:
6254 case MULT_EXPR:
6255 case TRUNC_DIV_EXPR:
6256 case CEIL_DIV_EXPR:
6257 case FLOOR_DIV_EXPR:
6258 case ROUND_DIV_EXPR:
6259 case EXACT_DIV_EXPR:
6260 case BIT_AND_EXPR:
6261 case BIT_ANDTC_EXPR:
6262 case BIT_IOR_EXPR:
6263 case BIT_XOR_EXPR:
6264 case TRUNC_MOD_EXPR:
6265 case FLOOR_MOD_EXPR:
6266 case TRUTH_ANDIF_EXPR:
6267 case TRUTH_ORIF_EXPR:
6268 case TRUTH_AND_EXPR:
6269 case TRUTH_OR_EXPR:
6270 case RSHIFT_EXPR:
6271 case LSHIFT_EXPR:
6272 case RROTATE_EXPR:
6273 case LROTATE_EXPR:
6274 case EQ_EXPR:
6275 case NE_EXPR:
6276 case MAX_EXPR:
6277 case MIN_EXPR:
6278 case LE_EXPR:
6279 case GE_EXPR:
6280 case LT_EXPR:
6281 case GT_EXPR:
6282 case COMPONENT_REF:
6283 case ARRAY_REF:
6284 case COMPOUND_EXPR:
6285 case SCOPE_REF:
6286 case DOTSTAR_EXPR:
6287 case MEMBER_REF:
6288 return build_nt
6289 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6290 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
6291
6292 case CALL_EXPR:
6293 {
6294 tree fn = TREE_OPERAND (t, 0);
6295 if (is_overloaded_fn (fn))
6296 fn = tsubst_copy (get_first_fn (fn), args, in_decl);
6297 else
6298 /* Sometimes FN is a LOOKUP_EXPR. */
6299 fn = tsubst_copy (fn, args, in_decl);
6300 return build_nt
6301 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6302 NULL_TREE);
6303 }
6304
6305 case METHOD_CALL_EXPR:
6306 {
6307 tree name = TREE_OPERAND (t, 0);
6308 if (TREE_CODE (name) == BIT_NOT_EXPR)
6309 {
6310 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
6311 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
6312 }
6313 else if (TREE_CODE (name) == SCOPE_REF
6314 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
6315 {
6316 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
6317 name = TREE_OPERAND (name, 1);
6318 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
6319 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
6320 name = build_nt (SCOPE_REF, base, name);
6321 }
6322 else
6323 name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
6324 return build_nt
6325 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6326 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
6327 NULL_TREE);
6328 }
6329
6330 case BIND_EXPR:
6331 case COND_EXPR:
6332 case MODOP_EXPR:
6333 {
6334 r = build_nt
6335 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6336 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6337 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
6338
6339 if (code == BIND_EXPR && !processing_template_decl)
6340 {
6341 /* This processing should really occur in tsubst_expr,
6342 However, tsubst_expr does not recurse into expressions,
6343 since it assumes that there aren't any statements
6344 inside them. Instead, it simply calls
6345 build_expr_from_tree. So, we need to expand the
6346 BIND_EXPR here. */
6347 tree rtl_expr = begin_stmt_expr ();
6348 tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
6349 r = finish_stmt_expr (rtl_expr, block);
6350 }
6351
6352 return r;
6353 }
6354
6355 case NEW_EXPR:
6356 {
6357 r = build_nt
6358 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6359 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6360 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
6361 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
6362 return r;
6363 }
6364
6365 case DELETE_EXPR:
6366 {
6367 r = build_nt
6368 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6369 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
6370 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
6371 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
6372 return r;
6373 }
6374
6375 case TEMPLATE_ID_EXPR:
6376 {
6377 /* Substituted template arguments */
6378 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
6379 tree chain;
6380 for (chain = targs; chain; chain = TREE_CHAIN (chain))
6381 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
6382
6383 return lookup_template_function
6384 (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
6385 }
6386
6387 case TREE_LIST:
6388 {
6389 tree purpose, value, chain;
6390
6391 if (t == void_list_node)
6392 return t;
6393
6394 purpose = TREE_PURPOSE (t);
6395 if (purpose)
6396 purpose = tsubst_copy (purpose, args, in_decl);
6397 value = TREE_VALUE (t);
6398 if (value)
6399 value = tsubst_copy (value, args, in_decl);
6400 chain = TREE_CHAIN (t);
6401 if (chain && chain != void_type_node)
6402 chain = tsubst_copy (chain, args, in_decl);
6403 if (purpose == TREE_PURPOSE (t)
6404 && value == TREE_VALUE (t)
6405 && chain == TREE_CHAIN (t))
6406 return t;
6407 return tree_cons (purpose, value, chain);
6408 }
6409
6410 case RECORD_TYPE:
6411 case UNION_TYPE:
6412 case ENUMERAL_TYPE:
6413 case INTEGER_TYPE:
6414 case TEMPLATE_TYPE_PARM:
6415 case TEMPLATE_TEMPLATE_PARM:
6416 case TEMPLATE_PARM_INDEX:
6417 case POINTER_TYPE:
6418 case REFERENCE_TYPE:
6419 case OFFSET_TYPE:
6420 case FUNCTION_TYPE:
6421 case METHOD_TYPE:
6422 case ARRAY_TYPE:
6423 case TYPENAME_TYPE:
6424 case TYPE_DECL:
6425 return tsubst (t, args, in_decl);
6426
6427 case IDENTIFIER_NODE:
6428 if (IDENTIFIER_TYPENAME_P (t)
6429 /* Make sure it's not just a variable named `__opr', for instance,
6430 which can occur in some existing code. */
6431 && TREE_TYPE (t))
6432 return build_typename_overload
6433 (tsubst (TREE_TYPE (t), args, in_decl));
6434 else
6435 return t;
6436
6437 case CONSTRUCTOR:
6438 {
6439 r = build
6440 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
6441 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
6442 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
6443 return r;
6444 }
6445
6446 default:
6447 return t;
6448 }
6449 }
6450
6451 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6452
6453 tree
6454 tsubst_expr (t, args, in_decl)
6455 tree t, args;
6456 tree in_decl;
6457 {
6458 if (t == NULL_TREE || t == error_mark_node)
6459 return t;
6460
6461 if (processing_template_decl)
6462 return tsubst_copy (t, args, in_decl);
6463
6464 switch (TREE_CODE (t))
6465 {
6466 case RETURN_STMT:
6467 lineno = TREE_COMPLEXITY (t);
6468 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
6469 args, in_decl));
6470 break;
6471
6472 case EXPR_STMT:
6473 lineno = TREE_COMPLEXITY (t);
6474 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
6475 args, in_decl));
6476 break;
6477
6478 case DECL_STMT:
6479 {
6480 int i = suspend_momentary ();
6481 tree dcl, init;
6482
6483 lineno = TREE_COMPLEXITY (t);
6484 emit_line_note (input_filename, lineno);
6485 dcl = start_decl
6486 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
6487 tsubst (TREE_OPERAND (t, 1), args, in_decl),
6488 TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
6489 init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
6490 cp_finish_decl
6491 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
6492 resume_momentary (i);
6493 return dcl;
6494 }
6495
6496 case FOR_STMT:
6497 {
6498 tree tmp;
6499 lineno = TREE_COMPLEXITY (t);
6500
6501 begin_for_stmt ();
6502 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
6503 tsubst_expr (tmp, args, in_decl);
6504 finish_for_init_stmt (NULL_TREE);
6505 finish_for_cond (tsubst_expr (FOR_COND (t), args,
6506 in_decl),
6507 NULL_TREE);
6508 tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
6509 finish_for_expr (tmp, NULL_TREE);
6510 tsubst_expr (FOR_BODY (t), args, in_decl);
6511 finish_for_stmt (tmp, NULL_TREE);
6512 }
6513 break;
6514
6515 case WHILE_STMT:
6516 {
6517 lineno = TREE_COMPLEXITY (t);
6518 begin_while_stmt ();
6519 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
6520 args, in_decl),
6521 NULL_TREE);
6522 tsubst_expr (WHILE_BODY (t), args, in_decl);
6523 finish_while_stmt (NULL_TREE);
6524 }
6525 break;
6526
6527 case DO_STMT:
6528 {
6529 lineno = TREE_COMPLEXITY (t);
6530 begin_do_stmt ();
6531 tsubst_expr (DO_BODY (t), args, in_decl);
6532 finish_do_body (NULL_TREE);
6533 finish_do_stmt (tsubst_expr (DO_COND (t), args,
6534 in_decl),
6535 NULL_TREE);
6536 }
6537 break;
6538
6539 case IF_STMT:
6540 {
6541 tree tmp;
6542
6543 lineno = TREE_COMPLEXITY (t);
6544 begin_if_stmt ();
6545 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
6546 args, in_decl),
6547 NULL_TREE);
6548
6549 if (tmp = THEN_CLAUSE (t), tmp)
6550 {
6551 tsubst_expr (tmp, args, in_decl);
6552 finish_then_clause (NULL_TREE);
6553 }
6554
6555 if (tmp = ELSE_CLAUSE (t), tmp)
6556 {
6557 begin_else_clause ();
6558 tsubst_expr (tmp, args, in_decl);
6559 finish_else_clause (NULL_TREE);
6560 }
6561
6562 finish_if_stmt ();
6563 }
6564 break;
6565
6566 case COMPOUND_STMT:
6567 {
6568 tree substmt;
6569
6570 lineno = TREE_COMPLEXITY (t);
6571 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
6572 for (substmt = COMPOUND_BODY (t);
6573 substmt != NULL_TREE;
6574 substmt = TREE_CHAIN (substmt))
6575 tsubst_expr (substmt, args, in_decl);
6576 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
6577 NULL_TREE);
6578 }
6579 break;
6580
6581 case BREAK_STMT:
6582 lineno = TREE_COMPLEXITY (t);
6583 finish_break_stmt ();
6584 break;
6585
6586 case CONTINUE_STMT:
6587 lineno = TREE_COMPLEXITY (t);
6588 finish_continue_stmt ();
6589 break;
6590
6591 case SWITCH_STMT:
6592 {
6593 tree val, tmp;
6594
6595 lineno = TREE_COMPLEXITY (t);
6596 begin_switch_stmt ();
6597 val = tsubst_expr (SWITCH_COND (t), args, in_decl);
6598 finish_switch_cond (val);
6599
6600 if (tmp = TREE_OPERAND (t, 1), tmp)
6601 tsubst_expr (tmp, args, in_decl);
6602
6603 finish_switch_stmt (val, NULL_TREE);
6604 }
6605 break;
6606
6607 case CASE_LABEL:
6608 finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
6609 tsubst_expr (CASE_HIGH (t), args, in_decl));
6610 break;
6611
6612 case LABEL_DECL:
6613 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
6614 DECL_NAME (t));
6615 if (t)
6616 expand_label (t);
6617 break;
6618
6619 case GOTO_STMT:
6620 lineno = TREE_COMPLEXITY (t);
6621 t = GOTO_DESTINATION (t);
6622 if (TREE_CODE (t) != IDENTIFIER_NODE)
6623 /* Computed goto's must be tsubst'd into. On the other hand,
6624 non-computed gotos must not be; the identifier in question
6625 will have no binding. */
6626 t = tsubst_expr (t, args, in_decl);
6627 finish_goto_stmt (t);
6628 break;
6629
6630 case ASM_STMT:
6631 lineno = TREE_COMPLEXITY (t);
6632 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
6633 tsubst_expr (ASM_STRING (t), args, in_decl),
6634 tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
6635 tsubst_expr (ASM_INPUTS (t), args, in_decl),
6636 tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
6637 break;
6638
6639 case TRY_BLOCK:
6640 lineno = TREE_COMPLEXITY (t);
6641 begin_try_block ();
6642 tsubst_expr (TRY_STMTS (t), args, in_decl);
6643 finish_try_block (NULL_TREE);
6644 {
6645 tree handler = TRY_HANDLERS (t);
6646 for (; handler; handler = TREE_CHAIN (handler))
6647 tsubst_expr (handler, args, in_decl);
6648 }
6649 finish_handler_sequence (NULL_TREE);
6650 break;
6651
6652 case HANDLER:
6653 lineno = TREE_COMPLEXITY (t);
6654 begin_handler ();
6655 if (HANDLER_PARMS (t))
6656 {
6657 tree d = HANDLER_PARMS (t);
6658 expand_start_catch_block
6659 (tsubst (TREE_OPERAND (d, 1), args, in_decl),
6660 tsubst (TREE_OPERAND (d, 0), args, in_decl));
6661 }
6662 else
6663 expand_start_catch_block (NULL_TREE, NULL_TREE);
6664 finish_handler_parms (NULL_TREE);
6665 tsubst_expr (HANDLER_BODY (t), args, in_decl);
6666 finish_handler (NULL_TREE);
6667 break;
6668
6669 case TAG_DEFN:
6670 lineno = TREE_COMPLEXITY (t);
6671 t = TREE_TYPE (t);
6672 if (TREE_CODE (t) == ENUMERAL_TYPE)
6673 tsubst (t, args, NULL_TREE);
6674 break;
6675
6676 default:
6677 return build_expr_from_tree (tsubst_copy (t, args, in_decl));
6678 }
6679 return NULL_TREE;
6680 }
6681
6682 /* Instantiate the indicated variable or function template TMPL with
6683 the template arguments in TARG_PTR. */
6684
6685 tree
6686 instantiate_template (tmpl, targ_ptr)
6687 tree tmpl, targ_ptr;
6688 {
6689 tree fndecl;
6690 tree gen_tmpl;
6691 tree spec;
6692 int i, len;
6693 struct obstack *old_fmp_obstack;
6694 extern struct obstack *function_maybepermanent_obstack;
6695 tree inner_args;
6696
6697 if (tmpl == error_mark_node)
6698 return error_mark_node;
6699
6700 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
6701
6702 /* Check to see if we already have this specialization. */
6703 spec = retrieve_specialization (tmpl, targ_ptr);
6704 if (spec != NULL_TREE)
6705 return spec;
6706
6707 if (DECL_TEMPLATE_INFO (tmpl))
6708 {
6709 /* The TMPL is a partial instantiation. To get a full set of
6710 arguments we must add the arguments used to perform the
6711 partial instantiation. */
6712 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
6713 targ_ptr);
6714 gen_tmpl = most_general_template (tmpl);
6715
6716 /* Check to see if we already have this specialization. */
6717 spec = retrieve_specialization (gen_tmpl, targ_ptr);
6718 if (spec != NULL_TREE)
6719 return spec;
6720 }
6721 else
6722 gen_tmpl = tmpl;
6723
6724 push_obstacks (&permanent_obstack, &permanent_obstack);
6725 old_fmp_obstack = function_maybepermanent_obstack;
6726 function_maybepermanent_obstack = &permanent_obstack;
6727
6728 len = DECL_NTPARMS (gen_tmpl);
6729 inner_args = innermost_args (targ_ptr);
6730 i = len;
6731 while (i--)
6732 {
6733 tree t = TREE_VEC_ELT (inner_args, i);
6734 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
6735 {
6736 tree nt = target_type (t);
6737 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
6738 {
6739 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
6740 cp_error (" trying to instantiate `%D'", gen_tmpl);
6741 fndecl = error_mark_node;
6742 goto out;
6743 }
6744 }
6745 }
6746 targ_ptr = copy_to_permanent (targ_ptr);
6747
6748 /* substitute template parameters */
6749 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl);
6750 /* The DECL_TI_TEMPLATE should always be the immediate parent
6751 template, not the most general template. */
6752 DECL_TI_TEMPLATE (fndecl) = tmpl;
6753
6754 if (flag_external_templates)
6755 add_pending_template (fndecl);
6756
6757 out:
6758 function_maybepermanent_obstack = old_fmp_obstack;
6759 pop_obstacks ();
6760
6761 return fndecl;
6762 }
6763
6764 /* Push the name of the class template into the scope of the instantiation. */
6765
6766 void
6767 overload_template_name (type)
6768 tree type;
6769 {
6770 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
6771 tree decl;
6772
6773 if (IDENTIFIER_CLASS_VALUE (id)
6774 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
6775 return;
6776
6777 decl = build_decl (TYPE_DECL, id, type);
6778 SET_DECL_ARTIFICIAL (decl);
6779 pushdecl_class_level (decl);
6780 }
6781
6782 /* Like type_unification but designed specially to handle conversion
6783 operators.
6784
6785 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6786 arguments that are being used when calling it.
6787
6788 If FN is a conversion operator, RETURN_TYPE is the type desired as
6789 the result of the conversion operator.
6790
6791 The EXTRA_FN_ARG, if any, is the type of an additional
6792 parameter to be added to the beginning of FN's parameter list.
6793
6794 The other arguments are as for type_unification. */
6795
6796 int
6797 fn_type_unification (fn, explicit_targs, targs, args, return_type,
6798 strict, extra_fn_arg)
6799 tree fn, explicit_targs, targs, args, return_type;
6800 unification_kind_t strict;
6801 tree extra_fn_arg;
6802 {
6803 tree parms;
6804
6805 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
6806
6807 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6808
6809 if (DECL_CONV_FN_P (fn))
6810 {
6811 /* This is a template conversion operator. Use the return types
6812 as well as the argument types. */
6813 parms = scratch_tree_cons (NULL_TREE,
6814 TREE_TYPE (TREE_TYPE (fn)),
6815 parms);
6816 args = scratch_tree_cons (NULL_TREE, return_type, args);
6817 }
6818
6819 if (extra_fn_arg != NULL_TREE)
6820 parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms);
6821
6822 /* We allow incomplete unification without an error message here
6823 because the standard doesn't seem to explicitly prohibit it. Our
6824 callers must be ready to deal with unification failures in any
6825 event. */
6826 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
6827 targs,
6828 parms,
6829 args,
6830 explicit_targs,
6831 strict, 1);
6832 }
6833
6834
6835 /* Type unification.
6836
6837 We have a function template signature with one or more references to
6838 template parameters, and a parameter list we wish to fit to this
6839 template. If possible, produce a list of parameters for the template
6840 which will cause it to fit the supplied parameter list.
6841
6842 Return zero for success, 2 for an incomplete match that doesn't resolve
6843 all the types, and 1 for complete failure. An error message will be
6844 printed only for an incomplete match.
6845
6846 TPARMS[NTPARMS] is an array of template parameter types.
6847
6848 TARGS[NTPARMS] is the array into which the deduced template
6849 parameter values are placed. PARMS is the function template's
6850 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6851 list we're trying to match against it.
6852
6853 The EXPLICIT_TARGS are explicit template arguments provided via a
6854 template-id.
6855
6856 The parameter STRICT is one of:
6857
6858 DEDUCE_CALL:
6859 We are deducing arguments for a function call, as in
6860 [temp.deduct.call].
6861
6862 DEDUCE_CONV:
6863 We are deducing arguments for a conversion function, as in
6864 [temp.deduct.conv].
6865
6866 DEDUCE_EXACT:
6867 We are deducing arguments when calculating the partial
6868 ordering between specializations of function or class
6869 templates, as in [temp.func.order] and [temp.class.order],
6870 when doing an explicit instantiation as in [temp.explicit],
6871 when determining an explicit specialization as in
6872 [temp.expl.spec], or when taking the address of a function
6873 template, as in [temp.deduct.funcaddr]. */
6874
6875 int
6876 type_unification (tparms, targs, parms, args, explicit_targs,
6877 strict, allow_incomplete)
6878 tree tparms, targs, parms, args, explicit_targs;
6879 unification_kind_t strict;
6880 int allow_incomplete;
6881 {
6882 int* explicit_mask;
6883 int i;
6884
6885 for (i = 0; i < TREE_VEC_LENGTH (tparms); i++)
6886 TREE_VEC_ELT (targs, i) = NULL_TREE;
6887
6888 if (explicit_targs != NULL_TREE)
6889 {
6890 tree arg_vec;
6891 arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0,
6892 0);
6893
6894 if (arg_vec == error_mark_node)
6895 return 1;
6896
6897 explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
6898 bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
6899
6900 for (i = 0;
6901 i < TREE_VEC_LENGTH (arg_vec)
6902 && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;
6903 ++i)
6904 {
6905 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
6906 /* Let unify know that this argument was explicit. */
6907 explicit_mask [i] = 1;
6908 }
6909 }
6910 else
6911 explicit_mask = 0;
6912
6913 return
6914 type_unification_real (tparms, targs, parms, args, 0,
6915 strict, allow_incomplete, explicit_mask);
6916 }
6917
6918 /* Adjust types before performing type deduction, as described in
6919 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
6920 sections are symmetric. PARM is the type of a function parameter
6921 or the return type of the conversion function. ARG is the type of
6922 the argument passed to the call, or the type of the value
6923 intialized with the result of the conversion function. */
6924
6925 void
6926 maybe_adjust_types_for_deduction (strict, parm, arg)
6927 unification_kind_t strict;
6928 tree* parm;
6929 tree* arg;
6930 {
6931 switch (strict)
6932 {
6933 case DEDUCE_CALL:
6934 break;
6935
6936 case DEDUCE_CONV:
6937 {
6938 /* Swap PARM and ARG throughout the remainder of this
6939 function; the handling is precisely symmetric since PARM
6940 will initialize ARG rather than vice versa. */
6941 tree* temp = parm;
6942 parm = arg;
6943 arg = temp;
6944 break;
6945 }
6946
6947 case DEDUCE_EXACT:
6948 /* There is nothing to do in this case. */
6949 return;
6950
6951 default:
6952 my_friendly_abort (0);
6953 }
6954
6955 if (TREE_CODE (*parm) != REFERENCE_TYPE)
6956 {
6957 /* [temp.deduct.call]
6958
6959 If P is not a reference type:
6960
6961 --If A is an array type, the pointer type produced by the
6962 array-to-pointer standard conversion (_conv.array_) is
6963 used in place of A for type deduction; otherwise,
6964
6965 --If A is a function type, the pointer type produced by
6966 the function-to-pointer standard conversion
6967 (_conv.func_) is used in place of A for type deduction;
6968 otherwise,
6969
6970 --If A is a cv-qualified type, the top level
6971 cv-qualifiers of A's type are ignored for type
6972 deduction. */
6973 if (TREE_CODE (*arg) == ARRAY_TYPE)
6974 *arg = build_pointer_type (TREE_TYPE (*arg));
6975 else if (TREE_CODE (*arg) == FUNCTION_TYPE
6976 || TREE_CODE (*arg) == METHOD_TYPE)
6977 *arg = build_pointer_type (*arg);
6978 else
6979 *arg = TYPE_MAIN_VARIANT (*arg);
6980 }
6981
6982 /* [temp.deduct.call]
6983
6984 If P is a cv-qualified type, the top level cv-qualifiers
6985 of P's type are ignored for type deduction. If P is a
6986 reference type, the type referred to by P is used for
6987 type deduction. */
6988 *parm = TYPE_MAIN_VARIANT (*parm);
6989 if (TREE_CODE (*parm) == REFERENCE_TYPE)
6990 *parm = TREE_TYPE (*parm);
6991 }
6992
6993 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
6994 integers, with ones in positions corresponding to arguments in
6995 targs that were provided explicitly, and zeros elsewhere.
6996
6997 If SUBR is 1, we're being called recursively (to unify the
6998 arguments of a function or method parameter of a function
6999 template). */
7000
7001 static int
7002 type_unification_real (tparms, targs, parms, args, subr,
7003 strict, allow_incomplete, explicit_mask)
7004 tree tparms, targs, parms, args;
7005 int subr;
7006 unification_kind_t strict;
7007 int allow_incomplete;
7008 int* explicit_mask;
7009 {
7010 tree parm, arg;
7011 int i;
7012 int ntparms = TREE_VEC_LENGTH (tparms);
7013 int sub_strict;
7014
7015 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7016 my_friendly_assert (parms == NULL_TREE
7017 || TREE_CODE (parms) == TREE_LIST, 290);
7018 /* ARGS could be NULL (via a call from parse.y to
7019 build_x_function_call). */
7020 if (args)
7021 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
7022 my_friendly_assert (ntparms > 0, 292);
7023
7024 switch (strict)
7025 {
7026 case DEDUCE_CALL:
7027 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
7028 break;
7029
7030 case DEDUCE_CONV:
7031 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7032 break;
7033
7034 case DEDUCE_EXACT:
7035 sub_strict = UNIFY_ALLOW_NONE;
7036 break;
7037
7038 default:
7039 my_friendly_abort (0);
7040 }
7041
7042 while (parms
7043 && parms != void_list_node
7044 && args
7045 && args != void_list_node)
7046 {
7047 parm = TREE_VALUE (parms);
7048 parms = TREE_CHAIN (parms);
7049 arg = TREE_VALUE (args);
7050 args = TREE_CHAIN (args);
7051
7052 if (arg == error_mark_node)
7053 return 1;
7054 if (arg == unknown_type_node)
7055 /* We can't deduce anything from this, but we might get all the
7056 template args from other function args. */
7057 continue;
7058
7059 /* Conversions will be performed on a function argument that
7060 corresponds with a function parameter that contains only
7061 non-deducible template parameters and explicitly specified
7062 template parameters. */
7063 if (! uses_template_parms (parm))
7064 {
7065 tree type;
7066
7067 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7068 type = TREE_TYPE (arg);
7069 else
7070 {
7071 type = arg;
7072 arg = NULL_TREE;
7073 }
7074
7075 if (strict == DEDUCE_EXACT)
7076 {
7077 if (same_type_p (parm, type))
7078 continue;
7079 }
7080 else
7081 /* It might work; we shouldn't check now, because we might
7082 get into infinite recursion. Overload resolution will
7083 handle it. */
7084 continue;
7085
7086 return 1;
7087 }
7088
7089 #if 0
7090 if (TREE_CODE (arg) == VAR_DECL)
7091 arg = TREE_TYPE (arg);
7092 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
7093 arg = TREE_TYPE (arg);
7094 #else
7095 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7096 {
7097 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
7098 if (type_unknown_p (arg))
7099 {
7100 /* [temp.deduct.type] A template-argument can be deduced from
7101 a pointer to function or pointer to member function
7102 argument if the set of overloaded functions does not
7103 contain function templates and at most one of a set of
7104 overloaded functions provides a unique match. */
7105
7106 if (resolve_overloaded_unification
7107 (tparms, targs, parm, arg, strict, sub_strict, explicit_mask)
7108 != 0)
7109 return 1;
7110 continue;
7111 }
7112 arg = TREE_TYPE (arg);
7113 }
7114 #endif
7115 if (!subr)
7116 maybe_adjust_types_for_deduction (strict, &parm, &arg);
7117
7118 switch (unify (tparms, targs, parm, arg, sub_strict,
7119 explicit_mask))
7120 {
7121 case 0:
7122 break;
7123 case 1:
7124 return 1;
7125 }
7126 }
7127 /* Fail if we've reached the end of the parm list, and more args
7128 are present, and the parm list isn't variadic. */
7129 if (args && args != void_list_node && parms == void_list_node)
7130 return 1;
7131 /* Fail if parms are left and they don't have default values. */
7132 if (parms
7133 && parms != void_list_node
7134 && TREE_PURPOSE (parms) == NULL_TREE)
7135 return 1;
7136 if (!subr)
7137 for (i = 0; i < ntparms; i++)
7138 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
7139 {
7140 if (!allow_incomplete)
7141 error ("incomplete type unification");
7142 return 2;
7143 }
7144 return 0;
7145 }
7146
7147 /* Subroutine of type_unification_real. Args are like the variables at the
7148 call site. ARG is an overloaded function (or template-id); we try
7149 deducing template args from each of the overloads, and if only one
7150 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7151
7152 static int
7153 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
7154 sub_strict, explicit_mask)
7155 tree tparms, targs, parm, arg;
7156 unification_kind_t strict;
7157 int sub_strict;
7158 int* explicit_mask;
7159 {
7160 tree tempargs = copy_node (targs);
7161 int good = 0;
7162
7163 if (TREE_CODE (arg) == ADDR_EXPR)
7164 arg = TREE_OPERAND (arg, 0);
7165
7166 /* Strip baselink information. */
7167 while (TREE_CODE (arg) == TREE_LIST)
7168 arg = TREE_VALUE (arg);
7169
7170 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
7171 {
7172 /* If we got some explicit template args, we need to plug them into
7173 the affected templates before we try to unify, in case the
7174 explicit args will completely resolve the templates in question. */
7175
7176 tree expl_subargs = TREE_OPERAND (arg, 1);
7177 arg = TREE_OPERAND (arg, 0);
7178
7179 for (; arg; arg = OVL_NEXT (arg))
7180 {
7181 tree fn = OVL_CURRENT (arg);
7182 tree subargs, elem;
7183
7184 if (TREE_CODE (fn) != TEMPLATE_DECL)
7185 continue;
7186
7187 subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
7188 if (subargs)
7189 {
7190 elem = tsubst (TREE_TYPE (fn), subargs, NULL_TREE);
7191 good += try_one_overload (tparms, targs, tempargs, parm, elem,
7192 strict, sub_strict, explicit_mask);
7193 }
7194 }
7195 }
7196 else if (TREE_CODE (arg) == OVERLOAD)
7197 {
7198 for (; arg; arg = OVL_NEXT (arg))
7199 good += try_one_overload (tparms, targs, tempargs, parm,
7200 TREE_TYPE (OVL_CURRENT (arg)),
7201 strict, sub_strict, explicit_mask);
7202 }
7203 else
7204 my_friendly_abort (981006);
7205
7206 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7207 to function or pointer to member function argument if the set of
7208 overloaded functions does not contain function templates and at most
7209 one of a set of overloaded functions provides a unique match.
7210
7211 So if we found multiple possibilities, we return success but don't
7212 deduce anything. */
7213
7214 if (good == 1)
7215 {
7216 int i = TREE_VEC_LENGTH (targs);
7217 for (; i--; )
7218 if (TREE_VEC_ELT (tempargs, i))
7219 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
7220 }
7221 if (good)
7222 return 0;
7223
7224 return 1;
7225 }
7226
7227 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7228 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7229 different overloads deduce different arguments for a given parm.
7230 Returns 1 on success. */
7231
7232 static int
7233 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
7234 sub_strict, explicit_mask)
7235 tree tparms, orig_targs, targs, parm, arg;
7236 unification_kind_t strict;
7237 int sub_strict;
7238 int* explicit_mask;
7239 {
7240 int nargs;
7241 tree tempargs;
7242 int i;
7243
7244 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7245 to function or pointer to member function argument if the set of
7246 overloaded functions does not contain function templates and at most
7247 one of a set of overloaded functions provides a unique match.
7248
7249 So if this is a template, just return success. */
7250
7251 if (uses_template_parms (arg))
7252 return 1;
7253
7254 maybe_adjust_types_for_deduction (strict, &parm, &arg);
7255
7256 /* We don't copy orig_targs for this because if we have already deduced
7257 some template args from previous args, unify would complain when we
7258 try to deduce a template parameter for the same argument, even though
7259 there isn't really a conflict. */
7260 nargs = TREE_VEC_LENGTH (targs);
7261 tempargs = make_scratch_vec (nargs);
7262
7263 if (unify (tparms, tempargs, parm, arg, sub_strict, explicit_mask) != 0)
7264 return 0;
7265
7266 /* First make sure we didn't deduce anything that conflicts with
7267 explicitly specified args. */
7268 for (i = nargs; i--; )
7269 {
7270 tree elt = TREE_VEC_ELT (tempargs, i);
7271 tree oldelt = TREE_VEC_ELT (orig_targs, i);
7272
7273 if (elt == NULL_TREE)
7274 continue;
7275 else if (uses_template_parms (elt))
7276 {
7277 /* Since we're unifying against ourselves, we will fill in template
7278 args used in the function parm list with our own template parms.
7279 Discard them. */
7280 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
7281 continue;
7282 }
7283 else if (oldelt && ! template_args_equal (oldelt, elt))
7284 return 0;
7285 }
7286
7287 for (i = nargs; i--; )
7288 {
7289 tree elt = TREE_VEC_ELT (tempargs, i);
7290
7291 if (elt)
7292 TREE_VEC_ELT (targs, i) = elt;
7293 }
7294
7295 return 1;
7296 }
7297
7298 /* Returns the level of DECL, which declares a template parameter. */
7299
7300 int
7301 template_decl_level (decl)
7302 tree decl;
7303 {
7304 switch (TREE_CODE (decl))
7305 {
7306 case TYPE_DECL:
7307 case TEMPLATE_DECL:
7308 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
7309
7310 case PARM_DECL:
7311 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
7312
7313 default:
7314 my_friendly_abort (0);
7315 return 0;
7316 }
7317 }
7318
7319 /* Decide whether ARG can be unified with PARM, considering only the
7320 cv-qualifiers of each type, given STRICT as documented for unify.
7321 Returns non-zero iff the unification is OK on that basis.*/
7322
7323 int
7324 check_cv_quals_for_unify (strict, arg, parm)
7325 int strict;
7326 tree arg;
7327 tree parm;
7328 {
7329 return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
7330 && !at_least_as_qualified_p (arg, parm))
7331 || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
7332 && (!at_least_as_qualified_p (parm, arg))));
7333 }
7334
7335 /* Takes parameters as for type_unification. Returns 0 if the
7336 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7337 bitwise or of the following flags:
7338
7339 UNIFY_ALLOW_NONE:
7340 Require an exact match between PARM and ARG.
7341 UNIFY_ALLOW_MORE_CV_QUAL:
7342 Allow the deduced ARG to be more cv-qualified than ARG.
7343 UNIFY_ALLOW_LESS_CV_QUAL:
7344 Allow the deduced ARG to be less cv-qualified than ARG.
7345 UNIFY_ALLOW_DERIVED:
7346 Allow the deduced ARG to be a template base class of ARG,
7347 or a pointer to a template base class of the type pointed to by
7348 ARG.
7349 UNIFY_ALLOW_INTEGER:
7350 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7351 case for more information. */
7352
7353 int
7354 unify (tparms, targs, parm, arg, strict, explicit_mask)
7355 tree tparms, targs, parm, arg;
7356 int strict;
7357 int* explicit_mask;
7358 {
7359 int idx;
7360 tree targ;
7361 tree tparm;
7362
7363 /* I don't think this will do the right thing with respect to types.
7364 But the only case I've seen it in so far has been array bounds, where
7365 signedness is the only information lost, and I think that will be
7366 okay. */
7367 while (TREE_CODE (parm) == NOP_EXPR)
7368 parm = TREE_OPERAND (parm, 0);
7369
7370 if (arg == error_mark_node)
7371 return 1;
7372 if (arg == unknown_type_node)
7373 /* We can't deduce anything from this, but we might get all the
7374 template args from other function args. */
7375 return 0;
7376
7377 /* If PARM uses template parameters, then we can't bail out here,
7378 even if ARG == PARM, since we won't record unifications for the
7379 template parameters. We might need them if we're trying to
7380 figure out which of two things is more specialized. */
7381 if (arg == parm && !uses_template_parms (parm))
7382 return 0;
7383
7384 /* Immediately reject some pairs that won't unify because of
7385 cv-qualification mismatches. */
7386 if (TREE_CODE (arg) == TREE_CODE (parm)
7387 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
7388 /* We check the cv-qualifiers when unifying with template type
7389 parameters below. We want to allow ARG `const T' to unify with
7390 PARM `T' for example, when computing which of two templates
7391 is more specialized, for example. */
7392 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
7393 && !check_cv_quals_for_unify (strict, arg, parm))
7394 return 1;
7395
7396 switch (TREE_CODE (parm))
7397 {
7398 case TYPENAME_TYPE:
7399 /* In a type which contains a nested-name-specifier, template
7400 argument values cannot be deduced for template parameters used
7401 within the nested-name-specifier. */
7402 return 0;
7403
7404 case TEMPLATE_TYPE_PARM:
7405 case TEMPLATE_TEMPLATE_PARM:
7406 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7407
7408 if (TEMPLATE_TYPE_LEVEL (parm)
7409 != template_decl_level (tparm))
7410 /* The PARM is not one we're trying to unify. Just check
7411 to see if it matches ARG. */
7412 return (TREE_CODE (arg) == TREE_CODE (parm)
7413 && same_type_p (parm, arg)) ? 0 : 1;
7414 idx = TEMPLATE_TYPE_IDX (parm);
7415 targ = TREE_VEC_ELT (targs, idx);
7416 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
7417
7418 /* Check for mixed types and values. */
7419 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
7420 && TREE_CODE (tparm) != TYPE_DECL)
7421 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
7422 && TREE_CODE (tparm) != TEMPLATE_DECL))
7423 return 1;
7424
7425 if (!strict && targ != NULL_TREE
7426 && explicit_mask && explicit_mask[idx])
7427 /* An explicit template argument. Don't even try to match
7428 here; the overload resolution code will manage check to
7429 see whether the call is legal. */
7430 return 0;
7431
7432 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
7433 {
7434 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
7435 {
7436 /* We arrive here when PARM does not involve template
7437 specialization. */
7438
7439 /* ARG must be constructed from a template class. */
7440 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
7441 return 1;
7442
7443 {
7444 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
7445 tree parmvec = TYPE_TI_ARGS (parm);
7446 tree argvec = CLASSTYPE_TI_ARGS (arg);
7447 tree argtmplvec
7448 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
7449 int i;
7450
7451 /* The parameter and argument roles have to be switched here
7452 in order to handle default arguments properly. For example,
7453 template<template <class> class TT> void f(TT<int>)
7454 should be able to accept vector<int> which comes from
7455 template <class T, class Allocator = allocator>
7456 class vector. */
7457
7458 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1)
7459 == error_mark_node)
7460 return 1;
7461
7462 /* Deduce arguments T, i from TT<T> or TT<i>. */
7463 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
7464 {
7465 tree t = TREE_VEC_ELT (parmvec, i);
7466 if (TREE_CODE (t) != TEMPLATE_TYPE_PARM
7467 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM
7468 && TREE_CODE (t) != TEMPLATE_PARM_INDEX)
7469 continue;
7470
7471 /* This argument can be deduced. */
7472
7473 if (unify (tparms, targs, t,
7474 TREE_VEC_ELT (argvec, i),
7475 UNIFY_ALLOW_NONE, explicit_mask))
7476 return 1;
7477 }
7478 }
7479 arg = CLASSTYPE_TI_TEMPLATE (arg);
7480 }
7481 }
7482 else
7483 {
7484 /* If PARM is `const T' and ARG is only `int', we don't have
7485 a match unless we are allowing additional qualification.
7486 If ARG is `const int' and PARM is just `T' that's OK;
7487 that binds `const int' to `T'. */
7488 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
7489 arg, parm))
7490 return 1;
7491
7492 /* Consider the case where ARG is `const volatile int' and
7493 PARM is `const T'. Then, T should be `volatile int'. */
7494 arg =
7495 cp_build_qualified_type (arg,
7496 CP_TYPE_QUALS (arg)
7497 & ~CP_TYPE_QUALS (parm));
7498 }
7499
7500 /* Simple cases: Value already set, does match or doesn't. */
7501 if (targ != NULL_TREE
7502 && (same_type_p (targ, arg)
7503 || (explicit_mask && explicit_mask[idx])))
7504 return 0;
7505 else if (targ)
7506 return 1;
7507 TREE_VEC_ELT (targs, idx) = arg;
7508 return 0;
7509
7510 case TEMPLATE_PARM_INDEX:
7511 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7512
7513 if (TEMPLATE_PARM_LEVEL (parm)
7514 != template_decl_level (tparm))
7515 /* The PARM is not one we're trying to unify. Just check
7516 to see if it matches ARG. */
7517 return (TREE_CODE (arg) == TREE_CODE (parm)
7518 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
7519
7520 idx = TEMPLATE_PARM_IDX (parm);
7521 targ = TREE_VEC_ELT (targs, idx);
7522
7523 if (targ)
7524 {
7525 int i = (cp_tree_equal (targ, arg) > 0);
7526 if (i == 1)
7527 return 0;
7528 else if (i == 0)
7529 return 1;
7530 else
7531 my_friendly_abort (42);
7532 }
7533
7534 /* [temp.deduct.type] If, in the declaration of a function template
7535 with a non-type template-parameter, the non-type
7536 template-parameter is used in an expression in the function
7537 parameter-list and, if the corresponding template-argument is
7538 deduced, the template-argument type shall match the type of the
7539 template-parameter exactly, except that a template-argument
7540 deduced from an array bound may be of any integral type. */
7541 if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
7542 /* OK */;
7543 else if ((strict & UNIFY_ALLOW_INTEGER)
7544 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
7545 || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
7546 /* OK */;
7547 else
7548 return 1;
7549
7550 TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
7551 return 0;
7552
7553 case POINTER_TYPE:
7554 {
7555 int sub_strict;
7556
7557 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
7558 return (unify (tparms, targs, parm,
7559 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict,
7560 explicit_mask));
7561
7562 if (TREE_CODE (arg) != POINTER_TYPE)
7563 return 1;
7564
7565 /* [temp.deduct.call]
7566
7567 A can be another pointer or pointer to member type that can
7568 be converted to the deduced A via a qualification
7569 conversion (_conv.qual_).
7570
7571 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
7572 This will allow for additional cv-qualification of the
7573 pointed-to types if appropriate. In general, this is a bit
7574 too generous; we are only supposed to allow qualification
7575 conversions and this method will allow an ARG of char** and
7576 a deduced ARG of const char**. However, overload
7577 resolution will subsequently invalidate the candidate, so
7578 this is probably OK. */
7579 sub_strict = strict;
7580
7581 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
7582 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
7583 /* The derived-to-base conversion only persists through one
7584 level of pointers. */
7585 sub_strict &= ~UNIFY_ALLOW_DERIVED;
7586
7587 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
7588 (arg), sub_strict, explicit_mask);
7589 }
7590
7591 case REFERENCE_TYPE:
7592 if (TREE_CODE (arg) != REFERENCE_TYPE)
7593 return 1;
7594 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7595 UNIFY_ALLOW_NONE, explicit_mask);
7596
7597 case ARRAY_TYPE:
7598 if (TREE_CODE (arg) != ARRAY_TYPE)
7599 return 1;
7600 if ((TYPE_DOMAIN (parm) == NULL_TREE)
7601 != (TYPE_DOMAIN (arg) == NULL_TREE))
7602 return 1;
7603 if (TYPE_DOMAIN (parm) != NULL_TREE
7604 && unify (tparms, targs, TYPE_DOMAIN (parm),
7605 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0)
7606 return 1;
7607 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7608 UNIFY_ALLOW_NONE, explicit_mask);
7609
7610 case REAL_TYPE:
7611 case COMPLEX_TYPE:
7612 case INTEGER_TYPE:
7613 case BOOLEAN_TYPE:
7614 case VOID_TYPE:
7615 if (TREE_CODE (arg) != TREE_CODE (parm))
7616 return 1;
7617
7618 if (TREE_CODE (parm) == INTEGER_TYPE
7619 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
7620 {
7621 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
7622 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
7623 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER,
7624 explicit_mask))
7625 return 1;
7626 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
7627 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
7628 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER,
7629 explicit_mask))
7630 return 1;
7631 }
7632 /* We use the TYPE_MAIN_VARIANT since we have already
7633 checked cv-qualification at the top of the
7634 function. */
7635 else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
7636 TYPE_MAIN_VARIANT (parm)))
7637 return 1;
7638
7639 /* As far as unification is concerned, this wins. Later checks
7640 will invalidate it if necessary. */
7641 return 0;
7642
7643 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
7644 /* Type INTEGER_CST can come from ordinary constant template args. */
7645 case INTEGER_CST:
7646 while (TREE_CODE (arg) == NOP_EXPR)
7647 arg = TREE_OPERAND (arg, 0);
7648
7649 if (TREE_CODE (arg) != INTEGER_CST)
7650 return 1;
7651 return !tree_int_cst_equal (parm, arg);
7652
7653 case TREE_VEC:
7654 {
7655 int i;
7656 if (TREE_CODE (arg) != TREE_VEC)
7657 return 1;
7658 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
7659 return 1;
7660 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
7661 if (unify (tparms, targs,
7662 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
7663 UNIFY_ALLOW_NONE, explicit_mask))
7664 return 1;
7665 return 0;
7666 }
7667
7668 case RECORD_TYPE:
7669 case UNION_TYPE:
7670 if (TYPE_PTRMEMFUNC_FLAG (parm))
7671 return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
7672 arg, strict, explicit_mask);
7673
7674 if (TREE_CODE (arg) != TREE_CODE (parm))
7675 return 1;
7676
7677 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
7678 {
7679 tree t = NULL_TREE;
7680 if (strict & UNIFY_ALLOW_DERIVED)
7681 /* [temp.deduct.call]
7682
7683 If P is a class, and P has the form template-id, then A
7684 can be a derived class of the deduced A. Likewise, if
7685 P is a pointer to a class of the form template-id, A
7686 can be a pointer to a derived class pointed to by the
7687 deduced A.
7688
7689 The call to get_template_base also handles the case
7690 where PARM and ARG are the same type, i.e., where no
7691 derivation is involved. */
7692 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
7693 else if (CLASSTYPE_TEMPLATE_INFO (arg)
7694 && (CLASSTYPE_TI_TEMPLATE (parm)
7695 == CLASSTYPE_TI_TEMPLATE (arg)))
7696 /* Perhaps PARM is something like S<U> and ARG is S<int>.
7697 Then, we should unify `int' and `U'. */
7698 t = arg;
7699
7700 if (! t || t == error_mark_node)
7701 return 1;
7702
7703 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
7704 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE,
7705 explicit_mask);
7706 }
7707 else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
7708 TYPE_MAIN_VARIANT (arg)))
7709 return 1;
7710 return 0;
7711
7712 case METHOD_TYPE:
7713 case FUNCTION_TYPE:
7714 if (TREE_CODE (arg) != TREE_CODE (parm))
7715 return 1;
7716
7717 if (unify (tparms, targs, TREE_TYPE (parm),
7718 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
7719 return 1;
7720 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
7721 TYPE_ARG_TYPES (arg), 1,
7722 DEDUCE_EXACT, 0, explicit_mask);
7723
7724 case OFFSET_TYPE:
7725 if (TREE_CODE (arg) != OFFSET_TYPE)
7726 return 1;
7727 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
7728 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
7729 return 1;
7730 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7731 strict, explicit_mask);
7732
7733 case CONST_DECL:
7734 if (arg != decl_constant_value (parm))
7735 return 1;
7736 return 0;
7737
7738 case TEMPLATE_DECL:
7739 /* Matched cases are handled by the ARG == PARM test above. */
7740 return 1;
7741
7742 case MINUS_EXPR:
7743 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
7744 {
7745 /* We handle this case specially, since it comes up with
7746 arrays. In particular, something like:
7747
7748 template <int N> void f(int (&x)[N]);
7749
7750 Here, we are trying to unify the range type, which
7751 looks like [0 ... (N - 1)]. */
7752 tree t, t1, t2;
7753 t1 = TREE_OPERAND (parm, 0);
7754 t2 = TREE_OPERAND (parm, 1);
7755
7756 /* Should this be a regular fold? */
7757 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
7758 integer_type_node,
7759 arg, t2));
7760
7761 return unify (tparms, targs, t1, t, strict, explicit_mask);
7762 }
7763 /* else fall through */
7764
7765 default:
7766 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
7767 {
7768 /* We're looking at an expression. This can happen with
7769 something like:
7770
7771 template <int I>
7772 void foo(S<I>, S<I + 2>);
7773
7774 If the call looked like:
7775
7776 foo(S<2>(), S<4>());
7777
7778 we would have already matched `I' with `2'. Now, we'd
7779 like to know if `4' matches `I + 2'. So, we substitute
7780 into that expression, and fold constants, in the hope of
7781 figuring it out. */
7782 tree t =
7783 maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE));
7784 tree a = maybe_fold_nontype_arg (arg);
7785
7786 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
7787 /* Good, we mangaged to simplify the exression. */
7788 return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE,
7789 explicit_mask);
7790 else
7791 /* Bad, we couldn't simplify this. Assume it doesn't
7792 unify. */
7793 return 1;
7794 }
7795 else
7796 sorry ("use of `%s' in template type unification",
7797 tree_code_name [(int) TREE_CODE (parm)]);
7798
7799 return 1;
7800 }
7801 }
7802 \f
7803 /* Called if RESULT is explicitly instantiated, or is a member of an
7804 explicitly instantiated class, or if using -frepo and the
7805 instantiation of RESULT has been assigned to this file. */
7806
7807 void
7808 mark_decl_instantiated (result, extern_p)
7809 tree result;
7810 int extern_p;
7811 {
7812 if (TREE_CODE (result) != FUNCTION_DECL)
7813 /* The TREE_PUBLIC flag for function declarations will have been
7814 set correctly by tsubst. */
7815 TREE_PUBLIC (result) = 1;
7816
7817 if (! extern_p)
7818 {
7819 DECL_INTERFACE_KNOWN (result) = 1;
7820 DECL_NOT_REALLY_EXTERN (result) = 1;
7821
7822 /* Always make artificials weak. */
7823 if (DECL_ARTIFICIAL (result) && flag_weak)
7824 comdat_linkage (result);
7825 /* For WIN32 we also want to put explicit instantiations in
7826 linkonce sections. */
7827 else if (TREE_PUBLIC (result))
7828 maybe_make_one_only (result);
7829 }
7830 else if (TREE_CODE (result) == FUNCTION_DECL)
7831 mark_inline_for_output (result);
7832 }
7833
7834 /* Given two function templates PAT1 and PAT2, and explicit template
7835 arguments EXPLICIT_ARGS return:
7836
7837 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
7838 -1 if PAT2 is more specialized than PAT1.
7839 0 if neither is more specialized. */
7840
7841 int
7842 more_specialized (pat1, pat2, explicit_args)
7843 tree pat1, pat2, explicit_args;
7844 {
7845 tree targs;
7846 int winner = 0;
7847
7848 targs = get_bindings_overload (pat1, pat2, explicit_args);
7849 if (targs)
7850 --winner;
7851
7852 targs = get_bindings_overload (pat2, pat1, explicit_args);
7853 if (targs)
7854 ++winner;
7855
7856 return winner;
7857 }
7858
7859 /* Given two class template specialization list nodes PAT1 and PAT2, return:
7860
7861 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
7862 -1 if PAT2 is more specialized than PAT1.
7863 0 if neither is more specialized. */
7864
7865 int
7866 more_specialized_class (pat1, pat2)
7867 tree pat1, pat2;
7868 {
7869 tree targs;
7870 int winner = 0;
7871
7872 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
7873 TREE_PURPOSE (pat2));
7874 if (targs)
7875 --winner;
7876
7877 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
7878 TREE_PURPOSE (pat1));
7879 if (targs)
7880 ++winner;
7881
7882 return winner;
7883 }
7884
7885 /* Return the template arguments that will produce the function signature
7886 DECL from the function template FN, with the explicit template
7887 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
7888 also match. */
7889
7890 static tree
7891 get_bindings_real (fn, decl, explicit_args, check_rettype)
7892 tree fn, decl, explicit_args;
7893 int check_rettype;
7894 {
7895 int ntparms = DECL_NTPARMS (fn);
7896 tree targs = make_scratch_vec (ntparms);
7897 tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
7898 tree extra_fn_arg = NULL_TREE;
7899 int i;
7900
7901 if (DECL_STATIC_FUNCTION_P (fn)
7902 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7903 {
7904 /* Sometimes we are trying to figure out what's being
7905 specialized by a declaration that looks like a method, and it
7906 turns out to be a static member function. */
7907 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
7908 && !is_member_template (fn))
7909 /* The natural thing to do here seems to be to remove the
7910 spurious `this' parameter from the DECL, but that prevents
7911 unification from making use of the class type. So,
7912 instead, we have fn_type_unification add to the parameters
7913 for FN. */
7914 extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
7915 else
7916 /* In this case, though, adding the extra_fn_arg can confuse
7917 things, so we remove from decl_arg_types instead. */
7918 decl_arg_types = TREE_CHAIN (decl_arg_types);
7919 }
7920
7921 i = fn_type_unification (fn, explicit_args, targs,
7922 decl_arg_types,
7923 TREE_TYPE (TREE_TYPE (decl)),
7924 DEDUCE_EXACT,
7925 extra_fn_arg);
7926
7927 if (i != 0)
7928 return NULL_TREE;
7929
7930 if (check_rettype)
7931 {
7932 /* Check to see that the resulting return type is also OK. */
7933 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
7934 NULL_TREE);
7935
7936 if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
7937 return NULL_TREE;
7938 }
7939
7940 return targs;
7941 }
7942
7943 /* For most uses, we want to check the return type. */
7944
7945 tree
7946 get_bindings (fn, decl, explicit_args)
7947 tree fn, decl, explicit_args;
7948 {
7949 return get_bindings_real (fn, decl, explicit_args, 1);
7950 }
7951
7952 /* But for more_specialized, we only care about the parameter types. */
7953
7954 static tree
7955 get_bindings_overload (fn, decl, explicit_args)
7956 tree fn, decl, explicit_args;
7957 {
7958 return get_bindings_real (fn, decl, explicit_args, 0);
7959 }
7960
7961 /* Return the innermost template arguments that, when applied to a
7962 template specialization whose innermost template parameters are
7963 TPARMS, and whose specialization arguments are ARGS, yield the
7964 ARGS.
7965
7966 For example, suppose we have:
7967
7968 template <class T, class U> struct S {};
7969 template <class T> struct S<T*, int> {};
7970
7971 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
7972 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
7973 int}. The resulting vector will be {double}, indicating that `T'
7974 is bound to `double'. */
7975
7976 static tree
7977 get_class_bindings (tparms, parms, args)
7978 tree tparms, parms, args;
7979 {
7980 int i, ntparms = TREE_VEC_LENGTH (tparms);
7981 tree vec = make_temp_vec (ntparms);
7982
7983 args = innermost_args (args);
7984
7985 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7986 {
7987 switch (unify (tparms, vec,
7988 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
7989 UNIFY_ALLOW_NONE, 0))
7990 {
7991 case 0:
7992 break;
7993 case 1:
7994 return NULL_TREE;
7995 }
7996 }
7997
7998 for (i = 0; i < ntparms; ++i)
7999 if (! TREE_VEC_ELT (vec, i))
8000 return NULL_TREE;
8001
8002 return vec;
8003 }
8004
8005 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8006 Pick the most specialized template, and return the corresponding
8007 instantiation, or if there is no corresponding instantiation, the
8008 template itself. EXPLICIT_ARGS is any template arguments explicity
8009 mentioned in a template-id. If there is no most specialized
8010 tempalte, error_mark_node is returned. If there are no templates
8011 at all, NULL_TREE is returned. */
8012
8013 tree
8014 most_specialized_instantiation (instantiations, explicit_args)
8015 tree instantiations;
8016 tree explicit_args;
8017 {
8018 tree fn, champ;
8019 int fate;
8020
8021 if (!instantiations)
8022 return NULL_TREE;
8023
8024 champ = instantiations;
8025 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
8026 {
8027 fate = more_specialized (TREE_VALUE (champ),
8028 TREE_VALUE (fn), explicit_args);
8029 if (fate == 1)
8030 ;
8031 else
8032 {
8033 if (fate == 0)
8034 {
8035 fn = TREE_CHAIN (fn);
8036 if (! fn)
8037 return error_mark_node;
8038 }
8039 champ = fn;
8040 }
8041 }
8042
8043 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
8044 {
8045 fate = more_specialized (TREE_VALUE (champ),
8046 TREE_VALUE (fn), explicit_args);
8047 if (fate != 1)
8048 return error_mark_node;
8049 }
8050
8051 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
8052 }
8053
8054 /* Return the most specialized of the list of templates in FNS that can
8055 produce an instantiation matching DECL, given the explicit template
8056 arguments EXPLICIT_ARGS. */
8057
8058 tree
8059 most_specialized (fns, decl, explicit_args)
8060 tree fns, decl, explicit_args;
8061 {
8062 tree candidates = NULL_TREE;
8063 tree fn, args;
8064
8065 for (fn = fns; fn; fn = TREE_CHAIN (fn))
8066 {
8067 tree candidate = TREE_VALUE (fn);
8068
8069 args = get_bindings (candidate, decl, explicit_args);
8070 if (args)
8071 candidates = scratch_tree_cons (NULL_TREE, candidate,
8072 candidates);
8073 }
8074
8075 return most_specialized_instantiation (candidates, explicit_args);
8076 }
8077
8078 /* If DECL is a specialization of some template, return the most
8079 general such template. For example, given:
8080
8081 template <class T> struct S { template <class U> void f(U); };
8082
8083 if TMPL is `template <class U> void S<int>::f(U)' this will return
8084 the full template. This function will not trace past partial
8085 specializations, however. For example, given in addition:
8086
8087 template <class T> struct S<T*> { template <class U> void f(U); };
8088
8089 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8090 `template <class T> template <class U> S<T*>::f(U)'. */
8091
8092 tree
8093 most_general_template (decl)
8094 tree decl;
8095 {
8096 while (DECL_TEMPLATE_INFO (decl))
8097 decl = DECL_TI_TEMPLATE (decl);
8098
8099 return decl;
8100 }
8101
8102 /* Return the most specialized of the class template specializations
8103 of TMPL which can produce an instantiation matching ARGS, or
8104 error_mark_node if the choice is ambiguous. */
8105
8106 tree
8107 most_specialized_class (tmpl, args)
8108 tree tmpl;
8109 tree args;
8110 {
8111 tree list = NULL_TREE;
8112 tree t;
8113 tree champ;
8114 int fate;
8115
8116 tmpl = most_general_template (tmpl);
8117 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
8118 {
8119 tree spec_args
8120 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
8121 if (spec_args)
8122 {
8123 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
8124 TREE_TYPE (list) = TREE_TYPE (t);
8125 }
8126 }
8127
8128 if (! list)
8129 return NULL_TREE;
8130
8131 t = list;
8132 champ = t;
8133 t = TREE_CHAIN (t);
8134 for (; t; t = TREE_CHAIN (t))
8135 {
8136 fate = more_specialized_class (champ, t);
8137 if (fate == 1)
8138 ;
8139 else
8140 {
8141 if (fate == 0)
8142 {
8143 t = TREE_CHAIN (t);
8144 if (! t)
8145 return error_mark_node;
8146 }
8147 champ = t;
8148 }
8149 }
8150
8151 for (t = list; t && t != champ; t = TREE_CHAIN (t))
8152 {
8153 fate = more_specialized_class (champ, t);
8154 if (fate != 1)
8155 return error_mark_node;
8156 }
8157
8158 return champ;
8159 }
8160
8161 /* called from the parser. */
8162
8163 void
8164 do_decl_instantiation (declspecs, declarator, storage)
8165 tree declspecs, declarator, storage;
8166 {
8167 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
8168 tree result = NULL_TREE;
8169 int extern_p = 0;
8170
8171 if (! DECL_LANG_SPECIFIC (decl))
8172 {
8173 cp_error ("explicit instantiation of non-template `%#D'", decl);
8174 return;
8175 }
8176 else if (TREE_CODE (decl) == VAR_DECL)
8177 {
8178 /* There is an asymmetry here in the way VAR_DECLs and
8179 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8180 the latter, the DECL we get back will be marked as a
8181 template instantiation, and the appropriate
8182 DECL_TEMPLATE_INFO will be set up. This does not happen for
8183 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8184 should handle VAR_DECLs as it currently handles
8185 FUNCTION_DECLs. */
8186 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
8187 if (result && TREE_CODE (result) != VAR_DECL)
8188 {
8189 cp_error ("no matching template for `%D' found", result);
8190 return;
8191 }
8192 }
8193 else if (TREE_CODE (decl) != FUNCTION_DECL)
8194 {
8195 cp_error ("explicit instantiation of `%#D'", decl);
8196 return;
8197 }
8198 else
8199 result = decl;
8200
8201 /* Check for various error cases. Note that if the explicit
8202 instantiation is legal the RESULT will currently be marked as an
8203 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8204 until we get here. */
8205
8206 if (DECL_TEMPLATE_SPECIALIZATION (result))
8207 {
8208 /* [temp.spec]
8209
8210 No program shall both explicitly instantiate and explicitly
8211 specialize a template. */
8212 cp_error ("explicit instantiation of `%#D' after", result);
8213 cp_error_at ("explicit specialization here", result);
8214 return;
8215 }
8216 else if (DECL_EXPLICIT_INSTANTIATION (result))
8217 {
8218 /* [temp.spec]
8219
8220 No program shall explicitly instantiate any template more
8221 than once.
8222
8223 We check DECL_INTERFACE_KNOWN so as not to complain when the
8224 first instantiation was `extern' and the second is not, and
8225 EXTERN_P for the opposite case. */
8226 if (DECL_INTERFACE_KNOWN (result) && !extern_p)
8227 cp_error ("duplicate explicit instantiation of `%#D'", result);
8228
8229 /* If we've already instantiated the template, just return now. */
8230 if (DECL_INTERFACE_KNOWN (result))
8231 return;
8232 }
8233 else if (!DECL_IMPLICIT_INSTANTIATION (result))
8234 {
8235 cp_error ("no matching template for `%D' found", result);
8236 return;
8237 }
8238 else if (!DECL_TEMPLATE_INFO (result))
8239 {
8240 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
8241 return;
8242 }
8243
8244 if (flag_external_templates)
8245 return;
8246
8247 if (storage == NULL_TREE)
8248 ;
8249 else if (storage == ridpointers[(int) RID_EXTERN])
8250 {
8251 if (pedantic)
8252 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
8253 extern_p = 1;
8254 }
8255 else
8256 cp_error ("storage class `%D' applied to template instantiation",
8257 storage);
8258
8259 SET_DECL_EXPLICIT_INSTANTIATION (result);
8260 mark_decl_instantiated (result, extern_p);
8261 repo_template_instantiated (result, extern_p);
8262 if (! extern_p)
8263 instantiate_decl (result);
8264 }
8265
8266 void
8267 mark_class_instantiated (t, extern_p)
8268 tree t;
8269 int extern_p;
8270 {
8271 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
8272 SET_CLASSTYPE_INTERFACE_KNOWN (t);
8273 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
8274 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
8275 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
8276 if (! extern_p)
8277 {
8278 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
8279 rest_of_type_compilation (t, 1);
8280 }
8281 }
8282
8283 void
8284 do_type_instantiation (t, storage)
8285 tree t, storage;
8286 {
8287 int extern_p = 0;
8288 int nomem_p = 0;
8289 int static_p = 0;
8290
8291 if (TREE_CODE (t) == TYPE_DECL)
8292 t = TREE_TYPE (t);
8293
8294 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
8295 {
8296 cp_error ("explicit instantiation of non-template type `%T'", t);
8297 return;
8298 }
8299
8300 complete_type (t);
8301
8302 /* With -fexternal-templates, explicit instantiations are treated the same
8303 as implicit ones. */
8304 if (flag_external_templates)
8305 return;
8306
8307 if (TYPE_SIZE (t) == NULL_TREE)
8308 {
8309 cp_error ("explicit instantiation of `%#T' before definition of template",
8310 t);
8311 return;
8312 }
8313
8314 if (storage != NULL_TREE)
8315 {
8316 if (pedantic)
8317 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8318 IDENTIFIER_POINTER (storage));
8319
8320 if (storage == ridpointers[(int) RID_INLINE])
8321 nomem_p = 1;
8322 else if (storage == ridpointers[(int) RID_EXTERN])
8323 extern_p = 1;
8324 else if (storage == ridpointers[(int) RID_STATIC])
8325 static_p = 1;
8326 else
8327 {
8328 cp_error ("storage class `%D' applied to template instantiation",
8329 storage);
8330 extern_p = 0;
8331 }
8332 }
8333
8334 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
8335 {
8336 /* [temp.spec]
8337
8338 No program shall both explicitly instantiate and explicitly
8339 specialize a template. */
8340 cp_error ("explicit instantiation of `%#T' after", t);
8341 cp_error_at ("explicit specialization here", t);
8342 return;
8343 }
8344 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
8345 {
8346 /* [temp.spec]
8347
8348 No program shall explicitly instantiate any template more
8349 than once.
8350
8351 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8352 instantiation was `extern', and if EXTERN_P then the second
8353 is. Both cases are OK. */
8354 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p)
8355 cp_error ("duplicate explicit instantiation of `%#T'", t);
8356
8357 /* If we've already instantiated the template, just return now. */
8358 if (!CLASSTYPE_INTERFACE_ONLY (t))
8359 return;
8360 }
8361
8362 mark_class_instantiated (t, extern_p);
8363 repo_template_instantiated (t, extern_p);
8364
8365 if (nomem_p)
8366 return;
8367
8368 {
8369 tree tmp;
8370
8371 /* In contrast to implicit instantiation, where only the
8372 declarations, and not the definitions, of members are
8373 instantiated, we have here:
8374
8375 [temp.explicit]
8376
8377 The explicit instantiation of a class template specialization
8378 implies the instantiation of all of its members not
8379 previously explicitly specialized in the translation unit
8380 containing the explicit instantiation.
8381
8382 Of course, we can't instantiate member template classes, since
8383 we don't have any arguments for them. Note that the standard
8384 is unclear on whether the instatiation of the members are
8385 *explicit* instantiations or not. We choose to be generous,
8386 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8387 the explicit instantiation of a class where some of the members
8388 have no definition in the current translation unit. */
8389
8390 if (! static_p)
8391 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
8392 if (TREE_CODE (tmp) == FUNCTION_DECL
8393 && DECL_TEMPLATE_INSTANTIATION (tmp))
8394 {
8395 mark_decl_instantiated (tmp, extern_p);
8396 repo_template_instantiated (tmp, extern_p);
8397 if (! extern_p)
8398 instantiate_decl (tmp);
8399 }
8400
8401 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
8402 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
8403 {
8404 mark_decl_instantiated (tmp, extern_p);
8405 repo_template_instantiated (tmp, extern_p);
8406 if (! extern_p)
8407 instantiate_decl (tmp);
8408 }
8409
8410 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
8411 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
8412 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
8413 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
8414 }
8415 }
8416
8417 /* Given a function DECL, which is a specialization of TMPL, modify
8418 DECL to be a re-instantiation of TMPL with the same template
8419 arguments. TMPL should be the template into which tsubst'ing
8420 should occur for DECL, not the most general template.
8421
8422 One reason for doing this is a scenario like this:
8423
8424 template <class T>
8425 void f(const T&, int i);
8426
8427 void g() { f(3, 7); }
8428
8429 template <class T>
8430 void f(const T& t, const int i) { }
8431
8432 Note that when the template is first instantiated, with
8433 instantiate_template, the resulting DECL will have no name for the
8434 first parameter, and the wrong type for the second. So, when we go
8435 to instantiate the DECL, we regenerate it. */
8436
8437 void
8438 regenerate_decl_from_template (decl, tmpl)
8439 tree decl;
8440 tree tmpl;
8441 {
8442 tree args;
8443 tree code_pattern;
8444 tree new_decl;
8445 tree gen_tmpl;
8446 int unregistered;
8447
8448 args = DECL_TI_ARGS (decl);
8449 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
8450
8451 /* Unregister the specialization so that when we tsubst we will not
8452 just return DECL. We don't have to unregister DECL from TMPL
8453 because if would only be registered there if it were a partial
8454 instantiation of a specialization, which it isn't: it's a full
8455 instantiation. */
8456 gen_tmpl = most_general_template (tmpl);
8457 unregistered = unregister_specialization (decl, gen_tmpl);
8458
8459 /* If the DECL was not unregistered then something peculiar is
8460 happening: we created a specialization but did not call
8461 register_specialization for it. */
8462 my_friendly_assert (unregistered, 0);
8463
8464 if (TREE_CODE (decl) == VAR_DECL)
8465 /* Make sure that we can see identifiers, and compute access
8466 correctly, for the class members used in the declaration of
8467 this static variable. */
8468 pushclass (DECL_CONTEXT (decl), 2);
8469
8470 /* Do the substitution to get the new declaration. */
8471 new_decl = tsubst (code_pattern, args, NULL_TREE);
8472
8473 if (TREE_CODE (decl) == VAR_DECL)
8474 {
8475 /* Set up DECL_INITIAL, since tsubst doesn't. */
8476 DECL_INITIAL (new_decl) =
8477 tsubst_expr (DECL_INITIAL (code_pattern), args,
8478 DECL_TI_TEMPLATE (decl));
8479 /* Pop the class context we pushed above. */
8480 popclass (1);
8481 }
8482
8483 if (TREE_CODE (decl) == FUNCTION_DECL)
8484 {
8485 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
8486 new decl. */
8487 DECL_INITIAL (new_decl) = error_mark_node;
8488 /* And don't complain about a duplicate definition. */
8489 DECL_INITIAL (decl) = NULL_TREE;
8490 }
8491
8492 /* The immediate parent of the new template is still whatever it was
8493 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
8494 general template. We also reset the DECL_ASSEMBLER_NAME since
8495 tsubst always calculates the name as if the function in question
8496 were really a template instance, and sometimes, with friend
8497 functions, this is not so. See tsubst_friend_function for
8498 details. */
8499 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
8500 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
8501 DECL_RTL (new_decl) = DECL_RTL (decl);
8502
8503 /* Call duplicate decls to merge the old and new declarations. */
8504 duplicate_decls (new_decl, decl);
8505
8506 /* Now, re-register the specialization. */
8507 register_specialization (decl, gen_tmpl, args);
8508 }
8509
8510 /* Produce the definition of D, a _DECL generated from a template. */
8511
8512 tree
8513 instantiate_decl (d)
8514 tree d;
8515 {
8516 tree tmpl = DECL_TI_TEMPLATE (d);
8517 tree args = DECL_TI_ARGS (d);
8518 tree td;
8519 tree code_pattern;
8520 tree spec;
8521 tree gen_tmpl;
8522 int nested = in_function_p ();
8523 int pattern_defined;
8524 int line = lineno;
8525 char *file = input_filename;
8526
8527 /* This function should only be used to instantiate templates for
8528 functions and static member variables. */
8529 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
8530 || TREE_CODE (d) == VAR_DECL, 0);
8531
8532 if (DECL_TEMPLATE_INSTANTIATED (d))
8533 /* D has already been instantiated. It might seem reasonable to
8534 check whether or not D is an explict instantiation, and, if so,
8535 stop here. But when an explicit instantiation is deferred
8536 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
8537 is set, even though we still need to do the instantiation. */
8538 return d;
8539
8540 /* If we already have a specialization of this declaration, then
8541 there's no reason to instantiate it. Note that
8542 retrieve_specialization gives us both instantiations and
8543 specializations, so we must explicitly check
8544 DECL_TEMPLATE_SPECIALIZATION. */
8545 gen_tmpl = most_general_template (tmpl);
8546 spec = retrieve_specialization (gen_tmpl, args);
8547 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
8548 return spec;
8549
8550 /* This needs to happen before any tsubsting. */
8551 if (! push_tinst_level (d))
8552 return d;
8553
8554 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
8555 for the instantiation. This is not always the most general
8556 template. Consider, for example:
8557
8558 template <class T>
8559 struct S { template <class U> void f();
8560 template <> void f<int>(); };
8561
8562 and an instantiation of S<double>::f<int>. We want TD to be the
8563 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
8564 td = tmpl;
8565 for (td = tmpl;
8566 /* An instantiation cannot have a definition, so we need a
8567 more general template. */
8568 DECL_TEMPLATE_INSTANTIATION (td)
8569 /* We must also deal with friend templates. Given:
8570
8571 template <class T> struct S {
8572 template <class U> friend void f() {};
8573 };
8574
8575 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
8576 so far as the language is concerned, but that's still
8577 where we get the pattern for the instantiation from. On
8578 ther hand, if the definition comes outside the class, say:
8579
8580 template <class T> struct S {
8581 template <class U> friend void f();
8582 };
8583 template <class U> friend void f() {}
8584
8585 we don't need to look any further. That's what the check for
8586 DECL_INITIAL is for. */
8587 || (TREE_CODE (d) == FUNCTION_DECL
8588 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
8589 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
8590 )
8591 {
8592 /* The present template, TD, should not be a definition. If it
8593 were a definition, we should be using it! Note that we
8594 cannot restructure the loop to just keep going until we find
8595 a template with a definition, since that might go too far if
8596 a specialization was declared, but not defined. */
8597 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
8598 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
8599 0);
8600
8601 /* Fetch the more general template. */
8602 td = DECL_TI_TEMPLATE (td);
8603 }
8604
8605 code_pattern = DECL_TEMPLATE_RESULT (td);
8606
8607 if (TREE_CODE (d) == FUNCTION_DECL)
8608 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
8609 else
8610 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
8611
8612 push_to_top_level ();
8613 lineno = DECL_SOURCE_LINE (d);
8614 input_filename = DECL_SOURCE_FILE (d);
8615
8616 if (pattern_defined)
8617 {
8618 repo_template_used (d);
8619
8620 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
8621 {
8622 if (flag_alt_external_templates)
8623 {
8624 if (interface_unknown)
8625 warn_if_unknown_interface (d);
8626 }
8627 else if (DECL_INTERFACE_KNOWN (code_pattern))
8628 {
8629 DECL_INTERFACE_KNOWN (d) = 1;
8630 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
8631 }
8632 else
8633 warn_if_unknown_interface (code_pattern);
8634 }
8635
8636 if (at_eof)
8637 import_export_decl (d);
8638 }
8639
8640 /* Reject all external templates except inline functions. */
8641 if (DECL_INTERFACE_KNOWN (d)
8642 && ! DECL_NOT_REALLY_EXTERN (d)
8643 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
8644 goto out;
8645
8646 if (TREE_CODE (d) == VAR_DECL
8647 && TREE_READONLY (d)
8648 && DECL_INITIAL (d) == NULL_TREE
8649 && DECL_INITIAL (code_pattern) != NULL_TREE)
8650 /* We need to set up DECL_INITIAL regardless of pattern_defined if
8651 the variable is a static const initialized in the class body. */;
8652 else if (! pattern_defined
8653 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
8654 && ! at_eof))
8655 {
8656 /* Defer all templates except inline functions used in another
8657 function. We restore the source position here because it's used
8658 by add_pending_template. */
8659 lineno = line;
8660 input_filename = file;
8661
8662 if (at_eof && !pattern_defined
8663 && DECL_EXPLICIT_INSTANTIATION (d))
8664 /* [temp.explicit]
8665
8666 The definition of a non-exported function template, a
8667 non-exported member function template, or a non-exported
8668 member function or static data member of a class template
8669 shall be present in every translation unit in which it is
8670 explicitly instantiated. */
8671 cp_error ("explicit instantiation of `%D' but no definition available",
8672 d);
8673
8674 add_pending_template (d);
8675 goto out;
8676 }
8677
8678 regenerate_decl_from_template (d, td);
8679 DECL_TEMPLATE_INSTANTIATED (d) = 1;
8680
8681 /* We already set the file and line above. Reset them now in case
8682 they changed as a result of calling regenerate_decl_from_template. */
8683 lineno = DECL_SOURCE_LINE (d);
8684 input_filename = DECL_SOURCE_FILE (d);
8685
8686 if (TREE_CODE (d) == VAR_DECL)
8687 {
8688 DECL_IN_AGGR_P (d) = 0;
8689 if (DECL_INTERFACE_KNOWN (d))
8690 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
8691 else
8692 {
8693 DECL_EXTERNAL (d) = 1;
8694 DECL_NOT_REALLY_EXTERN (d) = 1;
8695 }
8696 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
8697 }
8698 else if (TREE_CODE (d) == FUNCTION_DECL)
8699 {
8700 tree t = DECL_SAVED_TREE (code_pattern);
8701
8702 start_function (NULL_TREE, d, NULL_TREE, 1);
8703 store_parm_decls ();
8704
8705 if (t && TREE_CODE (t) == RETURN_INIT)
8706 {
8707 store_return_init
8708 (TREE_OPERAND (t, 0),
8709 tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
8710 t = TREE_CHAIN (t);
8711 }
8712
8713 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
8714 {
8715 current_member_init_list
8716 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
8717 current_base_init_list
8718 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
8719 t = TREE_CHAIN (t);
8720 }
8721
8722 setup_vtbl_ptr ();
8723 /* Always keep the BLOCK node associated with the outermost
8724 pair of curly braces of a function. These are needed
8725 for correct operation of dwarfout.c. */
8726 keep_next_level ();
8727
8728 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
8729 tsubst_expr (t, args, tmpl);
8730
8731 finish_function (lineno, 0, nested);
8732 }
8733
8734 out:
8735 lineno = line;
8736 input_filename = file;
8737
8738 pop_from_top_level ();
8739 pop_tinst_level ();
8740
8741 return d;
8742 }
8743
8744 static tree
8745 tsubst_expr_values (t, argvec)
8746 tree t, argvec;
8747 {
8748 tree first = NULL_TREE;
8749 tree *p = &first;
8750
8751 for (; t; t = TREE_CHAIN (t))
8752 {
8753 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
8754 tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
8755 *p = build_tree_list (pur, val);
8756 p = &TREE_CHAIN (*p);
8757 }
8758 return first;
8759 }
8760
8761 tree last_tree;
8762
8763 void
8764 add_tree (t)
8765 tree t;
8766 {
8767 last_tree = TREE_CHAIN (last_tree) = t;
8768 }
8769
8770
8771 void
8772 begin_tree ()
8773 {
8774 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
8775 last_tree = NULL_TREE;
8776 }
8777
8778
8779 void
8780 end_tree ()
8781 {
8782 my_friendly_assert (saved_trees != NULL_TREE, 0);
8783
8784 last_tree = TREE_VALUE (saved_trees);
8785 saved_trees = TREE_CHAIN (saved_trees);
8786 }
8787
8788 /* D is an undefined function declaration in the presence of templates with
8789 the same name, listed in FNS. If one of them can produce D as an
8790 instantiation, remember this so we can instantiate it at EOF if D has
8791 not been defined by that time. */
8792
8793 void
8794 add_maybe_template (d, fns)
8795 tree d, fns;
8796 {
8797 tree t;
8798
8799 if (DECL_MAYBE_TEMPLATE (d))
8800 return;
8801
8802 t = most_specialized (fns, d, NULL_TREE);
8803 if (! t)
8804 return;
8805 if (t == error_mark_node)
8806 {
8807 cp_error ("ambiguous template instantiation for `%D'", d);
8808 return;
8809 }
8810
8811 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
8812 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
8813 DECL_MAYBE_TEMPLATE (d) = 1;
8814 }
8815
8816 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
8817
8818 static void
8819 set_current_access_from_decl (decl)
8820 tree decl;
8821 {
8822 if (TREE_PRIVATE (decl))
8823 current_access_specifier = access_private_node;
8824 else if (TREE_PROTECTED (decl))
8825 current_access_specifier = access_protected_node;
8826 else
8827 current_access_specifier = access_public_node;
8828 }
8829
8830 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
8831 is the instantiation (which should have been created with
8832 start_enum) and ARGS are the template arguments to use. */
8833
8834 static void
8835 tsubst_enum (tag, newtag, args)
8836 tree tag;
8837 tree newtag;
8838 tree args;
8839 {
8840 tree e;
8841
8842 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
8843 {
8844 tree value;
8845 tree elt;
8846
8847 /* Note that in a template enum, the TREE_VALUE is the
8848 CONST_DECL, not the corresponding INTEGER_CST. */
8849 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
8850 args,
8851 NULL_TREE);
8852
8853 /* Give this enumeration constant the correct access. */
8854 set_current_access_from_decl (TREE_VALUE (e));
8855
8856 /* Actually build the enumerator itself. */
8857 elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
8858
8859 /* We save the enumerators we have built so far in the
8860 TYPE_VALUES so that if the enumeration constants for
8861 subsequent enumerators involve those for previous ones,
8862 tsubst_copy will be able to find them. */
8863 TREE_CHAIN (elt) = TYPE_VALUES (newtag);
8864 TYPE_VALUES (newtag) = elt;
8865 }
8866
8867 finish_enum (newtag);
8868 }
8869
8870 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
8871 is either an instantiation or specialization of a template
8872 function. */
8873
8874 static void
8875 set_mangled_name_for_template_decl (decl)
8876 tree decl;
8877 {
8878 tree saved_namespace;
8879 tree context = NULL_TREE;
8880 tree fn_type;
8881 tree ret_type;
8882 tree parm_types;
8883 tree tparms;
8884 tree targs;
8885 tree tmpl;
8886 int parm_depth;
8887
8888 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
8889 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
8890
8891 /* The names of template functions must be mangled so as to indicate
8892 what template is being specialized with what template arguments.
8893 For example, each of the following three functions must get
8894 different mangled names:
8895
8896 void f(int);
8897 template <> void f<7>(int);
8898 template <> void f<8>(int); */
8899
8900 targs = DECL_TI_ARGS (decl);
8901 if (uses_template_parms (targs))
8902 /* This DECL is for a partial instantiation. There's no need to
8903 mangle the name of such an entity. */
8904 return;
8905
8906 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
8907 tparms = DECL_TEMPLATE_PARMS (tmpl);
8908 parm_depth = TMPL_PARMS_DEPTH (tparms);
8909
8910 /* There should be as many levels of arguments as there are levels
8911 of parameters. */
8912 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
8913
8914 /* We now compute the PARMS and RET_TYPE to give to
8915 build_decl_overload_real. The PARMS and RET_TYPE are the
8916 parameter and return types of the template, after all but the
8917 innermost template arguments have been substituted, not the
8918 parameter and return types of the function DECL. For example,
8919 given:
8920
8921 template <class T> T f(T);
8922
8923 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
8924 A more subtle example is:
8925
8926 template <class T> struct S { template <class U> void f(T, U); }
8927
8928 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
8929 {int, U}. Thus, the args that we want to subsitute into the
8930 return and parameter type for the function are those in TARGS,
8931 with the innermost level omitted. */
8932 fn_type = TREE_TYPE (tmpl);
8933 if (DECL_STATIC_FUNCTION_P (decl))
8934 context = DECL_CLASS_CONTEXT (decl);
8935
8936 if (parm_depth == 1)
8937 /* No substitution is necessary. */
8938 ;
8939 else
8940 {
8941 int i;
8942 tree partial_args;
8943
8944 /* Replace the innermost level of the TARGS with NULL_TREEs to
8945 let tsubst know not to subsitute for those parameters. */
8946 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
8947 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
8948 SET_TMPL_ARGS_LEVEL (partial_args, i,
8949 TMPL_ARGS_LEVEL (targs, i));
8950 SET_TMPL_ARGS_LEVEL (partial_args,
8951 TMPL_ARGS_DEPTH (targs),
8952 make_temp_vec (DECL_NTPARMS (tmpl)));
8953
8954 /* Now, do the (partial) substitution to figure out the
8955 appropriate function type. */
8956 fn_type = tsubst (fn_type, partial_args, NULL_TREE);
8957 if (DECL_STATIC_FUNCTION_P (decl))
8958 context = tsubst (context, partial_args, NULL_TREE);
8959
8960 /* Substitute into the template parameters to obtain the real
8961 innermost set of parameters. This step is important if the
8962 innermost set of template parameters contains value
8963 parameters whose types depend on outer template parameters. */
8964 TREE_VEC_LENGTH (partial_args)--;
8965 tparms = tsubst_template_parms (tparms, partial_args);
8966 }
8967
8968 /* Now, get the innermost parameters and arguments, and figure out
8969 the parameter and return types. */
8970 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
8971 targs = innermost_args (targs);
8972 ret_type = TREE_TYPE (fn_type);
8973 parm_types = TYPE_ARG_TYPES (fn_type);
8974
8975 /* For a static member function, we generate a fake `this' pointer,
8976 for the purposes of mangling. This indicates of which class the
8977 function is a member. Because of:
8978
8979 [class.static]
8980
8981 There shall not be a static and a nonstatic member function
8982 with the same name and the same parameter types
8983
8984 we don't have to worry that this will result in a clash with a
8985 non-static member function. */
8986 if (DECL_STATIC_FUNCTION_P (decl))
8987 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
8988
8989 /* There should be the same number of template parameters as
8990 template arguments. */
8991 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
8992 0);
8993
8994 /* If the template is in a namespace, we need to put that into the
8995 mangled name. Unfortunately, build_decl_overload_real does not
8996 get the decl to mangle, so it relies on the current
8997 namespace. Therefore, we set that here temporarily. */
8998 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
8999 saved_namespace = current_namespace;
9000 current_namespace = CP_DECL_CONTEXT (decl);
9001
9002 /* Actually set the DCL_ASSEMBLER_NAME. */
9003 DECL_ASSEMBLER_NAME (decl)
9004 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
9005 tparms, targs,
9006 DECL_FUNCTION_MEMBER_P (decl)
9007 + DECL_CONSTRUCTOR_P (decl));
9008
9009 /* Restore the previously active namespace. */
9010 current_namespace = saved_namespace;
9011 }