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