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