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