Merge C++ from gomp-20050608-branch.
[official-gcc.git] / gcc / cp / pt.c
blob6cb78657eb340dc74d6a0c868ceb3fbb7d0b054a
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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 "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work.
56 The TREE_PURPOSE of each entry is either a DECL (for a function or
57 static data member), or a TYPE (for a class) indicating what we are
58 hoping to instantiate. The TREE_VALUE is not used. */
59 static GTY(()) tree pending_templates;
60 static GTY(()) tree last_pending_template;
62 int processing_template_parmlist;
63 static int template_header_count;
65 static GTY(()) tree saved_trees;
66 static GTY(()) varray_type inline_parm_levels;
67 static size_t inline_parm_levels_used;
69 static GTY(()) tree current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static htab_t local_specializations;
83 #define UNIFY_ALLOW_NONE 0
84 #define UNIFY_ALLOW_MORE_CV_QUAL 1
85 #define UNIFY_ALLOW_LESS_CV_QUAL 2
86 #define UNIFY_ALLOW_DERIVED 4
87 #define UNIFY_ALLOW_INTEGER 8
88 #define UNIFY_ALLOW_OUTER_LEVEL 16
89 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
90 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
92 static void push_access_scope (tree);
93 static void pop_access_scope (tree);
94 static int resolve_overloaded_unification (tree, tree, tree, tree,
95 unification_kind_t, int);
96 static int try_one_overload (tree, tree, tree, tree, tree,
97 unification_kind_t, int, bool);
98 static int unify (tree, tree, tree, tree, int);
99 static void add_pending_template (tree);
100 static int push_tinst_level (tree);
101 static void pop_tinst_level (void);
102 static void reopen_tinst_level (tree);
103 static tree classtype_mangled_name (tree);
104 static char* mangle_class_name_for_template (const char *, tree, tree);
105 static tree tsubst_initializer_list (tree, tree);
106 static tree get_class_bindings (tree, tree, tree);
107 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
108 bool, bool);
109 static void tsubst_enum (tree, tree, tree);
110 static tree add_to_template_args (tree, tree);
111 static tree add_outermost_template_args (tree, tree);
112 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
113 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
114 static int type_unification_real (tree, tree, tree, tree,
115 int, unification_kind_t, int);
116 static void note_template_header (int);
117 static tree convert_nontype_argument_function (tree, tree);
118 static tree convert_nontype_argument (tree, tree);
119 static tree convert_template_argument (tree, tree, tree,
120 tsubst_flags_t, int, tree);
121 static int for_each_template_parm (tree, tree_fn_t, void*,
122 struct pointer_set_t*);
123 static tree build_template_parm_index (int, int, int, tree, tree);
124 static int inline_needs_template_parms (tree);
125 static void push_inline_template_parms_recursive (tree, int);
126 static tree retrieve_local_specialization (tree);
127 static void register_local_specialization (tree, tree);
128 static tree reduce_template_parm_level (tree, tree, int);
129 static int mark_template_parm (tree, void *);
130 static int template_parm_this_level_p (tree, void *);
131 static tree tsubst_friend_function (tree, tree);
132 static tree tsubst_friend_class (tree, tree);
133 static int can_complete_type_without_circularity (tree);
134 static tree get_bindings (tree, tree, tree, bool);
135 static int template_decl_level (tree);
136 static int check_cv_quals_for_unify (int, tree, tree);
137 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
139 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140 static void regenerate_decl_from_template (tree, tree);
141 static tree most_specialized_class (tree, tree);
142 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
145 static void check_specialization_scope (void);
146 static tree process_partial_specialization (tree);
147 static void set_current_access_from_decl (tree);
148 static void check_default_tmpl_args (tree, tree, int, int);
149 static tree get_template_base (tree, tree, tree, tree);
150 static tree try_class_unification (tree, tree, tree, tree);
151 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152 tree, tree);
153 static tree determine_specialization (tree, tree, tree *, int, int);
154 static int template_args_equal (tree, tree);
155 static void tsubst_default_arguments (tree);
156 static tree for_each_template_parm_r (tree *, int *, void *);
157 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
158 static void copy_default_args_to_explicit_spec (tree);
159 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
160 static int eq_local_specializations (const void *, const void *);
161 static bool dependent_type_p_r (tree);
162 static tree tsubst (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
166 /* Make the current scope suitable for access checking when we are
167 processing T. T can be FUNCTION_DECL for instantiated function
168 template, or VAR_DECL for static member variable (need by
169 instantiate_decl). */
171 static void
172 push_access_scope (tree t)
174 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
175 || TREE_CODE (t) == VAR_DECL);
177 if (DECL_FRIEND_CONTEXT (t))
178 push_nested_class (DECL_FRIEND_CONTEXT (t));
179 else if (DECL_CLASS_SCOPE_P (t))
180 push_nested_class (DECL_CONTEXT (t));
181 else
182 push_to_top_level ();
184 if (TREE_CODE (t) == FUNCTION_DECL)
186 saved_access_scope = tree_cons
187 (NULL_TREE, current_function_decl, saved_access_scope);
188 current_function_decl = t;
192 /* Restore the scope set up by push_access_scope. T is the node we
193 are processing. */
195 static void
196 pop_access_scope (tree t)
198 if (TREE_CODE (t) == FUNCTION_DECL)
200 current_function_decl = TREE_VALUE (saved_access_scope);
201 saved_access_scope = TREE_CHAIN (saved_access_scope);
204 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
205 pop_nested_class ();
206 else
207 pop_from_top_level ();
210 /* Do any processing required when DECL (a member template
211 declaration) is finished. Returns the TEMPLATE_DECL corresponding
212 to DECL, unless it is a specialization, in which case the DECL
213 itself is returned. */
215 tree
216 finish_member_template_decl (tree decl)
218 if (decl == error_mark_node)
219 return error_mark_node;
221 gcc_assert (DECL_P (decl));
223 if (TREE_CODE (decl) == TYPE_DECL)
225 tree type;
227 type = TREE_TYPE (decl);
228 if (IS_AGGR_TYPE (type)
229 && CLASSTYPE_TEMPLATE_INFO (type)
230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
232 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
233 check_member_template (tmpl);
234 return tmpl;
236 return NULL_TREE;
238 else if (TREE_CODE (decl) == FIELD_DECL)
239 error ("data member %qD cannot be a member template", decl);
240 else if (DECL_TEMPLATE_INFO (decl))
242 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
244 check_member_template (DECL_TI_TEMPLATE (decl));
245 return DECL_TI_TEMPLATE (decl);
247 else
248 return decl;
250 else
251 error ("invalid member template declaration %qD", decl);
253 return error_mark_node;
256 /* Returns the template nesting level of the indicated class TYPE.
258 For example, in:
259 template <class T>
260 struct A
262 template <class U>
263 struct B {};
266 A<T>::B<U> has depth two, while A<T> has depth one.
267 Both A<T>::B<int> and A<int>::B<U> have depth one, if
268 they are instantiations, not specializations.
270 This function is guaranteed to return 0 if passed NULL_TREE so
271 that, for example, `template_class_depth (current_class_type)' is
272 always safe. */
275 template_class_depth (tree type)
277 int depth;
279 for (depth = 0;
280 type && TREE_CODE (type) != NAMESPACE_DECL;
281 type = (TREE_CODE (type) == FUNCTION_DECL)
282 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
284 if (TREE_CODE (type) != FUNCTION_DECL)
286 if (CLASSTYPE_TEMPLATE_INFO (type)
287 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
288 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
289 ++depth;
291 else
293 if (DECL_TEMPLATE_INFO (type)
294 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
295 && uses_template_parms (DECL_TI_ARGS (type)))
296 ++depth;
300 return depth;
303 /* Returns 1 if processing DECL as part of do_pending_inlines
304 needs us to push template parms. */
306 static int
307 inline_needs_template_parms (tree decl)
309 if (! DECL_TEMPLATE_INFO (decl))
310 return 0;
312 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
313 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
316 /* Subroutine of maybe_begin_member_template_processing.
317 Push the template parms in PARMS, starting from LEVELS steps into the
318 chain, and ending at the beginning, since template parms are listed
319 innermost first. */
321 static void
322 push_inline_template_parms_recursive (tree parmlist, int levels)
324 tree parms = TREE_VALUE (parmlist);
325 int i;
327 if (levels > 1)
328 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
330 ++processing_template_decl;
331 current_template_parms
332 = tree_cons (size_int (processing_template_decl),
333 parms, current_template_parms);
334 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
336 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
337 NULL);
338 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
340 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
341 gcc_assert (DECL_P (parm));
343 switch (TREE_CODE (parm))
345 case TYPE_DECL:
346 case TEMPLATE_DECL:
347 pushdecl (parm);
348 break;
350 case PARM_DECL:
352 /* Make a CONST_DECL as is done in process_template_parm.
353 It is ugly that we recreate this here; the original
354 version built in process_template_parm is no longer
355 available. */
356 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
357 TREE_TYPE (parm));
358 DECL_ARTIFICIAL (decl) = 1;
359 TREE_CONSTANT (decl) = 1;
360 TREE_INVARIANT (decl) = 1;
361 TREE_READONLY (decl) = 1;
362 DECL_INITIAL (decl) = DECL_INITIAL (parm);
363 SET_DECL_TEMPLATE_PARM_P (decl);
364 pushdecl (decl);
366 break;
368 default:
369 gcc_unreachable ();
374 /* Restore the template parameter context for a member template or
375 a friend template defined in a class definition. */
377 void
378 maybe_begin_member_template_processing (tree decl)
380 tree parms;
381 int levels = 0;
383 if (inline_needs_template_parms (decl))
385 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
386 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
388 if (DECL_TEMPLATE_SPECIALIZATION (decl))
390 --levels;
391 parms = TREE_CHAIN (parms);
394 push_inline_template_parms_recursive (parms, levels);
397 /* Remember how many levels of template parameters we pushed so that
398 we can pop them later. */
399 if (!inline_parm_levels)
400 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
401 if (inline_parm_levels_used == inline_parm_levels->num_elements)
402 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
403 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
404 ++inline_parm_levels_used;
407 /* Undo the effects of maybe_begin_member_template_processing. */
409 void
410 maybe_end_member_template_processing (void)
412 int i;
414 if (!inline_parm_levels_used)
415 return;
417 --inline_parm_levels_used;
418 for (i = 0;
419 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
420 ++i)
422 --processing_template_decl;
423 current_template_parms = TREE_CHAIN (current_template_parms);
424 poplevel (0, 0, 0);
428 /* Return a new template argument vector which contains all of ARGS,
429 but has as its innermost set of arguments the EXTRA_ARGS. */
431 static tree
432 add_to_template_args (tree args, tree extra_args)
434 tree new_args;
435 int extra_depth;
436 int i;
437 int j;
439 extra_depth = TMPL_ARGS_DEPTH (extra_args);
440 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
442 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
445 for (j = 1; j <= extra_depth; ++j, ++i)
446 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
448 return new_args;
451 /* Like add_to_template_args, but only the outermost ARGS are added to
452 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
453 (EXTRA_ARGS) levels are added. This function is used to combine
454 the template arguments from a partial instantiation with the
455 template arguments used to attain the full instantiation from the
456 partial instantiation. */
458 static tree
459 add_outermost_template_args (tree args, tree extra_args)
461 tree new_args;
463 /* If there are more levels of EXTRA_ARGS than there are ARGS,
464 something very fishy is going on. */
465 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
467 /* If *all* the new arguments will be the EXTRA_ARGS, just return
468 them. */
469 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
470 return extra_args;
472 /* For the moment, we make ARGS look like it contains fewer levels. */
473 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
475 new_args = add_to_template_args (args, extra_args);
477 /* Now, we restore ARGS to its full dimensions. */
478 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
480 return new_args;
483 /* Return the N levels of innermost template arguments from the ARGS. */
485 tree
486 get_innermost_template_args (tree args, int n)
488 tree new_args;
489 int extra_levels;
490 int i;
492 gcc_assert (n >= 0);
494 /* If N is 1, just return the innermost set of template arguments. */
495 if (n == 1)
496 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
498 /* If we're not removing anything, just return the arguments we were
499 given. */
500 extra_levels = TMPL_ARGS_DEPTH (args) - n;
501 gcc_assert (extra_levels >= 0);
502 if (extra_levels == 0)
503 return args;
505 /* Make a new set of arguments, not containing the outer arguments. */
506 new_args = make_tree_vec (n);
507 for (i = 1; i <= n; ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i,
509 TMPL_ARGS_LEVEL (args, i + extra_levels));
511 return new_args;
514 /* We've got a template header coming up; push to a new level for storing
515 the parms. */
517 void
518 begin_template_parm_list (void)
520 /* We use a non-tag-transparent scope here, which causes pushtag to
521 put tags in this scope, rather than in the enclosing class or
522 namespace scope. This is the right thing, since we want
523 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
524 global template class, push_template_decl handles putting the
525 TEMPLATE_DECL into top-level scope. For a nested template class,
526 e.g.:
528 template <class T> struct S1 {
529 template <class T> struct S2 {};
532 pushtag contains special code to call pushdecl_with_scope on the
533 TEMPLATE_DECL for S2. */
534 begin_scope (sk_template_parms, NULL);
535 ++processing_template_decl;
536 ++processing_template_parmlist;
537 note_template_header (0);
540 /* This routine is called when a specialization is declared. If it is
541 invalid to declare a specialization here, an error is reported. */
543 static void
544 check_specialization_scope (void)
546 tree scope = current_scope ();
548 /* [temp.expl.spec]
550 An explicit specialization shall be declared in the namespace of
551 which the template is a member, or, for member templates, in the
552 namespace of which the enclosing class or enclosing class
553 template is a member. An explicit specialization of a member
554 function, member class or static data member of a class template
555 shall be declared in the namespace of which the class template
556 is a member. */
557 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
558 error ("explicit specialization in non-namespace scope %qD", scope);
560 /* [temp.expl.spec]
562 In an explicit specialization declaration for a member of a class
563 template or a member template that appears in namespace scope,
564 the member template and some of its enclosing class templates may
565 remain unspecialized, except that the declaration shall not
566 explicitly specialize a class member template if its enclosing
567 class templates are not explicitly specialized as well. */
568 if (current_template_parms)
569 error ("enclosing class templates are not explicitly specialized");
572 /* We've just seen template <>. */
574 void
575 begin_specialization (void)
577 begin_scope (sk_template_spec, NULL);
578 note_template_header (1);
579 check_specialization_scope ();
582 /* Called at then end of processing a declaration preceded by
583 template<>. */
585 void
586 end_specialization (void)
588 finish_scope ();
589 reset_specialization ();
592 /* Any template <>'s that we have seen thus far are not referring to a
593 function specialization. */
595 void
596 reset_specialization (void)
598 processing_specialization = 0;
599 template_header_count = 0;
602 /* We've just seen a template header. If SPECIALIZATION is nonzero,
603 it was of the form template <>. */
605 static void
606 note_template_header (int specialization)
608 processing_specialization = specialization;
609 template_header_count++;
612 /* We're beginning an explicit instantiation. */
614 void
615 begin_explicit_instantiation (void)
617 gcc_assert (!processing_explicit_instantiation);
618 processing_explicit_instantiation = true;
622 void
623 end_explicit_instantiation (void)
625 gcc_assert (processing_explicit_instantiation);
626 processing_explicit_instantiation = false;
629 /* An explicit specialization or partial specialization TMPL is being
630 declared. Check that the namespace in which the specialization is
631 occurring is permissible. Returns false iff it is invalid to
632 specialize TMPL in the current namespace. */
634 static bool
635 check_specialization_namespace (tree tmpl)
637 tree tpl_ns = decl_namespace_context (tmpl);
639 /* [tmpl.expl.spec]
641 An explicit specialization shall be declared in the namespace of
642 which the template is a member, or, for member templates, in the
643 namespace of which the enclosing class or enclosing class
644 template is a member. An explicit specialization of a member
645 function, member class or static data member of a class template
646 shall be declared in the namespace of which the class template is
647 a member. */
648 if (is_associated_namespace (current_namespace, tpl_ns))
649 /* Same or super-using namespace. */
650 return true;
651 else
653 pedwarn ("specialization of %qD in different namespace", tmpl);
654 pedwarn (" from definition of %q+#D", tmpl);
655 return false;
659 /* SPEC is an explicit instantiation. Check that it is valid to
660 perform this explicit instantiation in the current namespace. */
662 static void
663 check_explicit_instantiation_namespace (tree spec)
665 tree ns;
667 /* DR 275: An explicit instantiation shall appear in an enclosing
668 namespace of its template. */
669 ns = decl_namespace_context (spec);
670 if (!is_ancestor (current_namespace, ns))
671 pedwarn ("explicit instantiation of %qD in namespace %qD "
672 "(which does not enclose namespace %qD)",
673 spec, current_namespace, ns);
676 /* The TYPE is being declared. If it is a template type, that means it
677 is a partial specialization. Do appropriate error-checking. */
679 void
680 maybe_process_partial_specialization (tree type)
682 tree context;
684 if (type == error_mark_node)
685 return;
687 context = TYPE_CONTEXT (type);
689 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
691 /* This is for ordinary explicit specialization and partial
692 specialization of a template class such as:
694 template <> class C<int>;
698 template <class T> class C<T*>;
700 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
702 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
703 && !COMPLETE_TYPE_P (type))
705 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
706 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
707 if (processing_template_decl)
708 push_template_decl (TYPE_MAIN_DECL (type));
710 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
711 error ("specialization of %qT after instantiation", type);
713 else if (CLASS_TYPE_P (type)
714 && !CLASSTYPE_USE_TEMPLATE (type)
715 && CLASSTYPE_TEMPLATE_INFO (type)
716 && context && CLASS_TYPE_P (context)
717 && CLASSTYPE_TEMPLATE_INFO (context))
719 /* This is for an explicit specialization of member class
720 template according to [temp.expl.spec/18]:
722 template <> template <class U> class C<int>::D;
724 The context `C<int>' must be an implicit instantiation.
725 Otherwise this is just a member class template declared
726 earlier like:
728 template <> class C<int> { template <class U> class D; };
729 template <> template <class U> class C<int>::D;
731 In the first case, `C<int>::D' is a specialization of `C<T>::D'
732 while in the second case, `C<int>::D' is a primary template
733 and `C<T>::D' may not exist. */
735 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
736 && !COMPLETE_TYPE_P (type))
738 tree t;
740 if (current_namespace
741 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
743 pedwarn ("specializing %q#T in different namespace", type);
744 pedwarn (" from definition of %q+#D",
745 CLASSTYPE_TI_TEMPLATE (type));
748 /* Check for invalid specialization after instantiation:
750 template <> template <> class C<int>::D<int>;
751 template <> template <class U> class C<int>::D; */
753 for (t = DECL_TEMPLATE_INSTANTIATIONS
754 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
755 t; t = TREE_CHAIN (t))
756 if (TREE_VALUE (t) != type
757 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
758 error ("specialization %qT after instantiation %qT",
759 type, TREE_VALUE (t));
761 /* Mark TYPE as a specialization. And as a result, we only
762 have one level of template argument for the innermost
763 class template. */
764 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
765 CLASSTYPE_TI_ARGS (type)
766 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
769 else if (processing_specialization)
770 error ("explicit specialization of non-template %qT", type);
773 /* Returns nonzero if we can optimize the retrieval of specializations
774 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
775 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
777 static inline bool
778 optimize_specialization_lookup_p (tree tmpl)
780 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
781 && DECL_CLASS_SCOPE_P (tmpl)
782 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
783 parameter. */
784 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
785 /* The optimized lookup depends on the fact that the
786 template arguments for the member function template apply
787 purely to the containing class, which is not true if the
788 containing class is an explicit or partial
789 specialization. */
790 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
791 && !DECL_MEMBER_TEMPLATE_P (tmpl)
792 && !DECL_CONV_FN_P (tmpl)
793 /* It is possible to have a template that is not a member
794 template and is not a member of a template class:
796 template <typename T>
797 struct S { friend A::f(); };
799 Here, the friend function is a template, but the context does
800 not have template information. The optimized lookup relies
801 on having ARGS be the template arguments for both the class
802 and the function template. */
803 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
806 /* Retrieve the specialization (in the sense of [temp.spec] - a
807 specialization is either an instantiation or an explicit
808 specialization) of TMPL for the given template ARGS. If there is
809 no such specialization, return NULL_TREE. The ARGS are a vector of
810 arguments, or a vector of vectors of arguments, in the case of
811 templates with more than one level of parameters.
813 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
814 then we search for a partial specialization matching ARGS. This
815 parameter is ignored if TMPL is not a class template. */
817 static tree
818 retrieve_specialization (tree tmpl, tree args,
819 bool class_specializations_p)
821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
823 /* There should be as many levels of arguments as there are
824 levels of parameters. */
825 gcc_assert (TMPL_ARGS_DEPTH (args)
826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
828 if (optimize_specialization_lookup_p (tmpl))
830 tree class_template;
831 tree class_specialization;
832 VEC(tree,gc) *methods;
833 tree fns;
834 int idx;
836 /* The template arguments actually apply to the containing
837 class. Find the class specialization with those
838 arguments. */
839 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
840 class_specialization
841 = retrieve_specialization (class_template, args,
842 /*class_specializations_p=*/false);
843 if (!class_specialization)
844 return NULL_TREE;
845 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
846 for the specialization. */
847 idx = class_method_index_for_fn (class_specialization, tmpl);
848 if (idx == -1)
849 return NULL_TREE;
850 /* Iterate through the methods with the indicated name, looking
851 for the one that has an instance of TMPL. */
852 methods = CLASSTYPE_METHOD_VEC (class_specialization);
853 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
855 tree fn = OVL_CURRENT (fns);
856 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
857 return fn;
859 return NULL_TREE;
861 else
863 tree *sp;
864 tree *head;
866 /* Class templates store their instantiations on the
867 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
868 DECL_TEMPLATE_SPECIALIZATIONS list. */
869 if (!class_specializations_p
870 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
871 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
872 else
873 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
874 head = sp;
875 /* Iterate through the list until we find a matching template. */
876 while (*sp != NULL_TREE)
878 tree spec = *sp;
880 if (comp_template_args (TREE_PURPOSE (spec), args))
882 /* Use the move-to-front heuristic to speed up future
883 searches. */
884 if (spec != *head)
886 *sp = TREE_CHAIN (*sp);
887 TREE_CHAIN (spec) = *head;
888 *head = spec;
890 return TREE_VALUE (spec);
892 sp = &TREE_CHAIN (spec);
896 return NULL_TREE;
899 /* Like retrieve_specialization, but for local declarations. */
901 static tree
902 retrieve_local_specialization (tree tmpl)
904 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
905 htab_hash_pointer (tmpl));
906 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
909 /* Returns nonzero iff DECL is a specialization of TMPL. */
912 is_specialization_of (tree decl, tree tmpl)
914 tree t;
916 if (TREE_CODE (decl) == FUNCTION_DECL)
918 for (t = decl;
919 t != NULL_TREE;
920 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
921 if (t == tmpl)
922 return 1;
924 else
926 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
928 for (t = TREE_TYPE (decl);
929 t != NULL_TREE;
930 t = CLASSTYPE_USE_TEMPLATE (t)
931 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
932 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
933 return 1;
936 return 0;
939 /* Returns nonzero iff DECL is a specialization of friend declaration
940 FRIEND according to [temp.friend]. */
942 bool
943 is_specialization_of_friend (tree decl, tree friend)
945 bool need_template = true;
946 int template_depth;
948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
949 || TREE_CODE (decl) == TYPE_DECL);
951 /* For [temp.friend/6] when FRIEND is an ordinary member function
952 of a template class, we want to check if DECL is a specialization
953 if this. */
954 if (TREE_CODE (friend) == FUNCTION_DECL
955 && DECL_TEMPLATE_INFO (friend)
956 && !DECL_USE_TEMPLATE (friend))
958 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
959 friend = DECL_TI_TEMPLATE (friend);
960 need_template = false;
962 else if (TREE_CODE (friend) == TEMPLATE_DECL
963 && !PRIMARY_TEMPLATE_P (friend))
964 need_template = false;
966 /* There is nothing to do if this is not a template friend. */
967 if (TREE_CODE (friend) != TEMPLATE_DECL)
968 return false;
970 if (is_specialization_of (decl, friend))
971 return true;
973 /* [temp.friend/6]
974 A member of a class template may be declared to be a friend of a
975 non-template class. In this case, the corresponding member of
976 every specialization of the class template is a friend of the
977 class granting friendship.
979 For example, given a template friend declaration
981 template <class T> friend void A<T>::f();
983 the member function below is considered a friend
985 template <> struct A<int> {
986 void f();
989 For this type of template friend, TEMPLATE_DEPTH below will be
990 nonzero. To determine if DECL is a friend of FRIEND, we first
991 check if the enclosing class is a specialization of another. */
993 template_depth = template_class_depth (DECL_CONTEXT (friend));
994 if (template_depth
995 && DECL_CLASS_SCOPE_P (decl)
996 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
997 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
999 /* Next, we check the members themselves. In order to handle
1000 a few tricky cases, such as when FRIEND's are
1002 template <class T> friend void A<T>::g(T t);
1003 template <class T> template <T t> friend void A<T>::h();
1005 and DECL's are
1007 void A<int>::g(int);
1008 template <int> void A<int>::h();
1010 we need to figure out ARGS, the template arguments from
1011 the context of DECL. This is required for template substitution
1012 of `T' in the function parameter of `g' and template parameter
1013 of `h' in the above examples. Here ARGS corresponds to `int'. */
1015 tree context = DECL_CONTEXT (decl);
1016 tree args = NULL_TREE;
1017 int current_depth = 0;
1019 while (current_depth < template_depth)
1021 if (CLASSTYPE_TEMPLATE_INFO (context))
1023 if (current_depth == 0)
1024 args = TYPE_TI_ARGS (context);
1025 else
1026 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1027 current_depth++;
1029 context = TYPE_CONTEXT (context);
1032 if (TREE_CODE (decl) == FUNCTION_DECL)
1034 bool is_template;
1035 tree friend_type;
1036 tree decl_type;
1037 tree friend_args_type;
1038 tree decl_args_type;
1040 /* Make sure that both DECL and FRIEND are templates or
1041 non-templates. */
1042 is_template = DECL_TEMPLATE_INFO (decl)
1043 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1044 if (need_template ^ is_template)
1045 return false;
1046 else if (is_template)
1048 /* If both are templates, check template parameter list. */
1049 tree friend_parms
1050 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1051 args, tf_none);
1052 if (!comp_template_parms
1053 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1054 friend_parms))
1055 return false;
1057 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1059 else
1060 decl_type = TREE_TYPE (decl);
1062 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1063 tf_none, NULL_TREE);
1064 if (friend_type == error_mark_node)
1065 return false;
1067 /* Check if return types match. */
1068 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1069 return false;
1071 /* Check if function parameter types match, ignoring the
1072 `this' parameter. */
1073 friend_args_type = TYPE_ARG_TYPES (friend_type);
1074 decl_args_type = TYPE_ARG_TYPES (decl_type);
1075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1076 friend_args_type = TREE_CHAIN (friend_args_type);
1077 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1078 decl_args_type = TREE_CHAIN (decl_args_type);
1080 return compparms (decl_args_type, friend_args_type);
1082 else
1084 /* DECL is a TYPE_DECL */
1085 bool is_template;
1086 tree decl_type = TREE_TYPE (decl);
1088 /* Make sure that both DECL and FRIEND are templates or
1089 non-templates. */
1090 is_template
1091 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1094 if (need_template ^ is_template)
1095 return false;
1096 else if (is_template)
1098 tree friend_parms;
1099 /* If both are templates, check the name of the two
1100 TEMPLATE_DECL's first because is_friend didn't. */
1101 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1102 != DECL_NAME (friend))
1103 return false;
1105 /* Now check template parameter list. */
1106 friend_parms
1107 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1108 args, tf_none);
1109 return comp_template_parms
1110 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1111 friend_parms);
1113 else
1114 return (DECL_NAME (decl)
1115 == DECL_NAME (friend));
1118 return false;
1121 /* Register the specialization SPEC as a specialization of TMPL with
1122 the indicated ARGS. IS_FRIEND indicates whether the specialization
1123 is actually just a friend declaration. Returns SPEC, or an
1124 equivalent prior declaration, if available. */
1126 static tree
1127 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1129 tree fn;
1131 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1133 if (TREE_CODE (spec) == FUNCTION_DECL
1134 && uses_template_parms (DECL_TI_ARGS (spec)))
1135 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1136 register it; we want the corresponding TEMPLATE_DECL instead.
1137 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1138 the more obvious `uses_template_parms (spec)' to avoid problems
1139 with default function arguments. In particular, given
1140 something like this:
1142 template <class T> void f(T t1, T t = T())
1144 the default argument expression is not substituted for in an
1145 instantiation unless and until it is actually needed. */
1146 return spec;
1148 fn = retrieve_specialization (tmpl, args,
1149 /*class_specializations_p=*/false);
1150 /* We can sometimes try to re-register a specialization that we've
1151 already got. In particular, regenerate_decl_from_template calls
1152 duplicate_decls which will update the specialization list. But,
1153 we'll still get called again here anyhow. It's more convenient
1154 to simply allow this than to try to prevent it. */
1155 if (fn == spec)
1156 return spec;
1157 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1159 if (DECL_TEMPLATE_INSTANTIATION (fn))
1161 if (TREE_USED (fn)
1162 || DECL_EXPLICIT_INSTANTIATION (fn))
1164 error ("specialization of %qD after instantiation",
1165 fn);
1166 return spec;
1168 else
1170 tree clone;
1171 /* This situation should occur only if the first
1172 specialization is an implicit instantiation, the
1173 second is an explicit specialization, and the
1174 implicit instantiation has not yet been used. That
1175 situation can occur if we have implicitly
1176 instantiated a member function and then specialized
1177 it later.
1179 We can also wind up here if a friend declaration that
1180 looked like an instantiation turns out to be a
1181 specialization:
1183 template <class T> void foo(T);
1184 class S { friend void foo<>(int) };
1185 template <> void foo(int);
1187 We transform the existing DECL in place so that any
1188 pointers to it become pointers to the updated
1189 declaration.
1191 If there was a definition for the template, but not
1192 for the specialization, we want this to look as if
1193 there were no definition, and vice versa. */
1194 DECL_INITIAL (fn) = NULL_TREE;
1195 duplicate_decls (spec, fn, is_friend);
1196 /* The call to duplicate_decls will have applied
1197 [temp.expl.spec]:
1199 An explicit specialization of a function template
1200 is inline only if it is explicitly declared to be,
1201 and independently of whether its function template
1204 to the primary function; now copy the inline bits to
1205 the various clones. */
1206 FOR_EACH_CLONE (clone, fn)
1208 DECL_DECLARED_INLINE_P (clone)
1209 = DECL_DECLARED_INLINE_P (fn);
1210 DECL_INLINE (clone)
1211 = DECL_INLINE (fn);
1213 check_specialization_namespace (fn);
1215 return fn;
1218 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1220 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1221 /* Dup decl failed, but this is a new definition. Set the
1222 line number so any errors match this new
1223 definition. */
1224 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1226 return fn;
1230 /* A specialization must be declared in the same namespace as the
1231 template it is specializing. */
1232 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1233 && !check_specialization_namespace (tmpl))
1234 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1236 if (!optimize_specialization_lookup_p (tmpl))
1237 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1238 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1240 return spec;
1243 /* Unregister the specialization SPEC as a specialization of TMPL.
1244 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1245 if the SPEC was listed as a specialization of TMPL. */
1247 bool
1248 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1250 tree* s;
1252 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1253 *s != NULL_TREE;
1254 s = &TREE_CHAIN (*s))
1255 if (TREE_VALUE (*s) == spec)
1257 if (!new_spec)
1258 *s = TREE_CHAIN (*s);
1259 else
1260 TREE_VALUE (*s) = new_spec;
1261 return 1;
1264 return 0;
1267 /* Compare an entry in the local specializations hash table P1 (which
1268 is really a pointer to a TREE_LIST) with P2 (which is really a
1269 DECL). */
1271 static int
1272 eq_local_specializations (const void *p1, const void *p2)
1274 return TREE_VALUE ((tree) p1) == (tree) p2;
1277 /* Hash P1, an entry in the local specializations table. */
1279 static hashval_t
1280 hash_local_specialization (const void* p1)
1282 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1285 /* Like register_specialization, but for local declarations. We are
1286 registering SPEC, an instantiation of TMPL. */
1288 static void
1289 register_local_specialization (tree spec, tree tmpl)
1291 void **slot;
1293 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1294 htab_hash_pointer (tmpl), INSERT);
1295 *slot = build_tree_list (spec, tmpl);
1298 /* Print the list of candidate FNS in an error message. */
1300 void
1301 print_candidates (tree fns)
1303 tree fn;
1305 const char *str = "candidates are:";
1307 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1309 tree f;
1311 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1312 error ("%s %+#D", str, OVL_CURRENT (f));
1313 str = " ";
1317 /* Returns the template (one of the functions given by TEMPLATE_ID)
1318 which can be specialized to match the indicated DECL with the
1319 explicit template args given in TEMPLATE_ID. The DECL may be
1320 NULL_TREE if none is available. In that case, the functions in
1321 TEMPLATE_ID are non-members.
1323 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1324 specialization of a member template.
1326 The TEMPLATE_COUNT is the number of references to qualifying
1327 template classes that appeared in the name of the function. See
1328 check_explicit_specialization for a more accurate description.
1330 The template args (those explicitly specified and those deduced)
1331 are output in a newly created vector *TARGS_OUT.
1333 If it is impossible to determine the result, an error message is
1334 issued. The error_mark_node is returned to indicate failure. */
1336 static tree
1337 determine_specialization (tree template_id,
1338 tree decl,
1339 tree* targs_out,
1340 int need_member_template,
1341 int template_count)
1343 tree fns;
1344 tree targs;
1345 tree explicit_targs;
1346 tree candidates = NULL_TREE;
1347 /* A TREE_LIST of templates of which DECL may be a specialization.
1348 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1349 corresponding TREE_PURPOSE is the set of template arguments that,
1350 when used to instantiate the template, would produce a function
1351 with the signature of DECL. */
1352 tree templates = NULL_TREE;
1353 int header_count;
1354 struct cp_binding_level *b;
1356 *targs_out = NULL_TREE;
1358 if (template_id == error_mark_node)
1359 return error_mark_node;
1361 fns = TREE_OPERAND (template_id, 0);
1362 explicit_targs = TREE_OPERAND (template_id, 1);
1364 if (fns == error_mark_node)
1365 return error_mark_node;
1367 /* Check for baselinks. */
1368 if (BASELINK_P (fns))
1369 fns = BASELINK_FUNCTIONS (fns);
1371 if (!is_overloaded_fn (fns))
1373 error ("%qD is not a function template", fns);
1374 return error_mark_node;
1377 /* Count the number of template headers specified for this
1378 specialization. */
1379 header_count = 0;
1380 for (b = current_binding_level;
1381 b->kind == sk_template_parms;
1382 b = b->level_chain)
1383 ++header_count;
1385 for (; fns; fns = OVL_NEXT (fns))
1387 tree fn = OVL_CURRENT (fns);
1389 if (TREE_CODE (fn) == TEMPLATE_DECL)
1391 tree decl_arg_types;
1392 tree fn_arg_types;
1394 /* DECL might be a specialization of FN. */
1396 /* Adjust the type of DECL in case FN is a static member. */
1397 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1398 if (DECL_STATIC_FUNCTION_P (fn)
1399 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1400 decl_arg_types = TREE_CHAIN (decl_arg_types);
1402 /* Check that the number of function parameters matches.
1403 For example,
1404 template <class T> void f(int i = 0);
1405 template <> void f<int>();
1406 The specialization f<int> is invalid but is not caught
1407 by get_bindings below. */
1409 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1410 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1411 continue;
1413 /* For a non-static member function, we need to make sure that
1414 the const qualification is the same. This can be done by
1415 checking the 'this' in the argument list. */
1416 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1417 && !same_type_p (TREE_VALUE (fn_arg_types),
1418 TREE_VALUE (decl_arg_types)))
1419 continue;
1421 /* In case of explicit specialization, we need to check if
1422 the number of template headers appearing in the specialization
1423 is correct. This is usually done in check_explicit_specialization,
1424 but the check done there cannot be exhaustive when specializing
1425 member functions. Consider the following code:
1427 template <> void A<int>::f(int);
1428 template <> template <> void A<int>::f(int);
1430 Assuming that A<int> is not itself an explicit specialization
1431 already, the first line specializes "f" which is a non-template
1432 member function, whilst the second line specializes "f" which
1433 is a template member function. So both lines are syntactically
1434 correct, and check_explicit_specialization does not reject
1435 them.
1437 Here, we can do better, as we are matching the specialization
1438 against the declarations. We count the number of template
1439 headers, and we check if they match TEMPLATE_COUNT + 1
1440 (TEMPLATE_COUNT is the number of qualifying template classes,
1441 plus there must be another header for the member template
1442 itself).
1444 Notice that if header_count is zero, this is not a
1445 specialization but rather a template instantiation, so there
1446 is no check we can perform here. */
1447 if (header_count && header_count != template_count + 1)
1448 continue;
1450 /* Check that the number of template arguments at the
1451 innermost level for DECL is the same as for FN. */
1452 if (current_binding_level->kind == sk_template_parms
1453 && !current_binding_level->explicit_spec_p
1454 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1455 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1456 (current_template_parms))))
1457 continue;
1459 /* See whether this function might be a specialization of this
1460 template. */
1461 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1463 if (!targs)
1464 /* We cannot deduce template arguments that when used to
1465 specialize TMPL will produce DECL. */
1466 continue;
1468 /* Save this template, and the arguments deduced. */
1469 templates = tree_cons (targs, fn, templates);
1471 else if (need_member_template)
1472 /* FN is an ordinary member function, and we need a
1473 specialization of a member template. */
1475 else if (TREE_CODE (fn) != FUNCTION_DECL)
1476 /* We can get IDENTIFIER_NODEs here in certain erroneous
1477 cases. */
1479 else if (!DECL_FUNCTION_MEMBER_P (fn))
1480 /* This is just an ordinary non-member function. Nothing can
1481 be a specialization of that. */
1483 else if (DECL_ARTIFICIAL (fn))
1484 /* Cannot specialize functions that are created implicitly. */
1486 else
1488 tree decl_arg_types;
1490 /* This is an ordinary member function. However, since
1491 we're here, we can assume it's enclosing class is a
1492 template class. For example,
1494 template <typename T> struct S { void f(); };
1495 template <> void S<int>::f() {}
1497 Here, S<int>::f is a non-template, but S<int> is a
1498 template class. If FN has the same type as DECL, we
1499 might be in business. */
1501 if (!DECL_TEMPLATE_INFO (fn))
1502 /* Its enclosing class is an explicit specialization
1503 of a template class. This is not a candidate. */
1504 continue;
1506 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1507 TREE_TYPE (TREE_TYPE (fn))))
1508 /* The return types differ. */
1509 continue;
1511 /* Adjust the type of DECL in case FN is a static member. */
1512 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1513 if (DECL_STATIC_FUNCTION_P (fn)
1514 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1515 decl_arg_types = TREE_CHAIN (decl_arg_types);
1517 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1518 decl_arg_types))
1519 /* They match! */
1520 candidates = tree_cons (NULL_TREE, fn, candidates);
1524 if (templates && TREE_CHAIN (templates))
1526 /* We have:
1528 [temp.expl.spec]
1530 It is possible for a specialization with a given function
1531 signature to be instantiated from more than one function
1532 template. In such cases, explicit specification of the
1533 template arguments must be used to uniquely identify the
1534 function template specialization being specialized.
1536 Note that here, there's no suggestion that we're supposed to
1537 determine which of the candidate templates is most
1538 specialized. However, we, also have:
1540 [temp.func.order]
1542 Partial ordering of overloaded function template
1543 declarations is used in the following contexts to select
1544 the function template to which a function template
1545 specialization refers:
1547 -- when an explicit specialization refers to a function
1548 template.
1550 So, we do use the partial ordering rules, at least for now.
1551 This extension can only serve to make invalid programs valid,
1552 so it's safe. And, there is strong anecdotal evidence that
1553 the committee intended the partial ordering rules to apply;
1554 the EDG front-end has that behavior, and John Spicer claims
1555 that the committee simply forgot to delete the wording in
1556 [temp.expl.spec]. */
1557 tree tmpl = most_specialized_instantiation (templates);
1558 if (tmpl != error_mark_node)
1560 templates = tmpl;
1561 TREE_CHAIN (templates) = NULL_TREE;
1565 if (templates == NULL_TREE && candidates == NULL_TREE)
1567 error ("template-id %qD for %q+D does not match any template "
1568 "declaration", template_id, decl);
1569 return error_mark_node;
1571 else if ((templates && TREE_CHAIN (templates))
1572 || (candidates && TREE_CHAIN (candidates))
1573 || (templates && candidates))
1575 error ("ambiguous template specialization %qD for %q+D",
1576 template_id, decl);
1577 chainon (candidates, templates);
1578 print_candidates (candidates);
1579 return error_mark_node;
1582 /* We have one, and exactly one, match. */
1583 if (candidates)
1585 /* It was a specialization of an ordinary member function in a
1586 template class. */
1587 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1588 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1591 /* It was a specialization of a template. */
1592 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1593 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1595 *targs_out = copy_node (targs);
1596 SET_TMPL_ARGS_LEVEL (*targs_out,
1597 TMPL_ARGS_DEPTH (*targs_out),
1598 TREE_PURPOSE (templates));
1600 else
1601 *targs_out = TREE_PURPOSE (templates);
1602 return TREE_VALUE (templates);
1605 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1606 but with the default argument values filled in from those in the
1607 TMPL_TYPES. */
1609 static tree
1610 copy_default_args_to_explicit_spec_1 (tree spec_types,
1611 tree tmpl_types)
1613 tree new_spec_types;
1615 if (!spec_types)
1616 return NULL_TREE;
1618 if (spec_types == void_list_node)
1619 return void_list_node;
1621 /* Substitute into the rest of the list. */
1622 new_spec_types =
1623 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1624 TREE_CHAIN (tmpl_types));
1626 /* Add the default argument for this parameter. */
1627 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1628 TREE_VALUE (spec_types),
1629 new_spec_types);
1632 /* DECL is an explicit specialization. Replicate default arguments
1633 from the template it specializes. (That way, code like:
1635 template <class T> void f(T = 3);
1636 template <> void f(double);
1637 void g () { f (); }
1639 works, as required.) An alternative approach would be to look up
1640 the correct default arguments at the call-site, but this approach
1641 is consistent with how implicit instantiations are handled. */
1643 static void
1644 copy_default_args_to_explicit_spec (tree decl)
1646 tree tmpl;
1647 tree spec_types;
1648 tree tmpl_types;
1649 tree new_spec_types;
1650 tree old_type;
1651 tree new_type;
1652 tree t;
1653 tree object_type = NULL_TREE;
1654 tree in_charge = NULL_TREE;
1655 tree vtt = NULL_TREE;
1657 /* See if there's anything we need to do. */
1658 tmpl = DECL_TI_TEMPLATE (decl);
1659 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1660 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1661 if (TREE_PURPOSE (t))
1662 break;
1663 if (!t)
1664 return;
1666 old_type = TREE_TYPE (decl);
1667 spec_types = TYPE_ARG_TYPES (old_type);
1669 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1671 /* Remove the this pointer, but remember the object's type for
1672 CV quals. */
1673 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1674 spec_types = TREE_CHAIN (spec_types);
1675 tmpl_types = TREE_CHAIN (tmpl_types);
1677 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1679 /* DECL may contain more parameters than TMPL due to the extra
1680 in-charge parameter in constructors and destructors. */
1681 in_charge = spec_types;
1682 spec_types = TREE_CHAIN (spec_types);
1684 if (DECL_HAS_VTT_PARM_P (decl))
1686 vtt = spec_types;
1687 spec_types = TREE_CHAIN (spec_types);
1691 /* Compute the merged default arguments. */
1692 new_spec_types =
1693 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1695 /* Compute the new FUNCTION_TYPE. */
1696 if (object_type)
1698 if (vtt)
1699 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1700 TREE_VALUE (vtt),
1701 new_spec_types);
1703 if (in_charge)
1704 /* Put the in-charge parameter back. */
1705 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1706 TREE_VALUE (in_charge),
1707 new_spec_types);
1709 new_type = build_method_type_directly (object_type,
1710 TREE_TYPE (old_type),
1711 new_spec_types);
1713 else
1714 new_type = build_function_type (TREE_TYPE (old_type),
1715 new_spec_types);
1716 new_type = cp_build_type_attribute_variant (new_type,
1717 TYPE_ATTRIBUTES (old_type));
1718 new_type = build_exception_variant (new_type,
1719 TYPE_RAISES_EXCEPTIONS (old_type));
1720 TREE_TYPE (decl) = new_type;
1723 /* Check to see if the function just declared, as indicated in
1724 DECLARATOR, and in DECL, is a specialization of a function
1725 template. We may also discover that the declaration is an explicit
1726 instantiation at this point.
1728 Returns DECL, or an equivalent declaration that should be used
1729 instead if all goes well. Issues an error message if something is
1730 amiss. Returns error_mark_node if the error is not easily
1731 recoverable.
1733 FLAGS is a bitmask consisting of the following flags:
1735 2: The function has a definition.
1736 4: The function is a friend.
1738 The TEMPLATE_COUNT is the number of references to qualifying
1739 template classes that appeared in the name of the function. For
1740 example, in
1742 template <class T> struct S { void f(); };
1743 void S<int>::f();
1745 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1746 classes are not counted in the TEMPLATE_COUNT, so that in
1748 template <class T> struct S {};
1749 template <> struct S<int> { void f(); }
1750 template <> void S<int>::f();
1752 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1753 invalid; there should be no template <>.)
1755 If the function is a specialization, it is marked as such via
1756 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1757 is set up correctly, and it is added to the list of specializations
1758 for that template. */
1760 tree
1761 check_explicit_specialization (tree declarator,
1762 tree decl,
1763 int template_count,
1764 int flags)
1766 int have_def = flags & 2;
1767 int is_friend = flags & 4;
1768 int specialization = 0;
1769 int explicit_instantiation = 0;
1770 int member_specialization = 0;
1771 tree ctype = DECL_CLASS_CONTEXT (decl);
1772 tree dname = DECL_NAME (decl);
1773 tmpl_spec_kind tsk;
1775 if (is_friend)
1777 if (!processing_specialization)
1778 tsk = tsk_none;
1779 else
1780 tsk = tsk_excessive_parms;
1782 else
1783 tsk = current_tmpl_spec_kind (template_count);
1785 switch (tsk)
1787 case tsk_none:
1788 if (processing_specialization)
1790 specialization = 1;
1791 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1793 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1795 if (is_friend)
1796 /* This could be something like:
1798 template <class T> void f(T);
1799 class S { friend void f<>(int); } */
1800 specialization = 1;
1801 else
1803 /* This case handles bogus declarations like template <>
1804 template <class T> void f<int>(); */
1806 error ("template-id %qD in declaration of primary template",
1807 declarator);
1808 return decl;
1811 break;
1813 case tsk_invalid_member_spec:
1814 /* The error has already been reported in
1815 check_specialization_scope. */
1816 return error_mark_node;
1818 case tsk_invalid_expl_inst:
1819 error ("template parameter list used in explicit instantiation");
1821 /* Fall through. */
1823 case tsk_expl_inst:
1824 if (have_def)
1825 error ("definition provided for explicit instantiation");
1827 explicit_instantiation = 1;
1828 break;
1830 case tsk_excessive_parms:
1831 case tsk_insufficient_parms:
1832 if (tsk == tsk_excessive_parms)
1833 error ("too many template parameter lists in declaration of %qD",
1834 decl);
1835 else if (template_header_count)
1836 error("too few template parameter lists in declaration of %qD", decl);
1837 else
1838 error("explicit specialization of %qD must be introduced by "
1839 "%<template <>%>", decl);
1841 /* Fall through. */
1842 case tsk_expl_spec:
1843 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1844 if (ctype)
1845 member_specialization = 1;
1846 else
1847 specialization = 1;
1848 break;
1850 case tsk_template:
1851 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1853 /* This case handles bogus declarations like template <>
1854 template <class T> void f<int>(); */
1856 if (uses_template_parms (declarator))
1857 error ("function template partial specialization %qD "
1858 "is not allowed", declarator);
1859 else
1860 error ("template-id %qD in declaration of primary template",
1861 declarator);
1862 return decl;
1865 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1866 /* This is a specialization of a member template, without
1867 specialization the containing class. Something like:
1869 template <class T> struct S {
1870 template <class U> void f (U);
1872 template <> template <class U> void S<int>::f(U) {}
1874 That's a specialization -- but of the entire template. */
1875 specialization = 1;
1876 break;
1878 default:
1879 gcc_unreachable ();
1882 if (specialization || member_specialization)
1884 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1885 for (; t; t = TREE_CHAIN (t))
1886 if (TREE_PURPOSE (t))
1888 pedwarn
1889 ("default argument specified in explicit specialization");
1890 break;
1894 if (specialization || member_specialization || explicit_instantiation)
1896 tree tmpl = NULL_TREE;
1897 tree targs = NULL_TREE;
1899 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1900 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1902 tree fns;
1904 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1905 if (ctype)
1906 fns = dname;
1907 else
1909 /* If there is no class context, the explicit instantiation
1910 must be at namespace scope. */
1911 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1913 /* Find the namespace binding, using the declaration
1914 context. */
1915 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1916 false, true);
1917 if (!fns || !is_overloaded_fn (fns))
1919 error ("%qD is not a template function", dname);
1920 fns = error_mark_node;
1922 else
1924 tree fn = OVL_CURRENT (fns);
1925 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1926 CP_DECL_CONTEXT (fn)))
1927 error ("%qD is not declared in %qD",
1928 decl, current_namespace);
1932 declarator = lookup_template_function (fns, NULL_TREE);
1935 if (declarator == error_mark_node)
1936 return error_mark_node;
1938 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1940 if (!explicit_instantiation)
1941 /* A specialization in class scope. This is invalid,
1942 but the error will already have been flagged by
1943 check_specialization_scope. */
1944 return error_mark_node;
1945 else
1947 /* It's not valid to write an explicit instantiation in
1948 class scope, e.g.:
1950 class C { template void f(); }
1952 This case is caught by the parser. However, on
1953 something like:
1955 template class C { void f(); };
1957 (which is invalid) we can get here. The error will be
1958 issued later. */
1962 return decl;
1964 else if (ctype != NULL_TREE
1965 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1966 IDENTIFIER_NODE))
1968 /* Find the list of functions in ctype that have the same
1969 name as the declared function. */
1970 tree name = TREE_OPERAND (declarator, 0);
1971 tree fns = NULL_TREE;
1972 int idx;
1974 if (constructor_name_p (name, ctype))
1976 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1978 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1979 : !CLASSTYPE_DESTRUCTORS (ctype))
1981 /* From [temp.expl.spec]:
1983 If such an explicit specialization for the member
1984 of a class template names an implicitly-declared
1985 special member function (clause _special_), the
1986 program is ill-formed.
1988 Similar language is found in [temp.explicit]. */
1989 error ("specialization of implicitly-declared special member function");
1990 return error_mark_node;
1993 name = is_constructor ? ctor_identifier : dtor_identifier;
1996 if (!DECL_CONV_FN_P (decl))
1998 idx = lookup_fnfields_1 (ctype, name);
1999 if (idx >= 0)
2000 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2002 else
2004 VEC(tree,gc) *methods;
2005 tree ovl;
2007 /* For a type-conversion operator, we cannot do a
2008 name-based lookup. We might be looking for `operator
2009 int' which will be a specialization of `operator T'.
2010 So, we find *all* the conversion operators, and then
2011 select from them. */
2012 fns = NULL_TREE;
2014 methods = CLASSTYPE_METHOD_VEC (ctype);
2015 if (methods)
2016 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2017 VEC_iterate (tree, methods, idx, ovl);
2018 ++idx)
2020 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2021 /* There are no more conversion functions. */
2022 break;
2024 /* Glue all these conversion functions together
2025 with those we already have. */
2026 for (; ovl; ovl = OVL_NEXT (ovl))
2027 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2031 if (fns == NULL_TREE)
2033 error ("no member function %qD declared in %qT", name, ctype);
2034 return error_mark_node;
2036 else
2037 TREE_OPERAND (declarator, 0) = fns;
2040 /* Figure out what exactly is being specialized at this point.
2041 Note that for an explicit instantiation, even one for a
2042 member function, we cannot tell apriori whether the
2043 instantiation is for a member template, or just a member
2044 function of a template class. Even if a member template is
2045 being instantiated, the member template arguments may be
2046 elided if they can be deduced from the rest of the
2047 declaration. */
2048 tmpl = determine_specialization (declarator, decl,
2049 &targs,
2050 member_specialization,
2051 template_count);
2053 if (!tmpl || tmpl == error_mark_node)
2054 /* We couldn't figure out what this declaration was
2055 specializing. */
2056 return error_mark_node;
2057 else
2059 tree gen_tmpl = most_general_template (tmpl);
2061 if (explicit_instantiation)
2063 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2064 is done by do_decl_instantiation later. */
2066 int arg_depth = TMPL_ARGS_DEPTH (targs);
2067 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2069 if (arg_depth > parm_depth)
2071 /* If TMPL is not the most general template (for
2072 example, if TMPL is a friend template that is
2073 injected into namespace scope), then there will
2074 be too many levels of TARGS. Remove some of them
2075 here. */
2076 int i;
2077 tree new_targs;
2079 new_targs = make_tree_vec (parm_depth);
2080 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2081 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2082 = TREE_VEC_ELT (targs, i);
2083 targs = new_targs;
2086 return instantiate_template (tmpl, targs, tf_error);
2089 /* If we thought that the DECL was a member function, but it
2090 turns out to be specializing a static member function,
2091 make DECL a static member function as well. */
2092 if (DECL_STATIC_FUNCTION_P (tmpl)
2093 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2094 revert_static_member_fn (decl);
2096 /* If this is a specialization of a member template of a
2097 template class. In we want to return the TEMPLATE_DECL,
2098 not the specialization of it. */
2099 if (tsk == tsk_template)
2101 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2102 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2103 if (have_def)
2105 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2106 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2107 = DECL_SOURCE_LOCATION (decl);
2108 /* We want to use the argument list specified in the
2109 definition, not in the original declaration. */
2110 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2111 = DECL_ARGUMENTS (decl);
2113 return tmpl;
2116 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2117 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2119 /* Inherit default function arguments from the template
2120 DECL is specializing. */
2121 copy_default_args_to_explicit_spec (decl);
2123 /* This specialization has the same protection as the
2124 template it specializes. */
2125 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2126 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2127 /* The specialization has the same visibility as the
2128 template it specializes. */
2129 if (DECL_VISIBILITY_SPECIFIED (gen_tmpl))
2131 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2132 DECL_VISIBILITY (decl) = DECL_VISIBILITY (gen_tmpl);
2134 /* If DECL is a friend declaration, declared using an
2135 unqualified name, the namespace associated with DECL may
2136 have been set incorrectly. For example, in:
2138 template <typename T> void f(T);
2139 namespace N {
2140 struct S { friend void f<int>(int); }
2143 we will have set the DECL_CONTEXT for the friend
2144 declaration to N, rather than to the global namespace. */
2145 if (DECL_NAMESPACE_SCOPE_P (decl))
2146 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2148 if (is_friend && !have_def)
2149 /* This is not really a declaration of a specialization.
2150 It's just the name of an instantiation. But, it's not
2151 a request for an instantiation, either. */
2152 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2153 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2154 /* This is indeed a specialization. In case of constructors
2155 and destructors, we need in-charge and not-in-charge
2156 versions in V3 ABI. */
2157 clone_function_decl (decl, /*update_method_vec_p=*/0);
2159 /* Register this specialization so that we can find it
2160 again. */
2161 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2165 return decl;
2168 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2169 parameters. These are represented in the same format used for
2170 DECL_TEMPLATE_PARMS. */
2173 comp_template_parms (tree parms1, tree parms2)
2175 tree p1;
2176 tree p2;
2178 if (parms1 == parms2)
2179 return 1;
2181 for (p1 = parms1, p2 = parms2;
2182 p1 != NULL_TREE && p2 != NULL_TREE;
2183 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2185 tree t1 = TREE_VALUE (p1);
2186 tree t2 = TREE_VALUE (p2);
2187 int i;
2189 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2190 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2192 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2193 return 0;
2195 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2197 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2198 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2200 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2201 return 0;
2203 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2204 continue;
2205 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2206 return 0;
2210 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2211 /* One set of parameters has more parameters lists than the
2212 other. */
2213 return 0;
2215 return 1;
2218 /* Complain if DECL shadows a template parameter.
2220 [temp.local]: A template-parameter shall not be redeclared within its
2221 scope (including nested scopes). */
2223 void
2224 check_template_shadow (tree decl)
2226 tree olddecl;
2228 /* If we're not in a template, we can't possibly shadow a template
2229 parameter. */
2230 if (!current_template_parms)
2231 return;
2233 /* Figure out what we're shadowing. */
2234 if (TREE_CODE (decl) == OVERLOAD)
2235 decl = OVL_CURRENT (decl);
2236 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2238 /* If there's no previous binding for this name, we're not shadowing
2239 anything, let alone a template parameter. */
2240 if (!olddecl)
2241 return;
2243 /* If we're not shadowing a template parameter, we're done. Note
2244 that OLDDECL might be an OVERLOAD (or perhaps even an
2245 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2246 node. */
2247 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2248 return;
2250 /* We check for decl != olddecl to avoid bogus errors for using a
2251 name inside a class. We check TPFI to avoid duplicate errors for
2252 inline member templates. */
2253 if (decl == olddecl
2254 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2255 return;
2257 error ("declaration of %q+#D", decl);
2258 error (" shadows template parm %q+#D", olddecl);
2261 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2262 ORIG_LEVEL, DECL, and TYPE. */
2264 static tree
2265 build_template_parm_index (int index,
2266 int level,
2267 int orig_level,
2268 tree decl,
2269 tree type)
2271 tree t = make_node (TEMPLATE_PARM_INDEX);
2272 TEMPLATE_PARM_IDX (t) = index;
2273 TEMPLATE_PARM_LEVEL (t) = level;
2274 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2275 TEMPLATE_PARM_DECL (t) = decl;
2276 TREE_TYPE (t) = type;
2277 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2278 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2279 TREE_READONLY (t) = TREE_READONLY (decl);
2281 return t;
2284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2287 new one is created. */
2289 static tree
2290 reduce_template_parm_level (tree index, tree type, int levels)
2292 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2293 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2294 != TEMPLATE_PARM_LEVEL (index) - levels))
2296 tree orig_decl = TEMPLATE_PARM_DECL (index);
2297 tree decl, t;
2299 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2300 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2301 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2302 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2303 DECL_ARTIFICIAL (decl) = 1;
2304 SET_DECL_TEMPLATE_PARM_P (decl);
2306 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2307 TEMPLATE_PARM_LEVEL (index) - levels,
2308 TEMPLATE_PARM_ORIG_LEVEL (index),
2309 decl, type);
2310 TEMPLATE_PARM_DESCENDANTS (index) = t;
2312 /* Template template parameters need this. */
2313 if (TREE_CODE (decl) != CONST_DECL)
2314 DECL_TEMPLATE_PARMS (decl)
2315 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2318 return TEMPLATE_PARM_DESCENDANTS (index);
2321 /* Process information from new template parameter NEXT and append it to the
2322 LIST being built. This new parameter is a non-type parameter iff
2323 IS_NON_TYPE is true. */
2325 tree
2326 process_template_parm (tree list, tree next, bool is_non_type)
2328 tree parm;
2329 tree decl = 0;
2330 tree defval;
2331 int idx;
2333 parm = next;
2334 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2335 defval = TREE_PURPOSE (parm);
2337 if (list)
2339 tree p = TREE_VALUE (tree_last (list));
2341 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2342 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2343 else
2344 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2345 ++idx;
2347 else
2348 idx = 0;
2350 if (is_non_type)
2352 parm = TREE_VALUE (parm);
2354 SET_DECL_TEMPLATE_PARM_P (parm);
2356 /* [temp.param]
2358 The top-level cv-qualifiers on the template-parameter are
2359 ignored when determining its type. */
2360 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2362 /* A template parameter is not modifiable. */
2363 TREE_CONSTANT (parm) = 1;
2364 TREE_INVARIANT (parm) = 1;
2365 TREE_READONLY (parm) = 1;
2366 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2367 TREE_TYPE (parm) = void_type_node;
2368 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2369 TREE_CONSTANT (decl) = 1;
2370 TREE_INVARIANT (decl) = 1;
2371 TREE_READONLY (decl) = 1;
2372 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2373 = build_template_parm_index (idx, processing_template_decl,
2374 processing_template_decl,
2375 decl, TREE_TYPE (parm));
2377 else
2379 tree t;
2380 parm = TREE_VALUE (TREE_VALUE (parm));
2382 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2384 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2385 /* This is for distinguishing between real templates and template
2386 template parameters */
2387 TREE_TYPE (parm) = t;
2388 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2389 decl = parm;
2391 else
2393 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2394 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2395 decl = build_decl (TYPE_DECL, parm, t);
2398 TYPE_NAME (t) = decl;
2399 TYPE_STUB_DECL (t) = decl;
2400 parm = decl;
2401 TEMPLATE_TYPE_PARM_INDEX (t)
2402 = build_template_parm_index (idx, processing_template_decl,
2403 processing_template_decl,
2404 decl, TREE_TYPE (parm));
2406 DECL_ARTIFICIAL (decl) = 1;
2407 SET_DECL_TEMPLATE_PARM_P (decl);
2408 pushdecl (decl);
2409 parm = build_tree_list (defval, parm);
2410 return chainon (list, parm);
2413 /* The end of a template parameter list has been reached. Process the
2414 tree list into a parameter vector, converting each parameter into a more
2415 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2416 as PARM_DECLs. */
2418 tree
2419 end_template_parm_list (tree parms)
2421 int nparms;
2422 tree parm, next;
2423 tree saved_parmlist = make_tree_vec (list_length (parms));
2425 current_template_parms
2426 = tree_cons (size_int (processing_template_decl),
2427 saved_parmlist, current_template_parms);
2429 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2431 next = TREE_CHAIN (parm);
2432 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2433 TREE_CHAIN (parm) = NULL_TREE;
2436 --processing_template_parmlist;
2438 return saved_parmlist;
2441 /* end_template_decl is called after a template declaration is seen. */
2443 void
2444 end_template_decl (void)
2446 reset_specialization ();
2448 if (! processing_template_decl)
2449 return;
2451 /* This matches the pushlevel in begin_template_parm_list. */
2452 finish_scope ();
2454 --processing_template_decl;
2455 current_template_parms = TREE_CHAIN (current_template_parms);
2458 /* Given a template argument vector containing the template PARMS.
2459 The innermost PARMS are given first. */
2461 static tree
2462 current_template_args (void)
2464 tree header;
2465 tree args = NULL_TREE;
2466 int length = TMPL_PARMS_DEPTH (current_template_parms);
2467 int l = length;
2469 /* If there is only one level of template parameters, we do not
2470 create a TREE_VEC of TREE_VECs. Instead, we return a single
2471 TREE_VEC containing the arguments. */
2472 if (length > 1)
2473 args = make_tree_vec (length);
2475 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2477 tree a = copy_node (TREE_VALUE (header));
2478 int i;
2480 TREE_TYPE (a) = NULL_TREE;
2481 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2483 tree t = TREE_VEC_ELT (a, i);
2485 /* T will be a list if we are called from within a
2486 begin/end_template_parm_list pair, but a vector directly
2487 if within a begin/end_member_template_processing pair. */
2488 if (TREE_CODE (t) == TREE_LIST)
2490 t = TREE_VALUE (t);
2492 if (TREE_CODE (t) == TYPE_DECL
2493 || TREE_CODE (t) == TEMPLATE_DECL)
2494 t = TREE_TYPE (t);
2495 else
2496 t = DECL_INITIAL (t);
2497 TREE_VEC_ELT (a, i) = t;
2501 if (length > 1)
2502 TREE_VEC_ELT (args, --l) = a;
2503 else
2504 args = a;
2507 return args;
2510 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2511 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2512 a member template. Used by push_template_decl below. */
2514 static tree
2515 build_template_decl (tree decl, tree parms, bool member_template_p)
2517 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2518 DECL_TEMPLATE_PARMS (tmpl) = parms;
2519 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2520 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2521 if (DECL_LANG_SPECIFIC (decl))
2523 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2524 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2525 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2526 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2527 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2528 if (DECL_OVERLOADED_OPERATOR_P (decl))
2529 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2530 DECL_OVERLOADED_OPERATOR_P (decl));
2533 return tmpl;
2536 struct template_parm_data
2538 /* The level of the template parameters we are currently
2539 processing. */
2540 int level;
2542 /* The index of the specialization argument we are currently
2543 processing. */
2544 int current_arg;
2546 /* An array whose size is the number of template parameters. The
2547 elements are nonzero if the parameter has been used in any one
2548 of the arguments processed so far. */
2549 int* parms;
2551 /* An array whose size is the number of template arguments. The
2552 elements are nonzero if the argument makes use of template
2553 parameters of this level. */
2554 int* arg_uses_template_parms;
2557 /* Subroutine of push_template_decl used to see if each template
2558 parameter in a partial specialization is used in the explicit
2559 argument list. If T is of the LEVEL given in DATA (which is
2560 treated as a template_parm_data*), then DATA->PARMS is marked
2561 appropriately. */
2563 static int
2564 mark_template_parm (tree t, void* data)
2566 int level;
2567 int idx;
2568 struct template_parm_data* tpd = (struct template_parm_data*) data;
2570 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2572 level = TEMPLATE_PARM_LEVEL (t);
2573 idx = TEMPLATE_PARM_IDX (t);
2575 else
2577 level = TEMPLATE_TYPE_LEVEL (t);
2578 idx = TEMPLATE_TYPE_IDX (t);
2581 if (level == tpd->level)
2583 tpd->parms[idx] = 1;
2584 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2587 /* Return zero so that for_each_template_parm will continue the
2588 traversal of the tree; we want to mark *every* template parm. */
2589 return 0;
2592 /* Process the partial specialization DECL. */
2594 static tree
2595 process_partial_specialization (tree decl)
2597 tree type = TREE_TYPE (decl);
2598 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2599 tree specargs = CLASSTYPE_TI_ARGS (type);
2600 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2601 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2602 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2603 int nargs = TREE_VEC_LENGTH (inner_args);
2604 int ntparms = TREE_VEC_LENGTH (inner_parms);
2605 int i;
2606 int did_error_intro = 0;
2607 struct template_parm_data tpd;
2608 struct template_parm_data tpd2;
2610 /* We check that each of the template parameters given in the
2611 partial specialization is used in the argument list to the
2612 specialization. For example:
2614 template <class T> struct S;
2615 template <class T> struct S<T*>;
2617 The second declaration is OK because `T*' uses the template
2618 parameter T, whereas
2620 template <class T> struct S<int>;
2622 is no good. Even trickier is:
2624 template <class T>
2625 struct S1
2627 template <class U>
2628 struct S2;
2629 template <class U>
2630 struct S2<T>;
2633 The S2<T> declaration is actually invalid; it is a
2634 full-specialization. Of course,
2636 template <class U>
2637 struct S2<T (*)(U)>;
2639 or some such would have been OK. */
2640 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2641 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2642 memset (tpd.parms, 0, sizeof (int) * ntparms);
2644 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2645 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2646 for (i = 0; i < nargs; ++i)
2648 tpd.current_arg = i;
2649 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2650 &mark_template_parm,
2651 &tpd,
2652 NULL);
2654 for (i = 0; i < ntparms; ++i)
2655 if (tpd.parms[i] == 0)
2657 /* One of the template parms was not used in the
2658 specialization. */
2659 if (!did_error_intro)
2661 error ("template parameters not used in partial specialization:");
2662 did_error_intro = 1;
2665 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2668 /* [temp.class.spec]
2670 The argument list of the specialization shall not be identical to
2671 the implicit argument list of the primary template. */
2672 if (comp_template_args
2673 (inner_args,
2674 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2675 (maintmpl)))))
2676 error ("partial specialization %qT does not specialize any template arguments", type);
2678 /* [temp.class.spec]
2680 A partially specialized non-type argument expression shall not
2681 involve template parameters of the partial specialization except
2682 when the argument expression is a simple identifier.
2684 The type of a template parameter corresponding to a specialized
2685 non-type argument shall not be dependent on a parameter of the
2686 specialization. */
2687 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2688 tpd2.parms = 0;
2689 for (i = 0; i < nargs; ++i)
2691 tree arg = TREE_VEC_ELT (inner_args, i);
2692 if (/* These first two lines are the `non-type' bit. */
2693 !TYPE_P (arg)
2694 && TREE_CODE (arg) != TEMPLATE_DECL
2695 /* This next line is the `argument expression is not just a
2696 simple identifier' condition and also the `specialized
2697 non-type argument' bit. */
2698 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2700 if (tpd.arg_uses_template_parms[i])
2701 error ("template argument %qE involves template parameter(s)", arg);
2702 else
2704 /* Look at the corresponding template parameter,
2705 marking which template parameters its type depends
2706 upon. */
2707 tree type =
2708 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2709 i)));
2711 if (!tpd2.parms)
2713 /* We haven't yet initialized TPD2. Do so now. */
2714 tpd2.arg_uses_template_parms
2715 = (int *) alloca (sizeof (int) * nargs);
2716 /* The number of parameters here is the number in the
2717 main template, which, as checked in the assertion
2718 above, is NARGS. */
2719 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2720 tpd2.level =
2721 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2724 /* Mark the template parameters. But this time, we're
2725 looking for the template parameters of the main
2726 template, not in the specialization. */
2727 tpd2.current_arg = i;
2728 tpd2.arg_uses_template_parms[i] = 0;
2729 memset (tpd2.parms, 0, sizeof (int) * nargs);
2730 for_each_template_parm (type,
2731 &mark_template_parm,
2732 &tpd2,
2733 NULL);
2735 if (tpd2.arg_uses_template_parms [i])
2737 /* The type depended on some template parameters.
2738 If they are fully specialized in the
2739 specialization, that's OK. */
2740 int j;
2741 for (j = 0; j < nargs; ++j)
2742 if (tpd2.parms[j] != 0
2743 && tpd.arg_uses_template_parms [j])
2745 error ("type %qT of template argument %qE depends "
2746 "on template parameter(s)",
2747 type,
2748 arg);
2749 break;
2756 if (retrieve_specialization (maintmpl, specargs,
2757 /*class_specializations_p=*/true))
2758 /* We've already got this specialization. */
2759 return decl;
2761 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2762 = tree_cons (specargs, inner_parms,
2763 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2764 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2765 return decl;
2768 /* Check that a template declaration's use of default arguments is not
2769 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2770 nonzero if DECL is the thing declared by a primary template.
2771 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2773 static void
2774 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2776 const char *msg;
2777 int last_level_to_check;
2778 tree parm_level;
2780 /* [temp.param]
2782 A default template-argument shall not be specified in a
2783 function template declaration or a function template definition, nor
2784 in the template-parameter-list of the definition of a member of a
2785 class template. */
2787 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2788 /* You can't have a function template declaration in a local
2789 scope, nor you can you define a member of a class template in a
2790 local scope. */
2791 return;
2793 if (current_class_type
2794 && !TYPE_BEING_DEFINED (current_class_type)
2795 && DECL_LANG_SPECIFIC (decl)
2796 /* If this is either a friend defined in the scope of the class
2797 or a member function. */
2798 && (DECL_FUNCTION_MEMBER_P (decl)
2799 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2800 : DECL_FRIEND_CONTEXT (decl)
2801 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2802 : false)
2803 /* And, if it was a member function, it really was defined in
2804 the scope of the class. */
2805 && (!DECL_FUNCTION_MEMBER_P (decl)
2806 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2807 /* We already checked these parameters when the template was
2808 declared, so there's no need to do it again now. This function
2809 was defined in class scope, but we're processing it's body now
2810 that the class is complete. */
2811 return;
2813 /* [temp.param]
2815 If a template-parameter has a default template-argument, all
2816 subsequent template-parameters shall have a default
2817 template-argument supplied. */
2818 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2820 tree inner_parms = TREE_VALUE (parm_level);
2821 int ntparms = TREE_VEC_LENGTH (inner_parms);
2822 int seen_def_arg_p = 0;
2823 int i;
2825 for (i = 0; i < ntparms; ++i)
2827 tree parm = TREE_VEC_ELT (inner_parms, i);
2828 if (TREE_PURPOSE (parm))
2829 seen_def_arg_p = 1;
2830 else if (seen_def_arg_p)
2832 error ("no default argument for %qD", TREE_VALUE (parm));
2833 /* For better subsequent error-recovery, we indicate that
2834 there should have been a default argument. */
2835 TREE_PURPOSE (parm) = error_mark_node;
2840 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2841 /* For an ordinary class template, default template arguments are
2842 allowed at the innermost level, e.g.:
2843 template <class T = int>
2844 struct S {};
2845 but, in a partial specialization, they're not allowed even
2846 there, as we have in [temp.class.spec]:
2848 The template parameter list of a specialization shall not
2849 contain default template argument values.
2851 So, for a partial specialization, or for a function template,
2852 we look at all of them. */
2854 else
2855 /* But, for a primary class template that is not a partial
2856 specialization we look at all template parameters except the
2857 innermost ones. */
2858 parms = TREE_CHAIN (parms);
2860 /* Figure out what error message to issue. */
2861 if (TREE_CODE (decl) == FUNCTION_DECL)
2862 msg = "default template arguments may not be used in function templates";
2863 else if (is_partial)
2864 msg = "default template arguments may not be used in partial specializations";
2865 else
2866 msg = "default argument for template parameter for class enclosing %qD";
2868 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2869 /* If we're inside a class definition, there's no need to
2870 examine the parameters to the class itself. On the one
2871 hand, they will be checked when the class is defined, and,
2872 on the other, default arguments are valid in things like:
2873 template <class T = double>
2874 struct S { template <class U> void f(U); };
2875 Here the default argument for `S' has no bearing on the
2876 declaration of `f'. */
2877 last_level_to_check = template_class_depth (current_class_type) + 1;
2878 else
2879 /* Check everything. */
2880 last_level_to_check = 0;
2882 for (parm_level = parms;
2883 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2884 parm_level = TREE_CHAIN (parm_level))
2886 tree inner_parms = TREE_VALUE (parm_level);
2887 int i;
2888 int ntparms;
2890 ntparms = TREE_VEC_LENGTH (inner_parms);
2891 for (i = 0; i < ntparms; ++i)
2892 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2894 if (msg)
2896 error (msg, decl);
2897 msg = 0;
2900 /* Clear out the default argument so that we are not
2901 confused later. */
2902 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2905 /* At this point, if we're still interested in issuing messages,
2906 they must apply to classes surrounding the object declared. */
2907 if (msg)
2908 msg = "default argument for template parameter for class enclosing %qD";
2912 /* Worker for push_template_decl_real, called via
2913 for_each_template_parm. DATA is really an int, indicating the
2914 level of the parameters we are interested in. If T is a template
2915 parameter of that level, return nonzero. */
2917 static int
2918 template_parm_this_level_p (tree t, void* data)
2920 int this_level = *(int *)data;
2921 int level;
2923 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2924 level = TEMPLATE_PARM_LEVEL (t);
2925 else
2926 level = TEMPLATE_TYPE_LEVEL (t);
2927 return level == this_level;
2930 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2931 parameters given by current_template_args, or reuses a
2932 previously existing one, if appropriate. Returns the DECL, or an
2933 equivalent one, if it is replaced via a call to duplicate_decls.
2935 If IS_FRIEND is true, DECL is a friend declaration. */
2937 tree
2938 push_template_decl_real (tree decl, bool is_friend)
2940 tree tmpl;
2941 tree args;
2942 tree info;
2943 tree ctx;
2944 int primary;
2945 int is_partial;
2946 int new_template_p = 0;
2947 /* True if the template is a member template, in the sense of
2948 [temp.mem]. */
2949 bool member_template_p = false;
2951 if (decl == error_mark_node)
2952 return decl;
2954 /* See if this is a partial specialization. */
2955 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2956 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2957 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2959 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
2960 is_friend = true;
2962 if (is_friend)
2963 /* For a friend, we want the context of the friend function, not
2964 the type of which it is a friend. */
2965 ctx = DECL_CONTEXT (decl);
2966 else if (CP_DECL_CONTEXT (decl)
2967 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2968 /* In the case of a virtual function, we want the class in which
2969 it is defined. */
2970 ctx = CP_DECL_CONTEXT (decl);
2971 else
2972 /* Otherwise, if we're currently defining some class, the DECL
2973 is assumed to be a member of the class. */
2974 ctx = current_scope ();
2976 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2977 ctx = NULL_TREE;
2979 if (!DECL_CONTEXT (decl))
2980 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2982 /* See if this is a primary template. */
2983 primary = template_parm_scope_p ();
2985 if (primary)
2987 if (DECL_CLASS_SCOPE_P (decl))
2988 member_template_p = true;
2989 if (TREE_CODE (decl) == TYPE_DECL
2990 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2991 error ("template class without a name");
2992 else if (TREE_CODE (decl) == FUNCTION_DECL)
2994 if (DECL_DESTRUCTOR_P (decl))
2996 /* [temp.mem]
2998 A destructor shall not be a member template. */
2999 error ("destructor %qD declared as member template", decl);
3000 return error_mark_node;
3002 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3003 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3004 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3005 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3006 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3007 == void_list_node)))
3009 /* [basic.stc.dynamic.allocation]
3011 An allocation function can be a function
3012 template. ... Template allocation functions shall
3013 have two or more parameters. */
3014 error ("invalid template declaration of %qD", decl);
3015 return decl;
3018 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3019 && CLASS_TYPE_P (TREE_TYPE (decl)))
3020 /* OK */;
3021 else
3023 error ("template declaration of %q#D", decl);
3024 return error_mark_node;
3028 /* Check to see that the rules regarding the use of default
3029 arguments are not being violated. */
3030 check_default_tmpl_args (decl, current_template_parms,
3031 primary, is_partial);
3033 if (is_partial)
3034 return process_partial_specialization (decl);
3036 args = current_template_args ();
3038 if (!ctx
3039 || TREE_CODE (ctx) == FUNCTION_DECL
3040 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3041 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3043 if (DECL_LANG_SPECIFIC (decl)
3044 && DECL_TEMPLATE_INFO (decl)
3045 && DECL_TI_TEMPLATE (decl))
3046 tmpl = DECL_TI_TEMPLATE (decl);
3047 /* If DECL is a TYPE_DECL for a class-template, then there won't
3048 be DECL_LANG_SPECIFIC. The information equivalent to
3049 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3050 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3051 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3052 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3054 /* Since a template declaration already existed for this
3055 class-type, we must be redeclaring it here. Make sure
3056 that the redeclaration is valid. */
3057 redeclare_class_template (TREE_TYPE (decl),
3058 current_template_parms);
3059 /* We don't need to create a new TEMPLATE_DECL; just use the
3060 one we already had. */
3061 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3063 else
3065 tmpl = build_template_decl (decl, current_template_parms,
3066 member_template_p);
3067 new_template_p = 1;
3069 if (DECL_LANG_SPECIFIC (decl)
3070 && DECL_TEMPLATE_SPECIALIZATION (decl))
3072 /* A specialization of a member template of a template
3073 class. */
3074 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3075 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3076 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3080 else
3082 tree a, t, current, parms;
3083 int i;
3085 if (TREE_CODE (decl) == TYPE_DECL)
3087 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3088 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3089 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3090 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3091 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3092 else
3094 error ("%qD does not declare a template type", decl);
3095 return decl;
3098 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3100 error ("template definition of non-template %q#D", decl);
3101 return decl;
3103 else
3104 tmpl = DECL_TI_TEMPLATE (decl);
3106 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3107 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3108 && DECL_TEMPLATE_SPECIALIZATION (decl)
3109 && DECL_MEMBER_TEMPLATE_P (tmpl))
3111 tree new_tmpl;
3113 /* The declaration is a specialization of a member
3114 template, declared outside the class. Therefore, the
3115 innermost template arguments will be NULL, so we
3116 replace them with the arguments determined by the
3117 earlier call to check_explicit_specialization. */
3118 args = DECL_TI_ARGS (decl);
3120 new_tmpl
3121 = build_template_decl (decl, current_template_parms,
3122 member_template_p);
3123 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3124 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3125 DECL_TI_TEMPLATE (decl) = new_tmpl;
3126 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3127 DECL_TEMPLATE_INFO (new_tmpl)
3128 = tree_cons (tmpl, args, NULL_TREE);
3130 register_specialization (new_tmpl,
3131 most_general_template (tmpl),
3132 args,
3133 is_friend);
3134 return decl;
3137 /* Make sure the template headers we got make sense. */
3139 parms = DECL_TEMPLATE_PARMS (tmpl);
3140 i = TMPL_PARMS_DEPTH (parms);
3141 if (TMPL_ARGS_DEPTH (args) != i)
3143 error ("expected %d levels of template parms for %q#D, got %d",
3144 i, decl, TMPL_ARGS_DEPTH (args));
3146 else
3147 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3149 a = TMPL_ARGS_LEVEL (args, i);
3150 t = INNERMOST_TEMPLATE_PARMS (parms);
3152 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3154 if (current == decl)
3155 error ("got %d template parameters for %q#D",
3156 TREE_VEC_LENGTH (a), decl);
3157 else
3158 error ("got %d template parameters for %q#T",
3159 TREE_VEC_LENGTH (a), current);
3160 error (" but %d required", TREE_VEC_LENGTH (t));
3161 return error_mark_node;
3164 /* Perhaps we should also check that the parms are used in the
3165 appropriate qualifying scopes in the declarator? */
3167 if (current == decl)
3168 current = ctx;
3169 else
3170 current = TYPE_CONTEXT (current);
3174 DECL_TEMPLATE_RESULT (tmpl) = decl;
3175 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3177 /* Push template declarations for global functions and types. Note
3178 that we do not try to push a global template friend declared in a
3179 template class; such a thing may well depend on the template
3180 parameters of the class. */
3181 if (new_template_p && !ctx
3182 && !(is_friend && template_class_depth (current_class_type) > 0))
3184 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3185 if (tmpl == error_mark_node)
3186 return error_mark_node;
3188 /* Hide template friend classes that haven't been declared yet. */
3189 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3191 DECL_ANTICIPATED (tmpl) = 1;
3192 DECL_FRIEND_P (tmpl) = 1;
3196 if (primary)
3198 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3199 if (DECL_CONV_FN_P (tmpl))
3201 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3203 /* It is a conversion operator. See if the type converted to
3204 depends on innermost template operands. */
3206 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3207 depth))
3208 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3212 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3213 back to its most general template. If TMPL is a specialization,
3214 ARGS may only have the innermost set of arguments. Add the missing
3215 argument levels if necessary. */
3216 if (DECL_TEMPLATE_INFO (tmpl))
3217 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3219 info = tree_cons (tmpl, args, NULL_TREE);
3221 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3223 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3224 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3225 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3226 /* Don't change the name if we've already set it up. */
3227 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3228 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3230 else if (DECL_LANG_SPECIFIC (decl))
3231 DECL_TEMPLATE_INFO (decl) = info;
3233 return DECL_TEMPLATE_RESULT (tmpl);
3236 tree
3237 push_template_decl (tree decl)
3239 return push_template_decl_real (decl, false);
3242 /* Called when a class template TYPE is redeclared with the indicated
3243 template PARMS, e.g.:
3245 template <class T> struct S;
3246 template <class T> struct S {}; */
3248 void
3249 redeclare_class_template (tree type, tree parms)
3251 tree tmpl;
3252 tree tmpl_parms;
3253 int i;
3255 if (!TYPE_TEMPLATE_INFO (type))
3257 error ("%qT is not a template type", type);
3258 return;
3261 tmpl = TYPE_TI_TEMPLATE (type);
3262 if (!PRIMARY_TEMPLATE_P (tmpl))
3263 /* The type is nested in some template class. Nothing to worry
3264 about here; there are no new template parameters for the nested
3265 type. */
3266 return;
3268 if (!parms)
3270 error ("template specifiers not specified in declaration of %qD",
3271 tmpl);
3272 return;
3275 parms = INNERMOST_TEMPLATE_PARMS (parms);
3276 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3278 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3280 error ("previous declaration %q+D", tmpl);
3281 error ("used %d template parameter(s) instead of %d",
3282 TREE_VEC_LENGTH (tmpl_parms),
3283 TREE_VEC_LENGTH (parms));
3284 return;
3287 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3289 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3290 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3291 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3292 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3294 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3295 TEMPLATE_DECL. */
3296 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3297 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3298 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3300 error ("template parameter %q+#D", tmpl_parm);
3301 error ("redeclared here as %q#D", parm);
3302 return;
3305 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3307 /* We have in [temp.param]:
3309 A template-parameter may not be given default arguments
3310 by two different declarations in the same scope. */
3311 error ("redefinition of default argument for %q#D", parm);
3312 error ("%J original definition appeared here", tmpl_parm);
3313 return;
3316 if (parm_default != NULL_TREE)
3317 /* Update the previous template parameters (which are the ones
3318 that will really count) with the new default value. */
3319 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3320 else if (tmpl_default != NULL_TREE)
3321 /* Update the new parameters, too; they'll be used as the
3322 parameters for any members. */
3323 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3327 /* Simplify EXPR if it is a non-dependent expression. Returns the
3328 (possibly simplified) expression. */
3330 tree
3331 fold_non_dependent_expr (tree expr)
3333 /* If we're in a template, but EXPR isn't value dependent, simplify
3334 it. We're supposed to treat:
3336 template <typename T> void f(T[1 + 1]);
3337 template <typename T> void f(T[2]);
3339 as two declarations of the same function, for example. */
3340 if (processing_template_decl
3341 && !type_dependent_expression_p (expr)
3342 && !value_dependent_expression_p (expr))
3344 HOST_WIDE_INT saved_processing_template_decl;
3346 saved_processing_template_decl = processing_template_decl;
3347 processing_template_decl = 0;
3348 expr = tsubst_copy_and_build (expr,
3349 /*args=*/NULL_TREE,
3350 tf_error,
3351 /*in_decl=*/NULL_TREE,
3352 /*function_p=*/false);
3353 processing_template_decl = saved_processing_template_decl;
3355 return expr;
3358 /* EXPR is an expression which is used in a constant-expression context.
3359 For instance, it could be a VAR_DECL with a constant initializer.
3360 Extract the innest constant expression.
3362 This is basically a more powerful version of
3363 integral_constant_value, which can be used also in templates where
3364 initializers can maintain a syntactic rather than semantic form
3365 (even if they are non-dependent, for access-checking purposes). */
3367 static tree
3368 fold_decl_constant_value (tree expr)
3370 tree const_expr = expr;
3373 expr = fold_non_dependent_expr (const_expr);
3374 const_expr = integral_constant_value (expr);
3376 while (expr != const_expr);
3378 return expr;
3381 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3382 must be a function or a pointer-to-function type, as specified
3383 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3384 and check that the resulting function has external linkage. */
3386 static tree
3387 convert_nontype_argument_function (tree type, tree expr)
3389 tree fns = expr;
3390 tree fn, fn_no_ptr;
3392 fn = instantiate_type (type, fns, tf_none);
3393 if (fn == error_mark_node)
3394 return error_mark_node;
3396 fn_no_ptr = fn;
3397 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3398 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3400 /* [temp.arg.nontype]/1
3402 A template-argument for a non-type, non-template template-parameter
3403 shall be one of:
3404 [...]
3405 -- the address of an object or function with external linkage. */
3406 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3408 error ("%qE is not a valid template argument for type %qT "
3409 "because function %qD has not external linkage",
3410 expr, type, fn_no_ptr);
3411 return NULL_TREE;
3414 return fn;
3417 /* Attempt to convert the non-type template parameter EXPR to the
3418 indicated TYPE. If the conversion is successful, return the
3419 converted value. If the conversion is unsuccessful, return
3420 NULL_TREE if we issued an error message, or error_mark_node if we
3421 did not. We issue error messages for out-and-out bad template
3422 parameters, but not simply because the conversion failed, since we
3423 might be just trying to do argument deduction. Both TYPE and EXPR
3424 must be non-dependent.
3426 The conversion follows the special rules described in
3427 [temp.arg.nontype], and it is much more strict than an implicit
3428 conversion.
3430 This function is called twice for each template argument (see
3431 lookup_template_class for a more accurate description of this
3432 problem). This means that we need to handle expressions which
3433 are not valid in a C++ source, but can be created from the
3434 first call (for instance, casts to perform conversions). These
3435 hacks can go away after we fix the double coercion problem. */
3437 static tree
3438 convert_nontype_argument (tree type, tree expr)
3440 tree expr_type;
3442 /* Detect immediately string literals as invalid non-type argument.
3443 This special-case is not needed for correctness (we would easily
3444 catch this later), but only to provide better diagnostic for this
3445 common user mistake. As suggested by DR 100, we do not mention
3446 linkage issues in the diagnostic as this is not the point. */
3447 if (TREE_CODE (expr) == STRING_CST)
3449 error ("%qE is not a valid template argument for type %qT "
3450 "because string literals can never be used in this context",
3451 expr, type);
3452 return NULL_TREE;
3455 /* If we are in a template, EXPR may be non-dependent, but still
3456 have a syntactic, rather than semantic, form. For example, EXPR
3457 might be a SCOPE_REF, rather than the VAR_DECL to which the
3458 SCOPE_REF refers. Preserving the qualifying scope is necessary
3459 so that access checking can be performed when the template is
3460 instantiated -- but here we need the resolved form so that we can
3461 convert the argument. */
3462 expr = fold_non_dependent_expr (expr);
3463 expr_type = TREE_TYPE (expr);
3465 /* HACK: Due to double coercion, we can get a
3466 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3467 which is the tree that we built on the first call (see
3468 below when coercing to reference to object or to reference to
3469 function). We just strip everything and get to the arg.
3470 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3471 for examples. */
3472 if (TREE_CODE (expr) == NOP_EXPR)
3474 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3476 /* ??? Maybe we could use convert_from_reference here, but we
3477 would need to relax its constraints because the NOP_EXPR
3478 could actually change the type to something more cv-qualified,
3479 and this is not folded by convert_from_reference. */
3480 tree addr = TREE_OPERAND (expr, 0);
3481 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3482 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3483 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3484 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3485 (TREE_TYPE (expr_type),
3486 TREE_TYPE (TREE_TYPE (addr))));
3488 expr = TREE_OPERAND (addr, 0);
3489 expr_type = TREE_TYPE (expr);
3492 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3493 parameter is a pointer to object, through decay and
3494 qualification conversion. Let's strip everything. */
3495 else if (TYPE_PTROBV_P (type))
3497 STRIP_NOPS (expr);
3498 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3499 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3500 /* Skip the ADDR_EXPR only if it is part of the decay for
3501 an array. Otherwise, it is part of the original argument
3502 in the source code. */
3503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3504 expr = TREE_OPERAND (expr, 0);
3505 expr_type = TREE_TYPE (expr);
3509 /* [temp.arg.nontype]/5, bullet 1
3511 For a non-type template-parameter of integral or enumeration type,
3512 integral promotions (_conv.prom_) and integral conversions
3513 (_conv.integral_) are applied. */
3514 if (INTEGRAL_TYPE_P (type))
3516 if (!INTEGRAL_TYPE_P (expr_type))
3517 return error_mark_node;
3519 expr = fold_decl_constant_value (expr);
3520 /* Notice that there are constant expressions like '4 % 0' which
3521 do not fold into integer constants. */
3522 if (TREE_CODE (expr) != INTEGER_CST)
3524 error ("%qE is not a valid template argument for type %qT "
3525 "because it is a non-constant expression", expr, type);
3526 return NULL_TREE;
3529 /* At this point, an implicit conversion does what we want,
3530 because we already know that the expression is of integral
3531 type. */
3532 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3533 if (expr == error_mark_node)
3534 return error_mark_node;
3536 /* Conversion was allowed: fold it to a bare integer constant. */
3537 expr = fold (expr);
3539 /* [temp.arg.nontype]/5, bullet 2
3541 For a non-type template-parameter of type pointer to object,
3542 qualification conversions (_conv.qual_) and the array-to-pointer
3543 conversion (_conv.array_) are applied. */
3544 else if (TYPE_PTROBV_P (type))
3546 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3548 A template-argument for a non-type, non-template template-parameter
3549 shall be one of: [...]
3551 -- the name of a non-type template-parameter;
3552 -- the address of an object or function with external linkage, [...]
3553 expressed as "& id-expression" where the & is optional if the name
3554 refers to a function or array, or if the corresponding
3555 template-parameter is a reference.
3557 Here, we do not care about functions, as they are invalid anyway
3558 for a parameter of type pointer-to-object. */
3559 bool constant_address_p =
3560 (TREE_CODE (expr) == ADDR_EXPR
3561 || TREE_CODE (expr_type) == ARRAY_TYPE
3562 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3564 expr = decay_conversion (expr);
3565 if (expr == error_mark_node)
3566 return error_mark_node;
3568 expr = perform_qualification_conversions (type, expr);
3569 if (expr == error_mark_node)
3570 return error_mark_node;
3572 if (!constant_address_p)
3574 error ("%qE is not a valid template argument for type %qT "
3575 "because it is not a constant pointer", expr, type);
3576 return NULL_TREE;
3579 /* [temp.arg.nontype]/5, bullet 3
3581 For a non-type template-parameter of type reference to object, no
3582 conversions apply. The type referred to by the reference may be more
3583 cv-qualified than the (otherwise identical) type of the
3584 template-argument. The template-parameter is bound directly to the
3585 template-argument, which must be an lvalue. */
3586 else if (TYPE_REF_OBJ_P (type))
3588 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3589 expr_type))
3590 return error_mark_node;
3592 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3594 error ("%qE is not a valid template argument for type %qT "
3595 "because of conflicts in cv-qualification", expr, type);
3596 return NULL_TREE;
3599 if (!real_lvalue_p (expr))
3601 error ("%qE is not a valid template argument for type %qT "
3602 "because it is not a lvalue", expr, type);
3603 return NULL_TREE;
3606 /* [temp.arg.nontype]/1
3608 A template-argument for a non-type, non-template template-parameter
3609 shall be one of: [...]
3611 -- the address of an object or function with external linkage. */
3612 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3614 error ("%qE is not a valid template argument for type %qT "
3615 "because object %qD has not external linkage",
3616 expr, type, expr);
3617 return NULL_TREE;
3620 expr = build_nop (type, build_address (expr));
3622 /* [temp.arg.nontype]/5, bullet 4
3624 For a non-type template-parameter of type pointer to function, only
3625 the function-to-pointer conversion (_conv.func_) is applied. If the
3626 template-argument represents a set of overloaded functions (or a
3627 pointer to such), the matching function is selected from the set
3628 (_over.over_). */
3629 else if (TYPE_PTRFN_P (type))
3631 /* If the argument is a template-id, we might not have enough
3632 context information to decay the pointer. */
3633 if (!type_unknown_p (expr_type))
3635 expr = decay_conversion (expr);
3636 if (expr == error_mark_node)
3637 return error_mark_node;
3640 expr = convert_nontype_argument_function (type, expr);
3641 if (!expr || expr == error_mark_node)
3642 return expr;
3644 /* [temp.arg.nontype]/5, bullet 5
3646 For a non-type template-parameter of type reference to function, no
3647 conversions apply. If the template-argument represents a set of
3648 overloaded functions, the matching function is selected from the set
3649 (_over.over_). */
3650 else if (TYPE_REFFN_P (type))
3652 if (TREE_CODE (expr) == ADDR_EXPR)
3654 error ("%qE is not a valid template argument for type %qT "
3655 "because it is a pointer", expr, type);
3656 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3657 return NULL_TREE;
3660 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3661 if (!expr || expr == error_mark_node)
3662 return expr;
3664 expr = build_nop (type, build_address (expr));
3666 /* [temp.arg.nontype]/5, bullet 6
3668 For a non-type template-parameter of type pointer to member function,
3669 no conversions apply. If the template-argument represents a set of
3670 overloaded member functions, the matching member function is selected
3671 from the set (_over.over_). */
3672 else if (TYPE_PTRMEMFUNC_P (type))
3674 expr = instantiate_type (type, expr, tf_none);
3675 if (expr == error_mark_node)
3676 return error_mark_node;
3678 /* There is no way to disable standard conversions in
3679 resolve_address_of_overloaded_function (called by
3680 instantiate_type). It is possible that the call succeeded by
3681 converting &B::I to &D::I (where B is a base of D), so we need
3682 to reject this conversion here.
3684 Actually, even if there was a way to disable standard conversions,
3685 it would still be better to reject them here so that we can
3686 provide a superior diagnostic. */
3687 if (!same_type_p (TREE_TYPE (expr), type))
3689 /* Make sure we are just one standard conversion off. */
3690 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3691 error ("%qE is not a valid template argument for type %qT "
3692 "because it is of type %qT", expr, type,
3693 TREE_TYPE (expr));
3694 inform ("standard conversions are not allowed in this context");
3695 return NULL_TREE;
3698 /* [temp.arg.nontype]/5, bullet 7
3700 For a non-type template-parameter of type pointer to data member,
3701 qualification conversions (_conv.qual_) are applied. */
3702 else if (TYPE_PTRMEM_P (type))
3704 expr = perform_qualification_conversions (type, expr);
3705 if (expr == error_mark_node)
3706 return expr;
3708 /* A template non-type parameter must be one of the above. */
3709 else
3710 gcc_unreachable ();
3712 /* Sanity check: did we actually convert the argument to the
3713 right type? */
3714 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3715 return expr;
3719 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3720 template template parameters. Both PARM_PARMS and ARG_PARMS are
3721 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3722 or PARM_DECL.
3724 Consider the example:
3725 template <class T> class A;
3726 template<template <class U> class TT> class B;
3728 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3729 the parameters to A, and OUTER_ARGS contains A. */
3731 static int
3732 coerce_template_template_parms (tree parm_parms,
3733 tree arg_parms,
3734 tsubst_flags_t complain,
3735 tree in_decl,
3736 tree outer_args)
3738 int nparms, nargs, i;
3739 tree parm, arg;
3741 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3742 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3744 nparms = TREE_VEC_LENGTH (parm_parms);
3745 nargs = TREE_VEC_LENGTH (arg_parms);
3747 if (nargs != nparms)
3748 return 0;
3750 for (i = 0; i < nparms; ++i)
3752 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3753 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3755 if (arg == NULL_TREE || arg == error_mark_node
3756 || parm == NULL_TREE || parm == error_mark_node)
3757 return 0;
3759 if (TREE_CODE (arg) != TREE_CODE (parm))
3760 return 0;
3762 switch (TREE_CODE (parm))
3764 case TYPE_DECL:
3765 break;
3767 case TEMPLATE_DECL:
3768 /* We encounter instantiations of templates like
3769 template <template <template <class> class> class TT>
3770 class C; */
3772 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3773 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3775 if (!coerce_template_template_parms
3776 (parmparm, argparm, complain, in_decl, outer_args))
3777 return 0;
3779 break;
3781 case PARM_DECL:
3782 /* The tsubst call is used to handle cases such as
3784 template <int> class C {};
3785 template <class T, template <T> class TT> class D {};
3786 D<int, C> d;
3788 i.e. the parameter list of TT depends on earlier parameters. */
3789 if (!dependent_type_p (TREE_TYPE (arg))
3790 && !same_type_p
3791 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3792 TREE_TYPE (arg)))
3793 return 0;
3794 break;
3796 default:
3797 gcc_unreachable ();
3800 return 1;
3803 /* Convert the indicated template ARG as necessary to match the
3804 indicated template PARM. Returns the converted ARG, or
3805 error_mark_node if the conversion was unsuccessful. Error and
3806 warning messages are issued under control of COMPLAIN. This
3807 conversion is for the Ith parameter in the parameter list. ARGS is
3808 the full set of template arguments deduced so far. */
3810 static tree
3811 convert_template_argument (tree parm,
3812 tree arg,
3813 tree args,
3814 tsubst_flags_t complain,
3815 int i,
3816 tree in_decl)
3818 tree val;
3819 tree inner_args;
3820 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3822 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3824 if (TREE_CODE (arg) == TREE_LIST
3825 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3827 /* The template argument was the name of some
3828 member function. That's usually
3829 invalid, but static members are OK. In any
3830 case, grab the underlying fields/functions
3831 and issue an error later if required. */
3832 arg = TREE_VALUE (arg);
3833 TREE_TYPE (arg) = unknown_type_node;
3836 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3837 requires_type = (TREE_CODE (parm) == TYPE_DECL
3838 || requires_tmpl_type);
3840 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3841 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3842 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3843 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3845 if (is_tmpl_type
3846 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3847 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3848 arg = TYPE_STUB_DECL (arg);
3850 is_type = TYPE_P (arg) || is_tmpl_type;
3852 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3853 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3855 pedwarn ("to refer to a type member of a template parameter, "
3856 "use %<typename %E%>", arg);
3858 arg = make_typename_type (TREE_OPERAND (arg, 0),
3859 TREE_OPERAND (arg, 1),
3860 typename_type,
3861 complain & tf_error);
3862 is_type = 1;
3864 if (is_type != requires_type)
3866 if (in_decl)
3868 if (complain & tf_error)
3870 error ("type/value mismatch at argument %d in template "
3871 "parameter list for %qD",
3872 i + 1, in_decl);
3873 if (is_type)
3874 error (" expected a constant of type %qT, got %qT",
3875 TREE_TYPE (parm),
3876 (is_tmpl_type ? DECL_NAME (arg) : arg));
3877 else if (requires_tmpl_type)
3878 error (" expected a class template, got %qE", arg);
3879 else
3880 error (" expected a type, got %qE", arg);
3883 return error_mark_node;
3885 if (is_tmpl_type ^ requires_tmpl_type)
3887 if (in_decl && (complain & tf_error))
3889 error ("type/value mismatch at argument %d in template "
3890 "parameter list for %qD",
3891 i + 1, in_decl);
3892 if (is_tmpl_type)
3893 error (" expected a type, got %qT", DECL_NAME (arg));
3894 else
3895 error (" expected a class template, got %qT", arg);
3897 return error_mark_node;
3900 if (is_type)
3902 if (requires_tmpl_type)
3904 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3905 /* The number of argument required is not known yet.
3906 Just accept it for now. */
3907 val = TREE_TYPE (arg);
3908 else
3910 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3911 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3913 if (coerce_template_template_parms (parmparm, argparm,
3914 complain, in_decl,
3915 inner_args))
3917 val = arg;
3919 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3920 TEMPLATE_DECL. */
3921 if (val != error_mark_node
3922 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3923 val = TREE_TYPE (val);
3925 else
3927 if (in_decl && (complain & tf_error))
3929 error ("type/value mismatch at argument %d in "
3930 "template parameter list for %qD",
3931 i + 1, in_decl);
3932 error (" expected a template of type %qD, got %qD",
3933 parm, arg);
3936 val = error_mark_node;
3940 else
3941 val = arg;
3942 /* We only form one instance of each template specialization.
3943 Therefore, if we use a non-canonical variant (i.e., a
3944 typedef), any future messages referring to the type will use
3945 the typedef, which is confusing if those future uses do not
3946 themselves also use the typedef. */
3947 if (TYPE_P (val))
3948 val = canonical_type_variant (val);
3950 else
3952 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3954 if (invalid_nontype_parm_type_p (t, complain))
3955 return error_mark_node;
3957 if (!uses_template_parms (arg) && !uses_template_parms (t))
3958 /* We used to call digest_init here. However, digest_init
3959 will report errors, which we don't want when complain
3960 is zero. More importantly, digest_init will try too
3961 hard to convert things: for example, `0' should not be
3962 converted to pointer type at this point according to
3963 the standard. Accepting this is not merely an
3964 extension, since deciding whether or not these
3965 conversions can occur is part of determining which
3966 function template to call, or whether a given explicit
3967 argument specification is valid. */
3968 val = convert_nontype_argument (t, arg);
3969 else
3970 val = arg;
3972 if (val == NULL_TREE)
3973 val = error_mark_node;
3974 else if (val == error_mark_node && (complain & tf_error))
3975 error ("could not convert template argument %qE to %qT", arg, t);
3978 return val;
3981 /* Convert all template arguments to their appropriate types, and
3982 return a vector containing the innermost resulting template
3983 arguments. If any error occurs, return error_mark_node. Error and
3984 warning messages are issued under control of COMPLAIN.
3986 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
3987 for arugments not specified in ARGS. Otherwise, if
3988 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
3989 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
3990 USE_DEFAULT_ARGS is false, then all arguments must be specified in
3991 ARGS. */
3993 static tree
3994 coerce_template_parms (tree parms,
3995 tree args,
3996 tree in_decl,
3997 tsubst_flags_t complain,
3998 bool require_all_args,
3999 bool use_default_args)
4001 int nparms, nargs, i, lost = 0;
4002 tree inner_args;
4003 tree new_args;
4004 tree new_inner_args;
4006 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4007 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4008 nparms = TREE_VEC_LENGTH (parms);
4010 if (nargs > nparms
4011 || (nargs < nparms
4012 && require_all_args
4013 && (!use_default_args
4014 || !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
4016 if (complain & tf_error)
4018 error ("wrong number of template arguments (%d, should be %d)",
4019 nargs, nparms);
4021 if (in_decl)
4022 error ("provided for %q+D", in_decl);
4025 return error_mark_node;
4028 new_inner_args = make_tree_vec (nparms);
4029 new_args = add_outermost_template_args (args, new_inner_args);
4030 for (i = 0; i < nparms; i++)
4032 tree arg;
4033 tree parm;
4035 /* Get the Ith template parameter. */
4036 parm = TREE_VEC_ELT (parms, i);
4038 /* Calculate the Ith argument. */
4039 if (i < nargs)
4040 arg = TREE_VEC_ELT (inner_args, i);
4041 else if (require_all_args)
4042 /* There must be a default arg in this case. */
4043 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4044 complain, in_decl);
4045 else
4046 break;
4048 gcc_assert (arg);
4049 if (arg == error_mark_node)
4051 if (complain & tf_error)
4052 error ("template argument %d is invalid", i + 1);
4054 else
4055 arg = convert_template_argument (TREE_VALUE (parm),
4056 arg, new_args, complain, i,
4057 in_decl);
4059 if (arg == error_mark_node)
4060 lost++;
4061 TREE_VEC_ELT (new_inner_args, i) = arg;
4064 if (lost)
4065 return error_mark_node;
4067 return new_inner_args;
4070 /* Returns 1 if template args OT and NT are equivalent. */
4072 static int
4073 template_args_equal (tree ot, tree nt)
4075 if (nt == ot)
4076 return 1;
4078 if (TREE_CODE (nt) == TREE_VEC)
4079 /* For member templates */
4080 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4081 else if (TYPE_P (nt))
4082 return TYPE_P (ot) && same_type_p (ot, nt);
4083 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4084 return 0;
4085 else
4086 return cp_tree_equal (ot, nt);
4089 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4090 of template arguments. Returns 0 otherwise. */
4093 comp_template_args (tree oldargs, tree newargs)
4095 int i;
4097 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4098 return 0;
4100 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4102 tree nt = TREE_VEC_ELT (newargs, i);
4103 tree ot = TREE_VEC_ELT (oldargs, i);
4105 if (! template_args_equal (ot, nt))
4106 return 0;
4108 return 1;
4111 /* Given class template name and parameter list, produce a user-friendly name
4112 for the instantiation. */
4114 static char *
4115 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4117 static struct obstack scratch_obstack;
4118 static char *scratch_firstobj;
4119 int i, nparms;
4121 if (!scratch_firstobj)
4122 gcc_obstack_init (&scratch_obstack);
4123 else
4124 obstack_free (&scratch_obstack, scratch_firstobj);
4125 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4127 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4128 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4130 cat (name);
4131 ccat ('<');
4132 nparms = TREE_VEC_LENGTH (parms);
4133 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4134 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4135 for (i = 0; i < nparms; i++)
4137 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4138 tree arg = TREE_VEC_ELT (arglist, i);
4140 if (i)
4141 ccat (',');
4143 if (TREE_CODE (parm) == TYPE_DECL)
4145 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4146 continue;
4148 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4150 if (TREE_CODE (arg) == TEMPLATE_DECL)
4152 /* Already substituted with real template. Just output
4153 the template name here */
4154 tree context = DECL_CONTEXT (arg);
4155 if (context)
4157 /* The template may be defined in a namespace, or
4158 may be a member template. */
4159 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4160 || CLASS_TYPE_P (context));
4161 cat (decl_as_string (DECL_CONTEXT (arg),
4162 TFF_PLAIN_IDENTIFIER));
4163 cat ("::");
4165 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4167 else
4168 /* Output the parameter declaration. */
4169 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4170 continue;
4172 else
4173 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4175 /* No need to check arglist against parmlist here; we did that
4176 in coerce_template_parms, called from lookup_template_class. */
4177 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4180 char *bufp = obstack_next_free (&scratch_obstack);
4181 int offset = 0;
4182 while (bufp[offset - 1] == ' ')
4183 offset--;
4184 obstack_blank_fast (&scratch_obstack, offset);
4186 /* B<C<char> >, not B<C<char>> */
4187 if (bufp[offset - 1] == '>')
4188 ccat (' ');
4190 ccat ('>');
4191 ccat ('\0');
4192 return (char *) obstack_base (&scratch_obstack);
4195 static tree
4196 classtype_mangled_name (tree t)
4198 if (CLASSTYPE_TEMPLATE_INFO (t)
4199 /* Specializations have already had their names set up in
4200 lookup_template_class. */
4201 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4203 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4205 /* For non-primary templates, the template parameters are
4206 implicit from their surrounding context. */
4207 if (PRIMARY_TEMPLATE_P (tmpl))
4209 tree name = DECL_NAME (tmpl);
4210 char *mangled_name = mangle_class_name_for_template
4211 (IDENTIFIER_POINTER (name),
4212 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4213 CLASSTYPE_TI_ARGS (t));
4214 tree id = get_identifier (mangled_name);
4215 IDENTIFIER_TEMPLATE (id) = name;
4216 return id;
4220 return TYPE_IDENTIFIER (t);
4223 static void
4224 add_pending_template (tree d)
4226 tree ti = (TYPE_P (d)
4227 ? CLASSTYPE_TEMPLATE_INFO (d)
4228 : DECL_TEMPLATE_INFO (d));
4229 tree pt;
4230 int level;
4232 if (TI_PENDING_TEMPLATE_FLAG (ti))
4233 return;
4235 /* We are called both from instantiate_decl, where we've already had a
4236 tinst_level pushed, and instantiate_template, where we haven't.
4237 Compensate. */
4238 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4240 if (level)
4241 push_tinst_level (d);
4243 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4244 if (last_pending_template)
4245 TREE_CHAIN (last_pending_template) = pt;
4246 else
4247 pending_templates = pt;
4249 last_pending_template = pt;
4251 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4253 if (level)
4254 pop_tinst_level ();
4258 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4259 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4260 documentation for TEMPLATE_ID_EXPR. */
4262 tree
4263 lookup_template_function (tree fns, tree arglist)
4265 tree type;
4267 if (fns == error_mark_node || arglist == error_mark_node)
4268 return error_mark_node;
4270 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4271 gcc_assert (fns && (is_overloaded_fn (fns)
4272 || TREE_CODE (fns) == IDENTIFIER_NODE));
4274 if (BASELINK_P (fns))
4276 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4277 unknown_type_node,
4278 BASELINK_FUNCTIONS (fns),
4279 arglist);
4280 return fns;
4283 type = TREE_TYPE (fns);
4284 if (TREE_CODE (fns) == OVERLOAD || !type)
4285 type = unknown_type_node;
4287 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4290 /* Within the scope of a template class S<T>, the name S gets bound
4291 (in build_self_reference) to a TYPE_DECL for the class, not a
4292 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4293 or one of its enclosing classes, and that type is a template,
4294 return the associated TEMPLATE_DECL. Otherwise, the original
4295 DECL is returned. */
4297 tree
4298 maybe_get_template_decl_from_type_decl (tree decl)
4300 return (decl != NULL_TREE
4301 && TREE_CODE (decl) == TYPE_DECL
4302 && DECL_ARTIFICIAL (decl)
4303 && CLASS_TYPE_P (TREE_TYPE (decl))
4304 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4305 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4308 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4309 parameters, find the desired type.
4311 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4313 IN_DECL, if non-NULL, is the template declaration we are trying to
4314 instantiate.
4316 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4317 the class we are looking up.
4319 Issue error and warning messages under control of COMPLAIN.
4321 If the template class is really a local class in a template
4322 function, then the FUNCTION_CONTEXT is the function in which it is
4323 being instantiated.
4325 ??? Note that this function is currently called *twice* for each
4326 template-id: the first time from the parser, while creating the
4327 incomplete type (finish_template_type), and the second type during the
4328 real instantiation (instantiate_template_class). This is surely something
4329 that we want to avoid. It also causes some problems with argument
4330 coercion (see convert_nontype_argument for more information on this). */
4332 tree
4333 lookup_template_class (tree d1,
4334 tree arglist,
4335 tree in_decl,
4336 tree context,
4337 int entering_scope,
4338 tsubst_flags_t complain)
4340 tree template = NULL_TREE, parmlist;
4341 tree t;
4343 timevar_push (TV_NAME_LOOKUP);
4345 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4347 tree value = innermost_non_namespace_value (d1);
4348 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4349 template = value;
4350 else
4352 if (context)
4353 push_decl_namespace (context);
4354 template = lookup_name (d1);
4355 template = maybe_get_template_decl_from_type_decl (template);
4356 if (context)
4357 pop_decl_namespace ();
4359 if (template)
4360 context = DECL_CONTEXT (template);
4362 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4364 tree type = TREE_TYPE (d1);
4366 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4367 an implicit typename for the second A. Deal with it. */
4368 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4369 type = TREE_TYPE (type);
4371 if (CLASSTYPE_TEMPLATE_INFO (type))
4373 template = CLASSTYPE_TI_TEMPLATE (type);
4374 d1 = DECL_NAME (template);
4377 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4378 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4380 template = TYPE_TI_TEMPLATE (d1);
4381 d1 = DECL_NAME (template);
4383 else if (TREE_CODE (d1) == TEMPLATE_DECL
4384 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4386 template = d1;
4387 d1 = DECL_NAME (template);
4388 context = DECL_CONTEXT (template);
4391 /* Issue an error message if we didn't find a template. */
4392 if (! template)
4394 if (complain & tf_error)
4395 error ("%qT is not a template", d1);
4396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4399 if (TREE_CODE (template) != TEMPLATE_DECL
4400 /* Make sure it's a user visible template, if it was named by
4401 the user. */
4402 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4403 && !PRIMARY_TEMPLATE_P (template)))
4405 if (complain & tf_error)
4407 error ("non-template type %qT used as a template", d1);
4408 if (in_decl)
4409 error ("for template declaration %q+D", in_decl);
4411 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4414 complain &= ~tf_user;
4416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4418 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4419 template arguments */
4421 tree parm;
4422 tree arglist2;
4424 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4426 /* Consider an example where a template template parameter declared as
4428 template <class T, class U = std::allocator<T> > class TT
4430 The template parameter level of T and U are one level larger than
4431 of TT. To proper process the default argument of U, say when an
4432 instantiation `TT<int>' is seen, we need to build the full
4433 arguments containing {int} as the innermost level. Outer levels,
4434 available when not appearing as default template argument, can be
4435 obtained from `current_template_args ()'.
4437 Suppose that TT is later substituted with std::vector. The above
4438 instantiation is `TT<int, std::allocator<T> >' with TT at
4439 level 1, and T at level 2, while the template arguments at level 1
4440 becomes {std::vector} and the inner level 2 is {int}. */
4442 if (current_template_parms)
4443 arglist = add_to_template_args (current_template_args (), arglist);
4445 arglist2 = coerce_template_parms (parmlist, arglist, template,
4446 complain,
4447 /*require_all_args=*/true,
4448 /*use_default_args=*/true);
4449 if (arglist2 == error_mark_node
4450 || (!uses_template_parms (arglist2)
4451 && check_instantiated_args (template, arglist2, complain)))
4452 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4454 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4455 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4457 else
4459 tree template_type = TREE_TYPE (template);
4460 tree gen_tmpl;
4461 tree type_decl;
4462 tree found = NULL_TREE;
4463 int arg_depth;
4464 int parm_depth;
4465 int is_partial_instantiation;
4467 gen_tmpl = most_general_template (template);
4468 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4469 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4470 arg_depth = TMPL_ARGS_DEPTH (arglist);
4472 if (arg_depth == 1 && parm_depth > 1)
4474 /* We've been given an incomplete set of template arguments.
4475 For example, given:
4477 template <class T> struct S1 {
4478 template <class U> struct S2 {};
4479 template <class U> struct S2<U*> {};
4482 we will be called with an ARGLIST of `U*', but the
4483 TEMPLATE will be `template <class T> template
4484 <class U> struct S1<T>::S2'. We must fill in the missing
4485 arguments. */
4486 arglist
4487 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4488 arglist);
4489 arg_depth = TMPL_ARGS_DEPTH (arglist);
4492 /* Now we should have enough arguments. */
4493 gcc_assert (parm_depth == arg_depth);
4495 /* From here on, we're only interested in the most general
4496 template. */
4497 template = gen_tmpl;
4499 /* Calculate the BOUND_ARGS. These will be the args that are
4500 actually tsubst'd into the definition to create the
4501 instantiation. */
4502 if (parm_depth > 1)
4504 /* We have multiple levels of arguments to coerce, at once. */
4505 int i;
4506 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4508 tree bound_args = make_tree_vec (parm_depth);
4510 for (i = saved_depth,
4511 t = DECL_TEMPLATE_PARMS (template);
4512 i > 0 && t != NULL_TREE;
4513 --i, t = TREE_CHAIN (t))
4515 tree a = coerce_template_parms (TREE_VALUE (t),
4516 arglist, template,
4517 complain,
4518 /*require_all_args=*/true,
4519 /*use_default_args=*/true);
4521 /* Don't process further if one of the levels fails. */
4522 if (a == error_mark_node)
4524 /* Restore the ARGLIST to its full size. */
4525 TREE_VEC_LENGTH (arglist) = saved_depth;
4526 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4529 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4531 /* We temporarily reduce the length of the ARGLIST so
4532 that coerce_template_parms will see only the arguments
4533 corresponding to the template parameters it is
4534 examining. */
4535 TREE_VEC_LENGTH (arglist)--;
4538 /* Restore the ARGLIST to its full size. */
4539 TREE_VEC_LENGTH (arglist) = saved_depth;
4541 arglist = bound_args;
4543 else
4544 arglist
4545 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4546 INNERMOST_TEMPLATE_ARGS (arglist),
4547 template,
4548 complain,
4549 /*require_all_args=*/true,
4550 /*use_default_args=*/true);
4552 if (arglist == error_mark_node)
4553 /* We were unable to bind the arguments. */
4554 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4556 /* In the scope of a template class, explicit references to the
4557 template class refer to the type of the template, not any
4558 instantiation of it. For example, in:
4560 template <class T> class C { void f(C<T>); }
4562 the `C<T>' is just the same as `C'. Outside of the
4563 class, however, such a reference is an instantiation. */
4564 if (comp_template_args (TYPE_TI_ARGS (template_type),
4565 arglist))
4567 found = template_type;
4569 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4571 tree ctx;
4573 for (ctx = current_class_type;
4574 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4575 ctx = (TYPE_P (ctx)
4576 ? TYPE_CONTEXT (ctx)
4577 : DECL_CONTEXT (ctx)))
4578 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4579 goto found_ctx;
4581 /* We're not in the scope of the class, so the
4582 TEMPLATE_TYPE is not the type we want after all. */
4583 found = NULL_TREE;
4584 found_ctx:;
4587 if (found)
4588 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4590 /* If we already have this specialization, return it. */
4591 found = retrieve_specialization (template, arglist,
4592 /*class_specializations_p=*/false);
4593 if (found)
4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4596 /* This type is a "partial instantiation" if any of the template
4597 arguments still involve template parameters. Note that we set
4598 IS_PARTIAL_INSTANTIATION for partial specializations as
4599 well. */
4600 is_partial_instantiation = uses_template_parms (arglist);
4602 /* If the deduced arguments are invalid, then the binding
4603 failed. */
4604 if (!is_partial_instantiation
4605 && check_instantiated_args (template,
4606 INNERMOST_TEMPLATE_ARGS (arglist),
4607 complain))
4608 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4610 if (!is_partial_instantiation
4611 && !PRIMARY_TEMPLATE_P (template)
4612 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4614 found = xref_tag_from_type (TREE_TYPE (template),
4615 DECL_NAME (template),
4616 /*tag_scope=*/ts_global);
4617 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4620 context = tsubst (DECL_CONTEXT (template), arglist,
4621 complain, in_decl);
4622 if (!context)
4623 context = global_namespace;
4625 /* Create the type. */
4626 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4628 if (!is_partial_instantiation)
4630 set_current_access_from_decl (TYPE_NAME (template_type));
4631 t = start_enum (TYPE_IDENTIFIER (template_type));
4633 else
4634 /* We don't want to call start_enum for this type, since
4635 the values for the enumeration constants may involve
4636 template parameters. And, no one should be interested
4637 in the enumeration constants for such a type. */
4638 t = make_node (ENUMERAL_TYPE);
4640 else
4642 t = make_aggr_type (TREE_CODE (template_type));
4643 CLASSTYPE_DECLARED_CLASS (t)
4644 = CLASSTYPE_DECLARED_CLASS (template_type);
4645 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4646 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4648 /* A local class. Make sure the decl gets registered properly. */
4649 if (context == current_function_decl)
4650 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4653 /* If we called start_enum or pushtag above, this information
4654 will already be set up. */
4655 if (!TYPE_NAME (t))
4657 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4659 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4660 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4661 TYPE_STUB_DECL (t) = type_decl;
4662 DECL_SOURCE_LOCATION (type_decl)
4663 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4665 else
4666 type_decl = TYPE_NAME (t);
4668 TREE_PRIVATE (type_decl)
4669 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4670 TREE_PROTECTED (type_decl)
4671 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4672 DECL_IN_SYSTEM_HEADER (type_decl)
4673 = DECL_IN_SYSTEM_HEADER (template);
4674 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4676 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4677 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4680 /* Set up the template information. We have to figure out which
4681 template is the immediate parent if this is a full
4682 instantiation. */
4683 if (parm_depth == 1 || is_partial_instantiation
4684 || !PRIMARY_TEMPLATE_P (template))
4685 /* This case is easy; there are no member templates involved. */
4686 found = template;
4687 else
4689 /* This is a full instantiation of a member template. Look
4690 for a partial instantiation of which this is an instance. */
4692 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4693 found; found = TREE_CHAIN (found))
4695 int success;
4696 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4698 /* We only want partial instantiations, here, not
4699 specializations or full instantiations. */
4700 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4701 || !uses_template_parms (TREE_VALUE (found)))
4702 continue;
4704 /* Temporarily reduce by one the number of levels in the
4705 ARGLIST and in FOUND so as to avoid comparing the
4706 last set of arguments. */
4707 TREE_VEC_LENGTH (arglist)--;
4708 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4710 /* See if the arguments match. If they do, then TMPL is
4711 the partial instantiation we want. */
4712 success = comp_template_args (TREE_PURPOSE (found), arglist);
4714 /* Restore the argument vectors to their full size. */
4715 TREE_VEC_LENGTH (arglist)++;
4716 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4718 if (success)
4720 found = tmpl;
4721 break;
4725 if (!found)
4727 /* There was no partial instantiation. This happens
4728 where C<T> is a member template of A<T> and it's used
4729 in something like
4731 template <typename T> struct B { A<T>::C<int> m; };
4732 B<float>;
4734 Create the partial instantiation.
4736 TREE_VEC_LENGTH (arglist)--;
4737 found = tsubst (template, arglist, complain, NULL_TREE);
4738 TREE_VEC_LENGTH (arglist)++;
4742 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4743 DECL_TEMPLATE_INSTANTIATIONS (template)
4744 = tree_cons (arglist, t,
4745 DECL_TEMPLATE_INSTANTIATIONS (template));
4747 if (TREE_CODE (t) == ENUMERAL_TYPE
4748 && !is_partial_instantiation)
4749 /* Now that the type has been registered on the instantiations
4750 list, we set up the enumerators. Because the enumeration
4751 constants may involve the enumeration type itself, we make
4752 sure to register the type first, and then create the
4753 constants. That way, doing tsubst_expr for the enumeration
4754 constants won't result in recursive calls here; we'll find
4755 the instantiation and exit above. */
4756 tsubst_enum (template_type, t, arglist);
4758 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4759 is set up. */
4760 if (TREE_CODE (t) != ENUMERAL_TYPE)
4761 DECL_NAME (type_decl) = classtype_mangled_name (t);
4762 if (is_partial_instantiation)
4763 /* If the type makes use of template parameters, the
4764 code that generates debugging information will crash. */
4765 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4769 timevar_pop (TV_NAME_LOOKUP);
4772 struct pair_fn_data
4774 tree_fn_t fn;
4775 void *data;
4776 struct pointer_set_t *visited;
4779 /* Called from for_each_template_parm via walk_tree. */
4781 static tree
4782 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4784 tree t = *tp;
4785 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4786 tree_fn_t fn = pfd->fn;
4787 void *data = pfd->data;
4789 if (TYPE_P (t)
4790 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4791 return error_mark_node;
4793 switch (TREE_CODE (t))
4795 case RECORD_TYPE:
4796 if (TYPE_PTRMEMFUNC_P (t))
4797 break;
4798 /* Fall through. */
4800 case UNION_TYPE:
4801 case ENUMERAL_TYPE:
4802 if (!TYPE_TEMPLATE_INFO (t))
4803 *walk_subtrees = 0;
4804 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4805 fn, data, pfd->visited))
4806 return error_mark_node;
4807 break;
4809 case METHOD_TYPE:
4810 /* Since we're not going to walk subtrees, we have to do this
4811 explicitly here. */
4812 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4813 pfd->visited))
4814 return error_mark_node;
4815 /* Fall through. */
4817 case FUNCTION_TYPE:
4818 /* Check the return type. */
4819 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4820 return error_mark_node;
4822 /* Check the parameter types. Since default arguments are not
4823 instantiated until they are needed, the TYPE_ARG_TYPES may
4824 contain expressions that involve template parameters. But,
4825 no-one should be looking at them yet. And, once they're
4826 instantiated, they don't contain template parameters, so
4827 there's no point in looking at them then, either. */
4829 tree parm;
4831 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4832 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4833 pfd->visited))
4834 return error_mark_node;
4836 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4837 want walk_tree walking into them itself. */
4838 *walk_subtrees = 0;
4840 break;
4842 case TYPEOF_TYPE:
4843 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4844 pfd->visited))
4845 return error_mark_node;
4846 break;
4848 case FUNCTION_DECL:
4849 case VAR_DECL:
4850 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4851 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4852 pfd->visited))
4853 return error_mark_node;
4854 /* Fall through. */
4856 case PARM_DECL:
4857 case CONST_DECL:
4858 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4859 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4860 pfd->visited))
4861 return error_mark_node;
4862 if (DECL_CONTEXT (t)
4863 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4864 pfd->visited))
4865 return error_mark_node;
4866 break;
4868 case BOUND_TEMPLATE_TEMPLATE_PARM:
4869 /* Record template parameters such as `T' inside `TT<T>'. */
4870 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4871 return error_mark_node;
4872 /* Fall through. */
4874 case TEMPLATE_TEMPLATE_PARM:
4875 case TEMPLATE_TYPE_PARM:
4876 case TEMPLATE_PARM_INDEX:
4877 if (fn && (*fn)(t, data))
4878 return error_mark_node;
4879 else if (!fn)
4880 return error_mark_node;
4881 break;
4883 case TEMPLATE_DECL:
4884 /* A template template parameter is encountered. */
4885 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4886 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4887 return error_mark_node;
4889 /* Already substituted template template parameter */
4890 *walk_subtrees = 0;
4891 break;
4893 case TYPENAME_TYPE:
4894 if (!fn
4895 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4896 data, pfd->visited))
4897 return error_mark_node;
4898 break;
4900 case CONSTRUCTOR:
4901 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4902 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4903 (TREE_TYPE (t)), fn, data,
4904 pfd->visited))
4905 return error_mark_node;
4906 break;
4908 case INDIRECT_REF:
4909 case COMPONENT_REF:
4910 /* If there's no type, then this thing must be some expression
4911 involving template parameters. */
4912 if (!fn && !TREE_TYPE (t))
4913 return error_mark_node;
4914 break;
4916 case MODOP_EXPR:
4917 case CAST_EXPR:
4918 case REINTERPRET_CAST_EXPR:
4919 case CONST_CAST_EXPR:
4920 case STATIC_CAST_EXPR:
4921 case DYNAMIC_CAST_EXPR:
4922 case ARROW_EXPR:
4923 case DOTSTAR_EXPR:
4924 case TYPEID_EXPR:
4925 case PSEUDO_DTOR_EXPR:
4926 if (!fn)
4927 return error_mark_node;
4928 break;
4930 case BASELINK:
4931 /* If we do not handle this case specially, we end up walking
4932 the BINFO hierarchy, which is circular, and therefore
4933 confuses walk_tree. */
4934 *walk_subtrees = 0;
4935 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4936 pfd->visited))
4937 return error_mark_node;
4938 break;
4940 default:
4941 break;
4944 /* We didn't find any template parameters we liked. */
4945 return NULL_TREE;
4948 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4949 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4950 call FN with the parameter and the DATA.
4951 If FN returns nonzero, the iteration is terminated, and
4952 for_each_template_parm returns 1. Otherwise, the iteration
4953 continues. If FN never returns a nonzero value, the value
4954 returned by for_each_template_parm is 0. If FN is NULL, it is
4955 considered to be the function which always returns 1. */
4957 static int
4958 for_each_template_parm (tree t, tree_fn_t fn, void* data,
4959 struct pointer_set_t *visited)
4961 struct pair_fn_data pfd;
4962 int result;
4964 /* Set up. */
4965 pfd.fn = fn;
4966 pfd.data = data;
4968 /* Walk the tree. (Conceptually, we would like to walk without
4969 duplicates, but for_each_template_parm_r recursively calls
4970 for_each_template_parm, so we would need to reorganize a fair
4971 bit to use walk_tree_without_duplicates, so we keep our own
4972 visited list.) */
4973 if (visited)
4974 pfd.visited = visited;
4975 else
4976 pfd.visited = pointer_set_create ();
4977 result = walk_tree (&t,
4978 for_each_template_parm_r,
4979 &pfd,
4980 pfd.visited) != NULL_TREE;
4982 /* Clean up. */
4983 if (!visited)
4985 pointer_set_destroy (pfd.visited);
4986 pfd.visited = 0;
4989 return result;
4992 /* Returns true if T depends on any template parameter. */
4995 uses_template_parms (tree t)
4997 bool dependent_p;
4998 int saved_processing_template_decl;
5000 saved_processing_template_decl = processing_template_decl;
5001 if (!saved_processing_template_decl)
5002 processing_template_decl = 1;
5003 if (TYPE_P (t))
5004 dependent_p = dependent_type_p (t);
5005 else if (TREE_CODE (t) == TREE_VEC)
5006 dependent_p = any_dependent_template_arguments_p (t);
5007 else if (TREE_CODE (t) == TREE_LIST)
5008 dependent_p = (uses_template_parms (TREE_VALUE (t))
5009 || uses_template_parms (TREE_CHAIN (t)));
5010 else if (TREE_CODE (t) == TYPE_DECL)
5011 dependent_p = dependent_type_p (TREE_TYPE (t));
5012 else if (DECL_P (t)
5013 || EXPR_P (t)
5014 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5015 || TREE_CODE (t) == OVERLOAD
5016 || TREE_CODE (t) == BASELINK
5017 || TREE_CODE (t) == IDENTIFIER_NODE
5018 || CONSTANT_CLASS_P (t))
5019 dependent_p = (type_dependent_expression_p (t)
5020 || value_dependent_expression_p (t));
5021 else
5023 gcc_assert (t == error_mark_node);
5024 dependent_p = false;
5027 processing_template_decl = saved_processing_template_decl;
5029 return dependent_p;
5032 /* Returns true if T depends on any template parameter with level LEVEL. */
5035 uses_template_parms_level (tree t, int level)
5037 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5040 static int tinst_depth;
5041 extern int max_tinst_depth;
5042 #ifdef GATHER_STATISTICS
5043 int depth_reached;
5044 #endif
5045 static int tinst_level_tick;
5046 static int last_template_error_tick;
5048 /* We're starting to instantiate D; record the template instantiation context
5049 for diagnostics and to restore it later. */
5051 static int
5052 push_tinst_level (tree d)
5054 tree new;
5056 if (tinst_depth >= max_tinst_depth)
5058 /* If the instantiation in question still has unbound template parms,
5059 we don't really care if we can't instantiate it, so just return.
5060 This happens with base instantiation for implicit `typename'. */
5061 if (uses_template_parms (d))
5062 return 0;
5064 last_template_error_tick = tinst_level_tick;
5065 error ("template instantiation depth exceeds maximum of %d (use "
5066 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5067 max_tinst_depth, d);
5069 print_instantiation_context ();
5071 return 0;
5074 new = make_node (TINST_LEVEL);
5075 TINST_DECL (new) = d;
5076 TINST_LOCATION (new) = input_location;
5077 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5078 TREE_CHAIN (new) = current_tinst_level;
5079 current_tinst_level = new;
5081 ++tinst_depth;
5082 #ifdef GATHER_STATISTICS
5083 if (tinst_depth > depth_reached)
5084 depth_reached = tinst_depth;
5085 #endif
5087 ++tinst_level_tick;
5088 return 1;
5091 /* We're done instantiating this template; return to the instantiation
5092 context. */
5094 static void
5095 pop_tinst_level (void)
5097 tree old = current_tinst_level;
5099 /* Restore the filename and line number stashed away when we started
5100 this instantiation. */
5101 input_location = TINST_LOCATION (old);
5102 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5103 current_tinst_level = TREE_CHAIN (old);
5104 --tinst_depth;
5105 ++tinst_level_tick;
5108 /* We're instantiating a deferred template; restore the template
5109 instantiation context in which the instantiation was requested, which
5110 is one step out from LEVEL. */
5112 static void
5113 reopen_tinst_level (tree level)
5115 tree t;
5117 tinst_depth = 0;
5118 for (t = level; t; t = TREE_CHAIN (t))
5119 ++tinst_depth;
5121 current_tinst_level = level;
5122 pop_tinst_level ();
5125 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5126 vector of template arguments, as for tsubst.
5128 Returns an appropriate tsubst'd friend declaration. */
5130 static tree
5131 tsubst_friend_function (tree decl, tree args)
5133 tree new_friend;
5135 if (TREE_CODE (decl) == FUNCTION_DECL
5136 && DECL_TEMPLATE_INSTANTIATION (decl)
5137 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5138 /* This was a friend declared with an explicit template
5139 argument list, e.g.:
5141 friend void f<>(T);
5143 to indicate that f was a template instantiation, not a new
5144 function declaration. Now, we have to figure out what
5145 instantiation of what template. */
5147 tree template_id, arglist, fns;
5148 tree new_args;
5149 tree tmpl;
5150 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5152 /* Friend functions are looked up in the containing namespace scope.
5153 We must enter that scope, to avoid finding member functions of the
5154 current cless with same name. */
5155 push_nested_namespace (ns);
5156 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5157 tf_warning_or_error, NULL_TREE);
5158 pop_nested_namespace (ns);
5159 arglist = tsubst (DECL_TI_ARGS (decl), args,
5160 tf_warning_or_error, NULL_TREE);
5161 template_id = lookup_template_function (fns, arglist);
5163 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5164 tmpl = determine_specialization (template_id, new_friend,
5165 &new_args,
5166 /*need_member_template=*/0,
5167 TREE_VEC_LENGTH (args));
5168 return instantiate_template (tmpl, new_args, tf_error);
5171 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5173 /* The NEW_FRIEND will look like an instantiation, to the
5174 compiler, but is not an instantiation from the point of view of
5175 the language. For example, we might have had:
5177 template <class T> struct S {
5178 template <class U> friend void f(T, U);
5181 Then, in S<int>, template <class U> void f(int, U) is not an
5182 instantiation of anything. */
5183 if (new_friend == error_mark_node)
5184 return error_mark_node;
5186 DECL_USE_TEMPLATE (new_friend) = 0;
5187 if (TREE_CODE (decl) == TEMPLATE_DECL)
5189 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5190 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5191 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5194 /* The mangled name for the NEW_FRIEND is incorrect. The function
5195 is not a template instantiation and should not be mangled like
5196 one. Therefore, we forget the mangling here; we'll recompute it
5197 later if we need it. */
5198 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5200 SET_DECL_RTL (new_friend, NULL_RTX);
5201 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5204 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5206 tree old_decl;
5207 tree new_friend_template_info;
5208 tree new_friend_result_template_info;
5209 tree ns;
5210 int new_friend_is_defn;
5212 /* We must save some information from NEW_FRIEND before calling
5213 duplicate decls since that function will free NEW_FRIEND if
5214 possible. */
5215 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5216 new_friend_is_defn =
5217 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5218 (template_for_substitution (new_friend)))
5219 != NULL_TREE);
5220 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5222 /* This declaration is a `primary' template. */
5223 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5225 new_friend_result_template_info
5226 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5228 else
5229 new_friend_result_template_info = NULL_TREE;
5231 /* Inside pushdecl_namespace_level, we will push into the
5232 current namespace. However, the friend function should go
5233 into the namespace of the template. */
5234 ns = decl_namespace_context (new_friend);
5235 push_nested_namespace (ns);
5236 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5237 pop_nested_namespace (ns);
5239 if (old_decl != new_friend)
5241 /* This new friend declaration matched an existing
5242 declaration. For example, given:
5244 template <class T> void f(T);
5245 template <class U> class C {
5246 template <class T> friend void f(T) {}
5249 the friend declaration actually provides the definition
5250 of `f', once C has been instantiated for some type. So,
5251 old_decl will be the out-of-class template declaration,
5252 while new_friend is the in-class definition.
5254 But, if `f' was called before this point, the
5255 instantiation of `f' will have DECL_TI_ARGS corresponding
5256 to `T' but not to `U', references to which might appear
5257 in the definition of `f'. Previously, the most general
5258 template for an instantiation of `f' was the out-of-class
5259 version; now it is the in-class version. Therefore, we
5260 run through all specialization of `f', adding to their
5261 DECL_TI_ARGS appropriately. In particular, they need a
5262 new set of outer arguments, corresponding to the
5263 arguments for this class instantiation.
5265 The same situation can arise with something like this:
5267 friend void f(int);
5268 template <class T> class C {
5269 friend void f(T) {}
5272 when `C<int>' is instantiated. Now, `f(int)' is defined
5273 in the class. */
5275 if (!new_friend_is_defn)
5276 /* On the other hand, if the in-class declaration does
5277 *not* provide a definition, then we don't want to alter
5278 existing definitions. We can just leave everything
5279 alone. */
5281 else
5283 /* Overwrite whatever template info was there before, if
5284 any, with the new template information pertaining to
5285 the declaration. */
5286 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5288 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5289 reregister_specialization (new_friend,
5290 most_general_template (old_decl),
5291 old_decl);
5292 else
5294 tree t;
5295 tree new_friend_args;
5297 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5298 = new_friend_result_template_info;
5300 new_friend_args = TI_ARGS (new_friend_template_info);
5301 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5302 t != NULL_TREE;
5303 t = TREE_CHAIN (t))
5305 tree spec = TREE_VALUE (t);
5307 DECL_TI_ARGS (spec)
5308 = add_outermost_template_args (new_friend_args,
5309 DECL_TI_ARGS (spec));
5312 /* Now, since specializations are always supposed to
5313 hang off of the most general template, we must move
5314 them. */
5315 t = most_general_template (old_decl);
5316 if (t != old_decl)
5318 DECL_TEMPLATE_SPECIALIZATIONS (t)
5319 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5320 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5321 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5326 /* The information from NEW_FRIEND has been merged into OLD_DECL
5327 by duplicate_decls. */
5328 new_friend = old_decl;
5331 else
5333 tree context = DECL_CONTEXT (new_friend);
5334 bool dependent_p;
5336 /* In the code
5337 template <class T> class C {
5338 template <class U> friend void C1<U>::f (); // case 1
5339 friend void C2<T>::f (); // case 2
5341 we only need to make sure CONTEXT is a complete type for
5342 case 2. To distinguish between the two cases, we note that
5343 CONTEXT of case 1 remains dependent type after tsubst while
5344 this isn't true for case 2. */
5345 ++processing_template_decl;
5346 dependent_p = dependent_type_p (context);
5347 --processing_template_decl;
5349 if (!dependent_p
5350 && !complete_type_or_else (context, NULL_TREE))
5351 return error_mark_node;
5353 if (COMPLETE_TYPE_P (context))
5355 /* Check to see that the declaration is really present, and,
5356 possibly obtain an improved declaration. */
5357 tree fn = check_classfn (context,
5358 new_friend, NULL_TREE);
5360 if (fn)
5361 new_friend = fn;
5365 return new_friend;
5368 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5369 template arguments, as for tsubst.
5371 Returns an appropriate tsubst'd friend type or error_mark_node on
5372 failure. */
5374 static tree
5375 tsubst_friend_class (tree friend_tmpl, tree args)
5377 tree friend_type;
5378 tree tmpl;
5379 tree context;
5381 context = DECL_CONTEXT (friend_tmpl);
5383 if (context)
5385 if (TREE_CODE (context) == NAMESPACE_DECL)
5386 push_nested_namespace (context);
5387 else
5388 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5391 /* First, we look for a class template. */
5392 tmpl = lookup_name (DECL_NAME (friend_tmpl));
5394 /* But, if we don't find one, it might be because we're in a
5395 situation like this:
5397 template <class T>
5398 struct S {
5399 template <class U>
5400 friend struct S;
5403 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5404 for `S<int>', not the TEMPLATE_DECL. */
5405 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5407 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5408 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5411 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5413 /* The friend template has already been declared. Just
5414 check to see that the declarations match, and install any new
5415 default parameters. We must tsubst the default parameters,
5416 of course. We only need the innermost template parameters
5417 because that is all that redeclare_class_template will look
5418 at. */
5419 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5420 > TMPL_ARGS_DEPTH (args))
5422 tree parms;
5423 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5424 args, tf_warning_or_error);
5425 redeclare_class_template (TREE_TYPE (tmpl), parms);
5428 friend_type = TREE_TYPE (tmpl);
5430 else
5432 /* The friend template has not already been declared. In this
5433 case, the instantiation of the template class will cause the
5434 injection of this template into the global scope. */
5435 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5437 /* The new TMPL is not an instantiation of anything, so we
5438 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5439 the new type because that is supposed to be the corresponding
5440 template decl, i.e., TMPL. */
5441 DECL_USE_TEMPLATE (tmpl) = 0;
5442 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5443 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5444 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5445 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5447 /* Inject this template into the global scope. */
5448 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5451 if (context)
5453 if (TREE_CODE (context) == NAMESPACE_DECL)
5454 pop_nested_namespace (context);
5455 else
5456 pop_nested_class ();
5459 return friend_type;
5462 /* Returns zero if TYPE cannot be completed later due to circularity.
5463 Otherwise returns one. */
5465 static int
5466 can_complete_type_without_circularity (tree type)
5468 if (type == NULL_TREE || type == error_mark_node)
5469 return 0;
5470 else if (COMPLETE_TYPE_P (type))
5471 return 1;
5472 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5473 return can_complete_type_without_circularity (TREE_TYPE (type));
5474 else if (CLASS_TYPE_P (type)
5475 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5476 return 0;
5477 else
5478 return 1;
5481 tree
5482 instantiate_class_template (tree type)
5484 tree template, args, pattern, t, member;
5485 tree typedecl;
5486 tree pbinfo;
5487 tree base_list;
5489 if (type == error_mark_node)
5490 return error_mark_node;
5492 if (TYPE_BEING_DEFINED (type)
5493 || COMPLETE_TYPE_P (type)
5494 || dependent_type_p (type))
5495 return type;
5497 /* Figure out which template is being instantiated. */
5498 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5499 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5501 /* Determine what specialization of the original template to
5502 instantiate. */
5503 t = most_specialized_class (type, template);
5504 if (t == error_mark_node)
5506 TYPE_BEING_DEFINED (type) = 1;
5507 return error_mark_node;
5509 else if (t)
5511 /* This TYPE is actually an instantiation of a partial
5512 specialization. We replace the innermost set of ARGS with
5513 the arguments appropriate for substitution. For example,
5514 given:
5516 template <class T> struct S {};
5517 template <class T> struct S<T*> {};
5519 and supposing that we are instantiating S<int*>, ARGS will
5520 presently be {int*} -- but we need {int}. */
5521 pattern = TREE_TYPE (t);
5522 args = TREE_PURPOSE (t);
5524 else
5526 pattern = TREE_TYPE (template);
5527 args = CLASSTYPE_TI_ARGS (type);
5530 /* If the template we're instantiating is incomplete, then clearly
5531 there's nothing we can do. */
5532 if (!COMPLETE_TYPE_P (pattern))
5533 return type;
5535 /* If we've recursively instantiated too many templates, stop. */
5536 if (! push_tinst_level (type))
5537 return type;
5539 /* Now we're really doing the instantiation. Mark the type as in
5540 the process of being defined. */
5541 TYPE_BEING_DEFINED (type) = 1;
5543 /* We may be in the middle of deferred access check. Disable
5544 it now. */
5545 push_deferring_access_checks (dk_no_deferred);
5547 push_to_top_level ();
5549 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5551 /* Set the input location to the template definition. This is needed
5552 if tsubsting causes an error. */
5553 typedecl = TYPE_MAIN_DECL (type);
5554 input_location = DECL_SOURCE_LOCATION (typedecl);
5555 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5557 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5558 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5559 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5560 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5561 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5562 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5563 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5564 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5565 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5566 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5567 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5568 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5569 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5570 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5571 if (ANON_AGGR_TYPE_P (pattern))
5572 SET_ANON_AGGR_TYPE_P (type);
5573 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5575 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5576 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5579 pbinfo = TYPE_BINFO (pattern);
5581 /* We should never instantiate a nested class before its enclosing
5582 class; we need to look up the nested class by name before we can
5583 instantiate it, and that lookup should instantiate the enclosing
5584 class. */
5585 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5586 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5587 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5589 base_list = NULL_TREE;
5590 if (BINFO_N_BASE_BINFOS (pbinfo))
5592 tree pbase_binfo;
5593 tree context = TYPE_CONTEXT (type);
5594 tree pushed_scope;
5595 int i;
5597 /* We must enter the scope containing the type, as that is where
5598 the accessibility of types named in dependent bases are
5599 looked up from. */
5600 pushed_scope = push_scope (context ? context : global_namespace);
5602 /* Substitute into each of the bases to determine the actual
5603 basetypes. */
5604 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5606 tree base;
5607 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5609 /* Substitute to figure out the base class. */
5610 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5611 if (base == error_mark_node)
5612 continue;
5614 base_list = tree_cons (access, base, base_list);
5615 if (BINFO_VIRTUAL_P (pbase_binfo))
5616 TREE_TYPE (base_list) = integer_type_node;
5619 /* The list is now in reverse order; correct that. */
5620 base_list = nreverse (base_list);
5622 if (pushed_scope)
5623 pop_scope (pushed_scope);
5625 /* Now call xref_basetypes to set up all the base-class
5626 information. */
5627 xref_basetypes (type, base_list);
5630 /* Now that our base classes are set up, enter the scope of the
5631 class, so that name lookups into base classes, etc. will work
5632 correctly. This is precisely analogous to what we do in
5633 begin_class_definition when defining an ordinary non-template
5634 class. */
5635 pushclass (type);
5637 /* Now members are processed in the order of declaration. */
5638 for (member = CLASSTYPE_DECL_LIST (pattern);
5639 member; member = TREE_CHAIN (member))
5641 tree t = TREE_VALUE (member);
5643 if (TREE_PURPOSE (member))
5645 if (TYPE_P (t))
5647 /* Build new CLASSTYPE_NESTED_UTDS. */
5649 tree newtag;
5650 bool class_template_p;
5652 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5653 && TYPE_LANG_SPECIFIC (t)
5654 && CLASSTYPE_IS_TEMPLATE (t));
5655 /* If the member is a class template, then -- even after
5656 substitution -- there may be dependent types in the
5657 template argument list for the class. We increment
5658 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5659 that function will assume that no types are dependent
5660 when outside of a template. */
5661 if (class_template_p)
5662 ++processing_template_decl;
5663 newtag = tsubst (t, args, tf_error, NULL_TREE);
5664 if (class_template_p)
5665 --processing_template_decl;
5666 if (newtag == error_mark_node)
5667 continue;
5669 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5671 tree name = TYPE_IDENTIFIER (t);
5673 if (class_template_p)
5674 /* Unfortunately, lookup_template_class sets
5675 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5676 instantiation (i.e., for the type of a member
5677 template class nested within a template class.)
5678 This behavior is required for
5679 maybe_process_partial_specialization to work
5680 correctly, but is not accurate in this case;
5681 the TAG is not an instantiation of anything.
5682 (The corresponding TEMPLATE_DECL is an
5683 instantiation, but the TYPE is not.) */
5684 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5686 /* Now, we call pushtag to put this NEWTAG into the scope of
5687 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5688 pushtag calling push_template_decl. We don't have to do
5689 this for enums because it will already have been done in
5690 tsubst_enum. */
5691 if (name)
5692 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5693 pushtag (name, newtag, /*tag_scope=*/ts_current);
5696 else if (TREE_CODE (t) == FUNCTION_DECL
5697 || DECL_FUNCTION_TEMPLATE_P (t))
5699 /* Build new TYPE_METHODS. */
5700 tree r;
5702 if (TREE_CODE (t) == TEMPLATE_DECL)
5703 ++processing_template_decl;
5704 r = tsubst (t, args, tf_error, NULL_TREE);
5705 if (TREE_CODE (t) == TEMPLATE_DECL)
5706 --processing_template_decl;
5707 set_current_access_from_decl (r);
5708 grok_special_member_properties (r);
5709 finish_member_declaration (r);
5711 else
5713 /* Build new TYPE_FIELDS. */
5715 if (TREE_CODE (t) != CONST_DECL)
5717 tree r;
5719 /* The the file and line for this declaration, to
5720 assist in error message reporting. Since we
5721 called push_tinst_level above, we don't need to
5722 restore these. */
5723 input_location = DECL_SOURCE_LOCATION (t);
5725 if (TREE_CODE (t) == TEMPLATE_DECL)
5726 ++processing_template_decl;
5727 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5728 if (TREE_CODE (t) == TEMPLATE_DECL)
5729 --processing_template_decl;
5730 if (TREE_CODE (r) == VAR_DECL)
5732 /* In [temp.inst]:
5734 [t]he initialization (and any associated
5735 side-effects) of a static data member does
5736 not occur unless the static data member is
5737 itself used in a way that requires the
5738 definition of the static data member to
5739 exist.
5741 Therefore, we do not substitute into the
5742 initialized for the static data member here. */
5743 finish_static_data_member_decl
5744 (r,
5745 /*init=*/NULL_TREE,
5746 /*init_const_expr_p=*/false,
5747 /*asmspec_tree=*/NULL_TREE,
5748 /*flags=*/0);
5749 if (DECL_INITIALIZED_IN_CLASS_P (r))
5750 check_static_variable_definition (r, TREE_TYPE (r));
5752 else if (TREE_CODE (r) == FIELD_DECL)
5754 /* Determine whether R has a valid type and can be
5755 completed later. If R is invalid, then it is
5756 replaced by error_mark_node so that it will not be
5757 added to TYPE_FIELDS. */
5758 tree rtype = TREE_TYPE (r);
5759 if (can_complete_type_without_circularity (rtype))
5760 complete_type (rtype);
5762 if (!COMPLETE_TYPE_P (rtype))
5764 cxx_incomplete_type_error (r, rtype);
5765 r = error_mark_node;
5769 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5770 such a thing will already have been added to the field
5771 list by tsubst_enum in finish_member_declaration in the
5772 CLASSTYPE_NESTED_UTDS case above. */
5773 if (!(TREE_CODE (r) == TYPE_DECL
5774 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5775 && DECL_ARTIFICIAL (r)))
5777 set_current_access_from_decl (r);
5778 finish_member_declaration (r);
5783 else
5785 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5787 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5789 tree friend_type = t;
5790 bool adjust_processing_template_decl = false;
5792 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5794 /* template <class T> friend class C; */
5795 friend_type = tsubst_friend_class (friend_type, args);
5796 adjust_processing_template_decl = true;
5798 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5800 /* template <class T> friend class C::D; */
5801 friend_type = tsubst (friend_type, args,
5802 tf_warning_or_error, NULL_TREE);
5803 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5804 friend_type = TREE_TYPE (friend_type);
5805 adjust_processing_template_decl = true;
5807 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5809 /* This could be either
5811 friend class T::C;
5813 when dependent_type_p is false or
5815 template <class U> friend class T::C;
5817 otherwise. */
5818 friend_type = tsubst (friend_type, args,
5819 tf_warning_or_error, NULL_TREE);
5820 /* Bump processing_template_decl for correct
5821 dependent_type_p calculation. */
5822 ++processing_template_decl;
5823 if (dependent_type_p (friend_type))
5824 adjust_processing_template_decl = true;
5825 --processing_template_decl;
5827 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5828 && hidden_name_p (TYPE_NAME (friend_type)))
5830 /* friend class C;
5832 where C hasn't been declared yet. Let's lookup name
5833 from namespace scope directly, bypassing any name that
5834 come from dependent base class. */
5835 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5837 /* The call to xref_tag_from_type does injection for friend
5838 classes. */
5839 push_nested_namespace (ns);
5840 friend_type =
5841 xref_tag_from_type (friend_type, NULL_TREE,
5842 /*tag_scope=*/ts_current);
5843 pop_nested_namespace (ns);
5845 else if (uses_template_parms (friend_type))
5846 /* friend class C<T>; */
5847 friend_type = tsubst (friend_type, args,
5848 tf_warning_or_error, NULL_TREE);
5849 /* Otherwise it's
5851 friend class C;
5853 where C is already declared or
5855 friend class C<int>;
5857 We don't have to do anything in these cases. */
5859 if (adjust_processing_template_decl)
5860 /* Trick make_friend_class into realizing that the friend
5861 we're adding is a template, not an ordinary class. It's
5862 important that we use make_friend_class since it will
5863 perform some error-checking and output cross-reference
5864 information. */
5865 ++processing_template_decl;
5867 if (friend_type != error_mark_node)
5868 make_friend_class (type, friend_type, /*complain=*/false);
5870 if (adjust_processing_template_decl)
5871 --processing_template_decl;
5873 else
5875 /* Build new DECL_FRIENDLIST. */
5876 tree r;
5878 /* The the file and line for this declaration, to
5879 assist in error message reporting. Since we
5880 called push_tinst_level above, we don't need to
5881 restore these. */
5882 input_location = DECL_SOURCE_LOCATION (t);
5884 if (TREE_CODE (t) == TEMPLATE_DECL)
5886 ++processing_template_decl;
5887 push_deferring_access_checks (dk_no_check);
5890 r = tsubst_friend_function (t, args);
5891 add_friend (type, r, /*complain=*/false);
5892 if (TREE_CODE (t) == TEMPLATE_DECL)
5894 pop_deferring_access_checks ();
5895 --processing_template_decl;
5901 /* Set the file and line number information to whatever is given for
5902 the class itself. This puts error messages involving generated
5903 implicit functions at a predictable point, and the same point
5904 that would be used for non-template classes. */
5905 input_location = DECL_SOURCE_LOCATION (typedecl);
5907 unreverse_member_declarations (type);
5908 finish_struct_1 (type);
5909 TYPE_BEING_DEFINED (type) = 0;
5911 /* Now that the class is complete, instantiate default arguments for
5912 any member functions. We don't do this earlier because the
5913 default arguments may reference members of the class. */
5914 if (!PRIMARY_TEMPLATE_P (template))
5915 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5916 if (TREE_CODE (t) == FUNCTION_DECL
5917 /* Implicitly generated member functions will not have template
5918 information; they are not instantiations, but instead are
5919 created "fresh" for each instantiation. */
5920 && DECL_TEMPLATE_INFO (t))
5921 tsubst_default_arguments (t);
5923 popclass ();
5924 pop_from_top_level ();
5925 pop_deferring_access_checks ();
5926 pop_tinst_level ();
5928 /* The vtable for a template class can be emitted in any translation
5929 unit in which the class is instantiated. When there is no key
5930 method, however, finish_struct_1 will already have added TYPE to
5931 the keyed_classes list. */
5932 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5933 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5935 return type;
5938 static tree
5939 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5941 tree r;
5943 if (!t)
5944 r = t;
5945 else if (TYPE_P (t))
5946 r = tsubst (t, args, complain, in_decl);
5947 else
5949 r = tsubst_expr (t, args, complain, in_decl);
5950 r = fold_non_dependent_expr (r);
5952 return r;
5955 /* Substitute ARGS into the vector or list of template arguments T. */
5957 static tree
5958 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5960 int len = TREE_VEC_LENGTH (t);
5961 int need_new = 0, i;
5962 tree *elts = (tree *) alloca (len * sizeof (tree));
5964 for (i = 0; i < len; i++)
5966 tree orig_arg = TREE_VEC_ELT (t, i);
5967 tree new_arg;
5969 if (TREE_CODE (orig_arg) == TREE_VEC)
5970 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5971 else
5972 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5974 if (new_arg == error_mark_node)
5975 return error_mark_node;
5977 elts[i] = new_arg;
5978 if (new_arg != orig_arg)
5979 need_new = 1;
5982 if (!need_new)
5983 return t;
5985 t = make_tree_vec (len);
5986 for (i = 0; i < len; i++)
5987 TREE_VEC_ELT (t, i) = elts[i];
5989 return t;
5992 /* Return the result of substituting ARGS into the template parameters
5993 given by PARMS. If there are m levels of ARGS and m + n levels of
5994 PARMS, then the result will contain n levels of PARMS. For
5995 example, if PARMS is `template <class T> template <class U>
5996 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5997 result will be `template <int*, double, class V>'. */
5999 static tree
6000 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6002 tree r = NULL_TREE;
6003 tree* new_parms;
6005 /* When substituting into a template, we must set
6006 PROCESSING_TEMPLATE_DECL as the template parameters may be
6007 dependent if they are based on one-another, and the dependency
6008 predicates are short-circuit outside of templates. */
6009 ++processing_template_decl;
6011 for (new_parms = &r;
6012 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6013 new_parms = &(TREE_CHAIN (*new_parms)),
6014 parms = TREE_CHAIN (parms))
6016 tree new_vec =
6017 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6018 int i;
6020 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6022 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6023 tree default_value = TREE_PURPOSE (tuple);
6024 tree parm_decl = TREE_VALUE (tuple);
6026 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6027 if (TREE_CODE (parm_decl) == PARM_DECL
6028 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6029 parm_decl = error_mark_node;
6030 default_value = tsubst_template_arg (default_value, args,
6031 complain, NULL_TREE);
6033 tuple = build_tree_list (default_value, parm_decl);
6034 TREE_VEC_ELT (new_vec, i) = tuple;
6037 *new_parms =
6038 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6039 - TMPL_ARGS_DEPTH (args)),
6040 new_vec, NULL_TREE);
6043 --processing_template_decl;
6045 return r;
6048 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6049 type T. If T is not an aggregate or enumeration type, it is
6050 handled as if by tsubst. IN_DECL is as for tsubst. If
6051 ENTERING_SCOPE is nonzero, T is the context for a template which
6052 we are presently tsubst'ing. Return the substituted value. */
6054 static tree
6055 tsubst_aggr_type (tree t,
6056 tree args,
6057 tsubst_flags_t complain,
6058 tree in_decl,
6059 int entering_scope)
6061 if (t == NULL_TREE)
6062 return NULL_TREE;
6064 switch (TREE_CODE (t))
6066 case RECORD_TYPE:
6067 if (TYPE_PTRMEMFUNC_P (t))
6068 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6070 /* Else fall through. */
6071 case ENUMERAL_TYPE:
6072 case UNION_TYPE:
6073 if (TYPE_TEMPLATE_INFO (t))
6075 tree argvec;
6076 tree context;
6077 tree r;
6078 bool saved_skip_evaluation;
6080 /* In "sizeof(X<I>)" we need to evaluate "I". */
6081 saved_skip_evaluation = skip_evaluation;
6082 skip_evaluation = false;
6084 /* First, determine the context for the type we are looking
6085 up. */
6086 context = TYPE_CONTEXT (t);
6087 if (context)
6088 context = tsubst_aggr_type (context, args, complain,
6089 in_decl, /*entering_scope=*/1);
6091 /* Then, figure out what arguments are appropriate for the
6092 type we are trying to find. For example, given:
6094 template <class T> struct S;
6095 template <class T, class U> void f(T, U) { S<U> su; }
6097 and supposing that we are instantiating f<int, double>,
6098 then our ARGS will be {int, double}, but, when looking up
6099 S we only want {double}. */
6100 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6101 complain, in_decl);
6102 if (argvec == error_mark_node)
6103 r = error_mark_node;
6104 else
6106 r = lookup_template_class (t, argvec, in_decl, context,
6107 entering_scope, complain);
6108 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6111 skip_evaluation = saved_skip_evaluation;
6113 return r;
6115 else
6116 /* This is not a template type, so there's nothing to do. */
6117 return t;
6119 default:
6120 return tsubst (t, args, complain, in_decl);
6124 /* Substitute into the default argument ARG (a default argument for
6125 FN), which has the indicated TYPE. */
6127 tree
6128 tsubst_default_argument (tree fn, tree type, tree arg)
6130 tree saved_class_ptr = NULL_TREE;
6131 tree saved_class_ref = NULL_TREE;
6133 /* This default argument came from a template. Instantiate the
6134 default argument here, not in tsubst. In the case of
6135 something like:
6137 template <class T>
6138 struct S {
6139 static T t();
6140 void f(T = t());
6143 we must be careful to do name lookup in the scope of S<T>,
6144 rather than in the current class. */
6145 push_access_scope (fn);
6146 /* The "this" pointer is not valid in a default argument. */
6147 if (cfun)
6149 saved_class_ptr = current_class_ptr;
6150 cp_function_chain->x_current_class_ptr = NULL_TREE;
6151 saved_class_ref = current_class_ref;
6152 cp_function_chain->x_current_class_ref = NULL_TREE;
6155 push_deferring_access_checks(dk_no_deferred);
6156 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6157 tf_warning_or_error, NULL_TREE);
6158 pop_deferring_access_checks();
6160 /* Restore the "this" pointer. */
6161 if (cfun)
6163 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6164 cp_function_chain->x_current_class_ref = saved_class_ref;
6167 pop_access_scope (fn);
6169 /* Make sure the default argument is reasonable. */
6170 arg = check_default_argument (type, arg);
6172 return arg;
6175 /* Substitute into all the default arguments for FN. */
6177 static void
6178 tsubst_default_arguments (tree fn)
6180 tree arg;
6181 tree tmpl_args;
6183 tmpl_args = DECL_TI_ARGS (fn);
6185 /* If this function is not yet instantiated, we certainly don't need
6186 its default arguments. */
6187 if (uses_template_parms (tmpl_args))
6188 return;
6190 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6191 arg;
6192 arg = TREE_CHAIN (arg))
6193 if (TREE_PURPOSE (arg))
6194 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6195 TREE_VALUE (arg),
6196 TREE_PURPOSE (arg));
6199 /* Substitute the ARGS into the T, which is a _DECL. Return the
6200 result of the substitution. Issue error and warning messages under
6201 control of COMPLAIN. */
6203 static tree
6204 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6206 location_t saved_loc;
6207 tree r = NULL_TREE;
6208 tree in_decl = t;
6210 /* Set the filename and linenumber to improve error-reporting. */
6211 saved_loc = input_location;
6212 input_location = DECL_SOURCE_LOCATION (t);
6214 switch (TREE_CODE (t))
6216 case TEMPLATE_DECL:
6218 /* We can get here when processing a member function template,
6219 member class template, and template template parameter of
6220 a template class. */
6221 tree decl = DECL_TEMPLATE_RESULT (t);
6222 tree spec;
6223 tree tmpl_args;
6224 tree full_args;
6226 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6228 /* Template template parameter is treated here. */
6229 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6230 if (new_type == error_mark_node)
6231 return error_mark_node;
6233 r = copy_decl (t);
6234 TREE_CHAIN (r) = NULL_TREE;
6235 TREE_TYPE (r) = new_type;
6236 DECL_TEMPLATE_RESULT (r)
6237 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6238 DECL_TEMPLATE_PARMS (r)
6239 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6240 complain);
6241 TYPE_NAME (new_type) = r;
6242 break;
6245 /* We might already have an instance of this template.
6246 The ARGS are for the surrounding class type, so the
6247 full args contain the tsubst'd args for the context,
6248 plus the innermost args from the template decl. */
6249 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6250 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6251 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6252 /* Because this is a template, the arguments will still be
6253 dependent, even after substitution. If
6254 PROCESSING_TEMPLATE_DECL is not set, the dependency
6255 predicates will short-circuit. */
6256 ++processing_template_decl;
6257 full_args = tsubst_template_args (tmpl_args, args,
6258 complain, in_decl);
6259 --processing_template_decl;
6260 if (full_args == error_mark_node)
6261 return error_mark_node;
6263 /* tsubst_template_args doesn't copy the vector if
6264 nothing changed. But, *something* should have
6265 changed. */
6266 gcc_assert (full_args != tmpl_args);
6268 spec = retrieve_specialization (t, full_args,
6269 /*class_specializations_p=*/true);
6270 if (spec != NULL_TREE)
6272 r = spec;
6273 break;
6276 /* Make a new template decl. It will be similar to the
6277 original, but will record the current template arguments.
6278 We also create a new function declaration, which is just
6279 like the old one, but points to this new template, rather
6280 than the old one. */
6281 r = copy_decl (t);
6282 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6283 TREE_CHAIN (r) = NULL_TREE;
6285 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6287 if (TREE_CODE (decl) == TYPE_DECL)
6289 tree new_type;
6290 ++processing_template_decl;
6291 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6292 --processing_template_decl;
6293 if (new_type == error_mark_node)
6294 return error_mark_node;
6296 TREE_TYPE (r) = new_type;
6297 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6298 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6299 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6300 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6302 else
6304 tree new_decl;
6305 ++processing_template_decl;
6306 new_decl = tsubst (decl, args, complain, in_decl);
6307 --processing_template_decl;
6308 if (new_decl == error_mark_node)
6309 return error_mark_node;
6311 DECL_TEMPLATE_RESULT (r) = new_decl;
6312 DECL_TI_TEMPLATE (new_decl) = r;
6313 TREE_TYPE (r) = TREE_TYPE (new_decl);
6314 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6315 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6318 SET_DECL_IMPLICIT_INSTANTIATION (r);
6319 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6320 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6322 /* The template parameters for this new template are all the
6323 template parameters for the old template, except the
6324 outermost level of parameters. */
6325 DECL_TEMPLATE_PARMS (r)
6326 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6327 complain);
6329 if (PRIMARY_TEMPLATE_P (t))
6330 DECL_PRIMARY_TEMPLATE (r) = r;
6332 if (TREE_CODE (decl) != TYPE_DECL)
6333 /* Record this non-type partial instantiation. */
6334 register_specialization (r, t,
6335 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6336 false);
6338 break;
6340 case FUNCTION_DECL:
6342 tree ctx;
6343 tree argvec = NULL_TREE;
6344 tree *friends;
6345 tree gen_tmpl;
6346 tree type;
6347 int member;
6348 int args_depth;
6349 int parms_depth;
6351 /* Nobody should be tsubst'ing into non-template functions. */
6352 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6354 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6356 tree spec;
6357 bool dependent_p;
6359 /* If T is not dependent, just return it. We have to
6360 increment PROCESSING_TEMPLATE_DECL because
6361 value_dependent_expression_p assumes that nothing is
6362 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6363 ++processing_template_decl;
6364 dependent_p = value_dependent_expression_p (t);
6365 --processing_template_decl;
6366 if (!dependent_p)
6367 return t;
6369 /* Calculate the most general template of which R is a
6370 specialization, and the complete set of arguments used to
6371 specialize R. */
6372 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6373 argvec = tsubst_template_args (DECL_TI_ARGS
6374 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6375 args, complain, in_decl);
6377 /* Check to see if we already have this specialization. */
6378 spec = retrieve_specialization (gen_tmpl, argvec,
6379 /*class_specializations_p=*/false);
6381 if (spec)
6383 r = spec;
6384 break;
6387 /* We can see more levels of arguments than parameters if
6388 there was a specialization of a member template, like
6389 this:
6391 template <class T> struct S { template <class U> void f(); }
6392 template <> template <class U> void S<int>::f(U);
6394 Here, we'll be substituting into the specialization,
6395 because that's where we can find the code we actually
6396 want to generate, but we'll have enough arguments for
6397 the most general template.
6399 We also deal with the peculiar case:
6401 template <class T> struct S {
6402 template <class U> friend void f();
6404 template <class U> void f() {}
6405 template S<int>;
6406 template void f<double>();
6408 Here, the ARGS for the instantiation of will be {int,
6409 double}. But, we only need as many ARGS as there are
6410 levels of template parameters in CODE_PATTERN. We are
6411 careful not to get fooled into reducing the ARGS in
6412 situations like:
6414 template <class T> struct S { template <class U> void f(U); }
6415 template <class T> template <> void S<T>::f(int) {}
6417 which we can spot because the pattern will be a
6418 specialization in this case. */
6419 args_depth = TMPL_ARGS_DEPTH (args);
6420 parms_depth =
6421 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6422 if (args_depth > parms_depth
6423 && !DECL_TEMPLATE_SPECIALIZATION (t))
6424 args = get_innermost_template_args (args, parms_depth);
6426 else
6428 /* This special case arises when we have something like this:
6430 template <class T> struct S {
6431 friend void f<int>(int, double);
6434 Here, the DECL_TI_TEMPLATE for the friend declaration
6435 will be an IDENTIFIER_NODE. We are being called from
6436 tsubst_friend_function, and we want only to create a
6437 new decl (R) with appropriate types so that we can call
6438 determine_specialization. */
6439 gen_tmpl = NULL_TREE;
6442 if (DECL_CLASS_SCOPE_P (t))
6444 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6445 member = 2;
6446 else
6447 member = 1;
6448 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6449 complain, t, /*entering_scope=*/1);
6451 else
6453 member = 0;
6454 ctx = DECL_CONTEXT (t);
6456 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6457 if (type == error_mark_node)
6458 return error_mark_node;
6460 /* We do NOT check for matching decls pushed separately at this
6461 point, as they may not represent instantiations of this
6462 template, and in any case are considered separate under the
6463 discrete model. */
6464 r = copy_decl (t);
6465 DECL_USE_TEMPLATE (r) = 0;
6466 TREE_TYPE (r) = type;
6467 /* Clear out the mangled name and RTL for the instantiation. */
6468 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6469 SET_DECL_RTL (r, NULL_RTX);
6470 DECL_INITIAL (r) = NULL_TREE;
6471 DECL_CONTEXT (r) = ctx;
6473 if (member && DECL_CONV_FN_P (r))
6474 /* Type-conversion operator. Reconstruct the name, in
6475 case it's the name of one of the template's parameters. */
6476 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6478 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6479 complain, t);
6480 DECL_RESULT (r) = NULL_TREE;
6482 TREE_STATIC (r) = 0;
6483 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6484 DECL_EXTERNAL (r) = 1;
6485 /* If this is an instantiation of a function with internal
6486 linkage, we already know what object file linkage will be
6487 assigned to the instantiation. */
6488 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6489 DECL_DEFER_OUTPUT (r) = 0;
6490 TREE_CHAIN (r) = NULL_TREE;
6491 DECL_PENDING_INLINE_INFO (r) = 0;
6492 DECL_PENDING_INLINE_P (r) = 0;
6493 DECL_SAVED_TREE (r) = NULL_TREE;
6494 TREE_USED (r) = 0;
6495 if (DECL_CLONED_FUNCTION (r))
6497 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6498 args, complain, t);
6499 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6500 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6503 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6504 this in the special friend case mentioned above where
6505 GEN_TMPL is NULL. */
6506 if (gen_tmpl)
6508 DECL_TEMPLATE_INFO (r)
6509 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6510 SET_DECL_IMPLICIT_INSTANTIATION (r);
6511 register_specialization (r, gen_tmpl, argvec, false);
6513 /* We're not supposed to instantiate default arguments
6514 until they are called, for a template. But, for a
6515 declaration like:
6517 template <class T> void f ()
6518 { extern void g(int i = T()); }
6520 we should do the substitution when the template is
6521 instantiated. We handle the member function case in
6522 instantiate_class_template since the default arguments
6523 might refer to other members of the class. */
6524 if (!member
6525 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6526 && !uses_template_parms (argvec))
6527 tsubst_default_arguments (r);
6529 else
6530 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6532 /* Copy the list of befriending classes. */
6533 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6534 *friends;
6535 friends = &TREE_CHAIN (*friends))
6537 *friends = copy_node (*friends);
6538 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6539 args, complain,
6540 in_decl);
6543 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6545 maybe_retrofit_in_chrg (r);
6546 if (DECL_CONSTRUCTOR_P (r))
6547 grok_ctor_properties (ctx, r);
6548 /* If this is an instantiation of a member template, clone it.
6549 If it isn't, that'll be handled by
6550 clone_constructors_and_destructors. */
6551 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6552 clone_function_decl (r, /*update_method_vec_p=*/0);
6554 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6555 grok_op_properties (r, (complain & tf_error) != 0);
6557 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6558 SET_DECL_FRIEND_CONTEXT (r,
6559 tsubst (DECL_FRIEND_CONTEXT (t),
6560 args, complain, in_decl));
6562 break;
6564 case PARM_DECL:
6566 tree type;
6568 r = copy_node (t);
6569 if (DECL_TEMPLATE_PARM_P (t))
6570 SET_DECL_TEMPLATE_PARM_P (r);
6572 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6573 type = type_decays_to (type);
6574 TREE_TYPE (r) = type;
6575 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6577 if (DECL_INITIAL (r))
6579 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6580 DECL_INITIAL (r) = TREE_TYPE (r);
6581 else
6582 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6583 complain, in_decl);
6586 DECL_CONTEXT (r) = NULL_TREE;
6588 if (!DECL_TEMPLATE_PARM_P (r))
6589 DECL_ARG_TYPE (r) = type_passed_as (type);
6590 if (TREE_CHAIN (t))
6591 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6592 complain, TREE_CHAIN (t));
6594 break;
6596 case FIELD_DECL:
6598 tree type;
6600 r = copy_decl (t);
6601 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6602 if (type == error_mark_node)
6603 return error_mark_node;
6604 TREE_TYPE (r) = type;
6605 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6607 /* We don't have to set DECL_CONTEXT here; it is set by
6608 finish_member_declaration. */
6609 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6610 complain, in_decl);
6611 TREE_CHAIN (r) = NULL_TREE;
6612 if (VOID_TYPE_P (type))
6613 error ("instantiation of %q+D as type %qT", r, type);
6615 break;
6617 case USING_DECL:
6618 /* We reach here only for member using decls. */
6619 if (DECL_DEPENDENT_P (t))
6621 r = do_class_using_decl
6622 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6623 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6624 if (!r)
6625 r = error_mark_node;
6627 else
6629 r = copy_node (t);
6630 TREE_CHAIN (r) = NULL_TREE;
6632 break;
6634 case TYPE_DECL:
6635 case VAR_DECL:
6637 tree argvec = NULL_TREE;
6638 tree gen_tmpl = NULL_TREE;
6639 tree spec;
6640 tree tmpl = NULL_TREE;
6641 tree ctx;
6642 tree type = NULL_TREE;
6643 int local_p;
6645 if (TREE_CODE (t) == TYPE_DECL)
6647 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6648 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6649 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6651 /* If this is the canonical decl, we don't have to
6652 mess with instantiations, and often we can't (for
6653 typename, template type parms and such). Note that
6654 TYPE_NAME is not correct for the above test if
6655 we've copied the type for a typedef. */
6656 r = TYPE_NAME (type);
6657 break;
6661 /* Assume this is a non-local variable. */
6662 local_p = 0;
6664 if (TYPE_P (CP_DECL_CONTEXT (t)))
6665 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6666 complain,
6667 in_decl, /*entering_scope=*/1);
6668 else if (DECL_NAMESPACE_SCOPE_P (t))
6669 ctx = DECL_CONTEXT (t);
6670 else
6672 /* Subsequent calls to pushdecl will fill this in. */
6673 ctx = NULL_TREE;
6674 local_p = 1;
6677 /* Check to see if we already have this specialization. */
6678 if (!local_p)
6680 tmpl = DECL_TI_TEMPLATE (t);
6681 gen_tmpl = most_general_template (tmpl);
6682 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6683 spec = retrieve_specialization (gen_tmpl, argvec,
6684 /*class_specializations_p=*/false);
6686 else
6687 spec = retrieve_local_specialization (t);
6689 if (spec)
6691 r = spec;
6692 break;
6695 r = copy_decl (t);
6696 if (TREE_CODE (r) == VAR_DECL)
6698 /* Even if the original location is out of scope, the
6699 newly substituted one is not. */
6700 DECL_DEAD_FOR_LOCAL (r) = 0;
6701 DECL_INITIALIZED_P (r) = 0;
6702 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6703 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6704 if (type == error_mark_node)
6705 return error_mark_node;
6706 type = complete_type (type);
6707 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6708 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6709 type = check_var_type (DECL_NAME (r), type);
6711 if (DECL_HAS_VALUE_EXPR_P (t))
6713 tree ve = DECL_VALUE_EXPR (t);
6714 ve = tsubst_expr (ve, args, complain, in_decl);
6715 SET_DECL_VALUE_EXPR (r, ve);
6718 else if (DECL_SELF_REFERENCE_P (t))
6719 SET_DECL_SELF_REFERENCE_P (r);
6720 TREE_TYPE (r) = type;
6721 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6722 DECL_CONTEXT (r) = ctx;
6723 /* Clear out the mangled name and RTL for the instantiation. */
6724 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6725 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6726 SET_DECL_RTL (r, NULL_RTX);
6727 /* The initializer must not be expanded until it is required;
6728 see [temp.inst]. */
6729 DECL_INITIAL (r) = NULL_TREE;
6730 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6731 SET_DECL_RTL (r, NULL_RTX);
6732 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6734 if (!local_p)
6736 /* A static data member declaration is always marked
6737 external when it is declared in-class, even if an
6738 initializer is present. We mimic the non-template
6739 processing here. */
6740 DECL_EXTERNAL (r) = 1;
6742 register_specialization (r, gen_tmpl, argvec, false);
6743 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6744 SET_DECL_IMPLICIT_INSTANTIATION (r);
6746 else
6747 register_local_specialization (r, t);
6749 TREE_CHAIN (r) = NULL_TREE;
6750 layout_decl (r, 0);
6752 break;
6754 default:
6755 gcc_unreachable ();
6758 /* Restore the file and line information. */
6759 input_location = saved_loc;
6761 return r;
6764 /* Substitute into the ARG_TYPES of a function type. */
6766 static tree
6767 tsubst_arg_types (tree arg_types,
6768 tree args,
6769 tsubst_flags_t complain,
6770 tree in_decl)
6772 tree remaining_arg_types;
6773 tree type;
6774 tree default_arg;
6775 tree result = NULL_TREE;
6777 if (!arg_types || arg_types == void_list_node)
6778 return arg_types;
6780 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6781 args, complain, in_decl);
6782 if (remaining_arg_types == error_mark_node)
6783 return error_mark_node;
6785 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6786 if (type == error_mark_node)
6787 return error_mark_node;
6788 if (VOID_TYPE_P (type))
6790 if (complain & tf_error)
6792 error ("invalid parameter type %qT", type);
6793 if (in_decl)
6794 error ("in declaration %q+D", in_decl);
6796 return error_mark_node;
6799 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6800 top-level qualifiers as required. */
6801 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6803 /* We do not substitute into default arguments here. The standard
6804 mandates that they be instantiated only when needed, which is
6805 done in build_over_call. */
6806 default_arg = TREE_PURPOSE (arg_types);
6808 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
6810 /* We've instantiated a template before its default arguments
6811 have been parsed. This can happen for a nested template
6812 class, and is not an error unless we require the default
6813 argument in a call of this function. */
6814 result = tree_cons (default_arg, type, remaining_arg_types);
6815 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
6817 else
6818 result = hash_tree_cons (default_arg, type, remaining_arg_types);
6820 return result;
6823 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6824 *not* handle the exception-specification for FNTYPE, because the
6825 initial substitution of explicitly provided template parameters
6826 during argument deduction forbids substitution into the
6827 exception-specification:
6829 [temp.deduct]
6831 All references in the function type of the function template to the
6832 corresponding template parameters are replaced by the specified tem-
6833 plate argument values. If a substitution in a template parameter or
6834 in the function type of the function template results in an invalid
6835 type, type deduction fails. [Note: The equivalent substitution in
6836 exception specifications is done only when the function is instanti-
6837 ated, at which point a program is ill-formed if the substitution
6838 results in an invalid type.] */
6840 static tree
6841 tsubst_function_type (tree t,
6842 tree args,
6843 tsubst_flags_t complain,
6844 tree in_decl)
6846 tree return_type;
6847 tree arg_types;
6848 tree fntype;
6850 /* The TYPE_CONTEXT is not used for function/method types. */
6851 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6853 /* Substitute the return type. */
6854 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6855 if (return_type == error_mark_node)
6856 return error_mark_node;
6857 /* The standard does not presently indicate that creation of a
6858 function type with an invalid return type is a deduction failure.
6859 However, that is clearly analogous to creating an array of "void"
6860 or a reference to a reference. This is core issue #486. */
6861 if (TREE_CODE (return_type) == ARRAY_TYPE
6862 || TREE_CODE (return_type) == FUNCTION_TYPE)
6864 if (complain & tf_error)
6866 if (TREE_CODE (return_type) == ARRAY_TYPE)
6867 error ("function returning an array");
6868 else
6869 error ("function returning a function");
6871 return error_mark_node;
6874 /* Substitute the argument types. */
6875 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6876 complain, in_decl);
6877 if (arg_types == error_mark_node)
6878 return error_mark_node;
6880 /* Construct a new type node and return it. */
6881 if (TREE_CODE (t) == FUNCTION_TYPE)
6882 fntype = build_function_type (return_type, arg_types);
6883 else
6885 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6886 if (! IS_AGGR_TYPE (r))
6888 /* [temp.deduct]
6890 Type deduction may fail for any of the following
6891 reasons:
6893 -- Attempting to create "pointer to member of T" when T
6894 is not a class type. */
6895 if (complain & tf_error)
6896 error ("creating pointer to member function of non-class type %qT",
6898 return error_mark_node;
6901 fntype = build_method_type_directly (r, return_type,
6902 TREE_CHAIN (arg_types));
6904 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6905 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6907 return fntype;
6910 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
6911 ARGS into that specification, and return the substituted
6912 specification. If there is no specification, return NULL_TREE. */
6914 static tree
6915 tsubst_exception_specification (tree fntype,
6916 tree args,
6917 tsubst_flags_t complain,
6918 tree in_decl)
6920 tree specs;
6921 tree new_specs;
6923 specs = TYPE_RAISES_EXCEPTIONS (fntype);
6924 new_specs = NULL_TREE;
6925 if (specs)
6927 if (! TREE_VALUE (specs))
6928 new_specs = specs;
6929 else
6930 while (specs)
6932 tree spec;
6933 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
6934 if (spec == error_mark_node)
6935 return spec;
6936 new_specs = add_exception_specifier (new_specs, spec, complain);
6937 specs = TREE_CHAIN (specs);
6940 return new_specs;
6943 /* Take the tree structure T and replace template parameters used
6944 therein with the argument vector ARGS. IN_DECL is an associated
6945 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6946 Issue error and warning messages under control of COMPLAIN. Note
6947 that we must be relatively non-tolerant of extensions here, in
6948 order to preserve conformance; if we allow substitutions that
6949 should not be allowed, we may allow argument deductions that should
6950 not succeed, and therefore report ambiguous overload situations
6951 where there are none. In theory, we could allow the substitution,
6952 but indicate that it should have failed, and allow our caller to
6953 make sure that the right thing happens, but we don't try to do this
6954 yet.
6956 This function is used for dealing with types, decls and the like;
6957 for expressions, use tsubst_expr or tsubst_copy. */
6959 static tree
6960 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6962 tree type, r;
6964 if (t == NULL_TREE || t == error_mark_node
6965 || t == integer_type_node
6966 || t == void_type_node
6967 || t == char_type_node
6968 || t == unknown_type_node
6969 || TREE_CODE (t) == NAMESPACE_DECL)
6970 return t;
6972 if (DECL_P (t))
6973 return tsubst_decl (t, args, complain);
6975 if (TREE_CODE (t) == IDENTIFIER_NODE)
6976 type = IDENTIFIER_TYPE_VALUE (t);
6977 else
6978 type = TREE_TYPE (t);
6980 gcc_assert (type != unknown_type_node);
6982 if (type
6983 && TREE_CODE (t) != TYPENAME_TYPE
6984 && TREE_CODE (t) != IDENTIFIER_NODE
6985 && TREE_CODE (t) != FUNCTION_TYPE
6986 && TREE_CODE (t) != METHOD_TYPE)
6987 type = tsubst (type, args, complain, in_decl);
6988 if (type == error_mark_node)
6989 return error_mark_node;
6991 switch (TREE_CODE (t))
6993 case RECORD_TYPE:
6994 case UNION_TYPE:
6995 case ENUMERAL_TYPE:
6996 return tsubst_aggr_type (t, args, complain, in_decl,
6997 /*entering_scope=*/0);
6999 case ERROR_MARK:
7000 case IDENTIFIER_NODE:
7001 case VOID_TYPE:
7002 case REAL_TYPE:
7003 case COMPLEX_TYPE:
7004 case VECTOR_TYPE:
7005 case BOOLEAN_TYPE:
7006 case INTEGER_CST:
7007 case REAL_CST:
7008 case STRING_CST:
7009 return t;
7011 case INTEGER_TYPE:
7012 if (t == integer_type_node)
7013 return t;
7015 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7016 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7017 return t;
7020 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7022 /* The array dimension behaves like a non-type template arg,
7023 in that we want to fold it as much as possible. */
7024 max = tsubst_template_arg (omax, args, complain, in_decl);
7025 max = fold_decl_constant_value (max);
7027 /* [temp.deduct]
7029 Type deduction may fail for any of the following
7030 reasons:
7032 Attempting to create an array with a size that is
7033 zero or negative. */
7034 if (integer_zerop (max) && !(complain & tf_error))
7035 /* We must fail if performing argument deduction (as
7036 indicated by the state of complain), so that
7037 another substitution can be found. */
7038 return error_mark_node;
7040 else if (TREE_CODE (max) == INTEGER_CST
7041 && INT_CST_LT (max, integer_zero_node))
7043 if (complain & tf_error)
7044 error ("creating array with negative size (%qE)", max);
7046 return error_mark_node;
7049 return compute_array_index_type (NULL_TREE, max);
7052 case TEMPLATE_TYPE_PARM:
7053 case TEMPLATE_TEMPLATE_PARM:
7054 case BOUND_TEMPLATE_TEMPLATE_PARM:
7055 case TEMPLATE_PARM_INDEX:
7057 int idx;
7058 int level;
7059 int levels;
7060 tree arg = NULL_TREE;
7062 r = NULL_TREE;
7064 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7065 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7066 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7067 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7069 idx = TEMPLATE_TYPE_IDX (t);
7070 level = TEMPLATE_TYPE_LEVEL (t);
7072 else
7074 idx = TEMPLATE_PARM_IDX (t);
7075 level = TEMPLATE_PARM_LEVEL (t);
7078 levels = TMPL_ARGS_DEPTH (args);
7079 if (level <= levels)
7080 arg = TMPL_ARG (args, level, idx);
7082 if (arg == error_mark_node)
7083 return error_mark_node;
7084 else if (arg != NULL_TREE)
7086 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7088 gcc_assert (TYPE_P (arg));
7089 return cp_build_qualified_type_real
7090 (arg, cp_type_quals (arg) | cp_type_quals (t),
7091 complain | tf_ignore_bad_quals);
7093 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7095 /* We are processing a type constructed from a
7096 template template parameter. */
7097 tree argvec = tsubst (TYPE_TI_ARGS (t),
7098 args, complain, in_decl);
7099 if (argvec == error_mark_node)
7100 return error_mark_node;
7102 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7103 are resolving nested-types in the signature of a
7104 member function templates. Otherwise ARG is a
7105 TEMPLATE_DECL and is the real template to be
7106 instantiated. */
7107 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7108 arg = TYPE_NAME (arg);
7110 r = lookup_template_class (arg,
7111 argvec, in_decl,
7112 DECL_CONTEXT (arg),
7113 /*entering_scope=*/0,
7114 complain);
7115 return cp_build_qualified_type_real
7116 (r, TYPE_QUALS (t), complain);
7118 else
7119 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7120 return arg;
7123 if (level == 1)
7124 /* This can happen during the attempted tsubst'ing in
7125 unify. This means that we don't yet have any information
7126 about the template parameter in question. */
7127 return t;
7129 /* If we get here, we must have been looking at a parm for a
7130 more deeply nested template. Make a new version of this
7131 template parameter, but with a lower level. */
7132 switch (TREE_CODE (t))
7134 case TEMPLATE_TYPE_PARM:
7135 case TEMPLATE_TEMPLATE_PARM:
7136 case BOUND_TEMPLATE_TEMPLATE_PARM:
7137 if (cp_type_quals (t))
7139 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7140 r = cp_build_qualified_type_real
7141 (r, cp_type_quals (t),
7142 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7143 ? tf_ignore_bad_quals : 0));
7145 else
7147 r = copy_type (t);
7148 TEMPLATE_TYPE_PARM_INDEX (r)
7149 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7150 r, levels);
7151 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7152 TYPE_MAIN_VARIANT (r) = r;
7153 TYPE_POINTER_TO (r) = NULL_TREE;
7154 TYPE_REFERENCE_TO (r) = NULL_TREE;
7156 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7158 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7159 complain, in_decl);
7160 if (argvec == error_mark_node)
7161 return error_mark_node;
7163 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7164 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7167 break;
7169 case TEMPLATE_PARM_INDEX:
7170 r = reduce_template_parm_level (t, type, levels);
7171 break;
7173 default:
7174 gcc_unreachable ();
7177 return r;
7180 case TREE_LIST:
7182 tree purpose, value, chain;
7184 if (t == void_list_node)
7185 return t;
7187 purpose = TREE_PURPOSE (t);
7188 if (purpose)
7190 purpose = tsubst (purpose, args, complain, in_decl);
7191 if (purpose == error_mark_node)
7192 return error_mark_node;
7194 value = TREE_VALUE (t);
7195 if (value)
7197 value = tsubst (value, args, complain, in_decl);
7198 if (value == error_mark_node)
7199 return error_mark_node;
7201 chain = TREE_CHAIN (t);
7202 if (chain && chain != void_type_node)
7204 chain = tsubst (chain, args, complain, in_decl);
7205 if (chain == error_mark_node)
7206 return error_mark_node;
7208 if (purpose == TREE_PURPOSE (t)
7209 && value == TREE_VALUE (t)
7210 && chain == TREE_CHAIN (t))
7211 return t;
7212 return hash_tree_cons (purpose, value, chain);
7215 case TREE_BINFO:
7216 /* We should never be tsubsting a binfo. */
7217 gcc_unreachable ();
7219 case TREE_VEC:
7220 /* A vector of template arguments. */
7221 gcc_assert (!type);
7222 return tsubst_template_args (t, args, complain, in_decl);
7224 case POINTER_TYPE:
7225 case REFERENCE_TYPE:
7227 enum tree_code code;
7229 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7230 return t;
7232 code = TREE_CODE (t);
7235 /* [temp.deduct]
7237 Type deduction may fail for any of the following
7238 reasons:
7240 -- Attempting to create a pointer to reference type.
7241 -- Attempting to create a reference to a reference type or
7242 a reference to void. */
7243 if (TREE_CODE (type) == REFERENCE_TYPE
7244 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7246 static location_t last_loc;
7248 /* We keep track of the last time we issued this error
7249 message to avoid spewing a ton of messages during a
7250 single bad template instantiation. */
7251 if (complain & tf_error
7252 #ifdef USE_MAPPED_LOCATION
7253 && last_loc != input_location
7254 #else
7255 && (last_loc.line != input_line
7256 || last_loc.file != input_filename)
7257 #endif
7260 if (TREE_CODE (type) == VOID_TYPE)
7261 error ("forming reference to void");
7262 else
7263 error ("forming %s to reference type %qT",
7264 (code == POINTER_TYPE) ? "pointer" : "reference",
7265 type);
7266 last_loc = input_location;
7269 return error_mark_node;
7271 else if (code == POINTER_TYPE)
7273 r = build_pointer_type (type);
7274 if (TREE_CODE (type) == METHOD_TYPE)
7275 r = build_ptrmemfunc_type (r);
7277 else
7278 r = build_reference_type (type);
7279 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7281 if (r != error_mark_node)
7282 /* Will this ever be needed for TYPE_..._TO values? */
7283 layout_type (r);
7285 return r;
7287 case OFFSET_TYPE:
7289 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7290 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7292 /* [temp.deduct]
7294 Type deduction may fail for any of the following
7295 reasons:
7297 -- Attempting to create "pointer to member of T" when T
7298 is not a class type. */
7299 if (complain & tf_error)
7300 error ("creating pointer to member of non-class type %qT", r);
7301 return error_mark_node;
7303 if (TREE_CODE (type) == REFERENCE_TYPE)
7305 if (complain & tf_error)
7306 error ("creating pointer to member reference type %qT", type);
7307 return error_mark_node;
7309 if (TREE_CODE (type) == VOID_TYPE)
7311 if (complain & tf_error)
7312 error ("creating pointer to member of type void");
7313 return error_mark_node;
7315 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7316 if (TREE_CODE (type) == FUNCTION_TYPE)
7318 /* The type of the implicit object parameter gets its
7319 cv-qualifiers from the FUNCTION_TYPE. */
7320 tree method_type;
7321 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7322 cp_type_quals (type));
7323 tree memptr;
7324 method_type = build_method_type_directly (this_type,
7325 TREE_TYPE (type),
7326 TYPE_ARG_TYPES (type));
7327 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7328 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7329 complain);
7331 else
7332 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7333 TYPE_QUALS (t),
7334 complain);
7336 case FUNCTION_TYPE:
7337 case METHOD_TYPE:
7339 tree fntype;
7340 tree specs;
7341 fntype = tsubst_function_type (t, args, complain, in_decl);
7342 if (fntype == error_mark_node)
7343 return error_mark_node;
7345 /* Substitute the exception specification. */
7346 specs = tsubst_exception_specification (t, args, complain,
7347 in_decl);
7348 if (specs == error_mark_node)
7349 return error_mark_node;
7350 if (specs)
7351 fntype = build_exception_variant (fntype, specs);
7352 return fntype;
7354 case ARRAY_TYPE:
7356 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7357 if (domain == error_mark_node)
7358 return error_mark_node;
7360 /* As an optimization, we avoid regenerating the array type if
7361 it will obviously be the same as T. */
7362 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7363 return t;
7365 /* These checks should match the ones in grokdeclarator.
7367 [temp.deduct]
7369 The deduction may fail for any of the following reasons:
7371 -- Attempting to create an array with an element type that
7372 is void, a function type, or a reference type, or [DR337]
7373 an abstract class type. */
7374 if (TREE_CODE (type) == VOID_TYPE
7375 || TREE_CODE (type) == FUNCTION_TYPE
7376 || TREE_CODE (type) == REFERENCE_TYPE)
7378 if (complain & tf_error)
7379 error ("creating array of %qT", type);
7380 return error_mark_node;
7382 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7384 if (complain & tf_error)
7385 error ("creating array of %qT, which is an abstract class type",
7386 type);
7387 return error_mark_node;
7390 r = build_cplus_array_type (type, domain);
7391 return r;
7394 case PLUS_EXPR:
7395 case MINUS_EXPR:
7397 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7398 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7400 if (e1 == error_mark_node || e2 == error_mark_node)
7401 return error_mark_node;
7403 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7406 case NEGATE_EXPR:
7407 case NOP_EXPR:
7409 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7410 if (e == error_mark_node)
7411 return error_mark_node;
7413 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7416 case TYPENAME_TYPE:
7418 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7419 in_decl, /*entering_scope=*/1);
7420 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7421 complain, in_decl);
7423 if (ctx == error_mark_node || f == error_mark_node)
7424 return error_mark_node;
7426 if (!IS_AGGR_TYPE (ctx))
7428 if (complain & tf_error)
7429 error ("%qT is not a class, struct, or union type", ctx);
7430 return error_mark_node;
7432 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7434 /* Normally, make_typename_type does not require that the CTX
7435 have complete type in order to allow things like:
7437 template <class T> struct S { typename S<T>::X Y; };
7439 But, such constructs have already been resolved by this
7440 point, so here CTX really should have complete type, unless
7441 it's a partial instantiation. */
7442 ctx = complete_type (ctx);
7443 if (!COMPLETE_TYPE_P (ctx))
7445 if (complain & tf_error)
7446 cxx_incomplete_type_error (NULL_TREE, ctx);
7447 return error_mark_node;
7451 f = make_typename_type (ctx, f, typename_type,
7452 (complain & tf_error) | tf_keep_type_decl);
7453 if (f == error_mark_node)
7454 return f;
7455 if (TREE_CODE (f) == TYPE_DECL)
7457 complain |= tf_ignore_bad_quals;
7458 f = TREE_TYPE (f);
7461 if (TREE_CODE (f) != TYPENAME_TYPE)
7463 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7464 error ("%qT resolves to %qT, which is not an enumeration type",
7465 t, f);
7466 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7467 error ("%qT resolves to %qT, which is is not a class type",
7468 t, f);
7471 return cp_build_qualified_type_real
7472 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7475 case UNBOUND_CLASS_TEMPLATE:
7477 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7478 in_decl, /*entering_scope=*/1);
7479 tree name = TYPE_IDENTIFIER (t);
7480 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7482 if (ctx == error_mark_node || name == error_mark_node)
7483 return error_mark_node;
7485 if (parm_list)
7486 parm_list = tsubst_template_parms (parm_list, args, complain);
7487 return make_unbound_class_template (ctx, name, parm_list, complain);
7490 case INDIRECT_REF:
7491 case ADDR_EXPR:
7492 case CALL_EXPR:
7493 gcc_unreachable ();
7495 case ARRAY_REF:
7497 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7498 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7499 if (e1 == error_mark_node || e2 == error_mark_node)
7500 return error_mark_node;
7502 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7505 case SCOPE_REF:
7507 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7508 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7509 if (e1 == error_mark_node || e2 == error_mark_node)
7510 return error_mark_node;
7512 return build_qualified_name (/*type=*/NULL_TREE,
7513 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7516 case TYPEOF_TYPE:
7518 tree type;
7520 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7521 complain, in_decl));
7522 return cp_build_qualified_type_real (type,
7523 cp_type_quals (t)
7524 | cp_type_quals (type),
7525 complain);
7528 default:
7529 sorry ("use of %qs in template",
7530 tree_code_name [(int) TREE_CODE (t)]);
7531 return error_mark_node;
7535 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7536 type of the expression on the left-hand side of the "." or "->"
7537 operator. */
7539 static tree
7540 tsubst_baselink (tree baselink, tree object_type,
7541 tree args, tsubst_flags_t complain, tree in_decl)
7543 tree name;
7544 tree qualifying_scope;
7545 tree fns;
7546 tree template_args = 0;
7547 bool template_id_p = false;
7549 /* A baselink indicates a function from a base class. The
7550 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7551 non-dependent types; otherwise, the lookup could not have
7552 succeeded. However, they may indicate bases of the template
7553 class, rather than the instantiated class.
7555 In addition, lookups that were not ambiguous before may be
7556 ambiguous now. Therefore, we perform the lookup again. */
7557 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7558 fns = BASELINK_FUNCTIONS (baselink);
7559 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7561 template_id_p = true;
7562 template_args = TREE_OPERAND (fns, 1);
7563 fns = TREE_OPERAND (fns, 0);
7564 if (template_args)
7565 template_args = tsubst_template_args (template_args, args,
7566 complain, in_decl);
7568 name = DECL_NAME (get_first_fn (fns));
7569 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7571 /* If lookup found a single function, mark it as used at this
7572 point. (If it lookup found multiple functions the one selected
7573 later by overload resolution will be marked as used at that
7574 point.) */
7575 if (BASELINK_P (baselink))
7576 fns = BASELINK_FUNCTIONS (baselink);
7577 if (!template_id_p && !really_overloaded_fn (fns))
7578 mark_used (OVL_CURRENT (fns));
7580 /* Add back the template arguments, if present. */
7581 if (BASELINK_P (baselink) && template_id_p)
7582 BASELINK_FUNCTIONS (baselink)
7583 = build_nt (TEMPLATE_ID_EXPR,
7584 BASELINK_FUNCTIONS (baselink),
7585 template_args);
7587 if (!object_type)
7588 object_type = current_class_type;
7589 return adjust_result_of_qualified_name_lookup (baselink,
7590 qualifying_scope,
7591 object_type);
7594 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7595 true if the qualified-id will be a postfix-expression in-and-of
7596 itself; false if more of the postfix-expression follows the
7597 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7598 of "&". */
7600 static tree
7601 tsubst_qualified_id (tree qualified_id, tree args,
7602 tsubst_flags_t complain, tree in_decl,
7603 bool done, bool address_p)
7605 tree expr;
7606 tree scope;
7607 tree name;
7608 bool is_template;
7609 tree template_args;
7611 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7613 /* Figure out what name to look up. */
7614 name = TREE_OPERAND (qualified_id, 1);
7615 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7617 is_template = true;
7618 template_args = TREE_OPERAND (name, 1);
7619 if (template_args)
7620 template_args = tsubst_template_args (template_args, args,
7621 complain, in_decl);
7622 name = TREE_OPERAND (name, 0);
7624 else
7626 is_template = false;
7627 template_args = NULL_TREE;
7630 /* Substitute into the qualifying scope. When there are no ARGS, we
7631 are just trying to simplify a non-dependent expression. In that
7632 case the qualifying scope may be dependent, and, in any case,
7633 substituting will not help. */
7634 scope = TREE_OPERAND (qualified_id, 0);
7635 if (args)
7637 scope = tsubst (scope, args, complain, in_decl);
7638 expr = tsubst_copy (name, args, complain, in_decl);
7640 else
7641 expr = name;
7643 if (dependent_type_p (scope))
7644 return build_qualified_name (/*type=*/NULL_TREE,
7645 scope, expr,
7646 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7648 if (!BASELINK_P (name) && !DECL_P (expr))
7650 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7651 /* If this were actually a destructor call, it would have been
7652 parsed as such by the parser. */
7653 expr = error_mark_node;
7654 else
7655 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7656 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7657 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7659 if (complain & tf_error)
7661 error ("dependent-name %qE is parsed as a non-type, but "
7662 "instantiation yields a type", qualified_id);
7663 inform ("say %<typename %E%> if a type is meant", qualified_id);
7665 return error_mark_node;
7669 if (DECL_P (expr))
7671 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7672 scope);
7673 /* Remember that there was a reference to this entity. */
7674 mark_used (expr);
7677 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7679 if (complain & tf_error)
7680 qualified_name_lookup_error (scope,
7681 TREE_OPERAND (qualified_id, 1),
7682 expr);
7683 return error_mark_node;
7686 if (is_template)
7687 expr = lookup_template_function (expr, template_args);
7689 if (expr == error_mark_node && complain & tf_error)
7690 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7691 expr);
7692 else if (TYPE_P (scope))
7694 expr = (adjust_result_of_qualified_name_lookup
7695 (expr, scope, current_class_type));
7696 expr = (finish_qualified_id_expr
7697 (scope, expr, done, address_p,
7698 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7699 /*template_arg_p=*/false));
7702 if (TREE_CODE (expr) != SCOPE_REF)
7703 expr = convert_from_reference (expr);
7705 return expr;
7708 /* Like tsubst, but deals with expressions. This function just replaces
7709 template parms; to finish processing the resultant expression, use
7710 tsubst_expr. */
7712 static tree
7713 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7715 enum tree_code code;
7716 tree r;
7718 if (t == NULL_TREE || t == error_mark_node)
7719 return t;
7721 code = TREE_CODE (t);
7723 switch (code)
7725 case PARM_DECL:
7726 r = retrieve_local_specialization (t);
7727 gcc_assert (r != NULL);
7728 mark_used (r);
7729 return r;
7731 case CONST_DECL:
7733 tree enum_type;
7734 tree v;
7736 if (DECL_TEMPLATE_PARM_P (t))
7737 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7738 /* There is no need to substitute into namespace-scope
7739 enumerators. */
7740 if (DECL_NAMESPACE_SCOPE_P (t))
7741 return t;
7742 /* If ARGS is NULL, then T is known to be non-dependent. */
7743 if (args == NULL_TREE)
7744 return integral_constant_value (t);
7746 /* Unfortunately, we cannot just call lookup_name here.
7747 Consider:
7749 template <int I> int f() {
7750 enum E { a = I };
7751 struct S { void g() { E e = a; } };
7754 When we instantiate f<7>::S::g(), say, lookup_name is not
7755 clever enough to find f<7>::a. */
7756 enum_type
7757 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7758 /*entering_scope=*/0);
7760 for (v = TYPE_VALUES (enum_type);
7761 v != NULL_TREE;
7762 v = TREE_CHAIN (v))
7763 if (TREE_PURPOSE (v) == DECL_NAME (t))
7764 return TREE_VALUE (v);
7766 /* We didn't find the name. That should never happen; if
7767 name-lookup found it during preliminary parsing, we
7768 should find it again here during instantiation. */
7769 gcc_unreachable ();
7771 return t;
7773 case FIELD_DECL:
7774 if (DECL_CONTEXT (t))
7776 tree ctx;
7778 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7779 /*entering_scope=*/1);
7780 if (ctx != DECL_CONTEXT (t))
7782 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7783 if (!r)
7785 if (complain & tf_error)
7786 error ("using invalid field %qD", t);
7787 return error_mark_node;
7789 return r;
7793 return t;
7795 case VAR_DECL:
7796 case FUNCTION_DECL:
7797 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7798 || local_variable_p (t))
7799 t = tsubst (t, args, complain, in_decl);
7800 mark_used (t);
7801 return t;
7803 case BASELINK:
7804 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7806 case TEMPLATE_DECL:
7807 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7808 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7809 args, complain, in_decl);
7810 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
7811 return tsubst (t, args, complain, in_decl);
7812 else if (DECL_CLASS_SCOPE_P (t)
7813 && uses_template_parms (DECL_CONTEXT (t)))
7815 /* Template template argument like the following example need
7816 special treatment:
7818 template <template <class> class TT> struct C {};
7819 template <class T> struct D {
7820 template <class U> struct E {};
7821 C<E> c; // #1
7823 D<int> d; // #2
7825 We are processing the template argument `E' in #1 for
7826 the template instantiation #2. Originally, `E' is a
7827 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7828 have to substitute this with one having context `D<int>'. */
7830 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7831 return lookup_field (context, DECL_NAME(t), 0, false);
7833 else
7834 /* Ordinary template template argument. */
7835 return t;
7837 case CAST_EXPR:
7838 case REINTERPRET_CAST_EXPR:
7839 case CONST_CAST_EXPR:
7840 case STATIC_CAST_EXPR:
7841 case DYNAMIC_CAST_EXPR:
7842 case NOP_EXPR:
7843 return build1
7844 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7845 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7847 case INDIRECT_REF:
7848 case NEGATE_EXPR:
7849 case TRUTH_NOT_EXPR:
7850 case BIT_NOT_EXPR:
7851 case ADDR_EXPR:
7852 case UNARY_PLUS_EXPR: /* Unary + */
7853 case SIZEOF_EXPR:
7854 case ALIGNOF_EXPR:
7855 case ARROW_EXPR:
7856 case THROW_EXPR:
7857 case TYPEID_EXPR:
7858 case REALPART_EXPR:
7859 case IMAGPART_EXPR:
7860 return build1
7861 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7862 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7864 case COMPONENT_REF:
7866 tree object;
7867 tree name;
7869 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7870 name = TREE_OPERAND (t, 1);
7871 if (TREE_CODE (name) == BIT_NOT_EXPR)
7873 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7874 complain, in_decl);
7875 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7877 else if (TREE_CODE (name) == SCOPE_REF
7878 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7880 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7881 complain, in_decl);
7882 name = TREE_OPERAND (name, 1);
7883 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7884 complain, in_decl);
7885 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7886 name = build_qualified_name (/*type=*/NULL_TREE,
7887 base, name,
7888 /*template_p=*/false);
7890 else if (TREE_CODE (name) == BASELINK)
7891 name = tsubst_baselink (name,
7892 non_reference (TREE_TYPE (object)),
7893 args, complain,
7894 in_decl);
7895 else
7896 name = tsubst_copy (name, args, complain, in_decl);
7897 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7900 case PLUS_EXPR:
7901 case MINUS_EXPR:
7902 case MULT_EXPR:
7903 case TRUNC_DIV_EXPR:
7904 case CEIL_DIV_EXPR:
7905 case FLOOR_DIV_EXPR:
7906 case ROUND_DIV_EXPR:
7907 case EXACT_DIV_EXPR:
7908 case BIT_AND_EXPR:
7909 case BIT_IOR_EXPR:
7910 case BIT_XOR_EXPR:
7911 case TRUNC_MOD_EXPR:
7912 case FLOOR_MOD_EXPR:
7913 case TRUTH_ANDIF_EXPR:
7914 case TRUTH_ORIF_EXPR:
7915 case TRUTH_AND_EXPR:
7916 case TRUTH_OR_EXPR:
7917 case RSHIFT_EXPR:
7918 case LSHIFT_EXPR:
7919 case RROTATE_EXPR:
7920 case LROTATE_EXPR:
7921 case EQ_EXPR:
7922 case NE_EXPR:
7923 case MAX_EXPR:
7924 case MIN_EXPR:
7925 case LE_EXPR:
7926 case GE_EXPR:
7927 case LT_EXPR:
7928 case GT_EXPR:
7929 case COMPOUND_EXPR:
7930 case DOTSTAR_EXPR:
7931 case MEMBER_REF:
7932 case PREDECREMENT_EXPR:
7933 case PREINCREMENT_EXPR:
7934 case POSTDECREMENT_EXPR:
7935 case POSTINCREMENT_EXPR:
7936 return build_nt
7937 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7938 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7940 case SCOPE_REF:
7941 return build_qualified_name (/*type=*/NULL_TREE,
7942 tsubst_copy (TREE_OPERAND (t, 0),
7943 args, complain, in_decl),
7944 tsubst_copy (TREE_OPERAND (t, 1),
7945 args, complain, in_decl),
7946 QUALIFIED_NAME_IS_TEMPLATE (t));
7948 case ARRAY_REF:
7949 return build_nt
7950 (ARRAY_REF,
7951 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7952 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7953 NULL_TREE, NULL_TREE);
7955 case CALL_EXPR:
7956 return build_nt (code,
7957 tsubst_copy (TREE_OPERAND (t, 0), args,
7958 complain, in_decl),
7959 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7960 in_decl),
7961 NULL_TREE);
7963 case COND_EXPR:
7964 case MODOP_EXPR:
7965 case PSEUDO_DTOR_EXPR:
7967 r = build_nt
7968 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7969 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7970 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7971 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7972 return r;
7975 case NEW_EXPR:
7977 r = build_nt
7978 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7979 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7980 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7981 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7982 return r;
7985 case DELETE_EXPR:
7987 r = build_nt
7988 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7989 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7990 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7991 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7992 return r;
7995 case TEMPLATE_ID_EXPR:
7997 /* Substituted template arguments */
7998 tree fn = TREE_OPERAND (t, 0);
7999 tree targs = TREE_OPERAND (t, 1);
8001 fn = tsubst_copy (fn, args, complain, in_decl);
8002 if (targs)
8003 targs = tsubst_template_args (targs, args, complain, in_decl);
8005 return lookup_template_function (fn, targs);
8008 case TREE_LIST:
8010 tree purpose, value, chain;
8012 if (t == void_list_node)
8013 return t;
8015 purpose = TREE_PURPOSE (t);
8016 if (purpose)
8017 purpose = tsubst_copy (purpose, args, complain, in_decl);
8018 value = TREE_VALUE (t);
8019 if (value)
8020 value = tsubst_copy (value, args, complain, in_decl);
8021 chain = TREE_CHAIN (t);
8022 if (chain && chain != void_type_node)
8023 chain = tsubst_copy (chain, args, complain, in_decl);
8024 if (purpose == TREE_PURPOSE (t)
8025 && value == TREE_VALUE (t)
8026 && chain == TREE_CHAIN (t))
8027 return t;
8028 return tree_cons (purpose, value, chain);
8031 case RECORD_TYPE:
8032 case UNION_TYPE:
8033 case ENUMERAL_TYPE:
8034 case INTEGER_TYPE:
8035 case TEMPLATE_TYPE_PARM:
8036 case TEMPLATE_TEMPLATE_PARM:
8037 case BOUND_TEMPLATE_TEMPLATE_PARM:
8038 case TEMPLATE_PARM_INDEX:
8039 case POINTER_TYPE:
8040 case REFERENCE_TYPE:
8041 case OFFSET_TYPE:
8042 case FUNCTION_TYPE:
8043 case METHOD_TYPE:
8044 case ARRAY_TYPE:
8045 case TYPENAME_TYPE:
8046 case UNBOUND_CLASS_TEMPLATE:
8047 case TYPEOF_TYPE:
8048 case TYPE_DECL:
8049 return tsubst (t, args, complain, in_decl);
8051 case IDENTIFIER_NODE:
8052 if (IDENTIFIER_TYPENAME_P (t))
8054 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8055 return mangle_conv_op_name_for_type (new_type);
8057 else
8058 return t;
8060 case CONSTRUCTOR:
8061 /* This is handled by tsubst_copy_and_build. */
8062 gcc_unreachable ();
8064 case VA_ARG_EXPR:
8065 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8066 in_decl),
8067 tsubst (TREE_TYPE (t), args, complain, in_decl));
8069 case CLEANUP_POINT_EXPR:
8070 /* We shouldn't have built any of these during initial template
8071 generation. Instead, they should be built during instantiation
8072 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8073 gcc_unreachable ();
8075 case OFFSET_REF:
8076 mark_used (TREE_OPERAND (t, 1));
8077 return t;
8079 default:
8080 return t;
8084 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8086 static tree
8087 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8088 tree in_decl)
8090 tree new_clauses = NULL, nc, oc;
8092 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8094 nc = copy_node (oc);
8095 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8096 new_clauses = nc;
8098 switch (OMP_CLAUSE_CODE (nc))
8100 case OMP_CLAUSE_PRIVATE:
8101 case OMP_CLAUSE_SHARED:
8102 case OMP_CLAUSE_FIRSTPRIVATE:
8103 case OMP_CLAUSE_LASTPRIVATE:
8104 case OMP_CLAUSE_REDUCTION:
8105 case OMP_CLAUSE_COPYIN:
8106 case OMP_CLAUSE_COPYPRIVATE:
8107 case OMP_CLAUSE_IF:
8108 case OMP_CLAUSE_NUM_THREADS:
8109 case OMP_CLAUSE_SCHEDULE:
8110 OMP_CLAUSE_OPERAND (nc, 0)
8111 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
8112 break;
8113 case OMP_CLAUSE_NOWAIT:
8114 case OMP_CLAUSE_ORDERED:
8115 case OMP_CLAUSE_DEFAULT:
8116 break;
8117 default:
8118 gcc_unreachable ();
8122 return finish_omp_clauses (nreverse (new_clauses));
8125 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8127 static tree
8128 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8129 tree in_decl)
8131 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8133 tree purpose, value, chain;
8135 if (t == NULL)
8136 return t;
8138 if (TREE_CODE (t) != TREE_LIST)
8139 return tsubst_copy_and_build (t, args, complain, in_decl,
8140 /*function_p=*/false);
8142 if (t == void_list_node)
8143 return t;
8145 purpose = TREE_PURPOSE (t);
8146 if (purpose)
8147 purpose = RECUR (purpose);
8148 value = TREE_VALUE (t);
8149 if (value)
8150 value = RECUR (value);
8151 chain = TREE_CHAIN (t);
8152 if (chain && chain != void_type_node)
8153 chain = RECUR (chain);
8154 return tree_cons (purpose, value, chain);
8155 #undef RECUR
8158 /* Like tsubst_copy for expressions, etc. but also does semantic
8159 processing. */
8161 static tree
8162 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8164 tree stmt, tmp;
8166 if (t == NULL_TREE || t == error_mark_node)
8167 return t;
8169 if (EXPR_HAS_LOCATION (t))
8170 input_location = EXPR_LOCATION (t);
8171 if (STATEMENT_CODE_P (TREE_CODE (t)))
8172 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8174 switch (TREE_CODE (t))
8176 case STATEMENT_LIST:
8178 tree_stmt_iterator i;
8179 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8180 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
8181 break;
8184 case CTOR_INITIALIZER:
8185 finish_mem_initializers (tsubst_initializer_list
8186 (TREE_OPERAND (t, 0), args));
8187 break;
8189 case RETURN_EXPR:
8190 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
8191 args, complain, in_decl));
8192 break;
8194 case EXPR_STMT:
8195 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
8196 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8197 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8198 else
8199 finish_expr_stmt (tmp);
8200 break;
8202 case USING_STMT:
8203 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
8204 args, complain, in_decl));
8205 break;
8207 case DECL_EXPR:
8209 tree decl;
8210 tree init;
8212 decl = DECL_EXPR_DECL (t);
8213 if (TREE_CODE (decl) == LABEL_DECL)
8214 finish_label_decl (DECL_NAME (decl));
8215 else if (TREE_CODE (decl) == USING_DECL)
8217 tree scope = USING_DECL_SCOPE (decl);
8218 tree name = DECL_NAME (decl);
8219 tree decl;
8221 scope = tsubst_expr (scope, args, complain, in_decl);
8222 decl = lookup_qualified_name (scope, name,
8223 /*is_type_p=*/false,
8224 /*complain=*/false);
8225 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8226 qualified_name_lookup_error (scope, name, decl);
8227 else
8228 do_local_using_decl (decl, scope, name);
8230 else
8232 init = DECL_INITIAL (decl);
8233 decl = tsubst (decl, args, complain, in_decl);
8234 if (decl != error_mark_node)
8236 if (init)
8237 DECL_INITIAL (decl) = error_mark_node;
8238 /* By marking the declaration as instantiated, we avoid
8239 trying to instantiate it. Since instantiate_decl can't
8240 handle local variables, and since we've already done
8241 all that needs to be done, that's the right thing to
8242 do. */
8243 if (TREE_CODE (decl) == VAR_DECL)
8244 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8245 if (TREE_CODE (decl) == VAR_DECL
8246 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8247 /* Anonymous aggregates are a special case. */
8248 finish_anon_union (decl);
8249 else
8251 maybe_push_decl (decl);
8252 if (TREE_CODE (decl) == VAR_DECL
8253 && DECL_PRETTY_FUNCTION_P (decl))
8255 /* For __PRETTY_FUNCTION__ we have to adjust the
8256 initializer. */
8257 const char *const name
8258 = cxx_printable_name (current_function_decl, 2);
8259 init = cp_fname_init (name, &TREE_TYPE (decl));
8261 else
8262 init = tsubst_expr (init, args, complain, in_decl);
8263 finish_decl (decl, init, NULL_TREE);
8268 /* A DECL_EXPR can also be used as an expression, in the condition
8269 clause of an if/for/while construct. */
8270 return decl;
8273 case FOR_STMT:
8274 stmt = begin_for_stmt ();
8275 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
8276 finish_for_init_stmt (stmt);
8277 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
8278 finish_for_cond (tmp, stmt);
8279 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
8280 finish_for_expr (tmp, stmt);
8281 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
8282 finish_for_stmt (stmt);
8283 break;
8285 case WHILE_STMT:
8286 stmt = begin_while_stmt ();
8287 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
8288 finish_while_stmt_cond (tmp, stmt);
8289 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
8290 finish_while_stmt (stmt);
8291 break;
8293 case DO_STMT:
8294 stmt = begin_do_stmt ();
8295 tsubst_expr (DO_BODY (t), args, complain, in_decl);
8296 finish_do_body (stmt);
8297 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
8298 finish_do_stmt (tmp, stmt);
8299 break;
8301 case IF_STMT:
8302 stmt = begin_if_stmt ();
8303 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
8304 finish_if_stmt_cond (tmp, stmt);
8305 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
8306 finish_then_clause (stmt);
8308 if (ELSE_CLAUSE (t))
8310 begin_else_clause (stmt);
8311 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
8312 finish_else_clause (stmt);
8315 finish_if_stmt (stmt);
8316 break;
8318 case BIND_EXPR:
8319 if (BIND_EXPR_BODY_BLOCK (t))
8320 stmt = begin_function_body ();
8321 else
8322 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8323 ? BCS_TRY_BLOCK : 0);
8325 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
8327 if (BIND_EXPR_BODY_BLOCK (t))
8328 finish_function_body (stmt);
8329 else
8330 finish_compound_stmt (stmt);
8331 break;
8333 case BREAK_STMT:
8334 finish_break_stmt ();
8335 break;
8337 case CONTINUE_STMT:
8338 finish_continue_stmt ();
8339 break;
8341 case SWITCH_STMT:
8342 stmt = begin_switch_stmt ();
8343 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
8344 finish_switch_cond (tmp, stmt);
8345 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
8346 finish_switch_stmt (stmt);
8347 break;
8349 case CASE_LABEL_EXPR:
8350 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
8351 tsubst_expr (CASE_HIGH (t), args, complain,
8352 in_decl));
8353 break;
8355 case LABEL_EXPR:
8356 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8357 break;
8359 case GOTO_EXPR:
8360 tmp = GOTO_DESTINATION (t);
8361 if (TREE_CODE (tmp) != LABEL_DECL)
8362 /* Computed goto's must be tsubst'd into. On the other hand,
8363 non-computed gotos must not be; the identifier in question
8364 will have no binding. */
8365 tmp = tsubst_expr (tmp, args, complain, in_decl);
8366 else
8367 tmp = DECL_NAME (tmp);
8368 finish_goto_stmt (tmp);
8369 break;
8371 case ASM_EXPR:
8372 tmp = finish_asm_stmt
8373 (ASM_VOLATILE_P (t),
8374 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8375 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8376 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8377 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8379 tree asm_expr = tmp;
8380 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8381 asm_expr = TREE_OPERAND (asm_expr, 0);
8382 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8384 break;
8386 case TRY_BLOCK:
8387 if (CLEANUP_P (t))
8389 stmt = begin_try_block ();
8390 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8391 finish_cleanup_try_block (stmt);
8392 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8393 complain, in_decl),
8394 stmt);
8396 else
8398 if (FN_TRY_BLOCK_P (t))
8399 stmt = begin_function_try_block ();
8400 else
8401 stmt = begin_try_block ();
8403 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8405 if (FN_TRY_BLOCK_P (t))
8406 finish_function_try_block (stmt);
8407 else
8408 finish_try_block (stmt);
8410 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8411 if (FN_TRY_BLOCK_P (t))
8412 finish_function_handler_sequence (stmt);
8413 else
8414 finish_handler_sequence (stmt);
8416 break;
8418 case HANDLER:
8420 tree decl;
8422 stmt = begin_handler ();
8423 if (HANDLER_PARMS (t))
8425 decl = HANDLER_PARMS (t);
8426 decl = tsubst (decl, args, complain, in_decl);
8427 /* Prevent instantiate_decl from trying to instantiate
8428 this variable. We've already done all that needs to be
8429 done. */
8430 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8432 else
8433 decl = NULL_TREE;
8434 finish_handler_parms (decl, stmt);
8435 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8436 finish_handler (stmt);
8438 break;
8440 case TAG_DEFN:
8441 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8442 break;
8444 case OMP_PARALLEL:
8445 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8446 args, complain, in_decl);
8447 stmt = begin_omp_parallel ();
8448 tsubst_expr (OMP_PARALLEL_BODY (t), args, complain, in_decl);
8449 finish_omp_parallel (tmp, stmt);
8450 break;
8452 case OMP_FOR:
8454 tree clauses, decl, init, cond, incr, body, pre_body;
8456 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8457 args, complain, in_decl);
8458 init = OMP_FOR_INIT (t);
8459 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8460 decl = tsubst_expr (TREE_OPERAND (init, 0), args, complain, in_decl);
8461 init = tsubst_expr (TREE_OPERAND (init, 1), args, complain, in_decl);
8462 cond = tsubst_expr (OMP_FOR_COND (t), args, complain, in_decl);
8463 incr = tsubst_expr (OMP_FOR_INCR (t), args, complain, in_decl);
8465 stmt = begin_omp_structured_block ();
8467 pre_body = push_stmt_list ();
8468 tsubst_expr (OMP_FOR_PRE_BODY (t), args, complain, in_decl);
8469 pre_body = pop_stmt_list (pre_body);
8471 body = push_stmt_list ();
8472 tsubst_expr (OMP_FOR_BODY (t), args, complain, in_decl);
8473 body = pop_stmt_list (body);
8475 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8476 pre_body);
8477 if (t)
8478 OMP_FOR_CLAUSES (t) = clauses;
8480 add_stmt (finish_omp_structured_block (stmt));
8482 break;
8484 case OMP_SECTIONS:
8485 case OMP_SINGLE:
8486 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8487 stmt = push_stmt_list ();
8488 tsubst_expr (OMP_BODY (t), args, complain, in_decl);
8489 stmt = pop_stmt_list (stmt);
8491 t = copy_node (t);
8492 OMP_BODY (t) = stmt;
8493 OMP_CLAUSES (t) = tmp;
8494 add_stmt (t);
8495 break;
8497 case OMP_SECTION:
8498 case OMP_CRITICAL:
8499 case OMP_MASTER:
8500 case OMP_ORDERED:
8501 stmt = push_stmt_list ();
8502 tsubst_expr (OMP_BODY (t), args, complain, in_decl);
8503 stmt = pop_stmt_list (stmt);
8505 t = copy_node (t);
8506 OMP_BODY (t) = stmt;
8507 add_stmt (t);
8508 break;
8510 case OMP_ATOMIC:
8512 tree op0, op1;
8513 op0 = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl);
8514 op1 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
8515 if (OMP_ATOMIC_DEPENDENT_P (t))
8516 c_finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
8517 else
8518 add_stmt (build2 (OMP_ATOMIC, void_type_node, op0, op1));
8520 break;
8522 default:
8523 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8525 return tsubst_copy_and_build (t, args, complain, in_decl,
8526 /*function_p=*/false);
8529 return NULL_TREE;
8532 /* T is a postfix-expression that is not being used in a function
8533 call. Return the substituted version of T. */
8535 static tree
8536 tsubst_non_call_postfix_expression (tree t, tree args,
8537 tsubst_flags_t complain,
8538 tree in_decl)
8540 if (TREE_CODE (t) == SCOPE_REF)
8541 t = tsubst_qualified_id (t, args, complain, in_decl,
8542 /*done=*/false, /*address_p=*/false);
8543 else
8544 t = tsubst_copy_and_build (t, args, complain, in_decl,
8545 /*function_p=*/false);
8547 return t;
8550 /* Like tsubst but deals with expressions and performs semantic
8551 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8553 tree
8554 tsubst_copy_and_build (tree t,
8555 tree args,
8556 tsubst_flags_t complain,
8557 tree in_decl,
8558 bool function_p)
8560 #define RECUR(NODE) \
8561 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8563 tree op1;
8565 if (t == NULL_TREE || t == error_mark_node)
8566 return t;
8568 switch (TREE_CODE (t))
8570 case USING_DECL:
8571 t = DECL_NAME (t);
8572 /* Fall through. */
8573 case IDENTIFIER_NODE:
8575 tree decl;
8576 cp_id_kind idk;
8577 bool non_integral_constant_expression_p;
8578 const char *error_msg;
8580 if (IDENTIFIER_TYPENAME_P (t))
8582 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8583 t = mangle_conv_op_name_for_type (new_type);
8586 /* Look up the name. */
8587 decl = lookup_name (t);
8589 /* By convention, expressions use ERROR_MARK_NODE to indicate
8590 failure, not NULL_TREE. */
8591 if (decl == NULL_TREE)
8592 decl = error_mark_node;
8594 decl = finish_id_expression (t, decl, NULL_TREE,
8595 &idk,
8596 /*integral_constant_expression_p=*/false,
8597 /*allow_non_integral_constant_expression_p=*/false,
8598 &non_integral_constant_expression_p,
8599 /*template_p=*/false,
8600 /*done=*/true,
8601 /*address_p=*/false,
8602 /*template_arg_p=*/false,
8603 &error_msg);
8604 if (error_msg)
8605 error (error_msg);
8606 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8607 decl = unqualified_name_lookup_error (decl);
8608 return decl;
8611 case TEMPLATE_ID_EXPR:
8613 tree object;
8614 tree template = RECUR (TREE_OPERAND (t, 0));
8615 tree targs = TREE_OPERAND (t, 1);
8617 if (targs)
8618 targs = tsubst_template_args (targs, args, complain, in_decl);
8620 if (TREE_CODE (template) == COMPONENT_REF)
8622 object = TREE_OPERAND (template, 0);
8623 template = TREE_OPERAND (template, 1);
8625 else
8626 object = NULL_TREE;
8627 template = lookup_template_function (template, targs);
8629 if (object)
8630 return build3 (COMPONENT_REF, TREE_TYPE (template),
8631 object, template, NULL_TREE);
8632 else
8633 return template;
8636 case INDIRECT_REF:
8638 tree r = RECUR (TREE_OPERAND (t, 0));
8640 if (REFERENCE_REF_P (t))
8642 /* A type conversion to reference type will be enclosed in
8643 such an indirect ref, but the substitution of the cast
8644 will have also added such an indirect ref. */
8645 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8646 r = convert_from_reference (r);
8648 else
8649 r = build_x_indirect_ref (r, "unary *");
8650 return r;
8653 case NOP_EXPR:
8654 return build_nop
8655 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8656 RECUR (TREE_OPERAND (t, 0)));
8658 case CAST_EXPR:
8659 return build_functional_cast
8660 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8661 RECUR (TREE_OPERAND (t, 0)));
8663 case REINTERPRET_CAST_EXPR:
8664 return build_reinterpret_cast
8665 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8666 RECUR (TREE_OPERAND (t, 0)));
8668 case CONST_CAST_EXPR:
8669 return build_const_cast
8670 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8671 RECUR (TREE_OPERAND (t, 0)));
8673 case DYNAMIC_CAST_EXPR:
8674 return build_dynamic_cast
8675 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8676 RECUR (TREE_OPERAND (t, 0)));
8678 case STATIC_CAST_EXPR:
8679 return build_static_cast
8680 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8681 RECUR (TREE_OPERAND (t, 0)));
8683 case POSTDECREMENT_EXPR:
8684 case POSTINCREMENT_EXPR:
8685 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8686 args, complain, in_decl);
8687 return build_x_unary_op (TREE_CODE (t), op1);
8689 case PREDECREMENT_EXPR:
8690 case PREINCREMENT_EXPR:
8691 case NEGATE_EXPR:
8692 case BIT_NOT_EXPR:
8693 case ABS_EXPR:
8694 case TRUTH_NOT_EXPR:
8695 case UNARY_PLUS_EXPR: /* Unary + */
8696 case REALPART_EXPR:
8697 case IMAGPART_EXPR:
8698 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8700 case ADDR_EXPR:
8701 op1 = TREE_OPERAND (t, 0);
8702 if (TREE_CODE (op1) == SCOPE_REF)
8703 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8704 /*done=*/true, /*address_p=*/true);
8705 else
8706 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8707 in_decl);
8708 if (TREE_CODE (op1) == LABEL_DECL)
8709 return finish_label_address_expr (DECL_NAME (op1));
8710 return build_x_unary_op (ADDR_EXPR, op1);
8712 case PLUS_EXPR:
8713 case MINUS_EXPR:
8714 case MULT_EXPR:
8715 case TRUNC_DIV_EXPR:
8716 case CEIL_DIV_EXPR:
8717 case FLOOR_DIV_EXPR:
8718 case ROUND_DIV_EXPR:
8719 case EXACT_DIV_EXPR:
8720 case BIT_AND_EXPR:
8721 case BIT_IOR_EXPR:
8722 case BIT_XOR_EXPR:
8723 case TRUNC_MOD_EXPR:
8724 case FLOOR_MOD_EXPR:
8725 case TRUTH_ANDIF_EXPR:
8726 case TRUTH_ORIF_EXPR:
8727 case TRUTH_AND_EXPR:
8728 case TRUTH_OR_EXPR:
8729 case RSHIFT_EXPR:
8730 case LSHIFT_EXPR:
8731 case RROTATE_EXPR:
8732 case LROTATE_EXPR:
8733 case EQ_EXPR:
8734 case NE_EXPR:
8735 case MAX_EXPR:
8736 case MIN_EXPR:
8737 case LE_EXPR:
8738 case GE_EXPR:
8739 case LT_EXPR:
8740 case GT_EXPR:
8741 case MEMBER_REF:
8742 case DOTSTAR_EXPR:
8743 return build_x_binary_op
8744 (TREE_CODE (t),
8745 RECUR (TREE_OPERAND (t, 0)),
8746 RECUR (TREE_OPERAND (t, 1)),
8747 /*overloaded_p=*/NULL);
8749 case SCOPE_REF:
8750 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8751 /*address_p=*/false);
8752 case ARRAY_REF:
8753 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8754 args, complain, in_decl);
8755 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8756 /*overloaded_p=*/NULL);
8758 case SIZEOF_EXPR:
8759 case ALIGNOF_EXPR:
8760 op1 = TREE_OPERAND (t, 0);
8761 if (!args)
8763 /* When there are no ARGS, we are trying to evaluate a
8764 non-dependent expression from the parser. Trying to do
8765 the substitutions may not work. */
8766 if (!TYPE_P (op1))
8767 op1 = TREE_TYPE (op1);
8769 else
8771 ++skip_evaluation;
8772 op1 = RECUR (op1);
8773 --skip_evaluation;
8775 if (TYPE_P (op1))
8776 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8777 else
8778 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8780 case MODOP_EXPR:
8782 tree r = build_x_modify_expr
8783 (RECUR (TREE_OPERAND (t, 0)),
8784 TREE_CODE (TREE_OPERAND (t, 1)),
8785 RECUR (TREE_OPERAND (t, 2)));
8786 /* TREE_NO_WARNING must be set if either the expression was
8787 parenthesized or it uses an operator such as >>= rather
8788 than plain assignment. In the former case, it was already
8789 set and must be copied. In the latter case,
8790 build_x_modify_expr sets it and it must not be reset
8791 here. */
8792 if (TREE_NO_WARNING (t))
8793 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8794 return r;
8797 case ARROW_EXPR:
8798 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8799 args, complain, in_decl);
8800 /* Remember that there was a reference to this entity. */
8801 if (DECL_P (op1))
8802 mark_used (op1);
8803 return build_x_arrow (op1);
8805 case NEW_EXPR:
8806 return build_new
8807 (RECUR (TREE_OPERAND (t, 0)),
8808 RECUR (TREE_OPERAND (t, 1)),
8809 RECUR (TREE_OPERAND (t, 2)),
8810 RECUR (TREE_OPERAND (t, 3)),
8811 NEW_EXPR_USE_GLOBAL (t));
8813 case DELETE_EXPR:
8814 return delete_sanity
8815 (RECUR (TREE_OPERAND (t, 0)),
8816 RECUR (TREE_OPERAND (t, 1)),
8817 DELETE_EXPR_USE_VEC (t),
8818 DELETE_EXPR_USE_GLOBAL (t));
8820 case COMPOUND_EXPR:
8821 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8822 RECUR (TREE_OPERAND (t, 1)));
8824 case CALL_EXPR:
8826 tree function;
8827 tree call_args;
8828 bool qualified_p;
8829 bool koenig_p;
8831 function = TREE_OPERAND (t, 0);
8832 /* When we parsed the expression, we determined whether or
8833 not Koenig lookup should be performed. */
8834 koenig_p = KOENIG_LOOKUP_P (t);
8835 if (TREE_CODE (function) == SCOPE_REF)
8837 qualified_p = true;
8838 function = tsubst_qualified_id (function, args, complain, in_decl,
8839 /*done=*/false,
8840 /*address_p=*/false);
8842 else
8844 if (TREE_CODE (function) == COMPONENT_REF)
8846 tree op = TREE_OPERAND (function, 1);
8848 qualified_p = (TREE_CODE (op) == SCOPE_REF
8849 || (BASELINK_P (op)
8850 && BASELINK_QUALIFIED_P (op)));
8852 else
8853 qualified_p = false;
8855 function = tsubst_copy_and_build (function, args, complain,
8856 in_decl,
8857 !qualified_p);
8858 if (BASELINK_P (function))
8859 qualified_p = true;
8862 call_args = RECUR (TREE_OPERAND (t, 1));
8864 /* We do not perform argument-dependent lookup if normal
8865 lookup finds a non-function, in accordance with the
8866 expected resolution of DR 218. */
8867 if (koenig_p
8868 && ((is_overloaded_fn (function)
8869 /* If lookup found a member function, the Koenig lookup is
8870 not appropriate, even if an unqualified-name was used
8871 to denote the function. */
8872 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
8873 || TREE_CODE (function) == IDENTIFIER_NODE))
8874 function = perform_koenig_lookup (function, call_args);
8876 if (TREE_CODE (function) == IDENTIFIER_NODE)
8878 unqualified_name_lookup_error (function);
8879 return error_mark_node;
8882 /* Remember that there was a reference to this entity. */
8883 if (DECL_P (function))
8884 mark_used (function);
8886 if (TREE_CODE (function) == OFFSET_REF)
8887 return build_offset_ref_call_from_tree (function, call_args);
8888 if (TREE_CODE (function) == COMPONENT_REF)
8890 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8891 return finish_call_expr (function, call_args,
8892 /*disallow_virtual=*/false,
8893 /*koenig_p=*/false);
8894 else
8895 return (build_new_method_call
8896 (TREE_OPERAND (function, 0),
8897 TREE_OPERAND (function, 1),
8898 call_args, NULL_TREE,
8899 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8901 return finish_call_expr (function, call_args,
8902 /*disallow_virtual=*/qualified_p,
8903 koenig_p);
8906 case COND_EXPR:
8907 return build_x_conditional_expr
8908 (RECUR (TREE_OPERAND (t, 0)),
8909 RECUR (TREE_OPERAND (t, 1)),
8910 RECUR (TREE_OPERAND (t, 2)));
8912 case PSEUDO_DTOR_EXPR:
8913 return finish_pseudo_destructor_expr
8914 (RECUR (TREE_OPERAND (t, 0)),
8915 RECUR (TREE_OPERAND (t, 1)),
8916 RECUR (TREE_OPERAND (t, 2)));
8918 case TREE_LIST:
8920 tree purpose, value, chain;
8922 if (t == void_list_node)
8923 return t;
8925 purpose = TREE_PURPOSE (t);
8926 if (purpose)
8927 purpose = RECUR (purpose);
8928 value = TREE_VALUE (t);
8929 if (value)
8930 value = RECUR (value);
8931 chain = TREE_CHAIN (t);
8932 if (chain && chain != void_type_node)
8933 chain = RECUR (chain);
8934 if (purpose == TREE_PURPOSE (t)
8935 && value == TREE_VALUE (t)
8936 && chain == TREE_CHAIN (t))
8937 return t;
8938 return tree_cons (purpose, value, chain);
8941 case COMPONENT_REF:
8943 tree object;
8944 tree object_type;
8945 tree member;
8947 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8948 args, complain, in_decl);
8949 /* Remember that there was a reference to this entity. */
8950 if (DECL_P (object))
8951 mark_used (object);
8952 object_type = TREE_TYPE (object);
8954 member = TREE_OPERAND (t, 1);
8955 if (BASELINK_P (member))
8956 member = tsubst_baselink (member,
8957 non_reference (TREE_TYPE (object)),
8958 args, complain, in_decl);
8959 else
8960 member = tsubst_copy (member, args, complain, in_decl);
8961 if (member == error_mark_node)
8962 return error_mark_node;
8964 if (object_type && !CLASS_TYPE_P (object_type))
8966 if (TREE_CODE (member) == BIT_NOT_EXPR)
8967 return finish_pseudo_destructor_expr (object,
8968 NULL_TREE,
8969 object_type);
8970 else if (TREE_CODE (member) == SCOPE_REF
8971 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8972 return finish_pseudo_destructor_expr (object,
8973 object,
8974 object_type);
8976 else if (TREE_CODE (member) == SCOPE_REF
8977 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8979 tree tmpl;
8980 tree args;
8982 /* Lookup the template functions now that we know what the
8983 scope is. */
8984 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8985 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8986 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8987 /*is_type_p=*/false,
8988 /*complain=*/false);
8989 if (BASELINK_P (member))
8991 BASELINK_FUNCTIONS (member)
8992 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8993 args);
8994 member = (adjust_result_of_qualified_name_lookup
8995 (member, BINFO_TYPE (BASELINK_BINFO (member)),
8996 object_type));
8998 else
9000 qualified_name_lookup_error (object_type, tmpl, member);
9001 return error_mark_node;
9004 else if (TREE_CODE (member) == SCOPE_REF
9005 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9006 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9008 if (complain & tf_error)
9010 if (TYPE_P (TREE_OPERAND (member, 0)))
9011 error ("%qT is not a class or namespace",
9012 TREE_OPERAND (member, 0));
9013 else
9014 error ("%qD is not a class or namespace",
9015 TREE_OPERAND (member, 0));
9017 return error_mark_node;
9019 else if (TREE_CODE (member) == FIELD_DECL)
9020 return finish_non_static_data_member (member, object, NULL_TREE);
9022 return finish_class_member_access_expr (object, member,
9023 /*template_p=*/false);
9026 case THROW_EXPR:
9027 return build_throw
9028 (RECUR (TREE_OPERAND (t, 0)));
9030 case CONSTRUCTOR:
9032 VEC(constructor_elt,gc) *n;
9033 constructor_elt *ce;
9034 unsigned HOST_WIDE_INT idx;
9035 tree r;
9036 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9037 bool process_index_p;
9039 /* digest_init will do the wrong thing if we let it. */
9040 if (type && TYPE_PTRMEMFUNC_P (type))
9041 return t;
9043 /* We do not want to process the index of aggregate
9044 initializers as they are identifier nodes which will be
9045 looked up by digest_init. */
9046 process_index_p = !(type && IS_AGGR_TYPE (type));
9048 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9049 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9051 if (ce->index && process_index_p)
9052 ce->index = RECUR (ce->index);
9053 ce->value = RECUR (ce->value);
9056 r = build_constructor (NULL_TREE, n);
9057 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
9059 if (type)
9060 return digest_init (type, r);
9061 return r;
9064 case TYPEID_EXPR:
9066 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9067 if (TYPE_P (operand_0))
9068 return get_typeid (operand_0);
9069 return build_typeid (operand_0);
9072 case VAR_DECL:
9073 if (!args)
9074 return t;
9075 /* Fall through */
9077 case PARM_DECL:
9079 tree r = tsubst_copy (t, args, complain, in_decl);
9081 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9082 /* If the original type was a reference, we'll be wrapped in
9083 the appropriate INDIRECT_REF. */
9084 r = convert_from_reference (r);
9085 return r;
9088 case VA_ARG_EXPR:
9089 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9090 tsubst_copy (TREE_TYPE (t), args, complain,
9091 in_decl));
9093 case OFFSETOF_EXPR:
9094 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
9096 case STMT_EXPR:
9098 tree old_stmt_expr = cur_stmt_expr;
9099 tree stmt_expr = begin_stmt_expr ();
9101 cur_stmt_expr = stmt_expr;
9102 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
9103 stmt_expr = finish_stmt_expr (stmt_expr, false);
9104 cur_stmt_expr = old_stmt_expr;
9106 return stmt_expr;
9109 case CONST_DECL:
9110 t = tsubst_copy (t, args, complain, in_decl);
9111 /* As in finish_id_expression, we resolve enumeration constants
9112 to their underlying values. */
9113 if (TREE_CODE (t) == CONST_DECL)
9114 return DECL_INITIAL (t);
9115 return t;
9117 default:
9118 /* Handle Objective-C++ constructs, if appropriate. */
9120 tree subst
9121 = objcp_tsubst_copy_and_build (t, args, complain,
9122 in_decl, /*function_p=*/false);
9123 if (subst)
9124 return subst;
9126 return tsubst_copy (t, args, complain, in_decl);
9129 #undef RECUR
9132 /* Verify that the instantiated ARGS are valid. For type arguments,
9133 make sure that the type's linkage is ok. For non-type arguments,
9134 make sure they are constants if they are integral or enumerations.
9135 Emit an error under control of COMPLAIN, and return TRUE on error. */
9137 static bool
9138 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9140 int ix, len = DECL_NTPARMS (tmpl);
9141 bool result = false;
9143 for (ix = 0; ix != len; ix++)
9145 tree t = TREE_VEC_ELT (args, ix);
9147 if (TYPE_P (t))
9149 /* [basic.link]: A name with no linkage (notably, the name
9150 of a class or enumeration declared in a local scope)
9151 shall not be used to declare an entity with linkage.
9152 This implies that names with no linkage cannot be used as
9153 template arguments. */
9154 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9156 if (nt)
9158 /* DR 488 makes use of a type with no linkage cause
9159 type deduction to fail. */
9160 if (complain & tf_error)
9162 if (TYPE_ANONYMOUS_P (nt))
9163 error ("%qT is/uses anonymous type", t);
9164 else
9165 error ("template argument for %qD uses local type %qT",
9166 tmpl, t);
9168 result = true;
9170 /* In order to avoid all sorts of complications, we do not
9171 allow variably-modified types as template arguments. */
9172 else if (variably_modified_type_p (t, NULL_TREE))
9174 if (complain & tf_error)
9175 error ("%qT is a variably modified type", t);
9176 result = true;
9179 /* A non-type argument of integral or enumerated type must be a
9180 constant. */
9181 else if (TREE_TYPE (t)
9182 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9183 && !TREE_CONSTANT (t))
9185 if (complain & tf_error)
9186 error ("integral expression %qE is not constant", t);
9187 result = true;
9190 if (result && (complain & tf_error))
9191 error (" trying to instantiate %qD", tmpl);
9192 return result;
9195 /* Instantiate the indicated variable or function template TMPL with
9196 the template arguments in TARG_PTR. */
9198 tree
9199 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9201 tree fndecl;
9202 tree gen_tmpl;
9203 tree spec;
9205 if (tmpl == error_mark_node)
9206 return error_mark_node;
9208 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9210 /* If this function is a clone, handle it specially. */
9211 if (DECL_CLONED_FUNCTION_P (tmpl))
9213 tree spec;
9214 tree clone;
9216 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9217 complain);
9218 if (spec == error_mark_node)
9219 return error_mark_node;
9221 /* Look for the clone. */
9222 FOR_EACH_CLONE (clone, spec)
9223 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9224 return clone;
9225 /* We should always have found the clone by now. */
9226 gcc_unreachable ();
9227 return NULL_TREE;
9230 /* Check to see if we already have this specialization. */
9231 spec = retrieve_specialization (tmpl, targ_ptr,
9232 /*class_specializations_p=*/false);
9233 if (spec != NULL_TREE)
9234 return spec;
9236 gen_tmpl = most_general_template (tmpl);
9237 if (tmpl != gen_tmpl)
9239 /* The TMPL is a partial instantiation. To get a full set of
9240 arguments we must add the arguments used to perform the
9241 partial instantiation. */
9242 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9243 targ_ptr);
9245 /* Check to see if we already have this specialization. */
9246 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9247 /*class_specializations_p=*/false);
9248 if (spec != NULL_TREE)
9249 return spec;
9252 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9253 complain))
9254 return error_mark_node;
9256 /* We are building a FUNCTION_DECL, during which the access of its
9257 parameters and return types have to be checked. However this
9258 FUNCTION_DECL which is the desired context for access checking
9259 is not built yet. We solve this chicken-and-egg problem by
9260 deferring all checks until we have the FUNCTION_DECL. */
9261 push_deferring_access_checks (dk_deferred);
9263 /* Substitute template parameters. */
9264 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9265 targ_ptr, complain, gen_tmpl);
9266 if (fndecl == error_mark_node)
9267 return error_mark_node;
9269 /* Now we know the specialization, compute access previously
9270 deferred. */
9271 push_access_scope (fndecl);
9272 perform_deferred_access_checks ();
9273 pop_access_scope (fndecl);
9274 pop_deferring_access_checks ();
9276 /* The DECL_TI_TEMPLATE should always be the immediate parent
9277 template, not the most general template. */
9278 DECL_TI_TEMPLATE (fndecl) = tmpl;
9280 /* If we've just instantiated the main entry point for a function,
9281 instantiate all the alternate entry points as well. We do this
9282 by cloning the instantiation of the main entry point, not by
9283 instantiating the template clones. */
9284 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9285 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9287 return fndecl;
9290 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9291 arguments that are being used when calling it. TARGS is a vector
9292 into which the deduced template arguments are placed.
9294 Return zero for success, 2 for an incomplete match that doesn't resolve
9295 all the types, and 1 for complete failure. An error message will be
9296 printed only for an incomplete match.
9298 If FN is a conversion operator, or we are trying to produce a specific
9299 specialization, RETURN_TYPE is the return type desired.
9301 The EXPLICIT_TARGS are explicit template arguments provided via a
9302 template-id.
9304 The parameter STRICT is one of:
9306 DEDUCE_CALL:
9307 We are deducing arguments for a function call, as in
9308 [temp.deduct.call].
9310 DEDUCE_CONV:
9311 We are deducing arguments for a conversion function, as in
9312 [temp.deduct.conv].
9314 DEDUCE_EXACT:
9315 We are deducing arguments when doing an explicit instantiation
9316 as in [temp.explicit], when determining an explicit specialization
9317 as in [temp.expl.spec], or when taking the address of a function
9318 template, as in [temp.deduct.funcaddr]. */
9321 fn_type_unification (tree fn,
9322 tree explicit_targs,
9323 tree targs,
9324 tree args,
9325 tree return_type,
9326 unification_kind_t strict,
9327 int flags)
9329 tree parms;
9330 tree fntype;
9331 int result;
9333 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9335 fntype = TREE_TYPE (fn);
9336 if (explicit_targs)
9338 /* [temp.deduct]
9340 The specified template arguments must match the template
9341 parameters in kind (i.e., type, nontype, template), and there
9342 must not be more arguments than there are parameters;
9343 otherwise type deduction fails.
9345 Nontype arguments must match the types of the corresponding
9346 nontype template parameters, or must be convertible to the
9347 types of the corresponding nontype parameters as specified in
9348 _temp.arg.nontype_, otherwise type deduction fails.
9350 All references in the function type of the function template
9351 to the corresponding template parameters are replaced by the
9352 specified template argument values. If a substitution in a
9353 template parameter or in the function type of the function
9354 template results in an invalid type, type deduction fails. */
9355 int i;
9356 tree converted_args;
9357 bool incomplete;
9359 if (explicit_targs == error_mark_node)
9360 return 1;
9362 converted_args
9363 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9364 explicit_targs, NULL_TREE, tf_none,
9365 /*require_all_args=*/false,
9366 /*use_default_args=*/false));
9367 if (converted_args == error_mark_node)
9368 return 1;
9370 /* Substitute the explicit args into the function type. This is
9371 necessary so that, for instance, explicitly declared function
9372 arguments can match null pointed constants. If we were given
9373 an incomplete set of explicit args, we must not do semantic
9374 processing during substitution as we could create partial
9375 instantiations. */
9376 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9377 processing_template_decl += incomplete;
9378 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9379 processing_template_decl -= incomplete;
9381 if (fntype == error_mark_node)
9382 return 1;
9384 /* Place the explicitly specified arguments in TARGS. */
9385 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9386 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9389 parms = TYPE_ARG_TYPES (fntype);
9390 /* Never do unification on the 'this' parameter. */
9391 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9392 parms = TREE_CHAIN (parms);
9394 if (return_type)
9396 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9397 args = tree_cons (NULL_TREE, return_type, args);
9400 /* We allow incomplete unification without an error message here
9401 because the standard doesn't seem to explicitly prohibit it. Our
9402 callers must be ready to deal with unification failures in any
9403 event. */
9404 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9405 targs, parms, args, /*subr=*/0,
9406 strict, flags);
9408 if (result == 0)
9409 /* All is well so far. Now, check:
9411 [temp.deduct]
9413 When all template arguments have been deduced, all uses of
9414 template parameters in nondeduced contexts are replaced with
9415 the corresponding deduced argument values. If the
9416 substitution results in an invalid type, as described above,
9417 type deduction fails. */
9418 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9419 == error_mark_node)
9420 return 1;
9422 return result;
9425 /* Adjust types before performing type deduction, as described in
9426 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9427 sections are symmetric. PARM is the type of a function parameter
9428 or the return type of the conversion function. ARG is the type of
9429 the argument passed to the call, or the type of the value
9430 initialized with the result of the conversion function. */
9432 static int
9433 maybe_adjust_types_for_deduction (unification_kind_t strict,
9434 tree* parm,
9435 tree* arg)
9437 int result = 0;
9439 switch (strict)
9441 case DEDUCE_CALL:
9442 break;
9444 case DEDUCE_CONV:
9446 /* Swap PARM and ARG throughout the remainder of this
9447 function; the handling is precisely symmetric since PARM
9448 will initialize ARG rather than vice versa. */
9449 tree* temp = parm;
9450 parm = arg;
9451 arg = temp;
9452 break;
9455 case DEDUCE_EXACT:
9456 /* There is nothing to do in this case. */
9457 return 0;
9459 default:
9460 gcc_unreachable ();
9463 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9465 /* [temp.deduct.call]
9467 If P is not a reference type:
9469 --If A is an array type, the pointer type produced by the
9470 array-to-pointer standard conversion (_conv.array_) is
9471 used in place of A for type deduction; otherwise,
9473 --If A is a function type, the pointer type produced by
9474 the function-to-pointer standard conversion
9475 (_conv.func_) is used in place of A for type deduction;
9476 otherwise,
9478 --If A is a cv-qualified type, the top level
9479 cv-qualifiers of A's type are ignored for type
9480 deduction. */
9481 if (TREE_CODE (*arg) == ARRAY_TYPE)
9482 *arg = build_pointer_type (TREE_TYPE (*arg));
9483 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9484 *arg = build_pointer_type (*arg);
9485 else
9486 *arg = TYPE_MAIN_VARIANT (*arg);
9489 /* [temp.deduct.call]
9491 If P is a cv-qualified type, the top level cv-qualifiers
9492 of P's type are ignored for type deduction. If P is a
9493 reference type, the type referred to by P is used for
9494 type deduction. */
9495 *parm = TYPE_MAIN_VARIANT (*parm);
9496 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9498 *parm = TREE_TYPE (*parm);
9499 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9502 /* DR 322. For conversion deduction, remove a reference type on parm
9503 too (which has been swapped into ARG). */
9504 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9505 *arg = TREE_TYPE (*arg);
9507 return result;
9510 /* Most parms like fn_type_unification.
9512 If SUBR is 1, we're being called recursively (to unify the
9513 arguments of a function or method parameter of a function
9514 template). */
9516 static int
9517 type_unification_real (tree tparms,
9518 tree targs,
9519 tree xparms,
9520 tree xargs,
9521 int subr,
9522 unification_kind_t strict,
9523 int flags)
9525 tree parm, arg;
9526 int i;
9527 int ntparms = TREE_VEC_LENGTH (tparms);
9528 int sub_strict;
9529 int saw_undeduced = 0;
9530 tree parms, args;
9532 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9533 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9534 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9535 gcc_assert (ntparms > 0);
9537 switch (strict)
9539 case DEDUCE_CALL:
9540 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9541 | UNIFY_ALLOW_DERIVED);
9542 break;
9544 case DEDUCE_CONV:
9545 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9546 break;
9548 case DEDUCE_EXACT:
9549 sub_strict = UNIFY_ALLOW_NONE;
9550 break;
9552 default:
9553 gcc_unreachable ();
9556 again:
9557 parms = xparms;
9558 args = xargs;
9560 while (parms && parms != void_list_node
9561 && args && args != void_list_node)
9563 parm = TREE_VALUE (parms);
9564 parms = TREE_CHAIN (parms);
9565 arg = TREE_VALUE (args);
9566 args = TREE_CHAIN (args);
9568 if (arg == error_mark_node)
9569 return 1;
9570 if (arg == unknown_type_node)
9571 /* We can't deduce anything from this, but we might get all the
9572 template args from other function args. */
9573 continue;
9575 /* Conversions will be performed on a function argument that
9576 corresponds with a function parameter that contains only
9577 non-deducible template parameters and explicitly specified
9578 template parameters. */
9579 if (!uses_template_parms (parm))
9581 tree type;
9583 if (!TYPE_P (arg))
9584 type = TREE_TYPE (arg);
9585 else
9586 type = arg;
9588 if (same_type_p (parm, type))
9589 continue;
9590 if (strict != DEDUCE_EXACT
9591 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9592 flags))
9593 continue;
9595 return 1;
9598 if (!TYPE_P (arg))
9600 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9601 if (type_unknown_p (arg))
9603 /* [temp.deduct.type] A template-argument can be deduced from
9604 a pointer to function or pointer to member function
9605 argument if the set of overloaded functions does not
9606 contain function templates and at most one of a set of
9607 overloaded functions provides a unique match. */
9609 if (resolve_overloaded_unification
9610 (tparms, targs, parm, arg, strict, sub_strict)
9611 != 0)
9612 return 1;
9613 continue;
9615 arg = TREE_TYPE (arg);
9616 if (arg == error_mark_node)
9617 return 1;
9621 int arg_strict = sub_strict;
9623 if (!subr)
9624 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9626 if (unify (tparms, targs, parm, arg, arg_strict))
9627 return 1;
9631 /* Fail if we've reached the end of the parm list, and more args
9632 are present, and the parm list isn't variadic. */
9633 if (args && args != void_list_node && parms == void_list_node)
9634 return 1;
9635 /* Fail if parms are left and they don't have default values. */
9636 if (parms && parms != void_list_node
9637 && TREE_PURPOSE (parms) == NULL_TREE)
9638 return 1;
9640 if (!subr)
9641 for (i = 0; i < ntparms; i++)
9642 if (!TREE_VEC_ELT (targs, i))
9644 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9646 /* If this is an undeduced nontype parameter that depends on
9647 a type parameter, try another pass; its type may have been
9648 deduced from a later argument than the one from which
9649 this parameter can be deduced. */
9650 if (TREE_CODE (tparm) == PARM_DECL
9651 && uses_template_parms (TREE_TYPE (tparm))
9652 && !saw_undeduced++)
9653 goto again;
9655 return 2;
9658 return 0;
9661 /* Subroutine of type_unification_real. Args are like the variables at the
9662 call site. ARG is an overloaded function (or template-id); we try
9663 deducing template args from each of the overloads, and if only one
9664 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9666 static int
9667 resolve_overloaded_unification (tree tparms,
9668 tree targs,
9669 tree parm,
9670 tree arg,
9671 unification_kind_t strict,
9672 int sub_strict)
9674 tree tempargs = copy_node (targs);
9675 int good = 0;
9676 bool addr_p;
9678 if (TREE_CODE (arg) == ADDR_EXPR)
9680 arg = TREE_OPERAND (arg, 0);
9681 addr_p = true;
9683 else
9684 addr_p = false;
9686 if (TREE_CODE (arg) == COMPONENT_REF)
9687 /* Handle `&x' where `x' is some static or non-static member
9688 function name. */
9689 arg = TREE_OPERAND (arg, 1);
9691 if (TREE_CODE (arg) == OFFSET_REF)
9692 arg = TREE_OPERAND (arg, 1);
9694 /* Strip baselink information. */
9695 if (BASELINK_P (arg))
9696 arg = BASELINK_FUNCTIONS (arg);
9698 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9700 /* If we got some explicit template args, we need to plug them into
9701 the affected templates before we try to unify, in case the
9702 explicit args will completely resolve the templates in question. */
9704 tree expl_subargs = TREE_OPERAND (arg, 1);
9705 arg = TREE_OPERAND (arg, 0);
9707 for (; arg; arg = OVL_NEXT (arg))
9709 tree fn = OVL_CURRENT (arg);
9710 tree subargs, elem;
9712 if (TREE_CODE (fn) != TEMPLATE_DECL)
9713 continue;
9715 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9716 expl_subargs, /*check_ret=*/false);
9717 if (subargs)
9719 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9720 good += try_one_overload (tparms, targs, tempargs, parm,
9721 elem, strict, sub_strict, addr_p);
9725 else
9727 gcc_assert (TREE_CODE (arg) == OVERLOAD
9728 || TREE_CODE (arg) == FUNCTION_DECL);
9730 for (; arg; arg = OVL_NEXT (arg))
9731 good += try_one_overload (tparms, targs, tempargs, parm,
9732 TREE_TYPE (OVL_CURRENT (arg)),
9733 strict, sub_strict, addr_p);
9736 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9737 to function or pointer to member function argument if the set of
9738 overloaded functions does not contain function templates and at most
9739 one of a set of overloaded functions provides a unique match.
9741 So if we found multiple possibilities, we return success but don't
9742 deduce anything. */
9744 if (good == 1)
9746 int i = TREE_VEC_LENGTH (targs);
9747 for (; i--; )
9748 if (TREE_VEC_ELT (tempargs, i))
9749 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9751 if (good)
9752 return 0;
9754 return 1;
9757 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9758 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9759 different overloads deduce different arguments for a given parm.
9760 ADDR_P is true if the expression for which deduction is being
9761 performed was of the form "& fn" rather than simply "fn".
9763 Returns 1 on success. */
9765 static int
9766 try_one_overload (tree tparms,
9767 tree orig_targs,
9768 tree targs,
9769 tree parm,
9770 tree arg,
9771 unification_kind_t strict,
9772 int sub_strict,
9773 bool addr_p)
9775 int nargs;
9776 tree tempargs;
9777 int i;
9779 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9780 to function or pointer to member function argument if the set of
9781 overloaded functions does not contain function templates and at most
9782 one of a set of overloaded functions provides a unique match.
9784 So if this is a template, just return success. */
9786 if (uses_template_parms (arg))
9787 return 1;
9789 if (TREE_CODE (arg) == METHOD_TYPE)
9790 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9791 else if (addr_p)
9792 arg = build_pointer_type (arg);
9794 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9796 /* We don't copy orig_targs for this because if we have already deduced
9797 some template args from previous args, unify would complain when we
9798 try to deduce a template parameter for the same argument, even though
9799 there isn't really a conflict. */
9800 nargs = TREE_VEC_LENGTH (targs);
9801 tempargs = make_tree_vec (nargs);
9803 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9804 return 0;
9806 /* First make sure we didn't deduce anything that conflicts with
9807 explicitly specified args. */
9808 for (i = nargs; i--; )
9810 tree elt = TREE_VEC_ELT (tempargs, i);
9811 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9813 if (!elt)
9814 /*NOP*/;
9815 else if (uses_template_parms (elt))
9816 /* Since we're unifying against ourselves, we will fill in
9817 template args used in the function parm list with our own
9818 template parms. Discard them. */
9819 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9820 else if (oldelt && !template_args_equal (oldelt, elt))
9821 return 0;
9824 for (i = nargs; i--; )
9826 tree elt = TREE_VEC_ELT (tempargs, i);
9828 if (elt)
9829 TREE_VEC_ELT (targs, i) = elt;
9832 return 1;
9835 /* PARM is a template class (perhaps with unbound template
9836 parameters). ARG is a fully instantiated type. If ARG can be
9837 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9838 TARGS are as for unify. */
9840 static tree
9841 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9843 tree copy_of_targs;
9845 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9846 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9847 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9848 return NULL_TREE;
9850 /* We need to make a new template argument vector for the call to
9851 unify. If we used TARGS, we'd clutter it up with the result of
9852 the attempted unification, even if this class didn't work out.
9853 We also don't want to commit ourselves to all the unifications
9854 we've already done, since unification is supposed to be done on
9855 an argument-by-argument basis. In other words, consider the
9856 following pathological case:
9858 template <int I, int J, int K>
9859 struct S {};
9861 template <int I, int J>
9862 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9864 template <int I, int J, int K>
9865 void f(S<I, J, K>, S<I, I, I>);
9867 void g() {
9868 S<0, 0, 0> s0;
9869 S<0, 1, 2> s2;
9871 f(s0, s2);
9874 Now, by the time we consider the unification involving `s2', we
9875 already know that we must have `f<0, 0, 0>'. But, even though
9876 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9877 because there are two ways to unify base classes of S<0, 1, 2>
9878 with S<I, I, I>. If we kept the already deduced knowledge, we
9879 would reject the possibility I=1. */
9880 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9882 /* If unification failed, we're done. */
9883 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9884 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9885 return NULL_TREE;
9887 return arg;
9890 /* Given a template type PARM and a class type ARG, find the unique
9891 base type in ARG that is an instance of PARM. We do not examine
9892 ARG itself; only its base-classes. If there is not exactly one
9893 appropriate base class, return NULL_TREE. PARM may be the type of
9894 a partial specialization, as well as a plain template type. Used
9895 by unify. */
9897 static tree
9898 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9900 tree rval = NULL_TREE;
9901 tree binfo;
9903 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9905 binfo = TYPE_BINFO (complete_type (arg));
9906 if (!binfo)
9907 /* The type could not be completed. */
9908 return NULL_TREE;
9910 /* Walk in inheritance graph order. The search order is not
9911 important, and this avoids multiple walks of virtual bases. */
9912 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9914 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9916 if (r)
9918 /* If there is more than one satisfactory baseclass, then:
9920 [temp.deduct.call]
9922 If they yield more than one possible deduced A, the type
9923 deduction fails.
9925 applies. */
9926 if (rval && !same_type_p (r, rval))
9927 return NULL_TREE;
9929 rval = r;
9933 return rval;
9936 /* Returns the level of DECL, which declares a template parameter. */
9938 static int
9939 template_decl_level (tree decl)
9941 switch (TREE_CODE (decl))
9943 case TYPE_DECL:
9944 case TEMPLATE_DECL:
9945 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9947 case PARM_DECL:
9948 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9950 default:
9951 gcc_unreachable ();
9953 return 0;
9956 /* Decide whether ARG can be unified with PARM, considering only the
9957 cv-qualifiers of each type, given STRICT as documented for unify.
9958 Returns nonzero iff the unification is OK on that basis. */
9960 static int
9961 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9963 int arg_quals = cp_type_quals (arg);
9964 int parm_quals = cp_type_quals (parm);
9966 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9967 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9969 /* Although a CVR qualifier is ignored when being applied to a
9970 substituted template parameter ([8.3.2]/1 for example), that
9971 does not apply during deduction [14.8.2.4]/1, (even though
9972 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9973 this). Except when we're allowing additional CV qualifiers
9974 at the outer level [14.8.2.1]/3,1st bullet. */
9975 if ((TREE_CODE (arg) == REFERENCE_TYPE
9976 || TREE_CODE (arg) == FUNCTION_TYPE
9977 || TREE_CODE (arg) == METHOD_TYPE)
9978 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9979 return 0;
9981 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9982 && (parm_quals & TYPE_QUAL_RESTRICT))
9983 return 0;
9986 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9987 && (arg_quals & parm_quals) != parm_quals)
9988 return 0;
9990 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9991 && (parm_quals & arg_quals) != arg_quals)
9992 return 0;
9994 return 1;
9997 /* Deduce the value of template parameters. TPARMS is the (innermost)
9998 set of template parameters to a template. TARGS is the bindings
9999 for those template parameters, as determined thus far; TARGS may
10000 include template arguments for outer levels of template parameters
10001 as well. PARM is a parameter to a template function, or a
10002 subcomponent of that parameter; ARG is the corresponding argument.
10003 This function attempts to match PARM with ARG in a manner
10004 consistent with the existing assignments in TARGS. If more values
10005 are deduced, then TARGS is updated.
10007 Returns 0 if the type deduction succeeds, 1 otherwise. The
10008 parameter STRICT is a bitwise or of the following flags:
10010 UNIFY_ALLOW_NONE:
10011 Require an exact match between PARM and ARG.
10012 UNIFY_ALLOW_MORE_CV_QUAL:
10013 Allow the deduced ARG to be more cv-qualified (by qualification
10014 conversion) than ARG.
10015 UNIFY_ALLOW_LESS_CV_QUAL:
10016 Allow the deduced ARG to be less cv-qualified than ARG.
10017 UNIFY_ALLOW_DERIVED:
10018 Allow the deduced ARG to be a template base class of ARG,
10019 or a pointer to a template base class of the type pointed to by
10020 ARG.
10021 UNIFY_ALLOW_INTEGER:
10022 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10023 case for more information.
10024 UNIFY_ALLOW_OUTER_LEVEL:
10025 This is the outermost level of a deduction. Used to determine validity
10026 of qualification conversions. A valid qualification conversion must
10027 have const qualified pointers leading up to the inner type which
10028 requires additional CV quals, except at the outer level, where const
10029 is not required [conv.qual]. It would be normal to set this flag in
10030 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10031 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10032 This is the outermost level of a deduction, and PARM can be more CV
10033 qualified at this point.
10034 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10035 This is the outermost level of a deduction, and PARM can be less CV
10036 qualified at this point. */
10038 static int
10039 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10041 int idx;
10042 tree targ;
10043 tree tparm;
10044 int strict_in = strict;
10046 /* I don't think this will do the right thing with respect to types.
10047 But the only case I've seen it in so far has been array bounds, where
10048 signedness is the only information lost, and I think that will be
10049 okay. */
10050 while (TREE_CODE (parm) == NOP_EXPR)
10051 parm = TREE_OPERAND (parm, 0);
10053 if (arg == error_mark_node)
10054 return 1;
10055 if (arg == unknown_type_node)
10056 /* We can't deduce anything from this, but we might get all the
10057 template args from other function args. */
10058 return 0;
10060 /* If PARM uses template parameters, then we can't bail out here,
10061 even if ARG == PARM, since we won't record unifications for the
10062 template parameters. We might need them if we're trying to
10063 figure out which of two things is more specialized. */
10064 if (arg == parm && !uses_template_parms (parm))
10065 return 0;
10067 /* Immediately reject some pairs that won't unify because of
10068 cv-qualification mismatches. */
10069 if (TREE_CODE (arg) == TREE_CODE (parm)
10070 && TYPE_P (arg)
10071 /* It is the elements of the array which hold the cv quals of an array
10072 type, and the elements might be template type parms. We'll check
10073 when we recurse. */
10074 && TREE_CODE (arg) != ARRAY_TYPE
10075 /* We check the cv-qualifiers when unifying with template type
10076 parameters below. We want to allow ARG `const T' to unify with
10077 PARM `T' for example, when computing which of two templates
10078 is more specialized, for example. */
10079 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10080 && !check_cv_quals_for_unify (strict_in, arg, parm))
10081 return 1;
10083 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10084 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10085 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10086 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10087 strict &= ~UNIFY_ALLOW_DERIVED;
10088 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10089 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10091 switch (TREE_CODE (parm))
10093 case TYPENAME_TYPE:
10094 case SCOPE_REF:
10095 case UNBOUND_CLASS_TEMPLATE:
10096 /* In a type which contains a nested-name-specifier, template
10097 argument values cannot be deduced for template parameters used
10098 within the nested-name-specifier. */
10099 return 0;
10101 case TEMPLATE_TYPE_PARM:
10102 case TEMPLATE_TEMPLATE_PARM:
10103 case BOUND_TEMPLATE_TEMPLATE_PARM:
10104 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10106 if (TEMPLATE_TYPE_LEVEL (parm)
10107 != template_decl_level (tparm))
10108 /* The PARM is not one we're trying to unify. Just check
10109 to see if it matches ARG. */
10110 return (TREE_CODE (arg) == TREE_CODE (parm)
10111 && same_type_p (parm, arg)) ? 0 : 1;
10112 idx = TEMPLATE_TYPE_IDX (parm);
10113 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10114 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10116 /* Check for mixed types and values. */
10117 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10118 && TREE_CODE (tparm) != TYPE_DECL)
10119 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10120 && TREE_CODE (tparm) != TEMPLATE_DECL))
10121 return 1;
10123 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10125 /* ARG must be constructed from a template class or a template
10126 template parameter. */
10127 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10128 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
10129 return 1;
10132 tree parmvec = TYPE_TI_ARGS (parm);
10133 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10134 tree argtmplvec
10135 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10136 int i;
10138 /* The resolution to DR150 makes clear that default
10139 arguments for an N-argument may not be used to bind T
10140 to a template template parameter with fewer than N
10141 parameters. It is not safe to permit the binding of
10142 default arguments as an extension, as that may change
10143 the meaning of a conforming program. Consider:
10145 struct Dense { static const unsigned int dim = 1; };
10147 template <template <typename> class View,
10148 typename Block>
10149 void operator+(float, View<Block> const&);
10151 template <typename Block,
10152 unsigned int Dim = Block::dim>
10153 struct Lvalue_proxy { operator float() const; };
10155 void
10156 test_1d (void) {
10157 Lvalue_proxy<Dense> p;
10158 float b;
10159 b + p;
10162 Here, if Lvalue_proxy is permitted to bind to View, then
10163 the global operator+ will be used; if they are not, the
10164 Lvalue_proxy will be converted to float. */
10165 if (coerce_template_parms (argtmplvec, parmvec,
10166 TYPE_TI_TEMPLATE (parm),
10167 tf_none,
10168 /*require_all_args=*/true,
10169 /*use_default_args=*/false)
10170 == error_mark_node)
10171 return 1;
10173 /* Deduce arguments T, i from TT<T> or TT<i>.
10174 We check each element of PARMVEC and ARGVEC individually
10175 rather than the whole TREE_VEC since they can have
10176 different number of elements. */
10178 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10180 if (unify (tparms, targs,
10181 TREE_VEC_ELT (parmvec, i),
10182 TREE_VEC_ELT (argvec, i),
10183 UNIFY_ALLOW_NONE))
10184 return 1;
10187 arg = TYPE_TI_TEMPLATE (arg);
10189 /* Fall through to deduce template name. */
10192 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10193 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10195 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10197 /* Simple cases: Value already set, does match or doesn't. */
10198 if (targ != NULL_TREE && template_args_equal (targ, arg))
10199 return 0;
10200 else if (targ)
10201 return 1;
10203 else
10205 /* If PARM is `const T' and ARG is only `int', we don't have
10206 a match unless we are allowing additional qualification.
10207 If ARG is `const int' and PARM is just `T' that's OK;
10208 that binds `const int' to `T'. */
10209 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10210 arg, parm))
10211 return 1;
10213 /* Consider the case where ARG is `const volatile int' and
10214 PARM is `const T'. Then, T should be `volatile int'. */
10215 arg = cp_build_qualified_type_real
10216 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10217 if (arg == error_mark_node)
10218 return 1;
10220 /* Simple cases: Value already set, does match or doesn't. */
10221 if (targ != NULL_TREE && same_type_p (targ, arg))
10222 return 0;
10223 else if (targ)
10224 return 1;
10226 /* Make sure that ARG is not a variable-sized array. (Note
10227 that were talking about variable-sized arrays (like
10228 `int[n]'), rather than arrays of unknown size (like
10229 `int[]').) We'll get very confused by such a type since
10230 the bound of the array will not be computable in an
10231 instantiation. Besides, such types are not allowed in
10232 ISO C++, so we can do as we please here. */
10233 if (variably_modified_type_p (arg, NULL_TREE))
10234 return 1;
10237 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10238 return 0;
10240 case TEMPLATE_PARM_INDEX:
10241 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10243 if (TEMPLATE_PARM_LEVEL (parm)
10244 != template_decl_level (tparm))
10245 /* The PARM is not one we're trying to unify. Just check
10246 to see if it matches ARG. */
10247 return !(TREE_CODE (arg) == TREE_CODE (parm)
10248 && cp_tree_equal (parm, arg));
10250 idx = TEMPLATE_PARM_IDX (parm);
10251 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10253 if (targ)
10254 return !cp_tree_equal (targ, arg);
10256 /* [temp.deduct.type] If, in the declaration of a function template
10257 with a non-type template-parameter, the non-type
10258 template-parameter is used in an expression in the function
10259 parameter-list and, if the corresponding template-argument is
10260 deduced, the template-argument type shall match the type of the
10261 template-parameter exactly, except that a template-argument
10262 deduced from an array bound may be of any integral type.
10263 The non-type parameter might use already deduced type parameters. */
10264 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10265 if (!TREE_TYPE (arg))
10266 /* Template-parameter dependent expression. Just accept it for now.
10267 It will later be processed in convert_template_argument. */
10269 else if (same_type_p (TREE_TYPE (arg), tparm))
10270 /* OK */;
10271 else if ((strict & UNIFY_ALLOW_INTEGER)
10272 && (TREE_CODE (tparm) == INTEGER_TYPE
10273 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10274 /* Convert the ARG to the type of PARM; the deduced non-type
10275 template argument must exactly match the types of the
10276 corresponding parameter. */
10277 arg = fold (build_nop (TREE_TYPE (parm), arg));
10278 else if (uses_template_parms (tparm))
10279 /* We haven't deduced the type of this parameter yet. Try again
10280 later. */
10281 return 0;
10282 else
10283 return 1;
10285 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10286 return 0;
10288 case PTRMEM_CST:
10290 /* A pointer-to-member constant can be unified only with
10291 another constant. */
10292 if (TREE_CODE (arg) != PTRMEM_CST)
10293 return 1;
10295 /* Just unify the class member. It would be useless (and possibly
10296 wrong, depending on the strict flags) to unify also
10297 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10298 arg refer to the same variable, even if through different
10299 classes. For instance:
10301 struct A { int x; };
10302 struct B : A { };
10304 Unification of &A::x and &B::x must succeed. */
10305 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10306 PTRMEM_CST_MEMBER (arg), strict);
10309 case POINTER_TYPE:
10311 if (TREE_CODE (arg) != POINTER_TYPE)
10312 return 1;
10314 /* [temp.deduct.call]
10316 A can be another pointer or pointer to member type that can
10317 be converted to the deduced A via a qualification
10318 conversion (_conv.qual_).
10320 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10321 This will allow for additional cv-qualification of the
10322 pointed-to types if appropriate. */
10324 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10325 /* The derived-to-base conversion only persists through one
10326 level of pointers. */
10327 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10329 return unify (tparms, targs, TREE_TYPE (parm),
10330 TREE_TYPE (arg), strict);
10333 case REFERENCE_TYPE:
10334 if (TREE_CODE (arg) != REFERENCE_TYPE)
10335 return 1;
10336 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10337 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10339 case ARRAY_TYPE:
10340 if (TREE_CODE (arg) != ARRAY_TYPE)
10341 return 1;
10342 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10343 != (TYPE_DOMAIN (arg) == NULL_TREE))
10344 return 1;
10345 if (TYPE_DOMAIN (parm) != NULL_TREE)
10347 tree parm_max;
10348 tree arg_max;
10350 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10351 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10353 /* Our representation of array types uses "N - 1" as the
10354 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10355 not an integer constant. */
10356 if (TREE_CODE (parm_max) == MINUS_EXPR)
10358 arg_max = fold_build2 (PLUS_EXPR,
10359 integer_type_node,
10360 arg_max,
10361 TREE_OPERAND (parm_max, 1));
10362 parm_max = TREE_OPERAND (parm_max, 0);
10365 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10366 return 1;
10368 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10369 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10371 case REAL_TYPE:
10372 case COMPLEX_TYPE:
10373 case VECTOR_TYPE:
10374 case INTEGER_TYPE:
10375 case BOOLEAN_TYPE:
10376 case ENUMERAL_TYPE:
10377 case VOID_TYPE:
10378 if (TREE_CODE (arg) != TREE_CODE (parm))
10379 return 1;
10381 /* We have already checked cv-qualification at the top of the
10382 function. */
10383 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10384 return 1;
10386 /* As far as unification is concerned, this wins. Later checks
10387 will invalidate it if necessary. */
10388 return 0;
10390 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10391 /* Type INTEGER_CST can come from ordinary constant template args. */
10392 case INTEGER_CST:
10393 while (TREE_CODE (arg) == NOP_EXPR)
10394 arg = TREE_OPERAND (arg, 0);
10396 if (TREE_CODE (arg) != INTEGER_CST)
10397 return 1;
10398 return !tree_int_cst_equal (parm, arg);
10400 case TREE_VEC:
10402 int i;
10403 if (TREE_CODE (arg) != TREE_VEC)
10404 return 1;
10405 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10406 return 1;
10407 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10408 if (unify (tparms, targs,
10409 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10410 UNIFY_ALLOW_NONE))
10411 return 1;
10412 return 0;
10415 case RECORD_TYPE:
10416 case UNION_TYPE:
10417 if (TREE_CODE (arg) != TREE_CODE (parm))
10418 return 1;
10420 if (TYPE_PTRMEMFUNC_P (parm))
10422 if (!TYPE_PTRMEMFUNC_P (arg))
10423 return 1;
10425 return unify (tparms, targs,
10426 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10427 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10428 strict);
10431 if (CLASSTYPE_TEMPLATE_INFO (parm))
10433 tree t = NULL_TREE;
10435 if (strict_in & UNIFY_ALLOW_DERIVED)
10437 /* First, we try to unify the PARM and ARG directly. */
10438 t = try_class_unification (tparms, targs,
10439 parm, arg);
10441 if (!t)
10443 /* Fallback to the special case allowed in
10444 [temp.deduct.call]:
10446 If P is a class, and P has the form
10447 template-id, then A can be a derived class of
10448 the deduced A. Likewise, if P is a pointer to
10449 a class of the form template-id, A can be a
10450 pointer to a derived class pointed to by the
10451 deduced A. */
10452 t = get_template_base (tparms, targs, parm, arg);
10454 if (!t)
10455 return 1;
10458 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10459 && (CLASSTYPE_TI_TEMPLATE (parm)
10460 == CLASSTYPE_TI_TEMPLATE (arg)))
10461 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10462 Then, we should unify `int' and `U'. */
10463 t = arg;
10464 else
10465 /* There's no chance of unification succeeding. */
10466 return 1;
10468 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10469 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10471 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10472 return 1;
10473 return 0;
10475 case METHOD_TYPE:
10476 case FUNCTION_TYPE:
10477 if (TREE_CODE (arg) != TREE_CODE (parm))
10478 return 1;
10480 /* CV qualifications for methods can never be deduced, they must
10481 match exactly. We need to check them explicitly here,
10482 because type_unification_real treats them as any other
10483 cvqualified parameter. */
10484 if (TREE_CODE (parm) == METHOD_TYPE
10485 && (!check_cv_quals_for_unify
10486 (UNIFY_ALLOW_NONE,
10487 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10488 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10489 return 1;
10491 if (unify (tparms, targs, TREE_TYPE (parm),
10492 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10493 return 1;
10494 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10495 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10496 LOOKUP_NORMAL);
10498 case OFFSET_TYPE:
10499 /* Unify a pointer to member with a pointer to member function, which
10500 deduces the type of the member as a function type. */
10501 if (TYPE_PTRMEMFUNC_P (arg))
10503 tree method_type;
10504 tree fntype;
10505 cp_cv_quals cv_quals;
10507 /* Check top-level cv qualifiers */
10508 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10509 return 1;
10511 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10512 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10513 return 1;
10515 /* Determine the type of the function we are unifying against. */
10516 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10517 fntype =
10518 build_function_type (TREE_TYPE (method_type),
10519 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10521 /* Extract the cv-qualifiers of the member function from the
10522 implicit object parameter and place them on the function
10523 type to be restored later. */
10524 cv_quals =
10525 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10526 fntype = build_qualified_type (fntype, cv_quals);
10527 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10530 if (TREE_CODE (arg) != OFFSET_TYPE)
10531 return 1;
10532 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10533 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10534 return 1;
10535 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10536 strict);
10538 case CONST_DECL:
10539 if (DECL_TEMPLATE_PARM_P (parm))
10540 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10541 if (arg != integral_constant_value (parm))
10542 return 1;
10543 return 0;
10545 case FIELD_DECL:
10546 case TEMPLATE_DECL:
10547 /* Matched cases are handled by the ARG == PARM test above. */
10548 return 1;
10550 default:
10551 gcc_assert (EXPR_P (parm));
10553 /* We must be looking at an expression. This can happen with
10554 something like:
10556 template <int I>
10557 void foo(S<I>, S<I + 2>);
10559 This is a "nondeduced context":
10561 [deduct.type]
10563 The nondeduced contexts are:
10565 --A type that is a template-id in which one or more of
10566 the template-arguments is an expression that references
10567 a template-parameter.
10569 In these cases, we assume deduction succeeded, but don't
10570 actually infer any unifications. */
10572 if (!uses_template_parms (parm)
10573 && !template_args_equal (parm, arg))
10574 return 1;
10575 else
10576 return 0;
10580 /* Note that DECL can be defined in this translation unit, if
10581 required. */
10583 static void
10584 mark_definable (tree decl)
10586 tree clone;
10587 DECL_NOT_REALLY_EXTERN (decl) = 1;
10588 FOR_EACH_CLONE (clone, decl)
10589 DECL_NOT_REALLY_EXTERN (clone) = 1;
10592 /* Called if RESULT is explicitly instantiated, or is a member of an
10593 explicitly instantiated class. */
10595 void
10596 mark_decl_instantiated (tree result, int extern_p)
10598 SET_DECL_EXPLICIT_INSTANTIATION (result);
10600 /* If this entity has already been written out, it's too late to
10601 make any modifications. */
10602 if (TREE_ASM_WRITTEN (result))
10603 return;
10605 if (TREE_CODE (result) != FUNCTION_DECL)
10606 /* The TREE_PUBLIC flag for function declarations will have been
10607 set correctly by tsubst. */
10608 TREE_PUBLIC (result) = 1;
10610 /* This might have been set by an earlier implicit instantiation. */
10611 DECL_COMDAT (result) = 0;
10613 if (extern_p)
10614 DECL_NOT_REALLY_EXTERN (result) = 0;
10615 else
10617 mark_definable (result);
10618 /* Always make artificials weak. */
10619 if (DECL_ARTIFICIAL (result) && flag_weak)
10620 comdat_linkage (result);
10621 /* For WIN32 we also want to put explicit instantiations in
10622 linkonce sections. */
10623 else if (TREE_PUBLIC (result))
10624 maybe_make_one_only (result);
10627 /* If EXTERN_P, then this function will not be emitted -- unless
10628 followed by an explicit instantiation, at which point its linkage
10629 will be adjusted. If !EXTERN_P, then this function will be
10630 emitted here. In neither circumstance do we want
10631 import_export_decl to adjust the linkage. */
10632 DECL_INTERFACE_KNOWN (result) = 1;
10635 /* Given two function templates PAT1 and PAT2, return:
10637 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10638 -1 if PAT2 is more specialized than PAT1.
10639 0 if neither is more specialized.
10641 LEN indicates the number of parameters we should consider
10642 (defaulted parameters should not be considered).
10644 The 1998 std underspecified function template partial ordering, and
10645 DR214 addresses the issue. We take pairs of arguments, one from
10646 each of the templates, and deduce them against each other. One of
10647 the templates will be more specialized if all the *other*
10648 template's arguments deduce against its arguments and at least one
10649 of its arguments *does* *not* deduce against the other template's
10650 corresponding argument. Deduction is done as for class templates.
10651 The arguments used in deduction have reference and top level cv
10652 qualifiers removed. Iff both arguments were originally reference
10653 types *and* deduction succeeds in both directions, the template
10654 with the more cv-qualified argument wins for that pairing (if
10655 neither is more cv-qualified, they both are equal). Unlike regular
10656 deduction, after all the arguments have been deduced in this way,
10657 we do *not* verify the deduced template argument values can be
10658 substituted into non-deduced contexts, nor do we have to verify
10659 that all template arguments have been deduced. */
10662 more_specialized_fn (tree pat1, tree pat2, int len)
10664 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10665 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10666 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10667 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10668 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10669 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10670 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10671 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10672 int better1 = 0;
10673 int better2 = 0;
10675 /* Remove the this parameter from non-static member functions. If
10676 one is a non-static member function and the other is not a static
10677 member function, remove the first parameter from that function
10678 also. This situation occurs for operator functions where we
10679 locate both a member function (with this pointer) and non-member
10680 operator (with explicit first operand). */
10681 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10683 len--; /* LEN is the number of significant arguments for DECL1 */
10684 args1 = TREE_CHAIN (args1);
10685 if (!DECL_STATIC_FUNCTION_P (decl2))
10686 args2 = TREE_CHAIN (args2);
10688 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10690 args2 = TREE_CHAIN (args2);
10691 if (!DECL_STATIC_FUNCTION_P (decl1))
10693 len--;
10694 args1 = TREE_CHAIN (args1);
10698 /* If only one is a conversion operator, they are unordered. */
10699 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10700 return 0;
10702 /* Consider the return type for a conversion function */
10703 if (DECL_CONV_FN_P (decl1))
10705 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10706 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10707 len++;
10710 processing_template_decl++;
10712 while (len--)
10714 tree arg1 = TREE_VALUE (args1);
10715 tree arg2 = TREE_VALUE (args2);
10716 int deduce1, deduce2;
10717 int quals1 = -1;
10718 int quals2 = -1;
10720 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10722 arg1 = TREE_TYPE (arg1);
10723 quals1 = cp_type_quals (arg1);
10726 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10728 arg2 = TREE_TYPE (arg2);
10729 quals2 = cp_type_quals (arg2);
10732 if ((quals1 < 0) != (quals2 < 0))
10734 /* Only of the args is a reference, see if we should apply
10735 array/function pointer decay to it. This is not part of
10736 DR214, but is, IMHO, consistent with the deduction rules
10737 for the function call itself, and with our earlier
10738 implementation of the underspecified partial ordering
10739 rules. (nathan). */
10740 if (quals1 >= 0)
10742 switch (TREE_CODE (arg1))
10744 case ARRAY_TYPE:
10745 arg1 = TREE_TYPE (arg1);
10746 /* FALLTHROUGH. */
10747 case FUNCTION_TYPE:
10748 arg1 = build_pointer_type (arg1);
10749 break;
10751 default:
10752 break;
10755 else
10757 switch (TREE_CODE (arg2))
10759 case ARRAY_TYPE:
10760 arg2 = TREE_TYPE (arg2);
10761 /* FALLTHROUGH. */
10762 case FUNCTION_TYPE:
10763 arg2 = build_pointer_type (arg2);
10764 break;
10766 default:
10767 break;
10772 arg1 = TYPE_MAIN_VARIANT (arg1);
10773 arg2 = TYPE_MAIN_VARIANT (arg2);
10775 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10776 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10778 if (!deduce1)
10779 better2 = -1;
10780 if (!deduce2)
10781 better1 = -1;
10782 if (better1 < 0 && better2 < 0)
10783 /* We've failed to deduce something in either direction.
10784 These must be unordered. */
10785 break;
10787 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10789 /* Deduces in both directions, see if quals can
10790 disambiguate. Pretend the worse one failed to deduce. */
10791 if ((quals1 & quals2) == quals2)
10792 deduce1 = 0;
10793 if ((quals1 & quals2) == quals1)
10794 deduce2 = 0;
10796 if (deduce1 && !deduce2 && !better2)
10797 better2 = 1;
10798 if (deduce2 && !deduce1 && !better1)
10799 better1 = 1;
10801 args1 = TREE_CHAIN (args1);
10802 args2 = TREE_CHAIN (args2);
10805 processing_template_decl--;
10807 return (better1 > 0) - (better2 > 0);
10810 /* Determine which of two partial specializations is more specialized.
10812 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
10813 to the first partial specialization. The TREE_VALUE is the
10814 innermost set of template parameters for the partial
10815 specialization. PAT2 is similar, but for the second template.
10817 Return 1 if the first partial specialization is more specialized;
10818 -1 if the second is more specialized; 0 if neither is more
10819 specialized.
10821 See [temp.class.order] for information about determining which of
10822 two templates is more specialized. */
10824 static int
10825 more_specialized_class (tree pat1, tree pat2)
10827 tree targs;
10828 tree tmpl1, tmpl2;
10829 int winner = 0;
10831 tmpl1 = TREE_TYPE (pat1);
10832 tmpl2 = TREE_TYPE (pat2);
10834 /* Just like what happens for functions, if we are ordering between
10835 different class template specializations, we may encounter dependent
10836 types in the arguments, and we need our dependency check functions
10837 to behave correctly. */
10838 ++processing_template_decl;
10839 targs = get_class_bindings (TREE_VALUE (pat1),
10840 CLASSTYPE_TI_ARGS (tmpl1),
10841 CLASSTYPE_TI_ARGS (tmpl2));
10842 if (targs)
10843 --winner;
10845 targs = get_class_bindings (TREE_VALUE (pat2),
10846 CLASSTYPE_TI_ARGS (tmpl2),
10847 CLASSTYPE_TI_ARGS (tmpl1));
10848 if (targs)
10849 ++winner;
10850 --processing_template_decl;
10852 return winner;
10855 /* Return the template arguments that will produce the function signature
10856 DECL from the function template FN, with the explicit template
10857 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
10858 also match. Return NULL_TREE if no satisfactory arguments could be
10859 found. */
10861 static tree
10862 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
10864 int ntparms = DECL_NTPARMS (fn);
10865 tree targs = make_tree_vec (ntparms);
10866 tree decl_type;
10867 tree decl_arg_types;
10869 /* Substitute the explicit template arguments into the type of DECL.
10870 The call to fn_type_unification will handle substitution into the
10871 FN. */
10872 decl_type = TREE_TYPE (decl);
10873 if (explicit_args && uses_template_parms (decl_type))
10875 tree tmpl;
10876 tree converted_args;
10878 if (DECL_TEMPLATE_INFO (decl))
10879 tmpl = DECL_TI_TEMPLATE (decl);
10880 else
10881 /* We can get here for some invalid specializations. */
10882 return NULL_TREE;
10884 converted_args
10885 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10886 explicit_args, NULL_TREE,
10887 tf_none,
10888 /*require_all_args=*/false,
10889 /*use_default_args=*/false);
10890 if (converted_args == error_mark_node)
10891 return NULL_TREE;
10893 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10894 if (decl_type == error_mark_node)
10895 return NULL_TREE;
10898 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10899 /* Never do unification on the 'this' parameter. */
10900 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10901 decl_arg_types = TREE_CHAIN (decl_arg_types);
10903 if (fn_type_unification (fn, explicit_args, targs,
10904 decl_arg_types,
10905 (check_rettype || DECL_CONV_FN_P (fn)
10906 ? TREE_TYPE (decl_type) : NULL_TREE),
10907 DEDUCE_EXACT, LOOKUP_NORMAL))
10908 return NULL_TREE;
10910 return targs;
10913 /* Return the innermost template arguments that, when applied to a
10914 template specialization whose innermost template parameters are
10915 TPARMS, and whose specialization arguments are PARMS, yield the
10916 ARGS.
10918 For example, suppose we have:
10920 template <class T, class U> struct S {};
10921 template <class T> struct S<T*, int> {};
10923 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10924 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
10925 int}. The resulting vector will be {double}, indicating that `T'
10926 is bound to `double'. */
10928 static tree
10929 get_class_bindings (tree tparms, tree spec_args, tree args)
10931 int i, ntparms = TREE_VEC_LENGTH (tparms);
10932 tree deduced_args;
10933 tree innermost_deduced_args;
10935 innermost_deduced_args = make_tree_vec (ntparms);
10936 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10938 deduced_args = copy_node (args);
10939 SET_TMPL_ARGS_LEVEL (deduced_args,
10940 TMPL_ARGS_DEPTH (deduced_args),
10941 innermost_deduced_args);
10943 else
10944 deduced_args = innermost_deduced_args;
10946 if (unify (tparms, deduced_args,
10947 INNERMOST_TEMPLATE_ARGS (spec_args),
10948 INNERMOST_TEMPLATE_ARGS (args),
10949 UNIFY_ALLOW_NONE))
10950 return NULL_TREE;
10952 for (i = 0; i < ntparms; ++i)
10953 if (! TREE_VEC_ELT (innermost_deduced_args, i))
10954 return NULL_TREE;
10956 /* Verify that nondeduced template arguments agree with the type
10957 obtained from argument deduction.
10959 For example:
10961 struct A { typedef int X; };
10962 template <class T, class U> struct C {};
10963 template <class T> struct C<T, typename T::X> {};
10965 Then with the instantiation `C<A, int>', we can deduce that
10966 `T' is `A' but unify () does not check whether `typename T::X'
10967 is `int'. */
10968 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
10969 if (spec_args == error_mark_node
10970 /* We only need to check the innermost arguments; the other
10971 arguments will always agree. */
10972 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
10973 INNERMOST_TEMPLATE_ARGS (args)))
10974 return NULL_TREE;
10976 return deduced_args;
10979 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
10980 Return the TREE_LIST node with the most specialized template, if
10981 any. If there is no most specialized template, the error_mark_node
10982 is returned.
10984 Note that this function does not look at, or modify, the
10985 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
10986 returned is one of the elements of INSTANTIATIONS, callers may
10987 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
10988 and retrieve it from the value returned. */
10990 tree
10991 most_specialized_instantiation (tree templates)
10993 tree fn, champ;
10995 ++processing_template_decl;
10997 champ = templates;
10998 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
11000 int fate = 0;
11002 if (get_bindings (TREE_VALUE (champ),
11003 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11004 NULL_TREE, /*check_ret=*/false))
11005 fate--;
11007 if (get_bindings (TREE_VALUE (fn),
11008 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11009 NULL_TREE, /*check_ret=*/false))
11010 fate++;
11012 if (fate == -1)
11013 champ = fn;
11014 else if (!fate)
11016 /* Equally specialized, move to next function. If there
11017 is no next function, nothing's most specialized. */
11018 fn = TREE_CHAIN (fn);
11019 champ = fn;
11020 if (!fn)
11021 break;
11025 if (champ)
11026 /* Now verify that champ is better than everything earlier in the
11027 instantiation list. */
11028 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11029 if (get_bindings (TREE_VALUE (champ),
11030 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11031 NULL_TREE, /*check_ret=*/false)
11032 || !get_bindings (TREE_VALUE (fn),
11033 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11034 NULL_TREE, /*check_ret=*/false))
11036 champ = NULL_TREE;
11037 break;
11040 processing_template_decl--;
11042 if (!champ)
11043 return error_mark_node;
11045 return champ;
11048 /* If DECL is a specialization of some template, return the most
11049 general such template. Otherwise, returns NULL_TREE.
11051 For example, given:
11053 template <class T> struct S { template <class U> void f(U); };
11055 if TMPL is `template <class U> void S<int>::f(U)' this will return
11056 the full template. This function will not trace past partial
11057 specializations, however. For example, given in addition:
11059 template <class T> struct S<T*> { template <class U> void f(U); };
11061 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11062 `template <class T> template <class U> S<T*>::f(U)'. */
11064 tree
11065 most_general_template (tree decl)
11067 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11068 an immediate specialization. */
11069 if (TREE_CODE (decl) == FUNCTION_DECL)
11071 if (DECL_TEMPLATE_INFO (decl)) {
11072 decl = DECL_TI_TEMPLATE (decl);
11074 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11075 template friend. */
11076 if (TREE_CODE (decl) != TEMPLATE_DECL)
11077 return NULL_TREE;
11078 } else
11079 return NULL_TREE;
11082 /* Look for more and more general templates. */
11083 while (DECL_TEMPLATE_INFO (decl))
11085 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11086 (See cp-tree.h for details.) */
11087 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11088 break;
11090 if (CLASS_TYPE_P (TREE_TYPE (decl))
11091 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11092 break;
11094 /* Stop if we run into an explicitly specialized class template. */
11095 if (!DECL_NAMESPACE_SCOPE_P (decl)
11096 && DECL_CONTEXT (decl)
11097 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11098 break;
11100 decl = DECL_TI_TEMPLATE (decl);
11103 return decl;
11106 /* Return the most specialized of the class template partial
11107 specializations of TMPL which can produce TYPE, a specialization of
11108 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11109 a _TYPE node corresponding to the partial specialization, while the
11110 TREE_PURPOSE is the set of template arguments that must be
11111 substituted into the TREE_TYPE in order to generate TYPE.
11113 If the choice of partial specialization is ambiguous, a diagnostic
11114 is issued, and the error_mark_node is returned. If there are no
11115 partial specializations of TMPL matching TYPE, then NULL_TREE is
11116 returned. */
11118 static tree
11119 most_specialized_class (tree type, tree tmpl)
11121 tree list = NULL_TREE;
11122 tree t;
11123 tree champ;
11124 int fate;
11125 bool ambiguous_p;
11126 tree args;
11128 tmpl = most_general_template (tmpl);
11129 args = CLASSTYPE_TI_ARGS (type);
11130 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11132 tree partial_spec_args;
11133 tree spec_args;
11135 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11136 spec_args = get_class_bindings (TREE_VALUE (t),
11137 partial_spec_args,
11138 args);
11139 if (spec_args)
11141 list = tree_cons (spec_args, TREE_VALUE (t), list);
11142 TREE_TYPE (list) = TREE_TYPE (t);
11146 if (! list)
11147 return NULL_TREE;
11149 ambiguous_p = false;
11150 t = list;
11151 champ = t;
11152 t = TREE_CHAIN (t);
11153 for (; t; t = TREE_CHAIN (t))
11155 fate = more_specialized_class (champ, t);
11156 if (fate == 1)
11158 else
11160 if (fate == 0)
11162 t = TREE_CHAIN (t);
11163 if (! t)
11165 ambiguous_p = true;
11166 break;
11169 champ = t;
11173 if (!ambiguous_p)
11174 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11176 fate = more_specialized_class (champ, t);
11177 if (fate != 1)
11179 ambiguous_p = true;
11180 break;
11184 if (ambiguous_p)
11186 const char *str = "candidates are:";
11187 error ("ambiguous class template instantiation for %q#T", type);
11188 for (t = list; t; t = TREE_CHAIN (t))
11190 error ("%s %+#T", str, TREE_TYPE (t));
11191 str = " ";
11193 return error_mark_node;
11196 return champ;
11199 /* Explicitly instantiate DECL. */
11201 void
11202 do_decl_instantiation (tree decl, tree storage)
11204 tree result = NULL_TREE;
11205 int extern_p = 0;
11207 if (!decl)
11208 /* An error occurred, for which grokdeclarator has already issued
11209 an appropriate message. */
11210 return;
11211 else if (! DECL_LANG_SPECIFIC (decl))
11213 error ("explicit instantiation of non-template %q#D", decl);
11214 return;
11216 else if (TREE_CODE (decl) == VAR_DECL)
11218 /* There is an asymmetry here in the way VAR_DECLs and
11219 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11220 the latter, the DECL we get back will be marked as a
11221 template instantiation, and the appropriate
11222 DECL_TEMPLATE_INFO will be set up. This does not happen for
11223 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11224 should handle VAR_DECLs as it currently handles
11225 FUNCTION_DECLs. */
11226 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11227 if (!result || TREE_CODE (result) != VAR_DECL)
11229 error ("no matching template for %qD found", decl);
11230 return;
11233 else if (TREE_CODE (decl) != FUNCTION_DECL)
11235 error ("explicit instantiation of %q#D", decl);
11236 return;
11238 else
11239 result = decl;
11241 /* Check for various error cases. Note that if the explicit
11242 instantiation is valid the RESULT will currently be marked as an
11243 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11244 until we get here. */
11246 if (DECL_TEMPLATE_SPECIALIZATION (result))
11248 /* DR 259 [temp.spec].
11250 Both an explicit instantiation and a declaration of an explicit
11251 specialization shall not appear in a program unless the explicit
11252 instantiation follows a declaration of the explicit specialization.
11254 For a given set of template parameters, if an explicit
11255 instantiation of a template appears after a declaration of an
11256 explicit specialization for that template, the explicit
11257 instantiation has no effect. */
11258 return;
11260 else if (DECL_EXPLICIT_INSTANTIATION (result))
11262 /* [temp.spec]
11264 No program shall explicitly instantiate any template more
11265 than once.
11267 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11268 the first instantiation was `extern' and the second is not,
11269 and EXTERN_P for the opposite case. */
11270 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11271 pedwarn ("duplicate explicit instantiation of %q#D", result);
11272 /* If an "extern" explicit instantiation follows an ordinary
11273 explicit instantiation, the template is instantiated. */
11274 if (extern_p)
11275 return;
11277 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11279 error ("no matching template for %qD found", result);
11280 return;
11282 else if (!DECL_TEMPLATE_INFO (result))
11284 pedwarn ("explicit instantiation of non-template %q#D", result);
11285 return;
11288 if (storage == NULL_TREE)
11290 else if (storage == ridpointers[(int) RID_EXTERN])
11292 if (pedantic && !in_system_header)
11293 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11294 "instantiations");
11295 extern_p = 1;
11297 else
11298 error ("storage class %qD applied to template instantiation", storage);
11300 check_explicit_instantiation_namespace (result);
11301 mark_decl_instantiated (result, extern_p);
11302 if (! extern_p)
11303 instantiate_decl (result, /*defer_ok=*/1,
11304 /*expl_inst_class_mem_p=*/false);
11307 static void
11308 mark_class_instantiated (tree t, int extern_p)
11310 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11311 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11312 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11313 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11314 if (! extern_p)
11316 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11317 rest_of_type_compilation (t, 1);
11321 /* Called from do_type_instantiation through binding_table_foreach to
11322 do recursive instantiation for the type bound in ENTRY. */
11323 static void
11324 bt_instantiate_type_proc (binding_entry entry, void *data)
11326 tree storage = *(tree *) data;
11328 if (IS_AGGR_TYPE (entry->type)
11329 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11330 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11333 /* Called from do_type_instantiation to instantiate a member
11334 (a member function or a static member variable) of an
11335 explicitly instantiated class template. */
11336 static void
11337 instantiate_class_member (tree decl, int extern_p)
11339 mark_decl_instantiated (decl, extern_p);
11340 if (! extern_p)
11341 instantiate_decl (decl, /*defer_ok=*/1,
11342 /*expl_inst_class_mem_p=*/true);
11345 /* Perform an explicit instantiation of template class T. STORAGE, if
11346 non-null, is the RID for extern, inline or static. COMPLAIN is
11347 nonzero if this is called from the parser, zero if called recursively,
11348 since the standard is unclear (as detailed below). */
11350 void
11351 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11353 int extern_p = 0;
11354 int nomem_p = 0;
11355 int static_p = 0;
11356 int previous_instantiation_extern_p = 0;
11358 if (TREE_CODE (t) == TYPE_DECL)
11359 t = TREE_TYPE (t);
11361 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11363 error ("explicit instantiation of non-template type %qT", t);
11364 return;
11367 complete_type (t);
11369 if (!COMPLETE_TYPE_P (t))
11371 if (complain & tf_error)
11372 error ("explicit instantiation of %q#T before definition of template",
11374 return;
11377 if (storage != NULL_TREE)
11379 if (pedantic && !in_system_header)
11380 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11381 storage);
11383 if (storage == ridpointers[(int) RID_INLINE])
11384 nomem_p = 1;
11385 else if (storage == ridpointers[(int) RID_EXTERN])
11386 extern_p = 1;
11387 else if (storage == ridpointers[(int) RID_STATIC])
11388 static_p = 1;
11389 else
11391 error ("storage class %qD applied to template instantiation",
11392 storage);
11393 extern_p = 0;
11397 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11399 /* DR 259 [temp.spec].
11401 Both an explicit instantiation and a declaration of an explicit
11402 specialization shall not appear in a program unless the explicit
11403 instantiation follows a declaration of the explicit specialization.
11405 For a given set of template parameters, if an explicit
11406 instantiation of a template appears after a declaration of an
11407 explicit specialization for that template, the explicit
11408 instantiation has no effect. */
11409 return;
11411 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11413 /* [temp.spec]
11415 No program shall explicitly instantiate any template more
11416 than once.
11418 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11419 instantiation was `extern'. If EXTERN_P then the second is.
11420 These cases are OK. */
11421 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11423 if (!previous_instantiation_extern_p && !extern_p
11424 && (complain & tf_error))
11425 pedwarn ("duplicate explicit instantiation of %q#T", t);
11427 /* If we've already instantiated the template, just return now. */
11428 if (!CLASSTYPE_INTERFACE_ONLY (t))
11429 return;
11432 check_explicit_instantiation_namespace (TYPE_NAME (t));
11433 mark_class_instantiated (t, extern_p);
11435 if (nomem_p)
11436 return;
11439 tree tmp;
11441 /* In contrast to implicit instantiation, where only the
11442 declarations, and not the definitions, of members are
11443 instantiated, we have here:
11445 [temp.explicit]
11447 The explicit instantiation of a class template specialization
11448 implies the instantiation of all of its members not
11449 previously explicitly specialized in the translation unit
11450 containing the explicit instantiation.
11452 Of course, we can't instantiate member template classes, since
11453 we don't have any arguments for them. Note that the standard
11454 is unclear on whether the instantiation of the members are
11455 *explicit* instantiations or not. However, the most natural
11456 interpretation is that it should be an explicit instantiation. */
11458 if (! static_p)
11459 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11460 if (TREE_CODE (tmp) == FUNCTION_DECL
11461 && DECL_TEMPLATE_INSTANTIATION (tmp))
11462 instantiate_class_member (tmp, extern_p);
11464 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11465 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11466 instantiate_class_member (tmp, extern_p);
11468 if (CLASSTYPE_NESTED_UTDS (t))
11469 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11470 bt_instantiate_type_proc, &storage);
11474 /* Given a function DECL, which is a specialization of TMPL, modify
11475 DECL to be a re-instantiation of TMPL with the same template
11476 arguments. TMPL should be the template into which tsubst'ing
11477 should occur for DECL, not the most general template.
11479 One reason for doing this is a scenario like this:
11481 template <class T>
11482 void f(const T&, int i);
11484 void g() { f(3, 7); }
11486 template <class T>
11487 void f(const T& t, const int i) { }
11489 Note that when the template is first instantiated, with
11490 instantiate_template, the resulting DECL will have no name for the
11491 first parameter, and the wrong type for the second. So, when we go
11492 to instantiate the DECL, we regenerate it. */
11494 static void
11495 regenerate_decl_from_template (tree decl, tree tmpl)
11497 /* The arguments used to instantiate DECL, from the most general
11498 template. */
11499 tree args;
11500 tree code_pattern;
11502 args = DECL_TI_ARGS (decl);
11503 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11505 /* Make sure that we can see identifiers, and compute access
11506 correctly. */
11507 push_access_scope (decl);
11509 if (TREE_CODE (decl) == FUNCTION_DECL)
11511 tree decl_parm;
11512 tree pattern_parm;
11513 tree specs;
11514 int args_depth;
11515 int parms_depth;
11517 args_depth = TMPL_ARGS_DEPTH (args);
11518 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11519 if (args_depth > parms_depth)
11520 args = get_innermost_template_args (args, parms_depth);
11522 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11523 args, tf_error, NULL_TREE);
11524 if (specs)
11525 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11526 specs);
11528 /* Merge parameter declarations. */
11529 decl_parm = skip_artificial_parms_for (decl,
11530 DECL_ARGUMENTS (decl));
11531 pattern_parm
11532 = skip_artificial_parms_for (code_pattern,
11533 DECL_ARGUMENTS (code_pattern));
11534 while (decl_parm)
11536 tree parm_type;
11537 tree attributes;
11539 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11540 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11541 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11542 NULL_TREE);
11543 parm_type = type_decays_to (parm_type);
11544 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11545 TREE_TYPE (decl_parm) = parm_type;
11546 attributes = DECL_ATTRIBUTES (pattern_parm);
11547 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11549 DECL_ATTRIBUTES (decl_parm) = attributes;
11550 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11552 decl_parm = TREE_CHAIN (decl_parm);
11553 pattern_parm = TREE_CHAIN (pattern_parm);
11556 /* Merge additional specifiers from the CODE_PATTERN. */
11557 if (DECL_DECLARED_INLINE_P (code_pattern)
11558 && !DECL_DECLARED_INLINE_P (decl))
11559 DECL_DECLARED_INLINE_P (decl) = 1;
11560 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11561 DECL_INLINE (decl) = 1;
11563 else if (TREE_CODE (decl) == VAR_DECL)
11564 DECL_INITIAL (decl) =
11565 tsubst_expr (DECL_INITIAL (code_pattern), args,
11566 tf_error, DECL_TI_TEMPLATE (decl));
11567 else
11568 gcc_unreachable ();
11570 pop_access_scope (decl);
11573 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11574 substituted to get DECL. */
11576 tree
11577 template_for_substitution (tree decl)
11579 tree tmpl = DECL_TI_TEMPLATE (decl);
11581 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11582 for the instantiation. This is not always the most general
11583 template. Consider, for example:
11585 template <class T>
11586 struct S { template <class U> void f();
11587 template <> void f<int>(); };
11589 and an instantiation of S<double>::f<int>. We want TD to be the
11590 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11591 while (/* An instantiation cannot have a definition, so we need a
11592 more general template. */
11593 DECL_TEMPLATE_INSTANTIATION (tmpl)
11594 /* We must also deal with friend templates. Given:
11596 template <class T> struct S {
11597 template <class U> friend void f() {};
11600 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11601 so far as the language is concerned, but that's still
11602 where we get the pattern for the instantiation from. On
11603 other hand, if the definition comes outside the class, say:
11605 template <class T> struct S {
11606 template <class U> friend void f();
11608 template <class U> friend void f() {}
11610 we don't need to look any further. That's what the check for
11611 DECL_INITIAL is for. */
11612 || (TREE_CODE (decl) == FUNCTION_DECL
11613 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11614 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11616 /* The present template, TD, should not be a definition. If it
11617 were a definition, we should be using it! Note that we
11618 cannot restructure the loop to just keep going until we find
11619 a template with a definition, since that might go too far if
11620 a specialization was declared, but not defined. */
11621 gcc_assert (TREE_CODE (decl) != VAR_DECL
11622 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11624 /* Fetch the more general template. */
11625 tmpl = DECL_TI_TEMPLATE (tmpl);
11628 return tmpl;
11631 /* Produce the definition of D, a _DECL generated from a template. If
11632 DEFER_OK is nonzero, then we don't have to actually do the
11633 instantiation now; we just have to do it sometime. Normally it is
11634 an error if this is an explicit instantiation but D is undefined.
11635 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11636 explicitly instantiated class template. */
11638 tree
11639 instantiate_decl (tree d, int defer_ok,
11640 bool expl_inst_class_mem_p)
11642 tree tmpl = DECL_TI_TEMPLATE (d);
11643 tree gen_args;
11644 tree args;
11645 tree td;
11646 tree code_pattern;
11647 tree spec;
11648 tree gen_tmpl;
11649 bool pattern_defined;
11650 int need_push;
11651 location_t saved_loc = input_location;
11652 bool external_p;
11654 /* This function should only be used to instantiate templates for
11655 functions and static member variables. */
11656 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11657 || TREE_CODE (d) == VAR_DECL);
11659 /* Variables are never deferred; if instantiation is required, they
11660 are instantiated right away. That allows for better code in the
11661 case that an expression refers to the value of the variable --
11662 if the variable has a constant value the referring expression can
11663 take advantage of that fact. */
11664 if (TREE_CODE (d) == VAR_DECL)
11665 defer_ok = 0;
11667 /* Don't instantiate cloned functions. Instead, instantiate the
11668 functions they cloned. */
11669 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11670 d = DECL_CLONED_FUNCTION (d);
11672 if (DECL_TEMPLATE_INSTANTIATED (d))
11673 /* D has already been instantiated. It might seem reasonable to
11674 check whether or not D is an explicit instantiation, and, if so,
11675 stop here. But when an explicit instantiation is deferred
11676 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11677 is set, even though we still need to do the instantiation. */
11678 return d;
11680 /* If we already have a specialization of this declaration, then
11681 there's no reason to instantiate it. Note that
11682 retrieve_specialization gives us both instantiations and
11683 specializations, so we must explicitly check
11684 DECL_TEMPLATE_SPECIALIZATION. */
11685 gen_tmpl = most_general_template (tmpl);
11686 gen_args = DECL_TI_ARGS (d);
11687 spec = retrieve_specialization (gen_tmpl, gen_args,
11688 /*class_specializations_p=*/false);
11689 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11690 return spec;
11692 /* This needs to happen before any tsubsting. */
11693 if (! push_tinst_level (d))
11694 return d;
11696 timevar_push (TV_PARSE);
11698 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11699 for the instantiation. */
11700 td = template_for_substitution (d);
11701 code_pattern = DECL_TEMPLATE_RESULT (td);
11703 /* We should never be trying to instantiate a member of a class
11704 template or partial specialization. */
11705 gcc_assert (d != code_pattern);
11707 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11708 || DECL_TEMPLATE_SPECIALIZATION (td))
11709 /* In the case of a friend template whose definition is provided
11710 outside the class, we may have too many arguments. Drop the
11711 ones we don't need. The same is true for specializations. */
11712 args = get_innermost_template_args
11713 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11714 else
11715 args = gen_args;
11717 if (TREE_CODE (d) == FUNCTION_DECL)
11718 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11719 else
11720 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11722 /* We may be in the middle of deferred access check. Disable it now. */
11723 push_deferring_access_checks (dk_no_deferred);
11725 /* Unless an explicit instantiation directive has already determined
11726 the linkage of D, remember that a definition is available for
11727 this entity. */
11728 if (pattern_defined
11729 && !DECL_INTERFACE_KNOWN (d)
11730 && !DECL_NOT_REALLY_EXTERN (d))
11731 mark_definable (d);
11733 input_location = DECL_SOURCE_LOCATION (d);
11735 /* If D is a member of an explicitly instantiated class template,
11736 and no definition is available, treat it like an implicit
11737 instantiation. */
11738 if (!pattern_defined && expl_inst_class_mem_p
11739 && DECL_EXPLICIT_INSTANTIATION (d))
11741 DECL_NOT_REALLY_EXTERN (d) = 0;
11742 DECL_INTERFACE_KNOWN (d) = 0;
11743 SET_DECL_IMPLICIT_INSTANTIATION (d);
11746 if (!defer_ok)
11748 /* Recheck the substitutions to obtain any warning messages
11749 about ignoring cv qualifiers. */
11750 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11751 tree type = TREE_TYPE (gen);
11753 /* Make sure that we can see identifiers, and compute access
11754 correctly. D is already the target FUNCTION_DECL with the
11755 right context. */
11756 push_access_scope (d);
11758 if (TREE_CODE (gen) == FUNCTION_DECL)
11760 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
11761 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11762 tf_warning_or_error, d);
11763 /* Don't simply tsubst the function type, as that will give
11764 duplicate warnings about poor parameter qualifications.
11765 The function arguments are the same as the decl_arguments
11766 without the top level cv qualifiers. */
11767 type = TREE_TYPE (type);
11769 tsubst (type, gen_args, tf_warning_or_error, d);
11771 pop_access_scope (d);
11774 /* Check to see whether we know that this template will be
11775 instantiated in some other file, as with "extern template"
11776 extension. */
11777 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
11778 /* In general, we do not instantiate such templates... */
11779 if (external_p
11780 /* ... but we instantiate inline functions so that we can inline
11781 them and ... */
11782 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
11783 /* ... we instantiate static data members whose values are
11784 needed in integral constant expressions. */
11785 && ! (TREE_CODE (d) == VAR_DECL
11786 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
11787 goto out;
11788 /* Defer all other templates, unless we have been explicitly
11789 forbidden from doing so. */
11790 if (/* If there is no definition, we cannot instantiate the
11791 template. */
11792 ! pattern_defined
11793 /* If it's OK to postpone instantiation, do so. */
11794 || defer_ok
11795 /* If this is a static data member that will be defined
11796 elsewhere, we don't want to instantiate the entire data
11797 member, but we do want to instantiate the initializer so that
11798 we can substitute that elsewhere. */
11799 || (external_p && TREE_CODE (d) == VAR_DECL))
11801 /* The definition of the static data member is now required so
11802 we must substitute the initializer. */
11803 if (TREE_CODE (d) == VAR_DECL
11804 && !DECL_INITIAL (d)
11805 && DECL_INITIAL (code_pattern))
11807 tree ns;
11808 tree init;
11810 ns = decl_namespace_context (d);
11811 push_nested_namespace (ns);
11812 push_nested_class (DECL_CONTEXT (d));
11813 init = tsubst_expr (DECL_INITIAL (code_pattern),
11814 args,
11815 tf_warning_or_error, NULL_TREE);
11816 DECL_INITIAL (d) = init;
11817 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
11818 /*asmspec_tree=*/NULL_TREE,
11819 LOOKUP_ONLYCONVERTING);
11820 pop_nested_class ();
11821 pop_nested_namespace (ns);
11824 /* We restore the source position here because it's used by
11825 add_pending_template. */
11826 input_location = saved_loc;
11828 if (at_eof && !pattern_defined
11829 && DECL_EXPLICIT_INSTANTIATION (d))
11830 /* [temp.explicit]
11832 The definition of a non-exported function template, a
11833 non-exported member function template, or a non-exported
11834 member function or static data member of a class template
11835 shall be present in every translation unit in which it is
11836 explicitly instantiated. */
11837 pedwarn
11838 ("explicit instantiation of %qD but no definition available", d);
11840 /* ??? Historically, we have instantiated inline functions, even
11841 when marked as "extern template". */
11842 if (!(external_p && TREE_CODE (d) == VAR_DECL))
11843 add_pending_template (d);
11844 goto out;
11846 /* Tell the repository that D is available in this translation unit
11847 -- and see if it is supposed to be instantiated here. */
11848 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11850 /* In a PCH file, despite the fact that the repository hasn't
11851 requested instantiation in the PCH it is still possible that
11852 an instantiation will be required in a file that includes the
11853 PCH. */
11854 if (pch_file)
11855 add_pending_template (d);
11856 /* Instantiate inline functions so that the inliner can do its
11857 job, even though we'll not be emitting a copy of this
11858 function. */
11859 if (!(TREE_CODE (d) == FUNCTION_DECL
11860 && flag_inline_trees
11861 && DECL_DECLARED_INLINE_P (d)))
11862 goto out;
11865 need_push = !cfun || !global_bindings_p ();
11866 if (need_push)
11867 push_to_top_level ();
11869 /* Mark D as instantiated so that recursive calls to
11870 instantiate_decl do not try to instantiate it again. */
11871 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11873 /* Regenerate the declaration in case the template has been modified
11874 by a subsequent redeclaration. */
11875 regenerate_decl_from_template (d, td);
11877 /* We already set the file and line above. Reset them now in case
11878 they changed as a result of calling regenerate_decl_from_template. */
11879 input_location = DECL_SOURCE_LOCATION (d);
11881 if (TREE_CODE (d) == VAR_DECL)
11883 /* Clear out DECL_RTL; whatever was there before may not be right
11884 since we've reset the type of the declaration. */
11885 SET_DECL_RTL (d, NULL_RTX);
11886 DECL_IN_AGGR_P (d) = 0;
11888 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11889 initializer. That function will defer actual emission until
11890 we have a chance to determine linkage. */
11891 DECL_EXTERNAL (d) = 0;
11893 /* Enter the scope of D so that access-checking works correctly. */
11894 push_nested_class (DECL_CONTEXT (d));
11895 finish_decl (d, DECL_INITIAL (d), NULL_TREE);
11896 pop_nested_class ();
11898 else if (TREE_CODE (d) == FUNCTION_DECL)
11900 htab_t saved_local_specializations;
11901 tree subst_decl;
11902 tree tmpl_parm;
11903 tree spec_parm;
11905 /* Save away the current list, in case we are instantiating one
11906 template from within the body of another. */
11907 saved_local_specializations = local_specializations;
11909 /* Set up the list of local specializations. */
11910 local_specializations = htab_create (37,
11911 hash_local_specialization,
11912 eq_local_specializations,
11913 NULL);
11915 /* Set up context. */
11916 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11918 /* Create substitution entries for the parameters. */
11919 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11920 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11921 spec_parm = DECL_ARGUMENTS (d);
11922 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11924 register_local_specialization (spec_parm, tmpl_parm);
11925 spec_parm = skip_artificial_parms_for (d, spec_parm);
11926 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11928 while (tmpl_parm)
11930 register_local_specialization (spec_parm, tmpl_parm);
11931 tmpl_parm = TREE_CHAIN (tmpl_parm);
11932 spec_parm = TREE_CHAIN (spec_parm);
11934 gcc_assert (!spec_parm);
11936 /* Substitute into the body of the function. */
11937 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11938 tf_warning_or_error, tmpl);
11940 /* We don't need the local specializations any more. */
11941 htab_delete (local_specializations);
11942 local_specializations = saved_local_specializations;
11944 /* Finish the function. */
11945 d = finish_function (0);
11946 expand_or_defer_fn (d);
11949 /* We're not deferring instantiation any more. */
11950 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11952 if (need_push)
11953 pop_from_top_level ();
11955 out:
11956 input_location = saved_loc;
11957 pop_deferring_access_checks ();
11958 pop_tinst_level ();
11960 timevar_pop (TV_PARSE);
11962 return d;
11965 /* Run through the list of templates that we wish we could
11966 instantiate, and instantiate any we can. RETRIES is the
11967 number of times we retry pending template instantiation. */
11969 void
11970 instantiate_pending_templates (int retries)
11972 tree *t;
11973 tree last = NULL_TREE;
11974 int reconsider;
11975 location_t saved_loc = input_location;
11976 int saved_in_system_header = in_system_header;
11978 /* Instantiating templates may trigger vtable generation. This in turn
11979 may require further template instantiations. We place a limit here
11980 to avoid infinite loop. */
11981 if (pending_templates && retries >= max_tinst_depth)
11983 tree decl = TREE_VALUE (pending_templates);
11985 error ("template instantiation depth exceeds maximum of %d"
11986 " instantiating %q+D, possibly from virtual table generation"
11987 " (use -ftemplate-depth-NN to increase the maximum)",
11988 max_tinst_depth, decl);
11989 if (TREE_CODE (decl) == FUNCTION_DECL)
11990 /* Pretend that we defined it. */
11991 DECL_INITIAL (decl) = error_mark_node;
11992 return;
11997 reconsider = 0;
11999 t = &pending_templates;
12000 while (*t)
12002 tree instantiation = TREE_VALUE (*t);
12004 reopen_tinst_level (TREE_PURPOSE (*t));
12006 if (TYPE_P (instantiation))
12008 tree fn;
12010 if (!COMPLETE_TYPE_P (instantiation))
12012 instantiate_class_template (instantiation);
12013 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12014 for (fn = TYPE_METHODS (instantiation);
12016 fn = TREE_CHAIN (fn))
12017 if (! DECL_ARTIFICIAL (fn))
12018 instantiate_decl (fn,
12019 /*defer_ok=*/0,
12020 /*expl_inst_class_mem_p=*/false);
12021 if (COMPLETE_TYPE_P (instantiation))
12022 reconsider = 1;
12025 if (COMPLETE_TYPE_P (instantiation))
12026 /* If INSTANTIATION has been instantiated, then we don't
12027 need to consider it again in the future. */
12028 *t = TREE_CHAIN (*t);
12029 else
12031 last = *t;
12032 t = &TREE_CHAIN (*t);
12035 else
12037 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12038 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12040 instantiation
12041 = instantiate_decl (instantiation,
12042 /*defer_ok=*/0,
12043 /*expl_inst_class_mem_p=*/false);
12044 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12045 reconsider = 1;
12048 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12049 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12050 /* If INSTANTIATION has been instantiated, then we don't
12051 need to consider it again in the future. */
12052 *t = TREE_CHAIN (*t);
12053 else
12055 last = *t;
12056 t = &TREE_CHAIN (*t);
12059 tinst_depth = 0;
12060 current_tinst_level = NULL_TREE;
12062 last_pending_template = last;
12064 while (reconsider);
12066 input_location = saved_loc;
12067 in_system_header = saved_in_system_header;
12070 /* Substitute ARGVEC into T, which is a list of initializers for
12071 either base class or a non-static data member. The TREE_PURPOSEs
12072 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12073 instantiate_decl. */
12075 static tree
12076 tsubst_initializer_list (tree t, tree argvec)
12078 tree inits = NULL_TREE;
12080 for (; t; t = TREE_CHAIN (t))
12082 tree decl;
12083 tree init;
12085 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
12086 NULL_TREE);
12087 decl = expand_member_init (decl);
12088 if (decl && !DECL_P (decl))
12089 in_base_initializer = 1;
12091 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12092 NULL_TREE);
12093 in_base_initializer = 0;
12095 if (decl)
12097 init = build_tree_list (decl, init);
12098 TREE_CHAIN (init) = inits;
12099 inits = init;
12102 return inits;
12105 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12107 static void
12108 set_current_access_from_decl (tree decl)
12110 if (TREE_PRIVATE (decl))
12111 current_access_specifier = access_private_node;
12112 else if (TREE_PROTECTED (decl))
12113 current_access_specifier = access_protected_node;
12114 else
12115 current_access_specifier = access_public_node;
12118 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12119 is the instantiation (which should have been created with
12120 start_enum) and ARGS are the template arguments to use. */
12122 static void
12123 tsubst_enum (tree tag, tree newtag, tree args)
12125 tree e;
12127 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12129 tree value;
12130 tree decl;
12132 decl = TREE_VALUE (e);
12133 /* Note that in a template enum, the TREE_VALUE is the
12134 CONST_DECL, not the corresponding INTEGER_CST. */
12135 value = tsubst_expr (DECL_INITIAL (decl),
12136 args, tf_warning_or_error, NULL_TREE);
12138 /* Give this enumeration constant the correct access. */
12139 set_current_access_from_decl (decl);
12141 /* Actually build the enumerator itself. */
12142 build_enumerator (DECL_NAME (decl), value, newtag);
12145 finish_enum (newtag);
12146 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12147 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12150 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12151 its type -- but without substituting the innermost set of template
12152 arguments. So, innermost set of template parameters will appear in
12153 the type. */
12155 tree
12156 get_mostly_instantiated_function_type (tree decl)
12158 tree fn_type;
12159 tree tmpl;
12160 tree targs;
12161 tree tparms;
12162 int parm_depth;
12164 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12165 targs = DECL_TI_ARGS (decl);
12166 tparms = DECL_TEMPLATE_PARMS (tmpl);
12167 parm_depth = TMPL_PARMS_DEPTH (tparms);
12169 /* There should be as many levels of arguments as there are levels
12170 of parameters. */
12171 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12173 fn_type = TREE_TYPE (tmpl);
12175 if (parm_depth == 1)
12176 /* No substitution is necessary. */
12178 else
12180 int i, save_access_control;
12181 tree partial_args;
12183 /* Replace the innermost level of the TARGS with NULL_TREEs to
12184 let tsubst know not to substitute for those parameters. */
12185 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12186 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12187 SET_TMPL_ARGS_LEVEL (partial_args, i,
12188 TMPL_ARGS_LEVEL (targs, i));
12189 SET_TMPL_ARGS_LEVEL (partial_args,
12190 TMPL_ARGS_DEPTH (targs),
12191 make_tree_vec (DECL_NTPARMS (tmpl)));
12193 /* Disable access control as this function is used only during
12194 name-mangling. */
12195 save_access_control = flag_access_control;
12196 flag_access_control = 0;
12198 ++processing_template_decl;
12199 /* Now, do the (partial) substitution to figure out the
12200 appropriate function type. */
12201 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12202 --processing_template_decl;
12204 /* Substitute into the template parameters to obtain the real
12205 innermost set of parameters. This step is important if the
12206 innermost set of template parameters contains value
12207 parameters whose types depend on outer template parameters. */
12208 TREE_VEC_LENGTH (partial_args)--;
12209 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12211 flag_access_control = save_access_control;
12214 return fn_type;
12217 /* Return truthvalue if we're processing a template different from
12218 the last one involved in diagnostics. */
12220 problematic_instantiation_changed (void)
12222 return last_template_error_tick != tinst_level_tick;
12225 /* Remember current template involved in diagnostics. */
12226 void
12227 record_last_problematic_instantiation (void)
12229 last_template_error_tick = tinst_level_tick;
12232 tree
12233 current_instantiation (void)
12235 return current_tinst_level;
12238 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12239 type. Return zero for ok, nonzero for disallowed. Issue error and
12240 warning messages under control of COMPLAIN. */
12242 static int
12243 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12245 if (INTEGRAL_TYPE_P (type))
12246 return 0;
12247 else if (POINTER_TYPE_P (type))
12248 return 0;
12249 else if (TYPE_PTR_TO_MEMBER_P (type))
12250 return 0;
12251 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12252 return 0;
12253 else if (TREE_CODE (type) == TYPENAME_TYPE)
12254 return 0;
12256 if (complain & tf_error)
12257 error ("%q#T is not a valid type for a template constant parameter", type);
12258 return 1;
12261 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12262 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12264 static bool
12265 dependent_type_p_r (tree type)
12267 tree scope;
12269 /* [temp.dep.type]
12271 A type is dependent if it is:
12273 -- a template parameter. Template template parameters are types
12274 for us (since TYPE_P holds true for them) so we handle
12275 them here. */
12276 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12277 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12278 return true;
12279 /* -- a qualified-id with a nested-name-specifier which contains a
12280 class-name that names a dependent type or whose unqualified-id
12281 names a dependent type. */
12282 if (TREE_CODE (type) == TYPENAME_TYPE)
12283 return true;
12284 /* -- a cv-qualified type where the cv-unqualified type is
12285 dependent. */
12286 type = TYPE_MAIN_VARIANT (type);
12287 /* -- a compound type constructed from any dependent type. */
12288 if (TYPE_PTR_TO_MEMBER_P (type))
12289 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12290 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12291 (type)));
12292 else if (TREE_CODE (type) == POINTER_TYPE
12293 || TREE_CODE (type) == REFERENCE_TYPE)
12294 return dependent_type_p (TREE_TYPE (type));
12295 else if (TREE_CODE (type) == FUNCTION_TYPE
12296 || TREE_CODE (type) == METHOD_TYPE)
12298 tree arg_type;
12300 if (dependent_type_p (TREE_TYPE (type)))
12301 return true;
12302 for (arg_type = TYPE_ARG_TYPES (type);
12303 arg_type;
12304 arg_type = TREE_CHAIN (arg_type))
12305 if (dependent_type_p (TREE_VALUE (arg_type)))
12306 return true;
12307 return false;
12309 /* -- an array type constructed from any dependent type or whose
12310 size is specified by a constant expression that is
12311 value-dependent. */
12312 if (TREE_CODE (type) == ARRAY_TYPE)
12314 if (TYPE_DOMAIN (type)
12315 && ((value_dependent_expression_p
12316 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12317 || (type_dependent_expression_p
12318 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12319 return true;
12320 return dependent_type_p (TREE_TYPE (type));
12323 /* -- a template-id in which either the template name is a template
12324 parameter ... */
12325 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12326 return true;
12327 /* ... or any of the template arguments is a dependent type or
12328 an expression that is type-dependent or value-dependent. */
12329 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12330 && (any_dependent_template_arguments_p
12331 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12332 return true;
12334 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12335 expression is not type-dependent, then it should already been
12336 have resolved. */
12337 if (TREE_CODE (type) == TYPEOF_TYPE)
12338 return true;
12340 /* The standard does not specifically mention types that are local
12341 to template functions or local classes, but they should be
12342 considered dependent too. For example:
12344 template <int I> void f() {
12345 enum E { a = I };
12346 S<sizeof (E)> s;
12349 The size of `E' cannot be known until the value of `I' has been
12350 determined. Therefore, `E' must be considered dependent. */
12351 scope = TYPE_CONTEXT (type);
12352 if (scope && TYPE_P (scope))
12353 return dependent_type_p (scope);
12354 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12355 return type_dependent_expression_p (scope);
12357 /* Other types are non-dependent. */
12358 return false;
12361 /* Returns TRUE if TYPE is dependent, in the sense of
12362 [temp.dep.type]. */
12364 bool
12365 dependent_type_p (tree type)
12367 /* If there are no template parameters in scope, then there can't be
12368 any dependent types. */
12369 if (!processing_template_decl)
12371 /* If we are not processing a template, then nobody should be
12372 providing us with a dependent type. */
12373 gcc_assert (type);
12374 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12375 return false;
12378 /* If the type is NULL, we have not computed a type for the entity
12379 in question; in that case, the type is dependent. */
12380 if (!type)
12381 return true;
12383 /* Erroneous types can be considered non-dependent. */
12384 if (type == error_mark_node)
12385 return false;
12387 /* If we have not already computed the appropriate value for TYPE,
12388 do so now. */
12389 if (!TYPE_DEPENDENT_P_VALID (type))
12391 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12392 TYPE_DEPENDENT_P_VALID (type) = 1;
12395 return TYPE_DEPENDENT_P (type);
12398 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12400 static bool
12401 dependent_scope_ref_p (tree expression, bool criterion (tree))
12403 tree scope;
12404 tree name;
12406 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12408 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12409 return true;
12411 scope = TREE_OPERAND (expression, 0);
12412 name = TREE_OPERAND (expression, 1);
12414 /* [temp.dep.expr]
12416 An id-expression is type-dependent if it contains a
12417 nested-name-specifier that contains a class-name that names a
12418 dependent type. */
12419 /* The suggested resolution to Core Issue 2 implies that if the
12420 qualifying type is the current class, then we must peek
12421 inside it. */
12422 if (DECL_P (name)
12423 && currently_open_class (scope)
12424 && !criterion (name))
12425 return false;
12426 if (dependent_type_p (scope))
12427 return true;
12429 return false;
12432 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12433 [temp.dep.constexpr] */
12435 bool
12436 value_dependent_expression_p (tree expression)
12438 if (!processing_template_decl)
12439 return false;
12441 /* A name declared with a dependent type. */
12442 if (DECL_P (expression) && type_dependent_expression_p (expression))
12443 return true;
12445 switch (TREE_CODE (expression))
12447 case IDENTIFIER_NODE:
12448 /* A name that has not been looked up -- must be dependent. */
12449 return true;
12451 case TEMPLATE_PARM_INDEX:
12452 /* A non-type template parm. */
12453 return true;
12455 case CONST_DECL:
12456 /* A non-type template parm. */
12457 if (DECL_TEMPLATE_PARM_P (expression))
12458 return true;
12459 return false;
12461 case VAR_DECL:
12462 /* A constant with integral or enumeration type and is initialized
12463 with an expression that is value-dependent. */
12464 if (DECL_INITIAL (expression)
12465 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12466 && value_dependent_expression_p (DECL_INITIAL (expression)))
12467 return true;
12468 return false;
12470 case DYNAMIC_CAST_EXPR:
12471 case STATIC_CAST_EXPR:
12472 case CONST_CAST_EXPR:
12473 case REINTERPRET_CAST_EXPR:
12474 case CAST_EXPR:
12475 /* These expressions are value-dependent if the type to which
12476 the cast occurs is dependent or the expression being casted
12477 is value-dependent. */
12479 tree type = TREE_TYPE (expression);
12481 if (dependent_type_p (type))
12482 return true;
12484 /* A functional cast has a list of operands. */
12485 expression = TREE_OPERAND (expression, 0);
12486 if (!expression)
12488 /* If there are no operands, it must be an expression such
12489 as "int()". This should not happen for aggregate types
12490 because it would form non-constant expressions. */
12491 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12493 return false;
12496 if (TREE_CODE (expression) == TREE_LIST)
12498 for (; expression; expression = TREE_CHAIN (expression))
12499 if (value_dependent_expression_p (TREE_VALUE (expression)))
12500 return true;
12501 return false;
12504 return value_dependent_expression_p (expression);
12507 case SIZEOF_EXPR:
12508 case ALIGNOF_EXPR:
12509 /* A `sizeof' expression is value-dependent if the operand is
12510 type-dependent. */
12511 expression = TREE_OPERAND (expression, 0);
12512 if (TYPE_P (expression))
12513 return dependent_type_p (expression);
12514 return type_dependent_expression_p (expression);
12516 case SCOPE_REF:
12517 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12519 case COMPONENT_REF:
12520 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12521 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12523 case CALL_EXPR:
12524 /* A CALL_EXPR is value-dependent if any argument is
12525 value-dependent. Why do we have to handle CALL_EXPRs in this
12526 function at all? First, some function calls, those for which
12527 value_dependent_expression_p is true, man appear in constant
12528 expressions. Second, there appear to be bugs which result in
12529 other CALL_EXPRs reaching this point. */
12531 tree function = TREE_OPERAND (expression, 0);
12532 tree args = TREE_OPERAND (expression, 1);
12534 if (value_dependent_expression_p (function))
12535 return true;
12537 if (! args)
12538 return false;
12540 if (TREE_CODE (args) == TREE_LIST)
12542 for (; args; args = TREE_CHAIN (args))
12543 if (value_dependent_expression_p (TREE_VALUE (args)))
12544 return true;
12545 return false;
12548 return value_dependent_expression_p (args);
12551 default:
12552 /* A constant expression is value-dependent if any subexpression is
12553 value-dependent. */
12554 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12556 case tcc_reference:
12557 case tcc_unary:
12558 return (value_dependent_expression_p
12559 (TREE_OPERAND (expression, 0)));
12561 case tcc_comparison:
12562 case tcc_binary:
12563 return ((value_dependent_expression_p
12564 (TREE_OPERAND (expression, 0)))
12565 || (value_dependent_expression_p
12566 (TREE_OPERAND (expression, 1))));
12568 case tcc_expression:
12570 int i;
12571 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12572 /* In some cases, some of the operands may be missing.
12573 (For example, in the case of PREDECREMENT_EXPR, the
12574 amount to increment by may be missing.) That doesn't
12575 make the expression dependent. */
12576 if (TREE_OPERAND (expression, i)
12577 && (value_dependent_expression_p
12578 (TREE_OPERAND (expression, i))))
12579 return true;
12580 return false;
12583 default:
12584 break;
12588 /* The expression is not value-dependent. */
12589 return false;
12592 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12593 [temp.dep.expr]. */
12595 bool
12596 type_dependent_expression_p (tree expression)
12598 if (!processing_template_decl)
12599 return false;
12601 if (expression == error_mark_node)
12602 return false;
12604 /* An unresolved name is always dependent. */
12605 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12606 return true;
12608 /* Some expression forms are never type-dependent. */
12609 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12610 || TREE_CODE (expression) == SIZEOF_EXPR
12611 || TREE_CODE (expression) == ALIGNOF_EXPR
12612 || TREE_CODE (expression) == TYPEID_EXPR
12613 || TREE_CODE (expression) == DELETE_EXPR
12614 || TREE_CODE (expression) == VEC_DELETE_EXPR
12615 || TREE_CODE (expression) == THROW_EXPR)
12616 return false;
12618 /* The types of these expressions depends only on the type to which
12619 the cast occurs. */
12620 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12621 || TREE_CODE (expression) == STATIC_CAST_EXPR
12622 || TREE_CODE (expression) == CONST_CAST_EXPR
12623 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12624 || TREE_CODE (expression) == CAST_EXPR)
12625 return dependent_type_p (TREE_TYPE (expression));
12627 /* The types of these expressions depends only on the type created
12628 by the expression. */
12629 if (TREE_CODE (expression) == NEW_EXPR
12630 || TREE_CODE (expression) == VEC_NEW_EXPR)
12632 /* For NEW_EXPR tree nodes created inside a template, either
12633 the object type itself or a TREE_LIST may appear as the
12634 operand 1. */
12635 tree type = TREE_OPERAND (expression, 1);
12636 if (TREE_CODE (type) == TREE_LIST)
12637 /* This is an array type. We need to check array dimensions
12638 as well. */
12639 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12640 || value_dependent_expression_p
12641 (TREE_OPERAND (TREE_VALUE (type), 1));
12642 else
12643 return dependent_type_p (type);
12646 if (TREE_CODE (expression) == SCOPE_REF
12647 && dependent_scope_ref_p (expression,
12648 type_dependent_expression_p))
12649 return true;
12651 if (TREE_CODE (expression) == FUNCTION_DECL
12652 && DECL_LANG_SPECIFIC (expression)
12653 && DECL_TEMPLATE_INFO (expression)
12654 && (any_dependent_template_arguments_p
12655 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12656 return true;
12658 if (TREE_CODE (expression) == TEMPLATE_DECL
12659 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12660 return false;
12662 if (TREE_TYPE (expression) == unknown_type_node)
12664 if (TREE_CODE (expression) == ADDR_EXPR)
12665 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12666 if (TREE_CODE (expression) == COMPONENT_REF
12667 || TREE_CODE (expression) == OFFSET_REF)
12669 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12670 return true;
12671 expression = TREE_OPERAND (expression, 1);
12672 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12673 return false;
12675 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12676 if (TREE_CODE (expression) == SCOPE_REF)
12677 return false;
12679 if (TREE_CODE (expression) == BASELINK)
12680 expression = BASELINK_FUNCTIONS (expression);
12682 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12684 if (any_dependent_template_arguments_p
12685 (TREE_OPERAND (expression, 1)))
12686 return true;
12687 expression = TREE_OPERAND (expression, 0);
12689 gcc_assert (TREE_CODE (expression) == OVERLOAD
12690 || TREE_CODE (expression) == FUNCTION_DECL);
12692 while (expression)
12694 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12695 return true;
12696 expression = OVL_NEXT (expression);
12698 return false;
12701 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
12703 return (dependent_type_p (TREE_TYPE (expression)));
12706 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12707 contains a type-dependent expression. */
12709 bool
12710 any_type_dependent_arguments_p (tree args)
12712 while (args)
12714 tree arg = TREE_VALUE (args);
12716 if (type_dependent_expression_p (arg))
12717 return true;
12718 args = TREE_CHAIN (args);
12720 return false;
12723 /* Returns TRUE if the ARG (a template argument) is dependent. */
12725 static bool
12726 dependent_template_arg_p (tree arg)
12728 if (!processing_template_decl)
12729 return false;
12731 if (TREE_CODE (arg) == TEMPLATE_DECL
12732 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12733 return dependent_template_p (arg);
12734 else if (TYPE_P (arg))
12735 return dependent_type_p (arg);
12736 else
12737 return (type_dependent_expression_p (arg)
12738 || value_dependent_expression_p (arg));
12741 /* Returns true if ARGS (a collection of template arguments) contains
12742 any dependent arguments. */
12744 bool
12745 any_dependent_template_arguments_p (tree args)
12747 int i;
12748 int j;
12750 if (!args)
12751 return false;
12753 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12755 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12756 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12757 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12758 return true;
12761 return false;
12764 /* Returns TRUE if the template TMPL is dependent. */
12766 bool
12767 dependent_template_p (tree tmpl)
12769 if (TREE_CODE (tmpl) == OVERLOAD)
12771 while (tmpl)
12773 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12774 return true;
12775 tmpl = OVL_CHAIN (tmpl);
12777 return false;
12780 /* Template template parameters are dependent. */
12781 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12782 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12783 return true;
12784 /* So are names that have not been looked up. */
12785 if (TREE_CODE (tmpl) == SCOPE_REF
12786 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12787 return true;
12788 /* So are member templates of dependent classes. */
12789 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12790 return dependent_type_p (DECL_CONTEXT (tmpl));
12791 return false;
12794 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12796 bool
12797 dependent_template_id_p (tree tmpl, tree args)
12799 return (dependent_template_p (tmpl)
12800 || any_dependent_template_arguments_p (args));
12803 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12804 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12805 can be found. Note that this function peers inside uninstantiated
12806 templates and therefore should be used only in extremely limited
12807 situations. ONLY_CURRENT_P restricts this peering to the currently
12808 open classes hierarchy (which is required when comparing types). */
12810 tree
12811 resolve_typename_type (tree type, bool only_current_p)
12813 tree scope;
12814 tree name;
12815 tree decl;
12816 int quals;
12817 tree pushed_scope;
12819 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12821 scope = TYPE_CONTEXT (type);
12822 name = TYPE_IDENTIFIER (type);
12824 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12825 it first before we can figure out what NAME refers to. */
12826 if (TREE_CODE (scope) == TYPENAME_TYPE)
12827 scope = resolve_typename_type (scope, only_current_p);
12828 /* If we don't know what SCOPE refers to, then we cannot resolve the
12829 TYPENAME_TYPE. */
12830 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12831 return error_mark_node;
12832 /* If the SCOPE is a template type parameter, we have no way of
12833 resolving the name. */
12834 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12835 return type;
12836 /* If the SCOPE is not the current instantiation, there's no reason
12837 to look inside it. */
12838 if (only_current_p && !currently_open_class (scope))
12839 return error_mark_node;
12840 /* If SCOPE is a partial instantiation, it will not have a valid
12841 TYPE_FIELDS list, so use the original template. */
12842 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12843 /* Enter the SCOPE so that name lookup will be resolved as if we
12844 were in the class definition. In particular, SCOPE will no
12845 longer be considered a dependent type. */
12846 pushed_scope = push_scope (scope);
12847 /* Look up the declaration. */
12848 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12849 /* Obtain the set of qualifiers applied to the TYPE. */
12850 quals = cp_type_quals (type);
12851 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12852 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12853 if (!decl)
12854 type = error_mark_node;
12855 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12856 && TREE_CODE (decl) == TYPE_DECL)
12857 type = TREE_TYPE (decl);
12858 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12859 && DECL_CLASS_TEMPLATE_P (decl))
12861 tree tmpl;
12862 tree args;
12863 /* Obtain the template and the arguments. */
12864 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12865 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12866 /* Instantiate the template. */
12867 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12868 /*entering_scope=*/0, tf_error | tf_user);
12870 else
12871 type = error_mark_node;
12872 /* Qualify the resulting type. */
12873 if (type != error_mark_node && quals)
12874 type = cp_build_qualified_type (type, quals);
12875 /* Leave the SCOPE. */
12876 if (pushed_scope)
12877 pop_scope (pushed_scope);
12879 return type;
12882 /* EXPR is an expression which is not type-dependent. Return a proxy
12883 for EXPR that can be used to compute the types of larger
12884 expressions containing EXPR. */
12886 tree
12887 build_non_dependent_expr (tree expr)
12889 tree inner_expr;
12891 /* Preserve null pointer constants so that the type of things like
12892 "p == 0" where "p" is a pointer can be determined. */
12893 if (null_ptr_cst_p (expr))
12894 return expr;
12895 /* Preserve OVERLOADs; the functions must be available to resolve
12896 types. */
12897 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12898 TREE_OPERAND (expr, 0) :
12899 TREE_CODE (expr) == COMPONENT_REF ?
12900 TREE_OPERAND (expr, 1) : expr);
12901 if (is_overloaded_fn (inner_expr)
12902 || TREE_CODE (inner_expr) == OFFSET_REF)
12903 return expr;
12904 /* There is no need to return a proxy for a variable. */
12905 if (TREE_CODE (expr) == VAR_DECL)
12906 return expr;
12907 /* Preserve string constants; conversions from string constants to
12908 "char *" are allowed, even though normally a "const char *"
12909 cannot be used to initialize a "char *". */
12910 if (TREE_CODE (expr) == STRING_CST)
12911 return expr;
12912 /* Preserve arithmetic constants, as an optimization -- there is no
12913 reason to create a new node. */
12914 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12915 return expr;
12916 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12917 There is at least one place where we want to know that a
12918 particular expression is a throw-expression: when checking a ?:
12919 expression, there are special rules if the second or third
12920 argument is a throw-expression. */
12921 if (TREE_CODE (expr) == THROW_EXPR)
12922 return expr;
12924 if (TREE_CODE (expr) == COND_EXPR)
12925 return build3 (COND_EXPR,
12926 TREE_TYPE (expr),
12927 TREE_OPERAND (expr, 0),
12928 (TREE_OPERAND (expr, 1)
12929 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12930 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12931 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12932 if (TREE_CODE (expr) == COMPOUND_EXPR
12933 && !COMPOUND_EXPR_OVERLOADED (expr))
12934 return build2 (COMPOUND_EXPR,
12935 TREE_TYPE (expr),
12936 TREE_OPERAND (expr, 0),
12937 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12939 /* If the type is unknown, it can't really be non-dependent */
12940 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
12942 /* Otherwise, build a NON_DEPENDENT_EXPR.
12944 REFERENCE_TYPEs are not stripped for expressions in templates
12945 because doing so would play havoc with mangling. Consider, for
12946 example:
12948 template <typename T> void f<T& g>() { g(); }
12950 In the body of "f", the expression for "g" will have
12951 REFERENCE_TYPE, even though the standard says that it should
12952 not. The reason is that we must preserve the syntactic form of
12953 the expression so that mangling (say) "f<g>" inside the body of
12954 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12955 stripped here. */
12956 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12959 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12960 Return a new TREE_LIST with the various arguments replaced with
12961 equivalent non-dependent expressions. */
12963 tree
12964 build_non_dependent_args (tree args)
12966 tree a;
12967 tree new_args;
12969 new_args = NULL_TREE;
12970 for (a = args; a; a = TREE_CHAIN (a))
12971 new_args = tree_cons (NULL_TREE,
12972 build_non_dependent_expr (TREE_VALUE (a)),
12973 new_args);
12974 return nreverse (new_args);
12977 #include "gt-cp-pt.h"