* trans-stmt.c (gfc_trans_simple_do): New function.
[official-gcc.git] / gcc / cp / pt.c
blob3813c38ed41c451019fdebb1c2e4785315ad11bb
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 static void push_access_scope (tree);
91 static void pop_access_scope (tree);
92 static int resolve_overloaded_unification (tree, tree, tree, tree,
93 unification_kind_t, int);
94 static int try_one_overload (tree, tree, tree, tree, tree,
95 unification_kind_t, int, bool);
96 static int unify (tree, tree, tree, tree, int);
97 static void add_pending_template (tree);
98 static void reopen_tinst_level (tree);
99 static tree classtype_mangled_name (tree);
100 static char* mangle_class_name_for_template (const char *, tree, tree);
101 static tree tsubst_initializer_list (tree, tree);
102 static tree get_class_bindings (tree, tree, tree);
103 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
104 static void tsubst_enum (tree, tree, tree);
105 static tree add_to_template_args (tree, tree);
106 static tree add_outermost_template_args (tree, tree);
107 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
108 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
109 static int type_unification_real (tree, tree, tree, tree,
110 int, unification_kind_t, int, int);
111 static void note_template_header (int);
112 static tree convert_nontype_argument (tree, tree);
113 static tree convert_template_argument (tree, tree, tree,
114 tsubst_flags_t, int, tree);
115 static tree get_bindings_overload (tree, tree, tree);
116 static int for_each_template_parm (tree, tree_fn_t, void*, htab_t);
117 static tree build_template_parm_index (int, int, int, tree, tree);
118 static int inline_needs_template_parms (tree);
119 static void push_inline_template_parms_recursive (tree, int);
120 static tree retrieve_specialization (tree, tree);
121 static tree retrieve_local_specialization (tree);
122 static tree register_specialization (tree, tree, tree);
123 static void register_local_specialization (tree, tree);
124 static tree reduce_template_parm_level (tree, tree, int);
125 static tree build_template_decl (tree, tree);
126 static int mark_template_parm (tree, void *);
127 static int template_parm_this_level_p (tree, void *);
128 static tree tsubst_friend_function (tree, tree);
129 static tree tsubst_friend_class (tree, tree);
130 static int can_complete_type_without_circularity (tree);
131 static tree get_bindings (tree, tree, tree);
132 static tree get_bindings_real (tree, tree, tree, int, int, int);
133 static int template_decl_level (tree);
134 static int check_cv_quals_for_unify (int, tree, tree);
135 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
136 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
137 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
138 static void regenerate_decl_from_template (tree, tree);
139 static tree most_specialized (tree, tree, tree);
140 static tree most_specialized_class (tree, tree);
141 static int template_class_depth_real (tree, int);
142 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143 static tree tsubst_decl (tree, tree, tree, tsubst_flags_t);
144 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
145 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
146 static void check_specialization_scope (void);
147 static tree process_partial_specialization (tree);
148 static void set_current_access_from_decl (tree);
149 static void check_default_tmpl_args (tree, tree, int, int);
150 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
151 static tree get_template_base (tree, tree, tree, tree);
152 static int verify_class_unification (tree, tree, tree);
153 static tree try_class_unification (tree, tree, tree, tree);
154 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
155 tree, tree);
156 static tree determine_specialization (tree, tree, tree *, int, int);
157 static int template_args_equal (tree, tree);
158 static void tsubst_default_arguments (tree);
159 static tree for_each_template_parm_r (tree *, int *, void *);
160 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
161 static void copy_default_args_to_explicit_spec (tree);
162 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
163 static int eq_local_specializations (const void *, const void *);
164 static bool dependent_type_p_r (tree);
165 static tree tsubst (tree, tree, tsubst_flags_t, tree);
166 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
167 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
169 /* Make the current scope suitable for access checking when we are
170 processing T. T can be FUNCTION_DECL for instantiated function
171 template, or VAR_DECL for static member variable (need by
172 instantiate_decl). */
174 static void
175 push_access_scope (tree t)
177 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
178 || TREE_CODE (t) == VAR_DECL);
180 if (DECL_CLASS_SCOPE_P (t))
181 push_nested_class (DECL_CONTEXT (t));
182 else
183 push_to_top_level ();
185 if (TREE_CODE (t) == FUNCTION_DECL)
187 saved_access_scope = tree_cons
188 (NULL_TREE, current_function_decl, saved_access_scope);
189 current_function_decl = t;
193 /* Restore the scope set up by push_access_scope. T is the node we
194 are processing. */
196 static void
197 pop_access_scope (tree t)
199 if (TREE_CODE (t) == FUNCTION_DECL)
201 current_function_decl = TREE_VALUE (saved_access_scope);
202 saved_access_scope = TREE_CHAIN (saved_access_scope);
205 if (DECL_CLASS_SCOPE_P (t))
206 pop_nested_class ();
207 else
208 pop_from_top_level ();
211 /* Do any processing required when DECL (a member template
212 declaration) is finished. Returns the TEMPLATE_DECL corresponding
213 to DECL, unless it is a specialization, in which case the DECL
214 itself is returned. */
216 tree
217 finish_member_template_decl (tree decl)
219 if (decl == error_mark_node)
220 return error_mark_node;
222 gcc_assert (DECL_P (decl));
224 if (TREE_CODE (decl) == TYPE_DECL)
226 tree type;
228 type = TREE_TYPE (decl);
229 if (IS_AGGR_TYPE (type)
230 && CLASSTYPE_TEMPLATE_INFO (type)
231 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
233 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
234 check_member_template (tmpl);
235 return tmpl;
237 return NULL_TREE;
239 else if (TREE_CODE (decl) == FIELD_DECL)
240 error ("data member %qD cannot be a member template", decl);
241 else if (DECL_TEMPLATE_INFO (decl))
243 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
245 check_member_template (DECL_TI_TEMPLATE (decl));
246 return DECL_TI_TEMPLATE (decl);
248 else
249 return decl;
251 else
252 error ("invalid member template declaration %qD", decl);
254 return error_mark_node;
257 /* Returns the template nesting level of the indicated class TYPE.
259 For example, in:
260 template <class T>
261 struct A
263 template <class U>
264 struct B {};
267 A<T>::B<U> has depth two, while A<T> has depth one.
268 Both A<T>::B<int> and A<int>::B<U> have depth one, if
269 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
270 specializations.
272 This function is guaranteed to return 0 if passed NULL_TREE so
273 that, for example, `template_class_depth (current_class_type)' is
274 always safe. */
276 static int
277 template_class_depth_real (tree type, int count_specializations)
279 int depth;
281 for (depth = 0;
282 type && TREE_CODE (type) != NAMESPACE_DECL;
283 type = (TREE_CODE (type) == FUNCTION_DECL)
284 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
286 if (TREE_CODE (type) != FUNCTION_DECL)
288 if (CLASSTYPE_TEMPLATE_INFO (type)
289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
290 && ((count_specializations
291 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
292 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
293 ++depth;
295 else
297 if (DECL_TEMPLATE_INFO (type)
298 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
299 && ((count_specializations
300 && DECL_TEMPLATE_SPECIALIZATION (type))
301 || uses_template_parms (DECL_TI_ARGS (type))))
302 ++depth;
306 return depth;
309 /* Returns the template nesting level of the indicated class TYPE.
310 Like template_class_depth_real, but instantiations do not count in
311 the depth. */
313 int
314 template_class_depth (tree type)
316 return template_class_depth_real (type, /*count_specializations=*/0);
319 /* Returns 1 if processing DECL as part of do_pending_inlines
320 needs us to push template parms. */
322 static int
323 inline_needs_template_parms (tree decl)
325 if (! DECL_TEMPLATE_INFO (decl))
326 return 0;
328 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
329 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
332 /* Subroutine of maybe_begin_member_template_processing.
333 Push the template parms in PARMS, starting from LEVELS steps into the
334 chain, and ending at the beginning, since template parms are listed
335 innermost first. */
337 static void
338 push_inline_template_parms_recursive (tree parmlist, int levels)
340 tree parms = TREE_VALUE (parmlist);
341 int i;
343 if (levels > 1)
344 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
346 ++processing_template_decl;
347 current_template_parms
348 = tree_cons (size_int (processing_template_decl),
349 parms, current_template_parms);
350 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
352 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
353 NULL);
354 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
356 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
357 gcc_assert (DECL_P (parm));
359 switch (TREE_CODE (parm))
361 case TYPE_DECL:
362 case TEMPLATE_DECL:
363 pushdecl (parm);
364 break;
366 case PARM_DECL:
368 /* Make a CONST_DECL as is done in process_template_parm.
369 It is ugly that we recreate this here; the original
370 version built in process_template_parm is no longer
371 available. */
372 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
373 TREE_TYPE (parm));
374 DECL_ARTIFICIAL (decl) = 1;
375 TREE_CONSTANT (decl) = 1;
376 TREE_INVARIANT (decl) = 1;
377 TREE_READONLY (decl) = 1;
378 DECL_INITIAL (decl) = DECL_INITIAL (parm);
379 SET_DECL_TEMPLATE_PARM_P (decl);
380 pushdecl (decl);
382 break;
384 default:
385 gcc_unreachable ();
390 /* Restore the template parameter context for a member template or
391 a friend template defined in a class definition. */
393 void
394 maybe_begin_member_template_processing (tree decl)
396 tree parms;
397 int levels = 0;
399 if (inline_needs_template_parms (decl))
401 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
402 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
404 if (DECL_TEMPLATE_SPECIALIZATION (decl))
406 --levels;
407 parms = TREE_CHAIN (parms);
410 push_inline_template_parms_recursive (parms, levels);
413 /* Remember how many levels of template parameters we pushed so that
414 we can pop them later. */
415 if (!inline_parm_levels)
416 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
417 if (inline_parm_levels_used == inline_parm_levels->num_elements)
418 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
419 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
420 ++inline_parm_levels_used;
423 /* Undo the effects of begin_member_template_processing. */
425 void
426 maybe_end_member_template_processing (void)
428 int i;
430 if (!inline_parm_levels_used)
431 return;
433 --inline_parm_levels_used;
434 for (i = 0;
435 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
436 ++i)
438 --processing_template_decl;
439 current_template_parms = TREE_CHAIN (current_template_parms);
440 poplevel (0, 0, 0);
444 /* Returns nonzero iff T is a member template function. We must be
445 careful as in
447 template <class T> class C { void f(); }
449 Here, f is a template function, and a member, but not a member
450 template. This function does not concern itself with the origin of
451 T, only its present state. So if we have
453 template <class T> class C { template <class U> void f(U); }
455 then neither C<int>::f<char> nor C<T>::f<double> is considered
456 to be a member template. But, `template <class U> void
457 C<int>::f(U)' is considered a member template. */
460 is_member_template (tree t)
462 if (!DECL_FUNCTION_TEMPLATE_P (t))
463 /* Anything that isn't a function or a template function is
464 certainly not a member template. */
465 return 0;
467 /* A local class can't have member templates. */
468 if (decl_function_context (t))
469 return 0;
471 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
472 /* If there are more levels of template parameters than
473 there are template classes surrounding the declaration,
474 then we have a member template. */
475 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
476 template_class_depth (DECL_CONTEXT (t))));
479 #if 0 /* UNUSED */
480 /* Returns nonzero iff T is a member template class. See
481 is_member_template for a description of what precisely constitutes
482 a member template. */
485 is_member_template_class (tree t)
487 if (!DECL_CLASS_TEMPLATE_P (t))
488 /* Anything that isn't a class template, is certainly not a member
489 template. */
490 return 0;
492 if (!DECL_CLASS_SCOPE_P (t))
493 /* Anything whose context isn't a class type is surely not a
494 member template. */
495 return 0;
497 /* If there are more levels of template parameters than there are
498 template classes surrounding the declaration, then we have a
499 member template. */
500 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
501 template_class_depth (DECL_CONTEXT (t)));
503 #endif
505 /* Return a new template argument vector which contains all of ARGS,
506 but has as its innermost set of arguments the EXTRA_ARGS. */
508 static tree
509 add_to_template_args (tree args, tree extra_args)
511 tree new_args;
512 int extra_depth;
513 int i;
514 int j;
516 extra_depth = TMPL_ARGS_DEPTH (extra_args);
517 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
519 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
520 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
522 for (j = 1; j <= extra_depth; ++j, ++i)
523 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
525 return new_args;
528 /* Like add_to_template_args, but only the outermost ARGS are added to
529 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
530 (EXTRA_ARGS) levels are added. This function is used to combine
531 the template arguments from a partial instantiation with the
532 template arguments used to attain the full instantiation from the
533 partial instantiation. */
535 static tree
536 add_outermost_template_args (tree args, tree extra_args)
538 tree new_args;
540 /* If there are more levels of EXTRA_ARGS than there are ARGS,
541 something very fishy is going on. */
542 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
544 /* If *all* the new arguments will be the EXTRA_ARGS, just return
545 them. */
546 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
547 return extra_args;
549 /* For the moment, we make ARGS look like it contains fewer levels. */
550 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
552 new_args = add_to_template_args (args, extra_args);
554 /* Now, we restore ARGS to its full dimensions. */
555 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
557 return new_args;
560 /* Return the N levels of innermost template arguments from the ARGS. */
562 tree
563 get_innermost_template_args (tree args, int n)
565 tree new_args;
566 int extra_levels;
567 int i;
569 gcc_assert (n >= 0);
571 /* If N is 1, just return the innermost set of template arguments. */
572 if (n == 1)
573 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
575 /* If we're not removing anything, just return the arguments we were
576 given. */
577 extra_levels = TMPL_ARGS_DEPTH (args) - n;
578 gcc_assert (extra_levels >= 0);
579 if (extra_levels == 0)
580 return args;
582 /* Make a new set of arguments, not containing the outer arguments. */
583 new_args = make_tree_vec (n);
584 for (i = 1; i <= n; ++i)
585 SET_TMPL_ARGS_LEVEL (new_args, i,
586 TMPL_ARGS_LEVEL (args, i + extra_levels));
588 return new_args;
591 /* We've got a template header coming up; push to a new level for storing
592 the parms. */
594 void
595 begin_template_parm_list (void)
597 /* We use a non-tag-transparent scope here, which causes pushtag to
598 put tags in this scope, rather than in the enclosing class or
599 namespace scope. This is the right thing, since we want
600 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
601 global template class, push_template_decl handles putting the
602 TEMPLATE_DECL into top-level scope. For a nested template class,
603 e.g.:
605 template <class T> struct S1 {
606 template <class T> struct S2 {};
609 pushtag contains special code to call pushdecl_with_scope on the
610 TEMPLATE_DECL for S2. */
611 begin_scope (sk_template_parms, NULL);
612 ++processing_template_decl;
613 ++processing_template_parmlist;
614 note_template_header (0);
617 /* This routine is called when a specialization is declared. If it is
618 invalid to declare a specialization here, an error is reported. */
620 static void
621 check_specialization_scope (void)
623 tree scope = current_scope ();
625 /* [temp.expl.spec]
627 An explicit specialization shall be declared in the namespace of
628 which the template is a member, or, for member templates, in the
629 namespace of which the enclosing class or enclosing class
630 template is a member. An explicit specialization of a member
631 function, member class or static data member of a class template
632 shall be declared in the namespace of which the class template
633 is a member. */
634 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
635 error ("explicit specialization in non-namespace scope %qD", scope);
637 /* [temp.expl.spec]
639 In an explicit specialization declaration for a member of a class
640 template or a member template that appears in namespace scope,
641 the member template and some of its enclosing class templates may
642 remain unspecialized, except that the declaration shall not
643 explicitly specialize a class member template if its enclosing
644 class templates are not explicitly specialized as well. */
645 if (current_template_parms)
646 error ("enclosing class templates are not explicitly specialized");
649 /* We've just seen template <>. */
651 void
652 begin_specialization (void)
654 begin_scope (sk_template_spec, NULL);
655 note_template_header (1);
656 check_specialization_scope ();
659 /* Called at then end of processing a declaration preceded by
660 template<>. */
662 void
663 end_specialization (void)
665 finish_scope ();
666 reset_specialization ();
669 /* Any template <>'s that we have seen thus far are not referring to a
670 function specialization. */
672 void
673 reset_specialization (void)
675 processing_specialization = 0;
676 template_header_count = 0;
679 /* We've just seen a template header. If SPECIALIZATION is nonzero,
680 it was of the form template <>. */
682 static void
683 note_template_header (int specialization)
685 processing_specialization = specialization;
686 template_header_count++;
689 /* We're beginning an explicit instantiation. */
691 void
692 begin_explicit_instantiation (void)
694 gcc_assert (!processing_explicit_instantiation);
695 processing_explicit_instantiation = true;
699 void
700 end_explicit_instantiation (void)
702 gcc_assert (processing_explicit_instantiation);
703 processing_explicit_instantiation = false;
706 /* A explicit specialization or partial specialization TMPL is being
707 declared. Check that the namespace in which the specialization is
708 occurring is permissible. Returns false iff it is invalid to
709 specialize TMPL in the current namespace. */
711 static bool
712 check_specialization_namespace (tree tmpl)
714 tree tpl_ns = decl_namespace_context (tmpl);
716 /* [tmpl.expl.spec]
718 An explicit specialization shall be declared in the namespace of
719 which the template is a member, or, for member templates, in the
720 namespace of which the enclosing class or enclosing class
721 template is a member. An explicit specialization of a member
722 function, member class or static data member of a class template
723 shall be declared in the namespace of which the class template is
724 a member. */
725 if (is_associated_namespace (current_namespace, tpl_ns))
726 /* Same or super-using namespace. */
727 return true;
728 else
730 pedwarn ("specialization of %qD in different namespace", tmpl);
731 cp_pedwarn_at (" from definition of %q#D", tmpl);
732 return false;
736 /* The TYPE is being declared. If it is a template type, that means it
737 is a partial specialization. Do appropriate error-checking. */
739 void
740 maybe_process_partial_specialization (tree type)
742 /* TYPE maybe an ERROR_MARK_NODE. */
743 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
745 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
747 /* This is for ordinary explicit specialization and partial
748 specialization of a template class such as:
750 template <> class C<int>;
754 template <class T> class C<T*>;
756 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
758 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
759 && !COMPLETE_TYPE_P (type))
761 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
762 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
763 if (processing_template_decl)
764 push_template_decl (TYPE_MAIN_DECL (type));
766 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
767 error ("specialization of %qT after instantiation", type);
769 else if (CLASS_TYPE_P (type)
770 && !CLASSTYPE_USE_TEMPLATE (type)
771 && CLASSTYPE_TEMPLATE_INFO (type)
772 && context && CLASS_TYPE_P (context)
773 && CLASSTYPE_TEMPLATE_INFO (context))
775 /* This is for an explicit specialization of member class
776 template according to [temp.expl.spec/18]:
778 template <> template <class U> class C<int>::D;
780 The context `C<int>' must be an implicit instantiation.
781 Otherwise this is just a member class template declared
782 earlier like:
784 template <> class C<int> { template <class U> class D; };
785 template <> template <class U> class C<int>::D;
787 In the first case, `C<int>::D' is a specialization of `C<T>::D'
788 while in the second case, `C<int>::D' is a primary template
789 and `C<T>::D' may not exist. */
791 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
792 && !COMPLETE_TYPE_P (type))
794 tree t;
796 if (current_namespace
797 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
799 pedwarn ("specializing %q#T in different namespace", type);
800 cp_pedwarn_at (" from definition of %q#D",
801 CLASSTYPE_TI_TEMPLATE (type));
804 /* Check for invalid specialization after instantiation:
806 template <> template <> class C<int>::D<int>;
807 template <> template <class U> class C<int>::D; */
809 for (t = DECL_TEMPLATE_INSTANTIATIONS
810 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
811 t; t = TREE_CHAIN (t))
812 if (TREE_VALUE (t) != type
813 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
814 error ("specialization %qT after instantiation %qT",
815 type, TREE_VALUE (t));
817 /* Mark TYPE as a specialization. And as a result, we only
818 have one level of template argument for the innermost
819 class template. */
820 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
821 CLASSTYPE_TI_ARGS (type)
822 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
825 else if (processing_specialization)
826 error ("explicit specialization of non-template %qT", type);
829 /* Retrieve the specialization (in the sense of [temp.spec] - a
830 specialization is either an instantiation or an explicit
831 specialization) of TMPL for the given template ARGS. If there is
832 no such specialization, return NULL_TREE. The ARGS are a vector of
833 arguments, or a vector of vectors of arguments, in the case of
834 templates with more than one level of parameters. */
836 static tree
837 retrieve_specialization (tree tmpl, tree args)
839 tree s;
841 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
843 /* There should be as many levels of arguments as there are
844 levels of parameters. */
845 gcc_assert (TMPL_ARGS_DEPTH (args)
846 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
848 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
849 s != NULL_TREE;
850 s = TREE_CHAIN (s))
851 if (comp_template_args (TREE_PURPOSE (s), args))
852 return TREE_VALUE (s);
854 return NULL_TREE;
857 /* Like retrieve_specialization, but for local declarations. */
859 static tree
860 retrieve_local_specialization (tree tmpl)
862 tree spec = htab_find_with_hash (local_specializations, tmpl,
863 htab_hash_pointer (tmpl));
864 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
867 /* Returns nonzero iff DECL is a specialization of TMPL. */
870 is_specialization_of (tree decl, tree tmpl)
872 tree t;
874 if (TREE_CODE (decl) == FUNCTION_DECL)
876 for (t = decl;
877 t != NULL_TREE;
878 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
879 if (t == tmpl)
880 return 1;
882 else
884 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
886 for (t = TREE_TYPE (decl);
887 t != NULL_TREE;
888 t = CLASSTYPE_USE_TEMPLATE (t)
889 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
890 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
891 return 1;
894 return 0;
897 /* Returns nonzero iff DECL is a specialization of friend declaration
898 FRIEND according to [temp.friend]. */
900 bool
901 is_specialization_of_friend (tree decl, tree friend)
903 bool need_template = true;
904 int template_depth;
906 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
908 /* For [temp.friend/6] when FRIEND is an ordinary member function
909 of a template class, we want to check if DECL is a specialization
910 if this. */
911 if (TREE_CODE (friend) == FUNCTION_DECL
912 && DECL_TEMPLATE_INFO (friend)
913 && !DECL_USE_TEMPLATE (friend))
915 friend = DECL_TI_TEMPLATE (friend);
916 need_template = false;
919 /* There is nothing to do if this is not a template friend. */
920 if (TREE_CODE (friend) != TEMPLATE_DECL)
921 return 0;
923 if (is_specialization_of (decl, friend))
924 return 1;
926 /* [temp.friend/6]
927 A member of a class template may be declared to be a friend of a
928 non-template class. In this case, the corresponding member of
929 every specialization of the class template is a friend of the
930 class granting friendship.
932 For example, given a template friend declaration
934 template <class T> friend void A<T>::f();
936 the member function below is considered a friend
938 template <> struct A<int> {
939 void f();
942 For this type of template friend, TEMPLATE_DEPTH below will be
943 nonzero. To determine if DECL is a friend of FRIEND, we first
944 check if the enclosing class is a specialization of another. */
946 template_depth = template_class_depth (DECL_CONTEXT (friend));
947 if (template_depth
948 && DECL_CLASS_SCOPE_P (decl)
949 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
950 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
952 /* Next, we check the members themselves. In order to handle
953 a few tricky cases like
955 template <class T> friend void A<T>::g(T t);
956 template <class T> template <T t> friend void A<T>::h();
958 we need to figure out what ARGS is (corresponding to `T' in above
959 examples) from DECL for later processing. */
961 tree context = DECL_CONTEXT (decl);
962 tree args = NULL_TREE;
963 int current_depth = 0;
964 while (current_depth < template_depth)
966 if (CLASSTYPE_TEMPLATE_INFO (context))
968 if (current_depth == 0)
969 args = TYPE_TI_ARGS (context);
970 else
971 args = add_to_template_args (TYPE_TI_ARGS (context), args);
972 current_depth++;
974 context = TYPE_CONTEXT (context);
977 if (TREE_CODE (decl) == FUNCTION_DECL)
979 bool is_template;
980 tree friend_type;
981 tree decl_type;
982 tree friend_args_type;
983 tree decl_args_type;
985 /* Make sure that both DECL and FRIEND are templates or
986 non-templates. */
987 is_template = DECL_TEMPLATE_INFO (decl)
988 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
989 if (need_template ^ is_template)
990 return 0;
991 else if (is_template)
993 /* If both are templates, check template parameter list. */
994 tree friend_parms
995 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
996 args, tf_none);
997 if (!comp_template_parms
998 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
999 friend_parms))
1000 return 0;
1002 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1004 else
1005 decl_type = TREE_TYPE (decl);
1007 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1008 tf_none, NULL_TREE);
1009 if (friend_type == error_mark_node)
1010 return 0;
1012 /* Check if return types match. */
1013 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1014 return 0;
1016 /* Check if function parameter types match, ignoring the
1017 `this' parameter. */
1018 friend_args_type = TYPE_ARG_TYPES (friend_type);
1019 decl_args_type = TYPE_ARG_TYPES (decl_type);
1020 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1021 friend_args_type = TREE_CHAIN (friend_args_type);
1022 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1023 decl_args_type = TREE_CHAIN (decl_args_type);
1024 if (compparms (decl_args_type, friend_args_type))
1025 return 1;
1028 return 0;
1031 /* Register the specialization SPEC as a specialization of TMPL with
1032 the indicated ARGS. Returns SPEC, or an equivalent prior
1033 declaration, if available. */
1035 static tree
1036 register_specialization (tree spec, tree tmpl, tree args)
1038 tree s;
1040 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1042 if (TREE_CODE (spec) == FUNCTION_DECL
1043 && uses_template_parms (DECL_TI_ARGS (spec)))
1044 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1045 register it; we want the corresponding TEMPLATE_DECL instead.
1046 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1047 the more obvious `uses_template_parms (spec)' to avoid problems
1048 with default function arguments. In particular, given
1049 something like this:
1051 template <class T> void f(T t1, T t = T())
1053 the default argument expression is not substituted for in an
1054 instantiation unless and until it is actually needed. */
1055 return spec;
1057 /* There should be as many levels of arguments as there are
1058 levels of parameters. */
1059 gcc_assert (TMPL_ARGS_DEPTH (args)
1060 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1062 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1063 s != NULL_TREE;
1064 s = TREE_CHAIN (s))
1066 tree fn = TREE_VALUE (s);
1068 /* We can sometimes try to re-register a specialization that we've
1069 already got. In particular, regenerate_decl_from_template
1070 calls duplicate_decls which will update the specialization
1071 list. But, we'll still get called again here anyhow. It's
1072 more convenient to simply allow this than to try to prevent it. */
1073 if (fn == spec)
1074 return spec;
1075 else if (DECL_TEMPLATE_SPECIALIZATION (spec)
1076 && comp_template_args (TREE_PURPOSE (s), args))
1078 if (DECL_TEMPLATE_INSTANTIATION (fn))
1080 if (TREE_USED (fn)
1081 || DECL_EXPLICIT_INSTANTIATION (fn))
1083 error ("specialization of %qD after instantiation", fn);
1084 return spec;
1086 else
1088 /* This situation should occur only if the first
1089 specialization is an implicit instantiation, the
1090 second is an explicit specialization, and the
1091 implicit instantiation has not yet been used.
1092 That situation can occur if we have implicitly
1093 instantiated a member function and then
1094 specialized it later.
1096 We can also wind up here if a friend declaration
1097 that looked like an instantiation turns out to be
1098 a specialization:
1100 template <class T> void foo(T);
1101 class S { friend void foo<>(int) };
1102 template <> void foo(int);
1104 We transform the existing DECL in place so that
1105 any pointers to it become pointers to the updated
1106 declaration.
1108 If there was a definition for the template, but
1109 not for the specialization, we want this to look
1110 as if there were no definition, and vice
1111 versa. */
1112 DECL_INITIAL (fn) = NULL_TREE;
1113 duplicate_decls (spec, fn);
1115 return fn;
1118 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1120 if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
1121 /* Dup decl failed, but this is a new definition. Set
1122 the line number so any errors match this new
1123 definition. */
1124 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1126 return fn;
1131 /* A specialization must be declared in the same namespace as the
1132 template it is specializing. */
1133 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1134 && !check_specialization_namespace (tmpl))
1135 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1137 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1138 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1140 return spec;
1143 /* Unregister the specialization SPEC as a specialization of TMPL.
1144 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1145 if the SPEC was listed as a specialization of TMPL. */
1147 bool
1148 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1150 tree* s;
1152 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1153 *s != NULL_TREE;
1154 s = &TREE_CHAIN (*s))
1155 if (TREE_VALUE (*s) == spec)
1157 if (!new_spec)
1158 *s = TREE_CHAIN (*s);
1159 else
1160 TREE_VALUE (*s) = new_spec;
1161 return 1;
1164 return 0;
1167 /* Compare an entry in the local specializations hash table P1 (which
1168 is really a pointer to a TREE_LIST) with P2 (which is really a
1169 DECL). */
1171 static int
1172 eq_local_specializations (const void *p1, const void *p2)
1174 return TREE_VALUE ((tree) p1) == (tree) p2;
1177 /* Hash P1, an entry in the local specializations table. */
1179 static hashval_t
1180 hash_local_specialization (const void* p1)
1182 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1185 /* Like register_specialization, but for local declarations. We are
1186 registering SPEC, an instantiation of TMPL. */
1188 static void
1189 register_local_specialization (tree spec, tree tmpl)
1191 void **slot;
1193 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1194 htab_hash_pointer (tmpl), INSERT);
1195 *slot = build_tree_list (spec, tmpl);
1198 /* Print the list of candidate FNS in an error message. */
1200 void
1201 print_candidates (tree fns)
1203 tree fn;
1205 const char *str = "candidates are:";
1207 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1209 tree f;
1211 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1212 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1213 str = " ";
1217 /* Returns the template (one of the functions given by TEMPLATE_ID)
1218 which can be specialized to match the indicated DECL with the
1219 explicit template args given in TEMPLATE_ID. The DECL may be
1220 NULL_TREE if none is available. In that case, the functions in
1221 TEMPLATE_ID are non-members.
1223 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1224 specialization of a member template.
1226 The TEMPLATE_COUNT is the number of references to qualifying
1227 template classes that appeared in the name of the function. See
1228 check_explicit_specialization for a more accurate description.
1230 The template args (those explicitly specified and those deduced)
1231 are output in a newly created vector *TARGS_OUT.
1233 If it is impossible to determine the result, an error message is
1234 issued. The error_mark_node is returned to indicate failure. */
1236 static tree
1237 determine_specialization (tree template_id,
1238 tree decl,
1239 tree* targs_out,
1240 int need_member_template,
1241 int template_count)
1243 tree fns;
1244 tree targs;
1245 tree explicit_targs;
1246 tree candidates = NULL_TREE;
1247 tree templates = NULL_TREE;
1248 int header_count;
1249 struct cp_binding_level *b;
1251 *targs_out = NULL_TREE;
1253 if (template_id == error_mark_node)
1254 return error_mark_node;
1256 fns = TREE_OPERAND (template_id, 0);
1257 explicit_targs = TREE_OPERAND (template_id, 1);
1259 if (fns == error_mark_node)
1260 return error_mark_node;
1262 /* Check for baselinks. */
1263 if (BASELINK_P (fns))
1264 fns = BASELINK_FUNCTIONS (fns);
1266 if (!is_overloaded_fn (fns))
1268 error ("%qD is not a function template", fns);
1269 return error_mark_node;
1272 /* Count the number of template headers specified for this
1273 specialization. */
1274 header_count = 0;
1275 for (b = current_binding_level;
1276 b->kind == sk_template_parms || b->kind == sk_template_spec;
1277 b = b->level_chain)
1278 ++header_count;
1280 for (; fns; fns = OVL_NEXT (fns))
1282 tree fn = OVL_CURRENT (fns);
1284 if (TREE_CODE (fn) == TEMPLATE_DECL)
1286 tree decl_arg_types;
1287 tree fn_arg_types;
1289 /* DECL might be a specialization of FN. */
1291 /* Adjust the type of DECL in case FN is a static member. */
1292 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1293 if (DECL_STATIC_FUNCTION_P (fn)
1294 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1295 decl_arg_types = TREE_CHAIN (decl_arg_types);
1297 /* Check that the number of function parameters matches.
1298 For example,
1299 template <class T> void f(int i = 0);
1300 template <> void f<int>();
1301 The specialization f<int> is invalid but is not caught
1302 by get_bindings below. */
1304 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1305 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1306 continue;
1308 /* For a non-static member function, we need to make sure that
1309 the const qualification is the same. This can be done by
1310 checking the 'this' in the argument list. */
1311 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1312 && !same_type_p (TREE_VALUE (fn_arg_types),
1313 TREE_VALUE (decl_arg_types)))
1314 continue;
1316 /* In case of explicit specialization, we need to check if
1317 the number of template headers appearing in the specialization
1318 is correct. This is usually done in check_explicit_specialization,
1319 but the check done there cannot be exhaustive when specializing
1320 member functions. Consider the following code:
1322 template <> void A<int>::f(int);
1323 template <> template <> void A<int>::f(int);
1325 Assuming that A<int> is not itself an explicit specialization
1326 already, the first line specializes "f" which is a non-template
1327 member function, whilst the second line specializes "f" which
1328 is a template member function. So both lines are syntactically
1329 correct, and check_explicit_specialization does not reject
1330 them.
1332 Here, we can do better, as we are matching the specialization
1333 against the declarations. We count the number of template
1334 headers, and we check if they match TEMPLATE_COUNT + 1
1335 (TEMPLATE_COUNT is the number of qualifying template classes,
1336 plus there must be another header for the member template
1337 itself).
1339 Notice that if header_count is zero, this is not a
1340 specialization but rather a template instantiation, so there
1341 is no check we can perform here. */
1342 if (header_count && header_count != template_count + 1)
1343 continue;
1345 /* See whether this function might be a specialization of this
1346 template. */
1347 targs = get_bindings (fn, decl, explicit_targs);
1349 if (!targs)
1350 /* We cannot deduce template arguments that when used to
1351 specialize TMPL will produce DECL. */
1352 continue;
1354 /* Save this template, and the arguments deduced. */
1355 templates = tree_cons (targs, fn, templates);
1357 else if (need_member_template)
1358 /* FN is an ordinary member function, and we need a
1359 specialization of a member template. */
1361 else if (TREE_CODE (fn) != FUNCTION_DECL)
1362 /* We can get IDENTIFIER_NODEs here in certain erroneous
1363 cases. */
1365 else if (!DECL_FUNCTION_MEMBER_P (fn))
1366 /* This is just an ordinary non-member function. Nothing can
1367 be a specialization of that. */
1369 else if (DECL_ARTIFICIAL (fn))
1370 /* Cannot specialize functions that are created implicitly. */
1372 else
1374 tree decl_arg_types;
1376 /* This is an ordinary member function. However, since
1377 we're here, we can assume it's enclosing class is a
1378 template class. For example,
1380 template <typename T> struct S { void f(); };
1381 template <> void S<int>::f() {}
1383 Here, S<int>::f is a non-template, but S<int> is a
1384 template class. If FN has the same type as DECL, we
1385 might be in business. */
1387 if (!DECL_TEMPLATE_INFO (fn))
1388 /* Its enclosing class is an explicit specialization
1389 of a template class. This is not a candidate. */
1390 continue;
1392 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1393 TREE_TYPE (TREE_TYPE (fn))))
1394 /* The return types differ. */
1395 continue;
1397 /* Adjust the type of DECL in case FN is a static member. */
1398 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1399 if (DECL_STATIC_FUNCTION_P (fn)
1400 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1401 decl_arg_types = TREE_CHAIN (decl_arg_types);
1403 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1404 decl_arg_types))
1405 /* They match! */
1406 candidates = tree_cons (NULL_TREE, fn, candidates);
1410 if (templates && TREE_CHAIN (templates))
1412 /* We have:
1414 [temp.expl.spec]
1416 It is possible for a specialization with a given function
1417 signature to be instantiated from more than one function
1418 template. In such cases, explicit specification of the
1419 template arguments must be used to uniquely identify the
1420 function template specialization being specialized.
1422 Note that here, there's no suggestion that we're supposed to
1423 determine which of the candidate templates is most
1424 specialized. However, we, also have:
1426 [temp.func.order]
1428 Partial ordering of overloaded function template
1429 declarations is used in the following contexts to select
1430 the function template to which a function template
1431 specialization refers:
1433 -- when an explicit specialization refers to a function
1434 template.
1436 So, we do use the partial ordering rules, at least for now.
1437 This extension can only serve to make invalid programs valid,
1438 so it's safe. And, there is strong anecdotal evidence that
1439 the committee intended the partial ordering rules to apply;
1440 the EDG front-end has that behavior, and John Spicer claims
1441 that the committee simply forgot to delete the wording in
1442 [temp.expl.spec]. */
1443 tree tmpl = most_specialized (templates, decl, explicit_targs);
1444 if (tmpl && tmpl != error_mark_node)
1446 targs = get_bindings (tmpl, decl, explicit_targs);
1447 templates = tree_cons (targs, tmpl, NULL_TREE);
1451 if (templates == NULL_TREE && candidates == NULL_TREE)
1453 cp_error_at ("template-id %qD for %q+D does not match any template "
1454 "declaration",
1455 template_id, decl);
1456 return error_mark_node;
1458 else if ((templates && TREE_CHAIN (templates))
1459 || (candidates && TREE_CHAIN (candidates))
1460 || (templates && candidates))
1462 cp_error_at ("ambiguous template specialization %qD for %q+D",
1463 template_id, decl);
1464 chainon (candidates, templates);
1465 print_candidates (candidates);
1466 return error_mark_node;
1469 /* We have one, and exactly one, match. */
1470 if (candidates)
1472 /* It was a specialization of an ordinary member function in a
1473 template class. */
1474 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1475 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1478 /* It was a specialization of a template. */
1479 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1480 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1482 *targs_out = copy_node (targs);
1483 SET_TMPL_ARGS_LEVEL (*targs_out,
1484 TMPL_ARGS_DEPTH (*targs_out),
1485 TREE_PURPOSE (templates));
1487 else
1488 *targs_out = TREE_PURPOSE (templates);
1489 return TREE_VALUE (templates);
1492 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1493 but with the default argument values filled in from those in the
1494 TMPL_TYPES. */
1496 static tree
1497 copy_default_args_to_explicit_spec_1 (tree spec_types,
1498 tree tmpl_types)
1500 tree new_spec_types;
1502 if (!spec_types)
1503 return NULL_TREE;
1505 if (spec_types == void_list_node)
1506 return void_list_node;
1508 /* Substitute into the rest of the list. */
1509 new_spec_types =
1510 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1511 TREE_CHAIN (tmpl_types));
1513 /* Add the default argument for this parameter. */
1514 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1515 TREE_VALUE (spec_types),
1516 new_spec_types);
1519 /* DECL is an explicit specialization. Replicate default arguments
1520 from the template it specializes. (That way, code like:
1522 template <class T> void f(T = 3);
1523 template <> void f(double);
1524 void g () { f (); }
1526 works, as required.) An alternative approach would be to look up
1527 the correct default arguments at the call-site, but this approach
1528 is consistent with how implicit instantiations are handled. */
1530 static void
1531 copy_default_args_to_explicit_spec (tree decl)
1533 tree tmpl;
1534 tree spec_types;
1535 tree tmpl_types;
1536 tree new_spec_types;
1537 tree old_type;
1538 tree new_type;
1539 tree t;
1540 tree object_type = NULL_TREE;
1541 tree in_charge = NULL_TREE;
1542 tree vtt = NULL_TREE;
1544 /* See if there's anything we need to do. */
1545 tmpl = DECL_TI_TEMPLATE (decl);
1546 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1547 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1548 if (TREE_PURPOSE (t))
1549 break;
1550 if (!t)
1551 return;
1553 old_type = TREE_TYPE (decl);
1554 spec_types = TYPE_ARG_TYPES (old_type);
1556 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1558 /* Remove the this pointer, but remember the object's type for
1559 CV quals. */
1560 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1561 spec_types = TREE_CHAIN (spec_types);
1562 tmpl_types = TREE_CHAIN (tmpl_types);
1564 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1566 /* DECL may contain more parameters than TMPL due to the extra
1567 in-charge parameter in constructors and destructors. */
1568 in_charge = spec_types;
1569 spec_types = TREE_CHAIN (spec_types);
1571 if (DECL_HAS_VTT_PARM_P (decl))
1573 vtt = spec_types;
1574 spec_types = TREE_CHAIN (spec_types);
1578 /* Compute the merged default arguments. */
1579 new_spec_types =
1580 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1582 /* Compute the new FUNCTION_TYPE. */
1583 if (object_type)
1585 if (vtt)
1586 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1587 TREE_VALUE (vtt),
1588 new_spec_types);
1590 if (in_charge)
1591 /* Put the in-charge parameter back. */
1592 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1593 TREE_VALUE (in_charge),
1594 new_spec_types);
1596 new_type = build_method_type_directly (object_type,
1597 TREE_TYPE (old_type),
1598 new_spec_types);
1600 else
1601 new_type = build_function_type (TREE_TYPE (old_type),
1602 new_spec_types);
1603 new_type = cp_build_type_attribute_variant (new_type,
1604 TYPE_ATTRIBUTES (old_type));
1605 new_type = build_exception_variant (new_type,
1606 TYPE_RAISES_EXCEPTIONS (old_type));
1607 TREE_TYPE (decl) = new_type;
1610 /* Check to see if the function just declared, as indicated in
1611 DECLARATOR, and in DECL, is a specialization of a function
1612 template. We may also discover that the declaration is an explicit
1613 instantiation at this point.
1615 Returns DECL, or an equivalent declaration that should be used
1616 instead if all goes well. Issues an error message if something is
1617 amiss. Returns error_mark_node if the error is not easily
1618 recoverable.
1620 FLAGS is a bitmask consisting of the following flags:
1622 2: The function has a definition.
1623 4: The function is a friend.
1625 The TEMPLATE_COUNT is the number of references to qualifying
1626 template classes that appeared in the name of the function. For
1627 example, in
1629 template <class T> struct S { void f(); };
1630 void S<int>::f();
1632 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1633 classes are not counted in the TEMPLATE_COUNT, so that in
1635 template <class T> struct S {};
1636 template <> struct S<int> { void f(); }
1637 template <> void S<int>::f();
1639 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1640 invalid; there should be no template <>.)
1642 If the function is a specialization, it is marked as such via
1643 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1644 is set up correctly, and it is added to the list of specializations
1645 for that template. */
1647 tree
1648 check_explicit_specialization (tree declarator,
1649 tree decl,
1650 int template_count,
1651 int flags)
1653 int have_def = flags & 2;
1654 int is_friend = flags & 4;
1655 int specialization = 0;
1656 int explicit_instantiation = 0;
1657 int member_specialization = 0;
1658 tree ctype = DECL_CLASS_CONTEXT (decl);
1659 tree dname = DECL_NAME (decl);
1660 tmpl_spec_kind tsk;
1662 tsk = current_tmpl_spec_kind (template_count);
1664 switch (tsk)
1666 case tsk_none:
1667 if (processing_specialization)
1669 specialization = 1;
1670 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1672 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1674 if (is_friend)
1675 /* This could be something like:
1677 template <class T> void f(T);
1678 class S { friend void f<>(int); } */
1679 specialization = 1;
1680 else
1682 /* This case handles bogus declarations like template <>
1683 template <class T> void f<int>(); */
1685 error ("template-id %qD in declaration of primary template",
1686 declarator);
1687 return decl;
1690 break;
1692 case tsk_invalid_member_spec:
1693 /* The error has already been reported in
1694 check_specialization_scope. */
1695 return error_mark_node;
1697 case tsk_invalid_expl_inst:
1698 error ("template parameter list used in explicit instantiation");
1700 /* Fall through. */
1702 case tsk_expl_inst:
1703 if (have_def)
1704 error ("definition provided for explicit instantiation");
1706 explicit_instantiation = 1;
1707 break;
1709 case tsk_excessive_parms:
1710 case tsk_insufficient_parms:
1711 if (tsk == tsk_excessive_parms)
1712 error ("too many template parameter lists in declaration of %qD",
1713 decl);
1714 else if (template_header_count)
1715 error("too few template parameter lists in declaration of %qD", decl);
1716 else
1717 error("explicit specialization of %qD must be introduced by "
1718 "`template <>'", decl);
1720 /* Fall through. */
1721 case tsk_expl_spec:
1722 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1723 if (ctype)
1724 member_specialization = 1;
1725 else
1726 specialization = 1;
1727 break;
1729 case tsk_template:
1730 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1732 /* This case handles bogus declarations like template <>
1733 template <class T> void f<int>(); */
1735 if (uses_template_parms (declarator))
1736 error ("function template partial specialization %qD "
1737 "is not allowed", declarator);
1738 else
1739 error ("template-id %qD in declaration of primary template",
1740 declarator);
1741 return decl;
1744 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1745 /* This is a specialization of a member template, without
1746 specialization the containing class. Something like:
1748 template <class T> struct S {
1749 template <class U> void f (U);
1751 template <> template <class U> void S<int>::f(U) {}
1753 That's a specialization -- but of the entire template. */
1754 specialization = 1;
1755 break;
1757 default:
1758 gcc_unreachable ();
1761 if (specialization || member_specialization)
1763 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1764 for (; t; t = TREE_CHAIN (t))
1765 if (TREE_PURPOSE (t))
1767 pedwarn
1768 ("default argument specified in explicit specialization");
1769 break;
1771 if (current_lang_name == lang_name_c)
1772 error ("template specialization with C linkage");
1775 if (specialization || member_specialization || explicit_instantiation)
1777 tree tmpl = NULL_TREE;
1778 tree targs = NULL_TREE;
1780 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1781 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1783 tree fns;
1785 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1786 if (ctype)
1787 fns = dname;
1788 else
1790 /* If there is no class context, the explicit instantiation
1791 must be at namespace scope. */
1792 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1794 /* Find the namespace binding, using the declaration
1795 context. */
1796 fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
1799 declarator = lookup_template_function (fns, NULL_TREE);
1802 if (declarator == error_mark_node)
1803 return error_mark_node;
1805 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1807 if (!explicit_instantiation)
1808 /* A specialization in class scope. This is invalid,
1809 but the error will already have been flagged by
1810 check_specialization_scope. */
1811 return error_mark_node;
1812 else
1814 /* It's not valid to write an explicit instantiation in
1815 class scope, e.g.:
1817 class C { template void f(); }
1819 This case is caught by the parser. However, on
1820 something like:
1822 template class C { void f(); };
1824 (which is invalid) we can get here. The error will be
1825 issued later. */
1829 return decl;
1831 else if (ctype != NULL_TREE
1832 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1833 IDENTIFIER_NODE))
1835 /* Find the list of functions in ctype that have the same
1836 name as the declared function. */
1837 tree name = TREE_OPERAND (declarator, 0);
1838 tree fns = NULL_TREE;
1839 int idx;
1841 if (constructor_name_p (name, ctype))
1843 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1845 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1846 : !TYPE_HAS_DESTRUCTOR (ctype))
1848 /* From [temp.expl.spec]:
1850 If such an explicit specialization for the member
1851 of a class template names an implicitly-declared
1852 special member function (clause _special_), the
1853 program is ill-formed.
1855 Similar language is found in [temp.explicit]. */
1856 error ("specialization of implicitly-declared special member function");
1857 return error_mark_node;
1860 name = is_constructor ? ctor_identifier : dtor_identifier;
1863 if (!DECL_CONV_FN_P (decl))
1865 idx = lookup_fnfields_1 (ctype, name);
1866 if (idx >= 0)
1867 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
1869 else
1871 VEC(tree) *methods;
1872 tree ovl;
1874 /* For a type-conversion operator, we cannot do a
1875 name-based lookup. We might be looking for `operator
1876 int' which will be a specialization of `operator T'.
1877 So, we find *all* the conversion operators, and then
1878 select from them. */
1879 fns = NULL_TREE;
1881 methods = CLASSTYPE_METHOD_VEC (ctype);
1882 if (methods)
1883 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1884 VEC_iterate (tree, methods, idx, ovl);
1885 ++idx)
1887 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1888 /* There are no more conversion functions. */
1889 break;
1891 /* Glue all these conversion functions together
1892 with those we already have. */
1893 for (; ovl; ovl = OVL_NEXT (ovl))
1894 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1898 if (fns == NULL_TREE)
1900 error ("no member function %qD declared in %qT", name, ctype);
1901 return error_mark_node;
1903 else
1904 TREE_OPERAND (declarator, 0) = fns;
1907 /* Figure out what exactly is being specialized at this point.
1908 Note that for an explicit instantiation, even one for a
1909 member function, we cannot tell apriori whether the
1910 instantiation is for a member template, or just a member
1911 function of a template class. Even if a member template is
1912 being instantiated, the member template arguments may be
1913 elided if they can be deduced from the rest of the
1914 declaration. */
1915 tmpl = determine_specialization (declarator, decl,
1916 &targs,
1917 member_specialization,
1918 template_count);
1920 if (!tmpl || tmpl == error_mark_node)
1921 /* We couldn't figure out what this declaration was
1922 specializing. */
1923 return error_mark_node;
1924 else
1926 tree gen_tmpl = most_general_template (tmpl);
1928 if (explicit_instantiation)
1930 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1931 is done by do_decl_instantiation later. */
1933 int arg_depth = TMPL_ARGS_DEPTH (targs);
1934 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1936 if (arg_depth > parm_depth)
1938 /* If TMPL is not the most general template (for
1939 example, if TMPL is a friend template that is
1940 injected into namespace scope), then there will
1941 be too many levels of TARGS. Remove some of them
1942 here. */
1943 int i;
1944 tree new_targs;
1946 new_targs = make_tree_vec (parm_depth);
1947 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1948 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1949 = TREE_VEC_ELT (targs, i);
1950 targs = new_targs;
1953 return instantiate_template (tmpl, targs, tf_error);
1956 /* If we thought that the DECL was a member function, but it
1957 turns out to be specializing a static member function,
1958 make DECL a static member function as well. */
1959 if (DECL_STATIC_FUNCTION_P (tmpl)
1960 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1961 revert_static_member_fn (decl);
1963 /* If this is a specialization of a member template of a
1964 template class. In we want to return the TEMPLATE_DECL,
1965 not the specialization of it. */
1966 if (tsk == tsk_template)
1968 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1969 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1970 if (have_def)
1972 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
1973 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
1974 = DECL_SOURCE_LOCATION (decl);
1976 return tmpl;
1979 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1980 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1982 /* Inherit default function arguments from the template
1983 DECL is specializing. */
1984 copy_default_args_to_explicit_spec (decl);
1986 /* This specialization has the same protection as the
1987 template it specializes. */
1988 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1989 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1991 if (is_friend && !have_def)
1992 /* This is not really a declaration of a specialization.
1993 It's just the name of an instantiation. But, it's not
1994 a request for an instantiation, either. */
1995 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1996 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1997 /* This is indeed a specialization. In case of constructors
1998 and destructors, we need in-charge and not-in-charge
1999 versions in V3 ABI. */
2000 clone_function_decl (decl, /*update_method_vec_p=*/0);
2002 /* Register this specialization so that we can find it
2003 again. */
2004 decl = register_specialization (decl, gen_tmpl, targs);
2008 return decl;
2011 /* TYPE is being declared. Verify that the use of template headers
2012 and such is reasonable. Issue error messages if not. */
2014 void
2015 maybe_check_template_type (tree type)
2017 if (template_header_count)
2019 /* We are in the scope of some `template <...>' header. */
2021 int context_depth
2022 = template_class_depth_real (TYPE_CONTEXT (type),
2023 /*count_specializations=*/1);
2025 if (template_header_count <= context_depth)
2026 /* This is OK; the template headers are for the context. We
2027 are actually too lenient here; like
2028 check_explicit_specialization we should consider the number
2029 of template types included in the actual declaration. For
2030 example,
2032 template <class T> struct S {
2033 template <class U> template <class V>
2034 struct I {};
2037 is invalid, but:
2039 template <class T> struct S {
2040 template <class U> struct I;
2043 template <class T> template <class U.
2044 struct S<T>::I {};
2046 is not. */
2048 else if (template_header_count > context_depth + 1)
2049 /* There are two many template parameter lists. */
2050 error ("too many template parameter lists in declaration of %qT", type);
2054 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2055 parameters. These are represented in the same format used for
2056 DECL_TEMPLATE_PARMS. */
2059 comp_template_parms (tree parms1, tree parms2)
2061 tree p1;
2062 tree p2;
2064 if (parms1 == parms2)
2065 return 1;
2067 for (p1 = parms1, p2 = parms2;
2068 p1 != NULL_TREE && p2 != NULL_TREE;
2069 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2071 tree t1 = TREE_VALUE (p1);
2072 tree t2 = TREE_VALUE (p2);
2073 int i;
2075 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2076 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2078 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2079 return 0;
2081 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2083 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2084 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2086 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2087 return 0;
2089 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2090 continue;
2091 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2092 return 0;
2096 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2097 /* One set of parameters has more parameters lists than the
2098 other. */
2099 return 0;
2101 return 1;
2104 /* Complain if DECL shadows a template parameter.
2106 [temp.local]: A template-parameter shall not be redeclared within its
2107 scope (including nested scopes). */
2109 void
2110 check_template_shadow (tree decl)
2112 tree olddecl;
2114 /* If we're not in a template, we can't possibly shadow a template
2115 parameter. */
2116 if (!current_template_parms)
2117 return;
2119 /* Figure out what we're shadowing. */
2120 if (TREE_CODE (decl) == OVERLOAD)
2121 decl = OVL_CURRENT (decl);
2122 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2124 /* If there's no previous binding for this name, we're not shadowing
2125 anything, let alone a template parameter. */
2126 if (!olddecl)
2127 return;
2129 /* If we're not shadowing a template parameter, we're done. Note
2130 that OLDDECL might be an OVERLOAD (or perhaps even an
2131 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2132 node. */
2133 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2134 return;
2136 /* We check for decl != olddecl to avoid bogus errors for using a
2137 name inside a class. We check TPFI to avoid duplicate errors for
2138 inline member templates. */
2139 if (decl == olddecl
2140 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2141 return;
2143 cp_error_at ("declaration of %q#D", decl);
2144 cp_error_at (" shadows template parm %q#D", olddecl);
2147 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2148 ORIG_LEVEL, DECL, and TYPE. */
2150 static tree
2151 build_template_parm_index (int index,
2152 int level,
2153 int orig_level,
2154 tree decl,
2155 tree type)
2157 tree t = make_node (TEMPLATE_PARM_INDEX);
2158 TEMPLATE_PARM_IDX (t) = index;
2159 TEMPLATE_PARM_LEVEL (t) = level;
2160 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2161 TEMPLATE_PARM_DECL (t) = decl;
2162 TREE_TYPE (t) = type;
2163 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2164 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2165 TREE_READONLY (t) = TREE_READONLY (decl);
2167 return t;
2170 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2171 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2172 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2173 new one is created. */
2175 static tree
2176 reduce_template_parm_level (tree index, tree type, int levels)
2178 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2179 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2180 != TEMPLATE_PARM_LEVEL (index) - levels))
2182 tree orig_decl = TEMPLATE_PARM_DECL (index);
2183 tree decl, t;
2185 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2186 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2187 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2188 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2189 DECL_ARTIFICIAL (decl) = 1;
2190 SET_DECL_TEMPLATE_PARM_P (decl);
2192 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2193 TEMPLATE_PARM_LEVEL (index) - levels,
2194 TEMPLATE_PARM_ORIG_LEVEL (index),
2195 decl, type);
2196 TEMPLATE_PARM_DESCENDANTS (index) = t;
2198 /* Template template parameters need this. */
2199 DECL_TEMPLATE_PARMS (decl)
2200 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2203 return TEMPLATE_PARM_DESCENDANTS (index);
2206 /* Process information from new template parameter NEXT and append it to the
2207 LIST being built. This new parameter is a non-type parameter iff
2208 IS_NON_TYPE is true. */
2210 tree
2211 process_template_parm (tree list, tree next, bool is_non_type)
2213 tree parm;
2214 tree decl = 0;
2215 tree defval;
2216 int idx;
2218 parm = next;
2219 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2220 defval = TREE_PURPOSE (parm);
2222 if (list)
2224 tree p = TREE_VALUE (tree_last (list));
2226 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2227 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2228 else
2229 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2230 ++idx;
2232 else
2233 idx = 0;
2235 if (is_non_type)
2237 parm = TREE_VALUE (parm);
2239 SET_DECL_TEMPLATE_PARM_P (parm);
2241 /* [temp.param]
2243 The top-level cv-qualifiers on the template-parameter are
2244 ignored when determining its type. */
2245 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2247 /* A template parameter is not modifiable. */
2248 TREE_CONSTANT (parm) = 1;
2249 TREE_INVARIANT (parm) = 1;
2250 TREE_READONLY (parm) = 1;
2251 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2252 TREE_TYPE (parm) = void_type_node;
2253 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2254 TREE_CONSTANT (decl) = 1;
2255 TREE_INVARIANT (decl) = 1;
2256 TREE_READONLY (decl) = 1;
2257 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2258 = build_template_parm_index (idx, processing_template_decl,
2259 processing_template_decl,
2260 decl, TREE_TYPE (parm));
2262 else
2264 tree t;
2265 parm = TREE_VALUE (TREE_VALUE (parm));
2267 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2269 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2270 /* This is for distinguishing between real templates and template
2271 template parameters */
2272 TREE_TYPE (parm) = t;
2273 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2274 decl = parm;
2276 else
2278 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2279 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2280 decl = build_decl (TYPE_DECL, parm, t);
2283 TYPE_NAME (t) = decl;
2284 TYPE_STUB_DECL (t) = decl;
2285 parm = decl;
2286 TEMPLATE_TYPE_PARM_INDEX (t)
2287 = build_template_parm_index (idx, processing_template_decl,
2288 processing_template_decl,
2289 decl, TREE_TYPE (parm));
2291 DECL_ARTIFICIAL (decl) = 1;
2292 SET_DECL_TEMPLATE_PARM_P (decl);
2293 pushdecl (decl);
2294 parm = build_tree_list (defval, parm);
2295 return chainon (list, parm);
2298 /* The end of a template parameter list has been reached. Process the
2299 tree list into a parameter vector, converting each parameter into a more
2300 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2301 as PARM_DECLs. */
2303 tree
2304 end_template_parm_list (tree parms)
2306 int nparms;
2307 tree parm, next;
2308 tree saved_parmlist = make_tree_vec (list_length (parms));
2310 current_template_parms
2311 = tree_cons (size_int (processing_template_decl),
2312 saved_parmlist, current_template_parms);
2314 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2316 next = TREE_CHAIN (parm);
2317 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2318 TREE_CHAIN (parm) = NULL_TREE;
2321 --processing_template_parmlist;
2323 return saved_parmlist;
2326 /* end_template_decl is called after a template declaration is seen. */
2328 void
2329 end_template_decl (void)
2331 reset_specialization ();
2333 if (! processing_template_decl)
2334 return;
2336 /* This matches the pushlevel in begin_template_parm_list. */
2337 finish_scope ();
2339 --processing_template_decl;
2340 current_template_parms = TREE_CHAIN (current_template_parms);
2343 /* Given a template argument vector containing the template PARMS.
2344 The innermost PARMS are given first. */
2346 tree
2347 current_template_args (void)
2349 tree header;
2350 tree args = NULL_TREE;
2351 int length = TMPL_PARMS_DEPTH (current_template_parms);
2352 int l = length;
2354 /* If there is only one level of template parameters, we do not
2355 create a TREE_VEC of TREE_VECs. Instead, we return a single
2356 TREE_VEC containing the arguments. */
2357 if (length > 1)
2358 args = make_tree_vec (length);
2360 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2362 tree a = copy_node (TREE_VALUE (header));
2363 int i;
2365 TREE_TYPE (a) = NULL_TREE;
2366 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2368 tree t = TREE_VEC_ELT (a, i);
2370 /* T will be a list if we are called from within a
2371 begin/end_template_parm_list pair, but a vector directly
2372 if within a begin/end_member_template_processing pair. */
2373 if (TREE_CODE (t) == TREE_LIST)
2375 t = TREE_VALUE (t);
2377 if (TREE_CODE (t) == TYPE_DECL
2378 || TREE_CODE (t) == TEMPLATE_DECL)
2379 t = TREE_TYPE (t);
2380 else
2381 t = DECL_INITIAL (t);
2382 TREE_VEC_ELT (a, i) = t;
2386 if (length > 1)
2387 TREE_VEC_ELT (args, --l) = a;
2388 else
2389 args = a;
2392 return args;
2395 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2396 template PARMS. Used by push_template_decl below. */
2398 static tree
2399 build_template_decl (tree decl, tree parms)
2401 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2402 DECL_TEMPLATE_PARMS (tmpl) = parms;
2403 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2404 if (DECL_LANG_SPECIFIC (decl))
2406 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2407 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2408 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2409 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2410 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2411 if (DECL_OVERLOADED_OPERATOR_P (decl))
2412 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2413 DECL_OVERLOADED_OPERATOR_P (decl));
2416 return tmpl;
2419 struct template_parm_data
2421 /* The level of the template parameters we are currently
2422 processing. */
2423 int level;
2425 /* The index of the specialization argument we are currently
2426 processing. */
2427 int current_arg;
2429 /* An array whose size is the number of template parameters. The
2430 elements are nonzero if the parameter has been used in any one
2431 of the arguments processed so far. */
2432 int* parms;
2434 /* An array whose size is the number of template arguments. The
2435 elements are nonzero if the argument makes use of template
2436 parameters of this level. */
2437 int* arg_uses_template_parms;
2440 /* Subroutine of push_template_decl used to see if each template
2441 parameter in a partial specialization is used in the explicit
2442 argument list. If T is of the LEVEL given in DATA (which is
2443 treated as a template_parm_data*), then DATA->PARMS is marked
2444 appropriately. */
2446 static int
2447 mark_template_parm (tree t, void* data)
2449 int level;
2450 int idx;
2451 struct template_parm_data* tpd = (struct template_parm_data*) data;
2453 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2455 level = TEMPLATE_PARM_LEVEL (t);
2456 idx = TEMPLATE_PARM_IDX (t);
2458 else
2460 level = TEMPLATE_TYPE_LEVEL (t);
2461 idx = TEMPLATE_TYPE_IDX (t);
2464 if (level == tpd->level)
2466 tpd->parms[idx] = 1;
2467 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2470 /* Return zero so that for_each_template_parm will continue the
2471 traversal of the tree; we want to mark *every* template parm. */
2472 return 0;
2475 /* Process the partial specialization DECL. */
2477 static tree
2478 process_partial_specialization (tree decl)
2480 tree type = TREE_TYPE (decl);
2481 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2482 tree specargs = CLASSTYPE_TI_ARGS (type);
2483 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2484 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2485 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2486 int nargs = TREE_VEC_LENGTH (inner_args);
2487 int ntparms = TREE_VEC_LENGTH (inner_parms);
2488 int i;
2489 int did_error_intro = 0;
2490 struct template_parm_data tpd;
2491 struct template_parm_data tpd2;
2493 /* We check that each of the template parameters given in the
2494 partial specialization is used in the argument list to the
2495 specialization. For example:
2497 template <class T> struct S;
2498 template <class T> struct S<T*>;
2500 The second declaration is OK because `T*' uses the template
2501 parameter T, whereas
2503 template <class T> struct S<int>;
2505 is no good. Even trickier is:
2507 template <class T>
2508 struct S1
2510 template <class U>
2511 struct S2;
2512 template <class U>
2513 struct S2<T>;
2516 The S2<T> declaration is actually invalid; it is a
2517 full-specialization. Of course,
2519 template <class U>
2520 struct S2<T (*)(U)>;
2522 or some such would have been OK. */
2523 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2524 tpd.parms = alloca (sizeof (int) * ntparms);
2525 memset (tpd.parms, 0, sizeof (int) * ntparms);
2527 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2528 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2529 for (i = 0; i < nargs; ++i)
2531 tpd.current_arg = i;
2532 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2533 &mark_template_parm,
2534 &tpd,
2535 NULL);
2537 for (i = 0; i < ntparms; ++i)
2538 if (tpd.parms[i] == 0)
2540 /* One of the template parms was not used in the
2541 specialization. */
2542 if (!did_error_intro)
2544 error ("template parameters not used in partial specialization:");
2545 did_error_intro = 1;
2548 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2551 /* [temp.class.spec]
2553 The argument list of the specialization shall not be identical to
2554 the implicit argument list of the primary template. */
2555 if (comp_template_args
2556 (inner_args,
2557 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2558 (maintmpl)))))
2559 error ("partial specialization %qT does not specialize any template arguments", type);
2561 /* [temp.class.spec]
2563 A partially specialized non-type argument expression shall not
2564 involve template parameters of the partial specialization except
2565 when the argument expression is a simple identifier.
2567 The type of a template parameter corresponding to a specialized
2568 non-type argument shall not be dependent on a parameter of the
2569 specialization. */
2570 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2571 tpd2.parms = 0;
2572 for (i = 0; i < nargs; ++i)
2574 tree arg = TREE_VEC_ELT (inner_args, i);
2575 if (/* These first two lines are the `non-type' bit. */
2576 !TYPE_P (arg)
2577 && TREE_CODE (arg) != TEMPLATE_DECL
2578 /* This next line is the `argument expression is not just a
2579 simple identifier' condition and also the `specialized
2580 non-type argument' bit. */
2581 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2583 if (tpd.arg_uses_template_parms[i])
2584 error ("template argument %qE involves template parameter(s)", arg);
2585 else
2587 /* Look at the corresponding template parameter,
2588 marking which template parameters its type depends
2589 upon. */
2590 tree type =
2591 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2592 i)));
2594 if (!tpd2.parms)
2596 /* We haven't yet initialized TPD2. Do so now. */
2597 tpd2.arg_uses_template_parms
2598 = alloca (sizeof (int) * nargs);
2599 /* The number of parameters here is the number in the
2600 main template, which, as checked in the assertion
2601 above, is NARGS. */
2602 tpd2.parms = alloca (sizeof (int) * nargs);
2603 tpd2.level =
2604 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2607 /* Mark the template parameters. But this time, we're
2608 looking for the template parameters of the main
2609 template, not in the specialization. */
2610 tpd2.current_arg = i;
2611 tpd2.arg_uses_template_parms[i] = 0;
2612 memset (tpd2.parms, 0, sizeof (int) * nargs);
2613 for_each_template_parm (type,
2614 &mark_template_parm,
2615 &tpd2,
2616 NULL);
2618 if (tpd2.arg_uses_template_parms [i])
2620 /* The type depended on some template parameters.
2621 If they are fully specialized in the
2622 specialization, that's OK. */
2623 int j;
2624 for (j = 0; j < nargs; ++j)
2625 if (tpd2.parms[j] != 0
2626 && tpd.arg_uses_template_parms [j])
2628 error ("type %qT of template argument %qE depends "
2629 "on template parameter(s)",
2630 type,
2631 arg);
2632 break;
2639 if (retrieve_specialization (maintmpl, specargs))
2640 /* We've already got this specialization. */
2641 return decl;
2643 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2644 = tree_cons (inner_args, inner_parms,
2645 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2646 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2647 return decl;
2650 /* Check that a template declaration's use of default arguments is not
2651 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2652 nonzero if DECL is the thing declared by a primary template.
2653 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2655 static void
2656 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2658 const char *msg;
2659 int last_level_to_check;
2660 tree parm_level;
2662 /* [temp.param]
2664 A default template-argument shall not be specified in a
2665 function template declaration or a function template definition, nor
2666 in the template-parameter-list of the definition of a member of a
2667 class template. */
2669 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2670 /* You can't have a function template declaration in a local
2671 scope, nor you can you define a member of a class template in a
2672 local scope. */
2673 return;
2675 if (current_class_type
2676 && !TYPE_BEING_DEFINED (current_class_type)
2677 && DECL_LANG_SPECIFIC (decl)
2678 /* If this is either a friend defined in the scope of the class
2679 or a member function. */
2680 && (DECL_FUNCTION_MEMBER_P (decl)
2681 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2682 : DECL_FRIEND_CONTEXT (decl)
2683 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2684 : false)
2685 /* And, if it was a member function, it really was defined in
2686 the scope of the class. */
2687 && (!DECL_FUNCTION_MEMBER_P (decl)
2688 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2689 /* We already checked these parameters when the template was
2690 declared, so there's no need to do it again now. This function
2691 was defined in class scope, but we're processing it's body now
2692 that the class is complete. */
2693 return;
2695 /* [temp.param]
2697 If a template-parameter has a default template-argument, all
2698 subsequent template-parameters shall have a default
2699 template-argument supplied. */
2700 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2702 tree inner_parms = TREE_VALUE (parm_level);
2703 int ntparms = TREE_VEC_LENGTH (inner_parms);
2704 int seen_def_arg_p = 0;
2705 int i;
2707 for (i = 0; i < ntparms; ++i)
2709 tree parm = TREE_VEC_ELT (inner_parms, i);
2710 if (TREE_PURPOSE (parm))
2711 seen_def_arg_p = 1;
2712 else if (seen_def_arg_p)
2714 error ("no default argument for %qD", TREE_VALUE (parm));
2715 /* For better subsequent error-recovery, we indicate that
2716 there should have been a default argument. */
2717 TREE_PURPOSE (parm) = error_mark_node;
2722 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2723 /* For an ordinary class template, default template arguments are
2724 allowed at the innermost level, e.g.:
2725 template <class T = int>
2726 struct S {};
2727 but, in a partial specialization, they're not allowed even
2728 there, as we have in [temp.class.spec]:
2730 The template parameter list of a specialization shall not
2731 contain default template argument values.
2733 So, for a partial specialization, or for a function template,
2734 we look at all of them. */
2736 else
2737 /* But, for a primary class template that is not a partial
2738 specialization we look at all template parameters except the
2739 innermost ones. */
2740 parms = TREE_CHAIN (parms);
2742 /* Figure out what error message to issue. */
2743 if (TREE_CODE (decl) == FUNCTION_DECL)
2744 msg = "default template arguments may not be used in function templates";
2745 else if (is_partial)
2746 msg = "default template arguments may not be used in partial specializations";
2747 else
2748 msg = "default argument for template parameter for class enclosing %qD";
2750 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2751 /* If we're inside a class definition, there's no need to
2752 examine the parameters to the class itself. On the one
2753 hand, they will be checked when the class is defined, and,
2754 on the other, default arguments are valid in things like:
2755 template <class T = double>
2756 struct S { template <class U> void f(U); };
2757 Here the default argument for `S' has no bearing on the
2758 declaration of `f'. */
2759 last_level_to_check = template_class_depth (current_class_type) + 1;
2760 else
2761 /* Check everything. */
2762 last_level_to_check = 0;
2764 for (parm_level = parms;
2765 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2766 parm_level = TREE_CHAIN (parm_level))
2768 tree inner_parms = TREE_VALUE (parm_level);
2769 int i;
2770 int ntparms;
2772 ntparms = TREE_VEC_LENGTH (inner_parms);
2773 for (i = 0; i < ntparms; ++i)
2774 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2776 if (msg)
2778 error (msg, decl);
2779 msg = 0;
2782 /* Clear out the default argument so that we are not
2783 confused later. */
2784 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2787 /* At this point, if we're still interested in issuing messages,
2788 they must apply to classes surrounding the object declared. */
2789 if (msg)
2790 msg = "default argument for template parameter for class enclosing %qD";
2794 /* Worker for push_template_decl_real, called via
2795 for_each_template_parm. DATA is really an int, indicating the
2796 level of the parameters we are interested in. If T is a template
2797 parameter of that level, return nonzero. */
2799 static int
2800 template_parm_this_level_p (tree t, void* data)
2802 int this_level = *(int *)data;
2803 int level;
2805 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2806 level = TEMPLATE_PARM_LEVEL (t);
2807 else
2808 level = TEMPLATE_TYPE_LEVEL (t);
2809 return level == this_level;
2812 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2813 parameters given by current_template_args, or reuses a
2814 previously existing one, if appropriate. Returns the DECL, or an
2815 equivalent one, if it is replaced via a call to duplicate_decls.
2817 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2819 tree
2820 push_template_decl_real (tree decl, int is_friend)
2822 tree tmpl;
2823 tree args;
2824 tree info;
2825 tree ctx;
2826 int primary;
2827 int is_partial;
2828 int new_template_p = 0;
2830 if (decl == error_mark_node)
2831 return decl;
2833 /* See if this is a partial specialization. */
2834 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2835 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2836 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2838 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2840 if (is_friend)
2841 /* For a friend, we want the context of the friend function, not
2842 the type of which it is a friend. */
2843 ctx = DECL_CONTEXT (decl);
2844 else if (CP_DECL_CONTEXT (decl)
2845 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2846 /* In the case of a virtual function, we want the class in which
2847 it is defined. */
2848 ctx = CP_DECL_CONTEXT (decl);
2849 else
2850 /* Otherwise, if we're currently defining some class, the DECL
2851 is assumed to be a member of the class. */
2852 ctx = current_scope ();
2854 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2855 ctx = NULL_TREE;
2857 if (!DECL_CONTEXT (decl))
2858 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2860 /* See if this is a primary template. */
2861 primary = template_parm_scope_p ();
2863 if (primary)
2865 if (current_lang_name == lang_name_c)
2866 error ("template with C linkage");
2867 else if (TREE_CODE (decl) == TYPE_DECL
2868 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2869 error ("template class without a name");
2870 else if (TREE_CODE (decl) == FUNCTION_DECL)
2872 if (DECL_DESTRUCTOR_P (decl))
2874 /* [temp.mem]
2876 A destructor shall not be a member template. */
2877 error ("destructor %qD declared as member template", decl);
2878 return error_mark_node;
2880 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
2881 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
2882 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
2883 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
2884 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
2885 == void_list_node)))
2887 /* [basic.stc.dynamic.allocation]
2889 An allocation function can be a function
2890 template. ... Template allocation functions shall
2891 have two or more parameters. */
2892 error ("invalid template declaration of %qD", decl);
2893 return decl;
2896 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2897 && CLASS_TYPE_P (TREE_TYPE (decl)))
2898 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)))
2899 /* OK */;
2900 else
2902 error ("template declaration of %q#D", decl);
2903 return error_mark_node;
2907 /* Check to see that the rules regarding the use of default
2908 arguments are not being violated. */
2909 check_default_tmpl_args (decl, current_template_parms,
2910 primary, is_partial);
2912 if (is_partial)
2913 return process_partial_specialization (decl);
2915 args = current_template_args ();
2917 if (!ctx
2918 || TREE_CODE (ctx) == FUNCTION_DECL
2919 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2920 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2922 if (DECL_LANG_SPECIFIC (decl)
2923 && DECL_TEMPLATE_INFO (decl)
2924 && DECL_TI_TEMPLATE (decl))
2925 tmpl = DECL_TI_TEMPLATE (decl);
2926 /* If DECL is a TYPE_DECL for a class-template, then there won't
2927 be DECL_LANG_SPECIFIC. The information equivalent to
2928 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2929 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2930 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2931 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2933 /* Since a template declaration already existed for this
2934 class-type, we must be redeclaring it here. Make sure
2935 that the redeclaration is valid. */
2936 redeclare_class_template (TREE_TYPE (decl),
2937 current_template_parms);
2938 /* We don't need to create a new TEMPLATE_DECL; just use the
2939 one we already had. */
2940 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2942 else
2944 tmpl = build_template_decl (decl, current_template_parms);
2945 new_template_p = 1;
2947 if (DECL_LANG_SPECIFIC (decl)
2948 && DECL_TEMPLATE_SPECIALIZATION (decl))
2950 /* A specialization of a member template of a template
2951 class. */
2952 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2953 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2954 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2958 else
2960 tree a, t, current, parms;
2961 int i;
2963 if (TREE_CODE (decl) == TYPE_DECL)
2965 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2966 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2967 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2968 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2969 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2970 else
2972 error ("%qD does not declare a template type", decl);
2973 return decl;
2976 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2978 error ("template definition of non-template %q#D", decl);
2979 return decl;
2981 else
2982 tmpl = DECL_TI_TEMPLATE (decl);
2984 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2985 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2986 && DECL_TEMPLATE_SPECIALIZATION (decl)
2987 && is_member_template (tmpl))
2989 tree new_tmpl;
2991 /* The declaration is a specialization of a member
2992 template, declared outside the class. Therefore, the
2993 innermost template arguments will be NULL, so we
2994 replace them with the arguments determined by the
2995 earlier call to check_explicit_specialization. */
2996 args = DECL_TI_ARGS (decl);
2998 new_tmpl
2999 = build_template_decl (decl, current_template_parms);
3000 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3001 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3002 DECL_TI_TEMPLATE (decl) = new_tmpl;
3003 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3004 DECL_TEMPLATE_INFO (new_tmpl)
3005 = tree_cons (tmpl, args, NULL_TREE);
3007 register_specialization (new_tmpl,
3008 most_general_template (tmpl),
3009 args);
3010 return decl;
3013 /* Make sure the template headers we got make sense. */
3015 parms = DECL_TEMPLATE_PARMS (tmpl);
3016 i = TMPL_PARMS_DEPTH (parms);
3017 if (TMPL_ARGS_DEPTH (args) != i)
3019 error ("expected %d levels of template parms for %q#D, got %d",
3020 i, decl, TMPL_ARGS_DEPTH (args));
3022 else
3023 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3025 a = TMPL_ARGS_LEVEL (args, i);
3026 t = INNERMOST_TEMPLATE_PARMS (parms);
3028 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3030 if (current == decl)
3031 error ("got %d template parameters for %q#D",
3032 TREE_VEC_LENGTH (a), decl);
3033 else
3034 error ("got %d template parameters for %q#T",
3035 TREE_VEC_LENGTH (a), current);
3036 error (" but %d required", TREE_VEC_LENGTH (t));
3039 /* Perhaps we should also check that the parms are used in the
3040 appropriate qualifying scopes in the declarator? */
3042 if (current == decl)
3043 current = ctx;
3044 else
3045 current = TYPE_CONTEXT (current);
3049 DECL_TEMPLATE_RESULT (tmpl) = decl;
3050 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3052 /* Push template declarations for global functions and types. Note
3053 that we do not try to push a global template friend declared in a
3054 template class; such a thing may well depend on the template
3055 parameters of the class. */
3056 if (new_template_p && !ctx
3057 && !(is_friend && template_class_depth (current_class_type) > 0))
3058 tmpl = pushdecl_namespace_level (tmpl);
3060 if (primary)
3062 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3063 if (DECL_CONV_FN_P (tmpl))
3065 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3067 /* It is a conversion operator. See if the type converted to
3068 depends on innermost template operands. */
3070 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3071 depth))
3072 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3076 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3077 back to its most general template. If TMPL is a specialization,
3078 ARGS may only have the innermost set of arguments. Add the missing
3079 argument levels if necessary. */
3080 if (DECL_TEMPLATE_INFO (tmpl))
3081 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3083 info = tree_cons (tmpl, args, NULL_TREE);
3085 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3087 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3088 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3089 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3090 /* Don't change the name if we've already set it up. */
3091 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3092 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3094 else if (DECL_LANG_SPECIFIC (decl))
3095 DECL_TEMPLATE_INFO (decl) = info;
3097 return DECL_TEMPLATE_RESULT (tmpl);
3100 tree
3101 push_template_decl (tree decl)
3103 return push_template_decl_real (decl, 0);
3106 /* Called when a class template TYPE is redeclared with the indicated
3107 template PARMS, e.g.:
3109 template <class T> struct S;
3110 template <class T> struct S {}; */
3112 void
3113 redeclare_class_template (tree type, tree parms)
3115 tree tmpl;
3116 tree tmpl_parms;
3117 int i;
3119 if (!TYPE_TEMPLATE_INFO (type))
3121 error ("%qT is not a template type", type);
3122 return;
3125 tmpl = TYPE_TI_TEMPLATE (type);
3126 if (!PRIMARY_TEMPLATE_P (tmpl))
3127 /* The type is nested in some template class. Nothing to worry
3128 about here; there are no new template parameters for the nested
3129 type. */
3130 return;
3132 parms = INNERMOST_TEMPLATE_PARMS (parms);
3133 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3135 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3137 cp_error_at ("previous declaration %qD", tmpl);
3138 error ("used %d template parameter%s instead of %d",
3139 TREE_VEC_LENGTH (tmpl_parms),
3140 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
3141 TREE_VEC_LENGTH (parms));
3142 return;
3145 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3147 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3148 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3149 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3150 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3152 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3153 TEMPLATE_DECL. */
3154 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3155 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3156 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3158 cp_error_at ("template parameter %q#D", tmpl_parm);
3159 error ("redeclared here as %q#D", parm);
3160 return;
3163 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3165 /* We have in [temp.param]:
3167 A template-parameter may not be given default arguments
3168 by two different declarations in the same scope. */
3169 error ("redefinition of default argument for %q#D", parm);
3170 error ("%J original definition appeared here", tmpl_parm);
3171 return;
3174 if (parm_default != NULL_TREE)
3175 /* Update the previous template parameters (which are the ones
3176 that will really count) with the new default value. */
3177 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3178 else if (tmpl_default != NULL_TREE)
3179 /* Update the new parameters, too; they'll be used as the
3180 parameters for any members. */
3181 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3185 /* Simplify EXPR if it is a non-dependent expression. Returns the
3186 (possibly simplified) expression. */
3188 tree
3189 fold_non_dependent_expr (tree expr)
3191 /* If we're in a template, but EXPR isn't value dependent, simplify
3192 it. We're supposed to treat:
3194 template <typename T> void f(T[1 + 1]);
3195 template <typename T> void f(T[2]);
3197 as two declarations of the same function, for example. */
3198 if (processing_template_decl
3199 && !type_dependent_expression_p (expr)
3200 && !value_dependent_expression_p (expr))
3202 HOST_WIDE_INT saved_processing_template_decl;
3204 saved_processing_template_decl = processing_template_decl;
3205 processing_template_decl = 0;
3206 expr = tsubst_copy_and_build (expr,
3207 /*args=*/NULL_TREE,
3208 tf_error,
3209 /*in_decl=*/NULL_TREE,
3210 /*function_p=*/false);
3211 processing_template_decl = saved_processing_template_decl;
3213 return expr;
3216 /* Attempt to convert the non-type template parameter EXPR to the
3217 indicated TYPE. If the conversion is successful, return the
3218 converted value. If the conversion is unsuccessful, return
3219 NULL_TREE if we issued an error message, or error_mark_node if we
3220 did not. We issue error messages for out-and-out bad template
3221 parameters, but not simply because the conversion failed, since we
3222 might be just trying to do argument deduction. Both TYPE and EXPR
3223 must be non-dependent. */
3225 static tree
3226 convert_nontype_argument (tree type, tree expr)
3228 tree expr_type;
3230 /* If we are in a template, EXPR may be non-dependent, but still
3231 have a syntactic, rather than semantic, form. For example, EXPR
3232 might be a SCOPE_REF, rather than the VAR_DECL to which the
3233 SCOPE_REF refers. Preserving the qualifying scope is necessary
3234 so that access checking can be performed when the template is
3235 instantiated -- but here we need the resolved form so that we can
3236 convert the argument. */
3237 expr = fold_non_dependent_expr (expr);
3238 expr_type = TREE_TYPE (expr);
3240 /* A template-argument for a non-type, non-template
3241 template-parameter shall be one of:
3243 --an integral constant-expression of integral or enumeration
3244 type; or
3246 --the name of a non-type template-parameter; or
3248 --the name of an object or function with external linkage,
3249 including function templates and function template-ids but
3250 excluding non-static class members, expressed as id-expression;
3253 --the address of an object or function with external linkage,
3254 including function templates and function template-ids but
3255 excluding non-static class members, expressed as & id-expression
3256 where the & is optional if the name refers to a function or
3257 array; or
3259 --a pointer to member expressed as described in _expr.unary.op_. */
3261 /* An integral constant-expression can include const variables or
3262 . enumerators. Simplify things by folding them to their values,
3263 unless we're about to bind the declaration to a reference
3264 parameter. */
3265 if (INTEGRAL_TYPE_P (expr_type) && TREE_CODE (type) != REFERENCE_TYPE)
3266 while (true)
3268 tree const_expr = decl_constant_value (expr);
3269 /* In a template, the initializer for a VAR_DECL may not be
3270 marked as TREE_CONSTANT, in which case decl_constant_value
3271 will not return the initializer. Handle that special case
3272 here. */
3273 if (expr == const_expr
3274 && DECL_INTEGRAL_CONSTANT_VAR_P (expr)
3275 /* DECL_INITIAL can be NULL if we are processing a
3276 variable initialized to an expression involving itself.
3277 We know it is initialized to a constant -- but not what
3278 constant, yet. */
3279 && DECL_INITIAL (expr))
3280 const_expr = DECL_INITIAL (expr);
3281 if (expr == const_expr)
3282 break;
3283 expr = fold_non_dependent_expr (const_expr);
3286 if (is_overloaded_fn (expr))
3287 /* OK for now. We'll check that it has external linkage later.
3288 Check this first since if expr_type is the unknown_type_node
3289 we would otherwise complain below. */
3291 else if (TYPE_PTR_TO_MEMBER_P (expr_type))
3293 if (TREE_CODE (expr) != PTRMEM_CST)
3294 goto bad_argument;
3296 else if (TYPE_PTR_P (expr_type)
3297 || TREE_CODE (expr_type) == ARRAY_TYPE
3298 || TREE_CODE (type) == REFERENCE_TYPE
3299 /* If expr is the address of an overloaded function, we
3300 will get the unknown_type_node at this point. */
3301 || expr_type == unknown_type_node)
3303 tree referent;
3304 tree e = expr;
3305 STRIP_NOPS (e);
3307 if (TREE_CODE (expr_type) == ARRAY_TYPE
3308 || (TREE_CODE (type) == REFERENCE_TYPE
3309 && TREE_CODE (e) != ADDR_EXPR))
3310 referent = e;
3311 else
3313 if (TREE_CODE (e) != ADDR_EXPR)
3315 bad_argument:
3316 error ("%qE is not a valid template argument", expr);
3317 if (TYPE_PTR_P (expr_type))
3319 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
3320 error ("it must be the address of a function with external linkage");
3321 else
3322 error ("it must be the address of an object with external linkage");
3324 else if (TYPE_PTR_TO_MEMBER_P (expr_type))
3325 error ("it must be a pointer-to-member of the form %<&X::Y%>");
3327 return NULL_TREE;
3330 referent = TREE_OPERAND (e, 0);
3331 STRIP_NOPS (referent);
3334 if (TREE_CODE (referent) == STRING_CST)
3336 error ("string literal %qE is not a valid template argument "
3337 "because it is the address of an object with static linkage",
3338 referent);
3339 return NULL_TREE;
3342 if (TREE_CODE (referent) == SCOPE_REF)
3343 referent = TREE_OPERAND (referent, 1);
3345 if (is_overloaded_fn (referent))
3346 /* We'll check that it has external linkage later. */
3348 else if (TREE_CODE (referent) != VAR_DECL)
3349 goto bad_argument;
3350 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
3352 error ("address of non-extern %qE cannot be used as "
3353 "template argument", referent);
3354 return error_mark_node;
3357 else if (INTEGRAL_TYPE_P (expr_type) || TYPE_PTR_TO_MEMBER_P (expr_type))
3359 if (! TREE_CONSTANT (expr))
3361 non_constant:
3362 error ("non-constant %qE cannot be used as template argument", expr);
3363 return NULL_TREE;
3366 else
3368 if (TYPE_P (expr))
3369 error ("type %qT cannot be used as a value for a non-type "
3370 "template-parameter", expr);
3371 else if (DECL_P (expr))
3372 error ("invalid use of %qD as a non-type template-argument", expr);
3373 else
3374 error ("invalid use of %qE as a non-type template-argument", expr);
3376 return NULL_TREE;
3379 switch (TREE_CODE (type))
3381 HOST_WIDE_INT saved_processing_template_decl;
3383 case INTEGER_TYPE:
3384 case BOOLEAN_TYPE:
3385 case ENUMERAL_TYPE:
3386 /* For a non-type template-parameter of integral or enumeration
3387 type, integral promotions (_conv.prom_) and integral
3388 conversions (_conv.integral_) are applied. */
3389 if (!INTEGRAL_TYPE_P (expr_type))
3390 return error_mark_node;
3392 /* [conv.integral] does not allow conversions between two different
3393 enumeration types. */
3394 if (TREE_CODE (type) == ENUMERAL_TYPE
3395 && TREE_CODE (expr_type) == ENUMERAL_TYPE
3396 && !same_type_ignoring_top_level_qualifiers_p (type, expr_type))
3397 return error_mark_node;
3399 /* It's safe to call digest_init in this case; we know we're
3400 just converting one integral constant expression to another.
3402 saved_processing_template_decl = processing_template_decl;
3403 processing_template_decl = 0;
3404 expr = digest_init (type, expr, (tree*) 0);
3405 processing_template_decl = saved_processing_template_decl;
3407 if (TREE_CODE (expr) != INTEGER_CST)
3408 /* Curiously, some TREE_CONSTANT integral expressions do not
3409 simplify to integer constants. For example, `3 % 0',
3410 remains a TRUNC_MOD_EXPR. */
3411 goto non_constant;
3413 return expr;
3415 case OFFSET_TYPE:
3417 tree e;
3419 /* For a non-type template-parameter of type pointer to data
3420 member, qualification conversions (_conv.qual_) are
3421 applied. */
3422 e = perform_qualification_conversions (type, expr);
3423 if (TREE_CODE (e) == NOP_EXPR)
3424 /* The call to perform_qualification_conversions will
3425 insert a NOP_EXPR over EXPR to do express conversion,
3426 if necessary. But, that will confuse us if we use
3427 this (converted) template parameter to instantiate
3428 another template; then the thing will not look like a
3429 valid template argument. So, just make a new
3430 constant, of the appropriate type. */
3431 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3432 return e;
3435 case POINTER_TYPE:
3437 tree type_pointed_to = TREE_TYPE (type);
3439 if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3441 /* For a non-type template-parameter of type pointer to
3442 function, only the function-to-pointer conversion
3443 (_conv.func_) is applied. If the template-argument
3444 represents a set of overloaded functions (or a pointer to
3445 such), the matching function is selected from the set
3446 (_over.over_). */
3447 tree fns;
3448 tree fn;
3450 if (TREE_CODE (expr) == ADDR_EXPR)
3451 fns = TREE_OPERAND (expr, 0);
3452 else
3453 fns = expr;
3455 fn = instantiate_type (type_pointed_to, fns, tf_none);
3457 if (fn == error_mark_node)
3458 return error_mark_node;
3460 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3462 if (really_overloaded_fn (fns))
3463 return error_mark_node;
3464 else
3465 goto bad_argument;
3468 expr = build_unary_op (ADDR_EXPR, fn, 0);
3470 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3471 return expr;
3473 else
3475 /* For a non-type template-parameter of type pointer to
3476 object, qualification conversions (_conv.qual_) and the
3477 array-to-pointer conversion (_conv.array_) are applied.
3478 [Note: In particular, neither the null pointer conversion
3479 (_conv.ptr_) nor the derived-to-base conversion
3480 (_conv.ptr_) are applied. Although 0 is a valid
3481 template-argument for a non-type template-parameter of
3482 integral type, it is not a valid template-argument for a
3483 non-type template-parameter of pointer type.]
3485 The call to decay_conversion performs the
3486 array-to-pointer conversion, if appropriate. */
3487 expr = decay_conversion (expr);
3489 if (expr == error_mark_node)
3490 return error_mark_node;
3491 else
3492 return perform_qualification_conversions (type, expr);
3495 break;
3497 case REFERENCE_TYPE:
3499 tree type_referred_to = TREE_TYPE (type);
3501 /* If this expression already has reference type, get the
3502 underlying object. */
3503 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3505 if (TREE_CODE (expr) == NOP_EXPR
3506 && TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
3507 STRIP_NOPS (expr);
3508 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3509 expr = TREE_OPERAND (expr, 0);
3510 expr_type = TREE_TYPE (expr);
3513 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3515 /* For a non-type template-parameter of type reference to
3516 function, no conversions apply. If the
3517 template-argument represents a set of overloaded
3518 functions, the matching function is selected from the
3519 set (_over.over_). */
3520 tree fn;
3522 fn = instantiate_type (type_referred_to, expr, tf_none);
3524 if (fn == error_mark_node)
3525 return error_mark_node;
3527 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3529 if (really_overloaded_fn (expr))
3530 /* Don't issue an error here; we might get a different
3531 function if the overloading had worked out
3532 differently. */
3533 return error_mark_node;
3534 else
3535 goto bad_argument;
3538 gcc_assert (same_type_p (type_referred_to, TREE_TYPE (fn)));
3539 expr = fn;
3541 else
3543 /* For a non-type template-parameter of type reference to
3544 object, no conversions apply. The type referred to by the
3545 reference may be more cv-qualified than the (otherwise
3546 identical) type of the template-argument. The
3547 template-parameter is bound directly to the
3548 template-argument, which must be an lvalue. */
3549 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3550 TYPE_MAIN_VARIANT (type_referred_to))
3551 || !at_least_as_qualified_p (type_referred_to,
3552 expr_type)
3553 || !real_lvalue_p (expr))
3554 return error_mark_node;
3557 cxx_mark_addressable (expr);
3558 return build_nop (type, build_address (expr));
3560 break;
3562 case RECORD_TYPE:
3564 gcc_assert (TYPE_PTRMEMFUNC_P (type));
3566 /* For a non-type template-parameter of type pointer to member
3567 function, no conversions apply. If the template-argument
3568 represents a set of overloaded member functions, the
3569 matching member function is selected from the set
3570 (_over.over_). */
3572 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3573 expr_type != unknown_type_node)
3574 return error_mark_node;
3576 if (TREE_CODE (expr) == PTRMEM_CST)
3578 /* A ptr-to-member constant. */
3579 if (!same_type_p (type, expr_type))
3580 return error_mark_node;
3581 else
3582 return expr;
3585 if (TREE_CODE (expr) != ADDR_EXPR)
3586 return error_mark_node;
3588 expr = instantiate_type (type, expr, tf_none);
3590 if (expr == error_mark_node)
3591 return error_mark_node;
3593 if (!same_type_p (type, TREE_TYPE (expr)))
3594 return error_mark_node;
3596 return expr;
3598 break;
3600 default:
3601 /* All non-type parameters must have one of these types. */
3602 gcc_unreachable ();
3605 return error_mark_node;
3608 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3609 template template parameters. Both PARM_PARMS and ARG_PARMS are
3610 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3611 or PARM_DECL.
3613 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3614 the case, then extra parameters must have default arguments.
3616 Consider the example:
3617 template <class T, class Allocator = allocator> class vector;
3618 template<template <class U> class TT> class C;
3620 C<vector> is a valid instantiation. PARM_PARMS for the above code
3621 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3622 T and Allocator) and OUTER_ARGS contains the argument that is used to
3623 substitute the TT parameter. */
3625 static int
3626 coerce_template_template_parms (tree parm_parms,
3627 tree arg_parms,
3628 tsubst_flags_t complain,
3629 tree in_decl,
3630 tree outer_args)
3632 int nparms, nargs, i;
3633 tree parm, arg;
3635 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3636 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3638 nparms = TREE_VEC_LENGTH (parm_parms);
3639 nargs = TREE_VEC_LENGTH (arg_parms);
3641 /* The rule here is opposite of coerce_template_parms. */
3642 if (nargs < nparms
3643 || (nargs > nparms
3644 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3645 return 0;
3647 for (i = 0; i < nparms; ++i)
3649 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3650 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3652 if (arg == NULL_TREE || arg == error_mark_node
3653 || parm == NULL_TREE || parm == error_mark_node)
3654 return 0;
3656 if (TREE_CODE (arg) != TREE_CODE (parm))
3657 return 0;
3659 switch (TREE_CODE (parm))
3661 case TYPE_DECL:
3662 break;
3664 case TEMPLATE_DECL:
3665 /* We encounter instantiations of templates like
3666 template <template <template <class> class> class TT>
3667 class C; */
3669 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3670 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3672 if (!coerce_template_template_parms
3673 (parmparm, argparm, complain, in_decl, outer_args))
3674 return 0;
3676 break;
3678 case PARM_DECL:
3679 /* The tsubst call is used to handle cases such as
3681 template <int> class C {};
3682 template <class T, template <T> class TT> class D {};
3683 D<int, C> d;
3685 i.e. the parameter list of TT depends on earlier parameters. */
3686 if (!dependent_type_p (TREE_TYPE (arg))
3687 && !same_type_p
3688 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3689 TREE_TYPE (arg)))
3690 return 0;
3691 break;
3693 default:
3694 gcc_unreachable ();
3697 return 1;
3700 /* Convert the indicated template ARG as necessary to match the
3701 indicated template PARM. Returns the converted ARG, or
3702 error_mark_node if the conversion was unsuccessful. Error and
3703 warning messages are issued under control of COMPLAIN. This
3704 conversion is for the Ith parameter in the parameter list. ARGS is
3705 the full set of template arguments deduced so far. */
3707 static tree
3708 convert_template_argument (tree parm,
3709 tree arg,
3710 tree args,
3711 tsubst_flags_t complain,
3712 int i,
3713 tree in_decl)
3715 tree val;
3716 tree inner_args;
3717 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3719 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3721 if (TREE_CODE (arg) == TREE_LIST
3722 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3724 /* The template argument was the name of some
3725 member function. That's usually
3726 invalid, but static members are OK. In any
3727 case, grab the underlying fields/functions
3728 and issue an error later if required. */
3729 arg = TREE_VALUE (arg);
3730 TREE_TYPE (arg) = unknown_type_node;
3733 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3734 requires_type = (TREE_CODE (parm) == TYPE_DECL
3735 || requires_tmpl_type);
3737 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3738 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3739 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3740 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3742 if (is_tmpl_type
3743 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3744 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3745 arg = TYPE_STUB_DECL (arg);
3747 is_type = TYPE_P (arg) || is_tmpl_type;
3749 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3750 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3752 pedwarn ("to refer to a type member of a template parameter, "
3753 "use %<typename %E%>", arg);
3755 arg = make_typename_type (TREE_OPERAND (arg, 0),
3756 TREE_OPERAND (arg, 1),
3757 complain & tf_error);
3758 is_type = 1;
3760 if (is_type != requires_type)
3762 if (in_decl)
3764 if (complain & tf_error)
3766 error ("type/value mismatch at argument %d in template "
3767 "parameter list for %qD",
3768 i + 1, in_decl);
3769 if (is_type)
3770 error (" expected a constant of type %qT, got %qT",
3771 TREE_TYPE (parm),
3772 (is_tmpl_type ? DECL_NAME (arg) : arg));
3773 else if (requires_tmpl_type)
3774 error (" expected a class template, got %qE", arg);
3775 else
3776 error (" expected a type, got %qE", arg);
3779 return error_mark_node;
3781 if (is_tmpl_type ^ requires_tmpl_type)
3783 if (in_decl && (complain & tf_error))
3785 error ("type/value mismatch at argument %d in template "
3786 "parameter list for %qD",
3787 i + 1, in_decl);
3788 if (is_tmpl_type)
3789 error (" expected a type, got %qT", DECL_NAME (arg));
3790 else
3791 error (" expected a class template, got %qT", 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 "
3826 "template parameter list for %qD",
3827 i + 1, in_decl);
3828 error (" expected a template of type %qD, got %qD",
3829 parm, arg);
3832 val = error_mark_node;
3836 else
3837 val = arg;
3839 else
3841 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3843 if (invalid_nontype_parm_type_p (t, complain))
3844 return error_mark_node;
3846 if (!uses_template_parms (arg) && !uses_template_parms (t))
3847 /* We used to call digest_init here. However, digest_init
3848 will report errors, which we don't want when complain
3849 is zero. More importantly, digest_init will try too
3850 hard to convert things: for example, `0' should not be
3851 converted to pointer type at this point according to
3852 the standard. Accepting this is not merely an
3853 extension, since deciding whether or not these
3854 conversions can occur is part of determining which
3855 function template to call, or whether a given explicit
3856 argument specification is valid. */
3857 val = convert_nontype_argument (t, arg);
3858 else
3859 val = arg;
3861 if (val == NULL_TREE)
3862 val = error_mark_node;
3863 else if (val == error_mark_node && (complain & tf_error))
3864 error ("could not convert template argument %qE to %qT", arg, t);
3867 return val;
3870 /* Convert all template arguments to their appropriate types, and
3871 return a vector containing the innermost resulting template
3872 arguments. If any error occurs, return error_mark_node. Error and
3873 warning messages are issued under control of COMPLAIN.
3875 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3876 provided in ARGLIST, or else trailing parameters must have default
3877 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3878 deduction for any unspecified trailing arguments. */
3880 static tree
3881 coerce_template_parms (tree parms,
3882 tree args,
3883 tree in_decl,
3884 tsubst_flags_t complain,
3885 int require_all_arguments)
3887 int nparms, nargs, i, lost = 0;
3888 tree inner_args;
3889 tree new_args;
3890 tree new_inner_args;
3892 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3893 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
3894 nparms = TREE_VEC_LENGTH (parms);
3896 if (nargs > nparms
3897 || (nargs < nparms
3898 && require_all_arguments
3899 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3901 if (complain & tf_error)
3903 error ("wrong number of template arguments (%d, should be %d)",
3904 nargs, nparms);
3906 if (in_decl)
3907 cp_error_at ("provided for %qD", in_decl);
3910 return error_mark_node;
3913 new_inner_args = make_tree_vec (nparms);
3914 new_args = add_outermost_template_args (args, new_inner_args);
3915 for (i = 0; i < nparms; i++)
3917 tree arg;
3918 tree parm;
3920 /* Get the Ith template parameter. */
3921 parm = TREE_VEC_ELT (parms, i);
3923 /* Calculate the Ith argument. */
3924 if (i < nargs)
3925 arg = TREE_VEC_ELT (inner_args, i);
3926 else if (require_all_arguments)
3927 /* There must be a default arg in this case. */
3928 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
3929 complain, in_decl);
3930 else
3931 break;
3933 gcc_assert (arg);
3934 if (arg == error_mark_node)
3935 error ("template argument %d is invalid", i + 1);
3936 else
3937 arg = convert_template_argument (TREE_VALUE (parm),
3938 arg, new_args, complain, i,
3939 in_decl);
3941 if (arg == error_mark_node)
3942 lost++;
3943 TREE_VEC_ELT (new_inner_args, i) = arg;
3946 if (lost)
3947 return error_mark_node;
3949 return new_inner_args;
3952 /* Returns 1 if template args OT and NT are equivalent. */
3954 static int
3955 template_args_equal (tree ot, tree nt)
3957 if (nt == ot)
3958 return 1;
3960 if (TREE_CODE (nt) == TREE_VEC)
3961 /* For member templates */
3962 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3963 else if (TYPE_P (nt))
3964 return TYPE_P (ot) && same_type_p (ot, nt);
3965 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3966 return 0;
3967 else
3968 return cp_tree_equal (ot, nt);
3971 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3972 of template arguments. Returns 0 otherwise. */
3975 comp_template_args (tree oldargs, tree newargs)
3977 int i;
3979 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3980 return 0;
3982 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3984 tree nt = TREE_VEC_ELT (newargs, i);
3985 tree ot = TREE_VEC_ELT (oldargs, i);
3987 if (! template_args_equal (ot, nt))
3988 return 0;
3990 return 1;
3993 /* Given class template name and parameter list, produce a user-friendly name
3994 for the instantiation. */
3996 static char *
3997 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
3999 static struct obstack scratch_obstack;
4000 static char *scratch_firstobj;
4001 int i, nparms;
4003 if (!scratch_firstobj)
4004 gcc_obstack_init (&scratch_obstack);
4005 else
4006 obstack_free (&scratch_obstack, scratch_firstobj);
4007 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
4009 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4010 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4012 cat (name);
4013 ccat ('<');
4014 nparms = TREE_VEC_LENGTH (parms);
4015 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4016 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4017 for (i = 0; i < nparms; i++)
4019 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4020 tree arg = TREE_VEC_ELT (arglist, i);
4022 if (i)
4023 ccat (',');
4025 if (TREE_CODE (parm) == TYPE_DECL)
4027 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4028 continue;
4030 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4032 if (TREE_CODE (arg) == TEMPLATE_DECL)
4034 /* Already substituted with real template. Just output
4035 the template name here */
4036 tree context = DECL_CONTEXT (arg);
4037 if (context)
4039 /* The template may be defined in a namespace, or
4040 may be a member template. */
4041 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4042 || CLASS_TYPE_P (context));
4043 cat (decl_as_string (DECL_CONTEXT (arg),
4044 TFF_PLAIN_IDENTIFIER));
4045 cat ("::");
4047 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4049 else
4050 /* Output the parameter declaration. */
4051 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4052 continue;
4054 else
4055 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4057 /* No need to check arglist against parmlist here; we did that
4058 in coerce_template_parms, called from lookup_template_class. */
4059 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4062 char *bufp = obstack_next_free (&scratch_obstack);
4063 int offset = 0;
4064 while (bufp[offset - 1] == ' ')
4065 offset--;
4066 obstack_blank_fast (&scratch_obstack, offset);
4068 /* B<C<char> >, not B<C<char>> */
4069 if (bufp[offset - 1] == '>')
4070 ccat (' ');
4072 ccat ('>');
4073 ccat ('\0');
4074 return (char *) obstack_base (&scratch_obstack);
4077 static tree
4078 classtype_mangled_name (tree t)
4080 if (CLASSTYPE_TEMPLATE_INFO (t)
4081 /* Specializations have already had their names set up in
4082 lookup_template_class. */
4083 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4085 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4087 /* For non-primary templates, the template parameters are
4088 implicit from their surrounding context. */
4089 if (PRIMARY_TEMPLATE_P (tmpl))
4091 tree name = DECL_NAME (tmpl);
4092 char *mangled_name = mangle_class_name_for_template
4093 (IDENTIFIER_POINTER (name),
4094 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4095 CLASSTYPE_TI_ARGS (t));
4096 tree id = get_identifier (mangled_name);
4097 IDENTIFIER_TEMPLATE (id) = name;
4098 return id;
4102 return TYPE_IDENTIFIER (t);
4105 static void
4106 add_pending_template (tree d)
4108 tree ti = (TYPE_P (d)
4109 ? CLASSTYPE_TEMPLATE_INFO (d)
4110 : DECL_TEMPLATE_INFO (d));
4111 tree pt;
4112 int level;
4114 if (TI_PENDING_TEMPLATE_FLAG (ti))
4115 return;
4117 /* We are called both from instantiate_decl, where we've already had a
4118 tinst_level pushed, and instantiate_template, where we haven't.
4119 Compensate. */
4120 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4122 if (level)
4123 push_tinst_level (d);
4125 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4126 if (last_pending_template)
4127 TREE_CHAIN (last_pending_template) = pt;
4128 else
4129 pending_templates = pt;
4131 last_pending_template = pt;
4133 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4135 if (level)
4136 pop_tinst_level ();
4140 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4141 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4142 documentation for TEMPLATE_ID_EXPR. */
4144 tree
4145 lookup_template_function (tree fns, tree arglist)
4147 tree type;
4149 if (fns == error_mark_node || arglist == error_mark_node)
4150 return error_mark_node;
4152 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4153 if (fns == NULL_TREE
4154 || TREE_CODE (fns) == FUNCTION_DECL)
4156 error ("non-template used as template");
4157 return error_mark_node;
4160 gcc_assert (TREE_CODE (fns) == TEMPLATE_DECL
4161 || TREE_CODE (fns) == OVERLOAD
4162 || BASELINK_P (fns)
4163 || TREE_CODE (fns) == IDENTIFIER_NODE);
4165 if (BASELINK_P (fns))
4167 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4168 unknown_type_node,
4169 BASELINK_FUNCTIONS (fns),
4170 arglist);
4171 return fns;
4174 type = TREE_TYPE (fns);
4175 if (TREE_CODE (fns) == OVERLOAD || !type)
4176 type = unknown_type_node;
4178 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4181 /* Within the scope of a template class S<T>, the name S gets bound
4182 (in build_self_reference) to a TYPE_DECL for the class, not a
4183 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4184 or one of its enclosing classes, and that type is a template,
4185 return the associated TEMPLATE_DECL. Otherwise, the original
4186 DECL is returned. */
4188 tree
4189 maybe_get_template_decl_from_type_decl (tree decl)
4191 return (decl != NULL_TREE
4192 && TREE_CODE (decl) == TYPE_DECL
4193 && DECL_ARTIFICIAL (decl)
4194 && CLASS_TYPE_P (TREE_TYPE (decl))
4195 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4196 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4199 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4200 parameters, find the desired type.
4202 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4204 IN_DECL, if non-NULL, is the template declaration we are trying to
4205 instantiate.
4207 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4208 the class we are looking up.
4210 Issue error and warning messages under control of COMPLAIN.
4212 If the template class is really a local class in a template
4213 function, then the FUNCTION_CONTEXT is the function in which it is
4214 being instantiated. */
4216 tree
4217 lookup_template_class (tree d1,
4218 tree arglist,
4219 tree in_decl,
4220 tree context,
4221 int entering_scope,
4222 tsubst_flags_t complain)
4224 tree template = NULL_TREE, parmlist;
4225 tree t;
4227 timevar_push (TV_NAME_LOOKUP);
4229 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4231 tree value = innermost_non_namespace_value (d1);
4232 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4233 template = value;
4234 else
4236 if (context)
4237 push_decl_namespace (context);
4238 template = lookup_name (d1, /*prefer_type=*/0);
4239 template = maybe_get_template_decl_from_type_decl (template);
4240 if (context)
4241 pop_decl_namespace ();
4243 if (template)
4244 context = DECL_CONTEXT (template);
4246 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4248 tree type = TREE_TYPE (d1);
4250 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4251 an implicit typename for the second A. Deal with it. */
4252 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4253 type = TREE_TYPE (type);
4255 if (CLASSTYPE_TEMPLATE_INFO (type))
4257 template = CLASSTYPE_TI_TEMPLATE (type);
4258 d1 = DECL_NAME (template);
4261 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4262 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4264 template = TYPE_TI_TEMPLATE (d1);
4265 d1 = DECL_NAME (template);
4267 else if (TREE_CODE (d1) == TEMPLATE_DECL
4268 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4270 template = d1;
4271 d1 = DECL_NAME (template);
4272 context = DECL_CONTEXT (template);
4275 /* Issue an error message if we didn't find a template. */
4276 if (! template)
4278 if (complain & tf_error)
4279 error ("%qT is not a template", d1);
4280 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4283 if (TREE_CODE (template) != TEMPLATE_DECL
4284 /* Make sure it's a user visible template, if it was named by
4285 the user. */
4286 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4287 && !PRIMARY_TEMPLATE_P (template)))
4289 if (complain & tf_error)
4291 error ("non-template type %qT used as a template", d1);
4292 if (in_decl)
4293 cp_error_at ("for template declaration %qD", in_decl);
4295 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4298 complain &= ~tf_user;
4300 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4302 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4303 template arguments */
4305 tree parm;
4306 tree arglist2;
4308 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4310 /* Consider an example where a template template parameter declared as
4312 template <class T, class U = std::allocator<T> > class TT
4314 The template parameter level of T and U are one level larger than
4315 of TT. To proper process the default argument of U, say when an
4316 instantiation `TT<int>' is seen, we need to build the full
4317 arguments containing {int} as the innermost level. Outer levels,
4318 available when not appearing as default template argument, can be
4319 obtained from `current_template_args ()'.
4321 Suppose that TT is later substituted with std::vector. The above
4322 instantiation is `TT<int, std::allocator<T> >' with TT at
4323 level 1, and T at level 2, while the template arguments at level 1
4324 becomes {std::vector} and the inner level 2 is {int}. */
4326 if (current_template_parms)
4327 arglist = add_to_template_args (current_template_args (), arglist);
4329 arglist2 = coerce_template_parms (parmlist, arglist, template,
4330 complain, /*require_all_args=*/1);
4331 if (arglist2 == error_mark_node
4332 || (!uses_template_parms (arglist2)
4333 && check_instantiated_args (template, arglist2, complain)))
4334 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4336 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4337 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4339 else
4341 tree template_type = TREE_TYPE (template);
4342 tree gen_tmpl;
4343 tree type_decl;
4344 tree found = NULL_TREE;
4345 tree *tp;
4346 int arg_depth;
4347 int parm_depth;
4348 int is_partial_instantiation;
4350 gen_tmpl = most_general_template (template);
4351 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4352 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4353 arg_depth = TMPL_ARGS_DEPTH (arglist);
4355 if (arg_depth == 1 && parm_depth > 1)
4357 /* We've been given an incomplete set of template arguments.
4358 For example, given:
4360 template <class T> struct S1 {
4361 template <class U> struct S2 {};
4362 template <class U> struct S2<U*> {};
4365 we will be called with an ARGLIST of `U*', but the
4366 TEMPLATE will be `template <class T> template
4367 <class U> struct S1<T>::S2'. We must fill in the missing
4368 arguments. */
4369 arglist
4370 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4371 arglist);
4372 arg_depth = TMPL_ARGS_DEPTH (arglist);
4375 /* Now we should have enough arguments. */
4376 gcc_assert (parm_depth == arg_depth);
4378 /* From here on, we're only interested in the most general
4379 template. */
4380 template = gen_tmpl;
4382 /* Calculate the BOUND_ARGS. These will be the args that are
4383 actually tsubst'd into the definition to create the
4384 instantiation. */
4385 if (parm_depth > 1)
4387 /* We have multiple levels of arguments to coerce, at once. */
4388 int i;
4389 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4391 tree bound_args = make_tree_vec (parm_depth);
4393 for (i = saved_depth,
4394 t = DECL_TEMPLATE_PARMS (template);
4395 i > 0 && t != NULL_TREE;
4396 --i, t = TREE_CHAIN (t))
4398 tree a = coerce_template_parms (TREE_VALUE (t),
4399 arglist, template,
4400 complain, /*require_all_args=*/1);
4402 /* Don't process further if one of the levels fails. */
4403 if (a == error_mark_node)
4405 /* Restore the ARGLIST to its full size. */
4406 TREE_VEC_LENGTH (arglist) = saved_depth;
4407 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4410 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4412 /* We temporarily reduce the length of the ARGLIST so
4413 that coerce_template_parms will see only the arguments
4414 corresponding to the template parameters it is
4415 examining. */
4416 TREE_VEC_LENGTH (arglist)--;
4419 /* Restore the ARGLIST to its full size. */
4420 TREE_VEC_LENGTH (arglist) = saved_depth;
4422 arglist = bound_args;
4424 else
4425 arglist
4426 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4427 INNERMOST_TEMPLATE_ARGS (arglist),
4428 template,
4429 complain, /*require_all_args=*/1);
4431 if (arglist == error_mark_node)
4432 /* We were unable to bind the arguments. */
4433 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4435 /* In the scope of a template class, explicit references to the
4436 template class refer to the type of the template, not any
4437 instantiation of it. For example, in:
4439 template <class T> class C { void f(C<T>); }
4441 the `C<T>' is just the same as `C'. Outside of the
4442 class, however, such a reference is an instantiation. */
4443 if (comp_template_args (TYPE_TI_ARGS (template_type),
4444 arglist))
4446 found = template_type;
4448 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4450 tree ctx;
4452 for (ctx = current_class_type;
4453 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4454 ctx = (TYPE_P (ctx)
4455 ? TYPE_CONTEXT (ctx)
4456 : DECL_CONTEXT (ctx)))
4457 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4458 goto found_ctx;
4460 /* We're not in the scope of the class, so the
4461 TEMPLATE_TYPE is not the type we want after all. */
4462 found = NULL_TREE;
4463 found_ctx:;
4466 if (found)
4467 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4469 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4470 *tp;
4471 tp = &TREE_CHAIN (*tp))
4472 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4474 found = *tp;
4476 /* Use the move-to-front heuristic to speed up future
4477 searches. */
4478 *tp = TREE_CHAIN (*tp);
4479 TREE_CHAIN (found)
4480 = DECL_TEMPLATE_INSTANTIATIONS (template);
4481 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4483 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
4486 /* This type is a "partial instantiation" if any of the template
4487 arguments still involve template parameters. Note that we set
4488 IS_PARTIAL_INSTANTIATION for partial specializations as
4489 well. */
4490 is_partial_instantiation = uses_template_parms (arglist);
4492 /* If the deduced arguments are invalid, then the binding
4493 failed. */
4494 if (!is_partial_instantiation
4495 && check_instantiated_args (template,
4496 INNERMOST_TEMPLATE_ARGS (arglist),
4497 complain))
4498 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4500 if (!is_partial_instantiation
4501 && !PRIMARY_TEMPLATE_P (template)
4502 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4504 found = xref_tag_from_type (TREE_TYPE (template),
4505 DECL_NAME (template),
4506 /*globalize=*/1);
4507 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4510 context = tsubst (DECL_CONTEXT (template), arglist,
4511 complain, in_decl);
4512 if (!context)
4513 context = global_namespace;
4515 /* Create the type. */
4516 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4518 if (!is_partial_instantiation)
4520 set_current_access_from_decl (TYPE_NAME (template_type));
4521 t = start_enum (TYPE_IDENTIFIER (template_type));
4523 else
4524 /* We don't want to call start_enum for this type, since
4525 the values for the enumeration constants may involve
4526 template parameters. And, no one should be interested
4527 in the enumeration constants for such a type. */
4528 t = make_node (ENUMERAL_TYPE);
4530 else
4532 t = make_aggr_type (TREE_CODE (template_type));
4533 CLASSTYPE_DECLARED_CLASS (t)
4534 = CLASSTYPE_DECLARED_CLASS (template_type);
4535 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4536 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4538 /* A local class. Make sure the decl gets registered properly. */
4539 if (context == current_function_decl)
4540 pushtag (DECL_NAME (template), t, 0);
4543 /* If we called start_enum or pushtag above, this information
4544 will already be set up. */
4545 if (!TYPE_NAME (t))
4547 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4549 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4550 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4551 TYPE_STUB_DECL (t) = type_decl;
4552 DECL_SOURCE_LOCATION (type_decl)
4553 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4555 else
4556 type_decl = TYPE_NAME (t);
4558 TREE_PRIVATE (type_decl)
4559 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4560 TREE_PROTECTED (type_decl)
4561 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4563 /* Set up the template information. We have to figure out which
4564 template is the immediate parent if this is a full
4565 instantiation. */
4566 if (parm_depth == 1 || is_partial_instantiation
4567 || !PRIMARY_TEMPLATE_P (template))
4568 /* This case is easy; there are no member templates involved. */
4569 found = template;
4570 else
4572 /* This is a full instantiation of a member template. Look
4573 for a partial instantiation of which this is an instance. */
4575 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4576 found; found = TREE_CHAIN (found))
4578 int success;
4579 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4581 /* We only want partial instantiations, here, not
4582 specializations or full instantiations. */
4583 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4584 || !uses_template_parms (TREE_VALUE (found)))
4585 continue;
4587 /* Temporarily reduce by one the number of levels in the
4588 ARGLIST and in FOUND so as to avoid comparing the
4589 last set of arguments. */
4590 TREE_VEC_LENGTH (arglist)--;
4591 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4593 /* See if the arguments match. If they do, then TMPL is
4594 the partial instantiation we want. */
4595 success = comp_template_args (TREE_PURPOSE (found), arglist);
4597 /* Restore the argument vectors to their full size. */
4598 TREE_VEC_LENGTH (arglist)++;
4599 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4601 if (success)
4603 found = tmpl;
4604 break;
4608 if (!found)
4610 /* There was no partial instantiation. This happens
4611 where C<T> is a member template of A<T> and it's used
4612 in something like
4614 template <typename T> struct B { A<T>::C<int> m; };
4615 B<float>;
4617 Create the partial instantiation.
4619 TREE_VEC_LENGTH (arglist)--;
4620 found = tsubst (template, arglist, complain, NULL_TREE);
4621 TREE_VEC_LENGTH (arglist)++;
4625 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4626 DECL_TEMPLATE_INSTANTIATIONS (template)
4627 = tree_cons (arglist, t,
4628 DECL_TEMPLATE_INSTANTIATIONS (template));
4630 if (TREE_CODE (t) == ENUMERAL_TYPE
4631 && !is_partial_instantiation)
4632 /* Now that the type has been registered on the instantiations
4633 list, we set up the enumerators. Because the enumeration
4634 constants may involve the enumeration type itself, we make
4635 sure to register the type first, and then create the
4636 constants. That way, doing tsubst_expr for the enumeration
4637 constants won't result in recursive calls here; we'll find
4638 the instantiation and exit above. */
4639 tsubst_enum (template_type, t, arglist);
4641 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4642 is set up. */
4643 if (TREE_CODE (t) != ENUMERAL_TYPE)
4644 DECL_NAME (type_decl) = classtype_mangled_name (t);
4645 if (is_partial_instantiation)
4646 /* If the type makes use of template parameters, the
4647 code that generates debugging information will crash. */
4648 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4650 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4652 timevar_pop (TV_NAME_LOOKUP);
4655 struct pair_fn_data
4657 tree_fn_t fn;
4658 void *data;
4659 htab_t visited;
4662 /* Called from for_each_template_parm via walk_tree. */
4664 static tree
4665 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4667 tree t = *tp;
4668 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4669 tree_fn_t fn = pfd->fn;
4670 void *data = pfd->data;
4672 if (TYPE_P (t)
4673 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4674 return error_mark_node;
4676 switch (TREE_CODE (t))
4678 case RECORD_TYPE:
4679 if (TYPE_PTRMEMFUNC_P (t))
4680 break;
4681 /* Fall through. */
4683 case UNION_TYPE:
4684 case ENUMERAL_TYPE:
4685 if (!TYPE_TEMPLATE_INFO (t))
4686 *walk_subtrees = 0;
4687 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4688 fn, data, pfd->visited))
4689 return error_mark_node;
4690 break;
4692 case METHOD_TYPE:
4693 /* Since we're not going to walk subtrees, we have to do this
4694 explicitly here. */
4695 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4696 pfd->visited))
4697 return error_mark_node;
4698 /* Fall through. */
4700 case FUNCTION_TYPE:
4701 /* Check the return type. */
4702 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4703 return error_mark_node;
4705 /* Check the parameter types. Since default arguments are not
4706 instantiated until they are needed, the TYPE_ARG_TYPES may
4707 contain expressions that involve template parameters. But,
4708 no-one should be looking at them yet. And, once they're
4709 instantiated, they don't contain template parameters, so
4710 there's no point in looking at them then, either. */
4712 tree parm;
4714 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4715 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4716 pfd->visited))
4717 return error_mark_node;
4719 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4720 want walk_tree walking into them itself. */
4721 *walk_subtrees = 0;
4723 break;
4725 case TYPEOF_TYPE:
4726 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4727 pfd->visited))
4728 return error_mark_node;
4729 break;
4731 case FUNCTION_DECL:
4732 case VAR_DECL:
4733 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4734 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4735 pfd->visited))
4736 return error_mark_node;
4737 /* Fall through. */
4739 case PARM_DECL:
4740 case CONST_DECL:
4741 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4742 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4743 pfd->visited))
4744 return error_mark_node;
4745 if (DECL_CONTEXT (t)
4746 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4747 pfd->visited))
4748 return error_mark_node;
4749 break;
4751 case BOUND_TEMPLATE_TEMPLATE_PARM:
4752 /* Record template parameters such as `T' inside `TT<T>'. */
4753 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4754 return error_mark_node;
4755 /* Fall through. */
4757 case TEMPLATE_TEMPLATE_PARM:
4758 case TEMPLATE_TYPE_PARM:
4759 case TEMPLATE_PARM_INDEX:
4760 if (fn && (*fn)(t, data))
4761 return error_mark_node;
4762 else if (!fn)
4763 return error_mark_node;
4764 break;
4766 case TEMPLATE_DECL:
4767 /* A template template parameter is encountered. */
4768 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4769 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4770 return error_mark_node;
4772 /* Already substituted template template parameter */
4773 *walk_subtrees = 0;
4774 break;
4776 case TYPENAME_TYPE:
4777 if (!fn
4778 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4779 data, pfd->visited))
4780 return error_mark_node;
4781 break;
4783 case CONSTRUCTOR:
4784 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4785 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4786 (TREE_TYPE (t)), fn, data,
4787 pfd->visited))
4788 return error_mark_node;
4789 break;
4791 case INDIRECT_REF:
4792 case COMPONENT_REF:
4793 /* If there's no type, then this thing must be some expression
4794 involving template parameters. */
4795 if (!fn && !TREE_TYPE (t))
4796 return error_mark_node;
4797 break;
4799 case MODOP_EXPR:
4800 case CAST_EXPR:
4801 case REINTERPRET_CAST_EXPR:
4802 case CONST_CAST_EXPR:
4803 case STATIC_CAST_EXPR:
4804 case DYNAMIC_CAST_EXPR:
4805 case ARROW_EXPR:
4806 case DOTSTAR_EXPR:
4807 case TYPEID_EXPR:
4808 case PSEUDO_DTOR_EXPR:
4809 if (!fn)
4810 return error_mark_node;
4811 break;
4813 case BASELINK:
4814 /* If we do not handle this case specially, we end up walking
4815 the BINFO hierarchy, which is circular, and therefore
4816 confuses walk_tree. */
4817 *walk_subtrees = 0;
4818 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4819 pfd->visited))
4820 return error_mark_node;
4821 break;
4823 default:
4824 break;
4827 /* We didn't find any template parameters we liked. */
4828 return NULL_TREE;
4831 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4832 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4833 call FN with the parameter and the DATA.
4834 If FN returns nonzero, the iteration is terminated, and
4835 for_each_template_parm returns 1. Otherwise, the iteration
4836 continues. If FN never returns a nonzero value, the value
4837 returned by for_each_template_parm is 0. If FN is NULL, it is
4838 considered to be the function which always returns 1. */
4840 static int
4841 for_each_template_parm (tree t, tree_fn_t fn, void* data, htab_t visited)
4843 struct pair_fn_data pfd;
4844 int result;
4846 /* Set up. */
4847 pfd.fn = fn;
4848 pfd.data = data;
4850 /* Walk the tree. (Conceptually, we would like to walk without
4851 duplicates, but for_each_template_parm_r recursively calls
4852 for_each_template_parm, so we would need to reorganize a fair
4853 bit to use walk_tree_without_duplicates, so we keep our own
4854 visited list.) */
4855 if (visited)
4856 pfd.visited = visited;
4857 else
4858 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer,
4859 NULL);
4860 result = walk_tree (&t,
4861 for_each_template_parm_r,
4862 &pfd,
4863 pfd.visited) != NULL_TREE;
4865 /* Clean up. */
4866 if (!visited)
4867 htab_delete (pfd.visited);
4869 return result;
4872 /* Returns true if T depends on any template parameter. */
4875 uses_template_parms (tree t)
4877 bool dependent_p;
4878 int saved_processing_template_decl;
4880 saved_processing_template_decl = processing_template_decl;
4881 if (!saved_processing_template_decl)
4882 processing_template_decl = 1;
4883 if (TYPE_P (t))
4884 dependent_p = dependent_type_p (t);
4885 else if (TREE_CODE (t) == TREE_VEC)
4886 dependent_p = any_dependent_template_arguments_p (t);
4887 else if (TREE_CODE (t) == TREE_LIST)
4888 dependent_p = (uses_template_parms (TREE_VALUE (t))
4889 || uses_template_parms (TREE_CHAIN (t)));
4890 else if (DECL_P (t)
4891 || EXPR_P (t)
4892 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
4893 || TREE_CODE (t) == OVERLOAD
4894 || TREE_CODE (t) == BASELINK
4895 || CONSTANT_CLASS_P (t))
4896 dependent_p = (type_dependent_expression_p (t)
4897 || value_dependent_expression_p (t));
4898 else
4900 gcc_assert (t == error_mark_node);
4901 dependent_p = false;
4904 processing_template_decl = saved_processing_template_decl;
4906 return dependent_p;
4909 /* Returns true if T depends on any template parameter with level LEVEL. */
4912 uses_template_parms_level (tree t, int level)
4914 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
4917 static int tinst_depth;
4918 extern int max_tinst_depth;
4919 #ifdef GATHER_STATISTICS
4920 int depth_reached;
4921 #endif
4922 static int tinst_level_tick;
4923 static int last_template_error_tick;
4925 /* We're starting to instantiate D; record the template instantiation context
4926 for diagnostics and to restore it later. */
4929 push_tinst_level (tree d)
4931 tree new;
4933 if (tinst_depth >= max_tinst_depth)
4935 /* If the instantiation in question still has unbound template parms,
4936 we don't really care if we can't instantiate it, so just return.
4937 This happens with base instantiation for implicit `typename'. */
4938 if (uses_template_parms (d))
4939 return 0;
4941 last_template_error_tick = tinst_level_tick;
4942 error ("template instantiation depth exceeds maximum of %d (use "
4943 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
4944 max_tinst_depth, d);
4946 print_instantiation_context ();
4948 return 0;
4951 new = make_tinst_level (d, input_location);
4952 TREE_CHAIN (new) = current_tinst_level;
4953 current_tinst_level = new;
4955 ++tinst_depth;
4956 #ifdef GATHER_STATISTICS
4957 if (tinst_depth > depth_reached)
4958 depth_reached = tinst_depth;
4959 #endif
4961 ++tinst_level_tick;
4962 return 1;
4965 /* We're done instantiating this template; return to the instantiation
4966 context. */
4968 void
4969 pop_tinst_level (void)
4971 tree old = current_tinst_level;
4973 /* Restore the filename and line number stashed away when we started
4974 this instantiation. */
4975 input_location = TINST_LOCATION (old);
4976 current_tinst_level = TREE_CHAIN (old);
4977 --tinst_depth;
4978 ++tinst_level_tick;
4981 /* We're instantiating a deferred template; restore the template
4982 instantiation context in which the instantiation was requested, which
4983 is one step out from LEVEL. */
4985 static void
4986 reopen_tinst_level (tree level)
4988 tree t;
4990 tinst_depth = 0;
4991 for (t = level; t; t = TREE_CHAIN (t))
4992 ++tinst_depth;
4994 current_tinst_level = level;
4995 pop_tinst_level ();
4998 /* Return the outermost template instantiation context, for use with
4999 -falt-external-templates. */
5001 tree
5002 tinst_for_decl (void)
5004 tree p = current_tinst_level;
5006 if (p)
5007 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
5009 return p;
5012 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5013 vector of template arguments, as for tsubst.
5015 Returns an appropriate tsubst'd friend declaration. */
5017 static tree
5018 tsubst_friend_function (tree decl, tree args)
5020 tree new_friend;
5021 location_t saved_loc = input_location;
5023 input_location = DECL_SOURCE_LOCATION (decl);
5025 if (TREE_CODE (decl) == FUNCTION_DECL
5026 && DECL_TEMPLATE_INSTANTIATION (decl)
5027 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5028 /* This was a friend declared with an explicit template
5029 argument list, e.g.:
5031 friend void f<>(T);
5033 to indicate that f was a template instantiation, not a new
5034 function declaration. Now, we have to figure out what
5035 instantiation of what template. */
5037 tree template_id, arglist, fns;
5038 tree new_args;
5039 tree tmpl;
5040 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5042 /* Friend functions are looked up in the containing namespace scope.
5043 We must enter that scope, to avoid finding member functions of the
5044 current cless with same name. */
5045 push_nested_namespace (ns);
5046 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5047 tf_error | tf_warning, NULL_TREE);
5048 pop_nested_namespace (ns);
5049 arglist = tsubst (DECL_TI_ARGS (decl), args,
5050 tf_error | tf_warning, NULL_TREE);
5051 template_id = lookup_template_function (fns, arglist);
5053 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5054 tmpl = determine_specialization (template_id, new_friend,
5055 &new_args,
5056 /*need_member_template=*/0,
5057 TREE_VEC_LENGTH (args));
5058 new_friend = instantiate_template (tmpl, new_args, tf_error);
5059 goto done;
5062 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5064 /* The NEW_FRIEND will look like an instantiation, to the
5065 compiler, but is not an instantiation from the point of view of
5066 the language. For example, we might have had:
5068 template <class T> struct S {
5069 template <class U> friend void f(T, U);
5072 Then, in S<int>, template <class U> void f(int, U) is not an
5073 instantiation of anything. */
5074 if (new_friend == error_mark_node)
5075 return error_mark_node;
5077 DECL_USE_TEMPLATE (new_friend) = 0;
5078 if (TREE_CODE (decl) == TEMPLATE_DECL)
5080 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5081 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5082 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5085 /* The mangled name for the NEW_FRIEND is incorrect. The function
5086 is not a template instantiation and should not be mangled like
5087 one. Therefore, we forget the mangling here; we'll recompute it
5088 later if we need it. */
5089 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5091 SET_DECL_RTL (new_friend, NULL_RTX);
5092 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5095 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5097 tree old_decl;
5098 tree new_friend_template_info;
5099 tree new_friend_result_template_info;
5100 tree ns;
5101 int new_friend_is_defn;
5103 /* We must save some information from NEW_FRIEND before calling
5104 duplicate decls since that function will free NEW_FRIEND if
5105 possible. */
5106 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5107 new_friend_is_defn =
5108 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5109 (template_for_substitution (new_friend)))
5110 != NULL_TREE);
5111 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5113 /* This declaration is a `primary' template. */
5114 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5116 new_friend_result_template_info
5117 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5119 else
5120 new_friend_result_template_info = NULL_TREE;
5122 /* Inside pushdecl_namespace_level, we will push into the
5123 current namespace. However, the friend function should go
5124 into the namespace of the template. */
5125 ns = decl_namespace_context (new_friend);
5126 push_nested_namespace (ns);
5127 old_decl = pushdecl_namespace_level (new_friend);
5128 pop_nested_namespace (ns);
5130 if (old_decl != new_friend)
5132 /* This new friend declaration matched an existing
5133 declaration. For example, given:
5135 template <class T> void f(T);
5136 template <class U> class C {
5137 template <class T> friend void f(T) {}
5140 the friend declaration actually provides the definition
5141 of `f', once C has been instantiated for some type. So,
5142 old_decl will be the out-of-class template declaration,
5143 while new_friend is the in-class definition.
5145 But, if `f' was called before this point, the
5146 instantiation of `f' will have DECL_TI_ARGS corresponding
5147 to `T' but not to `U', references to which might appear
5148 in the definition of `f'. Previously, the most general
5149 template for an instantiation of `f' was the out-of-class
5150 version; now it is the in-class version. Therefore, we
5151 run through all specialization of `f', adding to their
5152 DECL_TI_ARGS appropriately. In particular, they need a
5153 new set of outer arguments, corresponding to the
5154 arguments for this class instantiation.
5156 The same situation can arise with something like this:
5158 friend void f(int);
5159 template <class T> class C {
5160 friend void f(T) {}
5163 when `C<int>' is instantiated. Now, `f(int)' is defined
5164 in the class. */
5166 if (!new_friend_is_defn)
5167 /* On the other hand, if the in-class declaration does
5168 *not* provide a definition, then we don't want to alter
5169 existing definitions. We can just leave everything
5170 alone. */
5172 else
5174 /* Overwrite whatever template info was there before, if
5175 any, with the new template information pertaining to
5176 the declaration. */
5177 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5179 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5180 reregister_specialization (new_friend,
5181 most_general_template (old_decl),
5182 old_decl);
5183 else
5185 tree t;
5186 tree new_friend_args;
5188 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5189 = new_friend_result_template_info;
5191 new_friend_args = TI_ARGS (new_friend_template_info);
5192 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5193 t != NULL_TREE;
5194 t = TREE_CHAIN (t))
5196 tree spec = TREE_VALUE (t);
5198 DECL_TI_ARGS (spec)
5199 = add_outermost_template_args (new_friend_args,
5200 DECL_TI_ARGS (spec));
5203 /* Now, since specializations are always supposed to
5204 hang off of the most general template, we must move
5205 them. */
5206 t = most_general_template (old_decl);
5207 if (t != old_decl)
5209 DECL_TEMPLATE_SPECIALIZATIONS (t)
5210 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5211 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5212 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5217 /* The information from NEW_FRIEND has been merged into OLD_DECL
5218 by duplicate_decls. */
5219 new_friend = old_decl;
5222 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
5224 /* Check to see that the declaration is really present, and,
5225 possibly obtain an improved declaration. */
5226 tree fn = check_classfn (DECL_CONTEXT (new_friend),
5227 new_friend, NULL_TREE);
5229 if (fn)
5230 new_friend = fn;
5233 done:
5234 input_location = saved_loc;
5235 return new_friend;
5238 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5239 template arguments, as for tsubst.
5241 Returns an appropriate tsubst'd friend type or error_mark_node on
5242 failure. */
5244 static tree
5245 tsubst_friend_class (tree friend_tmpl, tree args)
5247 tree friend_type;
5248 tree tmpl;
5249 tree context;
5251 context = DECL_CONTEXT (friend_tmpl);
5253 if (context)
5255 if (TREE_CODE (context) == NAMESPACE_DECL)
5256 push_nested_namespace (context);
5257 else
5258 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5261 /* First, we look for a class template. */
5262 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5264 /* But, if we don't find one, it might be because we're in a
5265 situation like this:
5267 template <class T>
5268 struct S {
5269 template <class U>
5270 friend struct S;
5273 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5274 for `S<int>', not the TEMPLATE_DECL. */
5275 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5277 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5278 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5281 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5283 /* The friend template has already been declared. Just
5284 check to see that the declarations match, and install any new
5285 default parameters. We must tsubst the default parameters,
5286 of course. We only need the innermost template parameters
5287 because that is all that redeclare_class_template will look
5288 at. */
5289 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5290 > TMPL_ARGS_DEPTH (args))
5292 tree parms;
5293 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5294 args, tf_error | tf_warning);
5295 redeclare_class_template (TREE_TYPE (tmpl), parms);
5298 friend_type = TREE_TYPE (tmpl);
5300 else
5302 /* The friend template has not already been declared. In this
5303 case, the instantiation of the template class will cause the
5304 injection of this template into the global scope. */
5305 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5307 /* The new TMPL is not an instantiation of anything, so we
5308 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5309 the new type because that is supposed to be the corresponding
5310 template decl, i.e., TMPL. */
5311 DECL_USE_TEMPLATE (tmpl) = 0;
5312 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5313 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5314 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5315 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5317 /* Inject this template into the global scope. */
5318 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5321 if (context)
5323 if (TREE_CODE (context) == NAMESPACE_DECL)
5324 pop_nested_namespace (context);
5325 else
5326 pop_nested_class ();
5329 return friend_type;
5332 /* Returns zero if TYPE cannot be completed later due to circularity.
5333 Otherwise returns one. */
5335 static int
5336 can_complete_type_without_circularity (tree type)
5338 if (type == NULL_TREE || type == error_mark_node)
5339 return 0;
5340 else if (COMPLETE_TYPE_P (type))
5341 return 1;
5342 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5343 return can_complete_type_without_circularity (TREE_TYPE (type));
5344 else if (CLASS_TYPE_P (type)
5345 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5346 return 0;
5347 else
5348 return 1;
5351 tree
5352 instantiate_class_template (tree type)
5354 tree template, args, pattern, t, member;
5355 tree typedecl;
5356 tree pbinfo;
5357 tree base_list;
5359 if (type == error_mark_node)
5360 return error_mark_node;
5362 if (TYPE_BEING_DEFINED (type)
5363 || COMPLETE_TYPE_P (type)
5364 || dependent_type_p (type))
5365 return type;
5367 /* Figure out which template is being instantiated. */
5368 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5369 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5371 /* Figure out which arguments are being used to do the
5372 instantiation. */
5373 args = CLASSTYPE_TI_ARGS (type);
5375 /* Determine what specialization of the original template to
5376 instantiate. */
5377 t = most_specialized_class (template, args);
5378 if (t == error_mark_node)
5380 const char *str = "candidates are:";
5381 error ("ambiguous class template instantiation for %q#T", type);
5382 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5383 t = TREE_CHAIN (t))
5385 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
5387 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5388 str = " ";
5391 TYPE_BEING_DEFINED (type) = 1;
5392 return error_mark_node;
5395 if (t)
5396 pattern = TREE_TYPE (t);
5397 else
5398 pattern = TREE_TYPE (template);
5400 /* If the template we're instantiating is incomplete, then clearly
5401 there's nothing we can do. */
5402 if (!COMPLETE_TYPE_P (pattern))
5403 return type;
5405 /* If we've recursively instantiated too many templates, stop. */
5406 if (! push_tinst_level (type))
5407 return type;
5409 /* Now we're really doing the instantiation. Mark the type as in
5410 the process of being defined. */
5411 TYPE_BEING_DEFINED (type) = 1;
5413 /* We may be in the middle of deferred access check. Disable
5414 it now. */
5415 push_deferring_access_checks (dk_no_deferred);
5417 push_to_top_level ();
5419 if (t)
5421 /* This TYPE is actually an instantiation of a partial
5422 specialization. We replace the innermost set of ARGS with
5423 the arguments appropriate for substitution. For example,
5424 given:
5426 template <class T> struct S {};
5427 template <class T> struct S<T*> {};
5429 and supposing that we are instantiating S<int*>, ARGS will
5430 present be {int*} but we need {int}. */
5431 tree inner_args
5432 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5433 args);
5435 /* If there were multiple levels in ARGS, replacing the
5436 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5437 want, so we make a copy first. */
5438 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5440 args = copy_node (args);
5441 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5443 else
5444 args = inner_args;
5447 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5449 /* Set the input location to the template definition. This is needed
5450 if tsubsting causes an error. */
5451 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (pattern));
5453 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5454 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5455 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5456 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5457 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5458 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5459 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5460 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5461 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5462 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5463 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5464 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5465 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5466 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5467 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5468 if (ANON_AGGR_TYPE_P (pattern))
5469 SET_ANON_AGGR_TYPE_P (type);
5471 pbinfo = TYPE_BINFO (pattern);
5473 /* We should never instantiate a nested class before its enclosing
5474 class; we need to look up the nested class by name before we can
5475 instantiate it, and that lookup should instantiate the enclosing
5476 class. */
5477 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5478 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5479 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5481 base_list = NULL_TREE;
5482 if (BINFO_N_BASE_BINFOS (pbinfo))
5484 tree pbase_binfo;
5485 tree context = TYPE_CONTEXT (type);
5486 bool pop_p;
5487 int i;
5489 /* We must enter the scope containing the type, as that is where
5490 the accessibility of types named in dependent bases are
5491 looked up from. */
5492 pop_p = push_scope (context ? context : global_namespace);
5494 /* Substitute into each of the bases to determine the actual
5495 basetypes. */
5496 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5498 tree base;
5499 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5501 /* Substitute to figure out the base class. */
5502 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5503 if (base == error_mark_node)
5504 continue;
5506 base_list = tree_cons (access, base, base_list);
5507 if (BINFO_VIRTUAL_P (pbase_binfo))
5508 TREE_TYPE (base_list) = integer_type_node;
5511 /* The list is now in reverse order; correct that. */
5512 base_list = nreverse (base_list);
5514 if (pop_p)
5515 pop_scope (context ? context : global_namespace);
5517 /* Now call xref_basetypes to set up all the base-class
5518 information. */
5519 xref_basetypes (type, base_list);
5522 /* Now that our base classes are set up, enter the scope of the
5523 class, so that name lookups into base classes, etc. will work
5524 correctly. This is precisely analogous to what we do in
5525 begin_class_definition when defining an ordinary non-template
5526 class. */
5527 pushclass (type);
5529 /* Now members are processed in the order of declaration. */
5530 for (member = CLASSTYPE_DECL_LIST (pattern);
5531 member; member = TREE_CHAIN (member))
5533 tree t = TREE_VALUE (member);
5535 if (TREE_PURPOSE (member))
5537 if (TYPE_P (t))
5539 /* Build new CLASSTYPE_NESTED_UTDS. */
5541 tree tag = t;
5542 tree name = TYPE_IDENTIFIER (tag);
5543 tree newtag;
5545 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5546 if (newtag == error_mark_node)
5547 continue;
5549 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5551 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5552 /* Unfortunately, lookup_template_class sets
5553 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5554 instantiation (i.e., for the type of a member
5555 template class nested within a template class.)
5556 This behavior is required for
5557 maybe_process_partial_specialization to work
5558 correctly, but is not accurate in this case;
5559 the TAG is not an instantiation of anything.
5560 (The corresponding TEMPLATE_DECL is an
5561 instantiation, but the TYPE is not.) */
5562 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5564 /* Now, we call pushtag to put this NEWTAG into the scope of
5565 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5566 pushtag calling push_template_decl. We don't have to do
5567 this for enums because it will already have been done in
5568 tsubst_enum. */
5569 if (name)
5570 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5571 pushtag (name, newtag, /*globalize=*/0);
5574 else if (TREE_CODE (t) == FUNCTION_DECL
5575 || DECL_FUNCTION_TEMPLATE_P (t))
5577 /* Build new TYPE_METHODS. */
5578 tree r;
5580 if (TREE_CODE (t) == TEMPLATE_DECL)
5581 ++processing_template_decl;
5582 r = tsubst (t, args, tf_error, NULL_TREE);
5583 if (TREE_CODE (t) == TEMPLATE_DECL)
5584 --processing_template_decl;
5585 set_current_access_from_decl (r);
5586 grok_special_member_properties (r);
5587 finish_member_declaration (r);
5589 else
5591 /* Build new TYPE_FIELDS. */
5593 if (TREE_CODE (t) != CONST_DECL)
5595 tree r;
5597 /* The the file and line for this declaration, to
5598 assist in error message reporting. Since we
5599 called push_tinst_level above, we don't need to
5600 restore these. */
5601 input_location = DECL_SOURCE_LOCATION (t);
5603 if (TREE_CODE (t) == TEMPLATE_DECL)
5604 ++processing_template_decl;
5605 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5606 if (TREE_CODE (t) == TEMPLATE_DECL)
5607 --processing_template_decl;
5608 if (TREE_CODE (r) == VAR_DECL)
5610 tree init;
5612 if (DECL_INITIALIZED_IN_CLASS_P (r))
5613 init = tsubst_expr (DECL_INITIAL (t), args,
5614 tf_error | tf_warning, NULL_TREE);
5615 else
5616 init = NULL_TREE;
5618 finish_static_data_member_decl
5619 (r, init, /*asmspec_tree=*/NULL_TREE, /*flags=*/0);
5621 if (DECL_INITIALIZED_IN_CLASS_P (r))
5622 check_static_variable_definition (r, TREE_TYPE (r));
5624 else if (TREE_CODE (r) == FIELD_DECL)
5626 /* Determine whether R has a valid type and can be
5627 completed later. If R is invalid, then it is
5628 replaced by error_mark_node so that it will not be
5629 added to TYPE_FIELDS. */
5630 tree rtype = TREE_TYPE (r);
5631 if (can_complete_type_without_circularity (rtype))
5632 complete_type (rtype);
5634 if (!COMPLETE_TYPE_P (rtype))
5636 cxx_incomplete_type_error (r, rtype);
5637 r = error_mark_node;
5641 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5642 such a thing will already have been added to the field
5643 list by tsubst_enum in finish_member_declaration in the
5644 CLASSTYPE_NESTED_UTDS case above. */
5645 if (!(TREE_CODE (r) == TYPE_DECL
5646 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5647 && DECL_ARTIFICIAL (r)))
5649 set_current_access_from_decl (r);
5650 finish_member_declaration (r);
5655 else
5657 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5659 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5661 tree friend_type = t;
5662 tree new_friend_type;
5664 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5665 new_friend_type = tsubst_friend_class (friend_type, args);
5666 else if (uses_template_parms (friend_type))
5667 new_friend_type = tsubst (friend_type, args,
5668 tf_error | tf_warning, NULL_TREE);
5669 else if (CLASSTYPE_USE_TEMPLATE (friend_type))
5670 new_friend_type = friend_type;
5671 else
5673 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5675 /* The call to xref_tag_from_type does injection for friend
5676 classes. */
5677 push_nested_namespace (ns);
5678 new_friend_type =
5679 xref_tag_from_type (friend_type, NULL_TREE, 1);
5680 pop_nested_namespace (ns);
5683 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5684 /* Trick make_friend_class into realizing that the friend
5685 we're adding is a template, not an ordinary class. It's
5686 important that we use make_friend_class since it will
5687 perform some error-checking and output cross-reference
5688 information. */
5689 ++processing_template_decl;
5691 if (new_friend_type != error_mark_node)
5692 make_friend_class (type, new_friend_type,
5693 /*complain=*/false);
5695 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5696 --processing_template_decl;
5698 else
5700 /* Build new DECL_FRIENDLIST. */
5701 tree r;
5703 if (TREE_CODE (t) == TEMPLATE_DECL)
5705 ++processing_template_decl;
5706 push_deferring_access_checks (dk_no_check);
5709 r = tsubst_friend_function (t, args);
5710 add_friend (type, r, /*complain=*/false);
5711 if (TREE_CODE (t) == TEMPLATE_DECL)
5713 pop_deferring_access_checks ();
5714 --processing_template_decl;
5720 /* Set the file and line number information to whatever is given for
5721 the class itself. This puts error messages involving generated
5722 implicit functions at a predictable point, and the same point
5723 that would be used for non-template classes. */
5724 typedecl = TYPE_MAIN_DECL (type);
5725 input_location = DECL_SOURCE_LOCATION (typedecl);
5727 unreverse_member_declarations (type);
5728 finish_struct_1 (type);
5729 TYPE_BEING_DEFINED (type) = 0;
5731 /* Now that the class is complete, instantiate default arguments for
5732 any member functions. We don't do this earlier because the
5733 default arguments may reference members of the class. */
5734 if (!PRIMARY_TEMPLATE_P (template))
5735 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5736 if (TREE_CODE (t) == FUNCTION_DECL
5737 /* Implicitly generated member functions will not have template
5738 information; they are not instantiations, but instead are
5739 created "fresh" for each instantiation. */
5740 && DECL_TEMPLATE_INFO (t))
5741 tsubst_default_arguments (t);
5743 popclass ();
5744 pop_from_top_level ();
5745 pop_deferring_access_checks ();
5746 pop_tinst_level ();
5748 /* The vtable for a template class can be emitted in any translation
5749 unit in which the class is instantiated. When there is no key
5750 method, however, finish_struct_1 will already have added TYPE to
5751 the keyed_classes list. */
5752 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5753 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5755 return type;
5758 static tree
5759 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5761 tree r;
5763 if (!t)
5764 r = t;
5765 else if (TYPE_P (t))
5766 r = tsubst (t, args, complain, in_decl);
5767 else
5769 r = tsubst_expr (t, args, complain, in_decl);
5771 if (!uses_template_parms (r))
5773 /* Sometimes, one of the args was an expression involving a
5774 template constant parameter, like N - 1. Now that we've
5775 tsubst'd, we might have something like 2 - 1. This will
5776 confuse lookup_template_class, so we do constant folding
5777 here. We have to unset processing_template_decl, to fool
5778 tsubst_copy_and_build() into building an actual tree. */
5780 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5781 as simple as it's going to get, and trying to reprocess
5782 the trees will break. Once tsubst_expr et al DTRT for
5783 non-dependent exprs, this code can go away, as the type
5784 will always be set. */
5785 if (!TREE_TYPE (r))
5787 int saved_processing_template_decl = processing_template_decl;
5788 processing_template_decl = 0;
5789 r = tsubst_copy_and_build (r, /*args=*/NULL_TREE,
5790 tf_error, /*in_decl=*/NULL_TREE,
5791 /*function_p=*/false);
5792 processing_template_decl = saved_processing_template_decl;
5794 r = fold (r);
5797 return r;
5800 /* Substitute ARGS into the vector or list of template arguments T. */
5802 static tree
5803 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5805 int len = TREE_VEC_LENGTH (t);
5806 int need_new = 0, i;
5807 tree *elts = alloca (len * sizeof (tree));
5809 for (i = 0; i < len; i++)
5811 tree orig_arg = TREE_VEC_ELT (t, i);
5812 tree new_arg;
5814 if (TREE_CODE (orig_arg) == TREE_VEC)
5815 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5816 else
5817 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5819 if (new_arg == error_mark_node)
5820 return error_mark_node;
5822 elts[i] = new_arg;
5823 if (new_arg != orig_arg)
5824 need_new = 1;
5827 if (!need_new)
5828 return t;
5830 t = make_tree_vec (len);
5831 for (i = 0; i < len; i++)
5832 TREE_VEC_ELT (t, i) = elts[i];
5834 return t;
5837 /* Return the result of substituting ARGS into the template parameters
5838 given by PARMS. If there are m levels of ARGS and m + n levels of
5839 PARMS, then the result will contain n levels of PARMS. For
5840 example, if PARMS is `template <class T> template <class U>
5841 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5842 result will be `template <int*, double, class V>'. */
5844 static tree
5845 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
5847 tree r = NULL_TREE;
5848 tree* new_parms;
5850 for (new_parms = &r;
5851 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5852 new_parms = &(TREE_CHAIN (*new_parms)),
5853 parms = TREE_CHAIN (parms))
5855 tree new_vec =
5856 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5857 int i;
5859 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5861 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5862 tree default_value = TREE_PURPOSE (tuple);
5863 tree parm_decl = TREE_VALUE (tuple);
5865 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5866 default_value = tsubst_template_arg (default_value, args,
5867 complain, NULL_TREE);
5869 tuple = build_tree_list (default_value, parm_decl);
5870 TREE_VEC_ELT (new_vec, i) = tuple;
5873 *new_parms =
5874 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5875 - TMPL_ARGS_DEPTH (args)),
5876 new_vec, NULL_TREE);
5879 return r;
5882 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5883 type T. If T is not an aggregate or enumeration type, it is
5884 handled as if by tsubst. IN_DECL is as for tsubst. If
5885 ENTERING_SCOPE is nonzero, T is the context for a template which
5886 we are presently tsubst'ing. Return the substituted value. */
5888 static tree
5889 tsubst_aggr_type (tree t,
5890 tree args,
5891 tsubst_flags_t complain,
5892 tree in_decl,
5893 int entering_scope)
5895 if (t == NULL_TREE)
5896 return NULL_TREE;
5898 switch (TREE_CODE (t))
5900 case RECORD_TYPE:
5901 if (TYPE_PTRMEMFUNC_P (t))
5902 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5904 /* Else fall through. */
5905 case ENUMERAL_TYPE:
5906 case UNION_TYPE:
5907 if (TYPE_TEMPLATE_INFO (t))
5909 tree argvec;
5910 tree context;
5911 tree r;
5913 /* First, determine the context for the type we are looking
5914 up. */
5915 context = TYPE_CONTEXT (t);
5916 if (context)
5917 context = tsubst_aggr_type (context, args, complain,
5918 in_decl, /*entering_scope=*/1);
5920 /* Then, figure out what arguments are appropriate for the
5921 type we are trying to find. For example, given:
5923 template <class T> struct S;
5924 template <class T, class U> void f(T, U) { S<U> su; }
5926 and supposing that we are instantiating f<int, double>,
5927 then our ARGS will be {int, double}, but, when looking up
5928 S we only want {double}. */
5929 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
5930 complain, in_decl);
5931 if (argvec == error_mark_node)
5932 return error_mark_node;
5934 r = lookup_template_class (t, argvec, in_decl, context,
5935 entering_scope, complain);
5937 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5939 else
5940 /* This is not a template type, so there's nothing to do. */
5941 return t;
5943 default:
5944 return tsubst (t, args, complain, in_decl);
5948 /* Substitute into the default argument ARG (a default argument for
5949 FN), which has the indicated TYPE. */
5951 tree
5952 tsubst_default_argument (tree fn, tree type, tree arg)
5954 tree saved_class_ptr = NULL_TREE;
5955 tree saved_class_ref = NULL_TREE;
5957 /* This default argument came from a template. Instantiate the
5958 default argument here, not in tsubst. In the case of
5959 something like:
5961 template <class T>
5962 struct S {
5963 static T t();
5964 void f(T = t());
5967 we must be careful to do name lookup in the scope of S<T>,
5968 rather than in the current class. */
5969 push_access_scope (fn);
5970 /* The default argument expression should not be considered to be
5971 within the scope of FN. Since push_access_scope sets
5972 current_function_decl, we must explicitly clear it here. */
5973 current_function_decl = NULL_TREE;
5974 /* The "this" pointer is not valid in a default argument. */
5975 if (cfun)
5977 saved_class_ptr = current_class_ptr;
5978 cp_function_chain->x_current_class_ptr = NULL_TREE;
5979 saved_class_ref = current_class_ref;
5980 cp_function_chain->x_current_class_ref = NULL_TREE;
5983 push_deferring_access_checks(dk_no_deferred);
5984 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5985 tf_error | tf_warning, NULL_TREE);
5986 pop_deferring_access_checks();
5988 /* Restore the "this" pointer. */
5989 if (cfun)
5991 cp_function_chain->x_current_class_ptr = saved_class_ptr;
5992 cp_function_chain->x_current_class_ref = saved_class_ref;
5995 pop_access_scope (fn);
5997 /* Make sure the default argument is reasonable. */
5998 arg = check_default_argument (type, arg);
6000 return arg;
6003 /* Substitute into all the default arguments for FN. */
6005 static void
6006 tsubst_default_arguments (tree fn)
6008 tree arg;
6009 tree tmpl_args;
6011 tmpl_args = DECL_TI_ARGS (fn);
6013 /* If this function is not yet instantiated, we certainly don't need
6014 its default arguments. */
6015 if (uses_template_parms (tmpl_args))
6016 return;
6018 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6019 arg;
6020 arg = TREE_CHAIN (arg))
6021 if (TREE_PURPOSE (arg))
6022 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6023 TREE_VALUE (arg),
6024 TREE_PURPOSE (arg));
6027 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
6028 (already computed) substitution of ARGS into TREE_TYPE (T), if
6029 appropriate. Return the result of the substitution. Issue error
6030 and warning messages under control of COMPLAIN. */
6032 static tree
6033 tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
6035 location_t saved_loc;
6036 tree r = NULL_TREE;
6037 tree in_decl = t;
6039 /* Set the filename and linenumber to improve error-reporting. */
6040 saved_loc = input_location;
6041 input_location = DECL_SOURCE_LOCATION (t);
6043 switch (TREE_CODE (t))
6045 case TEMPLATE_DECL:
6047 /* We can get here when processing a member template function
6048 of a template class. */
6049 tree decl = DECL_TEMPLATE_RESULT (t);
6050 tree spec;
6051 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
6053 if (!is_template_template_parm)
6055 /* We might already have an instance of this template.
6056 The ARGS are for the surrounding class type, so the
6057 full args contain the tsubst'd args for the context,
6058 plus the innermost args from the template decl. */
6059 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6060 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6061 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6062 tree full_args;
6064 full_args = tsubst_template_args (tmpl_args, args,
6065 complain, in_decl);
6067 /* tsubst_template_args doesn't copy the vector if
6068 nothing changed. But, *something* should have
6069 changed. */
6070 gcc_assert (full_args != tmpl_args);
6072 spec = retrieve_specialization (t, full_args);
6073 if (spec != NULL_TREE)
6075 r = spec;
6076 break;
6080 /* Make a new template decl. It will be similar to the
6081 original, but will record the current template arguments.
6082 We also create a new function declaration, which is just
6083 like the old one, but points to this new template, rather
6084 than the old one. */
6085 r = copy_decl (t);
6086 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6087 TREE_CHAIN (r) = NULL_TREE;
6089 if (is_template_template_parm)
6091 tree new_decl = tsubst (decl, args, complain, in_decl);
6092 DECL_TEMPLATE_RESULT (r) = new_decl;
6093 TREE_TYPE (r) = TREE_TYPE (new_decl);
6094 break;
6097 DECL_CONTEXT (r)
6098 = tsubst_aggr_type (DECL_CONTEXT (t), args,
6099 complain, in_decl,
6100 /*entering_scope=*/1);
6101 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6103 if (TREE_CODE (decl) == TYPE_DECL)
6105 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6106 if (new_type == error_mark_node)
6107 return error_mark_node;
6109 TREE_TYPE (r) = new_type;
6110 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6111 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6112 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6114 else
6116 tree new_decl = tsubst (decl, args, complain, in_decl);
6117 if (new_decl == error_mark_node)
6118 return error_mark_node;
6120 DECL_TEMPLATE_RESULT (r) = new_decl;
6121 DECL_TI_TEMPLATE (new_decl) = r;
6122 TREE_TYPE (r) = TREE_TYPE (new_decl);
6123 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6126 SET_DECL_IMPLICIT_INSTANTIATION (r);
6127 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6128 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6130 /* The template parameters for this new template are all the
6131 template parameters for the old template, except the
6132 outermost level of parameters. */
6133 DECL_TEMPLATE_PARMS (r)
6134 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6135 complain);
6137 if (PRIMARY_TEMPLATE_P (t))
6138 DECL_PRIMARY_TEMPLATE (r) = r;
6140 if (TREE_CODE (decl) != TYPE_DECL)
6141 /* Record this non-type partial instantiation. */
6142 register_specialization (r, t,
6143 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
6145 break;
6147 case FUNCTION_DECL:
6149 tree ctx;
6150 tree argvec = NULL_TREE;
6151 tree *friends;
6152 tree gen_tmpl;
6153 int member;
6154 int args_depth;
6155 int parms_depth;
6157 /* Nobody should be tsubst'ing into non-template functions. */
6158 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6160 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6162 tree spec;
6163 bool dependent_p;
6165 /* If T is not dependent, just return it. We have to
6166 increment PROCESSING_TEMPLATE_DECL because
6167 value_dependent_expression_p assumes that nothing is
6168 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6169 ++processing_template_decl;
6170 dependent_p = value_dependent_expression_p (t);
6171 --processing_template_decl;
6172 if (!dependent_p)
6173 return t;
6175 /* Calculate the most general template of which R is a
6176 specialization, and the complete set of arguments used to
6177 specialize R. */
6178 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6179 argvec = tsubst_template_args (DECL_TI_ARGS
6180 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6181 args, complain, in_decl);
6183 /* Check to see if we already have this specialization. */
6184 spec = retrieve_specialization (gen_tmpl, argvec);
6186 if (spec)
6188 r = spec;
6189 break;
6192 /* We can see more levels of arguments than parameters if
6193 there was a specialization of a member template, like
6194 this:
6196 template <class T> struct S { template <class U> void f(); }
6197 template <> template <class U> void S<int>::f(U);
6199 Here, we'll be substituting into the specialization,
6200 because that's where we can find the code we actually
6201 want to generate, but we'll have enough arguments for
6202 the most general template.
6204 We also deal with the peculiar case:
6206 template <class T> struct S {
6207 template <class U> friend void f();
6209 template <class U> void f() {}
6210 template S<int>;
6211 template void f<double>();
6213 Here, the ARGS for the instantiation of will be {int,
6214 double}. But, we only need as many ARGS as there are
6215 levels of template parameters in CODE_PATTERN. We are
6216 careful not to get fooled into reducing the ARGS in
6217 situations like:
6219 template <class T> struct S { template <class U> void f(U); }
6220 template <class T> template <> void S<T>::f(int) {}
6222 which we can spot because the pattern will be a
6223 specialization in this case. */
6224 args_depth = TMPL_ARGS_DEPTH (args);
6225 parms_depth =
6226 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6227 if (args_depth > parms_depth
6228 && !DECL_TEMPLATE_SPECIALIZATION (t))
6229 args = get_innermost_template_args (args, parms_depth);
6231 else
6233 /* This special case arises when we have something like this:
6235 template <class T> struct S {
6236 friend void f<int>(int, double);
6239 Here, the DECL_TI_TEMPLATE for the friend declaration
6240 will be an IDENTIFIER_NODE. We are being called from
6241 tsubst_friend_function, and we want only to create a
6242 new decl (R) with appropriate types so that we can call
6243 determine_specialization. */
6244 gen_tmpl = NULL_TREE;
6247 if (DECL_CLASS_SCOPE_P (t))
6249 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6250 member = 2;
6251 else
6252 member = 1;
6253 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6254 complain, t, /*entering_scope=*/1);
6256 else
6258 member = 0;
6259 ctx = DECL_CONTEXT (t);
6261 type = tsubst (type, args, complain, in_decl);
6262 if (type == error_mark_node)
6263 return error_mark_node;
6265 /* We do NOT check for matching decls pushed separately at this
6266 point, as they may not represent instantiations of this
6267 template, and in any case are considered separate under the
6268 discrete model. */
6269 r = copy_decl (t);
6270 DECL_USE_TEMPLATE (r) = 0;
6271 TREE_TYPE (r) = type;
6272 /* Clear out the mangled name and RTL for the instantiation. */
6273 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6274 SET_DECL_RTL (r, NULL_RTX);
6275 DECL_INITIAL (r) = NULL_TREE;
6276 DECL_CONTEXT (r) = ctx;
6278 if (member && DECL_CONV_FN_P (r))
6279 /* Type-conversion operator. Reconstruct the name, in
6280 case it's the name of one of the template's parameters. */
6281 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6283 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6284 complain, t);
6285 DECL_RESULT (r) = NULL_TREE;
6287 TREE_STATIC (r) = 0;
6288 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6289 DECL_EXTERNAL (r) = 1;
6290 /* If this is an instantiation of a function with internal
6291 linkage, we already know what object file linkage will be
6292 assigned to the instantiation. */
6293 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6294 DECL_DEFER_OUTPUT (r) = 0;
6295 TREE_CHAIN (r) = NULL_TREE;
6296 DECL_PENDING_INLINE_INFO (r) = 0;
6297 DECL_PENDING_INLINE_P (r) = 0;
6298 DECL_SAVED_TREE (r) = NULL_TREE;
6299 TREE_USED (r) = 0;
6300 if (DECL_CLONED_FUNCTION (r))
6302 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6303 args, complain, t);
6304 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6305 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6308 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6309 this in the special friend case mentioned above where
6310 GEN_TMPL is NULL. */
6311 if (gen_tmpl)
6313 DECL_TEMPLATE_INFO (r)
6314 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6315 SET_DECL_IMPLICIT_INSTANTIATION (r);
6316 register_specialization (r, gen_tmpl, argvec);
6318 /* We're not supposed to instantiate default arguments
6319 until they are called, for a template. But, for a
6320 declaration like:
6322 template <class T> void f ()
6323 { extern void g(int i = T()); }
6325 we should do the substitution when the template is
6326 instantiated. We handle the member function case in
6327 instantiate_class_template since the default arguments
6328 might refer to other members of the class. */
6329 if (!member
6330 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6331 && !uses_template_parms (argvec))
6332 tsubst_default_arguments (r);
6335 /* Copy the list of befriending classes. */
6336 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6337 *friends;
6338 friends = &TREE_CHAIN (*friends))
6340 *friends = copy_node (*friends);
6341 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6342 args, complain,
6343 in_decl);
6346 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6348 maybe_retrofit_in_chrg (r);
6349 if (DECL_CONSTRUCTOR_P (r))
6350 grok_ctor_properties (ctx, r);
6351 /* If this is an instantiation of a member template, clone it.
6352 If it isn't, that'll be handled by
6353 clone_constructors_and_destructors. */
6354 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6355 clone_function_decl (r, /*update_method_vec_p=*/0);
6357 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6358 grok_op_properties (r, DECL_FRIEND_P (r),
6359 (complain & tf_error) != 0);
6361 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6362 SET_DECL_FRIEND_CONTEXT (r,
6363 tsubst (DECL_FRIEND_CONTEXT (t),
6364 args, complain, in_decl));
6366 break;
6368 case PARM_DECL:
6370 r = copy_node (t);
6371 if (DECL_TEMPLATE_PARM_P (t))
6372 SET_DECL_TEMPLATE_PARM_P (r);
6374 TREE_TYPE (r) = type;
6375 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6377 if (DECL_INITIAL (r))
6379 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6380 DECL_INITIAL (r) = TREE_TYPE (r);
6381 else
6382 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6383 complain, in_decl);
6386 DECL_CONTEXT (r) = NULL_TREE;
6388 if (!DECL_TEMPLATE_PARM_P (r))
6389 DECL_ARG_TYPE (r) = type_passed_as (type);
6390 if (TREE_CHAIN (t))
6391 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6392 complain, TREE_CHAIN (t));
6394 break;
6396 case FIELD_DECL:
6398 r = copy_decl (t);
6399 TREE_TYPE (r) = type;
6400 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6402 /* We don't have to set DECL_CONTEXT here; it is set by
6403 finish_member_declaration. */
6404 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6405 complain, in_decl);
6406 TREE_CHAIN (r) = NULL_TREE;
6407 if (VOID_TYPE_P (type))
6408 cp_error_at ("instantiation of %qD as type %qT", r, type);
6410 break;
6412 case USING_DECL:
6414 r = copy_node (t);
6415 /* It is not a dependent using decl any more. */
6416 TREE_TYPE (r) = void_type_node;
6417 DECL_INITIAL (r)
6418 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6419 DECL_NAME (r)
6420 = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
6421 TREE_CHAIN (r) = NULL_TREE;
6423 break;
6425 case TYPE_DECL:
6426 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6427 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6429 /* If this is the canonical decl, we don't have to mess with
6430 instantiations, and often we can't (for typename, template
6431 type parms and such). Note that TYPE_NAME is not correct for
6432 the above test if we've copied the type for a typedef. */
6433 r = TYPE_NAME (type);
6434 break;
6437 /* Fall through. */
6439 case VAR_DECL:
6441 tree argvec = NULL_TREE;
6442 tree gen_tmpl = NULL_TREE;
6443 tree spec;
6444 tree tmpl = NULL_TREE;
6445 tree ctx;
6446 int local_p;
6448 /* Assume this is a non-local variable. */
6449 local_p = 0;
6451 if (TYPE_P (CP_DECL_CONTEXT (t)))
6452 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6453 complain,
6454 in_decl, /*entering_scope=*/1);
6455 else if (DECL_NAMESPACE_SCOPE_P (t))
6456 ctx = DECL_CONTEXT (t);
6457 else
6459 /* Subsequent calls to pushdecl will fill this in. */
6460 ctx = NULL_TREE;
6461 local_p = 1;
6464 /* Check to see if we already have this specialization. */
6465 if (!local_p)
6467 tmpl = DECL_TI_TEMPLATE (t);
6468 gen_tmpl = most_general_template (tmpl);
6469 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6470 spec = retrieve_specialization (gen_tmpl, argvec);
6472 else
6473 spec = retrieve_local_specialization (t);
6475 if (spec)
6477 r = spec;
6478 break;
6481 r = copy_decl (t);
6482 if (TREE_CODE (r) == VAR_DECL)
6484 type = complete_type (type);
6485 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6486 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6488 else if (DECL_SELF_REFERENCE_P (t))
6489 SET_DECL_SELF_REFERENCE_P (r);
6490 TREE_TYPE (r) = type;
6491 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6492 DECL_CONTEXT (r) = ctx;
6493 /* Clear out the mangled name and RTL for the instantiation. */
6494 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6495 SET_DECL_RTL (r, NULL_RTX);
6497 /* Don't try to expand the initializer until someone tries to use
6498 this variable; otherwise we run into circular dependencies. */
6499 DECL_INITIAL (r) = NULL_TREE;
6500 SET_DECL_RTL (r, NULL_RTX);
6501 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6503 /* Even if the original location is out of scope, the newly
6504 substituted one is not. */
6505 if (TREE_CODE (r) == VAR_DECL)
6507 DECL_DEAD_FOR_LOCAL (r) = 0;
6508 DECL_INITIALIZED_P (r) = 0;
6511 if (!local_p)
6513 /* A static data member declaration is always marked
6514 external when it is declared in-class, even if an
6515 initializer is present. We mimic the non-template
6516 processing here. */
6517 DECL_EXTERNAL (r) = 1;
6519 register_specialization (r, gen_tmpl, argvec);
6520 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6521 SET_DECL_IMPLICIT_INSTANTIATION (r);
6523 else
6524 register_local_specialization (r, t);
6526 TREE_CHAIN (r) = NULL_TREE;
6527 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6528 cp_error_at ("instantiation of %qD as type %qT", r, type);
6529 /* Compute the size, alignment, etc. of R. */
6530 layout_decl (r, 0);
6532 break;
6534 default:
6535 gcc_unreachable ();
6538 /* Restore the file and line information. */
6539 input_location = saved_loc;
6541 return r;
6544 /* Substitute into the ARG_TYPES of a function type. */
6546 static tree
6547 tsubst_arg_types (tree arg_types,
6548 tree args,
6549 tsubst_flags_t complain,
6550 tree in_decl)
6552 tree remaining_arg_types;
6553 tree type;
6555 if (!arg_types || arg_types == void_list_node)
6556 return arg_types;
6558 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6559 args, complain, in_decl);
6560 if (remaining_arg_types == error_mark_node)
6561 return error_mark_node;
6563 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6564 if (type == error_mark_node)
6565 return error_mark_node;
6566 if (VOID_TYPE_P (type))
6568 if (complain & tf_error)
6570 error ("invalid parameter type %qT", type);
6571 if (in_decl)
6572 cp_error_at ("in declaration %qD", in_decl);
6574 return error_mark_node;
6577 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6578 top-level qualifiers as required. */
6579 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6581 /* Note that we do not substitute into default arguments here. The
6582 standard mandates that they be instantiated only when needed,
6583 which is done in build_over_call. */
6584 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6585 remaining_arg_types);
6589 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6590 *not* handle the exception-specification for FNTYPE, because the
6591 initial substitution of explicitly provided template parameters
6592 during argument deduction forbids substitution into the
6593 exception-specification:
6595 [temp.deduct]
6597 All references in the function type of the function template to the
6598 corresponding template parameters are replaced by the specified tem-
6599 plate argument values. If a substitution in a template parameter or
6600 in the function type of the function template results in an invalid
6601 type, type deduction fails. [Note: The equivalent substitution in
6602 exception specifications is done only when the function is instanti-
6603 ated, at which point a program is ill-formed if the substitution
6604 results in an invalid type.] */
6606 static tree
6607 tsubst_function_type (tree t,
6608 tree args,
6609 tsubst_flags_t complain,
6610 tree in_decl)
6612 tree return_type;
6613 tree arg_types;
6614 tree fntype;
6616 /* The TYPE_CONTEXT is not used for function/method types. */
6617 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6619 /* Substitute the return type. */
6620 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6621 if (return_type == error_mark_node)
6622 return error_mark_node;
6624 /* Substitute the argument types. */
6625 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6626 complain, in_decl);
6627 if (arg_types == error_mark_node)
6628 return error_mark_node;
6630 /* Construct a new type node and return it. */
6631 if (TREE_CODE (t) == FUNCTION_TYPE)
6632 fntype = build_function_type (return_type, arg_types);
6633 else
6635 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6636 if (! IS_AGGR_TYPE (r))
6638 /* [temp.deduct]
6640 Type deduction may fail for any of the following
6641 reasons:
6643 -- Attempting to create "pointer to member of T" when T
6644 is not a class type. */
6645 if (complain & tf_error)
6646 error ("creating pointer to member function of non-class type %qT",
6648 return error_mark_node;
6651 fntype = build_method_type_directly (r, return_type,
6652 TREE_CHAIN (arg_types));
6654 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6655 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6657 return fntype;
6660 /* Substitute into the PARMS of a call-declarator. */
6662 static tree
6663 tsubst_call_declarator_parms (tree parms,
6664 tree args,
6665 tsubst_flags_t complain,
6666 tree in_decl)
6668 tree new_parms;
6669 tree type;
6670 tree defarg;
6672 if (!parms || parms == void_list_node)
6673 return parms;
6675 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6676 args, complain, in_decl);
6678 /* Figure out the type of this parameter. */
6679 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6681 /* Figure out the default argument as well. Note that we use
6682 tsubst_expr since the default argument is really an expression. */
6683 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6685 /* Chain this parameter on to the front of those we have already
6686 processed. We don't use hash_tree_cons because that function
6687 doesn't check TREE_PARMLIST. */
6688 new_parms = tree_cons (defarg, type, new_parms);
6690 return new_parms;
6693 /* Take the tree structure T and replace template parameters used
6694 therein with the argument vector ARGS. IN_DECL is an associated
6695 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6696 Issue error and warning messages under control of COMPLAIN. Note
6697 that we must be relatively non-tolerant of extensions here, in
6698 order to preserve conformance; if we allow substitutions that
6699 should not be allowed, we may allow argument deductions that should
6700 not succeed, and therefore report ambiguous overload situations
6701 where there are none. In theory, we could allow the substitution,
6702 but indicate that it should have failed, and allow our caller to
6703 make sure that the right thing happens, but we don't try to do this
6704 yet.
6706 This function is used for dealing with types, decls and the like;
6707 for expressions, use tsubst_expr or tsubst_copy. */
6709 static tree
6710 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6712 tree type, r;
6714 if (t == NULL_TREE || t == error_mark_node
6715 || t == integer_type_node
6716 || t == void_type_node
6717 || t == char_type_node
6718 || t == unknown_type_node
6719 || TREE_CODE (t) == NAMESPACE_DECL)
6720 return t;
6722 if (TREE_CODE (t) == IDENTIFIER_NODE)
6723 type = IDENTIFIER_TYPE_VALUE (t);
6724 else
6725 type = TREE_TYPE (t);
6727 gcc_assert (type != unknown_type_node);
6729 if (type && TREE_CODE (t) != FUNCTION_DECL
6730 && TREE_CODE (t) != TYPENAME_TYPE
6731 && TREE_CODE (t) != TEMPLATE_DECL
6732 && TREE_CODE (t) != IDENTIFIER_NODE
6733 && TREE_CODE (t) != FUNCTION_TYPE
6734 && TREE_CODE (t) != METHOD_TYPE)
6735 type = tsubst (type, args, complain, in_decl);
6736 if (type == error_mark_node)
6737 return error_mark_node;
6739 if (DECL_P (t))
6740 return tsubst_decl (t, args, type, complain);
6742 switch (TREE_CODE (t))
6744 case RECORD_TYPE:
6745 case UNION_TYPE:
6746 case ENUMERAL_TYPE:
6747 return tsubst_aggr_type (t, args, complain, in_decl,
6748 /*entering_scope=*/0);
6750 case ERROR_MARK:
6751 case IDENTIFIER_NODE:
6752 case VOID_TYPE:
6753 case REAL_TYPE:
6754 case COMPLEX_TYPE:
6755 case VECTOR_TYPE:
6756 case BOOLEAN_TYPE:
6757 case INTEGER_CST:
6758 case REAL_CST:
6759 case STRING_CST:
6760 return t;
6762 case INTEGER_TYPE:
6763 if (t == integer_type_node)
6764 return t;
6766 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6767 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6768 return t;
6771 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6773 /* The array dimension behaves like a non-type template arg,
6774 in that we want to fold it as much as possible. */
6775 max = tsubst_template_arg (omax, args, complain, in_decl);
6776 if (!processing_template_decl)
6777 max = decl_constant_value (max);
6779 if (integer_zerop (omax))
6781 /* Still allow an explicit array of size zero. */
6782 if (pedantic)
6783 pedwarn ("creating array with size zero");
6785 else if (integer_zerop (max)
6786 || (TREE_CODE (max) == INTEGER_CST
6787 && INT_CST_LT (max, integer_zero_node)))
6789 /* [temp.deduct]
6791 Type deduction may fail for any of the following
6792 reasons:
6794 Attempting to create an array with a size that is
6795 zero or negative. */
6796 if (complain & tf_error)
6797 error ("creating array with size zero (%qE)", max);
6799 return error_mark_node;
6802 return compute_array_index_type (NULL_TREE, max);
6805 case TEMPLATE_TYPE_PARM:
6806 case TEMPLATE_TEMPLATE_PARM:
6807 case BOUND_TEMPLATE_TEMPLATE_PARM:
6808 case TEMPLATE_PARM_INDEX:
6810 int idx;
6811 int level;
6812 int levels;
6813 tree arg = NULL_TREE;
6815 r = NULL_TREE;
6817 gcc_assert (TREE_VEC_LENGTH (args) > 0);
6818 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6819 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6820 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6822 idx = TEMPLATE_TYPE_IDX (t);
6823 level = TEMPLATE_TYPE_LEVEL (t);
6825 else
6827 idx = TEMPLATE_PARM_IDX (t);
6828 level = TEMPLATE_PARM_LEVEL (t);
6831 levels = TMPL_ARGS_DEPTH (args);
6832 if (level <= levels)
6833 arg = TMPL_ARG (args, level, idx);
6835 if (arg == error_mark_node)
6836 return error_mark_node;
6837 else if (arg != NULL_TREE)
6839 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6841 gcc_assert (TYPE_P (arg));
6842 return cp_build_qualified_type_real
6843 (arg, cp_type_quals (arg) | cp_type_quals (t),
6844 complain | tf_ignore_bad_quals);
6846 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6848 /* We are processing a type constructed from a
6849 template template parameter. */
6850 tree argvec = tsubst (TYPE_TI_ARGS (t),
6851 args, complain, in_decl);
6852 if (argvec == error_mark_node)
6853 return error_mark_node;
6855 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
6856 are resolving nested-types in the signature of a
6857 member function templates. Otherwise ARG is a
6858 TEMPLATE_DECL and is the real template to be
6859 instantiated. */
6860 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6861 arg = TYPE_NAME (arg);
6863 r = lookup_template_class (arg,
6864 argvec, in_decl,
6865 DECL_CONTEXT (arg),
6866 /*entering_scope=*/0,
6867 complain);
6868 return cp_build_qualified_type_real
6869 (r, TYPE_QUALS (t), complain);
6871 else
6872 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6873 return arg;
6876 if (level == 1)
6877 /* This can happen during the attempted tsubst'ing in
6878 unify. This means that we don't yet have any information
6879 about the template parameter in question. */
6880 return t;
6882 /* If we get here, we must have been looking at a parm for a
6883 more deeply nested template. Make a new version of this
6884 template parameter, but with a lower level. */
6885 switch (TREE_CODE (t))
6887 case TEMPLATE_TYPE_PARM:
6888 case TEMPLATE_TEMPLATE_PARM:
6889 case BOUND_TEMPLATE_TEMPLATE_PARM:
6890 if (cp_type_quals (t))
6892 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6893 r = cp_build_qualified_type_real
6894 (r, cp_type_quals (t),
6895 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6896 ? tf_ignore_bad_quals : 0));
6898 else
6900 r = copy_type (t);
6901 TEMPLATE_TYPE_PARM_INDEX (r)
6902 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6903 r, levels);
6904 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6905 TYPE_MAIN_VARIANT (r) = r;
6906 TYPE_POINTER_TO (r) = NULL_TREE;
6907 TYPE_REFERENCE_TO (r) = NULL_TREE;
6909 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6911 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6912 complain, in_decl);
6913 if (argvec == error_mark_node)
6914 return error_mark_node;
6916 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6917 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6920 break;
6922 case TEMPLATE_PARM_INDEX:
6923 r = reduce_template_parm_level (t, type, levels);
6924 break;
6926 default:
6927 gcc_unreachable ();
6930 return r;
6933 case TREE_LIST:
6935 tree purpose, value, chain;
6937 if (t == void_list_node)
6938 return t;
6940 purpose = TREE_PURPOSE (t);
6941 if (purpose)
6943 purpose = tsubst (purpose, args, complain, in_decl);
6944 if (purpose == error_mark_node)
6945 return error_mark_node;
6947 value = TREE_VALUE (t);
6948 if (value)
6950 value = tsubst (value, args, complain, in_decl);
6951 if (value == error_mark_node)
6952 return error_mark_node;
6954 chain = TREE_CHAIN (t);
6955 if (chain && chain != void_type_node)
6957 chain = tsubst (chain, args, complain, in_decl);
6958 if (chain == error_mark_node)
6959 return error_mark_node;
6961 if (purpose == TREE_PURPOSE (t)
6962 && value == TREE_VALUE (t)
6963 && chain == TREE_CHAIN (t))
6964 return t;
6965 return hash_tree_cons (purpose, value, chain);
6968 case TREE_BINFO:
6969 /* We should never be tsubsting a binfo. */
6970 gcc_unreachable ();
6972 case TREE_VEC:
6973 /* A vector of template arguments. */
6974 gcc_assert (!type);
6975 return tsubst_template_args (t, args, complain, in_decl);
6977 case POINTER_TYPE:
6978 case REFERENCE_TYPE:
6980 enum tree_code code;
6982 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6983 return t;
6985 code = TREE_CODE (t);
6988 /* [temp.deduct]
6990 Type deduction may fail for any of the following
6991 reasons:
6993 -- Attempting to create a pointer to reference type.
6994 -- Attempting to create a reference to a reference type or
6995 a reference to void. */
6996 if (TREE_CODE (type) == REFERENCE_TYPE
6997 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6999 static location_t last_loc;
7001 /* We keep track of the last time we issued this error
7002 message to avoid spewing a ton of messages during a
7003 single bad template instantiation. */
7004 if (complain & tf_error
7005 #ifdef USE_MAPPED_LOCATION
7006 && last_loc != input_location
7007 #else
7008 && (last_loc.line != input_line
7009 || last_loc.file != input_filename)
7010 #endif
7013 if (TREE_CODE (type) == VOID_TYPE)
7014 error ("forming reference to void");
7015 else
7016 error ("forming %s to reference type %qT",
7017 (code == POINTER_TYPE) ? "pointer" : "reference",
7018 type);
7019 last_loc = input_location;
7022 return error_mark_node;
7024 else if (code == POINTER_TYPE)
7026 r = build_pointer_type (type);
7027 if (TREE_CODE (type) == METHOD_TYPE)
7028 r = build_ptrmemfunc_type (r);
7030 else
7031 r = build_reference_type (type);
7032 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7034 if (r != error_mark_node)
7035 /* Will this ever be needed for TYPE_..._TO values? */
7036 layout_type (r);
7038 return r;
7040 case OFFSET_TYPE:
7042 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7043 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7045 /* [temp.deduct]
7047 Type deduction may fail for any of the following
7048 reasons:
7050 -- Attempting to create "pointer to member of T" when T
7051 is not a class type. */
7052 if (complain & tf_error)
7053 error ("creating pointer to member of non-class type %qT", r);
7054 return error_mark_node;
7056 if (TREE_CODE (type) == REFERENCE_TYPE)
7058 if (complain & tf_error)
7059 error ("creating pointer to member reference type %qT", type);
7061 return error_mark_node;
7063 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7064 if (TREE_CODE (type) == FUNCTION_TYPE)
7066 /* This is really a method type. The cv qualifiers of the
7067 this pointer should _not_ be determined by the cv
7068 qualifiers of the class type. They should be held
7069 somewhere in the FUNCTION_TYPE, but we don't do that at
7070 the moment. Consider
7071 typedef void (Func) () const;
7073 template <typename T1> void Foo (Func T1::*);
7076 tree method_type;
7078 method_type = build_method_type_directly (TYPE_MAIN_VARIANT (r),
7079 TREE_TYPE (type),
7080 TYPE_ARG_TYPES (type));
7081 return build_ptrmemfunc_type (build_pointer_type (method_type));
7083 else
7084 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7085 TYPE_QUALS (t),
7086 complain);
7088 case FUNCTION_TYPE:
7089 case METHOD_TYPE:
7091 tree fntype;
7092 tree raises;
7094 fntype = tsubst_function_type (t, args, complain, in_decl);
7095 if (fntype == error_mark_node)
7096 return error_mark_node;
7098 /* Substitute the exception specification. */
7099 raises = TYPE_RAISES_EXCEPTIONS (t);
7100 if (raises)
7102 tree list = NULL_TREE;
7104 if (! TREE_VALUE (raises))
7105 list = raises;
7106 else
7107 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
7109 tree spec = TREE_VALUE (raises);
7111 spec = tsubst (spec, args, complain, in_decl);
7112 if (spec == error_mark_node)
7113 return spec;
7114 list = add_exception_specifier (list, spec, complain);
7116 fntype = build_exception_variant (fntype, list);
7118 return fntype;
7120 case ARRAY_TYPE:
7122 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7123 if (domain == error_mark_node)
7124 return error_mark_node;
7126 /* As an optimization, we avoid regenerating the array type if
7127 it will obviously be the same as T. */
7128 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7129 return t;
7131 /* These checks should match the ones in grokdeclarator.
7133 [temp.deduct]
7135 The deduction may fail for any of the following reasons:
7137 -- Attempting to create an array with an element type that
7138 is void, a function type, or a reference type, or [DR337]
7139 an abstract class type. */
7140 if (TREE_CODE (type) == VOID_TYPE
7141 || TREE_CODE (type) == FUNCTION_TYPE
7142 || TREE_CODE (type) == REFERENCE_TYPE)
7144 if (complain & tf_error)
7145 error ("creating array of %qT", type);
7146 return error_mark_node;
7148 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7150 if (complain & tf_error)
7151 error ("creating array of %qT, which is an abstract class type",
7152 type);
7153 return error_mark_node;
7156 r = build_cplus_array_type (type, domain);
7157 return r;
7160 case PLUS_EXPR:
7161 case MINUS_EXPR:
7163 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7164 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7166 if (e1 == error_mark_node || e2 == error_mark_node)
7167 return error_mark_node;
7169 return fold (build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2));
7172 case NEGATE_EXPR:
7173 case NOP_EXPR:
7175 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7176 if (e == error_mark_node)
7177 return error_mark_node;
7179 return fold (build1 (TREE_CODE (t), TREE_TYPE (t), e));
7182 case TYPENAME_TYPE:
7184 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7185 in_decl, /*entering_scope=*/1);
7186 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7187 complain, in_decl);
7189 if (ctx == error_mark_node || f == error_mark_node)
7190 return error_mark_node;
7192 if (!IS_AGGR_TYPE (ctx))
7194 if (complain & tf_error)
7195 error ("%qT is not a class, struct, or union type", ctx);
7196 return error_mark_node;
7198 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7200 /* Normally, make_typename_type does not require that the CTX
7201 have complete type in order to allow things like:
7203 template <class T> struct S { typename S<T>::X Y; };
7205 But, such constructs have already been resolved by this
7206 point, so here CTX really should have complete type, unless
7207 it's a partial instantiation. */
7208 ctx = complete_type (ctx);
7209 if (!COMPLETE_TYPE_P (ctx))
7211 if (complain & tf_error)
7212 cxx_incomplete_type_error (NULL_TREE, ctx);
7213 return error_mark_node;
7217 f = make_typename_type (ctx, f,
7218 (complain & tf_error) | tf_keep_type_decl);
7219 if (f == error_mark_node)
7220 return f;
7221 if (TREE_CODE (f) == TYPE_DECL)
7223 complain |= tf_ignore_bad_quals;
7224 f = TREE_TYPE (f);
7227 return cp_build_qualified_type_real
7228 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7231 case UNBOUND_CLASS_TEMPLATE:
7233 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7234 in_decl, /*entering_scope=*/1);
7235 tree name = TYPE_IDENTIFIER (t);
7237 if (ctx == error_mark_node || name == error_mark_node)
7238 return error_mark_node;
7240 return make_unbound_class_template (ctx, name, complain);
7243 case INDIRECT_REF:
7244 case ADDR_EXPR:
7245 case CALL_EXPR:
7246 gcc_unreachable ();
7248 case ARRAY_REF:
7250 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7251 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7252 if (e1 == error_mark_node || e2 == error_mark_node)
7253 return error_mark_node;
7255 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7258 case SCOPE_REF:
7260 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7261 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7262 if (e1 == error_mark_node || e2 == error_mark_node)
7263 return error_mark_node;
7265 return build_nt (TREE_CODE (t), e1, e2);
7268 case TYPEOF_TYPE:
7270 tree type;
7272 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7273 complain, in_decl));
7274 return cp_build_qualified_type_real (type,
7275 cp_type_quals (t)
7276 | cp_type_quals (type),
7277 complain);
7280 default:
7281 sorry ("use of `%s' in template",
7282 tree_code_name [(int) TREE_CODE (t)]);
7283 return error_mark_node;
7287 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7288 type of the expression on the left-hand side of the "." or "->"
7289 operator. */
7291 static tree
7292 tsubst_baselink (tree baselink, tree object_type,
7293 tree args, tsubst_flags_t complain, tree in_decl)
7295 tree name;
7296 tree qualifying_scope;
7297 tree fns;
7298 tree template_args = 0;
7299 bool template_id_p = false;
7301 /* A baselink indicates a function from a base class. The
7302 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7303 non-dependent types; otherwise, the lookup could not have
7304 succeeded. However, they may indicate bases of the template
7305 class, rather than the instantiated class.
7307 In addition, lookups that were not ambiguous before may be
7308 ambiguous now. Therefore, we perform the lookup again. */
7309 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7310 fns = BASELINK_FUNCTIONS (baselink);
7311 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7313 template_id_p = true;
7314 template_args = TREE_OPERAND (fns, 1);
7315 fns = TREE_OPERAND (fns, 0);
7316 if (template_args)
7317 template_args = tsubst_template_args (template_args, args,
7318 complain, in_decl);
7320 name = DECL_NAME (get_first_fn (fns));
7321 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7323 /* If lookup found a single function, mark it as used at this
7324 point. (If it lookup found multiple functions the one selected
7325 later by overload resolution will be marked as used at that
7326 point.) */
7327 if (BASELINK_P (baselink))
7328 fns = BASELINK_FUNCTIONS (baselink);
7329 if (!template_id_p && !really_overloaded_fn (fns))
7330 mark_used (OVL_CURRENT (fns));
7332 /* Add back the template arguments, if present. */
7333 if (BASELINK_P (baselink) && template_id_p)
7334 BASELINK_FUNCTIONS (baselink)
7335 = build_nt (TEMPLATE_ID_EXPR,
7336 BASELINK_FUNCTIONS (baselink),
7337 template_args);
7339 if (!object_type)
7340 object_type = current_class_type;
7341 return adjust_result_of_qualified_name_lookup (baselink,
7342 qualifying_scope,
7343 object_type);
7346 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7347 true if the qualified-id will be a postfix-expression in-and-of
7348 itself; false if more of the postfix-expression follows the
7349 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7350 of "&". */
7352 static tree
7353 tsubst_qualified_id (tree qualified_id, tree args,
7354 tsubst_flags_t complain, tree in_decl,
7355 bool done, bool address_p)
7357 tree expr;
7358 tree scope;
7359 tree name;
7360 bool is_template;
7361 tree template_args;
7363 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7365 /* Figure out what name to look up. */
7366 name = TREE_OPERAND (qualified_id, 1);
7367 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7369 is_template = true;
7370 template_args = TREE_OPERAND (name, 1);
7371 if (template_args)
7372 template_args = tsubst_template_args (template_args, args,
7373 complain, in_decl);
7374 name = TREE_OPERAND (name, 0);
7376 else
7378 is_template = false;
7379 template_args = NULL_TREE;
7382 /* Substitute into the qualifying scope. When there are no ARGS, we
7383 are just trying to simplify a non-dependent expression. In that
7384 case the qualifying scope may be dependent, and, in any case,
7385 substituting will not help. */
7386 scope = TREE_OPERAND (qualified_id, 0);
7387 if (args)
7389 scope = tsubst (scope, args, complain, in_decl);
7390 expr = tsubst_copy (name, args, complain, in_decl);
7392 else
7393 expr = name;
7395 if (dependent_type_p (scope))
7396 return build_nt (SCOPE_REF, scope, expr);
7398 if (!BASELINK_P (name) && !DECL_P (expr))
7400 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7401 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7402 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7404 if (complain & tf_error)
7406 error ("dependent-name %qE is parsed as a non-type, but "
7407 "instantiation yields a type", qualified_id);
7408 inform ("say %<typename %E%> if a type is meant", qualified_id);
7410 return error_mark_node;
7414 if (DECL_P (expr))
7416 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7417 scope);
7418 /* Remember that there was a reference to this entity. */
7419 mark_used (expr);
7422 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7424 if (complain & tf_error)
7425 qualified_name_lookup_error (scope,
7426 TREE_OPERAND (qualified_id, 1),
7427 expr);
7428 return error_mark_node;
7431 if (is_template)
7432 expr = lookup_template_function (expr, template_args);
7434 if (expr == error_mark_node && complain & tf_error)
7435 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7436 expr);
7437 else if (TYPE_P (scope))
7439 expr = (adjust_result_of_qualified_name_lookup
7440 (expr, scope, current_class_type));
7441 expr = finish_qualified_id_expr (scope, expr, done, address_p);
7444 return expr;
7447 /* Like tsubst, but deals with expressions. This function just replaces
7448 template parms; to finish processing the resultant expression, use
7449 tsubst_expr. */
7451 static tree
7452 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7454 enum tree_code code;
7455 tree r;
7457 if (t == NULL_TREE || t == error_mark_node)
7458 return t;
7460 code = TREE_CODE (t);
7462 switch (code)
7464 case PARM_DECL:
7465 r = retrieve_local_specialization (t);
7466 gcc_assert (r != NULL);
7467 mark_used (r);
7468 return r;
7470 case CONST_DECL:
7472 tree enum_type;
7473 tree v;
7475 if (DECL_TEMPLATE_PARM_P (t))
7476 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7477 /* There is no need to substitute into namespace-scope
7478 enumerators. */
7479 if (DECL_NAMESPACE_SCOPE_P (t))
7480 return t;
7481 /* If ARGS is NULL, then T is known to be non-dependent. */
7482 if (args == NULL_TREE)
7483 return decl_constant_value (t);
7485 /* Unfortunately, we cannot just call lookup_name here.
7486 Consider:
7488 template <int I> int f() {
7489 enum E { a = I };
7490 struct S { void g() { E e = a; } };
7493 When we instantiate f<7>::S::g(), say, lookup_name is not
7494 clever enough to find f<7>::a. */
7495 enum_type
7496 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7497 /*entering_scope=*/0);
7499 for (v = TYPE_VALUES (enum_type);
7500 v != NULL_TREE;
7501 v = TREE_CHAIN (v))
7502 if (TREE_PURPOSE (v) == DECL_NAME (t))
7503 return TREE_VALUE (v);
7505 /* We didn't find the name. That should never happen; if
7506 name-lookup found it during preliminary parsing, we
7507 should find it again here during instantiation. */
7508 gcc_unreachable ();
7510 return t;
7512 case FIELD_DECL:
7513 if (DECL_CONTEXT (t))
7515 tree ctx;
7517 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7518 /*entering_scope=*/1);
7519 if (ctx != DECL_CONTEXT (t))
7520 return lookup_field (ctx, DECL_NAME (t), 0, false);
7523 return t;
7525 case VAR_DECL:
7526 case FUNCTION_DECL:
7527 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7528 || local_variable_p (t))
7529 t = tsubst (t, args, complain, in_decl);
7530 mark_used (t);
7531 return t;
7533 case BASELINK:
7534 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7536 case TEMPLATE_DECL:
7537 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7538 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7539 args, complain, in_decl);
7540 else if (is_member_template (t))
7541 return tsubst (t, args, complain, in_decl);
7542 else if (DECL_CLASS_SCOPE_P (t)
7543 && uses_template_parms (DECL_CONTEXT (t)))
7545 /* Template template argument like the following example need
7546 special treatment:
7548 template <template <class> class TT> struct C {};
7549 template <class T> struct D {
7550 template <class U> struct E {};
7551 C<E> c; // #1
7553 D<int> d; // #2
7555 We are processing the template argument `E' in #1 for
7556 the template instantiation #2. Originally, `E' is a
7557 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7558 have to substitute this with one having context `D<int>'. */
7560 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7561 return lookup_field (context, DECL_NAME(t), 0, false);
7563 else
7564 /* Ordinary template template argument. */
7565 return t;
7567 case CAST_EXPR:
7568 case REINTERPRET_CAST_EXPR:
7569 case CONST_CAST_EXPR:
7570 case STATIC_CAST_EXPR:
7571 case DYNAMIC_CAST_EXPR:
7572 case NOP_EXPR:
7573 return build1
7574 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7575 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7577 case INDIRECT_REF:
7578 case NEGATE_EXPR:
7579 case TRUTH_NOT_EXPR:
7580 case BIT_NOT_EXPR:
7581 case ADDR_EXPR:
7582 case CONVERT_EXPR: /* Unary + */
7583 case SIZEOF_EXPR:
7584 case ALIGNOF_EXPR:
7585 case ARROW_EXPR:
7586 case THROW_EXPR:
7587 case TYPEID_EXPR:
7588 case REALPART_EXPR:
7589 case IMAGPART_EXPR:
7590 return build1
7591 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7592 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7594 case COMPONENT_REF:
7596 tree object;
7597 tree name;
7599 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7600 name = TREE_OPERAND (t, 1);
7601 if (TREE_CODE (name) == BIT_NOT_EXPR)
7603 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7604 complain, in_decl);
7605 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7607 else if (TREE_CODE (name) == SCOPE_REF
7608 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7610 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7611 complain, in_decl);
7612 name = TREE_OPERAND (name, 1);
7613 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7614 complain, in_decl);
7615 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7616 name = build_nt (SCOPE_REF, base, name);
7618 else if (TREE_CODE (name) == BASELINK)
7619 name = tsubst_baselink (name,
7620 non_reference (TREE_TYPE (object)),
7621 args, complain,
7622 in_decl);
7623 else
7624 name = tsubst_copy (name, args, complain, in_decl);
7625 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7628 case PLUS_EXPR:
7629 case MINUS_EXPR:
7630 case MULT_EXPR:
7631 case TRUNC_DIV_EXPR:
7632 case CEIL_DIV_EXPR:
7633 case FLOOR_DIV_EXPR:
7634 case ROUND_DIV_EXPR:
7635 case EXACT_DIV_EXPR:
7636 case BIT_AND_EXPR:
7637 case BIT_IOR_EXPR:
7638 case BIT_XOR_EXPR:
7639 case TRUNC_MOD_EXPR:
7640 case FLOOR_MOD_EXPR:
7641 case TRUTH_ANDIF_EXPR:
7642 case TRUTH_ORIF_EXPR:
7643 case TRUTH_AND_EXPR:
7644 case TRUTH_OR_EXPR:
7645 case RSHIFT_EXPR:
7646 case LSHIFT_EXPR:
7647 case RROTATE_EXPR:
7648 case LROTATE_EXPR:
7649 case EQ_EXPR:
7650 case NE_EXPR:
7651 case MAX_EXPR:
7652 case MIN_EXPR:
7653 case LE_EXPR:
7654 case GE_EXPR:
7655 case LT_EXPR:
7656 case GT_EXPR:
7657 case ARRAY_REF:
7658 case COMPOUND_EXPR:
7659 case SCOPE_REF:
7660 case DOTSTAR_EXPR:
7661 case MEMBER_REF:
7662 case PREDECREMENT_EXPR:
7663 case PREINCREMENT_EXPR:
7664 case POSTDECREMENT_EXPR:
7665 case POSTINCREMENT_EXPR:
7666 return build_nt
7667 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7668 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7670 case CALL_EXPR:
7671 return build_nt (code,
7672 tsubst_copy (TREE_OPERAND (t, 0), args,
7673 complain, in_decl),
7674 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7675 in_decl),
7676 NULL_TREE);
7678 case COND_EXPR:
7679 case MODOP_EXPR:
7680 case PSEUDO_DTOR_EXPR:
7682 r = build_nt
7683 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7684 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7685 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7686 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7687 return r;
7690 case NEW_EXPR:
7692 r = build_nt
7693 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7694 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7695 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7696 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7697 return r;
7700 case DELETE_EXPR:
7702 r = build_nt
7703 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7704 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7705 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7706 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7707 return r;
7710 case TEMPLATE_ID_EXPR:
7712 /* Substituted template arguments */
7713 tree fn = TREE_OPERAND (t, 0);
7714 tree targs = TREE_OPERAND (t, 1);
7716 fn = tsubst_copy (fn, args, complain, in_decl);
7717 if (targs)
7718 targs = tsubst_template_args (targs, args, complain, in_decl);
7720 return lookup_template_function (fn, targs);
7723 case TREE_LIST:
7725 tree purpose, value, chain;
7727 if (t == void_list_node)
7728 return t;
7730 purpose = TREE_PURPOSE (t);
7731 if (purpose)
7732 purpose = tsubst_copy (purpose, args, complain, in_decl);
7733 value = TREE_VALUE (t);
7734 if (value)
7735 value = tsubst_copy (value, args, complain, in_decl);
7736 chain = TREE_CHAIN (t);
7737 if (chain && chain != void_type_node)
7738 chain = tsubst_copy (chain, args, complain, in_decl);
7739 if (purpose == TREE_PURPOSE (t)
7740 && value == TREE_VALUE (t)
7741 && chain == TREE_CHAIN (t))
7742 return t;
7743 return tree_cons (purpose, value, chain);
7746 case RECORD_TYPE:
7747 case UNION_TYPE:
7748 case ENUMERAL_TYPE:
7749 case INTEGER_TYPE:
7750 case TEMPLATE_TYPE_PARM:
7751 case TEMPLATE_TEMPLATE_PARM:
7752 case BOUND_TEMPLATE_TEMPLATE_PARM:
7753 case TEMPLATE_PARM_INDEX:
7754 case POINTER_TYPE:
7755 case REFERENCE_TYPE:
7756 case OFFSET_TYPE:
7757 case FUNCTION_TYPE:
7758 case METHOD_TYPE:
7759 case ARRAY_TYPE:
7760 case TYPENAME_TYPE:
7761 case UNBOUND_CLASS_TEMPLATE:
7762 case TYPEOF_TYPE:
7763 case TYPE_DECL:
7764 return tsubst (t, args, complain, in_decl);
7766 case IDENTIFIER_NODE:
7767 if (IDENTIFIER_TYPENAME_P (t))
7769 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7770 return mangle_conv_op_name_for_type (new_type);
7772 else
7773 return t;
7775 case CONSTRUCTOR:
7777 r = build_constructor
7778 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7779 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
7780 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7781 return r;
7784 case VA_ARG_EXPR:
7785 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7786 in_decl),
7787 tsubst (TREE_TYPE (t), args, complain, in_decl));
7789 case CLEANUP_POINT_EXPR:
7790 /* We shouldn't have built any of these during initial template
7791 generation. Instead, they should be built during instantiation
7792 in response to the saved STMT_IS_FULL_EXPR_P setting. */
7793 gcc_unreachable ();
7795 default:
7796 return t;
7800 /* Like tsubst_copy for expressions, etc. but also does semantic
7801 processing. */
7803 static tree
7804 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7806 tree stmt, tmp;
7808 if (t == NULL_TREE || t == error_mark_node)
7809 return t;
7811 if (EXPR_HAS_LOCATION (t))
7812 input_location = EXPR_LOCATION (t);
7813 if (STATEMENT_CODE_P (TREE_CODE (t)))
7814 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
7816 switch (TREE_CODE (t))
7818 case STATEMENT_LIST:
7820 tree_stmt_iterator i;
7821 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7822 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
7823 break;
7826 case CTOR_INITIALIZER:
7827 finish_mem_initializers (tsubst_initializer_list
7828 (TREE_OPERAND (t, 0), args));
7829 break;
7831 case RETURN_EXPR:
7832 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
7833 args, complain, in_decl));
7834 break;
7836 case EXPR_STMT:
7837 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
7838 if (EXPR_STMT_STMT_EXPR_RESULT (t))
7839 finish_stmt_expr_expr (tmp, cur_stmt_expr);
7840 else
7841 finish_expr_stmt (tmp);
7842 break;
7844 case USING_STMT:
7845 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7846 args, complain, in_decl));
7847 break;
7849 case DECL_EXPR:
7851 tree decl;
7852 tree init;
7854 decl = DECL_EXPR_DECL (t);
7855 if (TREE_CODE (decl) == LABEL_DECL)
7856 finish_label_decl (DECL_NAME (decl));
7857 else if (TREE_CODE (decl) == USING_DECL)
7859 tree scope = DECL_INITIAL (decl);
7860 tree name = DECL_NAME (decl);
7861 tree decl;
7863 scope = tsubst_expr (scope, args, complain, in_decl);
7864 decl = lookup_qualified_name (scope, name,
7865 /*is_type_p=*/false,
7866 /*complain=*/false);
7867 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
7868 qualified_name_lookup_error (scope, name, decl);
7869 else
7870 do_local_using_decl (decl, scope, name);
7872 else
7874 init = DECL_INITIAL (decl);
7875 decl = tsubst (decl, args, complain, in_decl);
7876 if (decl != error_mark_node)
7878 if (init)
7879 DECL_INITIAL (decl) = error_mark_node;
7880 /* By marking the declaration as instantiated, we avoid
7881 trying to instantiate it. Since instantiate_decl can't
7882 handle local variables, and since we've already done
7883 all that needs to be done, that's the right thing to
7884 do. */
7885 if (TREE_CODE (decl) == VAR_DECL)
7886 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7887 if (TREE_CODE (decl) == VAR_DECL
7888 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7889 /* Anonymous aggregates are a special case. */
7890 finish_anon_union (decl);
7891 else
7893 maybe_push_decl (decl);
7894 if (TREE_CODE (decl) == VAR_DECL
7895 && DECL_PRETTY_FUNCTION_P (decl))
7897 /* For __PRETTY_FUNCTION__ we have to adjust the
7898 initializer. */
7899 const char *const name
7900 = cxx_printable_name (current_function_decl, 2);
7901 init = cp_fname_init (name, &TREE_TYPE (decl));
7903 else
7904 init = tsubst_expr (init, args, complain, in_decl);
7905 cp_finish_decl (decl, init, NULL_TREE, 0);
7910 /* A DECL_EXPR can also be used as an expression, in the condition
7911 clause of an if/for/while construct. */
7912 return decl;
7915 case FOR_STMT:
7916 stmt = begin_for_stmt ();
7917 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7918 finish_for_init_stmt (stmt);
7919 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
7920 finish_for_cond (tmp, stmt);
7921 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7922 finish_for_expr (tmp, stmt);
7923 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7924 finish_for_stmt (stmt);
7925 break;
7927 case WHILE_STMT:
7928 stmt = begin_while_stmt ();
7929 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
7930 finish_while_stmt_cond (tmp, stmt);
7931 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7932 finish_while_stmt (stmt);
7933 break;
7935 case DO_STMT:
7936 stmt = begin_do_stmt ();
7937 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7938 finish_do_body (stmt);
7939 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
7940 finish_do_stmt (tmp, stmt);
7941 break;
7943 case IF_STMT:
7944 stmt = begin_if_stmt ();
7945 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
7946 finish_if_stmt_cond (tmp, stmt);
7947 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
7948 finish_then_clause (stmt);
7950 if (ELSE_CLAUSE (t))
7952 begin_else_clause (stmt);
7953 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
7954 finish_else_clause (stmt);
7957 finish_if_stmt (stmt);
7958 break;
7960 case BIND_EXPR:
7961 if (BIND_EXPR_BODY_BLOCK (t))
7962 stmt = begin_function_body ();
7963 else
7964 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
7965 ? BCS_TRY_BLOCK : 0);
7967 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
7969 if (BIND_EXPR_BODY_BLOCK (t))
7970 finish_function_body (stmt);
7971 else
7972 finish_compound_stmt (stmt);
7973 break;
7975 case BREAK_STMT:
7976 finish_break_stmt ();
7977 break;
7979 case CONTINUE_STMT:
7980 finish_continue_stmt ();
7981 break;
7983 case SWITCH_STMT:
7984 stmt = begin_switch_stmt ();
7985 tmp = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7986 finish_switch_cond (tmp, stmt);
7987 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7988 finish_switch_stmt (stmt);
7989 break;
7991 case CASE_LABEL_EXPR:
7992 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7993 tsubst_expr (CASE_HIGH (t), args, complain,
7994 in_decl));
7995 break;
7997 case LABEL_EXPR:
7998 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
7999 break;
8001 case GOTO_EXPR:
8002 tmp = GOTO_DESTINATION (t);
8003 if (TREE_CODE (tmp) != LABEL_DECL)
8004 /* Computed goto's must be tsubst'd into. On the other hand,
8005 non-computed gotos must not be; the identifier in question
8006 will have no binding. */
8007 tmp = tsubst_expr (tmp, args, complain, in_decl);
8008 else
8009 tmp = DECL_NAME (tmp);
8010 finish_goto_stmt (tmp);
8011 break;
8013 case ASM_EXPR:
8014 tmp = finish_asm_stmt
8015 (ASM_VOLATILE_P (t),
8016 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8017 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
8018 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
8019 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
8020 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
8021 break;
8023 case TRY_BLOCK:
8024 if (CLEANUP_P (t))
8026 stmt = begin_try_block ();
8027 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8028 finish_cleanup_try_block (stmt);
8029 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8030 complain, in_decl),
8031 stmt);
8033 else
8035 if (FN_TRY_BLOCK_P (t))
8036 stmt = begin_function_try_block ();
8037 else
8038 stmt = begin_try_block ();
8040 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8042 if (FN_TRY_BLOCK_P (t))
8043 finish_function_try_block (stmt);
8044 else
8045 finish_try_block (stmt);
8047 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8048 if (FN_TRY_BLOCK_P (t))
8049 finish_function_handler_sequence (stmt);
8050 else
8051 finish_handler_sequence (stmt);
8053 break;
8055 case HANDLER:
8057 tree decl;
8059 stmt = begin_handler ();
8060 if (HANDLER_PARMS (t))
8062 decl = HANDLER_PARMS (t);
8063 decl = tsubst (decl, args, complain, in_decl);
8064 /* Prevent instantiate_decl from trying to instantiate
8065 this variable. We've already done all that needs to be
8066 done. */
8067 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8069 else
8070 decl = NULL_TREE;
8071 finish_handler_parms (decl, stmt);
8072 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8073 finish_handler (stmt);
8075 break;
8077 case TAG_DEFN:
8078 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8079 break;
8081 default:
8082 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8084 return tsubst_copy_and_build (t, args, complain, in_decl,
8085 /*function_p=*/false);
8088 return NULL_TREE;
8091 /* T is a postfix-expression that is not being used in a function
8092 call. Return the substituted version of T. */
8094 static tree
8095 tsubst_non_call_postfix_expression (tree t, tree args,
8096 tsubst_flags_t complain,
8097 tree in_decl)
8099 if (TREE_CODE (t) == SCOPE_REF)
8100 t = tsubst_qualified_id (t, args, complain, in_decl,
8101 /*done=*/false, /*address_p=*/false);
8102 else
8103 t = tsubst_copy_and_build (t, args, complain, in_decl,
8104 /*function_p=*/false);
8106 return t;
8109 /* Like tsubst but deals with expressions and performs semantic
8110 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8112 tree
8113 tsubst_copy_and_build (tree t,
8114 tree args,
8115 tsubst_flags_t complain,
8116 tree in_decl,
8117 bool function_p)
8119 #define RECUR(NODE) \
8120 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8122 tree op1;
8124 if (t == NULL_TREE || t == error_mark_node)
8125 return t;
8127 switch (TREE_CODE (t))
8129 case USING_DECL:
8130 t = DECL_NAME (t);
8131 /* Fall through. */
8132 case IDENTIFIER_NODE:
8134 tree decl;
8135 cp_id_kind idk;
8136 tree qualifying_class;
8137 bool non_integral_constant_expression_p;
8138 const char *error_msg;
8140 if (IDENTIFIER_TYPENAME_P (t))
8142 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8143 t = mangle_conv_op_name_for_type (new_type);
8146 /* Look up the name. */
8147 decl = lookup_name (t, 0);
8149 /* By convention, expressions use ERROR_MARK_NODE to indicate
8150 failure, not NULL_TREE. */
8151 if (decl == NULL_TREE)
8152 decl = error_mark_node;
8154 decl = finish_id_expression (t, decl, NULL_TREE,
8155 &idk,
8156 &qualifying_class,
8157 /*integral_constant_expression_p=*/false,
8158 /*allow_non_integral_constant_expression_p=*/false,
8159 &non_integral_constant_expression_p,
8160 &error_msg);
8161 if (error_msg)
8162 error (error_msg);
8163 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8164 decl = unqualified_name_lookup_error (decl);
8165 return decl;
8168 case TEMPLATE_ID_EXPR:
8170 tree object;
8171 tree template = RECUR (TREE_OPERAND (t, 0));
8172 tree targs = TREE_OPERAND (t, 1);
8174 if (targs)
8175 targs = tsubst_template_args (targs, args, complain, in_decl);
8177 if (TREE_CODE (template) == COMPONENT_REF)
8179 object = TREE_OPERAND (template, 0);
8180 template = TREE_OPERAND (template, 1);
8182 else
8183 object = NULL_TREE;
8184 template = lookup_template_function (template, targs);
8186 if (object)
8187 return build3 (COMPONENT_REF, TREE_TYPE (template),
8188 object, template, NULL_TREE);
8189 else
8190 return template;
8193 case INDIRECT_REF:
8194 return build_x_indirect_ref (RECUR (TREE_OPERAND (t, 0)), "unary *");
8196 case NOP_EXPR:
8197 return build_nop
8198 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8199 RECUR (TREE_OPERAND (t, 0)));
8201 case CAST_EXPR:
8202 return build_functional_cast
8203 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8204 RECUR (TREE_OPERAND (t, 0)));
8206 case REINTERPRET_CAST_EXPR:
8207 return build_reinterpret_cast
8208 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8209 RECUR (TREE_OPERAND (t, 0)));
8211 case CONST_CAST_EXPR:
8212 return build_const_cast
8213 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8214 RECUR (TREE_OPERAND (t, 0)));
8216 case DYNAMIC_CAST_EXPR:
8217 return build_dynamic_cast
8218 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8219 RECUR (TREE_OPERAND (t, 0)));
8221 case STATIC_CAST_EXPR:
8222 return build_static_cast
8223 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8224 RECUR (TREE_OPERAND (t, 0)));
8226 case POSTDECREMENT_EXPR:
8227 case POSTINCREMENT_EXPR:
8228 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8229 args, complain, in_decl);
8230 return build_x_unary_op (TREE_CODE (t), op1);
8232 case PREDECREMENT_EXPR:
8233 case PREINCREMENT_EXPR:
8234 case NEGATE_EXPR:
8235 case BIT_NOT_EXPR:
8236 case ABS_EXPR:
8237 case TRUTH_NOT_EXPR:
8238 case CONVERT_EXPR: /* Unary + */
8239 case REALPART_EXPR:
8240 case IMAGPART_EXPR:
8241 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8243 case ADDR_EXPR:
8244 op1 = TREE_OPERAND (t, 0);
8245 if (TREE_CODE (op1) == SCOPE_REF)
8246 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8247 /*done=*/true, /*address_p=*/true);
8248 else
8249 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8250 in_decl);
8251 if (TREE_CODE (op1) == LABEL_DECL)
8252 return finish_label_address_expr (DECL_NAME (op1));
8253 return build_x_unary_op (ADDR_EXPR, op1);
8255 case PLUS_EXPR:
8256 case MINUS_EXPR:
8257 case MULT_EXPR:
8258 case TRUNC_DIV_EXPR:
8259 case CEIL_DIV_EXPR:
8260 case FLOOR_DIV_EXPR:
8261 case ROUND_DIV_EXPR:
8262 case EXACT_DIV_EXPR:
8263 case BIT_AND_EXPR:
8264 case BIT_IOR_EXPR:
8265 case BIT_XOR_EXPR:
8266 case TRUNC_MOD_EXPR:
8267 case FLOOR_MOD_EXPR:
8268 case TRUTH_ANDIF_EXPR:
8269 case TRUTH_ORIF_EXPR:
8270 case TRUTH_AND_EXPR:
8271 case TRUTH_OR_EXPR:
8272 case RSHIFT_EXPR:
8273 case LSHIFT_EXPR:
8274 case RROTATE_EXPR:
8275 case LROTATE_EXPR:
8276 case EQ_EXPR:
8277 case NE_EXPR:
8278 case MAX_EXPR:
8279 case MIN_EXPR:
8280 case LE_EXPR:
8281 case GE_EXPR:
8282 case LT_EXPR:
8283 case GT_EXPR:
8284 case MEMBER_REF:
8285 case DOTSTAR_EXPR:
8286 return build_x_binary_op
8287 (TREE_CODE (t),
8288 RECUR (TREE_OPERAND (t, 0)),
8289 RECUR (TREE_OPERAND (t, 1)),
8290 /*overloaded_p=*/NULL);
8292 case SCOPE_REF:
8293 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8294 /*address_p=*/false);
8296 case ARRAY_REF:
8297 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8298 == NULL_TREE)
8299 /* new-type-id */
8300 return build_nt (ARRAY_REF, NULL_TREE, RECUR (TREE_OPERAND (t, 1)),
8301 NULL_TREE, NULL_TREE);
8303 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8304 args, complain, in_decl);
8305 /* Remember that there was a reference to this entity. */
8306 if (DECL_P (op1))
8307 mark_used (op1);
8308 return grok_array_decl (op1, RECUR (TREE_OPERAND (t, 1)));
8310 case SIZEOF_EXPR:
8311 case ALIGNOF_EXPR:
8312 op1 = TREE_OPERAND (t, 0);
8313 if (!args)
8315 /* When there are no ARGS, we are trying to evaluate a
8316 non-dependent expression from the parser. Trying to do
8317 the substitutions may not work. */
8318 if (!TYPE_P (op1))
8319 op1 = TREE_TYPE (op1);
8321 else
8323 ++skip_evaluation;
8324 op1 = RECUR (op1);
8325 --skip_evaluation;
8327 if (TYPE_P (op1))
8328 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8329 else
8330 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8332 case MODOP_EXPR:
8334 tree r = build_x_modify_expr
8335 (RECUR (TREE_OPERAND (t, 0)),
8336 TREE_CODE (TREE_OPERAND (t, 1)),
8337 RECUR (TREE_OPERAND (t, 2)));
8338 /* TREE_NO_WARNING must be set if either the expression was
8339 parenthesized or it uses an operator such as >>= rather
8340 than plain assignment. In the former case, it was already
8341 set and must be copied. In the latter case,
8342 build_x_modify_expr sets it and it must not be reset
8343 here. */
8344 if (TREE_NO_WARNING (t))
8345 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8346 return r;
8349 case ARROW_EXPR:
8350 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8351 args, complain, in_decl);
8352 /* Remember that there was a reference to this entity. */
8353 if (DECL_P (op1))
8354 mark_used (op1);
8355 return build_x_arrow (op1);
8357 case NEW_EXPR:
8358 return build_new
8359 (RECUR (TREE_OPERAND (t, 0)),
8360 RECUR (TREE_OPERAND (t, 1)),
8361 RECUR (TREE_OPERAND (t, 2)),
8362 RECUR (TREE_OPERAND (t, 3)),
8363 NEW_EXPR_USE_GLOBAL (t));
8365 case DELETE_EXPR:
8366 return delete_sanity
8367 (RECUR (TREE_OPERAND (t, 0)),
8368 RECUR (TREE_OPERAND (t, 1)),
8369 DELETE_EXPR_USE_VEC (t),
8370 DELETE_EXPR_USE_GLOBAL (t));
8372 case COMPOUND_EXPR:
8373 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8374 RECUR (TREE_OPERAND (t, 1)));
8376 case CALL_EXPR:
8378 tree function;
8379 tree call_args;
8380 bool qualified_p;
8381 bool koenig_p;
8383 function = TREE_OPERAND (t, 0);
8384 /* When we parsed the expression, we determined whether or
8385 not Koenig lookup should be performed. */
8386 koenig_p = KOENIG_LOOKUP_P (t);
8387 if (TREE_CODE (function) == SCOPE_REF)
8389 qualified_p = true;
8390 function = tsubst_qualified_id (function, args, complain, in_decl,
8391 /*done=*/false,
8392 /*address_p=*/false);
8394 else
8396 qualified_p = (TREE_CODE (function) == COMPONENT_REF
8397 && (TREE_CODE (TREE_OPERAND (function, 1))
8398 == SCOPE_REF));
8399 function = tsubst_copy_and_build (function, args, complain,
8400 in_decl,
8401 !qualified_p);
8402 if (BASELINK_P (function))
8403 qualified_p = true;
8406 call_args = RECUR (TREE_OPERAND (t, 1));
8408 /* We do not perform argument-dependent lookup if normal
8409 lookup finds a non-function, in accordance with the
8410 expected resolution of DR 218. */
8411 if (koenig_p
8412 && (is_overloaded_fn (function)
8413 || TREE_CODE (function) == IDENTIFIER_NODE))
8414 function = perform_koenig_lookup (function, call_args);
8416 if (TREE_CODE (function) == IDENTIFIER_NODE)
8418 unqualified_name_lookup_error (function);
8419 return error_mark_node;
8422 /* Remember that there was a reference to this entity. */
8423 if (DECL_P (function))
8424 mark_used (function);
8426 function = convert_from_reference (function);
8428 if (TREE_CODE (function) == OFFSET_REF)
8429 return build_offset_ref_call_from_tree (function, call_args);
8430 if (TREE_CODE (function) == COMPONENT_REF)
8432 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8433 return finish_call_expr (function, call_args,
8434 /*disallow_virtual=*/false,
8435 /*koenig_p=*/false);
8436 else
8437 return (build_new_method_call
8438 (TREE_OPERAND (function, 0),
8439 TREE_OPERAND (function, 1),
8440 call_args, NULL_TREE,
8441 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8443 return finish_call_expr (function, call_args,
8444 /*disallow_virtual=*/qualified_p,
8445 koenig_p);
8448 case COND_EXPR:
8449 return build_x_conditional_expr
8450 (RECUR (TREE_OPERAND (t, 0)),
8451 RECUR (TREE_OPERAND (t, 1)),
8452 RECUR (TREE_OPERAND (t, 2)));
8454 case PSEUDO_DTOR_EXPR:
8455 return finish_pseudo_destructor_expr
8456 (RECUR (TREE_OPERAND (t, 0)),
8457 RECUR (TREE_OPERAND (t, 1)),
8458 RECUR (TREE_OPERAND (t, 2)));
8460 case TREE_LIST:
8462 tree purpose, value, chain;
8464 if (t == void_list_node)
8465 return t;
8467 purpose = TREE_PURPOSE (t);
8468 if (purpose)
8469 purpose = RECUR (purpose);
8470 value = TREE_VALUE (t);
8471 if (value)
8472 value = RECUR (value);
8473 chain = TREE_CHAIN (t);
8474 if (chain && chain != void_type_node)
8475 chain = RECUR (chain);
8476 if (purpose == TREE_PURPOSE (t)
8477 && value == TREE_VALUE (t)
8478 && chain == TREE_CHAIN (t))
8479 return t;
8480 return tree_cons (purpose, value, chain);
8483 case COMPONENT_REF:
8485 tree object;
8486 tree member;
8488 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8489 args, complain, in_decl);
8490 /* Remember that there was a reference to this entity. */
8491 if (DECL_P (object))
8492 mark_used (object);
8494 member = TREE_OPERAND (t, 1);
8495 if (BASELINK_P (member))
8496 member = tsubst_baselink (member,
8497 non_reference (TREE_TYPE (object)),
8498 args, complain, in_decl);
8499 else
8500 member = tsubst_copy (member, args, complain, in_decl);
8502 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8504 if (TREE_CODE (member) == BIT_NOT_EXPR)
8505 return finish_pseudo_destructor_expr (object,
8506 NULL_TREE,
8507 TREE_TYPE (object));
8508 else if (TREE_CODE (member) == SCOPE_REF
8509 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8510 return finish_pseudo_destructor_expr (object,
8511 object,
8512 TREE_TYPE (object));
8514 else if (TREE_CODE (member) == SCOPE_REF
8515 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8517 tree tmpl;
8518 tree args;
8520 /* Lookup the template functions now that we know what the
8521 scope is. */
8522 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8523 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8524 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8525 /*is_type_p=*/false,
8526 /*complain=*/false);
8527 if (BASELINK_P (member))
8528 BASELINK_FUNCTIONS (member)
8529 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8530 args);
8531 else
8533 qualified_name_lookup_error (TREE_TYPE (object), tmpl,
8534 member);
8535 return error_mark_node;
8538 else if (TREE_CODE (member) == SCOPE_REF
8539 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
8540 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
8542 if (complain & tf_error)
8544 if (TYPE_P (TREE_OPERAND (member, 0)))
8545 error ("%qT is not a class or namespace",
8546 TREE_OPERAND (member, 0));
8547 else
8548 error ("%qD is not a class or namespace",
8549 TREE_OPERAND (member, 0));
8551 return error_mark_node;
8553 else if (TREE_CODE (member) == FIELD_DECL)
8554 return finish_non_static_data_member (member, object, NULL_TREE);
8556 return finish_class_member_access_expr (object, member);
8559 case THROW_EXPR:
8560 return build_throw
8561 (RECUR (TREE_OPERAND (t, 0)));
8563 case CONSTRUCTOR:
8565 tree r;
8566 tree elts;
8567 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8568 bool purpose_p;
8570 /* digest_init will do the wrong thing if we let it. */
8571 if (type && TYPE_PTRMEMFUNC_P (type))
8572 return t;
8574 r = NULL_TREE;
8575 /* We do not want to process the purpose of aggregate
8576 initializers as they are identifier nodes which will be
8577 looked up by digest_init. */
8578 purpose_p = !(type && IS_AGGR_TYPE (type));
8579 for (elts = CONSTRUCTOR_ELTS (t);
8580 elts;
8581 elts = TREE_CHAIN (elts))
8583 tree purpose = TREE_PURPOSE (elts);
8584 tree value = TREE_VALUE (elts);
8586 if (purpose && purpose_p)
8587 purpose = RECUR (purpose);
8588 value = RECUR (value);
8589 r = tree_cons (purpose, value, r);
8592 r = build_constructor (NULL_TREE, nreverse (r));
8593 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8595 if (type)
8596 return digest_init (type, r, 0);
8597 return r;
8600 case TYPEID_EXPR:
8602 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
8603 if (TYPE_P (operand_0))
8604 return get_typeid (operand_0);
8605 return build_typeid (operand_0);
8608 case PARM_DECL:
8609 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8611 case VAR_DECL:
8612 if (args)
8613 t = tsubst_copy (t, args, complain, in_decl);
8614 return convert_from_reference (t);
8616 case VA_ARG_EXPR:
8617 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
8618 tsubst_copy (TREE_TYPE (t), args, complain,
8619 in_decl));
8621 case OFFSETOF_EXPR:
8622 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
8624 case STMT_EXPR:
8626 tree old_stmt_expr = cur_stmt_expr;
8627 tree stmt_expr = begin_stmt_expr ();
8629 cur_stmt_expr = stmt_expr;
8630 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
8631 stmt_expr = finish_stmt_expr (stmt_expr, false);
8632 cur_stmt_expr = old_stmt_expr;
8634 return stmt_expr;
8637 default:
8638 return tsubst_copy (t, args, complain, in_decl);
8641 #undef RECUR
8644 /* Verify that the instantiated ARGS are valid. For type arguments,
8645 make sure that the type's linkage is ok. For non-type arguments,
8646 make sure they are constants if they are integral or enumerations.
8647 Emit an error under control of COMPLAIN, and return TRUE on error. */
8649 static bool
8650 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
8652 int ix, len = DECL_NTPARMS (tmpl);
8653 bool result = false;
8655 for (ix = 0; ix != len; ix++)
8657 tree t = TREE_VEC_ELT (args, ix);
8659 if (TYPE_P (t))
8661 /* [basic.link]: A name with no linkage (notably, the name
8662 of a class or enumeration declared in a local scope)
8663 shall not be used to declare an entity with linkage.
8664 This implies that names with no linkage cannot be used as
8665 template arguments. */
8666 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
8668 if (nt)
8670 if (!(complain & tf_error))
8671 /*OK*/;
8672 else if (TYPE_ANONYMOUS_P (nt))
8673 error ("%qT uses anonymous type", t);
8674 else
8675 error ("%qT uses local type %qT", t, nt);
8676 result = true;
8678 /* In order to avoid all sorts of complications, we do not
8679 allow variably-modified types as template arguments. */
8680 else if (variably_modified_type_p (t, NULL_TREE))
8682 if (complain & tf_error)
8683 error ("%qT is a variably modified type", t);
8684 result = true;
8687 /* A non-type argument of integral or enumerated type must be a
8688 constant. */
8689 else if (TREE_TYPE (t)
8690 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
8691 && !TREE_CONSTANT (t))
8693 if (complain & tf_error)
8694 error ("integral expression %qE is not constant", t);
8695 result = true;
8698 if (result && complain & tf_error)
8699 error (" trying to instantiate %qD", tmpl);
8700 return result;
8703 /* Instantiate the indicated variable or function template TMPL with
8704 the template arguments in TARG_PTR. */
8706 tree
8707 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8709 tree fndecl;
8710 tree gen_tmpl;
8711 tree spec;
8713 if (tmpl == error_mark_node)
8714 return error_mark_node;
8716 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
8718 /* If this function is a clone, handle it specially. */
8719 if (DECL_CLONED_FUNCTION_P (tmpl))
8721 tree spec;
8722 tree clone;
8724 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
8725 complain);
8726 if (spec == error_mark_node)
8727 return error_mark_node;
8729 /* Look for the clone. */
8730 FOR_EACH_CLONE (clone, spec)
8731 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8732 return clone;
8733 /* We should always have found the clone by now. */
8734 gcc_unreachable ();
8735 return NULL_TREE;
8738 /* Check to see if we already have this specialization. */
8739 spec = retrieve_specialization (tmpl, targ_ptr);
8740 if (spec != NULL_TREE)
8741 return spec;
8743 gen_tmpl = most_general_template (tmpl);
8744 if (tmpl != gen_tmpl)
8746 /* The TMPL is a partial instantiation. To get a full set of
8747 arguments we must add the arguments used to perform the
8748 partial instantiation. */
8749 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8750 targ_ptr);
8752 /* Check to see if we already have this specialization. */
8753 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8754 if (spec != NULL_TREE)
8755 return spec;
8758 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
8759 complain))
8760 return error_mark_node;
8762 /* We are building a FUNCTION_DECL, during which the access of its
8763 parameters and return types have to be checked. However this
8764 FUNCTION_DECL which is the desired context for access checking
8765 is not built yet. We solve this chicken-and-egg problem by
8766 deferring all checks until we have the FUNCTION_DECL. */
8767 push_deferring_access_checks (dk_deferred);
8769 /* Substitute template parameters. */
8770 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8771 targ_ptr, complain, gen_tmpl);
8773 /* Now we know the specialization, compute access previously
8774 deferred. */
8775 push_access_scope (fndecl);
8776 perform_deferred_access_checks ();
8777 pop_access_scope (fndecl);
8778 pop_deferring_access_checks ();
8780 /* The DECL_TI_TEMPLATE should always be the immediate parent
8781 template, not the most general template. */
8782 DECL_TI_TEMPLATE (fndecl) = tmpl;
8784 /* If we've just instantiated the main entry point for a function,
8785 instantiate all the alternate entry points as well. We do this
8786 by cloning the instantiation of the main entry point, not by
8787 instantiating the template clones. */
8788 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8789 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8791 return fndecl;
8794 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8795 arguments that are being used when calling it. TARGS is a vector
8796 into which the deduced template arguments are placed.
8798 Return zero for success, 2 for an incomplete match that doesn't resolve
8799 all the types, and 1 for complete failure. An error message will be
8800 printed only for an incomplete match.
8802 If FN is a conversion operator, or we are trying to produce a specific
8803 specialization, RETURN_TYPE is the return type desired.
8805 The EXPLICIT_TARGS are explicit template arguments provided via a
8806 template-id.
8808 The parameter STRICT is one of:
8810 DEDUCE_CALL:
8811 We are deducing arguments for a function call, as in
8812 [temp.deduct.call].
8814 DEDUCE_CONV:
8815 We are deducing arguments for a conversion function, as in
8816 [temp.deduct.conv].
8818 DEDUCE_EXACT:
8819 We are deducing arguments when doing an explicit instantiation
8820 as in [temp.explicit], when determining an explicit specialization
8821 as in [temp.expl.spec], or when taking the address of a function
8822 template, as in [temp.deduct.funcaddr].
8824 DEDUCE_ORDER:
8825 We are deducing arguments when calculating the partial
8826 ordering between specializations of function or class
8827 templates, as in [temp.func.order] and [temp.class.order].
8829 LEN is the number of parms to consider before returning success, or -1
8830 for all. This is used in partial ordering to avoid comparing parms for
8831 which no actual argument was passed, since they are not considered in
8832 overload resolution (and are explicitly excluded from consideration in
8833 partial ordering in [temp.func.order]/6). */
8836 fn_type_unification (tree fn,
8837 tree explicit_targs,
8838 tree targs,
8839 tree args,
8840 tree return_type,
8841 unification_kind_t strict,
8842 int len)
8844 tree parms;
8845 tree fntype;
8846 int result;
8848 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
8850 fntype = TREE_TYPE (fn);
8851 if (explicit_targs)
8853 /* [temp.deduct]
8855 The specified template arguments must match the template
8856 parameters in kind (i.e., type, nontype, template), and there
8857 must not be more arguments than there are parameters;
8858 otherwise type deduction fails.
8860 Nontype arguments must match the types of the corresponding
8861 nontype template parameters, or must be convertible to the
8862 types of the corresponding nontype parameters as specified in
8863 _temp.arg.nontype_, otherwise type deduction fails.
8865 All references in the function type of the function template
8866 to the corresponding template parameters are replaced by the
8867 specified template argument values. If a substitution in a
8868 template parameter or in the function type of the function
8869 template results in an invalid type, type deduction fails. */
8870 int i;
8871 tree converted_args;
8872 bool incomplete;
8874 converted_args
8875 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8876 explicit_targs, NULL_TREE, tf_none,
8877 /*require_all_arguments=*/0));
8878 if (converted_args == error_mark_node)
8879 return 1;
8881 /* Substitute the explicit args into the function type. This is
8882 necessary so that, for instance, explicitly declared function
8883 arguments can match null pointed constants. If we were given
8884 an incomplete set of explicit args, we must not do semantic
8885 processing during substitution as we could create partial
8886 instantiations. */
8887 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
8888 processing_template_decl += incomplete;
8889 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8890 processing_template_decl -= incomplete;
8892 if (fntype == error_mark_node)
8893 return 1;
8895 /* Place the explicitly specified arguments in TARGS. */
8896 for (i = NUM_TMPL_ARGS (converted_args); i--;)
8897 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8900 parms = TYPE_ARG_TYPES (fntype);
8901 /* Never do unification on the 'this' parameter. */
8902 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8903 parms = TREE_CHAIN (parms);
8905 if (return_type)
8907 /* We've been given a return type to match, prepend it. */
8908 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8909 args = tree_cons (NULL_TREE, return_type, args);
8910 if (len >= 0)
8911 ++len;
8914 /* We allow incomplete unification without an error message here
8915 because the standard doesn't seem to explicitly prohibit it. Our
8916 callers must be ready to deal with unification failures in any
8917 event. */
8918 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8919 targs, parms, args, /*subr=*/0,
8920 strict, /*allow_incomplete*/1, len);
8922 if (result == 0)
8923 /* All is well so far. Now, check:
8925 [temp.deduct]
8927 When all template arguments have been deduced, all uses of
8928 template parameters in nondeduced contexts are replaced with
8929 the corresponding deduced argument values. If the
8930 substitution results in an invalid type, as described above,
8931 type deduction fails. */
8932 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8933 == error_mark_node)
8934 return 1;
8936 return result;
8939 /* Adjust types before performing type deduction, as described in
8940 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8941 sections are symmetric. PARM is the type of a function parameter
8942 or the return type of the conversion function. ARG is the type of
8943 the argument passed to the call, or the type of the value
8944 initialized with the result of the conversion function. */
8946 static int
8947 maybe_adjust_types_for_deduction (unification_kind_t strict,
8948 tree* parm,
8949 tree* arg)
8951 int result = 0;
8953 switch (strict)
8955 case DEDUCE_CALL:
8956 break;
8958 case DEDUCE_CONV:
8960 /* Swap PARM and ARG throughout the remainder of this
8961 function; the handling is precisely symmetric since PARM
8962 will initialize ARG rather than vice versa. */
8963 tree* temp = parm;
8964 parm = arg;
8965 arg = temp;
8966 break;
8969 case DEDUCE_EXACT:
8970 /* There is nothing to do in this case. */
8971 return 0;
8973 case DEDUCE_ORDER:
8974 /* DR 214. [temp.func.order] is underspecified, and leads to no
8975 ordering between things like `T *' and `T const &' for `U *'.
8976 The former has T=U and the latter T=U*. The former looks more
8977 specialized and John Spicer considers it well-formed (the EDG
8978 compiler accepts it).
8980 John also confirms that deduction should proceed as in a function
8981 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8982 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8983 to an actual call can have such a type.
8985 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8986 If only ARG is a REFERENCE_TYPE, we look through that and then
8987 proceed as with DEDUCE_CALL (which could further convert it). */
8988 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8990 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8991 return 0;
8992 *arg = TREE_TYPE (*arg);
8994 break;
8995 default:
8996 gcc_unreachable ();
8999 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9001 /* [temp.deduct.call]
9003 If P is not a reference type:
9005 --If A is an array type, the pointer type produced by the
9006 array-to-pointer standard conversion (_conv.array_) is
9007 used in place of A for type deduction; otherwise,
9009 --If A is a function type, the pointer type produced by
9010 the function-to-pointer standard conversion
9011 (_conv.func_) is used in place of A for type deduction;
9012 otherwise,
9014 --If A is a cv-qualified type, the top level
9015 cv-qualifiers of A's type are ignored for type
9016 deduction. */
9017 if (TREE_CODE (*arg) == ARRAY_TYPE)
9018 *arg = build_pointer_type (TREE_TYPE (*arg));
9019 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9020 *arg = build_pointer_type (*arg);
9021 else
9022 *arg = TYPE_MAIN_VARIANT (*arg);
9025 /* [temp.deduct.call]
9027 If P is a cv-qualified type, the top level cv-qualifiers
9028 of P's type are ignored for type deduction. If P is a
9029 reference type, the type referred to by P is used for
9030 type deduction. */
9031 *parm = TYPE_MAIN_VARIANT (*parm);
9032 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9034 *parm = TREE_TYPE (*parm);
9035 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9038 /* DR 322. For conversion deduction, remove a reference type on parm
9039 too (which has been swapped into ARG). */
9040 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9041 *arg = TREE_TYPE (*arg);
9043 return result;
9046 /* Most parms like fn_type_unification.
9048 If SUBR is 1, we're being called recursively (to unify the
9049 arguments of a function or method parameter of a function
9050 template). */
9052 static int
9053 type_unification_real (tree tparms,
9054 tree targs,
9055 tree xparms,
9056 tree xargs,
9057 int subr,
9058 unification_kind_t strict,
9059 int allow_incomplete,
9060 int xlen)
9062 tree parm, arg;
9063 int i;
9064 int ntparms = TREE_VEC_LENGTH (tparms);
9065 int sub_strict;
9066 int saw_undeduced = 0;
9067 tree parms, args;
9068 int len;
9070 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9071 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9072 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9073 gcc_assert (ntparms > 0);
9075 switch (strict)
9077 case DEDUCE_CALL:
9078 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9079 | UNIFY_ALLOW_DERIVED);
9080 break;
9082 case DEDUCE_CONV:
9083 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9084 break;
9086 case DEDUCE_EXACT:
9087 sub_strict = UNIFY_ALLOW_NONE;
9088 break;
9090 case DEDUCE_ORDER:
9091 sub_strict = UNIFY_ALLOW_NONE;
9092 break;
9094 default:
9095 gcc_unreachable ();
9098 if (xlen == 0)
9099 return 0;
9101 again:
9102 parms = xparms;
9103 args = xargs;
9104 len = xlen;
9106 while (parms
9107 && parms != void_list_node
9108 && args
9109 && args != void_list_node)
9111 parm = TREE_VALUE (parms);
9112 parms = TREE_CHAIN (parms);
9113 arg = TREE_VALUE (args);
9114 args = TREE_CHAIN (args);
9116 if (arg == error_mark_node)
9117 return 1;
9118 if (arg == unknown_type_node)
9119 /* We can't deduce anything from this, but we might get all the
9120 template args from other function args. */
9121 continue;
9123 /* Conversions will be performed on a function argument that
9124 corresponds with a function parameter that contains only
9125 non-deducible template parameters and explicitly specified
9126 template parameters. */
9127 if (!uses_template_parms (parm))
9129 tree type;
9131 if (!TYPE_P (arg))
9132 type = TREE_TYPE (arg);
9133 else
9134 type = arg;
9136 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
9138 if (same_type_p (parm, type))
9139 continue;
9141 else
9142 /* It might work; we shouldn't check now, because we might
9143 get into infinite recursion. Overload resolution will
9144 handle it. */
9145 continue;
9147 return 1;
9150 if (!TYPE_P (arg))
9152 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9153 if (type_unknown_p (arg))
9155 /* [temp.deduct.type] A template-argument can be deduced from
9156 a pointer to function or pointer to member function
9157 argument if the set of overloaded functions does not
9158 contain function templates and at most one of a set of
9159 overloaded functions provides a unique match. */
9161 if (resolve_overloaded_unification
9162 (tparms, targs, parm, arg, strict, sub_strict)
9163 != 0)
9164 return 1;
9165 continue;
9167 arg = TREE_TYPE (arg);
9168 if (arg == error_mark_node)
9169 return 1;
9173 int arg_strict = sub_strict;
9175 if (!subr)
9176 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9178 if (unify (tparms, targs, parm, arg, arg_strict))
9179 return 1;
9182 /* Are we done with the interesting parms? */
9183 if (--len == 0)
9184 goto done;
9186 /* Fail if we've reached the end of the parm list, and more args
9187 are present, and the parm list isn't variadic. */
9188 if (args && args != void_list_node && parms == void_list_node)
9189 return 1;
9190 /* Fail if parms are left and they don't have default values. */
9191 if (parms
9192 && parms != void_list_node
9193 && TREE_PURPOSE (parms) == NULL_TREE)
9194 return 1;
9196 done:
9197 if (!subr)
9198 for (i = 0; i < ntparms; i++)
9199 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
9201 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9203 /* If this is an undeduced nontype parameter that depends on
9204 a type parameter, try another pass; its type may have been
9205 deduced from a later argument than the one from which
9206 this parameter can be deduced. */
9207 if (TREE_CODE (tparm) == PARM_DECL
9208 && uses_template_parms (TREE_TYPE (tparm))
9209 && !saw_undeduced++)
9210 goto again;
9212 if (!allow_incomplete)
9213 error ("incomplete type unification");
9214 return 2;
9216 return 0;
9219 /* Subroutine of type_unification_real. Args are like the variables at the
9220 call site. ARG is an overloaded function (or template-id); we try
9221 deducing template args from each of the overloads, and if only one
9222 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9224 static int
9225 resolve_overloaded_unification (tree tparms,
9226 tree targs,
9227 tree parm,
9228 tree arg,
9229 unification_kind_t strict,
9230 int sub_strict)
9232 tree tempargs = copy_node (targs);
9233 int good = 0;
9234 bool addr_p;
9236 if (TREE_CODE (arg) == ADDR_EXPR)
9238 arg = TREE_OPERAND (arg, 0);
9239 addr_p = true;
9241 else
9242 addr_p = false;
9244 if (TREE_CODE (arg) == COMPONENT_REF)
9245 /* Handle `&x' where `x' is some static or non-static member
9246 function name. */
9247 arg = TREE_OPERAND (arg, 1);
9249 if (TREE_CODE (arg) == OFFSET_REF)
9250 arg = TREE_OPERAND (arg, 1);
9252 /* Strip baselink information. */
9253 if (BASELINK_P (arg))
9254 arg = BASELINK_FUNCTIONS (arg);
9256 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9258 /* If we got some explicit template args, we need to plug them into
9259 the affected templates before we try to unify, in case the
9260 explicit args will completely resolve the templates in question. */
9262 tree expl_subargs = TREE_OPERAND (arg, 1);
9263 arg = TREE_OPERAND (arg, 0);
9265 for (; arg; arg = OVL_NEXT (arg))
9267 tree fn = OVL_CURRENT (arg);
9268 tree subargs, elem;
9270 if (TREE_CODE (fn) != TEMPLATE_DECL)
9271 continue;
9273 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
9274 expl_subargs);
9275 if (subargs)
9277 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9278 good += try_one_overload (tparms, targs, tempargs, parm,
9279 elem, strict, sub_strict, addr_p);
9283 else
9285 gcc_assert (TREE_CODE (arg) == OVERLOAD
9286 || TREE_CODE (arg) == FUNCTION_DECL);
9288 for (; arg; arg = OVL_NEXT (arg))
9289 good += try_one_overload (tparms, targs, tempargs, parm,
9290 TREE_TYPE (OVL_CURRENT (arg)),
9291 strict, sub_strict, addr_p);
9294 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9295 to function or pointer to member function argument if the set of
9296 overloaded functions does not contain function templates and at most
9297 one of a set of overloaded functions provides a unique match.
9299 So if we found multiple possibilities, we return success but don't
9300 deduce anything. */
9302 if (good == 1)
9304 int i = TREE_VEC_LENGTH (targs);
9305 for (; i--; )
9306 if (TREE_VEC_ELT (tempargs, i))
9307 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9309 if (good)
9310 return 0;
9312 return 1;
9315 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9316 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9317 different overloads deduce different arguments for a given parm.
9318 ADDR_P is true if the expression for which deduction is being
9319 performed was of the form "& fn" rather than simply "fn".
9321 Returns 1 on success. */
9323 static int
9324 try_one_overload (tree tparms,
9325 tree orig_targs,
9326 tree targs,
9327 tree parm,
9328 tree arg,
9329 unification_kind_t strict,
9330 int sub_strict,
9331 bool addr_p)
9333 int nargs;
9334 tree tempargs;
9335 int i;
9337 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9338 to function or pointer to member function argument if the set of
9339 overloaded functions does not contain function templates and at most
9340 one of a set of overloaded functions provides a unique match.
9342 So if this is a template, just return success. */
9344 if (uses_template_parms (arg))
9345 return 1;
9347 if (TREE_CODE (arg) == METHOD_TYPE)
9348 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9349 else if (addr_p)
9350 arg = build_pointer_type (arg);
9352 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9354 /* We don't copy orig_targs for this because if we have already deduced
9355 some template args from previous args, unify would complain when we
9356 try to deduce a template parameter for the same argument, even though
9357 there isn't really a conflict. */
9358 nargs = TREE_VEC_LENGTH (targs);
9359 tempargs = make_tree_vec (nargs);
9361 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9362 return 0;
9364 /* First make sure we didn't deduce anything that conflicts with
9365 explicitly specified args. */
9366 for (i = nargs; i--; )
9368 tree elt = TREE_VEC_ELT (tempargs, i);
9369 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9371 if (elt == NULL_TREE)
9372 continue;
9373 else if (uses_template_parms (elt))
9375 /* Since we're unifying against ourselves, we will fill in template
9376 args used in the function parm list with our own template parms.
9377 Discard them. */
9378 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9379 continue;
9381 else if (oldelt && ! template_args_equal (oldelt, elt))
9382 return 0;
9385 for (i = nargs; i--; )
9387 tree elt = TREE_VEC_ELT (tempargs, i);
9389 if (elt)
9390 TREE_VEC_ELT (targs, i) = elt;
9393 return 1;
9396 /* Verify that nondeduce template argument agrees with the type
9397 obtained from argument deduction. Return nonzero if the
9398 verification fails.
9400 For example:
9402 struct A { typedef int X; };
9403 template <class T, class U> struct C {};
9404 template <class T> struct C<T, typename T::X> {};
9406 Then with the instantiation `C<A, int>', we can deduce that
9407 `T' is `A' but unify () does not check whether `typename T::X'
9408 is `int'. This function ensure that they agree.
9410 TARGS, PARMS are the same as the arguments of unify.
9411 ARGS contains template arguments from all levels. */
9413 static int
9414 verify_class_unification (tree targs, tree parms, tree args)
9416 parms = tsubst (parms, add_outermost_template_args (args, targs),
9417 tf_none, NULL_TREE);
9418 if (parms == error_mark_node)
9419 return 1;
9421 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9424 /* PARM is a template class (perhaps with unbound template
9425 parameters). ARG is a fully instantiated type. If ARG can be
9426 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9427 TARGS are as for unify. */
9429 static tree
9430 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9432 tree copy_of_targs;
9434 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9435 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9436 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9437 return NULL_TREE;
9439 /* We need to make a new template argument vector for the call to
9440 unify. If we used TARGS, we'd clutter it up with the result of
9441 the attempted unification, even if this class didn't work out.
9442 We also don't want to commit ourselves to all the unifications
9443 we've already done, since unification is supposed to be done on
9444 an argument-by-argument basis. In other words, consider the
9445 following pathological case:
9447 template <int I, int J, int K>
9448 struct S {};
9450 template <int I, int J>
9451 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9453 template <int I, int J, int K>
9454 void f(S<I, J, K>, S<I, I, I>);
9456 void g() {
9457 S<0, 0, 0> s0;
9458 S<0, 1, 2> s2;
9460 f(s0, s2);
9463 Now, by the time we consider the unification involving `s2', we
9464 already know that we must have `f<0, 0, 0>'. But, even though
9465 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9466 because there are two ways to unify base classes of S<0, 1, 2>
9467 with S<I, I, I>. If we kept the already deduced knowledge, we
9468 would reject the possibility I=1. */
9469 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9471 /* If unification failed, we're done. */
9472 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9473 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9474 return NULL_TREE;
9476 return arg;
9479 /* Given a template type PARM and a class type ARG, find the unique
9480 base type in ARG that is an instance of PARM. We do not examine
9481 ARG itself; only its base-classes. If there is not exactly one
9482 appropriate base class, return NULL_TREE. PARM may be the type of
9483 a partial specialization, as well as a plain template type. Used
9484 by unify. */
9486 static tree
9487 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9489 tree rval = NULL_TREE;
9490 tree binfo;
9492 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9494 binfo = TYPE_BINFO (complete_type (arg));
9495 if (!binfo)
9496 /* The type could not be completed. */
9497 return NULL_TREE;
9499 /* Walk in inheritance graph order. The search order is not
9500 important, and this avoids multiple walks of virtual bases. */
9501 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9503 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9505 if (r)
9507 /* If there is more than one satisfactory baseclass, then:
9509 [temp.deduct.call]
9511 If they yield more than one possible deduced A, the type
9512 deduction fails.
9514 applies. */
9515 if (rval && !same_type_p (r, rval))
9516 return NULL_TREE;
9518 rval = r;
9522 return rval;
9525 /* Returns the level of DECL, which declares a template parameter. */
9527 static int
9528 template_decl_level (tree decl)
9530 switch (TREE_CODE (decl))
9532 case TYPE_DECL:
9533 case TEMPLATE_DECL:
9534 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9536 case PARM_DECL:
9537 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9539 default:
9540 gcc_unreachable ();
9542 return 0;
9545 /* Decide whether ARG can be unified with PARM, considering only the
9546 cv-qualifiers of each type, given STRICT as documented for unify.
9547 Returns nonzero iff the unification is OK on that basis. */
9549 static int
9550 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9552 int arg_quals = cp_type_quals (arg);
9553 int parm_quals = cp_type_quals (parm);
9555 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9556 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9558 /* Although a CVR qualifier is ignored when being applied to a
9559 substituted template parameter ([8.3.2]/1 for example), that
9560 does not apply during deduction [14.8.2.4]/1, (even though
9561 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9562 this). Except when we're allowing additional CV qualifiers
9563 at the outer level [14.8.2.1]/3,1st bullet. */
9564 if ((TREE_CODE (arg) == REFERENCE_TYPE
9565 || TREE_CODE (arg) == FUNCTION_TYPE
9566 || TREE_CODE (arg) == METHOD_TYPE)
9567 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9568 return 0;
9570 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9571 && (parm_quals & TYPE_QUAL_RESTRICT))
9572 return 0;
9575 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9576 && (arg_quals & parm_quals) != parm_quals)
9577 return 0;
9579 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9580 && (parm_quals & arg_quals) != arg_quals)
9581 return 0;
9583 return 1;
9586 /* Takes parameters as for type_unification. Returns 0 if the
9587 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9588 bitwise or of the following flags:
9590 UNIFY_ALLOW_NONE:
9591 Require an exact match between PARM and ARG.
9592 UNIFY_ALLOW_MORE_CV_QUAL:
9593 Allow the deduced ARG to be more cv-qualified (by qualification
9594 conversion) than ARG.
9595 UNIFY_ALLOW_LESS_CV_QUAL:
9596 Allow the deduced ARG to be less cv-qualified than ARG.
9597 UNIFY_ALLOW_DERIVED:
9598 Allow the deduced ARG to be a template base class of ARG,
9599 or a pointer to a template base class of the type pointed to by
9600 ARG.
9601 UNIFY_ALLOW_INTEGER:
9602 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9603 case for more information.
9604 UNIFY_ALLOW_OUTER_LEVEL:
9605 This is the outermost level of a deduction. Used to determine validity
9606 of qualification conversions. A valid qualification conversion must
9607 have const qualified pointers leading up to the inner type which
9608 requires additional CV quals, except at the outer level, where const
9609 is not required [conv.qual]. It would be normal to set this flag in
9610 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9611 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9612 This is the outermost level of a deduction, and PARM can be more CV
9613 qualified at this point.
9614 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9615 This is the outermost level of a deduction, and PARM can be less CV
9616 qualified at this point. */
9618 static int
9619 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
9621 int idx;
9622 tree targ;
9623 tree tparm;
9624 int strict_in = strict;
9626 /* I don't think this will do the right thing with respect to types.
9627 But the only case I've seen it in so far has been array bounds, where
9628 signedness is the only information lost, and I think that will be
9629 okay. */
9630 while (TREE_CODE (parm) == NOP_EXPR)
9631 parm = TREE_OPERAND (parm, 0);
9633 if (arg == error_mark_node)
9634 return 1;
9635 if (arg == unknown_type_node)
9636 /* We can't deduce anything from this, but we might get all the
9637 template args from other function args. */
9638 return 0;
9640 /* If PARM uses template parameters, then we can't bail out here,
9641 even if ARG == PARM, since we won't record unifications for the
9642 template parameters. We might need them if we're trying to
9643 figure out which of two things is more specialized. */
9644 if (arg == parm && !uses_template_parms (parm))
9645 return 0;
9647 /* Immediately reject some pairs that won't unify because of
9648 cv-qualification mismatches. */
9649 if (TREE_CODE (arg) == TREE_CODE (parm)
9650 && TYPE_P (arg)
9651 /* It is the elements of the array which hold the cv quals of an array
9652 type, and the elements might be template type parms. We'll check
9653 when we recurse. */
9654 && TREE_CODE (arg) != ARRAY_TYPE
9655 /* We check the cv-qualifiers when unifying with template type
9656 parameters below. We want to allow ARG `const T' to unify with
9657 PARM `T' for example, when computing which of two templates
9658 is more specialized, for example. */
9659 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9660 && !check_cv_quals_for_unify (strict_in, arg, parm))
9661 return 1;
9663 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9664 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9665 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9666 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9667 strict &= ~UNIFY_ALLOW_DERIVED;
9668 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9669 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9671 switch (TREE_CODE (parm))
9673 case TYPENAME_TYPE:
9674 case SCOPE_REF:
9675 case UNBOUND_CLASS_TEMPLATE:
9676 /* In a type which contains a nested-name-specifier, template
9677 argument values cannot be deduced for template parameters used
9678 within the nested-name-specifier. */
9679 return 0;
9681 case TEMPLATE_TYPE_PARM:
9682 case TEMPLATE_TEMPLATE_PARM:
9683 case BOUND_TEMPLATE_TEMPLATE_PARM:
9684 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9686 if (TEMPLATE_TYPE_LEVEL (parm)
9687 != template_decl_level (tparm))
9688 /* The PARM is not one we're trying to unify. Just check
9689 to see if it matches ARG. */
9690 return (TREE_CODE (arg) == TREE_CODE (parm)
9691 && same_type_p (parm, arg)) ? 0 : 1;
9692 idx = TEMPLATE_TYPE_IDX (parm);
9693 targ = TREE_VEC_ELT (targs, idx);
9694 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9696 /* Check for mixed types and values. */
9697 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9698 && TREE_CODE (tparm) != TYPE_DECL)
9699 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9700 && TREE_CODE (tparm) != TEMPLATE_DECL))
9701 return 1;
9703 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9705 /* ARG must be constructed from a template class or a template
9706 template parameter. */
9707 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9708 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9709 return 1;
9712 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9713 tree parmvec = TYPE_TI_ARGS (parm);
9714 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
9715 tree argtmplvec
9716 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9717 int i;
9719 /* The parameter and argument roles have to be switched here
9720 in order to handle default arguments properly. For example,
9721 template<template <class> class TT> void f(TT<int>)
9722 should be able to accept vector<int> which comes from
9723 template <class T, class Allocator = allocator>
9724 class vector. */
9726 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9727 == error_mark_node)
9728 return 1;
9730 /* Deduce arguments T, i from TT<T> or TT<i>.
9731 We check each element of PARMVEC and ARGVEC individually
9732 rather than the whole TREE_VEC since they can have
9733 different number of elements. */
9735 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9737 if (unify (tparms, targs,
9738 TREE_VEC_ELT (parmvec, i),
9739 TREE_VEC_ELT (argvec, i),
9740 UNIFY_ALLOW_NONE))
9741 return 1;
9744 arg = TYPE_TI_TEMPLATE (arg);
9746 /* Fall through to deduce template name. */
9749 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9750 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9752 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9754 /* Simple cases: Value already set, does match or doesn't. */
9755 if (targ != NULL_TREE && template_args_equal (targ, arg))
9756 return 0;
9757 else if (targ)
9758 return 1;
9760 else
9762 /* If PARM is `const T' and ARG is only `int', we don't have
9763 a match unless we are allowing additional qualification.
9764 If ARG is `const int' and PARM is just `T' that's OK;
9765 that binds `const int' to `T'. */
9766 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9767 arg, parm))
9768 return 1;
9770 /* Consider the case where ARG is `const volatile int' and
9771 PARM is `const T'. Then, T should be `volatile int'. */
9772 arg = cp_build_qualified_type_real
9773 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9774 if (arg == error_mark_node)
9775 return 1;
9777 /* Simple cases: Value already set, does match or doesn't. */
9778 if (targ != NULL_TREE && same_type_p (targ, arg))
9779 return 0;
9780 else if (targ)
9781 return 1;
9783 /* Make sure that ARG is not a variable-sized array. (Note
9784 that were talking about variable-sized arrays (like
9785 `int[n]'), rather than arrays of unknown size (like
9786 `int[]').) We'll get very confused by such a type since
9787 the bound of the array will not be computable in an
9788 instantiation. Besides, such types are not allowed in
9789 ISO C++, so we can do as we please here. */
9790 if (variably_modified_type_p (arg, NULL_TREE))
9791 return 1;
9794 TREE_VEC_ELT (targs, idx) = arg;
9795 return 0;
9797 case TEMPLATE_PARM_INDEX:
9798 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9800 if (TEMPLATE_PARM_LEVEL (parm)
9801 != template_decl_level (tparm))
9802 /* The PARM is not one we're trying to unify. Just check
9803 to see if it matches ARG. */
9804 return !(TREE_CODE (arg) == TREE_CODE (parm)
9805 && cp_tree_equal (parm, arg));
9807 idx = TEMPLATE_PARM_IDX (parm);
9808 targ = TREE_VEC_ELT (targs, idx);
9810 if (targ)
9811 return !cp_tree_equal (targ, arg);
9813 /* [temp.deduct.type] If, in the declaration of a function template
9814 with a non-type template-parameter, the non-type
9815 template-parameter is used in an expression in the function
9816 parameter-list and, if the corresponding template-argument is
9817 deduced, the template-argument type shall match the type of the
9818 template-parameter exactly, except that a template-argument
9819 deduced from an array bound may be of any integral type.
9820 The non-type parameter might use already deduced type parameters. */
9821 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9822 if (!TREE_TYPE (arg))
9823 /* Template-parameter dependent expression. Just accept it for now.
9824 It will later be processed in convert_template_argument. */
9826 else if (same_type_p (TREE_TYPE (arg), tparm))
9827 /* OK */;
9828 else if ((strict & UNIFY_ALLOW_INTEGER)
9829 && (TREE_CODE (tparm) == INTEGER_TYPE
9830 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9831 /* Convert the ARG to the type of PARM; the deduced non-type
9832 template argument must exactly match the types of the
9833 corresponding parameter. */
9834 arg = fold (build_nop (TREE_TYPE (parm), arg));
9835 else if (uses_template_parms (tparm))
9836 /* We haven't deduced the type of this parameter yet. Try again
9837 later. */
9838 return 0;
9839 else
9840 return 1;
9842 TREE_VEC_ELT (targs, idx) = arg;
9843 return 0;
9845 case PTRMEM_CST:
9847 /* A pointer-to-member constant can be unified only with
9848 another constant. */
9849 if (TREE_CODE (arg) != PTRMEM_CST)
9850 return 1;
9852 /* Just unify the class member. It would be useless (and possibly
9853 wrong, depending on the strict flags) to unify also
9854 PTRMEM_CST_CLASS, because we want to be sure that both parm and
9855 arg refer to the same variable, even if through different
9856 classes. For instance:
9858 struct A { int x; };
9859 struct B : A { };
9861 Unification of &A::x and &B::x must succeed. */
9862 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
9863 PTRMEM_CST_MEMBER (arg), strict);
9866 case POINTER_TYPE:
9868 if (TREE_CODE (arg) != POINTER_TYPE)
9869 return 1;
9871 /* [temp.deduct.call]
9873 A can be another pointer or pointer to member type that can
9874 be converted to the deduced A via a qualification
9875 conversion (_conv.qual_).
9877 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9878 This will allow for additional cv-qualification of the
9879 pointed-to types if appropriate. */
9881 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9882 /* The derived-to-base conversion only persists through one
9883 level of pointers. */
9884 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9886 return unify (tparms, targs, TREE_TYPE (parm),
9887 TREE_TYPE (arg), strict);
9890 case REFERENCE_TYPE:
9891 if (TREE_CODE (arg) != REFERENCE_TYPE)
9892 return 1;
9893 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9894 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9896 case ARRAY_TYPE:
9897 if (TREE_CODE (arg) != ARRAY_TYPE)
9898 return 1;
9899 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9900 != (TYPE_DOMAIN (arg) == NULL_TREE))
9901 return 1;
9902 if (TYPE_DOMAIN (parm) != NULL_TREE)
9904 tree parm_max;
9905 tree arg_max;
9907 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
9908 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
9910 /* Our representation of array types uses "N - 1" as the
9911 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
9912 not an integer constant. */
9913 if (TREE_CODE (parm_max) == MINUS_EXPR)
9915 arg_max = fold (build2 (PLUS_EXPR,
9916 integer_type_node,
9917 arg_max,
9918 TREE_OPERAND (parm_max, 1)));
9919 parm_max = TREE_OPERAND (parm_max, 0);
9922 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
9923 return 1;
9925 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9926 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9928 case REAL_TYPE:
9929 case COMPLEX_TYPE:
9930 case VECTOR_TYPE:
9931 case INTEGER_TYPE:
9932 case BOOLEAN_TYPE:
9933 case ENUMERAL_TYPE:
9934 case VOID_TYPE:
9935 if (TREE_CODE (arg) != TREE_CODE (parm))
9936 return 1;
9938 /* We have already checked cv-qualification at the top of the
9939 function. */
9940 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9941 return 1;
9943 /* As far as unification is concerned, this wins. Later checks
9944 will invalidate it if necessary. */
9945 return 0;
9947 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9948 /* Type INTEGER_CST can come from ordinary constant template args. */
9949 case INTEGER_CST:
9950 while (TREE_CODE (arg) == NOP_EXPR)
9951 arg = TREE_OPERAND (arg, 0);
9953 if (TREE_CODE (arg) != INTEGER_CST)
9954 return 1;
9955 return !tree_int_cst_equal (parm, arg);
9957 case TREE_VEC:
9959 int i;
9960 if (TREE_CODE (arg) != TREE_VEC)
9961 return 1;
9962 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9963 return 1;
9964 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
9965 if (unify (tparms, targs,
9966 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
9967 UNIFY_ALLOW_NONE))
9968 return 1;
9969 return 0;
9972 case RECORD_TYPE:
9973 case UNION_TYPE:
9974 if (TREE_CODE (arg) != TREE_CODE (parm))
9975 return 1;
9977 if (TYPE_PTRMEMFUNC_P (parm))
9979 if (!TYPE_PTRMEMFUNC_P (arg))
9980 return 1;
9982 return unify (tparms, targs,
9983 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9984 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9985 strict);
9988 if (CLASSTYPE_TEMPLATE_INFO (parm))
9990 tree t = NULL_TREE;
9992 if (strict_in & UNIFY_ALLOW_DERIVED)
9994 /* First, we try to unify the PARM and ARG directly. */
9995 t = try_class_unification (tparms, targs,
9996 parm, arg);
9998 if (!t)
10000 /* Fallback to the special case allowed in
10001 [temp.deduct.call]:
10003 If P is a class, and P has the form
10004 template-id, then A can be a derived class of
10005 the deduced A. Likewise, if P is a pointer to
10006 a class of the form template-id, A can be a
10007 pointer to a derived class pointed to by the
10008 deduced A. */
10009 t = get_template_base (tparms, targs, parm, arg);
10011 if (!t)
10012 return 1;
10015 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10016 && (CLASSTYPE_TI_TEMPLATE (parm)
10017 == CLASSTYPE_TI_TEMPLATE (arg)))
10018 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10019 Then, we should unify `int' and `U'. */
10020 t = arg;
10021 else
10022 /* There's no chance of unification succeeding. */
10023 return 1;
10025 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10026 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10028 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10029 return 1;
10030 return 0;
10032 case METHOD_TYPE:
10033 case FUNCTION_TYPE:
10034 if (TREE_CODE (arg) != TREE_CODE (parm))
10035 return 1;
10037 if (unify (tparms, targs, TREE_TYPE (parm),
10038 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10039 return 1;
10040 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10041 TYPE_ARG_TYPES (arg), 1,
10042 DEDUCE_EXACT, 0, -1);
10044 case OFFSET_TYPE:
10045 if (TREE_CODE (arg) != OFFSET_TYPE)
10046 return 1;
10047 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10048 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10049 return 1;
10050 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10051 strict);
10053 case CONST_DECL:
10054 if (DECL_TEMPLATE_PARM_P (parm))
10055 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10056 if (arg != decl_constant_value (parm))
10057 return 1;
10058 return 0;
10060 case FIELD_DECL:
10061 case TEMPLATE_DECL:
10062 /* Matched cases are handled by the ARG == PARM test above. */
10063 return 1;
10065 default:
10066 gcc_assert (EXPR_P (parm));
10068 /* We must be looking at an expression. This can happen with
10069 something like:
10071 template <int I>
10072 void foo(S<I>, S<I + 2>);
10074 This is a "nondeduced context":
10076 [deduct.type]
10078 The nondeduced contexts are:
10080 --A type that is a template-id in which one or more of
10081 the template-arguments is an expression that references
10082 a template-parameter.
10084 In these cases, we assume deduction succeeded, but don't
10085 actually infer any unifications. */
10087 if (!uses_template_parms (parm)
10088 && !template_args_equal (parm, arg))
10089 return 1;
10090 else
10091 return 0;
10095 /* Note that DECL can be defined in this translation unit, if
10096 required. */
10098 static void
10099 mark_definable (tree decl)
10101 tree clone;
10102 DECL_NOT_REALLY_EXTERN (decl) = 1;
10103 FOR_EACH_CLONE (clone, decl)
10104 DECL_NOT_REALLY_EXTERN (clone) = 1;
10107 /* Called if RESULT is explicitly instantiated, or is a member of an
10108 explicitly instantiated class. */
10110 void
10111 mark_decl_instantiated (tree result, int extern_p)
10113 SET_DECL_EXPLICIT_INSTANTIATION (result);
10115 /* If this entity has already been written out, it's too late to
10116 make any modifications. */
10117 if (TREE_ASM_WRITTEN (result))
10118 return;
10120 if (TREE_CODE (result) != FUNCTION_DECL)
10121 /* The TREE_PUBLIC flag for function declarations will have been
10122 set correctly by tsubst. */
10123 TREE_PUBLIC (result) = 1;
10125 /* This might have been set by an earlier implicit instantiation. */
10126 DECL_COMDAT (result) = 0;
10128 if (extern_p)
10129 DECL_NOT_REALLY_EXTERN (result) = 0;
10130 else
10132 mark_definable (result);
10133 /* Always make artificials weak. */
10134 if (DECL_ARTIFICIAL (result) && flag_weak)
10135 comdat_linkage (result);
10136 /* For WIN32 we also want to put explicit instantiations in
10137 linkonce sections. */
10138 else if (TREE_PUBLIC (result))
10139 maybe_make_one_only (result);
10142 /* If EXTERN_P, then this function will not be emitted -- unless
10143 followed by an explicit instantiation, at which point its linkage
10144 will be adjusted. If !EXTERN_P, then this function will be
10145 emitted here. In neither circumstance do we want
10146 import_export_decl to adjust the linkage. */
10147 DECL_INTERFACE_KNOWN (result) = 1;
10150 /* Given two function templates PAT1 and PAT2, return:
10152 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
10154 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10155 -1 if PAT2 is more specialized than PAT1.
10156 0 if neither is more specialized.
10158 LEN is passed through to fn_type_unification. */
10161 more_specialized (tree pat1, tree pat2, int deduce, int len)
10163 tree targs;
10164 int winner = 0;
10166 /* If template argument deduction succeeds, we substitute the
10167 resulting arguments into non-deduced contexts. While doing that,
10168 we must be aware that we may encounter dependent types. */
10169 ++processing_template_decl;
10170 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
10171 NULL_TREE, 0, deduce, len);
10172 if (targs)
10173 --winner;
10175 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
10176 NULL_TREE, 0, deduce, len);
10177 if (targs)
10178 ++winner;
10179 --processing_template_decl;
10181 return winner;
10184 /* Given two class template specialization list nodes PAT1 and PAT2, return:
10186 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
10187 -1 if PAT2 is more specialized than PAT1.
10188 0 if neither is more specialized.
10190 FULL_ARGS is the full set of template arguments that triggers this
10191 partial ordering. */
10194 more_specialized_class (tree pat1, tree pat2, tree full_args)
10196 tree targs;
10197 int winner = 0;
10199 /* Just like what happens for functions, if we are ordering between
10200 different class template specializations, we may encounter dependent
10201 types in the arguments, and we need our dependency check functions
10202 to behave correctly. */
10203 ++processing_template_decl;
10204 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
10205 add_outermost_template_args (full_args, TREE_PURPOSE (pat2)));
10206 if (targs)
10207 --winner;
10209 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
10210 add_outermost_template_args (full_args, TREE_PURPOSE (pat1)));
10211 if (targs)
10212 ++winner;
10213 --processing_template_decl;
10215 return winner;
10218 /* Return the template arguments that will produce the function signature
10219 DECL from the function template FN, with the explicit template
10220 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
10221 also match. Return NULL_TREE if no satisfactory arguments could be
10222 found. DEDUCE and LEN are passed through to fn_type_unification. */
10224 static tree
10225 get_bindings_real (tree fn,
10226 tree decl,
10227 tree explicit_args,
10228 int check_rettype,
10229 int deduce,
10230 int len)
10232 int ntparms = DECL_NTPARMS (fn);
10233 tree targs = make_tree_vec (ntparms);
10234 tree decl_type;
10235 tree decl_arg_types;
10236 int i;
10238 /* Substitute the explicit template arguments into the type of DECL.
10239 The call to fn_type_unification will handle substitution into the
10240 FN. */
10241 decl_type = TREE_TYPE (decl);
10242 if (explicit_args && uses_template_parms (decl_type))
10244 tree tmpl;
10245 tree converted_args;
10247 if (DECL_TEMPLATE_INFO (decl))
10248 tmpl = DECL_TI_TEMPLATE (decl);
10249 else
10250 /* We can get here for some invalid specializations. */
10251 return NULL_TREE;
10253 converted_args
10254 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10255 explicit_args, NULL_TREE,
10256 tf_none, /*require_all_arguments=*/0));
10257 if (converted_args == error_mark_node)
10258 return NULL_TREE;
10260 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10261 if (decl_type == error_mark_node)
10262 return NULL_TREE;
10265 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10266 /* Never do unification on the 'this' parameter. */
10267 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10268 decl_arg_types = TREE_CHAIN (decl_arg_types);
10270 i = fn_type_unification (fn, explicit_args, targs,
10271 decl_arg_types,
10272 (check_rettype || DECL_CONV_FN_P (fn)
10273 ? TREE_TYPE (decl_type) : NULL_TREE),
10274 deduce, len);
10276 if (i != 0)
10277 return NULL_TREE;
10279 return targs;
10282 /* For most uses, we want to check the return type. */
10284 static tree
10285 get_bindings (tree fn, tree decl, tree explicit_args)
10287 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
10290 /* But for resolve_overloaded_unification, we only care about the parameter
10291 types. */
10293 static tree
10294 get_bindings_overload (tree fn, tree decl, tree explicit_args)
10296 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
10299 /* Return the innermost template arguments that, when applied to a
10300 template specialization whose innermost template parameters are
10301 TPARMS, and whose specialization arguments are PARMS, yield the
10302 ARGS.
10304 For example, suppose we have:
10306 template <class T, class U> struct S {};
10307 template <class T> struct S<T*, int> {};
10309 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10310 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10311 int}. The resulting vector will be {double}, indicating that `T'
10312 is bound to `double'. */
10314 static tree
10315 get_class_bindings (tree tparms, tree parms, tree args)
10317 int i, ntparms = TREE_VEC_LENGTH (tparms);
10318 tree vec = make_tree_vec (ntparms);
10320 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10321 UNIFY_ALLOW_NONE))
10322 return NULL_TREE;
10324 for (i = 0; i < ntparms; ++i)
10325 if (! TREE_VEC_ELT (vec, i))
10326 return NULL_TREE;
10328 if (verify_class_unification (vec, parms, args))
10329 return NULL_TREE;
10331 return vec;
10334 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10335 Pick the most specialized template, and return the corresponding
10336 instantiation, or if there is no corresponding instantiation, the
10337 template itself. If there is no most specialized template,
10338 error_mark_node is returned. If there are no templates at all,
10339 NULL_TREE is returned. */
10341 tree
10342 most_specialized_instantiation (tree instantiations)
10344 tree fn, champ;
10345 int fate;
10347 if (!instantiations)
10348 return NULL_TREE;
10350 champ = instantiations;
10351 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10353 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10354 DEDUCE_EXACT, -1);
10355 if (fate == 1)
10357 else
10359 if (fate == 0)
10361 fn = TREE_CHAIN (fn);
10362 if (! fn)
10363 return error_mark_node;
10365 champ = fn;
10369 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10371 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10372 DEDUCE_EXACT, -1);
10373 if (fate != 1)
10374 return error_mark_node;
10377 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10380 /* Return the most specialized of the list of templates in FNS that can
10381 produce an instantiation matching DECL, given the explicit template
10382 arguments EXPLICIT_ARGS. */
10384 static tree
10385 most_specialized (tree fns, tree decl, tree explicit_args)
10387 tree candidates = NULL_TREE;
10388 tree fn, args;
10390 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10392 tree candidate = TREE_VALUE (fn);
10394 args = get_bindings (candidate, decl, explicit_args);
10395 if (args)
10396 candidates = tree_cons (NULL_TREE, candidate, candidates);
10399 return most_specialized_instantiation (candidates);
10402 /* If DECL is a specialization of some template, return the most
10403 general such template. Otherwise, returns NULL_TREE.
10405 For example, given:
10407 template <class T> struct S { template <class U> void f(U); };
10409 if TMPL is `template <class U> void S<int>::f(U)' this will return
10410 the full template. This function will not trace past partial
10411 specializations, however. For example, given in addition:
10413 template <class T> struct S<T*> { template <class U> void f(U); };
10415 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10416 `template <class T> template <class U> S<T*>::f(U)'. */
10418 tree
10419 most_general_template (tree decl)
10421 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10422 an immediate specialization. */
10423 if (TREE_CODE (decl) == FUNCTION_DECL)
10425 if (DECL_TEMPLATE_INFO (decl)) {
10426 decl = DECL_TI_TEMPLATE (decl);
10428 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10429 template friend. */
10430 if (TREE_CODE (decl) != TEMPLATE_DECL)
10431 return NULL_TREE;
10432 } else
10433 return NULL_TREE;
10436 /* Look for more and more general templates. */
10437 while (DECL_TEMPLATE_INFO (decl))
10439 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
10440 (See cp-tree.h for details.) */
10441 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10442 break;
10444 if (CLASS_TYPE_P (TREE_TYPE (decl))
10445 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10446 break;
10448 /* Stop if we run into an explicitly specialized class template. */
10449 if (!DECL_NAMESPACE_SCOPE_P (decl)
10450 && DECL_CONTEXT (decl)
10451 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10452 break;
10454 decl = DECL_TI_TEMPLATE (decl);
10457 return decl;
10460 /* Return the most specialized of the class template specializations
10461 of TMPL which can produce an instantiation matching ARGS, or
10462 error_mark_node if the choice is ambiguous. */
10464 static tree
10465 most_specialized_class (tree tmpl, tree args)
10467 tree list = NULL_TREE;
10468 tree t;
10469 tree champ;
10470 int fate;
10472 tmpl = most_general_template (tmpl);
10473 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10475 tree spec_args
10476 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10477 if (spec_args)
10479 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10480 TREE_TYPE (list) = TREE_TYPE (t);
10484 if (! list)
10485 return NULL_TREE;
10487 t = list;
10488 champ = t;
10489 t = TREE_CHAIN (t);
10490 for (; t; t = TREE_CHAIN (t))
10492 fate = more_specialized_class (champ, t, args);
10493 if (fate == 1)
10495 else
10497 if (fate == 0)
10499 t = TREE_CHAIN (t);
10500 if (! t)
10501 return error_mark_node;
10503 champ = t;
10507 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10509 fate = more_specialized_class (champ, t, args);
10510 if (fate != 1)
10511 return error_mark_node;
10514 return champ;
10517 /* Explicitly instantiate DECL. */
10519 void
10520 do_decl_instantiation (tree decl, tree storage)
10522 tree result = NULL_TREE;
10523 int extern_p = 0;
10525 if (!decl)
10526 /* An error occurred, for which grokdeclarator has already issued
10527 an appropriate message. */
10528 return;
10529 else if (! DECL_LANG_SPECIFIC (decl))
10531 error ("explicit instantiation of non-template %q#D", decl);
10532 return;
10534 else if (TREE_CODE (decl) == VAR_DECL)
10536 /* There is an asymmetry here in the way VAR_DECLs and
10537 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10538 the latter, the DECL we get back will be marked as a
10539 template instantiation, and the appropriate
10540 DECL_TEMPLATE_INFO will be set up. This does not happen for
10541 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10542 should handle VAR_DECLs as it currently handles
10543 FUNCTION_DECLs. */
10544 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10545 if (!result || TREE_CODE (result) != VAR_DECL)
10547 error ("no matching template for %qD found", decl);
10548 return;
10551 else if (TREE_CODE (decl) != FUNCTION_DECL)
10553 error ("explicit instantiation of %q#D", decl);
10554 return;
10556 else
10557 result = decl;
10559 /* Check for various error cases. Note that if the explicit
10560 instantiation is valid the RESULT will currently be marked as an
10561 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10562 until we get here. */
10564 if (DECL_TEMPLATE_SPECIALIZATION (result))
10566 /* DR 259 [temp.spec].
10568 Both an explicit instantiation and a declaration of an explicit
10569 specialization shall not appear in a program unless the explicit
10570 instantiation follows a declaration of the explicit specialization.
10572 For a given set of template parameters, if an explicit
10573 instantiation of a template appears after a declaration of an
10574 explicit specialization for that template, the explicit
10575 instantiation has no effect. */
10576 return;
10578 else if (DECL_EXPLICIT_INSTANTIATION (result))
10580 /* [temp.spec]
10582 No program shall explicitly instantiate any template more
10583 than once.
10585 We check DECL_NOT_REALLY_EXTERN so as not to complain when
10586 the first instantiation was `extern' and the second is not,
10587 and EXTERN_P for the opposite case. */
10588 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
10589 pedwarn ("duplicate explicit instantiation of %q#D", result);
10590 /* If an "extern" explicit instantiation follows an ordinary
10591 explicit instantiation, the template is instantiated. */
10592 if (extern_p)
10593 return;
10595 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10597 error ("no matching template for %qD found", result);
10598 return;
10600 else if (!DECL_TEMPLATE_INFO (result))
10602 pedwarn ("explicit instantiation of non-template %q#D", result);
10603 return;
10606 if (storage == NULL_TREE)
10608 else if (storage == ridpointers[(int) RID_EXTERN])
10610 if (pedantic && !in_system_header)
10611 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
10612 "instantiations");
10613 extern_p = 1;
10615 else
10616 error ("storage class %qD applied to template instantiation", storage);
10618 mark_decl_instantiated (result, extern_p);
10619 if (! extern_p)
10620 instantiate_decl (result, /*defer_ok=*/1, /*undefined_ok=*/0);
10623 void
10624 mark_class_instantiated (tree t, int extern_p)
10626 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10627 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10628 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10629 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10630 if (! extern_p)
10632 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10633 rest_of_type_compilation (t, 1);
10637 /* Called from do_type_instantiation through binding_table_foreach to
10638 do recursive instantiation for the type bound in ENTRY. */
10639 static void
10640 bt_instantiate_type_proc (binding_entry entry, void *data)
10642 tree storage = *(tree *) data;
10644 if (IS_AGGR_TYPE (entry->type)
10645 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
10646 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
10649 /* Called from do_type_instantiation to instantiate a member
10650 (a member function or a static member variable) of an
10651 explicitly instantiated class template. */
10652 static void
10653 instantiate_class_member (tree decl, int extern_p)
10655 mark_decl_instantiated (decl, extern_p);
10656 if (! extern_p)
10657 instantiate_decl (decl, /*defer_ok=*/1, /* undefined_ok=*/1);
10660 /* Perform an explicit instantiation of template class T. STORAGE, if
10661 non-null, is the RID for extern, inline or static. COMPLAIN is
10662 nonzero if this is called from the parser, zero if called recursively,
10663 since the standard is unclear (as detailed below). */
10665 void
10666 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
10668 int extern_p = 0;
10669 int nomem_p = 0;
10670 int static_p = 0;
10671 int previous_instantiation_extern_p = 0;
10673 if (TREE_CODE (t) == TYPE_DECL)
10674 t = TREE_TYPE (t);
10676 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10678 error ("explicit instantiation of non-template type %qT", t);
10679 return;
10682 complete_type (t);
10684 if (!COMPLETE_TYPE_P (t))
10686 if (complain & tf_error)
10687 error ("explicit instantiation of %q#T before definition of template",
10689 return;
10692 if (storage != NULL_TREE)
10694 if (pedantic && !in_system_header)
10695 pedwarn("ISO C++ forbids the use of `%E' on explicit instantiations",
10696 storage);
10698 if (storage == ridpointers[(int) RID_INLINE])
10699 nomem_p = 1;
10700 else if (storage == ridpointers[(int) RID_EXTERN])
10701 extern_p = 1;
10702 else if (storage == ridpointers[(int) RID_STATIC])
10703 static_p = 1;
10704 else
10706 error ("storage class %qD applied to template instantiation",
10707 storage);
10708 extern_p = 0;
10712 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10714 /* DR 259 [temp.spec].
10716 Both an explicit instantiation and a declaration of an explicit
10717 specialization shall not appear in a program unless the explicit
10718 instantiation follows a declaration of the explicit specialization.
10720 For a given set of template parameters, if an explicit
10721 instantiation of a template appears after a declaration of an
10722 explicit specialization for that template, the explicit
10723 instantiation has no effect. */
10724 return;
10726 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10728 /* [temp.spec]
10730 No program shall explicitly instantiate any template more
10731 than once.
10733 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
10734 instantiation was `extern'. If EXTERN_P then the second is.
10735 These cases are OK. */
10736 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
10738 if (!previous_instantiation_extern_p && !extern_p
10739 && (complain & tf_error))
10740 pedwarn ("duplicate explicit instantiation of %q#T", t);
10742 /* If we've already instantiated the template, just return now. */
10743 if (!CLASSTYPE_INTERFACE_ONLY (t))
10744 return;
10747 mark_class_instantiated (t, extern_p);
10749 if (nomem_p)
10750 return;
10753 tree tmp;
10755 /* In contrast to implicit instantiation, where only the
10756 declarations, and not the definitions, of members are
10757 instantiated, we have here:
10759 [temp.explicit]
10761 The explicit instantiation of a class template specialization
10762 implies the instantiation of all of its members not
10763 previously explicitly specialized in the translation unit
10764 containing the explicit instantiation.
10766 Of course, we can't instantiate member template classes, since
10767 we don't have any arguments for them. Note that the standard
10768 is unclear on whether the instantiation of the members are
10769 *explicit* instantiations or not. However, the most natural
10770 interpretation is that it should be an explicit instantiation. */
10772 if (! static_p)
10773 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10774 if (TREE_CODE (tmp) == FUNCTION_DECL
10775 && DECL_TEMPLATE_INSTANTIATION (tmp))
10776 instantiate_class_member (tmp, extern_p);
10778 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10779 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10780 instantiate_class_member (tmp, extern_p);
10782 if (CLASSTYPE_NESTED_UTDS (t))
10783 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
10784 bt_instantiate_type_proc, &storage);
10788 /* Given a function DECL, which is a specialization of TMPL, modify
10789 DECL to be a re-instantiation of TMPL with the same template
10790 arguments. TMPL should be the template into which tsubst'ing
10791 should occur for DECL, not the most general template.
10793 One reason for doing this is a scenario like this:
10795 template <class T>
10796 void f(const T&, int i);
10798 void g() { f(3, 7); }
10800 template <class T>
10801 void f(const T& t, const int i) { }
10803 Note that when the template is first instantiated, with
10804 instantiate_template, the resulting DECL will have no name for the
10805 first parameter, and the wrong type for the second. So, when we go
10806 to instantiate the DECL, we regenerate it. */
10808 static void
10809 regenerate_decl_from_template (tree decl, tree tmpl)
10811 /* The most general version of TMPL. */
10812 tree gen_tmpl;
10813 /* The arguments used to instantiate DECL, from the most general
10814 template. */
10815 tree args;
10816 tree code_pattern;
10817 tree new_decl;
10818 bool unregistered;
10820 args = DECL_TI_ARGS (decl);
10821 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10823 /* Unregister the specialization so that when we tsubst we will not
10824 just return DECL. We don't have to unregister DECL from TMPL
10825 because if would only be registered there if it were a partial
10826 instantiation of a specialization, which it isn't: it's a full
10827 instantiation. */
10828 gen_tmpl = most_general_template (tmpl);
10829 unregistered = reregister_specialization (decl, gen_tmpl,
10830 /*new_spec=*/NULL_TREE);
10832 /* If the DECL was not unregistered then something peculiar is
10833 happening: we created a specialization but did not call
10834 register_specialization for it. */
10835 gcc_assert (unregistered);
10837 /* Make sure that we can see identifiers, and compute access
10838 correctly. */
10839 push_access_scope (decl);
10841 /* Do the substitution to get the new declaration. */
10842 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10844 if (TREE_CODE (decl) == VAR_DECL)
10846 /* Set up DECL_INITIAL, since tsubst doesn't. */
10847 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10848 DECL_INITIAL (new_decl) =
10849 tsubst_expr (DECL_INITIAL (code_pattern), args,
10850 tf_error, DECL_TI_TEMPLATE (decl));
10852 else if (TREE_CODE (decl) == FUNCTION_DECL)
10854 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10855 new decl. */
10856 DECL_INITIAL (new_decl) = error_mark_node;
10857 /* And don't complain about a duplicate definition. */
10858 DECL_INITIAL (decl) = NULL_TREE;
10861 pop_access_scope (decl);
10863 /* The immediate parent of the new template is still whatever it was
10864 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10865 general template. We also reset the DECL_ASSEMBLER_NAME since
10866 tsubst always calculates the name as if the function in question
10867 were really a template instance, and sometimes, with friend
10868 functions, this is not so. See tsubst_friend_function for
10869 details. */
10870 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10871 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10872 COPY_DECL_RTL (decl, new_decl);
10873 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10875 /* Call duplicate decls to merge the old and new declarations. */
10876 duplicate_decls (new_decl, decl);
10878 /* Now, re-register the specialization. */
10879 register_specialization (decl, gen_tmpl, args);
10882 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10883 substituted to get DECL. */
10885 tree
10886 template_for_substitution (tree decl)
10888 tree tmpl = DECL_TI_TEMPLATE (decl);
10890 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10891 for the instantiation. This is not always the most general
10892 template. Consider, for example:
10894 template <class T>
10895 struct S { template <class U> void f();
10896 template <> void f<int>(); };
10898 and an instantiation of S<double>::f<int>. We want TD to be the
10899 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10900 while (/* An instantiation cannot have a definition, so we need a
10901 more general template. */
10902 DECL_TEMPLATE_INSTANTIATION (tmpl)
10903 /* We must also deal with friend templates. Given:
10905 template <class T> struct S {
10906 template <class U> friend void f() {};
10909 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10910 so far as the language is concerned, but that's still
10911 where we get the pattern for the instantiation from. On
10912 other hand, if the definition comes outside the class, say:
10914 template <class T> struct S {
10915 template <class U> friend void f();
10917 template <class U> friend void f() {}
10919 we don't need to look any further. That's what the check for
10920 DECL_INITIAL is for. */
10921 || (TREE_CODE (decl) == FUNCTION_DECL
10922 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10923 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10925 /* The present template, TD, should not be a definition. If it
10926 were a definition, we should be using it! Note that we
10927 cannot restructure the loop to just keep going until we find
10928 a template with a definition, since that might go too far if
10929 a specialization was declared, but not defined. */
10930 gcc_assert (TREE_CODE (decl) != VAR_DECL
10931 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
10933 /* Fetch the more general template. */
10934 tmpl = DECL_TI_TEMPLATE (tmpl);
10937 return tmpl;
10940 /* Produce the definition of D, a _DECL generated from a template. If
10941 DEFER_OK is nonzero, then we don't have to actually do the
10942 instantiation now; we just have to do it sometime. Normally it is
10943 an error if this is an explicit instantiation but D is undefined.
10944 If UNDEFINED_OK is nonzero, then instead we treat it as an implicit
10945 instantiation. UNDEFINED_OK is nonzero only if we are being used
10946 to instantiate the members of an explicitly instantiated class
10947 template. */
10950 tree
10951 instantiate_decl (tree d, int defer_ok, int undefined_ok)
10953 tree tmpl = DECL_TI_TEMPLATE (d);
10954 tree gen_args;
10955 tree args;
10956 tree td;
10957 tree code_pattern;
10958 tree spec;
10959 tree gen_tmpl;
10960 int pattern_defined;
10961 int need_push;
10962 location_t saved_loc = input_location;
10964 /* This function should only be used to instantiate templates for
10965 functions and static member variables. */
10966 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
10967 || TREE_CODE (d) == VAR_DECL);
10969 /* Variables are never deferred; if instantiation is required, they
10970 are instantiated right away. That allows for better code in the
10971 case that an expression refers to the value of the variable --
10972 if the variable has a constant value the referring expression can
10973 take advantage of that fact. */
10974 if (TREE_CODE (d) == VAR_DECL)
10975 defer_ok = 0;
10977 /* Don't instantiate cloned functions. Instead, instantiate the
10978 functions they cloned. */
10979 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10980 d = DECL_CLONED_FUNCTION (d);
10982 if (DECL_TEMPLATE_INSTANTIATED (d))
10983 /* D has already been instantiated. It might seem reasonable to
10984 check whether or not D is an explicit instantiation, and, if so,
10985 stop here. But when an explicit instantiation is deferred
10986 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10987 is set, even though we still need to do the instantiation. */
10988 return d;
10990 /* If we already have a specialization of this declaration, then
10991 there's no reason to instantiate it. Note that
10992 retrieve_specialization gives us both instantiations and
10993 specializations, so we must explicitly check
10994 DECL_TEMPLATE_SPECIALIZATION. */
10995 gen_tmpl = most_general_template (tmpl);
10996 gen_args = DECL_TI_ARGS (d);
10997 spec = retrieve_specialization (gen_tmpl, gen_args);
10998 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10999 return spec;
11001 /* This needs to happen before any tsubsting. */
11002 if (! push_tinst_level (d))
11003 return d;
11005 timevar_push (TV_PARSE);
11007 /* We may be in the middle of deferred access check. Disable it now. */
11008 push_deferring_access_checks (dk_no_deferred);
11010 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11011 for the instantiation. */
11012 td = template_for_substitution (d);
11013 code_pattern = DECL_TEMPLATE_RESULT (td);
11015 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11016 || DECL_TEMPLATE_SPECIALIZATION (td))
11017 /* In the case of a friend template whose definition is provided
11018 outside the class, we may have too many arguments. Drop the
11019 ones we don't need. The same is true for specializations. */
11020 args = get_innermost_template_args
11021 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11022 else
11023 args = gen_args;
11025 if (TREE_CODE (d) == FUNCTION_DECL)
11026 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11027 else
11028 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11029 /* Unless an explicit instantiation directive has already determined
11030 the linkage of D, remember that a definition is available for
11031 this entity. */
11032 if (pattern_defined
11033 && !DECL_INTERFACE_KNOWN (d)
11034 && !DECL_NOT_REALLY_EXTERN (d))
11035 mark_definable (d);
11037 input_location = DECL_SOURCE_LOCATION (d);
11039 if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok)
11041 DECL_NOT_REALLY_EXTERN (d) = 0;
11042 SET_DECL_IMPLICIT_INSTANTIATION (d);
11045 if (!defer_ok)
11047 /* Recheck the substitutions to obtain any warning messages
11048 about ignoring cv qualifiers. */
11049 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11050 tree type = TREE_TYPE (gen);
11052 /* Make sure that we can see identifiers, and compute access
11053 correctly. D is already the target FUNCTION_DECL with the
11054 right context. */
11055 push_access_scope (d);
11057 if (TREE_CODE (gen) == FUNCTION_DECL)
11059 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
11060 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11061 tf_error | tf_warning, d);
11062 /* Don't simply tsubst the function type, as that will give
11063 duplicate warnings about poor parameter qualifications.
11064 The function arguments are the same as the decl_arguments
11065 without the top level cv qualifiers. */
11066 type = TREE_TYPE (type);
11068 tsubst (type, gen_args, tf_error | tf_warning, d);
11070 pop_access_scope (d);
11073 /* We should have set up DECL_INITIAL in instantiate_class_template
11074 for in-class definitions of static data members. */
11075 gcc_assert (!(TREE_CODE (d) == VAR_DECL
11076 && DECL_INITIALIZED_IN_CLASS_P (d)
11077 && DECL_INITIAL (d) == NULL_TREE));
11079 /* Do not instantiate templates that we know will be defined
11080 elsewhere. */
11081 if (DECL_INTERFACE_KNOWN (d)
11082 && DECL_REALLY_EXTERN (d)
11083 && ! (TREE_CODE (d) == FUNCTION_DECL
11084 && DECL_INLINE (d)))
11085 goto out;
11086 /* Defer all other templates, unless we have been explicitly
11087 forbidden from doing so. We restore the source position here
11088 because it's used by add_pending_template. */
11089 else if (! pattern_defined || defer_ok)
11091 input_location = saved_loc;
11093 if (at_eof && !pattern_defined
11094 && DECL_EXPLICIT_INSTANTIATION (d))
11095 /* [temp.explicit]
11097 The definition of a non-exported function template, a
11098 non-exported member function template, or a non-exported
11099 member function or static data member of a class template
11100 shall be present in every translation unit in which it is
11101 explicitly instantiated. */
11102 pedwarn
11103 ("explicit instantiation of %qD but no definition available", d);
11105 add_pending_template (d);
11106 goto out;
11108 /* Tell the repository that D is available in this translation unit
11109 -- and see if it is supposed to be instantiated here. */
11110 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11112 /* In a PCH file, despite the fact that the repository hasn't
11113 requested instantiation in the PCH it is still possible that
11114 an instantiation will be required in a file that includes the
11115 PCH. */
11116 if (pch_file)
11117 add_pending_template (d);
11118 /* Instantiate inline functions so that the inliner can do its
11119 job, even though we'll not be emitting a copy of this
11120 function. */
11121 if (!(TREE_CODE (d) == FUNCTION_DECL
11122 && flag_inline_trees
11123 && DECL_DECLARED_INLINE_P (d)))
11124 goto out;
11127 need_push = !cfun || !global_bindings_p ();
11128 if (need_push)
11129 push_to_top_level ();
11131 /* Mark D as instantiated so that recursive calls to
11132 instantiate_decl do not try to instantiate it again. */
11133 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11135 /* Regenerate the declaration in case the template has been modified
11136 by a subsequent redeclaration. */
11137 regenerate_decl_from_template (d, td);
11139 /* We already set the file and line above. Reset them now in case
11140 they changed as a result of calling regenerate_decl_from_template. */
11141 input_location = DECL_SOURCE_LOCATION (d);
11143 if (TREE_CODE (d) == VAR_DECL)
11145 /* Clear out DECL_RTL; whatever was there before may not be right
11146 since we've reset the type of the declaration. */
11147 SET_DECL_RTL (d, NULL_RTX);
11148 DECL_IN_AGGR_P (d) = 0;
11150 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11151 initializer. That function will defer actual emission until
11152 we have a chance to determine linkage. */
11153 DECL_EXTERNAL (d) = 0;
11155 /* Enter the scope of D so that access-checking works correctly. */
11156 push_nested_class (DECL_CONTEXT (d));
11157 cp_finish_decl (d,
11158 (!DECL_INITIALIZED_IN_CLASS_P (d)
11159 ? DECL_INITIAL (d) : NULL_TREE),
11160 NULL_TREE, 0);
11161 pop_nested_class ();
11163 else if (TREE_CODE (d) == FUNCTION_DECL)
11165 htab_t saved_local_specializations;
11166 tree subst_decl;
11167 tree tmpl_parm;
11168 tree spec_parm;
11170 /* Save away the current list, in case we are instantiating one
11171 template from within the body of another. */
11172 saved_local_specializations = local_specializations;
11174 /* Set up the list of local specializations. */
11175 local_specializations = htab_create (37,
11176 hash_local_specialization,
11177 eq_local_specializations,
11178 NULL);
11180 /* Set up context. */
11181 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11183 /* Create substitution entries for the parameters. */
11184 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11185 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11186 spec_parm = DECL_ARGUMENTS (d);
11187 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11189 register_local_specialization (spec_parm, tmpl_parm);
11190 spec_parm = skip_artificial_parms_for (d, spec_parm);
11191 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11193 while (tmpl_parm)
11195 register_local_specialization (spec_parm, tmpl_parm);
11196 tmpl_parm = TREE_CHAIN (tmpl_parm);
11197 spec_parm = TREE_CHAIN (spec_parm);
11199 gcc_assert (!spec_parm);
11201 /* Substitute into the body of the function. */
11202 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11203 tf_error | tf_warning, tmpl);
11205 /* We don't need the local specializations any more. */
11206 htab_delete (local_specializations);
11207 local_specializations = saved_local_specializations;
11209 /* Finish the function. */
11210 d = finish_function (0);
11211 expand_or_defer_fn (d);
11214 /* We're not deferring instantiation any more. */
11215 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11217 if (need_push)
11218 pop_from_top_level ();
11220 out:
11221 input_location = saved_loc;
11222 pop_deferring_access_checks ();
11223 pop_tinst_level ();
11225 timevar_pop (TV_PARSE);
11227 return d;
11230 /* Run through the list of templates that we wish we could
11231 instantiate, and instantiate any we can. RETRIES is the
11232 number of times we retry pending template instantiation. */
11234 void
11235 instantiate_pending_templates (int retries)
11237 tree *t;
11238 tree last = NULL_TREE;
11239 int reconsider;
11240 location_t saved_loc = input_location;
11242 /* Instantiating templates may trigger vtable generation. This in turn
11243 may require further template instantiations. We place a limit here
11244 to avoid infinite loop. */
11245 if (pending_templates && retries >= max_tinst_depth)
11247 cp_error_at ("template instantiation depth exceeds maximum of %d"
11248 " (use -ftemplate-depth-NN to increase the maximum)"
11249 " instantiating %q+D, possibly from virtual table"
11250 " generation",
11251 max_tinst_depth, TREE_VALUE (pending_templates));
11252 return;
11257 reconsider = 0;
11259 t = &pending_templates;
11260 while (*t)
11262 tree instantiation = TREE_VALUE (*t);
11264 reopen_tinst_level (TREE_PURPOSE (*t));
11266 if (TYPE_P (instantiation))
11268 tree fn;
11270 if (!COMPLETE_TYPE_P (instantiation))
11272 instantiate_class_template (instantiation);
11273 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11274 for (fn = TYPE_METHODS (instantiation);
11276 fn = TREE_CHAIN (fn))
11277 if (! DECL_ARTIFICIAL (fn))
11278 instantiate_decl (fn, /*defer_ok=*/0,
11279 /*undefined_ok=*/0);
11280 if (COMPLETE_TYPE_P (instantiation))
11281 reconsider = 1;
11284 if (COMPLETE_TYPE_P (instantiation))
11285 /* If INSTANTIATION has been instantiated, then we don't
11286 need to consider it again in the future. */
11287 *t = TREE_CHAIN (*t);
11288 else
11290 last = *t;
11291 t = &TREE_CHAIN (*t);
11294 else
11296 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11297 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11299 instantiation = instantiate_decl (instantiation,
11300 /*defer_ok=*/0,
11301 /*undefined_ok=*/0);
11302 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11303 reconsider = 1;
11306 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11307 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11308 /* If INSTANTIATION has been instantiated, then we don't
11309 need to consider it again in the future. */
11310 *t = TREE_CHAIN (*t);
11311 else
11313 last = *t;
11314 t = &TREE_CHAIN (*t);
11317 tinst_depth = 0;
11318 current_tinst_level = NULL_TREE;
11320 last_pending_template = last;
11322 while (reconsider);
11324 input_location = saved_loc;
11327 /* Substitute ARGVEC into T, which is a list of initializers for
11328 either base class or a non-static data member. The TREE_PURPOSEs
11329 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11330 instantiate_decl. */
11332 static tree
11333 tsubst_initializer_list (tree t, tree argvec)
11335 tree inits = NULL_TREE;
11337 for (; t; t = TREE_CHAIN (t))
11339 tree decl;
11340 tree init;
11341 tree val;
11343 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11344 NULL_TREE);
11345 decl = expand_member_init (decl);
11346 if (decl && !DECL_P (decl))
11347 in_base_initializer = 1;
11349 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11350 NULL_TREE);
11351 if (!init)
11353 else if (TREE_CODE (init) == TREE_LIST)
11354 for (val = init; val; val = TREE_CHAIN (val))
11355 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11356 else if (init != void_type_node)
11357 init = convert_from_reference (init);
11359 in_base_initializer = 0;
11361 if (decl)
11363 init = build_tree_list (decl, init);
11364 TREE_CHAIN (init) = inits;
11365 inits = init;
11368 return inits;
11371 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11373 static void
11374 set_current_access_from_decl (tree decl)
11376 if (TREE_PRIVATE (decl))
11377 current_access_specifier = access_private_node;
11378 else if (TREE_PROTECTED (decl))
11379 current_access_specifier = access_protected_node;
11380 else
11381 current_access_specifier = access_public_node;
11384 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11385 is the instantiation (which should have been created with
11386 start_enum) and ARGS are the template arguments to use. */
11388 static void
11389 tsubst_enum (tree tag, tree newtag, tree args)
11391 tree e;
11393 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11395 tree value;
11396 tree decl;
11398 decl = TREE_VALUE (e);
11399 /* Note that in a template enum, the TREE_VALUE is the
11400 CONST_DECL, not the corresponding INTEGER_CST. */
11401 value = tsubst_expr (DECL_INITIAL (decl),
11402 args, tf_error | tf_warning,
11403 NULL_TREE);
11405 /* Give this enumeration constant the correct access. */
11406 set_current_access_from_decl (decl);
11408 /* Actually build the enumerator itself. */
11409 build_enumerator (DECL_NAME (decl), value, newtag);
11412 finish_enum (newtag);
11413 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11414 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11417 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11418 its type -- but without substituting the innermost set of template
11419 arguments. So, innermost set of template parameters will appear in
11420 the type. */
11422 tree
11423 get_mostly_instantiated_function_type (tree decl)
11425 tree fn_type;
11426 tree tmpl;
11427 tree targs;
11428 tree tparms;
11429 int parm_depth;
11431 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11432 targs = DECL_TI_ARGS (decl);
11433 tparms = DECL_TEMPLATE_PARMS (tmpl);
11434 parm_depth = TMPL_PARMS_DEPTH (tparms);
11436 /* There should be as many levels of arguments as there are levels
11437 of parameters. */
11438 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
11440 fn_type = TREE_TYPE (tmpl);
11442 if (parm_depth == 1)
11443 /* No substitution is necessary. */
11445 else
11447 int i;
11448 tree partial_args;
11450 /* Replace the innermost level of the TARGS with NULL_TREEs to
11451 let tsubst know not to substitute for those parameters. */
11452 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11453 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11454 SET_TMPL_ARGS_LEVEL (partial_args, i,
11455 TMPL_ARGS_LEVEL (targs, i));
11456 SET_TMPL_ARGS_LEVEL (partial_args,
11457 TMPL_ARGS_DEPTH (targs),
11458 make_tree_vec (DECL_NTPARMS (tmpl)));
11460 /* Make sure that we can see identifiers, and compute access
11461 correctly. We can just use the context of DECL for the
11462 partial substitution here. It depends only on outer template
11463 parameters, regardless of whether the innermost level is
11464 specialized or not. */
11465 push_access_scope (decl);
11467 ++processing_template_decl;
11468 /* Now, do the (partial) substitution to figure out the
11469 appropriate function type. */
11470 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11471 --processing_template_decl;
11473 /* Substitute into the template parameters to obtain the real
11474 innermost set of parameters. This step is important if the
11475 innermost set of template parameters contains value
11476 parameters whose types depend on outer template parameters. */
11477 TREE_VEC_LENGTH (partial_args)--;
11478 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11480 pop_access_scope (decl);
11483 return fn_type;
11486 /* Return truthvalue if we're processing a template different from
11487 the last one involved in diagnostics. */
11489 problematic_instantiation_changed (void)
11491 return last_template_error_tick != tinst_level_tick;
11494 /* Remember current template involved in diagnostics. */
11495 void
11496 record_last_problematic_instantiation (void)
11498 last_template_error_tick = tinst_level_tick;
11501 tree
11502 current_instantiation (void)
11504 return current_tinst_level;
11507 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11508 type. Return zero for ok, nonzero for disallowed. Issue error and
11509 warning messages under control of COMPLAIN. */
11511 static int
11512 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
11514 if (INTEGRAL_TYPE_P (type))
11515 return 0;
11516 else if (POINTER_TYPE_P (type))
11517 return 0;
11518 else if (TYPE_PTR_TO_MEMBER_P (type))
11519 return 0;
11520 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11521 return 0;
11522 else if (TREE_CODE (type) == TYPENAME_TYPE)
11523 return 0;
11525 if (complain & tf_error)
11526 error ("%q#T is not a valid type for a template constant parameter", type);
11527 return 1;
11530 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11531 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11533 static bool
11534 dependent_type_p_r (tree type)
11536 tree scope;
11538 /* [temp.dep.type]
11540 A type is dependent if it is:
11542 -- a template parameter. Template template parameters are types
11543 for us (since TYPE_P holds true for them) so we handle
11544 them here. */
11545 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11546 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
11547 return true;
11548 /* -- a qualified-id with a nested-name-specifier which contains a
11549 class-name that names a dependent type or whose unqualified-id
11550 names a dependent type. */
11551 if (TREE_CODE (type) == TYPENAME_TYPE)
11552 return true;
11553 /* -- a cv-qualified type where the cv-unqualified type is
11554 dependent. */
11555 type = TYPE_MAIN_VARIANT (type);
11556 /* -- a compound type constructed from any dependent type. */
11557 if (TYPE_PTR_TO_MEMBER_P (type))
11558 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11559 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11560 (type)));
11561 else if (TREE_CODE (type) == POINTER_TYPE
11562 || TREE_CODE (type) == REFERENCE_TYPE)
11563 return dependent_type_p (TREE_TYPE (type));
11564 else if (TREE_CODE (type) == FUNCTION_TYPE
11565 || TREE_CODE (type) == METHOD_TYPE)
11567 tree arg_type;
11569 if (dependent_type_p (TREE_TYPE (type)))
11570 return true;
11571 for (arg_type = TYPE_ARG_TYPES (type);
11572 arg_type;
11573 arg_type = TREE_CHAIN (arg_type))
11574 if (dependent_type_p (TREE_VALUE (arg_type)))
11575 return true;
11576 return false;
11578 /* -- an array type constructed from any dependent type or whose
11579 size is specified by a constant expression that is
11580 value-dependent. */
11581 if (TREE_CODE (type) == ARRAY_TYPE)
11583 if (TYPE_DOMAIN (type)
11584 && ((value_dependent_expression_p
11585 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11586 || (type_dependent_expression_p
11587 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11588 return true;
11589 return dependent_type_p (TREE_TYPE (type));
11592 /* -- a template-id in which either the template name is a template
11593 parameter ... */
11594 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11595 return true;
11596 /* ... or any of the template arguments is a dependent type or
11597 an expression that is type-dependent or value-dependent. */
11598 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11599 && (any_dependent_template_arguments_p
11600 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
11601 return true;
11603 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11604 expression is not type-dependent, then it should already been
11605 have resolved. */
11606 if (TREE_CODE (type) == TYPEOF_TYPE)
11607 return true;
11609 /* The standard does not specifically mention types that are local
11610 to template functions or local classes, but they should be
11611 considered dependent too. For example:
11613 template <int I> void f() {
11614 enum E { a = I };
11615 S<sizeof (E)> s;
11618 The size of `E' cannot be known until the value of `I' has been
11619 determined. Therefore, `E' must be considered dependent. */
11620 scope = TYPE_CONTEXT (type);
11621 if (scope && TYPE_P (scope))
11622 return dependent_type_p (scope);
11623 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11624 return type_dependent_expression_p (scope);
11626 /* Other types are non-dependent. */
11627 return false;
11630 /* Returns TRUE if TYPE is dependent, in the sense of
11631 [temp.dep.type]. */
11633 bool
11634 dependent_type_p (tree type)
11636 /* If there are no template parameters in scope, then there can't be
11637 any dependent types. */
11638 if (!processing_template_decl)
11639 return false;
11641 /* If the type is NULL, we have not computed a type for the entity
11642 in question; in that case, the type is dependent. */
11643 if (!type)
11644 return true;
11646 /* Erroneous types can be considered non-dependent. */
11647 if (type == error_mark_node)
11648 return false;
11650 /* If we have not already computed the appropriate value for TYPE,
11651 do so now. */
11652 if (!TYPE_DEPENDENT_P_VALID (type))
11654 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11655 TYPE_DEPENDENT_P_VALID (type) = 1;
11658 return TYPE_DEPENDENT_P (type);
11661 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
11663 static bool
11664 dependent_scope_ref_p (tree expression, bool criterion (tree))
11666 tree scope;
11667 tree name;
11669 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
11671 if (!TYPE_P (TREE_OPERAND (expression, 0)))
11672 return true;
11674 scope = TREE_OPERAND (expression, 0);
11675 name = TREE_OPERAND (expression, 1);
11677 /* [temp.dep.expr]
11679 An id-expression is type-dependent if it contains a
11680 nested-name-specifier that contains a class-name that names a
11681 dependent type. */
11682 /* The suggested resolution to Core Issue 2 implies that if the
11683 qualifying type is the current class, then we must peek
11684 inside it. */
11685 if (DECL_P (name)
11686 && currently_open_class (scope)
11687 && !criterion (name))
11688 return false;
11689 if (dependent_type_p (scope))
11690 return true;
11692 return false;
11695 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
11696 [temp.dep.constexpr] */
11698 bool
11699 value_dependent_expression_p (tree expression)
11701 if (!processing_template_decl)
11702 return false;
11704 /* A name declared with a dependent type. */
11705 if (TREE_CODE (expression) == IDENTIFIER_NODE
11706 || (DECL_P (expression)
11707 && type_dependent_expression_p (expression)))
11708 return true;
11709 /* A non-type template parameter. */
11710 if ((TREE_CODE (expression) == CONST_DECL
11711 && DECL_TEMPLATE_PARM_P (expression))
11712 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11713 return true;
11714 /* A constant with integral or enumeration type and is initialized
11715 with an expression that is value-dependent. */
11716 if (TREE_CODE (expression) == VAR_DECL
11717 && DECL_INITIAL (expression)
11718 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
11719 && value_dependent_expression_p (DECL_INITIAL (expression)))
11720 return true;
11721 /* These expressions are value-dependent if the type to which the
11722 cast occurs is dependent or the expression being casted is
11723 value-dependent. */
11724 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11725 || TREE_CODE (expression) == STATIC_CAST_EXPR
11726 || TREE_CODE (expression) == CONST_CAST_EXPR
11727 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11728 || TREE_CODE (expression) == CAST_EXPR)
11730 tree type = TREE_TYPE (expression);
11731 if (dependent_type_p (type))
11732 return true;
11733 /* A functional cast has a list of operands. */
11734 expression = TREE_OPERAND (expression, 0);
11735 if (!expression)
11737 /* If there are no operands, it must be an expression such
11738 as "int()". This should not happen for aggregate types
11739 because it would form non-constant expressions. */
11740 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
11742 return false;
11744 if (TREE_CODE (expression) == TREE_LIST)
11748 if (value_dependent_expression_p (TREE_VALUE (expression)))
11749 return true;
11750 expression = TREE_CHAIN (expression);
11752 while (expression);
11753 return false;
11755 else
11756 return value_dependent_expression_p (expression);
11758 /* A `sizeof' expression is value-dependent if the operand is
11759 type-dependent. */
11760 if (TREE_CODE (expression) == SIZEOF_EXPR
11761 || TREE_CODE (expression) == ALIGNOF_EXPR)
11763 expression = TREE_OPERAND (expression, 0);
11764 if (TYPE_P (expression))
11765 return dependent_type_p (expression);
11766 return type_dependent_expression_p (expression);
11768 if (TREE_CODE (expression) == SCOPE_REF)
11769 return dependent_scope_ref_p (expression, value_dependent_expression_p);
11770 if (TREE_CODE (expression) == COMPONENT_REF)
11771 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
11772 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
11773 /* A constant expression is value-dependent if any subexpression is
11774 value-dependent. */
11775 if (EXPR_P (expression))
11777 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11779 case tcc_unary:
11780 return (value_dependent_expression_p
11781 (TREE_OPERAND (expression, 0)));
11782 case tcc_comparison:
11783 case tcc_binary:
11784 return ((value_dependent_expression_p
11785 (TREE_OPERAND (expression, 0)))
11786 || (value_dependent_expression_p
11787 (TREE_OPERAND (expression, 1))));
11788 case tcc_expression:
11790 int i;
11791 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11792 /* In some cases, some of the operands may be missing.
11793 (For example, in the case of PREDECREMENT_EXPR, the
11794 amount to increment by may be missing.) That doesn't
11795 make the expression dependent. */
11796 if (TREE_OPERAND (expression, i)
11797 && (value_dependent_expression_p
11798 (TREE_OPERAND (expression, i))))
11799 return true;
11800 return false;
11802 case tcc_reference:
11803 case tcc_statement:
11804 /* These cannot be value dependent. */
11805 return false;
11807 default:
11808 gcc_unreachable ();
11812 /* The expression is not value-dependent. */
11813 return false;
11816 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11817 [temp.dep.expr]. */
11819 bool
11820 type_dependent_expression_p (tree expression)
11822 if (!processing_template_decl)
11823 return false;
11825 if (expression == error_mark_node)
11826 return false;
11828 /* An unresolved name is always dependent. */
11829 if (TREE_CODE (expression) == IDENTIFIER_NODE)
11830 return true;
11832 /* Some expression forms are never type-dependent. */
11833 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11834 || TREE_CODE (expression) == SIZEOF_EXPR
11835 || TREE_CODE (expression) == ALIGNOF_EXPR
11836 || TREE_CODE (expression) == TYPEID_EXPR
11837 || TREE_CODE (expression) == DELETE_EXPR
11838 || TREE_CODE (expression) == VEC_DELETE_EXPR
11839 || TREE_CODE (expression) == THROW_EXPR)
11840 return false;
11842 /* The types of these expressions depends only on the type to which
11843 the cast occurs. */
11844 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11845 || TREE_CODE (expression) == STATIC_CAST_EXPR
11846 || TREE_CODE (expression) == CONST_CAST_EXPR
11847 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11848 || TREE_CODE (expression) == CAST_EXPR)
11849 return dependent_type_p (TREE_TYPE (expression));
11851 /* The types of these expressions depends only on the type created
11852 by the expression. */
11853 if (TREE_CODE (expression) == NEW_EXPR
11854 || TREE_CODE (expression) == VEC_NEW_EXPR)
11856 /* For NEW_EXPR tree nodes created inside a template, either
11857 the object type itself or a TREE_LIST may appear as the
11858 operand 1. */
11859 tree type = TREE_OPERAND (expression, 1);
11860 if (TREE_CODE (type) == TREE_LIST)
11861 /* This is an array type. We need to check array dimensions
11862 as well. */
11863 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
11864 || value_dependent_expression_p
11865 (TREE_OPERAND (TREE_VALUE (type), 1));
11866 else
11867 return dependent_type_p (type);
11870 if (TREE_CODE (expression) == SCOPE_REF
11871 && dependent_scope_ref_p (expression,
11872 type_dependent_expression_p))
11873 return true;
11875 if (TREE_CODE (expression) == FUNCTION_DECL
11876 && DECL_LANG_SPECIFIC (expression)
11877 && DECL_TEMPLATE_INFO (expression)
11878 && (any_dependent_template_arguments_p
11879 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11880 return true;
11882 if (TREE_CODE (expression) == TEMPLATE_DECL
11883 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
11884 return false;
11886 if (TREE_TYPE (expression) == unknown_type_node)
11888 if (TREE_CODE (expression) == ADDR_EXPR)
11889 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
11890 if (TREE_CODE (expression) == COMPONENT_REF
11891 || TREE_CODE (expression) == OFFSET_REF)
11893 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
11894 return true;
11895 expression = TREE_OPERAND (expression, 1);
11896 if (TREE_CODE (expression) == IDENTIFIER_NODE)
11897 return false;
11899 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
11900 if (TREE_CODE (expression) == SCOPE_REF)
11901 return false;
11903 if (TREE_CODE (expression) == BASELINK)
11904 expression = BASELINK_FUNCTIONS (expression);
11906 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
11908 if (any_dependent_template_arguments_p
11909 (TREE_OPERAND (expression, 1)))
11910 return true;
11911 expression = TREE_OPERAND (expression, 0);
11913 gcc_assert (TREE_CODE (expression) == OVERLOAD);
11915 while (expression)
11917 if (type_dependent_expression_p (OVL_CURRENT (expression)))
11918 return true;
11919 expression = OVL_NEXT (expression);
11921 return false;
11924 return (dependent_type_p (TREE_TYPE (expression)));
11927 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
11928 contains a type-dependent expression. */
11930 bool
11931 any_type_dependent_arguments_p (tree args)
11933 while (args)
11935 tree arg = TREE_VALUE (args);
11937 if (type_dependent_expression_p (arg))
11938 return true;
11939 args = TREE_CHAIN (args);
11941 return false;
11944 /* Returns TRUE if the ARG (a template argument) is dependent. */
11946 static bool
11947 dependent_template_arg_p (tree arg)
11949 if (!processing_template_decl)
11950 return false;
11952 if (TREE_CODE (arg) == TEMPLATE_DECL
11953 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11954 return dependent_template_p (arg);
11955 else if (TYPE_P (arg))
11956 return dependent_type_p (arg);
11957 else
11958 return (type_dependent_expression_p (arg)
11959 || value_dependent_expression_p (arg));
11962 /* Returns true if ARGS (a collection of template arguments) contains
11963 any dependent arguments. */
11965 bool
11966 any_dependent_template_arguments_p (tree args)
11968 int i;
11969 int j;
11971 if (!args)
11972 return false;
11974 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
11976 tree level = TMPL_ARGS_LEVEL (args, i + 1);
11977 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
11978 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
11979 return true;
11982 return false;
11985 /* Returns TRUE if the template TMPL is dependent. */
11987 bool
11988 dependent_template_p (tree tmpl)
11990 if (TREE_CODE (tmpl) == OVERLOAD)
11992 while (tmpl)
11994 if (dependent_template_p (OVL_FUNCTION (tmpl)))
11995 return true;
11996 tmpl = OVL_CHAIN (tmpl);
11998 return false;
12001 /* Template template parameters are dependent. */
12002 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12003 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12004 return true;
12005 /* So are names that have not been looked up. */
12006 if (TREE_CODE (tmpl) == SCOPE_REF
12007 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12008 return true;
12009 /* So are member templates of dependent classes. */
12010 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12011 return dependent_type_p (DECL_CONTEXT (tmpl));
12012 return false;
12015 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12017 bool
12018 dependent_template_id_p (tree tmpl, tree args)
12020 return (dependent_template_p (tmpl)
12021 || any_dependent_template_arguments_p (args));
12024 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12025 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12026 can be found. Note that this function peers inside uninstantiated
12027 templates and therefore should be used only in extremely limited
12028 situations. */
12030 tree
12031 resolve_typename_type (tree type, bool only_current_p)
12033 tree scope;
12034 tree name;
12035 tree decl;
12036 int quals;
12037 bool pop_p;
12039 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12041 scope = TYPE_CONTEXT (type);
12042 name = TYPE_IDENTIFIER (type);
12044 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12045 it first before we can figure out what NAME refers to. */
12046 if (TREE_CODE (scope) == TYPENAME_TYPE)
12047 scope = resolve_typename_type (scope, only_current_p);
12048 /* If we don't know what SCOPE refers to, then we cannot resolve the
12049 TYPENAME_TYPE. */
12050 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12051 return error_mark_node;
12052 /* If the SCOPE is a template type parameter, we have no way of
12053 resolving the name. */
12054 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12055 return type;
12056 /* If the SCOPE is not the current instantiation, there's no reason
12057 to look inside it. */
12058 if (only_current_p && !currently_open_class (scope))
12059 return error_mark_node;
12060 /* If SCOPE is a partial instantiation, it will not have a valid
12061 TYPE_FIELDS list, so use the original template. */
12062 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12063 /* Enter the SCOPE so that name lookup will be resolved as if we
12064 were in the class definition. In particular, SCOPE will no
12065 longer be considered a dependent type. */
12066 pop_p = push_scope (scope);
12067 /* Look up the declaration. */
12068 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12069 /* Obtain the set of qualifiers applied to the TYPE. */
12070 quals = cp_type_quals (type);
12071 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12072 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12073 if (!decl)
12074 type = error_mark_node;
12075 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12076 && TREE_CODE (decl) == TYPE_DECL)
12077 type = TREE_TYPE (decl);
12078 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12079 && DECL_CLASS_TEMPLATE_P (decl))
12081 tree tmpl;
12082 tree args;
12083 /* Obtain the template and the arguments. */
12084 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12085 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12086 /* Instantiate the template. */
12087 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12088 /*entering_scope=*/0, tf_error | tf_user);
12090 else
12091 type = error_mark_node;
12092 /* Qualify the resulting type. */
12093 if (type != error_mark_node && quals)
12094 type = cp_build_qualified_type (type, quals);
12095 /* Leave the SCOPE. */
12096 if (pop_p)
12097 pop_scope (scope);
12099 return type;
12102 /* EXPR is an expression which is not type-dependent. Return a proxy
12103 for EXPR that can be used to compute the types of larger
12104 expressions containing EXPR. */
12106 tree
12107 build_non_dependent_expr (tree expr)
12109 tree inner_expr;
12111 /* Preserve null pointer constants so that the type of things like
12112 "p == 0" where "p" is a pointer can be determined. */
12113 if (null_ptr_cst_p (expr))
12114 return expr;
12115 /* Preserve OVERLOADs; the functions must be available to resolve
12116 types. */
12117 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12118 TREE_OPERAND (expr, 0) : expr);
12119 if (TREE_CODE (inner_expr) == OVERLOAD
12120 || TREE_CODE (inner_expr) == FUNCTION_DECL
12121 || TREE_CODE (inner_expr) == TEMPLATE_DECL
12122 || TREE_CODE (inner_expr) == TEMPLATE_ID_EXPR)
12123 return expr;
12124 /* Preserve string constants; conversions from string constants to
12125 "char *" are allowed, even though normally a "const char *"
12126 cannot be used to initialize a "char *". */
12127 if (TREE_CODE (expr) == STRING_CST)
12128 return expr;
12129 /* Preserve arithmetic constants, as an optimization -- there is no
12130 reason to create a new node. */
12131 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12132 return expr;
12133 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12134 There is at least one place where we want to know that a
12135 particular expression is a throw-expression: when checking a ?:
12136 expression, there are special rules if the second or third
12137 argument is a throw-expression. */
12138 if (TREE_CODE (expr) == THROW_EXPR)
12139 return expr;
12141 if (TREE_CODE (expr) == COND_EXPR)
12142 return build3 (COND_EXPR,
12143 TREE_TYPE (expr),
12144 TREE_OPERAND (expr, 0),
12145 (TREE_OPERAND (expr, 1)
12146 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12147 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12148 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12149 if (TREE_CODE (expr) == COMPOUND_EXPR
12150 && !COMPOUND_EXPR_OVERLOADED (expr))
12151 return build2 (COMPOUND_EXPR,
12152 TREE_TYPE (expr),
12153 TREE_OPERAND (expr, 0),
12154 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12156 /* Otherwise, build a NON_DEPENDENT_EXPR.
12158 REFERENCE_TYPEs are not stripped for expressions in templates
12159 because doing so would play havoc with mangling. Consider, for
12160 example:
12162 template <typename T> void f<T& g>() { g(); }
12164 In the body of "f", the expression for "g" will have
12165 REFERENCE_TYPE, even though the standard says that it should
12166 not. The reason is that we must preserve the syntactic form of
12167 the expression so that mangling (say) "f<g>" inside the body of
12168 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12169 stripped here. */
12170 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12173 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12174 Return a new TREE_LIST with the various arguments replaced with
12175 equivalent non-dependent expressions. */
12177 tree
12178 build_non_dependent_args (tree args)
12180 tree a;
12181 tree new_args;
12183 new_args = NULL_TREE;
12184 for (a = args; a; a = TREE_CHAIN (a))
12185 new_args = tree_cons (NULL_TREE,
12186 build_non_dependent_expr (TREE_VALUE (a)),
12187 new_args);
12188 return nreverse (new_args);
12191 #include "gt-cp-pt.h"