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