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