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