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