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