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