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