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