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