]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
re PR c++/26122 (Pure specifiers for templates causing trouble)
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 /* Known bugs or deficiencies include:
25
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
52
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work.
56 The TREE_PURPOSE of each entry is either a DECL (for a function or
57 static data member), or a TYPE (for a class) indicating what we are
58 hoping to instantiate. The TREE_VALUE is not used. */
59 static GTY(()) tree pending_templates;
60 static GTY(()) tree last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static GTY(()) varray_type inline_parm_levels;
67 static size_t inline_parm_levels_used;
68
69 static GTY(()) tree current_tinst_level;
70
71 static GTY(()) tree saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static htab_t local_specializations;
82
83 #define UNIFY_ALLOW_NONE 0
84 #define UNIFY_ALLOW_MORE_CV_QUAL 1
85 #define UNIFY_ALLOW_LESS_CV_QUAL 2
86 #define UNIFY_ALLOW_DERIVED 4
87 #define UNIFY_ALLOW_INTEGER 8
88 #define UNIFY_ALLOW_OUTER_LEVEL 16
89 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
90 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
91
92 static void push_access_scope (tree);
93 static void pop_access_scope (tree);
94 static int resolve_overloaded_unification (tree, tree, tree, tree,
95 unification_kind_t, int);
96 static int try_one_overload (tree, tree, tree, tree, tree,
97 unification_kind_t, int, bool);
98 static int unify (tree, tree, tree, tree, int);
99 static void add_pending_template (tree);
100 static int push_tinst_level (tree);
101 static void pop_tinst_level (void);
102 static void reopen_tinst_level (tree);
103 static tree classtype_mangled_name (tree);
104 static char* mangle_class_name_for_template (const char *, tree, tree);
105 static tree tsubst_initializer_list (tree, tree);
106 static tree get_class_bindings (tree, tree, tree);
107 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
108 bool, bool);
109 static void tsubst_enum (tree, tree, tree);
110 static tree add_to_template_args (tree, tree);
111 static tree add_outermost_template_args (tree, tree);
112 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
113 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
114 static int type_unification_real (tree, tree, tree, tree,
115 int, unification_kind_t, int);
116 static void note_template_header (int);
117 static tree convert_nontype_argument_function (tree, tree);
118 static tree convert_nontype_argument (tree, tree);
119 static tree convert_template_argument (tree, tree, tree,
120 tsubst_flags_t, int, tree);
121 static int for_each_template_parm (tree, tree_fn_t, void*,
122 struct pointer_set_t*);
123 static tree build_template_parm_index (int, int, int, tree, tree);
124 static int inline_needs_template_parms (tree);
125 static void push_inline_template_parms_recursive (tree, int);
126 static tree retrieve_local_specialization (tree);
127 static void register_local_specialization (tree, tree);
128 static tree reduce_template_parm_level (tree, tree, int);
129 static int mark_template_parm (tree, void *);
130 static int template_parm_this_level_p (tree, void *);
131 static tree tsubst_friend_function (tree, tree);
132 static tree tsubst_friend_class (tree, tree);
133 static int can_complete_type_without_circularity (tree);
134 static tree get_bindings (tree, tree, tree, bool);
135 static int template_decl_level (tree);
136 static int check_cv_quals_for_unify (int, tree, tree);
137 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
139 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140 static void regenerate_decl_from_template (tree, tree);
141 static tree most_specialized_class (tree, tree);
142 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
145 static void check_specialization_scope (void);
146 static tree process_partial_specialization (tree);
147 static void set_current_access_from_decl (tree);
148 static void check_default_tmpl_args (tree, tree, int, int);
149 static tree get_template_base (tree, tree, tree, tree);
150 static tree try_class_unification (tree, tree, tree, tree);
151 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152 tree, tree);
153 static tree determine_specialization (tree, tree, tree *, int, int);
154 static int template_args_equal (tree, tree);
155 static void tsubst_default_arguments (tree);
156 static tree for_each_template_parm_r (tree *, int *, void *);
157 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
158 static void copy_default_args_to_explicit_spec (tree);
159 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
160 static int eq_local_specializations (const void *, const void *);
161 static bool dependent_type_p_r (tree);
162 static tree tsubst (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
165
166 /* Make the current scope suitable for access checking when we are
167 processing T. T can be FUNCTION_DECL for instantiated function
168 template, or VAR_DECL for static member variable (need by
169 instantiate_decl). */
170
171 static void
172 push_access_scope (tree t)
173 {
174 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
175 || TREE_CODE (t) == VAR_DECL);
176
177 if (DECL_FRIEND_CONTEXT (t))
178 push_nested_class (DECL_FRIEND_CONTEXT (t));
179 else if (DECL_CLASS_SCOPE_P (t))
180 push_nested_class (DECL_CONTEXT (t));
181 else
182 push_to_top_level ();
183
184 if (TREE_CODE (t) == FUNCTION_DECL)
185 {
186 saved_access_scope = tree_cons
187 (NULL_TREE, current_function_decl, saved_access_scope);
188 current_function_decl = t;
189 }
190 }
191
192 /* Restore the scope set up by push_access_scope. T is the node we
193 are processing. */
194
195 static void
196 pop_access_scope (tree t)
197 {
198 if (TREE_CODE (t) == FUNCTION_DECL)
199 {
200 current_function_decl = TREE_VALUE (saved_access_scope);
201 saved_access_scope = TREE_CHAIN (saved_access_scope);
202 }
203
204 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
205 pop_nested_class ();
206 else
207 pop_from_top_level ();
208 }
209
210 /* Do any processing required when DECL (a member template
211 declaration) is finished. Returns the TEMPLATE_DECL corresponding
212 to DECL, unless it is a specialization, in which case the DECL
213 itself is returned. */
214
215 tree
216 finish_member_template_decl (tree decl)
217 {
218 if (decl == error_mark_node)
219 return error_mark_node;
220
221 gcc_assert (DECL_P (decl));
222
223 if (TREE_CODE (decl) == TYPE_DECL)
224 {
225 tree type;
226
227 type = TREE_TYPE (decl);
228 if (IS_AGGR_TYPE (type)
229 && CLASSTYPE_TEMPLATE_INFO (type)
230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
231 {
232 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
233 check_member_template (tmpl);
234 return tmpl;
235 }
236 return NULL_TREE;
237 }
238 else if (TREE_CODE (decl) == FIELD_DECL)
239 error ("data member %qD cannot be a member template", decl);
240 else if (DECL_TEMPLATE_INFO (decl))
241 {
242 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
243 {
244 check_member_template (DECL_TI_TEMPLATE (decl));
245 return DECL_TI_TEMPLATE (decl);
246 }
247 else
248 return decl;
249 }
250 else
251 error ("invalid member template declaration %qD", decl);
252
253 return error_mark_node;
254 }
255
256 /* Returns the template nesting level of the indicated class TYPE.
257
258 For example, in:
259 template <class T>
260 struct A
261 {
262 template <class U>
263 struct B {};
264 };
265
266 A<T>::B<U> has depth two, while A<T> has depth one.
267 Both A<T>::B<int> and A<int>::B<U> have depth one, if
268 they are instantiations, not specializations.
269
270 This function is guaranteed to return 0 if passed NULL_TREE so
271 that, for example, `template_class_depth (current_class_type)' is
272 always safe. */
273
274 int
275 template_class_depth (tree type)
276 {
277 int depth;
278
279 for (depth = 0;
280 type && TREE_CODE (type) != NAMESPACE_DECL;
281 type = (TREE_CODE (type) == FUNCTION_DECL)
282 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
283 {
284 if (TREE_CODE (type) != FUNCTION_DECL)
285 {
286 if (CLASSTYPE_TEMPLATE_INFO (type)
287 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
288 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
289 ++depth;
290 }
291 else
292 {
293 if (DECL_TEMPLATE_INFO (type)
294 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
295 && uses_template_parms (DECL_TI_ARGS (type)))
296 ++depth;
297 }
298 }
299
300 return depth;
301 }
302
303 /* Returns 1 if processing DECL as part of do_pending_inlines
304 needs us to push template parms. */
305
306 static int
307 inline_needs_template_parms (tree decl)
308 {
309 if (! DECL_TEMPLATE_INFO (decl))
310 return 0;
311
312 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
313 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
314 }
315
316 /* Subroutine of maybe_begin_member_template_processing.
317 Push the template parms in PARMS, starting from LEVELS steps into the
318 chain, and ending at the beginning, since template parms are listed
319 innermost first. */
320
321 static void
322 push_inline_template_parms_recursive (tree parmlist, int levels)
323 {
324 tree parms = TREE_VALUE (parmlist);
325 int i;
326
327 if (levels > 1)
328 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
329
330 ++processing_template_decl;
331 current_template_parms
332 = tree_cons (size_int (processing_template_decl),
333 parms, current_template_parms);
334 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
335
336 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
337 NULL);
338 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
339 {
340 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
341 gcc_assert (DECL_P (parm));
342
343 switch (TREE_CODE (parm))
344 {
345 case TYPE_DECL:
346 case TEMPLATE_DECL:
347 pushdecl (parm);
348 break;
349
350 case PARM_DECL:
351 {
352 /* Make a CONST_DECL as is done in process_template_parm.
353 It is ugly that we recreate this here; the original
354 version built in process_template_parm is no longer
355 available. */
356 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
357 TREE_TYPE (parm));
358 DECL_ARTIFICIAL (decl) = 1;
359 TREE_CONSTANT (decl) = 1;
360 TREE_INVARIANT (decl) = 1;
361 TREE_READONLY (decl) = 1;
362 DECL_INITIAL (decl) = DECL_INITIAL (parm);
363 SET_DECL_TEMPLATE_PARM_P (decl);
364 pushdecl (decl);
365 }
366 break;
367
368 default:
369 gcc_unreachable ();
370 }
371 }
372 }
373
374 /* Restore the template parameter context for a member template or
375 a friend template defined in a class definition. */
376
377 void
378 maybe_begin_member_template_processing (tree decl)
379 {
380 tree parms;
381 int levels = 0;
382
383 if (inline_needs_template_parms (decl))
384 {
385 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
386 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
387
388 if (DECL_TEMPLATE_SPECIALIZATION (decl))
389 {
390 --levels;
391 parms = TREE_CHAIN (parms);
392 }
393
394 push_inline_template_parms_recursive (parms, levels);
395 }
396
397 /* Remember how many levels of template parameters we pushed so that
398 we can pop them later. */
399 if (!inline_parm_levels)
400 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
401 if (inline_parm_levels_used == inline_parm_levels->num_elements)
402 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
403 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
404 ++inline_parm_levels_used;
405 }
406
407 /* Undo the effects of maybe_begin_member_template_processing. */
408
409 void
410 maybe_end_member_template_processing (void)
411 {
412 int i;
413
414 if (!inline_parm_levels_used)
415 return;
416
417 --inline_parm_levels_used;
418 for (i = 0;
419 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
420 ++i)
421 {
422 --processing_template_decl;
423 current_template_parms = TREE_CHAIN (current_template_parms);
424 poplevel (0, 0, 0);
425 }
426 }
427
428 /* Return a new template argument vector which contains all of ARGS,
429 but has as its innermost set of arguments the EXTRA_ARGS. */
430
431 static tree
432 add_to_template_args (tree args, tree extra_args)
433 {
434 tree new_args;
435 int extra_depth;
436 int i;
437 int j;
438
439 extra_depth = TMPL_ARGS_DEPTH (extra_args);
440 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
441
442 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
444
445 for (j = 1; j <= extra_depth; ++j, ++i)
446 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
447
448 return new_args;
449 }
450
451 /* Like add_to_template_args, but only the outermost ARGS are added to
452 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
453 (EXTRA_ARGS) levels are added. This function is used to combine
454 the template arguments from a partial instantiation with the
455 template arguments used to attain the full instantiation from the
456 partial instantiation. */
457
458 static tree
459 add_outermost_template_args (tree args, tree extra_args)
460 {
461 tree new_args;
462
463 /* If there are more levels of EXTRA_ARGS than there are ARGS,
464 something very fishy is going on. */
465 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
466
467 /* If *all* the new arguments will be the EXTRA_ARGS, just return
468 them. */
469 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
470 return extra_args;
471
472 /* For the moment, we make ARGS look like it contains fewer levels. */
473 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
474
475 new_args = add_to_template_args (args, extra_args);
476
477 /* Now, we restore ARGS to its full dimensions. */
478 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
479
480 return new_args;
481 }
482
483 /* Return the N levels of innermost template arguments from the ARGS. */
484
485 tree
486 get_innermost_template_args (tree args, int n)
487 {
488 tree new_args;
489 int extra_levels;
490 int i;
491
492 gcc_assert (n >= 0);
493
494 /* If N is 1, just return the innermost set of template arguments. */
495 if (n == 1)
496 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
497
498 /* If we're not removing anything, just return the arguments we were
499 given. */
500 extra_levels = TMPL_ARGS_DEPTH (args) - n;
501 gcc_assert (extra_levels >= 0);
502 if (extra_levels == 0)
503 return args;
504
505 /* Make a new set of arguments, not containing the outer arguments. */
506 new_args = make_tree_vec (n);
507 for (i = 1; i <= n; ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i,
509 TMPL_ARGS_LEVEL (args, i + extra_levels));
510
511 return new_args;
512 }
513
514 /* We've got a template header coming up; push to a new level for storing
515 the parms. */
516
517 void
518 begin_template_parm_list (void)
519 {
520 /* We use a non-tag-transparent scope here, which causes pushtag to
521 put tags in this scope, rather than in the enclosing class or
522 namespace scope. This is the right thing, since we want
523 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
524 global template class, push_template_decl handles putting the
525 TEMPLATE_DECL into top-level scope. For a nested template class,
526 e.g.:
527
528 template <class T> struct S1 {
529 template <class T> struct S2 {};
530 };
531
532 pushtag contains special code to call pushdecl_with_scope on the
533 TEMPLATE_DECL for S2. */
534 begin_scope (sk_template_parms, NULL);
535 ++processing_template_decl;
536 ++processing_template_parmlist;
537 note_template_header (0);
538 }
539
540 /* This routine is called when a specialization is declared. If it is
541 invalid to declare a specialization here, an error is reported. */
542
543 static void
544 check_specialization_scope (void)
545 {
546 tree scope = current_scope ();
547
548 /* [temp.expl.spec]
549
550 An explicit specialization shall be declared in the namespace of
551 which the template is a member, or, for member templates, in the
552 namespace of which the enclosing class or enclosing class
553 template is a member. An explicit specialization of a member
554 function, member class or static data member of a class template
555 shall be declared in the namespace of which the class template
556 is a member. */
557 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
558 error ("explicit specialization in non-namespace scope %qD", scope);
559
560 /* [temp.expl.spec]
561
562 In an explicit specialization declaration for a member of a class
563 template or a member template that appears in namespace scope,
564 the member template and some of its enclosing class templates may
565 remain unspecialized, except that the declaration shall not
566 explicitly specialize a class member template if its enclosing
567 class templates are not explicitly specialized as well. */
568 if (current_template_parms)
569 error ("enclosing class templates are not explicitly specialized");
570 }
571
572 /* We've just seen template <>. */
573
574 void
575 begin_specialization (void)
576 {
577 begin_scope (sk_template_spec, NULL);
578 note_template_header (1);
579 check_specialization_scope ();
580 }
581
582 /* Called at then end of processing a declaration preceded by
583 template<>. */
584
585 void
586 end_specialization (void)
587 {
588 finish_scope ();
589 reset_specialization ();
590 }
591
592 /* Any template <>'s that we have seen thus far are not referring to a
593 function specialization. */
594
595 void
596 reset_specialization (void)
597 {
598 processing_specialization = 0;
599 template_header_count = 0;
600 }
601
602 /* We've just seen a template header. If SPECIALIZATION is nonzero,
603 it was of the form template <>. */
604
605 static void
606 note_template_header (int specialization)
607 {
608 processing_specialization = specialization;
609 template_header_count++;
610 }
611
612 /* We're beginning an explicit instantiation. */
613
614 void
615 begin_explicit_instantiation (void)
616 {
617 gcc_assert (!processing_explicit_instantiation);
618 processing_explicit_instantiation = true;
619 }
620
621
622 void
623 end_explicit_instantiation (void)
624 {
625 gcc_assert (processing_explicit_instantiation);
626 processing_explicit_instantiation = false;
627 }
628
629 /* An explicit specialization or partial specialization TMPL is being
630 declared. Check that the namespace in which the specialization is
631 occurring is permissible. Returns false iff it is invalid to
632 specialize TMPL in the current namespace. */
633
634 static bool
635 check_specialization_namespace (tree tmpl)
636 {
637 tree tpl_ns = decl_namespace_context (tmpl);
638
639 /* [tmpl.expl.spec]
640
641 An explicit specialization shall be declared in the namespace of
642 which the template is a member, or, for member templates, in the
643 namespace of which the enclosing class or enclosing class
644 template is a member. An explicit specialization of a member
645 function, member class or static data member of a class template
646 shall be declared in the namespace of which the class template is
647 a member. */
648 if (is_associated_namespace (current_namespace, tpl_ns))
649 /* Same or super-using namespace. */
650 return true;
651 else
652 {
653 pedwarn ("specialization of %qD in different namespace", tmpl);
654 pedwarn (" from definition of %q+#D", tmpl);
655 return false;
656 }
657 }
658
659 /* SPEC is an explicit instantiation. Check that it is valid to
660 perform this explicit instantiation in the current namespace. */
661
662 static void
663 check_explicit_instantiation_namespace (tree spec)
664 {
665 tree ns;
666
667 /* DR 275: An explicit instantiation shall appear in an enclosing
668 namespace of its template. */
669 ns = decl_namespace_context (spec);
670 if (!is_ancestor (current_namespace, ns))
671 pedwarn ("explicit instantiation of %qD in namespace %qD "
672 "(which does not enclose namespace %qD)",
673 spec, current_namespace, ns);
674 }
675
676 /* The TYPE is being declared. If it is a template type, that means it
677 is a partial specialization. Do appropriate error-checking. */
678
679 void
680 maybe_process_partial_specialization (tree type)
681 {
682 tree context;
683
684 if (type == error_mark_node)
685 return;
686
687 context = TYPE_CONTEXT (type);
688
689 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
690 {
691 /* This is for ordinary explicit specialization and partial
692 specialization of a template class such as:
693
694 template <> class C<int>;
695
696 or:
697
698 template <class T> class C<T*>;
699
700 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
701
702 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
703 && !COMPLETE_TYPE_P (type))
704 {
705 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
706 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
707 if (processing_template_decl)
708 push_template_decl (TYPE_MAIN_DECL (type));
709 }
710 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
711 error ("specialization of %qT after instantiation", type);
712 }
713 else if (CLASS_TYPE_P (type)
714 && !CLASSTYPE_USE_TEMPLATE (type)
715 && CLASSTYPE_TEMPLATE_INFO (type)
716 && context && CLASS_TYPE_P (context)
717 && CLASSTYPE_TEMPLATE_INFO (context))
718 {
719 /* This is for an explicit specialization of member class
720 template according to [temp.expl.spec/18]:
721
722 template <> template <class U> class C<int>::D;
723
724 The context `C<int>' must be an implicit instantiation.
725 Otherwise this is just a member class template declared
726 earlier like:
727
728 template <> class C<int> { template <class U> class D; };
729 template <> template <class U> class C<int>::D;
730
731 In the first case, `C<int>::D' is a specialization of `C<T>::D'
732 while in the second case, `C<int>::D' is a primary template
733 and `C<T>::D' may not exist. */
734
735 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
736 && !COMPLETE_TYPE_P (type))
737 {
738 tree t;
739
740 if (current_namespace
741 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
742 {
743 pedwarn ("specializing %q#T in different namespace", type);
744 pedwarn (" from definition of %q+#D",
745 CLASSTYPE_TI_TEMPLATE (type));
746 }
747
748 /* Check for invalid specialization after instantiation:
749
750 template <> template <> class C<int>::D<int>;
751 template <> template <class U> class C<int>::D; */
752
753 for (t = DECL_TEMPLATE_INSTANTIATIONS
754 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
755 t; t = TREE_CHAIN (t))
756 if (TREE_VALUE (t) != type
757 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
758 error ("specialization %qT after instantiation %qT",
759 type, TREE_VALUE (t));
760
761 /* Mark TYPE as a specialization. And as a result, we only
762 have one level of template argument for the innermost
763 class template. */
764 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
765 CLASSTYPE_TI_ARGS (type)
766 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
767 }
768 }
769 else if (processing_specialization)
770 error ("explicit specialization of non-template %qT", type);
771 }
772
773 /* Returns nonzero if we can optimize the retrieval of specializations
774 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
775 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
776
777 static inline bool
778 optimize_specialization_lookup_p (tree tmpl)
779 {
780 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
781 && DECL_CLASS_SCOPE_P (tmpl)
782 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
783 parameter. */
784 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
785 /* The optimized lookup depends on the fact that the
786 template arguments for the member function template apply
787 purely to the containing class, which is not true if the
788 containing class is an explicit or partial
789 specialization. */
790 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
791 && !DECL_MEMBER_TEMPLATE_P (tmpl)
792 && !DECL_CONV_FN_P (tmpl)
793 /* It is possible to have a template that is not a member
794 template and is not a member of a template class:
795
796 template <typename T>
797 struct S { friend A::f(); };
798
799 Here, the friend function is a template, but the context does
800 not have template information. The optimized lookup relies
801 on having ARGS be the template arguments for both the class
802 and the function template. */
803 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
804 }
805
806 /* Retrieve the specialization (in the sense of [temp.spec] - a
807 specialization is either an instantiation or an explicit
808 specialization) of TMPL for the given template ARGS. If there is
809 no such specialization, return NULL_TREE. The ARGS are a vector of
810 arguments, or a vector of vectors of arguments, in the case of
811 templates with more than one level of parameters.
812
813 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
814 then we search for a partial specialization matching ARGS. This
815 parameter is ignored if TMPL is not a class template. */
816
817 static tree
818 retrieve_specialization (tree tmpl, tree args,
819 bool class_specializations_p)
820 {
821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
822
823 /* There should be as many levels of arguments as there are
824 levels of parameters. */
825 gcc_assert (TMPL_ARGS_DEPTH (args)
826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
827
828 if (optimize_specialization_lookup_p (tmpl))
829 {
830 tree class_template;
831 tree class_specialization;
832 VEC(tree,gc) *methods;
833 tree fns;
834 int idx;
835
836 /* The template arguments actually apply to the containing
837 class. Find the class specialization with those
838 arguments. */
839 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
840 class_specialization
841 = retrieve_specialization (class_template, args,
842 /*class_specializations_p=*/false);
843 if (!class_specialization)
844 return NULL_TREE;
845 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
846 for the specialization. */
847 idx = class_method_index_for_fn (class_specialization, tmpl);
848 if (idx == -1)
849 return NULL_TREE;
850 /* Iterate through the methods with the indicated name, looking
851 for the one that has an instance of TMPL. */
852 methods = CLASSTYPE_METHOD_VEC (class_specialization);
853 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
854 {
855 tree fn = OVL_CURRENT (fns);
856 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
857 return fn;
858 }
859 return NULL_TREE;
860 }
861 else
862 {
863 tree *sp;
864 tree *head;
865
866 /* Class templates store their instantiations on the
867 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
868 DECL_TEMPLATE_SPECIALIZATIONS list. */
869 if (!class_specializations_p
870 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
871 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
872 else
873 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
874 head = sp;
875 /* Iterate through the list until we find a matching template. */
876 while (*sp != NULL_TREE)
877 {
878 tree spec = *sp;
879
880 if (comp_template_args (TREE_PURPOSE (spec), args))
881 {
882 /* Use the move-to-front heuristic to speed up future
883 searches. */
884 if (spec != *head)
885 {
886 *sp = TREE_CHAIN (*sp);
887 TREE_CHAIN (spec) = *head;
888 *head = spec;
889 }
890 return TREE_VALUE (spec);
891 }
892 sp = &TREE_CHAIN (spec);
893 }
894 }
895
896 return NULL_TREE;
897 }
898
899 /* Like retrieve_specialization, but for local declarations. */
900
901 static tree
902 retrieve_local_specialization (tree tmpl)
903 {
904 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
905 htab_hash_pointer (tmpl));
906 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
907 }
908
909 /* Returns nonzero iff DECL is a specialization of TMPL. */
910
911 int
912 is_specialization_of (tree decl, tree tmpl)
913 {
914 tree t;
915
916 if (TREE_CODE (decl) == FUNCTION_DECL)
917 {
918 for (t = decl;
919 t != NULL_TREE;
920 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
921 if (t == tmpl)
922 return 1;
923 }
924 else
925 {
926 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
927
928 for (t = TREE_TYPE (decl);
929 t != NULL_TREE;
930 t = CLASSTYPE_USE_TEMPLATE (t)
931 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
932 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
933 return 1;
934 }
935
936 return 0;
937 }
938
939 /* Returns nonzero iff DECL is a specialization of friend declaration
940 FRIEND according to [temp.friend]. */
941
942 bool
943 is_specialization_of_friend (tree decl, tree friend)
944 {
945 bool need_template = true;
946 int template_depth;
947
948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
949 || TREE_CODE (decl) == TYPE_DECL);
950
951 /* For [temp.friend/6] when FRIEND is an ordinary member function
952 of a template class, we want to check if DECL is a specialization
953 if this. */
954 if (TREE_CODE (friend) == FUNCTION_DECL
955 && DECL_TEMPLATE_INFO (friend)
956 && !DECL_USE_TEMPLATE (friend))
957 {
958 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
959 friend = DECL_TI_TEMPLATE (friend);
960 need_template = false;
961 }
962 else if (TREE_CODE (friend) == TEMPLATE_DECL
963 && !PRIMARY_TEMPLATE_P (friend))
964 need_template = false;
965
966 /* There is nothing to do if this is not a template friend. */
967 if (TREE_CODE (friend) != TEMPLATE_DECL)
968 return false;
969
970 if (is_specialization_of (decl, friend))
971 return true;
972
973 /* [temp.friend/6]
974 A member of a class template may be declared to be a friend of a
975 non-template class. In this case, the corresponding member of
976 every specialization of the class template is a friend of the
977 class granting friendship.
978
979 For example, given a template friend declaration
980
981 template <class T> friend void A<T>::f();
982
983 the member function below is considered a friend
984
985 template <> struct A<int> {
986 void f();
987 };
988
989 For this type of template friend, TEMPLATE_DEPTH below will be
990 nonzero. To determine if DECL is a friend of FRIEND, we first
991 check if the enclosing class is a specialization of another. */
992
993 template_depth = template_class_depth (DECL_CONTEXT (friend));
994 if (template_depth
995 && DECL_CLASS_SCOPE_P (decl)
996 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
997 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
998 {
999 /* Next, we check the members themselves. In order to handle
1000 a few tricky cases, such as when FRIEND's are
1001
1002 template <class T> friend void A<T>::g(T t);
1003 template <class T> template <T t> friend void A<T>::h();
1004
1005 and DECL's are
1006
1007 void A<int>::g(int);
1008 template <int> void A<int>::h();
1009
1010 we need to figure out ARGS, the template arguments from
1011 the context of DECL. This is required for template substitution
1012 of `T' in the function parameter of `g' and template parameter
1013 of `h' in the above examples. Here ARGS corresponds to `int'. */
1014
1015 tree context = DECL_CONTEXT (decl);
1016 tree args = NULL_TREE;
1017 int current_depth = 0;
1018
1019 while (current_depth < template_depth)
1020 {
1021 if (CLASSTYPE_TEMPLATE_INFO (context))
1022 {
1023 if (current_depth == 0)
1024 args = TYPE_TI_ARGS (context);
1025 else
1026 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1027 current_depth++;
1028 }
1029 context = TYPE_CONTEXT (context);
1030 }
1031
1032 if (TREE_CODE (decl) == FUNCTION_DECL)
1033 {
1034 bool is_template;
1035 tree friend_type;
1036 tree decl_type;
1037 tree friend_args_type;
1038 tree decl_args_type;
1039
1040 /* Make sure that both DECL and FRIEND are templates or
1041 non-templates. */
1042 is_template = DECL_TEMPLATE_INFO (decl)
1043 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1044 if (need_template ^ is_template)
1045 return false;
1046 else if (is_template)
1047 {
1048 /* If both are templates, check template parameter list. */
1049 tree friend_parms
1050 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1051 args, tf_none);
1052 if (!comp_template_parms
1053 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1054 friend_parms))
1055 return false;
1056
1057 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1058 }
1059 else
1060 decl_type = TREE_TYPE (decl);
1061
1062 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1063 tf_none, NULL_TREE);
1064 if (friend_type == error_mark_node)
1065 return false;
1066
1067 /* Check if return types match. */
1068 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1069 return false;
1070
1071 /* Check if function parameter types match, ignoring the
1072 `this' parameter. */
1073 friend_args_type = TYPE_ARG_TYPES (friend_type);
1074 decl_args_type = TYPE_ARG_TYPES (decl_type);
1075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1076 friend_args_type = TREE_CHAIN (friend_args_type);
1077 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1078 decl_args_type = TREE_CHAIN (decl_args_type);
1079
1080 return compparms (decl_args_type, friend_args_type);
1081 }
1082 else
1083 {
1084 /* DECL is a TYPE_DECL */
1085 bool is_template;
1086 tree decl_type = TREE_TYPE (decl);
1087
1088 /* Make sure that both DECL and FRIEND are templates or
1089 non-templates. */
1090 is_template
1091 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1093
1094 if (need_template ^ is_template)
1095 return false;
1096 else if (is_template)
1097 {
1098 tree friend_parms;
1099 /* If both are templates, check the name of the two
1100 TEMPLATE_DECL's first because is_friend didn't. */
1101 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1102 != DECL_NAME (friend))
1103 return false;
1104
1105 /* Now check template parameter list. */
1106 friend_parms
1107 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1108 args, tf_none);
1109 return comp_template_parms
1110 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1111 friend_parms);
1112 }
1113 else
1114 return (DECL_NAME (decl)
1115 == DECL_NAME (friend));
1116 }
1117 }
1118 return false;
1119 }
1120
1121 /* Register the specialization SPEC as a specialization of TMPL with
1122 the indicated ARGS. IS_FRIEND indicates whether the specialization
1123 is actually just a friend declaration. Returns SPEC, or an
1124 equivalent prior declaration, if available. */
1125
1126 static tree
1127 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1128 {
1129 tree fn;
1130
1131 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1132
1133 if (TREE_CODE (spec) == FUNCTION_DECL
1134 && uses_template_parms (DECL_TI_ARGS (spec)))
1135 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1136 register it; we want the corresponding TEMPLATE_DECL instead.
1137 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1138 the more obvious `uses_template_parms (spec)' to avoid problems
1139 with default function arguments. In particular, given
1140 something like this:
1141
1142 template <class T> void f(T t1, T t = T())
1143
1144 the default argument expression is not substituted for in an
1145 instantiation unless and until it is actually needed. */
1146 return spec;
1147
1148 fn = retrieve_specialization (tmpl, args,
1149 /*class_specializations_p=*/false);
1150 /* We can sometimes try to re-register a specialization that we've
1151 already got. In particular, regenerate_decl_from_template calls
1152 duplicate_decls which will update the specialization list. But,
1153 we'll still get called again here anyhow. It's more convenient
1154 to simply allow this than to try to prevent it. */
1155 if (fn == spec)
1156 return spec;
1157 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1158 {
1159 if (DECL_TEMPLATE_INSTANTIATION (fn))
1160 {
1161 if (TREE_USED (fn)
1162 || DECL_EXPLICIT_INSTANTIATION (fn))
1163 {
1164 error ("specialization of %qD after instantiation",
1165 fn);
1166 return spec;
1167 }
1168 else
1169 {
1170 tree clone;
1171 /* This situation should occur only if the first
1172 specialization is an implicit instantiation, the
1173 second is an explicit specialization, and the
1174 implicit instantiation has not yet been used. That
1175 situation can occur if we have implicitly
1176 instantiated a member function and then specialized
1177 it later.
1178
1179 We can also wind up here if a friend declaration that
1180 looked like an instantiation turns out to be a
1181 specialization:
1182
1183 template <class T> void foo(T);
1184 class S { friend void foo<>(int) };
1185 template <> void foo(int);
1186
1187 We transform the existing DECL in place so that any
1188 pointers to it become pointers to the updated
1189 declaration.
1190
1191 If there was a definition for the template, but not
1192 for the specialization, we want this to look as if
1193 there were no definition, and vice versa. */
1194 DECL_INITIAL (fn) = NULL_TREE;
1195 duplicate_decls (spec, fn, is_friend);
1196 /* The call to duplicate_decls will have applied
1197 [temp.expl.spec]:
1198
1199 An explicit specialization of a function template
1200 is inline only if it is explicitly declared to be,
1201 and independently of whether its function template
1202 is.
1203
1204 to the primary function; now copy the inline bits to
1205 the various clones. */
1206 FOR_EACH_CLONE (clone, fn)
1207 {
1208 DECL_DECLARED_INLINE_P (clone)
1209 = DECL_DECLARED_INLINE_P (fn);
1210 DECL_INLINE (clone)
1211 = DECL_INLINE (fn);
1212 }
1213 check_specialization_namespace (fn);
1214
1215 return fn;
1216 }
1217 }
1218 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1219 {
1220 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1221 /* Dup decl failed, but this is a new definition. Set the
1222 line number so any errors match this new
1223 definition. */
1224 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1225
1226 return fn;
1227 }
1228 }
1229
1230 /* A specialization must be declared in the same namespace as the
1231 template it is specializing. */
1232 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1233 && !check_specialization_namespace (tmpl))
1234 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1235
1236 if (!optimize_specialization_lookup_p (tmpl))
1237 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1238 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1239
1240 return spec;
1241 }
1242
1243 /* Unregister the specialization SPEC as a specialization of TMPL.
1244 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1245 if the SPEC was listed as a specialization of TMPL. */
1246
1247 bool
1248 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1249 {
1250 tree* s;
1251
1252 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1253 *s != NULL_TREE;
1254 s = &TREE_CHAIN (*s))
1255 if (TREE_VALUE (*s) == spec)
1256 {
1257 if (!new_spec)
1258 *s = TREE_CHAIN (*s);
1259 else
1260 TREE_VALUE (*s) = new_spec;
1261 return 1;
1262 }
1263
1264 return 0;
1265 }
1266
1267 /* Compare an entry in the local specializations hash table P1 (which
1268 is really a pointer to a TREE_LIST) with P2 (which is really a
1269 DECL). */
1270
1271 static int
1272 eq_local_specializations (const void *p1, const void *p2)
1273 {
1274 return TREE_VALUE ((tree) p1) == (tree) p2;
1275 }
1276
1277 /* Hash P1, an entry in the local specializations table. */
1278
1279 static hashval_t
1280 hash_local_specialization (const void* p1)
1281 {
1282 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1283 }
1284
1285 /* Like register_specialization, but for local declarations. We are
1286 registering SPEC, an instantiation of TMPL. */
1287
1288 static void
1289 register_local_specialization (tree spec, tree tmpl)
1290 {
1291 void **slot;
1292
1293 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1294 htab_hash_pointer (tmpl), INSERT);
1295 *slot = build_tree_list (spec, tmpl);
1296 }
1297
1298 /* Print the list of candidate FNS in an error message. */
1299
1300 void
1301 print_candidates (tree fns)
1302 {
1303 tree fn;
1304
1305 const char *str = "candidates are:";
1306
1307 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1308 {
1309 tree f;
1310
1311 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1312 error ("%s %+#D", str, OVL_CURRENT (f));
1313 str = " ";
1314 }
1315 }
1316
1317 /* Returns the template (one of the functions given by TEMPLATE_ID)
1318 which can be specialized to match the indicated DECL with the
1319 explicit template args given in TEMPLATE_ID. The DECL may be
1320 NULL_TREE if none is available. In that case, the functions in
1321 TEMPLATE_ID are non-members.
1322
1323 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1324 specialization of a member template.
1325
1326 The TEMPLATE_COUNT is the number of references to qualifying
1327 template classes that appeared in the name of the function. See
1328 check_explicit_specialization for a more accurate description.
1329
1330 The template args (those explicitly specified and those deduced)
1331 are output in a newly created vector *TARGS_OUT.
1332
1333 If it is impossible to determine the result, an error message is
1334 issued. The error_mark_node is returned to indicate failure. */
1335
1336 static tree
1337 determine_specialization (tree template_id,
1338 tree decl,
1339 tree* targs_out,
1340 int need_member_template,
1341 int template_count)
1342 {
1343 tree fns;
1344 tree targs;
1345 tree explicit_targs;
1346 tree candidates = NULL_TREE;
1347 /* A TREE_LIST of templates of which DECL may be a specialization.
1348 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1349 corresponding TREE_PURPOSE is the set of template arguments that,
1350 when used to instantiate the template, would produce a function
1351 with the signature of DECL. */
1352 tree templates = NULL_TREE;
1353 int header_count;
1354 struct cp_binding_level *b;
1355
1356 *targs_out = NULL_TREE;
1357
1358 if (template_id == error_mark_node)
1359 return error_mark_node;
1360
1361 fns = TREE_OPERAND (template_id, 0);
1362 explicit_targs = TREE_OPERAND (template_id, 1);
1363
1364 if (fns == error_mark_node)
1365 return error_mark_node;
1366
1367 /* Check for baselinks. */
1368 if (BASELINK_P (fns))
1369 fns = BASELINK_FUNCTIONS (fns);
1370
1371 if (!is_overloaded_fn (fns))
1372 {
1373 error ("%qD is not a function template", fns);
1374 return error_mark_node;
1375 }
1376
1377 /* Count the number of template headers specified for this
1378 specialization. */
1379 header_count = 0;
1380 for (b = current_binding_level;
1381 b->kind == sk_template_parms;
1382 b = b->level_chain)
1383 ++header_count;
1384
1385 for (; fns; fns = OVL_NEXT (fns))
1386 {
1387 tree fn = OVL_CURRENT (fns);
1388
1389 if (TREE_CODE (fn) == TEMPLATE_DECL)
1390 {
1391 tree decl_arg_types;
1392 tree fn_arg_types;
1393
1394 /* DECL might be a specialization of FN. */
1395
1396 /* Adjust the type of DECL in case FN is a static member. */
1397 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1398 if (DECL_STATIC_FUNCTION_P (fn)
1399 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1400 decl_arg_types = TREE_CHAIN (decl_arg_types);
1401
1402 /* Check that the number of function parameters matches.
1403 For example,
1404 template <class T> void f(int i = 0);
1405 template <> void f<int>();
1406 The specialization f<int> is invalid but is not caught
1407 by get_bindings below. */
1408
1409 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1410 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1411 continue;
1412
1413 /* For a non-static member function, we need to make sure that
1414 the const qualification is the same. This can be done by
1415 checking the 'this' in the argument list. */
1416 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1417 && !same_type_p (TREE_VALUE (fn_arg_types),
1418 TREE_VALUE (decl_arg_types)))
1419 continue;
1420
1421 /* In case of explicit specialization, we need to check if
1422 the number of template headers appearing in the specialization
1423 is correct. This is usually done in check_explicit_specialization,
1424 but the check done there cannot be exhaustive when specializing
1425 member functions. Consider the following code:
1426
1427 template <> void A<int>::f(int);
1428 template <> template <> void A<int>::f(int);
1429
1430 Assuming that A<int> is not itself an explicit specialization
1431 already, the first line specializes "f" which is a non-template
1432 member function, whilst the second line specializes "f" which
1433 is a template member function. So both lines are syntactically
1434 correct, and check_explicit_specialization does not reject
1435 them.
1436
1437 Here, we can do better, as we are matching the specialization
1438 against the declarations. We count the number of template
1439 headers, and we check if they match TEMPLATE_COUNT + 1
1440 (TEMPLATE_COUNT is the number of qualifying template classes,
1441 plus there must be another header for the member template
1442 itself).
1443
1444 Notice that if header_count is zero, this is not a
1445 specialization but rather a template instantiation, so there
1446 is no check we can perform here. */
1447 if (header_count && header_count != template_count + 1)
1448 continue;
1449
1450 /* Check that the number of template arguments at the
1451 innermost level for DECL is the same as for FN. */
1452 if (current_binding_level->kind == sk_template_parms
1453 && !current_binding_level->explicit_spec_p
1454 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1455 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1456 (current_template_parms))))
1457 continue;
1458
1459 /* See whether this function might be a specialization of this
1460 template. */
1461 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1462
1463 if (!targs)
1464 /* We cannot deduce template arguments that when used to
1465 specialize TMPL will produce DECL. */
1466 continue;
1467
1468 /* Save this template, and the arguments deduced. */
1469 templates = tree_cons (targs, fn, templates);
1470 }
1471 else if (need_member_template)
1472 /* FN is an ordinary member function, and we need a
1473 specialization of a member template. */
1474 ;
1475 else if (TREE_CODE (fn) != FUNCTION_DECL)
1476 /* We can get IDENTIFIER_NODEs here in certain erroneous
1477 cases. */
1478 ;
1479 else if (!DECL_FUNCTION_MEMBER_P (fn))
1480 /* This is just an ordinary non-member function. Nothing can
1481 be a specialization of that. */
1482 ;
1483 else if (DECL_ARTIFICIAL (fn))
1484 /* Cannot specialize functions that are created implicitly. */
1485 ;
1486 else
1487 {
1488 tree decl_arg_types;
1489
1490 /* This is an ordinary member function. However, since
1491 we're here, we can assume it's enclosing class is a
1492 template class. For example,
1493
1494 template <typename T> struct S { void f(); };
1495 template <> void S<int>::f() {}
1496
1497 Here, S<int>::f is a non-template, but S<int> is a
1498 template class. If FN has the same type as DECL, we
1499 might be in business. */
1500
1501 if (!DECL_TEMPLATE_INFO (fn))
1502 /* Its enclosing class is an explicit specialization
1503 of a template class. This is not a candidate. */
1504 continue;
1505
1506 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1507 TREE_TYPE (TREE_TYPE (fn))))
1508 /* The return types differ. */
1509 continue;
1510
1511 /* Adjust the type of DECL in case FN is a static member. */
1512 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1513 if (DECL_STATIC_FUNCTION_P (fn)
1514 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1515 decl_arg_types = TREE_CHAIN (decl_arg_types);
1516
1517 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1518 decl_arg_types))
1519 /* They match! */
1520 candidates = tree_cons (NULL_TREE, fn, candidates);
1521 }
1522 }
1523
1524 if (templates && TREE_CHAIN (templates))
1525 {
1526 /* We have:
1527
1528 [temp.expl.spec]
1529
1530 It is possible for a specialization with a given function
1531 signature to be instantiated from more than one function
1532 template. In such cases, explicit specification of the
1533 template arguments must be used to uniquely identify the
1534 function template specialization being specialized.
1535
1536 Note that here, there's no suggestion that we're supposed to
1537 determine which of the candidate templates is most
1538 specialized. However, we, also have:
1539
1540 [temp.func.order]
1541
1542 Partial ordering of overloaded function template
1543 declarations is used in the following contexts to select
1544 the function template to which a function template
1545 specialization refers:
1546
1547 -- when an explicit specialization refers to a function
1548 template.
1549
1550 So, we do use the partial ordering rules, at least for now.
1551 This extension can only serve to make invalid programs valid,
1552 so it's safe. And, there is strong anecdotal evidence that
1553 the committee intended the partial ordering rules to apply;
1554 the EDG front-end has that behavior, and John Spicer claims
1555 that the committee simply forgot to delete the wording in
1556 [temp.expl.spec]. */
1557 tree tmpl = most_specialized_instantiation (templates);
1558 if (tmpl != error_mark_node)
1559 {
1560 templates = tmpl;
1561 TREE_CHAIN (templates) = NULL_TREE;
1562 }
1563 }
1564
1565 if (templates == NULL_TREE && candidates == NULL_TREE)
1566 {
1567 error ("template-id %qD for %q+D does not match any template "
1568 "declaration", template_id, decl);
1569 return error_mark_node;
1570 }
1571 else if ((templates && TREE_CHAIN (templates))
1572 || (candidates && TREE_CHAIN (candidates))
1573 || (templates && candidates))
1574 {
1575 error ("ambiguous template specialization %qD for %q+D",
1576 template_id, decl);
1577 chainon (candidates, templates);
1578 print_candidates (candidates);
1579 return error_mark_node;
1580 }
1581
1582 /* We have one, and exactly one, match. */
1583 if (candidates)
1584 {
1585 /* It was a specialization of an ordinary member function in a
1586 template class. */
1587 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1588 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1589 }
1590
1591 /* It was a specialization of a template. */
1592 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1593 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1594 {
1595 *targs_out = copy_node (targs);
1596 SET_TMPL_ARGS_LEVEL (*targs_out,
1597 TMPL_ARGS_DEPTH (*targs_out),
1598 TREE_PURPOSE (templates));
1599 }
1600 else
1601 *targs_out = TREE_PURPOSE (templates);
1602 return TREE_VALUE (templates);
1603 }
1604
1605 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1606 but with the default argument values filled in from those in the
1607 TMPL_TYPES. */
1608
1609 static tree
1610 copy_default_args_to_explicit_spec_1 (tree spec_types,
1611 tree tmpl_types)
1612 {
1613 tree new_spec_types;
1614
1615 if (!spec_types)
1616 return NULL_TREE;
1617
1618 if (spec_types == void_list_node)
1619 return void_list_node;
1620
1621 /* Substitute into the rest of the list. */
1622 new_spec_types =
1623 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1624 TREE_CHAIN (tmpl_types));
1625
1626 /* Add the default argument for this parameter. */
1627 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1628 TREE_VALUE (spec_types),
1629 new_spec_types);
1630 }
1631
1632 /* DECL is an explicit specialization. Replicate default arguments
1633 from the template it specializes. (That way, code like:
1634
1635 template <class T> void f(T = 3);
1636 template <> void f(double);
1637 void g () { f (); }
1638
1639 works, as required.) An alternative approach would be to look up
1640 the correct default arguments at the call-site, but this approach
1641 is consistent with how implicit instantiations are handled. */
1642
1643 static void
1644 copy_default_args_to_explicit_spec (tree decl)
1645 {
1646 tree tmpl;
1647 tree spec_types;
1648 tree tmpl_types;
1649 tree new_spec_types;
1650 tree old_type;
1651 tree new_type;
1652 tree t;
1653 tree object_type = NULL_TREE;
1654 tree in_charge = NULL_TREE;
1655 tree vtt = NULL_TREE;
1656
1657 /* See if there's anything we need to do. */
1658 tmpl = DECL_TI_TEMPLATE (decl);
1659 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1660 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1661 if (TREE_PURPOSE (t))
1662 break;
1663 if (!t)
1664 return;
1665
1666 old_type = TREE_TYPE (decl);
1667 spec_types = TYPE_ARG_TYPES (old_type);
1668
1669 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1670 {
1671 /* Remove the this pointer, but remember the object's type for
1672 CV quals. */
1673 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1674 spec_types = TREE_CHAIN (spec_types);
1675 tmpl_types = TREE_CHAIN (tmpl_types);
1676
1677 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1678 {
1679 /* DECL may contain more parameters than TMPL due to the extra
1680 in-charge parameter in constructors and destructors. */
1681 in_charge = spec_types;
1682 spec_types = TREE_CHAIN (spec_types);
1683 }
1684 if (DECL_HAS_VTT_PARM_P (decl))
1685 {
1686 vtt = spec_types;
1687 spec_types = TREE_CHAIN (spec_types);
1688 }
1689 }
1690
1691 /* Compute the merged default arguments. */
1692 new_spec_types =
1693 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1694
1695 /* Compute the new FUNCTION_TYPE. */
1696 if (object_type)
1697 {
1698 if (vtt)
1699 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1700 TREE_VALUE (vtt),
1701 new_spec_types);
1702
1703 if (in_charge)
1704 /* Put the in-charge parameter back. */
1705 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1706 TREE_VALUE (in_charge),
1707 new_spec_types);
1708
1709 new_type = build_method_type_directly (object_type,
1710 TREE_TYPE (old_type),
1711 new_spec_types);
1712 }
1713 else
1714 new_type = build_function_type (TREE_TYPE (old_type),
1715 new_spec_types);
1716 new_type = cp_build_type_attribute_variant (new_type,
1717 TYPE_ATTRIBUTES (old_type));
1718 new_type = build_exception_variant (new_type,
1719 TYPE_RAISES_EXCEPTIONS (old_type));
1720 TREE_TYPE (decl) = new_type;
1721 }
1722
1723 /* Check to see if the function just declared, as indicated in
1724 DECLARATOR, and in DECL, is a specialization of a function
1725 template. We may also discover that the declaration is an explicit
1726 instantiation at this point.
1727
1728 Returns DECL, or an equivalent declaration that should be used
1729 instead if all goes well. Issues an error message if something is
1730 amiss. Returns error_mark_node if the error is not easily
1731 recoverable.
1732
1733 FLAGS is a bitmask consisting of the following flags:
1734
1735 2: The function has a definition.
1736 4: The function is a friend.
1737
1738 The TEMPLATE_COUNT is the number of references to qualifying
1739 template classes that appeared in the name of the function. For
1740 example, in
1741
1742 template <class T> struct S { void f(); };
1743 void S<int>::f();
1744
1745 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1746 classes are not counted in the TEMPLATE_COUNT, so that in
1747
1748 template <class T> struct S {};
1749 template <> struct S<int> { void f(); }
1750 template <> void S<int>::f();
1751
1752 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1753 invalid; there should be no template <>.)
1754
1755 If the function is a specialization, it is marked as such via
1756 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1757 is set up correctly, and it is added to the list of specializations
1758 for that template. */
1759
1760 tree
1761 check_explicit_specialization (tree declarator,
1762 tree decl,
1763 int template_count,
1764 int flags)
1765 {
1766 int have_def = flags & 2;
1767 int is_friend = flags & 4;
1768 int specialization = 0;
1769 int explicit_instantiation = 0;
1770 int member_specialization = 0;
1771 tree ctype = DECL_CLASS_CONTEXT (decl);
1772 tree dname = DECL_NAME (decl);
1773 tmpl_spec_kind tsk;
1774
1775 if (is_friend)
1776 {
1777 if (!processing_specialization)
1778 tsk = tsk_none;
1779 else
1780 tsk = tsk_excessive_parms;
1781 }
1782 else
1783 tsk = current_tmpl_spec_kind (template_count);
1784
1785 switch (tsk)
1786 {
1787 case tsk_none:
1788 if (processing_specialization)
1789 {
1790 specialization = 1;
1791 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1792 }
1793 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1794 {
1795 if (is_friend)
1796 /* This could be something like:
1797
1798 template <class T> void f(T);
1799 class S { friend void f<>(int); } */
1800 specialization = 1;
1801 else
1802 {
1803 /* This case handles bogus declarations like template <>
1804 template <class T> void f<int>(); */
1805
1806 error ("template-id %qD in declaration of primary template",
1807 declarator);
1808 return decl;
1809 }
1810 }
1811 break;
1812
1813 case tsk_invalid_member_spec:
1814 /* The error has already been reported in
1815 check_specialization_scope. */
1816 return error_mark_node;
1817
1818 case tsk_invalid_expl_inst:
1819 error ("template parameter list used in explicit instantiation");
1820
1821 /* Fall through. */
1822
1823 case tsk_expl_inst:
1824 if (have_def)
1825 error ("definition provided for explicit instantiation");
1826
1827 explicit_instantiation = 1;
1828 break;
1829
1830 case tsk_excessive_parms:
1831 case tsk_insufficient_parms:
1832 if (tsk == tsk_excessive_parms)
1833 error ("too many template parameter lists in declaration of %qD",
1834 decl);
1835 else if (template_header_count)
1836 error("too few template parameter lists in declaration of %qD", decl);
1837 else
1838 error("explicit specialization of %qD must be introduced by "
1839 "%<template <>%>", decl);
1840
1841 /* Fall through. */
1842 case tsk_expl_spec:
1843 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1844 if (ctype)
1845 member_specialization = 1;
1846 else
1847 specialization = 1;
1848 break;
1849
1850 case tsk_template:
1851 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1852 {
1853 /* This case handles bogus declarations like template <>
1854 template <class T> void f<int>(); */
1855
1856 if (uses_template_parms (declarator))
1857 error ("function template partial specialization %qD "
1858 "is not allowed", declarator);
1859 else
1860 error ("template-id %qD in declaration of primary template",
1861 declarator);
1862 return decl;
1863 }
1864
1865 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1866 /* This is a specialization of a member template, without
1867 specialization the containing class. Something like:
1868
1869 template <class T> struct S {
1870 template <class U> void f (U);
1871 };
1872 template <> template <class U> void S<int>::f(U) {}
1873
1874 That's a specialization -- but of the entire template. */
1875 specialization = 1;
1876 break;
1877
1878 default:
1879 gcc_unreachable ();
1880 }
1881
1882 if (specialization || member_specialization)
1883 {
1884 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1885 for (; t; t = TREE_CHAIN (t))
1886 if (TREE_PURPOSE (t))
1887 {
1888 pedwarn
1889 ("default argument specified in explicit specialization");
1890 break;
1891 }
1892 }
1893
1894 if (specialization || member_specialization || explicit_instantiation)
1895 {
1896 tree tmpl = NULL_TREE;
1897 tree targs = NULL_TREE;
1898
1899 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1900 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1901 {
1902 tree fns;
1903
1904 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1905 if (ctype)
1906 fns = dname;
1907 else
1908 {
1909 /* If there is no class context, the explicit instantiation
1910 must be at namespace scope. */
1911 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1912
1913 /* Find the namespace binding, using the declaration
1914 context. */
1915 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1916 false, true);
1917 if (!fns || !is_overloaded_fn (fns))
1918 {
1919 error ("%qD is not a template function", dname);
1920 fns = error_mark_node;
1921 }
1922 else
1923 {
1924 tree fn = OVL_CURRENT (fns);
1925 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1926 CP_DECL_CONTEXT (fn)))
1927 error ("%qD is not declared in %qD",
1928 decl, current_namespace);
1929 }
1930 }
1931
1932 declarator = lookup_template_function (fns, NULL_TREE);
1933 }
1934
1935 if (declarator == error_mark_node)
1936 return error_mark_node;
1937
1938 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1939 {
1940 if (!explicit_instantiation)
1941 /* A specialization in class scope. This is invalid,
1942 but the error will already have been flagged by
1943 check_specialization_scope. */
1944 return error_mark_node;
1945 else
1946 {
1947 /* It's not valid to write an explicit instantiation in
1948 class scope, e.g.:
1949
1950 class C { template void f(); }
1951
1952 This case is caught by the parser. However, on
1953 something like:
1954
1955 template class C { void f(); };
1956
1957 (which is invalid) we can get here. The error will be
1958 issued later. */
1959 ;
1960 }
1961
1962 return decl;
1963 }
1964 else if (ctype != NULL_TREE
1965 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1966 IDENTIFIER_NODE))
1967 {
1968 /* Find the list of functions in ctype that have the same
1969 name as the declared function. */
1970 tree name = TREE_OPERAND (declarator, 0);
1971 tree fns = NULL_TREE;
1972 int idx;
1973
1974 if (constructor_name_p (name, ctype))
1975 {
1976 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1977
1978 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1979 : !CLASSTYPE_DESTRUCTORS (ctype))
1980 {
1981 /* From [temp.expl.spec]:
1982
1983 If such an explicit specialization for the member
1984 of a class template names an implicitly-declared
1985 special member function (clause _special_), the
1986 program is ill-formed.
1987
1988 Similar language is found in [temp.explicit]. */
1989 error ("specialization of implicitly-declared special member function");
1990 return error_mark_node;
1991 }
1992
1993 name = is_constructor ? ctor_identifier : dtor_identifier;
1994 }
1995
1996 if (!DECL_CONV_FN_P (decl))
1997 {
1998 idx = lookup_fnfields_1 (ctype, name);
1999 if (idx >= 0)
2000 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2001 }
2002 else
2003 {
2004 VEC(tree,gc) *methods;
2005 tree ovl;
2006
2007 /* For a type-conversion operator, we cannot do a
2008 name-based lookup. We might be looking for `operator
2009 int' which will be a specialization of `operator T'.
2010 So, we find *all* the conversion operators, and then
2011 select from them. */
2012 fns = NULL_TREE;
2013
2014 methods = CLASSTYPE_METHOD_VEC (ctype);
2015 if (methods)
2016 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2017 VEC_iterate (tree, methods, idx, ovl);
2018 ++idx)
2019 {
2020 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2021 /* There are no more conversion functions. */
2022 break;
2023
2024 /* Glue all these conversion functions together
2025 with those we already have. */
2026 for (; ovl; ovl = OVL_NEXT (ovl))
2027 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2028 }
2029 }
2030
2031 if (fns == NULL_TREE)
2032 {
2033 error ("no member function %qD declared in %qT", name, ctype);
2034 return error_mark_node;
2035 }
2036 else
2037 TREE_OPERAND (declarator, 0) = fns;
2038 }
2039
2040 /* Figure out what exactly is being specialized at this point.
2041 Note that for an explicit instantiation, even one for a
2042 member function, we cannot tell apriori whether the
2043 instantiation is for a member template, or just a member
2044 function of a template class. Even if a member template is
2045 being instantiated, the member template arguments may be
2046 elided if they can be deduced from the rest of the
2047 declaration. */
2048 tmpl = determine_specialization (declarator, decl,
2049 &targs,
2050 member_specialization,
2051 template_count);
2052
2053 if (!tmpl || tmpl == error_mark_node)
2054 /* We couldn't figure out what this declaration was
2055 specializing. */
2056 return error_mark_node;
2057 else
2058 {
2059 tree gen_tmpl = most_general_template (tmpl);
2060
2061 if (explicit_instantiation)
2062 {
2063 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2064 is done by do_decl_instantiation later. */
2065
2066 int arg_depth = TMPL_ARGS_DEPTH (targs);
2067 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2068
2069 if (arg_depth > parm_depth)
2070 {
2071 /* If TMPL is not the most general template (for
2072 example, if TMPL is a friend template that is
2073 injected into namespace scope), then there will
2074 be too many levels of TARGS. Remove some of them
2075 here. */
2076 int i;
2077 tree new_targs;
2078
2079 new_targs = make_tree_vec (parm_depth);
2080 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2081 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2082 = TREE_VEC_ELT (targs, i);
2083 targs = new_targs;
2084 }
2085
2086 return instantiate_template (tmpl, targs, tf_error);
2087 }
2088
2089 /* If we thought that the DECL was a member function, but it
2090 turns out to be specializing a static member function,
2091 make DECL a static member function as well. */
2092 if (DECL_STATIC_FUNCTION_P (tmpl)
2093 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2094 revert_static_member_fn (decl);
2095
2096 /* If this is a specialization of a member template of a
2097 template class. In we want to return the TEMPLATE_DECL,
2098 not the specialization of it. */
2099 if (tsk == tsk_template)
2100 {
2101 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2102 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2103 if (have_def)
2104 {
2105 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2106 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2107 = DECL_SOURCE_LOCATION (decl);
2108 /* We want to use the argument list specified in the
2109 definition, not in the original declaration. */
2110 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2111 = DECL_ARGUMENTS (decl);
2112 }
2113 return tmpl;
2114 }
2115
2116 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2117 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2118
2119 /* Inherit default function arguments from the template
2120 DECL is specializing. */
2121 copy_default_args_to_explicit_spec (decl);
2122
2123 /* This specialization has the same protection as the
2124 template it specializes. */
2125 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2126 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2127 /* The specialization has the same visibility as the
2128 template it specializes. */
2129 if (DECL_VISIBILITY_SPECIFIED (gen_tmpl))
2130 {
2131 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2132 DECL_VISIBILITY (decl) = DECL_VISIBILITY (gen_tmpl);
2133 }
2134 /* If DECL is a friend declaration, declared using an
2135 unqualified name, the namespace associated with DECL may
2136 have been set incorrectly. For example, in:
2137
2138 template <typename T> void f(T);
2139 namespace N {
2140 struct S { friend void f<int>(int); }
2141 }
2142
2143 we will have set the DECL_CONTEXT for the friend
2144 declaration to N, rather than to the global namespace. */
2145 if (DECL_NAMESPACE_SCOPE_P (decl))
2146 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2147
2148 if (is_friend && !have_def)
2149 /* This is not really a declaration of a specialization.
2150 It's just the name of an instantiation. But, it's not
2151 a request for an instantiation, either. */
2152 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2153 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2154 /* This is indeed a specialization. In case of constructors
2155 and destructors, we need in-charge and not-in-charge
2156 versions in V3 ABI. */
2157 clone_function_decl (decl, /*update_method_vec_p=*/0);
2158
2159 /* Register this specialization so that we can find it
2160 again. */
2161 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2162 }
2163 }
2164
2165 return decl;
2166 }
2167
2168 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2169 parameters. These are represented in the same format used for
2170 DECL_TEMPLATE_PARMS. */
2171
2172 int
2173 comp_template_parms (tree parms1, tree parms2)
2174 {
2175 tree p1;
2176 tree p2;
2177
2178 if (parms1 == parms2)
2179 return 1;
2180
2181 for (p1 = parms1, p2 = parms2;
2182 p1 != NULL_TREE && p2 != NULL_TREE;
2183 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2184 {
2185 tree t1 = TREE_VALUE (p1);
2186 tree t2 = TREE_VALUE (p2);
2187 int i;
2188
2189 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2190 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2191
2192 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2193 return 0;
2194
2195 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2196 {
2197 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2198 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2199
2200 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2201 return 0;
2202
2203 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2204 continue;
2205 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2206 return 0;
2207 }
2208 }
2209
2210 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2211 /* One set of parameters has more parameters lists than the
2212 other. */
2213 return 0;
2214
2215 return 1;
2216 }
2217
2218 /* Complain if DECL shadows a template parameter.
2219
2220 [temp.local]: A template-parameter shall not be redeclared within its
2221 scope (including nested scopes). */
2222
2223 void
2224 check_template_shadow (tree decl)
2225 {
2226 tree olddecl;
2227
2228 /* If we're not in a template, we can't possibly shadow a template
2229 parameter. */
2230 if (!current_template_parms)
2231 return;
2232
2233 /* Figure out what we're shadowing. */
2234 if (TREE_CODE (decl) == OVERLOAD)
2235 decl = OVL_CURRENT (decl);
2236 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2237
2238 /* If there's no previous binding for this name, we're not shadowing
2239 anything, let alone a template parameter. */
2240 if (!olddecl)
2241 return;
2242
2243 /* If we're not shadowing a template parameter, we're done. Note
2244 that OLDDECL might be an OVERLOAD (or perhaps even an
2245 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2246 node. */
2247 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2248 return;
2249
2250 /* We check for decl != olddecl to avoid bogus errors for using a
2251 name inside a class. We check TPFI to avoid duplicate errors for
2252 inline member templates. */
2253 if (decl == olddecl
2254 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2255 return;
2256
2257 error ("declaration of %q+#D", decl);
2258 error (" shadows template parm %q+#D", olddecl);
2259 }
2260
2261 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2262 ORIG_LEVEL, DECL, and TYPE. */
2263
2264 static tree
2265 build_template_parm_index (int index,
2266 int level,
2267 int orig_level,
2268 tree decl,
2269 tree type)
2270 {
2271 tree t = make_node (TEMPLATE_PARM_INDEX);
2272 TEMPLATE_PARM_IDX (t) = index;
2273 TEMPLATE_PARM_LEVEL (t) = level;
2274 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2275 TEMPLATE_PARM_DECL (t) = decl;
2276 TREE_TYPE (t) = type;
2277 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2278 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2279 TREE_READONLY (t) = TREE_READONLY (decl);
2280
2281 return t;
2282 }
2283
2284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2287 new one is created. */
2288
2289 static tree
2290 reduce_template_parm_level (tree index, tree type, int levels)
2291 {
2292 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2293 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2294 != TEMPLATE_PARM_LEVEL (index) - levels))
2295 {
2296 tree orig_decl = TEMPLATE_PARM_DECL (index);
2297 tree decl, t;
2298
2299 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2300 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2301 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2302 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2303 DECL_ARTIFICIAL (decl) = 1;
2304 SET_DECL_TEMPLATE_PARM_P (decl);
2305
2306 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2307 TEMPLATE_PARM_LEVEL (index) - levels,
2308 TEMPLATE_PARM_ORIG_LEVEL (index),
2309 decl, type);
2310 TEMPLATE_PARM_DESCENDANTS (index) = t;
2311
2312 /* Template template parameters need this. */
2313 if (TREE_CODE (decl) != CONST_DECL)
2314 DECL_TEMPLATE_PARMS (decl)
2315 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2316 }
2317
2318 return TEMPLATE_PARM_DESCENDANTS (index);
2319 }
2320
2321 /* Process information from new template parameter NEXT and append it to the
2322 LIST being built. This new parameter is a non-type parameter iff
2323 IS_NON_TYPE is true. */
2324
2325 tree
2326 process_template_parm (tree list, tree next, bool is_non_type)
2327 {
2328 tree parm;
2329 tree decl = 0;
2330 tree defval;
2331 int idx;
2332
2333 parm = next;
2334 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2335 defval = TREE_PURPOSE (parm);
2336
2337 if (list)
2338 {
2339 tree p = TREE_VALUE (tree_last (list));
2340
2341 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2342 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2343 else
2344 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2345 ++idx;
2346 }
2347 else
2348 idx = 0;
2349
2350 if (is_non_type)
2351 {
2352 parm = TREE_VALUE (parm);
2353
2354 SET_DECL_TEMPLATE_PARM_P (parm);
2355
2356 /* [temp.param]
2357
2358 The top-level cv-qualifiers on the template-parameter are
2359 ignored when determining its type. */
2360 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2361
2362 /* A template parameter is not modifiable. */
2363 TREE_CONSTANT (parm) = 1;
2364 TREE_INVARIANT (parm) = 1;
2365 TREE_READONLY (parm) = 1;
2366 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2367 TREE_TYPE (parm) = void_type_node;
2368 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2369 TREE_CONSTANT (decl) = 1;
2370 TREE_INVARIANT (decl) = 1;
2371 TREE_READONLY (decl) = 1;
2372 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2373 = build_template_parm_index (idx, processing_template_decl,
2374 processing_template_decl,
2375 decl, TREE_TYPE (parm));
2376 }
2377 else
2378 {
2379 tree t;
2380 parm = TREE_VALUE (TREE_VALUE (parm));
2381
2382 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2383 {
2384 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2385 /* This is for distinguishing between real templates and template
2386 template parameters */
2387 TREE_TYPE (parm) = t;
2388 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2389 decl = parm;
2390 }
2391 else
2392 {
2393 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2394 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2395 decl = build_decl (TYPE_DECL, parm, t);
2396 }
2397
2398 TYPE_NAME (t) = decl;
2399 TYPE_STUB_DECL (t) = decl;
2400 parm = decl;
2401 TEMPLATE_TYPE_PARM_INDEX (t)
2402 = build_template_parm_index (idx, processing_template_decl,
2403 processing_template_decl,
2404 decl, TREE_TYPE (parm));
2405 }
2406 DECL_ARTIFICIAL (decl) = 1;
2407 SET_DECL_TEMPLATE_PARM_P (decl);
2408 pushdecl (decl);
2409 parm = build_tree_list (defval, parm);
2410 return chainon (list, parm);
2411 }
2412
2413 /* The end of a template parameter list has been reached. Process the
2414 tree list into a parameter vector, converting each parameter into a more
2415 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2416 as PARM_DECLs. */
2417
2418 tree
2419 end_template_parm_list (tree parms)
2420 {
2421 int nparms;
2422 tree parm, next;
2423 tree saved_parmlist = make_tree_vec (list_length (parms));
2424
2425 current_template_parms
2426 = tree_cons (size_int (processing_template_decl),
2427 saved_parmlist, current_template_parms);
2428
2429 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2430 {
2431 next = TREE_CHAIN (parm);
2432 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2433 TREE_CHAIN (parm) = NULL_TREE;
2434 }
2435
2436 --processing_template_parmlist;
2437
2438 return saved_parmlist;
2439 }
2440
2441 /* end_template_decl is called after a template declaration is seen. */
2442
2443 void
2444 end_template_decl (void)
2445 {
2446 reset_specialization ();
2447
2448 if (! processing_template_decl)
2449 return;
2450
2451 /* This matches the pushlevel in begin_template_parm_list. */
2452 finish_scope ();
2453
2454 --processing_template_decl;
2455 current_template_parms = TREE_CHAIN (current_template_parms);
2456 }
2457
2458 /* Given a template argument vector containing the template PARMS.
2459 The innermost PARMS are given first. */
2460
2461 static tree
2462 current_template_args (void)
2463 {
2464 tree header;
2465 tree args = NULL_TREE;
2466 int length = TMPL_PARMS_DEPTH (current_template_parms);
2467 int l = length;
2468
2469 /* If there is only one level of template parameters, we do not
2470 create a TREE_VEC of TREE_VECs. Instead, we return a single
2471 TREE_VEC containing the arguments. */
2472 if (length > 1)
2473 args = make_tree_vec (length);
2474
2475 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2476 {
2477 tree a = copy_node (TREE_VALUE (header));
2478 int i;
2479
2480 TREE_TYPE (a) = NULL_TREE;
2481 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2482 {
2483 tree t = TREE_VEC_ELT (a, i);
2484
2485 /* T will be a list if we are called from within a
2486 begin/end_template_parm_list pair, but a vector directly
2487 if within a begin/end_member_template_processing pair. */
2488 if (TREE_CODE (t) == TREE_LIST)
2489 {
2490 t = TREE_VALUE (t);
2491
2492 if (TREE_CODE (t) == TYPE_DECL
2493 || TREE_CODE (t) == TEMPLATE_DECL)
2494 t = TREE_TYPE (t);
2495 else
2496 t = DECL_INITIAL (t);
2497 TREE_VEC_ELT (a, i) = t;
2498 }
2499 }
2500
2501 if (length > 1)
2502 TREE_VEC_ELT (args, --l) = a;
2503 else
2504 args = a;
2505 }
2506
2507 return args;
2508 }
2509
2510 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2511 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2512 a member template. Used by push_template_decl below. */
2513
2514 static tree
2515 build_template_decl (tree decl, tree parms, bool member_template_p)
2516 {
2517 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2518 DECL_TEMPLATE_PARMS (tmpl) = parms;
2519 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2520 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2521 if (DECL_LANG_SPECIFIC (decl))
2522 {
2523 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2524 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2525 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2526 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2527 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2528 if (DECL_OVERLOADED_OPERATOR_P (decl))
2529 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2530 DECL_OVERLOADED_OPERATOR_P (decl));
2531 }
2532
2533 return tmpl;
2534 }
2535
2536 struct template_parm_data
2537 {
2538 /* The level of the template parameters we are currently
2539 processing. */
2540 int level;
2541
2542 /* The index of the specialization argument we are currently
2543 processing. */
2544 int current_arg;
2545
2546 /* An array whose size is the number of template parameters. The
2547 elements are nonzero if the parameter has been used in any one
2548 of the arguments processed so far. */
2549 int* parms;
2550
2551 /* An array whose size is the number of template arguments. The
2552 elements are nonzero if the argument makes use of template
2553 parameters of this level. */
2554 int* arg_uses_template_parms;
2555 };
2556
2557 /* Subroutine of push_template_decl used to see if each template
2558 parameter in a partial specialization is used in the explicit
2559 argument list. If T is of the LEVEL given in DATA (which is
2560 treated as a template_parm_data*), then DATA->PARMS is marked
2561 appropriately. */
2562
2563 static int
2564 mark_template_parm (tree t, void* data)
2565 {
2566 int level;
2567 int idx;
2568 struct template_parm_data* tpd = (struct template_parm_data*) data;
2569
2570 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2571 {
2572 level = TEMPLATE_PARM_LEVEL (t);
2573 idx = TEMPLATE_PARM_IDX (t);
2574 }
2575 else
2576 {
2577 level = TEMPLATE_TYPE_LEVEL (t);
2578 idx = TEMPLATE_TYPE_IDX (t);
2579 }
2580
2581 if (level == tpd->level)
2582 {
2583 tpd->parms[idx] = 1;
2584 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2585 }
2586
2587 /* Return zero so that for_each_template_parm will continue the
2588 traversal of the tree; we want to mark *every* template parm. */
2589 return 0;
2590 }
2591
2592 /* Process the partial specialization DECL. */
2593
2594 static tree
2595 process_partial_specialization (tree decl)
2596 {
2597 tree type = TREE_TYPE (decl);
2598 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2599 tree specargs = CLASSTYPE_TI_ARGS (type);
2600 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2601 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2602 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2603 int nargs = TREE_VEC_LENGTH (inner_args);
2604 int ntparms = TREE_VEC_LENGTH (inner_parms);
2605 int i;
2606 int did_error_intro = 0;
2607 struct template_parm_data tpd;
2608 struct template_parm_data tpd2;
2609
2610 /* We check that each of the template parameters given in the
2611 partial specialization is used in the argument list to the
2612 specialization. For example:
2613
2614 template <class T> struct S;
2615 template <class T> struct S<T*>;
2616
2617 The second declaration is OK because `T*' uses the template
2618 parameter T, whereas
2619
2620 template <class T> struct S<int>;
2621
2622 is no good. Even trickier is:
2623
2624 template <class T>
2625 struct S1
2626 {
2627 template <class U>
2628 struct S2;
2629 template <class U>
2630 struct S2<T>;
2631 };
2632
2633 The S2<T> declaration is actually invalid; it is a
2634 full-specialization. Of course,
2635
2636 template <class U>
2637 struct S2<T (*)(U)>;
2638
2639 or some such would have been OK. */
2640 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2641 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2642 memset (tpd.parms, 0, sizeof (int) * ntparms);
2643
2644 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2645 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2646 for (i = 0; i < nargs; ++i)
2647 {
2648 tpd.current_arg = i;
2649 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2650 &mark_template_parm,
2651 &tpd,
2652 NULL);
2653 }
2654 for (i = 0; i < ntparms; ++i)
2655 if (tpd.parms[i] == 0)
2656 {
2657 /* One of the template parms was not used in the
2658 specialization. */
2659 if (!did_error_intro)
2660 {
2661 error ("template parameters not used in partial specialization:");
2662 did_error_intro = 1;
2663 }
2664
2665 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2666 }
2667
2668 /* [temp.class.spec]
2669
2670 The argument list of the specialization shall not be identical to
2671 the implicit argument list of the primary template. */
2672 if (comp_template_args
2673 (inner_args,
2674 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2675 (maintmpl)))))
2676 error ("partial specialization %qT does not specialize any template arguments", type);
2677
2678 /* [temp.class.spec]
2679
2680 A partially specialized non-type argument expression shall not
2681 involve template parameters of the partial specialization except
2682 when the argument expression is a simple identifier.
2683
2684 The type of a template parameter corresponding to a specialized
2685 non-type argument shall not be dependent on a parameter of the
2686 specialization. */
2687 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2688 tpd2.parms = 0;
2689 for (i = 0; i < nargs; ++i)
2690 {
2691 tree arg = TREE_VEC_ELT (inner_args, i);
2692 if (/* These first two lines are the `non-type' bit. */
2693 !TYPE_P (arg)
2694 && TREE_CODE (arg) != TEMPLATE_DECL
2695 /* This next line is the `argument expression is not just a
2696 simple identifier' condition and also the `specialized
2697 non-type argument' bit. */
2698 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2699 {
2700 if (tpd.arg_uses_template_parms[i])
2701 error ("template argument %qE involves template parameter(s)", arg);
2702 else
2703 {
2704 /* Look at the corresponding template parameter,
2705 marking which template parameters its type depends
2706 upon. */
2707 tree type =
2708 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2709 i)));
2710
2711 if (!tpd2.parms)
2712 {
2713 /* We haven't yet initialized TPD2. Do so now. */
2714 tpd2.arg_uses_template_parms
2715 = (int *) alloca (sizeof (int) * nargs);
2716 /* The number of parameters here is the number in the
2717 main template, which, as checked in the assertion
2718 above, is NARGS. */
2719 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2720 tpd2.level =
2721 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2722 }
2723
2724 /* Mark the template parameters. But this time, we're
2725 looking for the template parameters of the main
2726 template, not in the specialization. */
2727 tpd2.current_arg = i;
2728 tpd2.arg_uses_template_parms[i] = 0;
2729 memset (tpd2.parms, 0, sizeof (int) * nargs);
2730 for_each_template_parm (type,
2731 &mark_template_parm,
2732 &tpd2,
2733 NULL);
2734
2735 if (tpd2.arg_uses_template_parms [i])
2736 {
2737 /* The type depended on some template parameters.
2738 If they are fully specialized in the
2739 specialization, that's OK. */
2740 int j;
2741 for (j = 0; j < nargs; ++j)
2742 if (tpd2.parms[j] != 0
2743 && tpd.arg_uses_template_parms [j])
2744 {
2745 error ("type %qT of template argument %qE depends "
2746 "on template parameter(s)",
2747 type,
2748 arg);
2749 break;
2750 }
2751 }
2752 }
2753 }
2754 }
2755
2756 if (retrieve_specialization (maintmpl, specargs,
2757 /*class_specializations_p=*/true))
2758 /* We've already got this specialization. */
2759 return decl;
2760
2761 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2762 = tree_cons (specargs, inner_parms,
2763 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2764 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2765 return decl;
2766 }
2767
2768 /* Check that a template declaration's use of default arguments is not
2769 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2770 nonzero if DECL is the thing declared by a primary template.
2771 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2772
2773 static void
2774 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2775 {
2776 const char *msg;
2777 int last_level_to_check;
2778 tree parm_level;
2779
2780 /* [temp.param]
2781
2782 A default template-argument shall not be specified in a
2783 function template declaration or a function template definition, nor
2784 in the template-parameter-list of the definition of a member of a
2785 class template. */
2786
2787 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2788 /* You can't have a function template declaration in a local
2789 scope, nor you can you define a member of a class template in a
2790 local scope. */
2791 return;
2792
2793 if (current_class_type
2794 && !TYPE_BEING_DEFINED (current_class_type)
2795 && DECL_LANG_SPECIFIC (decl)
2796 /* If this is either a friend defined in the scope of the class
2797 or a member function. */
2798 && (DECL_FUNCTION_MEMBER_P (decl)
2799 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2800 : DECL_FRIEND_CONTEXT (decl)
2801 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2802 : false)
2803 /* And, if it was a member function, it really was defined in
2804 the scope of the class. */
2805 && (!DECL_FUNCTION_MEMBER_P (decl)
2806 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2807 /* We already checked these parameters when the template was
2808 declared, so there's no need to do it again now. This function
2809 was defined in class scope, but we're processing it's body now
2810 that the class is complete. */
2811 return;
2812
2813 /* [temp.param]
2814
2815 If a template-parameter has a default template-argument, all
2816 subsequent template-parameters shall have a default
2817 template-argument supplied. */
2818 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2819 {
2820 tree inner_parms = TREE_VALUE (parm_level);
2821 int ntparms = TREE_VEC_LENGTH (inner_parms);
2822 int seen_def_arg_p = 0;
2823 int i;
2824
2825 for (i = 0; i < ntparms; ++i)
2826 {
2827 tree parm = TREE_VEC_ELT (inner_parms, i);
2828 if (TREE_PURPOSE (parm))
2829 seen_def_arg_p = 1;
2830 else if (seen_def_arg_p)
2831 {
2832 error ("no default argument for %qD", TREE_VALUE (parm));
2833 /* For better subsequent error-recovery, we indicate that
2834 there should have been a default argument. */
2835 TREE_PURPOSE (parm) = error_mark_node;
2836 }
2837 }
2838 }
2839
2840 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2841 /* For an ordinary class template, default template arguments are
2842 allowed at the innermost level, e.g.:
2843 template <class T = int>
2844 struct S {};
2845 but, in a partial specialization, they're not allowed even
2846 there, as we have in [temp.class.spec]:
2847
2848 The template parameter list of a specialization shall not
2849 contain default template argument values.
2850
2851 So, for a partial specialization, or for a function template,
2852 we look at all of them. */
2853 ;
2854 else
2855 /* But, for a primary class template that is not a partial
2856 specialization we look at all template parameters except the
2857 innermost ones. */
2858 parms = TREE_CHAIN (parms);
2859
2860 /* Figure out what error message to issue. */
2861 if (TREE_CODE (decl) == FUNCTION_DECL)
2862 msg = "default template arguments may not be used in function templates";
2863 else if (is_partial)
2864 msg = "default template arguments may not be used in partial specializations";
2865 else
2866 msg = "default argument for template parameter for class enclosing %qD";
2867
2868 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2869 /* If we're inside a class definition, there's no need to
2870 examine the parameters to the class itself. On the one
2871 hand, they will be checked when the class is defined, and,
2872 on the other, default arguments are valid in things like:
2873 template <class T = double>
2874 struct S { template <class U> void f(U); };
2875 Here the default argument for `S' has no bearing on the
2876 declaration of `f'. */
2877 last_level_to_check = template_class_depth (current_class_type) + 1;
2878 else
2879 /* Check everything. */
2880 last_level_to_check = 0;
2881
2882 for (parm_level = parms;
2883 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2884 parm_level = TREE_CHAIN (parm_level))
2885 {
2886 tree inner_parms = TREE_VALUE (parm_level);
2887 int i;
2888 int ntparms;
2889
2890 ntparms = TREE_VEC_LENGTH (inner_parms);
2891 for (i = 0; i < ntparms; ++i)
2892 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2893 {
2894 if (msg)
2895 {
2896 error (msg, decl);
2897 msg = 0;
2898 }
2899
2900 /* Clear out the default argument so that we are not
2901 confused later. */
2902 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2903 }
2904
2905 /* At this point, if we're still interested in issuing messages,
2906 they must apply to classes surrounding the object declared. */
2907 if (msg)
2908 msg = "default argument for template parameter for class enclosing %qD";
2909 }
2910 }
2911
2912 /* Worker for push_template_decl_real, called via
2913 for_each_template_parm. DATA is really an int, indicating the
2914 level of the parameters we are interested in. If T is a template
2915 parameter of that level, return nonzero. */
2916
2917 static int
2918 template_parm_this_level_p (tree t, void* data)
2919 {
2920 int this_level = *(int *)data;
2921 int level;
2922
2923 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2924 level = TEMPLATE_PARM_LEVEL (t);
2925 else
2926 level = TEMPLATE_TYPE_LEVEL (t);
2927 return level == this_level;
2928 }
2929
2930 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2931 parameters given by current_template_args, or reuses a
2932 previously existing one, if appropriate. Returns the DECL, or an
2933 equivalent one, if it is replaced via a call to duplicate_decls.
2934
2935 If IS_FRIEND is true, DECL is a friend declaration. */
2936
2937 tree
2938 push_template_decl_real (tree decl, bool is_friend)
2939 {
2940 tree tmpl;
2941 tree args;
2942 tree info;
2943 tree ctx;
2944 int primary;
2945 int is_partial;
2946 int new_template_p = 0;
2947 /* True if the template is a member template, in the sense of
2948 [temp.mem]. */
2949 bool member_template_p = false;
2950
2951 if (decl == error_mark_node)
2952 return decl;
2953
2954 /* See if this is a partial specialization. */
2955 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2956 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2957 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2958
2959 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
2960 is_friend = true;
2961
2962 if (is_friend)
2963 /* For a friend, we want the context of the friend function, not
2964 the type of which it is a friend. */
2965 ctx = DECL_CONTEXT (decl);
2966 else if (CP_DECL_CONTEXT (decl)
2967 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2968 /* In the case of a virtual function, we want the class in which
2969 it is defined. */
2970 ctx = CP_DECL_CONTEXT (decl);
2971 else
2972 /* Otherwise, if we're currently defining some class, the DECL
2973 is assumed to be a member of the class. */
2974 ctx = current_scope ();
2975
2976 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2977 ctx = NULL_TREE;
2978
2979 if (!DECL_CONTEXT (decl))
2980 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2981
2982 /* See if this is a primary template. */
2983 primary = template_parm_scope_p ();
2984
2985 if (primary)
2986 {
2987 if (DECL_CLASS_SCOPE_P (decl))
2988 member_template_p = true;
2989 if (TREE_CODE (decl) == TYPE_DECL
2990 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2991 error ("template class without a name");
2992 else if (TREE_CODE (decl) == FUNCTION_DECL)
2993 {
2994 if (DECL_DESTRUCTOR_P (decl))
2995 {
2996 /* [temp.mem]
2997
2998 A destructor shall not be a member template. */
2999 error ("destructor %qD declared as member template", decl);
3000 return error_mark_node;
3001 }
3002 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3003 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3004 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3005 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3006 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3007 == void_list_node)))
3008 {
3009 /* [basic.stc.dynamic.allocation]
3010
3011 An allocation function can be a function
3012 template. ... Template allocation functions shall
3013 have two or more parameters. */
3014 error ("invalid template declaration of %qD", decl);
3015 return decl;
3016 }
3017 }
3018 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3019 && CLASS_TYPE_P (TREE_TYPE (decl)))
3020 /* OK */;
3021 else
3022 {
3023 error ("template declaration of %q#D", decl);
3024 return error_mark_node;
3025 }
3026 }
3027
3028 /* Check to see that the rules regarding the use of default
3029 arguments are not being violated. */
3030 check_default_tmpl_args (decl, current_template_parms,
3031 primary, is_partial);
3032
3033 if (is_partial)
3034 return process_partial_specialization (decl);
3035
3036 args = current_template_args ();
3037
3038 if (!ctx
3039 || TREE_CODE (ctx) == FUNCTION_DECL
3040 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3041 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3042 {
3043 if (DECL_LANG_SPECIFIC (decl)
3044 && DECL_TEMPLATE_INFO (decl)
3045 && DECL_TI_TEMPLATE (decl))
3046 tmpl = DECL_TI_TEMPLATE (decl);
3047 /* If DECL is a TYPE_DECL for a class-template, then there won't
3048 be DECL_LANG_SPECIFIC. The information equivalent to
3049 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3050 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3051 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3052 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3053 {
3054 /* Since a template declaration already existed for this
3055 class-type, we must be redeclaring it here. Make sure
3056 that the redeclaration is valid. */
3057 redeclare_class_template (TREE_TYPE (decl),
3058 current_template_parms);
3059 /* We don't need to create a new TEMPLATE_DECL; just use the
3060 one we already had. */
3061 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3062 }
3063 else
3064 {
3065 tmpl = build_template_decl (decl, current_template_parms,
3066 member_template_p);
3067 new_template_p = 1;
3068
3069 if (DECL_LANG_SPECIFIC (decl)
3070 && DECL_TEMPLATE_SPECIALIZATION (decl))
3071 {
3072 /* A specialization of a member template of a template
3073 class. */
3074 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3075 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3076 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3077 }
3078 }
3079 }
3080 else
3081 {
3082 tree a, t, current, parms;
3083 int i;
3084
3085 if (TREE_CODE (decl) == TYPE_DECL)
3086 {
3087 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3088 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3089 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3090 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3091 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3092 else
3093 {
3094 error ("%qD does not declare a template type", decl);
3095 return decl;
3096 }
3097 }
3098 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3099 {
3100 error ("template definition of non-template %q#D", decl);
3101 return decl;
3102 }
3103 else
3104 tmpl = DECL_TI_TEMPLATE (decl);
3105
3106 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3107 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3108 && DECL_TEMPLATE_SPECIALIZATION (decl)
3109 && DECL_MEMBER_TEMPLATE_P (tmpl))
3110 {
3111 tree new_tmpl;
3112
3113 /* The declaration is a specialization of a member
3114 template, declared outside the class. Therefore, the
3115 innermost template arguments will be NULL, so we
3116 replace them with the arguments determined by the
3117 earlier call to check_explicit_specialization. */
3118 args = DECL_TI_ARGS (decl);
3119
3120 new_tmpl
3121 = build_template_decl (decl, current_template_parms,
3122 member_template_p);
3123 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3124 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3125 DECL_TI_TEMPLATE (decl) = new_tmpl;
3126 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3127 DECL_TEMPLATE_INFO (new_tmpl)
3128 = tree_cons (tmpl, args, NULL_TREE);
3129
3130 register_specialization (new_tmpl,
3131 most_general_template (tmpl),
3132 args,
3133 is_friend);
3134 return decl;
3135 }
3136
3137 /* Make sure the template headers we got make sense. */
3138
3139 parms = DECL_TEMPLATE_PARMS (tmpl);
3140 i = TMPL_PARMS_DEPTH (parms);
3141 if (TMPL_ARGS_DEPTH (args) != i)
3142 {
3143 error ("expected %d levels of template parms for %q#D, got %d",
3144 i, decl, TMPL_ARGS_DEPTH (args));
3145 }
3146 else
3147 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3148 {
3149 a = TMPL_ARGS_LEVEL (args, i);
3150 t = INNERMOST_TEMPLATE_PARMS (parms);
3151
3152 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3153 {
3154 if (current == decl)
3155 error ("got %d template parameters for %q#D",
3156 TREE_VEC_LENGTH (a), decl);
3157 else
3158 error ("got %d template parameters for %q#T",
3159 TREE_VEC_LENGTH (a), current);
3160 error (" but %d required", TREE_VEC_LENGTH (t));
3161 return error_mark_node;
3162 }
3163
3164 /* Perhaps we should also check that the parms are used in the
3165 appropriate qualifying scopes in the declarator? */
3166
3167 if (current == decl)
3168 current = ctx;
3169 else
3170 current = TYPE_CONTEXT (current);
3171 }
3172 }
3173
3174 DECL_TEMPLATE_RESULT (tmpl) = decl;
3175 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3176
3177 /* Push template declarations for global functions and types. Note
3178 that we do not try to push a global template friend declared in a
3179 template class; such a thing may well depend on the template
3180 parameters of the class. */
3181 if (new_template_p && !ctx
3182 && !(is_friend && template_class_depth (current_class_type) > 0))
3183 {
3184 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3185 if (tmpl == error_mark_node)
3186 return error_mark_node;
3187
3188 /* Hide template friend classes that haven't been declared yet. */
3189 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3190 {
3191 DECL_ANTICIPATED (tmpl) = 1;
3192 DECL_FRIEND_P (tmpl) = 1;
3193 }
3194 }
3195
3196 if (primary)
3197 {
3198 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3199 if (DECL_CONV_FN_P (tmpl))
3200 {
3201 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3202
3203 /* It is a conversion operator. See if the type converted to
3204 depends on innermost template operands. */
3205
3206 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3207 depth))
3208 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3209 }
3210 }
3211
3212 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3213 back to its most general template. If TMPL is a specialization,
3214 ARGS may only have the innermost set of arguments. Add the missing
3215 argument levels if necessary. */
3216 if (DECL_TEMPLATE_INFO (tmpl))
3217 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3218
3219 info = tree_cons (tmpl, args, NULL_TREE);
3220
3221 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3222 {
3223 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3224 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3225 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3226 /* Don't change the name if we've already set it up. */
3227 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3228 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3229 }
3230 else if (DECL_LANG_SPECIFIC (decl))
3231 DECL_TEMPLATE_INFO (decl) = info;
3232
3233 return DECL_TEMPLATE_RESULT (tmpl);
3234 }
3235
3236 tree
3237 push_template_decl (tree decl)
3238 {
3239 return push_template_decl_real (decl, false);
3240 }
3241
3242 /* Called when a class template TYPE is redeclared with the indicated
3243 template PARMS, e.g.:
3244
3245 template <class T> struct S;
3246 template <class T> struct S {}; */
3247
3248 void
3249 redeclare_class_template (tree type, tree parms)
3250 {
3251 tree tmpl;
3252 tree tmpl_parms;
3253 int i;
3254
3255 if (!TYPE_TEMPLATE_INFO (type))
3256 {
3257 error ("%qT is not a template type", type);
3258 return;
3259 }
3260
3261 tmpl = TYPE_TI_TEMPLATE (type);
3262 if (!PRIMARY_TEMPLATE_P (tmpl))
3263 /* The type is nested in some template class. Nothing to worry
3264 about here; there are no new template parameters for the nested
3265 type. */
3266 return;
3267
3268 if (!parms)
3269 {
3270 error ("template specifiers not specified in declaration of %qD",
3271 tmpl);
3272 return;
3273 }
3274
3275 parms = INNERMOST_TEMPLATE_PARMS (parms);
3276 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3277
3278 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3279 {
3280 error ("previous declaration %q+D", tmpl);
3281 error ("used %d template parameter(s) instead of %d",
3282 TREE_VEC_LENGTH (tmpl_parms),
3283 TREE_VEC_LENGTH (parms));
3284 return;
3285 }
3286
3287 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3288 {
3289 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3290 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3291 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3292 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3293
3294 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3295 TEMPLATE_DECL. */
3296 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3297 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3298 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3299 {
3300 error ("template parameter %q+#D", tmpl_parm);
3301 error ("redeclared here as %q#D", parm);
3302 return;
3303 }
3304
3305 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3306 {
3307 /* We have in [temp.param]:
3308
3309 A template-parameter may not be given default arguments
3310 by two different declarations in the same scope. */
3311 error ("redefinition of default argument for %q#D", parm);
3312 error ("%J original definition appeared here", tmpl_parm);
3313 return;
3314 }
3315
3316 if (parm_default != NULL_TREE)
3317 /* Update the previous template parameters (which are the ones
3318 that will really count) with the new default value. */
3319 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3320 else if (tmpl_default != NULL_TREE)
3321 /* Update the new parameters, too; they'll be used as the
3322 parameters for any members. */
3323 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3324 }
3325 }
3326
3327 /* Simplify EXPR if it is a non-dependent expression. Returns the
3328 (possibly simplified) expression. */
3329
3330 tree
3331 fold_non_dependent_expr (tree expr)
3332 {
3333 /* If we're in a template, but EXPR isn't value dependent, simplify
3334 it. We're supposed to treat:
3335
3336 template <typename T> void f(T[1 + 1]);
3337 template <typename T> void f(T[2]);
3338
3339 as two declarations of the same function, for example. */
3340 if (processing_template_decl
3341 && !type_dependent_expression_p (expr)
3342 && !value_dependent_expression_p (expr))
3343 {
3344 HOST_WIDE_INT saved_processing_template_decl;
3345
3346 saved_processing_template_decl = processing_template_decl;
3347 processing_template_decl = 0;
3348 expr = tsubst_copy_and_build (expr,
3349 /*args=*/NULL_TREE,
3350 tf_error,
3351 /*in_decl=*/NULL_TREE,
3352 /*function_p=*/false);
3353 processing_template_decl = saved_processing_template_decl;
3354 }
3355 return expr;
3356 }
3357
3358 /* EXPR is an expression which is used in a constant-expression context.
3359 For instance, it could be a VAR_DECL with a constant initializer.
3360 Extract the innest constant expression.
3361
3362 This is basically a more powerful version of
3363 integral_constant_value, which can be used also in templates where
3364 initializers can maintain a syntactic rather than semantic form
3365 (even if they are non-dependent, for access-checking purposes). */
3366
3367 static tree
3368 fold_decl_constant_value (tree expr)
3369 {
3370 tree const_expr = expr;
3371 do
3372 {
3373 expr = fold_non_dependent_expr (const_expr);
3374 const_expr = integral_constant_value (expr);
3375 }
3376 while (expr != const_expr);
3377
3378 return expr;
3379 }
3380
3381 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3382 must be a function or a pointer-to-function type, as specified
3383 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3384 and check that the resulting function has external linkage. */
3385
3386 static tree
3387 convert_nontype_argument_function (tree type, tree expr)
3388 {
3389 tree fns = expr;
3390 tree fn, fn_no_ptr;
3391
3392 fn = instantiate_type (type, fns, tf_none);
3393 if (fn == error_mark_node)
3394 return error_mark_node;
3395
3396 fn_no_ptr = fn;
3397 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3398 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3399
3400 /* [temp.arg.nontype]/1
3401
3402 A template-argument for a non-type, non-template template-parameter
3403 shall be one of:
3404 [...]
3405 -- the address of an object or function with external linkage. */
3406 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3407 {
3408 error ("%qE is not a valid template argument for type %qT "
3409 "because function %qD has not external linkage",
3410 expr, type, fn_no_ptr);
3411 return NULL_TREE;
3412 }
3413
3414 return fn;
3415 }
3416
3417 /* Attempt to convert the non-type template parameter EXPR to the
3418 indicated TYPE. If the conversion is successful, return the
3419 converted value. If the conversion is unsuccessful, return
3420 NULL_TREE if we issued an error message, or error_mark_node if we
3421 did not. We issue error messages for out-and-out bad template
3422 parameters, but not simply because the conversion failed, since we
3423 might be just trying to do argument deduction. Both TYPE and EXPR
3424 must be non-dependent.
3425
3426 The conversion follows the special rules described in
3427 [temp.arg.nontype], and it is much more strict than an implicit
3428 conversion.
3429
3430 This function is called twice for each template argument (see
3431 lookup_template_class for a more accurate description of this
3432 problem). This means that we need to handle expressions which
3433 are not valid in a C++ source, but can be created from the
3434 first call (for instance, casts to perform conversions). These
3435 hacks can go away after we fix the double coercion problem. */
3436
3437 static tree
3438 convert_nontype_argument (tree type, tree expr)
3439 {
3440 tree expr_type;
3441
3442 /* Detect immediately string literals as invalid non-type argument.
3443 This special-case is not needed for correctness (we would easily
3444 catch this later), but only to provide better diagnostic for this
3445 common user mistake. As suggested by DR 100, we do not mention
3446 linkage issues in the diagnostic as this is not the point. */
3447 if (TREE_CODE (expr) == STRING_CST)
3448 {
3449 error ("%qE is not a valid template argument for type %qT "
3450 "because string literals can never be used in this context",
3451 expr, type);
3452 return NULL_TREE;
3453 }
3454
3455 /* If we are in a template, EXPR may be non-dependent, but still
3456 have a syntactic, rather than semantic, form. For example, EXPR
3457 might be a SCOPE_REF, rather than the VAR_DECL to which the
3458 SCOPE_REF refers. Preserving the qualifying scope is necessary
3459 so that access checking can be performed when the template is
3460 instantiated -- but here we need the resolved form so that we can
3461 convert the argument. */
3462 expr = fold_non_dependent_expr (expr);
3463 expr_type = TREE_TYPE (expr);
3464
3465 /* HACK: Due to double coercion, we can get a
3466 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3467 which is the tree that we built on the first call (see
3468 below when coercing to reference to object or to reference to
3469 function). We just strip everything and get to the arg.
3470 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3471 for examples. */
3472 if (TREE_CODE (expr) == NOP_EXPR)
3473 {
3474 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3475 {
3476 /* ??? Maybe we could use convert_from_reference here, but we
3477 would need to relax its constraints because the NOP_EXPR
3478 could actually change the type to something more cv-qualified,
3479 and this is not folded by convert_from_reference. */
3480 tree addr = TREE_OPERAND (expr, 0);
3481 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3482 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3483 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3484 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3485 (TREE_TYPE (expr_type),
3486 TREE_TYPE (TREE_TYPE (addr))));
3487
3488 expr = TREE_OPERAND (addr, 0);
3489 expr_type = TREE_TYPE (expr);
3490 }
3491
3492 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3493 parameter is a pointer to object, through decay and
3494 qualification conversion. Let's strip everything. */
3495 else if (TYPE_PTROBV_P (type))
3496 {
3497 STRIP_NOPS (expr);
3498 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3499 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3500 /* Skip the ADDR_EXPR only if it is part of the decay for
3501 an array. Otherwise, it is part of the original argument
3502 in the source code. */
3503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3504 expr = TREE_OPERAND (expr, 0);
3505 expr_type = TREE_TYPE (expr);
3506 }
3507 }
3508
3509 /* [temp.arg.nontype]/5, bullet 1
3510
3511 For a non-type template-parameter of integral or enumeration type,
3512 integral promotions (_conv.prom_) and integral conversions
3513 (_conv.integral_) are applied. */
3514 if (INTEGRAL_TYPE_P (type))
3515 {
3516 if (!INTEGRAL_TYPE_P (expr_type))
3517 return error_mark_node;
3518
3519 expr = fold_decl_constant_value (expr);
3520 /* Notice that there are constant expressions like '4 % 0' which
3521 do not fold into integer constants. */
3522 if (TREE_CODE (expr) != INTEGER_CST)
3523 {
3524 error ("%qE is not a valid template argument for type %qT "
3525 "because it is a non-constant expression", expr, type);
3526 return NULL_TREE;
3527 }
3528
3529 /* At this point, an implicit conversion does what we want,
3530 because we already know that the expression is of integral
3531 type. */
3532 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3533 if (expr == error_mark_node)
3534 return error_mark_node;
3535
3536 /* Conversion was allowed: fold it to a bare integer constant. */
3537 expr = fold (expr);
3538 }
3539 /* [temp.arg.nontype]/5, bullet 2
3540
3541 For a non-type template-parameter of type pointer to object,
3542 qualification conversions (_conv.qual_) and the array-to-pointer
3543 conversion (_conv.array_) are applied. */
3544 else if (TYPE_PTROBV_P (type))
3545 {
3546 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3547
3548 A template-argument for a non-type, non-template template-parameter
3549 shall be one of: [...]
3550
3551 -- the name of a non-type template-parameter;
3552 -- the address of an object or function with external linkage, [...]
3553 expressed as "& id-expression" where the & is optional if the name
3554 refers to a function or array, or if the corresponding
3555 template-parameter is a reference.
3556
3557 Here, we do not care about functions, as they are invalid anyway
3558 for a parameter of type pointer-to-object. */
3559 bool constant_address_p =
3560 (TREE_CODE (expr) == ADDR_EXPR
3561 || TREE_CODE (expr_type) == ARRAY_TYPE
3562 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3563
3564 expr = decay_conversion (expr);
3565 if (expr == error_mark_node)
3566 return error_mark_node;
3567
3568 expr = perform_qualification_conversions (type, expr);
3569 if (expr == error_mark_node)
3570 return error_mark_node;
3571
3572 if (!constant_address_p)
3573 {
3574 error ("%qE is not a valid template argument for type %qT "
3575 "because it is not a constant pointer", expr, type);
3576 return NULL_TREE;
3577 }
3578 }
3579 /* [temp.arg.nontype]/5, bullet 3
3580
3581 For a non-type template-parameter of type reference to object, no
3582 conversions apply. The type referred to by the reference may be more
3583 cv-qualified than the (otherwise identical) type of the
3584 template-argument. The template-parameter is bound directly to the
3585 template-argument, which must be an lvalue. */
3586 else if (TYPE_REF_OBJ_P (type))
3587 {
3588 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3589 expr_type))
3590 return error_mark_node;
3591
3592 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3593 {
3594 error ("%qE is not a valid template argument for type %qT "
3595 "because of conflicts in cv-qualification", expr, type);
3596 return NULL_TREE;
3597 }
3598
3599 if (!real_lvalue_p (expr))
3600 {
3601 error ("%qE is not a valid template argument for type %qT "
3602 "because it is not a lvalue", expr, type);
3603 return NULL_TREE;
3604 }
3605
3606 /* [temp.arg.nontype]/1
3607
3608 A template-argument for a non-type, non-template template-parameter
3609 shall be one of: [...]
3610
3611 -- the address of an object or function with external linkage. */
3612 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3613 {
3614 error ("%qE is not a valid template argument for type %qT "
3615 "because object %qD has not external linkage",
3616 expr, type, expr);
3617 return NULL_TREE;
3618 }
3619
3620 expr = build_nop (type, build_address (expr));
3621 }
3622 /* [temp.arg.nontype]/5, bullet 4
3623
3624 For a non-type template-parameter of type pointer to function, only
3625 the function-to-pointer conversion (_conv.func_) is applied. If the
3626 template-argument represents a set of overloaded functions (or a
3627 pointer to such), the matching function is selected from the set
3628 (_over.over_). */
3629 else if (TYPE_PTRFN_P (type))
3630 {
3631 /* If the argument is a template-id, we might not have enough
3632 context information to decay the pointer. */
3633 if (!type_unknown_p (expr_type))
3634 {
3635 expr = decay_conversion (expr);
3636 if (expr == error_mark_node)
3637 return error_mark_node;
3638 }
3639
3640 expr = convert_nontype_argument_function (type, expr);
3641 if (!expr || expr == error_mark_node)
3642 return expr;
3643 }
3644 /* [temp.arg.nontype]/5, bullet 5
3645
3646 For a non-type template-parameter of type reference to function, no
3647 conversions apply. If the template-argument represents a set of
3648 overloaded functions, the matching function is selected from the set
3649 (_over.over_). */
3650 else if (TYPE_REFFN_P (type))
3651 {
3652 if (TREE_CODE (expr) == ADDR_EXPR)
3653 {
3654 error ("%qE is not a valid template argument for type %qT "
3655 "because it is a pointer", expr, type);
3656 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3657 return NULL_TREE;
3658 }
3659
3660 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3661 if (!expr || expr == error_mark_node)
3662 return expr;
3663
3664 expr = build_nop (type, build_address (expr));
3665 }
3666 /* [temp.arg.nontype]/5, bullet 6
3667
3668 For a non-type template-parameter of type pointer to member function,
3669 no conversions apply. If the template-argument represents a set of
3670 overloaded member functions, the matching member function is selected
3671 from the set (_over.over_). */
3672 else if (TYPE_PTRMEMFUNC_P (type))
3673 {
3674 expr = instantiate_type (type, expr, tf_none);
3675 if (expr == error_mark_node)
3676 return error_mark_node;
3677
3678 /* There is no way to disable standard conversions in
3679 resolve_address_of_overloaded_function (called by
3680 instantiate_type). It is possible that the call succeeded by
3681 converting &B::I to &D::I (where B is a base of D), so we need
3682 to reject this conversion here.
3683
3684 Actually, even if there was a way to disable standard conversions,
3685 it would still be better to reject them here so that we can
3686 provide a superior diagnostic. */
3687 if (!same_type_p (TREE_TYPE (expr), type))
3688 {
3689 /* Make sure we are just one standard conversion off. */
3690 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3691 error ("%qE is not a valid template argument for type %qT "
3692 "because it is of type %qT", expr, type,
3693 TREE_TYPE (expr));
3694 inform ("standard conversions are not allowed in this context");
3695 return NULL_TREE;
3696 }
3697 }
3698 /* [temp.arg.nontype]/5, bullet 7
3699
3700 For a non-type template-parameter of type pointer to data member,
3701 qualification conversions (_conv.qual_) are applied. */
3702 else if (TYPE_PTRMEM_P (type))
3703 {
3704 expr = perform_qualification_conversions (type, expr);
3705 if (expr == error_mark_node)
3706 return expr;
3707 }
3708 /* A template non-type parameter must be one of the above. */
3709 else
3710 gcc_unreachable ();
3711
3712 /* Sanity check: did we actually convert the argument to the
3713 right type? */
3714 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3715 return expr;
3716 }
3717
3718
3719 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3720 template template parameters. Both PARM_PARMS and ARG_PARMS are
3721 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3722 or PARM_DECL.
3723
3724 Consider the example:
3725 template <class T> class A;
3726 template<template <class U> class TT> class B;
3727
3728 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3729 the parameters to A, and OUTER_ARGS contains A. */
3730
3731 static int
3732 coerce_template_template_parms (tree parm_parms,
3733 tree arg_parms,
3734 tsubst_flags_t complain,
3735 tree in_decl,
3736 tree outer_args)
3737 {
3738 int nparms, nargs, i;
3739 tree parm, arg;
3740
3741 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3742 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3743
3744 nparms = TREE_VEC_LENGTH (parm_parms);
3745 nargs = TREE_VEC_LENGTH (arg_parms);
3746
3747 if (nargs != nparms)
3748 return 0;
3749
3750 for (i = 0; i < nparms; ++i)
3751 {
3752 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3753 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3754
3755 if (arg == NULL_TREE || arg == error_mark_node
3756 || parm == NULL_TREE || parm == error_mark_node)
3757 return 0;
3758
3759 if (TREE_CODE (arg) != TREE_CODE (parm))
3760 return 0;
3761
3762 switch (TREE_CODE (parm))
3763 {
3764 case TYPE_DECL:
3765 break;
3766
3767 case TEMPLATE_DECL:
3768 /* We encounter instantiations of templates like
3769 template <template <template <class> class> class TT>
3770 class C; */
3771 {
3772 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3773 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3774
3775 if (!coerce_template_template_parms
3776 (parmparm, argparm, complain, in_decl, outer_args))
3777 return 0;
3778 }
3779 break;
3780
3781 case PARM_DECL:
3782 /* The tsubst call is used to handle cases such as
3783
3784 template <int> class C {};
3785 template <class T, template <T> class TT> class D {};
3786 D<int, C> d;
3787
3788 i.e. the parameter list of TT depends on earlier parameters. */
3789 if (!dependent_type_p (TREE_TYPE (arg))
3790 && !same_type_p
3791 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3792 TREE_TYPE (arg)))
3793 return 0;
3794 break;
3795
3796 default:
3797 gcc_unreachable ();
3798 }
3799 }
3800 return 1;
3801 }
3802
3803 /* Convert the indicated template ARG as necessary to match the
3804 indicated template PARM. Returns the converted ARG, or
3805 error_mark_node if the conversion was unsuccessful. Error and
3806 warning messages are issued under control of COMPLAIN. This
3807 conversion is for the Ith parameter in the parameter list. ARGS is
3808 the full set of template arguments deduced so far. */
3809
3810 static tree
3811 convert_template_argument (tree parm,
3812 tree arg,
3813 tree args,
3814 tsubst_flags_t complain,
3815 int i,
3816 tree in_decl)
3817 {
3818 tree val;
3819 tree inner_args;
3820 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3821
3822 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3823
3824 if (TREE_CODE (arg) == TREE_LIST
3825 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3826 {
3827 /* The template argument was the name of some
3828 member function. That's usually
3829 invalid, but static members are OK. In any
3830 case, grab the underlying fields/functions
3831 and issue an error later if required. */
3832 arg = TREE_VALUE (arg);
3833 TREE_TYPE (arg) = unknown_type_node;
3834 }
3835
3836 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3837 requires_type = (TREE_CODE (parm) == TYPE_DECL
3838 || requires_tmpl_type);
3839
3840 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3841 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3842 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3843 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3844
3845 if (is_tmpl_type
3846 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3847 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3848 arg = TYPE_STUB_DECL (arg);
3849
3850 is_type = TYPE_P (arg) || is_tmpl_type;
3851
3852 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3853 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3854 {
3855 pedwarn ("to refer to a type member of a template parameter, "
3856 "use %<typename %E%>", arg);
3857
3858 arg = make_typename_type (TREE_OPERAND (arg, 0),
3859 TREE_OPERAND (arg, 1),
3860 typename_type,
3861 complain & tf_error);
3862 is_type = 1;
3863 }
3864 if (is_type != requires_type)
3865 {
3866 if (in_decl)
3867 {
3868 if (complain & tf_error)
3869 {
3870 error ("type/value mismatch at argument %d in template "
3871 "parameter list for %qD",
3872 i + 1, in_decl);
3873 if (is_type)
3874 error (" expected a constant of type %qT, got %qT",
3875 TREE_TYPE (parm),
3876 (is_tmpl_type ? DECL_NAME (arg) : arg));
3877 else if (requires_tmpl_type)
3878 error (" expected a class template, got %qE", arg);
3879 else
3880 error (" expected a type, got %qE", arg);
3881 }
3882 }
3883 return error_mark_node;
3884 }
3885 if (is_tmpl_type ^ requires_tmpl_type)
3886 {
3887 if (in_decl && (complain & tf_error))
3888 {
3889 error ("type/value mismatch at argument %d in template "
3890 "parameter list for %qD",
3891 i + 1, in_decl);
3892 if (is_tmpl_type)
3893 error (" expected a type, got %qT", DECL_NAME (arg));
3894 else
3895 error (" expected a class template, got %qT", arg);
3896 }
3897 return error_mark_node;
3898 }
3899
3900 if (is_type)
3901 {
3902 if (requires_tmpl_type)
3903 {
3904 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3905 /* The number of argument required is not known yet.
3906 Just accept it for now. */
3907 val = TREE_TYPE (arg);
3908 else
3909 {
3910 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3911 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3912
3913 if (coerce_template_template_parms (parmparm, argparm,
3914 complain, in_decl,
3915 inner_args))
3916 {
3917 val = arg;
3918
3919 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3920 TEMPLATE_DECL. */
3921 if (val != error_mark_node
3922 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3923 val = TREE_TYPE (val);
3924 }
3925 else
3926 {
3927 if (in_decl && (complain & tf_error))
3928 {
3929 error ("type/value mismatch at argument %d in "
3930 "template parameter list for %qD",
3931 i + 1, in_decl);
3932 error (" expected a template of type %qD, got %qD",
3933 parm, arg);
3934 }
3935
3936 val = error_mark_node;
3937 }
3938 }
3939 }
3940 else
3941 val = arg;
3942 /* We only form one instance of each template specialization.
3943 Therefore, if we use a non-canonical variant (i.e., a
3944 typedef), any future messages referring to the type will use
3945 the typedef, which is confusing if those future uses do not
3946 themselves also use the typedef. */
3947 if (TYPE_P (val))
3948 val = canonical_type_variant (val);
3949 }
3950 else
3951 {
3952 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3953
3954 if (invalid_nontype_parm_type_p (t, complain))
3955 return error_mark_node;
3956
3957 if (!uses_template_parms (arg) && !uses_template_parms (t))
3958 /* We used to call digest_init here. However, digest_init
3959 will report errors, which we don't want when complain
3960 is zero. More importantly, digest_init will try too
3961 hard to convert things: for example, `0' should not be
3962 converted to pointer type at this point according to
3963 the standard. Accepting this is not merely an
3964 extension, since deciding whether or not these
3965 conversions can occur is part of determining which
3966 function template to call, or whether a given explicit
3967 argument specification is valid. */
3968 val = convert_nontype_argument (t, arg);
3969 else
3970 val = arg;
3971
3972 if (val == NULL_TREE)
3973 val = error_mark_node;
3974 else if (val == error_mark_node && (complain & tf_error))
3975 error ("could not convert template argument %qE to %qT", arg, t);
3976 }
3977
3978 return val;
3979 }
3980
3981 /* Convert all template arguments to their appropriate types, and
3982 return a vector containing the innermost resulting template
3983 arguments. If any error occurs, return error_mark_node. Error and
3984 warning messages are issued under control of COMPLAIN.
3985
3986 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
3987 for arguments not specified in ARGS. Otherwise, if
3988 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
3989 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
3990 USE_DEFAULT_ARGS is false, then all arguments must be specified in
3991 ARGS. */
3992
3993 static tree
3994 coerce_template_parms (tree parms,
3995 tree args,
3996 tree in_decl,
3997 tsubst_flags_t complain,
3998 bool require_all_args,
3999 bool use_default_args)
4000 {
4001 int nparms, nargs, i, lost = 0;
4002 tree inner_args;
4003 tree new_args;
4004 tree new_inner_args;
4005
4006 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4007 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4008 nparms = TREE_VEC_LENGTH (parms);
4009
4010 if (nargs > nparms
4011 || (nargs < nparms
4012 && require_all_args
4013 && (!use_default_args
4014 || !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
4015 {
4016 if (complain & tf_error)
4017 {
4018 error ("wrong number of template arguments (%d, should be %d)",
4019 nargs, nparms);
4020
4021 if (in_decl)
4022 error ("provided for %q+D", in_decl);
4023 }
4024
4025 return error_mark_node;
4026 }
4027
4028 new_inner_args = make_tree_vec (nparms);
4029 new_args = add_outermost_template_args (args, new_inner_args);
4030 for (i = 0; i < nparms; i++)
4031 {
4032 tree arg;
4033 tree parm;
4034
4035 /* Get the Ith template parameter. */
4036 parm = TREE_VEC_ELT (parms, i);
4037
4038 /* Calculate the Ith argument. */
4039 if (i < nargs)
4040 arg = TREE_VEC_ELT (inner_args, i);
4041 else if (require_all_args)
4042 /* There must be a default arg in this case. */
4043 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4044 complain, in_decl);
4045 else
4046 break;
4047
4048 gcc_assert (arg);
4049 if (arg == error_mark_node)
4050 {
4051 if (complain & tf_error)
4052 error ("template argument %d is invalid", i + 1);
4053 }
4054 else
4055 arg = convert_template_argument (TREE_VALUE (parm),
4056 arg, new_args, complain, i,
4057 in_decl);
4058
4059 if (arg == error_mark_node)
4060 lost++;
4061 TREE_VEC_ELT (new_inner_args, i) = arg;
4062 }
4063
4064 if (lost)
4065 return error_mark_node;
4066
4067 return new_inner_args;
4068 }
4069
4070 /* Returns 1 if template args OT and NT are equivalent. */
4071
4072 static int
4073 template_args_equal (tree ot, tree nt)
4074 {
4075 if (nt == ot)
4076 return 1;
4077
4078 if (TREE_CODE (nt) == TREE_VEC)
4079 /* For member templates */
4080 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4081 else if (TYPE_P (nt))
4082 return TYPE_P (ot) && same_type_p (ot, nt);
4083 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4084 return 0;
4085 else
4086 return cp_tree_equal (ot, nt);
4087 }
4088
4089 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4090 of template arguments. Returns 0 otherwise. */
4091
4092 int
4093 comp_template_args (tree oldargs, tree newargs)
4094 {
4095 int i;
4096
4097 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4098 return 0;
4099
4100 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4101 {
4102 tree nt = TREE_VEC_ELT (newargs, i);
4103 tree ot = TREE_VEC_ELT (oldargs, i);
4104
4105 if (! template_args_equal (ot, nt))
4106 return 0;
4107 }
4108 return 1;
4109 }
4110
4111 /* Given class template name and parameter list, produce a user-friendly name
4112 for the instantiation. */
4113
4114 static char *
4115 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4116 {
4117 static struct obstack scratch_obstack;
4118 static char *scratch_firstobj;
4119 int i, nparms;
4120
4121 if (!scratch_firstobj)
4122 gcc_obstack_init (&scratch_obstack);
4123 else
4124 obstack_free (&scratch_obstack, scratch_firstobj);
4125 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4126
4127 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4128 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4129
4130 cat (name);
4131 ccat ('<');
4132 nparms = TREE_VEC_LENGTH (parms);
4133 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4134 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4135 for (i = 0; i < nparms; i++)
4136 {
4137 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4138 tree arg = TREE_VEC_ELT (arglist, i);
4139
4140 if (i)
4141 ccat (',');
4142
4143 if (TREE_CODE (parm) == TYPE_DECL)
4144 {
4145 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4146 continue;
4147 }
4148 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4149 {
4150 if (TREE_CODE (arg) == TEMPLATE_DECL)
4151 {
4152 /* Already substituted with real template. Just output
4153 the template name here */
4154 tree context = DECL_CONTEXT (arg);
4155 if (context)
4156 {
4157 /* The template may be defined in a namespace, or
4158 may be a member template. */
4159 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4160 || CLASS_TYPE_P (context));
4161 cat (decl_as_string (DECL_CONTEXT (arg),
4162 TFF_PLAIN_IDENTIFIER));
4163 cat ("::");
4164 }
4165 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4166 }
4167 else
4168 /* Output the parameter declaration. */
4169 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4170 continue;
4171 }
4172 else
4173 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4174
4175 /* No need to check arglist against parmlist here; we did that
4176 in coerce_template_parms, called from lookup_template_class. */
4177 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4178 }
4179 {
4180 char *bufp = obstack_next_free (&scratch_obstack);
4181 int offset = 0;
4182 while (bufp[offset - 1] == ' ')
4183 offset--;
4184 obstack_blank_fast (&scratch_obstack, offset);
4185
4186 /* B<C<char> >, not B<C<char>> */
4187 if (bufp[offset - 1] == '>')
4188 ccat (' ');
4189 }
4190 ccat ('>');
4191 ccat ('\0');
4192 return (char *) obstack_base (&scratch_obstack);
4193 }
4194
4195 static tree
4196 classtype_mangled_name (tree t)
4197 {
4198 if (CLASSTYPE_TEMPLATE_INFO (t)
4199 /* Specializations have already had their names set up in
4200 lookup_template_class. */
4201 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4202 {
4203 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4204
4205 /* For non-primary templates, the template parameters are
4206 implicit from their surrounding context. */
4207 if (PRIMARY_TEMPLATE_P (tmpl))
4208 {
4209 tree name = DECL_NAME (tmpl);
4210 char *mangled_name = mangle_class_name_for_template
4211 (IDENTIFIER_POINTER (name),
4212 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4213 CLASSTYPE_TI_ARGS (t));
4214 tree id = get_identifier (mangled_name);
4215 IDENTIFIER_TEMPLATE (id) = name;
4216 return id;
4217 }
4218 }
4219
4220 return TYPE_IDENTIFIER (t);
4221 }
4222
4223 static void
4224 add_pending_template (tree d)
4225 {
4226 tree ti = (TYPE_P (d)
4227 ? CLASSTYPE_TEMPLATE_INFO (d)
4228 : DECL_TEMPLATE_INFO (d));
4229 tree pt;
4230 int level;
4231
4232 if (TI_PENDING_TEMPLATE_FLAG (ti))
4233 return;
4234
4235 /* We are called both from instantiate_decl, where we've already had a
4236 tinst_level pushed, and instantiate_template, where we haven't.
4237 Compensate. */
4238 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4239
4240 if (level)
4241 push_tinst_level (d);
4242
4243 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4244 if (last_pending_template)
4245 TREE_CHAIN (last_pending_template) = pt;
4246 else
4247 pending_templates = pt;
4248
4249 last_pending_template = pt;
4250
4251 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4252
4253 if (level)
4254 pop_tinst_level ();
4255 }
4256
4257
4258 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4259 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4260 documentation for TEMPLATE_ID_EXPR. */
4261
4262 tree
4263 lookup_template_function (tree fns, tree arglist)
4264 {
4265 tree type;
4266
4267 if (fns == error_mark_node || arglist == error_mark_node)
4268 return error_mark_node;
4269
4270 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4271 gcc_assert (fns && (is_overloaded_fn (fns)
4272 || TREE_CODE (fns) == IDENTIFIER_NODE));
4273
4274 if (BASELINK_P (fns))
4275 {
4276 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4277 unknown_type_node,
4278 BASELINK_FUNCTIONS (fns),
4279 arglist);
4280 return fns;
4281 }
4282
4283 type = TREE_TYPE (fns);
4284 if (TREE_CODE (fns) == OVERLOAD || !type)
4285 type = unknown_type_node;
4286
4287 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4288 }
4289
4290 /* Within the scope of a template class S<T>, the name S gets bound
4291 (in build_self_reference) to a TYPE_DECL for the class, not a
4292 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4293 or one of its enclosing classes, and that type is a template,
4294 return the associated TEMPLATE_DECL. Otherwise, the original
4295 DECL is returned. */
4296
4297 tree
4298 maybe_get_template_decl_from_type_decl (tree decl)
4299 {
4300 return (decl != NULL_TREE
4301 && TREE_CODE (decl) == TYPE_DECL
4302 && DECL_ARTIFICIAL (decl)
4303 && CLASS_TYPE_P (TREE_TYPE (decl))
4304 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4305 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4306 }
4307
4308 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4309 parameters, find the desired type.
4310
4311 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4312
4313 IN_DECL, if non-NULL, is the template declaration we are trying to
4314 instantiate.
4315
4316 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4317 the class we are looking up.
4318
4319 Issue error and warning messages under control of COMPLAIN.
4320
4321 If the template class is really a local class in a template
4322 function, then the FUNCTION_CONTEXT is the function in which it is
4323 being instantiated.
4324
4325 ??? Note that this function is currently called *twice* for each
4326 template-id: the first time from the parser, while creating the
4327 incomplete type (finish_template_type), and the second type during the
4328 real instantiation (instantiate_template_class). This is surely something
4329 that we want to avoid. It also causes some problems with argument
4330 coercion (see convert_nontype_argument for more information on this). */
4331
4332 tree
4333 lookup_template_class (tree d1,
4334 tree arglist,
4335 tree in_decl,
4336 tree context,
4337 int entering_scope,
4338 tsubst_flags_t complain)
4339 {
4340 tree template = NULL_TREE, parmlist;
4341 tree t;
4342
4343 timevar_push (TV_NAME_LOOKUP);
4344
4345 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4346 {
4347 tree value = innermost_non_namespace_value (d1);
4348 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4349 template = value;
4350 else
4351 {
4352 if (context)
4353 push_decl_namespace (context);
4354 template = lookup_name (d1);
4355 template = maybe_get_template_decl_from_type_decl (template);
4356 if (context)
4357 pop_decl_namespace ();
4358 }
4359 if (template)
4360 context = DECL_CONTEXT (template);
4361 }
4362 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4363 {
4364 tree type = TREE_TYPE (d1);
4365
4366 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4367 an implicit typename for the second A. Deal with it. */
4368 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4369 type = TREE_TYPE (type);
4370
4371 if (CLASSTYPE_TEMPLATE_INFO (type))
4372 {
4373 template = CLASSTYPE_TI_TEMPLATE (type);
4374 d1 = DECL_NAME (template);
4375 }
4376 }
4377 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4378 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4379 {
4380 template = TYPE_TI_TEMPLATE (d1);
4381 d1 = DECL_NAME (template);
4382 }
4383 else if (TREE_CODE (d1) == TEMPLATE_DECL
4384 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4385 {
4386 template = d1;
4387 d1 = DECL_NAME (template);
4388 context = DECL_CONTEXT (template);
4389 }
4390
4391 /* Issue an error message if we didn't find a template. */
4392 if (! template)
4393 {
4394 if (complain & tf_error)
4395 error ("%qT is not a template", d1);
4396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4397 }
4398
4399 if (TREE_CODE (template) != TEMPLATE_DECL
4400 /* Make sure it's a user visible template, if it was named by
4401 the user. */
4402 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4403 && !PRIMARY_TEMPLATE_P (template)))
4404 {
4405 if (complain & tf_error)
4406 {
4407 error ("non-template type %qT used as a template", d1);
4408 if (in_decl)
4409 error ("for template declaration %q+D", in_decl);
4410 }
4411 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4412 }
4413
4414 complain &= ~tf_user;
4415
4416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4417 {
4418 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4419 template arguments */
4420
4421 tree parm;
4422 tree arglist2;
4423
4424 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4425
4426 /* Consider an example where a template template parameter declared as
4427
4428 template <class T, class U = std::allocator<T> > class TT
4429
4430 The template parameter level of T and U are one level larger than
4431 of TT. To proper process the default argument of U, say when an
4432 instantiation `TT<int>' is seen, we need to build the full
4433 arguments containing {int} as the innermost level. Outer levels,
4434 available when not appearing as default template argument, can be
4435 obtained from `current_template_args ()'.
4436
4437 Suppose that TT is later substituted with std::vector. The above
4438 instantiation is `TT<int, std::allocator<T> >' with TT at
4439 level 1, and T at level 2, while the template arguments at level 1
4440 becomes {std::vector} and the inner level 2 is {int}. */
4441
4442 if (current_template_parms)
4443 arglist = add_to_template_args (current_template_args (), arglist);
4444
4445 arglist2 = coerce_template_parms (parmlist, arglist, template,
4446 complain,
4447 /*require_all_args=*/true,
4448 /*use_default_args=*/true);
4449 if (arglist2 == error_mark_node
4450 || (!uses_template_parms (arglist2)
4451 && check_instantiated_args (template, arglist2, complain)))
4452 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4453
4454 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4455 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4456 }
4457 else
4458 {
4459 tree template_type = TREE_TYPE (template);
4460 tree gen_tmpl;
4461 tree type_decl;
4462 tree found = NULL_TREE;
4463 int arg_depth;
4464 int parm_depth;
4465 int is_partial_instantiation;
4466
4467 gen_tmpl = most_general_template (template);
4468 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4469 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4470 arg_depth = TMPL_ARGS_DEPTH (arglist);
4471
4472 if (arg_depth == 1 && parm_depth > 1)
4473 {
4474 /* We've been given an incomplete set of template arguments.
4475 For example, given:
4476
4477 template <class T> struct S1 {
4478 template <class U> struct S2 {};
4479 template <class U> struct S2<U*> {};
4480 };
4481
4482 we will be called with an ARGLIST of `U*', but the
4483 TEMPLATE will be `template <class T> template
4484 <class U> struct S1<T>::S2'. We must fill in the missing
4485 arguments. */
4486 arglist
4487 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4488 arglist);
4489 arg_depth = TMPL_ARGS_DEPTH (arglist);
4490 }
4491
4492 /* Now we should have enough arguments. */
4493 gcc_assert (parm_depth == arg_depth);
4494
4495 /* From here on, we're only interested in the most general
4496 template. */
4497 template = gen_tmpl;
4498
4499 /* Calculate the BOUND_ARGS. These will be the args that are
4500 actually tsubst'd into the definition to create the
4501 instantiation. */
4502 if (parm_depth > 1)
4503 {
4504 /* We have multiple levels of arguments to coerce, at once. */
4505 int i;
4506 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4507
4508 tree bound_args = make_tree_vec (parm_depth);
4509
4510 for (i = saved_depth,
4511 t = DECL_TEMPLATE_PARMS (template);
4512 i > 0 && t != NULL_TREE;
4513 --i, t = TREE_CHAIN (t))
4514 {
4515 tree a = coerce_template_parms (TREE_VALUE (t),
4516 arglist, template,
4517 complain,
4518 /*require_all_args=*/true,
4519 /*use_default_args=*/true);
4520
4521 /* Don't process further if one of the levels fails. */
4522 if (a == error_mark_node)
4523 {
4524 /* Restore the ARGLIST to its full size. */
4525 TREE_VEC_LENGTH (arglist) = saved_depth;
4526 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4527 }
4528
4529 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4530
4531 /* We temporarily reduce the length of the ARGLIST so
4532 that coerce_template_parms will see only the arguments
4533 corresponding to the template parameters it is
4534 examining. */
4535 TREE_VEC_LENGTH (arglist)--;
4536 }
4537
4538 /* Restore the ARGLIST to its full size. */
4539 TREE_VEC_LENGTH (arglist) = saved_depth;
4540
4541 arglist = bound_args;
4542 }
4543 else
4544 arglist
4545 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4546 INNERMOST_TEMPLATE_ARGS (arglist),
4547 template,
4548 complain,
4549 /*require_all_args=*/true,
4550 /*use_default_args=*/true);
4551
4552 if (arglist == error_mark_node)
4553 /* We were unable to bind the arguments. */
4554 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4555
4556 /* In the scope of a template class, explicit references to the
4557 template class refer to the type of the template, not any
4558 instantiation of it. For example, in:
4559
4560 template <class T> class C { void f(C<T>); }
4561
4562 the `C<T>' is just the same as `C'. Outside of the
4563 class, however, such a reference is an instantiation. */
4564 if (comp_template_args (TYPE_TI_ARGS (template_type),
4565 arglist))
4566 {
4567 found = template_type;
4568
4569 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4570 {
4571 tree ctx;
4572
4573 for (ctx = current_class_type;
4574 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4575 ctx = (TYPE_P (ctx)
4576 ? TYPE_CONTEXT (ctx)
4577 : DECL_CONTEXT (ctx)))
4578 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4579 goto found_ctx;
4580
4581 /* We're not in the scope of the class, so the
4582 TEMPLATE_TYPE is not the type we want after all. */
4583 found = NULL_TREE;
4584 found_ctx:;
4585 }
4586 }
4587 if (found)
4588 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4589
4590 /* If we already have this specialization, return it. */
4591 found = retrieve_specialization (template, arglist,
4592 /*class_specializations_p=*/false);
4593 if (found)
4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4595
4596 /* This type is a "partial instantiation" if any of the template
4597 arguments still involve template parameters. Note that we set
4598 IS_PARTIAL_INSTANTIATION for partial specializations as
4599 well. */
4600 is_partial_instantiation = uses_template_parms (arglist);
4601
4602 /* If the deduced arguments are invalid, then the binding
4603 failed. */
4604 if (!is_partial_instantiation
4605 && check_instantiated_args (template,
4606 INNERMOST_TEMPLATE_ARGS (arglist),
4607 complain))
4608 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4609
4610 if (!is_partial_instantiation
4611 && !PRIMARY_TEMPLATE_P (template)
4612 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4613 {
4614 found = xref_tag_from_type (TREE_TYPE (template),
4615 DECL_NAME (template),
4616 /*tag_scope=*/ts_global);
4617 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4618 }
4619
4620 context = tsubst (DECL_CONTEXT (template), arglist,
4621 complain, in_decl);
4622 if (!context)
4623 context = global_namespace;
4624
4625 /* Create the type. */
4626 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4627 {
4628 if (!is_partial_instantiation)
4629 {
4630 set_current_access_from_decl (TYPE_NAME (template_type));
4631 t = start_enum (TYPE_IDENTIFIER (template_type));
4632 }
4633 else
4634 /* We don't want to call start_enum for this type, since
4635 the values for the enumeration constants may involve
4636 template parameters. And, no one should be interested
4637 in the enumeration constants for such a type. */
4638 t = make_node (ENUMERAL_TYPE);
4639 }
4640 else
4641 {
4642 t = make_aggr_type (TREE_CODE (template_type));
4643 CLASSTYPE_DECLARED_CLASS (t)
4644 = CLASSTYPE_DECLARED_CLASS (template_type);
4645 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4646 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4647
4648 /* A local class. Make sure the decl gets registered properly. */
4649 if (context == current_function_decl)
4650 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4651 }
4652
4653 /* If we called start_enum or pushtag above, this information
4654 will already be set up. */
4655 if (!TYPE_NAME (t))
4656 {
4657 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4658
4659 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4660 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4661 TYPE_STUB_DECL (t) = type_decl;
4662 DECL_SOURCE_LOCATION (type_decl)
4663 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4664 }
4665 else
4666 type_decl = TYPE_NAME (t);
4667
4668 TREE_PRIVATE (type_decl)
4669 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4670 TREE_PROTECTED (type_decl)
4671 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4672 DECL_IN_SYSTEM_HEADER (type_decl)
4673 = DECL_IN_SYSTEM_HEADER (template);
4674 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4675 {
4676 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4677 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4678 }
4679
4680 /* Set up the template information. We have to figure out which
4681 template is the immediate parent if this is a full
4682 instantiation. */
4683 if (parm_depth == 1 || is_partial_instantiation
4684 || !PRIMARY_TEMPLATE_P (template))
4685 /* This case is easy; there are no member templates involved. */
4686 found = template;
4687 else
4688 {
4689 /* This is a full instantiation of a member template. Look
4690 for a partial instantiation of which this is an instance. */
4691
4692 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4693 found; found = TREE_CHAIN (found))
4694 {
4695 int success;
4696 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4697
4698 /* We only want partial instantiations, here, not
4699 specializations or full instantiations. */
4700 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4701 || !uses_template_parms (TREE_VALUE (found)))
4702 continue;
4703
4704 /* Temporarily reduce by one the number of levels in the
4705 ARGLIST and in FOUND so as to avoid comparing the
4706 last set of arguments. */
4707 TREE_VEC_LENGTH (arglist)--;
4708 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4709
4710 /* See if the arguments match. If they do, then TMPL is
4711 the partial instantiation we want. */
4712 success = comp_template_args (TREE_PURPOSE (found), arglist);
4713
4714 /* Restore the argument vectors to their full size. */
4715 TREE_VEC_LENGTH (arglist)++;
4716 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4717
4718 if (success)
4719 {
4720 found = tmpl;
4721 break;
4722 }
4723 }
4724
4725 if (!found)
4726 {
4727 /* There was no partial instantiation. This happens
4728 where C<T> is a member template of A<T> and it's used
4729 in something like
4730
4731 template <typename T> struct B { A<T>::C<int> m; };
4732 B<float>;
4733
4734 Create the partial instantiation.
4735 */
4736 TREE_VEC_LENGTH (arglist)--;
4737 found = tsubst (template, arglist, complain, NULL_TREE);
4738 TREE_VEC_LENGTH (arglist)++;
4739 }
4740 }
4741
4742 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4743 DECL_TEMPLATE_INSTANTIATIONS (template)
4744 = tree_cons (arglist, t,
4745 DECL_TEMPLATE_INSTANTIATIONS (template));
4746
4747 if (TREE_CODE (t) == ENUMERAL_TYPE
4748 && !is_partial_instantiation)
4749 /* Now that the type has been registered on the instantiations
4750 list, we set up the enumerators. Because the enumeration
4751 constants may involve the enumeration type itself, we make
4752 sure to register the type first, and then create the
4753 constants. That way, doing tsubst_expr for the enumeration
4754 constants won't result in recursive calls here; we'll find
4755 the instantiation and exit above. */
4756 tsubst_enum (template_type, t, arglist);
4757
4758 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4759 is set up. */
4760 if (TREE_CODE (t) != ENUMERAL_TYPE)
4761 DECL_NAME (type_decl) = classtype_mangled_name (t);
4762 if (is_partial_instantiation)
4763 /* If the type makes use of template parameters, the
4764 code that generates debugging information will crash. */
4765 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4766
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4768 }
4769 timevar_pop (TV_NAME_LOOKUP);
4770 }
4771 \f
4772 struct pair_fn_data
4773 {
4774 tree_fn_t fn;
4775 void *data;
4776 struct pointer_set_t *visited;
4777 };
4778
4779 /* Called from for_each_template_parm via walk_tree. */
4780
4781 static tree
4782 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4783 {
4784 tree t = *tp;
4785 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4786 tree_fn_t fn = pfd->fn;
4787 void *data = pfd->data;
4788
4789 if (TYPE_P (t)
4790 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4791 return error_mark_node;
4792
4793 switch (TREE_CODE (t))
4794 {
4795 case RECORD_TYPE:
4796 if (TYPE_PTRMEMFUNC_P (t))
4797 break;
4798 /* Fall through. */
4799
4800 case UNION_TYPE:
4801 case ENUMERAL_TYPE:
4802 if (!TYPE_TEMPLATE_INFO (t))
4803 *walk_subtrees = 0;
4804 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4805 fn, data, pfd->visited))
4806 return error_mark_node;
4807 break;
4808
4809 case METHOD_TYPE:
4810 /* Since we're not going to walk subtrees, we have to do this
4811 explicitly here. */
4812 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4813 pfd->visited))
4814 return error_mark_node;
4815 /* Fall through. */
4816
4817 case FUNCTION_TYPE:
4818 /* Check the return type. */
4819 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4820 return error_mark_node;
4821
4822 /* Check the parameter types. Since default arguments are not
4823 instantiated until they are needed, the TYPE_ARG_TYPES may
4824 contain expressions that involve template parameters. But,
4825 no-one should be looking at them yet. And, once they're
4826 instantiated, they don't contain template parameters, so
4827 there's no point in looking at them then, either. */
4828 {
4829 tree parm;
4830
4831 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4832 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4833 pfd->visited))
4834 return error_mark_node;
4835
4836 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4837 want walk_tree walking into them itself. */
4838 *walk_subtrees = 0;
4839 }
4840 break;
4841
4842 case TYPEOF_TYPE:
4843 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4844 pfd->visited))
4845 return error_mark_node;
4846 break;
4847
4848 case FUNCTION_DECL:
4849 case VAR_DECL:
4850 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4851 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4852 pfd->visited))
4853 return error_mark_node;
4854 /* Fall through. */
4855
4856 case PARM_DECL:
4857 case CONST_DECL:
4858 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4859 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4860 pfd->visited))
4861 return error_mark_node;
4862 if (DECL_CONTEXT (t)
4863 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4864 pfd->visited))
4865 return error_mark_node;
4866 break;
4867
4868 case BOUND_TEMPLATE_TEMPLATE_PARM:
4869 /* Record template parameters such as `T' inside `TT<T>'. */
4870 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4871 return error_mark_node;
4872 /* Fall through. */
4873
4874 case TEMPLATE_TEMPLATE_PARM:
4875 case TEMPLATE_TYPE_PARM:
4876 case TEMPLATE_PARM_INDEX:
4877 if (fn && (*fn)(t, data))
4878 return error_mark_node;
4879 else if (!fn)
4880 return error_mark_node;
4881 break;
4882
4883 case TEMPLATE_DECL:
4884 /* A template template parameter is encountered. */
4885 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4886 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4887 return error_mark_node;
4888
4889 /* Already substituted template template parameter */
4890 *walk_subtrees = 0;
4891 break;
4892
4893 case TYPENAME_TYPE:
4894 if (!fn
4895 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4896 data, pfd->visited))
4897 return error_mark_node;
4898 break;
4899
4900 case CONSTRUCTOR:
4901 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4902 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4903 (TREE_TYPE (t)), fn, data,
4904 pfd->visited))
4905 return error_mark_node;
4906 break;
4907
4908 case INDIRECT_REF:
4909 case COMPONENT_REF:
4910 /* If there's no type, then this thing must be some expression
4911 involving template parameters. */
4912 if (!fn && !TREE_TYPE (t))
4913 return error_mark_node;
4914 break;
4915
4916 case MODOP_EXPR:
4917 case CAST_EXPR:
4918 case REINTERPRET_CAST_EXPR:
4919 case CONST_CAST_EXPR:
4920 case STATIC_CAST_EXPR:
4921 case DYNAMIC_CAST_EXPR:
4922 case ARROW_EXPR:
4923 case DOTSTAR_EXPR:
4924 case TYPEID_EXPR:
4925 case PSEUDO_DTOR_EXPR:
4926 if (!fn)
4927 return error_mark_node;
4928 break;
4929
4930 case BASELINK:
4931 /* If we do not handle this case specially, we end up walking
4932 the BINFO hierarchy, which is circular, and therefore
4933 confuses walk_tree. */
4934 *walk_subtrees = 0;
4935 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4936 pfd->visited))
4937 return error_mark_node;
4938 break;
4939
4940 default:
4941 break;
4942 }
4943
4944 /* We didn't find any template parameters we liked. */
4945 return NULL_TREE;
4946 }
4947
4948 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4949 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4950 call FN with the parameter and the DATA.
4951 If FN returns nonzero, the iteration is terminated, and
4952 for_each_template_parm returns 1. Otherwise, the iteration
4953 continues. If FN never returns a nonzero value, the value
4954 returned by for_each_template_parm is 0. If FN is NULL, it is
4955 considered to be the function which always returns 1. */
4956
4957 static int
4958 for_each_template_parm (tree t, tree_fn_t fn, void* data,
4959 struct pointer_set_t *visited)
4960 {
4961 struct pair_fn_data pfd;
4962 int result;
4963
4964 /* Set up. */
4965 pfd.fn = fn;
4966 pfd.data = data;
4967
4968 /* Walk the tree. (Conceptually, we would like to walk without
4969 duplicates, but for_each_template_parm_r recursively calls
4970 for_each_template_parm, so we would need to reorganize a fair
4971 bit to use walk_tree_without_duplicates, so we keep our own
4972 visited list.) */
4973 if (visited)
4974 pfd.visited = visited;
4975 else
4976 pfd.visited = pointer_set_create ();
4977 result = walk_tree (&t,
4978 for_each_template_parm_r,
4979 &pfd,
4980 pfd.visited) != NULL_TREE;
4981
4982 /* Clean up. */
4983 if (!visited)
4984 {
4985 pointer_set_destroy (pfd.visited);
4986 pfd.visited = 0;
4987 }
4988
4989 return result;
4990 }
4991
4992 /* Returns true if T depends on any template parameter. */
4993
4994 int
4995 uses_template_parms (tree t)
4996 {
4997 bool dependent_p;
4998 int saved_processing_template_decl;
4999
5000 saved_processing_template_decl = processing_template_decl;
5001 if (!saved_processing_template_decl)
5002 processing_template_decl = 1;
5003 if (TYPE_P (t))
5004 dependent_p = dependent_type_p (t);
5005 else if (TREE_CODE (t) == TREE_VEC)
5006 dependent_p = any_dependent_template_arguments_p (t);
5007 else if (TREE_CODE (t) == TREE_LIST)
5008 dependent_p = (uses_template_parms (TREE_VALUE (t))
5009 || uses_template_parms (TREE_CHAIN (t)));
5010 else if (TREE_CODE (t) == TYPE_DECL)
5011 dependent_p = dependent_type_p (TREE_TYPE (t));
5012 else if (DECL_P (t)
5013 || EXPR_P (t)
5014 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5015 || TREE_CODE (t) == OVERLOAD
5016 || TREE_CODE (t) == BASELINK
5017 || TREE_CODE (t) == IDENTIFIER_NODE
5018 || CONSTANT_CLASS_P (t))
5019 dependent_p = (type_dependent_expression_p (t)
5020 || value_dependent_expression_p (t));
5021 else
5022 {
5023 gcc_assert (t == error_mark_node);
5024 dependent_p = false;
5025 }
5026
5027 processing_template_decl = saved_processing_template_decl;
5028
5029 return dependent_p;
5030 }
5031
5032 /* Returns true if T depends on any template parameter with level LEVEL. */
5033
5034 int
5035 uses_template_parms_level (tree t, int level)
5036 {
5037 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5038 }
5039
5040 static int tinst_depth;
5041 extern int max_tinst_depth;
5042 #ifdef GATHER_STATISTICS
5043 int depth_reached;
5044 #endif
5045 static int tinst_level_tick;
5046 static int last_template_error_tick;
5047
5048 /* We're starting to instantiate D; record the template instantiation context
5049 for diagnostics and to restore it later. */
5050
5051 static int
5052 push_tinst_level (tree d)
5053 {
5054 tree new;
5055
5056 if (tinst_depth >= max_tinst_depth)
5057 {
5058 /* If the instantiation in question still has unbound template parms,
5059 we don't really care if we can't instantiate it, so just return.
5060 This happens with base instantiation for implicit `typename'. */
5061 if (uses_template_parms (d))
5062 return 0;
5063
5064 last_template_error_tick = tinst_level_tick;
5065 error ("template instantiation depth exceeds maximum of %d (use "
5066 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5067 max_tinst_depth, d);
5068
5069 print_instantiation_context ();
5070
5071 return 0;
5072 }
5073
5074 new = make_node (TINST_LEVEL);
5075 TINST_DECL (new) = d;
5076 TINST_LOCATION (new) = input_location;
5077 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5078 TREE_CHAIN (new) = current_tinst_level;
5079 current_tinst_level = new;
5080
5081 ++tinst_depth;
5082 #ifdef GATHER_STATISTICS
5083 if (tinst_depth > depth_reached)
5084 depth_reached = tinst_depth;
5085 #endif
5086
5087 ++tinst_level_tick;
5088 return 1;
5089 }
5090
5091 /* We're done instantiating this template; return to the instantiation
5092 context. */
5093
5094 static void
5095 pop_tinst_level (void)
5096 {
5097 tree old = current_tinst_level;
5098
5099 /* Restore the filename and line number stashed away when we started
5100 this instantiation. */
5101 input_location = TINST_LOCATION (old);
5102 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5103 current_tinst_level = TREE_CHAIN (old);
5104 --tinst_depth;
5105 ++tinst_level_tick;
5106 }
5107
5108 /* We're instantiating a deferred template; restore the template
5109 instantiation context in which the instantiation was requested, which
5110 is one step out from LEVEL. */
5111
5112 static void
5113 reopen_tinst_level (tree level)
5114 {
5115 tree t;
5116
5117 tinst_depth = 0;
5118 for (t = level; t; t = TREE_CHAIN (t))
5119 ++tinst_depth;
5120
5121 current_tinst_level = level;
5122 pop_tinst_level ();
5123 }
5124
5125 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5126 vector of template arguments, as for tsubst.
5127
5128 Returns an appropriate tsubst'd friend declaration. */
5129
5130 static tree
5131 tsubst_friend_function (tree decl, tree args)
5132 {
5133 tree new_friend;
5134
5135 if (TREE_CODE (decl) == FUNCTION_DECL
5136 && DECL_TEMPLATE_INSTANTIATION (decl)
5137 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5138 /* This was a friend declared with an explicit template
5139 argument list, e.g.:
5140
5141 friend void f<>(T);
5142
5143 to indicate that f was a template instantiation, not a new
5144 function declaration. Now, we have to figure out what
5145 instantiation of what template. */
5146 {
5147 tree template_id, arglist, fns;
5148 tree new_args;
5149 tree tmpl;
5150 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5151
5152 /* Friend functions are looked up in the containing namespace scope.
5153 We must enter that scope, to avoid finding member functions of the
5154 current cless with same name. */
5155 push_nested_namespace (ns);
5156 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5157 tf_warning_or_error, NULL_TREE);
5158 pop_nested_namespace (ns);
5159 arglist = tsubst (DECL_TI_ARGS (decl), args,
5160 tf_warning_or_error, NULL_TREE);
5161 template_id = lookup_template_function (fns, arglist);
5162
5163 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5164 tmpl = determine_specialization (template_id, new_friend,
5165 &new_args,
5166 /*need_member_template=*/0,
5167 TREE_VEC_LENGTH (args));
5168 return instantiate_template (tmpl, new_args, tf_error);
5169 }
5170
5171 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5172
5173 /* The NEW_FRIEND will look like an instantiation, to the
5174 compiler, but is not an instantiation from the point of view of
5175 the language. For example, we might have had:
5176
5177 template <class T> struct S {
5178 template <class U> friend void f(T, U);
5179 };
5180
5181 Then, in S<int>, template <class U> void f(int, U) is not an
5182 instantiation of anything. */
5183 if (new_friend == error_mark_node)
5184 return error_mark_node;
5185
5186 DECL_USE_TEMPLATE (new_friend) = 0;
5187 if (TREE_CODE (decl) == TEMPLATE_DECL)
5188 {
5189 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5190 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5191 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5192 }
5193
5194 /* The mangled name for the NEW_FRIEND is incorrect. The function
5195 is not a template instantiation and should not be mangled like
5196 one. Therefore, we forget the mangling here; we'll recompute it
5197 later if we need it. */
5198 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5199 {
5200 SET_DECL_RTL (new_friend, NULL_RTX);
5201 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5202 }
5203
5204 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5205 {
5206 tree old_decl;
5207 tree new_friend_template_info;
5208 tree new_friend_result_template_info;
5209 tree ns;
5210 int new_friend_is_defn;
5211
5212 /* We must save some information from NEW_FRIEND before calling
5213 duplicate decls since that function will free NEW_FRIEND if
5214 possible. */
5215 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5216 new_friend_is_defn =
5217 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5218 (template_for_substitution (new_friend)))
5219 != NULL_TREE);
5220 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5221 {
5222 /* This declaration is a `primary' template. */
5223 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5224
5225 new_friend_result_template_info
5226 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5227 }
5228 else
5229 new_friend_result_template_info = NULL_TREE;
5230
5231 /* Inside pushdecl_namespace_level, we will push into the
5232 current namespace. However, the friend function should go
5233 into the namespace of the template. */
5234 ns = decl_namespace_context (new_friend);
5235 push_nested_namespace (ns);
5236 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5237 pop_nested_namespace (ns);
5238
5239 if (old_decl != new_friend)
5240 {
5241 /* This new friend declaration matched an existing
5242 declaration. For example, given:
5243
5244 template <class T> void f(T);
5245 template <class U> class C {
5246 template <class T> friend void f(T) {}
5247 };
5248
5249 the friend declaration actually provides the definition
5250 of `f', once C has been instantiated for some type. So,
5251 old_decl will be the out-of-class template declaration,
5252 while new_friend is the in-class definition.
5253
5254 But, if `f' was called before this point, the
5255 instantiation of `f' will have DECL_TI_ARGS corresponding
5256 to `T' but not to `U', references to which might appear
5257 in the definition of `f'. Previously, the most general
5258 template for an instantiation of `f' was the out-of-class
5259 version; now it is the in-class version. Therefore, we
5260 run through all specialization of `f', adding to their
5261 DECL_TI_ARGS appropriately. In particular, they need a
5262 new set of outer arguments, corresponding to the
5263 arguments for this class instantiation.
5264
5265 The same situation can arise with something like this:
5266
5267 friend void f(int);
5268 template <class T> class C {
5269 friend void f(T) {}
5270 };
5271
5272 when `C<int>' is instantiated. Now, `f(int)' is defined
5273 in the class. */
5274
5275 if (!new_friend_is_defn)
5276 /* On the other hand, if the in-class declaration does
5277 *not* provide a definition, then we don't want to alter
5278 existing definitions. We can just leave everything
5279 alone. */
5280 ;
5281 else
5282 {
5283 /* Overwrite whatever template info was there before, if
5284 any, with the new template information pertaining to
5285 the declaration. */
5286 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5287
5288 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5289 reregister_specialization (new_friend,
5290 most_general_template (old_decl),
5291 old_decl);
5292 else
5293 {
5294 tree t;
5295 tree new_friend_args;
5296
5297 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5298 = new_friend_result_template_info;
5299
5300 new_friend_args = TI_ARGS (new_friend_template_info);
5301 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5302 t != NULL_TREE;
5303 t = TREE_CHAIN (t))
5304 {
5305 tree spec = TREE_VALUE (t);
5306
5307 DECL_TI_ARGS (spec)
5308 = add_outermost_template_args (new_friend_args,
5309 DECL_TI_ARGS (spec));
5310 }
5311
5312 /* Now, since specializations are always supposed to
5313 hang off of the most general template, we must move
5314 them. */
5315 t = most_general_template (old_decl);
5316 if (t != old_decl)
5317 {
5318 DECL_TEMPLATE_SPECIALIZATIONS (t)
5319 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5320 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5321 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5322 }
5323 }
5324 }
5325
5326 /* The information from NEW_FRIEND has been merged into OLD_DECL
5327 by duplicate_decls. */
5328 new_friend = old_decl;
5329 }
5330 }
5331 else
5332 {
5333 tree context = DECL_CONTEXT (new_friend);
5334 bool dependent_p;
5335
5336 /* In the code
5337 template <class T> class C {
5338 template <class U> friend void C1<U>::f (); // case 1
5339 friend void C2<T>::f (); // case 2
5340 };
5341 we only need to make sure CONTEXT is a complete type for
5342 case 2. To distinguish between the two cases, we note that
5343 CONTEXT of case 1 remains dependent type after tsubst while
5344 this isn't true for case 2. */
5345 ++processing_template_decl;
5346 dependent_p = dependent_type_p (context);
5347 --processing_template_decl;
5348
5349 if (!dependent_p
5350 && !complete_type_or_else (context, NULL_TREE))
5351 return error_mark_node;
5352
5353 if (COMPLETE_TYPE_P (context))
5354 {
5355 /* Check to see that the declaration is really present, and,
5356 possibly obtain an improved declaration. */
5357 tree fn = check_classfn (context,
5358 new_friend, NULL_TREE);
5359
5360 if (fn)
5361 new_friend = fn;
5362 }
5363 }
5364
5365 return new_friend;
5366 }
5367
5368 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5369 template arguments, as for tsubst.
5370
5371 Returns an appropriate tsubst'd friend type or error_mark_node on
5372 failure. */
5373
5374 static tree
5375 tsubst_friend_class (tree friend_tmpl, tree args)
5376 {
5377 tree friend_type;
5378 tree tmpl;
5379 tree context;
5380
5381 context = DECL_CONTEXT (friend_tmpl);
5382
5383 if (context)
5384 {
5385 if (TREE_CODE (context) == NAMESPACE_DECL)
5386 push_nested_namespace (context);
5387 else
5388 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5389 }
5390
5391 /* First, we look for a class template. */
5392 tmpl = lookup_name (DECL_NAME (friend_tmpl));
5393
5394 /* But, if we don't find one, it might be because we're in a
5395 situation like this:
5396
5397 template <class T>
5398 struct S {
5399 template <class U>
5400 friend struct S;
5401 };
5402
5403 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5404 for `S<int>', not the TEMPLATE_DECL. */
5405 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5406 {
5407 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5408 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5409 }
5410
5411 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5412 {
5413 /* The friend template has already been declared. Just
5414 check to see that the declarations match, and install any new
5415 default parameters. We must tsubst the default parameters,
5416 of course. We only need the innermost template parameters
5417 because that is all that redeclare_class_template will look
5418 at. */
5419 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5420 > TMPL_ARGS_DEPTH (args))
5421 {
5422 tree parms;
5423 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5424 args, tf_warning_or_error);
5425 redeclare_class_template (TREE_TYPE (tmpl), parms);
5426 }
5427
5428 friend_type = TREE_TYPE (tmpl);
5429 }
5430 else
5431 {
5432 /* The friend template has not already been declared. In this
5433 case, the instantiation of the template class will cause the
5434 injection of this template into the global scope. */
5435 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5436
5437 /* The new TMPL is not an instantiation of anything, so we
5438 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5439 the new type because that is supposed to be the corresponding
5440 template decl, i.e., TMPL. */
5441 DECL_USE_TEMPLATE (tmpl) = 0;
5442 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5443 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5444 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5445 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5446
5447 /* Inject this template into the global scope. */
5448 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5449 }
5450
5451 if (context)
5452 {
5453 if (TREE_CODE (context) == NAMESPACE_DECL)
5454 pop_nested_namespace (context);
5455 else
5456 pop_nested_class ();
5457 }
5458
5459 return friend_type;
5460 }
5461
5462 /* Returns zero if TYPE cannot be completed later due to circularity.
5463 Otherwise returns one. */
5464
5465 static int
5466 can_complete_type_without_circularity (tree type)
5467 {
5468 if (type == NULL_TREE || type == error_mark_node)
5469 return 0;
5470 else if (COMPLETE_TYPE_P (type))
5471 return 1;
5472 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5473 return can_complete_type_without_circularity (TREE_TYPE (type));
5474 else if (CLASS_TYPE_P (type)
5475 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5476 return 0;
5477 else
5478 return 1;
5479 }
5480
5481 tree
5482 instantiate_class_template (tree type)
5483 {
5484 tree template, args, pattern, t, member;
5485 tree typedecl;
5486 tree pbinfo;
5487 tree base_list;
5488
5489 if (type == error_mark_node)
5490 return error_mark_node;
5491
5492 if (TYPE_BEING_DEFINED (type)
5493 || COMPLETE_TYPE_P (type)
5494 || dependent_type_p (type))
5495 return type;
5496
5497 /* Figure out which template is being instantiated. */
5498 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5499 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5500
5501 /* Determine what specialization of the original template to
5502 instantiate. */
5503 t = most_specialized_class (type, template);
5504 if (t == error_mark_node)
5505 {
5506 TYPE_BEING_DEFINED (type) = 1;
5507 return error_mark_node;
5508 }
5509 else if (t)
5510 {
5511 /* This TYPE is actually an instantiation of a partial
5512 specialization. We replace the innermost set of ARGS with
5513 the arguments appropriate for substitution. For example,
5514 given:
5515
5516 template <class T> struct S {};
5517 template <class T> struct S<T*> {};
5518
5519 and supposing that we are instantiating S<int*>, ARGS will
5520 presently be {int*} -- but we need {int}. */
5521 pattern = TREE_TYPE (t);
5522 args = TREE_PURPOSE (t);
5523 }
5524 else
5525 {
5526 pattern = TREE_TYPE (template);
5527 args = CLASSTYPE_TI_ARGS (type);
5528 }
5529
5530 /* If the template we're instantiating is incomplete, then clearly
5531 there's nothing we can do. */
5532 if (!COMPLETE_TYPE_P (pattern))
5533 return type;
5534
5535 /* If we've recursively instantiated too many templates, stop. */
5536 if (! push_tinst_level (type))
5537 return type;
5538
5539 /* Now we're really doing the instantiation. Mark the type as in
5540 the process of being defined. */
5541 TYPE_BEING_DEFINED (type) = 1;
5542
5543 /* We may be in the middle of deferred access check. Disable
5544 it now. */
5545 push_deferring_access_checks (dk_no_deferred);
5546
5547 push_to_top_level ();
5548
5549 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5550
5551 /* Set the input location to the template definition. This is needed
5552 if tsubsting causes an error. */
5553 typedecl = TYPE_MAIN_DECL (type);
5554 input_location = DECL_SOURCE_LOCATION (typedecl);
5555 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5556
5557 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5558 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5559 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5560 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5561 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5562 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5563 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5564 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5565 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5566 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5567 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5568 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5569 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5570 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5571 if (ANON_AGGR_TYPE_P (pattern))
5572 SET_ANON_AGGR_TYPE_P (type);
5573 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5574 {
5575 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5576 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5577 }
5578
5579 pbinfo = TYPE_BINFO (pattern);
5580
5581 /* We should never instantiate a nested class before its enclosing
5582 class; we need to look up the nested class by name before we can
5583 instantiate it, and that lookup should instantiate the enclosing
5584 class. */
5585 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5586 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5587 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5588
5589 base_list = NULL_TREE;
5590 if (BINFO_N_BASE_BINFOS (pbinfo))
5591 {
5592 tree pbase_binfo;
5593 tree context = TYPE_CONTEXT (type);
5594 tree pushed_scope;
5595 int i;
5596
5597 /* We must enter the scope containing the type, as that is where
5598 the accessibility of types named in dependent bases are
5599 looked up from. */
5600 pushed_scope = push_scope (context ? context : global_namespace);
5601
5602 /* Substitute into each of the bases to determine the actual
5603 basetypes. */
5604 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5605 {
5606 tree base;
5607 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5608
5609 /* Substitute to figure out the base class. */
5610 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5611 if (base == error_mark_node)
5612 continue;
5613
5614 base_list = tree_cons (access, base, base_list);
5615 if (BINFO_VIRTUAL_P (pbase_binfo))
5616 TREE_TYPE (base_list) = integer_type_node;
5617 }
5618
5619 /* The list is now in reverse order; correct that. */
5620 base_list = nreverse (base_list);
5621
5622 if (pushed_scope)
5623 pop_scope (pushed_scope);
5624 }
5625 /* Now call xref_basetypes to set up all the base-class
5626 information. */
5627 xref_basetypes (type, base_list);
5628
5629
5630 /* Now that our base classes are set up, enter the scope of the
5631 class, so that name lookups into base classes, etc. will work
5632 correctly. This is precisely analogous to what we do in
5633 begin_class_definition when defining an ordinary non-template
5634 class. */
5635 pushclass (type);
5636
5637 /* Now members are processed in the order of declaration. */
5638 for (member = CLASSTYPE_DECL_LIST (pattern);
5639 member; member = TREE_CHAIN (member))
5640 {
5641 tree t = TREE_VALUE (member);
5642
5643 if (TREE_PURPOSE (member))
5644 {
5645 if (TYPE_P (t))
5646 {
5647 /* Build new CLASSTYPE_NESTED_UTDS. */
5648
5649 tree newtag;
5650 bool class_template_p;
5651
5652 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5653 && TYPE_LANG_SPECIFIC (t)
5654 && CLASSTYPE_IS_TEMPLATE (t));
5655 /* If the member is a class template, then -- even after
5656 substitution -- there may be dependent types in the
5657 template argument list for the class. We increment
5658 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5659 that function will assume that no types are dependent
5660 when outside of a template. */
5661 if (class_template_p)
5662 ++processing_template_decl;
5663 newtag = tsubst (t, args, tf_error, NULL_TREE);
5664 if (class_template_p)
5665 --processing_template_decl;
5666 if (newtag == error_mark_node)
5667 continue;
5668
5669 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5670 {
5671 tree name = TYPE_IDENTIFIER (t);
5672
5673 if (class_template_p)
5674 /* Unfortunately, lookup_template_class sets
5675 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5676 instantiation (i.e., for the type of a member
5677 template class nested within a template class.)
5678 This behavior is required for
5679 maybe_process_partial_specialization to work
5680 correctly, but is not accurate in this case;
5681 the TAG is not an instantiation of anything.
5682 (The corresponding TEMPLATE_DECL is an
5683 instantiation, but the TYPE is not.) */
5684 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5685
5686 /* Now, we call pushtag to put this NEWTAG into the scope of
5687 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5688 pushtag calling push_template_decl. We don't have to do
5689 this for enums because it will already have been done in
5690 tsubst_enum. */
5691 if (name)
5692 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5693 pushtag (name, newtag, /*tag_scope=*/ts_current);
5694 }
5695 }
5696 else if (TREE_CODE (t) == FUNCTION_DECL
5697 || DECL_FUNCTION_TEMPLATE_P (t))
5698 {
5699 /* Build new TYPE_METHODS. */
5700 tree r;
5701
5702 if (TREE_CODE (t) == TEMPLATE_DECL)
5703 ++processing_template_decl;
5704 r = tsubst (t, args, tf_error, NULL_TREE);
5705 if (TREE_CODE (t) == TEMPLATE_DECL)
5706 --processing_template_decl;
5707 set_current_access_from_decl (r);
5708 grok_special_member_properties (r);
5709 finish_member_declaration (r);
5710 }
5711 else
5712 {
5713 /* Build new TYPE_FIELDS. */
5714
5715 if (TREE_CODE (t) != CONST_DECL)
5716 {
5717 tree r;
5718
5719 /* The the file and line for this declaration, to
5720 assist in error message reporting. Since we
5721 called push_tinst_level above, we don't need to
5722 restore these. */
5723 input_location = DECL_SOURCE_LOCATION (t);
5724
5725 if (TREE_CODE (t) == TEMPLATE_DECL)
5726 ++processing_template_decl;
5727 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5728 if (TREE_CODE (t) == TEMPLATE_DECL)
5729 --processing_template_decl;
5730 if (TREE_CODE (r) == VAR_DECL)
5731 {
5732 /* In [temp.inst]:
5733
5734 [t]he initialization (and any associated
5735 side-effects) of a static data member does
5736 not occur unless the static data member is
5737 itself used in a way that requires the
5738 definition of the static data member to
5739 exist.
5740
5741 Therefore, we do not substitute into the
5742 initialized for the static data member here. */
5743 finish_static_data_member_decl
5744 (r,
5745 /*init=*/NULL_TREE,
5746 /*init_const_expr_p=*/false,
5747 /*asmspec_tree=*/NULL_TREE,
5748 /*flags=*/0);
5749 if (DECL_INITIALIZED_IN_CLASS_P (r))
5750 check_static_variable_definition (r, TREE_TYPE (r));
5751 }
5752 else if (TREE_CODE (r) == FIELD_DECL)
5753 {
5754 /* Determine whether R has a valid type and can be
5755 completed later. If R is invalid, then it is
5756 replaced by error_mark_node so that it will not be
5757 added to TYPE_FIELDS. */
5758 tree rtype = TREE_TYPE (r);
5759 if (can_complete_type_without_circularity (rtype))
5760 complete_type (rtype);
5761
5762 if (!COMPLETE_TYPE_P (rtype))
5763 {
5764 cxx_incomplete_type_error (r, rtype);
5765 r = error_mark_node;
5766 }
5767 }
5768
5769 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5770 such a thing will already have been added to the field
5771 list by tsubst_enum in finish_member_declaration in the
5772 CLASSTYPE_NESTED_UTDS case above. */
5773 if (!(TREE_CODE (r) == TYPE_DECL
5774 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5775 && DECL_ARTIFICIAL (r)))
5776 {
5777 set_current_access_from_decl (r);
5778 finish_member_declaration (r);
5779 }
5780 }
5781 }
5782 }
5783 else
5784 {
5785 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5786 {
5787 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5788
5789 tree friend_type = t;
5790 bool adjust_processing_template_decl = false;
5791
5792 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5793 {
5794 /* template <class T> friend class C; */
5795 friend_type = tsubst_friend_class (friend_type, args);
5796 adjust_processing_template_decl = true;
5797 }
5798 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5799 {
5800 /* template <class T> friend class C::D; */
5801 friend_type = tsubst (friend_type, args,
5802 tf_warning_or_error, NULL_TREE);
5803 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5804 friend_type = TREE_TYPE (friend_type);
5805 adjust_processing_template_decl = true;
5806 }
5807 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5808 {
5809 /* This could be either
5810
5811 friend class T::C;
5812
5813 when dependent_type_p is false or
5814
5815 template <class U> friend class T::C;
5816
5817 otherwise. */
5818 friend_type = tsubst (friend_type, args,
5819 tf_warning_or_error, NULL_TREE);
5820 /* Bump processing_template_decl for correct
5821 dependent_type_p calculation. */
5822 ++processing_template_decl;
5823 if (dependent_type_p (friend_type))
5824 adjust_processing_template_decl = true;
5825 --processing_template_decl;
5826 }
5827 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5828 && hidden_name_p (TYPE_NAME (friend_type)))
5829 {
5830 /* friend class C;
5831
5832 where C hasn't been declared yet. Let's lookup name
5833 from namespace scope directly, bypassing any name that
5834 come from dependent base class. */
5835 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5836
5837 /* The call to xref_tag_from_type does injection for friend
5838 classes. */
5839 push_nested_namespace (ns);
5840 friend_type =
5841 xref_tag_from_type (friend_type, NULL_TREE,
5842 /*tag_scope=*/ts_current);
5843 pop_nested_namespace (ns);
5844 }
5845 else if (uses_template_parms (friend_type))
5846 /* friend class C<T>; */
5847 friend_type = tsubst (friend_type, args,
5848 tf_warning_or_error, NULL_TREE);
5849 /* Otherwise it's
5850
5851 friend class C;
5852
5853 where C is already declared or
5854
5855 friend class C<int>;
5856
5857 We don't have to do anything in these cases. */
5858
5859 if (adjust_processing_template_decl)
5860 /* Trick make_friend_class into realizing that the friend
5861 we're adding is a template, not an ordinary class. It's
5862 important that we use make_friend_class since it will
5863 perform some error-checking and output cross-reference
5864 information. */
5865 ++processing_template_decl;
5866
5867 if (friend_type != error_mark_node)
5868 make_friend_class (type, friend_type, /*complain=*/false);
5869
5870 if (adjust_processing_template_decl)
5871 --processing_template_decl;
5872 }
5873 else
5874 {
5875 /* Build new DECL_FRIENDLIST. */
5876 tree r;
5877
5878 /* The the file and line for this declaration, to
5879 assist in error message reporting. Since we
5880 called push_tinst_level above, we don't need to
5881 restore these. */
5882 input_location = DECL_SOURCE_LOCATION (t);
5883
5884 if (TREE_CODE (t) == TEMPLATE_DECL)
5885 {
5886 ++processing_template_decl;
5887 push_deferring_access_checks (dk_no_check);
5888 }
5889
5890 r = tsubst_friend_function (t, args);
5891 add_friend (type, r, /*complain=*/false);
5892 if (TREE_CODE (t) == TEMPLATE_DECL)
5893 {
5894 pop_deferring_access_checks ();
5895 --processing_template_decl;
5896 }
5897 }
5898 }
5899 }
5900
5901 /* Set the file and line number information to whatever is given for
5902 the class itself. This puts error messages involving generated
5903 implicit functions at a predictable point, and the same point
5904 that would be used for non-template classes. */
5905 input_location = DECL_SOURCE_LOCATION (typedecl);
5906
5907 unreverse_member_declarations (type);
5908 finish_struct_1 (type);
5909 TYPE_BEING_DEFINED (type) = 0;
5910
5911 /* Now that the class is complete, instantiate default arguments for
5912 any member functions. We don't do this earlier because the
5913 default arguments may reference members of the class. */
5914 if (!PRIMARY_TEMPLATE_P (template))
5915 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5916 if (TREE_CODE (t) == FUNCTION_DECL
5917 /* Implicitly generated member functions will not have template
5918 information; they are not instantiations, but instead are
5919 created "fresh" for each instantiation. */
5920 && DECL_TEMPLATE_INFO (t))
5921 tsubst_default_arguments (t);
5922
5923 popclass ();
5924 pop_from_top_level ();
5925 pop_deferring_access_checks ();
5926 pop_tinst_level ();
5927
5928 /* The vtable for a template class can be emitted in any translation
5929 unit in which the class is instantiated. When there is no key
5930 method, however, finish_struct_1 will already have added TYPE to
5931 the keyed_classes list. */
5932 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5933 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5934
5935 return type;
5936 }
5937
5938 static tree
5939 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5940 {
5941 tree r;
5942
5943 if (!t)
5944 r = t;
5945 else if (TYPE_P (t))
5946 r = tsubst (t, args, complain, in_decl);
5947 else
5948 {
5949 r = tsubst_expr (t, args, complain, in_decl);
5950 r = fold_non_dependent_expr (r);
5951 }
5952 return r;
5953 }
5954
5955 /* Substitute ARGS into the vector or list of template arguments T. */
5956
5957 static tree
5958 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5959 {
5960 int len = TREE_VEC_LENGTH (t);
5961 int need_new = 0, i;
5962 tree *elts = (tree *) alloca (len * sizeof (tree));
5963
5964 for (i = 0; i < len; i++)
5965 {
5966 tree orig_arg = TREE_VEC_ELT (t, i);
5967 tree new_arg;
5968
5969 if (TREE_CODE (orig_arg) == TREE_VEC)
5970 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5971 else
5972 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5973
5974 if (new_arg == error_mark_node)
5975 return error_mark_node;
5976
5977 elts[i] = new_arg;
5978 if (new_arg != orig_arg)
5979 need_new = 1;
5980 }
5981
5982 if (!need_new)
5983 return t;
5984
5985 t = make_tree_vec (len);
5986 for (i = 0; i < len; i++)
5987 TREE_VEC_ELT (t, i) = elts[i];
5988
5989 return t;
5990 }
5991
5992 /* Return the result of substituting ARGS into the template parameters
5993 given by PARMS. If there are m levels of ARGS and m + n levels of
5994 PARMS, then the result will contain n levels of PARMS. For
5995 example, if PARMS is `template <class T> template <class U>
5996 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5997 result will be `template <int*, double, class V>'. */
5998
5999 static tree
6000 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6001 {
6002 tree r = NULL_TREE;
6003 tree* new_parms;
6004
6005 /* When substituting into a template, we must set
6006 PROCESSING_TEMPLATE_DECL as the template parameters may be
6007 dependent if they are based on one-another, and the dependency
6008 predicates are short-circuit outside of templates. */
6009 ++processing_template_decl;
6010
6011 for (new_parms = &r;
6012 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6013 new_parms = &(TREE_CHAIN (*new_parms)),
6014 parms = TREE_CHAIN (parms))
6015 {
6016 tree new_vec =
6017 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6018 int i;
6019
6020 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6021 {
6022 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6023 tree default_value = TREE_PURPOSE (tuple);
6024 tree parm_decl = TREE_VALUE (tuple);
6025
6026 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6027 if (TREE_CODE (parm_decl) == PARM_DECL
6028 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6029 parm_decl = error_mark_node;
6030 default_value = tsubst_template_arg (default_value, args,
6031 complain, NULL_TREE);
6032
6033 tuple = build_tree_list (default_value, parm_decl);
6034 TREE_VEC_ELT (new_vec, i) = tuple;
6035 }
6036
6037 *new_parms =
6038 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6039 - TMPL_ARGS_DEPTH (args)),
6040 new_vec, NULL_TREE);
6041 }
6042
6043 --processing_template_decl;
6044
6045 return r;
6046 }
6047
6048 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6049 type T. If T is not an aggregate or enumeration type, it is
6050 handled as if by tsubst. IN_DECL is as for tsubst. If
6051 ENTERING_SCOPE is nonzero, T is the context for a template which
6052 we are presently tsubst'ing. Return the substituted value. */
6053
6054 static tree
6055 tsubst_aggr_type (tree t,
6056 tree args,
6057 tsubst_flags_t complain,
6058 tree in_decl,
6059 int entering_scope)
6060 {
6061 if (t == NULL_TREE)
6062 return NULL_TREE;
6063
6064 switch (TREE_CODE (t))
6065 {
6066 case RECORD_TYPE:
6067 if (TYPE_PTRMEMFUNC_P (t))
6068 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6069
6070 /* Else fall through. */
6071 case ENUMERAL_TYPE:
6072 case UNION_TYPE:
6073 if (TYPE_TEMPLATE_INFO (t))
6074 {
6075 tree argvec;
6076 tree context;
6077 tree r;
6078 bool saved_skip_evaluation;
6079
6080 /* In "sizeof(X<I>)" we need to evaluate "I". */
6081 saved_skip_evaluation = skip_evaluation;
6082 skip_evaluation = false;
6083
6084 /* First, determine the context for the type we are looking
6085 up. */
6086 context = TYPE_CONTEXT (t);
6087 if (context)
6088 context = tsubst_aggr_type (context, args, complain,
6089 in_decl, /*entering_scope=*/1);
6090
6091 /* Then, figure out what arguments are appropriate for the
6092 type we are trying to find. For example, given:
6093
6094 template <class T> struct S;
6095 template <class T, class U> void f(T, U) { S<U> su; }
6096
6097 and supposing that we are instantiating f<int, double>,
6098 then our ARGS will be {int, double}, but, when looking up
6099 S we only want {double}. */
6100 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6101 complain, in_decl);
6102 if (argvec == error_mark_node)
6103 r = error_mark_node;
6104 else
6105 {
6106 r = lookup_template_class (t, argvec, in_decl, context,
6107 entering_scope, complain);
6108 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6109 }
6110
6111 skip_evaluation = saved_skip_evaluation;
6112
6113 return r;
6114 }
6115 else
6116 /* This is not a template type, so there's nothing to do. */
6117 return t;
6118
6119 default:
6120 return tsubst (t, args, complain, in_decl);
6121 }
6122 }
6123
6124 /* Substitute into the default argument ARG (a default argument for
6125 FN), which has the indicated TYPE. */
6126
6127 tree
6128 tsubst_default_argument (tree fn, tree type, tree arg)
6129 {
6130 tree saved_class_ptr = NULL_TREE;
6131 tree saved_class_ref = NULL_TREE;
6132
6133 /* This default argument came from a template. Instantiate the
6134 default argument here, not in tsubst. In the case of
6135 something like:
6136
6137 template <class T>
6138 struct S {
6139 static T t();
6140 void f(T = t());
6141 };
6142
6143 we must be careful to do name lookup in the scope of S<T>,
6144 rather than in the current class. */
6145 push_access_scope (fn);
6146 /* The "this" pointer is not valid in a default argument. */
6147 if (cfun)
6148 {
6149 saved_class_ptr = current_class_ptr;
6150 cp_function_chain->x_current_class_ptr = NULL_TREE;
6151 saved_class_ref = current_class_ref;
6152 cp_function_chain->x_current_class_ref = NULL_TREE;
6153 }
6154
6155 push_deferring_access_checks(dk_no_deferred);
6156 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6157 tf_warning_or_error, NULL_TREE);
6158 pop_deferring_access_checks();
6159
6160 /* Restore the "this" pointer. */
6161 if (cfun)
6162 {
6163 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6164 cp_function_chain->x_current_class_ref = saved_class_ref;
6165 }
6166
6167 pop_access_scope (fn);
6168
6169 /* Make sure the default argument is reasonable. */
6170 arg = check_default_argument (type, arg);
6171
6172 return arg;
6173 }
6174
6175 /* Substitute into all the default arguments for FN. */
6176
6177 static void
6178 tsubst_default_arguments (tree fn)
6179 {
6180 tree arg;
6181 tree tmpl_args;
6182
6183 tmpl_args = DECL_TI_ARGS (fn);
6184
6185 /* If this function is not yet instantiated, we certainly don't need
6186 its default arguments. */
6187 if (uses_template_parms (tmpl_args))
6188 return;
6189
6190 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6191 arg;
6192 arg = TREE_CHAIN (arg))
6193 if (TREE_PURPOSE (arg))
6194 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6195 TREE_VALUE (arg),
6196 TREE_PURPOSE (arg));
6197 }
6198
6199 /* Substitute the ARGS into the T, which is a _DECL. Return the
6200 result of the substitution. Issue error and warning messages under
6201 control of COMPLAIN. */
6202
6203 static tree
6204 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6205 {
6206 location_t saved_loc;
6207 tree r = NULL_TREE;
6208 tree in_decl = t;
6209
6210 /* Set the filename and linenumber to improve error-reporting. */
6211 saved_loc = input_location;
6212 input_location = DECL_SOURCE_LOCATION (t);
6213
6214 switch (TREE_CODE (t))
6215 {
6216 case TEMPLATE_DECL:
6217 {
6218 /* We can get here when processing a member function template,
6219 member class template, and template template parameter of
6220 a template class. */
6221 tree decl = DECL_TEMPLATE_RESULT (t);
6222 tree spec;
6223 tree tmpl_args;
6224 tree full_args;
6225
6226 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6227 {
6228 /* Template template parameter is treated here. */
6229 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6230 if (new_type == error_mark_node)
6231 return error_mark_node;
6232
6233 r = copy_decl (t);
6234 TREE_CHAIN (r) = NULL_TREE;
6235 TREE_TYPE (r) = new_type;
6236 DECL_TEMPLATE_RESULT (r)
6237 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6238 DECL_TEMPLATE_PARMS (r)
6239 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6240 complain);
6241 TYPE_NAME (new_type) = r;
6242 break;
6243 }
6244
6245 /* We might already have an instance of this template.
6246 The ARGS are for the surrounding class type, so the
6247 full args contain the tsubst'd args for the context,
6248 plus the innermost args from the template decl. */
6249 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6250 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6251 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6252 /* Because this is a template, the arguments will still be
6253 dependent, even after substitution. If
6254 PROCESSING_TEMPLATE_DECL is not set, the dependency
6255 predicates will short-circuit. */
6256 ++processing_template_decl;
6257 full_args = tsubst_template_args (tmpl_args, args,
6258 complain, in_decl);
6259 --processing_template_decl;
6260 if (full_args == error_mark_node)
6261 return error_mark_node;
6262
6263 /* tsubst_template_args doesn't copy the vector if
6264 nothing changed. But, *something* should have
6265 changed. */
6266 gcc_assert (full_args != tmpl_args);
6267
6268 spec = retrieve_specialization (t, full_args,
6269 /*class_specializations_p=*/true);
6270 if (spec != NULL_TREE)
6271 {
6272 r = spec;
6273 break;
6274 }
6275
6276 /* Make a new template decl. It will be similar to the
6277 original, but will record the current template arguments.
6278 We also create a new function declaration, which is just
6279 like the old one, but points to this new template, rather
6280 than the old one. */
6281 r = copy_decl (t);
6282 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6283 TREE_CHAIN (r) = NULL_TREE;
6284
6285 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6286
6287 if (TREE_CODE (decl) == TYPE_DECL)
6288 {
6289 tree new_type;
6290 ++processing_template_decl;
6291 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6292 --processing_template_decl;
6293 if (new_type == error_mark_node)
6294 return error_mark_node;
6295
6296 TREE_TYPE (r) = new_type;
6297 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6298 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6299 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6300 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6301 }
6302 else
6303 {
6304 tree new_decl;
6305 ++processing_template_decl;
6306 new_decl = tsubst (decl, args, complain, in_decl);
6307 --processing_template_decl;
6308 if (new_decl == error_mark_node)
6309 return error_mark_node;
6310
6311 DECL_TEMPLATE_RESULT (r) = new_decl;
6312 DECL_TI_TEMPLATE (new_decl) = r;
6313 TREE_TYPE (r) = TREE_TYPE (new_decl);
6314 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6315 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6316 }
6317
6318 SET_DECL_IMPLICIT_INSTANTIATION (r);
6319 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6320 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6321
6322 /* The template parameters for this new template are all the
6323 template parameters for the old template, except the
6324 outermost level of parameters. */
6325 DECL_TEMPLATE_PARMS (r)
6326 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6327 complain);
6328
6329 if (PRIMARY_TEMPLATE_P (t))
6330 DECL_PRIMARY_TEMPLATE (r) = r;
6331
6332 if (TREE_CODE (decl) != TYPE_DECL)
6333 /* Record this non-type partial instantiation. */
6334 register_specialization (r, t,
6335 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6336 false);
6337 }
6338 break;
6339
6340 case FUNCTION_DECL:
6341 {
6342 tree ctx;
6343 tree argvec = NULL_TREE;
6344 tree *friends;
6345 tree gen_tmpl;
6346 tree type;
6347 int member;
6348 int args_depth;
6349 int parms_depth;
6350
6351 /* Nobody should be tsubst'ing into non-template functions. */
6352 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6353
6354 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6355 {
6356 tree spec;
6357 bool dependent_p;
6358
6359 /* If T is not dependent, just return it. We have to
6360 increment PROCESSING_TEMPLATE_DECL because
6361 value_dependent_expression_p assumes that nothing is
6362 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6363 ++processing_template_decl;
6364 dependent_p = value_dependent_expression_p (t);
6365 --processing_template_decl;
6366 if (!dependent_p)
6367 return t;
6368
6369 /* Calculate the most general template of which R is a
6370 specialization, and the complete set of arguments used to
6371 specialize R. */
6372 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6373 argvec = tsubst_template_args (DECL_TI_ARGS
6374 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6375 args, complain, in_decl);
6376
6377 /* Check to see if we already have this specialization. */
6378 spec = retrieve_specialization (gen_tmpl, argvec,
6379 /*class_specializations_p=*/false);
6380
6381 if (spec)
6382 {
6383 r = spec;
6384 break;
6385 }
6386
6387 /* We can see more levels of arguments than parameters if
6388 there was a specialization of a member template, like
6389 this:
6390
6391 template <class T> struct S { template <class U> void f(); }
6392 template <> template <class U> void S<int>::f(U);
6393
6394 Here, we'll be substituting into the specialization,
6395 because that's where we can find the code we actually
6396 want to generate, but we'll have enough arguments for
6397 the most general template.
6398
6399 We also deal with the peculiar case:
6400
6401 template <class T> struct S {
6402 template <class U> friend void f();
6403 };
6404 template <class U> void f() {}
6405 template S<int>;
6406 template void f<double>();
6407
6408 Here, the ARGS for the instantiation of will be {int,
6409 double}. But, we only need as many ARGS as there are
6410 levels of template parameters in CODE_PATTERN. We are
6411 careful not to get fooled into reducing the ARGS in
6412 situations like:
6413
6414 template <class T> struct S { template <class U> void f(U); }
6415 template <class T> template <> void S<T>::f(int) {}
6416
6417 which we can spot because the pattern will be a
6418 specialization in this case. */
6419 args_depth = TMPL_ARGS_DEPTH (args);
6420 parms_depth =
6421 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6422 if (args_depth > parms_depth
6423 && !DECL_TEMPLATE_SPECIALIZATION (t))
6424 args = get_innermost_template_args (args, parms_depth);
6425 }
6426 else
6427 {
6428 /* This special case arises when we have something like this:
6429
6430 template <class T> struct S {
6431 friend void f<int>(int, double);
6432 };
6433
6434 Here, the DECL_TI_TEMPLATE for the friend declaration
6435 will be an IDENTIFIER_NODE. We are being called from
6436 tsubst_friend_function, and we want only to create a
6437 new decl (R) with appropriate types so that we can call
6438 determine_specialization. */
6439 gen_tmpl = NULL_TREE;
6440 }
6441
6442 if (DECL_CLASS_SCOPE_P (t))
6443 {
6444 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6445 member = 2;
6446 else
6447 member = 1;
6448 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6449 complain, t, /*entering_scope=*/1);
6450 }
6451 else
6452 {
6453 member = 0;
6454 ctx = DECL_CONTEXT (t);
6455 }
6456 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6457 if (type == error_mark_node)
6458 return error_mark_node;
6459
6460 /* We do NOT check for matching decls pushed separately at this
6461 point, as they may not represent instantiations of this
6462 template, and in any case are considered separate under the
6463 discrete model. */
6464 r = copy_decl (t);
6465 DECL_USE_TEMPLATE (r) = 0;
6466 TREE_TYPE (r) = type;
6467 /* Clear out the mangled name and RTL for the instantiation. */
6468 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6469 SET_DECL_RTL (r, NULL_RTX);
6470 DECL_INITIAL (r) = NULL_TREE;
6471 DECL_CONTEXT (r) = ctx;
6472
6473 if (member && DECL_CONV_FN_P (r))
6474 /* Type-conversion operator. Reconstruct the name, in
6475 case it's the name of one of the template's parameters. */
6476 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6477
6478 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6479 complain, t);
6480 DECL_RESULT (r) = NULL_TREE;
6481
6482 TREE_STATIC (r) = 0;
6483 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6484 DECL_EXTERNAL (r) = 1;
6485 /* If this is an instantiation of a function with internal
6486 linkage, we already know what object file linkage will be
6487 assigned to the instantiation. */
6488 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6489 DECL_DEFER_OUTPUT (r) = 0;
6490 TREE_CHAIN (r) = NULL_TREE;
6491 DECL_PENDING_INLINE_INFO (r) = 0;
6492 DECL_PENDING_INLINE_P (r) = 0;
6493 DECL_SAVED_TREE (r) = NULL_TREE;
6494 TREE_USED (r) = 0;
6495 if (DECL_CLONED_FUNCTION (r))
6496 {
6497 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6498 args, complain, t);
6499 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6500 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6501 }
6502
6503 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6504 this in the special friend case mentioned above where
6505 GEN_TMPL is NULL. */
6506 if (gen_tmpl)
6507 {
6508 DECL_TEMPLATE_INFO (r)
6509 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6510 SET_DECL_IMPLICIT_INSTANTIATION (r);
6511 register_specialization (r, gen_tmpl, argvec, false);
6512
6513 /* We're not supposed to instantiate default arguments
6514 until they are called, for a template. But, for a
6515 declaration like:
6516
6517 template <class T> void f ()
6518 { extern void g(int i = T()); }
6519
6520 we should do the substitution when the template is
6521 instantiated. We handle the member function case in
6522 instantiate_class_template since the default arguments
6523 might refer to other members of the class. */
6524 if (!member
6525 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6526 && !uses_template_parms (argvec))
6527 tsubst_default_arguments (r);
6528 }
6529 else
6530 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6531
6532 /* Copy the list of befriending classes. */
6533 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6534 *friends;
6535 friends = &TREE_CHAIN (*friends))
6536 {
6537 *friends = copy_node (*friends);
6538 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6539 args, complain,
6540 in_decl);
6541 }
6542
6543 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6544 {
6545 maybe_retrofit_in_chrg (r);
6546 if (DECL_CONSTRUCTOR_P (r))
6547 grok_ctor_properties (ctx, r);
6548 /* If this is an instantiation of a member template, clone it.
6549 If it isn't, that'll be handled by
6550 clone_constructors_and_destructors. */
6551 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6552 clone_function_decl (r, /*update_method_vec_p=*/0);
6553 }
6554 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6555 grok_op_properties (r, (complain & tf_error) != 0);
6556
6557 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6558 SET_DECL_FRIEND_CONTEXT (r,
6559 tsubst (DECL_FRIEND_CONTEXT (t),
6560 args, complain, in_decl));
6561 }
6562 break;
6563
6564 case PARM_DECL:
6565 {
6566 tree type;
6567
6568 r = copy_node (t);
6569 if (DECL_TEMPLATE_PARM_P (t))
6570 SET_DECL_TEMPLATE_PARM_P (r);
6571
6572 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6573 type = type_decays_to (type);
6574 TREE_TYPE (r) = type;
6575 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6576
6577 if (DECL_INITIAL (r))
6578 {
6579 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6580 DECL_INITIAL (r) = TREE_TYPE (r);
6581 else
6582 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6583 complain, in_decl);
6584 }
6585
6586 DECL_CONTEXT (r) = NULL_TREE;
6587
6588 if (!DECL_TEMPLATE_PARM_P (r))
6589 DECL_ARG_TYPE (r) = type_passed_as (type);
6590 if (TREE_CHAIN (t))
6591 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6592 complain, TREE_CHAIN (t));
6593 }
6594 break;
6595
6596 case FIELD_DECL:
6597 {
6598 tree type;
6599
6600 r = copy_decl (t);
6601 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6602 if (type == error_mark_node)
6603 return error_mark_node;
6604 TREE_TYPE (r) = type;
6605 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6606
6607 /* We don't have to set DECL_CONTEXT here; it is set by
6608 finish_member_declaration. */
6609 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6610 complain, in_decl);
6611 TREE_CHAIN (r) = NULL_TREE;
6612 if (VOID_TYPE_P (type))
6613 error ("instantiation of %q+D as type %qT", r, type);
6614 }
6615 break;
6616
6617 case USING_DECL:
6618 /* We reach here only for member using decls. */
6619 if (DECL_DEPENDENT_P (t))
6620 {
6621 r = do_class_using_decl
6622 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6623 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6624 if (!r)
6625 r = error_mark_node;
6626 }
6627 else
6628 {
6629 r = copy_node (t);
6630 TREE_CHAIN (r) = NULL_TREE;
6631 }
6632 break;
6633
6634 case TYPE_DECL:
6635 case VAR_DECL:
6636 {
6637 tree argvec = NULL_TREE;
6638 tree gen_tmpl = NULL_TREE;
6639 tree spec;
6640 tree tmpl = NULL_TREE;
6641 tree ctx;
6642 tree type = NULL_TREE;
6643 int local_p;
6644
6645 if (TREE_CODE (t) == TYPE_DECL)
6646 {
6647 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6648 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6649 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6650 {
6651 /* If this is the canonical decl, we don't have to
6652 mess with instantiations, and often we can't (for
6653 typename, template type parms and such). Note that
6654 TYPE_NAME is not correct for the above test if
6655 we've copied the type for a typedef. */
6656 r = TYPE_NAME (type);
6657 break;
6658 }
6659 }
6660
6661 /* Assume this is a non-local variable. */
6662 local_p = 0;
6663
6664 if (TYPE_P (CP_DECL_CONTEXT (t)))
6665 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6666 complain,
6667 in_decl, /*entering_scope=*/1);
6668 else if (DECL_NAMESPACE_SCOPE_P (t))
6669 ctx = DECL_CONTEXT (t);
6670 else
6671 {
6672 /* Subsequent calls to pushdecl will fill this in. */
6673 ctx = NULL_TREE;
6674 local_p = 1;
6675 }
6676
6677 /* Check to see if we already have this specialization. */
6678 if (!local_p)
6679 {
6680 tmpl = DECL_TI_TEMPLATE (t);
6681 gen_tmpl = most_general_template (tmpl);
6682 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6683 spec = retrieve_specialization (gen_tmpl, argvec,
6684 /*class_specializations_p=*/false);
6685 }
6686 else
6687 spec = retrieve_local_specialization (t);
6688
6689 if (spec)
6690 {
6691 r = spec;
6692 break;
6693 }
6694
6695 r = copy_decl (t);
6696 if (TREE_CODE (r) == VAR_DECL)
6697 {
6698 /* Even if the original location is out of scope, the
6699 newly substituted one is not. */
6700 DECL_DEAD_FOR_LOCAL (r) = 0;
6701 DECL_INITIALIZED_P (r) = 0;
6702 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6703 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6704 if (type == error_mark_node)
6705 return error_mark_node;
6706 type = complete_type (type);
6707 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6708 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6709 type = check_var_type (DECL_NAME (r), type);
6710
6711 if (DECL_HAS_VALUE_EXPR_P (t))
6712 {
6713 tree ve = DECL_VALUE_EXPR (t);
6714 ve = tsubst_expr (ve, args, complain, in_decl);
6715 SET_DECL_VALUE_EXPR (r, ve);
6716 }
6717 }
6718 else if (DECL_SELF_REFERENCE_P (t))
6719 SET_DECL_SELF_REFERENCE_P (r);
6720 TREE_TYPE (r) = type;
6721 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6722 DECL_CONTEXT (r) = ctx;
6723 /* Clear out the mangled name and RTL for the instantiation. */
6724 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6725 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6726 SET_DECL_RTL (r, NULL_RTX);
6727 /* The initializer must not be expanded until it is required;
6728 see [temp.inst]. */
6729 DECL_INITIAL (r) = NULL_TREE;
6730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6731 SET_DECL_RTL (r, NULL_RTX);
6732 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6733
6734 if (!local_p)
6735 {
6736 /* A static data member declaration is always marked
6737 external when it is declared in-class, even if an
6738 initializer is present. We mimic the non-template
6739 processing here. */
6740 DECL_EXTERNAL (r) = 1;
6741
6742 register_specialization (r, gen_tmpl, argvec, false);
6743 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6744 SET_DECL_IMPLICIT_INSTANTIATION (r);
6745 }
6746 else
6747 register_local_specialization (r, t);
6748
6749 TREE_CHAIN (r) = NULL_TREE;
6750 layout_decl (r, 0);
6751 }
6752 break;
6753
6754 default:
6755 gcc_unreachable ();
6756 }
6757
6758 /* Restore the file and line information. */
6759 input_location = saved_loc;
6760
6761 return r;
6762 }
6763
6764 /* Substitute into the ARG_TYPES of a function type. */
6765
6766 static tree
6767 tsubst_arg_types (tree arg_types,
6768 tree args,
6769 tsubst_flags_t complain,
6770 tree in_decl)
6771 {
6772 tree remaining_arg_types;
6773 tree type;
6774 tree default_arg;
6775 tree result = NULL_TREE;
6776
6777 if (!arg_types || arg_types == void_list_node)
6778 return arg_types;
6779
6780 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6781 args, complain, in_decl);
6782 if (remaining_arg_types == error_mark_node)
6783 return error_mark_node;
6784
6785 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6786 if (type == error_mark_node)
6787 return error_mark_node;
6788 if (VOID_TYPE_P (type))
6789 {
6790 if (complain & tf_error)
6791 {
6792 error ("invalid parameter type %qT", type);
6793 if (in_decl)
6794 error ("in declaration %q+D", in_decl);
6795 }
6796 return error_mark_node;
6797 }
6798
6799 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6800 top-level qualifiers as required. */
6801 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6802
6803 /* We do not substitute into default arguments here. The standard
6804 mandates that they be instantiated only when needed, which is
6805 done in build_over_call. */
6806 default_arg = TREE_PURPOSE (arg_types);
6807
6808 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
6809 {
6810 /* We've instantiated a template before its default arguments
6811 have been parsed. This can happen for a nested template
6812 class, and is not an error unless we require the default
6813 argument in a call of this function. */
6814 result = tree_cons (default_arg, type, remaining_arg_types);
6815 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
6816 }
6817 else
6818 result = hash_tree_cons (default_arg, type, remaining_arg_types);
6819
6820 return result;
6821 }
6822
6823 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6824 *not* handle the exception-specification for FNTYPE, because the
6825 initial substitution of explicitly provided template parameters
6826 during argument deduction forbids substitution into the
6827 exception-specification:
6828
6829 [temp.deduct]
6830
6831 All references in the function type of the function template to the
6832 corresponding template parameters are replaced by the specified tem-
6833 plate argument values. If a substitution in a template parameter or
6834 in the function type of the function template results in an invalid
6835 type, type deduction fails. [Note: The equivalent substitution in
6836 exception specifications is done only when the function is instanti-
6837 ated, at which point a program is ill-formed if the substitution
6838 results in an invalid type.] */
6839
6840 static tree
6841 tsubst_function_type (tree t,
6842 tree args,
6843 tsubst_flags_t complain,
6844 tree in_decl)
6845 {
6846 tree return_type;
6847 tree arg_types;
6848 tree fntype;
6849
6850 /* The TYPE_CONTEXT is not used for function/method types. */
6851 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6852
6853 /* Substitute the return type. */
6854 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6855 if (return_type == error_mark_node)
6856 return error_mark_node;
6857 /* The standard does not presently indicate that creation of a
6858 function type with an invalid return type is a deduction failure.
6859 However, that is clearly analogous to creating an array of "void"
6860 or a reference to a reference. This is core issue #486. */
6861 if (TREE_CODE (return_type) == ARRAY_TYPE
6862 || TREE_CODE (return_type) == FUNCTION_TYPE)
6863 {
6864 if (complain & tf_error)
6865 {
6866 if (TREE_CODE (return_type) == ARRAY_TYPE)
6867 error ("function returning an array");
6868 else
6869 error ("function returning a function");
6870 }
6871 return error_mark_node;
6872 }
6873
6874 /* Substitute the argument types. */
6875 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6876 complain, in_decl);
6877 if (arg_types == error_mark_node)
6878 return error_mark_node;
6879
6880 /* Construct a new type node and return it. */
6881 if (TREE_CODE (t) == FUNCTION_TYPE)
6882 fntype = build_function_type (return_type, arg_types);
6883 else
6884 {
6885 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6886 if (! IS_AGGR_TYPE (r))
6887 {
6888 /* [temp.deduct]
6889
6890 Type deduction may fail for any of the following
6891 reasons:
6892
6893 -- Attempting to create "pointer to member of T" when T
6894 is not a class type. */
6895 if (complain & tf_error)
6896 error ("creating pointer to member function of non-class type %qT",
6897 r);
6898 return error_mark_node;
6899 }
6900
6901 fntype = build_method_type_directly (r, return_type,
6902 TREE_CHAIN (arg_types));
6903 }
6904 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6905 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6906
6907 return fntype;
6908 }
6909
6910 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
6911 ARGS into that specification, and return the substituted
6912 specification. If there is no specification, return NULL_TREE. */
6913
6914 static tree
6915 tsubst_exception_specification (tree fntype,
6916 tree args,
6917 tsubst_flags_t complain,
6918 tree in_decl)
6919 {
6920 tree specs;
6921 tree new_specs;
6922
6923 specs = TYPE_RAISES_EXCEPTIONS (fntype);
6924 new_specs = NULL_TREE;
6925 if (specs)
6926 {
6927 if (! TREE_VALUE (specs))
6928 new_specs = specs;
6929 else
6930 while (specs)
6931 {
6932 tree spec;
6933 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
6934 if (spec == error_mark_node)
6935 return spec;
6936 new_specs = add_exception_specifier (new_specs, spec, complain);
6937 specs = TREE_CHAIN (specs);
6938 }
6939 }
6940 return new_specs;
6941 }
6942
6943 /* Take the tree structure T and replace template parameters used
6944 therein with the argument vector ARGS. IN_DECL is an associated
6945 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6946 Issue error and warning messages under control of COMPLAIN. Note
6947 that we must be relatively non-tolerant of extensions here, in
6948 order to preserve conformance; if we allow substitutions that
6949 should not be allowed, we may allow argument deductions that should
6950 not succeed, and therefore report ambiguous overload situations
6951 where there are none. In theory, we could allow the substitution,
6952 but indicate that it should have failed, and allow our caller to
6953 make sure that the right thing happens, but we don't try to do this
6954 yet.
6955
6956 This function is used for dealing with types, decls and the like;
6957 for expressions, use tsubst_expr or tsubst_copy. */
6958
6959 static tree
6960 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6961 {
6962 tree type, r;
6963
6964 if (t == NULL_TREE || t == error_mark_node
6965 || t == integer_type_node
6966 || t == void_type_node
6967 || t == char_type_node
6968 || t == unknown_type_node
6969 || TREE_CODE (t) == NAMESPACE_DECL)
6970 return t;
6971
6972 if (DECL_P (t))
6973 return tsubst_decl (t, args, complain);
6974
6975 if (TREE_CODE (t) == IDENTIFIER_NODE)
6976 type = IDENTIFIER_TYPE_VALUE (t);
6977 else
6978 type = TREE_TYPE (t);
6979
6980 gcc_assert (type != unknown_type_node);
6981
6982 if (type
6983 && TREE_CODE (t) != TYPENAME_TYPE
6984 && TREE_CODE (t) != IDENTIFIER_NODE
6985 && TREE_CODE (t) != FUNCTION_TYPE
6986 && TREE_CODE (t) != METHOD_TYPE)
6987 type = tsubst (type, args, complain, in_decl);
6988 if (type == error_mark_node)
6989 return error_mark_node;
6990
6991 switch (TREE_CODE (t))
6992 {
6993 case RECORD_TYPE:
6994 case UNION_TYPE:
6995 case ENUMERAL_TYPE:
6996 return tsubst_aggr_type (t, args, complain, in_decl,
6997 /*entering_scope=*/0);
6998
6999 case ERROR_MARK:
7000 case IDENTIFIER_NODE:
7001 case VOID_TYPE:
7002 case REAL_TYPE:
7003 case COMPLEX_TYPE:
7004 case VECTOR_TYPE:
7005 case BOOLEAN_TYPE:
7006 case INTEGER_CST:
7007 case REAL_CST:
7008 case STRING_CST:
7009 return t;
7010
7011 case INTEGER_TYPE:
7012 if (t == integer_type_node)
7013 return t;
7014
7015 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7016 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7017 return t;
7018
7019 {
7020 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7021
7022 /* The array dimension behaves like a non-type template arg,
7023 in that we want to fold it as much as possible. */
7024 max = tsubst_template_arg (omax, args, complain, in_decl);
7025 max = fold_decl_constant_value (max);
7026
7027 /* [temp.deduct]
7028
7029 Type deduction may fail for any of the following
7030 reasons:
7031
7032 Attempting to create an array with a size that is
7033 zero or negative. */
7034 if (integer_zerop (max) && !(complain & tf_error))
7035 /* We must fail if performing argument deduction (as
7036 indicated by the state of complain), so that
7037 another substitution can be found. */
7038 return error_mark_node;
7039
7040 else if (TREE_CODE (max) == INTEGER_CST
7041 && INT_CST_LT (max, integer_zero_node))
7042 {
7043 if (complain & tf_error)
7044 error ("creating array with negative size (%qE)", max);
7045
7046 return error_mark_node;
7047 }
7048
7049 return compute_array_index_type (NULL_TREE, max);
7050 }
7051
7052 case TEMPLATE_TYPE_PARM:
7053 case TEMPLATE_TEMPLATE_PARM:
7054 case BOUND_TEMPLATE_TEMPLATE_PARM:
7055 case TEMPLATE_PARM_INDEX:
7056 {
7057 int idx;
7058 int level;
7059 int levels;
7060 tree arg = NULL_TREE;
7061
7062 r = NULL_TREE;
7063
7064 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7065 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7066 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7067 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7068 {
7069 idx = TEMPLATE_TYPE_IDX (t);
7070 level = TEMPLATE_TYPE_LEVEL (t);
7071 }
7072 else
7073 {
7074 idx = TEMPLATE_PARM_IDX (t);
7075 level = TEMPLATE_PARM_LEVEL (t);
7076 }
7077
7078 levels = TMPL_ARGS_DEPTH (args);
7079 if (level <= levels)
7080 arg = TMPL_ARG (args, level, idx);
7081
7082 if (arg == error_mark_node)
7083 return error_mark_node;
7084 else if (arg != NULL_TREE)
7085 {
7086 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7087 {
7088 gcc_assert (TYPE_P (arg));
7089 return cp_build_qualified_type_real
7090 (arg, cp_type_quals (arg) | cp_type_quals (t),
7091 complain | tf_ignore_bad_quals);
7092 }
7093 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7094 {
7095 /* We are processing a type constructed from a
7096 template template parameter. */
7097 tree argvec = tsubst (TYPE_TI_ARGS (t),
7098 args, complain, in_decl);
7099 if (argvec == error_mark_node)
7100 return error_mark_node;
7101
7102 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7103 are resolving nested-types in the signature of a
7104 member function templates. Otherwise ARG is a
7105 TEMPLATE_DECL and is the real template to be
7106 instantiated. */
7107 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7108 arg = TYPE_NAME (arg);
7109
7110 r = lookup_template_class (arg,
7111 argvec, in_decl,
7112 DECL_CONTEXT (arg),
7113 /*entering_scope=*/0,
7114 complain);
7115 return cp_build_qualified_type_real
7116 (r, TYPE_QUALS (t), complain);
7117 }
7118 else
7119 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7120 return arg;
7121 }
7122
7123 if (level == 1)
7124 /* This can happen during the attempted tsubst'ing in
7125 unify. This means that we don't yet have any information
7126 about the template parameter in question. */
7127 return t;
7128
7129 /* If we get here, we must have been looking at a parm for a
7130 more deeply nested template. Make a new version of this
7131 template parameter, but with a lower level. */
7132 switch (TREE_CODE (t))
7133 {
7134 case TEMPLATE_TYPE_PARM:
7135 case TEMPLATE_TEMPLATE_PARM:
7136 case BOUND_TEMPLATE_TEMPLATE_PARM:
7137 if (cp_type_quals (t))
7138 {
7139 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7140 r = cp_build_qualified_type_real
7141 (r, cp_type_quals (t),
7142 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7143 ? tf_ignore_bad_quals : 0));
7144 }
7145 else
7146 {
7147 r = copy_type (t);
7148 TEMPLATE_TYPE_PARM_INDEX (r)
7149 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7150 r, levels);
7151 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7152 TYPE_MAIN_VARIANT (r) = r;
7153 TYPE_POINTER_TO (r) = NULL_TREE;
7154 TYPE_REFERENCE_TO (r) = NULL_TREE;
7155
7156 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7157 {
7158 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7159 complain, in_decl);
7160 if (argvec == error_mark_node)
7161 return error_mark_node;
7162
7163 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7164 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7165 }
7166 }
7167 break;
7168
7169 case TEMPLATE_PARM_INDEX:
7170 r = reduce_template_parm_level (t, type, levels);
7171 break;
7172
7173 default:
7174 gcc_unreachable ();
7175 }
7176
7177 return r;
7178 }
7179
7180 case TREE_LIST:
7181 {
7182 tree purpose, value, chain;
7183
7184 if (t == void_list_node)
7185 return t;
7186
7187 purpose = TREE_PURPOSE (t);
7188 if (purpose)
7189 {
7190 purpose = tsubst (purpose, args, complain, in_decl);
7191 if (purpose == error_mark_node)
7192 return error_mark_node;
7193 }
7194 value = TREE_VALUE (t);
7195 if (value)
7196 {
7197 value = tsubst (value, args, complain, in_decl);
7198 if (value == error_mark_node)
7199 return error_mark_node;
7200 }
7201 chain = TREE_CHAIN (t);
7202 if (chain && chain != void_type_node)
7203 {
7204 chain = tsubst (chain, args, complain, in_decl);
7205 if (chain == error_mark_node)
7206 return error_mark_node;
7207 }
7208 if (purpose == TREE_PURPOSE (t)
7209 && value == TREE_VALUE (t)
7210 && chain == TREE_CHAIN (t))
7211 return t;
7212 return hash_tree_cons (purpose, value, chain);
7213 }
7214
7215 case TREE_BINFO:
7216 /* We should never be tsubsting a binfo. */
7217 gcc_unreachable ();
7218
7219 case TREE_VEC:
7220 /* A vector of template arguments. */
7221 gcc_assert (!type);
7222 return tsubst_template_args (t, args, complain, in_decl);
7223
7224 case POINTER_TYPE:
7225 case REFERENCE_TYPE:
7226 {
7227 enum tree_code code;
7228
7229 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7230 return t;
7231
7232 code = TREE_CODE (t);
7233
7234
7235 /* [temp.deduct]
7236
7237 Type deduction may fail for any of the following
7238 reasons:
7239
7240 -- Attempting to create a pointer to reference type.
7241 -- Attempting to create a reference to a reference type or
7242 a reference to void. */
7243 if (TREE_CODE (type) == REFERENCE_TYPE
7244 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7245 {
7246 static location_t last_loc;
7247
7248 /* We keep track of the last time we issued this error
7249 message to avoid spewing a ton of messages during a
7250 single bad template instantiation. */
7251 if (complain & tf_error
7252 #ifdef USE_MAPPED_LOCATION
7253 && last_loc != input_location
7254 #else
7255 && (last_loc.line != input_line
7256 || last_loc.file != input_filename)
7257 #endif
7258 )
7259 {
7260 if (TREE_CODE (type) == VOID_TYPE)
7261 error ("forming reference to void");
7262 else
7263 error ("forming %s to reference type %qT",
7264 (code == POINTER_TYPE) ? "pointer" : "reference",
7265 type);
7266 last_loc = input_location;
7267 }
7268
7269 return error_mark_node;
7270 }
7271 else if (code == POINTER_TYPE)
7272 {
7273 r = build_pointer_type (type);
7274 if (TREE_CODE (type) == METHOD_TYPE)
7275 r = build_ptrmemfunc_type (r);
7276 }
7277 else
7278 r = build_reference_type (type);
7279 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7280
7281 if (r != error_mark_node)
7282 /* Will this ever be needed for TYPE_..._TO values? */
7283 layout_type (r);
7284
7285 return r;
7286 }
7287 case OFFSET_TYPE:
7288 {
7289 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7290 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7291 {
7292 /* [temp.deduct]
7293
7294 Type deduction may fail for any of the following
7295 reasons:
7296
7297 -- Attempting to create "pointer to member of T" when T
7298 is not a class type. */
7299 if (complain & tf_error)
7300 error ("creating pointer to member of non-class type %qT", r);
7301 return error_mark_node;
7302 }
7303 if (TREE_CODE (type) == REFERENCE_TYPE)
7304 {
7305 if (complain & tf_error)
7306 error ("creating pointer to member reference type %qT", type);
7307 return error_mark_node;
7308 }
7309 if (TREE_CODE (type) == VOID_TYPE)
7310 {
7311 if (complain & tf_error)
7312 error ("creating pointer to member of type void");
7313 return error_mark_node;
7314 }
7315 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7316 if (TREE_CODE (type) == FUNCTION_TYPE)
7317 {
7318 /* The type of the implicit object parameter gets its
7319 cv-qualifiers from the FUNCTION_TYPE. */
7320 tree method_type;
7321 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7322 cp_type_quals (type));
7323 tree memptr;
7324 method_type = build_method_type_directly (this_type,
7325 TREE_TYPE (type),
7326 TYPE_ARG_TYPES (type));
7327 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7328 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7329 complain);
7330 }
7331 else
7332 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7333 TYPE_QUALS (t),
7334 complain);
7335 }
7336 case FUNCTION_TYPE:
7337 case METHOD_TYPE:
7338 {
7339 tree fntype;
7340 tree specs;
7341 fntype = tsubst_function_type (t, args, complain, in_decl);
7342 if (fntype == error_mark_node)
7343 return error_mark_node;
7344
7345 /* Substitute the exception specification. */
7346 specs = tsubst_exception_specification (t, args, complain,
7347 in_decl);
7348 if (specs == error_mark_node)
7349 return error_mark_node;
7350 if (specs)
7351 fntype = build_exception_variant (fntype, specs);
7352 return fntype;
7353 }
7354 case ARRAY_TYPE:
7355 {
7356 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7357 if (domain == error_mark_node)
7358 return error_mark_node;
7359
7360 /* As an optimization, we avoid regenerating the array type if
7361 it will obviously be the same as T. */
7362 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7363 return t;
7364
7365 /* These checks should match the ones in grokdeclarator.
7366
7367 [temp.deduct]
7368
7369 The deduction may fail for any of the following reasons:
7370
7371 -- Attempting to create an array with an element type that
7372 is void, a function type, or a reference type, or [DR337]
7373 an abstract class type. */
7374 if (TREE_CODE (type) == VOID_TYPE
7375 || TREE_CODE (type) == FUNCTION_TYPE
7376 || TREE_CODE (type) == REFERENCE_TYPE)
7377 {
7378 if (complain & tf_error)
7379 error ("creating array of %qT", type);
7380 return error_mark_node;
7381 }
7382 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7383 {
7384 if (complain & tf_error)
7385 error ("creating array of %qT, which is an abstract class type",
7386 type);
7387 return error_mark_node;
7388 }
7389
7390 r = build_cplus_array_type (type, domain);
7391 return r;
7392 }
7393
7394 case PLUS_EXPR:
7395 case MINUS_EXPR:
7396 {
7397 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7398 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7399
7400 if (e1 == error_mark_node || e2 == error_mark_node)
7401 return error_mark_node;
7402
7403 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7404 }
7405
7406 case NEGATE_EXPR:
7407 case NOP_EXPR:
7408 {
7409 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7410 if (e == error_mark_node)
7411 return error_mark_node;
7412
7413 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7414 }
7415
7416 case TYPENAME_TYPE:
7417 {
7418 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7419 in_decl, /*entering_scope=*/1);
7420 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7421 complain, in_decl);
7422
7423 if (ctx == error_mark_node || f == error_mark_node)
7424 return error_mark_node;
7425
7426 if (!IS_AGGR_TYPE (ctx))
7427 {
7428 if (complain & tf_error)
7429 error ("%qT is not a class, struct, or union type", ctx);
7430 return error_mark_node;
7431 }
7432 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7433 {
7434 /* Normally, make_typename_type does not require that the CTX
7435 have complete type in order to allow things like:
7436
7437 template <class T> struct S { typename S<T>::X Y; };
7438
7439 But, such constructs have already been resolved by this
7440 point, so here CTX really should have complete type, unless
7441 it's a partial instantiation. */
7442 ctx = complete_type (ctx);
7443 if (!COMPLETE_TYPE_P (ctx))
7444 {
7445 if (complain & tf_error)
7446 cxx_incomplete_type_error (NULL_TREE, ctx);
7447 return error_mark_node;
7448 }
7449 }
7450
7451 f = make_typename_type (ctx, f, typename_type,
7452 (complain & tf_error) | tf_keep_type_decl);
7453 if (f == error_mark_node)
7454 return f;
7455 if (TREE_CODE (f) == TYPE_DECL)
7456 {
7457 complain |= tf_ignore_bad_quals;
7458 f = TREE_TYPE (f);
7459 }
7460
7461 if (TREE_CODE (f) != TYPENAME_TYPE)
7462 {
7463 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7464 error ("%qT resolves to %qT, which is not an enumeration type",
7465 t, f);
7466 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7467 error ("%qT resolves to %qT, which is is not a class type",
7468 t, f);
7469 }
7470
7471 return cp_build_qualified_type_real
7472 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7473 }
7474
7475 case UNBOUND_CLASS_TEMPLATE:
7476 {
7477 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7478 in_decl, /*entering_scope=*/1);
7479 tree name = TYPE_IDENTIFIER (t);
7480 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7481
7482 if (ctx == error_mark_node || name == error_mark_node)
7483 return error_mark_node;
7484
7485 if (parm_list)
7486 parm_list = tsubst_template_parms (parm_list, args, complain);
7487 return make_unbound_class_template (ctx, name, parm_list, complain);
7488 }
7489
7490 case INDIRECT_REF:
7491 case ADDR_EXPR:
7492 case CALL_EXPR:
7493 gcc_unreachable ();
7494
7495 case ARRAY_REF:
7496 {
7497 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7498 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7499 if (e1 == error_mark_node || e2 == error_mark_node)
7500 return error_mark_node;
7501
7502 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7503 }
7504
7505 case SCOPE_REF:
7506 {
7507 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7508 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7509 if (e1 == error_mark_node || e2 == error_mark_node)
7510 return error_mark_node;
7511
7512 return build_qualified_name (/*type=*/NULL_TREE,
7513 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7514 }
7515
7516 case TYPEOF_TYPE:
7517 {
7518 tree type;
7519
7520 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7521 complain, in_decl));
7522 return cp_build_qualified_type_real (type,
7523 cp_type_quals (t)
7524 | cp_type_quals (type),
7525 complain);
7526 }
7527
7528 default:
7529 sorry ("use of %qs in template",
7530 tree_code_name [(int) TREE_CODE (t)]);
7531 return error_mark_node;
7532 }
7533 }
7534
7535 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7536 type of the expression on the left-hand side of the "." or "->"
7537 operator. */
7538
7539 static tree
7540 tsubst_baselink (tree baselink, tree object_type,
7541 tree args, tsubst_flags_t complain, tree in_decl)
7542 {
7543 tree name;
7544 tree qualifying_scope;
7545 tree fns;
7546 tree template_args = 0;
7547 bool template_id_p = false;
7548
7549 /* A baselink indicates a function from a base class. The
7550 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7551 non-dependent types; otherwise, the lookup could not have
7552 succeeded. However, they may indicate bases of the template
7553 class, rather than the instantiated class.
7554
7555 In addition, lookups that were not ambiguous before may be
7556 ambiguous now. Therefore, we perform the lookup again. */
7557 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7558 fns = BASELINK_FUNCTIONS (baselink);
7559 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7560 {
7561 template_id_p = true;
7562 template_args = TREE_OPERAND (fns, 1);
7563 fns = TREE_OPERAND (fns, 0);
7564 if (template_args)
7565 template_args = tsubst_template_args (template_args, args,
7566 complain, in_decl);
7567 }
7568 name = DECL_NAME (get_first_fn (fns));
7569 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7570
7571 /* If lookup found a single function, mark it as used at this
7572 point. (If it lookup found multiple functions the one selected
7573 later by overload resolution will be marked as used at that
7574 point.) */
7575 if (BASELINK_P (baselink))
7576 fns = BASELINK_FUNCTIONS (baselink);
7577 if (!template_id_p && !really_overloaded_fn (fns))
7578 mark_used (OVL_CURRENT (fns));
7579
7580 /* Add back the template arguments, if present. */
7581 if (BASELINK_P (baselink) && template_id_p)
7582 BASELINK_FUNCTIONS (baselink)
7583 = build_nt (TEMPLATE_ID_EXPR,
7584 BASELINK_FUNCTIONS (baselink),
7585 template_args);
7586
7587 if (!object_type)
7588 object_type = current_class_type;
7589 return adjust_result_of_qualified_name_lookup (baselink,
7590 qualifying_scope,
7591 object_type);
7592 }
7593
7594 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7595 true if the qualified-id will be a postfix-expression in-and-of
7596 itself; false if more of the postfix-expression follows the
7597 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7598 of "&". */
7599
7600 static tree
7601 tsubst_qualified_id (tree qualified_id, tree args,
7602 tsubst_flags_t complain, tree in_decl,
7603 bool done, bool address_p)
7604 {
7605 tree expr;
7606 tree scope;
7607 tree name;
7608 bool is_template;
7609 tree template_args;
7610
7611 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7612
7613 /* Figure out what name to look up. */
7614 name = TREE_OPERAND (qualified_id, 1);
7615 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7616 {
7617 is_template = true;
7618 template_args = TREE_OPERAND (name, 1);
7619 if (template_args)
7620 template_args = tsubst_template_args (template_args, args,
7621 complain, in_decl);
7622 name = TREE_OPERAND (name, 0);
7623 }
7624 else
7625 {
7626 is_template = false;
7627 template_args = NULL_TREE;
7628 }
7629
7630 /* Substitute into the qualifying scope. When there are no ARGS, we
7631 are just trying to simplify a non-dependent expression. In that
7632 case the qualifying scope may be dependent, and, in any case,
7633 substituting will not help. */
7634 scope = TREE_OPERAND (qualified_id, 0);
7635 if (args)
7636 {
7637 scope = tsubst (scope, args, complain, in_decl);
7638 expr = tsubst_copy (name, args, complain, in_decl);
7639 }
7640 else
7641 expr = name;
7642
7643 if (dependent_type_p (scope))
7644 return build_qualified_name (/*type=*/NULL_TREE,
7645 scope, expr,
7646 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7647
7648 if (!BASELINK_P (name) && !DECL_P (expr))
7649 {
7650 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7651 /* If this were actually a destructor call, it would have been
7652 parsed as such by the parser. */
7653 expr = error_mark_node;
7654 else
7655 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7656 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7657 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7658 {
7659 if (complain & tf_error)
7660 {
7661 error ("dependent-name %qE is parsed as a non-type, but "
7662 "instantiation yields a type", qualified_id);
7663 inform ("say %<typename %E%> if a type is meant", qualified_id);
7664 }
7665 return error_mark_node;
7666 }
7667 }
7668
7669 if (DECL_P (expr))
7670 {
7671 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7672 scope);
7673 /* Remember that there was a reference to this entity. */
7674 mark_used (expr);
7675 }
7676
7677 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7678 {
7679 if (complain & tf_error)
7680 qualified_name_lookup_error (scope,
7681 TREE_OPERAND (qualified_id, 1),
7682 expr);
7683 return error_mark_node;
7684 }
7685
7686 if (is_template)
7687 expr = lookup_template_function (expr, template_args);
7688
7689 if (expr == error_mark_node && complain & tf_error)
7690 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7691 expr);
7692 else if (TYPE_P (scope))
7693 {
7694 expr = (adjust_result_of_qualified_name_lookup
7695 (expr, scope, current_class_type));
7696 expr = (finish_qualified_id_expr
7697 (scope, expr, done, address_p,
7698 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7699 /*template_arg_p=*/false));
7700 }
7701
7702 if (TREE_CODE (expr) != SCOPE_REF)
7703 expr = convert_from_reference (expr);
7704
7705 return expr;
7706 }
7707
7708 /* Like tsubst, but deals with expressions. This function just replaces
7709 template parms; to finish processing the resultant expression, use
7710 tsubst_expr. */
7711
7712 static tree
7713 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7714 {
7715 enum tree_code code;
7716 tree r;
7717
7718 if (t == NULL_TREE || t == error_mark_node)
7719 return t;
7720
7721 code = TREE_CODE (t);
7722
7723 switch (code)
7724 {
7725 case PARM_DECL:
7726 r = retrieve_local_specialization (t);
7727 gcc_assert (r != NULL);
7728 mark_used (r);
7729 return r;
7730
7731 case CONST_DECL:
7732 {
7733 tree enum_type;
7734 tree v;
7735
7736 if (DECL_TEMPLATE_PARM_P (t))
7737 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7738 /* There is no need to substitute into namespace-scope
7739 enumerators. */
7740 if (DECL_NAMESPACE_SCOPE_P (t))
7741 return t;
7742 /* If ARGS is NULL, then T is known to be non-dependent. */
7743 if (args == NULL_TREE)
7744 return integral_constant_value (t);
7745
7746 /* Unfortunately, we cannot just call lookup_name here.
7747 Consider:
7748
7749 template <int I> int f() {
7750 enum E { a = I };
7751 struct S { void g() { E e = a; } };
7752 };
7753
7754 When we instantiate f<7>::S::g(), say, lookup_name is not
7755 clever enough to find f<7>::a. */
7756 enum_type
7757 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7758 /*entering_scope=*/0);
7759
7760 for (v = TYPE_VALUES (enum_type);
7761 v != NULL_TREE;
7762 v = TREE_CHAIN (v))
7763 if (TREE_PURPOSE (v) == DECL_NAME (t))
7764 return TREE_VALUE (v);
7765
7766 /* We didn't find the name. That should never happen; if
7767 name-lookup found it during preliminary parsing, we
7768 should find it again here during instantiation. */
7769 gcc_unreachable ();
7770 }
7771 return t;
7772
7773 case FIELD_DECL:
7774 if (DECL_CONTEXT (t))
7775 {
7776 tree ctx;
7777
7778 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7779 /*entering_scope=*/1);
7780 if (ctx != DECL_CONTEXT (t))
7781 {
7782 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7783 if (!r)
7784 {
7785 if (complain & tf_error)
7786 error ("using invalid field %qD", t);
7787 return error_mark_node;
7788 }
7789 return r;
7790 }
7791 }
7792
7793 return t;
7794
7795 case VAR_DECL:
7796 case FUNCTION_DECL:
7797 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7798 || local_variable_p (t))
7799 t = tsubst (t, args, complain, in_decl);
7800 mark_used (t);
7801 return t;
7802
7803 case BASELINK:
7804 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7805
7806 case TEMPLATE_DECL:
7807 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7808 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7809 args, complain, in_decl);
7810 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
7811 return tsubst (t, args, complain, in_decl);
7812 else if (DECL_CLASS_SCOPE_P (t)
7813 && uses_template_parms (DECL_CONTEXT (t)))
7814 {
7815 /* Template template argument like the following example need
7816 special treatment:
7817
7818 template <template <class> class TT> struct C {};
7819 template <class T> struct D {
7820 template <class U> struct E {};
7821 C<E> c; // #1
7822 };
7823 D<int> d; // #2
7824
7825 We are processing the template argument `E' in #1 for
7826 the template instantiation #2. Originally, `E' is a
7827 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7828 have to substitute this with one having context `D<int>'. */
7829
7830 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7831 return lookup_field (context, DECL_NAME(t), 0, false);
7832 }
7833 else
7834 /* Ordinary template template argument. */
7835 return t;
7836
7837 case CAST_EXPR:
7838 case REINTERPRET_CAST_EXPR:
7839 case CONST_CAST_EXPR:
7840 case STATIC_CAST_EXPR:
7841 case DYNAMIC_CAST_EXPR:
7842 case NOP_EXPR:
7843 return build1
7844 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7845 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7846
7847 case INDIRECT_REF:
7848 case NEGATE_EXPR:
7849 case TRUTH_NOT_EXPR:
7850 case BIT_NOT_EXPR:
7851 case ADDR_EXPR:
7852 case UNARY_PLUS_EXPR: /* Unary + */
7853 case SIZEOF_EXPR:
7854 case ALIGNOF_EXPR:
7855 case ARROW_EXPR:
7856 case THROW_EXPR:
7857 case TYPEID_EXPR:
7858 case REALPART_EXPR:
7859 case IMAGPART_EXPR:
7860 return build1
7861 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7862 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7863
7864 case COMPONENT_REF:
7865 {
7866 tree object;
7867 tree name;
7868
7869 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7870 name = TREE_OPERAND (t, 1);
7871 if (TREE_CODE (name) == BIT_NOT_EXPR)
7872 {
7873 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7874 complain, in_decl);
7875 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7876 }
7877 else if (TREE_CODE (name) == SCOPE_REF
7878 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7879 {
7880 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7881 complain, in_decl);
7882 name = TREE_OPERAND (name, 1);
7883 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7884 complain, in_decl);
7885 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7886 name = build_qualified_name (/*type=*/NULL_TREE,
7887 base, name,
7888 /*template_p=*/false);
7889 }
7890 else if (TREE_CODE (name) == BASELINK)
7891 name = tsubst_baselink (name,
7892 non_reference (TREE_TYPE (object)),
7893 args, complain,
7894 in_decl);
7895 else
7896 name = tsubst_copy (name, args, complain, in_decl);
7897 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7898 }
7899
7900 case PLUS_EXPR:
7901 case MINUS_EXPR:
7902 case MULT_EXPR:
7903 case TRUNC_DIV_EXPR:
7904 case CEIL_DIV_EXPR:
7905 case FLOOR_DIV_EXPR:
7906 case ROUND_DIV_EXPR:
7907 case EXACT_DIV_EXPR:
7908 case BIT_AND_EXPR:
7909 case BIT_IOR_EXPR:
7910 case BIT_XOR_EXPR:
7911 case TRUNC_MOD_EXPR:
7912 case FLOOR_MOD_EXPR:
7913 case TRUTH_ANDIF_EXPR:
7914 case TRUTH_ORIF_EXPR:
7915 case TRUTH_AND_EXPR:
7916 case TRUTH_OR_EXPR:
7917 case RSHIFT_EXPR:
7918 case LSHIFT_EXPR:
7919 case RROTATE_EXPR:
7920 case LROTATE_EXPR:
7921 case EQ_EXPR:
7922 case NE_EXPR:
7923 case MAX_EXPR:
7924 case MIN_EXPR:
7925 case LE_EXPR:
7926 case GE_EXPR:
7927 case LT_EXPR:
7928 case GT_EXPR:
7929 case COMPOUND_EXPR:
7930 case DOTSTAR_EXPR:
7931 case MEMBER_REF:
7932 case PREDECREMENT_EXPR:
7933 case PREINCREMENT_EXPR:
7934 case POSTDECREMENT_EXPR:
7935 case POSTINCREMENT_EXPR:
7936 return build_nt
7937 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7938 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7939
7940 case SCOPE_REF:
7941 return build_qualified_name (/*type=*/NULL_TREE,
7942 tsubst_copy (TREE_OPERAND (t, 0),
7943 args, complain, in_decl),
7944 tsubst_copy (TREE_OPERAND (t, 1),
7945 args, complain, in_decl),
7946 QUALIFIED_NAME_IS_TEMPLATE (t));
7947
7948 case ARRAY_REF:
7949 return build_nt
7950 (ARRAY_REF,
7951 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7952 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7953 NULL_TREE, NULL_TREE);
7954
7955 case CALL_EXPR:
7956 return build_nt (code,
7957 tsubst_copy (TREE_OPERAND (t, 0), args,
7958 complain, in_decl),
7959 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7960 in_decl),
7961 NULL_TREE);
7962
7963 case COND_EXPR:
7964 case MODOP_EXPR:
7965 case PSEUDO_DTOR_EXPR:
7966 {
7967 r = build_nt
7968 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7969 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7970 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7971 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7972 return r;
7973 }
7974
7975 case NEW_EXPR:
7976 {
7977 r = build_nt
7978 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7979 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7980 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7981 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7982 return r;
7983 }
7984
7985 case DELETE_EXPR:
7986 {
7987 r = build_nt
7988 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7989 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7990 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7991 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7992 return r;
7993 }
7994
7995 case TEMPLATE_ID_EXPR:
7996 {
7997 /* Substituted template arguments */
7998 tree fn = TREE_OPERAND (t, 0);
7999 tree targs = TREE_OPERAND (t, 1);
8000
8001 fn = tsubst_copy (fn, args, complain, in_decl);
8002 if (targs)
8003 targs = tsubst_template_args (targs, args, complain, in_decl);
8004
8005 return lookup_template_function (fn, targs);
8006 }
8007
8008 case TREE_LIST:
8009 {
8010 tree purpose, value, chain;
8011
8012 if (t == void_list_node)
8013 return t;
8014
8015 purpose = TREE_PURPOSE (t);
8016 if (purpose)
8017 purpose = tsubst_copy (purpose, args, complain, in_decl);
8018 value = TREE_VALUE (t);
8019 if (value)
8020 value = tsubst_copy (value, args, complain, in_decl);
8021 chain = TREE_CHAIN (t);
8022 if (chain && chain != void_type_node)
8023 chain = tsubst_copy (chain, args, complain, in_decl);
8024 if (purpose == TREE_PURPOSE (t)
8025 && value == TREE_VALUE (t)
8026 && chain == TREE_CHAIN (t))
8027 return t;
8028 return tree_cons (purpose, value, chain);
8029 }
8030
8031 case RECORD_TYPE:
8032 case UNION_TYPE:
8033 case ENUMERAL_TYPE:
8034 case INTEGER_TYPE:
8035 case TEMPLATE_TYPE_PARM:
8036 case TEMPLATE_TEMPLATE_PARM:
8037 case BOUND_TEMPLATE_TEMPLATE_PARM:
8038 case TEMPLATE_PARM_INDEX:
8039 case POINTER_TYPE:
8040 case REFERENCE_TYPE:
8041 case OFFSET_TYPE:
8042 case FUNCTION_TYPE:
8043 case METHOD_TYPE:
8044 case ARRAY_TYPE:
8045 case TYPENAME_TYPE:
8046 case UNBOUND_CLASS_TEMPLATE:
8047 case TYPEOF_TYPE:
8048 case TYPE_DECL:
8049 return tsubst (t, args, complain, in_decl);
8050
8051 case IDENTIFIER_NODE:
8052 if (IDENTIFIER_TYPENAME_P (t))
8053 {
8054 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8055 return mangle_conv_op_name_for_type (new_type);
8056 }
8057 else
8058 return t;
8059
8060 case CONSTRUCTOR:
8061 /* This is handled by tsubst_copy_and_build. */
8062 gcc_unreachable ();
8063
8064 case VA_ARG_EXPR:
8065 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8066 in_decl),
8067 tsubst (TREE_TYPE (t), args, complain, in_decl));
8068
8069 case CLEANUP_POINT_EXPR:
8070 /* We shouldn't have built any of these during initial template
8071 generation. Instead, they should be built during instantiation
8072 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8073 gcc_unreachable ();
8074
8075 case OFFSET_REF:
8076 mark_used (TREE_OPERAND (t, 1));
8077 return t;
8078
8079 default:
8080 return t;
8081 }
8082 }
8083
8084 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8085
8086 static tree
8087 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8088 tree in_decl)
8089 {
8090 tree new_clauses = NULL, nc, oc;
8091
8092 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8093 {
8094 nc = copy_node (oc);
8095 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8096 new_clauses = nc;
8097
8098 switch (OMP_CLAUSE_CODE (nc))
8099 {
8100 case OMP_CLAUSE_PRIVATE:
8101 case OMP_CLAUSE_SHARED:
8102 case OMP_CLAUSE_FIRSTPRIVATE:
8103 case OMP_CLAUSE_LASTPRIVATE:
8104 case OMP_CLAUSE_REDUCTION:
8105 case OMP_CLAUSE_COPYIN:
8106 case OMP_CLAUSE_COPYPRIVATE:
8107 case OMP_CLAUSE_IF:
8108 case OMP_CLAUSE_NUM_THREADS:
8109 case OMP_CLAUSE_SCHEDULE:
8110 OMP_CLAUSE_OPERAND (nc, 0)
8111 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
8112 break;
8113 case OMP_CLAUSE_NOWAIT:
8114 case OMP_CLAUSE_ORDERED:
8115 case OMP_CLAUSE_DEFAULT:
8116 break;
8117 default:
8118 gcc_unreachable ();
8119 }
8120 }
8121
8122 return finish_omp_clauses (nreverse (new_clauses));
8123 }
8124
8125 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8126
8127 static tree
8128 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8129 tree in_decl)
8130 {
8131 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8132
8133 tree purpose, value, chain;
8134
8135 if (t == NULL)
8136 return t;
8137
8138 if (TREE_CODE (t) != TREE_LIST)
8139 return tsubst_copy_and_build (t, args, complain, in_decl,
8140 /*function_p=*/false);
8141
8142 if (t == void_list_node)
8143 return t;
8144
8145 purpose = TREE_PURPOSE (t);
8146 if (purpose)
8147 purpose = RECUR (purpose);
8148 value = TREE_VALUE (t);
8149 if (value)
8150 value = RECUR (value);
8151 chain = TREE_CHAIN (t);
8152 if (chain && chain != void_type_node)
8153 chain = RECUR (chain);
8154 return tree_cons (purpose, value, chain);
8155 #undef RECUR
8156 }
8157
8158 /* Like tsubst_copy for expressions, etc. but also does semantic
8159 processing. */
8160
8161 static tree
8162 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8163 {
8164 tree stmt, tmp;
8165
8166 if (t == NULL_TREE || t == error_mark_node)
8167 return t;
8168
8169 if (EXPR_HAS_LOCATION (t))
8170 input_location = EXPR_LOCATION (t);
8171 if (STATEMENT_CODE_P (TREE_CODE (t)))
8172 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8173
8174 switch (TREE_CODE (t))
8175 {
8176 case STATEMENT_LIST:
8177 {
8178 tree_stmt_iterator i;
8179 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8180 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
8181 break;
8182 }
8183
8184 case CTOR_INITIALIZER:
8185 finish_mem_initializers (tsubst_initializer_list
8186 (TREE_OPERAND (t, 0), args));
8187 break;
8188
8189 case RETURN_EXPR:
8190 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
8191 args, complain, in_decl));
8192 break;
8193
8194 case EXPR_STMT:
8195 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
8196 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8197 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8198 else
8199 finish_expr_stmt (tmp);
8200 break;
8201
8202 case USING_STMT:
8203 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
8204 args, complain, in_decl));
8205 break;
8206
8207 case DECL_EXPR:
8208 {
8209 tree decl;
8210 tree init;
8211
8212 decl = DECL_EXPR_DECL (t);
8213 if (TREE_CODE (decl) == LABEL_DECL)
8214 finish_label_decl (DECL_NAME (decl));
8215 else if (TREE_CODE (decl) == USING_DECL)
8216 {
8217 tree scope = USING_DECL_SCOPE (decl);
8218 tree name = DECL_NAME (decl);
8219 tree decl;
8220
8221 scope = tsubst_expr (scope, args, complain, in_decl);
8222 decl = lookup_qualified_name (scope, name,
8223 /*is_type_p=*/false,
8224 /*complain=*/false);
8225 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8226 qualified_name_lookup_error (scope, name, decl);
8227 else
8228 do_local_using_decl (decl, scope, name);
8229 }
8230 else
8231 {
8232 init = DECL_INITIAL (decl);
8233 decl = tsubst (decl, args, complain, in_decl);
8234 if (decl != error_mark_node)
8235 {
8236 if (init)
8237 DECL_INITIAL (decl) = error_mark_node;
8238 /* By marking the declaration as instantiated, we avoid
8239 trying to instantiate it. Since instantiate_decl can't
8240 handle local variables, and since we've already done
8241 all that needs to be done, that's the right thing to
8242 do. */
8243 if (TREE_CODE (decl) == VAR_DECL)
8244 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8245 if (TREE_CODE (decl) == VAR_DECL
8246 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8247 /* Anonymous aggregates are a special case. */
8248 finish_anon_union (decl);
8249 else
8250 {
8251 maybe_push_decl (decl);
8252 if (TREE_CODE (decl) == VAR_DECL
8253 && DECL_PRETTY_FUNCTION_P (decl))
8254 {
8255 /* For __PRETTY_FUNCTION__ we have to adjust the
8256 initializer. */
8257 const char *const name
8258 = cxx_printable_name (current_function_decl, 2);
8259 init = cp_fname_init (name, &TREE_TYPE (decl));
8260 }
8261 else
8262 init = tsubst_expr (init, args, complain, in_decl);
8263 finish_decl (decl, init, NULL_TREE);
8264 }
8265 }
8266 }
8267
8268 /* A DECL_EXPR can also be used as an expression, in the condition
8269 clause of an if/for/while construct. */
8270 return decl;
8271 }
8272
8273 case FOR_STMT:
8274 stmt = begin_for_stmt ();
8275 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
8276 finish_for_init_stmt (stmt);
8277 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
8278 finish_for_cond (tmp, stmt);
8279 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
8280 finish_for_expr (tmp, stmt);
8281 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
8282 finish_for_stmt (stmt);
8283 break;
8284
8285 case WHILE_STMT:
8286 stmt = begin_while_stmt ();
8287 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
8288 finish_while_stmt_cond (tmp, stmt);
8289 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
8290 finish_while_stmt (stmt);
8291 break;
8292
8293 case DO_STMT:
8294 stmt = begin_do_stmt ();
8295 tsubst_expr (DO_BODY (t), args, complain, in_decl);
8296 finish_do_body (stmt);
8297 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
8298 finish_do_stmt (tmp, stmt);
8299 break;
8300
8301 case IF_STMT:
8302 stmt = begin_if_stmt ();
8303 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
8304 finish_if_stmt_cond (tmp, stmt);
8305 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
8306 finish_then_clause (stmt);
8307
8308 if (ELSE_CLAUSE (t))
8309 {
8310 begin_else_clause (stmt);
8311 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
8312 finish_else_clause (stmt);
8313 }
8314
8315 finish_if_stmt (stmt);
8316 break;
8317
8318 case BIND_EXPR:
8319 if (BIND_EXPR_BODY_BLOCK (t))
8320 stmt = begin_function_body ();
8321 else
8322 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8323 ? BCS_TRY_BLOCK : 0);
8324
8325 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
8326
8327 if (BIND_EXPR_BODY_BLOCK (t))
8328 finish_function_body (stmt);
8329 else
8330 finish_compound_stmt (stmt);
8331 break;
8332
8333 case BREAK_STMT:
8334 finish_break_stmt ();
8335 break;
8336
8337 case CONTINUE_STMT:
8338 finish_continue_stmt ();
8339 break;
8340
8341 case SWITCH_STMT:
8342 stmt = begin_switch_stmt ();
8343 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
8344 finish_switch_cond (tmp, stmt);
8345 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
8346 finish_switch_stmt (stmt);
8347 break;
8348
8349 case CASE_LABEL_EXPR:
8350 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
8351 tsubst_expr (CASE_HIGH (t), args, complain,
8352 in_decl));
8353 break;
8354
8355 case LABEL_EXPR:
8356 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8357 break;
8358
8359 case GOTO_EXPR:
8360 tmp = GOTO_DESTINATION (t);
8361 if (TREE_CODE (tmp) != LABEL_DECL)
8362 /* Computed goto's must be tsubst'd into. On the other hand,
8363 non-computed gotos must not be; the identifier in question
8364 will have no binding. */
8365 tmp = tsubst_expr (tmp, args, complain, in_decl);
8366 else
8367 tmp = DECL_NAME (tmp);
8368 finish_goto_stmt (tmp);
8369 break;
8370
8371 case ASM_EXPR:
8372 tmp = finish_asm_stmt
8373 (ASM_VOLATILE_P (t),
8374 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8375 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8376 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8377 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8378 {
8379 tree asm_expr = tmp;
8380 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8381 asm_expr = TREE_OPERAND (asm_expr, 0);
8382 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8383 }
8384 break;
8385
8386 case TRY_BLOCK:
8387 if (CLEANUP_P (t))
8388 {
8389 stmt = begin_try_block ();
8390 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8391 finish_cleanup_try_block (stmt);
8392 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8393 complain, in_decl),
8394 stmt);
8395 }
8396 else
8397 {
8398 if (FN_TRY_BLOCK_P (t))
8399 stmt = begin_function_try_block ();
8400 else
8401 stmt = begin_try_block ();
8402
8403 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8404
8405 if (FN_TRY_BLOCK_P (t))
8406 finish_function_try_block (stmt);
8407 else
8408 finish_try_block (stmt);
8409
8410 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8411 if (FN_TRY_BLOCK_P (t))
8412 finish_function_handler_sequence (stmt);
8413 else
8414 finish_handler_sequence (stmt);
8415 }
8416 break;
8417
8418 case HANDLER:
8419 {
8420 tree decl;
8421
8422 stmt = begin_handler ();
8423 if (HANDLER_PARMS (t))
8424 {
8425 decl = HANDLER_PARMS (t);
8426 decl = tsubst (decl, args, complain, in_decl);
8427 /* Prevent instantiate_decl from trying to instantiate
8428 this variable. We've already done all that needs to be
8429 done. */
8430 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8431 }
8432 else
8433 decl = NULL_TREE;
8434 finish_handler_parms (decl, stmt);
8435 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8436 finish_handler (stmt);
8437 }
8438 break;
8439
8440 case TAG_DEFN:
8441 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8442 break;
8443
8444 case OMP_PARALLEL:
8445 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8446 args, complain, in_decl);
8447 stmt = begin_omp_parallel ();
8448 tsubst_expr (OMP_PARALLEL_BODY (t), args, complain, in_decl);
8449 finish_omp_parallel (tmp, stmt);
8450 break;
8451
8452 case OMP_FOR:
8453 {
8454 tree clauses, decl, init, cond, incr, body, pre_body;
8455
8456 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8457 args, complain, in_decl);
8458 init = OMP_FOR_INIT (t);
8459 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8460 decl = tsubst_expr (TREE_OPERAND (init, 0), args, complain, in_decl);
8461 init = tsubst_expr (TREE_OPERAND (init, 1), args, complain, in_decl);
8462 cond = tsubst_expr (OMP_FOR_COND (t), args, complain, in_decl);
8463 incr = tsubst_expr (OMP_FOR_INCR (t), args, complain, in_decl);
8464
8465 stmt = begin_omp_structured_block ();
8466
8467 pre_body = push_stmt_list ();
8468 tsubst_expr (OMP_FOR_PRE_BODY (t), args, complain, in_decl);
8469 pre_body = pop_stmt_list (pre_body);
8470
8471 body = push_stmt_list ();
8472 tsubst_expr (OMP_FOR_BODY (t), args, complain, in_decl);
8473 body = pop_stmt_list (body);
8474
8475 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8476 pre_body);
8477 if (t)
8478 OMP_FOR_CLAUSES (t) = clauses;
8479
8480 add_stmt (finish_omp_structured_block (stmt));
8481 }
8482 break;
8483
8484 case OMP_SECTIONS:
8485 case OMP_SINGLE:
8486 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8487 stmt = push_stmt_list ();
8488 tsubst_expr (OMP_BODY (t), args, complain, in_decl);
8489 stmt = pop_stmt_list (stmt);
8490
8491 t = copy_node (t);
8492 OMP_BODY (t) = stmt;
8493 OMP_CLAUSES (t) = tmp;
8494 add_stmt (t);
8495 break;
8496
8497 case OMP_SECTION:
8498 case OMP_CRITICAL:
8499 case OMP_MASTER:
8500 case OMP_ORDERED:
8501 stmt = push_stmt_list ();
8502 tsubst_expr (OMP_BODY (t), args, complain, in_decl);
8503 stmt = pop_stmt_list (stmt);
8504
8505 t = copy_node (t);
8506 OMP_BODY (t) = stmt;
8507 add_stmt (t);
8508 break;
8509
8510 case OMP_ATOMIC:
8511 {
8512 tree op0, op1;
8513 op0 = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl);
8514 op1 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
8515 if (OMP_ATOMIC_DEPENDENT_P (t))
8516 c_finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
8517 else
8518 add_stmt (build2 (OMP_ATOMIC, void_type_node, op0, op1));
8519 }
8520 break;
8521
8522 default:
8523 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8524
8525 return tsubst_copy_and_build (t, args, complain, in_decl,
8526 /*function_p=*/false);
8527 }
8528
8529 return NULL_TREE;
8530 }
8531
8532 /* T is a postfix-expression that is not being used in a function
8533 call. Return the substituted version of T. */
8534
8535 static tree
8536 tsubst_non_call_postfix_expression (tree t, tree args,
8537 tsubst_flags_t complain,
8538 tree in_decl)
8539 {
8540 if (TREE_CODE (t) == SCOPE_REF)
8541 t = tsubst_qualified_id (t, args, complain, in_decl,
8542 /*done=*/false, /*address_p=*/false);
8543 else
8544 t = tsubst_copy_and_build (t, args, complain, in_decl,
8545 /*function_p=*/false);
8546
8547 return t;
8548 }
8549
8550 /* Like tsubst but deals with expressions and performs semantic
8551 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8552
8553 tree
8554 tsubst_copy_and_build (tree t,
8555 tree args,
8556 tsubst_flags_t complain,
8557 tree in_decl,
8558 bool function_p)
8559 {
8560 #define RECUR(NODE) \
8561 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8562
8563 tree op1;
8564
8565 if (t == NULL_TREE || t == error_mark_node)
8566 return t;
8567
8568 switch (TREE_CODE (t))
8569 {
8570 case USING_DECL:
8571 t = DECL_NAME (t);
8572 /* Fall through. */
8573 case IDENTIFIER_NODE:
8574 {
8575 tree decl;
8576 cp_id_kind idk;
8577 bool non_integral_constant_expression_p;
8578 const char *error_msg;
8579
8580 if (IDENTIFIER_TYPENAME_P (t))
8581 {
8582 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8583 t = mangle_conv_op_name_for_type (new_type);
8584 }
8585
8586 /* Look up the name. */
8587 decl = lookup_name (t);
8588
8589 /* By convention, expressions use ERROR_MARK_NODE to indicate
8590 failure, not NULL_TREE. */
8591 if (decl == NULL_TREE)
8592 decl = error_mark_node;
8593
8594 decl = finish_id_expression (t, decl, NULL_TREE,
8595 &idk,
8596 /*integral_constant_expression_p=*/false,
8597 /*allow_non_integral_constant_expression_p=*/false,
8598 &non_integral_constant_expression_p,
8599 /*template_p=*/false,
8600 /*done=*/true,
8601 /*address_p=*/false,
8602 /*template_arg_p=*/false,
8603 &error_msg);
8604 if (error_msg)
8605 error (error_msg);
8606 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8607 decl = unqualified_name_lookup_error (decl);
8608 return decl;
8609 }
8610
8611 case TEMPLATE_ID_EXPR:
8612 {
8613 tree object;
8614 tree template = RECUR (TREE_OPERAND (t, 0));
8615 tree targs = TREE_OPERAND (t, 1);
8616
8617 if (targs)
8618 targs = tsubst_template_args (targs, args, complain, in_decl);
8619
8620 if (TREE_CODE (template) == COMPONENT_REF)
8621 {
8622 object = TREE_OPERAND (template, 0);
8623 template = TREE_OPERAND (template, 1);
8624 }
8625 else
8626 object = NULL_TREE;
8627 template = lookup_template_function (template, targs);
8628
8629 if (object)
8630 return build3 (COMPONENT_REF, TREE_TYPE (template),
8631 object, template, NULL_TREE);
8632 else
8633 return template;
8634 }
8635
8636 case INDIRECT_REF:
8637 {
8638 tree r = RECUR (TREE_OPERAND (t, 0));
8639
8640 if (REFERENCE_REF_P (t))
8641 {
8642 /* A type conversion to reference type will be enclosed in
8643 such an indirect ref, but the substitution of the cast
8644 will have also added such an indirect ref. */
8645 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8646 r = convert_from_reference (r);
8647 }
8648 else
8649 r = build_x_indirect_ref (r, "unary *");
8650 return r;
8651 }
8652
8653 case NOP_EXPR:
8654 return build_nop
8655 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8656 RECUR (TREE_OPERAND (t, 0)));
8657
8658 case CAST_EXPR:
8659 return build_functional_cast
8660 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8661 RECUR (TREE_OPERAND (t, 0)));
8662
8663 case REINTERPRET_CAST_EXPR:
8664 return build_reinterpret_cast
8665 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8666 RECUR (TREE_OPERAND (t, 0)));
8667
8668 case CONST_CAST_EXPR:
8669 return build_const_cast
8670 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8671 RECUR (TREE_OPERAND (t, 0)));
8672
8673 case DYNAMIC_CAST_EXPR:
8674 return build_dynamic_cast
8675 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8676 RECUR (TREE_OPERAND (t, 0)));
8677
8678 case STATIC_CAST_EXPR:
8679 return build_static_cast
8680 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8681 RECUR (TREE_OPERAND (t, 0)));
8682
8683 case POSTDECREMENT_EXPR:
8684 case POSTINCREMENT_EXPR:
8685 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8686 args, complain, in_decl);
8687 return build_x_unary_op (TREE_CODE (t), op1);
8688
8689 case PREDECREMENT_EXPR:
8690 case PREINCREMENT_EXPR:
8691 case NEGATE_EXPR:
8692 case BIT_NOT_EXPR:
8693 case ABS_EXPR:
8694 case TRUTH_NOT_EXPR:
8695 case UNARY_PLUS_EXPR: /* Unary + */
8696 case REALPART_EXPR:
8697 case IMAGPART_EXPR:
8698 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8699
8700 case ADDR_EXPR:
8701 op1 = TREE_OPERAND (t, 0);
8702 if (TREE_CODE (op1) == SCOPE_REF)
8703 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8704 /*done=*/true, /*address_p=*/true);
8705 else
8706 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8707 in_decl);
8708 if (TREE_CODE (op1) == LABEL_DECL)
8709 return finish_label_address_expr (DECL_NAME (op1));
8710 return build_x_unary_op (ADDR_EXPR, op1);
8711
8712 case PLUS_EXPR:
8713 case MINUS_EXPR:
8714 case MULT_EXPR:
8715 case TRUNC_DIV_EXPR:
8716 case CEIL_DIV_EXPR:
8717 case FLOOR_DIV_EXPR:
8718 case ROUND_DIV_EXPR:
8719 case EXACT_DIV_EXPR:
8720 case BIT_AND_EXPR:
8721 case BIT_IOR_EXPR:
8722 case BIT_XOR_EXPR:
8723 case TRUNC_MOD_EXPR:
8724 case FLOOR_MOD_EXPR:
8725 case TRUTH_ANDIF_EXPR:
8726 case TRUTH_ORIF_EXPR:
8727 case TRUTH_AND_EXPR:
8728 case TRUTH_OR_EXPR:
8729 case RSHIFT_EXPR:
8730 case LSHIFT_EXPR:
8731 case RROTATE_EXPR:
8732 case LROTATE_EXPR:
8733 case EQ_EXPR:
8734 case NE_EXPR:
8735 case MAX_EXPR:
8736 case MIN_EXPR:
8737 case LE_EXPR:
8738 case GE_EXPR:
8739 case LT_EXPR:
8740 case GT_EXPR:
8741 case MEMBER_REF:
8742 case DOTSTAR_EXPR:
8743 return build_x_binary_op
8744 (TREE_CODE (t),
8745 RECUR (TREE_OPERAND (t, 0)),
8746 RECUR (TREE_OPERAND (t, 1)),
8747 /*overloaded_p=*/NULL);
8748
8749 case SCOPE_REF:
8750 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8751 /*address_p=*/false);
8752 case ARRAY_REF:
8753 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8754 args, complain, in_decl);
8755 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8756 /*overloaded_p=*/NULL);
8757
8758 case SIZEOF_EXPR:
8759 case ALIGNOF_EXPR:
8760 op1 = TREE_OPERAND (t, 0);
8761 if (!args)
8762 {
8763 /* When there are no ARGS, we are trying to evaluate a
8764 non-dependent expression from the parser. Trying to do
8765 the substitutions may not work. */
8766 if (!TYPE_P (op1))
8767 op1 = TREE_TYPE (op1);
8768 }
8769 else
8770 {
8771 ++skip_evaluation;
8772 op1 = RECUR (op1);
8773 --skip_evaluation;
8774 }
8775 if (TYPE_P (op1))
8776 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8777 else
8778 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8779
8780 case MODOP_EXPR:
8781 {
8782 tree r = build_x_modify_expr
8783 (RECUR (TREE_OPERAND (t, 0)),
8784 TREE_CODE (TREE_OPERAND (t, 1)),
8785 RECUR (TREE_OPERAND (t, 2)));
8786 /* TREE_NO_WARNING must be set if either the expression was
8787 parenthesized or it uses an operator such as >>= rather
8788 than plain assignment. In the former case, it was already
8789 set and must be copied. In the latter case,
8790 build_x_modify_expr sets it and it must not be reset
8791 here. */
8792 if (TREE_NO_WARNING (t))
8793 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8794 return r;
8795 }
8796
8797 case ARROW_EXPR:
8798 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8799 args, complain, in_decl);
8800 /* Remember that there was a reference to this entity. */
8801 if (DECL_P (op1))
8802 mark_used (op1);
8803 return build_x_arrow (op1);
8804
8805 case NEW_EXPR:
8806 return build_new
8807 (RECUR (TREE_OPERAND (t, 0)),
8808 RECUR (TREE_OPERAND (t, 1)),
8809 RECUR (TREE_OPERAND (t, 2)),
8810 RECUR (TREE_OPERAND (t, 3)),
8811 NEW_EXPR_USE_GLOBAL (t));
8812
8813 case DELETE_EXPR:
8814 return delete_sanity
8815 (RECUR (TREE_OPERAND (t, 0)),
8816 RECUR (TREE_OPERAND (t, 1)),
8817 DELETE_EXPR_USE_VEC (t),
8818 DELETE_EXPR_USE_GLOBAL (t));
8819
8820 case COMPOUND_EXPR:
8821 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8822 RECUR (TREE_OPERAND (t, 1)));
8823
8824 case CALL_EXPR:
8825 {
8826 tree function;
8827 tree call_args;
8828 bool qualified_p;
8829 bool koenig_p;
8830
8831 function = TREE_OPERAND (t, 0);
8832 /* When we parsed the expression, we determined whether or
8833 not Koenig lookup should be performed. */
8834 koenig_p = KOENIG_LOOKUP_P (t);
8835 if (TREE_CODE (function) == SCOPE_REF)
8836 {
8837 qualified_p = true;
8838 function = tsubst_qualified_id (function, args, complain, in_decl,
8839 /*done=*/false,
8840 /*address_p=*/false);
8841 }
8842 else
8843 {
8844 if (TREE_CODE (function) == COMPONENT_REF)
8845 {
8846 tree op = TREE_OPERAND (function, 1);
8847
8848 qualified_p = (TREE_CODE (op) == SCOPE_REF
8849 || (BASELINK_P (op)
8850 && BASELINK_QUALIFIED_P (op)));
8851 }
8852 else
8853 qualified_p = false;
8854
8855 function = tsubst_copy_and_build (function, args, complain,
8856 in_decl,
8857 !qualified_p);
8858 if (BASELINK_P (function))
8859 qualified_p = true;
8860 }
8861
8862 call_args = RECUR (TREE_OPERAND (t, 1));
8863
8864 /* We do not perform argument-dependent lookup if normal
8865 lookup finds a non-function, in accordance with the
8866 expected resolution of DR 218. */
8867 if (koenig_p
8868 && ((is_overloaded_fn (function)
8869 /* If lookup found a member function, the Koenig lookup is
8870 not appropriate, even if an unqualified-name was used
8871 to denote the function. */
8872 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
8873 || TREE_CODE (function) == IDENTIFIER_NODE))
8874 function = perform_koenig_lookup (function, call_args);
8875
8876 if (TREE_CODE (function) == IDENTIFIER_NODE)
8877 {
8878 unqualified_name_lookup_error (function);
8879 return error_mark_node;
8880 }
8881
8882 /* Remember that there was a reference to this entity. */
8883 if (DECL_P (function))
8884 mark_used (function);
8885
8886 if (TREE_CODE (function) == OFFSET_REF)
8887 return build_offset_ref_call_from_tree (function, call_args);
8888 if (TREE_CODE (function) == COMPONENT_REF)
8889 {
8890 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8891 return finish_call_expr (function, call_args,
8892 /*disallow_virtual=*/false,
8893 /*koenig_p=*/false);
8894 else
8895 return (build_new_method_call
8896 (TREE_OPERAND (function, 0),
8897 TREE_OPERAND (function, 1),
8898 call_args, NULL_TREE,
8899 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
8900 /*fn_p=*/NULL));
8901 }
8902 return finish_call_expr (function, call_args,
8903 /*disallow_virtual=*/qualified_p,
8904 koenig_p);
8905 }
8906
8907 case COND_EXPR:
8908 return build_x_conditional_expr
8909 (RECUR (TREE_OPERAND (t, 0)),
8910 RECUR (TREE_OPERAND (t, 1)),
8911 RECUR (TREE_OPERAND (t, 2)));
8912
8913 case PSEUDO_DTOR_EXPR:
8914 return finish_pseudo_destructor_expr
8915 (RECUR (TREE_OPERAND (t, 0)),
8916 RECUR (TREE_OPERAND (t, 1)),
8917 RECUR (TREE_OPERAND (t, 2)));
8918
8919 case TREE_LIST:
8920 {
8921 tree purpose, value, chain;
8922
8923 if (t == void_list_node)
8924 return t;
8925
8926 purpose = TREE_PURPOSE (t);
8927 if (purpose)
8928 purpose = RECUR (purpose);
8929 value = TREE_VALUE (t);
8930 if (value)
8931 value = RECUR (value);
8932 chain = TREE_CHAIN (t);
8933 if (chain && chain != void_type_node)
8934 chain = RECUR (chain);
8935 if (purpose == TREE_PURPOSE (t)
8936 && value == TREE_VALUE (t)
8937 && chain == TREE_CHAIN (t))
8938 return t;
8939 return tree_cons (purpose, value, chain);
8940 }
8941
8942 case COMPONENT_REF:
8943 {
8944 tree object;
8945 tree object_type;
8946 tree member;
8947
8948 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8949 args, complain, in_decl);
8950 /* Remember that there was a reference to this entity. */
8951 if (DECL_P (object))
8952 mark_used (object);
8953 object_type = TREE_TYPE (object);
8954
8955 member = TREE_OPERAND (t, 1);
8956 if (BASELINK_P (member))
8957 member = tsubst_baselink (member,
8958 non_reference (TREE_TYPE (object)),
8959 args, complain, in_decl);
8960 else
8961 member = tsubst_copy (member, args, complain, in_decl);
8962 if (member == error_mark_node)
8963 return error_mark_node;
8964
8965 if (object_type && !CLASS_TYPE_P (object_type))
8966 {
8967 if (TREE_CODE (member) == BIT_NOT_EXPR)
8968 return finish_pseudo_destructor_expr (object,
8969 NULL_TREE,
8970 object_type);
8971 else if (TREE_CODE (member) == SCOPE_REF
8972 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8973 return finish_pseudo_destructor_expr (object,
8974 object,
8975 object_type);
8976 }
8977 else if (TREE_CODE (member) == SCOPE_REF
8978 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8979 {
8980 tree tmpl;
8981 tree args;
8982
8983 /* Lookup the template functions now that we know what the
8984 scope is. */
8985 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8986 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8987 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8988 /*is_type_p=*/false,
8989 /*complain=*/false);
8990 if (BASELINK_P (member))
8991 {
8992 BASELINK_FUNCTIONS (member)
8993 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8994 args);
8995 member = (adjust_result_of_qualified_name_lookup
8996 (member, BINFO_TYPE (BASELINK_BINFO (member)),
8997 object_type));
8998 }
8999 else
9000 {
9001 qualified_name_lookup_error (object_type, tmpl, member);
9002 return error_mark_node;
9003 }
9004 }
9005 else if (TREE_CODE (member) == SCOPE_REF
9006 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9007 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9008 {
9009 if (complain & tf_error)
9010 {
9011 if (TYPE_P (TREE_OPERAND (member, 0)))
9012 error ("%qT is not a class or namespace",
9013 TREE_OPERAND (member, 0));
9014 else
9015 error ("%qD is not a class or namespace",
9016 TREE_OPERAND (member, 0));
9017 }
9018 return error_mark_node;
9019 }
9020 else if (TREE_CODE (member) == FIELD_DECL)
9021 return finish_non_static_data_member (member, object, NULL_TREE);
9022
9023 return finish_class_member_access_expr (object, member,
9024 /*template_p=*/false);
9025 }
9026
9027 case THROW_EXPR:
9028 return build_throw
9029 (RECUR (TREE_OPERAND (t, 0)));
9030
9031 case CONSTRUCTOR:
9032 {
9033 VEC(constructor_elt,gc) *n;
9034 constructor_elt *ce;
9035 unsigned HOST_WIDE_INT idx;
9036 tree r;
9037 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9038 bool process_index_p;
9039
9040 /* digest_init will do the wrong thing if we let it. */
9041 if (type && TYPE_PTRMEMFUNC_P (type))
9042 return t;
9043
9044 /* We do not want to process the index of aggregate
9045 initializers as they are identifier nodes which will be
9046 looked up by digest_init. */
9047 process_index_p = !(type && IS_AGGR_TYPE (type));
9048
9049 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9050 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9051 {
9052 if (ce->index && process_index_p)
9053 ce->index = RECUR (ce->index);
9054 ce->value = RECUR (ce->value);
9055 }
9056
9057 r = build_constructor (NULL_TREE, n);
9058 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
9059
9060 if (type)
9061 return digest_init (type, r);
9062 return r;
9063 }
9064
9065 case TYPEID_EXPR:
9066 {
9067 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9068 if (TYPE_P (operand_0))
9069 return get_typeid (operand_0);
9070 return build_typeid (operand_0);
9071 }
9072
9073 case VAR_DECL:
9074 if (!args)
9075 return t;
9076 /* Fall through */
9077
9078 case PARM_DECL:
9079 {
9080 tree r = tsubst_copy (t, args, complain, in_decl);
9081
9082 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9083 /* If the original type was a reference, we'll be wrapped in
9084 the appropriate INDIRECT_REF. */
9085 r = convert_from_reference (r);
9086 return r;
9087 }
9088
9089 case VA_ARG_EXPR:
9090 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9091 tsubst_copy (TREE_TYPE (t), args, complain,
9092 in_decl));
9093
9094 case OFFSETOF_EXPR:
9095 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
9096
9097 case STMT_EXPR:
9098 {
9099 tree old_stmt_expr = cur_stmt_expr;
9100 tree stmt_expr = begin_stmt_expr ();
9101
9102 cur_stmt_expr = stmt_expr;
9103 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
9104 stmt_expr = finish_stmt_expr (stmt_expr, false);
9105 cur_stmt_expr = old_stmt_expr;
9106
9107 return stmt_expr;
9108 }
9109
9110 case CONST_DECL:
9111 t = tsubst_copy (t, args, complain, in_decl);
9112 /* As in finish_id_expression, we resolve enumeration constants
9113 to their underlying values. */
9114 if (TREE_CODE (t) == CONST_DECL)
9115 return DECL_INITIAL (t);
9116 return t;
9117
9118 default:
9119 /* Handle Objective-C++ constructs, if appropriate. */
9120 {
9121 tree subst
9122 = objcp_tsubst_copy_and_build (t, args, complain,
9123 in_decl, /*function_p=*/false);
9124 if (subst)
9125 return subst;
9126 }
9127 return tsubst_copy (t, args, complain, in_decl);
9128 }
9129
9130 #undef RECUR
9131 }
9132
9133 /* Verify that the instantiated ARGS are valid. For type arguments,
9134 make sure that the type's linkage is ok. For non-type arguments,
9135 make sure they are constants if they are integral or enumerations.
9136 Emit an error under control of COMPLAIN, and return TRUE on error. */
9137
9138 static bool
9139 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9140 {
9141 int ix, len = DECL_NTPARMS (tmpl);
9142 bool result = false;
9143
9144 for (ix = 0; ix != len; ix++)
9145 {
9146 tree t = TREE_VEC_ELT (args, ix);
9147
9148 if (TYPE_P (t))
9149 {
9150 /* [basic.link]: A name with no linkage (notably, the name
9151 of a class or enumeration declared in a local scope)
9152 shall not be used to declare an entity with linkage.
9153 This implies that names with no linkage cannot be used as
9154 template arguments. */
9155 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9156
9157 if (nt)
9158 {
9159 /* DR 488 makes use of a type with no linkage cause
9160 type deduction to fail. */
9161 if (complain & tf_error)
9162 {
9163 if (TYPE_ANONYMOUS_P (nt))
9164 error ("%qT is/uses anonymous type", t);
9165 else
9166 error ("template argument for %qD uses local type %qT",
9167 tmpl, t);
9168 }
9169 result = true;
9170 }
9171 /* In order to avoid all sorts of complications, we do not
9172 allow variably-modified types as template arguments. */
9173 else if (variably_modified_type_p (t, NULL_TREE))
9174 {
9175 if (complain & tf_error)
9176 error ("%qT is a variably modified type", t);
9177 result = true;
9178 }
9179 }
9180 /* A non-type argument of integral or enumerated type must be a
9181 constant. */
9182 else if (TREE_TYPE (t)
9183 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9184 && !TREE_CONSTANT (t))
9185 {
9186 if (complain & tf_error)
9187 error ("integral expression %qE is not constant", t);
9188 result = true;
9189 }
9190 }
9191 if (result && (complain & tf_error))
9192 error (" trying to instantiate %qD", tmpl);
9193 return result;
9194 }
9195
9196 /* Instantiate the indicated variable or function template TMPL with
9197 the template arguments in TARG_PTR. */
9198
9199 tree
9200 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9201 {
9202 tree fndecl;
9203 tree gen_tmpl;
9204 tree spec;
9205
9206 if (tmpl == error_mark_node)
9207 return error_mark_node;
9208
9209 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9210
9211 /* If this function is a clone, handle it specially. */
9212 if (DECL_CLONED_FUNCTION_P (tmpl))
9213 {
9214 tree spec;
9215 tree clone;
9216
9217 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9218 complain);
9219 if (spec == error_mark_node)
9220 return error_mark_node;
9221
9222 /* Look for the clone. */
9223 FOR_EACH_CLONE (clone, spec)
9224 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9225 return clone;
9226 /* We should always have found the clone by now. */
9227 gcc_unreachable ();
9228 return NULL_TREE;
9229 }
9230
9231 /* Check to see if we already have this specialization. */
9232 spec = retrieve_specialization (tmpl, targ_ptr,
9233 /*class_specializations_p=*/false);
9234 if (spec != NULL_TREE)
9235 return spec;
9236
9237 gen_tmpl = most_general_template (tmpl);
9238 if (tmpl != gen_tmpl)
9239 {
9240 /* The TMPL is a partial instantiation. To get a full set of
9241 arguments we must add the arguments used to perform the
9242 partial instantiation. */
9243 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9244 targ_ptr);
9245
9246 /* Check to see if we already have this specialization. */
9247 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9248 /*class_specializations_p=*/false);
9249 if (spec != NULL_TREE)
9250 return spec;
9251 }
9252
9253 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9254 complain))
9255 return error_mark_node;
9256
9257 /* We are building a FUNCTION_DECL, during which the access of its
9258 parameters and return types have to be checked. However this
9259 FUNCTION_DECL which is the desired context for access checking
9260 is not built yet. We solve this chicken-and-egg problem by
9261 deferring all checks until we have the FUNCTION_DECL. */
9262 push_deferring_access_checks (dk_deferred);
9263
9264 /* Substitute template parameters. */
9265 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9266 targ_ptr, complain, gen_tmpl);
9267 if (fndecl == error_mark_node)
9268 return error_mark_node;
9269
9270 /* Now we know the specialization, compute access previously
9271 deferred. */
9272 push_access_scope (fndecl);
9273 perform_deferred_access_checks ();
9274 pop_access_scope (fndecl);
9275 pop_deferring_access_checks ();
9276
9277 /* The DECL_TI_TEMPLATE should always be the immediate parent
9278 template, not the most general template. */
9279 DECL_TI_TEMPLATE (fndecl) = tmpl;
9280
9281 /* If we've just instantiated the main entry point for a function,
9282 instantiate all the alternate entry points as well. We do this
9283 by cloning the instantiation of the main entry point, not by
9284 instantiating the template clones. */
9285 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9286 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9287
9288 return fndecl;
9289 }
9290
9291 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9292 arguments that are being used when calling it. TARGS is a vector
9293 into which the deduced template arguments are placed.
9294
9295 Return zero for success, 2 for an incomplete match that doesn't resolve
9296 all the types, and 1 for complete failure. An error message will be
9297 printed only for an incomplete match.
9298
9299 If FN is a conversion operator, or we are trying to produce a specific
9300 specialization, RETURN_TYPE is the return type desired.
9301
9302 The EXPLICIT_TARGS are explicit template arguments provided via a
9303 template-id.
9304
9305 The parameter STRICT is one of:
9306
9307 DEDUCE_CALL:
9308 We are deducing arguments for a function call, as in
9309 [temp.deduct.call].
9310
9311 DEDUCE_CONV:
9312 We are deducing arguments for a conversion function, as in
9313 [temp.deduct.conv].
9314
9315 DEDUCE_EXACT:
9316 We are deducing arguments when doing an explicit instantiation
9317 as in [temp.explicit], when determining an explicit specialization
9318 as in [temp.expl.spec], or when taking the address of a function
9319 template, as in [temp.deduct.funcaddr]. */
9320
9321 int
9322 fn_type_unification (tree fn,
9323 tree explicit_targs,
9324 tree targs,
9325 tree args,
9326 tree return_type,
9327 unification_kind_t strict,
9328 int flags)
9329 {
9330 tree parms;
9331 tree fntype;
9332 int result;
9333
9334 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9335
9336 fntype = TREE_TYPE (fn);
9337 if (explicit_targs)
9338 {
9339 /* [temp.deduct]
9340
9341 The specified template arguments must match the template
9342 parameters in kind (i.e., type, nontype, template), and there
9343 must not be more arguments than there are parameters;
9344 otherwise type deduction fails.
9345
9346 Nontype arguments must match the types of the corresponding
9347 nontype template parameters, or must be convertible to the
9348 types of the corresponding nontype parameters as specified in
9349 _temp.arg.nontype_, otherwise type deduction fails.
9350
9351 All references in the function type of the function template
9352 to the corresponding template parameters are replaced by the
9353 specified template argument values. If a substitution in a
9354 template parameter or in the function type of the function
9355 template results in an invalid type, type deduction fails. */
9356 int i;
9357 tree converted_args;
9358 bool incomplete;
9359
9360 if (explicit_targs == error_mark_node)
9361 return 1;
9362
9363 converted_args
9364 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9365 explicit_targs, NULL_TREE, tf_none,
9366 /*require_all_args=*/false,
9367 /*use_default_args=*/false));
9368 if (converted_args == error_mark_node)
9369 return 1;
9370
9371 /* Substitute the explicit args into the function type. This is
9372 necessary so that, for instance, explicitly declared function
9373 arguments can match null pointed constants. If we were given
9374 an incomplete set of explicit args, we must not do semantic
9375 processing during substitution as we could create partial
9376 instantiations. */
9377 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9378 processing_template_decl += incomplete;
9379 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9380 processing_template_decl -= incomplete;
9381
9382 if (fntype == error_mark_node)
9383 return 1;
9384
9385 /* Place the explicitly specified arguments in TARGS. */
9386 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9387 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9388 }
9389
9390 parms = TYPE_ARG_TYPES (fntype);
9391 /* Never do unification on the 'this' parameter. */
9392 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9393 parms = TREE_CHAIN (parms);
9394
9395 if (return_type)
9396 {
9397 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9398 args = tree_cons (NULL_TREE, return_type, args);
9399 }
9400
9401 /* We allow incomplete unification without an error message here
9402 because the standard doesn't seem to explicitly prohibit it. Our
9403 callers must be ready to deal with unification failures in any
9404 event. */
9405 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9406 targs, parms, args, /*subr=*/0,
9407 strict, flags);
9408
9409 if (result == 0)
9410 /* All is well so far. Now, check:
9411
9412 [temp.deduct]
9413
9414 When all template arguments have been deduced, all uses of
9415 template parameters in nondeduced contexts are replaced with
9416 the corresponding deduced argument values. If the
9417 substitution results in an invalid type, as described above,
9418 type deduction fails. */
9419 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9420 == error_mark_node)
9421 return 1;
9422
9423 return result;
9424 }
9425
9426 /* Adjust types before performing type deduction, as described in
9427 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9428 sections are symmetric. PARM is the type of a function parameter
9429 or the return type of the conversion function. ARG is the type of
9430 the argument passed to the call, or the type of the value
9431 initialized with the result of the conversion function. */
9432
9433 static int
9434 maybe_adjust_types_for_deduction (unification_kind_t strict,
9435 tree* parm,
9436 tree* arg)
9437 {
9438 int result = 0;
9439
9440 switch (strict)
9441 {
9442 case DEDUCE_CALL:
9443 break;
9444
9445 case DEDUCE_CONV:
9446 {
9447 /* Swap PARM and ARG throughout the remainder of this
9448 function; the handling is precisely symmetric since PARM
9449 will initialize ARG rather than vice versa. */
9450 tree* temp = parm;
9451 parm = arg;
9452 arg = temp;
9453 break;
9454 }
9455
9456 case DEDUCE_EXACT:
9457 /* There is nothing to do in this case. */
9458 return 0;
9459
9460 default:
9461 gcc_unreachable ();
9462 }
9463
9464 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9465 {
9466 /* [temp.deduct.call]
9467
9468 If P is not a reference type:
9469
9470 --If A is an array type, the pointer type produced by the
9471 array-to-pointer standard conversion (_conv.array_) is
9472 used in place of A for type deduction; otherwise,
9473
9474 --If A is a function type, the pointer type produced by
9475 the function-to-pointer standard conversion
9476 (_conv.func_) is used in place of A for type deduction;
9477 otherwise,
9478
9479 --If A is a cv-qualified type, the top level
9480 cv-qualifiers of A's type are ignored for type
9481 deduction. */
9482 if (TREE_CODE (*arg) == ARRAY_TYPE)
9483 *arg = build_pointer_type (TREE_TYPE (*arg));
9484 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9485 *arg = build_pointer_type (*arg);
9486 else
9487 *arg = TYPE_MAIN_VARIANT (*arg);
9488 }
9489
9490 /* [temp.deduct.call]
9491
9492 If P is a cv-qualified type, the top level cv-qualifiers
9493 of P's type are ignored for type deduction. If P is a
9494 reference type, the type referred to by P is used for
9495 type deduction. */
9496 *parm = TYPE_MAIN_VARIANT (*parm);
9497 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9498 {
9499 *parm = TREE_TYPE (*parm);
9500 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9501 }
9502
9503 /* DR 322. For conversion deduction, remove a reference type on parm
9504 too (which has been swapped into ARG). */
9505 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9506 *arg = TREE_TYPE (*arg);
9507
9508 return result;
9509 }
9510
9511 /* Most parms like fn_type_unification.
9512
9513 If SUBR is 1, we're being called recursively (to unify the
9514 arguments of a function or method parameter of a function
9515 template). */
9516
9517 static int
9518 type_unification_real (tree tparms,
9519 tree targs,
9520 tree xparms,
9521 tree xargs,
9522 int subr,
9523 unification_kind_t strict,
9524 int flags)
9525 {
9526 tree parm, arg;
9527 int i;
9528 int ntparms = TREE_VEC_LENGTH (tparms);
9529 int sub_strict;
9530 int saw_undeduced = 0;
9531 tree parms, args;
9532
9533 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9534 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9535 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9536 gcc_assert (ntparms > 0);
9537
9538 switch (strict)
9539 {
9540 case DEDUCE_CALL:
9541 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9542 | UNIFY_ALLOW_DERIVED);
9543 break;
9544
9545 case DEDUCE_CONV:
9546 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9547 break;
9548
9549 case DEDUCE_EXACT:
9550 sub_strict = UNIFY_ALLOW_NONE;
9551 break;
9552
9553 default:
9554 gcc_unreachable ();
9555 }
9556
9557 again:
9558 parms = xparms;
9559 args = xargs;
9560
9561 while (parms && parms != void_list_node
9562 && args && args != void_list_node)
9563 {
9564 parm = TREE_VALUE (parms);
9565 parms = TREE_CHAIN (parms);
9566 arg = TREE_VALUE (args);
9567 args = TREE_CHAIN (args);
9568
9569 if (arg == error_mark_node)
9570 return 1;
9571 if (arg == unknown_type_node)
9572 /* We can't deduce anything from this, but we might get all the
9573 template args from other function args. */
9574 continue;
9575
9576 /* Conversions will be performed on a function argument that
9577 corresponds with a function parameter that contains only
9578 non-deducible template parameters and explicitly specified
9579 template parameters. */
9580 if (!uses_template_parms (parm))
9581 {
9582 tree type;
9583
9584 if (!TYPE_P (arg))
9585 type = TREE_TYPE (arg);
9586 else
9587 type = arg;
9588
9589 if (same_type_p (parm, type))
9590 continue;
9591 if (strict != DEDUCE_EXACT
9592 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9593 flags))
9594 continue;
9595
9596 return 1;
9597 }
9598
9599 if (!TYPE_P (arg))
9600 {
9601 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9602 if (type_unknown_p (arg))
9603 {
9604 /* [temp.deduct.type] A template-argument can be deduced from
9605 a pointer to function or pointer to member function
9606 argument if the set of overloaded functions does not
9607 contain function templates and at most one of a set of
9608 overloaded functions provides a unique match. */
9609
9610 if (resolve_overloaded_unification
9611 (tparms, targs, parm, arg, strict, sub_strict)
9612 != 0)
9613 return 1;
9614 continue;
9615 }
9616 arg = TREE_TYPE (arg);
9617 if (arg == error_mark_node)
9618 return 1;
9619 }
9620
9621 {
9622 int arg_strict = sub_strict;
9623
9624 if (!subr)
9625 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9626
9627 if (unify (tparms, targs, parm, arg, arg_strict))
9628 return 1;
9629 }
9630 }
9631
9632 /* Fail if we've reached the end of the parm list, and more args
9633 are present, and the parm list isn't variadic. */
9634 if (args && args != void_list_node && parms == void_list_node)
9635 return 1;
9636 /* Fail if parms are left and they don't have default values. */
9637 if (parms && parms != void_list_node
9638 && TREE_PURPOSE (parms) == NULL_TREE)
9639 return 1;
9640
9641 if (!subr)
9642 for (i = 0; i < ntparms; i++)
9643 if (!TREE_VEC_ELT (targs, i))
9644 {
9645 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9646
9647 /* If this is an undeduced nontype parameter that depends on
9648 a type parameter, try another pass; its type may have been
9649 deduced from a later argument than the one from which
9650 this parameter can be deduced. */
9651 if (TREE_CODE (tparm) == PARM_DECL
9652 && uses_template_parms (TREE_TYPE (tparm))
9653 && !saw_undeduced++)
9654 goto again;
9655
9656 return 2;
9657 }
9658
9659 return 0;
9660 }
9661
9662 /* Subroutine of type_unification_real. Args are like the variables at the
9663 call site. ARG is an overloaded function (or template-id); we try
9664 deducing template args from each of the overloads, and if only one
9665 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9666
9667 static int
9668 resolve_overloaded_unification (tree tparms,
9669 tree targs,
9670 tree parm,
9671 tree arg,
9672 unification_kind_t strict,
9673 int sub_strict)
9674 {
9675 tree tempargs = copy_node (targs);
9676 int good = 0;
9677 bool addr_p;
9678
9679 if (TREE_CODE (arg) == ADDR_EXPR)
9680 {
9681 arg = TREE_OPERAND (arg, 0);
9682 addr_p = true;
9683 }
9684 else
9685 addr_p = false;
9686
9687 if (TREE_CODE (arg) == COMPONENT_REF)
9688 /* Handle `&x' where `x' is some static or non-static member
9689 function name. */
9690 arg = TREE_OPERAND (arg, 1);
9691
9692 if (TREE_CODE (arg) == OFFSET_REF)
9693 arg = TREE_OPERAND (arg, 1);
9694
9695 /* Strip baselink information. */
9696 if (BASELINK_P (arg))
9697 arg = BASELINK_FUNCTIONS (arg);
9698
9699 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9700 {
9701 /* If we got some explicit template args, we need to plug them into
9702 the affected templates before we try to unify, in case the
9703 explicit args will completely resolve the templates in question. */
9704
9705 tree expl_subargs = TREE_OPERAND (arg, 1);
9706 arg = TREE_OPERAND (arg, 0);
9707
9708 for (; arg; arg = OVL_NEXT (arg))
9709 {
9710 tree fn = OVL_CURRENT (arg);
9711 tree subargs, elem;
9712
9713 if (TREE_CODE (fn) != TEMPLATE_DECL)
9714 continue;
9715
9716 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9717 expl_subargs, /*check_ret=*/false);
9718 if (subargs)
9719 {
9720 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9721 good += try_one_overload (tparms, targs, tempargs, parm,
9722 elem, strict, sub_strict, addr_p);
9723 }
9724 }
9725 }
9726 else
9727 {
9728 gcc_assert (TREE_CODE (arg) == OVERLOAD
9729 || TREE_CODE (arg) == FUNCTION_DECL);
9730
9731 for (; arg; arg = OVL_NEXT (arg))
9732 good += try_one_overload (tparms, targs, tempargs, parm,
9733 TREE_TYPE (OVL_CURRENT (arg)),
9734 strict, sub_strict, addr_p);
9735 }
9736
9737 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9738 to function or pointer to member function argument if the set of
9739 overloaded functions does not contain function templates and at most
9740 one of a set of overloaded functions provides a unique match.
9741
9742 So if we found multiple possibilities, we return success but don't
9743 deduce anything. */
9744
9745 if (good == 1)
9746 {
9747 int i = TREE_VEC_LENGTH (targs);
9748 for (; i--; )
9749 if (TREE_VEC_ELT (tempargs, i))
9750 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9751 }
9752 if (good)
9753 return 0;
9754
9755 return 1;
9756 }
9757
9758 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9759 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9760 different overloads deduce different arguments for a given parm.
9761 ADDR_P is true if the expression for which deduction is being
9762 performed was of the form "& fn" rather than simply "fn".
9763
9764 Returns 1 on success. */
9765
9766 static int
9767 try_one_overload (tree tparms,
9768 tree orig_targs,
9769 tree targs,
9770 tree parm,
9771 tree arg,
9772 unification_kind_t strict,
9773 int sub_strict,
9774 bool addr_p)
9775 {
9776 int nargs;
9777 tree tempargs;
9778 int i;
9779
9780 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9781 to function or pointer to member function argument if the set of
9782 overloaded functions does not contain function templates and at most
9783 one of a set of overloaded functions provides a unique match.
9784
9785 So if this is a template, just return success. */
9786
9787 if (uses_template_parms (arg))
9788 return 1;
9789
9790 if (TREE_CODE (arg) == METHOD_TYPE)
9791 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9792 else if (addr_p)
9793 arg = build_pointer_type (arg);
9794
9795 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9796
9797 /* We don't copy orig_targs for this because if we have already deduced
9798 some template args from previous args, unify would complain when we
9799 try to deduce a template parameter for the same argument, even though
9800 there isn't really a conflict. */
9801 nargs = TREE_VEC_LENGTH (targs);
9802 tempargs = make_tree_vec (nargs);
9803
9804 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9805 return 0;
9806
9807 /* First make sure we didn't deduce anything that conflicts with
9808 explicitly specified args. */
9809 for (i = nargs; i--; )
9810 {
9811 tree elt = TREE_VEC_ELT (tempargs, i);
9812 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9813
9814 if (!elt)
9815 /*NOP*/;
9816 else if (uses_template_parms (elt))
9817 /* Since we're unifying against ourselves, we will fill in
9818 template args used in the function parm list with our own
9819 template parms. Discard them. */
9820 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9821 else if (oldelt && !template_args_equal (oldelt, elt))
9822 return 0;
9823 }
9824
9825 for (i = nargs; i--; )
9826 {
9827 tree elt = TREE_VEC_ELT (tempargs, i);
9828
9829 if (elt)
9830 TREE_VEC_ELT (targs, i) = elt;
9831 }
9832
9833 return 1;
9834 }
9835
9836 /* PARM is a template class (perhaps with unbound template
9837 parameters). ARG is a fully instantiated type. If ARG can be
9838 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9839 TARGS are as for unify. */
9840
9841 static tree
9842 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9843 {
9844 tree copy_of_targs;
9845
9846 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9847 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9848 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9849 return NULL_TREE;
9850
9851 /* We need to make a new template argument vector for the call to
9852 unify. If we used TARGS, we'd clutter it up with the result of
9853 the attempted unification, even if this class didn't work out.
9854 We also don't want to commit ourselves to all the unifications
9855 we've already done, since unification is supposed to be done on
9856 an argument-by-argument basis. In other words, consider the
9857 following pathological case:
9858
9859 template <int I, int J, int K>
9860 struct S {};
9861
9862 template <int I, int J>
9863 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9864
9865 template <int I, int J, int K>
9866 void f(S<I, J, K>, S<I, I, I>);
9867
9868 void g() {
9869 S<0, 0, 0> s0;
9870 S<0, 1, 2> s2;
9871
9872 f(s0, s2);
9873 }
9874
9875 Now, by the time we consider the unification involving `s2', we
9876 already know that we must have `f<0, 0, 0>'. But, even though
9877 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9878 because there are two ways to unify base classes of S<0, 1, 2>
9879 with S<I, I, I>. If we kept the already deduced knowledge, we
9880 would reject the possibility I=1. */
9881 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9882
9883 /* If unification failed, we're done. */
9884 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9885 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9886 return NULL_TREE;
9887
9888 return arg;
9889 }
9890
9891 /* Given a template type PARM and a class type ARG, find the unique
9892 base type in ARG that is an instance of PARM. We do not examine
9893 ARG itself; only its base-classes. If there is not exactly one
9894 appropriate base class, return NULL_TREE. PARM may be the type of
9895 a partial specialization, as well as a plain template type. Used
9896 by unify. */
9897
9898 static tree
9899 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9900 {
9901 tree rval = NULL_TREE;
9902 tree binfo;
9903
9904 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9905
9906 binfo = TYPE_BINFO (complete_type (arg));
9907 if (!binfo)
9908 /* The type could not be completed. */
9909 return NULL_TREE;
9910
9911 /* Walk in inheritance graph order. The search order is not
9912 important, and this avoids multiple walks of virtual bases. */
9913 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9914 {
9915 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9916
9917 if (r)
9918 {
9919 /* If there is more than one satisfactory baseclass, then:
9920
9921 [temp.deduct.call]
9922
9923 If they yield more than one possible deduced A, the type
9924 deduction fails.
9925
9926 applies. */
9927 if (rval && !same_type_p (r, rval))
9928 return NULL_TREE;
9929
9930 rval = r;
9931 }
9932 }
9933
9934 return rval;
9935 }
9936
9937 /* Returns the level of DECL, which declares a template parameter. */
9938
9939 static int
9940 template_decl_level (tree decl)
9941 {
9942 switch (TREE_CODE (decl))
9943 {
9944 case TYPE_DECL:
9945 case TEMPLATE_DECL:
9946 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9947
9948 case PARM_DECL:
9949 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9950
9951 default:
9952 gcc_unreachable ();
9953 }
9954 return 0;
9955 }
9956
9957 /* Decide whether ARG can be unified with PARM, considering only the
9958 cv-qualifiers of each type, given STRICT as documented for unify.
9959 Returns nonzero iff the unification is OK on that basis. */
9960
9961 static int
9962 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9963 {
9964 int arg_quals = cp_type_quals (arg);
9965 int parm_quals = cp_type_quals (parm);
9966
9967 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9968 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9969 {
9970 /* Although a CVR qualifier is ignored when being applied to a
9971 substituted template parameter ([8.3.2]/1 for example), that
9972 does not apply during deduction [14.8.2.4]/1, (even though
9973 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9974 this). Except when we're allowing additional CV qualifiers
9975 at the outer level [14.8.2.1]/3,1st bullet. */
9976 if ((TREE_CODE (arg) == REFERENCE_TYPE
9977 || TREE_CODE (arg) == FUNCTION_TYPE
9978 || TREE_CODE (arg) == METHOD_TYPE)
9979 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9980 return 0;
9981
9982 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9983 && (parm_quals & TYPE_QUAL_RESTRICT))
9984 return 0;
9985 }
9986
9987 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9988 && (arg_quals & parm_quals) != parm_quals)
9989 return 0;
9990
9991 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9992 && (parm_quals & arg_quals) != arg_quals)
9993 return 0;
9994
9995 return 1;
9996 }
9997
9998 /* Deduce the value of template parameters. TPARMS is the (innermost)
9999 set of template parameters to a template. TARGS is the bindings
10000 for those template parameters, as determined thus far; TARGS may
10001 include template arguments for outer levels of template parameters
10002 as well. PARM is a parameter to a template function, or a
10003 subcomponent of that parameter; ARG is the corresponding argument.
10004 This function attempts to match PARM with ARG in a manner
10005 consistent with the existing assignments in TARGS. If more values
10006 are deduced, then TARGS is updated.
10007
10008 Returns 0 if the type deduction succeeds, 1 otherwise. The
10009 parameter STRICT is a bitwise or of the following flags:
10010
10011 UNIFY_ALLOW_NONE:
10012 Require an exact match between PARM and ARG.
10013 UNIFY_ALLOW_MORE_CV_QUAL:
10014 Allow the deduced ARG to be more cv-qualified (by qualification
10015 conversion) than ARG.
10016 UNIFY_ALLOW_LESS_CV_QUAL:
10017 Allow the deduced ARG to be less cv-qualified than ARG.
10018 UNIFY_ALLOW_DERIVED:
10019 Allow the deduced ARG to be a template base class of ARG,
10020 or a pointer to a template base class of the type pointed to by
10021 ARG.
10022 UNIFY_ALLOW_INTEGER:
10023 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10024 case for more information.
10025 UNIFY_ALLOW_OUTER_LEVEL:
10026 This is the outermost level of a deduction. Used to determine validity
10027 of qualification conversions. A valid qualification conversion must
10028 have const qualified pointers leading up to the inner type which
10029 requires additional CV quals, except at the outer level, where const
10030 is not required [conv.qual]. It would be normal to set this flag in
10031 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10032 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10033 This is the outermost level of a deduction, and PARM can be more CV
10034 qualified at this point.
10035 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10036 This is the outermost level of a deduction, and PARM can be less CV
10037 qualified at this point. */
10038
10039 static int
10040 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10041 {
10042 int idx;
10043 tree targ;
10044 tree tparm;
10045 int strict_in = strict;
10046
10047 /* I don't think this will do the right thing with respect to types.
10048 But the only case I've seen it in so far has been array bounds, where
10049 signedness is the only information lost, and I think that will be
10050 okay. */
10051 while (TREE_CODE (parm) == NOP_EXPR)
10052 parm = TREE_OPERAND (parm, 0);
10053
10054 if (arg == error_mark_node)
10055 return 1;
10056 if (arg == unknown_type_node)
10057 /* We can't deduce anything from this, but we might get all the
10058 template args from other function args. */
10059 return 0;
10060
10061 /* If PARM uses template parameters, then we can't bail out here,
10062 even if ARG == PARM, since we won't record unifications for the
10063 template parameters. We might need them if we're trying to
10064 figure out which of two things is more specialized. */
10065 if (arg == parm && !uses_template_parms (parm))
10066 return 0;
10067
10068 /* Immediately reject some pairs that won't unify because of
10069 cv-qualification mismatches. */
10070 if (TREE_CODE (arg) == TREE_CODE (parm)
10071 && TYPE_P (arg)
10072 /* It is the elements of the array which hold the cv quals of an array
10073 type, and the elements might be template type parms. We'll check
10074 when we recurse. */
10075 && TREE_CODE (arg) != ARRAY_TYPE
10076 /* We check the cv-qualifiers when unifying with template type
10077 parameters below. We want to allow ARG `const T' to unify with
10078 PARM `T' for example, when computing which of two templates
10079 is more specialized, for example. */
10080 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10081 && !check_cv_quals_for_unify (strict_in, arg, parm))
10082 return 1;
10083
10084 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10085 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10086 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10087 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10088 strict &= ~UNIFY_ALLOW_DERIVED;
10089 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10090 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10091
10092 switch (TREE_CODE (parm))
10093 {
10094 case TYPENAME_TYPE:
10095 case SCOPE_REF:
10096 case UNBOUND_CLASS_TEMPLATE:
10097 /* In a type which contains a nested-name-specifier, template
10098 argument values cannot be deduced for template parameters used
10099 within the nested-name-specifier. */
10100 return 0;
10101
10102 case TEMPLATE_TYPE_PARM:
10103 case TEMPLATE_TEMPLATE_PARM:
10104 case BOUND_TEMPLATE_TEMPLATE_PARM:
10105 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10106
10107 if (TEMPLATE_TYPE_LEVEL (parm)
10108 != template_decl_level (tparm))
10109 /* The PARM is not one we're trying to unify. Just check
10110 to see if it matches ARG. */
10111 return (TREE_CODE (arg) == TREE_CODE (parm)
10112 && same_type_p (parm, arg)) ? 0 : 1;
10113 idx = TEMPLATE_TYPE_IDX (parm);
10114 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10115 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10116
10117 /* Check for mixed types and values. */
10118 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10119 && TREE_CODE (tparm) != TYPE_DECL)
10120 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10121 && TREE_CODE (tparm) != TEMPLATE_DECL))
10122 return 1;
10123
10124 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10125 {
10126 /* ARG must be constructed from a template class or a template
10127 template parameter. */
10128 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10129 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
10130 return 1;
10131
10132 {
10133 tree parmvec = TYPE_TI_ARGS (parm);
10134 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10135 tree argtmplvec
10136 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10137 int i;
10138
10139 /* The resolution to DR150 makes clear that default
10140 arguments for an N-argument may not be used to bind T
10141 to a template template parameter with fewer than N
10142 parameters. It is not safe to permit the binding of
10143 default arguments as an extension, as that may change
10144 the meaning of a conforming program. Consider:
10145
10146 struct Dense { static const unsigned int dim = 1; };
10147
10148 template <template <typename> class View,
10149 typename Block>
10150 void operator+(float, View<Block> const&);
10151
10152 template <typename Block,
10153 unsigned int Dim = Block::dim>
10154 struct Lvalue_proxy { operator float() const; };
10155
10156 void
10157 test_1d (void) {
10158 Lvalue_proxy<Dense> p;
10159 float b;
10160 b + p;
10161 }
10162
10163 Here, if Lvalue_proxy is permitted to bind to View, then
10164 the global operator+ will be used; if they are not, the
10165 Lvalue_proxy will be converted to float. */
10166 if (coerce_template_parms (argtmplvec, parmvec,
10167 TYPE_TI_TEMPLATE (parm),
10168 tf_none,
10169 /*require_all_args=*/true,
10170 /*use_default_args=*/false)
10171 == error_mark_node)
10172 return 1;
10173
10174 /* Deduce arguments T, i from TT<T> or TT<i>.
10175 We check each element of PARMVEC and ARGVEC individually
10176 rather than the whole TREE_VEC since they can have
10177 different number of elements. */
10178
10179 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10180 {
10181 if (unify (tparms, targs,
10182 TREE_VEC_ELT (parmvec, i),
10183 TREE_VEC_ELT (argvec, i),
10184 UNIFY_ALLOW_NONE))
10185 return 1;
10186 }
10187 }
10188 arg = TYPE_TI_TEMPLATE (arg);
10189
10190 /* Fall through to deduce template name. */
10191 }
10192
10193 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10194 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10195 {
10196 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10197
10198 /* Simple cases: Value already set, does match or doesn't. */
10199 if (targ != NULL_TREE && template_args_equal (targ, arg))
10200 return 0;
10201 else if (targ)
10202 return 1;
10203 }
10204 else
10205 {
10206 /* If PARM is `const T' and ARG is only `int', we don't have
10207 a match unless we are allowing additional qualification.
10208 If ARG is `const int' and PARM is just `T' that's OK;
10209 that binds `const int' to `T'. */
10210 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10211 arg, parm))
10212 return 1;
10213
10214 /* Consider the case where ARG is `const volatile int' and
10215 PARM is `const T'. Then, T should be `volatile int'. */
10216 arg = cp_build_qualified_type_real
10217 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10218 if (arg == error_mark_node)
10219 return 1;
10220
10221 /* Simple cases: Value already set, does match or doesn't. */
10222 if (targ != NULL_TREE && same_type_p (targ, arg))
10223 return 0;
10224 else if (targ)
10225 return 1;
10226
10227 /* Make sure that ARG is not a variable-sized array. (Note
10228 that were talking about variable-sized arrays (like
10229 `int[n]'), rather than arrays of unknown size (like
10230 `int[]').) We'll get very confused by such a type since
10231 the bound of the array will not be computable in an
10232 instantiation. Besides, such types are not allowed in
10233 ISO C++, so we can do as we please here. */
10234 if (variably_modified_type_p (arg, NULL_TREE))
10235 return 1;
10236 }
10237
10238 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10239 return 0;
10240
10241 case TEMPLATE_PARM_INDEX:
10242 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10243
10244 if (TEMPLATE_PARM_LEVEL (parm)
10245 != template_decl_level (tparm))
10246 /* The PARM is not one we're trying to unify. Just check
10247 to see if it matches ARG. */
10248 return !(TREE_CODE (arg) == TREE_CODE (parm)
10249 && cp_tree_equal (parm, arg));
10250
10251 idx = TEMPLATE_PARM_IDX (parm);
10252 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10253
10254 if (targ)
10255 return !cp_tree_equal (targ, arg);
10256
10257 /* [temp.deduct.type] If, in the declaration of a function template
10258 with a non-type template-parameter, the non-type
10259 template-parameter is used in an expression in the function
10260 parameter-list and, if the corresponding template-argument is
10261 deduced, the template-argument type shall match the type of the
10262 template-parameter exactly, except that a template-argument
10263 deduced from an array bound may be of any integral type.
10264 The non-type parameter might use already deduced type parameters. */
10265 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10266 if (!TREE_TYPE (arg))
10267 /* Template-parameter dependent expression. Just accept it for now.
10268 It will later be processed in convert_template_argument. */
10269 ;
10270 else if (same_type_p (TREE_TYPE (arg), tparm))
10271 /* OK */;
10272 else if ((strict & UNIFY_ALLOW_INTEGER)
10273 && (TREE_CODE (tparm) == INTEGER_TYPE
10274 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10275 /* Convert the ARG to the type of PARM; the deduced non-type
10276 template argument must exactly match the types of the
10277 corresponding parameter. */
10278 arg = fold (build_nop (TREE_TYPE (parm), arg));
10279 else if (uses_template_parms (tparm))
10280 /* We haven't deduced the type of this parameter yet. Try again
10281 later. */
10282 return 0;
10283 else
10284 return 1;
10285
10286 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10287 return 0;
10288
10289 case PTRMEM_CST:
10290 {
10291 /* A pointer-to-member constant can be unified only with
10292 another constant. */
10293 if (TREE_CODE (arg) != PTRMEM_CST)
10294 return 1;
10295
10296 /* Just unify the class member. It would be useless (and possibly
10297 wrong, depending on the strict flags) to unify also
10298 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10299 arg refer to the same variable, even if through different
10300 classes. For instance:
10301
10302 struct A { int x; };
10303 struct B : A { };
10304
10305 Unification of &A::x and &B::x must succeed. */
10306 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10307 PTRMEM_CST_MEMBER (arg), strict);
10308 }
10309
10310 case POINTER_TYPE:
10311 {
10312 if (TREE_CODE (arg) != POINTER_TYPE)
10313 return 1;
10314
10315 /* [temp.deduct.call]
10316
10317 A can be another pointer or pointer to member type that can
10318 be converted to the deduced A via a qualification
10319 conversion (_conv.qual_).
10320
10321 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10322 This will allow for additional cv-qualification of the
10323 pointed-to types if appropriate. */
10324
10325 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10326 /* The derived-to-base conversion only persists through one
10327 level of pointers. */
10328 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10329
10330 return unify (tparms, targs, TREE_TYPE (parm),
10331 TREE_TYPE (arg), strict);
10332 }
10333
10334 case REFERENCE_TYPE:
10335 if (TREE_CODE (arg) != REFERENCE_TYPE)
10336 return 1;
10337 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10338 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10339
10340 case ARRAY_TYPE:
10341 if (TREE_CODE (arg) != ARRAY_TYPE)
10342 return 1;
10343 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10344 != (TYPE_DOMAIN (arg) == NULL_TREE))
10345 return 1;
10346 if (TYPE_DOMAIN (parm) != NULL_TREE)
10347 {
10348 tree parm_max;
10349 tree arg_max;
10350
10351 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10352 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10353
10354 /* Our representation of array types uses "N - 1" as the
10355 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10356 not an integer constant. */
10357 if (TREE_CODE (parm_max) == MINUS_EXPR)
10358 {
10359 arg_max = fold_build2 (PLUS_EXPR,
10360 integer_type_node,
10361 arg_max,
10362 TREE_OPERAND (parm_max, 1));
10363 parm_max = TREE_OPERAND (parm_max, 0);
10364 }
10365
10366 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10367 return 1;
10368 }
10369 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10370 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10371
10372 case REAL_TYPE:
10373 case COMPLEX_TYPE:
10374 case VECTOR_TYPE:
10375 case INTEGER_TYPE:
10376 case BOOLEAN_TYPE:
10377 case ENUMERAL_TYPE:
10378 case VOID_TYPE:
10379 if (TREE_CODE (arg) != TREE_CODE (parm))
10380 return 1;
10381
10382 /* We have already checked cv-qualification at the top of the
10383 function. */
10384 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10385 return 1;
10386
10387 /* As far as unification is concerned, this wins. Later checks
10388 will invalidate it if necessary. */
10389 return 0;
10390
10391 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10392 /* Type INTEGER_CST can come from ordinary constant template args. */
10393 case INTEGER_CST:
10394 while (TREE_CODE (arg) == NOP_EXPR)
10395 arg = TREE_OPERAND (arg, 0);
10396
10397 if (TREE_CODE (arg) != INTEGER_CST)
10398 return 1;
10399 return !tree_int_cst_equal (parm, arg);
10400
10401 case TREE_VEC:
10402 {
10403 int i;
10404 if (TREE_CODE (arg) != TREE_VEC)
10405 return 1;
10406 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10407 return 1;
10408 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10409 if (unify (tparms, targs,
10410 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10411 UNIFY_ALLOW_NONE))
10412 return 1;
10413 return 0;
10414 }
10415
10416 case RECORD_TYPE:
10417 case UNION_TYPE:
10418 if (TREE_CODE (arg) != TREE_CODE (parm))
10419 return 1;
10420
10421 if (TYPE_PTRMEMFUNC_P (parm))
10422 {
10423 if (!TYPE_PTRMEMFUNC_P (arg))
10424 return 1;
10425
10426 return unify (tparms, targs,
10427 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10428 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10429 strict);
10430 }
10431
10432 if (CLASSTYPE_TEMPLATE_INFO (parm))
10433 {
10434 tree t = NULL_TREE;
10435
10436 if (strict_in & UNIFY_ALLOW_DERIVED)
10437 {
10438 /* First, we try to unify the PARM and ARG directly. */
10439 t = try_class_unification (tparms, targs,
10440 parm, arg);
10441
10442 if (!t)
10443 {
10444 /* Fallback to the special case allowed in
10445 [temp.deduct.call]:
10446
10447 If P is a class, and P has the form
10448 template-id, then A can be a derived class of
10449 the deduced A. Likewise, if P is a pointer to
10450 a class of the form template-id, A can be a
10451 pointer to a derived class pointed to by the
10452 deduced A. */
10453 t = get_template_base (tparms, targs, parm, arg);
10454
10455 if (!t)
10456 return 1;
10457 }
10458 }
10459 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10460 && (CLASSTYPE_TI_TEMPLATE (parm)
10461 == CLASSTYPE_TI_TEMPLATE (arg)))
10462 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10463 Then, we should unify `int' and `U'. */
10464 t = arg;
10465 else
10466 /* There's no chance of unification succeeding. */
10467 return 1;
10468
10469 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10470 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10471 }
10472 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10473 return 1;
10474 return 0;
10475
10476 case METHOD_TYPE:
10477 case FUNCTION_TYPE:
10478 if (TREE_CODE (arg) != TREE_CODE (parm))
10479 return 1;
10480
10481 /* CV qualifications for methods can never be deduced, they must
10482 match exactly. We need to check them explicitly here,
10483 because type_unification_real treats them as any other
10484 cvqualified parameter. */
10485 if (TREE_CODE (parm) == METHOD_TYPE
10486 && (!check_cv_quals_for_unify
10487 (UNIFY_ALLOW_NONE,
10488 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10489 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10490 return 1;
10491
10492 if (unify (tparms, targs, TREE_TYPE (parm),
10493 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10494 return 1;
10495 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10496 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10497 LOOKUP_NORMAL);
10498
10499 case OFFSET_TYPE:
10500 /* Unify a pointer to member with a pointer to member function, which
10501 deduces the type of the member as a function type. */
10502 if (TYPE_PTRMEMFUNC_P (arg))
10503 {
10504 tree method_type;
10505 tree fntype;
10506 cp_cv_quals cv_quals;
10507
10508 /* Check top-level cv qualifiers */
10509 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10510 return 1;
10511
10512 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10513 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10514 return 1;
10515
10516 /* Determine the type of the function we are unifying against. */
10517 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10518 fntype =
10519 build_function_type (TREE_TYPE (method_type),
10520 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10521
10522 /* Extract the cv-qualifiers of the member function from the
10523 implicit object parameter and place them on the function
10524 type to be restored later. */
10525 cv_quals =
10526 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10527 fntype = build_qualified_type (fntype, cv_quals);
10528 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10529 }
10530
10531 if (TREE_CODE (arg) != OFFSET_TYPE)
10532 return 1;
10533 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10534 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10535 return 1;
10536 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10537 strict);
10538
10539 case CONST_DECL:
10540 if (DECL_TEMPLATE_PARM_P (parm))
10541 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10542 if (arg != integral_constant_value (parm))
10543 return 1;
10544 return 0;
10545
10546 case FIELD_DECL:
10547 case TEMPLATE_DECL:
10548 /* Matched cases are handled by the ARG == PARM test above. */
10549 return 1;
10550
10551 default:
10552 gcc_assert (EXPR_P (parm));
10553
10554 /* We must be looking at an expression. This can happen with
10555 something like:
10556
10557 template <int I>
10558 void foo(S<I>, S<I + 2>);
10559
10560 This is a "nondeduced context":
10561
10562 [deduct.type]
10563
10564 The nondeduced contexts are:
10565
10566 --A type that is a template-id in which one or more of
10567 the template-arguments is an expression that references
10568 a template-parameter.
10569
10570 In these cases, we assume deduction succeeded, but don't
10571 actually infer any unifications. */
10572
10573 if (!uses_template_parms (parm)
10574 && !template_args_equal (parm, arg))
10575 return 1;
10576 else
10577 return 0;
10578 }
10579 }
10580 \f
10581 /* Note that DECL can be defined in this translation unit, if
10582 required. */
10583
10584 static void
10585 mark_definable (tree decl)
10586 {
10587 tree clone;
10588 DECL_NOT_REALLY_EXTERN (decl) = 1;
10589 FOR_EACH_CLONE (clone, decl)
10590 DECL_NOT_REALLY_EXTERN (clone) = 1;
10591 }
10592
10593 /* Called if RESULT is explicitly instantiated, or is a member of an
10594 explicitly instantiated class. */
10595
10596 void
10597 mark_decl_instantiated (tree result, int extern_p)
10598 {
10599 SET_DECL_EXPLICIT_INSTANTIATION (result);
10600
10601 /* If this entity has already been written out, it's too late to
10602 make any modifications. */
10603 if (TREE_ASM_WRITTEN (result))
10604 return;
10605
10606 if (TREE_CODE (result) != FUNCTION_DECL)
10607 /* The TREE_PUBLIC flag for function declarations will have been
10608 set correctly by tsubst. */
10609 TREE_PUBLIC (result) = 1;
10610
10611 /* This might have been set by an earlier implicit instantiation. */
10612 DECL_COMDAT (result) = 0;
10613
10614 if (extern_p)
10615 DECL_NOT_REALLY_EXTERN (result) = 0;
10616 else
10617 {
10618 mark_definable (result);
10619 /* Always make artificials weak. */
10620 if (DECL_ARTIFICIAL (result) && flag_weak)
10621 comdat_linkage (result);
10622 /* For WIN32 we also want to put explicit instantiations in
10623 linkonce sections. */
10624 else if (TREE_PUBLIC (result))
10625 maybe_make_one_only (result);
10626 }
10627
10628 /* If EXTERN_P, then this function will not be emitted -- unless
10629 followed by an explicit instantiation, at which point its linkage
10630 will be adjusted. If !EXTERN_P, then this function will be
10631 emitted here. In neither circumstance do we want
10632 import_export_decl to adjust the linkage. */
10633 DECL_INTERFACE_KNOWN (result) = 1;
10634 }
10635
10636 /* Given two function templates PAT1 and PAT2, return:
10637
10638 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10639 -1 if PAT2 is more specialized than PAT1.
10640 0 if neither is more specialized.
10641
10642 LEN indicates the number of parameters we should consider
10643 (defaulted parameters should not be considered).
10644
10645 The 1998 std underspecified function template partial ordering, and
10646 DR214 addresses the issue. We take pairs of arguments, one from
10647 each of the templates, and deduce them against each other. One of
10648 the templates will be more specialized if all the *other*
10649 template's arguments deduce against its arguments and at least one
10650 of its arguments *does* *not* deduce against the other template's
10651 corresponding argument. Deduction is done as for class templates.
10652 The arguments used in deduction have reference and top level cv
10653 qualifiers removed. Iff both arguments were originally reference
10654 types *and* deduction succeeds in both directions, the template
10655 with the more cv-qualified argument wins for that pairing (if
10656 neither is more cv-qualified, they both are equal). Unlike regular
10657 deduction, after all the arguments have been deduced in this way,
10658 we do *not* verify the deduced template argument values can be
10659 substituted into non-deduced contexts, nor do we have to verify
10660 that all template arguments have been deduced. */
10661
10662 int
10663 more_specialized_fn (tree pat1, tree pat2, int len)
10664 {
10665 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10666 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10667 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10668 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10669 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10670 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10671 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10672 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10673 int better1 = 0;
10674 int better2 = 0;
10675
10676 /* Remove the this parameter from non-static member functions. If
10677 one is a non-static member function and the other is not a static
10678 member function, remove the first parameter from that function
10679 also. This situation occurs for operator functions where we
10680 locate both a member function (with this pointer) and non-member
10681 operator (with explicit first operand). */
10682 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10683 {
10684 len--; /* LEN is the number of significant arguments for DECL1 */
10685 args1 = TREE_CHAIN (args1);
10686 if (!DECL_STATIC_FUNCTION_P (decl2))
10687 args2 = TREE_CHAIN (args2);
10688 }
10689 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10690 {
10691 args2 = TREE_CHAIN (args2);
10692 if (!DECL_STATIC_FUNCTION_P (decl1))
10693 {
10694 len--;
10695 args1 = TREE_CHAIN (args1);
10696 }
10697 }
10698
10699 /* If only one is a conversion operator, they are unordered. */
10700 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10701 return 0;
10702
10703 /* Consider the return type for a conversion function */
10704 if (DECL_CONV_FN_P (decl1))
10705 {
10706 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10707 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10708 len++;
10709 }
10710
10711 processing_template_decl++;
10712
10713 while (len--)
10714 {
10715 tree arg1 = TREE_VALUE (args1);
10716 tree arg2 = TREE_VALUE (args2);
10717 int deduce1, deduce2;
10718 int quals1 = -1;
10719 int quals2 = -1;
10720
10721 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10722 {
10723 arg1 = TREE_TYPE (arg1);
10724 quals1 = cp_type_quals (arg1);
10725 }
10726
10727 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10728 {
10729 arg2 = TREE_TYPE (arg2);
10730 quals2 = cp_type_quals (arg2);
10731 }
10732
10733 if ((quals1 < 0) != (quals2 < 0))
10734 {
10735 /* Only of the args is a reference, see if we should apply
10736 array/function pointer decay to it. This is not part of
10737 DR214, but is, IMHO, consistent with the deduction rules
10738 for the function call itself, and with our earlier
10739 implementation of the underspecified partial ordering
10740 rules. (nathan). */
10741 if (quals1 >= 0)
10742 {
10743 switch (TREE_CODE (arg1))
10744 {
10745 case ARRAY_TYPE:
10746 arg1 = TREE_TYPE (arg1);
10747 /* FALLTHROUGH. */
10748 case FUNCTION_TYPE:
10749 arg1 = build_pointer_type (arg1);
10750 break;
10751
10752 default:
10753 break;
10754 }
10755 }
10756 else
10757 {
10758 switch (TREE_CODE (arg2))
10759 {
10760 case ARRAY_TYPE:
10761 arg2 = TREE_TYPE (arg2);
10762 /* FALLTHROUGH. */
10763 case FUNCTION_TYPE:
10764 arg2 = build_pointer_type (arg2);
10765 break;
10766
10767 default:
10768 break;
10769 }
10770 }
10771 }
10772
10773 arg1 = TYPE_MAIN_VARIANT (arg1);
10774 arg2 = TYPE_MAIN_VARIANT (arg2);
10775
10776 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10777 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10778
10779 if (!deduce1)
10780 better2 = -1;
10781 if (!deduce2)
10782 better1 = -1;
10783 if (better1 < 0 && better2 < 0)
10784 /* We've failed to deduce something in either direction.
10785 These must be unordered. */
10786 break;
10787
10788 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10789 {
10790 /* Deduces in both directions, see if quals can
10791 disambiguate. Pretend the worse one failed to deduce. */
10792 if ((quals1 & quals2) == quals2)
10793 deduce1 = 0;
10794 if ((quals1 & quals2) == quals1)
10795 deduce2 = 0;
10796 }
10797 if (deduce1 && !deduce2 && !better2)
10798 better2 = 1;
10799 if (deduce2 && !deduce1 && !better1)
10800 better1 = 1;
10801
10802 args1 = TREE_CHAIN (args1);
10803 args2 = TREE_CHAIN (args2);
10804 }
10805
10806 processing_template_decl--;
10807
10808 return (better1 > 0) - (better2 > 0);
10809 }
10810
10811 /* Determine which of two partial specializations is more specialized.
10812
10813 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
10814 to the first partial specialization. The TREE_VALUE is the
10815 innermost set of template parameters for the partial
10816 specialization. PAT2 is similar, but for the second template.
10817
10818 Return 1 if the first partial specialization is more specialized;
10819 -1 if the second is more specialized; 0 if neither is more
10820 specialized.
10821
10822 See [temp.class.order] for information about determining which of
10823 two templates is more specialized. */
10824
10825 static int
10826 more_specialized_class (tree pat1, tree pat2)
10827 {
10828 tree targs;
10829 tree tmpl1, tmpl2;
10830 int winner = 0;
10831
10832 tmpl1 = TREE_TYPE (pat1);
10833 tmpl2 = TREE_TYPE (pat2);
10834
10835 /* Just like what happens for functions, if we are ordering between
10836 different class template specializations, we may encounter dependent
10837 types in the arguments, and we need our dependency check functions
10838 to behave correctly. */
10839 ++processing_template_decl;
10840 targs = get_class_bindings (TREE_VALUE (pat1),
10841 CLASSTYPE_TI_ARGS (tmpl1),
10842 CLASSTYPE_TI_ARGS (tmpl2));
10843 if (targs)
10844 --winner;
10845
10846 targs = get_class_bindings (TREE_VALUE (pat2),
10847 CLASSTYPE_TI_ARGS (tmpl2),
10848 CLASSTYPE_TI_ARGS (tmpl1));
10849 if (targs)
10850 ++winner;
10851 --processing_template_decl;
10852
10853 return winner;
10854 }
10855
10856 /* Return the template arguments that will produce the function signature
10857 DECL from the function template FN, with the explicit template
10858 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
10859 also match. Return NULL_TREE if no satisfactory arguments could be
10860 found. */
10861
10862 static tree
10863 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
10864 {
10865 int ntparms = DECL_NTPARMS (fn);
10866 tree targs = make_tree_vec (ntparms);
10867 tree decl_type;
10868 tree decl_arg_types;
10869
10870 /* Substitute the explicit template arguments into the type of DECL.
10871 The call to fn_type_unification will handle substitution into the
10872 FN. */
10873 decl_type = TREE_TYPE (decl);
10874 if (explicit_args && uses_template_parms (decl_type))
10875 {
10876 tree tmpl;
10877 tree converted_args;
10878
10879 if (DECL_TEMPLATE_INFO (decl))
10880 tmpl = DECL_TI_TEMPLATE (decl);
10881 else
10882 /* We can get here for some invalid specializations. */
10883 return NULL_TREE;
10884
10885 converted_args
10886 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10887 explicit_args, NULL_TREE,
10888 tf_none,
10889 /*require_all_args=*/false,
10890 /*use_default_args=*/false);
10891 if (converted_args == error_mark_node)
10892 return NULL_TREE;
10893
10894 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10895 if (decl_type == error_mark_node)
10896 return NULL_TREE;
10897 }
10898
10899 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10900 /* Never do unification on the 'this' parameter. */
10901 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10902 decl_arg_types = TREE_CHAIN (decl_arg_types);
10903
10904 if (fn_type_unification (fn, explicit_args, targs,
10905 decl_arg_types,
10906 (check_rettype || DECL_CONV_FN_P (fn)
10907 ? TREE_TYPE (decl_type) : NULL_TREE),
10908 DEDUCE_EXACT, LOOKUP_NORMAL))
10909 return NULL_TREE;
10910
10911 return targs;
10912 }
10913
10914 /* Return the innermost template arguments that, when applied to a
10915 template specialization whose innermost template parameters are
10916 TPARMS, and whose specialization arguments are PARMS, yield the
10917 ARGS.
10918
10919 For example, suppose we have:
10920
10921 template <class T, class U> struct S {};
10922 template <class T> struct S<T*, int> {};
10923
10924 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10925 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
10926 int}. The resulting vector will be {double}, indicating that `T'
10927 is bound to `double'. */
10928
10929 static tree
10930 get_class_bindings (tree tparms, tree spec_args, tree args)
10931 {
10932 int i, ntparms = TREE_VEC_LENGTH (tparms);
10933 tree deduced_args;
10934 tree innermost_deduced_args;
10935
10936 innermost_deduced_args = make_tree_vec (ntparms);
10937 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10938 {
10939 deduced_args = copy_node (args);
10940 SET_TMPL_ARGS_LEVEL (deduced_args,
10941 TMPL_ARGS_DEPTH (deduced_args),
10942 innermost_deduced_args);
10943 }
10944 else
10945 deduced_args = innermost_deduced_args;
10946
10947 if (unify (tparms, deduced_args,
10948 INNERMOST_TEMPLATE_ARGS (spec_args),
10949 INNERMOST_TEMPLATE_ARGS (args),
10950 UNIFY_ALLOW_NONE))
10951 return NULL_TREE;
10952
10953 for (i = 0; i < ntparms; ++i)
10954 if (! TREE_VEC_ELT (innermost_deduced_args, i))
10955 return NULL_TREE;
10956
10957 /* Verify that nondeduced template arguments agree with the type
10958 obtained from argument deduction.
10959
10960 For example:
10961
10962 struct A { typedef int X; };
10963 template <class T, class U> struct C {};
10964 template <class T> struct C<T, typename T::X> {};
10965
10966 Then with the instantiation `C<A, int>', we can deduce that
10967 `T' is `A' but unify () does not check whether `typename T::X'
10968 is `int'. */
10969 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
10970 if (spec_args == error_mark_node
10971 /* We only need to check the innermost arguments; the other
10972 arguments will always agree. */
10973 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
10974 INNERMOST_TEMPLATE_ARGS (args)))
10975 return NULL_TREE;
10976
10977 return deduced_args;
10978 }
10979
10980 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
10981 Return the TREE_LIST node with the most specialized template, if
10982 any. If there is no most specialized template, the error_mark_node
10983 is returned.
10984
10985 Note that this function does not look at, or modify, the
10986 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
10987 returned is one of the elements of INSTANTIATIONS, callers may
10988 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
10989 and retrieve it from the value returned. */
10990
10991 tree
10992 most_specialized_instantiation (tree templates)
10993 {
10994 tree fn, champ;
10995
10996 ++processing_template_decl;
10997
10998 champ = templates;
10999 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
11000 {
11001 int fate = 0;
11002
11003 if (get_bindings (TREE_VALUE (champ),
11004 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11005 NULL_TREE, /*check_ret=*/false))
11006 fate--;
11007
11008 if (get_bindings (TREE_VALUE (fn),
11009 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11010 NULL_TREE, /*check_ret=*/false))
11011 fate++;
11012
11013 if (fate == -1)
11014 champ = fn;
11015 else if (!fate)
11016 {
11017 /* Equally specialized, move to next function. If there
11018 is no next function, nothing's most specialized. */
11019 fn = TREE_CHAIN (fn);
11020 champ = fn;
11021 if (!fn)
11022 break;
11023 }
11024 }
11025
11026 if (champ)
11027 /* Now verify that champ is better than everything earlier in the
11028 instantiation list. */
11029 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11030 if (get_bindings (TREE_VALUE (champ),
11031 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11032 NULL_TREE, /*check_ret=*/false)
11033 || !get_bindings (TREE_VALUE (fn),
11034 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11035 NULL_TREE, /*check_ret=*/false))
11036 {
11037 champ = NULL_TREE;
11038 break;
11039 }
11040
11041 processing_template_decl--;
11042
11043 if (!champ)
11044 return error_mark_node;
11045
11046 return champ;
11047 }
11048
11049 /* If DECL is a specialization of some template, return the most
11050 general such template. Otherwise, returns NULL_TREE.
11051
11052 For example, given:
11053
11054 template <class T> struct S { template <class U> void f(U); };
11055
11056 if TMPL is `template <class U> void S<int>::f(U)' this will return
11057 the full template. This function will not trace past partial
11058 specializations, however. For example, given in addition:
11059
11060 template <class T> struct S<T*> { template <class U> void f(U); };
11061
11062 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11063 `template <class T> template <class U> S<T*>::f(U)'. */
11064
11065 tree
11066 most_general_template (tree decl)
11067 {
11068 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11069 an immediate specialization. */
11070 if (TREE_CODE (decl) == FUNCTION_DECL)
11071 {
11072 if (DECL_TEMPLATE_INFO (decl)) {
11073 decl = DECL_TI_TEMPLATE (decl);
11074
11075 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11076 template friend. */
11077 if (TREE_CODE (decl) != TEMPLATE_DECL)
11078 return NULL_TREE;
11079 } else
11080 return NULL_TREE;
11081 }
11082
11083 /* Look for more and more general templates. */
11084 while (DECL_TEMPLATE_INFO (decl))
11085 {
11086 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11087 (See cp-tree.h for details.) */
11088 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11089 break;
11090
11091 if (CLASS_TYPE_P (TREE_TYPE (decl))
11092 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11093 break;
11094
11095 /* Stop if we run into an explicitly specialized class template. */
11096 if (!DECL_NAMESPACE_SCOPE_P (decl)
11097 && DECL_CONTEXT (decl)
11098 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11099 break;
11100
11101 decl = DECL_TI_TEMPLATE (decl);
11102 }
11103
11104 return decl;
11105 }
11106
11107 /* Return the most specialized of the class template partial
11108 specializations of TMPL which can produce TYPE, a specialization of
11109 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11110 a _TYPE node corresponding to the partial specialization, while the
11111 TREE_PURPOSE is the set of template arguments that must be
11112 substituted into the TREE_TYPE in order to generate TYPE.
11113
11114 If the choice of partial specialization is ambiguous, a diagnostic
11115 is issued, and the error_mark_node is returned. If there are no
11116 partial specializations of TMPL matching TYPE, then NULL_TREE is
11117 returned. */
11118
11119 static tree
11120 most_specialized_class (tree type, tree tmpl)
11121 {
11122 tree list = NULL_TREE;
11123 tree t;
11124 tree champ;
11125 int fate;
11126 bool ambiguous_p;
11127 tree args;
11128
11129 tmpl = most_general_template (tmpl);
11130 args = CLASSTYPE_TI_ARGS (type);
11131 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11132 {
11133 tree partial_spec_args;
11134 tree spec_args;
11135
11136 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11137 spec_args = get_class_bindings (TREE_VALUE (t),
11138 partial_spec_args,
11139 args);
11140 if (spec_args)
11141 {
11142 list = tree_cons (spec_args, TREE_VALUE (t), list);
11143 TREE_TYPE (list) = TREE_TYPE (t);
11144 }
11145 }
11146
11147 if (! list)
11148 return NULL_TREE;
11149
11150 ambiguous_p = false;
11151 t = list;
11152 champ = t;
11153 t = TREE_CHAIN (t);
11154 for (; t; t = TREE_CHAIN (t))
11155 {
11156 fate = more_specialized_class (champ, t);
11157 if (fate == 1)
11158 ;
11159 else
11160 {
11161 if (fate == 0)
11162 {
11163 t = TREE_CHAIN (t);
11164 if (! t)
11165 {
11166 ambiguous_p = true;
11167 break;
11168 }
11169 }
11170 champ = t;
11171 }
11172 }
11173
11174 if (!ambiguous_p)
11175 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11176 {
11177 fate = more_specialized_class (champ, t);
11178 if (fate != 1)
11179 {
11180 ambiguous_p = true;
11181 break;
11182 }
11183 }
11184
11185 if (ambiguous_p)
11186 {
11187 const char *str = "candidates are:";
11188 error ("ambiguous class template instantiation for %q#T", type);
11189 for (t = list; t; t = TREE_CHAIN (t))
11190 {
11191 error ("%s %+#T", str, TREE_TYPE (t));
11192 str = " ";
11193 }
11194 return error_mark_node;
11195 }
11196
11197 return champ;
11198 }
11199
11200 /* Explicitly instantiate DECL. */
11201
11202 void
11203 do_decl_instantiation (tree decl, tree storage)
11204 {
11205 tree result = NULL_TREE;
11206 int extern_p = 0;
11207
11208 if (!decl)
11209 /* An error occurred, for which grokdeclarator has already issued
11210 an appropriate message. */
11211 return;
11212 else if (! DECL_LANG_SPECIFIC (decl))
11213 {
11214 error ("explicit instantiation of non-template %q#D", decl);
11215 return;
11216 }
11217 else if (TREE_CODE (decl) == VAR_DECL)
11218 {
11219 /* There is an asymmetry here in the way VAR_DECLs and
11220 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11221 the latter, the DECL we get back will be marked as a
11222 template instantiation, and the appropriate
11223 DECL_TEMPLATE_INFO will be set up. This does not happen for
11224 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11225 should handle VAR_DECLs as it currently handles
11226 FUNCTION_DECLs. */
11227 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11228 if (!result || TREE_CODE (result) != VAR_DECL)
11229 {
11230 error ("no matching template for %qD found", decl);
11231 return;
11232 }
11233 }
11234 else if (TREE_CODE (decl) != FUNCTION_DECL)
11235 {
11236 error ("explicit instantiation of %q#D", decl);
11237 return;
11238 }
11239 else
11240 result = decl;
11241
11242 /* Check for various error cases. Note that if the explicit
11243 instantiation is valid the RESULT will currently be marked as an
11244 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11245 until we get here. */
11246
11247 if (DECL_TEMPLATE_SPECIALIZATION (result))
11248 {
11249 /* DR 259 [temp.spec].
11250
11251 Both an explicit instantiation and a declaration of an explicit
11252 specialization shall not appear in a program unless the explicit
11253 instantiation follows a declaration of the explicit specialization.
11254
11255 For a given set of template parameters, if an explicit
11256 instantiation of a template appears after a declaration of an
11257 explicit specialization for that template, the explicit
11258 instantiation has no effect. */
11259 return;
11260 }
11261 else if (DECL_EXPLICIT_INSTANTIATION (result))
11262 {
11263 /* [temp.spec]
11264
11265 No program shall explicitly instantiate any template more
11266 than once.
11267
11268 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11269 the first instantiation was `extern' and the second is not,
11270 and EXTERN_P for the opposite case. */
11271 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11272 pedwarn ("duplicate explicit instantiation of %q#D", result);
11273 /* If an "extern" explicit instantiation follows an ordinary
11274 explicit instantiation, the template is instantiated. */
11275 if (extern_p)
11276 return;
11277 }
11278 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11279 {
11280 error ("no matching template for %qD found", result);
11281 return;
11282 }
11283 else if (!DECL_TEMPLATE_INFO (result))
11284 {
11285 pedwarn ("explicit instantiation of non-template %q#D", result);
11286 return;
11287 }
11288
11289 if (storage == NULL_TREE)
11290 ;
11291 else if (storage == ridpointers[(int) RID_EXTERN])
11292 {
11293 if (pedantic && !in_system_header)
11294 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11295 "instantiations");
11296 extern_p = 1;
11297 }
11298 else
11299 error ("storage class %qD applied to template instantiation", storage);
11300
11301 check_explicit_instantiation_namespace (result);
11302 mark_decl_instantiated (result, extern_p);
11303 if (! extern_p)
11304 instantiate_decl (result, /*defer_ok=*/1,
11305 /*expl_inst_class_mem_p=*/false);
11306 }
11307
11308 static void
11309 mark_class_instantiated (tree t, int extern_p)
11310 {
11311 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11312 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11313 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11314 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11315 if (! extern_p)
11316 {
11317 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11318 rest_of_type_compilation (t, 1);
11319 }
11320 }
11321
11322 /* Called from do_type_instantiation through binding_table_foreach to
11323 do recursive instantiation for the type bound in ENTRY. */
11324 static void
11325 bt_instantiate_type_proc (binding_entry entry, void *data)
11326 {
11327 tree storage = *(tree *) data;
11328
11329 if (IS_AGGR_TYPE (entry->type)
11330 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11331 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11332 }
11333
11334 /* Called from do_type_instantiation to instantiate a member
11335 (a member function or a static member variable) of an
11336 explicitly instantiated class template. */
11337 static void
11338 instantiate_class_member (tree decl, int extern_p)
11339 {
11340 mark_decl_instantiated (decl, extern_p);
11341 if (! extern_p)
11342 instantiate_decl (decl, /*defer_ok=*/1,
11343 /*expl_inst_class_mem_p=*/true);
11344 }
11345
11346 /* Perform an explicit instantiation of template class T. STORAGE, if
11347 non-null, is the RID for extern, inline or static. COMPLAIN is
11348 nonzero if this is called from the parser, zero if called recursively,
11349 since the standard is unclear (as detailed below). */
11350
11351 void
11352 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11353 {
11354 int extern_p = 0;
11355 int nomem_p = 0;
11356 int static_p = 0;
11357 int previous_instantiation_extern_p = 0;
11358
11359 if (TREE_CODE (t) == TYPE_DECL)
11360 t = TREE_TYPE (t);
11361
11362 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11363 {
11364 error ("explicit instantiation of non-template type %qT", t);
11365 return;
11366 }
11367
11368 complete_type (t);
11369
11370 if (!COMPLETE_TYPE_P (t))
11371 {
11372 if (complain & tf_error)
11373 error ("explicit instantiation of %q#T before definition of template",
11374 t);
11375 return;
11376 }
11377
11378 if (storage != NULL_TREE)
11379 {
11380 if (pedantic && !in_system_header)
11381 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11382 storage);
11383
11384 if (storage == ridpointers[(int) RID_INLINE])
11385 nomem_p = 1;
11386 else if (storage == ridpointers[(int) RID_EXTERN])
11387 extern_p = 1;
11388 else if (storage == ridpointers[(int) RID_STATIC])
11389 static_p = 1;
11390 else
11391 {
11392 error ("storage class %qD applied to template instantiation",
11393 storage);
11394 extern_p = 0;
11395 }
11396 }
11397
11398 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11399 {
11400 /* DR 259 [temp.spec].
11401
11402 Both an explicit instantiation and a declaration of an explicit
11403 specialization shall not appear in a program unless the explicit
11404 instantiation follows a declaration of the explicit specialization.
11405
11406 For a given set of template parameters, if an explicit
11407 instantiation of a template appears after a declaration of an
11408 explicit specialization for that template, the explicit
11409 instantiation has no effect. */
11410 return;
11411 }
11412 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11413 {
11414 /* [temp.spec]
11415
11416 No program shall explicitly instantiate any template more
11417 than once.
11418
11419 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11420 instantiation was `extern'. If EXTERN_P then the second is.
11421 These cases are OK. */
11422 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11423
11424 if (!previous_instantiation_extern_p && !extern_p
11425 && (complain & tf_error))
11426 pedwarn ("duplicate explicit instantiation of %q#T", t);
11427
11428 /* If we've already instantiated the template, just return now. */
11429 if (!CLASSTYPE_INTERFACE_ONLY (t))
11430 return;
11431 }
11432
11433 check_explicit_instantiation_namespace (TYPE_NAME (t));
11434 mark_class_instantiated (t, extern_p);
11435
11436 if (nomem_p)
11437 return;
11438
11439 {
11440 tree tmp;
11441
11442 /* In contrast to implicit instantiation, where only the
11443 declarations, and not the definitions, of members are
11444 instantiated, we have here:
11445
11446 [temp.explicit]
11447
11448 The explicit instantiation of a class template specialization
11449 implies the instantiation of all of its members not
11450 previously explicitly specialized in the translation unit
11451 containing the explicit instantiation.
11452
11453 Of course, we can't instantiate member template classes, since
11454 we don't have any arguments for them. Note that the standard
11455 is unclear on whether the instantiation of the members are
11456 *explicit* instantiations or not. However, the most natural
11457 interpretation is that it should be an explicit instantiation. */
11458
11459 if (! static_p)
11460 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11461 if (TREE_CODE (tmp) == FUNCTION_DECL
11462 && DECL_TEMPLATE_INSTANTIATION (tmp))
11463 instantiate_class_member (tmp, extern_p);
11464
11465 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11466 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11467 instantiate_class_member (tmp, extern_p);
11468
11469 if (CLASSTYPE_NESTED_UTDS (t))
11470 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11471 bt_instantiate_type_proc, &storage);
11472 }
11473 }
11474
11475 /* Given a function DECL, which is a specialization of TMPL, modify
11476 DECL to be a re-instantiation of TMPL with the same template
11477 arguments. TMPL should be the template into which tsubst'ing
11478 should occur for DECL, not the most general template.
11479
11480 One reason for doing this is a scenario like this:
11481
11482 template <class T>
11483 void f(const T&, int i);
11484
11485 void g() { f(3, 7); }
11486
11487 template <class T>
11488 void f(const T& t, const int i) { }
11489
11490 Note that when the template is first instantiated, with
11491 instantiate_template, the resulting DECL will have no name for the
11492 first parameter, and the wrong type for the second. So, when we go
11493 to instantiate the DECL, we regenerate it. */
11494
11495 static void
11496 regenerate_decl_from_template (tree decl, tree tmpl)
11497 {
11498 /* The arguments used to instantiate DECL, from the most general
11499 template. */
11500 tree args;
11501 tree code_pattern;
11502
11503 args = DECL_TI_ARGS (decl);
11504 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11505
11506 /* Make sure that we can see identifiers, and compute access
11507 correctly. */
11508 push_access_scope (decl);
11509
11510 if (TREE_CODE (decl) == FUNCTION_DECL)
11511 {
11512 tree decl_parm;
11513 tree pattern_parm;
11514 tree specs;
11515 int args_depth;
11516 int parms_depth;
11517
11518 args_depth = TMPL_ARGS_DEPTH (args);
11519 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11520 if (args_depth > parms_depth)
11521 args = get_innermost_template_args (args, parms_depth);
11522
11523 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11524 args, tf_error, NULL_TREE);
11525 if (specs)
11526 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11527 specs);
11528
11529 /* Merge parameter declarations. */
11530 decl_parm = skip_artificial_parms_for (decl,
11531 DECL_ARGUMENTS (decl));
11532 pattern_parm
11533 = skip_artificial_parms_for (code_pattern,
11534 DECL_ARGUMENTS (code_pattern));
11535 while (decl_parm)
11536 {
11537 tree parm_type;
11538 tree attributes;
11539
11540 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11541 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11542 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11543 NULL_TREE);
11544 parm_type = type_decays_to (parm_type);
11545 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11546 TREE_TYPE (decl_parm) = parm_type;
11547 attributes = DECL_ATTRIBUTES (pattern_parm);
11548 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11549 {
11550 DECL_ATTRIBUTES (decl_parm) = attributes;
11551 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11552 }
11553 decl_parm = TREE_CHAIN (decl_parm);
11554 pattern_parm = TREE_CHAIN (pattern_parm);
11555 }
11556
11557 /* Merge additional specifiers from the CODE_PATTERN. */
11558 if (DECL_DECLARED_INLINE_P (code_pattern)
11559 && !DECL_DECLARED_INLINE_P (decl))
11560 DECL_DECLARED_INLINE_P (decl) = 1;
11561 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11562 DECL_INLINE (decl) = 1;
11563 }
11564 else if (TREE_CODE (decl) == VAR_DECL)
11565 DECL_INITIAL (decl) =
11566 tsubst_expr (DECL_INITIAL (code_pattern), args,
11567 tf_error, DECL_TI_TEMPLATE (decl));
11568 else
11569 gcc_unreachable ();
11570
11571 pop_access_scope (decl);
11572 }
11573
11574 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11575 substituted to get DECL. */
11576
11577 tree
11578 template_for_substitution (tree decl)
11579 {
11580 tree tmpl = DECL_TI_TEMPLATE (decl);
11581
11582 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11583 for the instantiation. This is not always the most general
11584 template. Consider, for example:
11585
11586 template <class T>
11587 struct S { template <class U> void f();
11588 template <> void f<int>(); };
11589
11590 and an instantiation of S<double>::f<int>. We want TD to be the
11591 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11592 while (/* An instantiation cannot have a definition, so we need a
11593 more general template. */
11594 DECL_TEMPLATE_INSTANTIATION (tmpl)
11595 /* We must also deal with friend templates. Given:
11596
11597 template <class T> struct S {
11598 template <class U> friend void f() {};
11599 };
11600
11601 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11602 so far as the language is concerned, but that's still
11603 where we get the pattern for the instantiation from. On
11604 other hand, if the definition comes outside the class, say:
11605
11606 template <class T> struct S {
11607 template <class U> friend void f();
11608 };
11609 template <class U> friend void f() {}
11610
11611 we don't need to look any further. That's what the check for
11612 DECL_INITIAL is for. */
11613 || (TREE_CODE (decl) == FUNCTION_DECL
11614 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11615 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11616 {
11617 /* The present template, TD, should not be a definition. If it
11618 were a definition, we should be using it! Note that we
11619 cannot restructure the loop to just keep going until we find
11620 a template with a definition, since that might go too far if
11621 a specialization was declared, but not defined. */
11622 gcc_assert (TREE_CODE (decl) != VAR_DECL
11623 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11624
11625 /* Fetch the more general template. */
11626 tmpl = DECL_TI_TEMPLATE (tmpl);
11627 }
11628
11629 return tmpl;
11630 }
11631
11632 /* Produce the definition of D, a _DECL generated from a template. If
11633 DEFER_OK is nonzero, then we don't have to actually do the
11634 instantiation now; we just have to do it sometime. Normally it is
11635 an error if this is an explicit instantiation but D is undefined.
11636 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11637 explicitly instantiated class template. */
11638
11639 tree
11640 instantiate_decl (tree d, int defer_ok,
11641 bool expl_inst_class_mem_p)
11642 {
11643 tree tmpl = DECL_TI_TEMPLATE (d);
11644 tree gen_args;
11645 tree args;
11646 tree td;
11647 tree code_pattern;
11648 tree spec;
11649 tree gen_tmpl;
11650 bool pattern_defined;
11651 int need_push;
11652 location_t saved_loc = input_location;
11653 bool external_p;
11654
11655 /* This function should only be used to instantiate templates for
11656 functions and static member variables. */
11657 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11658 || TREE_CODE (d) == VAR_DECL);
11659
11660 /* Variables are never deferred; if instantiation is required, they
11661 are instantiated right away. That allows for better code in the
11662 case that an expression refers to the value of the variable --
11663 if the variable has a constant value the referring expression can
11664 take advantage of that fact. */
11665 if (TREE_CODE (d) == VAR_DECL)
11666 defer_ok = 0;
11667
11668 /* Don't instantiate cloned functions. Instead, instantiate the
11669 functions they cloned. */
11670 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11671 d = DECL_CLONED_FUNCTION (d);
11672
11673 if (DECL_TEMPLATE_INSTANTIATED (d))
11674 /* D has already been instantiated. It might seem reasonable to
11675 check whether or not D is an explicit instantiation, and, if so,
11676 stop here. But when an explicit instantiation is deferred
11677 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11678 is set, even though we still need to do the instantiation. */
11679 return d;
11680
11681 /* If we already have a specialization of this declaration, then
11682 there's no reason to instantiate it. Note that
11683 retrieve_specialization gives us both instantiations and
11684 specializations, so we must explicitly check
11685 DECL_TEMPLATE_SPECIALIZATION. */
11686 gen_tmpl = most_general_template (tmpl);
11687 gen_args = DECL_TI_ARGS (d);
11688 spec = retrieve_specialization (gen_tmpl, gen_args,
11689 /*class_specializations_p=*/false);
11690 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11691 return spec;
11692
11693 /* This needs to happen before any tsubsting. */
11694 if (! push_tinst_level (d))
11695 return d;
11696
11697 timevar_push (TV_PARSE);
11698
11699 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11700 for the instantiation. */
11701 td = template_for_substitution (d);
11702 code_pattern = DECL_TEMPLATE_RESULT (td);
11703
11704 /* We should never be trying to instantiate a member of a class
11705 template or partial specialization. */
11706 gcc_assert (d != code_pattern);
11707
11708 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11709 || DECL_TEMPLATE_SPECIALIZATION (td))
11710 /* In the case of a friend template whose definition is provided
11711 outside the class, we may have too many arguments. Drop the
11712 ones we don't need. The same is true for specializations. */
11713 args = get_innermost_template_args
11714 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11715 else
11716 args = gen_args;
11717
11718 if (TREE_CODE (d) == FUNCTION_DECL)
11719 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11720 else
11721 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11722
11723 /* We may be in the middle of deferred access check. Disable it now. */
11724 push_deferring_access_checks (dk_no_deferred);
11725
11726 /* Unless an explicit instantiation directive has already determined
11727 the linkage of D, remember that a definition is available for
11728 this entity. */
11729 if (pattern_defined
11730 && !DECL_INTERFACE_KNOWN (d)
11731 && !DECL_NOT_REALLY_EXTERN (d))
11732 mark_definable (d);
11733
11734 input_location = DECL_SOURCE_LOCATION (d);
11735
11736 /* If D is a member of an explicitly instantiated class template,
11737 and no definition is available, treat it like an implicit
11738 instantiation. */
11739 if (!pattern_defined && expl_inst_class_mem_p
11740 && DECL_EXPLICIT_INSTANTIATION (d))
11741 {
11742 DECL_NOT_REALLY_EXTERN (d) = 0;
11743 DECL_INTERFACE_KNOWN (d) = 0;
11744 SET_DECL_IMPLICIT_INSTANTIATION (d);
11745 }
11746
11747 if (!defer_ok)
11748 {
11749 /* Recheck the substitutions to obtain any warning messages
11750 about ignoring cv qualifiers. */
11751 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11752 tree type = TREE_TYPE (gen);
11753
11754 /* Make sure that we can see identifiers, and compute access
11755 correctly. D is already the target FUNCTION_DECL with the
11756 right context. */
11757 push_access_scope (d);
11758
11759 if (TREE_CODE (gen) == FUNCTION_DECL)
11760 {
11761 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
11762 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11763 tf_warning_or_error, d);
11764 /* Don't simply tsubst the function type, as that will give
11765 duplicate warnings about poor parameter qualifications.
11766 The function arguments are the same as the decl_arguments
11767 without the top level cv qualifiers. */
11768 type = TREE_TYPE (type);
11769 }
11770 tsubst (type, gen_args, tf_warning_or_error, d);
11771
11772 pop_access_scope (d);
11773 }
11774
11775 /* Check to see whether we know that this template will be
11776 instantiated in some other file, as with "extern template"
11777 extension. */
11778 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
11779 /* In general, we do not instantiate such templates... */
11780 if (external_p
11781 /* ... but we instantiate inline functions so that we can inline
11782 them and ... */
11783 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
11784 /* ... we instantiate static data members whose values are
11785 needed in integral constant expressions. */
11786 && ! (TREE_CODE (d) == VAR_DECL
11787 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
11788 goto out;
11789 /* Defer all other templates, unless we have been explicitly
11790 forbidden from doing so. */
11791 if (/* If there is no definition, we cannot instantiate the
11792 template. */
11793 ! pattern_defined
11794 /* If it's OK to postpone instantiation, do so. */
11795 || defer_ok
11796 /* If this is a static data member that will be defined
11797 elsewhere, we don't want to instantiate the entire data
11798 member, but we do want to instantiate the initializer so that
11799 we can substitute that elsewhere. */
11800 || (external_p && TREE_CODE (d) == VAR_DECL))
11801 {
11802 /* The definition of the static data member is now required so
11803 we must substitute the initializer. */
11804 if (TREE_CODE (d) == VAR_DECL
11805 && !DECL_INITIAL (d)
11806 && DECL_INITIAL (code_pattern))
11807 {
11808 tree ns;
11809 tree init;
11810
11811 ns = decl_namespace_context (d);
11812 push_nested_namespace (ns);
11813 push_nested_class (DECL_CONTEXT (d));
11814 init = tsubst_expr (DECL_INITIAL (code_pattern),
11815 args,
11816 tf_warning_or_error, NULL_TREE);
11817 DECL_INITIAL (d) = init;
11818 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
11819 /*asmspec_tree=*/NULL_TREE,
11820 LOOKUP_ONLYCONVERTING);
11821 pop_nested_class ();
11822 pop_nested_namespace (ns);
11823 }
11824
11825 /* We restore the source position here because it's used by
11826 add_pending_template. */
11827 input_location = saved_loc;
11828
11829 if (at_eof && !pattern_defined
11830 && DECL_EXPLICIT_INSTANTIATION (d))
11831 /* [temp.explicit]
11832
11833 The definition of a non-exported function template, a
11834 non-exported member function template, or a non-exported
11835 member function or static data member of a class template
11836 shall be present in every translation unit in which it is
11837 explicitly instantiated. */
11838 pedwarn
11839 ("explicit instantiation of %qD but no definition available", d);
11840
11841 /* ??? Historically, we have instantiated inline functions, even
11842 when marked as "extern template". */
11843 if (!(external_p && TREE_CODE (d) == VAR_DECL))
11844 add_pending_template (d);
11845 goto out;
11846 }
11847 /* Tell the repository that D is available in this translation unit
11848 -- and see if it is supposed to be instantiated here. */
11849 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11850 {
11851 /* In a PCH file, despite the fact that the repository hasn't
11852 requested instantiation in the PCH it is still possible that
11853 an instantiation will be required in a file that includes the
11854 PCH. */
11855 if (pch_file)
11856 add_pending_template (d);
11857 /* Instantiate inline functions so that the inliner can do its
11858 job, even though we'll not be emitting a copy of this
11859 function. */
11860 if (!(TREE_CODE (d) == FUNCTION_DECL
11861 && flag_inline_trees
11862 && DECL_DECLARED_INLINE_P (d)))
11863 goto out;
11864 }
11865
11866 need_push = !cfun || !global_bindings_p ();
11867 if (need_push)
11868 push_to_top_level ();
11869
11870 /* Mark D as instantiated so that recursive calls to
11871 instantiate_decl do not try to instantiate it again. */
11872 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11873
11874 /* Regenerate the declaration in case the template has been modified
11875 by a subsequent redeclaration. */
11876 regenerate_decl_from_template (d, td);
11877
11878 /* We already set the file and line above. Reset them now in case
11879 they changed as a result of calling regenerate_decl_from_template. */
11880 input_location = DECL_SOURCE_LOCATION (d);
11881
11882 if (TREE_CODE (d) == VAR_DECL)
11883 {
11884 /* Clear out DECL_RTL; whatever was there before may not be right
11885 since we've reset the type of the declaration. */
11886 SET_DECL_RTL (d, NULL_RTX);
11887 DECL_IN_AGGR_P (d) = 0;
11888
11889 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11890 initializer. That function will defer actual emission until
11891 we have a chance to determine linkage. */
11892 DECL_EXTERNAL (d) = 0;
11893
11894 /* Enter the scope of D so that access-checking works correctly. */
11895 push_nested_class (DECL_CONTEXT (d));
11896 finish_decl (d, DECL_INITIAL (d), NULL_TREE);
11897 pop_nested_class ();
11898 }
11899 else if (TREE_CODE (d) == FUNCTION_DECL)
11900 {
11901 htab_t saved_local_specializations;
11902 tree subst_decl;
11903 tree tmpl_parm;
11904 tree spec_parm;
11905
11906 /* Save away the current list, in case we are instantiating one
11907 template from within the body of another. */
11908 saved_local_specializations = local_specializations;
11909
11910 /* Set up the list of local specializations. */
11911 local_specializations = htab_create (37,
11912 hash_local_specialization,
11913 eq_local_specializations,
11914 NULL);
11915
11916 /* Set up context. */
11917 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11918
11919 /* Create substitution entries for the parameters. */
11920 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11921 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11922 spec_parm = DECL_ARGUMENTS (d);
11923 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11924 {
11925 register_local_specialization (spec_parm, tmpl_parm);
11926 spec_parm = skip_artificial_parms_for (d, spec_parm);
11927 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11928 }
11929 while (tmpl_parm)
11930 {
11931 register_local_specialization (spec_parm, tmpl_parm);
11932 tmpl_parm = TREE_CHAIN (tmpl_parm);
11933 spec_parm = TREE_CHAIN (spec_parm);
11934 }
11935 gcc_assert (!spec_parm);
11936
11937 /* Substitute into the body of the function. */
11938 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11939 tf_warning_or_error, tmpl);
11940
11941 /* We don't need the local specializations any more. */
11942 htab_delete (local_specializations);
11943 local_specializations = saved_local_specializations;
11944
11945 /* Finish the function. */
11946 d = finish_function (0);
11947 expand_or_defer_fn (d);
11948 }
11949
11950 /* We're not deferring instantiation any more. */
11951 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11952
11953 if (need_push)
11954 pop_from_top_level ();
11955
11956 out:
11957 input_location = saved_loc;
11958 pop_deferring_access_checks ();
11959 pop_tinst_level ();
11960
11961 timevar_pop (TV_PARSE);
11962
11963 return d;
11964 }
11965
11966 /* Run through the list of templates that we wish we could
11967 instantiate, and instantiate any we can. RETRIES is the
11968 number of times we retry pending template instantiation. */
11969
11970 void
11971 instantiate_pending_templates (int retries)
11972 {
11973 tree *t;
11974 tree last = NULL_TREE;
11975 int reconsider;
11976 location_t saved_loc = input_location;
11977 int saved_in_system_header = in_system_header;
11978
11979 /* Instantiating templates may trigger vtable generation. This in turn
11980 may require further template instantiations. We place a limit here
11981 to avoid infinite loop. */
11982 if (pending_templates && retries >= max_tinst_depth)
11983 {
11984 tree decl = TREE_VALUE (pending_templates);
11985
11986 error ("template instantiation depth exceeds maximum of %d"
11987 " instantiating %q+D, possibly from virtual table generation"
11988 " (use -ftemplate-depth-NN to increase the maximum)",
11989 max_tinst_depth, decl);
11990 if (TREE_CODE (decl) == FUNCTION_DECL)
11991 /* Pretend that we defined it. */
11992 DECL_INITIAL (decl) = error_mark_node;
11993 return;
11994 }
11995
11996 do
11997 {
11998 reconsider = 0;
11999
12000 t = &pending_templates;
12001 while (*t)
12002 {
12003 tree instantiation = TREE_VALUE (*t);
12004
12005 reopen_tinst_level (TREE_PURPOSE (*t));
12006
12007 if (TYPE_P (instantiation))
12008 {
12009 tree fn;
12010
12011 if (!COMPLETE_TYPE_P (instantiation))
12012 {
12013 instantiate_class_template (instantiation);
12014 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12015 for (fn = TYPE_METHODS (instantiation);
12016 fn;
12017 fn = TREE_CHAIN (fn))
12018 if (! DECL_ARTIFICIAL (fn))
12019 instantiate_decl (fn,
12020 /*defer_ok=*/0,
12021 /*expl_inst_class_mem_p=*/false);
12022 if (COMPLETE_TYPE_P (instantiation))
12023 reconsider = 1;
12024 }
12025
12026 if (COMPLETE_TYPE_P (instantiation))
12027 /* If INSTANTIATION has been instantiated, then we don't
12028 need to consider it again in the future. */
12029 *t = TREE_CHAIN (*t);
12030 else
12031 {
12032 last = *t;
12033 t = &TREE_CHAIN (*t);
12034 }
12035 }
12036 else
12037 {
12038 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12039 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12040 {
12041 instantiation
12042 = instantiate_decl (instantiation,
12043 /*defer_ok=*/0,
12044 /*expl_inst_class_mem_p=*/false);
12045 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12046 reconsider = 1;
12047 }
12048
12049 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12050 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12051 /* If INSTANTIATION has been instantiated, then we don't
12052 need to consider it again in the future. */
12053 *t = TREE_CHAIN (*t);
12054 else
12055 {
12056 last = *t;
12057 t = &TREE_CHAIN (*t);
12058 }
12059 }
12060 tinst_depth = 0;
12061 current_tinst_level = NULL_TREE;
12062 }
12063 last_pending_template = last;
12064 }
12065 while (reconsider);
12066
12067 input_location = saved_loc;
12068 in_system_header = saved_in_system_header;
12069 }
12070
12071 /* Substitute ARGVEC into T, which is a list of initializers for
12072 either base class or a non-static data member. The TREE_PURPOSEs
12073 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12074 instantiate_decl. */
12075
12076 static tree
12077 tsubst_initializer_list (tree t, tree argvec)
12078 {
12079 tree inits = NULL_TREE;
12080
12081 for (; t; t = TREE_CHAIN (t))
12082 {
12083 tree decl;
12084 tree init;
12085
12086 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
12087 NULL_TREE);
12088 decl = expand_member_init (decl);
12089 if (decl && !DECL_P (decl))
12090 in_base_initializer = 1;
12091
12092 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12093 NULL_TREE);
12094 in_base_initializer = 0;
12095
12096 if (decl)
12097 {
12098 init = build_tree_list (decl, init);
12099 TREE_CHAIN (init) = inits;
12100 inits = init;
12101 }
12102 }
12103 return inits;
12104 }
12105
12106 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12107
12108 static void
12109 set_current_access_from_decl (tree decl)
12110 {
12111 if (TREE_PRIVATE (decl))
12112 current_access_specifier = access_private_node;
12113 else if (TREE_PROTECTED (decl))
12114 current_access_specifier = access_protected_node;
12115 else
12116 current_access_specifier = access_public_node;
12117 }
12118
12119 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12120 is the instantiation (which should have been created with
12121 start_enum) and ARGS are the template arguments to use. */
12122
12123 static void
12124 tsubst_enum (tree tag, tree newtag, tree args)
12125 {
12126 tree e;
12127
12128 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12129 {
12130 tree value;
12131 tree decl;
12132
12133 decl = TREE_VALUE (e);
12134 /* Note that in a template enum, the TREE_VALUE is the
12135 CONST_DECL, not the corresponding INTEGER_CST. */
12136 value = tsubst_expr (DECL_INITIAL (decl),
12137 args, tf_warning_or_error, NULL_TREE);
12138
12139 /* Give this enumeration constant the correct access. */
12140 set_current_access_from_decl (decl);
12141
12142 /* Actually build the enumerator itself. */
12143 build_enumerator (DECL_NAME (decl), value, newtag);
12144 }
12145
12146 finish_enum (newtag);
12147 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12148 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12149 }
12150
12151 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12152 its type -- but without substituting the innermost set of template
12153 arguments. So, innermost set of template parameters will appear in
12154 the type. */
12155
12156 tree
12157 get_mostly_instantiated_function_type (tree decl)
12158 {
12159 tree fn_type;
12160 tree tmpl;
12161 tree targs;
12162 tree tparms;
12163 int parm_depth;
12164
12165 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12166 targs = DECL_TI_ARGS (decl);
12167 tparms = DECL_TEMPLATE_PARMS (tmpl);
12168 parm_depth = TMPL_PARMS_DEPTH (tparms);
12169
12170 /* There should be as many levels of arguments as there are levels
12171 of parameters. */
12172 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12173
12174 fn_type = TREE_TYPE (tmpl);
12175
12176 if (parm_depth == 1)
12177 /* No substitution is necessary. */
12178 ;
12179 else
12180 {
12181 int i, save_access_control;
12182 tree partial_args;
12183
12184 /* Replace the innermost level of the TARGS with NULL_TREEs to
12185 let tsubst know not to substitute for those parameters. */
12186 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12187 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12188 SET_TMPL_ARGS_LEVEL (partial_args, i,
12189 TMPL_ARGS_LEVEL (targs, i));
12190 SET_TMPL_ARGS_LEVEL (partial_args,
12191 TMPL_ARGS_DEPTH (targs),
12192 make_tree_vec (DECL_NTPARMS (tmpl)));
12193
12194 /* Disable access control as this function is used only during
12195 name-mangling. */
12196 save_access_control = flag_access_control;
12197 flag_access_control = 0;
12198
12199 ++processing_template_decl;
12200 /* Now, do the (partial) substitution to figure out the
12201 appropriate function type. */
12202 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12203 --processing_template_decl;
12204
12205 /* Substitute into the template parameters to obtain the real
12206 innermost set of parameters. This step is important if the
12207 innermost set of template parameters contains value
12208 parameters whose types depend on outer template parameters. */
12209 TREE_VEC_LENGTH (partial_args)--;
12210 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12211
12212 flag_access_control = save_access_control;
12213 }
12214
12215 return fn_type;
12216 }
12217
12218 /* Return truthvalue if we're processing a template different from
12219 the last one involved in diagnostics. */
12220 int
12221 problematic_instantiation_changed (void)
12222 {
12223 return last_template_error_tick != tinst_level_tick;
12224 }
12225
12226 /* Remember current template involved in diagnostics. */
12227 void
12228 record_last_problematic_instantiation (void)
12229 {
12230 last_template_error_tick = tinst_level_tick;
12231 }
12232
12233 tree
12234 current_instantiation (void)
12235 {
12236 return current_tinst_level;
12237 }
12238
12239 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12240 type. Return zero for ok, nonzero for disallowed. Issue error and
12241 warning messages under control of COMPLAIN. */
12242
12243 static int
12244 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12245 {
12246 if (INTEGRAL_TYPE_P (type))
12247 return 0;
12248 else if (POINTER_TYPE_P (type))
12249 return 0;
12250 else if (TYPE_PTR_TO_MEMBER_P (type))
12251 return 0;
12252 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12253 return 0;
12254 else if (TREE_CODE (type) == TYPENAME_TYPE)
12255 return 0;
12256
12257 if (complain & tf_error)
12258 error ("%q#T is not a valid type for a template constant parameter", type);
12259 return 1;
12260 }
12261
12262 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12263 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12264
12265 static bool
12266 dependent_type_p_r (tree type)
12267 {
12268 tree scope;
12269
12270 /* [temp.dep.type]
12271
12272 A type is dependent if it is:
12273
12274 -- a template parameter. Template template parameters are types
12275 for us (since TYPE_P holds true for them) so we handle
12276 them here. */
12277 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12278 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12279 return true;
12280 /* -- a qualified-id with a nested-name-specifier which contains a
12281 class-name that names a dependent type or whose unqualified-id
12282 names a dependent type. */
12283 if (TREE_CODE (type) == TYPENAME_TYPE)
12284 return true;
12285 /* -- a cv-qualified type where the cv-unqualified type is
12286 dependent. */
12287 type = TYPE_MAIN_VARIANT (type);
12288 /* -- a compound type constructed from any dependent type. */
12289 if (TYPE_PTR_TO_MEMBER_P (type))
12290 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12291 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12292 (type)));
12293 else if (TREE_CODE (type) == POINTER_TYPE
12294 || TREE_CODE (type) == REFERENCE_TYPE)
12295 return dependent_type_p (TREE_TYPE (type));
12296 else if (TREE_CODE (type) == FUNCTION_TYPE
12297 || TREE_CODE (type) == METHOD_TYPE)
12298 {
12299 tree arg_type;
12300
12301 if (dependent_type_p (TREE_TYPE (type)))
12302 return true;
12303 for (arg_type = TYPE_ARG_TYPES (type);
12304 arg_type;
12305 arg_type = TREE_CHAIN (arg_type))
12306 if (dependent_type_p (TREE_VALUE (arg_type)))
12307 return true;
12308 return false;
12309 }
12310 /* -- an array type constructed from any dependent type or whose
12311 size is specified by a constant expression that is
12312 value-dependent. */
12313 if (TREE_CODE (type) == ARRAY_TYPE)
12314 {
12315 if (TYPE_DOMAIN (type)
12316 && ((value_dependent_expression_p
12317 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12318 || (type_dependent_expression_p
12319 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12320 return true;
12321 return dependent_type_p (TREE_TYPE (type));
12322 }
12323
12324 /* -- a template-id in which either the template name is a template
12325 parameter ... */
12326 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12327 return true;
12328 /* ... or any of the template arguments is a dependent type or
12329 an expression that is type-dependent or value-dependent. */
12330 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12331 && (any_dependent_template_arguments_p
12332 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12333 return true;
12334
12335 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12336 expression is not type-dependent, then it should already been
12337 have resolved. */
12338 if (TREE_CODE (type) == TYPEOF_TYPE)
12339 return true;
12340
12341 /* The standard does not specifically mention types that are local
12342 to template functions or local classes, but they should be
12343 considered dependent too. For example:
12344
12345 template <int I> void f() {
12346 enum E { a = I };
12347 S<sizeof (E)> s;
12348 }
12349
12350 The size of `E' cannot be known until the value of `I' has been
12351 determined. Therefore, `E' must be considered dependent. */
12352 scope = TYPE_CONTEXT (type);
12353 if (scope && TYPE_P (scope))
12354 return dependent_type_p (scope);
12355 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12356 return type_dependent_expression_p (scope);
12357
12358 /* Other types are non-dependent. */
12359 return false;
12360 }
12361
12362 /* Returns TRUE if TYPE is dependent, in the sense of
12363 [temp.dep.type]. */
12364
12365 bool
12366 dependent_type_p (tree type)
12367 {
12368 /* If there are no template parameters in scope, then there can't be
12369 any dependent types. */
12370 if (!processing_template_decl)
12371 {
12372 /* If we are not processing a template, then nobody should be
12373 providing us with a dependent type. */
12374 gcc_assert (type);
12375 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12376 return false;
12377 }
12378
12379 /* If the type is NULL, we have not computed a type for the entity
12380 in question; in that case, the type is dependent. */
12381 if (!type)
12382 return true;
12383
12384 /* Erroneous types can be considered non-dependent. */
12385 if (type == error_mark_node)
12386 return false;
12387
12388 /* If we have not already computed the appropriate value for TYPE,
12389 do so now. */
12390 if (!TYPE_DEPENDENT_P_VALID (type))
12391 {
12392 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12393 TYPE_DEPENDENT_P_VALID (type) = 1;
12394 }
12395
12396 return TYPE_DEPENDENT_P (type);
12397 }
12398
12399 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12400
12401 static bool
12402 dependent_scope_ref_p (tree expression, bool criterion (tree))
12403 {
12404 tree scope;
12405 tree name;
12406
12407 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12408
12409 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12410 return true;
12411
12412 scope = TREE_OPERAND (expression, 0);
12413 name = TREE_OPERAND (expression, 1);
12414
12415 /* [temp.dep.expr]
12416
12417 An id-expression is type-dependent if it contains a
12418 nested-name-specifier that contains a class-name that names a
12419 dependent type. */
12420 /* The suggested resolution to Core Issue 2 implies that if the
12421 qualifying type is the current class, then we must peek
12422 inside it. */
12423 if (DECL_P (name)
12424 && currently_open_class (scope)
12425 && !criterion (name))
12426 return false;
12427 if (dependent_type_p (scope))
12428 return true;
12429
12430 return false;
12431 }
12432
12433 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12434 [temp.dep.constexpr] */
12435
12436 bool
12437 value_dependent_expression_p (tree expression)
12438 {
12439 if (!processing_template_decl)
12440 return false;
12441
12442 /* A name declared with a dependent type. */
12443 if (DECL_P (expression) && type_dependent_expression_p (expression))
12444 return true;
12445
12446 switch (TREE_CODE (expression))
12447 {
12448 case IDENTIFIER_NODE:
12449 /* A name that has not been looked up -- must be dependent. */
12450 return true;
12451
12452 case TEMPLATE_PARM_INDEX:
12453 /* A non-type template parm. */
12454 return true;
12455
12456 case CONST_DECL:
12457 /* A non-type template parm. */
12458 if (DECL_TEMPLATE_PARM_P (expression))
12459 return true;
12460 return false;
12461
12462 case VAR_DECL:
12463 /* A constant with integral or enumeration type and is initialized
12464 with an expression that is value-dependent. */
12465 if (DECL_INITIAL (expression)
12466 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12467 && value_dependent_expression_p (DECL_INITIAL (expression)))
12468 return true;
12469 return false;
12470
12471 case DYNAMIC_CAST_EXPR:
12472 case STATIC_CAST_EXPR:
12473 case CONST_CAST_EXPR:
12474 case REINTERPRET_CAST_EXPR:
12475 case CAST_EXPR:
12476 /* These expressions are value-dependent if the type to which
12477 the cast occurs is dependent or the expression being casted
12478 is value-dependent. */
12479 {
12480 tree type = TREE_TYPE (expression);
12481
12482 if (dependent_type_p (type))
12483 return true;
12484
12485 /* A functional cast has a list of operands. */
12486 expression = TREE_OPERAND (expression, 0);
12487 if (!expression)
12488 {
12489 /* If there are no operands, it must be an expression such
12490 as "int()". This should not happen for aggregate types
12491 because it would form non-constant expressions. */
12492 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12493
12494 return false;
12495 }
12496
12497 if (TREE_CODE (expression) == TREE_LIST)
12498 {
12499 for (; expression; expression = TREE_CHAIN (expression))
12500 if (value_dependent_expression_p (TREE_VALUE (expression)))
12501 return true;
12502 return false;
12503 }
12504
12505 return value_dependent_expression_p (expression);
12506 }
12507
12508 case SIZEOF_EXPR:
12509 case ALIGNOF_EXPR:
12510 /* A `sizeof' expression is value-dependent if the operand is
12511 type-dependent. */
12512 expression = TREE_OPERAND (expression, 0);
12513 if (TYPE_P (expression))
12514 return dependent_type_p (expression);
12515 return type_dependent_expression_p (expression);
12516
12517 case SCOPE_REF:
12518 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12519
12520 case COMPONENT_REF:
12521 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12522 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12523
12524 case CALL_EXPR:
12525 /* A CALL_EXPR is value-dependent if any argument is
12526 value-dependent. Why do we have to handle CALL_EXPRs in this
12527 function at all? First, some function calls, those for which
12528 value_dependent_expression_p is true, man appear in constant
12529 expressions. Second, there appear to be bugs which result in
12530 other CALL_EXPRs reaching this point. */
12531 {
12532 tree function = TREE_OPERAND (expression, 0);
12533 tree args = TREE_OPERAND (expression, 1);
12534
12535 if (value_dependent_expression_p (function))
12536 return true;
12537
12538 if (! args)
12539 return false;
12540
12541 if (TREE_CODE (args) == TREE_LIST)
12542 {
12543 for (; args; args = TREE_CHAIN (args))
12544 if (value_dependent_expression_p (TREE_VALUE (args)))
12545 return true;
12546 return false;
12547 }
12548
12549 return value_dependent_expression_p (args);
12550 }
12551
12552 default:
12553 /* A constant expression is value-dependent if any subexpression is
12554 value-dependent. */
12555 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12556 {
12557 case tcc_reference:
12558 case tcc_unary:
12559 return (value_dependent_expression_p
12560 (TREE_OPERAND (expression, 0)));
12561
12562 case tcc_comparison:
12563 case tcc_binary:
12564 return ((value_dependent_expression_p
12565 (TREE_OPERAND (expression, 0)))
12566 || (value_dependent_expression_p
12567 (TREE_OPERAND (expression, 1))));
12568
12569 case tcc_expression:
12570 {
12571 int i;
12572 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12573 /* In some cases, some of the operands may be missing.
12574 (For example, in the case of PREDECREMENT_EXPR, the
12575 amount to increment by may be missing.) That doesn't
12576 make the expression dependent. */
12577 if (TREE_OPERAND (expression, i)
12578 && (value_dependent_expression_p
12579 (TREE_OPERAND (expression, i))))
12580 return true;
12581 return false;
12582 }
12583
12584 default:
12585 break;
12586 }
12587 }
12588
12589 /* The expression is not value-dependent. */
12590 return false;
12591 }
12592
12593 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12594 [temp.dep.expr]. */
12595
12596 bool
12597 type_dependent_expression_p (tree expression)
12598 {
12599 if (!processing_template_decl)
12600 return false;
12601
12602 if (expression == error_mark_node)
12603 return false;
12604
12605 /* An unresolved name is always dependent. */
12606 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12607 return true;
12608
12609 /* Some expression forms are never type-dependent. */
12610 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12611 || TREE_CODE (expression) == SIZEOF_EXPR
12612 || TREE_CODE (expression) == ALIGNOF_EXPR
12613 || TREE_CODE (expression) == TYPEID_EXPR
12614 || TREE_CODE (expression) == DELETE_EXPR
12615 || TREE_CODE (expression) == VEC_DELETE_EXPR
12616 || TREE_CODE (expression) == THROW_EXPR)
12617 return false;
12618
12619 /* The types of these expressions depends only on the type to which
12620 the cast occurs. */
12621 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12622 || TREE_CODE (expression) == STATIC_CAST_EXPR
12623 || TREE_CODE (expression) == CONST_CAST_EXPR
12624 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12625 || TREE_CODE (expression) == CAST_EXPR)
12626 return dependent_type_p (TREE_TYPE (expression));
12627
12628 /* The types of these expressions depends only on the type created
12629 by the expression. */
12630 if (TREE_CODE (expression) == NEW_EXPR
12631 || TREE_CODE (expression) == VEC_NEW_EXPR)
12632 {
12633 /* For NEW_EXPR tree nodes created inside a template, either
12634 the object type itself or a TREE_LIST may appear as the
12635 operand 1. */
12636 tree type = TREE_OPERAND (expression, 1);
12637 if (TREE_CODE (type) == TREE_LIST)
12638 /* This is an array type. We need to check array dimensions
12639 as well. */
12640 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12641 || value_dependent_expression_p
12642 (TREE_OPERAND (TREE_VALUE (type), 1));
12643 else
12644 return dependent_type_p (type);
12645 }
12646
12647 if (TREE_CODE (expression) == SCOPE_REF
12648 && dependent_scope_ref_p (expression,
12649 type_dependent_expression_p))
12650 return true;
12651
12652 if (TREE_CODE (expression) == FUNCTION_DECL
12653 && DECL_LANG_SPECIFIC (expression)
12654 && DECL_TEMPLATE_INFO (expression)
12655 && (any_dependent_template_arguments_p
12656 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12657 return true;
12658
12659 if (TREE_CODE (expression) == TEMPLATE_DECL
12660 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12661 return false;
12662
12663 if (TREE_TYPE (expression) == unknown_type_node)
12664 {
12665 if (TREE_CODE (expression) == ADDR_EXPR)
12666 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12667 if (TREE_CODE (expression) == COMPONENT_REF
12668 || TREE_CODE (expression) == OFFSET_REF)
12669 {
12670 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12671 return true;
12672 expression = TREE_OPERAND (expression, 1);
12673 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12674 return false;
12675 }
12676 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12677 if (TREE_CODE (expression) == SCOPE_REF)
12678 return false;
12679
12680 if (TREE_CODE (expression) == BASELINK)
12681 expression = BASELINK_FUNCTIONS (expression);
12682
12683 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12684 {
12685 if (any_dependent_template_arguments_p
12686 (TREE_OPERAND (expression, 1)))
12687 return true;
12688 expression = TREE_OPERAND (expression, 0);
12689 }
12690 gcc_assert (TREE_CODE (expression) == OVERLOAD
12691 || TREE_CODE (expression) == FUNCTION_DECL);
12692
12693 while (expression)
12694 {
12695 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12696 return true;
12697 expression = OVL_NEXT (expression);
12698 }
12699 return false;
12700 }
12701
12702 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
12703
12704 return (dependent_type_p (TREE_TYPE (expression)));
12705 }
12706
12707 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12708 contains a type-dependent expression. */
12709
12710 bool
12711 any_type_dependent_arguments_p (tree args)
12712 {
12713 while (args)
12714 {
12715 tree arg = TREE_VALUE (args);
12716
12717 if (type_dependent_expression_p (arg))
12718 return true;
12719 args = TREE_CHAIN (args);
12720 }
12721 return false;
12722 }
12723
12724 /* Returns TRUE if the ARG (a template argument) is dependent. */
12725
12726 static bool
12727 dependent_template_arg_p (tree arg)
12728 {
12729 if (!processing_template_decl)
12730 return false;
12731
12732 if (TREE_CODE (arg) == TEMPLATE_DECL
12733 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12734 return dependent_template_p (arg);
12735 else if (TYPE_P (arg))
12736 return dependent_type_p (arg);
12737 else
12738 return (type_dependent_expression_p (arg)
12739 || value_dependent_expression_p (arg));
12740 }
12741
12742 /* Returns true if ARGS (a collection of template arguments) contains
12743 any dependent arguments. */
12744
12745 bool
12746 any_dependent_template_arguments_p (tree args)
12747 {
12748 int i;
12749 int j;
12750
12751 if (!args)
12752 return false;
12753
12754 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12755 {
12756 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12757 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12758 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12759 return true;
12760 }
12761
12762 return false;
12763 }
12764
12765 /* Returns TRUE if the template TMPL is dependent. */
12766
12767 bool
12768 dependent_template_p (tree tmpl)
12769 {
12770 if (TREE_CODE (tmpl) == OVERLOAD)
12771 {
12772 while (tmpl)
12773 {
12774 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12775 return true;
12776 tmpl = OVL_CHAIN (tmpl);
12777 }
12778 return false;
12779 }
12780
12781 /* Template template parameters are dependent. */
12782 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12783 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12784 return true;
12785 /* So are names that have not been looked up. */
12786 if (TREE_CODE (tmpl) == SCOPE_REF
12787 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12788 return true;
12789 /* So are member templates of dependent classes. */
12790 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12791 return dependent_type_p (DECL_CONTEXT (tmpl));
12792 return false;
12793 }
12794
12795 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12796
12797 bool
12798 dependent_template_id_p (tree tmpl, tree args)
12799 {
12800 return (dependent_template_p (tmpl)
12801 || any_dependent_template_arguments_p (args));
12802 }
12803
12804 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12805 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12806 can be found. Note that this function peers inside uninstantiated
12807 templates and therefore should be used only in extremely limited
12808 situations. ONLY_CURRENT_P restricts this peering to the currently
12809 open classes hierarchy (which is required when comparing types). */
12810
12811 tree
12812 resolve_typename_type (tree type, bool only_current_p)
12813 {
12814 tree scope;
12815 tree name;
12816 tree decl;
12817 int quals;
12818 tree pushed_scope;
12819
12820 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12821
12822 scope = TYPE_CONTEXT (type);
12823 name = TYPE_IDENTIFIER (type);
12824
12825 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12826 it first before we can figure out what NAME refers to. */
12827 if (TREE_CODE (scope) == TYPENAME_TYPE)
12828 scope = resolve_typename_type (scope, only_current_p);
12829 /* If we don't know what SCOPE refers to, then we cannot resolve the
12830 TYPENAME_TYPE. */
12831 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12832 return error_mark_node;
12833 /* If the SCOPE is a template type parameter, we have no way of
12834 resolving the name. */
12835 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12836 return type;
12837 /* If the SCOPE is not the current instantiation, there's no reason
12838 to look inside it. */
12839 if (only_current_p && !currently_open_class (scope))
12840 return error_mark_node;
12841 /* If SCOPE is a partial instantiation, it will not have a valid
12842 TYPE_FIELDS list, so use the original template. */
12843 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12844 /* Enter the SCOPE so that name lookup will be resolved as if we
12845 were in the class definition. In particular, SCOPE will no
12846 longer be considered a dependent type. */
12847 pushed_scope = push_scope (scope);
12848 /* Look up the declaration. */
12849 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12850 /* Obtain the set of qualifiers applied to the TYPE. */
12851 quals = cp_type_quals (type);
12852 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12853 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12854 if (!decl)
12855 type = error_mark_node;
12856 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12857 && TREE_CODE (decl) == TYPE_DECL)
12858 type = TREE_TYPE (decl);
12859 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12860 && DECL_CLASS_TEMPLATE_P (decl))
12861 {
12862 tree tmpl;
12863 tree args;
12864 /* Obtain the template and the arguments. */
12865 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12866 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12867 /* Instantiate the template. */
12868 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12869 /*entering_scope=*/0, tf_error | tf_user);
12870 }
12871 else
12872 type = error_mark_node;
12873 /* Qualify the resulting type. */
12874 if (type != error_mark_node && quals)
12875 type = cp_build_qualified_type (type, quals);
12876 /* Leave the SCOPE. */
12877 if (pushed_scope)
12878 pop_scope (pushed_scope);
12879
12880 return type;
12881 }
12882
12883 /* EXPR is an expression which is not type-dependent. Return a proxy
12884 for EXPR that can be used to compute the types of larger
12885 expressions containing EXPR. */
12886
12887 tree
12888 build_non_dependent_expr (tree expr)
12889 {
12890 tree inner_expr;
12891
12892 /* Preserve null pointer constants so that the type of things like
12893 "p == 0" where "p" is a pointer can be determined. */
12894 if (null_ptr_cst_p (expr))
12895 return expr;
12896 /* Preserve OVERLOADs; the functions must be available to resolve
12897 types. */
12898 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12899 TREE_OPERAND (expr, 0) :
12900 TREE_CODE (expr) == COMPONENT_REF ?
12901 TREE_OPERAND (expr, 1) : expr);
12902 if (is_overloaded_fn (inner_expr)
12903 || TREE_CODE (inner_expr) == OFFSET_REF)
12904 return expr;
12905 /* There is no need to return a proxy for a variable. */
12906 if (TREE_CODE (expr) == VAR_DECL)
12907 return expr;
12908 /* Preserve string constants; conversions from string constants to
12909 "char *" are allowed, even though normally a "const char *"
12910 cannot be used to initialize a "char *". */
12911 if (TREE_CODE (expr) == STRING_CST)
12912 return expr;
12913 /* Preserve arithmetic constants, as an optimization -- there is no
12914 reason to create a new node. */
12915 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12916 return expr;
12917 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12918 There is at least one place where we want to know that a
12919 particular expression is a throw-expression: when checking a ?:
12920 expression, there are special rules if the second or third
12921 argument is a throw-expression. */
12922 if (TREE_CODE (expr) == THROW_EXPR)
12923 return expr;
12924
12925 if (TREE_CODE (expr) == COND_EXPR)
12926 return build3 (COND_EXPR,
12927 TREE_TYPE (expr),
12928 TREE_OPERAND (expr, 0),
12929 (TREE_OPERAND (expr, 1)
12930 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12931 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12932 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12933 if (TREE_CODE (expr) == COMPOUND_EXPR
12934 && !COMPOUND_EXPR_OVERLOADED (expr))
12935 return build2 (COMPOUND_EXPR,
12936 TREE_TYPE (expr),
12937 TREE_OPERAND (expr, 0),
12938 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12939
12940 /* If the type is unknown, it can't really be non-dependent */
12941 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
12942
12943 /* Otherwise, build a NON_DEPENDENT_EXPR.
12944
12945 REFERENCE_TYPEs are not stripped for expressions in templates
12946 because doing so would play havoc with mangling. Consider, for
12947 example:
12948
12949 template <typename T> void f<T& g>() { g(); }
12950
12951 In the body of "f", the expression for "g" will have
12952 REFERENCE_TYPE, even though the standard says that it should
12953 not. The reason is that we must preserve the syntactic form of
12954 the expression so that mangling (say) "f<g>" inside the body of
12955 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12956 stripped here. */
12957 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12958 }
12959
12960 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12961 Return a new TREE_LIST with the various arguments replaced with
12962 equivalent non-dependent expressions. */
12963
12964 tree
12965 build_non_dependent_args (tree args)
12966 {
12967 tree a;
12968 tree new_args;
12969
12970 new_args = NULL_TREE;
12971 for (a = args; a; a = TREE_CHAIN (a))
12972 new_args = tree_cons (NULL_TREE,
12973 build_non_dependent_expr (TREE_VALUE (a)),
12974 new_args);
12975 return nreverse (new_args);
12976 }
12977
12978 #include "gt-cp-pt.h"