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