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