* Merge with edge-vector-mergepoint-20040918.
[official-gcc.git] / gcc / cp / pt.c
blobd7e59039ce4aeb03fdad32effd151d9e99d2f4e9
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 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
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.
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.
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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
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". */
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 "flags.h"
36 #include "cp-tree.h"
37 #include "tree-inline.h"
38 #include "decl.h"
39 #include "output.h"
40 #include "except.h"
41 #include "toplev.h"
42 #include "rtl.h"
43 #include "timevar.h"
44 #include "tree-iterator.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work.
53 The TREE_PURPOSE of each entry is either a DECL (for a function or
54 static data member), or a TYPE (for a class) indicating what we are
55 hoping to instantiate. The TREE_VALUE is not used. */
56 static GTY(()) tree pending_templates;
57 static GTY(()) tree last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static GTY(()) varray_type inline_parm_levels;
64 static size_t inline_parm_levels_used;
66 static GTY(()) tree current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
75 /* A map from local variable declarations in the body of the template
76 presently being instantiated to the corresponding instantiated
77 local variables. */
78 static htab_t local_specializations;
80 #define UNIFY_ALLOW_NONE 0
81 #define UNIFY_ALLOW_MORE_CV_QUAL 1
82 #define UNIFY_ALLOW_LESS_CV_QUAL 2
83 #define UNIFY_ALLOW_DERIVED 4
84 #define UNIFY_ALLOW_INTEGER 8
85 #define UNIFY_ALLOW_OUTER_LEVEL 16
86 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
87 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
88 #define UNIFY_ALLOW_MAX_CORRECTION 128
90 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
91 virtual, or a base class of a virtual
92 base. */
93 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
94 type with the desired type. */
96 static void push_access_scope (tree);
97 static void pop_access_scope (tree);
98 static int resolve_overloaded_unification (tree, tree, tree, tree,
99 unification_kind_t, int);
100 static int try_one_overload (tree, tree, tree, tree, tree,
101 unification_kind_t, int, bool);
102 static int unify (tree, tree, tree, tree, int);
103 static void add_pending_template (tree);
104 static void reopen_tinst_level (tree);
105 static tree classtype_mangled_name (tree);
106 static char* mangle_class_name_for_template (const char *, tree, tree);
107 static tree tsubst_initializer_list (tree, tree);
108 static tree get_class_bindings (tree, tree, tree);
109 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
110 static void tsubst_enum (tree, tree, tree);
111 static tree add_to_template_args (tree, tree);
112 static tree add_outermost_template_args (tree, tree);
113 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
114 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
115 static int type_unification_real (tree, tree, tree, tree,
116 int, unification_kind_t, int, int);
117 static void note_template_header (int);
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 tree get_bindings_overload (tree, tree, tree);
122 static int for_each_template_parm (tree, tree_fn_t, void*, htab_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_specialization (tree, tree);
127 static tree retrieve_local_specialization (tree);
128 static tree register_specialization (tree, tree, tree);
129 static void register_local_specialization (tree, tree);
130 static tree reduce_template_parm_level (tree, tree, int);
131 static tree build_template_decl (tree, tree);
132 static int mark_template_parm (tree, void *);
133 static int template_parm_this_level_p (tree, void *);
134 static tree tsubst_friend_function (tree, tree);
135 static tree tsubst_friend_class (tree, tree);
136 static int can_complete_type_without_circularity (tree);
137 static tree get_bindings (tree, tree, tree);
138 static tree get_bindings_real (tree, tree, tree, int, int, int);
139 static int template_decl_level (tree);
140 static int check_cv_quals_for_unify (int, tree, tree);
141 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
142 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
143 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
144 static void regenerate_decl_from_template (tree, tree);
145 static tree most_specialized (tree, tree, tree);
146 static tree most_specialized_class (tree, tree);
147 static int template_class_depth_real (tree, int);
148 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
149 static tree tsubst_decl (tree, tree, tree, tsubst_flags_t);
150 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
151 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
152 static void check_specialization_scope (void);
153 static tree process_partial_specialization (tree);
154 static void set_current_access_from_decl (tree);
155 static void check_default_tmpl_args (tree, tree, int, int);
156 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
157 static tree get_template_base_recursive (tree, tree, tree, tree, tree, int);
158 static tree get_template_base (tree, tree, tree, tree);
159 static int verify_class_unification (tree, tree, tree);
160 static tree try_class_unification (tree, tree, tree, tree);
161 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
162 tree, tree);
163 static tree determine_specialization (tree, tree, tree *, int, int);
164 static int template_args_equal (tree, tree);
165 static void tsubst_default_arguments (tree);
166 static tree for_each_template_parm_r (tree *, int *, void *);
167 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
168 static void copy_default_args_to_explicit_spec (tree);
169 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
170 static int eq_local_specializations (const void *, const void *);
171 static bool dependent_type_p_r (tree);
172 static tree tsubst (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
176 /* Make the current scope suitable for access checking when we are
177 processing T. T can be FUNCTION_DECL for instantiated function
178 template, or VAR_DECL for static member variable (need by
179 instantiate_decl). */
181 static void
182 push_access_scope (tree t)
184 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
185 || TREE_CODE (t) == VAR_DECL);
187 if (DECL_CLASS_SCOPE_P (t))
188 push_nested_class (DECL_CONTEXT (t));
189 else
190 push_to_top_level ();
192 if (TREE_CODE (t) == FUNCTION_DECL)
194 saved_access_scope = tree_cons
195 (NULL_TREE, current_function_decl, saved_access_scope);
196 current_function_decl = t;
200 /* Restore the scope set up by push_access_scope. T is the node we
201 are processing. */
203 static void
204 pop_access_scope (tree t)
206 if (TREE_CODE (t) == FUNCTION_DECL)
208 current_function_decl = TREE_VALUE (saved_access_scope);
209 saved_access_scope = TREE_CHAIN (saved_access_scope);
212 if (DECL_CLASS_SCOPE_P (t))
213 pop_nested_class ();
214 else
215 pop_from_top_level ();
218 /* Do any processing required when DECL (a member template
219 declaration) is finished. Returns the TEMPLATE_DECL corresponding
220 to DECL, unless it is a specialization, in which case the DECL
221 itself is returned. */
223 tree
224 finish_member_template_decl (tree decl)
226 if (decl == error_mark_node)
227 return error_mark_node;
229 gcc_assert (DECL_P (decl));
231 if (TREE_CODE (decl) == TYPE_DECL)
233 tree type;
235 type = TREE_TYPE (decl);
236 if (IS_AGGR_TYPE (type)
237 && CLASSTYPE_TEMPLATE_INFO (type)
238 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
240 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
241 check_member_template (tmpl);
242 return tmpl;
244 return NULL_TREE;
246 else if (TREE_CODE (decl) == FIELD_DECL)
247 error ("data member `%D' cannot be a member template", decl);
248 else if (DECL_TEMPLATE_INFO (decl))
250 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
252 check_member_template (DECL_TI_TEMPLATE (decl));
253 return DECL_TI_TEMPLATE (decl);
255 else
256 return decl;
258 else
259 error ("invalid member template declaration `%D'", decl);
261 return error_mark_node;
264 /* Returns the template nesting level of the indicated class TYPE.
266 For example, in:
267 template <class T>
268 struct A
270 template <class U>
271 struct B {};
274 A<T>::B<U> has depth two, while A<T> has depth one.
275 Both A<T>::B<int> and A<int>::B<U> have depth one, if
276 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
277 specializations.
279 This function is guaranteed to return 0 if passed NULL_TREE so
280 that, for example, `template_class_depth (current_class_type)' is
281 always safe. */
283 static int
284 template_class_depth_real (tree type, int count_specializations)
286 int depth;
288 for (depth = 0;
289 type && TREE_CODE (type) != NAMESPACE_DECL;
290 type = (TREE_CODE (type) == FUNCTION_DECL)
291 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
293 if (TREE_CODE (type) != FUNCTION_DECL)
295 if (CLASSTYPE_TEMPLATE_INFO (type)
296 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
297 && ((count_specializations
298 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
299 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
300 ++depth;
302 else
304 if (DECL_TEMPLATE_INFO (type)
305 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
306 && ((count_specializations
307 && DECL_TEMPLATE_SPECIALIZATION (type))
308 || uses_template_parms (DECL_TI_ARGS (type))))
309 ++depth;
313 return depth;
316 /* Returns the template nesting level of the indicated class TYPE.
317 Like template_class_depth_real, but instantiations do not count in
318 the depth. */
320 int
321 template_class_depth (tree type)
323 return template_class_depth_real (type, /*count_specializations=*/0);
326 /* Returns 1 if processing DECL as part of do_pending_inlines
327 needs us to push template parms. */
329 static int
330 inline_needs_template_parms (tree decl)
332 if (! DECL_TEMPLATE_INFO (decl))
333 return 0;
335 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
336 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
339 /* Subroutine of maybe_begin_member_template_processing.
340 Push the template parms in PARMS, starting from LEVELS steps into the
341 chain, and ending at the beginning, since template parms are listed
342 innermost first. */
344 static void
345 push_inline_template_parms_recursive (tree parmlist, int levels)
347 tree parms = TREE_VALUE (parmlist);
348 int i;
350 if (levels > 1)
351 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
353 ++processing_template_decl;
354 current_template_parms
355 = tree_cons (size_int (processing_template_decl),
356 parms, current_template_parms);
357 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
359 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
360 NULL);
361 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
363 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
364 gcc_assert (DECL_P (parm));
366 switch (TREE_CODE (parm))
368 case TYPE_DECL:
369 case TEMPLATE_DECL:
370 pushdecl (parm);
371 break;
373 case PARM_DECL:
375 /* Make a CONST_DECL as is done in process_template_parm.
376 It is ugly that we recreate this here; the original
377 version built in process_template_parm is no longer
378 available. */
379 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
380 TREE_TYPE (parm));
381 DECL_ARTIFICIAL (decl) = 1;
382 TREE_CONSTANT (decl) = 1;
383 TREE_INVARIANT (decl) = 1;
384 TREE_READONLY (decl) = 1;
385 DECL_INITIAL (decl) = DECL_INITIAL (parm);
386 SET_DECL_TEMPLATE_PARM_P (decl);
387 pushdecl (decl);
389 break;
391 default:
392 gcc_unreachable ();
397 /* Restore the template parameter context for a member template or
398 a friend template defined in a class definition. */
400 void
401 maybe_begin_member_template_processing (tree decl)
403 tree parms;
404 int levels = 0;
406 if (inline_needs_template_parms (decl))
408 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
409 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
411 if (DECL_TEMPLATE_SPECIALIZATION (decl))
413 --levels;
414 parms = TREE_CHAIN (parms);
417 push_inline_template_parms_recursive (parms, levels);
420 /* Remember how many levels of template parameters we pushed so that
421 we can pop them later. */
422 if (!inline_parm_levels)
423 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
424 if (inline_parm_levels_used == inline_parm_levels->num_elements)
425 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
426 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
427 ++inline_parm_levels_used;
430 /* Undo the effects of begin_member_template_processing. */
432 void
433 maybe_end_member_template_processing (void)
435 int i;
437 if (!inline_parm_levels_used)
438 return;
440 --inline_parm_levels_used;
441 for (i = 0;
442 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
443 ++i)
445 --processing_template_decl;
446 current_template_parms = TREE_CHAIN (current_template_parms);
447 poplevel (0, 0, 0);
451 /* Returns nonzero iff T is a member template function. We must be
452 careful as in
454 template <class T> class C { void f(); }
456 Here, f is a template function, and a member, but not a member
457 template. This function does not concern itself with the origin of
458 T, only its present state. So if we have
460 template <class T> class C { template <class U> void f(U); }
462 then neither C<int>::f<char> nor C<T>::f<double> is considered
463 to be a member template. But, `template <class U> void
464 C<int>::f(U)' is considered a member template. */
467 is_member_template (tree t)
469 if (!DECL_FUNCTION_TEMPLATE_P (t))
470 /* Anything that isn't a function or a template function is
471 certainly not a member template. */
472 return 0;
474 /* A local class can't have member templates. */
475 if (decl_function_context (t))
476 return 0;
478 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
479 /* If there are more levels of template parameters than
480 there are template classes surrounding the declaration,
481 then we have a member template. */
482 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
483 template_class_depth (DECL_CONTEXT (t))));
486 #if 0 /* UNUSED */
487 /* Returns nonzero iff T is a member template class. See
488 is_member_template for a description of what precisely constitutes
489 a member template. */
492 is_member_template_class (tree t)
494 if (!DECL_CLASS_TEMPLATE_P (t))
495 /* Anything that isn't a class template, is certainly not a member
496 template. */
497 return 0;
499 if (!DECL_CLASS_SCOPE_P (t))
500 /* Anything whose context isn't a class type is surely not a
501 member template. */
502 return 0;
504 /* If there are more levels of template parameters than there are
505 template classes surrounding the declaration, then we have a
506 member template. */
507 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
508 template_class_depth (DECL_CONTEXT (t)));
510 #endif
512 /* Return a new template argument vector which contains all of ARGS,
513 but has as its innermost set of arguments the EXTRA_ARGS. */
515 static tree
516 add_to_template_args (tree args, tree extra_args)
518 tree new_args;
519 int extra_depth;
520 int i;
521 int j;
523 extra_depth = TMPL_ARGS_DEPTH (extra_args);
524 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
526 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
527 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
529 for (j = 1; j <= extra_depth; ++j, ++i)
530 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
532 return new_args;
535 /* Like add_to_template_args, but only the outermost ARGS are added to
536 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
537 (EXTRA_ARGS) levels are added. This function is used to combine
538 the template arguments from a partial instantiation with the
539 template arguments used to attain the full instantiation from the
540 partial instantiation. */
542 static tree
543 add_outermost_template_args (tree args, tree extra_args)
545 tree new_args;
547 /* If there are more levels of EXTRA_ARGS than there are ARGS,
548 something very fishy is going on. */
549 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
551 /* If *all* the new arguments will be the EXTRA_ARGS, just return
552 them. */
553 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
554 return extra_args;
556 /* For the moment, we make ARGS look like it contains fewer levels. */
557 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
559 new_args = add_to_template_args (args, extra_args);
561 /* Now, we restore ARGS to its full dimensions. */
562 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
564 return new_args;
567 /* Return the N levels of innermost template arguments from the ARGS. */
569 tree
570 get_innermost_template_args (tree args, int n)
572 tree new_args;
573 int extra_levels;
574 int i;
576 gcc_assert (n >= 0);
578 /* If N is 1, just return the innermost set of template arguments. */
579 if (n == 1)
580 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
582 /* If we're not removing anything, just return the arguments we were
583 given. */
584 extra_levels = TMPL_ARGS_DEPTH (args) - n;
585 gcc_assert (extra_levels >= 0);
586 if (extra_levels == 0)
587 return args;
589 /* Make a new set of arguments, not containing the outer arguments. */
590 new_args = make_tree_vec (n);
591 for (i = 1; i <= n; ++i)
592 SET_TMPL_ARGS_LEVEL (new_args, i,
593 TMPL_ARGS_LEVEL (args, i + extra_levels));
595 return new_args;
598 /* We've got a template header coming up; push to a new level for storing
599 the parms. */
601 void
602 begin_template_parm_list (void)
604 /* We use a non-tag-transparent scope here, which causes pushtag to
605 put tags in this scope, rather than in the enclosing class or
606 namespace scope. This is the right thing, since we want
607 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
608 global template class, push_template_decl handles putting the
609 TEMPLATE_DECL into top-level scope. For a nested template class,
610 e.g.:
612 template <class T> struct S1 {
613 template <class T> struct S2 {};
616 pushtag contains special code to call pushdecl_with_scope on the
617 TEMPLATE_DECL for S2. */
618 begin_scope (sk_template_parms, NULL);
619 ++processing_template_decl;
620 ++processing_template_parmlist;
621 note_template_header (0);
624 /* This routine is called when a specialization is declared. If it is
625 invalid to declare a specialization here, an error is reported. */
627 static void
628 check_specialization_scope (void)
630 tree scope = current_scope ();
632 /* [temp.expl.spec]
634 An explicit specialization shall be declared in the namespace of
635 which the template is a member, or, for member templates, in the
636 namespace of which the enclosing class or enclosing class
637 template is a member. An explicit specialization of a member
638 function, member class or static data member of a class template
639 shall be declared in the namespace of which the class template
640 is a member. */
641 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
642 error ("explicit specialization in non-namespace scope `%D'",
643 scope);
645 /* [temp.expl.spec]
647 In an explicit specialization declaration for a member of a class
648 template or a member template that appears in namespace scope,
649 the member template and some of its enclosing class templates may
650 remain unspecialized, except that the declaration shall not
651 explicitly specialize a class member template if its enclosing
652 class templates are not explicitly specialized as well. */
653 if (current_template_parms)
654 error ("enclosing class templates are not explicitly specialized");
657 /* We've just seen template <>. */
659 void
660 begin_specialization (void)
662 begin_scope (sk_template_spec, NULL);
663 note_template_header (1);
664 check_specialization_scope ();
667 /* Called at then end of processing a declaration preceded by
668 template<>. */
670 void
671 end_specialization (void)
673 finish_scope ();
674 reset_specialization ();
677 /* Any template <>'s that we have seen thus far are not referring to a
678 function specialization. */
680 void
681 reset_specialization (void)
683 processing_specialization = 0;
684 template_header_count = 0;
687 /* We've just seen a template header. If SPECIALIZATION is nonzero,
688 it was of the form template <>. */
690 static void
691 note_template_header (int specialization)
693 processing_specialization = specialization;
694 template_header_count++;
697 /* We're beginning an explicit instantiation. */
699 void
700 begin_explicit_instantiation (void)
702 gcc_assert (!processing_explicit_instantiation);
703 processing_explicit_instantiation = true;
707 void
708 end_explicit_instantiation (void)
710 gcc_assert (processing_explicit_instantiation);
711 processing_explicit_instantiation = false;
714 /* A explicit specialization or partial specialization TMPL is being
715 declared. Check that the namespace in which the specialization is
716 occurring is permissible. Returns false iff it is invalid to
717 specialize TMPL in the current namespace. */
719 static bool
720 check_specialization_namespace (tree tmpl)
722 tree tpl_ns = decl_namespace_context (tmpl);
724 /* [tmpl.expl.spec]
726 An explicit specialization shall be declared in the namespace of
727 which the template is a member, or, for member templates, in the
728 namespace of which the enclosing class or enclosing class
729 template is a member. An explicit specialization of a member
730 function, member class or static data member of a class template
731 shall be declared in the namespace of which the class template is
732 a member. */
733 if (is_associated_namespace (current_namespace, tpl_ns))
734 /* Same or super-using namespace. */
735 return true;
736 else
738 pedwarn ("specialization of `%D' in different namespace", tmpl);
739 cp_pedwarn_at (" from definition of `%#D'", tmpl);
740 return false;
744 /* The TYPE is being declared. If it is a template type, that means it
745 is a partial specialization. Do appropriate error-checking. */
747 void
748 maybe_process_partial_specialization (tree type)
750 /* TYPE maybe an ERROR_MARK_NODE. */
751 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
753 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
755 /* This is for ordinary explicit specialization and partial
756 specialization of a template class such as:
758 template <> class C<int>;
762 template <class T> class C<T*>;
764 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
766 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
767 && !COMPLETE_TYPE_P (type))
769 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
770 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
771 if (processing_template_decl)
772 push_template_decl (TYPE_MAIN_DECL (type));
774 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
775 error ("specialization of `%T' after instantiation", type);
777 else if (CLASS_TYPE_P (type)
778 && !CLASSTYPE_USE_TEMPLATE (type)
779 && CLASSTYPE_TEMPLATE_INFO (type)
780 && context && CLASS_TYPE_P (context)
781 && CLASSTYPE_TEMPLATE_INFO (context))
783 /* This is for an explicit specialization of member class
784 template according to [temp.expl.spec/18]:
786 template <> template <class U> class C<int>::D;
788 The context `C<int>' must be an implicit instantiation.
789 Otherwise this is just a member class template declared
790 earlier like:
792 template <> class C<int> { template <class U> class D; };
793 template <> template <class U> class C<int>::D;
795 In the first case, `C<int>::D' is a specialization of `C<T>::D'
796 while in the second case, `C<int>::D' is a primary template
797 and `C<T>::D' may not exist. */
799 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
800 && !COMPLETE_TYPE_P (type))
802 tree t;
804 if (current_namespace
805 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
807 pedwarn ("specializing `%#T' in different namespace", type);
808 cp_pedwarn_at (" from definition of `%#D'",
809 CLASSTYPE_TI_TEMPLATE (type));
812 /* Check for invalid specialization after instantiation:
814 template <> template <> class C<int>::D<int>;
815 template <> template <class U> class C<int>::D; */
817 for (t = DECL_TEMPLATE_INSTANTIATIONS
818 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
819 t; t = TREE_CHAIN (t))
820 if (TREE_VALUE (t) != type
821 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
822 error ("specialization `%T' after instantiation `%T'",
823 type, TREE_VALUE (t));
825 /* Mark TYPE as a specialization. And as a result, we only
826 have one level of template argument for the innermost
827 class template. */
828 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
829 CLASSTYPE_TI_ARGS (type)
830 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
833 else if (processing_specialization)
834 error ("explicit specialization of non-template `%T'", type);
837 /* Retrieve the specialization (in the sense of [temp.spec] - a
838 specialization is either an instantiation or an explicit
839 specialization) of TMPL for the given template ARGS. If there is
840 no such specialization, return NULL_TREE. The ARGS are a vector of
841 arguments, or a vector of vectors of arguments, in the case of
842 templates with more than one level of parameters. */
844 static tree
845 retrieve_specialization (tree tmpl, tree args)
847 tree s;
849 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
851 /* There should be as many levels of arguments as there are
852 levels of parameters. */
853 gcc_assert (TMPL_ARGS_DEPTH (args)
854 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
856 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
857 s != NULL_TREE;
858 s = TREE_CHAIN (s))
859 if (comp_template_args (TREE_PURPOSE (s), args))
860 return TREE_VALUE (s);
862 return NULL_TREE;
865 /* Like retrieve_specialization, but for local declarations. */
867 static tree
868 retrieve_local_specialization (tree tmpl)
870 tree spec = htab_find_with_hash (local_specializations, tmpl,
871 htab_hash_pointer (tmpl));
872 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
875 /* Returns nonzero iff DECL is a specialization of TMPL. */
878 is_specialization_of (tree decl, tree tmpl)
880 tree t;
882 if (TREE_CODE (decl) == FUNCTION_DECL)
884 for (t = decl;
885 t != NULL_TREE;
886 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
887 if (t == tmpl)
888 return 1;
890 else
892 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
894 for (t = TREE_TYPE (decl);
895 t != NULL_TREE;
896 t = CLASSTYPE_USE_TEMPLATE (t)
897 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
898 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
899 return 1;
902 return 0;
905 /* Returns nonzero iff DECL is a specialization of friend declaration
906 FRIEND according to [temp.friend]. */
908 bool
909 is_specialization_of_friend (tree decl, tree friend)
911 bool need_template = true;
912 int template_depth;
914 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
916 /* For [temp.friend/6] when FRIEND is an ordinary member function
917 of a template class, we want to check if DECL is a specialization
918 if this. */
919 if (TREE_CODE (friend) == FUNCTION_DECL
920 && DECL_TEMPLATE_INFO (friend)
921 && !DECL_USE_TEMPLATE (friend))
923 friend = DECL_TI_TEMPLATE (friend);
924 need_template = false;
927 /* There is nothing to do if this is not a template friend. */
928 if (TREE_CODE (friend) != TEMPLATE_DECL)
929 return 0;
931 if (is_specialization_of (decl, friend))
932 return 1;
934 /* [temp.friend/6]
935 A member of a class template may be declared to be a friend of a
936 non-template class. In this case, the corresponding member of
937 every specialization of the class template is a friend of the
938 class granting friendship.
940 For example, given a template friend declaration
942 template <class T> friend void A<T>::f();
944 the member function below is considered a friend
946 template <> struct A<int> {
947 void f();
950 For this type of template friend, TEMPLATE_DEPTH below will be
951 nonzero. To determine if DECL is a friend of FRIEND, we first
952 check if the enclosing class is a specialization of another. */
954 template_depth = template_class_depth (DECL_CONTEXT (friend));
955 if (template_depth
956 && DECL_CLASS_SCOPE_P (decl)
957 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
958 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
960 /* Next, we check the members themselves. In order to handle
961 a few tricky cases like
963 template <class T> friend void A<T>::g(T t);
964 template <class T> template <T t> friend void A<T>::h();
966 we need to figure out what ARGS is (corresponding to `T' in above
967 examples) from DECL for later processing. */
969 tree context = DECL_CONTEXT (decl);
970 tree args = NULL_TREE;
971 int current_depth = 0;
972 while (current_depth < template_depth)
974 if (CLASSTYPE_TEMPLATE_INFO (context))
976 if (current_depth == 0)
977 args = TYPE_TI_ARGS (context);
978 else
979 args = add_to_template_args (TYPE_TI_ARGS (context), args);
980 current_depth++;
982 context = TYPE_CONTEXT (context);
985 if (TREE_CODE (decl) == FUNCTION_DECL)
987 bool is_template;
988 tree friend_type;
989 tree decl_type;
990 tree friend_args_type;
991 tree decl_args_type;
993 /* Make sure that both DECL and FRIEND are templates or
994 non-templates. */
995 is_template = DECL_TEMPLATE_INFO (decl)
996 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
997 if (need_template ^ is_template)
998 return 0;
999 else if (is_template)
1001 /* If both are templates, check template parameter list. */
1002 tree friend_parms
1003 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1004 args, tf_none);
1005 if (!comp_template_parms
1006 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1007 friend_parms))
1008 return 0;
1010 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1012 else
1013 decl_type = TREE_TYPE (decl);
1015 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1016 tf_none, NULL_TREE);
1017 if (friend_type == error_mark_node)
1018 return 0;
1020 /* Check if return types match. */
1021 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1022 return 0;
1024 /* Check if function parameter types match, ignoring the
1025 `this' parameter. */
1026 friend_args_type = TYPE_ARG_TYPES (friend_type);
1027 decl_args_type = TYPE_ARG_TYPES (decl_type);
1028 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1029 friend_args_type = TREE_CHAIN (friend_args_type);
1030 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1031 decl_args_type = TREE_CHAIN (decl_args_type);
1032 if (compparms (decl_args_type, friend_args_type))
1033 return 1;
1036 return 0;
1039 /* Register the specialization SPEC as a specialization of TMPL with
1040 the indicated ARGS. Returns SPEC, or an equivalent prior
1041 declaration, if available. */
1043 static tree
1044 register_specialization (tree spec, tree tmpl, tree args)
1046 tree s;
1048 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1050 if (TREE_CODE (spec) == FUNCTION_DECL
1051 && uses_template_parms (DECL_TI_ARGS (spec)))
1052 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1053 register it; we want the corresponding TEMPLATE_DECL instead.
1054 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1055 the more obvious `uses_template_parms (spec)' to avoid problems
1056 with default function arguments. In particular, given
1057 something like this:
1059 template <class T> void f(T t1, T t = T())
1061 the default argument expression is not substituted for in an
1062 instantiation unless and until it is actually needed. */
1063 return spec;
1065 /* There should be as many levels of arguments as there are
1066 levels of parameters. */
1067 gcc_assert (TMPL_ARGS_DEPTH (args)
1068 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1070 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1071 s != NULL_TREE;
1072 s = TREE_CHAIN (s))
1074 tree fn = TREE_VALUE (s);
1076 /* We can sometimes try to re-register a specialization that we've
1077 already got. In particular, regenerate_decl_from_template
1078 calls duplicate_decls which will update the specialization
1079 list. But, we'll still get called again here anyhow. It's
1080 more convenient to simply allow this than to try to prevent it. */
1081 if (fn == spec)
1082 return spec;
1083 else if (DECL_TEMPLATE_SPECIALIZATION (spec)
1084 && comp_template_args (TREE_PURPOSE (s), args))
1086 if (DECL_TEMPLATE_INSTANTIATION (fn))
1088 if (TREE_USED (fn)
1089 || DECL_EXPLICIT_INSTANTIATION (fn))
1091 error ("specialization of %D after instantiation",
1092 fn);
1093 return spec;
1095 else
1097 /* This situation should occur only if the first
1098 specialization is an implicit instantiation, the
1099 second is an explicit specialization, and the
1100 implicit instantiation has not yet been used.
1101 That situation can occur if we have implicitly
1102 instantiated a member function and then
1103 specialized it later.
1105 We can also wind up here if a friend declaration
1106 that looked like an instantiation turns out to be
1107 a specialization:
1109 template <class T> void foo(T);
1110 class S { friend void foo<>(int) };
1111 template <> void foo(int);
1113 We transform the existing DECL in place so that
1114 any pointers to it become pointers to the updated
1115 declaration.
1117 If there was a definition for the template, but
1118 not for the specialization, we want this to look
1119 as if there were no definition, and vice
1120 versa. */
1121 DECL_INITIAL (fn) = NULL_TREE;
1122 duplicate_decls (spec, fn);
1124 return fn;
1127 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1129 if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
1130 /* Dup decl failed, but this is a new definition. Set
1131 the line number so any errors match this new
1132 definition. */
1133 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1135 return fn;
1140 /* A specialization must be declared in the same namespace as the
1141 template it is specializing. */
1142 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1143 && !check_specialization_namespace (tmpl))
1144 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1146 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1147 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1149 return spec;
1152 /* Unregister the specialization SPEC as a specialization of TMPL.
1153 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1154 if the SPEC was listed as a specialization of TMPL. */
1156 bool
1157 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1159 tree* s;
1161 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1162 *s != NULL_TREE;
1163 s = &TREE_CHAIN (*s))
1164 if (TREE_VALUE (*s) == spec)
1166 if (!new_spec)
1167 *s = TREE_CHAIN (*s);
1168 else
1169 TREE_VALUE (*s) = new_spec;
1170 return 1;
1173 return 0;
1176 /* Compare an entry in the local specializations hash table P1 (which
1177 is really a pointer to a TREE_LIST) with P2 (which is really a
1178 DECL). */
1180 static int
1181 eq_local_specializations (const void *p1, const void *p2)
1183 return TREE_VALUE ((tree) p1) == (tree) p2;
1186 /* Hash P1, an entry in the local specializations table. */
1188 static hashval_t
1189 hash_local_specialization (const void* p1)
1191 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1194 /* Like register_specialization, but for local declarations. We are
1195 registering SPEC, an instantiation of TMPL. */
1197 static void
1198 register_local_specialization (tree spec, tree tmpl)
1200 void **slot;
1202 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1203 htab_hash_pointer (tmpl), INSERT);
1204 *slot = build_tree_list (spec, tmpl);
1207 /* Print the list of candidate FNS in an error message. */
1209 void
1210 print_candidates (tree fns)
1212 tree fn;
1214 const char *str = "candidates are:";
1216 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1218 tree f;
1220 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1221 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1222 str = " ";
1226 /* Returns the template (one of the functions given by TEMPLATE_ID)
1227 which can be specialized to match the indicated DECL with the
1228 explicit template args given in TEMPLATE_ID. The DECL may be
1229 NULL_TREE if none is available. In that case, the functions in
1230 TEMPLATE_ID are non-members.
1232 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1233 specialization of a member template.
1235 The TEMPLATE_COUNT is the number of references to qualifying
1236 template classes that appeared in the name of the function. See
1237 check_explicit_specialization for a more accurate description.
1239 The template args (those explicitly specified and those deduced)
1240 are output in a newly created vector *TARGS_OUT.
1242 If it is impossible to determine the result, an error message is
1243 issued. The error_mark_node is returned to indicate failure. */
1245 static tree
1246 determine_specialization (tree template_id,
1247 tree decl,
1248 tree* targs_out,
1249 int need_member_template,
1250 int template_count)
1252 tree fns;
1253 tree targs;
1254 tree explicit_targs;
1255 tree candidates = NULL_TREE;
1256 tree templates = NULL_TREE;
1257 int header_count;
1258 struct cp_binding_level *b;
1260 *targs_out = NULL_TREE;
1262 if (template_id == error_mark_node)
1263 return error_mark_node;
1265 fns = TREE_OPERAND (template_id, 0);
1266 explicit_targs = TREE_OPERAND (template_id, 1);
1268 if (fns == error_mark_node)
1269 return error_mark_node;
1271 /* Check for baselinks. */
1272 if (BASELINK_P (fns))
1273 fns = BASELINK_FUNCTIONS (fns);
1275 if (!is_overloaded_fn (fns))
1277 error ("`%D' is not a function template", fns);
1278 return error_mark_node;
1281 /* Count the number of template headers specified for this
1282 specialization. */
1283 header_count = 0;
1284 for (b = current_binding_level;
1285 b->kind == sk_template_parms || b->kind == sk_template_spec;
1286 b = b->level_chain)
1287 ++header_count;
1289 for (; fns; fns = OVL_NEXT (fns))
1291 tree fn = OVL_CURRENT (fns);
1293 if (TREE_CODE (fn) == TEMPLATE_DECL)
1295 tree decl_arg_types;
1296 tree fn_arg_types;
1298 /* DECL might be a specialization of FN. */
1300 /* Adjust the type of DECL in case FN is a static member. */
1301 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1302 if (DECL_STATIC_FUNCTION_P (fn)
1303 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1304 decl_arg_types = TREE_CHAIN (decl_arg_types);
1306 /* Check that the number of function parameters matches.
1307 For example,
1308 template <class T> void f(int i = 0);
1309 template <> void f<int>();
1310 The specialization f<int> is invalid but is not caught
1311 by get_bindings below. */
1313 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1314 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1315 continue;
1317 /* For a non-static member function, we need to make sure that
1318 the const qualification is the same. This can be done by
1319 checking the 'this' in the argument list. */
1320 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1321 && !same_type_p (TREE_VALUE (fn_arg_types),
1322 TREE_VALUE (decl_arg_types)))
1323 continue;
1325 /* In case of explicit specialization, we need to check if
1326 the number of template headers appearing in the specialization
1327 is correct. This is usually done in check_explicit_specialization,
1328 but the check done there cannot be exhaustive when specializing
1329 member functions. Consider the following code:
1331 template <> void A<int>::f(int);
1332 template <> template <> void A<int>::f(int);
1334 Assuming that A<int> is not itself an explicit specialization
1335 already, the first line specializes "f" which is a non-template
1336 member function, whilst the second line specializes "f" which
1337 is a template member function. So both lines are syntactically
1338 correct, and check_explicit_specialization does not reject
1339 them.
1341 Here, we can do better, as we are matching the specialization
1342 against the declarations. We count the number of template
1343 headers, and we check if they match TEMPLATE_COUNT + 1
1344 (TEMPLATE_COUNT is the number of qualifying template classes,
1345 plus there must be another header for the member template
1346 itself).
1348 Notice that if header_count is zero, this is not a
1349 specialization but rather a template instantiation, so there
1350 is no check we can perform here. */
1351 if (header_count && header_count != template_count + 1)
1352 continue;
1354 /* See whether this function might be a specialization of this
1355 template. */
1356 targs = get_bindings (fn, decl, explicit_targs);
1358 if (!targs)
1359 /* We cannot deduce template arguments that when used to
1360 specialize TMPL will produce DECL. */
1361 continue;
1363 /* Save this template, and the arguments deduced. */
1364 templates = tree_cons (targs, fn, templates);
1366 else if (need_member_template)
1367 /* FN is an ordinary member function, and we need a
1368 specialization of a member template. */
1370 else if (TREE_CODE (fn) != FUNCTION_DECL)
1371 /* We can get IDENTIFIER_NODEs here in certain erroneous
1372 cases. */
1374 else if (!DECL_FUNCTION_MEMBER_P (fn))
1375 /* This is just an ordinary non-member function. Nothing can
1376 be a specialization of that. */
1378 else if (DECL_ARTIFICIAL (fn))
1379 /* Cannot specialize functions that are created implicitly. */
1381 else
1383 tree decl_arg_types;
1385 /* This is an ordinary member function. However, since
1386 we're here, we can assume it's enclosing class is a
1387 template class. For example,
1389 template <typename T> struct S { void f(); };
1390 template <> void S<int>::f() {}
1392 Here, S<int>::f is a non-template, but S<int> is a
1393 template class. If FN has the same type as DECL, we
1394 might be in business. */
1396 if (!DECL_TEMPLATE_INFO (fn))
1397 /* Its enclosing class is an explicit specialization
1398 of a template class. This is not a candidate. */
1399 continue;
1401 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1402 TREE_TYPE (TREE_TYPE (fn))))
1403 /* The return types differ. */
1404 continue;
1406 /* Adjust the type of DECL in case FN is a static member. */
1407 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1408 if (DECL_STATIC_FUNCTION_P (fn)
1409 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1410 decl_arg_types = TREE_CHAIN (decl_arg_types);
1412 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1413 decl_arg_types))
1414 /* They match! */
1415 candidates = tree_cons (NULL_TREE, fn, candidates);
1419 if (templates && TREE_CHAIN (templates))
1421 /* We have:
1423 [temp.expl.spec]
1425 It is possible for a specialization with a given function
1426 signature to be instantiated from more than one function
1427 template. In such cases, explicit specification of the
1428 template arguments must be used to uniquely identify the
1429 function template specialization being specialized.
1431 Note that here, there's no suggestion that we're supposed to
1432 determine which of the candidate templates is most
1433 specialized. However, we, also have:
1435 [temp.func.order]
1437 Partial ordering of overloaded function template
1438 declarations is used in the following contexts to select
1439 the function template to which a function template
1440 specialization refers:
1442 -- when an explicit specialization refers to a function
1443 template.
1445 So, we do use the partial ordering rules, at least for now.
1446 This extension can only serve to make invalid programs valid,
1447 so it's safe. And, there is strong anecdotal evidence that
1448 the committee intended the partial ordering rules to apply;
1449 the EDG front-end has that behavior, and John Spicer claims
1450 that the committee simply forgot to delete the wording in
1451 [temp.expl.spec]. */
1452 tree tmpl = most_specialized (templates, decl, explicit_targs);
1453 if (tmpl && tmpl != error_mark_node)
1455 targs = get_bindings (tmpl, decl, explicit_targs);
1456 templates = tree_cons (targs, tmpl, NULL_TREE);
1460 if (templates == NULL_TREE && candidates == NULL_TREE)
1462 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1463 template_id, decl);
1464 return error_mark_node;
1466 else if ((templates && TREE_CHAIN (templates))
1467 || (candidates && TREE_CHAIN (candidates))
1468 || (templates && candidates))
1470 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1471 template_id, decl);
1472 chainon (candidates, templates);
1473 print_candidates (candidates);
1474 return error_mark_node;
1477 /* We have one, and exactly one, match. */
1478 if (candidates)
1480 /* It was a specialization of an ordinary member function in a
1481 template class. */
1482 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1483 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1486 /* It was a specialization of a template. */
1487 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1488 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1490 *targs_out = copy_node (targs);
1491 SET_TMPL_ARGS_LEVEL (*targs_out,
1492 TMPL_ARGS_DEPTH (*targs_out),
1493 TREE_PURPOSE (templates));
1495 else
1496 *targs_out = TREE_PURPOSE (templates);
1497 return TREE_VALUE (templates);
1500 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1501 but with the default argument values filled in from those in the
1502 TMPL_TYPES. */
1504 static tree
1505 copy_default_args_to_explicit_spec_1 (tree spec_types,
1506 tree tmpl_types)
1508 tree new_spec_types;
1510 if (!spec_types)
1511 return NULL_TREE;
1513 if (spec_types == void_list_node)
1514 return void_list_node;
1516 /* Substitute into the rest of the list. */
1517 new_spec_types =
1518 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1519 TREE_CHAIN (tmpl_types));
1521 /* Add the default argument for this parameter. */
1522 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1523 TREE_VALUE (spec_types),
1524 new_spec_types);
1527 /* DECL is an explicit specialization. Replicate default arguments
1528 from the template it specializes. (That way, code like:
1530 template <class T> void f(T = 3);
1531 template <> void f(double);
1532 void g () { f (); }
1534 works, as required.) An alternative approach would be to look up
1535 the correct default arguments at the call-site, but this approach
1536 is consistent with how implicit instantiations are handled. */
1538 static void
1539 copy_default_args_to_explicit_spec (tree decl)
1541 tree tmpl;
1542 tree spec_types;
1543 tree tmpl_types;
1544 tree new_spec_types;
1545 tree old_type;
1546 tree new_type;
1547 tree t;
1548 tree object_type = NULL_TREE;
1549 tree in_charge = NULL_TREE;
1550 tree vtt = NULL_TREE;
1552 /* See if there's anything we need to do. */
1553 tmpl = DECL_TI_TEMPLATE (decl);
1554 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1555 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1556 if (TREE_PURPOSE (t))
1557 break;
1558 if (!t)
1559 return;
1561 old_type = TREE_TYPE (decl);
1562 spec_types = TYPE_ARG_TYPES (old_type);
1564 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1566 /* Remove the this pointer, but remember the object's type for
1567 CV quals. */
1568 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1569 spec_types = TREE_CHAIN (spec_types);
1570 tmpl_types = TREE_CHAIN (tmpl_types);
1572 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1574 /* DECL may contain more parameters than TMPL due to the extra
1575 in-charge parameter in constructors and destructors. */
1576 in_charge = spec_types;
1577 spec_types = TREE_CHAIN (spec_types);
1579 if (DECL_HAS_VTT_PARM_P (decl))
1581 vtt = spec_types;
1582 spec_types = TREE_CHAIN (spec_types);
1586 /* Compute the merged default arguments. */
1587 new_spec_types =
1588 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1590 /* Compute the new FUNCTION_TYPE. */
1591 if (object_type)
1593 if (vtt)
1594 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1595 TREE_VALUE (vtt),
1596 new_spec_types);
1598 if (in_charge)
1599 /* Put the in-charge parameter back. */
1600 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1601 TREE_VALUE (in_charge),
1602 new_spec_types);
1604 new_type = build_method_type_directly (object_type,
1605 TREE_TYPE (old_type),
1606 new_spec_types);
1608 else
1609 new_type = build_function_type (TREE_TYPE (old_type),
1610 new_spec_types);
1611 new_type = cp_build_type_attribute_variant (new_type,
1612 TYPE_ATTRIBUTES (old_type));
1613 new_type = build_exception_variant (new_type,
1614 TYPE_RAISES_EXCEPTIONS (old_type));
1615 TREE_TYPE (decl) = new_type;
1618 /* Check to see if the function just declared, as indicated in
1619 DECLARATOR, and in DECL, is a specialization of a function
1620 template. We may also discover that the declaration is an explicit
1621 instantiation at this point.
1623 Returns DECL, or an equivalent declaration that should be used
1624 instead if all goes well. Issues an error message if something is
1625 amiss. Returns error_mark_node if the error is not easily
1626 recoverable.
1628 FLAGS is a bitmask consisting of the following flags:
1630 2: The function has a definition.
1631 4: The function is a friend.
1633 The TEMPLATE_COUNT is the number of references to qualifying
1634 template classes that appeared in the name of the function. For
1635 example, in
1637 template <class T> struct S { void f(); };
1638 void S<int>::f();
1640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1641 classes are not counted in the TEMPLATE_COUNT, so that in
1643 template <class T> struct S {};
1644 template <> struct S<int> { void f(); }
1645 template <> void S<int>::f();
1647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1648 invalid; there should be no template <>.)
1650 If the function is a specialization, it is marked as such via
1651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1652 is set up correctly, and it is added to the list of specializations
1653 for that template. */
1655 tree
1656 check_explicit_specialization (tree declarator,
1657 tree decl,
1658 int template_count,
1659 int flags)
1661 int have_def = flags & 2;
1662 int is_friend = flags & 4;
1663 int specialization = 0;
1664 int explicit_instantiation = 0;
1665 int member_specialization = 0;
1666 tree ctype = DECL_CLASS_CONTEXT (decl);
1667 tree dname = DECL_NAME (decl);
1668 tmpl_spec_kind tsk;
1670 tsk = current_tmpl_spec_kind (template_count);
1672 switch (tsk)
1674 case tsk_none:
1675 if (processing_specialization)
1677 specialization = 1;
1678 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1680 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1682 if (is_friend)
1683 /* This could be something like:
1685 template <class T> void f(T);
1686 class S { friend void f<>(int); } */
1687 specialization = 1;
1688 else
1690 /* This case handles bogus declarations like template <>
1691 template <class T> void f<int>(); */
1693 error ("template-id `%D' in declaration of primary template",
1694 declarator);
1695 return decl;
1698 break;
1700 case tsk_invalid_member_spec:
1701 /* The error has already been reported in
1702 check_specialization_scope. */
1703 return error_mark_node;
1705 case tsk_invalid_expl_inst:
1706 error ("template parameter list used in explicit instantiation");
1708 /* Fall through. */
1710 case tsk_expl_inst:
1711 if (have_def)
1712 error ("definition provided for explicit instantiation");
1714 explicit_instantiation = 1;
1715 break;
1717 case tsk_excessive_parms:
1718 case tsk_insufficient_parms:
1719 if (tsk == tsk_excessive_parms)
1720 error ("too many template parameter lists in declaration of `%D'",
1721 decl);
1722 else if (template_header_count)
1723 error("too few template parameter lists in declaration of `%D'",
1724 decl);
1725 else
1726 error("explicit specialization of `%D' must be introduced by "
1727 "`template <>'", decl);
1729 /* Fall through. */
1730 case tsk_expl_spec:
1731 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1732 if (ctype)
1733 member_specialization = 1;
1734 else
1735 specialization = 1;
1736 break;
1738 case tsk_template:
1739 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1741 /* This case handles bogus declarations like template <>
1742 template <class T> void f<int>(); */
1744 if (uses_template_parms (declarator))
1745 error ("function template partial specialization `%D' "
1746 "is not allowed", declarator);
1747 else
1748 error ("template-id `%D' in declaration of primary template",
1749 declarator);
1750 return decl;
1753 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1754 /* This is a specialization of a member template, without
1755 specialization the containing class. Something like:
1757 template <class T> struct S {
1758 template <class U> void f (U);
1760 template <> template <class U> void S<int>::f(U) {}
1762 That's a specialization -- but of the entire template. */
1763 specialization = 1;
1764 break;
1766 default:
1767 gcc_unreachable ();
1770 if (specialization || member_specialization)
1772 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1773 for (; t; t = TREE_CHAIN (t))
1774 if (TREE_PURPOSE (t))
1776 pedwarn
1777 ("default argument specified in explicit specialization");
1778 break;
1780 if (current_lang_name == lang_name_c)
1781 error ("template specialization with C linkage");
1784 if (specialization || member_specialization || explicit_instantiation)
1786 tree tmpl = NULL_TREE;
1787 tree targs = NULL_TREE;
1789 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1790 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1792 tree fns;
1794 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1795 if (ctype)
1796 fns = dname;
1797 else
1799 /* If there is no class context, the explicit instantiation
1800 must be at namespace scope. */
1801 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1803 /* Find the namespace binding, using the declaration
1804 context. */
1805 fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
1808 declarator = lookup_template_function (fns, NULL_TREE);
1811 if (declarator == error_mark_node)
1812 return error_mark_node;
1814 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1816 if (!explicit_instantiation)
1817 /* A specialization in class scope. This is invalid,
1818 but the error will already have been flagged by
1819 check_specialization_scope. */
1820 return error_mark_node;
1821 else
1823 /* It's not valid to write an explicit instantiation in
1824 class scope, e.g.:
1826 class C { template void f(); }
1828 This case is caught by the parser. However, on
1829 something like:
1831 template class C { void f(); };
1833 (which is invalid) we can get here. The error will be
1834 issued later. */
1838 return decl;
1840 else if (ctype != NULL_TREE
1841 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1842 IDENTIFIER_NODE))
1844 /* Find the list of functions in ctype that have the same
1845 name as the declared function. */
1846 tree name = TREE_OPERAND (declarator, 0);
1847 tree fns = NULL_TREE;
1848 int idx;
1850 if (constructor_name_p (name, ctype))
1852 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1854 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1855 : !TYPE_HAS_DESTRUCTOR (ctype))
1857 /* From [temp.expl.spec]:
1859 If such an explicit specialization for the member
1860 of a class template names an implicitly-declared
1861 special member function (clause _special_), the
1862 program is ill-formed.
1864 Similar language is found in [temp.explicit]. */
1865 error ("specialization of implicitly-declared special member function");
1866 return error_mark_node;
1869 name = is_constructor ? ctor_identifier : dtor_identifier;
1872 if (!DECL_CONV_FN_P (decl))
1874 idx = lookup_fnfields_1 (ctype, name);
1875 if (idx >= 0)
1876 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
1878 else
1880 VEC(tree) *methods;
1881 tree ovl;
1883 /* For a type-conversion operator, we cannot do a
1884 name-based lookup. We might be looking for `operator
1885 int' which will be a specialization of `operator T'.
1886 So, we find *all* the conversion operators, and then
1887 select from them. */
1888 fns = NULL_TREE;
1890 methods = CLASSTYPE_METHOD_VEC (ctype);
1891 if (methods)
1892 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1893 VEC_iterate (tree, methods, idx, ovl);
1894 ++idx)
1896 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1897 /* There are no more conversion functions. */
1898 break;
1900 /* Glue all these conversion functions together
1901 with those we already have. */
1902 for (; ovl; ovl = OVL_NEXT (ovl))
1903 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1907 if (fns == NULL_TREE)
1909 error ("no member function `%D' declared in `%T'",
1910 name, ctype);
1911 return error_mark_node;
1913 else
1914 TREE_OPERAND (declarator, 0) = fns;
1917 /* Figure out what exactly is being specialized at this point.
1918 Note that for an explicit instantiation, even one for a
1919 member function, we cannot tell apriori whether the
1920 instantiation is for a member template, or just a member
1921 function of a template class. Even if a member template is
1922 being instantiated, the member template arguments may be
1923 elided if they can be deduced from the rest of the
1924 declaration. */
1925 tmpl = determine_specialization (declarator, decl,
1926 &targs,
1927 member_specialization,
1928 template_count);
1930 if (!tmpl || tmpl == error_mark_node)
1931 /* We couldn't figure out what this declaration was
1932 specializing. */
1933 return error_mark_node;
1934 else
1936 tree gen_tmpl = most_general_template (tmpl);
1938 if (explicit_instantiation)
1940 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1941 is done by do_decl_instantiation later. */
1943 int arg_depth = TMPL_ARGS_DEPTH (targs);
1944 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1946 if (arg_depth > parm_depth)
1948 /* If TMPL is not the most general template (for
1949 example, if TMPL is a friend template that is
1950 injected into namespace scope), then there will
1951 be too many levels of TARGS. Remove some of them
1952 here. */
1953 int i;
1954 tree new_targs;
1956 new_targs = make_tree_vec (parm_depth);
1957 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1958 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1959 = TREE_VEC_ELT (targs, i);
1960 targs = new_targs;
1963 return instantiate_template (tmpl, targs, tf_error);
1966 /* If we thought that the DECL was a member function, but it
1967 turns out to be specializing a static member function,
1968 make DECL a static member function as well. */
1969 if (DECL_STATIC_FUNCTION_P (tmpl)
1970 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1971 revert_static_member_fn (decl);
1973 /* If this is a specialization of a member template of a
1974 template class. In we want to return the TEMPLATE_DECL,
1975 not the specialization of it. */
1976 if (tsk == tsk_template)
1978 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1979 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1980 if (have_def)
1982 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
1983 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
1984 = DECL_SOURCE_LOCATION (decl);
1986 return tmpl;
1989 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1990 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1992 /* Inherit default function arguments from the template
1993 DECL is specializing. */
1994 copy_default_args_to_explicit_spec (decl);
1996 /* This specialization has the same protection as the
1997 template it specializes. */
1998 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1999 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2001 if (is_friend && !have_def)
2002 /* This is not really a declaration of a specialization.
2003 It's just the name of an instantiation. But, it's not
2004 a request for an instantiation, either. */
2005 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2006 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2007 /* This is indeed a specialization. In case of constructors
2008 and destructors, we need in-charge and not-in-charge
2009 versions in V3 ABI. */
2010 clone_function_decl (decl, /*update_method_vec_p=*/0);
2012 /* Register this specialization so that we can find it
2013 again. */
2014 decl = register_specialization (decl, gen_tmpl, targs);
2018 return decl;
2021 /* TYPE is being declared. Verify that the use of template headers
2022 and such is reasonable. Issue error messages if not. */
2024 void
2025 maybe_check_template_type (tree type)
2027 if (template_header_count)
2029 /* We are in the scope of some `template <...>' header. */
2031 int context_depth
2032 = template_class_depth_real (TYPE_CONTEXT (type),
2033 /*count_specializations=*/1);
2035 if (template_header_count <= context_depth)
2036 /* This is OK; the template headers are for the context. We
2037 are actually too lenient here; like
2038 check_explicit_specialization we should consider the number
2039 of template types included in the actual declaration. For
2040 example,
2042 template <class T> struct S {
2043 template <class U> template <class V>
2044 struct I {};
2047 is invalid, but:
2049 template <class T> struct S {
2050 template <class U> struct I;
2053 template <class T> template <class U.
2054 struct S<T>::I {};
2056 is not. */
2058 else if (template_header_count > context_depth + 1)
2059 /* There are two many template parameter lists. */
2060 error ("too many template parameter lists in declaration of `%T'", type);
2064 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2065 parameters. These are represented in the same format used for
2066 DECL_TEMPLATE_PARMS. */
2069 comp_template_parms (tree parms1, tree parms2)
2071 tree p1;
2072 tree p2;
2074 if (parms1 == parms2)
2075 return 1;
2077 for (p1 = parms1, p2 = parms2;
2078 p1 != NULL_TREE && p2 != NULL_TREE;
2079 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2081 tree t1 = TREE_VALUE (p1);
2082 tree t2 = TREE_VALUE (p2);
2083 int i;
2085 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2086 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2088 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2089 return 0;
2091 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2093 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2094 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2096 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2097 return 0;
2099 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2100 continue;
2101 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2102 return 0;
2106 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2107 /* One set of parameters has more parameters lists than the
2108 other. */
2109 return 0;
2111 return 1;
2114 /* Complain if DECL shadows a template parameter.
2116 [temp.local]: A template-parameter shall not be redeclared within its
2117 scope (including nested scopes). */
2119 void
2120 check_template_shadow (tree decl)
2122 tree olddecl;
2124 /* If we're not in a template, we can't possibly shadow a template
2125 parameter. */
2126 if (!current_template_parms)
2127 return;
2129 /* Figure out what we're shadowing. */
2130 if (TREE_CODE (decl) == OVERLOAD)
2131 decl = OVL_CURRENT (decl);
2132 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2134 /* If there's no previous binding for this name, we're not shadowing
2135 anything, let alone a template parameter. */
2136 if (!olddecl)
2137 return;
2139 /* If we're not shadowing a template parameter, we're done. Note
2140 that OLDDECL might be an OVERLOAD (or perhaps even an
2141 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2142 node. */
2143 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2144 return;
2146 /* We check for decl != olddecl to avoid bogus errors for using a
2147 name inside a class. We check TPFI to avoid duplicate errors for
2148 inline member templates. */
2149 if (decl == olddecl
2150 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2151 return;
2153 cp_error_at ("declaration of `%#D'", decl);
2154 cp_error_at (" shadows template parm `%#D'", olddecl);
2157 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2158 ORIG_LEVEL, DECL, and TYPE. */
2160 static tree
2161 build_template_parm_index (int index,
2162 int level,
2163 int orig_level,
2164 tree decl,
2165 tree type)
2167 tree t = make_node (TEMPLATE_PARM_INDEX);
2168 TEMPLATE_PARM_IDX (t) = index;
2169 TEMPLATE_PARM_LEVEL (t) = level;
2170 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2171 TEMPLATE_PARM_DECL (t) = decl;
2172 TREE_TYPE (t) = type;
2173 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2174 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2175 TREE_READONLY (t) = TREE_READONLY (decl);
2177 return t;
2180 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2181 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2182 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2183 new one is created. */
2185 static tree
2186 reduce_template_parm_level (tree index, tree type, int levels)
2188 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2189 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2190 != TEMPLATE_PARM_LEVEL (index) - levels))
2192 tree orig_decl = TEMPLATE_PARM_DECL (index);
2193 tree decl, t;
2195 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2196 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2197 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2198 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2199 DECL_ARTIFICIAL (decl) = 1;
2200 SET_DECL_TEMPLATE_PARM_P (decl);
2202 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2203 TEMPLATE_PARM_LEVEL (index) - levels,
2204 TEMPLATE_PARM_ORIG_LEVEL (index),
2205 decl, type);
2206 TEMPLATE_PARM_DESCENDANTS (index) = t;
2208 /* Template template parameters need this. */
2209 DECL_TEMPLATE_PARMS (decl)
2210 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2213 return TEMPLATE_PARM_DESCENDANTS (index);
2216 /* Process information from new template parameter NEXT and append it to the
2217 LIST being built. This new parameter is a non-type parameter iff
2218 IS_NON_TYPE is true. */
2220 tree
2221 process_template_parm (tree list, tree next, bool is_non_type)
2223 tree parm;
2224 tree decl = 0;
2225 tree defval;
2226 int idx;
2228 parm = next;
2229 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2230 defval = TREE_PURPOSE (parm);
2232 if (list)
2234 tree p = TREE_VALUE (tree_last (list));
2236 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2237 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2238 else
2239 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2240 ++idx;
2242 else
2243 idx = 0;
2245 if (is_non_type)
2247 parm = TREE_VALUE (parm);
2249 SET_DECL_TEMPLATE_PARM_P (parm);
2251 /* [temp.param]
2253 The top-level cv-qualifiers on the template-parameter are
2254 ignored when determining its type. */
2255 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2257 /* A template parameter is not modifiable. */
2258 TREE_CONSTANT (parm) = 1;
2259 TREE_INVARIANT (parm) = 1;
2260 TREE_READONLY (parm) = 1;
2261 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2262 TREE_TYPE (parm) = void_type_node;
2263 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2264 TREE_CONSTANT (decl) = 1;
2265 TREE_INVARIANT (decl) = 1;
2266 TREE_READONLY (decl) = 1;
2267 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2268 = build_template_parm_index (idx, processing_template_decl,
2269 processing_template_decl,
2270 decl, TREE_TYPE (parm));
2272 else
2274 tree t;
2275 parm = TREE_VALUE (TREE_VALUE (parm));
2277 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2279 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2280 /* This is for distinguishing between real templates and template
2281 template parameters */
2282 TREE_TYPE (parm) = t;
2283 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2284 decl = parm;
2286 else
2288 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2289 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2290 decl = build_decl (TYPE_DECL, parm, t);
2293 TYPE_NAME (t) = decl;
2294 TYPE_STUB_DECL (t) = decl;
2295 parm = decl;
2296 TEMPLATE_TYPE_PARM_INDEX (t)
2297 = build_template_parm_index (idx, processing_template_decl,
2298 processing_template_decl,
2299 decl, TREE_TYPE (parm));
2301 DECL_ARTIFICIAL (decl) = 1;
2302 SET_DECL_TEMPLATE_PARM_P (decl);
2303 pushdecl (decl);
2304 parm = build_tree_list (defval, parm);
2305 return chainon (list, parm);
2308 /* The end of a template parameter list has been reached. Process the
2309 tree list into a parameter vector, converting each parameter into a more
2310 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2311 as PARM_DECLs. */
2313 tree
2314 end_template_parm_list (tree parms)
2316 int nparms;
2317 tree parm, next;
2318 tree saved_parmlist = make_tree_vec (list_length (parms));
2320 current_template_parms
2321 = tree_cons (size_int (processing_template_decl),
2322 saved_parmlist, current_template_parms);
2324 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2326 next = TREE_CHAIN (parm);
2327 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2328 TREE_CHAIN (parm) = NULL_TREE;
2331 --processing_template_parmlist;
2333 return saved_parmlist;
2336 /* end_template_decl is called after a template declaration is seen. */
2338 void
2339 end_template_decl (void)
2341 reset_specialization ();
2343 if (! processing_template_decl)
2344 return;
2346 /* This matches the pushlevel in begin_template_parm_list. */
2347 finish_scope ();
2349 --processing_template_decl;
2350 current_template_parms = TREE_CHAIN (current_template_parms);
2353 /* Given a template argument vector containing the template PARMS.
2354 The innermost PARMS are given first. */
2356 tree
2357 current_template_args (void)
2359 tree header;
2360 tree args = NULL_TREE;
2361 int length = TMPL_PARMS_DEPTH (current_template_parms);
2362 int l = length;
2364 /* If there is only one level of template parameters, we do not
2365 create a TREE_VEC of TREE_VECs. Instead, we return a single
2366 TREE_VEC containing the arguments. */
2367 if (length > 1)
2368 args = make_tree_vec (length);
2370 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2372 tree a = copy_node (TREE_VALUE (header));
2373 int i;
2375 TREE_TYPE (a) = NULL_TREE;
2376 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2378 tree t = TREE_VEC_ELT (a, i);
2380 /* T will be a list if we are called from within a
2381 begin/end_template_parm_list pair, but a vector directly
2382 if within a begin/end_member_template_processing pair. */
2383 if (TREE_CODE (t) == TREE_LIST)
2385 t = TREE_VALUE (t);
2387 if (TREE_CODE (t) == TYPE_DECL
2388 || TREE_CODE (t) == TEMPLATE_DECL)
2389 t = TREE_TYPE (t);
2390 else
2391 t = DECL_INITIAL (t);
2392 TREE_VEC_ELT (a, i) = t;
2396 if (length > 1)
2397 TREE_VEC_ELT (args, --l) = a;
2398 else
2399 args = a;
2402 return args;
2405 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2406 template PARMS. Used by push_template_decl below. */
2408 static tree
2409 build_template_decl (tree decl, tree parms)
2411 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2412 DECL_TEMPLATE_PARMS (tmpl) = parms;
2413 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2414 if (DECL_LANG_SPECIFIC (decl))
2416 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2417 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2418 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2419 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2420 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2421 if (DECL_OVERLOADED_OPERATOR_P (decl))
2422 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2423 DECL_OVERLOADED_OPERATOR_P (decl));
2426 return tmpl;
2429 struct template_parm_data
2431 /* The level of the template parameters we are currently
2432 processing. */
2433 int level;
2435 /* The index of the specialization argument we are currently
2436 processing. */
2437 int current_arg;
2439 /* An array whose size is the number of template parameters. The
2440 elements are nonzero if the parameter has been used in any one
2441 of the arguments processed so far. */
2442 int* parms;
2444 /* An array whose size is the number of template arguments. The
2445 elements are nonzero if the argument makes use of template
2446 parameters of this level. */
2447 int* arg_uses_template_parms;
2450 /* Subroutine of push_template_decl used to see if each template
2451 parameter in a partial specialization is used in the explicit
2452 argument list. If T is of the LEVEL given in DATA (which is
2453 treated as a template_parm_data*), then DATA->PARMS is marked
2454 appropriately. */
2456 static int
2457 mark_template_parm (tree t, void* data)
2459 int level;
2460 int idx;
2461 struct template_parm_data* tpd = (struct template_parm_data*) data;
2463 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2465 level = TEMPLATE_PARM_LEVEL (t);
2466 idx = TEMPLATE_PARM_IDX (t);
2468 else
2470 level = TEMPLATE_TYPE_LEVEL (t);
2471 idx = TEMPLATE_TYPE_IDX (t);
2474 if (level == tpd->level)
2476 tpd->parms[idx] = 1;
2477 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2480 /* Return zero so that for_each_template_parm will continue the
2481 traversal of the tree; we want to mark *every* template parm. */
2482 return 0;
2485 /* Process the partial specialization DECL. */
2487 static tree
2488 process_partial_specialization (tree decl)
2490 tree type = TREE_TYPE (decl);
2491 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2492 tree specargs = CLASSTYPE_TI_ARGS (type);
2493 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2494 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2495 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2496 int nargs = TREE_VEC_LENGTH (inner_args);
2497 int ntparms = TREE_VEC_LENGTH (inner_parms);
2498 int i;
2499 int did_error_intro = 0;
2500 struct template_parm_data tpd;
2501 struct template_parm_data tpd2;
2503 /* We check that each of the template parameters given in the
2504 partial specialization is used in the argument list to the
2505 specialization. For example:
2507 template <class T> struct S;
2508 template <class T> struct S<T*>;
2510 The second declaration is OK because `T*' uses the template
2511 parameter T, whereas
2513 template <class T> struct S<int>;
2515 is no good. Even trickier is:
2517 template <class T>
2518 struct S1
2520 template <class U>
2521 struct S2;
2522 template <class U>
2523 struct S2<T>;
2526 The S2<T> declaration is actually invalid; it is a
2527 full-specialization. Of course,
2529 template <class U>
2530 struct S2<T (*)(U)>;
2532 or some such would have been OK. */
2533 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2534 tpd.parms = alloca (sizeof (int) * ntparms);
2535 memset (tpd.parms, 0, sizeof (int) * ntparms);
2537 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2538 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2539 for (i = 0; i < nargs; ++i)
2541 tpd.current_arg = i;
2542 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2543 &mark_template_parm,
2544 &tpd,
2545 NULL);
2547 for (i = 0; i < ntparms; ++i)
2548 if (tpd.parms[i] == 0)
2550 /* One of the template parms was not used in the
2551 specialization. */
2552 if (!did_error_intro)
2554 error ("template parameters not used in partial specialization:");
2555 did_error_intro = 1;
2558 error (" `%D'",
2559 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2562 /* [temp.class.spec]
2564 The argument list of the specialization shall not be identical to
2565 the implicit argument list of the primary template. */
2566 if (comp_template_args
2567 (inner_args,
2568 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2569 (maintmpl)))))
2570 error ("partial specialization `%T' does not specialize any template arguments", type);
2572 /* [temp.class.spec]
2574 A partially specialized non-type argument expression shall not
2575 involve template parameters of the partial specialization except
2576 when the argument expression is a simple identifier.
2578 The type of a template parameter corresponding to a specialized
2579 non-type argument shall not be dependent on a parameter of the
2580 specialization. */
2581 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2582 tpd2.parms = 0;
2583 for (i = 0; i < nargs; ++i)
2585 tree arg = TREE_VEC_ELT (inner_args, i);
2586 if (/* These first two lines are the `non-type' bit. */
2587 !TYPE_P (arg)
2588 && TREE_CODE (arg) != TEMPLATE_DECL
2589 /* This next line is the `argument expression is not just a
2590 simple identifier' condition and also the `specialized
2591 non-type argument' bit. */
2592 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2594 if (tpd.arg_uses_template_parms[i])
2595 error ("template argument `%E' involves template parameter(s)", arg);
2596 else
2598 /* Look at the corresponding template parameter,
2599 marking which template parameters its type depends
2600 upon. */
2601 tree type =
2602 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2603 i)));
2605 if (!tpd2.parms)
2607 /* We haven't yet initialized TPD2. Do so now. */
2608 tpd2.arg_uses_template_parms
2609 = alloca (sizeof (int) * nargs);
2610 /* The number of parameters here is the number in the
2611 main template, which, as checked in the assertion
2612 above, is NARGS. */
2613 tpd2.parms = alloca (sizeof (int) * nargs);
2614 tpd2.level =
2615 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2618 /* Mark the template parameters. But this time, we're
2619 looking for the template parameters of the main
2620 template, not in the specialization. */
2621 tpd2.current_arg = i;
2622 tpd2.arg_uses_template_parms[i] = 0;
2623 memset (tpd2.parms, 0, sizeof (int) * nargs);
2624 for_each_template_parm (type,
2625 &mark_template_parm,
2626 &tpd2,
2627 NULL);
2629 if (tpd2.arg_uses_template_parms [i])
2631 /* The type depended on some template parameters.
2632 If they are fully specialized in the
2633 specialization, that's OK. */
2634 int j;
2635 for (j = 0; j < nargs; ++j)
2636 if (tpd2.parms[j] != 0
2637 && tpd.arg_uses_template_parms [j])
2639 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2640 type,
2641 arg);
2642 break;
2649 if (retrieve_specialization (maintmpl, specargs))
2650 /* We've already got this specialization. */
2651 return decl;
2653 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2654 = tree_cons (inner_args, inner_parms,
2655 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2656 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2657 return decl;
2660 /* Check that a template declaration's use of default arguments is not
2661 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2662 nonzero if DECL is the thing declared by a primary template.
2663 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2665 static void
2666 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2668 const char *msg;
2669 int last_level_to_check;
2670 tree parm_level;
2672 /* [temp.param]
2674 A default template-argument shall not be specified in a
2675 function template declaration or a function template definition, nor
2676 in the template-parameter-list of the definition of a member of a
2677 class template. */
2679 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2680 /* You can't have a function template declaration in a local
2681 scope, nor you can you define a member of a class template in a
2682 local scope. */
2683 return;
2685 if (current_class_type
2686 && !TYPE_BEING_DEFINED (current_class_type)
2687 && DECL_LANG_SPECIFIC (decl)
2688 /* If this is either a friend defined in the scope of the class
2689 or a member function. */
2690 && (DECL_FUNCTION_MEMBER_P (decl)
2691 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2692 : DECL_FRIEND_CONTEXT (decl)
2693 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2694 : false)
2695 /* And, if it was a member function, it really was defined in
2696 the scope of the class. */
2697 && (!DECL_FUNCTION_MEMBER_P (decl)
2698 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2699 /* We already checked these parameters when the template was
2700 declared, so there's no need to do it again now. This function
2701 was defined in class scope, but we're processing it's body now
2702 that the class is complete. */
2703 return;
2705 /* [temp.param]
2707 If a template-parameter has a default template-argument, all
2708 subsequent template-parameters shall have a default
2709 template-argument supplied. */
2710 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2712 tree inner_parms = TREE_VALUE (parm_level);
2713 int ntparms = TREE_VEC_LENGTH (inner_parms);
2714 int seen_def_arg_p = 0;
2715 int i;
2717 for (i = 0; i < ntparms; ++i)
2719 tree parm = TREE_VEC_ELT (inner_parms, i);
2720 if (TREE_PURPOSE (parm))
2721 seen_def_arg_p = 1;
2722 else if (seen_def_arg_p)
2724 error ("no default argument for `%D'", TREE_VALUE (parm));
2725 /* For better subsequent error-recovery, we indicate that
2726 there should have been a default argument. */
2727 TREE_PURPOSE (parm) = error_mark_node;
2732 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2733 /* For an ordinary class template, default template arguments are
2734 allowed at the innermost level, e.g.:
2735 template <class T = int>
2736 struct S {};
2737 but, in a partial specialization, they're not allowed even
2738 there, as we have in [temp.class.spec]:
2740 The template parameter list of a specialization shall not
2741 contain default template argument values.
2743 So, for a partial specialization, or for a function template,
2744 we look at all of them. */
2746 else
2747 /* But, for a primary class template that is not a partial
2748 specialization we look at all template parameters except the
2749 innermost ones. */
2750 parms = TREE_CHAIN (parms);
2752 /* Figure out what error message to issue. */
2753 if (TREE_CODE (decl) == FUNCTION_DECL)
2754 msg = "default template arguments may not be used in function templates";
2755 else if (is_partial)
2756 msg = "default template arguments may not be used in partial specializations";
2757 else
2758 msg = "default argument for template parameter for class enclosing `%D'";
2760 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2761 /* If we're inside a class definition, there's no need to
2762 examine the parameters to the class itself. On the one
2763 hand, they will be checked when the class is defined, and,
2764 on the other, default arguments are valid in things like:
2765 template <class T = double>
2766 struct S { template <class U> void f(U); };
2767 Here the default argument for `S' has no bearing on the
2768 declaration of `f'. */
2769 last_level_to_check = template_class_depth (current_class_type) + 1;
2770 else
2771 /* Check everything. */
2772 last_level_to_check = 0;
2774 for (parm_level = parms;
2775 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2776 parm_level = TREE_CHAIN (parm_level))
2778 tree inner_parms = TREE_VALUE (parm_level);
2779 int i;
2780 int ntparms;
2782 ntparms = TREE_VEC_LENGTH (inner_parms);
2783 for (i = 0; i < ntparms; ++i)
2784 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2786 if (msg)
2788 error (msg, decl);
2789 msg = 0;
2792 /* Clear out the default argument so that we are not
2793 confused later. */
2794 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2797 /* At this point, if we're still interested in issuing messages,
2798 they must apply to classes surrounding the object declared. */
2799 if (msg)
2800 msg = "default argument for template parameter for class enclosing `%D'";
2804 /* Worker for push_template_decl_real, called via
2805 for_each_template_parm. DATA is really an int, indicating the
2806 level of the parameters we are interested in. If T is a template
2807 parameter of that level, return nonzero. */
2809 static int
2810 template_parm_this_level_p (tree t, void* data)
2812 int this_level = *(int *)data;
2813 int level;
2815 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2816 level = TEMPLATE_PARM_LEVEL (t);
2817 else
2818 level = TEMPLATE_TYPE_LEVEL (t);
2819 return level == this_level;
2822 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2823 parameters given by current_template_args, or reuses a
2824 previously existing one, if appropriate. Returns the DECL, or an
2825 equivalent one, if it is replaced via a call to duplicate_decls.
2827 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2829 tree
2830 push_template_decl_real (tree decl, int is_friend)
2832 tree tmpl;
2833 tree args;
2834 tree info;
2835 tree ctx;
2836 int primary;
2837 int is_partial;
2838 int new_template_p = 0;
2840 if (decl == error_mark_node)
2841 return decl;
2843 /* See if this is a partial specialization. */
2844 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2845 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2846 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2848 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2850 if (is_friend)
2851 /* For a friend, we want the context of the friend function, not
2852 the type of which it is a friend. */
2853 ctx = DECL_CONTEXT (decl);
2854 else if (CP_DECL_CONTEXT (decl)
2855 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2856 /* In the case of a virtual function, we want the class in which
2857 it is defined. */
2858 ctx = CP_DECL_CONTEXT (decl);
2859 else
2860 /* Otherwise, if we're currently defining some class, the DECL
2861 is assumed to be a member of the class. */
2862 ctx = current_scope ();
2864 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2865 ctx = NULL_TREE;
2867 if (!DECL_CONTEXT (decl))
2868 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2870 /* See if this is a primary template. */
2871 primary = template_parm_scope_p ();
2873 if (primary)
2875 if (current_lang_name == lang_name_c)
2876 error ("template with C linkage");
2877 else if (TREE_CODE (decl) == TYPE_DECL
2878 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2879 error ("template class without a name");
2880 else if (TREE_CODE (decl) == FUNCTION_DECL)
2882 if (DECL_DESTRUCTOR_P (decl))
2884 /* [temp.mem]
2886 A destructor shall not be a member template. */
2887 error ("destructor `%D' declared as member template", decl);
2888 return error_mark_node;
2890 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
2891 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
2892 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
2893 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
2894 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
2895 == void_list_node)))
2897 /* [basic.stc.dynamic.allocation]
2899 An allocation function can be a function
2900 template. ... Template allocation functions shall
2901 have two or more parameters. */
2902 error ("invalid template declaration of `%D'", decl);
2903 return decl;
2906 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2907 && CLASS_TYPE_P (TREE_TYPE (decl)))
2908 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)))
2909 /* OK */;
2910 else
2912 error ("template declaration of `%#D'", decl);
2913 return error_mark_node;
2917 /* Check to see that the rules regarding the use of default
2918 arguments are not being violated. */
2919 check_default_tmpl_args (decl, current_template_parms,
2920 primary, is_partial);
2922 if (is_partial)
2923 return process_partial_specialization (decl);
2925 args = current_template_args ();
2927 if (!ctx
2928 || TREE_CODE (ctx) == FUNCTION_DECL
2929 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2930 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2932 if (DECL_LANG_SPECIFIC (decl)
2933 && DECL_TEMPLATE_INFO (decl)
2934 && DECL_TI_TEMPLATE (decl))
2935 tmpl = DECL_TI_TEMPLATE (decl);
2936 /* If DECL is a TYPE_DECL for a class-template, then there won't
2937 be DECL_LANG_SPECIFIC. The information equivalent to
2938 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2939 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2940 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2941 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2943 /* Since a template declaration already existed for this
2944 class-type, we must be redeclaring it here. Make sure
2945 that the redeclaration is valid. */
2946 redeclare_class_template (TREE_TYPE (decl),
2947 current_template_parms);
2948 /* We don't need to create a new TEMPLATE_DECL; just use the
2949 one we already had. */
2950 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2952 else
2954 tmpl = build_template_decl (decl, current_template_parms);
2955 new_template_p = 1;
2957 if (DECL_LANG_SPECIFIC (decl)
2958 && DECL_TEMPLATE_SPECIALIZATION (decl))
2960 /* A specialization of a member template of a template
2961 class. */
2962 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2963 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2964 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2968 else
2970 tree a, t, current, parms;
2971 int i;
2973 if (TREE_CODE (decl) == TYPE_DECL)
2975 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2976 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2977 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2978 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2979 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2980 else
2982 error ("`%D' does not declare a template type", decl);
2983 return decl;
2986 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2988 error ("template definition of non-template `%#D'", decl);
2989 return decl;
2991 else
2992 tmpl = DECL_TI_TEMPLATE (decl);
2994 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2995 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2996 && DECL_TEMPLATE_SPECIALIZATION (decl)
2997 && is_member_template (tmpl))
2999 tree new_tmpl;
3001 /* The declaration is a specialization of a member
3002 template, declared outside the class. Therefore, the
3003 innermost template arguments will be NULL, so we
3004 replace them with the arguments determined by the
3005 earlier call to check_explicit_specialization. */
3006 args = DECL_TI_ARGS (decl);
3008 new_tmpl
3009 = build_template_decl (decl, current_template_parms);
3010 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3011 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3012 DECL_TI_TEMPLATE (decl) = new_tmpl;
3013 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3014 DECL_TEMPLATE_INFO (new_tmpl)
3015 = tree_cons (tmpl, args, NULL_TREE);
3017 register_specialization (new_tmpl,
3018 most_general_template (tmpl),
3019 args);
3020 return decl;
3023 /* Make sure the template headers we got make sense. */
3025 parms = DECL_TEMPLATE_PARMS (tmpl);
3026 i = TMPL_PARMS_DEPTH (parms);
3027 if (TMPL_ARGS_DEPTH (args) != i)
3029 error ("expected %d levels of template parms for `%#D', got %d",
3030 i, decl, TMPL_ARGS_DEPTH (args));
3032 else
3033 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3035 a = TMPL_ARGS_LEVEL (args, i);
3036 t = INNERMOST_TEMPLATE_PARMS (parms);
3038 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3040 if (current == decl)
3041 error ("got %d template parameters for `%#D'",
3042 TREE_VEC_LENGTH (a), decl);
3043 else
3044 error ("got %d template parameters for `%#T'",
3045 TREE_VEC_LENGTH (a), current);
3046 error (" but %d required", TREE_VEC_LENGTH (t));
3049 /* Perhaps we should also check that the parms are used in the
3050 appropriate qualifying scopes in the declarator? */
3052 if (current == decl)
3053 current = ctx;
3054 else
3055 current = TYPE_CONTEXT (current);
3059 DECL_TEMPLATE_RESULT (tmpl) = decl;
3060 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3062 /* Push template declarations for global functions and types. Note
3063 that we do not try to push a global template friend declared in a
3064 template class; such a thing may well depend on the template
3065 parameters of the class. */
3066 if (new_template_p && !ctx
3067 && !(is_friend && template_class_depth (current_class_type) > 0))
3068 tmpl = pushdecl_namespace_level (tmpl);
3070 if (primary)
3072 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3073 if (DECL_CONV_FN_P (tmpl))
3075 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3077 /* It is a conversion operator. See if the type converted to
3078 depends on innermost template operands. */
3080 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3081 depth))
3082 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3086 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3087 back to its most general template. If TMPL is a specialization,
3088 ARGS may only have the innermost set of arguments. Add the missing
3089 argument levels if necessary. */
3090 if (DECL_TEMPLATE_INFO (tmpl))
3091 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3093 info = tree_cons (tmpl, args, NULL_TREE);
3095 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3097 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3098 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3099 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3100 /* Don't change the name if we've already set it up. */
3101 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3102 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3104 else if (DECL_LANG_SPECIFIC (decl))
3105 DECL_TEMPLATE_INFO (decl) = info;
3107 return DECL_TEMPLATE_RESULT (tmpl);
3110 tree
3111 push_template_decl (tree decl)
3113 return push_template_decl_real (decl, 0);
3116 /* Called when a class template TYPE is redeclared with the indicated
3117 template PARMS, e.g.:
3119 template <class T> struct S;
3120 template <class T> struct S {}; */
3122 void
3123 redeclare_class_template (tree type, tree parms)
3125 tree tmpl;
3126 tree tmpl_parms;
3127 int i;
3129 if (!TYPE_TEMPLATE_INFO (type))
3131 error ("`%T' is not a template type", type);
3132 return;
3135 tmpl = TYPE_TI_TEMPLATE (type);
3136 if (!PRIMARY_TEMPLATE_P (tmpl))
3137 /* The type is nested in some template class. Nothing to worry
3138 about here; there are no new template parameters for the nested
3139 type. */
3140 return;
3142 parms = INNERMOST_TEMPLATE_PARMS (parms);
3143 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3145 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3147 cp_error_at ("previous declaration `%D'", tmpl);
3148 error ("used %d template parameter%s instead of %d",
3149 TREE_VEC_LENGTH (tmpl_parms),
3150 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
3151 TREE_VEC_LENGTH (parms));
3152 return;
3155 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3157 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3158 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3159 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3160 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3162 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3163 TEMPLATE_DECL. */
3164 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3165 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3166 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3168 cp_error_at ("template parameter `%#D'", tmpl_parm);
3169 error ("redeclared here as `%#D'", parm);
3170 return;
3173 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3175 /* We have in [temp.param]:
3177 A template-parameter may not be given default arguments
3178 by two different declarations in the same scope. */
3179 error ("redefinition of default argument for `%#D'", parm);
3180 error ("%J original definition appeared here", tmpl_parm);
3181 return;
3184 if (parm_default != NULL_TREE)
3185 /* Update the previous template parameters (which are the ones
3186 that will really count) with the new default value. */
3187 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3188 else if (tmpl_default != NULL_TREE)
3189 /* Update the new parameters, too; they'll be used as the
3190 parameters for any members. */
3191 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3195 /* Simplify EXPR if it is a non-dependent expression. Returns the
3196 (possibly simplified) expression. */
3198 tree
3199 fold_non_dependent_expr (tree expr)
3201 /* If we're in a template, but EXPR isn't value dependent, simplify
3202 it. We're supposed to treat:
3204 template <typename T> void f(T[1 + 1]);
3205 template <typename T> void f(T[2]);
3207 as two declarations of the same function, for example. */
3208 if (processing_template_decl
3209 && !type_dependent_expression_p (expr)
3210 && !value_dependent_expression_p (expr))
3212 HOST_WIDE_INT saved_processing_template_decl;
3214 saved_processing_template_decl = processing_template_decl;
3215 processing_template_decl = 0;
3216 expr = tsubst_copy_and_build (expr,
3217 /*args=*/NULL_TREE,
3218 tf_error,
3219 /*in_decl=*/NULL_TREE,
3220 /*function_p=*/false);
3221 processing_template_decl = saved_processing_template_decl;
3223 return expr;
3226 /* Attempt to convert the non-type template parameter EXPR to the
3227 indicated TYPE. If the conversion is successful, return the
3228 converted value. If the conversion is unsuccessful, return
3229 NULL_TREE if we issued an error message, or error_mark_node if we
3230 did not. We issue error messages for out-and-out bad template
3231 parameters, but not simply because the conversion failed, since we
3232 might be just trying to do argument deduction. Both TYPE and EXPR
3233 must be non-dependent. */
3235 static tree
3236 convert_nontype_argument (tree type, tree expr)
3238 tree expr_type;
3240 /* If we are in a template, EXPR may be non-dependent, but still
3241 have a syntactic, rather than semantic, form. For example, EXPR
3242 might be a SCOPE_REF, rather than the VAR_DECL to which the
3243 SCOPE_REF refers. Preserving the qualifying scope is necessary
3244 so that access checking can be performed when the template is
3245 instantiated -- but here we need the resolved form so that we can
3246 convert the argument. */
3247 expr = fold_non_dependent_expr (expr);
3248 expr_type = TREE_TYPE (expr);
3250 /* A template-argument for a non-type, non-template
3251 template-parameter shall be one of:
3253 --an integral constant-expression of integral or enumeration
3254 type; or
3256 --the name of a non-type template-parameter; or
3258 --the name of an object or function with external linkage,
3259 including function templates and function template-ids but
3260 excluding non-static class members, expressed as id-expression;
3263 --the address of an object or function with external linkage,
3264 including function templates and function template-ids but
3265 excluding non-static class members, expressed as & id-expression
3266 where the & is optional if the name refers to a function or
3267 array; or
3269 --a pointer to member expressed as described in _expr.unary.op_. */
3271 /* An integral constant-expression can include const variables or
3272 . enumerators. Simplify things by folding them to their values,
3273 unless we're about to bind the declaration to a reference
3274 parameter. */
3275 if (INTEGRAL_TYPE_P (expr_type) && TREE_CODE (type) != REFERENCE_TYPE)
3276 while (true)
3278 tree const_expr = decl_constant_value (expr);
3279 /* In a template, the initializer for a VAR_DECL may not be
3280 marked as TREE_CONSTANT, in which case decl_constant_value
3281 will not return the initializer. Handle that special case
3282 here. */
3283 if (expr == const_expr
3284 && DECL_INTEGRAL_CONSTANT_VAR_P (expr)
3285 /* DECL_INITIAL can be NULL if we are processing a
3286 variable initialized to an expression involving itself.
3287 We know it is initialized to a constant -- but not what
3288 constant, yet. */
3289 && DECL_INITIAL (expr))
3290 const_expr = DECL_INITIAL (expr);
3291 if (expr == const_expr)
3292 break;
3293 expr = fold_non_dependent_expr (const_expr);
3296 if (is_overloaded_fn (expr))
3297 /* OK for now. We'll check that it has external linkage later.
3298 Check this first since if expr_type is the unknown_type_node
3299 we would otherwise complain below. */
3301 else if (TYPE_PTR_TO_MEMBER_P (expr_type))
3303 if (TREE_CODE (expr) != PTRMEM_CST)
3304 goto bad_argument;
3306 else if (TYPE_PTR_P (expr_type)
3307 || TREE_CODE (expr_type) == ARRAY_TYPE
3308 || TREE_CODE (type) == REFERENCE_TYPE
3309 /* If expr is the address of an overloaded function, we
3310 will get the unknown_type_node at this point. */
3311 || expr_type == unknown_type_node)
3313 tree referent;
3314 tree e = expr;
3315 STRIP_NOPS (e);
3317 if (TREE_CODE (expr_type) == ARRAY_TYPE
3318 || (TREE_CODE (type) == REFERENCE_TYPE
3319 && TREE_CODE (e) != ADDR_EXPR))
3320 referent = e;
3321 else
3323 if (TREE_CODE (e) != ADDR_EXPR)
3325 bad_argument:
3326 error ("`%E' is not a valid template argument", expr);
3327 if (TYPE_PTR_P (expr_type))
3329 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
3330 error ("it must be the address of a function with external linkage");
3331 else
3332 error ("it must be the address of an object with external linkage");
3334 else if (TYPE_PTR_TO_MEMBER_P (expr_type))
3335 error ("it must be a pointer-to-member of the form `&X::Y'");
3337 return NULL_TREE;
3340 referent = TREE_OPERAND (e, 0);
3341 STRIP_NOPS (referent);
3344 if (TREE_CODE (referent) == STRING_CST)
3346 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
3347 referent);
3348 return NULL_TREE;
3351 if (TREE_CODE (referent) == SCOPE_REF)
3352 referent = TREE_OPERAND (referent, 1);
3354 if (is_overloaded_fn (referent))
3355 /* We'll check that it has external linkage later. */
3357 else if (TREE_CODE (referent) != VAR_DECL)
3358 goto bad_argument;
3359 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
3361 error ("address of non-extern `%E' cannot be used as template argument", referent);
3362 return error_mark_node;
3365 else if (INTEGRAL_TYPE_P (expr_type) || TYPE_PTR_TO_MEMBER_P (expr_type))
3367 if (! TREE_CONSTANT (expr))
3369 non_constant:
3370 error ("non-constant `%E' cannot be used as template argument",
3371 expr);
3372 return NULL_TREE;
3375 else
3377 if (TYPE_P (expr))
3378 error ("type '%T' cannot be used as a value for a non-type "
3379 "template-parameter", expr);
3380 else if (DECL_P (expr))
3381 error ("invalid use of '%D' as a non-type template-argument", expr);
3382 else
3383 error ("invalid use of '%E' as a non-type template-argument", expr);
3385 return NULL_TREE;
3388 switch (TREE_CODE (type))
3390 case INTEGER_TYPE:
3391 case BOOLEAN_TYPE:
3392 case ENUMERAL_TYPE:
3393 /* For a non-type template-parameter of integral or enumeration
3394 type, integral promotions (_conv.prom_) and integral
3395 conversions (_conv.integral_) are applied. */
3396 if (!INTEGRAL_TYPE_P (expr_type))
3397 return error_mark_node;
3399 /* [conv.integral] does not allow conversions between two different
3400 enumeration types. */
3401 if (TREE_CODE (type) == ENUMERAL_TYPE
3402 && TREE_CODE (expr_type) == ENUMERAL_TYPE
3403 && !same_type_ignoring_top_level_qualifiers_p (type, expr_type))
3404 return error_mark_node;
3406 /* It's safe to call digest_init in this case; we know we're
3407 just converting one integral constant expression to another. */
3408 expr = digest_init (type, expr, (tree*) 0);
3410 if (TREE_CODE (expr) != INTEGER_CST)
3411 /* Curiously, some TREE_CONSTANT integral expressions do not
3412 simplify to integer constants. For example, `3 % 0',
3413 remains a TRUNC_MOD_EXPR. */
3414 goto non_constant;
3416 return expr;
3418 case OFFSET_TYPE:
3420 tree e;
3422 /* For a non-type template-parameter of type pointer to data
3423 member, qualification conversions (_conv.qual_) are
3424 applied. */
3425 e = perform_qualification_conversions (type, expr);
3426 if (TREE_CODE (e) == NOP_EXPR)
3427 /* The call to perform_qualification_conversions will
3428 insert a NOP_EXPR over EXPR to do express conversion,
3429 if necessary. But, that will confuse us if we use
3430 this (converted) template parameter to instantiate
3431 another template; then the thing will not look like a
3432 valid template argument. So, just make a new
3433 constant, of the appropriate type. */
3434 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3435 return e;
3438 case POINTER_TYPE:
3440 tree type_pointed_to = TREE_TYPE (type);
3442 if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3444 /* For a non-type template-parameter of type pointer to
3445 function, only the function-to-pointer conversion
3446 (_conv.func_) is applied. If the template-argument
3447 represents a set of overloaded functions (or a pointer to
3448 such), the matching function is selected from the set
3449 (_over.over_). */
3450 tree fns;
3451 tree fn;
3453 if (TREE_CODE (expr) == ADDR_EXPR)
3454 fns = TREE_OPERAND (expr, 0);
3455 else
3456 fns = expr;
3458 fn = instantiate_type (type_pointed_to, fns, tf_none);
3460 if (fn == error_mark_node)
3461 return error_mark_node;
3463 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3465 if (really_overloaded_fn (fns))
3466 return error_mark_node;
3467 else
3468 goto bad_argument;
3471 expr = build_unary_op (ADDR_EXPR, fn, 0);
3473 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3474 return expr;
3476 else
3478 /* For a non-type template-parameter of type pointer to
3479 object, qualification conversions (_conv.qual_) and the
3480 array-to-pointer conversion (_conv.array_) are applied.
3481 [Note: In particular, neither the null pointer conversion
3482 (_conv.ptr_) nor the derived-to-base conversion
3483 (_conv.ptr_) are applied. Although 0 is a valid
3484 template-argument for a non-type template-parameter of
3485 integral type, it is not a valid template-argument for a
3486 non-type template-parameter of pointer type.]
3488 The call to decay_conversion performs the
3489 array-to-pointer conversion, if appropriate. */
3490 expr = decay_conversion (expr);
3492 if (expr == error_mark_node)
3493 return error_mark_node;
3494 else
3495 return perform_qualification_conversions (type, expr);
3498 break;
3500 case REFERENCE_TYPE:
3502 tree type_referred_to = TREE_TYPE (type);
3504 /* If this expression already has reference type, get the
3505 underlying object. */
3506 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3508 if (TREE_CODE (expr) == NOP_EXPR
3509 && TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
3510 STRIP_NOPS (expr);
3511 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3512 expr = TREE_OPERAND (expr, 0);
3513 expr_type = TREE_TYPE (expr);
3516 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3518 /* For a non-type template-parameter of type reference to
3519 function, no conversions apply. If the
3520 template-argument represents a set of overloaded
3521 functions, the matching function is selected from the
3522 set (_over.over_). */
3523 tree fn;
3525 fn = instantiate_type (type_referred_to, expr, tf_none);
3527 if (fn == error_mark_node)
3528 return error_mark_node;
3530 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3532 if (really_overloaded_fn (expr))
3533 /* Don't issue an error here; we might get a different
3534 function if the overloading had worked out
3535 differently. */
3536 return error_mark_node;
3537 else
3538 goto bad_argument;
3541 gcc_assert (same_type_p (type_referred_to, TREE_TYPE (fn)));
3542 expr = fn;
3544 else
3546 /* For a non-type template-parameter of type reference to
3547 object, no conversions apply. The type referred to by the
3548 reference may be more cv-qualified than the (otherwise
3549 identical) type of the template-argument. The
3550 template-parameter is bound directly to the
3551 template-argument, which must be an lvalue. */
3552 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3553 TYPE_MAIN_VARIANT (type_referred_to))
3554 || !at_least_as_qualified_p (type_referred_to,
3555 expr_type)
3556 || !real_lvalue_p (expr))
3557 return error_mark_node;
3560 cxx_mark_addressable (expr);
3561 return build_nop (type, build_address (expr));
3563 break;
3565 case RECORD_TYPE:
3567 gcc_assert (TYPE_PTRMEMFUNC_P (type));
3569 /* For a non-type template-parameter of type pointer to member
3570 function, no conversions apply. If the template-argument
3571 represents a set of overloaded member functions, the
3572 matching member function is selected from the set
3573 (_over.over_). */
3575 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3576 expr_type != unknown_type_node)
3577 return error_mark_node;
3579 if (TREE_CODE (expr) == PTRMEM_CST)
3581 /* A ptr-to-member constant. */
3582 if (!same_type_p (type, expr_type))
3583 return error_mark_node;
3584 else
3585 return expr;
3588 if (TREE_CODE (expr) != ADDR_EXPR)
3589 return error_mark_node;
3591 expr = instantiate_type (type, expr, tf_none);
3593 if (expr == error_mark_node)
3594 return error_mark_node;
3596 if (!same_type_p (type, TREE_TYPE (expr)))
3597 return error_mark_node;
3599 return expr;
3601 break;
3603 default:
3604 /* All non-type parameters must have one of these types. */
3605 gcc_unreachable ();
3608 return error_mark_node;
3611 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3612 template template parameters. Both PARM_PARMS and ARG_PARMS are
3613 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3614 or PARM_DECL.
3616 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3617 the case, then extra parameters must have default arguments.
3619 Consider the example:
3620 template <class T, class Allocator = allocator> class vector;
3621 template<template <class U> class TT> class C;
3623 C<vector> is a valid instantiation. PARM_PARMS for the above code
3624 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3625 T and Allocator) and OUTER_ARGS contains the argument that is used to
3626 substitute the TT parameter. */
3628 static int
3629 coerce_template_template_parms (tree parm_parms,
3630 tree arg_parms,
3631 tsubst_flags_t complain,
3632 tree in_decl,
3633 tree outer_args)
3635 int nparms, nargs, i;
3636 tree parm, arg;
3638 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3639 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3641 nparms = TREE_VEC_LENGTH (parm_parms);
3642 nargs = TREE_VEC_LENGTH (arg_parms);
3644 /* The rule here is opposite of coerce_template_parms. */
3645 if (nargs < nparms
3646 || (nargs > nparms
3647 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3648 return 0;
3650 for (i = 0; i < nparms; ++i)
3652 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3653 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3655 if (arg == NULL_TREE || arg == error_mark_node
3656 || parm == NULL_TREE || parm == error_mark_node)
3657 return 0;
3659 if (TREE_CODE (arg) != TREE_CODE (parm))
3660 return 0;
3662 switch (TREE_CODE (parm))
3664 case TYPE_DECL:
3665 break;
3667 case TEMPLATE_DECL:
3668 /* We encounter instantiations of templates like
3669 template <template <template <class> class> class TT>
3670 class C; */
3672 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3673 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3675 if (!coerce_template_template_parms
3676 (parmparm, argparm, complain, in_decl, outer_args))
3677 return 0;
3679 break;
3681 case PARM_DECL:
3682 /* The tsubst call is used to handle cases such as
3684 template <int> class C {};
3685 template <class T, template <T> class TT> class D {};
3686 D<int, C> d;
3688 i.e. the parameter list of TT depends on earlier parameters. */
3689 if (!dependent_type_p (TREE_TYPE (arg))
3690 && !same_type_p
3691 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3692 TREE_TYPE (arg)))
3693 return 0;
3694 break;
3696 default:
3697 gcc_unreachable ();
3700 return 1;
3703 /* Convert the indicated template ARG as necessary to match the
3704 indicated template PARM. Returns the converted ARG, or
3705 error_mark_node if the conversion was unsuccessful. Error and
3706 warning messages are issued under control of COMPLAIN. This
3707 conversion is for the Ith parameter in the parameter list. ARGS is
3708 the full set of template arguments deduced so far. */
3710 static tree
3711 convert_template_argument (tree parm,
3712 tree arg,
3713 tree args,
3714 tsubst_flags_t complain,
3715 int i,
3716 tree in_decl)
3718 tree val;
3719 tree inner_args;
3720 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3722 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3724 if (TREE_CODE (arg) == TREE_LIST
3725 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3727 /* The template argument was the name of some
3728 member function. That's usually
3729 invalid, but static members are OK. In any
3730 case, grab the underlying fields/functions
3731 and issue an error later if required. */
3732 arg = TREE_VALUE (arg);
3733 TREE_TYPE (arg) = unknown_type_node;
3736 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3737 requires_type = (TREE_CODE (parm) == TYPE_DECL
3738 || requires_tmpl_type);
3740 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3741 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3742 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3743 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3745 if (is_tmpl_type
3746 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3747 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3748 arg = TYPE_STUB_DECL (arg);
3750 is_type = TYPE_P (arg) || is_tmpl_type;
3752 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3753 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3755 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3757 arg = make_typename_type (TREE_OPERAND (arg, 0),
3758 TREE_OPERAND (arg, 1),
3759 complain & tf_error);
3760 is_type = 1;
3762 if (is_type != requires_type)
3764 if (in_decl)
3766 if (complain & tf_error)
3768 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3769 i + 1, in_decl);
3770 if (is_type)
3771 error (" expected a constant of type `%T', got `%T'",
3772 TREE_TYPE (parm),
3773 (is_tmpl_type ? DECL_NAME (arg) : arg));
3774 else if (requires_tmpl_type)
3775 error (" expected a class template, got `%E'", arg);
3776 else
3777 error (" expected a type, got `%E'", arg);
3780 return error_mark_node;
3782 if (is_tmpl_type ^ requires_tmpl_type)
3784 if (in_decl && (complain & tf_error))
3786 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3787 i + 1, in_decl);
3788 if (is_tmpl_type)
3789 error (" expected a type, got `%T'", DECL_NAME (arg));
3790 else
3791 error (" expected a class template, got `%T'", arg);
3793 return error_mark_node;
3796 if (is_type)
3798 if (requires_tmpl_type)
3800 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3801 /* The number of argument required is not known yet.
3802 Just accept it for now. */
3803 val = TREE_TYPE (arg);
3804 else
3806 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3807 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3809 if (coerce_template_template_parms (parmparm, argparm,
3810 complain, in_decl,
3811 inner_args))
3813 val = arg;
3815 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3816 TEMPLATE_DECL. */
3817 if (val != error_mark_node
3818 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3819 val = TREE_TYPE (val);
3821 else
3823 if (in_decl && (complain & tf_error))
3825 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3826 i + 1, in_decl);
3827 error (" expected a template of type `%D', got `%D'", parm, arg);
3830 val = error_mark_node;
3834 else
3835 val = arg;
3837 else
3839 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3841 if (invalid_nontype_parm_type_p (t, complain))
3842 return error_mark_node;
3844 if (!uses_template_parms (arg) && !uses_template_parms (t))
3845 /* We used to call digest_init here. However, digest_init
3846 will report errors, which we don't want when complain
3847 is zero. More importantly, digest_init will try too
3848 hard to convert things: for example, `0' should not be
3849 converted to pointer type at this point according to
3850 the standard. Accepting this is not merely an
3851 extension, since deciding whether or not these
3852 conversions can occur is part of determining which
3853 function template to call, or whether a given explicit
3854 argument specification is valid. */
3855 val = convert_nontype_argument (t, arg);
3856 else
3857 val = arg;
3859 if (val == NULL_TREE)
3860 val = error_mark_node;
3861 else if (val == error_mark_node && (complain & tf_error))
3862 error ("could not convert template argument `%E' to `%T'",
3863 arg, t);
3866 return val;
3869 /* Convert all template arguments to their appropriate types, and
3870 return a vector containing the innermost resulting template
3871 arguments. If any error occurs, return error_mark_node. Error and
3872 warning messages are issued under control of COMPLAIN.
3874 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3875 provided in ARGLIST, or else trailing parameters must have default
3876 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3877 deduction for any unspecified trailing arguments. */
3879 static tree
3880 coerce_template_parms (tree parms,
3881 tree args,
3882 tree in_decl,
3883 tsubst_flags_t complain,
3884 int require_all_arguments)
3886 int nparms, nargs, i, lost = 0;
3887 tree inner_args;
3888 tree new_args;
3889 tree new_inner_args;
3891 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3892 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
3893 nparms = TREE_VEC_LENGTH (parms);
3895 if (nargs > nparms
3896 || (nargs < nparms
3897 && require_all_arguments
3898 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3900 if (complain & tf_error)
3902 error ("wrong number of template arguments (%d, should be %d)",
3903 nargs, nparms);
3905 if (in_decl)
3906 cp_error_at ("provided for `%D'", in_decl);
3909 return error_mark_node;
3912 new_inner_args = make_tree_vec (nparms);
3913 new_args = add_outermost_template_args (args, new_inner_args);
3914 for (i = 0; i < nparms; i++)
3916 tree arg;
3917 tree parm;
3919 /* Get the Ith template parameter. */
3920 parm = TREE_VEC_ELT (parms, i);
3922 /* Calculate the Ith argument. */
3923 if (i < nargs)
3924 arg = TREE_VEC_ELT (inner_args, i);
3925 else if (require_all_arguments)
3926 /* There must be a default arg in this case. */
3927 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
3928 complain, in_decl);
3929 else
3930 break;
3932 gcc_assert (arg);
3933 if (arg == error_mark_node)
3934 error ("template argument %d is invalid", i + 1);
3935 else
3936 arg = convert_template_argument (TREE_VALUE (parm),
3937 arg, new_args, complain, i,
3938 in_decl);
3940 if (arg == error_mark_node)
3941 lost++;
3942 TREE_VEC_ELT (new_inner_args, i) = arg;
3945 if (lost)
3946 return error_mark_node;
3948 return new_inner_args;
3951 /* Returns 1 if template args OT and NT are equivalent. */
3953 static int
3954 template_args_equal (tree ot, tree nt)
3956 if (nt == ot)
3957 return 1;
3959 if (TREE_CODE (nt) == TREE_VEC)
3960 /* For member templates */
3961 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3962 else if (TYPE_P (nt))
3963 return TYPE_P (ot) && same_type_p (ot, nt);
3964 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3965 return 0;
3966 else
3967 return cp_tree_equal (ot, nt);
3970 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3971 of template arguments. Returns 0 otherwise. */
3974 comp_template_args (tree oldargs, tree newargs)
3976 int i;
3978 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3979 return 0;
3981 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3983 tree nt = TREE_VEC_ELT (newargs, i);
3984 tree ot = TREE_VEC_ELT (oldargs, i);
3986 if (! template_args_equal (ot, nt))
3987 return 0;
3989 return 1;
3992 /* Given class template name and parameter list, produce a user-friendly name
3993 for the instantiation. */
3995 static char *
3996 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
3998 static struct obstack scratch_obstack;
3999 static char *scratch_firstobj;
4000 int i, nparms;
4002 if (!scratch_firstobj)
4003 gcc_obstack_init (&scratch_obstack);
4004 else
4005 obstack_free (&scratch_obstack, scratch_firstobj);
4006 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
4008 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4009 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4011 cat (name);
4012 ccat ('<');
4013 nparms = TREE_VEC_LENGTH (parms);
4014 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4015 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4016 for (i = 0; i < nparms; i++)
4018 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4019 tree arg = TREE_VEC_ELT (arglist, i);
4021 if (i)
4022 ccat (',');
4024 if (TREE_CODE (parm) == TYPE_DECL)
4026 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4027 continue;
4029 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4031 if (TREE_CODE (arg) == TEMPLATE_DECL)
4033 /* Already substituted with real template. Just output
4034 the template name here */
4035 tree context = DECL_CONTEXT (arg);
4036 if (context)
4038 /* The template may be defined in a namespace, or
4039 may be a member template. */
4040 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4041 || CLASS_TYPE_P (context));
4042 cat (decl_as_string (DECL_CONTEXT (arg),
4043 TFF_PLAIN_IDENTIFIER));
4044 cat ("::");
4046 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4048 else
4049 /* Output the parameter declaration. */
4050 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4051 continue;
4053 else
4054 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4056 /* No need to check arglist against parmlist here; we did that
4057 in coerce_template_parms, called from lookup_template_class. */
4058 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4061 char *bufp = obstack_next_free (&scratch_obstack);
4062 int offset = 0;
4063 while (bufp[offset - 1] == ' ')
4064 offset--;
4065 obstack_blank_fast (&scratch_obstack, offset);
4067 /* B<C<char> >, not B<C<char>> */
4068 if (bufp[offset - 1] == '>')
4069 ccat (' ');
4071 ccat ('>');
4072 ccat ('\0');
4073 return (char *) obstack_base (&scratch_obstack);
4076 static tree
4077 classtype_mangled_name (tree t)
4079 if (CLASSTYPE_TEMPLATE_INFO (t)
4080 /* Specializations have already had their names set up in
4081 lookup_template_class. */
4082 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4084 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4086 /* For non-primary templates, the template parameters are
4087 implicit from their surrounding context. */
4088 if (PRIMARY_TEMPLATE_P (tmpl))
4090 tree name = DECL_NAME (tmpl);
4091 char *mangled_name = mangle_class_name_for_template
4092 (IDENTIFIER_POINTER (name),
4093 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4094 CLASSTYPE_TI_ARGS (t));
4095 tree id = get_identifier (mangled_name);
4096 IDENTIFIER_TEMPLATE (id) = name;
4097 return id;
4101 return TYPE_IDENTIFIER (t);
4104 static void
4105 add_pending_template (tree d)
4107 tree ti = (TYPE_P (d)
4108 ? CLASSTYPE_TEMPLATE_INFO (d)
4109 : DECL_TEMPLATE_INFO (d));
4110 tree pt;
4111 int level;
4113 if (TI_PENDING_TEMPLATE_FLAG (ti))
4114 return;
4116 /* We are called both from instantiate_decl, where we've already had a
4117 tinst_level pushed, and instantiate_template, where we haven't.
4118 Compensate. */
4119 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4121 if (level)
4122 push_tinst_level (d);
4124 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4125 if (last_pending_template)
4126 TREE_CHAIN (last_pending_template) = pt;
4127 else
4128 pending_templates = pt;
4130 last_pending_template = pt;
4132 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4134 if (level)
4135 pop_tinst_level ();
4139 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4140 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4141 documentation for TEMPLATE_ID_EXPR. */
4143 tree
4144 lookup_template_function (tree fns, tree arglist)
4146 tree type;
4148 if (fns == error_mark_node || arglist == error_mark_node)
4149 return error_mark_node;
4151 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4152 if (fns == NULL_TREE
4153 || TREE_CODE (fns) == FUNCTION_DECL)
4155 error ("non-template used as template");
4156 return error_mark_node;
4159 gcc_assert (TREE_CODE (fns) == TEMPLATE_DECL
4160 || TREE_CODE (fns) == OVERLOAD
4161 || BASELINK_P (fns)
4162 || TREE_CODE (fns) == IDENTIFIER_NODE);
4164 if (BASELINK_P (fns))
4166 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4167 unknown_type_node,
4168 BASELINK_FUNCTIONS (fns),
4169 arglist);
4170 return fns;
4173 type = TREE_TYPE (fns);
4174 if (TREE_CODE (fns) == OVERLOAD || !type)
4175 type = unknown_type_node;
4177 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4180 /* Within the scope of a template class S<T>, the name S gets bound
4181 (in build_self_reference) to a TYPE_DECL for the class, not a
4182 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4183 or one of its enclosing classes, and that type is a template,
4184 return the associated TEMPLATE_DECL. Otherwise, the original
4185 DECL is returned. */
4187 tree
4188 maybe_get_template_decl_from_type_decl (tree decl)
4190 return (decl != NULL_TREE
4191 && TREE_CODE (decl) == TYPE_DECL
4192 && DECL_ARTIFICIAL (decl)
4193 && CLASS_TYPE_P (TREE_TYPE (decl))
4194 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4195 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4198 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4199 parameters, find the desired type.
4201 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4203 IN_DECL, if non-NULL, is the template declaration we are trying to
4204 instantiate.
4206 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4207 the class we are looking up.
4209 Issue error and warning messages under control of COMPLAIN.
4211 If the template class is really a local class in a template
4212 function, then the FUNCTION_CONTEXT is the function in which it is
4213 being instantiated. */
4215 tree
4216 lookup_template_class (tree d1,
4217 tree arglist,
4218 tree in_decl,
4219 tree context,
4220 int entering_scope,
4221 tsubst_flags_t complain)
4223 tree template = NULL_TREE, parmlist;
4224 tree t;
4226 timevar_push (TV_NAME_LOOKUP);
4228 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4230 tree value = innermost_non_namespace_value (d1);
4231 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4232 template = value;
4233 else
4235 if (context)
4236 push_decl_namespace (context);
4237 template = lookup_name (d1, /*prefer_type=*/0);
4238 template = maybe_get_template_decl_from_type_decl (template);
4239 if (context)
4240 pop_decl_namespace ();
4242 if (template)
4243 context = DECL_CONTEXT (template);
4245 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4247 tree type = TREE_TYPE (d1);
4249 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4250 an implicit typename for the second A. Deal with it. */
4251 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4252 type = TREE_TYPE (type);
4254 if (CLASSTYPE_TEMPLATE_INFO (type))
4256 template = CLASSTYPE_TI_TEMPLATE (type);
4257 d1 = DECL_NAME (template);
4260 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4261 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4263 template = TYPE_TI_TEMPLATE (d1);
4264 d1 = DECL_NAME (template);
4266 else if (TREE_CODE (d1) == TEMPLATE_DECL
4267 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4269 template = d1;
4270 d1 = DECL_NAME (template);
4271 context = DECL_CONTEXT (template);
4274 /* Issue an error message if we didn't find a template. */
4275 if (! template)
4277 if (complain & tf_error)
4278 error ("`%T' is not a template", d1);
4279 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4282 if (TREE_CODE (template) != TEMPLATE_DECL
4283 /* Make sure it's a user visible template, if it was named by
4284 the user. */
4285 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4286 && !PRIMARY_TEMPLATE_P (template)))
4288 if (complain & tf_error)
4290 error ("non-template type `%T' used as a template", d1);
4291 if (in_decl)
4292 cp_error_at ("for template declaration `%D'", in_decl);
4294 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4297 complain &= ~tf_user;
4299 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4301 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4302 template arguments */
4304 tree parm;
4305 tree arglist2;
4307 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4309 /* Consider an example where a template template parameter declared as
4311 template <class T, class U = std::allocator<T> > class TT
4313 The template parameter level of T and U are one level larger than
4314 of TT. To proper process the default argument of U, say when an
4315 instantiation `TT<int>' is seen, we need to build the full
4316 arguments containing {int} as the innermost level. Outer levels,
4317 available when not appearing as default template argument, can be
4318 obtained from `current_template_args ()'.
4320 Suppose that TT is later substituted with std::vector. The above
4321 instantiation is `TT<int, std::allocator<T> >' with TT at
4322 level 1, and T at level 2, while the template arguments at level 1
4323 becomes {std::vector} and the inner level 2 is {int}. */
4325 if (current_template_parms)
4326 arglist = add_to_template_args (current_template_args (), arglist);
4328 arglist2 = coerce_template_parms (parmlist, arglist, template,
4329 complain, /*require_all_args=*/1);
4330 if (arglist2 == error_mark_node
4331 || (!uses_template_parms (arglist2)
4332 && check_instantiated_args (template, arglist2, complain)))
4333 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4335 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4336 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4338 else
4340 tree template_type = TREE_TYPE (template);
4341 tree gen_tmpl;
4342 tree type_decl;
4343 tree found = NULL_TREE;
4344 tree *tp;
4345 int arg_depth;
4346 int parm_depth;
4347 int is_partial_instantiation;
4349 gen_tmpl = most_general_template (template);
4350 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4351 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4352 arg_depth = TMPL_ARGS_DEPTH (arglist);
4354 if (arg_depth == 1 && parm_depth > 1)
4356 /* We've been given an incomplete set of template arguments.
4357 For example, given:
4359 template <class T> struct S1 {
4360 template <class U> struct S2 {};
4361 template <class U> struct S2<U*> {};
4364 we will be called with an ARGLIST of `U*', but the
4365 TEMPLATE will be `template <class T> template
4366 <class U> struct S1<T>::S2'. We must fill in the missing
4367 arguments. */
4368 arglist
4369 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4370 arglist);
4371 arg_depth = TMPL_ARGS_DEPTH (arglist);
4374 /* Now we should have enough arguments. */
4375 gcc_assert (parm_depth == arg_depth);
4377 /* From here on, we're only interested in the most general
4378 template. */
4379 template = gen_tmpl;
4381 /* Calculate the BOUND_ARGS. These will be the args that are
4382 actually tsubst'd into the definition to create the
4383 instantiation. */
4384 if (parm_depth > 1)
4386 /* We have multiple levels of arguments to coerce, at once. */
4387 int i;
4388 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4390 tree bound_args = make_tree_vec (parm_depth);
4392 for (i = saved_depth,
4393 t = DECL_TEMPLATE_PARMS (template);
4394 i > 0 && t != NULL_TREE;
4395 --i, t = TREE_CHAIN (t))
4397 tree a = coerce_template_parms (TREE_VALUE (t),
4398 arglist, template,
4399 complain, /*require_all_args=*/1);
4401 /* Don't process further if one of the levels fails. */
4402 if (a == error_mark_node)
4404 /* Restore the ARGLIST to its full size. */
4405 TREE_VEC_LENGTH (arglist) = saved_depth;
4406 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4409 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4411 /* We temporarily reduce the length of the ARGLIST so
4412 that coerce_template_parms will see only the arguments
4413 corresponding to the template parameters it is
4414 examining. */
4415 TREE_VEC_LENGTH (arglist)--;
4418 /* Restore the ARGLIST to its full size. */
4419 TREE_VEC_LENGTH (arglist) = saved_depth;
4421 arglist = bound_args;
4423 else
4424 arglist
4425 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4426 INNERMOST_TEMPLATE_ARGS (arglist),
4427 template,
4428 complain, /*require_all_args=*/1);
4430 if (arglist == error_mark_node)
4431 /* We were unable to bind the arguments. */
4432 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4434 /* In the scope of a template class, explicit references to the
4435 template class refer to the type of the template, not any
4436 instantiation of it. For example, in:
4438 template <class T> class C { void f(C<T>); }
4440 the `C<T>' is just the same as `C'. Outside of the
4441 class, however, such a reference is an instantiation. */
4442 if (comp_template_args (TYPE_TI_ARGS (template_type),
4443 arglist))
4445 found = template_type;
4447 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4449 tree ctx;
4451 for (ctx = current_class_type;
4452 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4453 ctx = (TYPE_P (ctx)
4454 ? TYPE_CONTEXT (ctx)
4455 : DECL_CONTEXT (ctx)))
4456 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4457 goto found_ctx;
4459 /* We're not in the scope of the class, so the
4460 TEMPLATE_TYPE is not the type we want after all. */
4461 found = NULL_TREE;
4462 found_ctx:;
4465 if (found)
4466 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4468 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4469 *tp;
4470 tp = &TREE_CHAIN (*tp))
4471 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4473 found = *tp;
4475 /* Use the move-to-front heuristic to speed up future
4476 searches. */
4477 *tp = TREE_CHAIN (*tp);
4478 TREE_CHAIN (found)
4479 = DECL_TEMPLATE_INSTANTIATIONS (template);
4480 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4482 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
4485 /* This type is a "partial instantiation" if any of the template
4486 arguments still involve template parameters. Note that we set
4487 IS_PARTIAL_INSTANTIATION for partial specializations as
4488 well. */
4489 is_partial_instantiation = uses_template_parms (arglist);
4491 /* If the deduced arguments are invalid, then the binding
4492 failed. */
4493 if (!is_partial_instantiation
4494 && check_instantiated_args (template,
4495 INNERMOST_TEMPLATE_ARGS (arglist),
4496 complain))
4497 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4499 if (!is_partial_instantiation
4500 && !PRIMARY_TEMPLATE_P (template)
4501 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4503 found = xref_tag_from_type (TREE_TYPE (template),
4504 DECL_NAME (template),
4505 /*globalize=*/1);
4506 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4509 context = tsubst (DECL_CONTEXT (template), arglist,
4510 complain, in_decl);
4511 if (!context)
4512 context = global_namespace;
4514 /* Create the type. */
4515 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4517 if (!is_partial_instantiation)
4519 set_current_access_from_decl (TYPE_NAME (template_type));
4520 t = start_enum (TYPE_IDENTIFIER (template_type));
4522 else
4523 /* We don't want to call start_enum for this type, since
4524 the values for the enumeration constants may involve
4525 template parameters. And, no one should be interested
4526 in the enumeration constants for such a type. */
4527 t = make_node (ENUMERAL_TYPE);
4529 else
4531 t = make_aggr_type (TREE_CODE (template_type));
4532 CLASSTYPE_DECLARED_CLASS (t)
4533 = CLASSTYPE_DECLARED_CLASS (template_type);
4534 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4535 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4537 /* A local class. Make sure the decl gets registered properly. */
4538 if (context == current_function_decl)
4539 pushtag (DECL_NAME (template), t, 0);
4542 /* If we called start_enum or pushtag above, this information
4543 will already be set up. */
4544 if (!TYPE_NAME (t))
4546 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4548 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4549 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4550 TYPE_STUB_DECL (t) = type_decl;
4551 DECL_SOURCE_LOCATION (type_decl)
4552 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4554 else
4555 type_decl = TYPE_NAME (t);
4557 TREE_PRIVATE (type_decl)
4558 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4559 TREE_PROTECTED (type_decl)
4560 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4562 /* Set up the template information. We have to figure out which
4563 template is the immediate parent if this is a full
4564 instantiation. */
4565 if (parm_depth == 1 || is_partial_instantiation
4566 || !PRIMARY_TEMPLATE_P (template))
4567 /* This case is easy; there are no member templates involved. */
4568 found = template;
4569 else
4571 /* This is a full instantiation of a member template. Look
4572 for a partial instantiation of which this is an instance. */
4574 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4575 found; found = TREE_CHAIN (found))
4577 int success;
4578 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4580 /* We only want partial instantiations, here, not
4581 specializations or full instantiations. */
4582 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4583 || !uses_template_parms (TREE_VALUE (found)))
4584 continue;
4586 /* Temporarily reduce by one the number of levels in the
4587 ARGLIST and in FOUND so as to avoid comparing the
4588 last set of arguments. */
4589 TREE_VEC_LENGTH (arglist)--;
4590 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4592 /* See if the arguments match. If they do, then TMPL is
4593 the partial instantiation we want. */
4594 success = comp_template_args (TREE_PURPOSE (found), arglist);
4596 /* Restore the argument vectors to their full size. */
4597 TREE_VEC_LENGTH (arglist)++;
4598 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4600 if (success)
4602 found = tmpl;
4603 break;
4607 if (!found)
4609 /* There was no partial instantiation. This happens
4610 where C<T> is a member template of A<T> and it's used
4611 in something like
4613 template <typename T> struct B { A<T>::C<int> m; };
4614 B<float>;
4616 Create the partial instantiation.
4618 TREE_VEC_LENGTH (arglist)--;
4619 found = tsubst (template, arglist, complain, NULL_TREE);
4620 TREE_VEC_LENGTH (arglist)++;
4624 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4625 DECL_TEMPLATE_INSTANTIATIONS (template)
4626 = tree_cons (arglist, t,
4627 DECL_TEMPLATE_INSTANTIATIONS (template));
4629 if (TREE_CODE (t) == ENUMERAL_TYPE
4630 && !is_partial_instantiation)
4631 /* Now that the type has been registered on the instantiations
4632 list, we set up the enumerators. Because the enumeration
4633 constants may involve the enumeration type itself, we make
4634 sure to register the type first, and then create the
4635 constants. That way, doing tsubst_expr for the enumeration
4636 constants won't result in recursive calls here; we'll find
4637 the instantiation and exit above. */
4638 tsubst_enum (template_type, t, arglist);
4640 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4641 is set up. */
4642 if (TREE_CODE (t) != ENUMERAL_TYPE)
4643 DECL_NAME (type_decl) = classtype_mangled_name (t);
4644 if (is_partial_instantiation)
4645 /* If the type makes use of template parameters, the
4646 code that generates debugging information will crash. */
4647 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4651 timevar_pop (TV_NAME_LOOKUP);
4654 struct pair_fn_data
4656 tree_fn_t fn;
4657 void *data;
4658 htab_t visited;
4661 /* Called from for_each_template_parm via walk_tree. */
4663 static tree
4664 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4666 tree t = *tp;
4667 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4668 tree_fn_t fn = pfd->fn;
4669 void *data = pfd->data;
4671 if (TYPE_P (t)
4672 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4673 return error_mark_node;
4675 switch (TREE_CODE (t))
4677 case RECORD_TYPE:
4678 if (TYPE_PTRMEMFUNC_P (t))
4679 break;
4680 /* Fall through. */
4682 case UNION_TYPE:
4683 case ENUMERAL_TYPE:
4684 if (!TYPE_TEMPLATE_INFO (t))
4685 *walk_subtrees = 0;
4686 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4687 fn, data, pfd->visited))
4688 return error_mark_node;
4689 break;
4691 case METHOD_TYPE:
4692 /* Since we're not going to walk subtrees, we have to do this
4693 explicitly here. */
4694 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4695 pfd->visited))
4696 return error_mark_node;
4697 /* Fall through. */
4699 case FUNCTION_TYPE:
4700 /* Check the return type. */
4701 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4702 return error_mark_node;
4704 /* Check the parameter types. Since default arguments are not
4705 instantiated until they are needed, the TYPE_ARG_TYPES may
4706 contain expressions that involve template parameters. But,
4707 no-one should be looking at them yet. And, once they're
4708 instantiated, they don't contain template parameters, so
4709 there's no point in looking at them then, either. */
4711 tree parm;
4713 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4714 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4715 pfd->visited))
4716 return error_mark_node;
4718 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4719 want walk_tree walking into them itself. */
4720 *walk_subtrees = 0;
4722 break;
4724 case TYPEOF_TYPE:
4725 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4726 pfd->visited))
4727 return error_mark_node;
4728 break;
4730 case FUNCTION_DECL:
4731 case VAR_DECL:
4732 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4733 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4734 pfd->visited))
4735 return error_mark_node;
4736 /* Fall through. */
4738 case PARM_DECL:
4739 case CONST_DECL:
4740 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4741 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4742 pfd->visited))
4743 return error_mark_node;
4744 if (DECL_CONTEXT (t)
4745 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4746 pfd->visited))
4747 return error_mark_node;
4748 break;
4750 case BOUND_TEMPLATE_TEMPLATE_PARM:
4751 /* Record template parameters such as `T' inside `TT<T>'. */
4752 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4753 return error_mark_node;
4754 /* Fall through. */
4756 case TEMPLATE_TEMPLATE_PARM:
4757 case TEMPLATE_TYPE_PARM:
4758 case TEMPLATE_PARM_INDEX:
4759 if (fn && (*fn)(t, data))
4760 return error_mark_node;
4761 else if (!fn)
4762 return error_mark_node;
4763 break;
4765 case TEMPLATE_DECL:
4766 /* A template template parameter is encountered. */
4767 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4768 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4769 return error_mark_node;
4771 /* Already substituted template template parameter */
4772 *walk_subtrees = 0;
4773 break;
4775 case TYPENAME_TYPE:
4776 if (!fn
4777 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4778 data, pfd->visited))
4779 return error_mark_node;
4780 break;
4782 case CONSTRUCTOR:
4783 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4784 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4785 (TREE_TYPE (t)), fn, data,
4786 pfd->visited))
4787 return error_mark_node;
4788 break;
4790 case INDIRECT_REF:
4791 case COMPONENT_REF:
4792 /* If there's no type, then this thing must be some expression
4793 involving template parameters. */
4794 if (!fn && !TREE_TYPE (t))
4795 return error_mark_node;
4796 break;
4798 case MODOP_EXPR:
4799 case CAST_EXPR:
4800 case REINTERPRET_CAST_EXPR:
4801 case CONST_CAST_EXPR:
4802 case STATIC_CAST_EXPR:
4803 case DYNAMIC_CAST_EXPR:
4804 case ARROW_EXPR:
4805 case DOTSTAR_EXPR:
4806 case TYPEID_EXPR:
4807 case PSEUDO_DTOR_EXPR:
4808 if (!fn)
4809 return error_mark_node;
4810 break;
4812 case BASELINK:
4813 /* If we do not handle this case specially, we end up walking
4814 the BINFO hierarchy, which is circular, and therefore
4815 confuses walk_tree. */
4816 *walk_subtrees = 0;
4817 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4818 pfd->visited))
4819 return error_mark_node;
4820 break;
4822 default:
4823 break;
4826 /* We didn't find any template parameters we liked. */
4827 return NULL_TREE;
4830 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4831 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4832 call FN with the parameter and the DATA.
4833 If FN returns nonzero, the iteration is terminated, and
4834 for_each_template_parm returns 1. Otherwise, the iteration
4835 continues. If FN never returns a nonzero value, the value
4836 returned by for_each_template_parm is 0. If FN is NULL, it is
4837 considered to be the function which always returns 1. */
4839 static int
4840 for_each_template_parm (tree t, tree_fn_t fn, void* data, htab_t visited)
4842 struct pair_fn_data pfd;
4843 int result;
4845 /* Set up. */
4846 pfd.fn = fn;
4847 pfd.data = data;
4849 /* Walk the tree. (Conceptually, we would like to walk without
4850 duplicates, but for_each_template_parm_r recursively calls
4851 for_each_template_parm, so we would need to reorganize a fair
4852 bit to use walk_tree_without_duplicates, so we keep our own
4853 visited list.) */
4854 if (visited)
4855 pfd.visited = visited;
4856 else
4857 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer,
4858 NULL);
4859 result = walk_tree (&t,
4860 for_each_template_parm_r,
4861 &pfd,
4862 pfd.visited) != NULL_TREE;
4864 /* Clean up. */
4865 if (!visited)
4866 htab_delete (pfd.visited);
4868 return result;
4871 /* Returns true if T depends on any template parameter. */
4874 uses_template_parms (tree t)
4876 bool dependent_p;
4877 int saved_processing_template_decl;
4879 saved_processing_template_decl = processing_template_decl;
4880 if (!saved_processing_template_decl)
4881 processing_template_decl = 1;
4882 if (TYPE_P (t))
4883 dependent_p = dependent_type_p (t);
4884 else if (TREE_CODE (t) == TREE_VEC)
4885 dependent_p = any_dependent_template_arguments_p (t);
4886 else if (TREE_CODE (t) == TREE_LIST)
4887 dependent_p = (uses_template_parms (TREE_VALUE (t))
4888 || uses_template_parms (TREE_CHAIN (t)));
4889 else if (DECL_P (t)
4890 || EXPR_P (t)
4891 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
4892 || TREE_CODE (t) == OVERLOAD
4893 || TREE_CODE (t) == BASELINK
4894 || CONSTANT_CLASS_P (t))
4895 dependent_p = (type_dependent_expression_p (t)
4896 || value_dependent_expression_p (t));
4897 else
4899 gcc_assert (t == error_mark_node);
4900 dependent_p = false;
4903 processing_template_decl = saved_processing_template_decl;
4905 return dependent_p;
4908 /* Returns true if T depends on any template parameter with level LEVEL. */
4911 uses_template_parms_level (tree t, int level)
4913 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
4916 static int tinst_depth;
4917 extern int max_tinst_depth;
4918 #ifdef GATHER_STATISTICS
4919 int depth_reached;
4920 #endif
4921 static int tinst_level_tick;
4922 static int last_template_error_tick;
4924 /* We're starting to instantiate D; record the template instantiation context
4925 for diagnostics and to restore it later. */
4928 push_tinst_level (tree d)
4930 tree new;
4932 if (tinst_depth >= max_tinst_depth)
4934 /* If the instantiation in question still has unbound template parms,
4935 we don't really care if we can't instantiate it, so just return.
4936 This happens with base instantiation for implicit `typename'. */
4937 if (uses_template_parms (d))
4938 return 0;
4940 last_template_error_tick = tinst_level_tick;
4941 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4942 max_tinst_depth, d);
4944 print_instantiation_context ();
4946 return 0;
4949 new = make_tinst_level (d, input_location);
4950 TREE_CHAIN (new) = current_tinst_level;
4951 current_tinst_level = new;
4953 ++tinst_depth;
4954 #ifdef GATHER_STATISTICS
4955 if (tinst_depth > depth_reached)
4956 depth_reached = tinst_depth;
4957 #endif
4959 ++tinst_level_tick;
4960 return 1;
4963 /* We're done instantiating this template; return to the instantiation
4964 context. */
4966 void
4967 pop_tinst_level (void)
4969 tree old = current_tinst_level;
4971 /* Restore the filename and line number stashed away when we started
4972 this instantiation. */
4973 input_location = TINST_LOCATION (old);
4974 current_tinst_level = TREE_CHAIN (old);
4975 --tinst_depth;
4976 ++tinst_level_tick;
4979 /* We're instantiating a deferred template; restore the template
4980 instantiation context in which the instantiation was requested, which
4981 is one step out from LEVEL. */
4983 static void
4984 reopen_tinst_level (tree level)
4986 tree t;
4988 tinst_depth = 0;
4989 for (t = level; t; t = TREE_CHAIN (t))
4990 ++tinst_depth;
4992 current_tinst_level = level;
4993 pop_tinst_level ();
4996 /* Return the outermost template instantiation context, for use with
4997 -falt-external-templates. */
4999 tree
5000 tinst_for_decl (void)
5002 tree p = current_tinst_level;
5004 if (p)
5005 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
5007 return p;
5010 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5011 vector of template arguments, as for tsubst.
5013 Returns an appropriate tsubst'd friend declaration. */
5015 static tree
5016 tsubst_friend_function (tree decl, tree args)
5018 tree new_friend;
5019 location_t saved_loc = input_location;
5021 input_location = DECL_SOURCE_LOCATION (decl);
5023 if (TREE_CODE (decl) == FUNCTION_DECL
5024 && DECL_TEMPLATE_INSTANTIATION (decl)
5025 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5026 /* This was a friend declared with an explicit template
5027 argument list, e.g.:
5029 friend void f<>(T);
5031 to indicate that f was a template instantiation, not a new
5032 function declaration. Now, we have to figure out what
5033 instantiation of what template. */
5035 tree template_id, arglist, fns;
5036 tree new_args;
5037 tree tmpl;
5038 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5040 /* Friend functions are looked up in the containing namespace scope.
5041 We must enter that scope, to avoid finding member functions of the
5042 current cless with same name. */
5043 push_nested_namespace (ns);
5044 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5045 tf_error | tf_warning, NULL_TREE);
5046 pop_nested_namespace (ns);
5047 arglist = tsubst (DECL_TI_ARGS (decl), args,
5048 tf_error | tf_warning, NULL_TREE);
5049 template_id = lookup_template_function (fns, arglist);
5051 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5052 tmpl = determine_specialization (template_id, new_friend,
5053 &new_args,
5054 /*need_member_template=*/0,
5055 TREE_VEC_LENGTH (args));
5056 new_friend = instantiate_template (tmpl, new_args, tf_error);
5057 goto done;
5060 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5062 /* The NEW_FRIEND will look like an instantiation, to the
5063 compiler, but is not an instantiation from the point of view of
5064 the language. For example, we might have had:
5066 template <class T> struct S {
5067 template <class U> friend void f(T, U);
5070 Then, in S<int>, template <class U> void f(int, U) is not an
5071 instantiation of anything. */
5072 if (new_friend == error_mark_node)
5073 return error_mark_node;
5075 DECL_USE_TEMPLATE (new_friend) = 0;
5076 if (TREE_CODE (decl) == TEMPLATE_DECL)
5078 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5079 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5080 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5083 /* The mangled name for the NEW_FRIEND is incorrect. The function
5084 is not a template instantiation and should not be mangled like
5085 one. Therefore, we forget the mangling here; we'll recompute it
5086 later if we need it. */
5087 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5089 SET_DECL_RTL (new_friend, NULL_RTX);
5090 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5093 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5095 tree old_decl;
5096 tree new_friend_template_info;
5097 tree new_friend_result_template_info;
5098 tree ns;
5099 int new_friend_is_defn;
5101 /* We must save some information from NEW_FRIEND before calling
5102 duplicate decls since that function will free NEW_FRIEND if
5103 possible. */
5104 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5105 new_friend_is_defn =
5106 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5107 (template_for_substitution (new_friend)))
5108 != NULL_TREE);
5109 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5111 /* This declaration is a `primary' template. */
5112 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5114 new_friend_result_template_info
5115 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5117 else
5118 new_friend_result_template_info = NULL_TREE;
5120 /* Inside pushdecl_namespace_level, we will push into the
5121 current namespace. However, the friend function should go
5122 into the namespace of the template. */
5123 ns = decl_namespace_context (new_friend);
5124 push_nested_namespace (ns);
5125 old_decl = pushdecl_namespace_level (new_friend);
5126 pop_nested_namespace (ns);
5128 if (old_decl != new_friend)
5130 /* This new friend declaration matched an existing
5131 declaration. For example, given:
5133 template <class T> void f(T);
5134 template <class U> class C {
5135 template <class T> friend void f(T) {}
5138 the friend declaration actually provides the definition
5139 of `f', once C has been instantiated for some type. So,
5140 old_decl will be the out-of-class template declaration,
5141 while new_friend is the in-class definition.
5143 But, if `f' was called before this point, the
5144 instantiation of `f' will have DECL_TI_ARGS corresponding
5145 to `T' but not to `U', references to which might appear
5146 in the definition of `f'. Previously, the most general
5147 template for an instantiation of `f' was the out-of-class
5148 version; now it is the in-class version. Therefore, we
5149 run through all specialization of `f', adding to their
5150 DECL_TI_ARGS appropriately. In particular, they need a
5151 new set of outer arguments, corresponding to the
5152 arguments for this class instantiation.
5154 The same situation can arise with something like this:
5156 friend void f(int);
5157 template <class T> class C {
5158 friend void f(T) {}
5161 when `C<int>' is instantiated. Now, `f(int)' is defined
5162 in the class. */
5164 if (!new_friend_is_defn)
5165 /* On the other hand, if the in-class declaration does
5166 *not* provide a definition, then we don't want to alter
5167 existing definitions. We can just leave everything
5168 alone. */
5170 else
5172 /* Overwrite whatever template info was there before, if
5173 any, with the new template information pertaining to
5174 the declaration. */
5175 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5177 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5178 reregister_specialization (new_friend,
5179 most_general_template (old_decl),
5180 old_decl);
5181 else
5183 tree t;
5184 tree new_friend_args;
5186 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5187 = new_friend_result_template_info;
5189 new_friend_args = TI_ARGS (new_friend_template_info);
5190 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5191 t != NULL_TREE;
5192 t = TREE_CHAIN (t))
5194 tree spec = TREE_VALUE (t);
5196 DECL_TI_ARGS (spec)
5197 = add_outermost_template_args (new_friend_args,
5198 DECL_TI_ARGS (spec));
5201 /* Now, since specializations are always supposed to
5202 hang off of the most general template, we must move
5203 them. */
5204 t = most_general_template (old_decl);
5205 if (t != old_decl)
5207 DECL_TEMPLATE_SPECIALIZATIONS (t)
5208 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5209 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5210 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5215 /* The information from NEW_FRIEND has been merged into OLD_DECL
5216 by duplicate_decls. */
5217 new_friend = old_decl;
5220 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
5222 /* Check to see that the declaration is really present, and,
5223 possibly obtain an improved declaration. */
5224 tree fn = check_classfn (DECL_CONTEXT (new_friend),
5225 new_friend, NULL_TREE);
5227 if (fn)
5228 new_friend = fn;
5231 done:
5232 input_location = saved_loc;
5233 return new_friend;
5236 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5237 template arguments, as for tsubst.
5239 Returns an appropriate tsubst'd friend type or error_mark_node on
5240 failure. */
5242 static tree
5243 tsubst_friend_class (tree friend_tmpl, tree args)
5245 tree friend_type;
5246 tree tmpl;
5247 tree context;
5249 context = DECL_CONTEXT (friend_tmpl);
5251 if (context)
5253 if (TREE_CODE (context) == NAMESPACE_DECL)
5254 push_nested_namespace (context);
5255 else
5256 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5259 /* First, we look for a class template. */
5260 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5262 /* But, if we don't find one, it might be because we're in a
5263 situation like this:
5265 template <class T>
5266 struct S {
5267 template <class U>
5268 friend struct S;
5271 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5272 for `S<int>', not the TEMPLATE_DECL. */
5273 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5275 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5276 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5279 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5281 /* The friend template has already been declared. Just
5282 check to see that the declarations match, and install any new
5283 default parameters. We must tsubst the default parameters,
5284 of course. We only need the innermost template parameters
5285 because that is all that redeclare_class_template will look
5286 at. */
5287 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5288 > TMPL_ARGS_DEPTH (args))
5290 tree parms;
5291 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5292 args, tf_error | tf_warning);
5293 redeclare_class_template (TREE_TYPE (tmpl), parms);
5296 friend_type = TREE_TYPE (tmpl);
5298 else
5300 /* The friend template has not already been declared. In this
5301 case, the instantiation of the template class will cause the
5302 injection of this template into the global scope. */
5303 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5305 /* The new TMPL is not an instantiation of anything, so we
5306 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5307 the new type because that is supposed to be the corresponding
5308 template decl, i.e., TMPL. */
5309 DECL_USE_TEMPLATE (tmpl) = 0;
5310 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5311 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5312 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5313 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5315 /* Inject this template into the global scope. */
5316 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5319 if (context)
5321 if (TREE_CODE (context) == NAMESPACE_DECL)
5322 pop_nested_namespace (context);
5323 else
5324 pop_nested_class ();
5327 return friend_type;
5330 /* Returns zero if TYPE cannot be completed later due to circularity.
5331 Otherwise returns one. */
5333 static int
5334 can_complete_type_without_circularity (tree type)
5336 if (type == NULL_TREE || type == error_mark_node)
5337 return 0;
5338 else if (COMPLETE_TYPE_P (type))
5339 return 1;
5340 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5341 return can_complete_type_without_circularity (TREE_TYPE (type));
5342 else if (CLASS_TYPE_P (type)
5343 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5344 return 0;
5345 else
5346 return 1;
5349 tree
5350 instantiate_class_template (tree type)
5352 tree template, args, pattern, t, member;
5353 tree typedecl;
5354 tree pbinfo;
5355 tree base_list;
5357 if (type == error_mark_node)
5358 return error_mark_node;
5360 if (TYPE_BEING_DEFINED (type)
5361 || COMPLETE_TYPE_P (type)
5362 || dependent_type_p (type))
5363 return type;
5365 /* Figure out which template is being instantiated. */
5366 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5367 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5369 /* Figure out which arguments are being used to do the
5370 instantiation. */
5371 args = CLASSTYPE_TI_ARGS (type);
5373 /* Determine what specialization of the original template to
5374 instantiate. */
5375 t = most_specialized_class (template, args);
5376 if (t == error_mark_node)
5378 const char *str = "candidates are:";
5379 error ("ambiguous class template instantiation for `%#T'", type);
5380 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5381 t = TREE_CHAIN (t))
5383 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
5385 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5386 str = " ";
5389 TYPE_BEING_DEFINED (type) = 1;
5390 return error_mark_node;
5393 if (t)
5394 pattern = TREE_TYPE (t);
5395 else
5396 pattern = TREE_TYPE (template);
5398 /* If the template we're instantiating is incomplete, then clearly
5399 there's nothing we can do. */
5400 if (!COMPLETE_TYPE_P (pattern))
5401 return type;
5403 /* If we've recursively instantiated too many templates, stop. */
5404 if (! push_tinst_level (type))
5405 return type;
5407 /* Now we're really doing the instantiation. Mark the type as in
5408 the process of being defined. */
5409 TYPE_BEING_DEFINED (type) = 1;
5411 /* We may be in the middle of deferred access check. Disable
5412 it now. */
5413 push_deferring_access_checks (dk_no_deferred);
5415 push_to_top_level ();
5417 if (t)
5419 /* This TYPE is actually an instantiation of a partial
5420 specialization. We replace the innermost set of ARGS with
5421 the arguments appropriate for substitution. For example,
5422 given:
5424 template <class T> struct S {};
5425 template <class T> struct S<T*> {};
5427 and supposing that we are instantiating S<int*>, ARGS will
5428 present be {int*} but we need {int}. */
5429 tree inner_args
5430 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5431 args);
5433 /* If there were multiple levels in ARGS, replacing the
5434 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5435 want, so we make a copy first. */
5436 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5438 args = copy_node (args);
5439 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5441 else
5442 args = inner_args;
5445 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5447 /* Set the input location to the template definition. This is needed
5448 if tsubsting causes an error. */
5449 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (pattern));
5451 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5452 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5453 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5454 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5455 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5456 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5457 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5458 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5459 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5460 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5461 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5462 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5463 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5464 TYPE_USES_MULTIPLE_INHERITANCE (type)
5465 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5466 TYPE_USES_VIRTUAL_BASECLASSES (type)
5467 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5468 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5469 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5470 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5471 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5472 if (ANON_AGGR_TYPE_P (pattern))
5473 SET_ANON_AGGR_TYPE_P (type);
5475 pbinfo = TYPE_BINFO (pattern);
5477 /* We should never instantiate a nested class before its enclosing
5478 class; we need to look up the nested class by name before we can
5479 instantiate it, and that lookup should instantiate the enclosing
5480 class. */
5481 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5482 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5483 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5485 base_list = NULL_TREE;
5486 if (BINFO_N_BASE_BINFOS (pbinfo))
5488 tree pbase_binfo;
5489 tree context = TYPE_CONTEXT (type);
5490 bool pop_p;
5491 int i;
5493 /* We must enter the scope containing the type, as that is where
5494 the accessibility of types named in dependent bases are
5495 looked up from. */
5496 pop_p = push_scope (context ? context : global_namespace);
5498 /* Substitute into each of the bases to determine the actual
5499 basetypes. */
5500 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5502 tree base;
5503 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5505 /* Substitute to figure out the base class. */
5506 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5507 if (base == error_mark_node)
5508 continue;
5510 base_list = tree_cons (access, base, base_list);
5511 if (BINFO_VIRTUAL_P (pbase_binfo))
5512 TREE_TYPE (base_list) = integer_type_node;
5515 /* The list is now in reverse order; correct that. */
5516 base_list = nreverse (base_list);
5518 if (pop_p)
5519 pop_scope (context ? context : global_namespace);
5521 /* Now call xref_basetypes to set up all the base-class
5522 information. */
5523 xref_basetypes (type, base_list);
5526 /* Now that our base classes are set up, enter the scope of the
5527 class, so that name lookups into base classes, etc. will work
5528 correctly. This is precisely analogous to what we do in
5529 begin_class_definition when defining an ordinary non-template
5530 class. */
5531 pushclass (type);
5533 /* Now members are processed in the order of declaration. */
5534 for (member = CLASSTYPE_DECL_LIST (pattern);
5535 member; member = TREE_CHAIN (member))
5537 tree t = TREE_VALUE (member);
5539 if (TREE_PURPOSE (member))
5541 if (TYPE_P (t))
5543 /* Build new CLASSTYPE_NESTED_UTDS. */
5545 tree tag = t;
5546 tree name = TYPE_IDENTIFIER (tag);
5547 tree newtag;
5549 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5550 if (newtag == error_mark_node)
5551 continue;
5553 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5555 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5556 /* Unfortunately, lookup_template_class sets
5557 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5558 instantiation (i.e., for the type of a member
5559 template class nested within a template class.)
5560 This behavior is required for
5561 maybe_process_partial_specialization to work
5562 correctly, but is not accurate in this case;
5563 the TAG is not an instantiation of anything.
5564 (The corresponding TEMPLATE_DECL is an
5565 instantiation, but the TYPE is not.) */
5566 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5568 /* Now, we call pushtag to put this NEWTAG into the scope of
5569 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5570 pushtag calling push_template_decl. We don't have to do
5571 this for enums because it will already have been done in
5572 tsubst_enum. */
5573 if (name)
5574 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5575 pushtag (name, newtag, /*globalize=*/0);
5578 else if (TREE_CODE (t) == FUNCTION_DECL
5579 || DECL_FUNCTION_TEMPLATE_P (t))
5581 /* Build new TYPE_METHODS. */
5582 tree r;
5584 if (TREE_CODE (t) == TEMPLATE_DECL)
5585 ++processing_template_decl;
5586 r = tsubst (t, args, tf_error, NULL_TREE);
5587 if (TREE_CODE (t) == TEMPLATE_DECL)
5588 --processing_template_decl;
5589 set_current_access_from_decl (r);
5590 grok_special_member_properties (r);
5591 finish_member_declaration (r);
5593 else
5595 /* Build new TYPE_FIELDS. */
5597 if (TREE_CODE (t) != CONST_DECL)
5599 tree r;
5601 /* The the file and line for this declaration, to
5602 assist in error message reporting. Since we
5603 called push_tinst_level above, we don't need to
5604 restore these. */
5605 input_location = DECL_SOURCE_LOCATION (t);
5607 if (TREE_CODE (t) == TEMPLATE_DECL)
5608 ++processing_template_decl;
5609 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5610 if (TREE_CODE (t) == TEMPLATE_DECL)
5611 --processing_template_decl;
5612 if (TREE_CODE (r) == VAR_DECL)
5614 tree init;
5616 if (DECL_INITIALIZED_IN_CLASS_P (r))
5617 init = tsubst_expr (DECL_INITIAL (t), args,
5618 tf_error | tf_warning, NULL_TREE);
5619 else
5620 init = NULL_TREE;
5622 finish_static_data_member_decl
5623 (r, init, /*asmspec_tree=*/NULL_TREE, /*flags=*/0);
5625 if (DECL_INITIALIZED_IN_CLASS_P (r))
5626 check_static_variable_definition (r, TREE_TYPE (r));
5628 else if (TREE_CODE (r) == FIELD_DECL)
5630 /* Determine whether R has a valid type and can be
5631 completed later. If R is invalid, then it is
5632 replaced by error_mark_node so that it will not be
5633 added to TYPE_FIELDS. */
5634 tree rtype = TREE_TYPE (r);
5635 if (can_complete_type_without_circularity (rtype))
5636 complete_type (rtype);
5638 if (!COMPLETE_TYPE_P (rtype))
5640 cxx_incomplete_type_error (r, rtype);
5641 r = error_mark_node;
5645 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5646 such a thing will already have been added to the field
5647 list by tsubst_enum in finish_member_declaration in the
5648 CLASSTYPE_NESTED_UTDS case above. */
5649 if (!(TREE_CODE (r) == TYPE_DECL
5650 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5651 && DECL_ARTIFICIAL (r)))
5653 set_current_access_from_decl (r);
5654 finish_member_declaration (r);
5659 else
5661 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5663 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5665 tree friend_type = t;
5666 tree new_friend_type;
5668 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5669 new_friend_type = tsubst_friend_class (friend_type, args);
5670 else if (uses_template_parms (friend_type))
5671 new_friend_type = tsubst (friend_type, args,
5672 tf_error | tf_warning, NULL_TREE);
5673 else if (CLASSTYPE_USE_TEMPLATE (friend_type))
5674 new_friend_type = friend_type;
5675 else
5677 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5679 /* The call to xref_tag_from_type does injection for friend
5680 classes. */
5681 push_nested_namespace (ns);
5682 new_friend_type =
5683 xref_tag_from_type (friend_type, NULL_TREE, 1);
5684 pop_nested_namespace (ns);
5687 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5688 /* Trick make_friend_class into realizing that the friend
5689 we're adding is a template, not an ordinary class. It's
5690 important that we use make_friend_class since it will
5691 perform some error-checking and output cross-reference
5692 information. */
5693 ++processing_template_decl;
5695 if (new_friend_type != error_mark_node)
5696 make_friend_class (type, new_friend_type,
5697 /*complain=*/false);
5699 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5700 --processing_template_decl;
5702 else
5704 /* Build new DECL_FRIENDLIST. */
5705 tree r;
5707 if (TREE_CODE (t) == TEMPLATE_DECL)
5709 ++processing_template_decl;
5710 push_deferring_access_checks (dk_no_check);
5713 r = tsubst_friend_function (t, args);
5714 add_friend (type, r, /*complain=*/false);
5715 if (TREE_CODE (t) == TEMPLATE_DECL)
5717 pop_deferring_access_checks ();
5718 --processing_template_decl;
5724 /* Set the file and line number information to whatever is given for
5725 the class itself. This puts error messages involving generated
5726 implicit functions at a predictable point, and the same point
5727 that would be used for non-template classes. */
5728 typedecl = TYPE_MAIN_DECL (type);
5729 input_location = DECL_SOURCE_LOCATION (typedecl);
5731 unreverse_member_declarations (type);
5732 finish_struct_1 (type);
5733 TYPE_BEING_DEFINED (type) = 0;
5735 /* Now that the class is complete, instantiate default arguments for
5736 any member functions. We don't do this earlier because the
5737 default arguments may reference members of the class. */
5738 if (!PRIMARY_TEMPLATE_P (template))
5739 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5740 if (TREE_CODE (t) == FUNCTION_DECL
5741 /* Implicitly generated member functions will not have template
5742 information; they are not instantiations, but instead are
5743 created "fresh" for each instantiation. */
5744 && DECL_TEMPLATE_INFO (t))
5745 tsubst_default_arguments (t);
5747 popclass ();
5748 pop_from_top_level ();
5749 pop_deferring_access_checks ();
5750 pop_tinst_level ();
5752 /* The vtable for a template class can be emitted in any translation
5753 unit in which the class is instantiated. When there is no key
5754 method, however, finish_struct_1 will already have added TYPE to
5755 the keyed_classes list. */
5756 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5757 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5759 return type;
5762 static tree
5763 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5765 tree r;
5767 if (!t)
5768 r = t;
5769 else if (TYPE_P (t))
5770 r = tsubst (t, args, complain, in_decl);
5771 else
5773 r = tsubst_expr (t, args, complain, in_decl);
5775 if (!uses_template_parms (r))
5777 /* Sometimes, one of the args was an expression involving a
5778 template constant parameter, like N - 1. Now that we've
5779 tsubst'd, we might have something like 2 - 1. This will
5780 confuse lookup_template_class, so we do constant folding
5781 here. We have to unset processing_template_decl, to fool
5782 tsubst_copy_and_build() into building an actual tree. */
5784 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5785 as simple as it's going to get, and trying to reprocess
5786 the trees will break. Once tsubst_expr et al DTRT for
5787 non-dependent exprs, this code can go away, as the type
5788 will always be set. */
5789 if (!TREE_TYPE (r))
5791 int saved_processing_template_decl = processing_template_decl;
5792 processing_template_decl = 0;
5793 r = tsubst_copy_and_build (r, /*args=*/NULL_TREE,
5794 tf_error, /*in_decl=*/NULL_TREE,
5795 /*function_p=*/false);
5796 processing_template_decl = saved_processing_template_decl;
5798 r = fold (r);
5801 return r;
5804 /* Substitute ARGS into the vector or list of template arguments T. */
5806 static tree
5807 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5809 int len = TREE_VEC_LENGTH (t);
5810 int need_new = 0, i;
5811 tree *elts = alloca (len * sizeof (tree));
5813 for (i = 0; i < len; i++)
5815 tree orig_arg = TREE_VEC_ELT (t, i);
5816 tree new_arg;
5818 if (TREE_CODE (orig_arg) == TREE_VEC)
5819 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5820 else
5821 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5823 if (new_arg == error_mark_node)
5824 return error_mark_node;
5826 elts[i] = new_arg;
5827 if (new_arg != orig_arg)
5828 need_new = 1;
5831 if (!need_new)
5832 return t;
5834 t = make_tree_vec (len);
5835 for (i = 0; i < len; i++)
5836 TREE_VEC_ELT (t, i) = elts[i];
5838 return t;
5841 /* Return the result of substituting ARGS into the template parameters
5842 given by PARMS. If there are m levels of ARGS and m + n levels of
5843 PARMS, then the result will contain n levels of PARMS. For
5844 example, if PARMS is `template <class T> template <class U>
5845 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5846 result will be `template <int*, double, class V>'. */
5848 static tree
5849 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
5851 tree r = NULL_TREE;
5852 tree* new_parms;
5854 for (new_parms = &r;
5855 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5856 new_parms = &(TREE_CHAIN (*new_parms)),
5857 parms = TREE_CHAIN (parms))
5859 tree new_vec =
5860 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5861 int i;
5863 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5865 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5866 tree default_value = TREE_PURPOSE (tuple);
5867 tree parm_decl = TREE_VALUE (tuple);
5869 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5870 default_value = tsubst_template_arg (default_value, args,
5871 complain, NULL_TREE);
5873 tuple = build_tree_list (default_value, parm_decl);
5874 TREE_VEC_ELT (new_vec, i) = tuple;
5877 *new_parms =
5878 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5879 - TMPL_ARGS_DEPTH (args)),
5880 new_vec, NULL_TREE);
5883 return r;
5886 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5887 type T. If T is not an aggregate or enumeration type, it is
5888 handled as if by tsubst. IN_DECL is as for tsubst. If
5889 ENTERING_SCOPE is nonzero, T is the context for a template which
5890 we are presently tsubst'ing. Return the substituted value. */
5892 static tree
5893 tsubst_aggr_type (tree t,
5894 tree args,
5895 tsubst_flags_t complain,
5896 tree in_decl,
5897 int entering_scope)
5899 if (t == NULL_TREE)
5900 return NULL_TREE;
5902 switch (TREE_CODE (t))
5904 case RECORD_TYPE:
5905 if (TYPE_PTRMEMFUNC_P (t))
5906 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5908 /* Else fall through. */
5909 case ENUMERAL_TYPE:
5910 case UNION_TYPE:
5911 if (TYPE_TEMPLATE_INFO (t))
5913 tree argvec;
5914 tree context;
5915 tree r;
5917 /* First, determine the context for the type we are looking
5918 up. */
5919 context = TYPE_CONTEXT (t);
5920 if (context)
5921 context = tsubst_aggr_type (context, args, complain,
5922 in_decl, /*entering_scope=*/1);
5924 /* Then, figure out what arguments are appropriate for the
5925 type we are trying to find. For example, given:
5927 template <class T> struct S;
5928 template <class T, class U> void f(T, U) { S<U> su; }
5930 and supposing that we are instantiating f<int, double>,
5931 then our ARGS will be {int, double}, but, when looking up
5932 S we only want {double}. */
5933 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
5934 complain, in_decl);
5935 if (argvec == error_mark_node)
5936 return error_mark_node;
5938 r = lookup_template_class (t, argvec, in_decl, context,
5939 entering_scope, complain);
5941 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5943 else
5944 /* This is not a template type, so there's nothing to do. */
5945 return t;
5947 default:
5948 return tsubst (t, args, complain, in_decl);
5952 /* Substitute into the default argument ARG (a default argument for
5953 FN), which has the indicated TYPE. */
5955 tree
5956 tsubst_default_argument (tree fn, tree type, tree arg)
5958 tree saved_class_ptr = NULL_TREE;
5959 tree saved_class_ref = NULL_TREE;
5961 /* This default argument came from a template. Instantiate the
5962 default argument here, not in tsubst. In the case of
5963 something like:
5965 template <class T>
5966 struct S {
5967 static T t();
5968 void f(T = t());
5971 we must be careful to do name lookup in the scope of S<T>,
5972 rather than in the current class. */
5973 push_access_scope (fn);
5974 /* The default argument expression should not be considered to be
5975 within the scope of FN. Since push_access_scope sets
5976 current_function_decl, we must explicitly clear it here. */
5977 current_function_decl = NULL_TREE;
5978 /* The "this" pointer is not valid in a default argument. */
5979 if (cfun)
5981 saved_class_ptr = current_class_ptr;
5982 cp_function_chain->x_current_class_ptr = NULL_TREE;
5983 saved_class_ref = current_class_ref;
5984 cp_function_chain->x_current_class_ref = NULL_TREE;
5987 push_deferring_access_checks(dk_no_deferred);
5988 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5989 tf_error | tf_warning, NULL_TREE);
5990 pop_deferring_access_checks();
5992 /* Restore the "this" pointer. */
5993 if (cfun)
5995 cp_function_chain->x_current_class_ptr = saved_class_ptr;
5996 cp_function_chain->x_current_class_ref = saved_class_ref;
5999 pop_access_scope (fn);
6001 /* Make sure the default argument is reasonable. */
6002 arg = check_default_argument (type, arg);
6004 return arg;
6007 /* Substitute into all the default arguments for FN. */
6009 static void
6010 tsubst_default_arguments (tree fn)
6012 tree arg;
6013 tree tmpl_args;
6015 tmpl_args = DECL_TI_ARGS (fn);
6017 /* If this function is not yet instantiated, we certainly don't need
6018 its default arguments. */
6019 if (uses_template_parms (tmpl_args))
6020 return;
6022 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6023 arg;
6024 arg = TREE_CHAIN (arg))
6025 if (TREE_PURPOSE (arg))
6026 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6027 TREE_VALUE (arg),
6028 TREE_PURPOSE (arg));
6031 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
6032 (already computed) substitution of ARGS into TREE_TYPE (T), if
6033 appropriate. Return the result of the substitution. Issue error
6034 and warning messages under control of COMPLAIN. */
6036 static tree
6037 tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
6039 location_t saved_loc;
6040 tree r = NULL_TREE;
6041 tree in_decl = t;
6043 /* Set the filename and linenumber to improve error-reporting. */
6044 saved_loc = input_location;
6045 input_location = DECL_SOURCE_LOCATION (t);
6047 switch (TREE_CODE (t))
6049 case TEMPLATE_DECL:
6051 /* We can get here when processing a member template function
6052 of a template class. */
6053 tree decl = DECL_TEMPLATE_RESULT (t);
6054 tree spec;
6055 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
6057 if (!is_template_template_parm)
6059 /* We might already have an instance of this template.
6060 The ARGS are for the surrounding class type, so the
6061 full args contain the tsubst'd args for the context,
6062 plus the innermost args from the template decl. */
6063 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6064 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6065 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6066 tree full_args;
6068 full_args = tsubst_template_args (tmpl_args, args,
6069 complain, in_decl);
6071 /* tsubst_template_args doesn't copy the vector if
6072 nothing changed. But, *something* should have
6073 changed. */
6074 gcc_assert (full_args != tmpl_args);
6076 spec = retrieve_specialization (t, full_args);
6077 if (spec != NULL_TREE)
6079 r = spec;
6080 break;
6084 /* Make a new template decl. It will be similar to the
6085 original, but will record the current template arguments.
6086 We also create a new function declaration, which is just
6087 like the old one, but points to this new template, rather
6088 than the old one. */
6089 r = copy_decl (t);
6090 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6091 TREE_CHAIN (r) = NULL_TREE;
6093 if (is_template_template_parm)
6095 tree new_decl = tsubst (decl, args, complain, in_decl);
6096 DECL_TEMPLATE_RESULT (r) = new_decl;
6097 TREE_TYPE (r) = TREE_TYPE (new_decl);
6098 break;
6101 DECL_CONTEXT (r)
6102 = tsubst_aggr_type (DECL_CONTEXT (t), args,
6103 complain, in_decl,
6104 /*entering_scope=*/1);
6105 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6107 if (TREE_CODE (decl) == TYPE_DECL)
6109 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6110 if (new_type == error_mark_node)
6111 return error_mark_node;
6113 TREE_TYPE (r) = new_type;
6114 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6115 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6116 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6118 else
6120 tree new_decl = tsubst (decl, args, complain, in_decl);
6121 if (new_decl == error_mark_node)
6122 return error_mark_node;
6124 DECL_TEMPLATE_RESULT (r) = new_decl;
6125 DECL_TI_TEMPLATE (new_decl) = r;
6126 TREE_TYPE (r) = TREE_TYPE (new_decl);
6127 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6130 SET_DECL_IMPLICIT_INSTANTIATION (r);
6131 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6132 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6134 /* The template parameters for this new template are all the
6135 template parameters for the old template, except the
6136 outermost level of parameters. */
6137 DECL_TEMPLATE_PARMS (r)
6138 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6139 complain);
6141 if (PRIMARY_TEMPLATE_P (t))
6142 DECL_PRIMARY_TEMPLATE (r) = r;
6144 if (TREE_CODE (decl) != TYPE_DECL)
6145 /* Record this non-type partial instantiation. */
6146 register_specialization (r, t,
6147 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
6149 break;
6151 case FUNCTION_DECL:
6153 tree ctx;
6154 tree argvec = NULL_TREE;
6155 tree *friends;
6156 tree gen_tmpl;
6157 int member;
6158 int args_depth;
6159 int parms_depth;
6161 /* Nobody should be tsubst'ing into non-template functions. */
6162 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6164 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6166 tree spec;
6167 bool dependent_p;
6169 /* If T is not dependent, just return it. We have to
6170 increment PROCESSING_TEMPLATE_DECL because
6171 value_dependent_expression_p assumes that nothing is
6172 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6173 ++processing_template_decl;
6174 dependent_p = value_dependent_expression_p (t);
6175 --processing_template_decl;
6176 if (!dependent_p)
6177 return t;
6179 /* Calculate the most general template of which R is a
6180 specialization, and the complete set of arguments used to
6181 specialize R. */
6182 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6183 argvec = tsubst_template_args (DECL_TI_ARGS
6184 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6185 args, complain, in_decl);
6187 /* Check to see if we already have this specialization. */
6188 spec = retrieve_specialization (gen_tmpl, argvec);
6190 if (spec)
6192 r = spec;
6193 break;
6196 /* We can see more levels of arguments than parameters if
6197 there was a specialization of a member template, like
6198 this:
6200 template <class T> struct S { template <class U> void f(); }
6201 template <> template <class U> void S<int>::f(U);
6203 Here, we'll be substituting into the specialization,
6204 because that's where we can find the code we actually
6205 want to generate, but we'll have enough arguments for
6206 the most general template.
6208 We also deal with the peculiar case:
6210 template <class T> struct S {
6211 template <class U> friend void f();
6213 template <class U> void f() {}
6214 template S<int>;
6215 template void f<double>();
6217 Here, the ARGS for the instantiation of will be {int,
6218 double}. But, we only need as many ARGS as there are
6219 levels of template parameters in CODE_PATTERN. We are
6220 careful not to get fooled into reducing the ARGS in
6221 situations like:
6223 template <class T> struct S { template <class U> void f(U); }
6224 template <class T> template <> void S<T>::f(int) {}
6226 which we can spot because the pattern will be a
6227 specialization in this case. */
6228 args_depth = TMPL_ARGS_DEPTH (args);
6229 parms_depth =
6230 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6231 if (args_depth > parms_depth
6232 && !DECL_TEMPLATE_SPECIALIZATION (t))
6233 args = get_innermost_template_args (args, parms_depth);
6235 else
6237 /* This special case arises when we have something like this:
6239 template <class T> struct S {
6240 friend void f<int>(int, double);
6243 Here, the DECL_TI_TEMPLATE for the friend declaration
6244 will be an IDENTIFIER_NODE. We are being called from
6245 tsubst_friend_function, and we want only to create a
6246 new decl (R) with appropriate types so that we can call
6247 determine_specialization. */
6248 gen_tmpl = NULL_TREE;
6251 if (DECL_CLASS_SCOPE_P (t))
6253 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6254 member = 2;
6255 else
6256 member = 1;
6257 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6258 complain, t, /*entering_scope=*/1);
6260 else
6262 member = 0;
6263 ctx = DECL_CONTEXT (t);
6265 type = tsubst (type, args, complain, in_decl);
6266 if (type == error_mark_node)
6267 return error_mark_node;
6269 /* We do NOT check for matching decls pushed separately at this
6270 point, as they may not represent instantiations of this
6271 template, and in any case are considered separate under the
6272 discrete model. */
6273 r = copy_decl (t);
6274 DECL_USE_TEMPLATE (r) = 0;
6275 TREE_TYPE (r) = type;
6276 /* Clear out the mangled name and RTL for the instantiation. */
6277 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6278 SET_DECL_RTL (r, NULL_RTX);
6279 DECL_INITIAL (r) = NULL_TREE;
6280 DECL_CONTEXT (r) = ctx;
6282 if (member && DECL_CONV_FN_P (r))
6283 /* Type-conversion operator. Reconstruct the name, in
6284 case it's the name of one of the template's parameters. */
6285 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6287 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6288 complain, t);
6289 DECL_RESULT (r) = NULL_TREE;
6291 TREE_STATIC (r) = 0;
6292 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6293 DECL_EXTERNAL (r) = 1;
6294 /* If this is an instantiation of a function with internal
6295 linkage, we already know what object file linkage will be
6296 assigned to the instantiation. */
6297 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6298 DECL_DEFER_OUTPUT (r) = 0;
6299 TREE_CHAIN (r) = NULL_TREE;
6300 DECL_PENDING_INLINE_INFO (r) = 0;
6301 DECL_PENDING_INLINE_P (r) = 0;
6302 DECL_SAVED_TREE (r) = NULL_TREE;
6303 TREE_USED (r) = 0;
6304 if (DECL_CLONED_FUNCTION (r))
6306 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6307 args, complain, t);
6308 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6309 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6312 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6313 this in the special friend case mentioned above where
6314 GEN_TMPL is NULL. */
6315 if (gen_tmpl)
6317 DECL_TEMPLATE_INFO (r)
6318 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6319 SET_DECL_IMPLICIT_INSTANTIATION (r);
6320 register_specialization (r, gen_tmpl, argvec);
6322 /* We're not supposed to instantiate default arguments
6323 until they are called, for a template. But, for a
6324 declaration like:
6326 template <class T> void f ()
6327 { extern void g(int i = T()); }
6329 we should do the substitution when the template is
6330 instantiated. We handle the member function case in
6331 instantiate_class_template since the default arguments
6332 might refer to other members of the class. */
6333 if (!member
6334 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6335 && !uses_template_parms (argvec))
6336 tsubst_default_arguments (r);
6339 /* Copy the list of befriending classes. */
6340 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6341 *friends;
6342 friends = &TREE_CHAIN (*friends))
6344 *friends = copy_node (*friends);
6345 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6346 args, complain,
6347 in_decl);
6350 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6352 maybe_retrofit_in_chrg (r);
6353 if (DECL_CONSTRUCTOR_P (r))
6354 grok_ctor_properties (ctx, r);
6355 /* If this is an instantiation of a member template, clone it.
6356 If it isn't, that'll be handled by
6357 clone_constructors_and_destructors. */
6358 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6359 clone_function_decl (r, /*update_method_vec_p=*/0);
6361 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6362 grok_op_properties (r, DECL_FRIEND_P (r),
6363 (complain & tf_error) != 0);
6365 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6366 SET_DECL_FRIEND_CONTEXT (r,
6367 tsubst (DECL_FRIEND_CONTEXT (t),
6368 args, complain, in_decl));
6370 break;
6372 case PARM_DECL:
6374 r = copy_node (t);
6375 if (DECL_TEMPLATE_PARM_P (t))
6376 SET_DECL_TEMPLATE_PARM_P (r);
6378 TREE_TYPE (r) = type;
6379 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6381 if (DECL_INITIAL (r))
6383 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6384 DECL_INITIAL (r) = TREE_TYPE (r);
6385 else
6386 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6387 complain, in_decl);
6390 DECL_CONTEXT (r) = NULL_TREE;
6392 if (!DECL_TEMPLATE_PARM_P (r))
6393 DECL_ARG_TYPE (r) = type_passed_as (type);
6394 if (TREE_CHAIN (t))
6395 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6396 complain, TREE_CHAIN (t));
6398 break;
6400 case FIELD_DECL:
6402 r = copy_decl (t);
6403 TREE_TYPE (r) = type;
6404 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6406 /* We don't have to set DECL_CONTEXT here; it is set by
6407 finish_member_declaration. */
6408 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6409 complain, in_decl);
6410 TREE_CHAIN (r) = NULL_TREE;
6411 if (VOID_TYPE_P (type))
6412 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6414 break;
6416 case USING_DECL:
6418 r = copy_node (t);
6419 /* It is not a dependent using decl any more. */
6420 TREE_TYPE (r) = void_type_node;
6421 DECL_INITIAL (r)
6422 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6423 DECL_NAME (r)
6424 = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
6425 TREE_CHAIN (r) = NULL_TREE;
6427 break;
6429 case TYPE_DECL:
6430 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6431 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6433 /* If this is the canonical decl, we don't have to mess with
6434 instantiations, and often we can't (for typename, template
6435 type parms and such). Note that TYPE_NAME is not correct for
6436 the above test if we've copied the type for a typedef. */
6437 r = TYPE_NAME (type);
6438 break;
6441 /* Fall through. */
6443 case VAR_DECL:
6445 tree argvec = NULL_TREE;
6446 tree gen_tmpl = NULL_TREE;
6447 tree spec;
6448 tree tmpl = NULL_TREE;
6449 tree ctx;
6450 int local_p;
6452 /* Assume this is a non-local variable. */
6453 local_p = 0;
6455 if (TYPE_P (CP_DECL_CONTEXT (t)))
6456 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6457 complain,
6458 in_decl, /*entering_scope=*/1);
6459 else if (DECL_NAMESPACE_SCOPE_P (t))
6460 ctx = DECL_CONTEXT (t);
6461 else
6463 /* Subsequent calls to pushdecl will fill this in. */
6464 ctx = NULL_TREE;
6465 local_p = 1;
6468 /* Check to see if we already have this specialization. */
6469 if (!local_p)
6471 tmpl = DECL_TI_TEMPLATE (t);
6472 gen_tmpl = most_general_template (tmpl);
6473 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6474 spec = retrieve_specialization (gen_tmpl, argvec);
6476 else
6477 spec = retrieve_local_specialization (t);
6479 if (spec)
6481 r = spec;
6482 break;
6485 r = copy_decl (t);
6486 if (TREE_CODE (r) == VAR_DECL)
6488 type = complete_type (type);
6489 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6490 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6492 else if (DECL_SELF_REFERENCE_P (t))
6493 SET_DECL_SELF_REFERENCE_P (r);
6494 TREE_TYPE (r) = type;
6495 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6496 DECL_CONTEXT (r) = ctx;
6497 /* Clear out the mangled name and RTL for the instantiation. */
6498 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6499 SET_DECL_RTL (r, NULL_RTX);
6501 /* Don't try to expand the initializer until someone tries to use
6502 this variable; otherwise we run into circular dependencies. */
6503 DECL_INITIAL (r) = NULL_TREE;
6504 SET_DECL_RTL (r, NULL_RTX);
6505 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6507 /* Even if the original location is out of scope, the newly
6508 substituted one is not. */
6509 if (TREE_CODE (r) == VAR_DECL)
6511 DECL_DEAD_FOR_LOCAL (r) = 0;
6512 DECL_INITIALIZED_P (r) = 0;
6515 if (!local_p)
6517 /* A static data member declaration is always marked
6518 external when it is declared in-class, even if an
6519 initializer is present. We mimic the non-template
6520 processing here. */
6521 DECL_EXTERNAL (r) = 1;
6523 register_specialization (r, gen_tmpl, argvec);
6524 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6525 SET_DECL_IMPLICIT_INSTANTIATION (r);
6527 else
6528 register_local_specialization (r, t);
6530 TREE_CHAIN (r) = NULL_TREE;
6531 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6532 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6533 /* Compute the size, alignment, etc. of R. */
6534 layout_decl (r, 0);
6536 break;
6538 default:
6539 gcc_unreachable ();
6542 /* Restore the file and line information. */
6543 input_location = saved_loc;
6545 return r;
6548 /* Substitute into the ARG_TYPES of a function type. */
6550 static tree
6551 tsubst_arg_types (tree arg_types,
6552 tree args,
6553 tsubst_flags_t complain,
6554 tree in_decl)
6556 tree remaining_arg_types;
6557 tree type;
6559 if (!arg_types || arg_types == void_list_node)
6560 return arg_types;
6562 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6563 args, complain, in_decl);
6564 if (remaining_arg_types == error_mark_node)
6565 return error_mark_node;
6567 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6568 if (type == error_mark_node)
6569 return error_mark_node;
6570 if (VOID_TYPE_P (type))
6572 if (complain & tf_error)
6574 error ("invalid parameter type `%T'", type);
6575 if (in_decl)
6576 cp_error_at ("in declaration `%D'", in_decl);
6578 return error_mark_node;
6581 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6582 top-level qualifiers as required. */
6583 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6585 /* Note that we do not substitute into default arguments here. The
6586 standard mandates that they be instantiated only when needed,
6587 which is done in build_over_call. */
6588 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6589 remaining_arg_types);
6593 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6594 *not* handle the exception-specification for FNTYPE, because the
6595 initial substitution of explicitly provided template parameters
6596 during argument deduction forbids substitution into the
6597 exception-specification:
6599 [temp.deduct]
6601 All references in the function type of the function template to the
6602 corresponding template parameters are replaced by the specified tem-
6603 plate argument values. If a substitution in a template parameter or
6604 in the function type of the function template results in an invalid
6605 type, type deduction fails. [Note: The equivalent substitution in
6606 exception specifications is done only when the function is instanti-
6607 ated, at which point a program is ill-formed if the substitution
6608 results in an invalid type.] */
6610 static tree
6611 tsubst_function_type (tree t,
6612 tree args,
6613 tsubst_flags_t complain,
6614 tree in_decl)
6616 tree return_type;
6617 tree arg_types;
6618 tree fntype;
6620 /* The TYPE_CONTEXT is not used for function/method types. */
6621 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6623 /* Substitute the return type. */
6624 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6625 if (return_type == error_mark_node)
6626 return error_mark_node;
6628 /* Substitute the argument types. */
6629 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6630 complain, in_decl);
6631 if (arg_types == error_mark_node)
6632 return error_mark_node;
6634 /* Construct a new type node and return it. */
6635 if (TREE_CODE (t) == FUNCTION_TYPE)
6636 fntype = build_function_type (return_type, arg_types);
6637 else
6639 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6640 if (! IS_AGGR_TYPE (r))
6642 /* [temp.deduct]
6644 Type deduction may fail for any of the following
6645 reasons:
6647 -- Attempting to create "pointer to member of T" when T
6648 is not a class type. */
6649 if (complain & tf_error)
6650 error ("creating pointer to member function of non-class type `%T'",
6652 return error_mark_node;
6655 fntype = build_method_type_directly (r, return_type,
6656 TREE_CHAIN (arg_types));
6658 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6659 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6661 return fntype;
6664 /* Substitute into the PARMS of a call-declarator. */
6666 static tree
6667 tsubst_call_declarator_parms (tree parms,
6668 tree args,
6669 tsubst_flags_t complain,
6670 tree in_decl)
6672 tree new_parms;
6673 tree type;
6674 tree defarg;
6676 if (!parms || parms == void_list_node)
6677 return parms;
6679 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6680 args, complain, in_decl);
6682 /* Figure out the type of this parameter. */
6683 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6685 /* Figure out the default argument as well. Note that we use
6686 tsubst_expr since the default argument is really an expression. */
6687 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6689 /* Chain this parameter on to the front of those we have already
6690 processed. We don't use hash_tree_cons because that function
6691 doesn't check TREE_PARMLIST. */
6692 new_parms = tree_cons (defarg, type, new_parms);
6694 return new_parms;
6697 /* Take the tree structure T and replace template parameters used
6698 therein with the argument vector ARGS. IN_DECL is an associated
6699 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6700 Issue error and warning messages under control of COMPLAIN. Note
6701 that we must be relatively non-tolerant of extensions here, in
6702 order to preserve conformance; if we allow substitutions that
6703 should not be allowed, we may allow argument deductions that should
6704 not succeed, and therefore report ambiguous overload situations
6705 where there are none. In theory, we could allow the substitution,
6706 but indicate that it should have failed, and allow our caller to
6707 make sure that the right thing happens, but we don't try to do this
6708 yet.
6710 This function is used for dealing with types, decls and the like;
6711 for expressions, use tsubst_expr or tsubst_copy. */
6713 static tree
6714 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6716 tree type, r;
6718 if (t == NULL_TREE || t == error_mark_node
6719 || t == integer_type_node
6720 || t == void_type_node
6721 || t == char_type_node
6722 || t == unknown_type_node
6723 || TREE_CODE (t) == NAMESPACE_DECL)
6724 return t;
6726 if (TREE_CODE (t) == IDENTIFIER_NODE)
6727 type = IDENTIFIER_TYPE_VALUE (t);
6728 else
6729 type = TREE_TYPE (t);
6731 gcc_assert (type != unknown_type_node);
6733 if (type && TREE_CODE (t) != FUNCTION_DECL
6734 && TREE_CODE (t) != TYPENAME_TYPE
6735 && TREE_CODE (t) != TEMPLATE_DECL
6736 && TREE_CODE (t) != IDENTIFIER_NODE
6737 && TREE_CODE (t) != FUNCTION_TYPE
6738 && TREE_CODE (t) != METHOD_TYPE)
6739 type = tsubst (type, args, complain, in_decl);
6740 if (type == error_mark_node)
6741 return error_mark_node;
6743 if (DECL_P (t))
6744 return tsubst_decl (t, args, type, complain);
6746 switch (TREE_CODE (t))
6748 case RECORD_TYPE:
6749 case UNION_TYPE:
6750 case ENUMERAL_TYPE:
6751 return tsubst_aggr_type (t, args, complain, in_decl,
6752 /*entering_scope=*/0);
6754 case ERROR_MARK:
6755 case IDENTIFIER_NODE:
6756 case VOID_TYPE:
6757 case REAL_TYPE:
6758 case COMPLEX_TYPE:
6759 case VECTOR_TYPE:
6760 case BOOLEAN_TYPE:
6761 case INTEGER_CST:
6762 case REAL_CST:
6763 case STRING_CST:
6764 return t;
6766 case INTEGER_TYPE:
6767 if (t == integer_type_node)
6768 return t;
6770 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6771 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6772 return t;
6775 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6777 /* The array dimension behaves like a non-type template arg,
6778 in that we want to fold it as much as possible. */
6779 max = tsubst_template_arg (omax, args, complain, in_decl);
6780 if (!processing_template_decl)
6781 max = decl_constant_value (max);
6783 if (integer_zerop (omax))
6785 /* Still allow an explicit array of size zero. */
6786 if (pedantic)
6787 pedwarn ("creating array with size zero");
6789 else if (integer_zerop (max)
6790 || (TREE_CODE (max) == INTEGER_CST
6791 && INT_CST_LT (max, integer_zero_node)))
6793 /* [temp.deduct]
6795 Type deduction may fail for any of the following
6796 reasons:
6798 Attempting to create an array with a size that is
6799 zero or negative. */
6800 if (complain & tf_error)
6801 error ("creating array with size zero (`%E')", max);
6803 return error_mark_node;
6806 return compute_array_index_type (NULL_TREE, max);
6809 case TEMPLATE_TYPE_PARM:
6810 case TEMPLATE_TEMPLATE_PARM:
6811 case BOUND_TEMPLATE_TEMPLATE_PARM:
6812 case TEMPLATE_PARM_INDEX:
6814 int idx;
6815 int level;
6816 int levels;
6817 tree arg = NULL_TREE;
6819 r = NULL_TREE;
6821 gcc_assert (TREE_VEC_LENGTH (args) > 0);
6822 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6823 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6824 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6826 idx = TEMPLATE_TYPE_IDX (t);
6827 level = TEMPLATE_TYPE_LEVEL (t);
6829 else
6831 idx = TEMPLATE_PARM_IDX (t);
6832 level = TEMPLATE_PARM_LEVEL (t);
6835 levels = TMPL_ARGS_DEPTH (args);
6836 if (level <= levels)
6837 arg = TMPL_ARG (args, level, idx);
6839 if (arg == error_mark_node)
6840 return error_mark_node;
6841 else if (arg != NULL_TREE)
6843 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6845 gcc_assert (TYPE_P (arg));
6846 return cp_build_qualified_type_real
6847 (arg, cp_type_quals (arg) | cp_type_quals (t),
6848 complain | tf_ignore_bad_quals);
6850 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6852 /* We are processing a type constructed from a
6853 template template parameter. */
6854 tree argvec = tsubst (TYPE_TI_ARGS (t),
6855 args, complain, in_decl);
6856 if (argvec == error_mark_node)
6857 return error_mark_node;
6859 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
6860 are resolving nested-types in the signature of a
6861 member function templates. Otherwise ARG is a
6862 TEMPLATE_DECL and is the real template to be
6863 instantiated. */
6864 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6865 arg = TYPE_NAME (arg);
6867 r = lookup_template_class (arg,
6868 argvec, in_decl,
6869 DECL_CONTEXT (arg),
6870 /*entering_scope=*/0,
6871 complain);
6872 return cp_build_qualified_type_real
6873 (r, TYPE_QUALS (t), complain);
6875 else
6876 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6877 return arg;
6880 if (level == 1)
6881 /* This can happen during the attempted tsubst'ing in
6882 unify. This means that we don't yet have any information
6883 about the template parameter in question. */
6884 return t;
6886 /* If we get here, we must have been looking at a parm for a
6887 more deeply nested template. Make a new version of this
6888 template parameter, but with a lower level. */
6889 switch (TREE_CODE (t))
6891 case TEMPLATE_TYPE_PARM:
6892 case TEMPLATE_TEMPLATE_PARM:
6893 case BOUND_TEMPLATE_TEMPLATE_PARM:
6894 if (cp_type_quals (t))
6896 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6897 r = cp_build_qualified_type_real
6898 (r, cp_type_quals (t),
6899 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6900 ? tf_ignore_bad_quals : 0));
6902 else
6904 r = copy_type (t);
6905 TEMPLATE_TYPE_PARM_INDEX (r)
6906 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6907 r, levels);
6908 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6909 TYPE_MAIN_VARIANT (r) = r;
6910 TYPE_POINTER_TO (r) = NULL_TREE;
6911 TYPE_REFERENCE_TO (r) = NULL_TREE;
6913 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6915 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6916 complain, in_decl);
6917 if (argvec == error_mark_node)
6918 return error_mark_node;
6920 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6921 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6924 break;
6926 case TEMPLATE_PARM_INDEX:
6927 r = reduce_template_parm_level (t, type, levels);
6928 break;
6930 default:
6931 gcc_unreachable ();
6934 return r;
6937 case TREE_LIST:
6939 tree purpose, value, chain;
6941 if (t == void_list_node)
6942 return t;
6944 purpose = TREE_PURPOSE (t);
6945 if (purpose)
6947 purpose = tsubst (purpose, args, complain, in_decl);
6948 if (purpose == error_mark_node)
6949 return error_mark_node;
6951 value = TREE_VALUE (t);
6952 if (value)
6954 value = tsubst (value, args, complain, in_decl);
6955 if (value == error_mark_node)
6956 return error_mark_node;
6958 chain = TREE_CHAIN (t);
6959 if (chain && chain != void_type_node)
6961 chain = tsubst (chain, args, complain, in_decl);
6962 if (chain == error_mark_node)
6963 return error_mark_node;
6965 if (purpose == TREE_PURPOSE (t)
6966 && value == TREE_VALUE (t)
6967 && chain == TREE_CHAIN (t))
6968 return t;
6969 return hash_tree_cons (purpose, value, chain);
6972 case TREE_BINFO:
6973 /* We should never be tsubsting a binfo. */
6974 gcc_unreachable ();
6976 case TREE_VEC:
6977 /* A vector of template arguments. */
6978 gcc_assert (!type);
6979 return tsubst_template_args (t, args, complain, in_decl);
6981 case POINTER_TYPE:
6982 case REFERENCE_TYPE:
6984 enum tree_code code;
6986 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6987 return t;
6989 code = TREE_CODE (t);
6992 /* [temp.deduct]
6994 Type deduction may fail for any of the following
6995 reasons:
6997 -- Attempting to create a pointer to reference type.
6998 -- Attempting to create a reference to a reference type or
6999 a reference to void. */
7000 if (TREE_CODE (type) == REFERENCE_TYPE
7001 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7003 static location_t last_loc;
7005 /* We keep track of the last time we issued this error
7006 message to avoid spewing a ton of messages during a
7007 single bad template instantiation. */
7008 if (complain & tf_error
7009 #ifdef USE_MAPPED_LOCATION
7010 && last_loc != input_location)
7011 #else
7012 && (last_loc.line != input_line
7013 || last_loc.file != input_filename))
7014 #endif
7016 if (TREE_CODE (type) == VOID_TYPE)
7017 error ("forming reference to void");
7018 else
7019 error ("forming %s to reference type `%T'",
7020 (code == POINTER_TYPE) ? "pointer" : "reference",
7021 type);
7022 last_loc = input_location;
7025 return error_mark_node;
7027 else if (code == POINTER_TYPE)
7029 r = build_pointer_type (type);
7030 if (TREE_CODE (type) == METHOD_TYPE)
7031 r = build_ptrmemfunc_type (r);
7033 else
7034 r = build_reference_type (type);
7035 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7037 if (r != error_mark_node)
7038 /* Will this ever be needed for TYPE_..._TO values? */
7039 layout_type (r);
7041 return r;
7043 case OFFSET_TYPE:
7045 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7046 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7048 /* [temp.deduct]
7050 Type deduction may fail for any of the following
7051 reasons:
7053 -- Attempting to create "pointer to member of T" when T
7054 is not a class type. */
7055 if (complain & tf_error)
7056 error ("creating pointer to member of non-class type `%T'", r);
7057 return error_mark_node;
7059 if (TREE_CODE (type) == REFERENCE_TYPE)
7061 if (complain & tf_error)
7062 error ("creating pointer to member reference type `%T'", type);
7064 return error_mark_node;
7066 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7067 if (TREE_CODE (type) == FUNCTION_TYPE)
7069 /* This is really a method type. The cv qualifiers of the
7070 this pointer should _not_ be determined by the cv
7071 qualifiers of the class type. They should be held
7072 somewhere in the FUNCTION_TYPE, but we don't do that at
7073 the moment. Consider
7074 typedef void (Func) () const;
7076 template <typename T1> void Foo (Func T1::*);
7079 tree method_type;
7081 method_type = build_method_type_directly (TYPE_MAIN_VARIANT (r),
7082 TREE_TYPE (type),
7083 TYPE_ARG_TYPES (type));
7084 return build_ptrmemfunc_type (build_pointer_type (method_type));
7086 else
7087 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7088 TYPE_QUALS (t),
7089 complain);
7091 case FUNCTION_TYPE:
7092 case METHOD_TYPE:
7094 tree fntype;
7095 tree raises;
7097 fntype = tsubst_function_type (t, args, complain, in_decl);
7098 if (fntype == error_mark_node)
7099 return error_mark_node;
7101 /* Substitute the exception specification. */
7102 raises = TYPE_RAISES_EXCEPTIONS (t);
7103 if (raises)
7105 tree list = NULL_TREE;
7107 if (! TREE_VALUE (raises))
7108 list = raises;
7109 else
7110 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
7112 tree spec = TREE_VALUE (raises);
7114 spec = tsubst (spec, args, complain, in_decl);
7115 if (spec == error_mark_node)
7116 return spec;
7117 list = add_exception_specifier (list, spec, complain);
7119 fntype = build_exception_variant (fntype, list);
7121 return fntype;
7123 case ARRAY_TYPE:
7125 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7126 if (domain == error_mark_node)
7127 return error_mark_node;
7129 /* As an optimization, we avoid regenerating the array type if
7130 it will obviously be the same as T. */
7131 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7132 return t;
7134 /* These checks should match the ones in grokdeclarator.
7136 [temp.deduct]
7138 The deduction may fail for any of the following reasons:
7140 -- Attempting to create an array with an element type that
7141 is void, a function type, or a reference type, or [DR337]
7142 an abstract class type. */
7143 if (TREE_CODE (type) == VOID_TYPE
7144 || TREE_CODE (type) == FUNCTION_TYPE
7145 || TREE_CODE (type) == REFERENCE_TYPE)
7147 if (complain & tf_error)
7148 error ("creating array of `%T'", type);
7149 return error_mark_node;
7151 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7153 if (complain & tf_error)
7154 error ("creating array of `%T', which is an abstract class type",
7155 type);
7156 return error_mark_node;
7159 r = build_cplus_array_type (type, domain);
7160 return r;
7163 case PLUS_EXPR:
7164 case MINUS_EXPR:
7166 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7167 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7169 if (e1 == error_mark_node || e2 == error_mark_node)
7170 return error_mark_node;
7172 return fold (build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2));
7175 case NEGATE_EXPR:
7176 case NOP_EXPR:
7178 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7179 if (e == error_mark_node)
7180 return error_mark_node;
7182 return fold (build1 (TREE_CODE (t), TREE_TYPE (t), e));
7185 case TYPENAME_TYPE:
7187 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7188 in_decl, /*entering_scope=*/1);
7189 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7190 complain, in_decl);
7192 if (ctx == error_mark_node || f == error_mark_node)
7193 return error_mark_node;
7195 if (!IS_AGGR_TYPE (ctx))
7197 if (complain & tf_error)
7198 error ("`%T' is not a class, struct, or union type",
7199 ctx);
7200 return error_mark_node;
7202 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7204 /* Normally, make_typename_type does not require that the CTX
7205 have complete type in order to allow things like:
7207 template <class T> struct S { typename S<T>::X Y; };
7209 But, such constructs have already been resolved by this
7210 point, so here CTX really should have complete type, unless
7211 it's a partial instantiation. */
7212 ctx = complete_type (ctx);
7213 if (!COMPLETE_TYPE_P (ctx))
7215 if (complain & tf_error)
7216 cxx_incomplete_type_error (NULL_TREE, ctx);
7217 return error_mark_node;
7221 f = make_typename_type (ctx, f,
7222 (complain & tf_error) | tf_keep_type_decl);
7223 if (f == error_mark_node)
7224 return f;
7225 if (TREE_CODE (f) == TYPE_DECL)
7227 complain |= tf_ignore_bad_quals;
7228 f = TREE_TYPE (f);
7231 return cp_build_qualified_type_real
7232 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7235 case UNBOUND_CLASS_TEMPLATE:
7237 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7238 in_decl, /*entering_scope=*/1);
7239 tree name = TYPE_IDENTIFIER (t);
7241 if (ctx == error_mark_node || name == error_mark_node)
7242 return error_mark_node;
7244 return make_unbound_class_template (ctx, name, complain);
7247 case INDIRECT_REF:
7248 case ADDR_EXPR:
7249 case CALL_EXPR:
7250 gcc_unreachable ();
7252 case ARRAY_REF:
7254 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7255 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7256 if (e1 == error_mark_node || e2 == error_mark_node)
7257 return error_mark_node;
7259 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7262 case SCOPE_REF:
7264 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7265 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7266 if (e1 == error_mark_node || e2 == error_mark_node)
7267 return error_mark_node;
7269 return build_nt (TREE_CODE (t), e1, e2);
7272 case TYPEOF_TYPE:
7274 tree type;
7276 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7277 complain, in_decl));
7278 return cp_build_qualified_type_real (type,
7279 cp_type_quals (t)
7280 | cp_type_quals (type),
7281 complain);
7284 default:
7285 sorry ("use of `%s' in template",
7286 tree_code_name [(int) TREE_CODE (t)]);
7287 return error_mark_node;
7291 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7292 type of the expression on the left-hand side of the "." or "->"
7293 operator. */
7295 static tree
7296 tsubst_baselink (tree baselink, tree object_type,
7297 tree args, tsubst_flags_t complain, tree in_decl)
7299 tree name;
7300 tree qualifying_scope;
7301 tree fns;
7302 tree template_args = 0;
7303 bool template_id_p = false;
7305 /* A baselink indicates a function from a base class. The
7306 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7307 non-dependent types; otherwise, the lookup could not have
7308 succeeded. However, they may indicate bases of the template
7309 class, rather than the instantiated class.
7311 In addition, lookups that were not ambiguous before may be
7312 ambiguous now. Therefore, we perform the lookup again. */
7313 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7314 fns = BASELINK_FUNCTIONS (baselink);
7315 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7317 template_id_p = true;
7318 template_args = TREE_OPERAND (fns, 1);
7319 fns = TREE_OPERAND (fns, 0);
7320 if (template_args)
7321 template_args = tsubst_template_args (template_args, args,
7322 complain, in_decl);
7324 name = DECL_NAME (get_first_fn (fns));
7325 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7326 if (BASELINK_P (baselink) && template_id_p)
7327 BASELINK_FUNCTIONS (baselink)
7328 = build_nt (TEMPLATE_ID_EXPR,
7329 BASELINK_FUNCTIONS (baselink),
7330 template_args);
7331 if (!object_type)
7332 object_type = current_class_type;
7333 return adjust_result_of_qualified_name_lookup (baselink,
7334 qualifying_scope,
7335 object_type);
7338 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7339 true if the qualified-id will be a postfix-expression in-and-of
7340 itself; false if more of the postfix-expression follows the
7341 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7342 of "&". */
7344 static tree
7345 tsubst_qualified_id (tree qualified_id, tree args,
7346 tsubst_flags_t complain, tree in_decl,
7347 bool done, bool address_p)
7349 tree expr;
7350 tree scope;
7351 tree name;
7352 bool is_template;
7353 tree template_args;
7355 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7357 /* Figure out what name to look up. */
7358 name = TREE_OPERAND (qualified_id, 1);
7359 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7361 is_template = true;
7362 template_args = TREE_OPERAND (name, 1);
7363 if (template_args)
7364 template_args = tsubst_template_args (template_args, args,
7365 complain, in_decl);
7366 name = TREE_OPERAND (name, 0);
7368 else
7370 is_template = false;
7371 template_args = NULL_TREE;
7374 /* Substitute into the qualifying scope. When there are no ARGS, we
7375 are just trying to simplify a non-dependent expression. In that
7376 case the qualifying scope may be dependent, and, in any case,
7377 substituting will not help. */
7378 scope = TREE_OPERAND (qualified_id, 0);
7379 if (args)
7381 scope = tsubst (scope, args, complain, in_decl);
7382 expr = tsubst_copy (name, args, complain, in_decl);
7384 else
7385 expr = name;
7387 if (dependent_type_p (scope))
7388 return build_nt (SCOPE_REF, scope, expr);
7390 if (!BASELINK_P (name) && !DECL_P (expr))
7392 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7393 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7394 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7396 if (complain & tf_error)
7398 error ("dependent-name `%E' is parsed as a non-type, but "
7399 "instantiation yields a type", qualified_id);
7400 inform ("say `typename %E' if a type is meant", qualified_id);
7402 return error_mark_node;
7406 if (DECL_P (expr))
7408 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7409 scope);
7410 /* Remember that there was a reference to this entity. */
7411 mark_used (expr);
7414 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7416 if (complain & tf_error)
7417 qualified_name_lookup_error (scope,
7418 TREE_OPERAND (qualified_id, 1),
7419 expr);
7420 return error_mark_node;
7423 if (is_template)
7424 expr = lookup_template_function (expr, template_args);
7426 if (expr == error_mark_node && complain & tf_error)
7427 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7428 expr);
7429 else if (TYPE_P (scope))
7431 expr = (adjust_result_of_qualified_name_lookup
7432 (expr, scope, current_class_type));
7433 expr = finish_qualified_id_expr (scope, expr, done, address_p);
7436 return expr;
7439 /* Like tsubst, but deals with expressions. This function just replaces
7440 template parms; to finish processing the resultant expression, use
7441 tsubst_expr. */
7443 static tree
7444 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7446 enum tree_code code;
7447 tree r;
7449 if (t == NULL_TREE || t == error_mark_node)
7450 return t;
7452 code = TREE_CODE (t);
7454 switch (code)
7456 case PARM_DECL:
7457 r = retrieve_local_specialization (t);
7458 gcc_assert (r != NULL);
7459 mark_used (r);
7460 return r;
7462 case CONST_DECL:
7464 tree enum_type;
7465 tree v;
7467 if (DECL_TEMPLATE_PARM_P (t))
7468 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7469 /* There is no need to substitute into namespace-scope
7470 enumerators. */
7471 if (DECL_NAMESPACE_SCOPE_P (t))
7472 return t;
7473 /* If ARGS is NULL, then T is known to be non-dependent. */
7474 if (args == NULL_TREE)
7475 return decl_constant_value (t);
7477 /* Unfortunately, we cannot just call lookup_name here.
7478 Consider:
7480 template <int I> int f() {
7481 enum E { a = I };
7482 struct S { void g() { E e = a; } };
7485 When we instantiate f<7>::S::g(), say, lookup_name is not
7486 clever enough to find f<7>::a. */
7487 enum_type
7488 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7489 /*entering_scope=*/0);
7491 for (v = TYPE_VALUES (enum_type);
7492 v != NULL_TREE;
7493 v = TREE_CHAIN (v))
7494 if (TREE_PURPOSE (v) == DECL_NAME (t))
7495 return TREE_VALUE (v);
7497 /* We didn't find the name. That should never happen; if
7498 name-lookup found it during preliminary parsing, we
7499 should find it again here during instantiation. */
7500 gcc_unreachable ();
7502 return t;
7504 case FIELD_DECL:
7505 if (DECL_CONTEXT (t))
7507 tree ctx;
7509 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7510 /*entering_scope=*/1);
7511 if (ctx != DECL_CONTEXT (t))
7512 return lookup_field (ctx, DECL_NAME (t), 0, false);
7515 return t;
7517 case VAR_DECL:
7518 case FUNCTION_DECL:
7519 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7520 || local_variable_p (t))
7521 t = tsubst (t, args, complain, in_decl);
7522 mark_used (t);
7523 return t;
7525 case BASELINK:
7526 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7528 case TEMPLATE_DECL:
7529 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7530 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7531 args, complain, in_decl);
7532 else if (is_member_template (t))
7533 return tsubst (t, args, complain, in_decl);
7534 else if (DECL_CLASS_SCOPE_P (t)
7535 && uses_template_parms (DECL_CONTEXT (t)))
7537 /* Template template argument like the following example need
7538 special treatment:
7540 template <template <class> class TT> struct C {};
7541 template <class T> struct D {
7542 template <class U> struct E {};
7543 C<E> c; // #1
7545 D<int> d; // #2
7547 We are processing the template argument `E' in #1 for
7548 the template instantiation #2. Originally, `E' is a
7549 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7550 have to substitute this with one having context `D<int>'. */
7552 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7553 return lookup_field (context, DECL_NAME(t), 0, false);
7555 else
7556 /* Ordinary template template argument. */
7557 return t;
7559 case CAST_EXPR:
7560 case REINTERPRET_CAST_EXPR:
7561 case CONST_CAST_EXPR:
7562 case STATIC_CAST_EXPR:
7563 case DYNAMIC_CAST_EXPR:
7564 case NOP_EXPR:
7565 return build1
7566 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7567 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7569 case INDIRECT_REF:
7570 case NEGATE_EXPR:
7571 case TRUTH_NOT_EXPR:
7572 case BIT_NOT_EXPR:
7573 case ADDR_EXPR:
7574 case CONVERT_EXPR: /* Unary + */
7575 case SIZEOF_EXPR:
7576 case ALIGNOF_EXPR:
7577 case ARROW_EXPR:
7578 case THROW_EXPR:
7579 case TYPEID_EXPR:
7580 case REALPART_EXPR:
7581 case IMAGPART_EXPR:
7582 return build1
7583 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7584 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7586 case COMPONENT_REF:
7588 tree object;
7589 tree name;
7591 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7592 name = TREE_OPERAND (t, 1);
7593 if (TREE_CODE (name) == BIT_NOT_EXPR)
7595 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7596 complain, in_decl);
7597 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7599 else if (TREE_CODE (name) == SCOPE_REF
7600 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7602 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7603 complain, in_decl);
7604 name = TREE_OPERAND (name, 1);
7605 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7606 complain, in_decl);
7607 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7608 name = build_nt (SCOPE_REF, base, name);
7610 else if (TREE_CODE (name) == BASELINK)
7611 name = tsubst_baselink (name,
7612 non_reference (TREE_TYPE (object)),
7613 args, complain,
7614 in_decl);
7615 else
7616 name = tsubst_copy (name, args, complain, in_decl);
7617 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7620 case PLUS_EXPR:
7621 case MINUS_EXPR:
7622 case MULT_EXPR:
7623 case TRUNC_DIV_EXPR:
7624 case CEIL_DIV_EXPR:
7625 case FLOOR_DIV_EXPR:
7626 case ROUND_DIV_EXPR:
7627 case EXACT_DIV_EXPR:
7628 case BIT_AND_EXPR:
7629 case BIT_IOR_EXPR:
7630 case BIT_XOR_EXPR:
7631 case TRUNC_MOD_EXPR:
7632 case FLOOR_MOD_EXPR:
7633 case TRUTH_ANDIF_EXPR:
7634 case TRUTH_ORIF_EXPR:
7635 case TRUTH_AND_EXPR:
7636 case TRUTH_OR_EXPR:
7637 case RSHIFT_EXPR:
7638 case LSHIFT_EXPR:
7639 case RROTATE_EXPR:
7640 case LROTATE_EXPR:
7641 case EQ_EXPR:
7642 case NE_EXPR:
7643 case MAX_EXPR:
7644 case MIN_EXPR:
7645 case LE_EXPR:
7646 case GE_EXPR:
7647 case LT_EXPR:
7648 case GT_EXPR:
7649 case ARRAY_REF:
7650 case COMPOUND_EXPR:
7651 case SCOPE_REF:
7652 case DOTSTAR_EXPR:
7653 case MEMBER_REF:
7654 case PREDECREMENT_EXPR:
7655 case PREINCREMENT_EXPR:
7656 case POSTDECREMENT_EXPR:
7657 case POSTINCREMENT_EXPR:
7658 return build_nt
7659 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7660 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7662 case CALL_EXPR:
7663 return build_nt (code,
7664 tsubst_copy (TREE_OPERAND (t, 0), args,
7665 complain, in_decl),
7666 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7667 in_decl),
7668 NULL_TREE);
7670 case COND_EXPR:
7671 case MODOP_EXPR:
7672 case PSEUDO_DTOR_EXPR:
7674 r = build_nt
7675 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7676 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7677 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7678 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7679 return r;
7682 case NEW_EXPR:
7684 r = build_nt
7685 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7686 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7687 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7688 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7689 return r;
7692 case DELETE_EXPR:
7694 r = build_nt
7695 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7696 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7697 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7698 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7699 return r;
7702 case TEMPLATE_ID_EXPR:
7704 /* Substituted template arguments */
7705 tree fn = TREE_OPERAND (t, 0);
7706 tree targs = TREE_OPERAND (t, 1);
7708 fn = tsubst_copy (fn, args, complain, in_decl);
7709 if (targs)
7710 targs = tsubst_template_args (targs, args, complain, in_decl);
7712 return lookup_template_function (fn, targs);
7715 case TREE_LIST:
7717 tree purpose, value, chain;
7719 if (t == void_list_node)
7720 return t;
7722 purpose = TREE_PURPOSE (t);
7723 if (purpose)
7724 purpose = tsubst_copy (purpose, args, complain, in_decl);
7725 value = TREE_VALUE (t);
7726 if (value)
7727 value = tsubst_copy (value, args, complain, in_decl);
7728 chain = TREE_CHAIN (t);
7729 if (chain && chain != void_type_node)
7730 chain = tsubst_copy (chain, args, complain, in_decl);
7731 if (purpose == TREE_PURPOSE (t)
7732 && value == TREE_VALUE (t)
7733 && chain == TREE_CHAIN (t))
7734 return t;
7735 return tree_cons (purpose, value, chain);
7738 case RECORD_TYPE:
7739 case UNION_TYPE:
7740 case ENUMERAL_TYPE:
7741 case INTEGER_TYPE:
7742 case TEMPLATE_TYPE_PARM:
7743 case TEMPLATE_TEMPLATE_PARM:
7744 case BOUND_TEMPLATE_TEMPLATE_PARM:
7745 case TEMPLATE_PARM_INDEX:
7746 case POINTER_TYPE:
7747 case REFERENCE_TYPE:
7748 case OFFSET_TYPE:
7749 case FUNCTION_TYPE:
7750 case METHOD_TYPE:
7751 case ARRAY_TYPE:
7752 case TYPENAME_TYPE:
7753 case UNBOUND_CLASS_TEMPLATE:
7754 case TYPEOF_TYPE:
7755 case TYPE_DECL:
7756 return tsubst (t, args, complain, in_decl);
7758 case IDENTIFIER_NODE:
7759 if (IDENTIFIER_TYPENAME_P (t))
7761 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7762 return mangle_conv_op_name_for_type (new_type);
7764 else
7765 return t;
7767 case CONSTRUCTOR:
7769 r = build_constructor
7770 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7771 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
7772 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7773 return r;
7776 case VA_ARG_EXPR:
7777 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7778 in_decl),
7779 tsubst (TREE_TYPE (t), args, complain, in_decl));
7781 case CLEANUP_POINT_EXPR:
7782 /* We shouldn't have built any of these during initial template
7783 generation. Instead, they should be built during instantiation
7784 in response to the saved STMT_IS_FULL_EXPR_P setting. */
7785 gcc_unreachable ();
7787 default:
7788 return t;
7792 /* Like tsubst_copy for expressions, etc. but also does semantic
7793 processing. */
7795 static tree
7796 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7798 tree stmt, tmp;
7800 if (t == NULL_TREE || t == error_mark_node)
7801 return t;
7803 if (EXPR_HAS_LOCATION (t))
7804 input_location = EXPR_LOCATION (t);
7805 if (STATEMENT_CODE_P (TREE_CODE (t)))
7806 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
7808 switch (TREE_CODE (t))
7810 case STATEMENT_LIST:
7812 tree_stmt_iterator i;
7813 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7814 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
7815 break;
7818 case CTOR_INITIALIZER:
7819 finish_mem_initializers (tsubst_initializer_list
7820 (TREE_OPERAND (t, 0), args));
7821 break;
7823 case RETURN_EXPR:
7824 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
7825 args, complain, in_decl));
7826 break;
7828 case EXPR_STMT:
7829 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
7830 if (EXPR_STMT_STMT_EXPR_RESULT (t))
7831 finish_stmt_expr_expr (tmp, cur_stmt_expr);
7832 else
7833 finish_expr_stmt (tmp);
7834 break;
7836 case USING_STMT:
7837 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7838 args, complain, in_decl));
7839 break;
7841 case DECL_EXPR:
7843 tree decl;
7844 tree init;
7846 decl = DECL_EXPR_DECL (t);
7847 if (TREE_CODE (decl) == LABEL_DECL)
7848 finish_label_decl (DECL_NAME (decl));
7849 else if (TREE_CODE (decl) == USING_DECL)
7851 tree scope = DECL_INITIAL (decl);
7852 tree name = DECL_NAME (decl);
7853 tree decl;
7855 scope = tsubst_expr (scope, args, complain, in_decl);
7856 decl = lookup_qualified_name (scope, name,
7857 /*is_type_p=*/false,
7858 /*complain=*/false);
7859 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
7860 qualified_name_lookup_error (scope, name, decl);
7861 else
7862 do_local_using_decl (decl, scope, name);
7864 else
7866 init = DECL_INITIAL (decl);
7867 decl = tsubst (decl, args, complain, in_decl);
7868 if (decl != error_mark_node)
7870 if (init)
7871 DECL_INITIAL (decl) = error_mark_node;
7872 /* By marking the declaration as instantiated, we avoid
7873 trying to instantiate it. Since instantiate_decl can't
7874 handle local variables, and since we've already done
7875 all that needs to be done, that's the right thing to
7876 do. */
7877 if (TREE_CODE (decl) == VAR_DECL)
7878 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7879 if (TREE_CODE (decl) == VAR_DECL
7880 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7881 /* Anonymous aggregates are a special case. */
7882 finish_anon_union (decl);
7883 else
7885 maybe_push_decl (decl);
7886 if (TREE_CODE (decl) == VAR_DECL
7887 && DECL_PRETTY_FUNCTION_P (decl))
7889 /* For __PRETTY_FUNCTION__ we have to adjust the
7890 initializer. */
7891 const char *const name
7892 = cxx_printable_name (current_function_decl, 2);
7893 init = cp_fname_init (name, &TREE_TYPE (decl));
7895 else
7896 init = tsubst_expr (init, args, complain, in_decl);
7897 cp_finish_decl (decl, init, NULL_TREE, 0);
7902 /* A DECL_EXPR can also be used as an expression, in the condition
7903 clause of an if/for/while construct. */
7904 return decl;
7907 case FOR_STMT:
7908 stmt = begin_for_stmt ();
7909 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7910 finish_for_init_stmt (stmt);
7911 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
7912 finish_for_cond (tmp, stmt);
7913 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7914 finish_for_expr (tmp, stmt);
7915 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7916 finish_for_stmt (stmt);
7917 break;
7919 case WHILE_STMT:
7920 stmt = begin_while_stmt ();
7921 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
7922 finish_while_stmt_cond (tmp, stmt);
7923 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7924 finish_while_stmt (stmt);
7925 break;
7927 case DO_STMT:
7928 stmt = begin_do_stmt ();
7929 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7930 finish_do_body (stmt);
7931 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
7932 finish_do_stmt (tmp, stmt);
7933 break;
7935 case IF_STMT:
7936 stmt = begin_if_stmt ();
7937 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
7938 finish_if_stmt_cond (tmp, stmt);
7939 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
7940 finish_then_clause (stmt);
7942 if (ELSE_CLAUSE (t))
7944 begin_else_clause (stmt);
7945 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
7946 finish_else_clause (stmt);
7949 finish_if_stmt (stmt);
7950 break;
7952 case BIND_EXPR:
7953 if (BIND_EXPR_BODY_BLOCK (t))
7954 stmt = begin_function_body ();
7955 else
7956 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
7957 ? BCS_TRY_BLOCK : 0);
7959 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
7961 if (BIND_EXPR_BODY_BLOCK (t))
7962 finish_function_body (stmt);
7963 else
7964 finish_compound_stmt (stmt);
7965 break;
7967 case BREAK_STMT:
7968 finish_break_stmt ();
7969 break;
7971 case CONTINUE_STMT:
7972 finish_continue_stmt ();
7973 break;
7975 case SWITCH_STMT:
7976 stmt = begin_switch_stmt ();
7977 tmp = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7978 finish_switch_cond (tmp, stmt);
7979 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7980 finish_switch_stmt (stmt);
7981 break;
7983 case CASE_LABEL_EXPR:
7984 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7985 tsubst_expr (CASE_HIGH (t), args, complain,
7986 in_decl));
7987 break;
7989 case LABEL_EXPR:
7990 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
7991 break;
7993 case GOTO_EXPR:
7994 tmp = GOTO_DESTINATION (t);
7995 if (TREE_CODE (tmp) != LABEL_DECL)
7996 /* Computed goto's must be tsubst'd into. On the other hand,
7997 non-computed gotos must not be; the identifier in question
7998 will have no binding. */
7999 tmp = tsubst_expr (tmp, args, complain, in_decl);
8000 else
8001 tmp = DECL_NAME (tmp);
8002 finish_goto_stmt (tmp);
8003 break;
8005 case ASM_EXPR:
8006 tmp = finish_asm_stmt
8007 (ASM_VOLATILE_P (t),
8008 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8009 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
8010 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
8011 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
8012 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
8013 break;
8015 case TRY_BLOCK:
8016 if (CLEANUP_P (t))
8018 stmt = begin_try_block ();
8019 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8020 finish_cleanup_try_block (stmt);
8021 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8022 complain, in_decl),
8023 stmt);
8025 else
8027 if (FN_TRY_BLOCK_P (t))
8028 stmt = begin_function_try_block ();
8029 else
8030 stmt = begin_try_block ();
8032 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8034 if (FN_TRY_BLOCK_P (t))
8035 finish_function_try_block (stmt);
8036 else
8037 finish_try_block (stmt);
8039 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8040 if (FN_TRY_BLOCK_P (t))
8041 finish_function_handler_sequence (stmt);
8042 else
8043 finish_handler_sequence (stmt);
8045 break;
8047 case HANDLER:
8049 tree decl;
8051 stmt = begin_handler ();
8052 if (HANDLER_PARMS (t))
8054 decl = HANDLER_PARMS (t);
8055 decl = tsubst (decl, args, complain, in_decl);
8056 /* Prevent instantiate_decl from trying to instantiate
8057 this variable. We've already done all that needs to be
8058 done. */
8059 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8061 else
8062 decl = NULL_TREE;
8063 finish_handler_parms (decl, stmt);
8064 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8065 finish_handler (stmt);
8067 break;
8069 case TAG_DEFN:
8070 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8071 break;
8073 default:
8074 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8076 return tsubst_copy_and_build (t, args, complain, in_decl,
8077 /*function_p=*/false);
8080 return NULL_TREE;
8083 /* T is a postfix-expression that is not being used in a function
8084 call. Return the substituted version of T. */
8086 static tree
8087 tsubst_non_call_postfix_expression (tree t, tree args,
8088 tsubst_flags_t complain,
8089 tree in_decl)
8091 if (TREE_CODE (t) == SCOPE_REF)
8092 t = tsubst_qualified_id (t, args, complain, in_decl,
8093 /*done=*/false, /*address_p=*/false);
8094 else
8095 t = tsubst_copy_and_build (t, args, complain, in_decl,
8096 /*function_p=*/false);
8098 return t;
8101 /* Like tsubst but deals with expressions and performs semantic
8102 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8104 tree
8105 tsubst_copy_and_build (tree t,
8106 tree args,
8107 tsubst_flags_t complain,
8108 tree in_decl,
8109 bool function_p)
8111 #define RECUR(NODE) \
8112 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8114 tree op1;
8116 if (t == NULL_TREE || t == error_mark_node)
8117 return t;
8119 switch (TREE_CODE (t))
8121 case USING_DECL:
8122 t = DECL_NAME (t);
8123 /* Fall through. */
8124 case IDENTIFIER_NODE:
8126 tree decl;
8127 cp_id_kind idk;
8128 tree qualifying_class;
8129 bool non_integral_constant_expression_p;
8130 const char *error_msg;
8132 if (IDENTIFIER_TYPENAME_P (t))
8134 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8135 t = mangle_conv_op_name_for_type (new_type);
8138 /* Look up the name. */
8139 decl = lookup_name (t, 0);
8141 /* By convention, expressions use ERROR_MARK_NODE to indicate
8142 failure, not NULL_TREE. */
8143 if (decl == NULL_TREE)
8144 decl = error_mark_node;
8146 decl = finish_id_expression (t, decl, NULL_TREE,
8147 &idk,
8148 &qualifying_class,
8149 /*integral_constant_expression_p=*/false,
8150 /*allow_non_integral_constant_expression_p=*/false,
8151 &non_integral_constant_expression_p,
8152 &error_msg);
8153 if (error_msg)
8154 error (error_msg);
8155 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8156 decl = unqualified_name_lookup_error (decl);
8157 return decl;
8160 case TEMPLATE_ID_EXPR:
8162 tree object;
8163 tree template = RECUR (TREE_OPERAND (t, 0));
8164 tree targs = TREE_OPERAND (t, 1);
8166 if (targs)
8167 targs = tsubst_template_args (targs, args, complain, in_decl);
8169 if (TREE_CODE (template) == COMPONENT_REF)
8171 object = TREE_OPERAND (template, 0);
8172 template = TREE_OPERAND (template, 1);
8174 else
8175 object = NULL_TREE;
8176 template = lookup_template_function (template, targs);
8178 if (object)
8179 return build3 (COMPONENT_REF, TREE_TYPE (template),
8180 object, template, NULL_TREE);
8181 else
8182 return template;
8185 case INDIRECT_REF:
8186 return build_x_indirect_ref (RECUR (TREE_OPERAND (t, 0)), "unary *");
8188 case NOP_EXPR:
8189 return build_nop
8190 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8191 RECUR (TREE_OPERAND (t, 0)));
8193 case CAST_EXPR:
8194 return build_functional_cast
8195 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8196 RECUR (TREE_OPERAND (t, 0)));
8198 case REINTERPRET_CAST_EXPR:
8199 return build_reinterpret_cast
8200 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8201 RECUR (TREE_OPERAND (t, 0)));
8203 case CONST_CAST_EXPR:
8204 return build_const_cast
8205 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8206 RECUR (TREE_OPERAND (t, 0)));
8208 case DYNAMIC_CAST_EXPR:
8209 return build_dynamic_cast
8210 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8211 RECUR (TREE_OPERAND (t, 0)));
8213 case STATIC_CAST_EXPR:
8214 return build_static_cast
8215 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8216 RECUR (TREE_OPERAND (t, 0)));
8218 case POSTDECREMENT_EXPR:
8219 case POSTINCREMENT_EXPR:
8220 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8221 args, complain, in_decl);
8222 return build_x_unary_op (TREE_CODE (t), op1);
8224 case PREDECREMENT_EXPR:
8225 case PREINCREMENT_EXPR:
8226 case NEGATE_EXPR:
8227 case BIT_NOT_EXPR:
8228 case ABS_EXPR:
8229 case TRUTH_NOT_EXPR:
8230 case CONVERT_EXPR: /* Unary + */
8231 case REALPART_EXPR:
8232 case IMAGPART_EXPR:
8233 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8235 case ADDR_EXPR:
8236 op1 = TREE_OPERAND (t, 0);
8237 if (TREE_CODE (op1) == SCOPE_REF)
8238 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8239 /*done=*/true, /*address_p=*/true);
8240 else
8241 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8242 in_decl);
8243 if (TREE_CODE (op1) == LABEL_DECL)
8244 return finish_label_address_expr (DECL_NAME (op1));
8245 return build_x_unary_op (ADDR_EXPR, op1);
8247 case PLUS_EXPR:
8248 case MINUS_EXPR:
8249 case MULT_EXPR:
8250 case TRUNC_DIV_EXPR:
8251 case CEIL_DIV_EXPR:
8252 case FLOOR_DIV_EXPR:
8253 case ROUND_DIV_EXPR:
8254 case EXACT_DIV_EXPR:
8255 case BIT_AND_EXPR:
8256 case BIT_IOR_EXPR:
8257 case BIT_XOR_EXPR:
8258 case TRUNC_MOD_EXPR:
8259 case FLOOR_MOD_EXPR:
8260 case TRUTH_ANDIF_EXPR:
8261 case TRUTH_ORIF_EXPR:
8262 case TRUTH_AND_EXPR:
8263 case TRUTH_OR_EXPR:
8264 case RSHIFT_EXPR:
8265 case LSHIFT_EXPR:
8266 case RROTATE_EXPR:
8267 case LROTATE_EXPR:
8268 case EQ_EXPR:
8269 case NE_EXPR:
8270 case MAX_EXPR:
8271 case MIN_EXPR:
8272 case LE_EXPR:
8273 case GE_EXPR:
8274 case LT_EXPR:
8275 case GT_EXPR:
8276 case MEMBER_REF:
8277 case DOTSTAR_EXPR:
8278 return build_x_binary_op
8279 (TREE_CODE (t),
8280 RECUR (TREE_OPERAND (t, 0)),
8281 RECUR (TREE_OPERAND (t, 1)),
8282 /*overloaded_p=*/NULL);
8284 case SCOPE_REF:
8285 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8286 /*address_p=*/false);
8288 case ARRAY_REF:
8289 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8290 == NULL_TREE)
8291 /* new-type-id */
8292 return build_nt (ARRAY_REF, NULL_TREE, RECUR (TREE_OPERAND (t, 1)),
8293 NULL_TREE, NULL_TREE);
8295 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8296 args, complain, in_decl);
8297 /* Remember that there was a reference to this entity. */
8298 if (DECL_P (op1))
8299 mark_used (op1);
8300 return grok_array_decl (op1, RECUR (TREE_OPERAND (t, 1)));
8302 case SIZEOF_EXPR:
8303 case ALIGNOF_EXPR:
8304 op1 = TREE_OPERAND (t, 0);
8305 if (!args)
8307 /* When there are no ARGS, we are trying to evaluate a
8308 non-dependent expression from the parser. Trying to do
8309 the substitutions may not work. */
8310 if (!TYPE_P (op1))
8311 op1 = TREE_TYPE (op1);
8313 else
8315 ++skip_evaluation;
8316 op1 = RECUR (op1);
8317 --skip_evaluation;
8319 if (TYPE_P (op1))
8320 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8321 else
8322 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8324 case MODOP_EXPR:
8326 tree r = build_x_modify_expr
8327 (RECUR (TREE_OPERAND (t, 0)),
8328 TREE_CODE (TREE_OPERAND (t, 1)),
8329 RECUR (TREE_OPERAND (t, 2)));
8330 /* TREE_NO_WARNING must be set if either the expression was
8331 parenthesized or it uses an operator such as >>= rather
8332 than plain assignment. In the former case, it was already
8333 set and must be copied. In the latter case,
8334 build_x_modify_expr sets it and it must not be reset
8335 here. */
8336 if (TREE_NO_WARNING (t))
8337 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8338 return r;
8341 case ARROW_EXPR:
8342 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8343 args, complain, in_decl);
8344 /* Remember that there was a reference to this entity. */
8345 if (DECL_P (op1))
8346 mark_used (op1);
8347 return build_x_arrow (op1);
8349 case NEW_EXPR:
8350 return build_new
8351 (RECUR (TREE_OPERAND (t, 0)),
8352 RECUR (TREE_OPERAND (t, 1)),
8353 RECUR (TREE_OPERAND (t, 2)),
8354 RECUR (TREE_OPERAND (t, 3)),
8355 NEW_EXPR_USE_GLOBAL (t));
8357 case DELETE_EXPR:
8358 return delete_sanity
8359 (RECUR (TREE_OPERAND (t, 0)),
8360 RECUR (TREE_OPERAND (t, 1)),
8361 DELETE_EXPR_USE_VEC (t),
8362 DELETE_EXPR_USE_GLOBAL (t));
8364 case COMPOUND_EXPR:
8365 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8366 RECUR (TREE_OPERAND (t, 1)));
8368 case CALL_EXPR:
8370 tree function;
8371 tree call_args;
8372 bool qualified_p;
8373 bool koenig_p;
8375 function = TREE_OPERAND (t, 0);
8376 /* When we parsed the expression, we determined whether or
8377 not Koenig lookup should be performed. */
8378 koenig_p = KOENIG_LOOKUP_P (t);
8379 if (TREE_CODE (function) == SCOPE_REF)
8381 qualified_p = true;
8382 function = tsubst_qualified_id (function, args, complain, in_decl,
8383 /*done=*/false,
8384 /*address_p=*/false);
8386 else
8388 qualified_p = (TREE_CODE (function) == COMPONENT_REF
8389 && (TREE_CODE (TREE_OPERAND (function, 1))
8390 == SCOPE_REF));
8391 function = tsubst_copy_and_build (function, args, complain,
8392 in_decl,
8393 !qualified_p);
8394 if (BASELINK_P (function))
8395 qualified_p = true;
8398 call_args = RECUR (TREE_OPERAND (t, 1));
8400 /* We do not perform argument-dependent lookup if normal
8401 lookup finds a non-function, in accordance with the
8402 expected resolution of DR 218. */
8403 if (koenig_p
8404 && (is_overloaded_fn (function)
8405 || TREE_CODE (function) == IDENTIFIER_NODE))
8406 function = perform_koenig_lookup (function, call_args);
8408 if (TREE_CODE (function) == IDENTIFIER_NODE)
8410 unqualified_name_lookup_error (function);
8411 return error_mark_node;
8414 /* Remember that there was a reference to this entity. */
8415 if (DECL_P (function))
8416 mark_used (function);
8418 function = convert_from_reference (function);
8420 if (TREE_CODE (function) == OFFSET_REF)
8421 return build_offset_ref_call_from_tree (function, call_args);
8422 if (TREE_CODE (function) == COMPONENT_REF)
8424 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8425 return finish_call_expr (function, call_args,
8426 /*disallow_virtual=*/false,
8427 /*koenig_p=*/false);
8428 else
8429 return (build_new_method_call
8430 (TREE_OPERAND (function, 0),
8431 TREE_OPERAND (function, 1),
8432 call_args, NULL_TREE,
8433 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8435 return finish_call_expr (function, call_args,
8436 /*disallow_virtual=*/qualified_p,
8437 koenig_p);
8440 case COND_EXPR:
8441 return build_x_conditional_expr
8442 (RECUR (TREE_OPERAND (t, 0)),
8443 RECUR (TREE_OPERAND (t, 1)),
8444 RECUR (TREE_OPERAND (t, 2)));
8446 case PSEUDO_DTOR_EXPR:
8447 return finish_pseudo_destructor_expr
8448 (RECUR (TREE_OPERAND (t, 0)),
8449 RECUR (TREE_OPERAND (t, 1)),
8450 RECUR (TREE_OPERAND (t, 2)));
8452 case TREE_LIST:
8454 tree purpose, value, chain;
8456 if (t == void_list_node)
8457 return t;
8459 purpose = TREE_PURPOSE (t);
8460 if (purpose)
8461 purpose = RECUR (purpose);
8462 value = TREE_VALUE (t);
8463 if (value)
8464 value = RECUR (value);
8465 chain = TREE_CHAIN (t);
8466 if (chain && chain != void_type_node)
8467 chain = RECUR (chain);
8468 if (purpose == TREE_PURPOSE (t)
8469 && value == TREE_VALUE (t)
8470 && chain == TREE_CHAIN (t))
8471 return t;
8472 return tree_cons (purpose, value, chain);
8475 case COMPONENT_REF:
8477 tree object;
8478 tree member;
8480 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8481 args, complain, in_decl);
8482 /* Remember that there was a reference to this entity. */
8483 if (DECL_P (object))
8484 mark_used (object);
8486 member = TREE_OPERAND (t, 1);
8487 if (BASELINK_P (member))
8488 member = tsubst_baselink (member,
8489 non_reference (TREE_TYPE (object)),
8490 args, complain, in_decl);
8491 else
8492 member = tsubst_copy (member, args, complain, in_decl);
8494 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8496 if (TREE_CODE (member) == BIT_NOT_EXPR)
8497 return finish_pseudo_destructor_expr (object,
8498 NULL_TREE,
8499 TREE_TYPE (object));
8500 else if (TREE_CODE (member) == SCOPE_REF
8501 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8502 return finish_pseudo_destructor_expr (object,
8503 object,
8504 TREE_TYPE (object));
8506 else if (TREE_CODE (member) == SCOPE_REF
8507 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8509 tree tmpl;
8510 tree args;
8512 /* Lookup the template functions now that we know what the
8513 scope is. */
8514 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8515 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8516 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8517 /*is_type_p=*/false,
8518 /*complain=*/false);
8519 if (BASELINK_P (member))
8520 BASELINK_FUNCTIONS (member)
8521 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8522 args);
8523 else
8525 qualified_name_lookup_error (TREE_TYPE (object), tmpl,
8526 member);
8527 return error_mark_node;
8530 else if (TREE_CODE (member) == SCOPE_REF
8531 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
8532 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
8534 if (complain & tf_error)
8536 if (TYPE_P (TREE_OPERAND (member, 0)))
8537 error ("`%T' is not a class or namespace",
8538 TREE_OPERAND (member, 0));
8539 else
8540 error ("`%D' is not a class or namespace",
8541 TREE_OPERAND (member, 0));
8543 return error_mark_node;
8545 else if (TREE_CODE (member) == FIELD_DECL)
8546 return finish_non_static_data_member (member, object, NULL_TREE);
8548 return finish_class_member_access_expr (object, member);
8551 case THROW_EXPR:
8552 return build_throw
8553 (RECUR (TREE_OPERAND (t, 0)));
8555 case CONSTRUCTOR:
8557 tree r;
8558 tree elts;
8559 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8560 bool purpose_p;
8562 /* digest_init will do the wrong thing if we let it. */
8563 if (type && TYPE_PTRMEMFUNC_P (type))
8564 return t;
8566 r = NULL_TREE;
8567 /* We do not want to process the purpose of aggregate
8568 initializers as they are identifier nodes which will be
8569 looked up by digest_init. */
8570 purpose_p = !(type && IS_AGGR_TYPE (type));
8571 for (elts = CONSTRUCTOR_ELTS (t);
8572 elts;
8573 elts = TREE_CHAIN (elts))
8575 tree purpose = TREE_PURPOSE (elts);
8576 tree value = TREE_VALUE (elts);
8578 if (purpose && purpose_p)
8579 purpose = RECUR (purpose);
8580 value = RECUR (value);
8581 r = tree_cons (purpose, value, r);
8584 r = build_constructor (NULL_TREE, nreverse (r));
8585 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8587 if (type)
8588 return digest_init (type, r, 0);
8589 return r;
8592 case TYPEID_EXPR:
8594 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
8595 if (TYPE_P (operand_0))
8596 return get_typeid (operand_0);
8597 return build_typeid (operand_0);
8600 case PARM_DECL:
8601 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8603 case VAR_DECL:
8604 if (args)
8605 t = tsubst_copy (t, args, complain, in_decl);
8606 return convert_from_reference (t);
8608 case VA_ARG_EXPR:
8609 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
8610 tsubst_copy (TREE_TYPE (t), args, complain,
8611 in_decl));
8613 case OFFSETOF_EXPR:
8614 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
8616 case STMT_EXPR:
8618 tree old_stmt_expr = cur_stmt_expr;
8619 tree stmt_expr = begin_stmt_expr ();
8621 cur_stmt_expr = stmt_expr;
8622 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
8623 stmt_expr = finish_stmt_expr (stmt_expr, false);
8624 cur_stmt_expr = old_stmt_expr;
8626 return stmt_expr;
8629 default:
8630 return tsubst_copy (t, args, complain, in_decl);
8633 #undef RECUR
8636 /* Verify that the instantiated ARGS are valid. For type arguments,
8637 make sure that the type's linkage is ok. For non-type arguments,
8638 make sure they are constants if they are integral or enumerations.
8639 Emit an error under control of COMPLAIN, and return TRUE on error. */
8641 static bool
8642 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
8644 int ix, len = DECL_NTPARMS (tmpl);
8645 bool result = false;
8647 for (ix = 0; ix != len; ix++)
8649 tree t = TREE_VEC_ELT (args, ix);
8651 if (TYPE_P (t))
8653 /* [basic.link]: A name with no linkage (notably, the name
8654 of a class or enumeration declared in a local scope)
8655 shall not be used to declare an entity with linkage.
8656 This implies that names with no linkage cannot be used as
8657 template arguments. */
8658 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
8660 if (nt)
8662 if (!(complain & tf_error))
8663 /*OK*/;
8664 else if (TYPE_ANONYMOUS_P (nt))
8665 error ("`%T' uses anonymous type", t);
8666 else
8667 error ("`%T' uses local type `%T'", t, nt);
8668 result = true;
8670 /* In order to avoid all sorts of complications, we do not
8671 allow variably-modified types as template arguments. */
8672 else if (variably_modified_type_p (t, NULL_TREE))
8674 if (complain & tf_error)
8675 error ("`%T' is a variably modified type", t);
8676 result = true;
8679 /* A non-type argument of integral or enumerated type must be a
8680 constant. */
8681 else if (TREE_TYPE (t)
8682 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
8683 && !TREE_CONSTANT (t))
8685 if (complain & tf_error)
8686 error ("integral expression `%E' is not constant", t);
8687 result = true;
8690 if (result && complain & tf_error)
8691 error (" trying to instantiate `%D'", tmpl);
8692 return result;
8695 /* Instantiate the indicated variable or function template TMPL with
8696 the template arguments in TARG_PTR. */
8698 tree
8699 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8701 tree fndecl;
8702 tree gen_tmpl;
8703 tree spec;
8705 if (tmpl == error_mark_node)
8706 return error_mark_node;
8708 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
8710 /* If this function is a clone, handle it specially. */
8711 if (DECL_CLONED_FUNCTION_P (tmpl))
8713 tree spec;
8714 tree clone;
8716 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
8717 complain);
8718 if (spec == error_mark_node)
8719 return error_mark_node;
8721 /* Look for the clone. */
8722 FOR_EACH_CLONE (clone, spec)
8723 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8724 return clone;
8725 /* We should always have found the clone by now. */
8726 gcc_unreachable ();
8727 return NULL_TREE;
8730 /* Check to see if we already have this specialization. */
8731 spec = retrieve_specialization (tmpl, targ_ptr);
8732 if (spec != NULL_TREE)
8733 return spec;
8735 gen_tmpl = most_general_template (tmpl);
8736 if (tmpl != gen_tmpl)
8738 /* The TMPL is a partial instantiation. To get a full set of
8739 arguments we must add the arguments used to perform the
8740 partial instantiation. */
8741 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8742 targ_ptr);
8744 /* Check to see if we already have this specialization. */
8745 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8746 if (spec != NULL_TREE)
8747 return spec;
8750 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
8751 complain))
8752 return error_mark_node;
8754 /* We are building a FUNCTION_DECL, during which the access of its
8755 parameters and return types have to be checked. However this
8756 FUNCTION_DECL which is the desired context for access checking
8757 is not built yet. We solve this chicken-and-egg problem by
8758 deferring all checks until we have the FUNCTION_DECL. */
8759 push_deferring_access_checks (dk_deferred);
8761 /* Substitute template parameters. */
8762 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8763 targ_ptr, complain, gen_tmpl);
8765 /* Now we know the specialization, compute access previously
8766 deferred. */
8767 push_access_scope (fndecl);
8768 perform_deferred_access_checks ();
8769 pop_access_scope (fndecl);
8770 pop_deferring_access_checks ();
8772 /* The DECL_TI_TEMPLATE should always be the immediate parent
8773 template, not the most general template. */
8774 DECL_TI_TEMPLATE (fndecl) = tmpl;
8776 /* If we've just instantiated the main entry point for a function,
8777 instantiate all the alternate entry points as well. We do this
8778 by cloning the instantiation of the main entry point, not by
8779 instantiating the template clones. */
8780 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8781 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8783 return fndecl;
8786 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8787 arguments that are being used when calling it. TARGS is a vector
8788 into which the deduced template arguments are placed.
8790 Return zero for success, 2 for an incomplete match that doesn't resolve
8791 all the types, and 1 for complete failure. An error message will be
8792 printed only for an incomplete match.
8794 If FN is a conversion operator, or we are trying to produce a specific
8795 specialization, RETURN_TYPE is the return type desired.
8797 The EXPLICIT_TARGS are explicit template arguments provided via a
8798 template-id.
8800 The parameter STRICT is one of:
8802 DEDUCE_CALL:
8803 We are deducing arguments for a function call, as in
8804 [temp.deduct.call].
8806 DEDUCE_CONV:
8807 We are deducing arguments for a conversion function, as in
8808 [temp.deduct.conv].
8810 DEDUCE_EXACT:
8811 We are deducing arguments when doing an explicit instantiation
8812 as in [temp.explicit], when determining an explicit specialization
8813 as in [temp.expl.spec], or when taking the address of a function
8814 template, as in [temp.deduct.funcaddr].
8816 DEDUCE_ORDER:
8817 We are deducing arguments when calculating the partial
8818 ordering between specializations of function or class
8819 templates, as in [temp.func.order] and [temp.class.order].
8821 LEN is the number of parms to consider before returning success, or -1
8822 for all. This is used in partial ordering to avoid comparing parms for
8823 which no actual argument was passed, since they are not considered in
8824 overload resolution (and are explicitly excluded from consideration in
8825 partial ordering in [temp.func.order]/6). */
8828 fn_type_unification (tree fn,
8829 tree explicit_targs,
8830 tree targs,
8831 tree args,
8832 tree return_type,
8833 unification_kind_t strict,
8834 int len)
8836 tree parms;
8837 tree fntype;
8838 int result;
8840 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
8842 fntype = TREE_TYPE (fn);
8843 if (explicit_targs)
8845 /* [temp.deduct]
8847 The specified template arguments must match the template
8848 parameters in kind (i.e., type, nontype, template), and there
8849 must not be more arguments than there are parameters;
8850 otherwise type deduction fails.
8852 Nontype arguments must match the types of the corresponding
8853 nontype template parameters, or must be convertible to the
8854 types of the corresponding nontype parameters as specified in
8855 _temp.arg.nontype_, otherwise type deduction fails.
8857 All references in the function type of the function template
8858 to the corresponding template parameters are replaced by the
8859 specified template argument values. If a substitution in a
8860 template parameter or in the function type of the function
8861 template results in an invalid type, type deduction fails. */
8862 int i;
8863 tree converted_args;
8864 bool incomplete;
8866 converted_args
8867 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8868 explicit_targs, NULL_TREE, tf_none,
8869 /*require_all_arguments=*/0));
8870 if (converted_args == error_mark_node)
8871 return 1;
8873 /* Substitute the explicit args into the function type. This is
8874 necessary so that, for instance, explicitly declared function
8875 arguments can match null pointed constants. If we were given
8876 an incomplete set of explicit args, we must not do semantic
8877 processing during substitution as we could create partial
8878 instantiations. */
8879 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
8880 processing_template_decl += incomplete;
8881 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8882 processing_template_decl -= incomplete;
8884 if (fntype == error_mark_node)
8885 return 1;
8887 /* Place the explicitly specified arguments in TARGS. */
8888 for (i = NUM_TMPL_ARGS (converted_args); i--;)
8889 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8892 parms = TYPE_ARG_TYPES (fntype);
8893 /* Never do unification on the 'this' parameter. */
8894 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8895 parms = TREE_CHAIN (parms);
8897 if (return_type)
8899 /* We've been given a return type to match, prepend it. */
8900 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8901 args = tree_cons (NULL_TREE, return_type, args);
8902 if (len >= 0)
8903 ++len;
8906 /* We allow incomplete unification without an error message here
8907 because the standard doesn't seem to explicitly prohibit it. Our
8908 callers must be ready to deal with unification failures in any
8909 event. */
8910 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8911 targs, parms, args, /*subr=*/0,
8912 strict, /*allow_incomplete*/1, len);
8914 if (result == 0)
8915 /* All is well so far. Now, check:
8917 [temp.deduct]
8919 When all template arguments have been deduced, all uses of
8920 template parameters in nondeduced contexts are replaced with
8921 the corresponding deduced argument values. If the
8922 substitution results in an invalid type, as described above,
8923 type deduction fails. */
8924 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8925 == error_mark_node)
8926 return 1;
8928 return result;
8931 /* Adjust types before performing type deduction, as described in
8932 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8933 sections are symmetric. PARM is the type of a function parameter
8934 or the return type of the conversion function. ARG is the type of
8935 the argument passed to the call, or the type of the value
8936 initialized with the result of the conversion function. */
8938 static int
8939 maybe_adjust_types_for_deduction (unification_kind_t strict,
8940 tree* parm,
8941 tree* arg)
8943 int result = 0;
8945 switch (strict)
8947 case DEDUCE_CALL:
8948 break;
8950 case DEDUCE_CONV:
8952 /* Swap PARM and ARG throughout the remainder of this
8953 function; the handling is precisely symmetric since PARM
8954 will initialize ARG rather than vice versa. */
8955 tree* temp = parm;
8956 parm = arg;
8957 arg = temp;
8958 break;
8961 case DEDUCE_EXACT:
8962 /* There is nothing to do in this case. */
8963 return 0;
8965 case DEDUCE_ORDER:
8966 /* DR 214. [temp.func.order] is underspecified, and leads to no
8967 ordering between things like `T *' and `T const &' for `U *'.
8968 The former has T=U and the latter T=U*. The former looks more
8969 specialized and John Spicer considers it well-formed (the EDG
8970 compiler accepts it).
8972 John also confirms that deduction should proceed as in a function
8973 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8974 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8975 to an actual call can have such a type.
8977 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8978 If only ARG is a REFERENCE_TYPE, we look through that and then
8979 proceed as with DEDUCE_CALL (which could further convert it). */
8980 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8982 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8983 return 0;
8984 *arg = TREE_TYPE (*arg);
8986 break;
8987 default:
8988 gcc_unreachable ();
8991 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8993 /* [temp.deduct.call]
8995 If P is not a reference type:
8997 --If A is an array type, the pointer type produced by the
8998 array-to-pointer standard conversion (_conv.array_) is
8999 used in place of A for type deduction; otherwise,
9001 --If A is a function type, the pointer type produced by
9002 the function-to-pointer standard conversion
9003 (_conv.func_) is used in place of A for type deduction;
9004 otherwise,
9006 --If A is a cv-qualified type, the top level
9007 cv-qualifiers of A's type are ignored for type
9008 deduction. */
9009 if (TREE_CODE (*arg) == ARRAY_TYPE)
9010 *arg = build_pointer_type (TREE_TYPE (*arg));
9011 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9012 *arg = build_pointer_type (*arg);
9013 else
9014 *arg = TYPE_MAIN_VARIANT (*arg);
9017 /* [temp.deduct.call]
9019 If P is a cv-qualified type, the top level cv-qualifiers
9020 of P's type are ignored for type deduction. If P is a
9021 reference type, the type referred to by P is used for
9022 type deduction. */
9023 *parm = TYPE_MAIN_VARIANT (*parm);
9024 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9026 *parm = TREE_TYPE (*parm);
9027 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9030 /* DR 322. For conversion deduction, remove a reference type on parm
9031 too (which has been swapped into ARG). */
9032 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9033 *arg = TREE_TYPE (*arg);
9035 return result;
9038 /* Most parms like fn_type_unification.
9040 If SUBR is 1, we're being called recursively (to unify the
9041 arguments of a function or method parameter of a function
9042 template). */
9044 static int
9045 type_unification_real (tree tparms,
9046 tree targs,
9047 tree xparms,
9048 tree xargs,
9049 int subr,
9050 unification_kind_t strict,
9051 int allow_incomplete,
9052 int xlen)
9054 tree parm, arg;
9055 int i;
9056 int ntparms = TREE_VEC_LENGTH (tparms);
9057 int sub_strict;
9058 int saw_undeduced = 0;
9059 tree parms, args;
9060 int len;
9062 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9063 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9064 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9065 gcc_assert (ntparms > 0);
9067 switch (strict)
9069 case DEDUCE_CALL:
9070 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9071 | UNIFY_ALLOW_DERIVED);
9072 break;
9074 case DEDUCE_CONV:
9075 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9076 break;
9078 case DEDUCE_EXACT:
9079 sub_strict = UNIFY_ALLOW_NONE;
9080 break;
9082 case DEDUCE_ORDER:
9083 sub_strict = UNIFY_ALLOW_NONE;
9084 break;
9086 default:
9087 gcc_unreachable ();
9090 if (xlen == 0)
9091 return 0;
9093 again:
9094 parms = xparms;
9095 args = xargs;
9096 len = xlen;
9098 while (parms
9099 && parms != void_list_node
9100 && args
9101 && args != void_list_node)
9103 parm = TREE_VALUE (parms);
9104 parms = TREE_CHAIN (parms);
9105 arg = TREE_VALUE (args);
9106 args = TREE_CHAIN (args);
9108 if (arg == error_mark_node)
9109 return 1;
9110 if (arg == unknown_type_node)
9111 /* We can't deduce anything from this, but we might get all the
9112 template args from other function args. */
9113 continue;
9115 /* Conversions will be performed on a function argument that
9116 corresponds with a function parameter that contains only
9117 non-deducible template parameters and explicitly specified
9118 template parameters. */
9119 if (!uses_template_parms (parm))
9121 tree type;
9123 if (!TYPE_P (arg))
9124 type = TREE_TYPE (arg);
9125 else
9126 type = arg;
9128 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
9130 if (same_type_p (parm, type))
9131 continue;
9133 else
9134 /* It might work; we shouldn't check now, because we might
9135 get into infinite recursion. Overload resolution will
9136 handle it. */
9137 continue;
9139 return 1;
9142 if (!TYPE_P (arg))
9144 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9145 if (type_unknown_p (arg))
9147 /* [temp.deduct.type] A template-argument can be deduced from
9148 a pointer to function or pointer to member function
9149 argument if the set of overloaded functions does not
9150 contain function templates and at most one of a set of
9151 overloaded functions provides a unique match. */
9153 if (resolve_overloaded_unification
9154 (tparms, targs, parm, arg, strict, sub_strict)
9155 != 0)
9156 return 1;
9157 continue;
9159 arg = TREE_TYPE (arg);
9160 if (arg == error_mark_node)
9161 return 1;
9165 int arg_strict = sub_strict;
9167 if (!subr)
9168 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9170 if (unify (tparms, targs, parm, arg, arg_strict))
9171 return 1;
9174 /* Are we done with the interesting parms? */
9175 if (--len == 0)
9176 goto done;
9178 /* Fail if we've reached the end of the parm list, and more args
9179 are present, and the parm list isn't variadic. */
9180 if (args && args != void_list_node && parms == void_list_node)
9181 return 1;
9182 /* Fail if parms are left and they don't have default values. */
9183 if (parms
9184 && parms != void_list_node
9185 && TREE_PURPOSE (parms) == NULL_TREE)
9186 return 1;
9188 done:
9189 if (!subr)
9190 for (i = 0; i < ntparms; i++)
9191 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
9193 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9195 /* If this is an undeduced nontype parameter that depends on
9196 a type parameter, try another pass; its type may have been
9197 deduced from a later argument than the one from which
9198 this parameter can be deduced. */
9199 if (TREE_CODE (tparm) == PARM_DECL
9200 && uses_template_parms (TREE_TYPE (tparm))
9201 && !saw_undeduced++)
9202 goto again;
9204 if (!allow_incomplete)
9205 error ("incomplete type unification");
9206 return 2;
9208 return 0;
9211 /* Subroutine of type_unification_real. Args are like the variables at the
9212 call site. ARG is an overloaded function (or template-id); we try
9213 deducing template args from each of the overloads, and if only one
9214 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9216 static int
9217 resolve_overloaded_unification (tree tparms,
9218 tree targs,
9219 tree parm,
9220 tree arg,
9221 unification_kind_t strict,
9222 int sub_strict)
9224 tree tempargs = copy_node (targs);
9225 int good = 0;
9226 bool addr_p;
9228 if (TREE_CODE (arg) == ADDR_EXPR)
9230 arg = TREE_OPERAND (arg, 0);
9231 addr_p = true;
9233 else
9234 addr_p = false;
9236 if (TREE_CODE (arg) == COMPONENT_REF)
9237 /* Handle `&x' where `x' is some static or non-static member
9238 function name. */
9239 arg = TREE_OPERAND (arg, 1);
9241 if (TREE_CODE (arg) == OFFSET_REF)
9242 arg = TREE_OPERAND (arg, 1);
9244 /* Strip baselink information. */
9245 if (BASELINK_P (arg))
9246 arg = BASELINK_FUNCTIONS (arg);
9248 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9250 /* If we got some explicit template args, we need to plug them into
9251 the affected templates before we try to unify, in case the
9252 explicit args will completely resolve the templates in question. */
9254 tree expl_subargs = TREE_OPERAND (arg, 1);
9255 arg = TREE_OPERAND (arg, 0);
9257 for (; arg; arg = OVL_NEXT (arg))
9259 tree fn = OVL_CURRENT (arg);
9260 tree subargs, elem;
9262 if (TREE_CODE (fn) != TEMPLATE_DECL)
9263 continue;
9265 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
9266 expl_subargs);
9267 if (subargs)
9269 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9270 good += try_one_overload (tparms, targs, tempargs, parm,
9271 elem, strict, sub_strict, addr_p);
9275 else
9277 gcc_assert (TREE_CODE (arg) == OVERLOAD
9278 || TREE_CODE (arg) == FUNCTION_DECL);
9280 for (; arg; arg = OVL_NEXT (arg))
9281 good += try_one_overload (tparms, targs, tempargs, parm,
9282 TREE_TYPE (OVL_CURRENT (arg)),
9283 strict, sub_strict, addr_p);
9286 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9287 to function or pointer to member function argument if the set of
9288 overloaded functions does not contain function templates and at most
9289 one of a set of overloaded functions provides a unique match.
9291 So if we found multiple possibilities, we return success but don't
9292 deduce anything. */
9294 if (good == 1)
9296 int i = TREE_VEC_LENGTH (targs);
9297 for (; i--; )
9298 if (TREE_VEC_ELT (tempargs, i))
9299 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9301 if (good)
9302 return 0;
9304 return 1;
9307 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9308 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9309 different overloads deduce different arguments for a given parm.
9310 ADDR_P is true if the expression for which deduction is being
9311 performed was of the form "& fn" rather than simply "fn".
9313 Returns 1 on success. */
9315 static int
9316 try_one_overload (tree tparms,
9317 tree orig_targs,
9318 tree targs,
9319 tree parm,
9320 tree arg,
9321 unification_kind_t strict,
9322 int sub_strict,
9323 bool addr_p)
9325 int nargs;
9326 tree tempargs;
9327 int i;
9329 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9330 to function or pointer to member function argument if the set of
9331 overloaded functions does not contain function templates and at most
9332 one of a set of overloaded functions provides a unique match.
9334 So if this is a template, just return success. */
9336 if (uses_template_parms (arg))
9337 return 1;
9339 if (TREE_CODE (arg) == METHOD_TYPE)
9340 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9341 else if (addr_p)
9342 arg = build_pointer_type (arg);
9344 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9346 /* We don't copy orig_targs for this because if we have already deduced
9347 some template args from previous args, unify would complain when we
9348 try to deduce a template parameter for the same argument, even though
9349 there isn't really a conflict. */
9350 nargs = TREE_VEC_LENGTH (targs);
9351 tempargs = make_tree_vec (nargs);
9353 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9354 return 0;
9356 /* First make sure we didn't deduce anything that conflicts with
9357 explicitly specified args. */
9358 for (i = nargs; i--; )
9360 tree elt = TREE_VEC_ELT (tempargs, i);
9361 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9363 if (elt == NULL_TREE)
9364 continue;
9365 else if (uses_template_parms (elt))
9367 /* Since we're unifying against ourselves, we will fill in template
9368 args used in the function parm list with our own template parms.
9369 Discard them. */
9370 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9371 continue;
9373 else if (oldelt && ! template_args_equal (oldelt, elt))
9374 return 0;
9377 for (i = nargs; i--; )
9379 tree elt = TREE_VEC_ELT (tempargs, i);
9381 if (elt)
9382 TREE_VEC_ELT (targs, i) = elt;
9385 return 1;
9388 /* Verify that nondeduce template argument agrees with the type
9389 obtained from argument deduction. Return nonzero if the
9390 verification fails.
9392 For example:
9394 struct A { typedef int X; };
9395 template <class T, class U> struct C {};
9396 template <class T> struct C<T, typename T::X> {};
9398 Then with the instantiation `C<A, int>', we can deduce that
9399 `T' is `A' but unify () does not check whether `typename T::X'
9400 is `int'. This function ensure that they agree.
9402 TARGS, PARMS are the same as the arguments of unify.
9403 ARGS contains template arguments from all levels. */
9405 static int
9406 verify_class_unification (tree targs, tree parms, tree args)
9408 parms = tsubst (parms, add_outermost_template_args (args, targs),
9409 tf_none, NULL_TREE);
9410 if (parms == error_mark_node)
9411 return 1;
9413 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9416 /* PARM is a template class (perhaps with unbound template
9417 parameters). ARG is a fully instantiated type. If ARG can be
9418 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9419 TARGS are as for unify. */
9421 static tree
9422 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9424 tree copy_of_targs;
9426 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9427 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9428 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9429 return NULL_TREE;
9431 /* We need to make a new template argument vector for the call to
9432 unify. If we used TARGS, we'd clutter it up with the result of
9433 the attempted unification, even if this class didn't work out.
9434 We also don't want to commit ourselves to all the unifications
9435 we've already done, since unification is supposed to be done on
9436 an argument-by-argument basis. In other words, consider the
9437 following pathological case:
9439 template <int I, int J, int K>
9440 struct S {};
9442 template <int I, int J>
9443 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9445 template <int I, int J, int K>
9446 void f(S<I, J, K>, S<I, I, I>);
9448 void g() {
9449 S<0, 0, 0> s0;
9450 S<0, 1, 2> s2;
9452 f(s0, s2);
9455 Now, by the time we consider the unification involving `s2', we
9456 already know that we must have `f<0, 0, 0>'. But, even though
9457 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9458 because there are two ways to unify base classes of S<0, 1, 2>
9459 with S<I, I, I>. If we kept the already deduced knowledge, we
9460 would reject the possibility I=1. */
9461 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9463 /* If unification failed, we're done. */
9464 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9465 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9466 return NULL_TREE;
9468 return arg;
9471 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9472 have already discovered to be satisfactory. ARG_BINFO is the binfo
9473 for the base class of ARG that we are currently examining. */
9475 static tree
9476 get_template_base_recursive (tree tparms,
9477 tree targs,
9478 tree parm,
9479 tree arg_binfo,
9480 tree rval,
9481 int flags)
9483 tree base_binfo;
9484 int i;
9485 tree arg = BINFO_TYPE (arg_binfo);
9487 if (!(flags & GTB_IGNORE_TYPE))
9489 tree r = try_class_unification (tparms, targs,
9490 parm, arg);
9492 /* If there is more than one satisfactory baseclass, then:
9494 [temp.deduct.call]
9496 If they yield more than one possible deduced A, the type
9497 deduction fails.
9499 applies. */
9500 if (r && rval && !same_type_p (r, rval))
9501 return error_mark_node;
9502 else if (r)
9503 rval = r;
9506 /* Process base types. */
9507 for (i = 0; BINFO_BASE_ITERATE (arg_binfo, i, base_binfo); i++)
9509 int this_virtual;
9511 /* Skip this base, if we've already seen it. */
9512 if (BINFO_MARKED (base_binfo))
9513 continue;
9515 this_virtual =
9516 (flags & GTB_VIA_VIRTUAL) || BINFO_VIRTUAL_P (base_binfo);
9518 /* When searching for a non-virtual, we cannot mark virtually
9519 found binfos. */
9520 if (! this_virtual)
9521 BINFO_MARKED (base_binfo) = 1;
9523 rval = get_template_base_recursive (tparms, targs,
9524 parm,
9525 base_binfo,
9526 rval,
9527 GTB_VIA_VIRTUAL * this_virtual);
9529 /* If we discovered more than one matching base class, we can
9530 stop now. */
9531 if (rval == error_mark_node)
9532 return error_mark_node;
9535 return rval;
9538 /* Given a template type PARM and a class type ARG, find the unique
9539 base type in ARG that is an instance of PARM. We do not examine
9540 ARG itself; only its base-classes. If there is no appropriate base
9541 class, return NULL_TREE. If there is more than one, return
9542 error_mark_node. PARM may be the type of a partial specialization,
9543 as well as a plain template type. Used by unify. */
9545 static tree
9546 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9548 tree rval;
9549 tree arg_binfo;
9551 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9553 arg_binfo = TYPE_BINFO (complete_type (arg));
9554 if (!arg_binfo)
9555 /* The type could not be completed. */
9556 return NULL_TREE;
9558 rval = get_template_base_recursive (tparms, targs,
9559 parm, arg_binfo,
9560 NULL_TREE,
9561 GTB_IGNORE_TYPE);
9563 /* Since get_template_base_recursive marks the bases classes, we
9564 must unmark them here. */
9565 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9567 return rval;
9570 /* Returns the level of DECL, which declares a template parameter. */
9572 static int
9573 template_decl_level (tree decl)
9575 switch (TREE_CODE (decl))
9577 case TYPE_DECL:
9578 case TEMPLATE_DECL:
9579 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9581 case PARM_DECL:
9582 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9584 default:
9585 gcc_unreachable ();
9587 return 0;
9590 /* Decide whether ARG can be unified with PARM, considering only the
9591 cv-qualifiers of each type, given STRICT as documented for unify.
9592 Returns nonzero iff the unification is OK on that basis. */
9594 static int
9595 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9597 int arg_quals = cp_type_quals (arg);
9598 int parm_quals = cp_type_quals (parm);
9600 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9601 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9603 /* Although a CVR qualifier is ignored when being applied to a
9604 substituted template parameter ([8.3.2]/1 for example), that
9605 does not apply during deduction [14.8.2.4]/1, (even though
9606 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9607 this). Except when we're allowing additional CV qualifiers
9608 at the outer level [14.8.2.1]/3,1st bullet. */
9609 if ((TREE_CODE (arg) == REFERENCE_TYPE
9610 || TREE_CODE (arg) == FUNCTION_TYPE
9611 || TREE_CODE (arg) == METHOD_TYPE)
9612 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9613 return 0;
9615 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9616 && (parm_quals & TYPE_QUAL_RESTRICT))
9617 return 0;
9620 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9621 && (arg_quals & parm_quals) != parm_quals)
9622 return 0;
9624 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9625 && (parm_quals & arg_quals) != arg_quals)
9626 return 0;
9628 return 1;
9631 /* Takes parameters as for type_unification. Returns 0 if the
9632 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9633 bitwise or of the following flags:
9635 UNIFY_ALLOW_NONE:
9636 Require an exact match between PARM and ARG.
9637 UNIFY_ALLOW_MORE_CV_QUAL:
9638 Allow the deduced ARG to be more cv-qualified (by qualification
9639 conversion) than ARG.
9640 UNIFY_ALLOW_LESS_CV_QUAL:
9641 Allow the deduced ARG to be less cv-qualified than ARG.
9642 UNIFY_ALLOW_DERIVED:
9643 Allow the deduced ARG to be a template base class of ARG,
9644 or a pointer to a template base class of the type pointed to by
9645 ARG.
9646 UNIFY_ALLOW_INTEGER:
9647 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9648 case for more information.
9649 UNIFY_ALLOW_OUTER_LEVEL:
9650 This is the outermost level of a deduction. Used to determine validity
9651 of qualification conversions. A valid qualification conversion must
9652 have const qualified pointers leading up to the inner type which
9653 requires additional CV quals, except at the outer level, where const
9654 is not required [conv.qual]. It would be normal to set this flag in
9655 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9656 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9657 This is the outermost level of a deduction, and PARM can be more CV
9658 qualified at this point.
9659 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9660 This is the outermost level of a deduction, and PARM can be less CV
9661 qualified at this point. */
9663 static int
9664 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
9666 int idx;
9667 tree targ;
9668 tree tparm;
9669 int strict_in = strict;
9671 /* I don't think this will do the right thing with respect to types.
9672 But the only case I've seen it in so far has been array bounds, where
9673 signedness is the only information lost, and I think that will be
9674 okay. */
9675 while (TREE_CODE (parm) == NOP_EXPR)
9676 parm = TREE_OPERAND (parm, 0);
9678 if (arg == error_mark_node)
9679 return 1;
9680 if (arg == unknown_type_node)
9681 /* We can't deduce anything from this, but we might get all the
9682 template args from other function args. */
9683 return 0;
9685 /* If PARM uses template parameters, then we can't bail out here,
9686 even if ARG == PARM, since we won't record unifications for the
9687 template parameters. We might need them if we're trying to
9688 figure out which of two things is more specialized. */
9689 if (arg == parm && !uses_template_parms (parm))
9690 return 0;
9692 /* Immediately reject some pairs that won't unify because of
9693 cv-qualification mismatches. */
9694 if (TREE_CODE (arg) == TREE_CODE (parm)
9695 && TYPE_P (arg)
9696 /* It is the elements of the array which hold the cv quals of an array
9697 type, and the elements might be template type parms. We'll check
9698 when we recurse. */
9699 && TREE_CODE (arg) != ARRAY_TYPE
9700 /* We check the cv-qualifiers when unifying with template type
9701 parameters below. We want to allow ARG `const T' to unify with
9702 PARM `T' for example, when computing which of two templates
9703 is more specialized, for example. */
9704 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9705 && !check_cv_quals_for_unify (strict_in, arg, parm))
9706 return 1;
9708 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9709 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9710 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9711 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9712 strict &= ~UNIFY_ALLOW_DERIVED;
9713 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9714 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9716 switch (TREE_CODE (parm))
9718 case TYPENAME_TYPE:
9719 case SCOPE_REF:
9720 case UNBOUND_CLASS_TEMPLATE:
9721 /* In a type which contains a nested-name-specifier, template
9722 argument values cannot be deduced for template parameters used
9723 within the nested-name-specifier. */
9724 return 0;
9726 case TEMPLATE_TYPE_PARM:
9727 case TEMPLATE_TEMPLATE_PARM:
9728 case BOUND_TEMPLATE_TEMPLATE_PARM:
9729 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9731 if (TEMPLATE_TYPE_LEVEL (parm)
9732 != template_decl_level (tparm))
9733 /* The PARM is not one we're trying to unify. Just check
9734 to see if it matches ARG. */
9735 return (TREE_CODE (arg) == TREE_CODE (parm)
9736 && same_type_p (parm, arg)) ? 0 : 1;
9737 idx = TEMPLATE_TYPE_IDX (parm);
9738 targ = TREE_VEC_ELT (targs, idx);
9739 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9741 /* Check for mixed types and values. */
9742 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9743 && TREE_CODE (tparm) != TYPE_DECL)
9744 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9745 && TREE_CODE (tparm) != TEMPLATE_DECL))
9746 return 1;
9748 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9750 /* ARG must be constructed from a template class or a template
9751 template parameter. */
9752 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9753 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9754 return 1;
9757 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9758 tree parmvec = TYPE_TI_ARGS (parm);
9759 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
9760 tree argtmplvec
9761 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9762 int i;
9764 /* The parameter and argument roles have to be switched here
9765 in order to handle default arguments properly. For example,
9766 template<template <class> class TT> void f(TT<int>)
9767 should be able to accept vector<int> which comes from
9768 template <class T, class Allocator = allocator>
9769 class vector. */
9771 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9772 == error_mark_node)
9773 return 1;
9775 /* Deduce arguments T, i from TT<T> or TT<i>.
9776 We check each element of PARMVEC and ARGVEC individually
9777 rather than the whole TREE_VEC since they can have
9778 different number of elements. */
9780 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9782 if (unify (tparms, targs,
9783 TREE_VEC_ELT (parmvec, i),
9784 TREE_VEC_ELT (argvec, i),
9785 UNIFY_ALLOW_NONE))
9786 return 1;
9789 arg = TYPE_TI_TEMPLATE (arg);
9791 /* Fall through to deduce template name. */
9794 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9795 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9797 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9799 /* Simple cases: Value already set, does match or doesn't. */
9800 if (targ != NULL_TREE && template_args_equal (targ, arg))
9801 return 0;
9802 else if (targ)
9803 return 1;
9805 else
9807 /* If PARM is `const T' and ARG is only `int', we don't have
9808 a match unless we are allowing additional qualification.
9809 If ARG is `const int' and PARM is just `T' that's OK;
9810 that binds `const int' to `T'. */
9811 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9812 arg, parm))
9813 return 1;
9815 /* Consider the case where ARG is `const volatile int' and
9816 PARM is `const T'. Then, T should be `volatile int'. */
9817 arg = cp_build_qualified_type_real
9818 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9819 if (arg == error_mark_node)
9820 return 1;
9822 /* Simple cases: Value already set, does match or doesn't. */
9823 if (targ != NULL_TREE && same_type_p (targ, arg))
9824 return 0;
9825 else if (targ)
9826 return 1;
9828 /* Make sure that ARG is not a variable-sized array. (Note
9829 that were talking about variable-sized arrays (like
9830 `int[n]'), rather than arrays of unknown size (like
9831 `int[]').) We'll get very confused by such a type since
9832 the bound of the array will not be computable in an
9833 instantiation. Besides, such types are not allowed in
9834 ISO C++, so we can do as we please here. */
9835 if (variably_modified_type_p (arg, NULL_TREE))
9836 return 1;
9839 TREE_VEC_ELT (targs, idx) = arg;
9840 return 0;
9842 case TEMPLATE_PARM_INDEX:
9843 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9845 if (TEMPLATE_PARM_LEVEL (parm)
9846 != template_decl_level (tparm))
9847 /* The PARM is not one we're trying to unify. Just check
9848 to see if it matches ARG. */
9849 return !(TREE_CODE (arg) == TREE_CODE (parm)
9850 && cp_tree_equal (parm, arg));
9852 idx = TEMPLATE_PARM_IDX (parm);
9853 targ = TREE_VEC_ELT (targs, idx);
9855 if (targ)
9856 return !cp_tree_equal (targ, arg);
9858 /* [temp.deduct.type] If, in the declaration of a function template
9859 with a non-type template-parameter, the non-type
9860 template-parameter is used in an expression in the function
9861 parameter-list and, if the corresponding template-argument is
9862 deduced, the template-argument type shall match the type of the
9863 template-parameter exactly, except that a template-argument
9864 deduced from an array bound may be of any integral type.
9865 The non-type parameter might use already deduced type parameters. */
9866 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9867 if (!TREE_TYPE (arg))
9868 /* Template-parameter dependent expression. Just accept it for now.
9869 It will later be processed in convert_template_argument. */
9871 else if (same_type_p (TREE_TYPE (arg), tparm))
9872 /* OK */;
9873 else if ((strict & UNIFY_ALLOW_INTEGER)
9874 && (TREE_CODE (tparm) == INTEGER_TYPE
9875 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9876 /* Convert the ARG to the type of PARM; the deduced non-type
9877 template argument must exactly match the types of the
9878 corresponding parameter. */
9879 arg = fold (build_nop (TREE_TYPE (parm), arg));
9880 else if (uses_template_parms (tparm))
9881 /* We haven't deduced the type of this parameter yet. Try again
9882 later. */
9883 return 0;
9884 else
9885 return 1;
9887 TREE_VEC_ELT (targs, idx) = arg;
9888 return 0;
9890 case PTRMEM_CST:
9892 /* A pointer-to-member constant can be unified only with
9893 another constant. */
9894 if (TREE_CODE (arg) != PTRMEM_CST)
9895 return 1;
9897 /* Just unify the class member. It would be useless (and possibly
9898 wrong, depending on the strict flags) to unify also
9899 PTRMEM_CST_CLASS, because we want to be sure that both parm and
9900 arg refer to the same variable, even if through different
9901 classes. For instance:
9903 struct A { int x; };
9904 struct B : A { };
9906 Unification of &A::x and &B::x must succeed. */
9907 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
9908 PTRMEM_CST_MEMBER (arg), strict);
9911 case POINTER_TYPE:
9913 if (TREE_CODE (arg) != POINTER_TYPE)
9914 return 1;
9916 /* [temp.deduct.call]
9918 A can be another pointer or pointer to member type that can
9919 be converted to the deduced A via a qualification
9920 conversion (_conv.qual_).
9922 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9923 This will allow for additional cv-qualification of the
9924 pointed-to types if appropriate. */
9926 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9927 /* The derived-to-base conversion only persists through one
9928 level of pointers. */
9929 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9931 return unify (tparms, targs, TREE_TYPE (parm),
9932 TREE_TYPE (arg), strict);
9935 case REFERENCE_TYPE:
9936 if (TREE_CODE (arg) != REFERENCE_TYPE)
9937 return 1;
9938 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9939 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9941 case ARRAY_TYPE:
9942 if (TREE_CODE (arg) != ARRAY_TYPE)
9943 return 1;
9944 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9945 != (TYPE_DOMAIN (arg) == NULL_TREE))
9946 return 1;
9947 if (TYPE_DOMAIN (parm) != NULL_TREE)
9949 tree parm_max;
9950 tree arg_max;
9952 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
9953 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
9955 /* Our representation of array types uses "N - 1" as the
9956 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
9957 not an integer constant. */
9958 if (TREE_CODE (parm_max) == MINUS_EXPR)
9960 arg_max = fold (build2 (PLUS_EXPR,
9961 integer_type_node,
9962 arg_max,
9963 TREE_OPERAND (parm_max, 1)));
9964 parm_max = TREE_OPERAND (parm_max, 0);
9967 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
9968 return 1;
9970 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9971 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9973 case REAL_TYPE:
9974 case COMPLEX_TYPE:
9975 case VECTOR_TYPE:
9976 case INTEGER_TYPE:
9977 case BOOLEAN_TYPE:
9978 case ENUMERAL_TYPE:
9979 case VOID_TYPE:
9980 if (TREE_CODE (arg) != TREE_CODE (parm))
9981 return 1;
9983 /* We have already checked cv-qualification at the top of the
9984 function. */
9985 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9986 return 1;
9988 /* As far as unification is concerned, this wins. Later checks
9989 will invalidate it if necessary. */
9990 return 0;
9992 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9993 /* Type INTEGER_CST can come from ordinary constant template args. */
9994 case INTEGER_CST:
9995 while (TREE_CODE (arg) == NOP_EXPR)
9996 arg = TREE_OPERAND (arg, 0);
9998 if (TREE_CODE (arg) != INTEGER_CST)
9999 return 1;
10000 return !tree_int_cst_equal (parm, arg);
10002 case TREE_VEC:
10004 int i;
10005 if (TREE_CODE (arg) != TREE_VEC)
10006 return 1;
10007 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10008 return 1;
10009 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10010 if (unify (tparms, targs,
10011 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10012 UNIFY_ALLOW_NONE))
10013 return 1;
10014 return 0;
10017 case RECORD_TYPE:
10018 case UNION_TYPE:
10019 if (TREE_CODE (arg) != TREE_CODE (parm))
10020 return 1;
10022 if (TYPE_PTRMEMFUNC_P (parm))
10024 if (!TYPE_PTRMEMFUNC_P (arg))
10025 return 1;
10027 return unify (tparms, targs,
10028 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10029 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10030 strict);
10033 if (CLASSTYPE_TEMPLATE_INFO (parm))
10035 tree t = NULL_TREE;
10037 if (strict_in & UNIFY_ALLOW_DERIVED)
10039 /* First, we try to unify the PARM and ARG directly. */
10040 t = try_class_unification (tparms, targs,
10041 parm, arg);
10043 if (!t)
10045 /* Fallback to the special case allowed in
10046 [temp.deduct.call]:
10048 If P is a class, and P has the form
10049 template-id, then A can be a derived class of
10050 the deduced A. Likewise, if P is a pointer to
10051 a class of the form template-id, A can be a
10052 pointer to a derived class pointed to by the
10053 deduced A. */
10054 t = get_template_base (tparms, targs,
10055 parm, arg);
10057 if (! t || t == error_mark_node)
10058 return 1;
10061 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10062 && (CLASSTYPE_TI_TEMPLATE (parm)
10063 == CLASSTYPE_TI_TEMPLATE (arg)))
10064 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10065 Then, we should unify `int' and `U'. */
10066 t = arg;
10067 else
10068 /* There's no chance of unification succeeding. */
10069 return 1;
10071 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10072 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10074 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10075 return 1;
10076 return 0;
10078 case METHOD_TYPE:
10079 case FUNCTION_TYPE:
10080 if (TREE_CODE (arg) != TREE_CODE (parm))
10081 return 1;
10083 if (unify (tparms, targs, TREE_TYPE (parm),
10084 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10085 return 1;
10086 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10087 TYPE_ARG_TYPES (arg), 1,
10088 DEDUCE_EXACT, 0, -1);
10090 case OFFSET_TYPE:
10091 if (TREE_CODE (arg) != OFFSET_TYPE)
10092 return 1;
10093 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10094 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10095 return 1;
10096 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10097 strict);
10099 case CONST_DECL:
10100 if (DECL_TEMPLATE_PARM_P (parm))
10101 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10102 if (arg != decl_constant_value (parm))
10103 return 1;
10104 return 0;
10106 case FIELD_DECL:
10107 case TEMPLATE_DECL:
10108 /* Matched cases are handled by the ARG == PARM test above. */
10109 return 1;
10111 default:
10112 gcc_assert (EXPR_P (parm));
10114 /* We must be looking at an expression. This can happen with
10115 something like:
10117 template <int I>
10118 void foo(S<I>, S<I + 2>);
10120 This is a "nondeduced context":
10122 [deduct.type]
10124 The nondeduced contexts are:
10126 --A type that is a template-id in which one or more of
10127 the template-arguments is an expression that references
10128 a template-parameter.
10130 In these cases, we assume deduction succeeded, but don't
10131 actually infer any unifications. */
10133 if (!uses_template_parms (parm)
10134 && !template_args_equal (parm, arg))
10135 return 1;
10136 else
10137 return 0;
10141 /* Note that DECL can be defined in this translation unit, if
10142 required. */
10144 static void
10145 mark_definable (tree decl)
10147 tree clone;
10148 DECL_NOT_REALLY_EXTERN (decl) = 1;
10149 FOR_EACH_CLONE (clone, decl)
10150 DECL_NOT_REALLY_EXTERN (clone) = 1;
10153 /* Called if RESULT is explicitly instantiated, or is a member of an
10154 explicitly instantiated class. */
10156 void
10157 mark_decl_instantiated (tree result, int extern_p)
10159 SET_DECL_EXPLICIT_INSTANTIATION (result);
10161 /* If this entity has already been written out, it's too late to
10162 make any modifications. */
10163 if (TREE_ASM_WRITTEN (result))
10164 return;
10166 if (TREE_CODE (result) != FUNCTION_DECL)
10167 /* The TREE_PUBLIC flag for function declarations will have been
10168 set correctly by tsubst. */
10169 TREE_PUBLIC (result) = 1;
10171 /* This might have been set by an earlier implicit instantiation. */
10172 DECL_COMDAT (result) = 0;
10174 if (extern_p)
10175 DECL_NOT_REALLY_EXTERN (result) = 0;
10176 else
10178 mark_definable (result);
10179 /* Always make artificials weak. */
10180 if (DECL_ARTIFICIAL (result) && flag_weak)
10181 comdat_linkage (result);
10182 /* For WIN32 we also want to put explicit instantiations in
10183 linkonce sections. */
10184 else if (TREE_PUBLIC (result))
10185 maybe_make_one_only (result);
10188 /* If EXTERN_P, then this function will not be emitted -- unless
10189 followed by an explicit instantiation, at which point its linkage
10190 will be adjusted. If !EXTERN_P, then this function will be
10191 emitted here. In neither circumstance do we want
10192 import_export_decl to adjust the linkage. */
10193 DECL_INTERFACE_KNOWN (result) = 1;
10196 /* Given two function templates PAT1 and PAT2, return:
10198 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
10200 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10201 -1 if PAT2 is more specialized than PAT1.
10202 0 if neither is more specialized.
10204 LEN is passed through to fn_type_unification. */
10207 more_specialized (tree pat1, tree pat2, int deduce, int len)
10209 tree targs;
10210 int winner = 0;
10212 /* If template argument deduction succeeds, we substitute the
10213 resulting arguments into non-deduced contexts. While doing that,
10214 we must be aware that we may encounter dependent types. */
10215 ++processing_template_decl;
10216 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
10217 NULL_TREE, 0, deduce, len);
10218 if (targs)
10219 --winner;
10221 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
10222 NULL_TREE, 0, deduce, len);
10223 if (targs)
10224 ++winner;
10225 --processing_template_decl;
10227 return winner;
10230 /* Given two class template specialization list nodes PAT1 and PAT2, return:
10232 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
10233 -1 if PAT2 is more specialized than PAT1.
10234 0 if neither is more specialized.
10236 FULL_ARGS is the full set of template arguments that triggers this
10237 partial ordering. */
10240 more_specialized_class (tree pat1, tree pat2, tree full_args)
10242 tree targs;
10243 int winner = 0;
10245 /* Just like what happens for functions, if we are ordering between
10246 different class template specializations, we may encounter dependent
10247 types in the arguments, and we need our dependency check functions
10248 to behave correctly. */
10249 ++processing_template_decl;
10250 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
10251 add_outermost_template_args (full_args, TREE_PURPOSE (pat2)));
10252 if (targs)
10253 --winner;
10255 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
10256 add_outermost_template_args (full_args, TREE_PURPOSE (pat1)));
10257 if (targs)
10258 ++winner;
10259 --processing_template_decl;
10261 return winner;
10264 /* Return the template arguments that will produce the function signature
10265 DECL from the function template FN, with the explicit template
10266 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
10267 also match. Return NULL_TREE if no satisfactory arguments could be
10268 found. DEDUCE and LEN are passed through to fn_type_unification. */
10270 static tree
10271 get_bindings_real (tree fn,
10272 tree decl,
10273 tree explicit_args,
10274 int check_rettype,
10275 int deduce,
10276 int len)
10278 int ntparms = DECL_NTPARMS (fn);
10279 tree targs = make_tree_vec (ntparms);
10280 tree decl_type;
10281 tree decl_arg_types;
10282 int i;
10284 /* Substitute the explicit template arguments into the type of DECL.
10285 The call to fn_type_unification will handle substitution into the
10286 FN. */
10287 decl_type = TREE_TYPE (decl);
10288 if (explicit_args && uses_template_parms (decl_type))
10290 tree tmpl;
10291 tree converted_args;
10293 if (DECL_TEMPLATE_INFO (decl))
10294 tmpl = DECL_TI_TEMPLATE (decl);
10295 else
10296 /* We can get here for some invalid specializations. */
10297 return NULL_TREE;
10299 converted_args
10300 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10301 explicit_args, NULL_TREE,
10302 tf_none, /*require_all_arguments=*/0));
10303 if (converted_args == error_mark_node)
10304 return NULL_TREE;
10306 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10307 if (decl_type == error_mark_node)
10308 return NULL_TREE;
10311 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10312 /* Never do unification on the 'this' parameter. */
10313 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10314 decl_arg_types = TREE_CHAIN (decl_arg_types);
10316 i = fn_type_unification (fn, explicit_args, targs,
10317 decl_arg_types,
10318 (check_rettype || DECL_CONV_FN_P (fn)
10319 ? TREE_TYPE (decl_type) : NULL_TREE),
10320 deduce, len);
10322 if (i != 0)
10323 return NULL_TREE;
10325 return targs;
10328 /* For most uses, we want to check the return type. */
10330 static tree
10331 get_bindings (tree fn, tree decl, tree explicit_args)
10333 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
10336 /* But for resolve_overloaded_unification, we only care about the parameter
10337 types. */
10339 static tree
10340 get_bindings_overload (tree fn, tree decl, tree explicit_args)
10342 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
10345 /* Return the innermost template arguments that, when applied to a
10346 template specialization whose innermost template parameters are
10347 TPARMS, and whose specialization arguments are PARMS, yield the
10348 ARGS.
10350 For example, suppose we have:
10352 template <class T, class U> struct S {};
10353 template <class T> struct S<T*, int> {};
10355 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10356 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10357 int}. The resulting vector will be {double}, indicating that `T'
10358 is bound to `double'. */
10360 static tree
10361 get_class_bindings (tree tparms, tree parms, tree args)
10363 int i, ntparms = TREE_VEC_LENGTH (tparms);
10364 tree vec = make_tree_vec (ntparms);
10366 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10367 UNIFY_ALLOW_NONE))
10368 return NULL_TREE;
10370 for (i = 0; i < ntparms; ++i)
10371 if (! TREE_VEC_ELT (vec, i))
10372 return NULL_TREE;
10374 if (verify_class_unification (vec, parms, args))
10375 return NULL_TREE;
10377 return vec;
10380 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10381 Pick the most specialized template, and return the corresponding
10382 instantiation, or if there is no corresponding instantiation, the
10383 template itself. If there is no most specialized template,
10384 error_mark_node is returned. If there are no templates at all,
10385 NULL_TREE is returned. */
10387 tree
10388 most_specialized_instantiation (tree instantiations)
10390 tree fn, champ;
10391 int fate;
10393 if (!instantiations)
10394 return NULL_TREE;
10396 champ = instantiations;
10397 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10399 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10400 DEDUCE_EXACT, -1);
10401 if (fate == 1)
10403 else
10405 if (fate == 0)
10407 fn = TREE_CHAIN (fn);
10408 if (! fn)
10409 return error_mark_node;
10411 champ = fn;
10415 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10417 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10418 DEDUCE_EXACT, -1);
10419 if (fate != 1)
10420 return error_mark_node;
10423 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10426 /* Return the most specialized of the list of templates in FNS that can
10427 produce an instantiation matching DECL, given the explicit template
10428 arguments EXPLICIT_ARGS. */
10430 static tree
10431 most_specialized (tree fns, tree decl, tree explicit_args)
10433 tree candidates = NULL_TREE;
10434 tree fn, args;
10436 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10438 tree candidate = TREE_VALUE (fn);
10440 args = get_bindings (candidate, decl, explicit_args);
10441 if (args)
10442 candidates = tree_cons (NULL_TREE, candidate, candidates);
10445 return most_specialized_instantiation (candidates);
10448 /* If DECL is a specialization of some template, return the most
10449 general such template. Otherwise, returns NULL_TREE.
10451 For example, given:
10453 template <class T> struct S { template <class U> void f(U); };
10455 if TMPL is `template <class U> void S<int>::f(U)' this will return
10456 the full template. This function will not trace past partial
10457 specializations, however. For example, given in addition:
10459 template <class T> struct S<T*> { template <class U> void f(U); };
10461 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10462 `template <class T> template <class U> S<T*>::f(U)'. */
10464 tree
10465 most_general_template (tree decl)
10467 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10468 an immediate specialization. */
10469 if (TREE_CODE (decl) == FUNCTION_DECL)
10471 if (DECL_TEMPLATE_INFO (decl)) {
10472 decl = DECL_TI_TEMPLATE (decl);
10474 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10475 template friend. */
10476 if (TREE_CODE (decl) != TEMPLATE_DECL)
10477 return NULL_TREE;
10478 } else
10479 return NULL_TREE;
10482 /* Look for more and more general templates. */
10483 while (DECL_TEMPLATE_INFO (decl))
10485 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
10486 (See cp-tree.h for details.) */
10487 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10488 break;
10490 if (CLASS_TYPE_P (TREE_TYPE (decl))
10491 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10492 break;
10494 /* Stop if we run into an explicitly specialized class template. */
10495 if (!DECL_NAMESPACE_SCOPE_P (decl)
10496 && DECL_CONTEXT (decl)
10497 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10498 break;
10500 decl = DECL_TI_TEMPLATE (decl);
10503 return decl;
10506 /* Return the most specialized of the class template specializations
10507 of TMPL which can produce an instantiation matching ARGS, or
10508 error_mark_node if the choice is ambiguous. */
10510 static tree
10511 most_specialized_class (tree tmpl, tree args)
10513 tree list = NULL_TREE;
10514 tree t;
10515 tree champ;
10516 int fate;
10518 tmpl = most_general_template (tmpl);
10519 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10521 tree spec_args
10522 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10523 if (spec_args)
10525 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10526 TREE_TYPE (list) = TREE_TYPE (t);
10530 if (! list)
10531 return NULL_TREE;
10533 t = list;
10534 champ = t;
10535 t = TREE_CHAIN (t);
10536 for (; t; t = TREE_CHAIN (t))
10538 fate = more_specialized_class (champ, t, args);
10539 if (fate == 1)
10541 else
10543 if (fate == 0)
10545 t = TREE_CHAIN (t);
10546 if (! t)
10547 return error_mark_node;
10549 champ = t;
10553 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10555 fate = more_specialized_class (champ, t, args);
10556 if (fate != 1)
10557 return error_mark_node;
10560 return champ;
10563 /* Explicitly instantiate DECL. */
10565 void
10566 do_decl_instantiation (tree decl, tree storage)
10568 tree result = NULL_TREE;
10569 int extern_p = 0;
10571 if (!decl)
10572 /* An error occurred, for which grokdeclarator has already issued
10573 an appropriate message. */
10574 return;
10575 else if (! DECL_LANG_SPECIFIC (decl))
10577 error ("explicit instantiation of non-template `%#D'", decl);
10578 return;
10580 else if (TREE_CODE (decl) == VAR_DECL)
10582 /* There is an asymmetry here in the way VAR_DECLs and
10583 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10584 the latter, the DECL we get back will be marked as a
10585 template instantiation, and the appropriate
10586 DECL_TEMPLATE_INFO will be set up. This does not happen for
10587 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10588 should handle VAR_DECLs as it currently handles
10589 FUNCTION_DECLs. */
10590 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10591 if (!result || TREE_CODE (result) != VAR_DECL)
10593 error ("no matching template for `%D' found", decl);
10594 return;
10597 else if (TREE_CODE (decl) != FUNCTION_DECL)
10599 error ("explicit instantiation of `%#D'", decl);
10600 return;
10602 else
10603 result = decl;
10605 /* Check for various error cases. Note that if the explicit
10606 instantiation is valid the RESULT will currently be marked as an
10607 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10608 until we get here. */
10610 if (DECL_TEMPLATE_SPECIALIZATION (result))
10612 /* DR 259 [temp.spec].
10614 Both an explicit instantiation and a declaration of an explicit
10615 specialization shall not appear in a program unless the explicit
10616 instantiation follows a declaration of the explicit specialization.
10618 For a given set of template parameters, if an explicit
10619 instantiation of a template appears after a declaration of an
10620 explicit specialization for that template, the explicit
10621 instantiation has no effect. */
10622 return;
10624 else if (DECL_EXPLICIT_INSTANTIATION (result))
10626 /* [temp.spec]
10628 No program shall explicitly instantiate any template more
10629 than once.
10631 We check DECL_NOT_REALLY_EXTERN so as not to complain when
10632 the first instantiation was `extern' and the second is not,
10633 and EXTERN_P for the opposite case. */
10634 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
10635 pedwarn ("duplicate explicit instantiation of `%#D'", result);
10636 /* If an "extern" explicit instantiation follows an ordinary
10637 explicit instantiation, the template is instantiated. */
10638 if (extern_p)
10639 return;
10641 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10643 error ("no matching template for `%D' found", result);
10644 return;
10646 else if (!DECL_TEMPLATE_INFO (result))
10648 pedwarn ("explicit instantiation of non-template `%#D'", result);
10649 return;
10652 if (storage == NULL_TREE)
10654 else if (storage == ridpointers[(int) RID_EXTERN])
10656 if (pedantic && !in_system_header)
10657 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10658 extern_p = 1;
10660 else
10661 error ("storage class `%D' applied to template instantiation",
10662 storage);
10664 mark_decl_instantiated (result, extern_p);
10665 if (! extern_p)
10666 instantiate_decl (result, /*defer_ok=*/1, /*undefined_ok=*/0);
10669 void
10670 mark_class_instantiated (tree t, int extern_p)
10672 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10673 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10674 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10675 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10676 if (! extern_p)
10678 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10679 rest_of_type_compilation (t, 1);
10683 /* Called from do_type_instantiation through binding_table_foreach to
10684 do recursive instantiation for the type bound in ENTRY. */
10685 static void
10686 bt_instantiate_type_proc (binding_entry entry, void *data)
10688 tree storage = *(tree *) data;
10690 if (IS_AGGR_TYPE (entry->type)
10691 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
10692 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
10695 /* Called from do_type_instantiation to instantiate a member
10696 (a member function or a static member variable) of an
10697 explicitly instantiated class template. */
10698 static void
10699 instantiate_class_member (tree decl, int extern_p)
10701 mark_decl_instantiated (decl, extern_p);
10702 if (! extern_p)
10703 instantiate_decl (decl, /*defer_ok=*/1, /* undefined_ok=*/1);
10706 /* Perform an explicit instantiation of template class T. STORAGE, if
10707 non-null, is the RID for extern, inline or static. COMPLAIN is
10708 nonzero if this is called from the parser, zero if called recursively,
10709 since the standard is unclear (as detailed below). */
10711 void
10712 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
10714 int extern_p = 0;
10715 int nomem_p = 0;
10716 int static_p = 0;
10717 int previous_instantiation_extern_p = 0;
10719 if (TREE_CODE (t) == TYPE_DECL)
10720 t = TREE_TYPE (t);
10722 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10724 error ("explicit instantiation of non-template type `%T'", t);
10725 return;
10728 complete_type (t);
10730 if (!COMPLETE_TYPE_P (t))
10732 if (complain & tf_error)
10733 error ("explicit instantiation of `%#T' before definition of template",
10735 return;
10738 if (storage != NULL_TREE)
10740 if (pedantic && !in_system_header)
10741 pedwarn("ISO C++ forbids the use of `%E' on explicit instantiations",
10742 storage);
10744 if (storage == ridpointers[(int) RID_INLINE])
10745 nomem_p = 1;
10746 else if (storage == ridpointers[(int) RID_EXTERN])
10747 extern_p = 1;
10748 else if (storage == ridpointers[(int) RID_STATIC])
10749 static_p = 1;
10750 else
10752 error ("storage class `%D' applied to template instantiation",
10753 storage);
10754 extern_p = 0;
10758 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10760 /* DR 259 [temp.spec].
10762 Both an explicit instantiation and a declaration of an explicit
10763 specialization shall not appear in a program unless the explicit
10764 instantiation follows a declaration of the explicit specialization.
10766 For a given set of template parameters, if an explicit
10767 instantiation of a template appears after a declaration of an
10768 explicit specialization for that template, the explicit
10769 instantiation has no effect. */
10770 return;
10772 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10774 /* [temp.spec]
10776 No program shall explicitly instantiate any template more
10777 than once.
10779 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
10780 instantiation was `extern'. If EXTERN_P then the second is.
10781 These cases are OK. */
10782 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
10784 if (!previous_instantiation_extern_p && !extern_p
10785 && (complain & tf_error))
10786 pedwarn ("duplicate explicit instantiation of `%#T'", t);
10788 /* If we've already instantiated the template, just return now. */
10789 if (!CLASSTYPE_INTERFACE_ONLY (t))
10790 return;
10793 mark_class_instantiated (t, extern_p);
10795 if (nomem_p)
10796 return;
10799 tree tmp;
10801 /* In contrast to implicit instantiation, where only the
10802 declarations, and not the definitions, of members are
10803 instantiated, we have here:
10805 [temp.explicit]
10807 The explicit instantiation of a class template specialization
10808 implies the instantiation of all of its members not
10809 previously explicitly specialized in the translation unit
10810 containing the explicit instantiation.
10812 Of course, we can't instantiate member template classes, since
10813 we don't have any arguments for them. Note that the standard
10814 is unclear on whether the instantiation of the members are
10815 *explicit* instantiations or not. However, the most natural
10816 interpretation is that it should be an explicit instantiation. */
10818 if (! static_p)
10819 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10820 if (TREE_CODE (tmp) == FUNCTION_DECL
10821 && DECL_TEMPLATE_INSTANTIATION (tmp))
10822 instantiate_class_member (tmp, extern_p);
10824 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10825 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10826 instantiate_class_member (tmp, extern_p);
10828 if (CLASSTYPE_NESTED_UTDS (t))
10829 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
10830 bt_instantiate_type_proc, &storage);
10834 /* Given a function DECL, which is a specialization of TMPL, modify
10835 DECL to be a re-instantiation of TMPL with the same template
10836 arguments. TMPL should be the template into which tsubst'ing
10837 should occur for DECL, not the most general template.
10839 One reason for doing this is a scenario like this:
10841 template <class T>
10842 void f(const T&, int i);
10844 void g() { f(3, 7); }
10846 template <class T>
10847 void f(const T& t, const int i) { }
10849 Note that when the template is first instantiated, with
10850 instantiate_template, the resulting DECL will have no name for the
10851 first parameter, and the wrong type for the second. So, when we go
10852 to instantiate the DECL, we regenerate it. */
10854 static void
10855 regenerate_decl_from_template (tree decl, tree tmpl)
10857 /* The most general version of TMPL. */
10858 tree gen_tmpl;
10859 /* The arguments used to instantiate DECL, from the most general
10860 template. */
10861 tree args;
10862 tree code_pattern;
10863 tree new_decl;
10864 bool unregistered;
10866 args = DECL_TI_ARGS (decl);
10867 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10869 /* Unregister the specialization so that when we tsubst we will not
10870 just return DECL. We don't have to unregister DECL from TMPL
10871 because if would only be registered there if it were a partial
10872 instantiation of a specialization, which it isn't: it's a full
10873 instantiation. */
10874 gen_tmpl = most_general_template (tmpl);
10875 unregistered = reregister_specialization (decl, gen_tmpl,
10876 /*new_spec=*/NULL_TREE);
10878 /* If the DECL was not unregistered then something peculiar is
10879 happening: we created a specialization but did not call
10880 register_specialization for it. */
10881 gcc_assert (unregistered);
10883 /* Make sure that we can see identifiers, and compute access
10884 correctly. */
10885 push_access_scope (decl);
10887 /* Do the substitution to get the new declaration. */
10888 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10890 if (TREE_CODE (decl) == VAR_DECL)
10892 /* Set up DECL_INITIAL, since tsubst doesn't. */
10893 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10894 DECL_INITIAL (new_decl) =
10895 tsubst_expr (DECL_INITIAL (code_pattern), args,
10896 tf_error, DECL_TI_TEMPLATE (decl));
10898 else if (TREE_CODE (decl) == FUNCTION_DECL)
10900 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10901 new decl. */
10902 DECL_INITIAL (new_decl) = error_mark_node;
10903 /* And don't complain about a duplicate definition. */
10904 DECL_INITIAL (decl) = NULL_TREE;
10907 pop_access_scope (decl);
10909 /* The immediate parent of the new template is still whatever it was
10910 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10911 general template. We also reset the DECL_ASSEMBLER_NAME since
10912 tsubst always calculates the name as if the function in question
10913 were really a template instance, and sometimes, with friend
10914 functions, this is not so. See tsubst_friend_function for
10915 details. */
10916 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10917 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10918 COPY_DECL_RTL (decl, new_decl);
10919 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10921 /* Call duplicate decls to merge the old and new declarations. */
10922 duplicate_decls (new_decl, decl);
10924 /* Now, re-register the specialization. */
10925 register_specialization (decl, gen_tmpl, args);
10928 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10929 substituted to get DECL. */
10931 tree
10932 template_for_substitution (tree decl)
10934 tree tmpl = DECL_TI_TEMPLATE (decl);
10936 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10937 for the instantiation. This is not always the most general
10938 template. Consider, for example:
10940 template <class T>
10941 struct S { template <class U> void f();
10942 template <> void f<int>(); };
10944 and an instantiation of S<double>::f<int>. We want TD to be the
10945 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10946 while (/* An instantiation cannot have a definition, so we need a
10947 more general template. */
10948 DECL_TEMPLATE_INSTANTIATION (tmpl)
10949 /* We must also deal with friend templates. Given:
10951 template <class T> struct S {
10952 template <class U> friend void f() {};
10955 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10956 so far as the language is concerned, but that's still
10957 where we get the pattern for the instantiation from. On
10958 other hand, if the definition comes outside the class, say:
10960 template <class T> struct S {
10961 template <class U> friend void f();
10963 template <class U> friend void f() {}
10965 we don't need to look any further. That's what the check for
10966 DECL_INITIAL is for. */
10967 || (TREE_CODE (decl) == FUNCTION_DECL
10968 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10969 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10971 /* The present template, TD, should not be a definition. If it
10972 were a definition, we should be using it! Note that we
10973 cannot restructure the loop to just keep going until we find
10974 a template with a definition, since that might go too far if
10975 a specialization was declared, but not defined. */
10976 gcc_assert (TREE_CODE (decl) != VAR_DECL
10977 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
10979 /* Fetch the more general template. */
10980 tmpl = DECL_TI_TEMPLATE (tmpl);
10983 return tmpl;
10986 /* Produce the definition of D, a _DECL generated from a template. If
10987 DEFER_OK is nonzero, then we don't have to actually do the
10988 instantiation now; we just have to do it sometime. Normally it is
10989 an error if this is an explicit instantiation but D is undefined.
10990 If UNDEFINED_OK is nonzero, then instead we treat it as an implicit
10991 instantiation. UNDEFINED_OK is nonzero only if we are being used
10992 to instantiate the members of an explicitly instantiated class
10993 template. */
10996 tree
10997 instantiate_decl (tree d, int defer_ok, int undefined_ok)
10999 tree tmpl = DECL_TI_TEMPLATE (d);
11000 tree gen_args;
11001 tree args;
11002 tree td;
11003 tree code_pattern;
11004 tree spec;
11005 tree gen_tmpl;
11006 int pattern_defined;
11007 int need_push;
11008 location_t saved_loc = input_location;
11010 /* This function should only be used to instantiate templates for
11011 functions and static member variables. */
11012 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11013 || TREE_CODE (d) == VAR_DECL);
11015 /* Variables are never deferred; if instantiation is required, they
11016 are instantiated right away. That allows for better code in the
11017 case that an expression refers to the value of the variable --
11018 if the variable has a constant value the referring expression can
11019 take advantage of that fact. */
11020 if (TREE_CODE (d) == VAR_DECL)
11021 defer_ok = 0;
11023 /* Don't instantiate cloned functions. Instead, instantiate the
11024 functions they cloned. */
11025 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11026 d = DECL_CLONED_FUNCTION (d);
11028 if (DECL_TEMPLATE_INSTANTIATED (d))
11029 /* D has already been instantiated. It might seem reasonable to
11030 check whether or not D is an explicit instantiation, and, if so,
11031 stop here. But when an explicit instantiation is deferred
11032 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11033 is set, even though we still need to do the instantiation. */
11034 return d;
11036 /* If we already have a specialization of this declaration, then
11037 there's no reason to instantiate it. Note that
11038 retrieve_specialization gives us both instantiations and
11039 specializations, so we must explicitly check
11040 DECL_TEMPLATE_SPECIALIZATION. */
11041 gen_tmpl = most_general_template (tmpl);
11042 gen_args = DECL_TI_ARGS (d);
11043 spec = retrieve_specialization (gen_tmpl, gen_args);
11044 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11045 return spec;
11047 /* This needs to happen before any tsubsting. */
11048 if (! push_tinst_level (d))
11049 return d;
11051 timevar_push (TV_PARSE);
11053 /* We may be in the middle of deferred access check. Disable it now. */
11054 push_deferring_access_checks (dk_no_deferred);
11056 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11057 for the instantiation. */
11058 td = template_for_substitution (d);
11059 code_pattern = DECL_TEMPLATE_RESULT (td);
11061 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11062 || DECL_TEMPLATE_SPECIALIZATION (td))
11063 /* In the case of a friend template whose definition is provided
11064 outside the class, we may have too many arguments. Drop the
11065 ones we don't need. The same is true for specializations. */
11066 args = get_innermost_template_args
11067 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11068 else
11069 args = gen_args;
11071 if (TREE_CODE (d) == FUNCTION_DECL)
11072 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11073 else
11074 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11075 /* Unless an explicit instantiation directive has already determined
11076 the linkage of D, remember that a definition is available for
11077 this entity. */
11078 if (pattern_defined
11079 && !DECL_INTERFACE_KNOWN (d)
11080 && !DECL_NOT_REALLY_EXTERN (d))
11081 mark_definable (d);
11083 input_location = DECL_SOURCE_LOCATION (d);
11085 if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok)
11087 DECL_NOT_REALLY_EXTERN (d) = 0;
11088 SET_DECL_IMPLICIT_INSTANTIATION (d);
11091 if (!defer_ok)
11093 /* Recheck the substitutions to obtain any warning messages
11094 about ignoring cv qualifiers. */
11095 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11096 tree type = TREE_TYPE (gen);
11098 /* Make sure that we can see identifiers, and compute access
11099 correctly. D is already the target FUNCTION_DECL with the
11100 right context. */
11101 push_access_scope (d);
11103 if (TREE_CODE (gen) == FUNCTION_DECL)
11105 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
11106 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11107 tf_error | tf_warning, d);
11108 /* Don't simply tsubst the function type, as that will give
11109 duplicate warnings about poor parameter qualifications.
11110 The function arguments are the same as the decl_arguments
11111 without the top level cv qualifiers. */
11112 type = TREE_TYPE (type);
11114 tsubst (type, gen_args, tf_error | tf_warning, d);
11116 pop_access_scope (d);
11119 /* We should have set up DECL_INITIAL in instantiate_class_template
11120 for in-class definitions of static data members. */
11121 gcc_assert (!(TREE_CODE (d) == VAR_DECL
11122 && DECL_INITIALIZED_IN_CLASS_P (d)
11123 && DECL_INITIAL (d) == NULL_TREE));
11125 /* Do not instantiate templates that we know will be defined
11126 elsewhere. */
11127 if (DECL_INTERFACE_KNOWN (d)
11128 && DECL_REALLY_EXTERN (d)
11129 && ! (TREE_CODE (d) == FUNCTION_DECL
11130 && DECL_INLINE (d)))
11131 goto out;
11132 /* Defer all other templates, unless we have been explicitly
11133 forbidden from doing so. We restore the source position here
11134 because it's used by add_pending_template. */
11135 else if (! pattern_defined || defer_ok)
11137 input_location = saved_loc;
11139 if (at_eof && !pattern_defined
11140 && DECL_EXPLICIT_INSTANTIATION (d))
11141 /* [temp.explicit]
11143 The definition of a non-exported function template, a
11144 non-exported member function template, or a non-exported
11145 member function or static data member of a class template
11146 shall be present in every translation unit in which it is
11147 explicitly instantiated. */
11148 pedwarn
11149 ("explicit instantiation of `%D' but no definition available", d);
11151 add_pending_template (d);
11152 goto out;
11154 /* Tell the repository that D is available in this translation unit
11155 -- and see if it is supposed to be instantiated here. */
11156 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11158 /* In a PCH file, despite the fact that the repository hasn't
11159 requested instantiation in the PCH it is still possible that
11160 an instantiation will be required in a file that includes the
11161 PCH. */
11162 if (pch_file)
11163 add_pending_template (d);
11164 /* Instantiate inline functions so that the inliner can do its
11165 job, even though we'll not be emitting a copy of this
11166 function. */
11167 if (!(TREE_CODE (d) == FUNCTION_DECL
11168 && flag_inline_trees
11169 && DECL_DECLARED_INLINE_P (d)))
11170 goto out;
11173 need_push = !cfun || !global_bindings_p ();
11174 if (need_push)
11175 push_to_top_level ();
11177 /* Mark D as instantiated so that recursive calls to
11178 instantiate_decl do not try to instantiate it again. */
11179 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11181 /* Regenerate the declaration in case the template has been modified
11182 by a subsequent redeclaration. */
11183 regenerate_decl_from_template (d, td);
11185 /* We already set the file and line above. Reset them now in case
11186 they changed as a result of calling regenerate_decl_from_template. */
11187 input_location = DECL_SOURCE_LOCATION (d);
11189 if (TREE_CODE (d) == VAR_DECL)
11191 /* Clear out DECL_RTL; whatever was there before may not be right
11192 since we've reset the type of the declaration. */
11193 SET_DECL_RTL (d, NULL_RTX);
11194 DECL_IN_AGGR_P (d) = 0;
11196 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11197 initializer. That function will defer actual emission until
11198 we have a chance to determine linkage. */
11199 DECL_EXTERNAL (d) = 0;
11201 /* Enter the scope of D so that access-checking works correctly. */
11202 push_nested_class (DECL_CONTEXT (d));
11203 cp_finish_decl (d,
11204 (!DECL_INITIALIZED_IN_CLASS_P (d)
11205 ? DECL_INITIAL (d) : NULL_TREE),
11206 NULL_TREE, 0);
11207 pop_nested_class ();
11209 else if (TREE_CODE (d) == FUNCTION_DECL)
11211 htab_t saved_local_specializations;
11212 tree subst_decl;
11213 tree tmpl_parm;
11214 tree spec_parm;
11216 /* Save away the current list, in case we are instantiating one
11217 template from within the body of another. */
11218 saved_local_specializations = local_specializations;
11220 /* Set up the list of local specializations. */
11221 local_specializations = htab_create (37,
11222 hash_local_specialization,
11223 eq_local_specializations,
11224 NULL);
11226 /* Set up context. */
11227 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11229 /* Create substitution entries for the parameters. */
11230 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11231 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11232 spec_parm = DECL_ARGUMENTS (d);
11233 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11235 register_local_specialization (spec_parm, tmpl_parm);
11236 spec_parm = skip_artificial_parms_for (d, spec_parm);
11237 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11239 while (tmpl_parm)
11241 register_local_specialization (spec_parm, tmpl_parm);
11242 tmpl_parm = TREE_CHAIN (tmpl_parm);
11243 spec_parm = TREE_CHAIN (spec_parm);
11245 gcc_assert (!spec_parm);
11247 /* Substitute into the body of the function. */
11248 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11249 tf_error | tf_warning, tmpl);
11251 /* We don't need the local specializations any more. */
11252 htab_delete (local_specializations);
11253 local_specializations = saved_local_specializations;
11255 /* Finish the function. */
11256 d = finish_function (0);
11257 expand_or_defer_fn (d);
11260 /* We're not deferring instantiation any more. */
11261 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11263 if (need_push)
11264 pop_from_top_level ();
11266 out:
11267 input_location = saved_loc;
11268 pop_deferring_access_checks ();
11269 pop_tinst_level ();
11271 timevar_pop (TV_PARSE);
11273 return d;
11276 /* Run through the list of templates that we wish we could
11277 instantiate, and instantiate any we can. RETRIES is the
11278 number of times we retry pending template instantiation. */
11280 void
11281 instantiate_pending_templates (int retries)
11283 tree *t;
11284 tree last = NULL_TREE;
11285 int reconsider;
11286 location_t saved_loc = input_location;
11288 /* Instantiating templates may trigger vtable generation. This in turn
11289 may require further template instantiations. We place a limit here
11290 to avoid infinite loop. */
11291 if (pending_templates && retries >= max_tinst_depth)
11293 cp_error_at ("template instantiation depth exceeds maximum of %d"
11294 " (use -ftemplate-depth-NN to increase the maximum)"
11295 " instantiating `%+D', possibly from virtual table"
11296 " generation",
11297 max_tinst_depth, TREE_VALUE (pending_templates));
11298 return;
11303 reconsider = 0;
11305 t = &pending_templates;
11306 while (*t)
11308 tree instantiation = TREE_VALUE (*t);
11310 reopen_tinst_level (TREE_PURPOSE (*t));
11312 if (TYPE_P (instantiation))
11314 tree fn;
11316 if (!COMPLETE_TYPE_P (instantiation))
11318 instantiate_class_template (instantiation);
11319 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11320 for (fn = TYPE_METHODS (instantiation);
11322 fn = TREE_CHAIN (fn))
11323 if (! DECL_ARTIFICIAL (fn))
11324 instantiate_decl (fn, /*defer_ok=*/0,
11325 /*undefined_ok=*/0);
11326 if (COMPLETE_TYPE_P (instantiation))
11327 reconsider = 1;
11330 if (COMPLETE_TYPE_P (instantiation))
11331 /* If INSTANTIATION has been instantiated, then we don't
11332 need to consider it again in the future. */
11333 *t = TREE_CHAIN (*t);
11334 else
11336 last = *t;
11337 t = &TREE_CHAIN (*t);
11340 else
11342 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11343 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11345 instantiation = instantiate_decl (instantiation,
11346 /*defer_ok=*/0,
11347 /*undefined_ok=*/0);
11348 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11349 reconsider = 1;
11352 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11353 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11354 /* If INSTANTIATION has been instantiated, then we don't
11355 need to consider it again in the future. */
11356 *t = TREE_CHAIN (*t);
11357 else
11359 last = *t;
11360 t = &TREE_CHAIN (*t);
11363 tinst_depth = 0;
11364 current_tinst_level = NULL_TREE;
11366 last_pending_template = last;
11368 while (reconsider);
11370 input_location = saved_loc;
11373 /* Substitute ARGVEC into T, which is a list of initializers for
11374 either base class or a non-static data member. The TREE_PURPOSEs
11375 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11376 instantiate_decl. */
11378 static tree
11379 tsubst_initializer_list (tree t, tree argvec)
11381 tree inits = NULL_TREE;
11383 for (; t; t = TREE_CHAIN (t))
11385 tree decl;
11386 tree init;
11387 tree val;
11389 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11390 NULL_TREE);
11391 decl = expand_member_init (decl);
11392 if (decl && !DECL_P (decl))
11393 in_base_initializer = 1;
11395 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11396 NULL_TREE);
11397 if (!init)
11399 else if (TREE_CODE (init) == TREE_LIST)
11400 for (val = init; val; val = TREE_CHAIN (val))
11401 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11402 else if (init != void_type_node)
11403 init = convert_from_reference (init);
11405 in_base_initializer = 0;
11407 if (decl)
11409 init = build_tree_list (decl, init);
11410 TREE_CHAIN (init) = inits;
11411 inits = init;
11414 return inits;
11417 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11419 static void
11420 set_current_access_from_decl (tree decl)
11422 if (TREE_PRIVATE (decl))
11423 current_access_specifier = access_private_node;
11424 else if (TREE_PROTECTED (decl))
11425 current_access_specifier = access_protected_node;
11426 else
11427 current_access_specifier = access_public_node;
11430 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11431 is the instantiation (which should have been created with
11432 start_enum) and ARGS are the template arguments to use. */
11434 static void
11435 tsubst_enum (tree tag, tree newtag, tree args)
11437 tree e;
11439 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11441 tree value;
11442 tree decl;
11444 decl = TREE_VALUE (e);
11445 /* Note that in a template enum, the TREE_VALUE is the
11446 CONST_DECL, not the corresponding INTEGER_CST. */
11447 value = tsubst_expr (DECL_INITIAL (decl),
11448 args, tf_error | tf_warning,
11449 NULL_TREE);
11451 /* Give this enumeration constant the correct access. */
11452 set_current_access_from_decl (decl);
11454 /* Actually build the enumerator itself. */
11455 build_enumerator (DECL_NAME (decl), value, newtag);
11458 finish_enum (newtag);
11459 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11460 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11463 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11464 its type -- but without substituting the innermost set of template
11465 arguments. So, innermost set of template parameters will appear in
11466 the type. */
11468 tree
11469 get_mostly_instantiated_function_type (tree decl)
11471 tree fn_type;
11472 tree tmpl;
11473 tree targs;
11474 tree tparms;
11475 int parm_depth;
11477 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11478 targs = DECL_TI_ARGS (decl);
11479 tparms = DECL_TEMPLATE_PARMS (tmpl);
11480 parm_depth = TMPL_PARMS_DEPTH (tparms);
11482 /* There should be as many levels of arguments as there are levels
11483 of parameters. */
11484 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
11486 fn_type = TREE_TYPE (tmpl);
11488 if (parm_depth == 1)
11489 /* No substitution is necessary. */
11491 else
11493 int i;
11494 tree partial_args;
11496 /* Replace the innermost level of the TARGS with NULL_TREEs to
11497 let tsubst know not to substitute for those parameters. */
11498 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11499 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11500 SET_TMPL_ARGS_LEVEL (partial_args, i,
11501 TMPL_ARGS_LEVEL (targs, i));
11502 SET_TMPL_ARGS_LEVEL (partial_args,
11503 TMPL_ARGS_DEPTH (targs),
11504 make_tree_vec (DECL_NTPARMS (tmpl)));
11506 /* Make sure that we can see identifiers, and compute access
11507 correctly. We can just use the context of DECL for the
11508 partial substitution here. It depends only on outer template
11509 parameters, regardless of whether the innermost level is
11510 specialized or not. */
11511 push_access_scope (decl);
11513 ++processing_template_decl;
11514 /* Now, do the (partial) substitution to figure out the
11515 appropriate function type. */
11516 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11517 --processing_template_decl;
11519 /* Substitute into the template parameters to obtain the real
11520 innermost set of parameters. This step is important if the
11521 innermost set of template parameters contains value
11522 parameters whose types depend on outer template parameters. */
11523 TREE_VEC_LENGTH (partial_args)--;
11524 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11526 pop_access_scope (decl);
11529 return fn_type;
11532 /* Return truthvalue if we're processing a template different from
11533 the last one involved in diagnostics. */
11535 problematic_instantiation_changed (void)
11537 return last_template_error_tick != tinst_level_tick;
11540 /* Remember current template involved in diagnostics. */
11541 void
11542 record_last_problematic_instantiation (void)
11544 last_template_error_tick = tinst_level_tick;
11547 tree
11548 current_instantiation (void)
11550 return current_tinst_level;
11553 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11554 type. Return zero for ok, nonzero for disallowed. Issue error and
11555 warning messages under control of COMPLAIN. */
11557 static int
11558 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
11560 if (INTEGRAL_TYPE_P (type))
11561 return 0;
11562 else if (POINTER_TYPE_P (type))
11563 return 0;
11564 else if (TYPE_PTR_TO_MEMBER_P (type))
11565 return 0;
11566 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11567 return 0;
11568 else if (TREE_CODE (type) == TYPENAME_TYPE)
11569 return 0;
11571 if (complain & tf_error)
11572 error ("`%#T' is not a valid type for a template constant parameter",
11573 type);
11574 return 1;
11577 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11578 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11580 static bool
11581 dependent_type_p_r (tree type)
11583 tree scope;
11585 /* [temp.dep.type]
11587 A type is dependent if it is:
11589 -- a template parameter. Template template parameters are types
11590 for us (since TYPE_P holds true for them) so we handle
11591 them here. */
11592 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11593 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
11594 return true;
11595 /* -- a qualified-id with a nested-name-specifier which contains a
11596 class-name that names a dependent type or whose unqualified-id
11597 names a dependent type. */
11598 if (TREE_CODE (type) == TYPENAME_TYPE)
11599 return true;
11600 /* -- a cv-qualified type where the cv-unqualified type is
11601 dependent. */
11602 type = TYPE_MAIN_VARIANT (type);
11603 /* -- a compound type constructed from any dependent type. */
11604 if (TYPE_PTR_TO_MEMBER_P (type))
11605 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11606 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11607 (type)));
11608 else if (TREE_CODE (type) == POINTER_TYPE
11609 || TREE_CODE (type) == REFERENCE_TYPE)
11610 return dependent_type_p (TREE_TYPE (type));
11611 else if (TREE_CODE (type) == FUNCTION_TYPE
11612 || TREE_CODE (type) == METHOD_TYPE)
11614 tree arg_type;
11616 if (dependent_type_p (TREE_TYPE (type)))
11617 return true;
11618 for (arg_type = TYPE_ARG_TYPES (type);
11619 arg_type;
11620 arg_type = TREE_CHAIN (arg_type))
11621 if (dependent_type_p (TREE_VALUE (arg_type)))
11622 return true;
11623 return false;
11625 /* -- an array type constructed from any dependent type or whose
11626 size is specified by a constant expression that is
11627 value-dependent. */
11628 if (TREE_CODE (type) == ARRAY_TYPE)
11630 if (TYPE_DOMAIN (type)
11631 && ((value_dependent_expression_p
11632 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11633 || (type_dependent_expression_p
11634 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11635 return true;
11636 return dependent_type_p (TREE_TYPE (type));
11639 /* -- a template-id in which either the template name is a template
11640 parameter ... */
11641 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11642 return true;
11643 /* ... or any of the template arguments is a dependent type or
11644 an expression that is type-dependent or value-dependent. */
11645 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11646 && (any_dependent_template_arguments_p
11647 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
11648 return true;
11650 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11651 expression is not type-dependent, then it should already been
11652 have resolved. */
11653 if (TREE_CODE (type) == TYPEOF_TYPE)
11654 return true;
11656 /* The standard does not specifically mention types that are local
11657 to template functions or local classes, but they should be
11658 considered dependent too. For example:
11660 template <int I> void f() {
11661 enum E { a = I };
11662 S<sizeof (E)> s;
11665 The size of `E' cannot be known until the value of `I' has been
11666 determined. Therefore, `E' must be considered dependent. */
11667 scope = TYPE_CONTEXT (type);
11668 if (scope && TYPE_P (scope))
11669 return dependent_type_p (scope);
11670 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11671 return type_dependent_expression_p (scope);
11673 /* Other types are non-dependent. */
11674 return false;
11677 /* Returns TRUE if TYPE is dependent, in the sense of
11678 [temp.dep.type]. */
11680 bool
11681 dependent_type_p (tree type)
11683 /* If there are no template parameters in scope, then there can't be
11684 any dependent types. */
11685 if (!processing_template_decl)
11686 return false;
11688 /* If the type is NULL, we have not computed a type for the entity
11689 in question; in that case, the type is dependent. */
11690 if (!type)
11691 return true;
11693 /* Erroneous types can be considered non-dependent. */
11694 if (type == error_mark_node)
11695 return false;
11697 /* If we have not already computed the appropriate value for TYPE,
11698 do so now. */
11699 if (!TYPE_DEPENDENT_P_VALID (type))
11701 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11702 TYPE_DEPENDENT_P_VALID (type) = 1;
11705 return TYPE_DEPENDENT_P (type);
11708 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
11710 static bool
11711 dependent_scope_ref_p (tree expression, bool criterion (tree))
11713 tree scope;
11714 tree name;
11716 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
11718 if (!TYPE_P (TREE_OPERAND (expression, 0)))
11719 return true;
11721 scope = TREE_OPERAND (expression, 0);
11722 name = TREE_OPERAND (expression, 1);
11724 /* [temp.dep.expr]
11726 An id-expression is type-dependent if it contains a
11727 nested-name-specifier that contains a class-name that names a
11728 dependent type. */
11729 /* The suggested resolution to Core Issue 2 implies that if the
11730 qualifying type is the current class, then we must peek
11731 inside it. */
11732 if (DECL_P (name)
11733 && currently_open_class (scope)
11734 && !criterion (name))
11735 return false;
11736 if (dependent_type_p (scope))
11737 return true;
11739 return false;
11742 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
11743 [temp.dep.constexpr] */
11745 bool
11746 value_dependent_expression_p (tree expression)
11748 if (!processing_template_decl)
11749 return false;
11751 /* A name declared with a dependent type. */
11752 if (TREE_CODE (expression) == IDENTIFIER_NODE
11753 || (DECL_P (expression)
11754 && type_dependent_expression_p (expression)))
11755 return true;
11756 /* A non-type template parameter. */
11757 if ((TREE_CODE (expression) == CONST_DECL
11758 && DECL_TEMPLATE_PARM_P (expression))
11759 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11760 return true;
11761 /* A constant with integral or enumeration type and is initialized
11762 with an expression that is value-dependent. */
11763 if (TREE_CODE (expression) == VAR_DECL
11764 && DECL_INITIAL (expression)
11765 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
11766 && value_dependent_expression_p (DECL_INITIAL (expression)))
11767 return true;
11768 /* These expressions are value-dependent if the type to which the
11769 cast occurs is dependent or the expression being casted is
11770 value-dependent. */
11771 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11772 || TREE_CODE (expression) == STATIC_CAST_EXPR
11773 || TREE_CODE (expression) == CONST_CAST_EXPR
11774 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11775 || TREE_CODE (expression) == CAST_EXPR)
11777 tree type = TREE_TYPE (expression);
11778 if (dependent_type_p (type))
11779 return true;
11780 /* A functional cast has a list of operands. */
11781 expression = TREE_OPERAND (expression, 0);
11782 if (!expression)
11784 /* If there are no operands, it must be an expression such
11785 as "int()". This should not happen for aggregate types
11786 because it would form non-constant expressions. */
11787 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
11789 return false;
11791 if (TREE_CODE (expression) == TREE_LIST)
11795 if (value_dependent_expression_p (TREE_VALUE (expression)))
11796 return true;
11797 expression = TREE_CHAIN (expression);
11799 while (expression);
11800 return false;
11802 else
11803 return value_dependent_expression_p (expression);
11805 /* A `sizeof' expression is value-dependent if the operand is
11806 type-dependent. */
11807 if (TREE_CODE (expression) == SIZEOF_EXPR
11808 || TREE_CODE (expression) == ALIGNOF_EXPR)
11810 expression = TREE_OPERAND (expression, 0);
11811 if (TYPE_P (expression))
11812 return dependent_type_p (expression);
11813 return type_dependent_expression_p (expression);
11815 if (TREE_CODE (expression) == SCOPE_REF)
11816 return dependent_scope_ref_p (expression, value_dependent_expression_p);
11817 if (TREE_CODE (expression) == COMPONENT_REF)
11818 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
11819 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
11820 /* A constant expression is value-dependent if any subexpression is
11821 value-dependent. */
11822 if (EXPR_P (expression))
11824 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11826 case tcc_unary:
11827 return (value_dependent_expression_p
11828 (TREE_OPERAND (expression, 0)));
11829 case tcc_comparison:
11830 case tcc_binary:
11831 return ((value_dependent_expression_p
11832 (TREE_OPERAND (expression, 0)))
11833 || (value_dependent_expression_p
11834 (TREE_OPERAND (expression, 1))));
11835 case tcc_expression:
11837 int i;
11838 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11839 /* In some cases, some of the operands may be missing.
11840 (For example, in the case of PREDECREMENT_EXPR, the
11841 amount to increment by may be missing.) That doesn't
11842 make the expression dependent. */
11843 if (TREE_OPERAND (expression, i)
11844 && (value_dependent_expression_p
11845 (TREE_OPERAND (expression, i))))
11846 return true;
11847 return false;
11849 case tcc_reference:
11850 case tcc_statement:
11851 /* These cannot be value dependent. */
11852 return false;
11854 default:
11855 gcc_unreachable ();
11859 /* The expression is not value-dependent. */
11860 return false;
11863 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11864 [temp.dep.expr]. */
11866 bool
11867 type_dependent_expression_p (tree expression)
11869 if (!processing_template_decl)
11870 return false;
11872 if (expression == error_mark_node)
11873 return false;
11875 /* An unresolved name is always dependent. */
11876 if (TREE_CODE (expression) == IDENTIFIER_NODE)
11877 return true;
11879 /* Some expression forms are never type-dependent. */
11880 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11881 || TREE_CODE (expression) == SIZEOF_EXPR
11882 || TREE_CODE (expression) == ALIGNOF_EXPR
11883 || TREE_CODE (expression) == TYPEID_EXPR
11884 || TREE_CODE (expression) == DELETE_EXPR
11885 || TREE_CODE (expression) == VEC_DELETE_EXPR
11886 || TREE_CODE (expression) == THROW_EXPR)
11887 return false;
11889 /* The types of these expressions depends only on the type to which
11890 the cast occurs. */
11891 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11892 || TREE_CODE (expression) == STATIC_CAST_EXPR
11893 || TREE_CODE (expression) == CONST_CAST_EXPR
11894 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11895 || TREE_CODE (expression) == CAST_EXPR)
11896 return dependent_type_p (TREE_TYPE (expression));
11898 /* The types of these expressions depends only on the type created
11899 by the expression. */
11900 if (TREE_CODE (expression) == NEW_EXPR
11901 || TREE_CODE (expression) == VEC_NEW_EXPR)
11903 /* For NEW_EXPR tree nodes created inside a template, either
11904 the object type itself or a TREE_LIST may appear as the
11905 operand 1. */
11906 tree type = TREE_OPERAND (expression, 1);
11907 if (TREE_CODE (type) == TREE_LIST)
11908 /* This is an array type. We need to check array dimensions
11909 as well. */
11910 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
11911 || value_dependent_expression_p
11912 (TREE_OPERAND (TREE_VALUE (type), 1));
11913 else
11914 return dependent_type_p (type);
11917 if (TREE_CODE (expression) == SCOPE_REF
11918 && dependent_scope_ref_p (expression,
11919 type_dependent_expression_p))
11920 return true;
11922 if (TREE_CODE (expression) == FUNCTION_DECL
11923 && DECL_LANG_SPECIFIC (expression)
11924 && DECL_TEMPLATE_INFO (expression)
11925 && (any_dependent_template_arguments_p
11926 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11927 return true;
11929 if (TREE_CODE (expression) == TEMPLATE_DECL
11930 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
11931 return false;
11933 if (TREE_TYPE (expression) == unknown_type_node)
11935 if (TREE_CODE (expression) == ADDR_EXPR)
11936 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
11937 if (TREE_CODE (expression) == COMPONENT_REF
11938 || TREE_CODE (expression) == OFFSET_REF)
11940 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
11941 return true;
11942 expression = TREE_OPERAND (expression, 1);
11943 if (TREE_CODE (expression) == IDENTIFIER_NODE)
11944 return false;
11946 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
11947 if (TREE_CODE (expression) == SCOPE_REF)
11948 return false;
11950 if (TREE_CODE (expression) == BASELINK)
11951 expression = BASELINK_FUNCTIONS (expression);
11953 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
11955 if (any_dependent_template_arguments_p
11956 (TREE_OPERAND (expression, 1)))
11957 return true;
11958 expression = TREE_OPERAND (expression, 0);
11960 gcc_assert (TREE_CODE (expression) == OVERLOAD);
11962 while (expression)
11964 if (type_dependent_expression_p (OVL_CURRENT (expression)))
11965 return true;
11966 expression = OVL_NEXT (expression);
11968 return false;
11971 return (dependent_type_p (TREE_TYPE (expression)));
11974 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
11975 contains a type-dependent expression. */
11977 bool
11978 any_type_dependent_arguments_p (tree args)
11980 while (args)
11982 tree arg = TREE_VALUE (args);
11984 if (type_dependent_expression_p (arg))
11985 return true;
11986 args = TREE_CHAIN (args);
11988 return false;
11991 /* Returns TRUE if the ARG (a template argument) is dependent. */
11993 static bool
11994 dependent_template_arg_p (tree arg)
11996 if (!processing_template_decl)
11997 return false;
11999 if (TREE_CODE (arg) == TEMPLATE_DECL
12000 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12001 return dependent_template_p (arg);
12002 else if (TYPE_P (arg))
12003 return dependent_type_p (arg);
12004 else
12005 return (type_dependent_expression_p (arg)
12006 || value_dependent_expression_p (arg));
12009 /* Returns true if ARGS (a collection of template arguments) contains
12010 any dependent arguments. */
12012 bool
12013 any_dependent_template_arguments_p (tree args)
12015 int i;
12016 int j;
12018 if (!args)
12019 return false;
12021 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12023 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12024 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12025 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12026 return true;
12029 return false;
12032 /* Returns TRUE if the template TMPL is dependent. */
12034 bool
12035 dependent_template_p (tree tmpl)
12037 if (TREE_CODE (tmpl) == OVERLOAD)
12039 while (tmpl)
12041 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12042 return true;
12043 tmpl = OVL_CHAIN (tmpl);
12045 return false;
12048 /* Template template parameters are dependent. */
12049 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12050 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12051 return true;
12052 /* So are names that have not been looked up. */
12053 if (TREE_CODE (tmpl) == SCOPE_REF
12054 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12055 return true;
12056 /* So are member templates of dependent classes. */
12057 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12058 return dependent_type_p (DECL_CONTEXT (tmpl));
12059 return false;
12062 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12064 bool
12065 dependent_template_id_p (tree tmpl, tree args)
12067 return (dependent_template_p (tmpl)
12068 || any_dependent_template_arguments_p (args));
12071 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12072 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12073 can be found. Note that this function peers inside uninstantiated
12074 templates and therefore should be used only in extremely limited
12075 situations. */
12077 tree
12078 resolve_typename_type (tree type, bool only_current_p)
12080 tree scope;
12081 tree name;
12082 tree decl;
12083 int quals;
12084 bool pop_p;
12086 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12088 scope = TYPE_CONTEXT (type);
12089 name = TYPE_IDENTIFIER (type);
12091 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12092 it first before we can figure out what NAME refers to. */
12093 if (TREE_CODE (scope) == TYPENAME_TYPE)
12094 scope = resolve_typename_type (scope, only_current_p);
12095 /* If we don't know what SCOPE refers to, then we cannot resolve the
12096 TYPENAME_TYPE. */
12097 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12098 return error_mark_node;
12099 /* If the SCOPE is a template type parameter, we have no way of
12100 resolving the name. */
12101 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12102 return type;
12103 /* If the SCOPE is not the current instantiation, there's no reason
12104 to look inside it. */
12105 if (only_current_p && !currently_open_class (scope))
12106 return error_mark_node;
12107 /* If SCOPE is a partial instantiation, it will not have a valid
12108 TYPE_FIELDS list, so use the original template. */
12109 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12110 /* Enter the SCOPE so that name lookup will be resolved as if we
12111 were in the class definition. In particular, SCOPE will no
12112 longer be considered a dependent type. */
12113 pop_p = push_scope (scope);
12114 /* Look up the declaration. */
12115 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12116 /* Obtain the set of qualifiers applied to the TYPE. */
12117 quals = cp_type_quals (type);
12118 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12119 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12120 if (!decl)
12121 type = error_mark_node;
12122 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12123 && TREE_CODE (decl) == TYPE_DECL)
12124 type = TREE_TYPE (decl);
12125 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12126 && DECL_CLASS_TEMPLATE_P (decl))
12128 tree tmpl;
12129 tree args;
12130 /* Obtain the template and the arguments. */
12131 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12132 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12133 /* Instantiate the template. */
12134 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12135 /*entering_scope=*/0, tf_error | tf_user);
12137 else
12138 type = error_mark_node;
12139 /* Qualify the resulting type. */
12140 if (type != error_mark_node && quals)
12141 type = cp_build_qualified_type (type, quals);
12142 /* Leave the SCOPE. */
12143 if (pop_p)
12144 pop_scope (scope);
12146 return type;
12149 /* EXPR is an expression which is not type-dependent. Return a proxy
12150 for EXPR that can be used to compute the types of larger
12151 expressions containing EXPR. */
12153 tree
12154 build_non_dependent_expr (tree expr)
12156 tree inner_expr;
12158 /* Preserve null pointer constants so that the type of things like
12159 "p == 0" where "p" is a pointer can be determined. */
12160 if (null_ptr_cst_p (expr))
12161 return expr;
12162 /* Preserve OVERLOADs; the functions must be available to resolve
12163 types. */
12164 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12165 TREE_OPERAND (expr, 0) : expr);
12166 if (TREE_CODE (inner_expr) == OVERLOAD
12167 || TREE_CODE (inner_expr) == FUNCTION_DECL
12168 || TREE_CODE (inner_expr) == TEMPLATE_DECL
12169 || TREE_CODE (inner_expr) == TEMPLATE_ID_EXPR)
12170 return expr;
12171 /* Preserve string constants; conversions from string constants to
12172 "char *" are allowed, even though normally a "const char *"
12173 cannot be used to initialize a "char *". */
12174 if (TREE_CODE (expr) == STRING_CST)
12175 return expr;
12176 /* Preserve arithmetic constants, as an optimization -- there is no
12177 reason to create a new node. */
12178 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12179 return expr;
12180 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12181 There is at least one place where we want to know that a
12182 particular expression is a throw-expression: when checking a ?:
12183 expression, there are special rules if the second or third
12184 argument is a throw-expression. */
12185 if (TREE_CODE (expr) == THROW_EXPR)
12186 return expr;
12188 if (TREE_CODE (expr) == COND_EXPR)
12189 return build3 (COND_EXPR,
12190 TREE_TYPE (expr),
12191 TREE_OPERAND (expr, 0),
12192 (TREE_OPERAND (expr, 1)
12193 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12194 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12195 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12196 if (TREE_CODE (expr) == COMPOUND_EXPR
12197 && !COMPOUND_EXPR_OVERLOADED (expr))
12198 return build2 (COMPOUND_EXPR,
12199 TREE_TYPE (expr),
12200 TREE_OPERAND (expr, 0),
12201 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12203 /* Otherwise, build a NON_DEPENDENT_EXPR.
12205 REFERENCE_TYPEs are not stripped for expressions in templates
12206 because doing so would play havoc with mangling. Consider, for
12207 example:
12209 template <typename T> void f<T& g>() { g(); }
12211 In the body of "f", the expression for "g" will have
12212 REFERENCE_TYPE, even though the standard says that it should
12213 not. The reason is that we must preserve the syntactic form of
12214 the expression so that mangling (say) "f<g>" inside the body of
12215 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12216 stripped here. */
12217 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12220 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12221 Return a new TREE_LIST with the various arguments replaced with
12222 equivalent non-dependent expressions. */
12224 tree
12225 build_non_dependent_args (tree args)
12227 tree a;
12228 tree new_args;
12230 new_args = NULL_TREE;
12231 for (a = args; a; a = TREE_CHAIN (a))
12232 new_args = tree_cons (NULL_TREE,
12233 build_non_dependent_expr (TREE_VALUE (a)),
12234 new_args);
12235 return nreverse (new_args);
12238 #include "gt-cp-pt.h"