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