* rtl.h (NOTE_EXPECTED_VALUE): Remove.
[official-gcc.git] / gcc / cp / pt.c
blobf9a728fa4b5e7bc299590913e8b9d359f798fb59
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"
48 #include "vecprim.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work.
57 The TREE_PURPOSE of each entry is either a DECL (for a function or
58 static data member), or a TYPE (for a class) indicating what we are
59 hoping to instantiate. The TREE_VALUE is not used. */
60 static GTY(()) tree pending_templates;
61 static GTY(()) tree last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
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 bool 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 int template_args_equal (tree, tree);
154 static void tsubst_default_arguments (tree);
155 static tree for_each_template_parm_r (tree *, int *, void *);
156 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
157 static void copy_default_args_to_explicit_spec (tree);
158 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
159 static int eq_local_specializations (const void *, const void *);
160 static bool dependent_type_p_r (tree);
161 static tree tsubst (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
163 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
165 /* Make the current scope suitable for access checking when we are
166 processing T. T can be FUNCTION_DECL for instantiated function
167 template, or VAR_DECL for static member variable (need by
168 instantiate_decl). */
170 static void
171 push_access_scope (tree t)
173 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
174 || TREE_CODE (t) == VAR_DECL);
176 if (DECL_FRIEND_CONTEXT (t))
177 push_nested_class (DECL_FRIEND_CONTEXT (t));
178 else if (DECL_CLASS_SCOPE_P (t))
179 push_nested_class (DECL_CONTEXT (t));
180 else
181 push_to_top_level ();
183 if (TREE_CODE (t) == FUNCTION_DECL)
185 saved_access_scope = tree_cons
186 (NULL_TREE, current_function_decl, saved_access_scope);
187 current_function_decl = t;
191 /* Restore the scope set up by push_access_scope. T is the node we
192 are processing. */
194 static void
195 pop_access_scope (tree t)
197 if (TREE_CODE (t) == FUNCTION_DECL)
199 current_function_decl = TREE_VALUE (saved_access_scope);
200 saved_access_scope = TREE_CHAIN (saved_access_scope);
203 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
204 pop_nested_class ();
205 else
206 pop_from_top_level ();
209 /* Do any processing required when DECL (a member template
210 declaration) is finished. Returns the TEMPLATE_DECL corresponding
211 to DECL, unless it is a specialization, in which case the DECL
212 itself is returned. */
214 tree
215 finish_member_template_decl (tree decl)
217 if (decl == error_mark_node)
218 return error_mark_node;
220 gcc_assert (DECL_P (decl));
222 if (TREE_CODE (decl) == TYPE_DECL)
224 tree type;
226 type = TREE_TYPE (decl);
227 if (IS_AGGR_TYPE (type)
228 && CLASSTYPE_TEMPLATE_INFO (type)
229 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
231 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
232 check_member_template (tmpl);
233 return tmpl;
235 return NULL_TREE;
237 else if (TREE_CODE (decl) == FIELD_DECL)
238 error ("data member %qD cannot be a member template", decl);
239 else if (DECL_TEMPLATE_INFO (decl))
241 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
243 check_member_template (DECL_TI_TEMPLATE (decl));
244 return DECL_TI_TEMPLATE (decl);
246 else
247 return decl;
249 else
250 error ("invalid member template declaration %qD", decl);
252 return error_mark_node;
255 /* Returns the template nesting level of the indicated class TYPE.
257 For example, in:
258 template <class T>
259 struct A
261 template <class U>
262 struct B {};
265 A<T>::B<U> has depth two, while A<T> has depth one.
266 Both A<T>::B<int> and A<int>::B<U> have depth one, if
267 they are instantiations, not specializations.
269 This function is guaranteed to return 0 if passed NULL_TREE so
270 that, for example, `template_class_depth (current_class_type)' is
271 always safe. */
274 template_class_depth (tree type)
276 int depth;
278 for (depth = 0;
279 type && TREE_CODE (type) != NAMESPACE_DECL;
280 type = (TREE_CODE (type) == FUNCTION_DECL)
281 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
283 if (TREE_CODE (type) != FUNCTION_DECL)
285 if (CLASSTYPE_TEMPLATE_INFO (type)
286 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
287 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
288 ++depth;
290 else
292 if (DECL_TEMPLATE_INFO (type)
293 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
294 && uses_template_parms (DECL_TI_ARGS (type)))
295 ++depth;
299 return depth;
302 /* Returns 1 if processing DECL as part of do_pending_inlines
303 needs us to push template parms. */
305 static int
306 inline_needs_template_parms (tree decl)
308 if (! DECL_TEMPLATE_INFO (decl))
309 return 0;
311 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
312 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
315 /* Subroutine of maybe_begin_member_template_processing.
316 Push the template parms in PARMS, starting from LEVELS steps into the
317 chain, and ending at the beginning, since template parms are listed
318 innermost first. */
320 static void
321 push_inline_template_parms_recursive (tree parmlist, int levels)
323 tree parms = TREE_VALUE (parmlist);
324 int i;
326 if (levels > 1)
327 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
329 ++processing_template_decl;
330 current_template_parms
331 = tree_cons (size_int (processing_template_decl),
332 parms, current_template_parms);
333 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
335 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
336 NULL);
337 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
339 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
341 if (parm == error_mark_node)
342 continue;
344 gcc_assert (DECL_P (parm));
346 switch (TREE_CODE (parm))
348 case TYPE_DECL:
349 case TEMPLATE_DECL:
350 pushdecl (parm);
351 break;
353 case PARM_DECL:
355 /* Make a CONST_DECL as is done in process_template_parm.
356 It is ugly that we recreate this here; the original
357 version built in process_template_parm is no longer
358 available. */
359 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
360 TREE_TYPE (parm));
361 DECL_ARTIFICIAL (decl) = 1;
362 TREE_CONSTANT (decl) = 1;
363 TREE_INVARIANT (decl) = 1;
364 TREE_READONLY (decl) = 1;
365 DECL_INITIAL (decl) = DECL_INITIAL (parm);
366 SET_DECL_TEMPLATE_PARM_P (decl);
367 pushdecl (decl);
369 break;
371 default:
372 gcc_unreachable ();
377 /* Restore the template parameter context for a member template or
378 a friend template defined in a class definition. */
380 void
381 maybe_begin_member_template_processing (tree decl)
383 tree parms;
384 int levels = 0;
386 if (inline_needs_template_parms (decl))
388 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
389 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
391 if (DECL_TEMPLATE_SPECIALIZATION (decl))
393 --levels;
394 parms = TREE_CHAIN (parms);
397 push_inline_template_parms_recursive (parms, levels);
400 /* Remember how many levels of template parameters we pushed so that
401 we can pop them later. */
402 VEC_safe_push (int, heap, inline_parm_levels, levels);
405 /* Undo the effects of maybe_begin_member_template_processing. */
407 void
408 maybe_end_member_template_processing (void)
410 int i;
411 int last;
413 if (VEC_length (int, inline_parm_levels) == 0)
414 return;
416 last = VEC_pop (int, inline_parm_levels);
417 for (i = 0; i < last; ++i)
419 --processing_template_decl;
420 current_template_parms = TREE_CHAIN (current_template_parms);
421 poplevel (0, 0, 0);
425 /* Return a new template argument vector which contains all of ARGS,
426 but has as its innermost set of arguments the EXTRA_ARGS. */
428 static tree
429 add_to_template_args (tree args, tree extra_args)
431 tree new_args;
432 int extra_depth;
433 int i;
434 int j;
436 extra_depth = TMPL_ARGS_DEPTH (extra_args);
437 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
439 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
440 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
442 for (j = 1; j <= extra_depth; ++j, ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
445 return new_args;
448 /* Like add_to_template_args, but only the outermost ARGS are added to
449 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
450 (EXTRA_ARGS) levels are added. This function is used to combine
451 the template arguments from a partial instantiation with the
452 template arguments used to attain the full instantiation from the
453 partial instantiation. */
455 static tree
456 add_outermost_template_args (tree args, tree extra_args)
458 tree new_args;
460 /* If there are more levels of EXTRA_ARGS than there are ARGS,
461 something very fishy is going on. */
462 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
464 /* If *all* the new arguments will be the EXTRA_ARGS, just return
465 them. */
466 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
467 return extra_args;
469 /* For the moment, we make ARGS look like it contains fewer levels. */
470 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
472 new_args = add_to_template_args (args, extra_args);
474 /* Now, we restore ARGS to its full dimensions. */
475 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
477 return new_args;
480 /* Return the N levels of innermost template arguments from the ARGS. */
482 tree
483 get_innermost_template_args (tree args, int n)
485 tree new_args;
486 int extra_levels;
487 int i;
489 gcc_assert (n >= 0);
491 /* If N is 1, just return the innermost set of template arguments. */
492 if (n == 1)
493 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
495 /* If we're not removing anything, just return the arguments we were
496 given. */
497 extra_levels = TMPL_ARGS_DEPTH (args) - n;
498 gcc_assert (extra_levels >= 0);
499 if (extra_levels == 0)
500 return args;
502 /* Make a new set of arguments, not containing the outer arguments. */
503 new_args = make_tree_vec (n);
504 for (i = 1; i <= n; ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i,
506 TMPL_ARGS_LEVEL (args, i + extra_levels));
508 return new_args;
511 /* We've got a template header coming up; push to a new level for storing
512 the parms. */
514 void
515 begin_template_parm_list (void)
517 /* We use a non-tag-transparent scope here, which causes pushtag to
518 put tags in this scope, rather than in the enclosing class or
519 namespace scope. This is the right thing, since we want
520 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
521 global template class, push_template_decl handles putting the
522 TEMPLATE_DECL into top-level scope. For a nested template class,
523 e.g.:
525 template <class T> struct S1 {
526 template <class T> struct S2 {};
529 pushtag contains special code to call pushdecl_with_scope on the
530 TEMPLATE_DECL for S2. */
531 begin_scope (sk_template_parms, NULL);
532 ++processing_template_decl;
533 ++processing_template_parmlist;
534 note_template_header (0);
537 /* This routine is called when a specialization is declared. If it is
538 invalid to declare a specialization here, an error is reported and
539 false is returned, otherwise this routine will return true. */
541 static bool
542 check_specialization_scope (void)
544 tree scope = current_scope ();
546 /* [temp.expl.spec]
548 An explicit specialization shall be declared in the namespace of
549 which the template is a member, or, for member templates, in the
550 namespace of which the enclosing class or enclosing class
551 template is a member. An explicit specialization of a member
552 function, member class or static data member of a class template
553 shall be declared in the namespace of which the class template
554 is a member. */
555 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
557 error ("explicit specialization in non-namespace scope %qD", scope);
558 return false;
561 /* [temp.expl.spec]
563 In an explicit specialization declaration for a member of a class
564 template or a member template that appears in namespace scope,
565 the member template and some of its enclosing class templates may
566 remain unspecialized, except that the declaration shall not
567 explicitly specialize a class member template if its enclosing
568 class templates are not explicitly specialized as well. */
569 if (current_template_parms)
571 error ("enclosing class templates are not explicitly specialized");
572 return false;
575 return true;
578 /* We've just seen template <>. */
580 bool
581 begin_specialization (void)
583 begin_scope (sk_template_spec, NULL);
584 note_template_header (1);
585 return check_specialization_scope ();
588 /* Called at then end of processing a declaration preceded by
589 template<>. */
591 void
592 end_specialization (void)
594 finish_scope ();
595 reset_specialization ();
598 /* Any template <>'s that we have seen thus far are not referring to a
599 function specialization. */
601 void
602 reset_specialization (void)
604 processing_specialization = 0;
605 template_header_count = 0;
608 /* We've just seen a template header. If SPECIALIZATION is nonzero,
609 it was of the form template <>. */
611 static void
612 note_template_header (int specialization)
614 processing_specialization = specialization;
615 template_header_count++;
618 /* We're beginning an explicit instantiation. */
620 void
621 begin_explicit_instantiation (void)
623 gcc_assert (!processing_explicit_instantiation);
624 processing_explicit_instantiation = true;
628 void
629 end_explicit_instantiation (void)
631 gcc_assert (processing_explicit_instantiation);
632 processing_explicit_instantiation = false;
635 /* An explicit specialization or partial specialization TMPL is being
636 declared. Check that the namespace in which the specialization is
637 occurring is permissible. Returns false iff it is invalid to
638 specialize TMPL in the current namespace. */
640 static bool
641 check_specialization_namespace (tree tmpl)
643 tree tpl_ns = decl_namespace_context (tmpl);
645 /* [tmpl.expl.spec]
647 An explicit specialization shall be declared in the namespace of
648 which the template is a member, or, for member templates, in the
649 namespace of which the enclosing class or enclosing class
650 template is a member. An explicit specialization of a member
651 function, member class or static data member of a class template
652 shall be declared in the namespace of which the class template is
653 a member. */
654 if (is_associated_namespace (current_namespace, tpl_ns))
655 /* Same or super-using namespace. */
656 return true;
657 else
659 pedwarn ("specialization of %qD in different namespace", tmpl);
660 pedwarn (" from definition of %q+#D", tmpl);
661 return false;
665 /* SPEC is an explicit instantiation. Check that it is valid to
666 perform this explicit instantiation in the current namespace. */
668 static void
669 check_explicit_instantiation_namespace (tree spec)
671 tree ns;
673 /* DR 275: An explicit instantiation shall appear in an enclosing
674 namespace of its template. */
675 ns = decl_namespace_context (spec);
676 if (!is_ancestor (current_namespace, ns))
677 pedwarn ("explicit instantiation of %qD in namespace %qD "
678 "(which does not enclose namespace %qD)",
679 spec, current_namespace, ns);
682 /* The TYPE is being declared. If it is a template type, that means it
683 is a partial specialization. Do appropriate error-checking. */
685 tree
686 maybe_process_partial_specialization (tree type)
688 tree context;
690 if (type == error_mark_node)
691 return error_mark_node;
693 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
695 error ("name of class shadows template template parameter %qD",
696 TYPE_NAME (type));
697 return error_mark_node;
700 context = TYPE_CONTEXT (type);
702 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
704 /* This is for ordinary explicit specialization and partial
705 specialization of a template class such as:
707 template <> class C<int>;
711 template <class T> class C<T*>;
713 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
716 && !COMPLETE_TYPE_P (type))
718 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
719 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
720 if (processing_template_decl)
721 push_template_decl (TYPE_MAIN_DECL (type));
723 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
724 error ("specialization of %qT after instantiation", type);
726 else if (CLASS_TYPE_P (type)
727 && !CLASSTYPE_USE_TEMPLATE (type)
728 && CLASSTYPE_TEMPLATE_INFO (type)
729 && context && CLASS_TYPE_P (context)
730 && CLASSTYPE_TEMPLATE_INFO (context))
732 /* This is for an explicit specialization of member class
733 template according to [temp.expl.spec/18]:
735 template <> template <class U> class C<int>::D;
737 The context `C<int>' must be an implicit instantiation.
738 Otherwise this is just a member class template declared
739 earlier like:
741 template <> class C<int> { template <class U> class D; };
742 template <> template <class U> class C<int>::D;
744 In the first case, `C<int>::D' is a specialization of `C<T>::D'
745 while in the second case, `C<int>::D' is a primary template
746 and `C<T>::D' may not exist. */
748 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
749 && !COMPLETE_TYPE_P (type))
751 tree t;
753 if (current_namespace
754 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
756 pedwarn ("specializing %q#T in different namespace", type);
757 pedwarn (" from definition of %q+#D",
758 CLASSTYPE_TI_TEMPLATE (type));
761 /* Check for invalid specialization after instantiation:
763 template <> template <> class C<int>::D<int>;
764 template <> template <class U> class C<int>::D; */
766 for (t = DECL_TEMPLATE_INSTANTIATIONS
767 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
768 t; t = TREE_CHAIN (t))
769 if (TREE_VALUE (t) != type
770 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
771 error ("specialization %qT after instantiation %qT",
772 type, TREE_VALUE (t));
774 /* Mark TYPE as a specialization. And as a result, we only
775 have one level of template argument for the innermost
776 class template. */
777 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
778 CLASSTYPE_TI_ARGS (type)
779 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
782 else if (processing_specialization)
784 error ("explicit specialization of non-template %qT", type);
785 return error_mark_node;
788 return type;
791 /* Returns nonzero if we can optimize the retrieval of specializations
792 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
793 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
795 static inline bool
796 optimize_specialization_lookup_p (tree tmpl)
798 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
799 && DECL_CLASS_SCOPE_P (tmpl)
800 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
801 parameter. */
802 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
803 /* The optimized lookup depends on the fact that the
804 template arguments for the member function template apply
805 purely to the containing class, which is not true if the
806 containing class is an explicit or partial
807 specialization. */
808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
809 && !DECL_MEMBER_TEMPLATE_P (tmpl)
810 && !DECL_CONV_FN_P (tmpl)
811 /* It is possible to have a template that is not a member
812 template and is not a member of a template class:
814 template <typename T>
815 struct S { friend A::f(); };
817 Here, the friend function is a template, but the context does
818 not have template information. The optimized lookup relies
819 on having ARGS be the template arguments for both the class
820 and the function template. */
821 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
824 /* Retrieve the specialization (in the sense of [temp.spec] - a
825 specialization is either an instantiation or an explicit
826 specialization) of TMPL for the given template ARGS. If there is
827 no such specialization, return NULL_TREE. The ARGS are a vector of
828 arguments, or a vector of vectors of arguments, in the case of
829 templates with more than one level of parameters.
831 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
832 then we search for a partial specialization matching ARGS. This
833 parameter is ignored if TMPL is not a class template. */
835 static tree
836 retrieve_specialization (tree tmpl, tree args,
837 bool class_specializations_p)
839 if (args == error_mark_node)
840 return NULL_TREE;
842 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
844 /* There should be as many levels of arguments as there are
845 levels of parameters. */
846 gcc_assert (TMPL_ARGS_DEPTH (args)
847 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
849 if (optimize_specialization_lookup_p (tmpl))
851 tree class_template;
852 tree class_specialization;
853 VEC(tree,gc) *methods;
854 tree fns;
855 int idx;
857 /* The template arguments actually apply to the containing
858 class. Find the class specialization with those
859 arguments. */
860 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
861 class_specialization
862 = retrieve_specialization (class_template, args,
863 /*class_specializations_p=*/false);
864 if (!class_specialization)
865 return NULL_TREE;
866 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
867 for the specialization. */
868 idx = class_method_index_for_fn (class_specialization, tmpl);
869 if (idx == -1)
870 return NULL_TREE;
871 /* Iterate through the methods with the indicated name, looking
872 for the one that has an instance of TMPL. */
873 methods = CLASSTYPE_METHOD_VEC (class_specialization);
874 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
876 tree fn = OVL_CURRENT (fns);
877 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
878 return fn;
880 return NULL_TREE;
882 else
884 tree *sp;
885 tree *head;
887 /* Class templates store their instantiations on the
888 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
889 DECL_TEMPLATE_SPECIALIZATIONS list. */
890 if (!class_specializations_p
891 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
892 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
893 else
894 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
895 head = sp;
896 /* Iterate through the list until we find a matching template. */
897 while (*sp != NULL_TREE)
899 tree spec = *sp;
901 if (comp_template_args (TREE_PURPOSE (spec), args))
903 /* Use the move-to-front heuristic to speed up future
904 searches. */
905 if (spec != *head)
907 *sp = TREE_CHAIN (*sp);
908 TREE_CHAIN (spec) = *head;
909 *head = spec;
911 return TREE_VALUE (spec);
913 sp = &TREE_CHAIN (spec);
917 return NULL_TREE;
920 /* Like retrieve_specialization, but for local declarations. */
922 static tree
923 retrieve_local_specialization (tree tmpl)
925 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
926 htab_hash_pointer (tmpl));
927 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
930 /* Returns nonzero iff DECL is a specialization of TMPL. */
933 is_specialization_of (tree decl, tree tmpl)
935 tree t;
937 if (TREE_CODE (decl) == FUNCTION_DECL)
939 for (t = decl;
940 t != NULL_TREE;
941 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
942 if (t == tmpl)
943 return 1;
945 else
947 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
949 for (t = TREE_TYPE (decl);
950 t != NULL_TREE;
951 t = CLASSTYPE_USE_TEMPLATE (t)
952 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
953 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
954 return 1;
957 return 0;
960 /* Returns nonzero iff DECL is a specialization of friend declaration
961 FRIEND according to [temp.friend]. */
963 bool
964 is_specialization_of_friend (tree decl, tree friend)
966 bool need_template = true;
967 int template_depth;
969 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
970 || TREE_CODE (decl) == TYPE_DECL);
972 /* For [temp.friend/6] when FRIEND is an ordinary member function
973 of a template class, we want to check if DECL is a specialization
974 if this. */
975 if (TREE_CODE (friend) == FUNCTION_DECL
976 && DECL_TEMPLATE_INFO (friend)
977 && !DECL_USE_TEMPLATE (friend))
979 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
980 friend = DECL_TI_TEMPLATE (friend);
981 need_template = false;
983 else if (TREE_CODE (friend) == TEMPLATE_DECL
984 && !PRIMARY_TEMPLATE_P (friend))
985 need_template = false;
987 /* There is nothing to do if this is not a template friend. */
988 if (TREE_CODE (friend) != TEMPLATE_DECL)
989 return false;
991 if (is_specialization_of (decl, friend))
992 return true;
994 /* [temp.friend/6]
995 A member of a class template may be declared to be a friend of a
996 non-template class. In this case, the corresponding member of
997 every specialization of the class template is a friend of the
998 class granting friendship.
1000 For example, given a template friend declaration
1002 template <class T> friend void A<T>::f();
1004 the member function below is considered a friend
1006 template <> struct A<int> {
1007 void f();
1010 For this type of template friend, TEMPLATE_DEPTH below will be
1011 nonzero. To determine if DECL is a friend of FRIEND, we first
1012 check if the enclosing class is a specialization of another. */
1014 template_depth = template_class_depth (DECL_CONTEXT (friend));
1015 if (template_depth
1016 && DECL_CLASS_SCOPE_P (decl)
1017 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1018 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1020 /* Next, we check the members themselves. In order to handle
1021 a few tricky cases, such as when FRIEND's are
1023 template <class T> friend void A<T>::g(T t);
1024 template <class T> template <T t> friend void A<T>::h();
1026 and DECL's are
1028 void A<int>::g(int);
1029 template <int> void A<int>::h();
1031 we need to figure out ARGS, the template arguments from
1032 the context of DECL. This is required for template substitution
1033 of `T' in the function parameter of `g' and template parameter
1034 of `h' in the above examples. Here ARGS corresponds to `int'. */
1036 tree context = DECL_CONTEXT (decl);
1037 tree args = NULL_TREE;
1038 int current_depth = 0;
1040 while (current_depth < template_depth)
1042 if (CLASSTYPE_TEMPLATE_INFO (context))
1044 if (current_depth == 0)
1045 args = TYPE_TI_ARGS (context);
1046 else
1047 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1048 current_depth++;
1050 context = TYPE_CONTEXT (context);
1053 if (TREE_CODE (decl) == FUNCTION_DECL)
1055 bool is_template;
1056 tree friend_type;
1057 tree decl_type;
1058 tree friend_args_type;
1059 tree decl_args_type;
1061 /* Make sure that both DECL and FRIEND are templates or
1062 non-templates. */
1063 is_template = DECL_TEMPLATE_INFO (decl)
1064 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1065 if (need_template ^ is_template)
1066 return false;
1067 else if (is_template)
1069 /* If both are templates, check template parameter list. */
1070 tree friend_parms
1071 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1072 args, tf_none);
1073 if (!comp_template_parms
1074 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1075 friend_parms))
1076 return false;
1078 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1080 else
1081 decl_type = TREE_TYPE (decl);
1083 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1084 tf_none, NULL_TREE);
1085 if (friend_type == error_mark_node)
1086 return false;
1088 /* Check if return types match. */
1089 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1090 return false;
1092 /* Check if function parameter types match, ignoring the
1093 `this' parameter. */
1094 friend_args_type = TYPE_ARG_TYPES (friend_type);
1095 decl_args_type = TYPE_ARG_TYPES (decl_type);
1096 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1097 friend_args_type = TREE_CHAIN (friend_args_type);
1098 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1099 decl_args_type = TREE_CHAIN (decl_args_type);
1101 return compparms (decl_args_type, friend_args_type);
1103 else
1105 /* DECL is a TYPE_DECL */
1106 bool is_template;
1107 tree decl_type = TREE_TYPE (decl);
1109 /* Make sure that both DECL and FRIEND are templates or
1110 non-templates. */
1111 is_template
1112 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1115 if (need_template ^ is_template)
1116 return false;
1117 else if (is_template)
1119 tree friend_parms;
1120 /* If both are templates, check the name of the two
1121 TEMPLATE_DECL's first because is_friend didn't. */
1122 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1123 != DECL_NAME (friend))
1124 return false;
1126 /* Now check template parameter list. */
1127 friend_parms
1128 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1129 args, tf_none);
1130 return comp_template_parms
1131 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1132 friend_parms);
1134 else
1135 return (DECL_NAME (decl)
1136 == DECL_NAME (friend));
1139 return false;
1142 /* Register the specialization SPEC as a specialization of TMPL with
1143 the indicated ARGS. IS_FRIEND indicates whether the specialization
1144 is actually just a friend declaration. Returns SPEC, or an
1145 equivalent prior declaration, if available. */
1147 static tree
1148 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1150 tree fn;
1152 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1154 if (TREE_CODE (spec) == FUNCTION_DECL
1155 && uses_template_parms (DECL_TI_ARGS (spec)))
1156 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1157 register it; we want the corresponding TEMPLATE_DECL instead.
1158 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1159 the more obvious `uses_template_parms (spec)' to avoid problems
1160 with default function arguments. In particular, given
1161 something like this:
1163 template <class T> void f(T t1, T t = T())
1165 the default argument expression is not substituted for in an
1166 instantiation unless and until it is actually needed. */
1167 return spec;
1169 fn = retrieve_specialization (tmpl, args,
1170 /*class_specializations_p=*/false);
1171 /* We can sometimes try to re-register a specialization that we've
1172 already got. In particular, regenerate_decl_from_template calls
1173 duplicate_decls which will update the specialization list. But,
1174 we'll still get called again here anyhow. It's more convenient
1175 to simply allow this than to try to prevent it. */
1176 if (fn == spec)
1177 return spec;
1178 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1180 if (DECL_TEMPLATE_INSTANTIATION (fn))
1182 if (TREE_USED (fn)
1183 || DECL_EXPLICIT_INSTANTIATION (fn))
1185 error ("specialization of %qD after instantiation",
1186 fn);
1187 return error_mark_node;
1189 else
1191 tree clone;
1192 /* This situation should occur only if the first
1193 specialization is an implicit instantiation, the
1194 second is an explicit specialization, and the
1195 implicit instantiation has not yet been used. That
1196 situation can occur if we have implicitly
1197 instantiated a member function and then specialized
1198 it later.
1200 We can also wind up here if a friend declaration that
1201 looked like an instantiation turns out to be a
1202 specialization:
1204 template <class T> void foo(T);
1205 class S { friend void foo<>(int) };
1206 template <> void foo(int);
1208 We transform the existing DECL in place so that any
1209 pointers to it become pointers to the updated
1210 declaration.
1212 If there was a definition for the template, but not
1213 for the specialization, we want this to look as if
1214 there were no definition, and vice versa. */
1215 DECL_INITIAL (fn) = NULL_TREE;
1216 duplicate_decls (spec, fn, is_friend);
1217 /* The call to duplicate_decls will have applied
1218 [temp.expl.spec]:
1220 An explicit specialization of a function template
1221 is inline only if it is explicitly declared to be,
1222 and independently of whether its function template
1225 to the primary function; now copy the inline bits to
1226 the various clones. */
1227 FOR_EACH_CLONE (clone, fn)
1229 DECL_DECLARED_INLINE_P (clone)
1230 = DECL_DECLARED_INLINE_P (fn);
1231 DECL_INLINE (clone)
1232 = DECL_INLINE (fn);
1234 check_specialization_namespace (fn);
1236 return fn;
1239 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1241 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1242 /* Dup decl failed, but this is a new definition. Set the
1243 line number so any errors match this new
1244 definition. */
1245 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1247 return fn;
1251 /* A specialization must be declared in the same namespace as the
1252 template it is specializing. */
1253 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1254 && !check_specialization_namespace (tmpl))
1255 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1257 if (!optimize_specialization_lookup_p (tmpl))
1258 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1259 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1261 return spec;
1264 /* Unregister the specialization SPEC as a specialization of TMPL.
1265 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1266 if the SPEC was listed as a specialization of TMPL. */
1268 bool
1269 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1271 tree* s;
1273 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1274 *s != NULL_TREE;
1275 s = &TREE_CHAIN (*s))
1276 if (TREE_VALUE (*s) == spec)
1278 if (!new_spec)
1279 *s = TREE_CHAIN (*s);
1280 else
1281 TREE_VALUE (*s) = new_spec;
1282 return 1;
1285 return 0;
1288 /* Compare an entry in the local specializations hash table P1 (which
1289 is really a pointer to a TREE_LIST) with P2 (which is really a
1290 DECL). */
1292 static int
1293 eq_local_specializations (const void *p1, const void *p2)
1295 return TREE_VALUE ((tree) p1) == (tree) p2;
1298 /* Hash P1, an entry in the local specializations table. */
1300 static hashval_t
1301 hash_local_specialization (const void* p1)
1303 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1306 /* Like register_specialization, but for local declarations. We are
1307 registering SPEC, an instantiation of TMPL. */
1309 static void
1310 register_local_specialization (tree spec, tree tmpl)
1312 void **slot;
1314 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1315 htab_hash_pointer (tmpl), INSERT);
1316 *slot = build_tree_list (spec, tmpl);
1319 /* TYPE is a class type. Returns true if TYPE is an explicitly
1320 specialized class. */
1322 bool
1323 explicit_class_specialization_p (tree type)
1325 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1326 return false;
1327 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1330 /* Print the list of candidate FNS in an error message. */
1332 void
1333 print_candidates (tree fns)
1335 tree fn;
1337 const char *str = "candidates are:";
1339 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1341 tree f;
1343 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1344 error ("%s %+#D", str, OVL_CURRENT (f));
1345 str = " ";
1349 /* Returns the template (one of the functions given by TEMPLATE_ID)
1350 which can be specialized to match the indicated DECL with the
1351 explicit template args given in TEMPLATE_ID. The DECL may be
1352 NULL_TREE if none is available. In that case, the functions in
1353 TEMPLATE_ID are non-members.
1355 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1356 specialization of a member template.
1358 The TEMPLATE_COUNT is the number of references to qualifying
1359 template classes that appeared in the name of the function. See
1360 check_explicit_specialization for a more accurate description.
1362 TSK indicates what kind of template declaration (if any) is being
1363 declared. TSK_TEMPLATE indicates that the declaration given by
1364 DECL, though a FUNCTION_DECL, has template parameters, and is
1365 therefore a template function.
1367 The template args (those explicitly specified and those deduced)
1368 are output in a newly created vector *TARGS_OUT.
1370 If it is impossible to determine the result, an error message is
1371 issued. The error_mark_node is returned to indicate failure. */
1373 static tree
1374 determine_specialization (tree template_id,
1375 tree decl,
1376 tree* targs_out,
1377 int need_member_template,
1378 int template_count,
1379 tmpl_spec_kind tsk)
1381 tree fns;
1382 tree targs;
1383 tree explicit_targs;
1384 tree candidates = NULL_TREE;
1385 /* A TREE_LIST of templates of which DECL may be a specialization.
1386 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1387 corresponding TREE_PURPOSE is the set of template arguments that,
1388 when used to instantiate the template, would produce a function
1389 with the signature of DECL. */
1390 tree templates = NULL_TREE;
1391 int header_count;
1392 struct cp_binding_level *b;
1394 *targs_out = NULL_TREE;
1396 if (template_id == error_mark_node || decl == error_mark_node)
1397 return error_mark_node;
1399 fns = TREE_OPERAND (template_id, 0);
1400 explicit_targs = TREE_OPERAND (template_id, 1);
1402 if (fns == error_mark_node)
1403 return error_mark_node;
1405 /* Check for baselinks. */
1406 if (BASELINK_P (fns))
1407 fns = BASELINK_FUNCTIONS (fns);
1409 if (!is_overloaded_fn (fns))
1411 error ("%qD is not a function template", fns);
1412 return error_mark_node;
1415 /* Count the number of template headers specified for this
1416 specialization. */
1417 header_count = 0;
1418 for (b = current_binding_level;
1419 b->kind == sk_template_parms;
1420 b = b->level_chain)
1421 ++header_count;
1423 for (; fns; fns = OVL_NEXT (fns))
1425 tree fn = OVL_CURRENT (fns);
1427 if (TREE_CODE (fn) == TEMPLATE_DECL)
1429 tree decl_arg_types;
1430 tree fn_arg_types;
1432 /* DECL might be a specialization of FN. */
1434 /* Adjust the type of DECL in case FN is a static member. */
1435 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1436 if (DECL_STATIC_FUNCTION_P (fn)
1437 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1438 decl_arg_types = TREE_CHAIN (decl_arg_types);
1440 /* Check that the number of function parameters matches.
1441 For example,
1442 template <class T> void f(int i = 0);
1443 template <> void f<int>();
1444 The specialization f<int> is invalid but is not caught
1445 by get_bindings below. */
1447 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1448 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1449 continue;
1451 /* For a non-static member function, we need to make sure that
1452 the const qualification is the same. This can be done by
1453 checking the 'this' in the argument list. */
1454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1455 && !same_type_p (TREE_VALUE (fn_arg_types),
1456 TREE_VALUE (decl_arg_types)))
1457 continue;
1459 /* In case of explicit specialization, we need to check if
1460 the number of template headers appearing in the specialization
1461 is correct. This is usually done in check_explicit_specialization,
1462 but the check done there cannot be exhaustive when specializing
1463 member functions. Consider the following code:
1465 template <> void A<int>::f(int);
1466 template <> template <> void A<int>::f(int);
1468 Assuming that A<int> is not itself an explicit specialization
1469 already, the first line specializes "f" which is a non-template
1470 member function, whilst the second line specializes "f" which
1471 is a template member function. So both lines are syntactically
1472 correct, and check_explicit_specialization does not reject
1473 them.
1475 Here, we can do better, as we are matching the specialization
1476 against the declarations. We count the number of template
1477 headers, and we check if they match TEMPLATE_COUNT + 1
1478 (TEMPLATE_COUNT is the number of qualifying template classes,
1479 plus there must be another header for the member template
1480 itself).
1482 Notice that if header_count is zero, this is not a
1483 specialization but rather a template instantiation, so there
1484 is no check we can perform here. */
1485 if (header_count && header_count != template_count + 1)
1486 continue;
1488 /* Check that the number of template arguments at the
1489 innermost level for DECL is the same as for FN. */
1490 if (current_binding_level->kind == sk_template_parms
1491 && !current_binding_level->explicit_spec_p
1492 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1493 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1494 (current_template_parms))))
1495 continue;
1497 /* Function templates cannot be specializations; there are
1498 no partial specializations of functions. Therefore, if
1499 the type of DECL does not match FN, there is no
1500 match. */
1501 if (tsk == tsk_template)
1503 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1504 decl_arg_types))
1505 candidates = tree_cons (NULL_TREE, fn, candidates);
1506 continue;
1509 /* See whether this function might be a specialization of this
1510 template. */
1511 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1513 if (!targs)
1514 /* We cannot deduce template arguments that when used to
1515 specialize TMPL will produce DECL. */
1516 continue;
1518 /* Save this template, and the arguments deduced. */
1519 templates = tree_cons (targs, fn, templates);
1521 else if (need_member_template)
1522 /* FN is an ordinary member function, and we need a
1523 specialization of a member template. */
1525 else if (TREE_CODE (fn) != FUNCTION_DECL)
1526 /* We can get IDENTIFIER_NODEs here in certain erroneous
1527 cases. */
1529 else if (!DECL_FUNCTION_MEMBER_P (fn))
1530 /* This is just an ordinary non-member function. Nothing can
1531 be a specialization of that. */
1533 else if (DECL_ARTIFICIAL (fn))
1534 /* Cannot specialize functions that are created implicitly. */
1536 else
1538 tree decl_arg_types;
1540 /* This is an ordinary member function. However, since
1541 we're here, we can assume it's enclosing class is a
1542 template class. For example,
1544 template <typename T> struct S { void f(); };
1545 template <> void S<int>::f() {}
1547 Here, S<int>::f is a non-template, but S<int> is a
1548 template class. If FN has the same type as DECL, we
1549 might be in business. */
1551 if (!DECL_TEMPLATE_INFO (fn))
1552 /* Its enclosing class is an explicit specialization
1553 of a template class. This is not a candidate. */
1554 continue;
1556 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1557 TREE_TYPE (TREE_TYPE (fn))))
1558 /* The return types differ. */
1559 continue;
1561 /* Adjust the type of DECL in case FN is a static member. */
1562 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1563 if (DECL_STATIC_FUNCTION_P (fn)
1564 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1565 decl_arg_types = TREE_CHAIN (decl_arg_types);
1567 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1568 decl_arg_types))
1569 /* They match! */
1570 candidates = tree_cons (NULL_TREE, fn, candidates);
1574 if (templates && TREE_CHAIN (templates))
1576 /* We have:
1578 [temp.expl.spec]
1580 It is possible for a specialization with a given function
1581 signature to be instantiated from more than one function
1582 template. In such cases, explicit specification of the
1583 template arguments must be used to uniquely identify the
1584 function template specialization being specialized.
1586 Note that here, there's no suggestion that we're supposed to
1587 determine which of the candidate templates is most
1588 specialized. However, we, also have:
1590 [temp.func.order]
1592 Partial ordering of overloaded function template
1593 declarations is used in the following contexts to select
1594 the function template to which a function template
1595 specialization refers:
1597 -- when an explicit specialization refers to a function
1598 template.
1600 So, we do use the partial ordering rules, at least for now.
1601 This extension can only serve to make invalid programs valid,
1602 so it's safe. And, there is strong anecdotal evidence that
1603 the committee intended the partial ordering rules to apply;
1604 the EDG front-end has that behavior, and John Spicer claims
1605 that the committee simply forgot to delete the wording in
1606 [temp.expl.spec]. */
1607 tree tmpl = most_specialized_instantiation (templates);
1608 if (tmpl != error_mark_node)
1610 templates = tmpl;
1611 TREE_CHAIN (templates) = NULL_TREE;
1615 if (templates == NULL_TREE && candidates == NULL_TREE)
1617 error ("template-id %qD for %q+D does not match any template "
1618 "declaration", template_id, decl);
1619 return error_mark_node;
1621 else if ((templates && TREE_CHAIN (templates))
1622 || (candidates && TREE_CHAIN (candidates))
1623 || (templates && candidates))
1625 error ("ambiguous template specialization %qD for %q+D",
1626 template_id, decl);
1627 chainon (candidates, templates);
1628 print_candidates (candidates);
1629 return error_mark_node;
1632 /* We have one, and exactly one, match. */
1633 if (candidates)
1635 tree fn = TREE_VALUE (candidates);
1636 /* DECL is a re-declaration of a template function. */
1637 if (TREE_CODE (fn) == TEMPLATE_DECL)
1638 return fn;
1639 /* It was a specialization of an ordinary member function in a
1640 template class. */
1641 *targs_out = copy_node (DECL_TI_ARGS (fn));
1642 return DECL_TI_TEMPLATE (fn);
1645 /* It was a specialization of a template. */
1646 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1647 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1649 *targs_out = copy_node (targs);
1650 SET_TMPL_ARGS_LEVEL (*targs_out,
1651 TMPL_ARGS_DEPTH (*targs_out),
1652 TREE_PURPOSE (templates));
1654 else
1655 *targs_out = TREE_PURPOSE (templates);
1656 return TREE_VALUE (templates);
1659 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1660 but with the default argument values filled in from those in the
1661 TMPL_TYPES. */
1663 static tree
1664 copy_default_args_to_explicit_spec_1 (tree spec_types,
1665 tree tmpl_types)
1667 tree new_spec_types;
1669 if (!spec_types)
1670 return NULL_TREE;
1672 if (spec_types == void_list_node)
1673 return void_list_node;
1675 /* Substitute into the rest of the list. */
1676 new_spec_types =
1677 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1678 TREE_CHAIN (tmpl_types));
1680 /* Add the default argument for this parameter. */
1681 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1682 TREE_VALUE (spec_types),
1683 new_spec_types);
1686 /* DECL is an explicit specialization. Replicate default arguments
1687 from the template it specializes. (That way, code like:
1689 template <class T> void f(T = 3);
1690 template <> void f(double);
1691 void g () { f (); }
1693 works, as required.) An alternative approach would be to look up
1694 the correct default arguments at the call-site, but this approach
1695 is consistent with how implicit instantiations are handled. */
1697 static void
1698 copy_default_args_to_explicit_spec (tree decl)
1700 tree tmpl;
1701 tree spec_types;
1702 tree tmpl_types;
1703 tree new_spec_types;
1704 tree old_type;
1705 tree new_type;
1706 tree t;
1707 tree object_type = NULL_TREE;
1708 tree in_charge = NULL_TREE;
1709 tree vtt = NULL_TREE;
1711 /* See if there's anything we need to do. */
1712 tmpl = DECL_TI_TEMPLATE (decl);
1713 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1714 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1715 if (TREE_PURPOSE (t))
1716 break;
1717 if (!t)
1718 return;
1720 old_type = TREE_TYPE (decl);
1721 spec_types = TYPE_ARG_TYPES (old_type);
1723 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1725 /* Remove the this pointer, but remember the object's type for
1726 CV quals. */
1727 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1728 spec_types = TREE_CHAIN (spec_types);
1729 tmpl_types = TREE_CHAIN (tmpl_types);
1731 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1733 /* DECL may contain more parameters than TMPL due to the extra
1734 in-charge parameter in constructors and destructors. */
1735 in_charge = spec_types;
1736 spec_types = TREE_CHAIN (spec_types);
1738 if (DECL_HAS_VTT_PARM_P (decl))
1740 vtt = spec_types;
1741 spec_types = TREE_CHAIN (spec_types);
1745 /* Compute the merged default arguments. */
1746 new_spec_types =
1747 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1749 /* Compute the new FUNCTION_TYPE. */
1750 if (object_type)
1752 if (vtt)
1753 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1754 TREE_VALUE (vtt),
1755 new_spec_types);
1757 if (in_charge)
1758 /* Put the in-charge parameter back. */
1759 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1760 TREE_VALUE (in_charge),
1761 new_spec_types);
1763 new_type = build_method_type_directly (object_type,
1764 TREE_TYPE (old_type),
1765 new_spec_types);
1767 else
1768 new_type = build_function_type (TREE_TYPE (old_type),
1769 new_spec_types);
1770 new_type = cp_build_type_attribute_variant (new_type,
1771 TYPE_ATTRIBUTES (old_type));
1772 new_type = build_exception_variant (new_type,
1773 TYPE_RAISES_EXCEPTIONS (old_type));
1774 TREE_TYPE (decl) = new_type;
1777 /* Check to see if the function just declared, as indicated in
1778 DECLARATOR, and in DECL, is a specialization of a function
1779 template. We may also discover that the declaration is an explicit
1780 instantiation at this point.
1782 Returns DECL, or an equivalent declaration that should be used
1783 instead if all goes well. Issues an error message if something is
1784 amiss. Returns error_mark_node if the error is not easily
1785 recoverable.
1787 FLAGS is a bitmask consisting of the following flags:
1789 2: The function has a definition.
1790 4: The function is a friend.
1792 The TEMPLATE_COUNT is the number of references to qualifying
1793 template classes that appeared in the name of the function. For
1794 example, in
1796 template <class T> struct S { void f(); };
1797 void S<int>::f();
1799 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1800 classes are not counted in the TEMPLATE_COUNT, so that in
1802 template <class T> struct S {};
1803 template <> struct S<int> { void f(); }
1804 template <> void S<int>::f();
1806 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1807 invalid; there should be no template <>.)
1809 If the function is a specialization, it is marked as such via
1810 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1811 is set up correctly, and it is added to the list of specializations
1812 for that template. */
1814 tree
1815 check_explicit_specialization (tree declarator,
1816 tree decl,
1817 int template_count,
1818 int flags)
1820 int have_def = flags & 2;
1821 int is_friend = flags & 4;
1822 int specialization = 0;
1823 int explicit_instantiation = 0;
1824 int member_specialization = 0;
1825 tree ctype = DECL_CLASS_CONTEXT (decl);
1826 tree dname = DECL_NAME (decl);
1827 tmpl_spec_kind tsk;
1829 if (is_friend)
1831 if (!processing_specialization)
1832 tsk = tsk_none;
1833 else
1834 tsk = tsk_excessive_parms;
1836 else
1837 tsk = current_tmpl_spec_kind (template_count);
1839 switch (tsk)
1841 case tsk_none:
1842 if (processing_specialization)
1844 specialization = 1;
1845 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1847 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1849 if (is_friend)
1850 /* This could be something like:
1852 template <class T> void f(T);
1853 class S { friend void f<>(int); } */
1854 specialization = 1;
1855 else
1857 /* This case handles bogus declarations like template <>
1858 template <class T> void f<int>(); */
1860 error ("template-id %qD in declaration of primary template",
1861 declarator);
1862 return decl;
1865 break;
1867 case tsk_invalid_member_spec:
1868 /* The error has already been reported in
1869 check_specialization_scope. */
1870 return error_mark_node;
1872 case tsk_invalid_expl_inst:
1873 error ("template parameter list used in explicit instantiation");
1875 /* Fall through. */
1877 case tsk_expl_inst:
1878 if (have_def)
1879 error ("definition provided for explicit instantiation");
1881 explicit_instantiation = 1;
1882 break;
1884 case tsk_excessive_parms:
1885 case tsk_insufficient_parms:
1886 if (tsk == tsk_excessive_parms)
1887 error ("too many template parameter lists in declaration of %qD",
1888 decl);
1889 else if (template_header_count)
1890 error("too few template parameter lists in declaration of %qD", decl);
1891 else
1892 error("explicit specialization of %qD must be introduced by "
1893 "%<template <>%>", decl);
1895 /* Fall through. */
1896 case tsk_expl_spec:
1897 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1898 if (ctype)
1899 member_specialization = 1;
1900 else
1901 specialization = 1;
1902 break;
1904 case tsk_template:
1905 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1907 /* This case handles bogus declarations like template <>
1908 template <class T> void f<int>(); */
1910 if (uses_template_parms (declarator))
1911 error ("function template partial specialization %qD "
1912 "is not allowed", declarator);
1913 else
1914 error ("template-id %qD in declaration of primary template",
1915 declarator);
1916 return decl;
1919 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1920 /* This is a specialization of a member template, without
1921 specialization the containing class. Something like:
1923 template <class T> struct S {
1924 template <class U> void f (U);
1926 template <> template <class U> void S<int>::f(U) {}
1928 That's a specialization -- but of the entire template. */
1929 specialization = 1;
1930 break;
1932 default:
1933 gcc_unreachable ();
1936 if (specialization || member_specialization)
1938 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1939 for (; t; t = TREE_CHAIN (t))
1940 if (TREE_PURPOSE (t))
1942 pedwarn
1943 ("default argument specified in explicit specialization");
1944 break;
1948 if (specialization || member_specialization || explicit_instantiation)
1950 tree tmpl = NULL_TREE;
1951 tree targs = NULL_TREE;
1953 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1954 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1956 tree fns;
1958 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1959 if (ctype)
1960 fns = dname;
1961 else
1963 /* If there is no class context, the explicit instantiation
1964 must be at namespace scope. */
1965 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1967 /* Find the namespace binding, using the declaration
1968 context. */
1969 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1970 false, true);
1971 if (!fns || !is_overloaded_fn (fns))
1973 error ("%qD is not a template function", dname);
1974 fns = error_mark_node;
1976 else
1978 tree fn = OVL_CURRENT (fns);
1979 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1980 CP_DECL_CONTEXT (fn)))
1981 error ("%qD is not declared in %qD",
1982 decl, current_namespace);
1986 declarator = lookup_template_function (fns, NULL_TREE);
1989 if (declarator == error_mark_node)
1990 return error_mark_node;
1992 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1994 if (!explicit_instantiation)
1995 /* A specialization in class scope. This is invalid,
1996 but the error will already have been flagged by
1997 check_specialization_scope. */
1998 return error_mark_node;
1999 else
2001 /* It's not valid to write an explicit instantiation in
2002 class scope, e.g.:
2004 class C { template void f(); }
2006 This case is caught by the parser. However, on
2007 something like:
2009 template class C { void f(); };
2011 (which is invalid) we can get here. The error will be
2012 issued later. */
2016 return decl;
2018 else if (ctype != NULL_TREE
2019 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2020 IDENTIFIER_NODE))
2022 /* Find the list of functions in ctype that have the same
2023 name as the declared function. */
2024 tree name = TREE_OPERAND (declarator, 0);
2025 tree fns = NULL_TREE;
2026 int idx;
2028 if (constructor_name_p (name, ctype))
2030 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2032 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2033 : !CLASSTYPE_DESTRUCTORS (ctype))
2035 /* From [temp.expl.spec]:
2037 If such an explicit specialization for the member
2038 of a class template names an implicitly-declared
2039 special member function (clause _special_), the
2040 program is ill-formed.
2042 Similar language is found in [temp.explicit]. */
2043 error ("specialization of implicitly-declared special member function");
2044 return error_mark_node;
2047 name = is_constructor ? ctor_identifier : dtor_identifier;
2050 if (!DECL_CONV_FN_P (decl))
2052 idx = lookup_fnfields_1 (ctype, name);
2053 if (idx >= 0)
2054 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2056 else
2058 VEC(tree,gc) *methods;
2059 tree ovl;
2061 /* For a type-conversion operator, we cannot do a
2062 name-based lookup. We might be looking for `operator
2063 int' which will be a specialization of `operator T'.
2064 So, we find *all* the conversion operators, and then
2065 select from them. */
2066 fns = NULL_TREE;
2068 methods = CLASSTYPE_METHOD_VEC (ctype);
2069 if (methods)
2070 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2071 VEC_iterate (tree, methods, idx, ovl);
2072 ++idx)
2074 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2075 /* There are no more conversion functions. */
2076 break;
2078 /* Glue all these conversion functions together
2079 with those we already have. */
2080 for (; ovl; ovl = OVL_NEXT (ovl))
2081 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2085 if (fns == NULL_TREE)
2087 error ("no member function %qD declared in %qT", name, ctype);
2088 return error_mark_node;
2090 else
2091 TREE_OPERAND (declarator, 0) = fns;
2094 /* Figure out what exactly is being specialized at this point.
2095 Note that for an explicit instantiation, even one for a
2096 member function, we cannot tell apriori whether the
2097 instantiation is for a member template, or just a member
2098 function of a template class. Even if a member template is
2099 being instantiated, the member template arguments may be
2100 elided if they can be deduced from the rest of the
2101 declaration. */
2102 tmpl = determine_specialization (declarator, decl,
2103 &targs,
2104 member_specialization,
2105 template_count,
2106 tsk);
2108 if (!tmpl || tmpl == error_mark_node)
2109 /* We couldn't figure out what this declaration was
2110 specializing. */
2111 return error_mark_node;
2112 else
2114 tree gen_tmpl = most_general_template (tmpl);
2116 if (explicit_instantiation)
2118 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2119 is done by do_decl_instantiation later. */
2121 int arg_depth = TMPL_ARGS_DEPTH (targs);
2122 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2124 if (arg_depth > parm_depth)
2126 /* If TMPL is not the most general template (for
2127 example, if TMPL is a friend template that is
2128 injected into namespace scope), then there will
2129 be too many levels of TARGS. Remove some of them
2130 here. */
2131 int i;
2132 tree new_targs;
2134 new_targs = make_tree_vec (parm_depth);
2135 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2136 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2137 = TREE_VEC_ELT (targs, i);
2138 targs = new_targs;
2141 return instantiate_template (tmpl, targs, tf_error);
2144 /* If we thought that the DECL was a member function, but it
2145 turns out to be specializing a static member function,
2146 make DECL a static member function as well. */
2147 if (DECL_STATIC_FUNCTION_P (tmpl)
2148 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2149 revert_static_member_fn (decl);
2151 /* If this is a specialization of a member template of a
2152 template class, we want to return the TEMPLATE_DECL, not
2153 the specialization of it. */
2154 if (tsk == tsk_template)
2156 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2157 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2158 if (have_def)
2160 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2161 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2162 = DECL_SOURCE_LOCATION (decl);
2163 /* We want to use the argument list specified in the
2164 definition, not in the original declaration. */
2165 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2166 = DECL_ARGUMENTS (decl);
2168 return tmpl;
2171 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2172 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2174 /* Inherit default function arguments from the template
2175 DECL is specializing. */
2176 copy_default_args_to_explicit_spec (decl);
2178 /* This specialization has the same protection as the
2179 template it specializes. */
2180 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2181 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2183 /* If DECL is a friend declaration, declared using an
2184 unqualified name, the namespace associated with DECL may
2185 have been set incorrectly. For example, in:
2187 template <typename T> void f(T);
2188 namespace N {
2189 struct S { friend void f<int>(int); }
2192 we will have set the DECL_CONTEXT for the friend
2193 declaration to N, rather than to the global namespace. */
2194 if (DECL_NAMESPACE_SCOPE_P (decl))
2195 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2197 if (is_friend && !have_def)
2198 /* This is not really a declaration of a specialization.
2199 It's just the name of an instantiation. But, it's not
2200 a request for an instantiation, either. */
2201 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2202 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2203 /* This is indeed a specialization. In case of constructors
2204 and destructors, we need in-charge and not-in-charge
2205 versions in V3 ABI. */
2206 clone_function_decl (decl, /*update_method_vec_p=*/0);
2208 /* Register this specialization so that we can find it
2209 again. */
2210 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2214 return decl;
2217 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2218 parameters. These are represented in the same format used for
2219 DECL_TEMPLATE_PARMS. */
2222 comp_template_parms (tree parms1, tree parms2)
2224 tree p1;
2225 tree p2;
2227 if (parms1 == parms2)
2228 return 1;
2230 for (p1 = parms1, p2 = parms2;
2231 p1 != NULL_TREE && p2 != NULL_TREE;
2232 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2234 tree t1 = TREE_VALUE (p1);
2235 tree t2 = TREE_VALUE (p2);
2236 int i;
2238 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2239 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2241 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2242 return 0;
2244 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2246 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2247 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2249 /* If either of the template parameters are invalid, assume
2250 they match for the sake of error recovery. */
2251 if (parm1 == error_mark_node || parm2 == error_mark_node)
2252 return 1;
2254 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2255 return 0;
2257 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2258 continue;
2259 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2260 return 0;
2264 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2265 /* One set of parameters has more parameters lists than the
2266 other. */
2267 return 0;
2269 return 1;
2272 /* Complain if DECL shadows a template parameter.
2274 [temp.local]: A template-parameter shall not be redeclared within its
2275 scope (including nested scopes). */
2277 void
2278 check_template_shadow (tree decl)
2280 tree olddecl;
2282 /* If we're not in a template, we can't possibly shadow a template
2283 parameter. */
2284 if (!current_template_parms)
2285 return;
2287 /* Figure out what we're shadowing. */
2288 if (TREE_CODE (decl) == OVERLOAD)
2289 decl = OVL_CURRENT (decl);
2290 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2292 /* If there's no previous binding for this name, we're not shadowing
2293 anything, let alone a template parameter. */
2294 if (!olddecl)
2295 return;
2297 /* If we're not shadowing a template parameter, we're done. Note
2298 that OLDDECL might be an OVERLOAD (or perhaps even an
2299 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2300 node. */
2301 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2302 return;
2304 /* We check for decl != olddecl to avoid bogus errors for using a
2305 name inside a class. We check TPFI to avoid duplicate errors for
2306 inline member templates. */
2307 if (decl == olddecl
2308 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2309 return;
2311 error ("declaration of %q+#D", decl);
2312 error (" shadows template parm %q+#D", olddecl);
2315 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2316 ORIG_LEVEL, DECL, and TYPE. */
2318 static tree
2319 build_template_parm_index (int index,
2320 int level,
2321 int orig_level,
2322 tree decl,
2323 tree type)
2325 tree t = make_node (TEMPLATE_PARM_INDEX);
2326 TEMPLATE_PARM_IDX (t) = index;
2327 TEMPLATE_PARM_LEVEL (t) = level;
2328 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2329 TEMPLATE_PARM_DECL (t) = decl;
2330 TREE_TYPE (t) = type;
2331 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2332 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2333 TREE_READONLY (t) = TREE_READONLY (decl);
2335 return t;
2338 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2339 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2340 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2341 new one is created. */
2343 static tree
2344 reduce_template_parm_level (tree index, tree type, int levels)
2346 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2347 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2348 != TEMPLATE_PARM_LEVEL (index) - levels))
2350 tree orig_decl = TEMPLATE_PARM_DECL (index);
2351 tree decl, t;
2353 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2354 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2355 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2356 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2357 DECL_ARTIFICIAL (decl) = 1;
2358 SET_DECL_TEMPLATE_PARM_P (decl);
2360 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2361 TEMPLATE_PARM_LEVEL (index) - levels,
2362 TEMPLATE_PARM_ORIG_LEVEL (index),
2363 decl, type);
2364 TEMPLATE_PARM_DESCENDANTS (index) = t;
2366 /* Template template parameters need this. */
2367 if (TREE_CODE (decl) != CONST_DECL)
2368 DECL_TEMPLATE_PARMS (decl)
2369 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2372 return TEMPLATE_PARM_DESCENDANTS (index);
2375 /* Process information from new template parameter PARM and append it to the
2376 LIST being built. This new parameter is a non-type parameter iff
2377 IS_NON_TYPE is true. */
2379 tree
2380 process_template_parm (tree list, tree parm, bool is_non_type)
2382 tree decl = 0;
2383 tree defval;
2384 tree err_parm_list;
2385 int idx = 0;
2387 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2388 defval = TREE_PURPOSE (parm);
2390 if (list)
2392 tree p = tree_last (list);
2394 if (p && TREE_VALUE (p) != error_mark_node)
2396 p = TREE_VALUE (p);
2397 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2398 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2399 else
2400 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2403 ++idx;
2405 else
2406 idx = 0;
2408 if (is_non_type)
2410 parm = TREE_VALUE (parm);
2412 SET_DECL_TEMPLATE_PARM_P (parm);
2414 if (TREE_TYPE (parm) == error_mark_node)
2416 err_parm_list = build_tree_list (defval, parm);
2417 TREE_VALUE (err_parm_list) = error_mark_node;
2418 return chainon (list, err_parm_list);
2420 else
2422 /* [temp.param]
2424 The top-level cv-qualifiers on the template-parameter are
2425 ignored when determining its type. */
2426 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2427 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2429 err_parm_list = build_tree_list (defval, parm);
2430 TREE_VALUE (err_parm_list) = error_mark_node;
2431 return chainon (list, err_parm_list);
2435 /* A template parameter is not modifiable. */
2436 TREE_CONSTANT (parm) = 1;
2437 TREE_INVARIANT (parm) = 1;
2438 TREE_READONLY (parm) = 1;
2439 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2440 TREE_CONSTANT (decl) = 1;
2441 TREE_INVARIANT (decl) = 1;
2442 TREE_READONLY (decl) = 1;
2443 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2444 = build_template_parm_index (idx, processing_template_decl,
2445 processing_template_decl,
2446 decl, TREE_TYPE (parm));
2448 else
2450 tree t;
2451 parm = TREE_VALUE (TREE_VALUE (parm));
2453 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2455 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2456 /* This is for distinguishing between real templates and template
2457 template parameters */
2458 TREE_TYPE (parm) = t;
2459 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2460 decl = parm;
2462 else
2464 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2465 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2466 decl = build_decl (TYPE_DECL, parm, t);
2469 TYPE_NAME (t) = decl;
2470 TYPE_STUB_DECL (t) = decl;
2471 parm = decl;
2472 TEMPLATE_TYPE_PARM_INDEX (t)
2473 = build_template_parm_index (idx, processing_template_decl,
2474 processing_template_decl,
2475 decl, TREE_TYPE (parm));
2477 DECL_ARTIFICIAL (decl) = 1;
2478 SET_DECL_TEMPLATE_PARM_P (decl);
2479 pushdecl (decl);
2480 parm = build_tree_list (defval, parm);
2481 return chainon (list, parm);
2484 /* The end of a template parameter list has been reached. Process the
2485 tree list into a parameter vector, converting each parameter into a more
2486 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2487 as PARM_DECLs. */
2489 tree
2490 end_template_parm_list (tree parms)
2492 int nparms;
2493 tree parm, next;
2494 tree saved_parmlist = make_tree_vec (list_length (parms));
2496 current_template_parms
2497 = tree_cons (size_int (processing_template_decl),
2498 saved_parmlist, current_template_parms);
2500 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2502 next = TREE_CHAIN (parm);
2503 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2504 TREE_CHAIN (parm) = NULL_TREE;
2507 --processing_template_parmlist;
2509 return saved_parmlist;
2512 /* end_template_decl is called after a template declaration is seen. */
2514 void
2515 end_template_decl (void)
2517 reset_specialization ();
2519 if (! processing_template_decl)
2520 return;
2522 /* This matches the pushlevel in begin_template_parm_list. */
2523 finish_scope ();
2525 --processing_template_decl;
2526 current_template_parms = TREE_CHAIN (current_template_parms);
2529 /* Given a template argument vector containing the template PARMS.
2530 The innermost PARMS are given first. */
2532 static tree
2533 current_template_args (void)
2535 tree header;
2536 tree args = NULL_TREE;
2537 int length = TMPL_PARMS_DEPTH (current_template_parms);
2538 int l = length;
2540 /* If there is only one level of template parameters, we do not
2541 create a TREE_VEC of TREE_VECs. Instead, we return a single
2542 TREE_VEC containing the arguments. */
2543 if (length > 1)
2544 args = make_tree_vec (length);
2546 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2548 tree a = copy_node (TREE_VALUE (header));
2549 int i;
2551 TREE_TYPE (a) = NULL_TREE;
2552 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2554 tree t = TREE_VEC_ELT (a, i);
2556 /* T will be a list if we are called from within a
2557 begin/end_template_parm_list pair, but a vector directly
2558 if within a begin/end_member_template_processing pair. */
2559 if (TREE_CODE (t) == TREE_LIST)
2561 t = TREE_VALUE (t);
2563 if (t != error_mark_node)
2565 if (TREE_CODE (t) == TYPE_DECL
2566 || TREE_CODE (t) == TEMPLATE_DECL)
2567 t = TREE_TYPE (t);
2568 else
2569 t = DECL_INITIAL (t);
2572 TREE_VEC_ELT (a, i) = t;
2576 if (length > 1)
2577 TREE_VEC_ELT (args, --l) = a;
2578 else
2579 args = a;
2582 return args;
2585 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2586 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2587 a member template. Used by push_template_decl below. */
2589 static tree
2590 build_template_decl (tree decl, tree parms, bool member_template_p)
2592 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2593 DECL_TEMPLATE_PARMS (tmpl) = parms;
2594 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2595 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2596 if (DECL_LANG_SPECIFIC (decl))
2598 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2599 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2600 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2601 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2602 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2603 if (DECL_OVERLOADED_OPERATOR_P (decl))
2604 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2605 DECL_OVERLOADED_OPERATOR_P (decl));
2608 return tmpl;
2611 struct template_parm_data
2613 /* The level of the template parameters we are currently
2614 processing. */
2615 int level;
2617 /* The index of the specialization argument we are currently
2618 processing. */
2619 int current_arg;
2621 /* An array whose size is the number of template parameters. The
2622 elements are nonzero if the parameter has been used in any one
2623 of the arguments processed so far. */
2624 int* parms;
2626 /* An array whose size is the number of template arguments. The
2627 elements are nonzero if the argument makes use of template
2628 parameters of this level. */
2629 int* arg_uses_template_parms;
2632 /* Subroutine of push_template_decl used to see if each template
2633 parameter in a partial specialization is used in the explicit
2634 argument list. If T is of the LEVEL given in DATA (which is
2635 treated as a template_parm_data*), then DATA->PARMS is marked
2636 appropriately. */
2638 static int
2639 mark_template_parm (tree t, void* data)
2641 int level;
2642 int idx;
2643 struct template_parm_data* tpd = (struct template_parm_data*) data;
2645 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2647 level = TEMPLATE_PARM_LEVEL (t);
2648 idx = TEMPLATE_PARM_IDX (t);
2650 else
2652 level = TEMPLATE_TYPE_LEVEL (t);
2653 idx = TEMPLATE_TYPE_IDX (t);
2656 if (level == tpd->level)
2658 tpd->parms[idx] = 1;
2659 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2662 /* Return zero so that for_each_template_parm will continue the
2663 traversal of the tree; we want to mark *every* template parm. */
2664 return 0;
2667 /* Process the partial specialization DECL. */
2669 static tree
2670 process_partial_specialization (tree decl)
2672 tree type = TREE_TYPE (decl);
2673 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2674 tree specargs = CLASSTYPE_TI_ARGS (type);
2675 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2676 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2677 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2678 int nargs = TREE_VEC_LENGTH (inner_args);
2679 int ntparms = TREE_VEC_LENGTH (inner_parms);
2680 int i;
2681 int did_error_intro = 0;
2682 struct template_parm_data tpd;
2683 struct template_parm_data tpd2;
2685 /* We check that each of the template parameters given in the
2686 partial specialization is used in the argument list to the
2687 specialization. For example:
2689 template <class T> struct S;
2690 template <class T> struct S<T*>;
2692 The second declaration is OK because `T*' uses the template
2693 parameter T, whereas
2695 template <class T> struct S<int>;
2697 is no good. Even trickier is:
2699 template <class T>
2700 struct S1
2702 template <class U>
2703 struct S2;
2704 template <class U>
2705 struct S2<T>;
2708 The S2<T> declaration is actually invalid; it is a
2709 full-specialization. Of course,
2711 template <class U>
2712 struct S2<T (*)(U)>;
2714 or some such would have been OK. */
2715 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2716 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2717 memset (tpd.parms, 0, sizeof (int) * ntparms);
2719 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2720 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2721 for (i = 0; i < nargs; ++i)
2723 tpd.current_arg = i;
2724 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2725 &mark_template_parm,
2726 &tpd,
2727 NULL);
2729 for (i = 0; i < ntparms; ++i)
2730 if (tpd.parms[i] == 0)
2732 /* One of the template parms was not used in the
2733 specialization. */
2734 if (!did_error_intro)
2736 error ("template parameters not used in partial specialization:");
2737 did_error_intro = 1;
2740 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2743 /* [temp.class.spec]
2745 The argument list of the specialization shall not be identical to
2746 the implicit argument list of the primary template. */
2747 if (comp_template_args
2748 (inner_args,
2749 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2750 (maintmpl)))))
2751 error ("partial specialization %qT does not specialize any template arguments", type);
2753 /* [temp.class.spec]
2755 A partially specialized non-type argument expression shall not
2756 involve template parameters of the partial specialization except
2757 when the argument expression is a simple identifier.
2759 The type of a template parameter corresponding to a specialized
2760 non-type argument shall not be dependent on a parameter of the
2761 specialization. */
2762 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2763 tpd2.parms = 0;
2764 for (i = 0; i < nargs; ++i)
2766 tree arg = TREE_VEC_ELT (inner_args, i);
2767 if (/* These first two lines are the `non-type' bit. */
2768 !TYPE_P (arg)
2769 && TREE_CODE (arg) != TEMPLATE_DECL
2770 /* This next line is the `argument expression is not just a
2771 simple identifier' condition and also the `specialized
2772 non-type argument' bit. */
2773 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2775 if (tpd.arg_uses_template_parms[i])
2776 error ("template argument %qE involves template parameter(s)", arg);
2777 else
2779 /* Look at the corresponding template parameter,
2780 marking which template parameters its type depends
2781 upon. */
2782 tree type =
2783 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2784 i)));
2786 if (!tpd2.parms)
2788 /* We haven't yet initialized TPD2. Do so now. */
2789 tpd2.arg_uses_template_parms
2790 = (int *) alloca (sizeof (int) * nargs);
2791 /* The number of parameters here is the number in the
2792 main template, which, as checked in the assertion
2793 above, is NARGS. */
2794 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2795 tpd2.level =
2796 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2799 /* Mark the template parameters. But this time, we're
2800 looking for the template parameters of the main
2801 template, not in the specialization. */
2802 tpd2.current_arg = i;
2803 tpd2.arg_uses_template_parms[i] = 0;
2804 memset (tpd2.parms, 0, sizeof (int) * nargs);
2805 for_each_template_parm (type,
2806 &mark_template_parm,
2807 &tpd2,
2808 NULL);
2810 if (tpd2.arg_uses_template_parms [i])
2812 /* The type depended on some template parameters.
2813 If they are fully specialized in the
2814 specialization, that's OK. */
2815 int j;
2816 for (j = 0; j < nargs; ++j)
2817 if (tpd2.parms[j] != 0
2818 && tpd.arg_uses_template_parms [j])
2820 error ("type %qT of template argument %qE depends "
2821 "on template parameter(s)",
2822 type,
2823 arg);
2824 break;
2831 if (retrieve_specialization (maintmpl, specargs,
2832 /*class_specializations_p=*/true))
2833 /* We've already got this specialization. */
2834 return decl;
2836 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2837 = tree_cons (specargs, inner_parms,
2838 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2839 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2840 return decl;
2843 /* Check that a template declaration's use of default arguments is not
2844 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2845 nonzero if DECL is the thing declared by a primary template.
2846 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2848 static void
2849 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2851 const char *msg;
2852 int last_level_to_check;
2853 tree parm_level;
2855 /* [temp.param]
2857 A default template-argument shall not be specified in a
2858 function template declaration or a function template definition, nor
2859 in the template-parameter-list of the definition of a member of a
2860 class template. */
2862 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2863 /* You can't have a function template declaration in a local
2864 scope, nor you can you define a member of a class template in a
2865 local scope. */
2866 return;
2868 if (current_class_type
2869 && !TYPE_BEING_DEFINED (current_class_type)
2870 && DECL_LANG_SPECIFIC (decl)
2871 /* If this is either a friend defined in the scope of the class
2872 or a member function. */
2873 && (DECL_FUNCTION_MEMBER_P (decl)
2874 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2875 : DECL_FRIEND_CONTEXT (decl)
2876 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2877 : false)
2878 /* And, if it was a member function, it really was defined in
2879 the scope of the class. */
2880 && (!DECL_FUNCTION_MEMBER_P (decl)
2881 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2882 /* We already checked these parameters when the template was
2883 declared, so there's no need to do it again now. This function
2884 was defined in class scope, but we're processing it's body now
2885 that the class is complete. */
2886 return;
2888 /* [temp.param]
2890 If a template-parameter has a default template-argument, all
2891 subsequent template-parameters shall have a default
2892 template-argument supplied. */
2893 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2895 tree inner_parms = TREE_VALUE (parm_level);
2896 int ntparms = TREE_VEC_LENGTH (inner_parms);
2897 int seen_def_arg_p = 0;
2898 int i;
2900 for (i = 0; i < ntparms; ++i)
2902 tree parm = TREE_VEC_ELT (inner_parms, i);
2904 if (parm == error_mark_node)
2905 continue;
2907 if (TREE_PURPOSE (parm))
2908 seen_def_arg_p = 1;
2909 else if (seen_def_arg_p)
2911 error ("no default argument for %qD", TREE_VALUE (parm));
2912 /* For better subsequent error-recovery, we indicate that
2913 there should have been a default argument. */
2914 TREE_PURPOSE (parm) = error_mark_node;
2919 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2920 /* For an ordinary class template, default template arguments are
2921 allowed at the innermost level, e.g.:
2922 template <class T = int>
2923 struct S {};
2924 but, in a partial specialization, they're not allowed even
2925 there, as we have in [temp.class.spec]:
2927 The template parameter list of a specialization shall not
2928 contain default template argument values.
2930 So, for a partial specialization, or for a function template,
2931 we look at all of them. */
2933 else
2934 /* But, for a primary class template that is not a partial
2935 specialization we look at all template parameters except the
2936 innermost ones. */
2937 parms = TREE_CHAIN (parms);
2939 /* Figure out what error message to issue. */
2940 if (TREE_CODE (decl) == FUNCTION_DECL)
2941 msg = "default template arguments may not be used in function templates";
2942 else if (is_partial)
2943 msg = "default template arguments may not be used in partial specializations";
2944 else
2945 msg = "default argument for template parameter for class enclosing %qD";
2947 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2948 /* If we're inside a class definition, there's no need to
2949 examine the parameters to the class itself. On the one
2950 hand, they will be checked when the class is defined, and,
2951 on the other, default arguments are valid in things like:
2952 template <class T = double>
2953 struct S { template <class U> void f(U); };
2954 Here the default argument for `S' has no bearing on the
2955 declaration of `f'. */
2956 last_level_to_check = template_class_depth (current_class_type) + 1;
2957 else
2958 /* Check everything. */
2959 last_level_to_check = 0;
2961 for (parm_level = parms;
2962 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2963 parm_level = TREE_CHAIN (parm_level))
2965 tree inner_parms = TREE_VALUE (parm_level);
2966 int i;
2967 int ntparms;
2969 ntparms = TREE_VEC_LENGTH (inner_parms);
2970 for (i = 0; i < ntparms; ++i)
2972 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
2973 continue;
2975 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2977 if (msg)
2979 error (msg, decl);
2980 msg = 0;
2983 /* Clear out the default argument so that we are not
2984 confused later. */
2985 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2989 /* At this point, if we're still interested in issuing messages,
2990 they must apply to classes surrounding the object declared. */
2991 if (msg)
2992 msg = "default argument for template parameter for class enclosing %qD";
2996 /* Worker for push_template_decl_real, called via
2997 for_each_template_parm. DATA is really an int, indicating the
2998 level of the parameters we are interested in. If T is a template
2999 parameter of that level, return nonzero. */
3001 static int
3002 template_parm_this_level_p (tree t, void* data)
3004 int this_level = *(int *)data;
3005 int level;
3007 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3008 level = TEMPLATE_PARM_LEVEL (t);
3009 else
3010 level = TEMPLATE_TYPE_LEVEL (t);
3011 return level == this_level;
3014 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3015 parameters given by current_template_args, or reuses a
3016 previously existing one, if appropriate. Returns the DECL, or an
3017 equivalent one, if it is replaced via a call to duplicate_decls.
3019 If IS_FRIEND is true, DECL is a friend declaration. */
3021 tree
3022 push_template_decl_real (tree decl, bool is_friend)
3024 tree tmpl;
3025 tree args;
3026 tree info;
3027 tree ctx;
3028 int primary;
3029 int is_partial;
3030 int new_template_p = 0;
3031 /* True if the template is a member template, in the sense of
3032 [temp.mem]. */
3033 bool member_template_p = false;
3035 if (decl == error_mark_node)
3036 return decl;
3038 /* See if this is a partial specialization. */
3039 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3040 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3043 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3044 is_friend = true;
3046 if (is_friend)
3047 /* For a friend, we want the context of the friend function, not
3048 the type of which it is a friend. */
3049 ctx = DECL_CONTEXT (decl);
3050 else if (CP_DECL_CONTEXT (decl)
3051 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3052 /* In the case of a virtual function, we want the class in which
3053 it is defined. */
3054 ctx = CP_DECL_CONTEXT (decl);
3055 else
3056 /* Otherwise, if we're currently defining some class, the DECL
3057 is assumed to be a member of the class. */
3058 ctx = current_scope ();
3060 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3061 ctx = NULL_TREE;
3063 if (!DECL_CONTEXT (decl))
3064 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3066 /* See if this is a primary template. */
3067 if (is_friend && ctx)
3068 /* A friend template that specifies a class context, i.e.
3069 template <typename T> friend void A<T>::f();
3070 is not primary. */
3071 primary = 0;
3072 else
3073 primary = template_parm_scope_p ();
3075 if (primary)
3077 if (DECL_CLASS_SCOPE_P (decl))
3078 member_template_p = true;
3079 if (TREE_CODE (decl) == TYPE_DECL
3080 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3081 error ("template class without a name");
3082 else if (TREE_CODE (decl) == FUNCTION_DECL)
3084 if (DECL_DESTRUCTOR_P (decl))
3086 /* [temp.mem]
3088 A destructor shall not be a member template. */
3089 error ("destructor %qD declared as member template", decl);
3090 return error_mark_node;
3092 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3093 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3094 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3095 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3096 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3097 == void_list_node)))
3099 /* [basic.stc.dynamic.allocation]
3101 An allocation function can be a function
3102 template. ... Template allocation functions shall
3103 have two or more parameters. */
3104 error ("invalid template declaration of %qD", decl);
3105 return error_mark_node;
3108 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3109 && CLASS_TYPE_P (TREE_TYPE (decl)))
3110 /* OK */;
3111 else
3113 error ("template declaration of %q#D", decl);
3114 return error_mark_node;
3118 /* Check to see that the rules regarding the use of default
3119 arguments are not being violated. */
3120 check_default_tmpl_args (decl, current_template_parms,
3121 primary, is_partial);
3123 if (is_partial)
3124 return process_partial_specialization (decl);
3126 args = current_template_args ();
3128 if (!ctx
3129 || TREE_CODE (ctx) == FUNCTION_DECL
3130 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3131 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3133 if (DECL_LANG_SPECIFIC (decl)
3134 && DECL_TEMPLATE_INFO (decl)
3135 && DECL_TI_TEMPLATE (decl))
3136 tmpl = DECL_TI_TEMPLATE (decl);
3137 /* If DECL is a TYPE_DECL for a class-template, then there won't
3138 be DECL_LANG_SPECIFIC. The information equivalent to
3139 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3140 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3141 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3142 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3144 /* Since a template declaration already existed for this
3145 class-type, we must be redeclaring it here. Make sure
3146 that the redeclaration is valid. */
3147 redeclare_class_template (TREE_TYPE (decl),
3148 current_template_parms);
3149 /* We don't need to create a new TEMPLATE_DECL; just use the
3150 one we already had. */
3151 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3153 else
3155 tmpl = build_template_decl (decl, current_template_parms,
3156 member_template_p);
3157 new_template_p = 1;
3159 if (DECL_LANG_SPECIFIC (decl)
3160 && DECL_TEMPLATE_SPECIALIZATION (decl))
3162 /* A specialization of a member template of a template
3163 class. */
3164 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3165 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3166 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3170 else
3172 tree a, t, current, parms;
3173 int i;
3175 if (TREE_CODE (decl) == TYPE_DECL)
3177 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3178 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3179 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3180 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3181 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3182 else
3184 error ("%qD does not declare a template type", decl);
3185 return decl;
3188 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3190 error ("template definition of non-template %q#D", decl);
3191 return decl;
3193 else
3194 tmpl = DECL_TI_TEMPLATE (decl);
3196 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3197 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3198 && DECL_TEMPLATE_SPECIALIZATION (decl)
3199 && DECL_MEMBER_TEMPLATE_P (tmpl))
3201 tree new_tmpl;
3203 /* The declaration is a specialization of a member
3204 template, declared outside the class. Therefore, the
3205 innermost template arguments will be NULL, so we
3206 replace them with the arguments determined by the
3207 earlier call to check_explicit_specialization. */
3208 args = DECL_TI_ARGS (decl);
3210 new_tmpl
3211 = build_template_decl (decl, current_template_parms,
3212 member_template_p);
3213 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3214 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3215 DECL_TI_TEMPLATE (decl) = new_tmpl;
3216 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3217 DECL_TEMPLATE_INFO (new_tmpl)
3218 = tree_cons (tmpl, args, NULL_TREE);
3220 register_specialization (new_tmpl,
3221 most_general_template (tmpl),
3222 args,
3223 is_friend);
3224 return decl;
3227 /* Make sure the template headers we got make sense. */
3229 parms = DECL_TEMPLATE_PARMS (tmpl);
3230 i = TMPL_PARMS_DEPTH (parms);
3231 if (TMPL_ARGS_DEPTH (args) != i)
3233 error ("expected %d levels of template parms for %q#D, got %d",
3234 i, decl, TMPL_ARGS_DEPTH (args));
3236 else
3237 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3239 a = TMPL_ARGS_LEVEL (args, i);
3240 t = INNERMOST_TEMPLATE_PARMS (parms);
3242 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3244 if (current == decl)
3245 error ("got %d template parameters for %q#D",
3246 TREE_VEC_LENGTH (a), decl);
3247 else
3248 error ("got %d template parameters for %q#T",
3249 TREE_VEC_LENGTH (a), current);
3250 error (" but %d required", TREE_VEC_LENGTH (t));
3251 return error_mark_node;
3254 /* Perhaps we should also check that the parms are used in the
3255 appropriate qualifying scopes in the declarator? */
3257 if (current == decl)
3258 current = ctx;
3259 else
3260 current = TYPE_CONTEXT (current);
3264 DECL_TEMPLATE_RESULT (tmpl) = decl;
3265 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3267 /* Push template declarations for global functions and types. Note
3268 that we do not try to push a global template friend declared in a
3269 template class; such a thing may well depend on the template
3270 parameters of the class. */
3271 if (new_template_p && !ctx
3272 && !(is_friend && template_class_depth (current_class_type) > 0))
3274 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3275 if (tmpl == error_mark_node)
3276 return error_mark_node;
3278 /* Hide template friend classes that haven't been declared yet. */
3279 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3281 DECL_ANTICIPATED (tmpl) = 1;
3282 DECL_FRIEND_P (tmpl) = 1;
3286 if (primary)
3288 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3289 if (DECL_CONV_FN_P (tmpl))
3291 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3293 /* It is a conversion operator. See if the type converted to
3294 depends on innermost template operands. */
3296 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3297 depth))
3298 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3302 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3303 back to its most general template. If TMPL is a specialization,
3304 ARGS may only have the innermost set of arguments. Add the missing
3305 argument levels if necessary. */
3306 if (DECL_TEMPLATE_INFO (tmpl))
3307 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3309 info = tree_cons (tmpl, args, NULL_TREE);
3311 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3313 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3314 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3315 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3316 /* Don't change the name if we've already set it up. */
3317 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3318 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3320 else if (DECL_LANG_SPECIFIC (decl))
3321 DECL_TEMPLATE_INFO (decl) = info;
3323 return DECL_TEMPLATE_RESULT (tmpl);
3326 tree
3327 push_template_decl (tree decl)
3329 return push_template_decl_real (decl, false);
3332 /* Called when a class template TYPE is redeclared with the indicated
3333 template PARMS, e.g.:
3335 template <class T> struct S;
3336 template <class T> struct S {}; */
3338 bool
3339 redeclare_class_template (tree type, tree parms)
3341 tree tmpl;
3342 tree tmpl_parms;
3343 int i;
3345 if (!TYPE_TEMPLATE_INFO (type))
3347 error ("%qT is not a template type", type);
3348 return false;
3351 tmpl = TYPE_TI_TEMPLATE (type);
3352 if (!PRIMARY_TEMPLATE_P (tmpl))
3353 /* The type is nested in some template class. Nothing to worry
3354 about here; there are no new template parameters for the nested
3355 type. */
3356 return true;
3358 if (!parms)
3360 error ("template specifiers not specified in declaration of %qD",
3361 tmpl);
3362 return false;
3365 parms = INNERMOST_TEMPLATE_PARMS (parms);
3366 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3368 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3370 error ("previous declaration %q+D", tmpl);
3371 error ("used %d template parameter(s) instead of %d",
3372 TREE_VEC_LENGTH (tmpl_parms),
3373 TREE_VEC_LENGTH (parms));
3374 return false;
3377 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3379 tree tmpl_parm;
3380 tree parm;
3381 tree tmpl_default;
3382 tree parm_default;
3384 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3385 || TREE_VEC_ELT (parms, i) == error_mark_node)
3386 continue;
3388 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3389 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3390 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3391 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3393 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3394 TEMPLATE_DECL. */
3395 if (tmpl_parm != error_mark_node
3396 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3397 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3398 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
3400 error ("template parameter %q+#D", tmpl_parm);
3401 error ("redeclared here as %q#D", parm);
3402 return false;
3405 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3407 /* We have in [temp.param]:
3409 A template-parameter may not be given default arguments
3410 by two different declarations in the same scope. */
3411 error ("redefinition of default argument for %q#D", parm);
3412 error ("%J original definition appeared here", tmpl_parm);
3413 return false;
3416 if (parm_default != NULL_TREE)
3417 /* Update the previous template parameters (which are the ones
3418 that will really count) with the new default value. */
3419 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3420 else if (tmpl_default != NULL_TREE)
3421 /* Update the new parameters, too; they'll be used as the
3422 parameters for any members. */
3423 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3426 return true;
3429 /* Simplify EXPR if it is a non-dependent expression. Returns the
3430 (possibly simplified) expression. */
3432 tree
3433 fold_non_dependent_expr (tree expr)
3435 if (expr == NULL_TREE)
3436 return NULL_TREE;
3438 /* If we're in a template, but EXPR isn't value dependent, simplify
3439 it. We're supposed to treat:
3441 template <typename T> void f(T[1 + 1]);
3442 template <typename T> void f(T[2]);
3444 as two declarations of the same function, for example. */
3445 if (processing_template_decl
3446 && !type_dependent_expression_p (expr)
3447 && !value_dependent_expression_p (expr))
3449 HOST_WIDE_INT saved_processing_template_decl;
3451 saved_processing_template_decl = processing_template_decl;
3452 processing_template_decl = 0;
3453 expr = tsubst_copy_and_build (expr,
3454 /*args=*/NULL_TREE,
3455 tf_error,
3456 /*in_decl=*/NULL_TREE,
3457 /*function_p=*/false,
3458 /*integral_constant_expression_p=*/true);
3459 processing_template_decl = saved_processing_template_decl;
3461 return expr;
3464 /* EXPR is an expression which is used in a constant-expression context.
3465 For instance, it could be a VAR_DECL with a constant initializer.
3466 Extract the innest constant expression.
3468 This is basically a more powerful version of
3469 integral_constant_value, which can be used also in templates where
3470 initializers can maintain a syntactic rather than semantic form
3471 (even if they are non-dependent, for access-checking purposes). */
3473 static tree
3474 fold_decl_constant_value (tree expr)
3476 tree const_expr = expr;
3479 expr = fold_non_dependent_expr (const_expr);
3480 const_expr = integral_constant_value (expr);
3482 while (expr != const_expr);
3484 return expr;
3487 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3488 must be a function or a pointer-to-function type, as specified
3489 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3490 and check that the resulting function has external linkage. */
3492 static tree
3493 convert_nontype_argument_function (tree type, tree expr)
3495 tree fns = expr;
3496 tree fn, fn_no_ptr;
3498 fn = instantiate_type (type, fns, tf_none);
3499 if (fn == error_mark_node)
3500 return error_mark_node;
3502 fn_no_ptr = fn;
3503 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3504 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3505 if (TREE_CODE (fn_no_ptr) == BASELINK)
3506 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3508 /* [temp.arg.nontype]/1
3510 A template-argument for a non-type, non-template template-parameter
3511 shall be one of:
3512 [...]
3513 -- the address of an object or function with external linkage. */
3514 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3516 error ("%qE is not a valid template argument for type %qT "
3517 "because function %qD has not external linkage",
3518 expr, type, fn_no_ptr);
3519 return NULL_TREE;
3522 return fn;
3525 /* Attempt to convert the non-type template parameter EXPR to the
3526 indicated TYPE. If the conversion is successful, return the
3527 converted value. If the conversion is unsuccessful, return
3528 NULL_TREE if we issued an error message, or error_mark_node if we
3529 did not. We issue error messages for out-and-out bad template
3530 parameters, but not simply because the conversion failed, since we
3531 might be just trying to do argument deduction. Both TYPE and EXPR
3532 must be non-dependent.
3534 The conversion follows the special rules described in
3535 [temp.arg.nontype], and it is much more strict than an implicit
3536 conversion.
3538 This function is called twice for each template argument (see
3539 lookup_template_class for a more accurate description of this
3540 problem). This means that we need to handle expressions which
3541 are not valid in a C++ source, but can be created from the
3542 first call (for instance, casts to perform conversions). These
3543 hacks can go away after we fix the double coercion problem. */
3545 static tree
3546 convert_nontype_argument (tree type, tree expr)
3548 tree expr_type;
3550 /* Detect immediately string literals as invalid non-type argument.
3551 This special-case is not needed for correctness (we would easily
3552 catch this later), but only to provide better diagnostic for this
3553 common user mistake. As suggested by DR 100, we do not mention
3554 linkage issues in the diagnostic as this is not the point. */
3555 if (TREE_CODE (expr) == STRING_CST)
3557 error ("%qE is not a valid template argument for type %qT "
3558 "because string literals can never be used in this context",
3559 expr, type);
3560 return NULL_TREE;
3563 /* If we are in a template, EXPR may be non-dependent, but still
3564 have a syntactic, rather than semantic, form. For example, EXPR
3565 might be a SCOPE_REF, rather than the VAR_DECL to which the
3566 SCOPE_REF refers. Preserving the qualifying scope is necessary
3567 so that access checking can be performed when the template is
3568 instantiated -- but here we need the resolved form so that we can
3569 convert the argument. */
3570 expr = fold_non_dependent_expr (expr);
3571 if (error_operand_p (expr))
3572 return error_mark_node;
3573 expr_type = TREE_TYPE (expr);
3575 /* HACK: Due to double coercion, we can get a
3576 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3577 which is the tree that we built on the first call (see
3578 below when coercing to reference to object or to reference to
3579 function). We just strip everything and get to the arg.
3580 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3581 for examples. */
3582 if (TREE_CODE (expr) == NOP_EXPR)
3584 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3586 /* ??? Maybe we could use convert_from_reference here, but we
3587 would need to relax its constraints because the NOP_EXPR
3588 could actually change the type to something more cv-qualified,
3589 and this is not folded by convert_from_reference. */
3590 tree addr = TREE_OPERAND (expr, 0);
3591 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3592 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3593 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3594 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3595 (TREE_TYPE (expr_type),
3596 TREE_TYPE (TREE_TYPE (addr))));
3598 expr = TREE_OPERAND (addr, 0);
3599 expr_type = TREE_TYPE (expr);
3602 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3603 parameter is a pointer to object, through decay and
3604 qualification conversion. Let's strip everything. */
3605 else if (TYPE_PTROBV_P (type))
3607 STRIP_NOPS (expr);
3608 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3609 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3610 /* Skip the ADDR_EXPR only if it is part of the decay for
3611 an array. Otherwise, it is part of the original argument
3612 in the source code. */
3613 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3614 expr = TREE_OPERAND (expr, 0);
3615 expr_type = TREE_TYPE (expr);
3619 /* [temp.arg.nontype]/5, bullet 1
3621 For a non-type template-parameter of integral or enumeration type,
3622 integral promotions (_conv.prom_) and integral conversions
3623 (_conv.integral_) are applied. */
3624 if (INTEGRAL_TYPE_P (type))
3626 if (!INTEGRAL_TYPE_P (expr_type))
3627 return error_mark_node;
3629 expr = fold_decl_constant_value (expr);
3630 /* Notice that there are constant expressions like '4 % 0' which
3631 do not fold into integer constants. */
3632 if (TREE_CODE (expr) != INTEGER_CST)
3634 error ("%qE is not a valid template argument for type %qT "
3635 "because it is a non-constant expression", expr, type);
3636 return NULL_TREE;
3639 /* At this point, an implicit conversion does what we want,
3640 because we already know that the expression is of integral
3641 type. */
3642 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3643 if (expr == error_mark_node)
3644 return error_mark_node;
3646 /* Conversion was allowed: fold it to a bare integer constant. */
3647 expr = fold (expr);
3649 /* [temp.arg.nontype]/5, bullet 2
3651 For a non-type template-parameter of type pointer to object,
3652 qualification conversions (_conv.qual_) and the array-to-pointer
3653 conversion (_conv.array_) are applied. */
3654 else if (TYPE_PTROBV_P (type))
3656 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3658 A template-argument for a non-type, non-template template-parameter
3659 shall be one of: [...]
3661 -- the name of a non-type template-parameter;
3662 -- the address of an object or function with external linkage, [...]
3663 expressed as "& id-expression" where the & is optional if the name
3664 refers to a function or array, or if the corresponding
3665 template-parameter is a reference.
3667 Here, we do not care about functions, as they are invalid anyway
3668 for a parameter of type pointer-to-object. */
3670 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
3671 /* Non-type template parameters are OK. */
3673 else if (TREE_CODE (expr) != ADDR_EXPR
3674 && TREE_CODE (expr_type) != ARRAY_TYPE)
3676 if (TREE_CODE (expr) == VAR_DECL)
3678 error ("%qD is not a valid template argument "
3679 "because %qD is a variable, not the address of "
3680 "a variable",
3681 expr, expr);
3682 return NULL_TREE;
3684 /* Other values, like integer constants, might be valid
3685 non-type arguments of some other type. */
3686 return error_mark_node;
3688 else
3690 tree decl;
3692 decl = ((TREE_CODE (expr) == ADDR_EXPR)
3693 ? TREE_OPERAND (expr, 0) : expr);
3694 if (TREE_CODE (decl) != VAR_DECL)
3696 error ("%qE is not a valid template argument of type %qT "
3697 "because %qE is not a variable",
3698 expr, type, decl);
3699 return NULL_TREE;
3701 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
3703 error ("%qE is not a valid template argument of type %qT "
3704 "because %qD does not have external linkage",
3705 expr, type, decl);
3706 return NULL_TREE;
3710 expr = decay_conversion (expr);
3711 if (expr == error_mark_node)
3712 return error_mark_node;
3714 expr = perform_qualification_conversions (type, expr);
3715 if (expr == error_mark_node)
3716 return error_mark_node;
3718 /* [temp.arg.nontype]/5, bullet 3
3720 For a non-type template-parameter of type reference to object, no
3721 conversions apply. The type referred to by the reference may be more
3722 cv-qualified than the (otherwise identical) type of the
3723 template-argument. The template-parameter is bound directly to the
3724 template-argument, which must be an lvalue. */
3725 else if (TYPE_REF_OBJ_P (type))
3727 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3728 expr_type))
3729 return error_mark_node;
3731 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3733 error ("%qE is not a valid template argument for type %qT "
3734 "because of conflicts in cv-qualification", expr, type);
3735 return NULL_TREE;
3738 if (!real_lvalue_p (expr))
3740 error ("%qE is not a valid template argument for type %qT "
3741 "because it is not an lvalue", expr, type);
3742 return NULL_TREE;
3745 /* [temp.arg.nontype]/1
3747 A template-argument for a non-type, non-template template-parameter
3748 shall be one of: [...]
3750 -- the address of an object or function with external linkage. */
3751 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3753 error ("%qE is not a valid template argument for type %qT "
3754 "because object %qD has not external linkage",
3755 expr, type, expr);
3756 return NULL_TREE;
3759 expr = build_nop (type, build_address (expr));
3761 /* [temp.arg.nontype]/5, bullet 4
3763 For a non-type template-parameter of type pointer to function, only
3764 the function-to-pointer conversion (_conv.func_) is applied. If the
3765 template-argument represents a set of overloaded functions (or a
3766 pointer to such), the matching function is selected from the set
3767 (_over.over_). */
3768 else if (TYPE_PTRFN_P (type))
3770 /* If the argument is a template-id, we might not have enough
3771 context information to decay the pointer. */
3772 if (!type_unknown_p (expr_type))
3774 expr = decay_conversion (expr);
3775 if (expr == error_mark_node)
3776 return error_mark_node;
3779 expr = convert_nontype_argument_function (type, expr);
3780 if (!expr || expr == error_mark_node)
3781 return expr;
3783 /* [temp.arg.nontype]/5, bullet 5
3785 For a non-type template-parameter of type reference to function, no
3786 conversions apply. If the template-argument represents a set of
3787 overloaded functions, the matching function is selected from the set
3788 (_over.over_). */
3789 else if (TYPE_REFFN_P (type))
3791 if (TREE_CODE (expr) == ADDR_EXPR)
3793 error ("%qE is not a valid template argument for type %qT "
3794 "because it is a pointer", expr, type);
3795 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3796 return NULL_TREE;
3799 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3800 if (!expr || expr == error_mark_node)
3801 return expr;
3803 expr = build_nop (type, build_address (expr));
3805 /* [temp.arg.nontype]/5, bullet 6
3807 For a non-type template-parameter of type pointer to member function,
3808 no conversions apply. If the template-argument represents a set of
3809 overloaded member functions, the matching member function is selected
3810 from the set (_over.over_). */
3811 else if (TYPE_PTRMEMFUNC_P (type))
3813 expr = instantiate_type (type, expr, tf_none);
3814 if (expr == error_mark_node)
3815 return error_mark_node;
3817 /* There is no way to disable standard conversions in
3818 resolve_address_of_overloaded_function (called by
3819 instantiate_type). It is possible that the call succeeded by
3820 converting &B::I to &D::I (where B is a base of D), so we need
3821 to reject this conversion here.
3823 Actually, even if there was a way to disable standard conversions,
3824 it would still be better to reject them here so that we can
3825 provide a superior diagnostic. */
3826 if (!same_type_p (TREE_TYPE (expr), type))
3828 /* Make sure we are just one standard conversion off. */
3829 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3830 error ("%qE is not a valid template argument for type %qT "
3831 "because it is of type %qT", expr, type,
3832 TREE_TYPE (expr));
3833 inform ("standard conversions are not allowed in this context");
3834 return NULL_TREE;
3837 /* [temp.arg.nontype]/5, bullet 7
3839 For a non-type template-parameter of type pointer to data member,
3840 qualification conversions (_conv.qual_) are applied. */
3841 else if (TYPE_PTRMEM_P (type))
3843 expr = perform_qualification_conversions (type, expr);
3844 if (expr == error_mark_node)
3845 return expr;
3847 /* A template non-type parameter must be one of the above. */
3848 else
3849 gcc_unreachable ();
3851 /* Sanity check: did we actually convert the argument to the
3852 right type? */
3853 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3854 return expr;
3858 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3859 template template parameters. Both PARM_PARMS and ARG_PARMS are
3860 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3861 or PARM_DECL.
3863 Consider the example:
3864 template <class T> class A;
3865 template<template <class U> class TT> class B;
3867 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3868 the parameters to A, and OUTER_ARGS contains A. */
3870 static int
3871 coerce_template_template_parms (tree parm_parms,
3872 tree arg_parms,
3873 tsubst_flags_t complain,
3874 tree in_decl,
3875 tree outer_args)
3877 int nparms, nargs, i;
3878 tree parm, arg;
3880 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3881 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3883 nparms = TREE_VEC_LENGTH (parm_parms);
3884 nargs = TREE_VEC_LENGTH (arg_parms);
3886 if (nargs != nparms)
3887 return 0;
3889 for (i = 0; i < nparms; ++i)
3891 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3892 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
3893 continue;
3895 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3896 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3898 if (arg == NULL_TREE || arg == error_mark_node
3899 || parm == NULL_TREE || parm == error_mark_node)
3900 return 0;
3902 if (TREE_CODE (arg) != TREE_CODE (parm))
3903 return 0;
3905 switch (TREE_CODE (parm))
3907 case TYPE_DECL:
3908 break;
3910 case TEMPLATE_DECL:
3911 /* We encounter instantiations of templates like
3912 template <template <template <class> class> class TT>
3913 class C; */
3915 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3916 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3918 if (!coerce_template_template_parms
3919 (parmparm, argparm, complain, in_decl, outer_args))
3920 return 0;
3922 break;
3924 case PARM_DECL:
3925 /* The tsubst call is used to handle cases such as
3927 template <int> class C {};
3928 template <class T, template <T> class TT> class D {};
3929 D<int, C> d;
3931 i.e. the parameter list of TT depends on earlier parameters. */
3932 if (!dependent_type_p (TREE_TYPE (arg))
3933 && !same_type_p
3934 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3935 TREE_TYPE (arg)))
3936 return 0;
3937 break;
3939 default:
3940 gcc_unreachable ();
3943 return 1;
3946 /* Convert the indicated template ARG as necessary to match the
3947 indicated template PARM. Returns the converted ARG, or
3948 error_mark_node if the conversion was unsuccessful. Error and
3949 warning messages are issued under control of COMPLAIN. This
3950 conversion is for the Ith parameter in the parameter list. ARGS is
3951 the full set of template arguments deduced so far. */
3953 static tree
3954 convert_template_argument (tree parm,
3955 tree arg,
3956 tree args,
3957 tsubst_flags_t complain,
3958 int i,
3959 tree in_decl)
3961 tree val;
3962 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3964 if (TREE_CODE (arg) == TREE_LIST
3965 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3967 /* The template argument was the name of some
3968 member function. That's usually
3969 invalid, but static members are OK. In any
3970 case, grab the underlying fields/functions
3971 and issue an error later if required. */
3972 arg = TREE_VALUE (arg);
3973 TREE_TYPE (arg) = unknown_type_node;
3976 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3977 requires_type = (TREE_CODE (parm) == TYPE_DECL
3978 || requires_tmpl_type);
3980 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3981 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3982 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3983 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3985 if (is_tmpl_type
3986 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3987 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3988 arg = TYPE_STUB_DECL (arg);
3990 is_type = TYPE_P (arg) || is_tmpl_type;
3992 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3993 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3995 pedwarn ("to refer to a type member of a template parameter, "
3996 "use %<typename %E%>", arg);
3998 arg = make_typename_type (TREE_OPERAND (arg, 0),
3999 TREE_OPERAND (arg, 1),
4000 typename_type,
4001 complain & tf_error);
4002 is_type = 1;
4004 if (is_type != requires_type)
4006 if (in_decl)
4008 if (complain & tf_error)
4010 error ("type/value mismatch at argument %d in template "
4011 "parameter list for %qD",
4012 i + 1, in_decl);
4013 if (is_type)
4014 error (" expected a constant of type %qT, got %qT",
4015 TREE_TYPE (parm),
4016 (is_tmpl_type ? DECL_NAME (arg) : arg));
4017 else if (requires_tmpl_type)
4018 error (" expected a class template, got %qE", arg);
4019 else
4020 error (" expected a type, got %qE", arg);
4023 return error_mark_node;
4025 if (is_tmpl_type ^ requires_tmpl_type)
4027 if (in_decl && (complain & tf_error))
4029 error ("type/value mismatch at argument %d in template "
4030 "parameter list for %qD",
4031 i + 1, in_decl);
4032 if (is_tmpl_type)
4033 error (" expected a type, got %qT", DECL_NAME (arg));
4034 else
4035 error (" expected a class template, got %qT", arg);
4037 return error_mark_node;
4040 if (is_type)
4042 if (requires_tmpl_type)
4044 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4045 /* The number of argument required is not known yet.
4046 Just accept it for now. */
4047 val = TREE_TYPE (arg);
4048 else
4050 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4051 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4053 if (coerce_template_template_parms (parmparm, argparm,
4054 complain, in_decl,
4055 args))
4057 val = arg;
4059 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4060 TEMPLATE_DECL. */
4061 if (val != error_mark_node
4062 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4063 val = TREE_TYPE (val);
4065 else
4067 if (in_decl && (complain & tf_error))
4069 error ("type/value mismatch at argument %d in "
4070 "template parameter list for %qD",
4071 i + 1, in_decl);
4072 error (" expected a template of type %qD, got %qD",
4073 parm, arg);
4076 val = error_mark_node;
4080 else
4081 val = arg;
4082 /* We only form one instance of each template specialization.
4083 Therefore, if we use a non-canonical variant (i.e., a
4084 typedef), any future messages referring to the type will use
4085 the typedef, which is confusing if those future uses do not
4086 themselves also use the typedef. */
4087 if (TYPE_P (val))
4088 val = canonical_type_variant (val);
4090 else
4092 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4094 if (invalid_nontype_parm_type_p (t, complain))
4095 return error_mark_node;
4097 if (!uses_template_parms (arg) && !uses_template_parms (t))
4098 /* We used to call digest_init here. However, digest_init
4099 will report errors, which we don't want when complain
4100 is zero. More importantly, digest_init will try too
4101 hard to convert things: for example, `0' should not be
4102 converted to pointer type at this point according to
4103 the standard. Accepting this is not merely an
4104 extension, since deciding whether or not these
4105 conversions can occur is part of determining which
4106 function template to call, or whether a given explicit
4107 argument specification is valid. */
4108 val = convert_nontype_argument (t, arg);
4109 else
4110 val = arg;
4112 if (val == NULL_TREE)
4113 val = error_mark_node;
4114 else if (val == error_mark_node && (complain & tf_error))
4115 error ("could not convert template argument %qE to %qT", arg, t);
4118 return val;
4121 /* Convert all template arguments to their appropriate types, and
4122 return a vector containing the innermost resulting template
4123 arguments. If any error occurs, return error_mark_node. Error and
4124 warning messages are issued under control of COMPLAIN.
4126 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4127 for arguments not specified in ARGS. Otherwise, if
4128 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4129 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4130 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4131 ARGS. */
4133 static tree
4134 coerce_template_parms (tree parms,
4135 tree args,
4136 tree in_decl,
4137 tsubst_flags_t complain,
4138 bool require_all_args,
4139 bool use_default_args)
4141 int nparms, nargs, i, lost = 0;
4142 tree inner_args;
4143 tree new_args;
4144 tree new_inner_args;
4145 bool saved_skip_evaluation;
4147 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4148 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4149 nparms = TREE_VEC_LENGTH (parms);
4151 if (nargs > nparms
4152 || (nargs < nparms
4153 && require_all_args
4154 && (!use_default_args
4155 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4156 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
4158 if (complain & tf_error)
4160 error ("wrong number of template arguments (%d, should be %d)",
4161 nargs, nparms);
4163 if (in_decl)
4164 error ("provided for %q+D", in_decl);
4167 return error_mark_node;
4170 /* We need to evaluate the template arguments, even though this
4171 template-id may be nested within a "sizeof". */
4172 saved_skip_evaluation = skip_evaluation;
4173 skip_evaluation = false;
4174 new_inner_args = make_tree_vec (nparms);
4175 new_args = add_outermost_template_args (args, new_inner_args);
4176 for (i = 0; i < nparms; i++)
4178 tree arg;
4179 tree parm;
4181 /* Get the Ith template parameter. */
4182 parm = TREE_VEC_ELT (parms, i);
4184 if (parm == error_mark_node)
4186 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
4187 continue;
4190 /* Calculate the Ith argument. */
4191 if (i < nargs)
4192 arg = TREE_VEC_ELT (inner_args, i);
4193 else if (require_all_args)
4194 /* There must be a default arg in this case. */
4195 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4196 complain, in_decl);
4197 else
4198 break;
4200 gcc_assert (arg);
4201 if (arg == error_mark_node)
4203 if (complain & tf_error)
4204 error ("template argument %d is invalid", i + 1);
4206 else
4207 arg = convert_template_argument (TREE_VALUE (parm),
4208 arg, new_args, complain, i,
4209 in_decl);
4211 if (arg == error_mark_node)
4212 lost++;
4213 TREE_VEC_ELT (new_inner_args, i) = arg;
4215 skip_evaluation = saved_skip_evaluation;
4217 if (lost)
4218 return error_mark_node;
4220 return new_inner_args;
4223 /* Returns 1 if template args OT and NT are equivalent. */
4225 static int
4226 template_args_equal (tree ot, tree nt)
4228 if (nt == ot)
4229 return 1;
4231 if (TREE_CODE (nt) == TREE_VEC)
4232 /* For member templates */
4233 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4234 else if (TYPE_P (nt))
4235 return TYPE_P (ot) && same_type_p (ot, nt);
4236 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4237 return 0;
4238 else
4239 return cp_tree_equal (ot, nt);
4242 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4243 of template arguments. Returns 0 otherwise. */
4246 comp_template_args (tree oldargs, tree newargs)
4248 int i;
4250 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4251 return 0;
4253 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4255 tree nt = TREE_VEC_ELT (newargs, i);
4256 tree ot = TREE_VEC_ELT (oldargs, i);
4258 if (! template_args_equal (ot, nt))
4259 return 0;
4261 return 1;
4264 /* Given class template name and parameter list, produce a user-friendly name
4265 for the instantiation. */
4267 static char *
4268 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4270 static struct obstack scratch_obstack;
4271 static char *scratch_firstobj;
4272 int i, nparms;
4274 if (!scratch_firstobj)
4275 gcc_obstack_init (&scratch_obstack);
4276 else
4277 obstack_free (&scratch_obstack, scratch_firstobj);
4278 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4280 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4281 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4283 cat (name);
4284 ccat ('<');
4285 nparms = TREE_VEC_LENGTH (parms);
4286 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4287 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4288 for (i = 0; i < nparms; i++)
4290 tree parm;
4291 tree arg;
4293 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4294 arg = TREE_VEC_ELT (arglist, i);
4296 if (parm == error_mark_node)
4297 continue;
4299 if (i)
4300 ccat (',');
4302 if (TREE_CODE (parm) == TYPE_DECL)
4304 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4305 continue;
4307 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4309 if (TREE_CODE (arg) == TEMPLATE_DECL)
4311 /* Already substituted with real template. Just output
4312 the template name here */
4313 tree context = DECL_CONTEXT (arg);
4314 if (context)
4316 /* The template may be defined in a namespace, or
4317 may be a member template. */
4318 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4319 || CLASS_TYPE_P (context));
4320 cat (decl_as_string (DECL_CONTEXT (arg),
4321 TFF_PLAIN_IDENTIFIER));
4322 cat ("::");
4324 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4326 else
4327 /* Output the parameter declaration. */
4328 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4329 continue;
4331 else
4332 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4334 /* No need to check arglist against parmlist here; we did that
4335 in coerce_template_parms, called from lookup_template_class. */
4336 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4339 char *bufp = obstack_next_free (&scratch_obstack);
4340 int offset = 0;
4341 while (bufp[offset - 1] == ' ')
4342 offset--;
4343 obstack_blank_fast (&scratch_obstack, offset);
4345 /* B<C<char> >, not B<C<char>> */
4346 if (bufp[offset - 1] == '>')
4347 ccat (' ');
4349 ccat ('>');
4350 ccat ('\0');
4351 return (char *) obstack_base (&scratch_obstack);
4354 static tree
4355 classtype_mangled_name (tree t)
4357 if (CLASSTYPE_TEMPLATE_INFO (t)
4358 /* Specializations have already had their names set up in
4359 lookup_template_class. */
4360 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4362 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4364 /* For non-primary templates, the template parameters are
4365 implicit from their surrounding context. */
4366 if (PRIMARY_TEMPLATE_P (tmpl))
4368 tree name = DECL_NAME (tmpl);
4369 char *mangled_name = mangle_class_name_for_template
4370 (IDENTIFIER_POINTER (name),
4371 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4372 CLASSTYPE_TI_ARGS (t));
4373 tree id = get_identifier (mangled_name);
4374 IDENTIFIER_TEMPLATE (id) = name;
4375 return id;
4379 return TYPE_IDENTIFIER (t);
4382 static void
4383 add_pending_template (tree d)
4385 tree ti = (TYPE_P (d)
4386 ? CLASSTYPE_TEMPLATE_INFO (d)
4387 : DECL_TEMPLATE_INFO (d));
4388 tree pt;
4389 int level;
4391 if (TI_PENDING_TEMPLATE_FLAG (ti))
4392 return;
4394 /* We are called both from instantiate_decl, where we've already had a
4395 tinst_level pushed, and instantiate_template, where we haven't.
4396 Compensate. */
4397 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4399 if (level)
4400 push_tinst_level (d);
4402 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4403 if (last_pending_template)
4404 TREE_CHAIN (last_pending_template) = pt;
4405 else
4406 pending_templates = pt;
4408 last_pending_template = pt;
4410 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4412 if (level)
4413 pop_tinst_level ();
4417 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4418 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4419 documentation for TEMPLATE_ID_EXPR. */
4421 tree
4422 lookup_template_function (tree fns, tree arglist)
4424 tree type;
4426 if (fns == error_mark_node || arglist == error_mark_node)
4427 return error_mark_node;
4429 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4430 gcc_assert (fns && (is_overloaded_fn (fns)
4431 || TREE_CODE (fns) == IDENTIFIER_NODE));
4433 if (BASELINK_P (fns))
4435 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4436 unknown_type_node,
4437 BASELINK_FUNCTIONS (fns),
4438 arglist);
4439 return fns;
4442 type = TREE_TYPE (fns);
4443 if (TREE_CODE (fns) == OVERLOAD || !type)
4444 type = unknown_type_node;
4446 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4449 /* Within the scope of a template class S<T>, the name S gets bound
4450 (in build_self_reference) to a TYPE_DECL for the class, not a
4451 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4452 or one of its enclosing classes, and that type is a template,
4453 return the associated TEMPLATE_DECL. Otherwise, the original
4454 DECL is returned. */
4456 tree
4457 maybe_get_template_decl_from_type_decl (tree decl)
4459 return (decl != NULL_TREE
4460 && TREE_CODE (decl) == TYPE_DECL
4461 && DECL_ARTIFICIAL (decl)
4462 && CLASS_TYPE_P (TREE_TYPE (decl))
4463 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4464 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4467 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4468 parameters, find the desired type.
4470 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4472 IN_DECL, if non-NULL, is the template declaration we are trying to
4473 instantiate.
4475 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4476 the class we are looking up.
4478 Issue error and warning messages under control of COMPLAIN.
4480 If the template class is really a local class in a template
4481 function, then the FUNCTION_CONTEXT is the function in which it is
4482 being instantiated.
4484 ??? Note that this function is currently called *twice* for each
4485 template-id: the first time from the parser, while creating the
4486 incomplete type (finish_template_type), and the second type during the
4487 real instantiation (instantiate_template_class). This is surely something
4488 that we want to avoid. It also causes some problems with argument
4489 coercion (see convert_nontype_argument for more information on this). */
4491 tree
4492 lookup_template_class (tree d1,
4493 tree arglist,
4494 tree in_decl,
4495 tree context,
4496 int entering_scope,
4497 tsubst_flags_t complain)
4499 tree template = NULL_TREE, parmlist;
4500 tree t;
4502 timevar_push (TV_NAME_LOOKUP);
4504 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4506 tree value = innermost_non_namespace_value (d1);
4507 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4508 template = value;
4509 else
4511 if (context)
4512 push_decl_namespace (context);
4513 template = lookup_name (d1);
4514 template = maybe_get_template_decl_from_type_decl (template);
4515 if (context)
4516 pop_decl_namespace ();
4518 if (template)
4519 context = DECL_CONTEXT (template);
4521 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4523 tree type = TREE_TYPE (d1);
4525 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4526 an implicit typename for the second A. Deal with it. */
4527 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4528 type = TREE_TYPE (type);
4530 if (CLASSTYPE_TEMPLATE_INFO (type))
4532 template = CLASSTYPE_TI_TEMPLATE (type);
4533 d1 = DECL_NAME (template);
4536 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4537 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4539 template = TYPE_TI_TEMPLATE (d1);
4540 d1 = DECL_NAME (template);
4542 else if (TREE_CODE (d1) == TEMPLATE_DECL
4543 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4545 template = d1;
4546 d1 = DECL_NAME (template);
4547 context = DECL_CONTEXT (template);
4550 /* Issue an error message if we didn't find a template. */
4551 if (! template)
4553 if (complain & tf_error)
4554 error ("%qT is not a template", d1);
4555 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4558 if (TREE_CODE (template) != TEMPLATE_DECL
4559 /* Make sure it's a user visible template, if it was named by
4560 the user. */
4561 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4562 && !PRIMARY_TEMPLATE_P (template)))
4564 if (complain & tf_error)
4566 error ("non-template type %qT used as a template", d1);
4567 if (in_decl)
4568 error ("for template declaration %q+D", in_decl);
4570 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4573 complain &= ~tf_user;
4575 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4577 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4578 template arguments */
4580 tree parm;
4581 tree arglist2;
4583 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4585 /* Consider an example where a template template parameter declared as
4587 template <class T, class U = std::allocator<T> > class TT
4589 The template parameter level of T and U are one level larger than
4590 of TT. To proper process the default argument of U, say when an
4591 instantiation `TT<int>' is seen, we need to build the full
4592 arguments containing {int} as the innermost level. Outer levels,
4593 available when not appearing as default template argument, can be
4594 obtained from `current_template_args ()'.
4596 Suppose that TT is later substituted with std::vector. The above
4597 instantiation is `TT<int, std::allocator<T> >' with TT at
4598 level 1, and T at level 2, while the template arguments at level 1
4599 becomes {std::vector} and the inner level 2 is {int}. */
4601 if (current_template_parms)
4602 arglist = add_to_template_args (current_template_args (), arglist);
4604 arglist2 = coerce_template_parms (parmlist, arglist, template,
4605 complain,
4606 /*require_all_args=*/true,
4607 /*use_default_args=*/true);
4608 if (arglist2 == error_mark_node
4609 || (!uses_template_parms (arglist2)
4610 && check_instantiated_args (template, arglist2, complain)))
4611 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4613 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4614 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4616 else
4618 tree template_type = TREE_TYPE (template);
4619 tree gen_tmpl;
4620 tree type_decl;
4621 tree found = NULL_TREE;
4622 int arg_depth;
4623 int parm_depth;
4624 int is_partial_instantiation;
4626 gen_tmpl = most_general_template (template);
4627 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4628 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4629 arg_depth = TMPL_ARGS_DEPTH (arglist);
4631 if (arg_depth == 1 && parm_depth > 1)
4633 /* We've been given an incomplete set of template arguments.
4634 For example, given:
4636 template <class T> struct S1 {
4637 template <class U> struct S2 {};
4638 template <class U> struct S2<U*> {};
4641 we will be called with an ARGLIST of `U*', but the
4642 TEMPLATE will be `template <class T> template
4643 <class U> struct S1<T>::S2'. We must fill in the missing
4644 arguments. */
4645 arglist
4646 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4647 arglist);
4648 arg_depth = TMPL_ARGS_DEPTH (arglist);
4651 /* Now we should have enough arguments. */
4652 gcc_assert (parm_depth == arg_depth);
4654 /* From here on, we're only interested in the most general
4655 template. */
4656 template = gen_tmpl;
4658 /* Calculate the BOUND_ARGS. These will be the args that are
4659 actually tsubst'd into the definition to create the
4660 instantiation. */
4661 if (parm_depth > 1)
4663 /* We have multiple levels of arguments to coerce, at once. */
4664 int i;
4665 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4667 tree bound_args = make_tree_vec (parm_depth);
4669 for (i = saved_depth,
4670 t = DECL_TEMPLATE_PARMS (template);
4671 i > 0 && t != NULL_TREE;
4672 --i, t = TREE_CHAIN (t))
4674 tree a = coerce_template_parms (TREE_VALUE (t),
4675 arglist, template,
4676 complain,
4677 /*require_all_args=*/true,
4678 /*use_default_args=*/true);
4680 /* Don't process further if one of the levels fails. */
4681 if (a == error_mark_node)
4683 /* Restore the ARGLIST to its full size. */
4684 TREE_VEC_LENGTH (arglist) = saved_depth;
4685 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4688 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4690 /* We temporarily reduce the length of the ARGLIST so
4691 that coerce_template_parms will see only the arguments
4692 corresponding to the template parameters it is
4693 examining. */
4694 TREE_VEC_LENGTH (arglist)--;
4697 /* Restore the ARGLIST to its full size. */
4698 TREE_VEC_LENGTH (arglist) = saved_depth;
4700 arglist = bound_args;
4702 else
4703 arglist
4704 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4705 INNERMOST_TEMPLATE_ARGS (arglist),
4706 template,
4707 complain,
4708 /*require_all_args=*/true,
4709 /*use_default_args=*/true);
4711 if (arglist == error_mark_node)
4712 /* We were unable to bind the arguments. */
4713 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4715 /* In the scope of a template class, explicit references to the
4716 template class refer to the type of the template, not any
4717 instantiation of it. For example, in:
4719 template <class T> class C { void f(C<T>); }
4721 the `C<T>' is just the same as `C'. Outside of the
4722 class, however, such a reference is an instantiation. */
4723 if (comp_template_args (TYPE_TI_ARGS (template_type),
4724 arglist))
4726 found = template_type;
4728 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4730 tree ctx;
4732 for (ctx = current_class_type;
4733 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4734 ctx = (TYPE_P (ctx)
4735 ? TYPE_CONTEXT (ctx)
4736 : DECL_CONTEXT (ctx)))
4737 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4738 goto found_ctx;
4740 /* We're not in the scope of the class, so the
4741 TEMPLATE_TYPE is not the type we want after all. */
4742 found = NULL_TREE;
4743 found_ctx:;
4746 if (found)
4747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4749 /* If we already have this specialization, return it. */
4750 found = retrieve_specialization (template, arglist,
4751 /*class_specializations_p=*/false);
4752 if (found)
4753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4755 /* This type is a "partial instantiation" if any of the template
4756 arguments still involve template parameters. Note that we set
4757 IS_PARTIAL_INSTANTIATION for partial specializations as
4758 well. */
4759 is_partial_instantiation = uses_template_parms (arglist);
4761 /* If the deduced arguments are invalid, then the binding
4762 failed. */
4763 if (!is_partial_instantiation
4764 && check_instantiated_args (template,
4765 INNERMOST_TEMPLATE_ARGS (arglist),
4766 complain))
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4769 if (!is_partial_instantiation
4770 && !PRIMARY_TEMPLATE_P (template)
4771 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4773 found = xref_tag_from_type (TREE_TYPE (template),
4774 DECL_NAME (template),
4775 /*tag_scope=*/ts_global);
4776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4779 context = tsubst (DECL_CONTEXT (template), arglist,
4780 complain, in_decl);
4781 if (!context)
4782 context = global_namespace;
4784 /* Create the type. */
4785 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4787 if (!is_partial_instantiation)
4789 set_current_access_from_decl (TYPE_NAME (template_type));
4790 t = start_enum (TYPE_IDENTIFIER (template_type));
4792 else
4793 /* We don't want to call start_enum for this type, since
4794 the values for the enumeration constants may involve
4795 template parameters. And, no one should be interested
4796 in the enumeration constants for such a type. */
4797 t = make_node (ENUMERAL_TYPE);
4799 else
4801 t = make_aggr_type (TREE_CODE (template_type));
4802 CLASSTYPE_DECLARED_CLASS (t)
4803 = CLASSTYPE_DECLARED_CLASS (template_type);
4804 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4805 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4807 /* A local class. Make sure the decl gets registered properly. */
4808 if (context == current_function_decl)
4809 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4812 /* If we called start_enum or pushtag above, this information
4813 will already be set up. */
4814 if (!TYPE_NAME (t))
4816 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4818 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4819 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4820 TYPE_STUB_DECL (t) = type_decl;
4821 DECL_SOURCE_LOCATION (type_decl)
4822 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4824 else
4825 type_decl = TYPE_NAME (t);
4827 TREE_PRIVATE (type_decl)
4828 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4829 TREE_PROTECTED (type_decl)
4830 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4831 DECL_IN_SYSTEM_HEADER (type_decl)
4832 = DECL_IN_SYSTEM_HEADER (template);
4833 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4835 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4836 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4839 /* Set up the template information. We have to figure out which
4840 template is the immediate parent if this is a full
4841 instantiation. */
4842 if (parm_depth == 1 || is_partial_instantiation
4843 || !PRIMARY_TEMPLATE_P (template))
4844 /* This case is easy; there are no member templates involved. */
4845 found = template;
4846 else
4848 /* This is a full instantiation of a member template. Look
4849 for a partial instantiation of which this is an instance. */
4851 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4852 found; found = TREE_CHAIN (found))
4854 int success;
4855 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4857 /* We only want partial instantiations, here, not
4858 specializations or full instantiations. */
4859 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4860 || !uses_template_parms (TREE_VALUE (found)))
4861 continue;
4863 /* Temporarily reduce by one the number of levels in the
4864 ARGLIST and in FOUND so as to avoid comparing the
4865 last set of arguments. */
4866 TREE_VEC_LENGTH (arglist)--;
4867 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4869 /* See if the arguments match. If they do, then TMPL is
4870 the partial instantiation we want. */
4871 success = comp_template_args (TREE_PURPOSE (found), arglist);
4873 /* Restore the argument vectors to their full size. */
4874 TREE_VEC_LENGTH (arglist)++;
4875 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4877 if (success)
4879 found = tmpl;
4880 break;
4884 if (!found)
4886 /* There was no partial instantiation. This happens
4887 where C<T> is a member template of A<T> and it's used
4888 in something like
4890 template <typename T> struct B { A<T>::C<int> m; };
4891 B<float>;
4893 Create the partial instantiation.
4895 TREE_VEC_LENGTH (arglist)--;
4896 found = tsubst (template, arglist, complain, NULL_TREE);
4897 TREE_VEC_LENGTH (arglist)++;
4901 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4902 DECL_TEMPLATE_INSTANTIATIONS (template)
4903 = tree_cons (arglist, t,
4904 DECL_TEMPLATE_INSTANTIATIONS (template));
4906 if (TREE_CODE (t) == ENUMERAL_TYPE
4907 && !is_partial_instantiation)
4908 /* Now that the type has been registered on the instantiations
4909 list, we set up the enumerators. Because the enumeration
4910 constants may involve the enumeration type itself, we make
4911 sure to register the type first, and then create the
4912 constants. That way, doing tsubst_expr for the enumeration
4913 constants won't result in recursive calls here; we'll find
4914 the instantiation and exit above. */
4915 tsubst_enum (template_type, t, arglist);
4917 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4918 is set up. */
4919 if (TREE_CODE (t) != ENUMERAL_TYPE)
4920 DECL_NAME (type_decl) = classtype_mangled_name (t);
4921 if (is_partial_instantiation)
4922 /* If the type makes use of template parameters, the
4923 code that generates debugging information will crash. */
4924 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4926 /* Possibly limit visibility based on template args. */
4927 TREE_PUBLIC (type_decl) = 1;
4928 determine_visibility (type_decl);
4930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4932 timevar_pop (TV_NAME_LOOKUP);
4935 struct pair_fn_data
4937 tree_fn_t fn;
4938 void *data;
4939 struct pointer_set_t *visited;
4942 /* Called from for_each_template_parm via walk_tree. */
4944 static tree
4945 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4947 tree t = *tp;
4948 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4949 tree_fn_t fn = pfd->fn;
4950 void *data = pfd->data;
4952 if (TYPE_P (t)
4953 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4954 return error_mark_node;
4956 switch (TREE_CODE (t))
4958 case RECORD_TYPE:
4959 if (TYPE_PTRMEMFUNC_P (t))
4960 break;
4961 /* Fall through. */
4963 case UNION_TYPE:
4964 case ENUMERAL_TYPE:
4965 if (!TYPE_TEMPLATE_INFO (t))
4966 *walk_subtrees = 0;
4967 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4968 fn, data, pfd->visited))
4969 return error_mark_node;
4970 break;
4972 case INTEGER_TYPE:
4973 if (for_each_template_parm (TYPE_MIN_VALUE (t),
4974 fn, data, pfd->visited)
4975 || for_each_template_parm (TYPE_MAX_VALUE (t),
4976 fn, data, pfd->visited))
4977 return error_mark_node;
4978 break;
4980 case METHOD_TYPE:
4981 /* Since we're not going to walk subtrees, we have to do this
4982 explicitly here. */
4983 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4984 pfd->visited))
4985 return error_mark_node;
4986 /* Fall through. */
4988 case FUNCTION_TYPE:
4989 /* Check the return type. */
4990 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4991 return error_mark_node;
4993 /* Check the parameter types. Since default arguments are not
4994 instantiated until they are needed, the TYPE_ARG_TYPES may
4995 contain expressions that involve template parameters. But,
4996 no-one should be looking at them yet. And, once they're
4997 instantiated, they don't contain template parameters, so
4998 there's no point in looking at them then, either. */
5000 tree parm;
5002 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5003 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5004 pfd->visited))
5005 return error_mark_node;
5007 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5008 want walk_tree walking into them itself. */
5009 *walk_subtrees = 0;
5011 break;
5013 case TYPEOF_TYPE:
5014 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5015 pfd->visited))
5016 return error_mark_node;
5017 break;
5019 case FUNCTION_DECL:
5020 case VAR_DECL:
5021 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5022 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5023 pfd->visited))
5024 return error_mark_node;
5025 /* Fall through. */
5027 case PARM_DECL:
5028 case CONST_DECL:
5029 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5030 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5031 pfd->visited))
5032 return error_mark_node;
5033 if (DECL_CONTEXT (t)
5034 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5035 pfd->visited))
5036 return error_mark_node;
5037 break;
5039 case BOUND_TEMPLATE_TEMPLATE_PARM:
5040 /* Record template parameters such as `T' inside `TT<T>'. */
5041 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5042 return error_mark_node;
5043 /* Fall through. */
5045 case TEMPLATE_TEMPLATE_PARM:
5046 case TEMPLATE_TYPE_PARM:
5047 case TEMPLATE_PARM_INDEX:
5048 if (fn && (*fn)(t, data))
5049 return error_mark_node;
5050 else if (!fn)
5051 return error_mark_node;
5052 break;
5054 case TEMPLATE_DECL:
5055 /* A template template parameter is encountered. */
5056 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5057 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5058 return error_mark_node;
5060 /* Already substituted template template parameter */
5061 *walk_subtrees = 0;
5062 break;
5064 case TYPENAME_TYPE:
5065 if (!fn
5066 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5067 data, pfd->visited))
5068 return error_mark_node;
5069 break;
5071 case CONSTRUCTOR:
5072 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5073 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5074 (TREE_TYPE (t)), fn, data,
5075 pfd->visited))
5076 return error_mark_node;
5077 break;
5079 case INDIRECT_REF:
5080 case COMPONENT_REF:
5081 /* If there's no type, then this thing must be some expression
5082 involving template parameters. */
5083 if (!fn && !TREE_TYPE (t))
5084 return error_mark_node;
5085 break;
5087 case MODOP_EXPR:
5088 case CAST_EXPR:
5089 case REINTERPRET_CAST_EXPR:
5090 case CONST_CAST_EXPR:
5091 case STATIC_CAST_EXPR:
5092 case DYNAMIC_CAST_EXPR:
5093 case ARROW_EXPR:
5094 case DOTSTAR_EXPR:
5095 case TYPEID_EXPR:
5096 case PSEUDO_DTOR_EXPR:
5097 if (!fn)
5098 return error_mark_node;
5099 break;
5101 case BASELINK:
5102 /* If we do not handle this case specially, we end up walking
5103 the BINFO hierarchy, which is circular, and therefore
5104 confuses walk_tree. */
5105 *walk_subtrees = 0;
5106 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5107 pfd->visited))
5108 return error_mark_node;
5109 break;
5111 default:
5112 break;
5115 /* We didn't find any template parameters we liked. */
5116 return NULL_TREE;
5119 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5120 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5121 call FN with the parameter and the DATA.
5122 If FN returns nonzero, the iteration is terminated, and
5123 for_each_template_parm returns 1. Otherwise, the iteration
5124 continues. If FN never returns a nonzero value, the value
5125 returned by for_each_template_parm is 0. If FN is NULL, it is
5126 considered to be the function which always returns 1. */
5128 static int
5129 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5130 struct pointer_set_t *visited)
5132 struct pair_fn_data pfd;
5133 int result;
5135 /* Set up. */
5136 pfd.fn = fn;
5137 pfd.data = data;
5139 /* Walk the tree. (Conceptually, we would like to walk without
5140 duplicates, but for_each_template_parm_r recursively calls
5141 for_each_template_parm, so we would need to reorganize a fair
5142 bit to use walk_tree_without_duplicates, so we keep our own
5143 visited list.) */
5144 if (visited)
5145 pfd.visited = visited;
5146 else
5147 pfd.visited = pointer_set_create ();
5148 result = walk_tree (&t,
5149 for_each_template_parm_r,
5150 &pfd,
5151 pfd.visited) != NULL_TREE;
5153 /* Clean up. */
5154 if (!visited)
5156 pointer_set_destroy (pfd.visited);
5157 pfd.visited = 0;
5160 return result;
5163 /* Returns true if T depends on any template parameter. */
5166 uses_template_parms (tree t)
5168 bool dependent_p;
5169 int saved_processing_template_decl;
5171 saved_processing_template_decl = processing_template_decl;
5172 if (!saved_processing_template_decl)
5173 processing_template_decl = 1;
5174 if (TYPE_P (t))
5175 dependent_p = dependent_type_p (t);
5176 else if (TREE_CODE (t) == TREE_VEC)
5177 dependent_p = any_dependent_template_arguments_p (t);
5178 else if (TREE_CODE (t) == TREE_LIST)
5179 dependent_p = (uses_template_parms (TREE_VALUE (t))
5180 || uses_template_parms (TREE_CHAIN (t)));
5181 else if (TREE_CODE (t) == TYPE_DECL)
5182 dependent_p = dependent_type_p (TREE_TYPE (t));
5183 else if (DECL_P (t)
5184 || EXPR_P (t)
5185 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5186 || TREE_CODE (t) == OVERLOAD
5187 || TREE_CODE (t) == BASELINK
5188 || TREE_CODE (t) == IDENTIFIER_NODE
5189 || CONSTANT_CLASS_P (t))
5190 dependent_p = (type_dependent_expression_p (t)
5191 || value_dependent_expression_p (t));
5192 else
5194 gcc_assert (t == error_mark_node);
5195 dependent_p = false;
5198 processing_template_decl = saved_processing_template_decl;
5200 return dependent_p;
5203 /* Returns true if T depends on any template parameter with level LEVEL. */
5206 uses_template_parms_level (tree t, int level)
5208 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5211 static int tinst_depth;
5212 extern int max_tinst_depth;
5213 #ifdef GATHER_STATISTICS
5214 int depth_reached;
5215 #endif
5216 static int tinst_level_tick;
5217 static int last_template_error_tick;
5219 /* We're starting to instantiate D; record the template instantiation context
5220 for diagnostics and to restore it later. */
5222 static int
5223 push_tinst_level (tree d)
5225 tree new;
5227 if (tinst_depth >= max_tinst_depth)
5229 /* If the instantiation in question still has unbound template parms,
5230 we don't really care if we can't instantiate it, so just return.
5231 This happens with base instantiation for implicit `typename'. */
5232 if (uses_template_parms (d))
5233 return 0;
5235 last_template_error_tick = tinst_level_tick;
5236 error ("template instantiation depth exceeds maximum of %d (use "
5237 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5238 max_tinst_depth, d);
5240 print_instantiation_context ();
5242 return 0;
5245 new = make_node (TINST_LEVEL);
5246 TINST_DECL (new) = d;
5247 TINST_LOCATION (new) = input_location;
5248 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5249 TREE_CHAIN (new) = current_tinst_level;
5250 current_tinst_level = new;
5252 ++tinst_depth;
5253 #ifdef GATHER_STATISTICS
5254 if (tinst_depth > depth_reached)
5255 depth_reached = tinst_depth;
5256 #endif
5258 ++tinst_level_tick;
5259 return 1;
5262 /* We're done instantiating this template; return to the instantiation
5263 context. */
5265 static void
5266 pop_tinst_level (void)
5268 tree old = current_tinst_level;
5270 /* Restore the filename and line number stashed away when we started
5271 this instantiation. */
5272 input_location = TINST_LOCATION (old);
5273 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5274 current_tinst_level = TREE_CHAIN (old);
5275 --tinst_depth;
5276 ++tinst_level_tick;
5279 /* We're instantiating a deferred template; restore the template
5280 instantiation context in which the instantiation was requested, which
5281 is one step out from LEVEL. */
5283 static void
5284 reopen_tinst_level (tree level)
5286 tree t;
5288 tinst_depth = 0;
5289 for (t = level; t; t = TREE_CHAIN (t))
5290 ++tinst_depth;
5292 current_tinst_level = level;
5293 pop_tinst_level ();
5296 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5297 vector of template arguments, as for tsubst.
5299 Returns an appropriate tsubst'd friend declaration. */
5301 static tree
5302 tsubst_friend_function (tree decl, tree args)
5304 tree new_friend;
5306 if (TREE_CODE (decl) == FUNCTION_DECL
5307 && DECL_TEMPLATE_INSTANTIATION (decl)
5308 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5309 /* This was a friend declared with an explicit template
5310 argument list, e.g.:
5312 friend void f<>(T);
5314 to indicate that f was a template instantiation, not a new
5315 function declaration. Now, we have to figure out what
5316 instantiation of what template. */
5318 tree template_id, arglist, fns;
5319 tree new_args;
5320 tree tmpl;
5321 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5323 /* Friend functions are looked up in the containing namespace scope.
5324 We must enter that scope, to avoid finding member functions of the
5325 current cless with same name. */
5326 push_nested_namespace (ns);
5327 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5328 tf_warning_or_error, NULL_TREE,
5329 /*integral_constant_expression_p=*/false);
5330 pop_nested_namespace (ns);
5331 arglist = tsubst (DECL_TI_ARGS (decl), args,
5332 tf_warning_or_error, NULL_TREE);
5333 template_id = lookup_template_function (fns, arglist);
5335 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5336 tmpl = determine_specialization (template_id, new_friend,
5337 &new_args,
5338 /*need_member_template=*/0,
5339 TREE_VEC_LENGTH (args),
5340 tsk_none);
5341 return instantiate_template (tmpl, new_args, tf_error);
5344 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5346 /* The NEW_FRIEND will look like an instantiation, to the
5347 compiler, but is not an instantiation from the point of view of
5348 the language. For example, we might have had:
5350 template <class T> struct S {
5351 template <class U> friend void f(T, U);
5354 Then, in S<int>, template <class U> void f(int, U) is not an
5355 instantiation of anything. */
5356 if (new_friend == error_mark_node)
5357 return error_mark_node;
5359 DECL_USE_TEMPLATE (new_friend) = 0;
5360 if (TREE_CODE (decl) == TEMPLATE_DECL)
5362 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5363 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5364 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5367 /* The mangled name for the NEW_FRIEND is incorrect. The function
5368 is not a template instantiation and should not be mangled like
5369 one. Therefore, we forget the mangling here; we'll recompute it
5370 later if we need it. */
5371 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5373 SET_DECL_RTL (new_friend, NULL_RTX);
5374 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5377 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5379 tree old_decl;
5380 tree new_friend_template_info;
5381 tree new_friend_result_template_info;
5382 tree ns;
5383 int new_friend_is_defn;
5385 /* We must save some information from NEW_FRIEND before calling
5386 duplicate decls since that function will free NEW_FRIEND if
5387 possible. */
5388 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5389 new_friend_is_defn =
5390 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5391 (template_for_substitution (new_friend)))
5392 != NULL_TREE);
5393 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5395 /* This declaration is a `primary' template. */
5396 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5398 new_friend_result_template_info
5399 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5401 else
5402 new_friend_result_template_info = NULL_TREE;
5404 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5405 if (new_friend_is_defn)
5406 DECL_INITIAL (new_friend) = error_mark_node;
5408 /* Inside pushdecl_namespace_level, we will push into the
5409 current namespace. However, the friend function should go
5410 into the namespace of the template. */
5411 ns = decl_namespace_context (new_friend);
5412 push_nested_namespace (ns);
5413 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5414 pop_nested_namespace (ns);
5416 if (old_decl == error_mark_node)
5417 return error_mark_node;
5419 if (old_decl != new_friend)
5421 /* This new friend declaration matched an existing
5422 declaration. For example, given:
5424 template <class T> void f(T);
5425 template <class U> class C {
5426 template <class T> friend void f(T) {}
5429 the friend declaration actually provides the definition
5430 of `f', once C has been instantiated for some type. So,
5431 old_decl will be the out-of-class template declaration,
5432 while new_friend is the in-class definition.
5434 But, if `f' was called before this point, the
5435 instantiation of `f' will have DECL_TI_ARGS corresponding
5436 to `T' but not to `U', references to which might appear
5437 in the definition of `f'. Previously, the most general
5438 template for an instantiation of `f' was the out-of-class
5439 version; now it is the in-class version. Therefore, we
5440 run through all specialization of `f', adding to their
5441 DECL_TI_ARGS appropriately. In particular, they need a
5442 new set of outer arguments, corresponding to the
5443 arguments for this class instantiation.
5445 The same situation can arise with something like this:
5447 friend void f(int);
5448 template <class T> class C {
5449 friend void f(T) {}
5452 when `C<int>' is instantiated. Now, `f(int)' is defined
5453 in the class. */
5455 if (!new_friend_is_defn)
5456 /* On the other hand, if the in-class declaration does
5457 *not* provide a definition, then we don't want to alter
5458 existing definitions. We can just leave everything
5459 alone. */
5461 else
5463 /* Overwrite whatever template info was there before, if
5464 any, with the new template information pertaining to
5465 the declaration. */
5466 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5468 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5469 reregister_specialization (new_friend,
5470 most_general_template (old_decl),
5471 old_decl);
5472 else
5474 tree t;
5475 tree new_friend_args;
5477 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5478 = new_friend_result_template_info;
5480 new_friend_args = TI_ARGS (new_friend_template_info);
5481 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5482 t != NULL_TREE;
5483 t = TREE_CHAIN (t))
5485 tree spec = TREE_VALUE (t);
5487 DECL_TI_ARGS (spec)
5488 = add_outermost_template_args (new_friend_args,
5489 DECL_TI_ARGS (spec));
5492 /* Now, since specializations are always supposed to
5493 hang off of the most general template, we must move
5494 them. */
5495 t = most_general_template (old_decl);
5496 if (t != old_decl)
5498 DECL_TEMPLATE_SPECIALIZATIONS (t)
5499 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5500 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5501 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5506 /* The information from NEW_FRIEND has been merged into OLD_DECL
5507 by duplicate_decls. */
5508 new_friend = old_decl;
5511 else
5513 tree context = DECL_CONTEXT (new_friend);
5514 bool dependent_p;
5516 /* In the code
5517 template <class T> class C {
5518 template <class U> friend void C1<U>::f (); // case 1
5519 friend void C2<T>::f (); // case 2
5521 we only need to make sure CONTEXT is a complete type for
5522 case 2. To distinguish between the two cases, we note that
5523 CONTEXT of case 1 remains dependent type after tsubst while
5524 this isn't true for case 2. */
5525 ++processing_template_decl;
5526 dependent_p = dependent_type_p (context);
5527 --processing_template_decl;
5529 if (!dependent_p
5530 && !complete_type_or_else (context, NULL_TREE))
5531 return error_mark_node;
5533 if (COMPLETE_TYPE_P (context))
5535 /* Check to see that the declaration is really present, and,
5536 possibly obtain an improved declaration. */
5537 tree fn = check_classfn (context,
5538 new_friend, NULL_TREE);
5540 if (fn)
5541 new_friend = fn;
5545 return new_friend;
5548 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5549 template arguments, as for tsubst.
5551 Returns an appropriate tsubst'd friend type or error_mark_node on
5552 failure. */
5554 static tree
5555 tsubst_friend_class (tree friend_tmpl, tree args)
5557 tree friend_type;
5558 tree tmpl;
5559 tree context;
5561 context = DECL_CONTEXT (friend_tmpl);
5563 if (context)
5565 if (TREE_CODE (context) == NAMESPACE_DECL)
5566 push_nested_namespace (context);
5567 else
5568 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5571 /* Look for a class template declaration. We look for hidden names
5572 because two friend declarations of the same template are the
5573 same. For example, in:
5575 struct A {
5576 template <typename> friend class F;
5578 template <typename> struct B {
5579 template <typename> friend class F;
5582 both F templates are the same. */
5583 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5584 /*block_p=*/true, 0,
5585 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
5587 /* But, if we don't find one, it might be because we're in a
5588 situation like this:
5590 template <class T>
5591 struct S {
5592 template <class U>
5593 friend struct S;
5596 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5597 for `S<int>', not the TEMPLATE_DECL. */
5598 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5600 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5601 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5604 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5606 /* The friend template has already been declared. Just
5607 check to see that the declarations match, and install any new
5608 default parameters. We must tsubst the default parameters,
5609 of course. We only need the innermost template parameters
5610 because that is all that redeclare_class_template will look
5611 at. */
5612 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5613 > TMPL_ARGS_DEPTH (args))
5615 tree parms;
5616 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5617 args, tf_warning_or_error);
5618 redeclare_class_template (TREE_TYPE (tmpl), parms);
5621 friend_type = TREE_TYPE (tmpl);
5623 else
5625 /* The friend template has not already been declared. In this
5626 case, the instantiation of the template class will cause the
5627 injection of this template into the global scope. */
5628 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5629 if (tmpl == error_mark_node)
5630 return error_mark_node;
5632 /* The new TMPL is not an instantiation of anything, so we
5633 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5634 the new type because that is supposed to be the corresponding
5635 template decl, i.e., TMPL. */
5636 DECL_USE_TEMPLATE (tmpl) = 0;
5637 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5638 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5639 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5640 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5642 /* Inject this template into the global scope. */
5643 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5646 if (context)
5648 if (TREE_CODE (context) == NAMESPACE_DECL)
5649 pop_nested_namespace (context);
5650 else
5651 pop_nested_class ();
5654 return friend_type;
5657 /* Returns zero if TYPE cannot be completed later due to circularity.
5658 Otherwise returns one. */
5660 static int
5661 can_complete_type_without_circularity (tree type)
5663 if (type == NULL_TREE || type == error_mark_node)
5664 return 0;
5665 else if (COMPLETE_TYPE_P (type))
5666 return 1;
5667 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5668 return can_complete_type_without_circularity (TREE_TYPE (type));
5669 else if (CLASS_TYPE_P (type)
5670 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5671 return 0;
5672 else
5673 return 1;
5676 tree
5677 instantiate_class_template (tree type)
5679 tree template, args, pattern, t, member;
5680 tree typedecl;
5681 tree pbinfo;
5682 tree base_list;
5684 if (type == error_mark_node)
5685 return error_mark_node;
5687 if (TYPE_BEING_DEFINED (type)
5688 || COMPLETE_TYPE_P (type)
5689 || dependent_type_p (type))
5690 return type;
5692 /* Figure out which template is being instantiated. */
5693 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5694 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5696 /* Determine what specialization of the original template to
5697 instantiate. */
5698 t = most_specialized_class (type, template);
5699 if (t == error_mark_node)
5701 TYPE_BEING_DEFINED (type) = 1;
5702 return error_mark_node;
5704 else if (t)
5706 /* This TYPE is actually an instantiation of a partial
5707 specialization. We replace the innermost set of ARGS with
5708 the arguments appropriate for substitution. For example,
5709 given:
5711 template <class T> struct S {};
5712 template <class T> struct S<T*> {};
5714 and supposing that we are instantiating S<int*>, ARGS will
5715 presently be {int*} -- but we need {int}. */
5716 pattern = TREE_TYPE (t);
5717 args = TREE_PURPOSE (t);
5719 else
5721 pattern = TREE_TYPE (template);
5722 args = CLASSTYPE_TI_ARGS (type);
5725 /* If the template we're instantiating is incomplete, then clearly
5726 there's nothing we can do. */
5727 if (!COMPLETE_TYPE_P (pattern))
5728 return type;
5730 /* If we've recursively instantiated too many templates, stop. */
5731 if (! push_tinst_level (type))
5732 return type;
5734 /* Now we're really doing the instantiation. Mark the type as in
5735 the process of being defined. */
5736 TYPE_BEING_DEFINED (type) = 1;
5738 /* We may be in the middle of deferred access check. Disable
5739 it now. */
5740 push_deferring_access_checks (dk_no_deferred);
5742 push_to_top_level ();
5744 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5746 /* Set the input location to the template definition. This is needed
5747 if tsubsting causes an error. */
5748 typedecl = TYPE_MAIN_DECL (type);
5749 input_location = DECL_SOURCE_LOCATION (typedecl);
5750 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5752 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5753 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5754 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5755 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5756 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5757 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5758 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5759 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5760 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5761 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5762 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5763 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5764 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5765 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5766 if (ANON_AGGR_TYPE_P (pattern))
5767 SET_ANON_AGGR_TYPE_P (type);
5768 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5770 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5771 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5774 pbinfo = TYPE_BINFO (pattern);
5776 /* We should never instantiate a nested class before its enclosing
5777 class; we need to look up the nested class by name before we can
5778 instantiate it, and that lookup should instantiate the enclosing
5779 class. */
5780 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5781 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5782 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5784 base_list = NULL_TREE;
5785 if (BINFO_N_BASE_BINFOS (pbinfo))
5787 tree pbase_binfo;
5788 tree context = TYPE_CONTEXT (type);
5789 tree pushed_scope;
5790 int i;
5792 /* We must enter the scope containing the type, as that is where
5793 the accessibility of types named in dependent bases are
5794 looked up from. */
5795 pushed_scope = push_scope (context ? context : global_namespace);
5797 /* Substitute into each of the bases to determine the actual
5798 basetypes. */
5799 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5801 tree base;
5802 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5804 /* Substitute to figure out the base class. */
5805 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5806 if (base == error_mark_node)
5807 continue;
5809 base_list = tree_cons (access, base, base_list);
5810 if (BINFO_VIRTUAL_P (pbase_binfo))
5811 TREE_TYPE (base_list) = integer_type_node;
5814 /* The list is now in reverse order; correct that. */
5815 base_list = nreverse (base_list);
5817 if (pushed_scope)
5818 pop_scope (pushed_scope);
5820 /* Now call xref_basetypes to set up all the base-class
5821 information. */
5822 xref_basetypes (type, base_list);
5825 /* Now that our base classes are set up, enter the scope of the
5826 class, so that name lookups into base classes, etc. will work
5827 correctly. This is precisely analogous to what we do in
5828 begin_class_definition when defining an ordinary non-template
5829 class. */
5830 pushclass (type);
5832 /* Now members are processed in the order of declaration. */
5833 for (member = CLASSTYPE_DECL_LIST (pattern);
5834 member; member = TREE_CHAIN (member))
5836 tree t = TREE_VALUE (member);
5838 if (TREE_PURPOSE (member))
5840 if (TYPE_P (t))
5842 /* Build new CLASSTYPE_NESTED_UTDS. */
5844 tree newtag;
5845 bool class_template_p;
5847 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5848 && TYPE_LANG_SPECIFIC (t)
5849 && CLASSTYPE_IS_TEMPLATE (t));
5850 /* If the member is a class template, then -- even after
5851 substitution -- there may be dependent types in the
5852 template argument list for the class. We increment
5853 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5854 that function will assume that no types are dependent
5855 when outside of a template. */
5856 if (class_template_p)
5857 ++processing_template_decl;
5858 newtag = tsubst (t, args, tf_error, NULL_TREE);
5859 if (class_template_p)
5860 --processing_template_decl;
5861 if (newtag == error_mark_node)
5862 continue;
5864 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5866 tree name = TYPE_IDENTIFIER (t);
5868 if (class_template_p)
5869 /* Unfortunately, lookup_template_class sets
5870 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5871 instantiation (i.e., for the type of a member
5872 template class nested within a template class.)
5873 This behavior is required for
5874 maybe_process_partial_specialization to work
5875 correctly, but is not accurate in this case;
5876 the TAG is not an instantiation of anything.
5877 (The corresponding TEMPLATE_DECL is an
5878 instantiation, but the TYPE is not.) */
5879 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5881 /* Now, we call pushtag to put this NEWTAG into the scope of
5882 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5883 pushtag calling push_template_decl. We don't have to do
5884 this for enums because it will already have been done in
5885 tsubst_enum. */
5886 if (name)
5887 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5888 pushtag (name, newtag, /*tag_scope=*/ts_current);
5891 else if (TREE_CODE (t) == FUNCTION_DECL
5892 || DECL_FUNCTION_TEMPLATE_P (t))
5894 /* Build new TYPE_METHODS. */
5895 tree r;
5897 if (TREE_CODE (t) == TEMPLATE_DECL)
5898 ++processing_template_decl;
5899 r = tsubst (t, args, tf_error, NULL_TREE);
5900 if (TREE_CODE (t) == TEMPLATE_DECL)
5901 --processing_template_decl;
5902 set_current_access_from_decl (r);
5903 finish_member_declaration (r);
5905 else
5907 /* Build new TYPE_FIELDS. */
5908 if (TREE_CODE (t) == STATIC_ASSERT)
5910 tree condition =
5911 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
5912 tf_warning_or_error, NULL_TREE,
5913 /*integral_constant_expression_p=*/true);
5914 finish_static_assert (condition,
5915 STATIC_ASSERT_MESSAGE (t),
5916 STATIC_ASSERT_SOURCE_LOCATION (t),
5917 /*member_p=*/true);
5919 else if (TREE_CODE (t) != CONST_DECL)
5921 tree r;
5923 /* The the file and line for this declaration, to
5924 assist in error message reporting. Since we
5925 called push_tinst_level above, we don't need to
5926 restore these. */
5927 input_location = DECL_SOURCE_LOCATION (t);
5929 if (TREE_CODE (t) == TEMPLATE_DECL)
5930 ++processing_template_decl;
5931 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5932 if (TREE_CODE (t) == TEMPLATE_DECL)
5933 --processing_template_decl;
5934 if (TREE_CODE (r) == VAR_DECL)
5936 /* In [temp.inst]:
5938 [t]he initialization (and any associated
5939 side-effects) of a static data member does
5940 not occur unless the static data member is
5941 itself used in a way that requires the
5942 definition of the static data member to
5943 exist.
5945 Therefore, we do not substitute into the
5946 initialized for the static data member here. */
5947 finish_static_data_member_decl
5949 /*init=*/NULL_TREE,
5950 /*init_const_expr_p=*/false,
5951 /*asmspec_tree=*/NULL_TREE,
5952 /*flags=*/0);
5953 if (DECL_INITIALIZED_IN_CLASS_P (r))
5954 check_static_variable_definition (r, TREE_TYPE (r));
5956 else if (TREE_CODE (r) == FIELD_DECL)
5958 /* Determine whether R has a valid type and can be
5959 completed later. If R is invalid, then it is
5960 replaced by error_mark_node so that it will not be
5961 added to TYPE_FIELDS. */
5962 tree rtype = TREE_TYPE (r);
5963 if (can_complete_type_without_circularity (rtype))
5964 complete_type (rtype);
5966 if (!COMPLETE_TYPE_P (rtype))
5968 cxx_incomplete_type_error (r, rtype);
5969 r = error_mark_node;
5973 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5974 such a thing will already have been added to the field
5975 list by tsubst_enum in finish_member_declaration in the
5976 CLASSTYPE_NESTED_UTDS case above. */
5977 if (!(TREE_CODE (r) == TYPE_DECL
5978 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5979 && DECL_ARTIFICIAL (r)))
5981 set_current_access_from_decl (r);
5982 finish_member_declaration (r);
5987 else
5989 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5991 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5993 tree friend_type = t;
5994 bool adjust_processing_template_decl = false;
5996 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5998 /* template <class T> friend class C; */
5999 friend_type = tsubst_friend_class (friend_type, args);
6000 adjust_processing_template_decl = true;
6002 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6004 /* template <class T> friend class C::D; */
6005 friend_type = tsubst (friend_type, args,
6006 tf_warning_or_error, NULL_TREE);
6007 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6008 friend_type = TREE_TYPE (friend_type);
6009 adjust_processing_template_decl = true;
6011 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6013 /* This could be either
6015 friend class T::C;
6017 when dependent_type_p is false or
6019 template <class U> friend class T::C;
6021 otherwise. */
6022 friend_type = tsubst (friend_type, args,
6023 tf_warning_or_error, NULL_TREE);
6024 /* Bump processing_template_decl for correct
6025 dependent_type_p calculation. */
6026 ++processing_template_decl;
6027 if (dependent_type_p (friend_type))
6028 adjust_processing_template_decl = true;
6029 --processing_template_decl;
6031 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6032 && hidden_name_p (TYPE_NAME (friend_type)))
6034 /* friend class C;
6036 where C hasn't been declared yet. Let's lookup name
6037 from namespace scope directly, bypassing any name that
6038 come from dependent base class. */
6039 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6041 /* The call to xref_tag_from_type does injection for friend
6042 classes. */
6043 push_nested_namespace (ns);
6044 friend_type =
6045 xref_tag_from_type (friend_type, NULL_TREE,
6046 /*tag_scope=*/ts_current);
6047 pop_nested_namespace (ns);
6049 else if (uses_template_parms (friend_type))
6050 /* friend class C<T>; */
6051 friend_type = tsubst (friend_type, args,
6052 tf_warning_or_error, NULL_TREE);
6053 /* Otherwise it's
6055 friend class C;
6057 where C is already declared or
6059 friend class C<int>;
6061 We don't have to do anything in these cases. */
6063 if (adjust_processing_template_decl)
6064 /* Trick make_friend_class into realizing that the friend
6065 we're adding is a template, not an ordinary class. It's
6066 important that we use make_friend_class since it will
6067 perform some error-checking and output cross-reference
6068 information. */
6069 ++processing_template_decl;
6071 if (friend_type != error_mark_node)
6072 make_friend_class (type, friend_type, /*complain=*/false);
6074 if (adjust_processing_template_decl)
6075 --processing_template_decl;
6077 else
6079 /* Build new DECL_FRIENDLIST. */
6080 tree r;
6082 /* The the file and line for this declaration, to
6083 assist in error message reporting. Since we
6084 called push_tinst_level above, we don't need to
6085 restore these. */
6086 input_location = DECL_SOURCE_LOCATION (t);
6088 if (TREE_CODE (t) == TEMPLATE_DECL)
6090 ++processing_template_decl;
6091 push_deferring_access_checks (dk_no_check);
6094 r = tsubst_friend_function (t, args);
6095 add_friend (type, r, /*complain=*/false);
6096 if (TREE_CODE (t) == TEMPLATE_DECL)
6098 pop_deferring_access_checks ();
6099 --processing_template_decl;
6105 /* Set the file and line number information to whatever is given for
6106 the class itself. This puts error messages involving generated
6107 implicit functions at a predictable point, and the same point
6108 that would be used for non-template classes. */
6109 input_location = DECL_SOURCE_LOCATION (typedecl);
6111 unreverse_member_declarations (type);
6112 finish_struct_1 (type);
6113 TYPE_BEING_DEFINED (type) = 0;
6115 /* Now that the class is complete, instantiate default arguments for
6116 any member functions. We don't do this earlier because the
6117 default arguments may reference members of the class. */
6118 if (!PRIMARY_TEMPLATE_P (template))
6119 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6120 if (TREE_CODE (t) == FUNCTION_DECL
6121 /* Implicitly generated member functions will not have template
6122 information; they are not instantiations, but instead are
6123 created "fresh" for each instantiation. */
6124 && DECL_TEMPLATE_INFO (t))
6125 tsubst_default_arguments (t);
6127 popclass ();
6128 pop_from_top_level ();
6129 pop_deferring_access_checks ();
6130 pop_tinst_level ();
6132 /* The vtable for a template class can be emitted in any translation
6133 unit in which the class is instantiated. When there is no key
6134 method, however, finish_struct_1 will already have added TYPE to
6135 the keyed_classes list. */
6136 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6137 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6139 return type;
6142 static tree
6143 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6145 tree r;
6147 if (!t)
6148 r = t;
6149 else if (TYPE_P (t))
6150 r = tsubst (t, args, complain, in_decl);
6151 else
6153 r = tsubst_expr (t, args, complain, in_decl,
6154 /*integral_constant_expression_p=*/true);
6155 r = fold_non_dependent_expr (r);
6157 return r;
6160 /* Substitute ARGS into the vector or list of template arguments T. */
6162 static tree
6163 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6165 int len = TREE_VEC_LENGTH (t);
6166 int need_new = 0, i;
6167 tree *elts = (tree *) alloca (len * sizeof (tree));
6169 for (i = 0; i < len; i++)
6171 tree orig_arg = TREE_VEC_ELT (t, i);
6172 tree new_arg;
6174 if (TREE_CODE (orig_arg) == TREE_VEC)
6175 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
6176 else
6177 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
6179 if (new_arg == error_mark_node)
6180 return error_mark_node;
6182 elts[i] = new_arg;
6183 if (new_arg != orig_arg)
6184 need_new = 1;
6187 if (!need_new)
6188 return t;
6190 t = make_tree_vec (len);
6191 for (i = 0; i < len; i++)
6192 TREE_VEC_ELT (t, i) = elts[i];
6194 return t;
6197 /* Return the result of substituting ARGS into the template parameters
6198 given by PARMS. If there are m levels of ARGS and m + n levels of
6199 PARMS, then the result will contain n levels of PARMS. For
6200 example, if PARMS is `template <class T> template <class U>
6201 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6202 result will be `template <int*, double, class V>'. */
6204 static tree
6205 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6207 tree r = NULL_TREE;
6208 tree* new_parms;
6210 /* When substituting into a template, we must set
6211 PROCESSING_TEMPLATE_DECL as the template parameters may be
6212 dependent if they are based on one-another, and the dependency
6213 predicates are short-circuit outside of templates. */
6214 ++processing_template_decl;
6216 for (new_parms = &r;
6217 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6218 new_parms = &(TREE_CHAIN (*new_parms)),
6219 parms = TREE_CHAIN (parms))
6221 tree new_vec =
6222 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6223 int i;
6225 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6227 tree tuple;
6228 tree default_value;
6229 tree parm_decl;
6231 if (parms == error_mark_node)
6232 continue;
6234 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6236 if (tuple == error_mark_node)
6237 continue;
6239 default_value = TREE_PURPOSE (tuple);
6240 parm_decl = TREE_VALUE (tuple);
6242 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6243 if (TREE_CODE (parm_decl) == PARM_DECL
6244 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6245 parm_decl = error_mark_node;
6246 default_value = tsubst_template_arg (default_value, args,
6247 complain, NULL_TREE);
6249 tuple = build_tree_list (default_value, parm_decl);
6250 TREE_VEC_ELT (new_vec, i) = tuple;
6253 *new_parms =
6254 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6255 - TMPL_ARGS_DEPTH (args)),
6256 new_vec, NULL_TREE);
6259 --processing_template_decl;
6261 return r;
6264 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6265 type T. If T is not an aggregate or enumeration type, it is
6266 handled as if by tsubst. IN_DECL is as for tsubst. If
6267 ENTERING_SCOPE is nonzero, T is the context for a template which
6268 we are presently tsubst'ing. Return the substituted value. */
6270 static tree
6271 tsubst_aggr_type (tree t,
6272 tree args,
6273 tsubst_flags_t complain,
6274 tree in_decl,
6275 int entering_scope)
6277 if (t == NULL_TREE)
6278 return NULL_TREE;
6280 switch (TREE_CODE (t))
6282 case RECORD_TYPE:
6283 if (TYPE_PTRMEMFUNC_P (t))
6284 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6286 /* Else fall through. */
6287 case ENUMERAL_TYPE:
6288 case UNION_TYPE:
6289 if (TYPE_TEMPLATE_INFO (t))
6291 tree argvec;
6292 tree context;
6293 tree r;
6294 bool saved_skip_evaluation;
6296 /* In "sizeof(X<I>)" we need to evaluate "I". */
6297 saved_skip_evaluation = skip_evaluation;
6298 skip_evaluation = false;
6300 /* First, determine the context for the type we are looking
6301 up. */
6302 context = TYPE_CONTEXT (t);
6303 if (context)
6304 context = tsubst_aggr_type (context, args, complain,
6305 in_decl, /*entering_scope=*/1);
6307 /* Then, figure out what arguments are appropriate for the
6308 type we are trying to find. For example, given:
6310 template <class T> struct S;
6311 template <class T, class U> void f(T, U) { S<U> su; }
6313 and supposing that we are instantiating f<int, double>,
6314 then our ARGS will be {int, double}, but, when looking up
6315 S we only want {double}. */
6316 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6317 complain, in_decl);
6318 if (argvec == error_mark_node)
6319 r = error_mark_node;
6320 else
6322 r = lookup_template_class (t, argvec, in_decl, context,
6323 entering_scope, complain);
6324 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6327 skip_evaluation = saved_skip_evaluation;
6329 return r;
6331 else
6332 /* This is not a template type, so there's nothing to do. */
6333 return t;
6335 default:
6336 return tsubst (t, args, complain, in_decl);
6340 /* Substitute into the default argument ARG (a default argument for
6341 FN), which has the indicated TYPE. */
6343 tree
6344 tsubst_default_argument (tree fn, tree type, tree arg)
6346 tree saved_class_ptr = NULL_TREE;
6347 tree saved_class_ref = NULL_TREE;
6349 /* This default argument came from a template. Instantiate the
6350 default argument here, not in tsubst. In the case of
6351 something like:
6353 template <class T>
6354 struct S {
6355 static T t();
6356 void f(T = t());
6359 we must be careful to do name lookup in the scope of S<T>,
6360 rather than in the current class. */
6361 push_access_scope (fn);
6362 /* The "this" pointer is not valid in a default argument. */
6363 if (cfun)
6365 saved_class_ptr = current_class_ptr;
6366 cp_function_chain->x_current_class_ptr = NULL_TREE;
6367 saved_class_ref = current_class_ref;
6368 cp_function_chain->x_current_class_ref = NULL_TREE;
6371 push_deferring_access_checks(dk_no_deferred);
6372 /* The default argument expression may cause implicitly defined
6373 member functions to be synthesized, which will result in garbage
6374 collection. We must treat this situation as if we were within
6375 the body of function so as to avoid collecting live data on the
6376 stack. */
6377 ++function_depth;
6378 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6379 tf_warning_or_error, NULL_TREE,
6380 /*integral_constant_expression_p=*/false);
6381 --function_depth;
6382 pop_deferring_access_checks();
6384 /* Restore the "this" pointer. */
6385 if (cfun)
6387 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6388 cp_function_chain->x_current_class_ref = saved_class_ref;
6391 pop_access_scope (fn);
6393 /* Make sure the default argument is reasonable. */
6394 arg = check_default_argument (type, arg);
6396 return arg;
6399 /* Substitute into all the default arguments for FN. */
6401 static void
6402 tsubst_default_arguments (tree fn)
6404 tree arg;
6405 tree tmpl_args;
6407 tmpl_args = DECL_TI_ARGS (fn);
6409 /* If this function is not yet instantiated, we certainly don't need
6410 its default arguments. */
6411 if (uses_template_parms (tmpl_args))
6412 return;
6414 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6415 arg;
6416 arg = TREE_CHAIN (arg))
6417 if (TREE_PURPOSE (arg))
6418 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6419 TREE_VALUE (arg),
6420 TREE_PURPOSE (arg));
6423 /* Substitute the ARGS into the T, which is a _DECL. Return the
6424 result of the substitution. Issue error and warning messages under
6425 control of COMPLAIN. */
6427 static tree
6428 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6430 location_t saved_loc;
6431 tree r = NULL_TREE;
6432 tree in_decl = t;
6434 /* Set the filename and linenumber to improve error-reporting. */
6435 saved_loc = input_location;
6436 input_location = DECL_SOURCE_LOCATION (t);
6438 switch (TREE_CODE (t))
6440 case TEMPLATE_DECL:
6442 /* We can get here when processing a member function template,
6443 member class template, and template template parameter of
6444 a template class. */
6445 tree decl = DECL_TEMPLATE_RESULT (t);
6446 tree spec;
6447 tree tmpl_args;
6448 tree full_args;
6450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6452 /* Template template parameter is treated here. */
6453 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6454 if (new_type == error_mark_node)
6455 return error_mark_node;
6457 r = copy_decl (t);
6458 TREE_CHAIN (r) = NULL_TREE;
6459 TREE_TYPE (r) = new_type;
6460 DECL_TEMPLATE_RESULT (r)
6461 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6462 DECL_TEMPLATE_PARMS (r)
6463 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6464 complain);
6465 TYPE_NAME (new_type) = r;
6466 break;
6469 /* We might already have an instance of this template.
6470 The ARGS are for the surrounding class type, so the
6471 full args contain the tsubst'd args for the context,
6472 plus the innermost args from the template decl. */
6473 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6474 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6475 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6476 /* Because this is a template, the arguments will still be
6477 dependent, even after substitution. If
6478 PROCESSING_TEMPLATE_DECL is not set, the dependency
6479 predicates will short-circuit. */
6480 ++processing_template_decl;
6481 full_args = tsubst_template_args (tmpl_args, args,
6482 complain, in_decl);
6483 --processing_template_decl;
6484 if (full_args == error_mark_node)
6485 return error_mark_node;
6487 /* tsubst_template_args doesn't copy the vector if
6488 nothing changed. But, *something* should have
6489 changed. */
6490 gcc_assert (full_args != tmpl_args);
6492 spec = retrieve_specialization (t, full_args,
6493 /*class_specializations_p=*/true);
6494 if (spec != NULL_TREE)
6496 r = spec;
6497 break;
6500 /* Make a new template decl. It will be similar to the
6501 original, but will record the current template arguments.
6502 We also create a new function declaration, which is just
6503 like the old one, but points to this new template, rather
6504 than the old one. */
6505 r = copy_decl (t);
6506 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6507 TREE_CHAIN (r) = NULL_TREE;
6509 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6511 if (TREE_CODE (decl) == TYPE_DECL)
6513 tree new_type;
6514 ++processing_template_decl;
6515 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6516 --processing_template_decl;
6517 if (new_type == error_mark_node)
6518 return error_mark_node;
6520 TREE_TYPE (r) = new_type;
6521 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6522 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6523 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6524 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6526 else
6528 tree new_decl;
6529 ++processing_template_decl;
6530 new_decl = tsubst (decl, args, complain, in_decl);
6531 --processing_template_decl;
6532 if (new_decl == error_mark_node)
6533 return error_mark_node;
6535 DECL_TEMPLATE_RESULT (r) = new_decl;
6536 DECL_TI_TEMPLATE (new_decl) = r;
6537 TREE_TYPE (r) = TREE_TYPE (new_decl);
6538 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6539 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6542 SET_DECL_IMPLICIT_INSTANTIATION (r);
6543 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6544 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6546 /* The template parameters for this new template are all the
6547 template parameters for the old template, except the
6548 outermost level of parameters. */
6549 DECL_TEMPLATE_PARMS (r)
6550 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6551 complain);
6553 if (PRIMARY_TEMPLATE_P (t))
6554 DECL_PRIMARY_TEMPLATE (r) = r;
6556 if (TREE_CODE (decl) != TYPE_DECL)
6557 /* Record this non-type partial instantiation. */
6558 register_specialization (r, t,
6559 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6560 false);
6562 break;
6564 case FUNCTION_DECL:
6566 tree ctx;
6567 tree argvec = NULL_TREE;
6568 tree *friends;
6569 tree gen_tmpl;
6570 tree type;
6571 int member;
6572 int args_depth;
6573 int parms_depth;
6575 /* Nobody should be tsubst'ing into non-template functions. */
6576 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6578 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6580 tree spec;
6581 bool dependent_p;
6583 /* If T is not dependent, just return it. We have to
6584 increment PROCESSING_TEMPLATE_DECL because
6585 value_dependent_expression_p assumes that nothing is
6586 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6587 ++processing_template_decl;
6588 dependent_p = value_dependent_expression_p (t);
6589 --processing_template_decl;
6590 if (!dependent_p)
6591 return t;
6593 /* Calculate the most general template of which R is a
6594 specialization, and the complete set of arguments used to
6595 specialize R. */
6596 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6597 argvec = tsubst_template_args (DECL_TI_ARGS
6598 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6599 args, complain, in_decl);
6601 /* Check to see if we already have this specialization. */
6602 spec = retrieve_specialization (gen_tmpl, argvec,
6603 /*class_specializations_p=*/false);
6605 if (spec)
6607 r = spec;
6608 break;
6611 /* We can see more levels of arguments than parameters if
6612 there was a specialization of a member template, like
6613 this:
6615 template <class T> struct S { template <class U> void f(); }
6616 template <> template <class U> void S<int>::f(U);
6618 Here, we'll be substituting into the specialization,
6619 because that's where we can find the code we actually
6620 want to generate, but we'll have enough arguments for
6621 the most general template.
6623 We also deal with the peculiar case:
6625 template <class T> struct S {
6626 template <class U> friend void f();
6628 template <class U> void f() {}
6629 template S<int>;
6630 template void f<double>();
6632 Here, the ARGS for the instantiation of will be {int,
6633 double}. But, we only need as many ARGS as there are
6634 levels of template parameters in CODE_PATTERN. We are
6635 careful not to get fooled into reducing the ARGS in
6636 situations like:
6638 template <class T> struct S { template <class U> void f(U); }
6639 template <class T> template <> void S<T>::f(int) {}
6641 which we can spot because the pattern will be a
6642 specialization in this case. */
6643 args_depth = TMPL_ARGS_DEPTH (args);
6644 parms_depth =
6645 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6646 if (args_depth > parms_depth
6647 && !DECL_TEMPLATE_SPECIALIZATION (t))
6648 args = get_innermost_template_args (args, parms_depth);
6650 else
6652 /* This special case arises when we have something like this:
6654 template <class T> struct S {
6655 friend void f<int>(int, double);
6658 Here, the DECL_TI_TEMPLATE for the friend declaration
6659 will be an IDENTIFIER_NODE. We are being called from
6660 tsubst_friend_function, and we want only to create a
6661 new decl (R) with appropriate types so that we can call
6662 determine_specialization. */
6663 gen_tmpl = NULL_TREE;
6666 if (DECL_CLASS_SCOPE_P (t))
6668 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6669 member = 2;
6670 else
6671 member = 1;
6672 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6673 complain, t, /*entering_scope=*/1);
6675 else
6677 member = 0;
6678 ctx = DECL_CONTEXT (t);
6680 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6681 if (type == error_mark_node)
6682 return error_mark_node;
6684 /* We do NOT check for matching decls pushed separately at this
6685 point, as they may not represent instantiations of this
6686 template, and in any case are considered separate under the
6687 discrete model. */
6688 r = copy_decl (t);
6689 DECL_USE_TEMPLATE (r) = 0;
6690 TREE_TYPE (r) = type;
6691 /* Clear out the mangled name and RTL for the instantiation. */
6692 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6693 SET_DECL_RTL (r, NULL_RTX);
6694 DECL_INITIAL (r) = NULL_TREE;
6695 DECL_CONTEXT (r) = ctx;
6697 if (member && DECL_CONV_FN_P (r))
6698 /* Type-conversion operator. Reconstruct the name, in
6699 case it's the name of one of the template's parameters. */
6700 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6702 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6703 complain, t);
6704 DECL_RESULT (r) = NULL_TREE;
6706 TREE_STATIC (r) = 0;
6707 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6708 DECL_EXTERNAL (r) = 1;
6709 /* If this is an instantiation of a function with internal
6710 linkage, we already know what object file linkage will be
6711 assigned to the instantiation. */
6712 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6713 DECL_DEFER_OUTPUT (r) = 0;
6714 TREE_CHAIN (r) = NULL_TREE;
6715 DECL_PENDING_INLINE_INFO (r) = 0;
6716 DECL_PENDING_INLINE_P (r) = 0;
6717 DECL_SAVED_TREE (r) = NULL_TREE;
6718 TREE_USED (r) = 0;
6719 if (DECL_CLONED_FUNCTION (r))
6721 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6722 args, complain, t);
6723 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6724 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6727 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6728 this in the special friend case mentioned above where
6729 GEN_TMPL is NULL. */
6730 if (gen_tmpl)
6732 DECL_TEMPLATE_INFO (r)
6733 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6734 SET_DECL_IMPLICIT_INSTANTIATION (r);
6735 register_specialization (r, gen_tmpl, argvec, false);
6737 /* We're not supposed to instantiate default arguments
6738 until they are called, for a template. But, for a
6739 declaration like:
6741 template <class T> void f ()
6742 { extern void g(int i = T()); }
6744 we should do the substitution when the template is
6745 instantiated. We handle the member function case in
6746 instantiate_class_template since the default arguments
6747 might refer to other members of the class. */
6748 if (!member
6749 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6750 && !uses_template_parms (argvec))
6751 tsubst_default_arguments (r);
6753 else
6754 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6756 /* Copy the list of befriending classes. */
6757 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6758 *friends;
6759 friends = &TREE_CHAIN (*friends))
6761 *friends = copy_node (*friends);
6762 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6763 args, complain,
6764 in_decl);
6767 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6769 maybe_retrofit_in_chrg (r);
6770 if (DECL_CONSTRUCTOR_P (r))
6771 grok_ctor_properties (ctx, r);
6772 /* If this is an instantiation of a member template, clone it.
6773 If it isn't, that'll be handled by
6774 clone_constructors_and_destructors. */
6775 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6776 clone_function_decl (r, /*update_method_vec_p=*/0);
6778 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6779 && !grok_op_properties (r, (complain & tf_error) != 0))
6780 return error_mark_node;
6782 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6783 SET_DECL_FRIEND_CONTEXT (r,
6784 tsubst (DECL_FRIEND_CONTEXT (t),
6785 args, complain, in_decl));
6787 /* Possibly limit visibility based on template args. */
6788 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
6789 if (DECL_VISIBILITY_SPECIFIED (t))
6791 DECL_VISIBILITY_SPECIFIED (r) = 0;
6792 DECL_ATTRIBUTES (r)
6793 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6795 determine_visibility (r);
6797 break;
6799 case PARM_DECL:
6801 tree type;
6803 r = copy_node (t);
6804 if (DECL_TEMPLATE_PARM_P (t))
6805 SET_DECL_TEMPLATE_PARM_P (r);
6807 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6808 type = type_decays_to (type);
6809 TREE_TYPE (r) = type;
6810 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6812 if (DECL_INITIAL (r))
6814 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6815 DECL_INITIAL (r) = TREE_TYPE (r);
6816 else
6817 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6818 complain, in_decl);
6821 DECL_CONTEXT (r) = NULL_TREE;
6823 if (!DECL_TEMPLATE_PARM_P (r))
6824 DECL_ARG_TYPE (r) = type_passed_as (type);
6825 if (TREE_CHAIN (t))
6826 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6827 complain, TREE_CHAIN (t));
6829 break;
6831 case FIELD_DECL:
6833 tree type;
6835 r = copy_decl (t);
6836 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6837 if (type == error_mark_node)
6838 return error_mark_node;
6839 TREE_TYPE (r) = type;
6840 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6842 /* DECL_INITIAL gives the number of bits in a bit-field. */
6843 DECL_INITIAL (r)
6844 = tsubst_expr (DECL_INITIAL (t), args,
6845 complain, in_decl,
6846 /*integral_constant_expression_p=*/true);
6847 /* We don't have to set DECL_CONTEXT here; it is set by
6848 finish_member_declaration. */
6849 TREE_CHAIN (r) = NULL_TREE;
6850 if (VOID_TYPE_P (type))
6851 error ("instantiation of %q+D as type %qT", r, type);
6853 break;
6855 case USING_DECL:
6856 /* We reach here only for member using decls. */
6857 if (DECL_DEPENDENT_P (t))
6859 r = do_class_using_decl
6860 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6861 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6862 if (!r)
6863 r = error_mark_node;
6865 else
6867 r = copy_node (t);
6868 TREE_CHAIN (r) = NULL_TREE;
6870 break;
6872 case TYPE_DECL:
6873 case VAR_DECL:
6875 tree argvec = NULL_TREE;
6876 tree gen_tmpl = NULL_TREE;
6877 tree spec;
6878 tree tmpl = NULL_TREE;
6879 tree ctx;
6880 tree type = NULL_TREE;
6881 bool local_p;
6883 if (TREE_CODE (t) == TYPE_DECL)
6885 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6886 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6887 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6889 /* If this is the canonical decl, we don't have to
6890 mess with instantiations, and often we can't (for
6891 typename, template type parms and such). Note that
6892 TYPE_NAME is not correct for the above test if
6893 we've copied the type for a typedef. */
6894 r = TYPE_NAME (type);
6895 break;
6899 /* Check to see if we already have the specialization we
6900 need. */
6901 spec = NULL_TREE;
6902 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6904 /* T is a static data member or namespace-scope entity.
6905 We have to substitute into namespace-scope variables
6906 (even though such entities are never templates) because
6907 of cases like:
6909 template <class T> void f() { extern T t; }
6911 where the entity referenced is not known until
6912 instantiation time. */
6913 local_p = false;
6914 ctx = DECL_CONTEXT (t);
6915 if (DECL_CLASS_SCOPE_P (t))
6917 ctx = tsubst_aggr_type (ctx, args,
6918 complain,
6919 in_decl, /*entering_scope=*/1);
6920 /* If CTX is unchanged, then T is in fact the
6921 specialization we want. That situation occurs when
6922 referencing a static data member within in its own
6923 class. We can use pointer equality, rather than
6924 same_type_p, because DECL_CONTEXT is always
6925 canonical. */
6926 if (ctx == DECL_CONTEXT (t))
6927 spec = t;
6930 if (!spec)
6932 tmpl = DECL_TI_TEMPLATE (t);
6933 gen_tmpl = most_general_template (tmpl);
6934 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6935 spec = (retrieve_specialization
6936 (gen_tmpl, argvec,
6937 /*class_specializations_p=*/false));
6940 else
6942 /* A local variable. */
6943 local_p = true;
6944 /* Subsequent calls to pushdecl will fill this in. */
6945 ctx = NULL_TREE;
6946 spec = retrieve_local_specialization (t);
6948 /* If we already have the specialization we need, there is
6949 nothing more to do. */
6950 if (spec)
6952 r = spec;
6953 break;
6956 /* Create a new node for the specialization we need. */
6957 r = copy_decl (t);
6958 if (TREE_CODE (r) == VAR_DECL)
6960 /* Even if the original location is out of scope, the
6961 newly substituted one is not. */
6962 DECL_DEAD_FOR_LOCAL (r) = 0;
6963 DECL_INITIALIZED_P (r) = 0;
6964 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6965 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6966 if (type == error_mark_node)
6967 return error_mark_node;
6968 if (TREE_CODE (type) == FUNCTION_TYPE)
6970 /* It may seem that this case cannot occur, since:
6972 typedef void f();
6973 void g() { f x; }
6975 declares a function, not a variable. However:
6977 typedef void f();
6978 template <typename T> void g() { T t; }
6979 template void g<f>();
6981 is an attempt to declare a variable with function
6982 type. */
6983 error ("variable %qD has function type",
6984 /* R is not yet sufficiently initialized, so we
6985 just use its name. */
6986 DECL_NAME (r));
6987 return error_mark_node;
6989 type = complete_type (type);
6990 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6991 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6992 type = check_var_type (DECL_NAME (r), type);
6994 if (DECL_HAS_VALUE_EXPR_P (t))
6996 tree ve = DECL_VALUE_EXPR (t);
6997 ve = tsubst_expr (ve, args, complain, in_decl,
6998 /*constant_expression_p=*/false);
6999 SET_DECL_VALUE_EXPR (r, ve);
7002 else if (DECL_SELF_REFERENCE_P (t))
7003 SET_DECL_SELF_REFERENCE_P (r);
7004 TREE_TYPE (r) = type;
7005 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7006 DECL_CONTEXT (r) = ctx;
7007 /* Clear out the mangled name and RTL for the instantiation. */
7008 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7009 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7010 SET_DECL_RTL (r, NULL_RTX);
7011 /* The initializer must not be expanded until it is required;
7012 see [temp.inst]. */
7013 DECL_INITIAL (r) = NULL_TREE;
7014 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7015 SET_DECL_RTL (r, NULL_RTX);
7016 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
7017 if (TREE_CODE (r) == VAR_DECL)
7019 /* Possibly limit visibility based on template args. */
7020 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7021 if (DECL_VISIBILITY_SPECIFIED (t))
7023 DECL_VISIBILITY_SPECIFIED (r) = 0;
7024 DECL_ATTRIBUTES (r)
7025 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7027 determine_visibility (r);
7030 if (!local_p)
7032 /* A static data member declaration is always marked
7033 external when it is declared in-class, even if an
7034 initializer is present. We mimic the non-template
7035 processing here. */
7036 DECL_EXTERNAL (r) = 1;
7038 register_specialization (r, gen_tmpl, argvec, false);
7039 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
7040 SET_DECL_IMPLICIT_INSTANTIATION (r);
7042 else
7043 register_local_specialization (r, t);
7045 TREE_CHAIN (r) = NULL_TREE;
7046 layout_decl (r, 0);
7048 break;
7050 default:
7051 gcc_unreachable ();
7054 /* Restore the file and line information. */
7055 input_location = saved_loc;
7057 return r;
7060 /* Substitute into the ARG_TYPES of a function type. */
7062 static tree
7063 tsubst_arg_types (tree arg_types,
7064 tree args,
7065 tsubst_flags_t complain,
7066 tree in_decl)
7068 tree remaining_arg_types;
7069 tree type;
7070 tree default_arg;
7071 tree result = NULL_TREE;
7073 if (!arg_types || arg_types == void_list_node)
7074 return arg_types;
7076 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
7077 args, complain, in_decl);
7078 if (remaining_arg_types == error_mark_node)
7079 return error_mark_node;
7081 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
7082 if (type == error_mark_node)
7083 return error_mark_node;
7084 if (VOID_TYPE_P (type))
7086 if (complain & tf_error)
7088 error ("invalid parameter type %qT", type);
7089 if (in_decl)
7090 error ("in declaration %q+D", in_decl);
7092 return error_mark_node;
7095 /* Do array-to-pointer, function-to-pointer conversion, and ignore
7096 top-level qualifiers as required. */
7097 type = TYPE_MAIN_VARIANT (type_decays_to (type));
7099 /* We do not substitute into default arguments here. The standard
7100 mandates that they be instantiated only when needed, which is
7101 done in build_over_call. */
7102 default_arg = TREE_PURPOSE (arg_types);
7104 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
7106 /* We've instantiated a template before its default arguments
7107 have been parsed. This can happen for a nested template
7108 class, and is not an error unless we require the default
7109 argument in a call of this function. */
7110 result = tree_cons (default_arg, type, remaining_arg_types);
7111 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
7113 else
7114 result = hash_tree_cons (default_arg, type, remaining_arg_types);
7116 return result;
7119 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7120 *not* handle the exception-specification for FNTYPE, because the
7121 initial substitution of explicitly provided template parameters
7122 during argument deduction forbids substitution into the
7123 exception-specification:
7125 [temp.deduct]
7127 All references in the function type of the function template to the
7128 corresponding template parameters are replaced by the specified tem-
7129 plate argument values. If a substitution in a template parameter or
7130 in the function type of the function template results in an invalid
7131 type, type deduction fails. [Note: The equivalent substitution in
7132 exception specifications is done only when the function is instanti-
7133 ated, at which point a program is ill-formed if the substitution
7134 results in an invalid type.] */
7136 static tree
7137 tsubst_function_type (tree t,
7138 tree args,
7139 tsubst_flags_t complain,
7140 tree in_decl)
7142 tree return_type;
7143 tree arg_types;
7144 tree fntype;
7146 /* The TYPE_CONTEXT is not used for function/method types. */
7147 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
7149 /* Substitute the return type. */
7150 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7151 if (return_type == error_mark_node)
7152 return error_mark_node;
7153 /* The standard does not presently indicate that creation of a
7154 function type with an invalid return type is a deduction failure.
7155 However, that is clearly analogous to creating an array of "void"
7156 or a reference to a reference. This is core issue #486. */
7157 if (TREE_CODE (return_type) == ARRAY_TYPE
7158 || TREE_CODE (return_type) == FUNCTION_TYPE)
7160 if (complain & tf_error)
7162 if (TREE_CODE (return_type) == ARRAY_TYPE)
7163 error ("function returning an array");
7164 else
7165 error ("function returning a function");
7167 return error_mark_node;
7170 /* Substitute the argument types. */
7171 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
7172 complain, in_decl);
7173 if (arg_types == error_mark_node)
7174 return error_mark_node;
7176 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
7177 && in_decl != NULL_TREE
7178 && !TREE_NO_WARNING (in_decl)
7179 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
7180 warning (OPT_Wreturn_type,
7181 "type qualifiers ignored on function return type");
7183 /* Construct a new type node and return it. */
7184 if (TREE_CODE (t) == FUNCTION_TYPE)
7185 fntype = build_function_type (return_type, arg_types);
7186 else
7188 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7189 if (! IS_AGGR_TYPE (r))
7191 /* [temp.deduct]
7193 Type deduction may fail for any of the following
7194 reasons:
7196 -- Attempting to create "pointer to member of T" when T
7197 is not a class type. */
7198 if (complain & tf_error)
7199 error ("creating pointer to member function of non-class type %qT",
7201 return error_mark_node;
7204 fntype = build_method_type_directly (r, return_type,
7205 TREE_CHAIN (arg_types));
7207 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
7208 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
7210 return fntype;
7213 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7214 ARGS into that specification, and return the substituted
7215 specification. If there is no specification, return NULL_TREE. */
7217 static tree
7218 tsubst_exception_specification (tree fntype,
7219 tree args,
7220 tsubst_flags_t complain,
7221 tree in_decl)
7223 tree specs;
7224 tree new_specs;
7226 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7227 new_specs = NULL_TREE;
7228 if (specs)
7230 if (! TREE_VALUE (specs))
7231 new_specs = specs;
7232 else
7233 while (specs)
7235 tree spec;
7236 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7237 if (spec == error_mark_node)
7238 return spec;
7239 new_specs = add_exception_specifier (new_specs, spec, complain);
7240 specs = TREE_CHAIN (specs);
7243 return new_specs;
7246 /* Take the tree structure T and replace template parameters used
7247 therein with the argument vector ARGS. IN_DECL is an associated
7248 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
7249 Issue error and warning messages under control of COMPLAIN. Note
7250 that we must be relatively non-tolerant of extensions here, in
7251 order to preserve conformance; if we allow substitutions that
7252 should not be allowed, we may allow argument deductions that should
7253 not succeed, and therefore report ambiguous overload situations
7254 where there are none. In theory, we could allow the substitution,
7255 but indicate that it should have failed, and allow our caller to
7256 make sure that the right thing happens, but we don't try to do this
7257 yet.
7259 This function is used for dealing with types, decls and the like;
7260 for expressions, use tsubst_expr or tsubst_copy. */
7262 static tree
7263 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7265 tree type, r;
7267 if (t == NULL_TREE || t == error_mark_node
7268 || t == integer_type_node
7269 || t == void_type_node
7270 || t == char_type_node
7271 || t == unknown_type_node
7272 || TREE_CODE (t) == NAMESPACE_DECL)
7273 return t;
7275 if (DECL_P (t))
7276 return tsubst_decl (t, args, complain);
7278 if (TREE_CODE (t) == IDENTIFIER_NODE)
7279 type = IDENTIFIER_TYPE_VALUE (t);
7280 else
7281 type = TREE_TYPE (t);
7283 gcc_assert (type != unknown_type_node);
7285 if (type
7286 && TREE_CODE (t) != TYPENAME_TYPE
7287 && TREE_CODE (t) != IDENTIFIER_NODE
7288 && TREE_CODE (t) != FUNCTION_TYPE
7289 && TREE_CODE (t) != METHOD_TYPE)
7290 type = tsubst (type, args, complain, in_decl);
7291 if (type == error_mark_node)
7292 return error_mark_node;
7294 switch (TREE_CODE (t))
7296 case RECORD_TYPE:
7297 case UNION_TYPE:
7298 case ENUMERAL_TYPE:
7299 return tsubst_aggr_type (t, args, complain, in_decl,
7300 /*entering_scope=*/0);
7302 case ERROR_MARK:
7303 case IDENTIFIER_NODE:
7304 case VOID_TYPE:
7305 case REAL_TYPE:
7306 case COMPLEX_TYPE:
7307 case VECTOR_TYPE:
7308 case BOOLEAN_TYPE:
7309 case INTEGER_CST:
7310 case REAL_CST:
7311 case STRING_CST:
7312 return t;
7314 case INTEGER_TYPE:
7315 if (t == integer_type_node)
7316 return t;
7318 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7319 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7320 return t;
7323 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7325 max = tsubst_expr (omax, args, complain, in_decl,
7326 /*integral_constant_expression_p=*/false);
7327 max = fold_decl_constant_value (max);
7329 if (TREE_CODE (max) != INTEGER_CST
7330 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7331 && !at_function_scope_p ())
7333 if (complain & tf_error)
7334 error ("array bound is not an integer constant");
7335 return error_mark_node;
7338 /* [temp.deduct]
7340 Type deduction may fail for any of the following
7341 reasons:
7343 Attempting to create an array with a size that is
7344 zero or negative. */
7345 if (integer_zerop (max) && !(complain & tf_error))
7346 /* We must fail if performing argument deduction (as
7347 indicated by the state of complain), so that
7348 another substitution can be found. */
7349 return error_mark_node;
7350 else if (TREE_CODE (max) == INTEGER_CST
7351 && INT_CST_LT (max, integer_zero_node))
7353 if (complain & tf_error)
7354 error ("creating array with negative size (%qE)", max);
7356 return error_mark_node;
7359 return compute_array_index_type (NULL_TREE, max);
7362 case TEMPLATE_TYPE_PARM:
7363 case TEMPLATE_TEMPLATE_PARM:
7364 case BOUND_TEMPLATE_TEMPLATE_PARM:
7365 case TEMPLATE_PARM_INDEX:
7367 int idx;
7368 int level;
7369 int levels;
7370 tree arg = NULL_TREE;
7372 r = NULL_TREE;
7374 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7375 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7376 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7377 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7379 idx = TEMPLATE_TYPE_IDX (t);
7380 level = TEMPLATE_TYPE_LEVEL (t);
7382 else
7384 idx = TEMPLATE_PARM_IDX (t);
7385 level = TEMPLATE_PARM_LEVEL (t);
7388 levels = TMPL_ARGS_DEPTH (args);
7389 if (level <= levels)
7390 arg = TMPL_ARG (args, level, idx);
7392 if (arg == error_mark_node)
7393 return error_mark_node;
7394 else if (arg != NULL_TREE)
7396 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7398 int quals;
7399 gcc_assert (TYPE_P (arg));
7401 /* cv-quals from the template are discarded when
7402 substituting in a function or reference type. */
7403 if (TREE_CODE (arg) == FUNCTION_TYPE
7404 || TREE_CODE (arg) == METHOD_TYPE
7405 || TREE_CODE (arg) == REFERENCE_TYPE)
7406 quals = cp_type_quals (arg);
7407 else
7408 quals = cp_type_quals (arg) | cp_type_quals (t);
7410 return cp_build_qualified_type_real
7411 (arg, quals, complain | tf_ignore_bad_quals);
7413 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7415 /* We are processing a type constructed from a
7416 template template parameter. */
7417 tree argvec = tsubst (TYPE_TI_ARGS (t),
7418 args, complain, in_decl);
7419 if (argvec == error_mark_node)
7420 return error_mark_node;
7422 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7423 are resolving nested-types in the signature of a
7424 member function templates. Otherwise ARG is a
7425 TEMPLATE_DECL and is the real template to be
7426 instantiated. */
7427 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7428 arg = TYPE_NAME (arg);
7430 r = lookup_template_class (arg,
7431 argvec, in_decl,
7432 DECL_CONTEXT (arg),
7433 /*entering_scope=*/0,
7434 complain);
7435 return cp_build_qualified_type_real
7436 (r, TYPE_QUALS (t), complain);
7438 else
7439 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7440 return arg;
7443 if (level == 1)
7444 /* This can happen during the attempted tsubst'ing in
7445 unify. This means that we don't yet have any information
7446 about the template parameter in question. */
7447 return t;
7449 /* If we get here, we must have been looking at a parm for a
7450 more deeply nested template. Make a new version of this
7451 template parameter, but with a lower level. */
7452 switch (TREE_CODE (t))
7454 case TEMPLATE_TYPE_PARM:
7455 case TEMPLATE_TEMPLATE_PARM:
7456 case BOUND_TEMPLATE_TEMPLATE_PARM:
7457 if (cp_type_quals (t))
7459 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7460 r = cp_build_qualified_type_real
7461 (r, cp_type_quals (t),
7462 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7463 ? tf_ignore_bad_quals : 0));
7465 else
7467 r = copy_type (t);
7468 TEMPLATE_TYPE_PARM_INDEX (r)
7469 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7470 r, levels);
7471 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7472 TYPE_MAIN_VARIANT (r) = r;
7473 TYPE_POINTER_TO (r) = NULL_TREE;
7474 TYPE_REFERENCE_TO (r) = NULL_TREE;
7476 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7478 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7479 complain, in_decl);
7480 if (argvec == error_mark_node)
7481 return error_mark_node;
7483 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7484 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7487 break;
7489 case TEMPLATE_PARM_INDEX:
7490 r = reduce_template_parm_level (t, type, levels);
7491 break;
7493 default:
7494 gcc_unreachable ();
7497 return r;
7500 case TREE_LIST:
7502 tree purpose, value, chain;
7504 if (t == void_list_node)
7505 return t;
7507 purpose = TREE_PURPOSE (t);
7508 if (purpose)
7510 purpose = tsubst (purpose, args, complain, in_decl);
7511 if (purpose == error_mark_node)
7512 return error_mark_node;
7514 value = TREE_VALUE (t);
7515 if (value)
7517 value = tsubst (value, args, complain, in_decl);
7518 if (value == error_mark_node)
7519 return error_mark_node;
7521 chain = TREE_CHAIN (t);
7522 if (chain && chain != void_type_node)
7524 chain = tsubst (chain, args, complain, in_decl);
7525 if (chain == error_mark_node)
7526 return error_mark_node;
7528 if (purpose == TREE_PURPOSE (t)
7529 && value == TREE_VALUE (t)
7530 && chain == TREE_CHAIN (t))
7531 return t;
7532 return hash_tree_cons (purpose, value, chain);
7535 case TREE_BINFO:
7536 /* We should never be tsubsting a binfo. */
7537 gcc_unreachable ();
7539 case TREE_VEC:
7540 /* A vector of template arguments. */
7541 gcc_assert (!type);
7542 return tsubst_template_args (t, args, complain, in_decl);
7544 case POINTER_TYPE:
7545 case REFERENCE_TYPE:
7547 enum tree_code code;
7549 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7550 return t;
7552 code = TREE_CODE (t);
7555 /* [temp.deduct]
7557 Type deduction may fail for any of the following
7558 reasons:
7560 -- Attempting to create a pointer to reference type.
7561 -- Attempting to create a reference to a reference type or
7562 a reference to void. */
7563 if (TREE_CODE (type) == REFERENCE_TYPE
7564 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7566 static location_t last_loc;
7568 /* We keep track of the last time we issued this error
7569 message to avoid spewing a ton of messages during a
7570 single bad template instantiation. */
7571 if (complain & tf_error
7572 #ifdef USE_MAPPED_LOCATION
7573 && last_loc != input_location
7574 #else
7575 && (last_loc.line != input_line
7576 || last_loc.file != input_filename)
7577 #endif
7580 if (TREE_CODE (type) == VOID_TYPE)
7581 error ("forming reference to void");
7582 else
7583 error ("forming %s to reference type %qT",
7584 (code == POINTER_TYPE) ? "pointer" : "reference",
7585 type);
7586 last_loc = input_location;
7589 return error_mark_node;
7591 else if (code == POINTER_TYPE)
7593 r = build_pointer_type (type);
7594 if (TREE_CODE (type) == METHOD_TYPE)
7595 r = build_ptrmemfunc_type (r);
7597 else
7598 r = build_reference_type (type);
7599 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7601 if (r != error_mark_node)
7602 /* Will this ever be needed for TYPE_..._TO values? */
7603 layout_type (r);
7605 return r;
7607 case OFFSET_TYPE:
7609 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7610 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7612 /* [temp.deduct]
7614 Type deduction may fail for any of the following
7615 reasons:
7617 -- Attempting to create "pointer to member of T" when T
7618 is not a class type. */
7619 if (complain & tf_error)
7620 error ("creating pointer to member of non-class type %qT", r);
7621 return error_mark_node;
7623 if (TREE_CODE (type) == REFERENCE_TYPE)
7625 if (complain & tf_error)
7626 error ("creating pointer to member reference type %qT", type);
7627 return error_mark_node;
7629 if (TREE_CODE (type) == VOID_TYPE)
7631 if (complain & tf_error)
7632 error ("creating pointer to member of type void");
7633 return error_mark_node;
7635 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7636 if (TREE_CODE (type) == FUNCTION_TYPE)
7638 /* The type of the implicit object parameter gets its
7639 cv-qualifiers from the FUNCTION_TYPE. */
7640 tree method_type;
7641 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7642 cp_type_quals (type));
7643 tree memptr;
7644 method_type = build_method_type_directly (this_type,
7645 TREE_TYPE (type),
7646 TYPE_ARG_TYPES (type));
7647 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7648 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7649 complain);
7651 else
7652 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7653 TYPE_QUALS (t),
7654 complain);
7656 case FUNCTION_TYPE:
7657 case METHOD_TYPE:
7659 tree fntype;
7660 tree specs;
7661 fntype = tsubst_function_type (t, args, complain, in_decl);
7662 if (fntype == error_mark_node)
7663 return error_mark_node;
7665 /* Substitute the exception specification. */
7666 specs = tsubst_exception_specification (t, args, complain,
7667 in_decl);
7668 if (specs == error_mark_node)
7669 return error_mark_node;
7670 if (specs)
7671 fntype = build_exception_variant (fntype, specs);
7672 return fntype;
7674 case ARRAY_TYPE:
7676 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7677 if (domain == error_mark_node)
7678 return error_mark_node;
7680 /* As an optimization, we avoid regenerating the array type if
7681 it will obviously be the same as T. */
7682 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7683 return t;
7685 /* These checks should match the ones in grokdeclarator.
7687 [temp.deduct]
7689 The deduction may fail for any of the following reasons:
7691 -- Attempting to create an array with an element type that
7692 is void, a function type, or a reference type, or [DR337]
7693 an abstract class type. */
7694 if (TREE_CODE (type) == VOID_TYPE
7695 || TREE_CODE (type) == FUNCTION_TYPE
7696 || TREE_CODE (type) == REFERENCE_TYPE)
7698 if (complain & tf_error)
7699 error ("creating array of %qT", type);
7700 return error_mark_node;
7702 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7704 if (complain & tf_error)
7705 error ("creating array of %qT, which is an abstract class type",
7706 type);
7707 return error_mark_node;
7710 r = build_cplus_array_type (type, domain);
7711 return r;
7714 case PLUS_EXPR:
7715 case MINUS_EXPR:
7717 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7718 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7720 if (e1 == error_mark_node || e2 == error_mark_node)
7721 return error_mark_node;
7723 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7726 case NEGATE_EXPR:
7727 case NOP_EXPR:
7729 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7730 if (e == error_mark_node)
7731 return error_mark_node;
7733 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7736 case TYPENAME_TYPE:
7738 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7739 in_decl, /*entering_scope=*/1);
7740 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7741 complain, in_decl);
7743 if (ctx == error_mark_node || f == error_mark_node)
7744 return error_mark_node;
7746 if (!IS_AGGR_TYPE (ctx))
7748 if (complain & tf_error)
7749 error ("%qT is not a class, struct, or union type", ctx);
7750 return error_mark_node;
7752 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7754 /* Normally, make_typename_type does not require that the CTX
7755 have complete type in order to allow things like:
7757 template <class T> struct S { typename S<T>::X Y; };
7759 But, such constructs have already been resolved by this
7760 point, so here CTX really should have complete type, unless
7761 it's a partial instantiation. */
7762 ctx = complete_type (ctx);
7763 if (!COMPLETE_TYPE_P (ctx))
7765 if (complain & tf_error)
7766 cxx_incomplete_type_error (NULL_TREE, ctx);
7767 return error_mark_node;
7771 f = make_typename_type (ctx, f, typename_type,
7772 (complain & tf_error) | tf_keep_type_decl);
7773 if (f == error_mark_node)
7774 return f;
7775 if (TREE_CODE (f) == TYPE_DECL)
7777 complain |= tf_ignore_bad_quals;
7778 f = TREE_TYPE (f);
7781 if (TREE_CODE (f) != TYPENAME_TYPE)
7783 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7784 error ("%qT resolves to %qT, which is not an enumeration type",
7785 t, f);
7786 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7787 error ("%qT resolves to %qT, which is is not a class type",
7788 t, f);
7791 return cp_build_qualified_type_real
7792 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7795 case UNBOUND_CLASS_TEMPLATE:
7797 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7798 in_decl, /*entering_scope=*/1);
7799 tree name = TYPE_IDENTIFIER (t);
7800 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7802 if (ctx == error_mark_node || name == error_mark_node)
7803 return error_mark_node;
7805 if (parm_list)
7806 parm_list = tsubst_template_parms (parm_list, args, complain);
7807 return make_unbound_class_template (ctx, name, parm_list, complain);
7810 case INDIRECT_REF:
7811 case ADDR_EXPR:
7812 case CALL_EXPR:
7813 gcc_unreachable ();
7815 case ARRAY_REF:
7817 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7818 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7819 /*integral_constant_expression_p=*/false);
7820 if (e1 == error_mark_node || e2 == error_mark_node)
7821 return error_mark_node;
7823 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7826 case SCOPE_REF:
7828 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7829 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7830 if (e1 == error_mark_node || e2 == error_mark_node)
7831 return error_mark_node;
7833 return build_qualified_name (/*type=*/NULL_TREE,
7834 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7837 case TYPEOF_TYPE:
7839 tree type;
7841 type = finish_typeof (tsubst_expr
7842 (TYPEOF_TYPE_EXPR (t), args,
7843 complain, in_decl,
7844 /*integral_constant_expression_p=*/false));
7845 return cp_build_qualified_type_real (type,
7846 cp_type_quals (t)
7847 | cp_type_quals (type),
7848 complain);
7851 default:
7852 sorry ("use of %qs in template",
7853 tree_code_name [(int) TREE_CODE (t)]);
7854 return error_mark_node;
7858 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7859 type of the expression on the left-hand side of the "." or "->"
7860 operator. */
7862 static tree
7863 tsubst_baselink (tree baselink, tree object_type,
7864 tree args, tsubst_flags_t complain, tree in_decl)
7866 tree name;
7867 tree qualifying_scope;
7868 tree fns;
7869 tree optype;
7870 tree template_args = 0;
7871 bool template_id_p = false;
7873 /* A baselink indicates a function from a base class. Both the
7874 BASELINK_ACCESS_BINFO and the base class referenced may
7875 indicate bases of the template class, rather than the
7876 instantiated class. In addition, lookups that were not
7877 ambiguous before may be ambiguous now. Therefore, we perform
7878 the lookup again. */
7879 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7880 qualifying_scope = tsubst (qualifying_scope, args,
7881 complain, in_decl);
7882 fns = BASELINK_FUNCTIONS (baselink);
7883 optype = BASELINK_OPTYPE (baselink);
7884 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7886 template_id_p = true;
7887 template_args = TREE_OPERAND (fns, 1);
7888 fns = TREE_OPERAND (fns, 0);
7889 if (template_args)
7890 template_args = tsubst_template_args (template_args, args,
7891 complain, in_decl);
7893 name = DECL_NAME (get_first_fn (fns));
7894 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7896 /* If lookup found a single function, mark it as used at this
7897 point. (If it lookup found multiple functions the one selected
7898 later by overload resolution will be marked as used at that
7899 point.) */
7900 if (BASELINK_P (baselink))
7901 fns = BASELINK_FUNCTIONS (baselink);
7902 if (!template_id_p && !really_overloaded_fn (fns))
7903 mark_used (OVL_CURRENT (fns));
7905 /* Add back the template arguments, if present. */
7906 if (BASELINK_P (baselink) && template_id_p)
7907 BASELINK_FUNCTIONS (baselink)
7908 = build_nt (TEMPLATE_ID_EXPR,
7909 BASELINK_FUNCTIONS (baselink),
7910 template_args);
7911 /* Update the conversion operator type. */
7912 BASELINK_OPTYPE (baselink)
7913 = tsubst (optype, args, complain, in_decl);
7915 if (!object_type)
7916 object_type = current_class_type;
7917 return adjust_result_of_qualified_name_lookup (baselink,
7918 qualifying_scope,
7919 object_type);
7922 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7923 true if the qualified-id will be a postfix-expression in-and-of
7924 itself; false if more of the postfix-expression follows the
7925 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7926 of "&". */
7928 static tree
7929 tsubst_qualified_id (tree qualified_id, tree args,
7930 tsubst_flags_t complain, tree in_decl,
7931 bool done, bool address_p)
7933 tree expr;
7934 tree scope;
7935 tree name;
7936 bool is_template;
7937 tree template_args;
7939 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7941 /* Figure out what name to look up. */
7942 name = TREE_OPERAND (qualified_id, 1);
7943 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7945 is_template = true;
7946 template_args = TREE_OPERAND (name, 1);
7947 if (template_args)
7948 template_args = tsubst_template_args (template_args, args,
7949 complain, in_decl);
7950 name = TREE_OPERAND (name, 0);
7952 else
7954 is_template = false;
7955 template_args = NULL_TREE;
7958 /* Substitute into the qualifying scope. When there are no ARGS, we
7959 are just trying to simplify a non-dependent expression. In that
7960 case the qualifying scope may be dependent, and, in any case,
7961 substituting will not help. */
7962 scope = TREE_OPERAND (qualified_id, 0);
7963 if (args)
7965 scope = tsubst (scope, args, complain, in_decl);
7966 expr = tsubst_copy (name, args, complain, in_decl);
7968 else
7969 expr = name;
7971 if (dependent_type_p (scope))
7972 return build_qualified_name (/*type=*/NULL_TREE,
7973 scope, expr,
7974 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7976 if (!BASELINK_P (name) && !DECL_P (expr))
7978 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7979 /* If this were actually a destructor call, it would have been
7980 parsed as such by the parser. */
7981 expr = error_mark_node;
7982 else
7983 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7984 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7985 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7987 if (complain & tf_error)
7989 error ("dependent-name %qE is parsed as a non-type, but "
7990 "instantiation yields a type", qualified_id);
7991 inform ("say %<typename %E%> if a type is meant", qualified_id);
7993 return error_mark_node;
7997 if (DECL_P (expr))
7999 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
8000 scope);
8001 /* Remember that there was a reference to this entity. */
8002 mark_used (expr);
8005 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
8007 if (complain & tf_error)
8008 qualified_name_lookup_error (scope,
8009 TREE_OPERAND (qualified_id, 1),
8010 expr);
8011 return error_mark_node;
8014 if (is_template)
8015 expr = lookup_template_function (expr, template_args);
8017 if (expr == error_mark_node && complain & tf_error)
8018 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
8019 expr);
8020 else if (TYPE_P (scope))
8022 expr = (adjust_result_of_qualified_name_lookup
8023 (expr, scope, current_class_type));
8024 expr = (finish_qualified_id_expr
8025 (scope, expr, done, address_p,
8026 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
8027 /*template_arg_p=*/false));
8030 /* Expressions do not generally have reference type. */
8031 if (TREE_CODE (expr) != SCOPE_REF
8032 /* However, if we're about to form a pointer-to-member, we just
8033 want the referenced member referenced. */
8034 && TREE_CODE (expr) != OFFSET_REF)
8035 expr = convert_from_reference (expr);
8037 return expr;
8040 /* Like tsubst, but deals with expressions. This function just replaces
8041 template parms; to finish processing the resultant expression, use
8042 tsubst_expr. */
8044 static tree
8045 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8047 enum tree_code code;
8048 tree r;
8050 if (t == NULL_TREE || t == error_mark_node)
8051 return t;
8053 code = TREE_CODE (t);
8055 switch (code)
8057 case PARM_DECL:
8058 r = retrieve_local_specialization (t);
8059 gcc_assert (r != NULL);
8060 mark_used (r);
8061 return r;
8063 case CONST_DECL:
8065 tree enum_type;
8066 tree v;
8068 if (DECL_TEMPLATE_PARM_P (t))
8069 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
8070 /* There is no need to substitute into namespace-scope
8071 enumerators. */
8072 if (DECL_NAMESPACE_SCOPE_P (t))
8073 return t;
8074 /* If ARGS is NULL, then T is known to be non-dependent. */
8075 if (args == NULL_TREE)
8076 return integral_constant_value (t);
8078 /* Unfortunately, we cannot just call lookup_name here.
8079 Consider:
8081 template <int I> int f() {
8082 enum E { a = I };
8083 struct S { void g() { E e = a; } };
8086 When we instantiate f<7>::S::g(), say, lookup_name is not
8087 clever enough to find f<7>::a. */
8088 enum_type
8089 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
8090 /*entering_scope=*/0);
8092 for (v = TYPE_VALUES (enum_type);
8093 v != NULL_TREE;
8094 v = TREE_CHAIN (v))
8095 if (TREE_PURPOSE (v) == DECL_NAME (t))
8096 return TREE_VALUE (v);
8098 /* We didn't find the name. That should never happen; if
8099 name-lookup found it during preliminary parsing, we
8100 should find it again here during instantiation. */
8101 gcc_unreachable ();
8103 return t;
8105 case FIELD_DECL:
8106 if (DECL_CONTEXT (t))
8108 tree ctx;
8110 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
8111 /*entering_scope=*/1);
8112 if (ctx != DECL_CONTEXT (t))
8114 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
8115 if (!r)
8117 if (complain & tf_error)
8118 error ("using invalid field %qD", t);
8119 return error_mark_node;
8121 return r;
8125 return t;
8127 case VAR_DECL:
8128 case FUNCTION_DECL:
8129 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8130 || local_variable_p (t))
8131 t = tsubst (t, args, complain, in_decl);
8132 mark_used (t);
8133 return t;
8135 case BASELINK:
8136 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
8138 case TEMPLATE_DECL:
8139 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8140 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
8141 args, complain, in_decl);
8142 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
8143 return tsubst (t, args, complain, in_decl);
8144 else if (DECL_CLASS_SCOPE_P (t)
8145 && uses_template_parms (DECL_CONTEXT (t)))
8147 /* Template template argument like the following example need
8148 special treatment:
8150 template <template <class> class TT> struct C {};
8151 template <class T> struct D {
8152 template <class U> struct E {};
8153 C<E> c; // #1
8155 D<int> d; // #2
8157 We are processing the template argument `E' in #1 for
8158 the template instantiation #2. Originally, `E' is a
8159 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8160 have to substitute this with one having context `D<int>'. */
8162 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8163 return lookup_field (context, DECL_NAME(t), 0, false);
8165 else
8166 /* Ordinary template template argument. */
8167 return t;
8169 case CAST_EXPR:
8170 case REINTERPRET_CAST_EXPR:
8171 case CONST_CAST_EXPR:
8172 case STATIC_CAST_EXPR:
8173 case DYNAMIC_CAST_EXPR:
8174 case NOP_EXPR:
8175 return build1
8176 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8177 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8179 case INDIRECT_REF:
8180 case NEGATE_EXPR:
8181 case TRUTH_NOT_EXPR:
8182 case BIT_NOT_EXPR:
8183 case ADDR_EXPR:
8184 case UNARY_PLUS_EXPR: /* Unary + */
8185 case SIZEOF_EXPR:
8186 case ALIGNOF_EXPR:
8187 case ARROW_EXPR:
8188 case THROW_EXPR:
8189 case TYPEID_EXPR:
8190 case REALPART_EXPR:
8191 case IMAGPART_EXPR:
8192 return build1
8193 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8194 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8196 case COMPONENT_REF:
8198 tree object;
8199 tree name;
8201 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8202 name = TREE_OPERAND (t, 1);
8203 if (TREE_CODE (name) == BIT_NOT_EXPR)
8205 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8206 complain, in_decl);
8207 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8209 else if (TREE_CODE (name) == SCOPE_REF
8210 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8212 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8213 complain, in_decl);
8214 name = TREE_OPERAND (name, 1);
8215 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8216 complain, in_decl);
8217 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8218 name = build_qualified_name (/*type=*/NULL_TREE,
8219 base, name,
8220 /*template_p=*/false);
8222 else if (TREE_CODE (name) == BASELINK)
8223 name = tsubst_baselink (name,
8224 non_reference (TREE_TYPE (object)),
8225 args, complain,
8226 in_decl);
8227 else
8228 name = tsubst_copy (name, args, complain, in_decl);
8229 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
8232 case PLUS_EXPR:
8233 case MINUS_EXPR:
8234 case MULT_EXPR:
8235 case TRUNC_DIV_EXPR:
8236 case CEIL_DIV_EXPR:
8237 case FLOOR_DIV_EXPR:
8238 case ROUND_DIV_EXPR:
8239 case EXACT_DIV_EXPR:
8240 case BIT_AND_EXPR:
8241 case BIT_IOR_EXPR:
8242 case BIT_XOR_EXPR:
8243 case TRUNC_MOD_EXPR:
8244 case FLOOR_MOD_EXPR:
8245 case TRUTH_ANDIF_EXPR:
8246 case TRUTH_ORIF_EXPR:
8247 case TRUTH_AND_EXPR:
8248 case TRUTH_OR_EXPR:
8249 case RSHIFT_EXPR:
8250 case LSHIFT_EXPR:
8251 case RROTATE_EXPR:
8252 case LROTATE_EXPR:
8253 case EQ_EXPR:
8254 case NE_EXPR:
8255 case MAX_EXPR:
8256 case MIN_EXPR:
8257 case LE_EXPR:
8258 case GE_EXPR:
8259 case LT_EXPR:
8260 case GT_EXPR:
8261 case COMPOUND_EXPR:
8262 case DOTSTAR_EXPR:
8263 case MEMBER_REF:
8264 case PREDECREMENT_EXPR:
8265 case PREINCREMENT_EXPR:
8266 case POSTDECREMENT_EXPR:
8267 case POSTINCREMENT_EXPR:
8268 return build_nt
8269 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8270 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8272 case SCOPE_REF:
8273 return build_qualified_name (/*type=*/NULL_TREE,
8274 tsubst_copy (TREE_OPERAND (t, 0),
8275 args, complain, in_decl),
8276 tsubst_copy (TREE_OPERAND (t, 1),
8277 args, complain, in_decl),
8278 QUALIFIED_NAME_IS_TEMPLATE (t));
8280 case ARRAY_REF:
8281 return build_nt
8282 (ARRAY_REF,
8283 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8284 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8285 NULL_TREE, NULL_TREE);
8287 case CALL_EXPR:
8288 return build_nt (code,
8289 tsubst_copy (TREE_OPERAND (t, 0), args,
8290 complain, in_decl),
8291 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8292 in_decl),
8293 NULL_TREE);
8295 case COND_EXPR:
8296 case MODOP_EXPR:
8297 case PSEUDO_DTOR_EXPR:
8299 r = build_nt
8300 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8301 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8302 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8303 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8304 return r;
8307 case NEW_EXPR:
8309 r = build_nt
8310 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8311 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8312 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8313 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8314 return r;
8317 case DELETE_EXPR:
8319 r = build_nt
8320 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8321 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8322 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8323 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8324 return r;
8327 case TEMPLATE_ID_EXPR:
8329 /* Substituted template arguments */
8330 tree fn = TREE_OPERAND (t, 0);
8331 tree targs = TREE_OPERAND (t, 1);
8333 fn = tsubst_copy (fn, args, complain, in_decl);
8334 if (targs)
8335 targs = tsubst_template_args (targs, args, complain, in_decl);
8337 return lookup_template_function (fn, targs);
8340 case TREE_LIST:
8342 tree purpose, value, chain;
8344 if (t == void_list_node)
8345 return t;
8347 purpose = TREE_PURPOSE (t);
8348 if (purpose)
8349 purpose = tsubst_copy (purpose, args, complain, in_decl);
8350 value = TREE_VALUE (t);
8351 if (value)
8352 value = tsubst_copy (value, args, complain, in_decl);
8353 chain = TREE_CHAIN (t);
8354 if (chain && chain != void_type_node)
8355 chain = tsubst_copy (chain, args, complain, in_decl);
8356 if (purpose == TREE_PURPOSE (t)
8357 && value == TREE_VALUE (t)
8358 && chain == TREE_CHAIN (t))
8359 return t;
8360 return tree_cons (purpose, value, chain);
8363 case RECORD_TYPE:
8364 case UNION_TYPE:
8365 case ENUMERAL_TYPE:
8366 case INTEGER_TYPE:
8367 case TEMPLATE_TYPE_PARM:
8368 case TEMPLATE_TEMPLATE_PARM:
8369 case BOUND_TEMPLATE_TEMPLATE_PARM:
8370 case TEMPLATE_PARM_INDEX:
8371 case POINTER_TYPE:
8372 case REFERENCE_TYPE:
8373 case OFFSET_TYPE:
8374 case FUNCTION_TYPE:
8375 case METHOD_TYPE:
8376 case ARRAY_TYPE:
8377 case TYPENAME_TYPE:
8378 case UNBOUND_CLASS_TEMPLATE:
8379 case TYPEOF_TYPE:
8380 case TYPE_DECL:
8381 return tsubst (t, args, complain, in_decl);
8383 case IDENTIFIER_NODE:
8384 if (IDENTIFIER_TYPENAME_P (t))
8386 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8387 return mangle_conv_op_name_for_type (new_type);
8389 else
8390 return t;
8392 case CONSTRUCTOR:
8393 /* This is handled by tsubst_copy_and_build. */
8394 gcc_unreachable ();
8396 case VA_ARG_EXPR:
8397 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8398 in_decl),
8399 tsubst (TREE_TYPE (t), args, complain, in_decl));
8401 case CLEANUP_POINT_EXPR:
8402 /* We shouldn't have built any of these during initial template
8403 generation. Instead, they should be built during instantiation
8404 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8405 gcc_unreachable ();
8407 case OFFSET_REF:
8408 mark_used (TREE_OPERAND (t, 1));
8409 return t;
8411 default:
8412 return t;
8416 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8418 static tree
8419 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8420 tree in_decl)
8422 tree new_clauses = NULL, nc, oc;
8424 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8426 nc = copy_node (oc);
8427 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8428 new_clauses = nc;
8430 switch (OMP_CLAUSE_CODE (nc))
8432 case OMP_CLAUSE_PRIVATE:
8433 case OMP_CLAUSE_SHARED:
8434 case OMP_CLAUSE_FIRSTPRIVATE:
8435 case OMP_CLAUSE_LASTPRIVATE:
8436 case OMP_CLAUSE_REDUCTION:
8437 case OMP_CLAUSE_COPYIN:
8438 case OMP_CLAUSE_COPYPRIVATE:
8439 case OMP_CLAUSE_IF:
8440 case OMP_CLAUSE_NUM_THREADS:
8441 case OMP_CLAUSE_SCHEDULE:
8442 OMP_CLAUSE_OPERAND (nc, 0)
8443 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8444 in_decl, /*integral_constant_expression_p=*/false);
8445 break;
8446 case OMP_CLAUSE_NOWAIT:
8447 case OMP_CLAUSE_ORDERED:
8448 case OMP_CLAUSE_DEFAULT:
8449 break;
8450 default:
8451 gcc_unreachable ();
8455 return finish_omp_clauses (nreverse (new_clauses));
8458 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8460 static tree
8461 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8462 tree in_decl)
8464 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8466 tree purpose, value, chain;
8468 if (t == NULL)
8469 return t;
8471 if (TREE_CODE (t) != TREE_LIST)
8472 return tsubst_copy_and_build (t, args, complain, in_decl,
8473 /*function_p=*/false,
8474 /*integral_constant_expression_p=*/false);
8476 if (t == void_list_node)
8477 return t;
8479 purpose = TREE_PURPOSE (t);
8480 if (purpose)
8481 purpose = RECUR (purpose);
8482 value = TREE_VALUE (t);
8483 if (value)
8484 value = RECUR (value);
8485 chain = TREE_CHAIN (t);
8486 if (chain && chain != void_type_node)
8487 chain = RECUR (chain);
8488 return tree_cons (purpose, value, chain);
8489 #undef RECUR
8492 /* Like tsubst_copy for expressions, etc. but also does semantic
8493 processing. */
8495 static tree
8496 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8497 bool integral_constant_expression_p)
8499 #define RECUR(NODE) \
8500 tsubst_expr ((NODE), args, complain, in_decl, \
8501 integral_constant_expression_p)
8503 tree stmt, tmp;
8505 if (t == NULL_TREE || t == error_mark_node)
8506 return t;
8508 if (EXPR_HAS_LOCATION (t))
8509 input_location = EXPR_LOCATION (t);
8510 if (STATEMENT_CODE_P (TREE_CODE (t)))
8511 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8513 switch (TREE_CODE (t))
8515 case STATEMENT_LIST:
8517 tree_stmt_iterator i;
8518 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8519 RECUR (tsi_stmt (i));
8520 break;
8523 case CTOR_INITIALIZER:
8524 finish_mem_initializers (tsubst_initializer_list
8525 (TREE_OPERAND (t, 0), args));
8526 break;
8528 case RETURN_EXPR:
8529 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
8530 break;
8532 case EXPR_STMT:
8533 tmp = RECUR (EXPR_STMT_EXPR (t));
8534 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8535 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8536 else
8537 finish_expr_stmt (tmp);
8538 break;
8540 case USING_STMT:
8541 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
8542 break;
8544 case DECL_EXPR:
8546 tree decl;
8547 tree init;
8549 decl = DECL_EXPR_DECL (t);
8550 if (TREE_CODE (decl) == LABEL_DECL)
8551 finish_label_decl (DECL_NAME (decl));
8552 else if (TREE_CODE (decl) == USING_DECL)
8554 tree scope = USING_DECL_SCOPE (decl);
8555 tree name = DECL_NAME (decl);
8556 tree decl;
8558 scope = RECUR (scope);
8559 decl = lookup_qualified_name (scope, name,
8560 /*is_type_p=*/false,
8561 /*complain=*/false);
8562 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8563 qualified_name_lookup_error (scope, name, decl);
8564 else
8565 do_local_using_decl (decl, scope, name);
8567 else
8569 init = DECL_INITIAL (decl);
8570 decl = tsubst (decl, args, complain, in_decl);
8571 if (decl != error_mark_node)
8573 /* By marking the declaration as instantiated, we avoid
8574 trying to instantiate it. Since instantiate_decl can't
8575 handle local variables, and since we've already done
8576 all that needs to be done, that's the right thing to
8577 do. */
8578 if (TREE_CODE (decl) == VAR_DECL)
8579 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8580 if (TREE_CODE (decl) == VAR_DECL
8581 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8582 /* Anonymous aggregates are a special case. */
8583 finish_anon_union (decl);
8584 else
8586 maybe_push_decl (decl);
8587 if (TREE_CODE (decl) == VAR_DECL
8588 && DECL_PRETTY_FUNCTION_P (decl))
8590 /* For __PRETTY_FUNCTION__ we have to adjust the
8591 initializer. */
8592 const char *const name
8593 = cxx_printable_name (current_function_decl, 2);
8594 init = cp_fname_init (name, &TREE_TYPE (decl));
8596 else
8597 init = RECUR (init);
8598 finish_decl (decl, init, NULL_TREE);
8603 /* A DECL_EXPR can also be used as an expression, in the condition
8604 clause of an if/for/while construct. */
8605 return decl;
8608 case FOR_STMT:
8609 stmt = begin_for_stmt ();
8610 RECUR (FOR_INIT_STMT (t));
8611 finish_for_init_stmt (stmt);
8612 tmp = RECUR (FOR_COND (t));
8613 finish_for_cond (tmp, stmt);
8614 tmp = RECUR (FOR_EXPR (t));
8615 finish_for_expr (tmp, stmt);
8616 RECUR (FOR_BODY (t));
8617 finish_for_stmt (stmt);
8618 break;
8620 case WHILE_STMT:
8621 stmt = begin_while_stmt ();
8622 tmp = RECUR (WHILE_COND (t));
8623 finish_while_stmt_cond (tmp, stmt);
8624 RECUR (WHILE_BODY (t));
8625 finish_while_stmt (stmt);
8626 break;
8628 case DO_STMT:
8629 stmt = begin_do_stmt ();
8630 RECUR (DO_BODY (t));
8631 finish_do_body (stmt);
8632 tmp = RECUR (DO_COND (t));
8633 finish_do_stmt (tmp, stmt);
8634 break;
8636 case IF_STMT:
8637 stmt = begin_if_stmt ();
8638 tmp = RECUR (IF_COND (t));
8639 finish_if_stmt_cond (tmp, stmt);
8640 RECUR (THEN_CLAUSE (t));
8641 finish_then_clause (stmt);
8643 if (ELSE_CLAUSE (t))
8645 begin_else_clause (stmt);
8646 RECUR (ELSE_CLAUSE (t));
8647 finish_else_clause (stmt);
8650 finish_if_stmt (stmt);
8651 break;
8653 case BIND_EXPR:
8654 if (BIND_EXPR_BODY_BLOCK (t))
8655 stmt = begin_function_body ();
8656 else
8657 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8658 ? BCS_TRY_BLOCK : 0);
8660 RECUR (BIND_EXPR_BODY (t));
8662 if (BIND_EXPR_BODY_BLOCK (t))
8663 finish_function_body (stmt);
8664 else
8665 finish_compound_stmt (stmt);
8666 break;
8668 case BREAK_STMT:
8669 finish_break_stmt ();
8670 break;
8672 case CONTINUE_STMT:
8673 finish_continue_stmt ();
8674 break;
8676 case SWITCH_STMT:
8677 stmt = begin_switch_stmt ();
8678 tmp = RECUR (SWITCH_STMT_COND (t));
8679 finish_switch_cond (tmp, stmt);
8680 RECUR (SWITCH_STMT_BODY (t));
8681 finish_switch_stmt (stmt);
8682 break;
8684 case CASE_LABEL_EXPR:
8685 finish_case_label (RECUR (CASE_LOW (t)),
8686 RECUR (CASE_HIGH (t)));
8687 break;
8689 case LABEL_EXPR:
8690 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8691 break;
8693 case GOTO_EXPR:
8694 tmp = GOTO_DESTINATION (t);
8695 if (TREE_CODE (tmp) != LABEL_DECL)
8696 /* Computed goto's must be tsubst'd into. On the other hand,
8697 non-computed gotos must not be; the identifier in question
8698 will have no binding. */
8699 tmp = RECUR (tmp);
8700 else
8701 tmp = DECL_NAME (tmp);
8702 finish_goto_stmt (tmp);
8703 break;
8705 case ASM_EXPR:
8706 tmp = finish_asm_stmt
8707 (ASM_VOLATILE_P (t),
8708 RECUR (ASM_STRING (t)),
8709 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8710 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8711 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8713 tree asm_expr = tmp;
8714 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8715 asm_expr = TREE_OPERAND (asm_expr, 0);
8716 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8718 break;
8720 case TRY_BLOCK:
8721 if (CLEANUP_P (t))
8723 stmt = begin_try_block ();
8724 RECUR (TRY_STMTS (t));
8725 finish_cleanup_try_block (stmt);
8726 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
8728 else
8730 tree compound_stmt = NULL_TREE;
8732 if (FN_TRY_BLOCK_P (t))
8733 stmt = begin_function_try_block (&compound_stmt);
8734 else
8735 stmt = begin_try_block ();
8737 RECUR (TRY_STMTS (t));
8739 if (FN_TRY_BLOCK_P (t))
8740 finish_function_try_block (stmt);
8741 else
8742 finish_try_block (stmt);
8744 RECUR (TRY_HANDLERS (t));
8745 if (FN_TRY_BLOCK_P (t))
8746 finish_function_handler_sequence (stmt, compound_stmt);
8747 else
8748 finish_handler_sequence (stmt);
8750 break;
8752 case HANDLER:
8754 tree decl = HANDLER_PARMS (t);
8756 if (decl)
8758 decl = tsubst (decl, args, complain, in_decl);
8759 /* Prevent instantiate_decl from trying to instantiate
8760 this variable. We've already done all that needs to be
8761 done. */
8762 if (decl != error_mark_node)
8763 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8765 stmt = begin_handler ();
8766 finish_handler_parms (decl, stmt);
8767 RECUR (HANDLER_BODY (t));
8768 finish_handler (stmt);
8770 break;
8772 case TAG_DEFN:
8773 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8774 break;
8776 case STATIC_ASSERT:
8778 tree condition =
8779 tsubst_expr (STATIC_ASSERT_CONDITION (t),
8780 args,
8781 complain, in_decl,
8782 /*integral_constant_expression_p=*/true);
8783 finish_static_assert (condition,
8784 STATIC_ASSERT_MESSAGE (t),
8785 STATIC_ASSERT_SOURCE_LOCATION (t),
8786 /*member_p=*/false);
8788 break;
8790 case OMP_PARALLEL:
8791 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8792 args, complain, in_decl);
8793 stmt = begin_omp_parallel ();
8794 RECUR (OMP_PARALLEL_BODY (t));
8795 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8796 = OMP_PARALLEL_COMBINED (t);
8797 break;
8799 case OMP_FOR:
8801 tree clauses, decl, init, cond, incr, body, pre_body;
8803 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8804 args, complain, in_decl);
8805 init = OMP_FOR_INIT (t);
8806 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8807 decl = RECUR (TREE_OPERAND (init, 0));
8808 init = RECUR (TREE_OPERAND (init, 1));
8809 cond = RECUR (OMP_FOR_COND (t));
8810 incr = RECUR (OMP_FOR_INCR (t));
8812 stmt = begin_omp_structured_block ();
8814 pre_body = push_stmt_list ();
8815 RECUR (OMP_FOR_PRE_BODY (t));
8816 pre_body = pop_stmt_list (pre_body);
8818 body = push_stmt_list ();
8819 RECUR (OMP_FOR_BODY (t));
8820 body = pop_stmt_list (body);
8822 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8823 pre_body);
8824 if (t)
8825 OMP_FOR_CLAUSES (t) = clauses;
8827 add_stmt (finish_omp_structured_block (stmt));
8829 break;
8831 case OMP_SECTIONS:
8832 case OMP_SINGLE:
8833 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8834 stmt = push_stmt_list ();
8835 RECUR (OMP_BODY (t));
8836 stmt = pop_stmt_list (stmt);
8838 t = copy_node (t);
8839 OMP_BODY (t) = stmt;
8840 OMP_CLAUSES (t) = tmp;
8841 add_stmt (t);
8842 break;
8844 case OMP_SECTION:
8845 case OMP_CRITICAL:
8846 case OMP_MASTER:
8847 case OMP_ORDERED:
8848 stmt = push_stmt_list ();
8849 RECUR (OMP_BODY (t));
8850 stmt = pop_stmt_list (stmt);
8852 t = copy_node (t);
8853 OMP_BODY (t) = stmt;
8854 add_stmt (t);
8855 break;
8857 case OMP_ATOMIC:
8859 tree op0, op1;
8860 op0 = RECUR (TREE_OPERAND (t, 0));
8861 op1 = RECUR (TREE_OPERAND (t, 1));
8862 finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
8864 break;
8866 default:
8867 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8869 return tsubst_copy_and_build (t, args, complain, in_decl,
8870 /*function_p=*/false,
8871 integral_constant_expression_p);
8874 return NULL_TREE;
8875 #undef RECUR
8878 /* T is a postfix-expression that is not being used in a function
8879 call. Return the substituted version of T. */
8881 static tree
8882 tsubst_non_call_postfix_expression (tree t, tree args,
8883 tsubst_flags_t complain,
8884 tree in_decl)
8886 if (TREE_CODE (t) == SCOPE_REF)
8887 t = tsubst_qualified_id (t, args, complain, in_decl,
8888 /*done=*/false, /*address_p=*/false);
8889 else
8890 t = tsubst_copy_and_build (t, args, complain, in_decl,
8891 /*function_p=*/false,
8892 /*integral_constant_expression_p=*/false);
8894 return t;
8897 /* Like tsubst but deals with expressions and performs semantic
8898 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8900 tree
8901 tsubst_copy_and_build (tree t,
8902 tree args,
8903 tsubst_flags_t complain,
8904 tree in_decl,
8905 bool function_p,
8906 bool integral_constant_expression_p)
8908 #define RECUR(NODE) \
8909 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8910 /*function_p=*/false, \
8911 integral_constant_expression_p)
8913 tree op1;
8915 if (t == NULL_TREE || t == error_mark_node)
8916 return t;
8918 switch (TREE_CODE (t))
8920 case USING_DECL:
8921 t = DECL_NAME (t);
8922 /* Fall through. */
8923 case IDENTIFIER_NODE:
8925 tree decl;
8926 cp_id_kind idk;
8927 bool non_integral_constant_expression_p;
8928 const char *error_msg;
8930 if (IDENTIFIER_TYPENAME_P (t))
8932 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8933 t = mangle_conv_op_name_for_type (new_type);
8936 /* Look up the name. */
8937 decl = lookup_name (t);
8939 /* By convention, expressions use ERROR_MARK_NODE to indicate
8940 failure, not NULL_TREE. */
8941 if (decl == NULL_TREE)
8942 decl = error_mark_node;
8944 decl = finish_id_expression (t, decl, NULL_TREE,
8945 &idk,
8946 integral_constant_expression_p,
8947 /*allow_non_integral_constant_expression_p=*/false,
8948 &non_integral_constant_expression_p,
8949 /*template_p=*/false,
8950 /*done=*/true,
8951 /*address_p=*/false,
8952 /*template_arg_p=*/false,
8953 &error_msg);
8954 if (error_msg)
8955 error (error_msg);
8956 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8957 decl = unqualified_name_lookup_error (decl);
8958 return decl;
8961 case TEMPLATE_ID_EXPR:
8963 tree object;
8964 tree template = RECUR (TREE_OPERAND (t, 0));
8965 tree targs = TREE_OPERAND (t, 1);
8967 if (targs)
8968 targs = tsubst_template_args (targs, args, complain, in_decl);
8970 if (TREE_CODE (template) == COMPONENT_REF)
8972 object = TREE_OPERAND (template, 0);
8973 template = TREE_OPERAND (template, 1);
8975 else
8976 object = NULL_TREE;
8977 template = lookup_template_function (template, targs);
8979 if (object)
8980 return build3 (COMPONENT_REF, TREE_TYPE (template),
8981 object, template, NULL_TREE);
8982 else
8983 return baselink_for_fns (template);
8986 case INDIRECT_REF:
8988 tree r = RECUR (TREE_OPERAND (t, 0));
8990 if (REFERENCE_REF_P (t))
8992 /* A type conversion to reference type will be enclosed in
8993 such an indirect ref, but the substitution of the cast
8994 will have also added such an indirect ref. */
8995 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8996 r = convert_from_reference (r);
8998 else
8999 r = build_x_indirect_ref (r, "unary *");
9000 return r;
9003 case NOP_EXPR:
9004 return build_nop
9005 (tsubst (TREE_TYPE (t), args, complain, in_decl),
9006 RECUR (TREE_OPERAND (t, 0)));
9008 case CAST_EXPR:
9009 case REINTERPRET_CAST_EXPR:
9010 case CONST_CAST_EXPR:
9011 case DYNAMIC_CAST_EXPR:
9012 case STATIC_CAST_EXPR:
9014 tree type;
9015 tree op;
9017 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9018 if (integral_constant_expression_p
9019 && !cast_valid_in_integral_constant_expression_p (type))
9021 error ("a cast to a type other than an integral or "
9022 "enumeration type cannot appear in a constant-expression");
9023 return error_mark_node;
9026 op = RECUR (TREE_OPERAND (t, 0));
9028 switch (TREE_CODE (t))
9030 case CAST_EXPR:
9031 return build_functional_cast (type, op);
9032 case REINTERPRET_CAST_EXPR:
9033 return build_reinterpret_cast (type, op);
9034 case CONST_CAST_EXPR:
9035 return build_const_cast (type, op);
9036 case DYNAMIC_CAST_EXPR:
9037 return build_dynamic_cast (type, op);
9038 case STATIC_CAST_EXPR:
9039 return build_static_cast (type, op);
9040 default:
9041 gcc_unreachable ();
9045 case POSTDECREMENT_EXPR:
9046 case POSTINCREMENT_EXPR:
9047 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9048 args, complain, in_decl);
9049 return build_x_unary_op (TREE_CODE (t), op1);
9051 case PREDECREMENT_EXPR:
9052 case PREINCREMENT_EXPR:
9053 case NEGATE_EXPR:
9054 case BIT_NOT_EXPR:
9055 case ABS_EXPR:
9056 case TRUTH_NOT_EXPR:
9057 case UNARY_PLUS_EXPR: /* Unary + */
9058 case REALPART_EXPR:
9059 case IMAGPART_EXPR:
9060 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
9062 case ADDR_EXPR:
9063 op1 = TREE_OPERAND (t, 0);
9064 if (TREE_CODE (op1) == SCOPE_REF)
9065 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
9066 /*done=*/true, /*address_p=*/true);
9067 else
9068 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
9069 in_decl);
9070 if (TREE_CODE (op1) == LABEL_DECL)
9071 return finish_label_address_expr (DECL_NAME (op1));
9072 return build_x_unary_op (ADDR_EXPR, op1);
9074 case PLUS_EXPR:
9075 case MINUS_EXPR:
9076 case MULT_EXPR:
9077 case TRUNC_DIV_EXPR:
9078 case CEIL_DIV_EXPR:
9079 case FLOOR_DIV_EXPR:
9080 case ROUND_DIV_EXPR:
9081 case EXACT_DIV_EXPR:
9082 case BIT_AND_EXPR:
9083 case BIT_IOR_EXPR:
9084 case BIT_XOR_EXPR:
9085 case TRUNC_MOD_EXPR:
9086 case FLOOR_MOD_EXPR:
9087 case TRUTH_ANDIF_EXPR:
9088 case TRUTH_ORIF_EXPR:
9089 case TRUTH_AND_EXPR:
9090 case TRUTH_OR_EXPR:
9091 case RSHIFT_EXPR:
9092 case LSHIFT_EXPR:
9093 case RROTATE_EXPR:
9094 case LROTATE_EXPR:
9095 case EQ_EXPR:
9096 case NE_EXPR:
9097 case MAX_EXPR:
9098 case MIN_EXPR:
9099 case LE_EXPR:
9100 case GE_EXPR:
9101 case LT_EXPR:
9102 case GT_EXPR:
9103 case MEMBER_REF:
9104 case DOTSTAR_EXPR:
9105 return build_x_binary_op
9106 (TREE_CODE (t),
9107 RECUR (TREE_OPERAND (t, 0)),
9108 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
9109 ? ERROR_MARK
9110 : TREE_CODE (TREE_OPERAND (t, 0))),
9111 RECUR (TREE_OPERAND (t, 1)),
9112 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
9113 ? ERROR_MARK
9114 : TREE_CODE (TREE_OPERAND (t, 1))),
9115 /*overloaded_p=*/NULL);
9117 case SCOPE_REF:
9118 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
9119 /*address_p=*/false);
9120 case ARRAY_REF:
9121 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9122 args, complain, in_decl);
9123 return build_x_binary_op (ARRAY_REF, op1,
9124 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
9125 ? ERROR_MARK
9126 : TREE_CODE (TREE_OPERAND (t, 0))),
9127 RECUR (TREE_OPERAND (t, 1)),
9128 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
9129 ? ERROR_MARK
9130 : TREE_CODE (TREE_OPERAND (t, 1))),
9131 /*overloaded_p=*/NULL);
9133 case SIZEOF_EXPR:
9134 case ALIGNOF_EXPR:
9135 op1 = TREE_OPERAND (t, 0);
9136 if (!args)
9138 /* When there are no ARGS, we are trying to evaluate a
9139 non-dependent expression from the parser. Trying to do
9140 the substitutions may not work. */
9141 if (!TYPE_P (op1))
9142 op1 = TREE_TYPE (op1);
9144 else
9146 ++skip_evaluation;
9147 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9148 /*function_p=*/false,
9149 /*integral_constant_expression_p=*/false);
9150 --skip_evaluation;
9152 if (TYPE_P (op1))
9153 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
9154 else
9155 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
9157 case MODOP_EXPR:
9159 tree r = build_x_modify_expr
9160 (RECUR (TREE_OPERAND (t, 0)),
9161 TREE_CODE (TREE_OPERAND (t, 1)),
9162 RECUR (TREE_OPERAND (t, 2)));
9163 /* TREE_NO_WARNING must be set if either the expression was
9164 parenthesized or it uses an operator such as >>= rather
9165 than plain assignment. In the former case, it was already
9166 set and must be copied. In the latter case,
9167 build_x_modify_expr sets it and it must not be reset
9168 here. */
9169 if (TREE_NO_WARNING (t))
9170 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9171 return r;
9174 case ARROW_EXPR:
9175 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9176 args, complain, in_decl);
9177 /* Remember that there was a reference to this entity. */
9178 if (DECL_P (op1))
9179 mark_used (op1);
9180 return build_x_arrow (op1);
9182 case NEW_EXPR:
9183 return build_new
9184 (RECUR (TREE_OPERAND (t, 0)),
9185 RECUR (TREE_OPERAND (t, 1)),
9186 RECUR (TREE_OPERAND (t, 2)),
9187 RECUR (TREE_OPERAND (t, 3)),
9188 NEW_EXPR_USE_GLOBAL (t));
9190 case DELETE_EXPR:
9191 return delete_sanity
9192 (RECUR (TREE_OPERAND (t, 0)),
9193 RECUR (TREE_OPERAND (t, 1)),
9194 DELETE_EXPR_USE_VEC (t),
9195 DELETE_EXPR_USE_GLOBAL (t));
9197 case COMPOUND_EXPR:
9198 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9199 RECUR (TREE_OPERAND (t, 1)));
9201 case CALL_EXPR:
9203 tree function;
9204 tree call_args;
9205 bool qualified_p;
9206 bool koenig_p;
9208 function = TREE_OPERAND (t, 0);
9209 /* When we parsed the expression, we determined whether or
9210 not Koenig lookup should be performed. */
9211 koenig_p = KOENIG_LOOKUP_P (t);
9212 if (TREE_CODE (function) == SCOPE_REF)
9214 qualified_p = true;
9215 function = tsubst_qualified_id (function, args, complain, in_decl,
9216 /*done=*/false,
9217 /*address_p=*/false);
9219 else
9221 if (TREE_CODE (function) == COMPONENT_REF)
9223 tree op = TREE_OPERAND (function, 1);
9225 qualified_p = (TREE_CODE (op) == SCOPE_REF
9226 || (BASELINK_P (op)
9227 && BASELINK_QUALIFIED_P (op)));
9229 else
9230 qualified_p = false;
9232 function = tsubst_copy_and_build (function, args, complain,
9233 in_decl,
9234 !qualified_p,
9235 integral_constant_expression_p);
9237 if (BASELINK_P (function))
9238 qualified_p = true;
9241 call_args = RECUR (TREE_OPERAND (t, 1));
9243 /* We do not perform argument-dependent lookup if normal
9244 lookup finds a non-function, in accordance with the
9245 expected resolution of DR 218. */
9246 if (koenig_p
9247 && ((is_overloaded_fn (function)
9248 /* If lookup found a member function, the Koenig lookup is
9249 not appropriate, even if an unqualified-name was used
9250 to denote the function. */
9251 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
9252 || TREE_CODE (function) == IDENTIFIER_NODE))
9253 function = perform_koenig_lookup (function, call_args);
9255 if (TREE_CODE (function) == IDENTIFIER_NODE)
9257 unqualified_name_lookup_error (function);
9258 return error_mark_node;
9261 /* Remember that there was a reference to this entity. */
9262 if (DECL_P (function))
9263 mark_used (function);
9265 if (TREE_CODE (function) == OFFSET_REF)
9266 return build_offset_ref_call_from_tree (function, call_args);
9267 if (TREE_CODE (function) == COMPONENT_REF)
9269 if (!BASELINK_P (TREE_OPERAND (function, 1)))
9270 return finish_call_expr (function, call_args,
9271 /*disallow_virtual=*/false,
9272 /*koenig_p=*/false);
9273 else
9274 return (build_new_method_call
9275 (TREE_OPERAND (function, 0),
9276 TREE_OPERAND (function, 1),
9277 call_args, NULL_TREE,
9278 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9279 /*fn_p=*/NULL));
9281 return finish_call_expr (function, call_args,
9282 /*disallow_virtual=*/qualified_p,
9283 koenig_p);
9286 case COND_EXPR:
9287 return build_x_conditional_expr
9288 (RECUR (TREE_OPERAND (t, 0)),
9289 RECUR (TREE_OPERAND (t, 1)),
9290 RECUR (TREE_OPERAND (t, 2)));
9292 case PSEUDO_DTOR_EXPR:
9293 return finish_pseudo_destructor_expr
9294 (RECUR (TREE_OPERAND (t, 0)),
9295 RECUR (TREE_OPERAND (t, 1)),
9296 RECUR (TREE_OPERAND (t, 2)));
9298 case TREE_LIST:
9300 tree purpose, value, chain;
9302 if (t == void_list_node)
9303 return t;
9305 purpose = TREE_PURPOSE (t);
9306 if (purpose)
9307 purpose = RECUR (purpose);
9308 value = TREE_VALUE (t);
9309 if (value)
9310 value = RECUR (value);
9311 chain = TREE_CHAIN (t);
9312 if (chain && chain != void_type_node)
9313 chain = RECUR (chain);
9314 if (purpose == TREE_PURPOSE (t)
9315 && value == TREE_VALUE (t)
9316 && chain == TREE_CHAIN (t))
9317 return t;
9318 return tree_cons (purpose, value, chain);
9321 case COMPONENT_REF:
9323 tree object;
9324 tree object_type;
9325 tree member;
9327 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9328 args, complain, in_decl);
9329 /* Remember that there was a reference to this entity. */
9330 if (DECL_P (object))
9331 mark_used (object);
9332 object_type = TREE_TYPE (object);
9334 member = TREE_OPERAND (t, 1);
9335 if (BASELINK_P (member))
9336 member = tsubst_baselink (member,
9337 non_reference (TREE_TYPE (object)),
9338 args, complain, in_decl);
9339 else
9340 member = tsubst_copy (member, args, complain, in_decl);
9341 if (member == error_mark_node)
9342 return error_mark_node;
9344 if (object_type && !CLASS_TYPE_P (object_type))
9346 if (TREE_CODE (member) == BIT_NOT_EXPR)
9347 return finish_pseudo_destructor_expr (object,
9348 NULL_TREE,
9349 object_type);
9350 else if (TREE_CODE (member) == SCOPE_REF
9351 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
9352 return finish_pseudo_destructor_expr (object,
9353 object,
9354 object_type);
9356 else if (TREE_CODE (member) == SCOPE_REF
9357 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9359 tree tmpl;
9360 tree args;
9362 /* Lookup the template functions now that we know what the
9363 scope is. */
9364 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9365 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
9366 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
9367 /*is_type_p=*/false,
9368 /*complain=*/false);
9369 if (BASELINK_P (member))
9371 BASELINK_FUNCTIONS (member)
9372 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9373 args);
9374 member = (adjust_result_of_qualified_name_lookup
9375 (member, BINFO_TYPE (BASELINK_BINFO (member)),
9376 object_type));
9378 else
9380 qualified_name_lookup_error (object_type, tmpl, member);
9381 return error_mark_node;
9384 else if (TREE_CODE (member) == SCOPE_REF
9385 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9386 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9388 if (complain & tf_error)
9390 if (TYPE_P (TREE_OPERAND (member, 0)))
9391 error ("%qT is not a class or namespace",
9392 TREE_OPERAND (member, 0));
9393 else
9394 error ("%qD is not a class or namespace",
9395 TREE_OPERAND (member, 0));
9397 return error_mark_node;
9399 else if (TREE_CODE (member) == FIELD_DECL)
9400 return finish_non_static_data_member (member, object, NULL_TREE);
9402 return finish_class_member_access_expr (object, member,
9403 /*template_p=*/false);
9406 case THROW_EXPR:
9407 return build_throw
9408 (RECUR (TREE_OPERAND (t, 0)));
9410 case CONSTRUCTOR:
9412 VEC(constructor_elt,gc) *n;
9413 constructor_elt *ce;
9414 unsigned HOST_WIDE_INT idx;
9415 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9416 bool process_index_p;
9418 if (type == error_mark_node)
9419 return error_mark_node;
9421 /* digest_init will do the wrong thing if we let it. */
9422 if (type && TYPE_PTRMEMFUNC_P (type))
9423 return t;
9425 /* We do not want to process the index of aggregate
9426 initializers as they are identifier nodes which will be
9427 looked up by digest_init. */
9428 process_index_p = !(type && IS_AGGR_TYPE (type));
9430 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9431 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9433 if (ce->index && process_index_p)
9434 ce->index = RECUR (ce->index);
9435 ce->value = RECUR (ce->value);
9438 if (TREE_HAS_CONSTRUCTOR (t))
9439 return finish_compound_literal (type, n);
9441 return build_constructor (NULL_TREE, n);
9444 case TYPEID_EXPR:
9446 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9447 if (TYPE_P (operand_0))
9448 return get_typeid (operand_0);
9449 return build_typeid (operand_0);
9452 case VAR_DECL:
9453 if (!args)
9454 return t;
9455 /* Fall through */
9457 case PARM_DECL:
9459 tree r = tsubst_copy (t, args, complain, in_decl);
9461 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9462 /* If the original type was a reference, we'll be wrapped in
9463 the appropriate INDIRECT_REF. */
9464 r = convert_from_reference (r);
9465 return r;
9468 case VA_ARG_EXPR:
9469 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9470 tsubst_copy (TREE_TYPE (t), args, complain,
9471 in_decl));
9473 case OFFSETOF_EXPR:
9474 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
9476 case STMT_EXPR:
9478 tree old_stmt_expr = cur_stmt_expr;
9479 tree stmt_expr = begin_stmt_expr ();
9481 cur_stmt_expr = stmt_expr;
9482 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9483 integral_constant_expression_p);
9484 stmt_expr = finish_stmt_expr (stmt_expr, false);
9485 cur_stmt_expr = old_stmt_expr;
9487 return stmt_expr;
9490 case CONST_DECL:
9491 t = tsubst_copy (t, args, complain, in_decl);
9492 /* As in finish_id_expression, we resolve enumeration constants
9493 to their underlying values. */
9494 if (TREE_CODE (t) == CONST_DECL)
9496 used_types_insert (TREE_TYPE (t));
9497 return DECL_INITIAL (t);
9499 return t;
9501 default:
9502 /* Handle Objective-C++ constructs, if appropriate. */
9504 tree subst
9505 = objcp_tsubst_copy_and_build (t, args, complain,
9506 in_decl, /*function_p=*/false);
9507 if (subst)
9508 return subst;
9510 return tsubst_copy (t, args, complain, in_decl);
9513 #undef RECUR
9516 /* Verify that the instantiated ARGS are valid. For type arguments,
9517 make sure that the type's linkage is ok. For non-type arguments,
9518 make sure they are constants if they are integral or enumerations.
9519 Emit an error under control of COMPLAIN, and return TRUE on error. */
9521 static bool
9522 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9524 int ix, len = DECL_NTPARMS (tmpl);
9525 bool result = false;
9527 for (ix = 0; ix != len; ix++)
9529 tree t = TREE_VEC_ELT (args, ix);
9531 if (TYPE_P (t))
9533 /* [basic.link]: A name with no linkage (notably, the name
9534 of a class or enumeration declared in a local scope)
9535 shall not be used to declare an entity with linkage.
9536 This implies that names with no linkage cannot be used as
9537 template arguments. */
9538 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9540 if (nt)
9542 /* DR 488 makes use of a type with no linkage cause
9543 type deduction to fail. */
9544 if (complain & tf_error)
9546 if (TYPE_ANONYMOUS_P (nt))
9547 error ("%qT is/uses anonymous type", t);
9548 else
9549 error ("template argument for %qD uses local type %qT",
9550 tmpl, t);
9552 result = true;
9554 /* In order to avoid all sorts of complications, we do not
9555 allow variably-modified types as template arguments. */
9556 else if (variably_modified_type_p (t, NULL_TREE))
9558 if (complain & tf_error)
9559 error ("%qT is a variably modified type", t);
9560 result = true;
9563 /* A non-type argument of integral or enumerated type must be a
9564 constant. */
9565 else if (TREE_TYPE (t)
9566 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9567 && !TREE_CONSTANT (t))
9569 if (complain & tf_error)
9570 error ("integral expression %qE is not constant", t);
9571 result = true;
9574 if (result && (complain & tf_error))
9575 error (" trying to instantiate %qD", tmpl);
9576 return result;
9579 /* Instantiate the indicated variable or function template TMPL with
9580 the template arguments in TARG_PTR. */
9582 tree
9583 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9585 tree fndecl;
9586 tree gen_tmpl;
9587 tree spec;
9588 HOST_WIDE_INT saved_processing_template_decl;
9590 if (tmpl == error_mark_node)
9591 return error_mark_node;
9593 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9595 /* If this function is a clone, handle it specially. */
9596 if (DECL_CLONED_FUNCTION_P (tmpl))
9598 tree spec;
9599 tree clone;
9601 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9602 complain);
9603 if (spec == error_mark_node)
9604 return error_mark_node;
9606 /* Look for the clone. */
9607 FOR_EACH_CLONE (clone, spec)
9608 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9609 return clone;
9610 /* We should always have found the clone by now. */
9611 gcc_unreachable ();
9612 return NULL_TREE;
9615 /* Check to see if we already have this specialization. */
9616 spec = retrieve_specialization (tmpl, targ_ptr,
9617 /*class_specializations_p=*/false);
9618 if (spec != NULL_TREE)
9619 return spec;
9621 gen_tmpl = most_general_template (tmpl);
9622 if (tmpl != gen_tmpl)
9624 /* The TMPL is a partial instantiation. To get a full set of
9625 arguments we must add the arguments used to perform the
9626 partial instantiation. */
9627 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9628 targ_ptr);
9630 /* Check to see if we already have this specialization. */
9631 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9632 /*class_specializations_p=*/false);
9633 if (spec != NULL_TREE)
9634 return spec;
9637 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9638 complain))
9639 return error_mark_node;
9641 /* We are building a FUNCTION_DECL, during which the access of its
9642 parameters and return types have to be checked. However this
9643 FUNCTION_DECL which is the desired context for access checking
9644 is not built yet. We solve this chicken-and-egg problem by
9645 deferring all checks until we have the FUNCTION_DECL. */
9646 push_deferring_access_checks (dk_deferred);
9648 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9649 (because, for example, we have encountered a non-dependent
9650 function call in the body of a template function and must now
9651 determine which of several overloaded functions will be called),
9652 within the instantiation itself we are not processing a
9653 template. */
9654 saved_processing_template_decl = processing_template_decl;
9655 processing_template_decl = 0;
9656 /* Substitute template parameters to obtain the specialization. */
9657 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9658 targ_ptr, complain, gen_tmpl);
9659 processing_template_decl = saved_processing_template_decl;
9660 if (fndecl == error_mark_node)
9661 return error_mark_node;
9663 /* Now we know the specialization, compute access previously
9664 deferred. */
9665 push_access_scope (fndecl);
9666 perform_deferred_access_checks ();
9667 pop_access_scope (fndecl);
9668 pop_deferring_access_checks ();
9670 /* The DECL_TI_TEMPLATE should always be the immediate parent
9671 template, not the most general template. */
9672 DECL_TI_TEMPLATE (fndecl) = tmpl;
9674 /* If we've just instantiated the main entry point for a function,
9675 instantiate all the alternate entry points as well. We do this
9676 by cloning the instantiation of the main entry point, not by
9677 instantiating the template clones. */
9678 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9679 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9681 return fndecl;
9684 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9685 arguments that are being used when calling it. TARGS is a vector
9686 into which the deduced template arguments are placed.
9688 Return zero for success, 2 for an incomplete match that doesn't resolve
9689 all the types, and 1 for complete failure. An error message will be
9690 printed only for an incomplete match.
9692 If FN is a conversion operator, or we are trying to produce a specific
9693 specialization, RETURN_TYPE is the return type desired.
9695 The EXPLICIT_TARGS are explicit template arguments provided via a
9696 template-id.
9698 The parameter STRICT is one of:
9700 DEDUCE_CALL:
9701 We are deducing arguments for a function call, as in
9702 [temp.deduct.call].
9704 DEDUCE_CONV:
9705 We are deducing arguments for a conversion function, as in
9706 [temp.deduct.conv].
9708 DEDUCE_EXACT:
9709 We are deducing arguments when doing an explicit instantiation
9710 as in [temp.explicit], when determining an explicit specialization
9711 as in [temp.expl.spec], or when taking the address of a function
9712 template, as in [temp.deduct.funcaddr]. */
9715 fn_type_unification (tree fn,
9716 tree explicit_targs,
9717 tree targs,
9718 tree args,
9719 tree return_type,
9720 unification_kind_t strict,
9721 int flags)
9723 tree parms;
9724 tree fntype;
9725 int result;
9727 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9729 fntype = TREE_TYPE (fn);
9730 if (explicit_targs)
9732 /* [temp.deduct]
9734 The specified template arguments must match the template
9735 parameters in kind (i.e., type, nontype, template), and there
9736 must not be more arguments than there are parameters;
9737 otherwise type deduction fails.
9739 Nontype arguments must match the types of the corresponding
9740 nontype template parameters, or must be convertible to the
9741 types of the corresponding nontype parameters as specified in
9742 _temp.arg.nontype_, otherwise type deduction fails.
9744 All references in the function type of the function template
9745 to the corresponding template parameters are replaced by the
9746 specified template argument values. If a substitution in a
9747 template parameter or in the function type of the function
9748 template results in an invalid type, type deduction fails. */
9749 int i;
9750 tree converted_args;
9751 bool incomplete;
9753 if (explicit_targs == error_mark_node)
9754 return 1;
9756 converted_args
9757 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9758 explicit_targs, NULL_TREE, tf_none,
9759 /*require_all_args=*/false,
9760 /*use_default_args=*/false));
9761 if (converted_args == error_mark_node)
9762 return 1;
9764 /* Substitute the explicit args into the function type. This is
9765 necessary so that, for instance, explicitly declared function
9766 arguments can match null pointed constants. If we were given
9767 an incomplete set of explicit args, we must not do semantic
9768 processing during substitution as we could create partial
9769 instantiations. */
9770 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9771 processing_template_decl += incomplete;
9772 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9773 processing_template_decl -= incomplete;
9775 if (fntype == error_mark_node)
9776 return 1;
9778 /* Place the explicitly specified arguments in TARGS. */
9779 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9780 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9783 parms = TYPE_ARG_TYPES (fntype);
9784 /* Never do unification on the 'this' parameter. */
9785 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9786 parms = TREE_CHAIN (parms);
9788 if (return_type)
9790 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9791 args = tree_cons (NULL_TREE, return_type, args);
9794 /* We allow incomplete unification without an error message here
9795 because the standard doesn't seem to explicitly prohibit it. Our
9796 callers must be ready to deal with unification failures in any
9797 event. */
9798 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9799 targs, parms, args, /*subr=*/0,
9800 strict, flags);
9802 if (result == 0)
9803 /* All is well so far. Now, check:
9805 [temp.deduct]
9807 When all template arguments have been deduced, all uses of
9808 template parameters in nondeduced contexts are replaced with
9809 the corresponding deduced argument values. If the
9810 substitution results in an invalid type, as described above,
9811 type deduction fails. */
9812 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9813 == error_mark_node)
9814 return 1;
9816 return result;
9819 /* Adjust types before performing type deduction, as described in
9820 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9821 sections are symmetric. PARM is the type of a function parameter
9822 or the return type of the conversion function. ARG is the type of
9823 the argument passed to the call, or the type of the value
9824 initialized with the result of the conversion function. */
9826 static int
9827 maybe_adjust_types_for_deduction (unification_kind_t strict,
9828 tree* parm,
9829 tree* arg)
9831 int result = 0;
9833 switch (strict)
9835 case DEDUCE_CALL:
9836 break;
9838 case DEDUCE_CONV:
9840 /* Swap PARM and ARG throughout the remainder of this
9841 function; the handling is precisely symmetric since PARM
9842 will initialize ARG rather than vice versa. */
9843 tree* temp = parm;
9844 parm = arg;
9845 arg = temp;
9846 break;
9849 case DEDUCE_EXACT:
9850 /* There is nothing to do in this case. */
9851 return 0;
9853 default:
9854 gcc_unreachable ();
9857 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9859 /* [temp.deduct.call]
9861 If P is not a reference type:
9863 --If A is an array type, the pointer type produced by the
9864 array-to-pointer standard conversion (_conv.array_) is
9865 used in place of A for type deduction; otherwise,
9867 --If A is a function type, the pointer type produced by
9868 the function-to-pointer standard conversion
9869 (_conv.func_) is used in place of A for type deduction;
9870 otherwise,
9872 --If A is a cv-qualified type, the top level
9873 cv-qualifiers of A's type are ignored for type
9874 deduction. */
9875 if (TREE_CODE (*arg) == ARRAY_TYPE)
9876 *arg = build_pointer_type (TREE_TYPE (*arg));
9877 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9878 *arg = build_pointer_type (*arg);
9879 else
9880 *arg = TYPE_MAIN_VARIANT (*arg);
9883 /* [temp.deduct.call]
9885 If P is a cv-qualified type, the top level cv-qualifiers
9886 of P's type are ignored for type deduction. If P is a
9887 reference type, the type referred to by P is used for
9888 type deduction. */
9889 *parm = TYPE_MAIN_VARIANT (*parm);
9890 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9892 *parm = TREE_TYPE (*parm);
9893 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9896 /* DR 322. For conversion deduction, remove a reference type on parm
9897 too (which has been swapped into ARG). */
9898 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9899 *arg = TREE_TYPE (*arg);
9901 return result;
9904 /* Most parms like fn_type_unification.
9906 If SUBR is 1, we're being called recursively (to unify the
9907 arguments of a function or method parameter of a function
9908 template). */
9910 static int
9911 type_unification_real (tree tparms,
9912 tree targs,
9913 tree xparms,
9914 tree xargs,
9915 int subr,
9916 unification_kind_t strict,
9917 int flags)
9919 tree parm, arg;
9920 int i;
9921 int ntparms = TREE_VEC_LENGTH (tparms);
9922 int sub_strict;
9923 int saw_undeduced = 0;
9924 tree parms, args;
9926 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9927 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9928 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9929 gcc_assert (ntparms > 0);
9931 switch (strict)
9933 case DEDUCE_CALL:
9934 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9935 | UNIFY_ALLOW_DERIVED);
9936 break;
9938 case DEDUCE_CONV:
9939 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9940 break;
9942 case DEDUCE_EXACT:
9943 sub_strict = UNIFY_ALLOW_NONE;
9944 break;
9946 default:
9947 gcc_unreachable ();
9950 again:
9951 parms = xparms;
9952 args = xargs;
9954 while (parms && parms != void_list_node
9955 && args && args != void_list_node)
9957 parm = TREE_VALUE (parms);
9958 parms = TREE_CHAIN (parms);
9959 arg = TREE_VALUE (args);
9960 args = TREE_CHAIN (args);
9962 if (arg == error_mark_node)
9963 return 1;
9964 if (arg == unknown_type_node)
9965 /* We can't deduce anything from this, but we might get all the
9966 template args from other function args. */
9967 continue;
9969 /* Conversions will be performed on a function argument that
9970 corresponds with a function parameter that contains only
9971 non-deducible template parameters and explicitly specified
9972 template parameters. */
9973 if (!uses_template_parms (parm))
9975 tree type;
9977 if (!TYPE_P (arg))
9978 type = TREE_TYPE (arg);
9979 else
9980 type = arg;
9982 if (same_type_p (parm, type))
9983 continue;
9984 if (strict != DEDUCE_EXACT
9985 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9986 flags))
9987 continue;
9989 return 1;
9992 if (!TYPE_P (arg))
9994 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9995 if (type_unknown_p (arg))
9997 /* [temp.deduct.type] A template-argument can be deduced from
9998 a pointer to function or pointer to member function
9999 argument if the set of overloaded functions does not
10000 contain function templates and at most one of a set of
10001 overloaded functions provides a unique match. */
10003 if (resolve_overloaded_unification
10004 (tparms, targs, parm, arg, strict, sub_strict)
10005 != 0)
10006 return 1;
10007 continue;
10009 arg = TREE_TYPE (arg);
10010 if (arg == error_mark_node)
10011 return 1;
10015 int arg_strict = sub_strict;
10017 if (!subr)
10018 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
10020 if (unify (tparms, targs, parm, arg, arg_strict))
10021 return 1;
10025 /* Fail if we've reached the end of the parm list, and more args
10026 are present, and the parm list isn't variadic. */
10027 if (args && args != void_list_node && parms == void_list_node)
10028 return 1;
10029 /* Fail if parms are left and they don't have default values. */
10030 if (parms && parms != void_list_node
10031 && TREE_PURPOSE (parms) == NULL_TREE)
10032 return 1;
10034 if (!subr)
10035 for (i = 0; i < ntparms; i++)
10036 if (!TREE_VEC_ELT (targs, i))
10038 tree tparm;
10040 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
10041 continue;
10043 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
10045 /* If this is an undeduced nontype parameter that depends on
10046 a type parameter, try another pass; its type may have been
10047 deduced from a later argument than the one from which
10048 this parameter can be deduced. */
10049 if (TREE_CODE (tparm) == PARM_DECL
10050 && uses_template_parms (TREE_TYPE (tparm))
10051 && !saw_undeduced++)
10052 goto again;
10054 return 2;
10057 return 0;
10060 /* Subroutine of type_unification_real. Args are like the variables at the
10061 call site. ARG is an overloaded function (or template-id); we try
10062 deducing template args from each of the overloads, and if only one
10063 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
10065 static int
10066 resolve_overloaded_unification (tree tparms,
10067 tree targs,
10068 tree parm,
10069 tree arg,
10070 unification_kind_t strict,
10071 int sub_strict)
10073 tree tempargs = copy_node (targs);
10074 int good = 0;
10075 bool addr_p;
10077 if (TREE_CODE (arg) == ADDR_EXPR)
10079 arg = TREE_OPERAND (arg, 0);
10080 addr_p = true;
10082 else
10083 addr_p = false;
10085 if (TREE_CODE (arg) == COMPONENT_REF)
10086 /* Handle `&x' where `x' is some static or non-static member
10087 function name. */
10088 arg = TREE_OPERAND (arg, 1);
10090 if (TREE_CODE (arg) == OFFSET_REF)
10091 arg = TREE_OPERAND (arg, 1);
10093 /* Strip baselink information. */
10094 if (BASELINK_P (arg))
10095 arg = BASELINK_FUNCTIONS (arg);
10097 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
10099 /* If we got some explicit template args, we need to plug them into
10100 the affected templates before we try to unify, in case the
10101 explicit args will completely resolve the templates in question. */
10103 tree expl_subargs = TREE_OPERAND (arg, 1);
10104 arg = TREE_OPERAND (arg, 0);
10106 for (; arg; arg = OVL_NEXT (arg))
10108 tree fn = OVL_CURRENT (arg);
10109 tree subargs, elem;
10111 if (TREE_CODE (fn) != TEMPLATE_DECL)
10112 continue;
10114 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
10115 expl_subargs, /*check_ret=*/false);
10116 if (subargs)
10118 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
10119 good += try_one_overload (tparms, targs, tempargs, parm,
10120 elem, strict, sub_strict, addr_p);
10124 else
10126 gcc_assert (TREE_CODE (arg) == OVERLOAD
10127 || TREE_CODE (arg) == FUNCTION_DECL);
10129 for (; arg; arg = OVL_NEXT (arg))
10130 good += try_one_overload (tparms, targs, tempargs, parm,
10131 TREE_TYPE (OVL_CURRENT (arg)),
10132 strict, sub_strict, addr_p);
10135 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10136 to function or pointer to member function argument if the set of
10137 overloaded functions does not contain function templates and at most
10138 one of a set of overloaded functions provides a unique match.
10140 So if we found multiple possibilities, we return success but don't
10141 deduce anything. */
10143 if (good == 1)
10145 int i = TREE_VEC_LENGTH (targs);
10146 for (; i--; )
10147 if (TREE_VEC_ELT (tempargs, i))
10148 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
10150 if (good)
10151 return 0;
10153 return 1;
10156 /* Subroutine of resolve_overloaded_unification; does deduction for a single
10157 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10158 different overloads deduce different arguments for a given parm.
10159 ADDR_P is true if the expression for which deduction is being
10160 performed was of the form "& fn" rather than simply "fn".
10162 Returns 1 on success. */
10164 static int
10165 try_one_overload (tree tparms,
10166 tree orig_targs,
10167 tree targs,
10168 tree parm,
10169 tree arg,
10170 unification_kind_t strict,
10171 int sub_strict,
10172 bool addr_p)
10174 int nargs;
10175 tree tempargs;
10176 int i;
10178 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10179 to function or pointer to member function argument if the set of
10180 overloaded functions does not contain function templates and at most
10181 one of a set of overloaded functions provides a unique match.
10183 So if this is a template, just return success. */
10185 if (uses_template_parms (arg))
10186 return 1;
10188 if (TREE_CODE (arg) == METHOD_TYPE)
10189 arg = build_ptrmemfunc_type (build_pointer_type (arg));
10190 else if (addr_p)
10191 arg = build_pointer_type (arg);
10193 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
10195 /* We don't copy orig_targs for this because if we have already deduced
10196 some template args from previous args, unify would complain when we
10197 try to deduce a template parameter for the same argument, even though
10198 there isn't really a conflict. */
10199 nargs = TREE_VEC_LENGTH (targs);
10200 tempargs = make_tree_vec (nargs);
10202 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
10203 return 0;
10205 /* First make sure we didn't deduce anything that conflicts with
10206 explicitly specified args. */
10207 for (i = nargs; i--; )
10209 tree elt = TREE_VEC_ELT (tempargs, i);
10210 tree oldelt = TREE_VEC_ELT (orig_targs, i);
10212 if (!elt)
10213 /*NOP*/;
10214 else if (uses_template_parms (elt))
10215 /* Since we're unifying against ourselves, we will fill in
10216 template args used in the function parm list with our own
10217 template parms. Discard them. */
10218 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10219 else if (oldelt && !template_args_equal (oldelt, elt))
10220 return 0;
10223 for (i = nargs; i--; )
10225 tree elt = TREE_VEC_ELT (tempargs, i);
10227 if (elt)
10228 TREE_VEC_ELT (targs, i) = elt;
10231 return 1;
10234 /* PARM is a template class (perhaps with unbound template
10235 parameters). ARG is a fully instantiated type. If ARG can be
10236 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10237 TARGS are as for unify. */
10239 static tree
10240 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
10242 tree copy_of_targs;
10244 if (!CLASSTYPE_TEMPLATE_INFO (arg)
10245 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
10246 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
10247 return NULL_TREE;
10249 /* We need to make a new template argument vector for the call to
10250 unify. If we used TARGS, we'd clutter it up with the result of
10251 the attempted unification, even if this class didn't work out.
10252 We also don't want to commit ourselves to all the unifications
10253 we've already done, since unification is supposed to be done on
10254 an argument-by-argument basis. In other words, consider the
10255 following pathological case:
10257 template <int I, int J, int K>
10258 struct S {};
10260 template <int I, int J>
10261 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
10263 template <int I, int J, int K>
10264 void f(S<I, J, K>, S<I, I, I>);
10266 void g() {
10267 S<0, 0, 0> s0;
10268 S<0, 1, 2> s2;
10270 f(s0, s2);
10273 Now, by the time we consider the unification involving `s2', we
10274 already know that we must have `f<0, 0, 0>'. But, even though
10275 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
10276 because there are two ways to unify base classes of S<0, 1, 2>
10277 with S<I, I, I>. If we kept the already deduced knowledge, we
10278 would reject the possibility I=1. */
10279 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
10281 /* If unification failed, we're done. */
10282 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
10283 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
10284 return NULL_TREE;
10286 return arg;
10289 /* Given a template type PARM and a class type ARG, find the unique
10290 base type in ARG that is an instance of PARM. We do not examine
10291 ARG itself; only its base-classes. If there is not exactly one
10292 appropriate base class, return NULL_TREE. PARM may be the type of
10293 a partial specialization, as well as a plain template type. Used
10294 by unify. */
10296 static tree
10297 get_template_base (tree tparms, tree targs, tree parm, tree arg)
10299 tree rval = NULL_TREE;
10300 tree binfo;
10302 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
10304 binfo = TYPE_BINFO (complete_type (arg));
10305 if (!binfo)
10306 /* The type could not be completed. */
10307 return NULL_TREE;
10309 /* Walk in inheritance graph order. The search order is not
10310 important, and this avoids multiple walks of virtual bases. */
10311 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
10313 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
10315 if (r)
10317 /* If there is more than one satisfactory baseclass, then:
10319 [temp.deduct.call]
10321 If they yield more than one possible deduced A, the type
10322 deduction fails.
10324 applies. */
10325 if (rval && !same_type_p (r, rval))
10326 return NULL_TREE;
10328 rval = r;
10332 return rval;
10335 /* Returns the level of DECL, which declares a template parameter. */
10337 static int
10338 template_decl_level (tree decl)
10340 switch (TREE_CODE (decl))
10342 case TYPE_DECL:
10343 case TEMPLATE_DECL:
10344 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10346 case PARM_DECL:
10347 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10349 default:
10350 gcc_unreachable ();
10352 return 0;
10355 /* Decide whether ARG can be unified with PARM, considering only the
10356 cv-qualifiers of each type, given STRICT as documented for unify.
10357 Returns nonzero iff the unification is OK on that basis. */
10359 static int
10360 check_cv_quals_for_unify (int strict, tree arg, tree parm)
10362 int arg_quals = cp_type_quals (arg);
10363 int parm_quals = cp_type_quals (parm);
10365 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10366 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10368 /* Although a CVR qualifier is ignored when being applied to a
10369 substituted template parameter ([8.3.2]/1 for example), that
10370 does not apply during deduction [14.8.2.4]/1, (even though
10371 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10372 this). Except when we're allowing additional CV qualifiers
10373 at the outer level [14.8.2.1]/3,1st bullet. */
10374 if ((TREE_CODE (arg) == REFERENCE_TYPE
10375 || TREE_CODE (arg) == FUNCTION_TYPE
10376 || TREE_CODE (arg) == METHOD_TYPE)
10377 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10378 return 0;
10380 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10381 && (parm_quals & TYPE_QUAL_RESTRICT))
10382 return 0;
10385 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10386 && (arg_quals & parm_quals) != parm_quals)
10387 return 0;
10389 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
10390 && (parm_quals & arg_quals) != arg_quals)
10391 return 0;
10393 return 1;
10396 /* Deduce the value of template parameters. TPARMS is the (innermost)
10397 set of template parameters to a template. TARGS is the bindings
10398 for those template parameters, as determined thus far; TARGS may
10399 include template arguments for outer levels of template parameters
10400 as well. PARM is a parameter to a template function, or a
10401 subcomponent of that parameter; ARG is the corresponding argument.
10402 This function attempts to match PARM with ARG in a manner
10403 consistent with the existing assignments in TARGS. If more values
10404 are deduced, then TARGS is updated.
10406 Returns 0 if the type deduction succeeds, 1 otherwise. The
10407 parameter STRICT is a bitwise or of the following flags:
10409 UNIFY_ALLOW_NONE:
10410 Require an exact match between PARM and ARG.
10411 UNIFY_ALLOW_MORE_CV_QUAL:
10412 Allow the deduced ARG to be more cv-qualified (by qualification
10413 conversion) than ARG.
10414 UNIFY_ALLOW_LESS_CV_QUAL:
10415 Allow the deduced ARG to be less cv-qualified than ARG.
10416 UNIFY_ALLOW_DERIVED:
10417 Allow the deduced ARG to be a template base class of ARG,
10418 or a pointer to a template base class of the type pointed to by
10419 ARG.
10420 UNIFY_ALLOW_INTEGER:
10421 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10422 case for more information.
10423 UNIFY_ALLOW_OUTER_LEVEL:
10424 This is the outermost level of a deduction. Used to determine validity
10425 of qualification conversions. A valid qualification conversion must
10426 have const qualified pointers leading up to the inner type which
10427 requires additional CV quals, except at the outer level, where const
10428 is not required [conv.qual]. It would be normal to set this flag in
10429 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10430 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10431 This is the outermost level of a deduction, and PARM can be more CV
10432 qualified at this point.
10433 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10434 This is the outermost level of a deduction, and PARM can be less CV
10435 qualified at this point. */
10437 static int
10438 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10440 int idx;
10441 tree targ;
10442 tree tparm;
10443 int strict_in = strict;
10445 /* I don't think this will do the right thing with respect to types.
10446 But the only case I've seen it in so far has been array bounds, where
10447 signedness is the only information lost, and I think that will be
10448 okay. */
10449 while (TREE_CODE (parm) == NOP_EXPR)
10450 parm = TREE_OPERAND (parm, 0);
10452 if (arg == error_mark_node)
10453 return 1;
10454 if (arg == unknown_type_node)
10455 /* We can't deduce anything from this, but we might get all the
10456 template args from other function args. */
10457 return 0;
10459 /* If PARM uses template parameters, then we can't bail out here,
10460 even if ARG == PARM, since we won't record unifications for the
10461 template parameters. We might need them if we're trying to
10462 figure out which of two things is more specialized. */
10463 if (arg == parm && !uses_template_parms (parm))
10464 return 0;
10466 /* Immediately reject some pairs that won't unify because of
10467 cv-qualification mismatches. */
10468 if (TREE_CODE (arg) == TREE_CODE (parm)
10469 && TYPE_P (arg)
10470 /* It is the elements of the array which hold the cv quals of an array
10471 type, and the elements might be template type parms. We'll check
10472 when we recurse. */
10473 && TREE_CODE (arg) != ARRAY_TYPE
10474 /* We check the cv-qualifiers when unifying with template type
10475 parameters below. We want to allow ARG `const T' to unify with
10476 PARM `T' for example, when computing which of two templates
10477 is more specialized, for example. */
10478 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10479 && !check_cv_quals_for_unify (strict_in, arg, parm))
10480 return 1;
10482 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10483 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10484 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10485 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10486 strict &= ~UNIFY_ALLOW_DERIVED;
10487 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10488 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10490 switch (TREE_CODE (parm))
10492 case TYPENAME_TYPE:
10493 case SCOPE_REF:
10494 case UNBOUND_CLASS_TEMPLATE:
10495 /* In a type which contains a nested-name-specifier, template
10496 argument values cannot be deduced for template parameters used
10497 within the nested-name-specifier. */
10498 return 0;
10500 case TEMPLATE_TYPE_PARM:
10501 case TEMPLATE_TEMPLATE_PARM:
10502 case BOUND_TEMPLATE_TEMPLATE_PARM:
10503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10505 if (TEMPLATE_TYPE_LEVEL (parm)
10506 != template_decl_level (tparm))
10507 /* The PARM is not one we're trying to unify. Just check
10508 to see if it matches ARG. */
10509 return (TREE_CODE (arg) == TREE_CODE (parm)
10510 && same_type_p (parm, arg)) ? 0 : 1;
10511 idx = TEMPLATE_TYPE_IDX (parm);
10512 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10513 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10515 /* Check for mixed types and values. */
10516 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10517 && TREE_CODE (tparm) != TYPE_DECL)
10518 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10519 && TREE_CODE (tparm) != TEMPLATE_DECL))
10520 return 1;
10522 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10524 /* ARG must be constructed from a template class or a template
10525 template parameter. */
10526 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10527 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
10528 return 1;
10531 tree parmvec = TYPE_TI_ARGS (parm);
10532 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10533 tree argtmplvec
10534 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10535 int i;
10537 /* The resolution to DR150 makes clear that default
10538 arguments for an N-argument may not be used to bind T
10539 to a template template parameter with fewer than N
10540 parameters. It is not safe to permit the binding of
10541 default arguments as an extension, as that may change
10542 the meaning of a conforming program. Consider:
10544 struct Dense { static const unsigned int dim = 1; };
10546 template <template <typename> class View,
10547 typename Block>
10548 void operator+(float, View<Block> const&);
10550 template <typename Block,
10551 unsigned int Dim = Block::dim>
10552 struct Lvalue_proxy { operator float() const; };
10554 void
10555 test_1d (void) {
10556 Lvalue_proxy<Dense> p;
10557 float b;
10558 b + p;
10561 Here, if Lvalue_proxy is permitted to bind to View, then
10562 the global operator+ will be used; if they are not, the
10563 Lvalue_proxy will be converted to float. */
10564 if (coerce_template_parms (argtmplvec, parmvec,
10565 TYPE_TI_TEMPLATE (parm),
10566 tf_none,
10567 /*require_all_args=*/true,
10568 /*use_default_args=*/false)
10569 == error_mark_node)
10570 return 1;
10572 /* Deduce arguments T, i from TT<T> or TT<i>.
10573 We check each element of PARMVEC and ARGVEC individually
10574 rather than the whole TREE_VEC since they can have
10575 different number of elements. */
10577 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10579 if (unify (tparms, targs,
10580 TREE_VEC_ELT (parmvec, i),
10581 TREE_VEC_ELT (argvec, i),
10582 UNIFY_ALLOW_NONE))
10583 return 1;
10586 arg = TYPE_TI_TEMPLATE (arg);
10588 /* Fall through to deduce template name. */
10591 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10592 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10594 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10596 /* Simple cases: Value already set, does match or doesn't. */
10597 if (targ != NULL_TREE && template_args_equal (targ, arg))
10598 return 0;
10599 else if (targ)
10600 return 1;
10602 else
10604 /* If PARM is `const T' and ARG is only `int', we don't have
10605 a match unless we are allowing additional qualification.
10606 If ARG is `const int' and PARM is just `T' that's OK;
10607 that binds `const int' to `T'. */
10608 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10609 arg, parm))
10610 return 1;
10612 /* Consider the case where ARG is `const volatile int' and
10613 PARM is `const T'. Then, T should be `volatile int'. */
10614 arg = cp_build_qualified_type_real
10615 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10616 if (arg == error_mark_node)
10617 return 1;
10619 /* Simple cases: Value already set, does match or doesn't. */
10620 if (targ != NULL_TREE && same_type_p (targ, arg))
10621 return 0;
10622 else if (targ)
10623 return 1;
10625 /* Make sure that ARG is not a variable-sized array. (Note
10626 that were talking about variable-sized arrays (like
10627 `int[n]'), rather than arrays of unknown size (like
10628 `int[]').) We'll get very confused by such a type since
10629 the bound of the array will not be computable in an
10630 instantiation. Besides, such types are not allowed in
10631 ISO C++, so we can do as we please here. */
10632 if (variably_modified_type_p (arg, NULL_TREE))
10633 return 1;
10636 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10637 return 0;
10639 case TEMPLATE_PARM_INDEX:
10640 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10641 if (tparm == error_mark_node)
10642 return 1;
10644 if (TEMPLATE_PARM_LEVEL (parm)
10645 != template_decl_level (tparm))
10646 /* The PARM is not one we're trying to unify. Just check
10647 to see if it matches ARG. */
10648 return !(TREE_CODE (arg) == TREE_CODE (parm)
10649 && cp_tree_equal (parm, arg));
10651 idx = TEMPLATE_PARM_IDX (parm);
10652 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10654 if (targ)
10655 return !cp_tree_equal (targ, arg);
10657 /* [temp.deduct.type] If, in the declaration of a function template
10658 with a non-type template-parameter, the non-type
10659 template-parameter is used in an expression in the function
10660 parameter-list and, if the corresponding template-argument is
10661 deduced, the template-argument type shall match the type of the
10662 template-parameter exactly, except that a template-argument
10663 deduced from an array bound may be of any integral type.
10664 The non-type parameter might use already deduced type parameters. */
10665 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10666 if (!TREE_TYPE (arg))
10667 /* Template-parameter dependent expression. Just accept it for now.
10668 It will later be processed in convert_template_argument. */
10670 else if (same_type_p (TREE_TYPE (arg), tparm))
10671 /* OK */;
10672 else if ((strict & UNIFY_ALLOW_INTEGER)
10673 && (TREE_CODE (tparm) == INTEGER_TYPE
10674 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10675 /* Convert the ARG to the type of PARM; the deduced non-type
10676 template argument must exactly match the types of the
10677 corresponding parameter. */
10678 arg = fold (build_nop (TREE_TYPE (parm), arg));
10679 else if (uses_template_parms (tparm))
10680 /* We haven't deduced the type of this parameter yet. Try again
10681 later. */
10682 return 0;
10683 else
10684 return 1;
10686 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10687 return 0;
10689 case PTRMEM_CST:
10691 /* A pointer-to-member constant can be unified only with
10692 another constant. */
10693 if (TREE_CODE (arg) != PTRMEM_CST)
10694 return 1;
10696 /* Just unify the class member. It would be useless (and possibly
10697 wrong, depending on the strict flags) to unify also
10698 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10699 arg refer to the same variable, even if through different
10700 classes. For instance:
10702 struct A { int x; };
10703 struct B : A { };
10705 Unification of &A::x and &B::x must succeed. */
10706 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10707 PTRMEM_CST_MEMBER (arg), strict);
10710 case POINTER_TYPE:
10712 if (TREE_CODE (arg) != POINTER_TYPE)
10713 return 1;
10715 /* [temp.deduct.call]
10717 A can be another pointer or pointer to member type that can
10718 be converted to the deduced A via a qualification
10719 conversion (_conv.qual_).
10721 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10722 This will allow for additional cv-qualification of the
10723 pointed-to types if appropriate. */
10725 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10726 /* The derived-to-base conversion only persists through one
10727 level of pointers. */
10728 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10730 return unify (tparms, targs, TREE_TYPE (parm),
10731 TREE_TYPE (arg), strict);
10734 case REFERENCE_TYPE:
10735 if (TREE_CODE (arg) != REFERENCE_TYPE)
10736 return 1;
10737 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10738 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10740 case ARRAY_TYPE:
10741 if (TREE_CODE (arg) != ARRAY_TYPE)
10742 return 1;
10743 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10744 != (TYPE_DOMAIN (arg) == NULL_TREE))
10745 return 1;
10746 if (TYPE_DOMAIN (parm) != NULL_TREE)
10748 tree parm_max;
10749 tree arg_max;
10750 bool parm_cst;
10751 bool arg_cst;
10753 /* Our representation of array types uses "N - 1" as the
10754 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10755 not an integer constant. We cannot unify arbitrarily
10756 complex expressions, so we eliminate the MINUS_EXPRs
10757 here. */
10758 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10759 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10760 if (!parm_cst)
10762 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
10763 parm_max = TREE_OPERAND (parm_max, 0);
10765 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10766 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10767 if (!arg_cst)
10769 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10770 trying to unify the type of a variable with the type
10771 of a template parameter. For example:
10773 template <unsigned int N>
10774 void f (char (&) [N]);
10775 int g();
10776 void h(int i) {
10777 char a[g(i)];
10778 f(a);
10781 Here, the type of the ARG will be "int [g(i)]", and
10782 may be a SAVE_EXPR, etc. */
10783 if (TREE_CODE (arg_max) != MINUS_EXPR)
10784 return 1;
10785 arg_max = TREE_OPERAND (arg_max, 0);
10788 /* If only one of the bounds used a MINUS_EXPR, compensate
10789 by adding one to the other bound. */
10790 if (parm_cst && !arg_cst)
10791 parm_max = fold_build2 (PLUS_EXPR,
10792 integer_type_node,
10793 parm_max,
10794 integer_one_node);
10795 else if (arg_cst && !parm_cst)
10796 arg_max = fold_build2 (PLUS_EXPR,
10797 integer_type_node,
10798 arg_max,
10799 integer_one_node);
10801 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10802 return 1;
10804 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10805 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10807 case REAL_TYPE:
10808 case COMPLEX_TYPE:
10809 case VECTOR_TYPE:
10810 case INTEGER_TYPE:
10811 case BOOLEAN_TYPE:
10812 case ENUMERAL_TYPE:
10813 case VOID_TYPE:
10814 if (TREE_CODE (arg) != TREE_CODE (parm))
10815 return 1;
10817 /* We have already checked cv-qualification at the top of the
10818 function. */
10819 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10820 return 1;
10822 /* As far as unification is concerned, this wins. Later checks
10823 will invalidate it if necessary. */
10824 return 0;
10826 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10827 /* Type INTEGER_CST can come from ordinary constant template args. */
10828 case INTEGER_CST:
10829 while (TREE_CODE (arg) == NOP_EXPR)
10830 arg = TREE_OPERAND (arg, 0);
10832 if (TREE_CODE (arg) != INTEGER_CST)
10833 return 1;
10834 return !tree_int_cst_equal (parm, arg);
10836 case TREE_VEC:
10838 int i;
10839 if (TREE_CODE (arg) != TREE_VEC)
10840 return 1;
10841 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10842 return 1;
10843 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10844 if (unify (tparms, targs,
10845 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10846 UNIFY_ALLOW_NONE))
10847 return 1;
10848 return 0;
10851 case RECORD_TYPE:
10852 case UNION_TYPE:
10853 if (TREE_CODE (arg) != TREE_CODE (parm))
10854 return 1;
10856 if (TYPE_PTRMEMFUNC_P (parm))
10858 if (!TYPE_PTRMEMFUNC_P (arg))
10859 return 1;
10861 return unify (tparms, targs,
10862 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10863 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10864 strict);
10867 if (CLASSTYPE_TEMPLATE_INFO (parm))
10869 tree t = NULL_TREE;
10871 if (strict_in & UNIFY_ALLOW_DERIVED)
10873 /* First, we try to unify the PARM and ARG directly. */
10874 t = try_class_unification (tparms, targs,
10875 parm, arg);
10877 if (!t)
10879 /* Fallback to the special case allowed in
10880 [temp.deduct.call]:
10882 If P is a class, and P has the form
10883 template-id, then A can be a derived class of
10884 the deduced A. Likewise, if P is a pointer to
10885 a class of the form template-id, A can be a
10886 pointer to a derived class pointed to by the
10887 deduced A. */
10888 t = get_template_base (tparms, targs, parm, arg);
10890 if (!t)
10891 return 1;
10894 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10895 && (CLASSTYPE_TI_TEMPLATE (parm)
10896 == CLASSTYPE_TI_TEMPLATE (arg)))
10897 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10898 Then, we should unify `int' and `U'. */
10899 t = arg;
10900 else
10901 /* There's no chance of unification succeeding. */
10902 return 1;
10904 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10905 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10907 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10908 return 1;
10909 return 0;
10911 case METHOD_TYPE:
10912 case FUNCTION_TYPE:
10913 if (TREE_CODE (arg) != TREE_CODE (parm))
10914 return 1;
10916 /* CV qualifications for methods can never be deduced, they must
10917 match exactly. We need to check them explicitly here,
10918 because type_unification_real treats them as any other
10919 cvqualified parameter. */
10920 if (TREE_CODE (parm) == METHOD_TYPE
10921 && (!check_cv_quals_for_unify
10922 (UNIFY_ALLOW_NONE,
10923 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10924 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10925 return 1;
10927 if (unify (tparms, targs, TREE_TYPE (parm),
10928 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10929 return 1;
10930 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10931 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10932 LOOKUP_NORMAL);
10934 case OFFSET_TYPE:
10935 /* Unify a pointer to member with a pointer to member function, which
10936 deduces the type of the member as a function type. */
10937 if (TYPE_PTRMEMFUNC_P (arg))
10939 tree method_type;
10940 tree fntype;
10941 cp_cv_quals cv_quals;
10943 /* Check top-level cv qualifiers */
10944 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10945 return 1;
10947 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10948 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10949 return 1;
10951 /* Determine the type of the function we are unifying against. */
10952 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10953 fntype =
10954 build_function_type (TREE_TYPE (method_type),
10955 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10957 /* Extract the cv-qualifiers of the member function from the
10958 implicit object parameter and place them on the function
10959 type to be restored later. */
10960 cv_quals =
10961 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10962 fntype = build_qualified_type (fntype, cv_quals);
10963 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10966 if (TREE_CODE (arg) != OFFSET_TYPE)
10967 return 1;
10968 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10969 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10970 return 1;
10971 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10972 strict);
10974 case CONST_DECL:
10975 if (DECL_TEMPLATE_PARM_P (parm))
10976 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10977 if (arg != integral_constant_value (parm))
10978 return 1;
10979 return 0;
10981 case FIELD_DECL:
10982 case TEMPLATE_DECL:
10983 /* Matched cases are handled by the ARG == PARM test above. */
10984 return 1;
10986 default:
10987 gcc_assert (EXPR_P (parm));
10989 /* We must be looking at an expression. This can happen with
10990 something like:
10992 template <int I>
10993 void foo(S<I>, S<I + 2>);
10995 This is a "nondeduced context":
10997 [deduct.type]
10999 The nondeduced contexts are:
11001 --A type that is a template-id in which one or more of
11002 the template-arguments is an expression that references
11003 a template-parameter.
11005 In these cases, we assume deduction succeeded, but don't
11006 actually infer any unifications. */
11008 if (!uses_template_parms (parm)
11009 && !template_args_equal (parm, arg))
11010 return 1;
11011 else
11012 return 0;
11016 /* Note that DECL can be defined in this translation unit, if
11017 required. */
11019 static void
11020 mark_definable (tree decl)
11022 tree clone;
11023 DECL_NOT_REALLY_EXTERN (decl) = 1;
11024 FOR_EACH_CLONE (clone, decl)
11025 DECL_NOT_REALLY_EXTERN (clone) = 1;
11028 /* Called if RESULT is explicitly instantiated, or is a member of an
11029 explicitly instantiated class. */
11031 void
11032 mark_decl_instantiated (tree result, int extern_p)
11034 SET_DECL_EXPLICIT_INSTANTIATION (result);
11036 /* If this entity has already been written out, it's too late to
11037 make any modifications. */
11038 if (TREE_ASM_WRITTEN (result))
11039 return;
11041 if (TREE_CODE (result) != FUNCTION_DECL)
11042 /* The TREE_PUBLIC flag for function declarations will have been
11043 set correctly by tsubst. */
11044 TREE_PUBLIC (result) = 1;
11046 /* This might have been set by an earlier implicit instantiation. */
11047 DECL_COMDAT (result) = 0;
11049 if (extern_p)
11050 DECL_NOT_REALLY_EXTERN (result) = 0;
11051 else
11053 mark_definable (result);
11054 /* Always make artificials weak. */
11055 if (DECL_ARTIFICIAL (result) && flag_weak)
11056 comdat_linkage (result);
11057 /* For WIN32 we also want to put explicit instantiations in
11058 linkonce sections. */
11059 else if (TREE_PUBLIC (result))
11060 maybe_make_one_only (result);
11063 /* If EXTERN_P, then this function will not be emitted -- unless
11064 followed by an explicit instantiation, at which point its linkage
11065 will be adjusted. If !EXTERN_P, then this function will be
11066 emitted here. In neither circumstance do we want
11067 import_export_decl to adjust the linkage. */
11068 DECL_INTERFACE_KNOWN (result) = 1;
11071 /* Given two function templates PAT1 and PAT2, return:
11073 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
11074 -1 if PAT2 is more specialized than PAT1.
11075 0 if neither is more specialized.
11077 LEN indicates the number of parameters we should consider
11078 (defaulted parameters should not be considered).
11080 The 1998 std underspecified function template partial ordering, and
11081 DR214 addresses the issue. We take pairs of arguments, one from
11082 each of the templates, and deduce them against each other. One of
11083 the templates will be more specialized if all the *other*
11084 template's arguments deduce against its arguments and at least one
11085 of its arguments *does* *not* deduce against the other template's
11086 corresponding argument. Deduction is done as for class templates.
11087 The arguments used in deduction have reference and top level cv
11088 qualifiers removed. Iff both arguments were originally reference
11089 types *and* deduction succeeds in both directions, the template
11090 with the more cv-qualified argument wins for that pairing (if
11091 neither is more cv-qualified, they both are equal). Unlike regular
11092 deduction, after all the arguments have been deduced in this way,
11093 we do *not* verify the deduced template argument values can be
11094 substituted into non-deduced contexts, nor do we have to verify
11095 that all template arguments have been deduced. */
11098 more_specialized_fn (tree pat1, tree pat2, int len)
11100 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
11101 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
11102 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
11103 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
11104 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
11105 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
11106 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
11107 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
11108 int better1 = 0;
11109 int better2 = 0;
11111 /* Remove the this parameter from non-static member functions. If
11112 one is a non-static member function and the other is not a static
11113 member function, remove the first parameter from that function
11114 also. This situation occurs for operator functions where we
11115 locate both a member function (with this pointer) and non-member
11116 operator (with explicit first operand). */
11117 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
11119 len--; /* LEN is the number of significant arguments for DECL1 */
11120 args1 = TREE_CHAIN (args1);
11121 if (!DECL_STATIC_FUNCTION_P (decl2))
11122 args2 = TREE_CHAIN (args2);
11124 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
11126 args2 = TREE_CHAIN (args2);
11127 if (!DECL_STATIC_FUNCTION_P (decl1))
11129 len--;
11130 args1 = TREE_CHAIN (args1);
11134 /* If only one is a conversion operator, they are unordered. */
11135 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
11136 return 0;
11138 /* Consider the return type for a conversion function */
11139 if (DECL_CONV_FN_P (decl1))
11141 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
11142 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
11143 len++;
11146 processing_template_decl++;
11148 while (len--)
11150 tree arg1 = TREE_VALUE (args1);
11151 tree arg2 = TREE_VALUE (args2);
11152 int deduce1, deduce2;
11153 int quals1 = -1;
11154 int quals2 = -1;
11156 if (TREE_CODE (arg1) == REFERENCE_TYPE)
11158 arg1 = TREE_TYPE (arg1);
11159 quals1 = cp_type_quals (arg1);
11162 if (TREE_CODE (arg2) == REFERENCE_TYPE)
11164 arg2 = TREE_TYPE (arg2);
11165 quals2 = cp_type_quals (arg2);
11168 if ((quals1 < 0) != (quals2 < 0))
11170 /* Only of the args is a reference, see if we should apply
11171 array/function pointer decay to it. This is not part of
11172 DR214, but is, IMHO, consistent with the deduction rules
11173 for the function call itself, and with our earlier
11174 implementation of the underspecified partial ordering
11175 rules. (nathan). */
11176 if (quals1 >= 0)
11178 switch (TREE_CODE (arg1))
11180 case ARRAY_TYPE:
11181 arg1 = TREE_TYPE (arg1);
11182 /* FALLTHROUGH. */
11183 case FUNCTION_TYPE:
11184 arg1 = build_pointer_type (arg1);
11185 break;
11187 default:
11188 break;
11191 else
11193 switch (TREE_CODE (arg2))
11195 case ARRAY_TYPE:
11196 arg2 = TREE_TYPE (arg2);
11197 /* FALLTHROUGH. */
11198 case FUNCTION_TYPE:
11199 arg2 = build_pointer_type (arg2);
11200 break;
11202 default:
11203 break;
11208 arg1 = TYPE_MAIN_VARIANT (arg1);
11209 arg2 = TYPE_MAIN_VARIANT (arg2);
11211 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11212 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11214 if (!deduce1)
11215 better2 = -1;
11216 if (!deduce2)
11217 better1 = -1;
11218 if (better1 < 0 && better2 < 0)
11219 /* We've failed to deduce something in either direction.
11220 These must be unordered. */
11221 break;
11223 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11225 /* Deduces in both directions, see if quals can
11226 disambiguate. Pretend the worse one failed to deduce. */
11227 if ((quals1 & quals2) == quals2)
11228 deduce1 = 0;
11229 if ((quals1 & quals2) == quals1)
11230 deduce2 = 0;
11232 if (deduce1 && !deduce2 && !better2)
11233 better2 = 1;
11234 if (deduce2 && !deduce1 && !better1)
11235 better1 = 1;
11237 args1 = TREE_CHAIN (args1);
11238 args2 = TREE_CHAIN (args2);
11241 processing_template_decl--;
11243 return (better1 > 0) - (better2 > 0);
11246 /* Determine which of two partial specializations is more specialized.
11248 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11249 to the first partial specialization. The TREE_VALUE is the
11250 innermost set of template parameters for the partial
11251 specialization. PAT2 is similar, but for the second template.
11253 Return 1 if the first partial specialization is more specialized;
11254 -1 if the second is more specialized; 0 if neither is more
11255 specialized.
11257 See [temp.class.order] for information about determining which of
11258 two templates is more specialized. */
11260 static int
11261 more_specialized_class (tree pat1, tree pat2)
11263 tree targs;
11264 tree tmpl1, tmpl2;
11265 int winner = 0;
11267 tmpl1 = TREE_TYPE (pat1);
11268 tmpl2 = TREE_TYPE (pat2);
11270 /* Just like what happens for functions, if we are ordering between
11271 different class template specializations, we may encounter dependent
11272 types in the arguments, and we need our dependency check functions
11273 to behave correctly. */
11274 ++processing_template_decl;
11275 targs = get_class_bindings (TREE_VALUE (pat1),
11276 CLASSTYPE_TI_ARGS (tmpl1),
11277 CLASSTYPE_TI_ARGS (tmpl2));
11278 if (targs)
11279 --winner;
11281 targs = get_class_bindings (TREE_VALUE (pat2),
11282 CLASSTYPE_TI_ARGS (tmpl2),
11283 CLASSTYPE_TI_ARGS (tmpl1));
11284 if (targs)
11285 ++winner;
11286 --processing_template_decl;
11288 return winner;
11291 /* Return the template arguments that will produce the function signature
11292 DECL from the function template FN, with the explicit template
11293 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
11294 also match. Return NULL_TREE if no satisfactory arguments could be
11295 found. */
11297 static tree
11298 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
11300 int ntparms = DECL_NTPARMS (fn);
11301 tree targs = make_tree_vec (ntparms);
11302 tree decl_type;
11303 tree decl_arg_types;
11305 /* Substitute the explicit template arguments into the type of DECL.
11306 The call to fn_type_unification will handle substitution into the
11307 FN. */
11308 decl_type = TREE_TYPE (decl);
11309 if (explicit_args && uses_template_parms (decl_type))
11311 tree tmpl;
11312 tree converted_args;
11314 if (DECL_TEMPLATE_INFO (decl))
11315 tmpl = DECL_TI_TEMPLATE (decl);
11316 else
11317 /* We can get here for some invalid specializations. */
11318 return NULL_TREE;
11320 converted_args
11321 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11322 explicit_args, NULL_TREE,
11323 tf_none,
11324 /*require_all_args=*/false,
11325 /*use_default_args=*/false);
11326 if (converted_args == error_mark_node)
11327 return NULL_TREE;
11329 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
11330 if (decl_type == error_mark_node)
11331 return NULL_TREE;
11334 decl_arg_types = TYPE_ARG_TYPES (decl_type);
11335 /* Never do unification on the 'this' parameter. */
11336 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11337 decl_arg_types = TREE_CHAIN (decl_arg_types);
11339 if (fn_type_unification (fn, explicit_args, targs,
11340 decl_arg_types,
11341 (check_rettype || DECL_CONV_FN_P (fn)
11342 ? TREE_TYPE (decl_type) : NULL_TREE),
11343 DEDUCE_EXACT, LOOKUP_NORMAL))
11344 return NULL_TREE;
11346 return targs;
11349 /* Return the innermost template arguments that, when applied to a
11350 template specialization whose innermost template parameters are
11351 TPARMS, and whose specialization arguments are PARMS, yield the
11352 ARGS.
11354 For example, suppose we have:
11356 template <class T, class U> struct S {};
11357 template <class T> struct S<T*, int> {};
11359 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
11360 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
11361 int}. The resulting vector will be {double}, indicating that `T'
11362 is bound to `double'. */
11364 static tree
11365 get_class_bindings (tree tparms, tree spec_args, tree args)
11367 int i, ntparms = TREE_VEC_LENGTH (tparms);
11368 tree deduced_args;
11369 tree innermost_deduced_args;
11371 innermost_deduced_args = make_tree_vec (ntparms);
11372 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11374 deduced_args = copy_node (args);
11375 SET_TMPL_ARGS_LEVEL (deduced_args,
11376 TMPL_ARGS_DEPTH (deduced_args),
11377 innermost_deduced_args);
11379 else
11380 deduced_args = innermost_deduced_args;
11382 if (unify (tparms, deduced_args,
11383 INNERMOST_TEMPLATE_ARGS (spec_args),
11384 INNERMOST_TEMPLATE_ARGS (args),
11385 UNIFY_ALLOW_NONE))
11386 return NULL_TREE;
11388 for (i = 0; i < ntparms; ++i)
11389 if (! TREE_VEC_ELT (innermost_deduced_args, i))
11390 return NULL_TREE;
11392 /* Verify that nondeduced template arguments agree with the type
11393 obtained from argument deduction.
11395 For example:
11397 struct A { typedef int X; };
11398 template <class T, class U> struct C {};
11399 template <class T> struct C<T, typename T::X> {};
11401 Then with the instantiation `C<A, int>', we can deduce that
11402 `T' is `A' but unify () does not check whether `typename T::X'
11403 is `int'. */
11404 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11405 if (spec_args == error_mark_node
11406 /* We only need to check the innermost arguments; the other
11407 arguments will always agree. */
11408 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11409 INNERMOST_TEMPLATE_ARGS (args)))
11410 return NULL_TREE;
11412 return deduced_args;
11415 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11416 Return the TREE_LIST node with the most specialized template, if
11417 any. If there is no most specialized template, the error_mark_node
11418 is returned.
11420 Note that this function does not look at, or modify, the
11421 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11422 returned is one of the elements of INSTANTIATIONS, callers may
11423 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11424 and retrieve it from the value returned. */
11426 tree
11427 most_specialized_instantiation (tree templates)
11429 tree fn, champ;
11431 ++processing_template_decl;
11433 champ = templates;
11434 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
11436 int fate = 0;
11438 if (get_bindings (TREE_VALUE (champ),
11439 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11440 NULL_TREE, /*check_ret=*/false))
11441 fate--;
11443 if (get_bindings (TREE_VALUE (fn),
11444 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11445 NULL_TREE, /*check_ret=*/false))
11446 fate++;
11448 if (fate == -1)
11449 champ = fn;
11450 else if (!fate)
11452 /* Equally specialized, move to next function. If there
11453 is no next function, nothing's most specialized. */
11454 fn = TREE_CHAIN (fn);
11455 champ = fn;
11456 if (!fn)
11457 break;
11461 if (champ)
11462 /* Now verify that champ is better than everything earlier in the
11463 instantiation list. */
11464 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11465 if (get_bindings (TREE_VALUE (champ),
11466 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11467 NULL_TREE, /*check_ret=*/false)
11468 || !get_bindings (TREE_VALUE (fn),
11469 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11470 NULL_TREE, /*check_ret=*/false))
11472 champ = NULL_TREE;
11473 break;
11476 processing_template_decl--;
11478 if (!champ)
11479 return error_mark_node;
11481 return champ;
11484 /* If DECL is a specialization of some template, return the most
11485 general such template. Otherwise, returns NULL_TREE.
11487 For example, given:
11489 template <class T> struct S { template <class U> void f(U); };
11491 if TMPL is `template <class U> void S<int>::f(U)' this will return
11492 the full template. This function will not trace past partial
11493 specializations, however. For example, given in addition:
11495 template <class T> struct S<T*> { template <class U> void f(U); };
11497 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11498 `template <class T> template <class U> S<T*>::f(U)'. */
11500 tree
11501 most_general_template (tree decl)
11503 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11504 an immediate specialization. */
11505 if (TREE_CODE (decl) == FUNCTION_DECL)
11507 if (DECL_TEMPLATE_INFO (decl)) {
11508 decl = DECL_TI_TEMPLATE (decl);
11510 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11511 template friend. */
11512 if (TREE_CODE (decl) != TEMPLATE_DECL)
11513 return NULL_TREE;
11514 } else
11515 return NULL_TREE;
11518 /* Look for more and more general templates. */
11519 while (DECL_TEMPLATE_INFO (decl))
11521 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11522 (See cp-tree.h for details.) */
11523 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11524 break;
11526 if (CLASS_TYPE_P (TREE_TYPE (decl))
11527 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11528 break;
11530 /* Stop if we run into an explicitly specialized class template. */
11531 if (!DECL_NAMESPACE_SCOPE_P (decl)
11532 && DECL_CONTEXT (decl)
11533 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11534 break;
11536 decl = DECL_TI_TEMPLATE (decl);
11539 return decl;
11542 /* Return the most specialized of the class template partial
11543 specializations of TMPL which can produce TYPE, a specialization of
11544 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11545 a _TYPE node corresponding to the partial specialization, while the
11546 TREE_PURPOSE is the set of template arguments that must be
11547 substituted into the TREE_TYPE in order to generate TYPE.
11549 If the choice of partial specialization is ambiguous, a diagnostic
11550 is issued, and the error_mark_node is returned. If there are no
11551 partial specializations of TMPL matching TYPE, then NULL_TREE is
11552 returned. */
11554 static tree
11555 most_specialized_class (tree type, tree tmpl)
11557 tree list = NULL_TREE;
11558 tree t;
11559 tree champ;
11560 int fate;
11561 bool ambiguous_p;
11562 tree args;
11564 tmpl = most_general_template (tmpl);
11565 args = CLASSTYPE_TI_ARGS (type);
11566 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11568 tree partial_spec_args;
11569 tree spec_args;
11571 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11572 spec_args = get_class_bindings (TREE_VALUE (t),
11573 partial_spec_args,
11574 args);
11575 if (spec_args)
11577 list = tree_cons (spec_args, TREE_VALUE (t), list);
11578 TREE_TYPE (list) = TREE_TYPE (t);
11582 if (! list)
11583 return NULL_TREE;
11585 ambiguous_p = false;
11586 t = list;
11587 champ = t;
11588 t = TREE_CHAIN (t);
11589 for (; t; t = TREE_CHAIN (t))
11591 fate = more_specialized_class (champ, t);
11592 if (fate == 1)
11594 else
11596 if (fate == 0)
11598 t = TREE_CHAIN (t);
11599 if (! t)
11601 ambiguous_p = true;
11602 break;
11605 champ = t;
11609 if (!ambiguous_p)
11610 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11612 fate = more_specialized_class (champ, t);
11613 if (fate != 1)
11615 ambiguous_p = true;
11616 break;
11620 if (ambiguous_p)
11622 const char *str = "candidates are:";
11623 error ("ambiguous class template instantiation for %q#T", type);
11624 for (t = list; t; t = TREE_CHAIN (t))
11626 error ("%s %+#T", str, TREE_TYPE (t));
11627 str = " ";
11629 return error_mark_node;
11632 return champ;
11635 /* Explicitly instantiate DECL. */
11637 void
11638 do_decl_instantiation (tree decl, tree storage)
11640 tree result = NULL_TREE;
11641 int extern_p = 0;
11643 if (!decl || decl == error_mark_node)
11644 /* An error occurred, for which grokdeclarator has already issued
11645 an appropriate message. */
11646 return;
11647 else if (! DECL_LANG_SPECIFIC (decl))
11649 error ("explicit instantiation of non-template %q#D", decl);
11650 return;
11652 else if (TREE_CODE (decl) == VAR_DECL)
11654 /* There is an asymmetry here in the way VAR_DECLs and
11655 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11656 the latter, the DECL we get back will be marked as a
11657 template instantiation, and the appropriate
11658 DECL_TEMPLATE_INFO will be set up. This does not happen for
11659 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11660 should handle VAR_DECLs as it currently handles
11661 FUNCTION_DECLs. */
11662 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11663 if (!result || TREE_CODE (result) != VAR_DECL)
11665 error ("no matching template for %qD found", decl);
11666 return;
11669 else if (TREE_CODE (decl) != FUNCTION_DECL)
11671 error ("explicit instantiation of %q#D", decl);
11672 return;
11674 else
11675 result = decl;
11677 /* Check for various error cases. Note that if the explicit
11678 instantiation is valid the RESULT will currently be marked as an
11679 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11680 until we get here. */
11682 if (DECL_TEMPLATE_SPECIALIZATION (result))
11684 /* DR 259 [temp.spec].
11686 Both an explicit instantiation and a declaration of an explicit
11687 specialization shall not appear in a program unless the explicit
11688 instantiation follows a declaration of the explicit specialization.
11690 For a given set of template parameters, if an explicit
11691 instantiation of a template appears after a declaration of an
11692 explicit specialization for that template, the explicit
11693 instantiation has no effect. */
11694 return;
11696 else if (DECL_EXPLICIT_INSTANTIATION (result))
11698 /* [temp.spec]
11700 No program shall explicitly instantiate any template more
11701 than once.
11703 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11704 the first instantiation was `extern' and the second is not,
11705 and EXTERN_P for the opposite case. */
11706 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11707 pedwarn ("duplicate explicit instantiation of %q#D", result);
11708 /* If an "extern" explicit instantiation follows an ordinary
11709 explicit instantiation, the template is instantiated. */
11710 if (extern_p)
11711 return;
11713 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11715 error ("no matching template for %qD found", result);
11716 return;
11718 else if (!DECL_TEMPLATE_INFO (result))
11720 pedwarn ("explicit instantiation of non-template %q#D", result);
11721 return;
11724 if (storage == NULL_TREE)
11726 else if (storage == ridpointers[(int) RID_EXTERN])
11728 if (pedantic && !in_system_header)
11729 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11730 "instantiations");
11731 extern_p = 1;
11733 else
11734 error ("storage class %qD applied to template instantiation", storage);
11736 check_explicit_instantiation_namespace (result);
11737 mark_decl_instantiated (result, extern_p);
11738 if (! extern_p)
11739 instantiate_decl (result, /*defer_ok=*/1,
11740 /*expl_inst_class_mem_p=*/false);
11743 static void
11744 mark_class_instantiated (tree t, int extern_p)
11746 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11747 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11748 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11749 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11750 if (! extern_p)
11752 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11753 rest_of_type_compilation (t, 1);
11757 /* Called from do_type_instantiation through binding_table_foreach to
11758 do recursive instantiation for the type bound in ENTRY. */
11759 static void
11760 bt_instantiate_type_proc (binding_entry entry, void *data)
11762 tree storage = *(tree *) data;
11764 if (IS_AGGR_TYPE (entry->type)
11765 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11766 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11769 /* Called from do_type_instantiation to instantiate a member
11770 (a member function or a static member variable) of an
11771 explicitly instantiated class template. */
11772 static void
11773 instantiate_class_member (tree decl, int extern_p)
11775 mark_decl_instantiated (decl, extern_p);
11776 if (! extern_p)
11777 instantiate_decl (decl, /*defer_ok=*/1,
11778 /*expl_inst_class_mem_p=*/true);
11781 /* Perform an explicit instantiation of template class T. STORAGE, if
11782 non-null, is the RID for extern, inline or static. COMPLAIN is
11783 nonzero if this is called from the parser, zero if called recursively,
11784 since the standard is unclear (as detailed below). */
11786 void
11787 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11789 int extern_p = 0;
11790 int nomem_p = 0;
11791 int static_p = 0;
11792 int previous_instantiation_extern_p = 0;
11794 if (TREE_CODE (t) == TYPE_DECL)
11795 t = TREE_TYPE (t);
11797 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11799 error ("explicit instantiation of non-template type %qT", t);
11800 return;
11803 complete_type (t);
11805 if (!COMPLETE_TYPE_P (t))
11807 if (complain & tf_error)
11808 error ("explicit instantiation of %q#T before definition of template",
11810 return;
11813 if (storage != NULL_TREE)
11815 if (pedantic && !in_system_header)
11816 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11817 storage);
11819 if (storage == ridpointers[(int) RID_INLINE])
11820 nomem_p = 1;
11821 else if (storage == ridpointers[(int) RID_EXTERN])
11822 extern_p = 1;
11823 else if (storage == ridpointers[(int) RID_STATIC])
11824 static_p = 1;
11825 else
11827 error ("storage class %qD applied to template instantiation",
11828 storage);
11829 extern_p = 0;
11833 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11835 /* DR 259 [temp.spec].
11837 Both an explicit instantiation and a declaration of an explicit
11838 specialization shall not appear in a program unless the explicit
11839 instantiation follows a declaration of the explicit specialization.
11841 For a given set of template parameters, if an explicit
11842 instantiation of a template appears after a declaration of an
11843 explicit specialization for that template, the explicit
11844 instantiation has no effect. */
11845 return;
11847 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11849 /* [temp.spec]
11851 No program shall explicitly instantiate any template more
11852 than once.
11854 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11855 instantiation was `extern'. If EXTERN_P then the second is.
11856 These cases are OK. */
11857 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11859 if (!previous_instantiation_extern_p && !extern_p
11860 && (complain & tf_error))
11861 pedwarn ("duplicate explicit instantiation of %q#T", t);
11863 /* If we've already instantiated the template, just return now. */
11864 if (!CLASSTYPE_INTERFACE_ONLY (t))
11865 return;
11868 check_explicit_instantiation_namespace (TYPE_NAME (t));
11869 mark_class_instantiated (t, extern_p);
11871 if (nomem_p)
11872 return;
11875 tree tmp;
11877 /* In contrast to implicit instantiation, where only the
11878 declarations, and not the definitions, of members are
11879 instantiated, we have here:
11881 [temp.explicit]
11883 The explicit instantiation of a class template specialization
11884 implies the instantiation of all of its members not
11885 previously explicitly specialized in the translation unit
11886 containing the explicit instantiation.
11888 Of course, we can't instantiate member template classes, since
11889 we don't have any arguments for them. Note that the standard
11890 is unclear on whether the instantiation of the members are
11891 *explicit* instantiations or not. However, the most natural
11892 interpretation is that it should be an explicit instantiation. */
11894 if (! static_p)
11895 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11896 if (TREE_CODE (tmp) == FUNCTION_DECL
11897 && DECL_TEMPLATE_INSTANTIATION (tmp))
11898 instantiate_class_member (tmp, extern_p);
11900 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11901 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11902 instantiate_class_member (tmp, extern_p);
11904 if (CLASSTYPE_NESTED_UTDS (t))
11905 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11906 bt_instantiate_type_proc, &storage);
11910 /* Given a function DECL, which is a specialization of TMPL, modify
11911 DECL to be a re-instantiation of TMPL with the same template
11912 arguments. TMPL should be the template into which tsubst'ing
11913 should occur for DECL, not the most general template.
11915 One reason for doing this is a scenario like this:
11917 template <class T>
11918 void f(const T&, int i);
11920 void g() { f(3, 7); }
11922 template <class T>
11923 void f(const T& t, const int i) { }
11925 Note that when the template is first instantiated, with
11926 instantiate_template, the resulting DECL will have no name for the
11927 first parameter, and the wrong type for the second. So, when we go
11928 to instantiate the DECL, we regenerate it. */
11930 static void
11931 regenerate_decl_from_template (tree decl, tree tmpl)
11933 /* The arguments used to instantiate DECL, from the most general
11934 template. */
11935 tree args;
11936 tree code_pattern;
11938 args = DECL_TI_ARGS (decl);
11939 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11941 /* Make sure that we can see identifiers, and compute access
11942 correctly. */
11943 push_access_scope (decl);
11945 if (TREE_CODE (decl) == FUNCTION_DECL)
11947 tree decl_parm;
11948 tree pattern_parm;
11949 tree specs;
11950 int args_depth;
11951 int parms_depth;
11953 args_depth = TMPL_ARGS_DEPTH (args);
11954 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11955 if (args_depth > parms_depth)
11956 args = get_innermost_template_args (args, parms_depth);
11958 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11959 args, tf_error, NULL_TREE);
11960 if (specs)
11961 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11962 specs);
11964 /* Merge parameter declarations. */
11965 decl_parm = skip_artificial_parms_for (decl,
11966 DECL_ARGUMENTS (decl));
11967 pattern_parm
11968 = skip_artificial_parms_for (code_pattern,
11969 DECL_ARGUMENTS (code_pattern));
11970 while (decl_parm)
11972 tree parm_type;
11973 tree attributes;
11975 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11976 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11977 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11978 NULL_TREE);
11979 parm_type = type_decays_to (parm_type);
11980 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11981 TREE_TYPE (decl_parm) = parm_type;
11982 attributes = DECL_ATTRIBUTES (pattern_parm);
11983 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11985 DECL_ATTRIBUTES (decl_parm) = attributes;
11986 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11988 decl_parm = TREE_CHAIN (decl_parm);
11989 pattern_parm = TREE_CHAIN (pattern_parm);
11992 /* Merge additional specifiers from the CODE_PATTERN. */
11993 if (DECL_DECLARED_INLINE_P (code_pattern)
11994 && !DECL_DECLARED_INLINE_P (decl))
11995 DECL_DECLARED_INLINE_P (decl) = 1;
11996 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11997 DECL_INLINE (decl) = 1;
11999 else if (TREE_CODE (decl) == VAR_DECL)
12000 DECL_INITIAL (decl) =
12001 tsubst_expr (DECL_INITIAL (code_pattern), args,
12002 tf_error, DECL_TI_TEMPLATE (decl),
12003 /*integral_constant_expression_p=*/false);
12004 else
12005 gcc_unreachable ();
12007 pop_access_scope (decl);
12010 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
12011 substituted to get DECL. */
12013 tree
12014 template_for_substitution (tree decl)
12016 tree tmpl = DECL_TI_TEMPLATE (decl);
12018 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
12019 for the instantiation. This is not always the most general
12020 template. Consider, for example:
12022 template <class T>
12023 struct S { template <class U> void f();
12024 template <> void f<int>(); };
12026 and an instantiation of S<double>::f<int>. We want TD to be the
12027 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
12028 while (/* An instantiation cannot have a definition, so we need a
12029 more general template. */
12030 DECL_TEMPLATE_INSTANTIATION (tmpl)
12031 /* We must also deal with friend templates. Given:
12033 template <class T> struct S {
12034 template <class U> friend void f() {};
12037 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
12038 so far as the language is concerned, but that's still
12039 where we get the pattern for the instantiation from. On
12040 other hand, if the definition comes outside the class, say:
12042 template <class T> struct S {
12043 template <class U> friend void f();
12045 template <class U> friend void f() {}
12047 we don't need to look any further. That's what the check for
12048 DECL_INITIAL is for. */
12049 || (TREE_CODE (decl) == FUNCTION_DECL
12050 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
12051 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
12053 /* The present template, TD, should not be a definition. If it
12054 were a definition, we should be using it! Note that we
12055 cannot restructure the loop to just keep going until we find
12056 a template with a definition, since that might go too far if
12057 a specialization was declared, but not defined. */
12058 gcc_assert (TREE_CODE (decl) != VAR_DECL
12059 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
12061 /* Fetch the more general template. */
12062 tmpl = DECL_TI_TEMPLATE (tmpl);
12065 return tmpl;
12068 /* Produce the definition of D, a _DECL generated from a template. If
12069 DEFER_OK is nonzero, then we don't have to actually do the
12070 instantiation now; we just have to do it sometime. Normally it is
12071 an error if this is an explicit instantiation but D is undefined.
12072 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
12073 explicitly instantiated class template. */
12075 tree
12076 instantiate_decl (tree d, int defer_ok,
12077 bool expl_inst_class_mem_p)
12079 tree tmpl = DECL_TI_TEMPLATE (d);
12080 tree gen_args;
12081 tree args;
12082 tree td;
12083 tree code_pattern;
12084 tree spec;
12085 tree gen_tmpl;
12086 bool pattern_defined;
12087 int need_push;
12088 location_t saved_loc = input_location;
12089 bool external_p;
12091 /* This function should only be used to instantiate templates for
12092 functions and static member variables. */
12093 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
12094 || TREE_CODE (d) == VAR_DECL);
12096 /* Variables are never deferred; if instantiation is required, they
12097 are instantiated right away. That allows for better code in the
12098 case that an expression refers to the value of the variable --
12099 if the variable has a constant value the referring expression can
12100 take advantage of that fact. */
12101 if (TREE_CODE (d) == VAR_DECL)
12102 defer_ok = 0;
12104 /* Don't instantiate cloned functions. Instead, instantiate the
12105 functions they cloned. */
12106 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
12107 d = DECL_CLONED_FUNCTION (d);
12109 if (DECL_TEMPLATE_INSTANTIATED (d))
12110 /* D has already been instantiated. It might seem reasonable to
12111 check whether or not D is an explicit instantiation, and, if so,
12112 stop here. But when an explicit instantiation is deferred
12113 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
12114 is set, even though we still need to do the instantiation. */
12115 return d;
12117 /* If we already have a specialization of this declaration, then
12118 there's no reason to instantiate it. Note that
12119 retrieve_specialization gives us both instantiations and
12120 specializations, so we must explicitly check
12121 DECL_TEMPLATE_SPECIALIZATION. */
12122 gen_tmpl = most_general_template (tmpl);
12123 gen_args = DECL_TI_ARGS (d);
12124 spec = retrieve_specialization (gen_tmpl, gen_args,
12125 /*class_specializations_p=*/false);
12126 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
12127 return spec;
12129 /* This needs to happen before any tsubsting. */
12130 if (! push_tinst_level (d))
12131 return d;
12133 timevar_push (TV_PARSE);
12135 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
12136 for the instantiation. */
12137 td = template_for_substitution (d);
12138 code_pattern = DECL_TEMPLATE_RESULT (td);
12140 /* We should never be trying to instantiate a member of a class
12141 template or partial specialization. */
12142 gcc_assert (d != code_pattern);
12144 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
12145 || DECL_TEMPLATE_SPECIALIZATION (td))
12146 /* In the case of a friend template whose definition is provided
12147 outside the class, we may have too many arguments. Drop the
12148 ones we don't need. The same is true for specializations. */
12149 args = get_innermost_template_args
12150 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
12151 else
12152 args = gen_args;
12154 if (TREE_CODE (d) == FUNCTION_DECL)
12155 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
12156 else
12157 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
12159 /* We may be in the middle of deferred access check. Disable it now. */
12160 push_deferring_access_checks (dk_no_deferred);
12162 /* Unless an explicit instantiation directive has already determined
12163 the linkage of D, remember that a definition is available for
12164 this entity. */
12165 if (pattern_defined
12166 && !DECL_INTERFACE_KNOWN (d)
12167 && !DECL_NOT_REALLY_EXTERN (d))
12168 mark_definable (d);
12170 input_location = DECL_SOURCE_LOCATION (d);
12172 /* If D is a member of an explicitly instantiated class template,
12173 and no definition is available, treat it like an implicit
12174 instantiation. */
12175 if (!pattern_defined && expl_inst_class_mem_p
12176 && DECL_EXPLICIT_INSTANTIATION (d))
12178 DECL_NOT_REALLY_EXTERN (d) = 0;
12179 DECL_INTERFACE_KNOWN (d) = 0;
12180 SET_DECL_IMPLICIT_INSTANTIATION (d);
12183 if (!defer_ok)
12185 /* Recheck the substitutions to obtain any warning messages
12186 about ignoring cv qualifiers. */
12187 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
12188 tree type = TREE_TYPE (gen);
12190 /* Make sure that we can see identifiers, and compute access
12191 correctly. D is already the target FUNCTION_DECL with the
12192 right context. */
12193 push_access_scope (d);
12195 if (TREE_CODE (gen) == FUNCTION_DECL)
12197 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
12198 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
12199 tf_warning_or_error, d);
12200 /* Don't simply tsubst the function type, as that will give
12201 duplicate warnings about poor parameter qualifications.
12202 The function arguments are the same as the decl_arguments
12203 without the top level cv qualifiers. */
12204 type = TREE_TYPE (type);
12206 tsubst (type, gen_args, tf_warning_or_error, d);
12208 pop_access_scope (d);
12211 /* Check to see whether we know that this template will be
12212 instantiated in some other file, as with "extern template"
12213 extension. */
12214 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12215 /* In general, we do not instantiate such templates... */
12216 if (external_p
12217 /* ... but we instantiate inline functions so that we can inline
12218 them and ... */
12219 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12220 /* ... we instantiate static data members whose values are
12221 needed in integral constant expressions. */
12222 && ! (TREE_CODE (d) == VAR_DECL
12223 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
12224 goto out;
12225 /* Defer all other templates, unless we have been explicitly
12226 forbidden from doing so. */
12227 if (/* If there is no definition, we cannot instantiate the
12228 template. */
12229 ! pattern_defined
12230 /* If it's OK to postpone instantiation, do so. */
12231 || defer_ok
12232 /* If this is a static data member that will be defined
12233 elsewhere, we don't want to instantiate the entire data
12234 member, but we do want to instantiate the initializer so that
12235 we can substitute that elsewhere. */
12236 || (external_p && TREE_CODE (d) == VAR_DECL))
12238 /* The definition of the static data member is now required so
12239 we must substitute the initializer. */
12240 if (TREE_CODE (d) == VAR_DECL
12241 && !DECL_INITIAL (d)
12242 && DECL_INITIAL (code_pattern))
12244 tree ns;
12245 tree init;
12247 ns = decl_namespace_context (d);
12248 push_nested_namespace (ns);
12249 push_nested_class (DECL_CONTEXT (d));
12250 init = tsubst_expr (DECL_INITIAL (code_pattern),
12251 args,
12252 tf_warning_or_error, NULL_TREE,
12253 /*integral_constant_expression_p=*/false);
12254 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12255 /*asmspec_tree=*/NULL_TREE,
12256 LOOKUP_ONLYCONVERTING);
12257 pop_nested_class ();
12258 pop_nested_namespace (ns);
12261 /* We restore the source position here because it's used by
12262 add_pending_template. */
12263 input_location = saved_loc;
12265 if (at_eof && !pattern_defined
12266 && DECL_EXPLICIT_INSTANTIATION (d))
12267 /* [temp.explicit]
12269 The definition of a non-exported function template, a
12270 non-exported member function template, or a non-exported
12271 member function or static data member of a class template
12272 shall be present in every translation unit in which it is
12273 explicitly instantiated. */
12274 pedwarn
12275 ("explicit instantiation of %qD but no definition available", d);
12277 /* ??? Historically, we have instantiated inline functions, even
12278 when marked as "extern template". */
12279 if (!(external_p && TREE_CODE (d) == VAR_DECL))
12280 add_pending_template (d);
12281 goto out;
12283 /* Tell the repository that D is available in this translation unit
12284 -- and see if it is supposed to be instantiated here. */
12285 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12287 /* In a PCH file, despite the fact that the repository hasn't
12288 requested instantiation in the PCH it is still possible that
12289 an instantiation will be required in a file that includes the
12290 PCH. */
12291 if (pch_file)
12292 add_pending_template (d);
12293 /* Instantiate inline functions so that the inliner can do its
12294 job, even though we'll not be emitting a copy of this
12295 function. */
12296 if (!(TREE_CODE (d) == FUNCTION_DECL
12297 && flag_inline_trees
12298 && DECL_DECLARED_INLINE_P (d)))
12299 goto out;
12302 need_push = !cfun || !global_bindings_p ();
12303 if (need_push)
12304 push_to_top_level ();
12306 /* Mark D as instantiated so that recursive calls to
12307 instantiate_decl do not try to instantiate it again. */
12308 DECL_TEMPLATE_INSTANTIATED (d) = 1;
12310 /* Regenerate the declaration in case the template has been modified
12311 by a subsequent redeclaration. */
12312 regenerate_decl_from_template (d, td);
12314 /* We already set the file and line above. Reset them now in case
12315 they changed as a result of calling regenerate_decl_from_template. */
12316 input_location = DECL_SOURCE_LOCATION (d);
12318 if (TREE_CODE (d) == VAR_DECL)
12320 tree init;
12322 /* Clear out DECL_RTL; whatever was there before may not be right
12323 since we've reset the type of the declaration. */
12324 SET_DECL_RTL (d, NULL_RTX);
12325 DECL_IN_AGGR_P (d) = 0;
12327 /* The initializer is placed in DECL_INITIAL by
12328 regenerate_decl_from_template. Pull it out so that
12329 finish_decl can process it. */
12330 init = DECL_INITIAL (d);
12331 DECL_INITIAL (d) = NULL_TREE;
12332 DECL_INITIALIZED_P (d) = 0;
12334 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12335 initializer. That function will defer actual emission until
12336 we have a chance to determine linkage. */
12337 DECL_EXTERNAL (d) = 0;
12339 /* Enter the scope of D so that access-checking works correctly. */
12340 push_nested_class (DECL_CONTEXT (d));
12341 finish_decl (d, init, NULL_TREE);
12342 pop_nested_class ();
12344 else if (TREE_CODE (d) == FUNCTION_DECL)
12346 htab_t saved_local_specializations;
12347 tree subst_decl;
12348 tree tmpl_parm;
12349 tree spec_parm;
12351 /* Save away the current list, in case we are instantiating one
12352 template from within the body of another. */
12353 saved_local_specializations = local_specializations;
12355 /* Set up the list of local specializations. */
12356 local_specializations = htab_create (37,
12357 hash_local_specialization,
12358 eq_local_specializations,
12359 NULL);
12361 /* Set up context. */
12362 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
12364 /* Create substitution entries for the parameters. */
12365 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12366 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12367 spec_parm = DECL_ARGUMENTS (d);
12368 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12370 register_local_specialization (spec_parm, tmpl_parm);
12371 spec_parm = skip_artificial_parms_for (d, spec_parm);
12372 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12374 while (tmpl_parm)
12376 register_local_specialization (spec_parm, tmpl_parm);
12377 tmpl_parm = TREE_CHAIN (tmpl_parm);
12378 spec_parm = TREE_CHAIN (spec_parm);
12380 gcc_assert (!spec_parm);
12382 /* Substitute into the body of the function. */
12383 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
12384 tf_warning_or_error, tmpl,
12385 /*integral_constant_expression_p=*/false);
12387 /* We don't need the local specializations any more. */
12388 htab_delete (local_specializations);
12389 local_specializations = saved_local_specializations;
12391 /* Finish the function. */
12392 d = finish_function (0);
12393 expand_or_defer_fn (d);
12396 /* We're not deferring instantiation any more. */
12397 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12399 if (need_push)
12400 pop_from_top_level ();
12402 out:
12403 input_location = saved_loc;
12404 pop_deferring_access_checks ();
12405 pop_tinst_level ();
12407 timevar_pop (TV_PARSE);
12409 return d;
12412 /* Run through the list of templates that we wish we could
12413 instantiate, and instantiate any we can. RETRIES is the
12414 number of times we retry pending template instantiation. */
12416 void
12417 instantiate_pending_templates (int retries)
12419 tree *t;
12420 tree last = NULL_TREE;
12421 int reconsider;
12422 location_t saved_loc = input_location;
12423 int saved_in_system_header = in_system_header;
12425 /* Instantiating templates may trigger vtable generation. This in turn
12426 may require further template instantiations. We place a limit here
12427 to avoid infinite loop. */
12428 if (pending_templates && retries >= max_tinst_depth)
12430 tree decl = TREE_VALUE (pending_templates);
12432 error ("template instantiation depth exceeds maximum of %d"
12433 " instantiating %q+D, possibly from virtual table generation"
12434 " (use -ftemplate-depth-NN to increase the maximum)",
12435 max_tinst_depth, decl);
12436 if (TREE_CODE (decl) == FUNCTION_DECL)
12437 /* Pretend that we defined it. */
12438 DECL_INITIAL (decl) = error_mark_node;
12439 return;
12444 reconsider = 0;
12446 t = &pending_templates;
12447 while (*t)
12449 tree instantiation = TREE_VALUE (*t);
12451 reopen_tinst_level (TREE_PURPOSE (*t));
12453 if (TYPE_P (instantiation))
12455 tree fn;
12457 if (!COMPLETE_TYPE_P (instantiation))
12459 instantiate_class_template (instantiation);
12460 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12461 for (fn = TYPE_METHODS (instantiation);
12463 fn = TREE_CHAIN (fn))
12464 if (! DECL_ARTIFICIAL (fn))
12465 instantiate_decl (fn,
12466 /*defer_ok=*/0,
12467 /*expl_inst_class_mem_p=*/false);
12468 if (COMPLETE_TYPE_P (instantiation))
12469 reconsider = 1;
12472 if (COMPLETE_TYPE_P (instantiation))
12473 /* If INSTANTIATION has been instantiated, then we don't
12474 need to consider it again in the future. */
12475 *t = TREE_CHAIN (*t);
12476 else
12478 last = *t;
12479 t = &TREE_CHAIN (*t);
12482 else
12484 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12485 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12487 instantiation
12488 = instantiate_decl (instantiation,
12489 /*defer_ok=*/0,
12490 /*expl_inst_class_mem_p=*/false);
12491 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12492 reconsider = 1;
12495 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12496 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12497 /* If INSTANTIATION has been instantiated, then we don't
12498 need to consider it again in the future. */
12499 *t = TREE_CHAIN (*t);
12500 else
12502 last = *t;
12503 t = &TREE_CHAIN (*t);
12506 tinst_depth = 0;
12507 current_tinst_level = NULL_TREE;
12509 last_pending_template = last;
12511 while (reconsider);
12513 input_location = saved_loc;
12514 in_system_header = saved_in_system_header;
12517 /* Substitute ARGVEC into T, which is a list of initializers for
12518 either base class or a non-static data member. The TREE_PURPOSEs
12519 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12520 instantiate_decl. */
12522 static tree
12523 tsubst_initializer_list (tree t, tree argvec)
12525 tree inits = NULL_TREE;
12527 for (; t; t = TREE_CHAIN (t))
12529 tree decl;
12530 tree init;
12532 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
12533 NULL_TREE);
12534 decl = expand_member_init (decl);
12535 if (decl && !DECL_P (decl))
12536 in_base_initializer = 1;
12538 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12539 NULL_TREE,
12540 /*integral_constant_expression_p=*/false);
12541 in_base_initializer = 0;
12543 if (decl)
12545 init = build_tree_list (decl, init);
12546 TREE_CHAIN (init) = inits;
12547 inits = init;
12550 return inits;
12553 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12555 static void
12556 set_current_access_from_decl (tree decl)
12558 if (TREE_PRIVATE (decl))
12559 current_access_specifier = access_private_node;
12560 else if (TREE_PROTECTED (decl))
12561 current_access_specifier = access_protected_node;
12562 else
12563 current_access_specifier = access_public_node;
12566 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12567 is the instantiation (which should have been created with
12568 start_enum) and ARGS are the template arguments to use. */
12570 static void
12571 tsubst_enum (tree tag, tree newtag, tree args)
12573 tree e;
12575 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12577 tree value;
12578 tree decl;
12580 decl = TREE_VALUE (e);
12581 /* Note that in a template enum, the TREE_VALUE is the
12582 CONST_DECL, not the corresponding INTEGER_CST. */
12583 value = tsubst_expr (DECL_INITIAL (decl),
12584 args, tf_warning_or_error, NULL_TREE,
12585 /*integral_constant_expression_p=*/true);
12587 /* Give this enumeration constant the correct access. */
12588 set_current_access_from_decl (decl);
12590 /* Actually build the enumerator itself. */
12591 build_enumerator (DECL_NAME (decl), value, newtag);
12594 finish_enum (newtag);
12595 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12596 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12599 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12600 its type -- but without substituting the innermost set of template
12601 arguments. So, innermost set of template parameters will appear in
12602 the type. */
12604 tree
12605 get_mostly_instantiated_function_type (tree decl)
12607 tree fn_type;
12608 tree tmpl;
12609 tree targs;
12610 tree tparms;
12611 int parm_depth;
12613 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12614 targs = DECL_TI_ARGS (decl);
12615 tparms = DECL_TEMPLATE_PARMS (tmpl);
12616 parm_depth = TMPL_PARMS_DEPTH (tparms);
12618 /* There should be as many levels of arguments as there are levels
12619 of parameters. */
12620 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12622 fn_type = TREE_TYPE (tmpl);
12624 if (parm_depth == 1)
12625 /* No substitution is necessary. */
12627 else
12629 int i, save_access_control;
12630 tree partial_args;
12632 /* Replace the innermost level of the TARGS with NULL_TREEs to
12633 let tsubst know not to substitute for those parameters. */
12634 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12635 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12636 SET_TMPL_ARGS_LEVEL (partial_args, i,
12637 TMPL_ARGS_LEVEL (targs, i));
12638 SET_TMPL_ARGS_LEVEL (partial_args,
12639 TMPL_ARGS_DEPTH (targs),
12640 make_tree_vec (DECL_NTPARMS (tmpl)));
12642 /* Disable access control as this function is used only during
12643 name-mangling. */
12644 save_access_control = flag_access_control;
12645 flag_access_control = 0;
12647 ++processing_template_decl;
12648 /* Now, do the (partial) substitution to figure out the
12649 appropriate function type. */
12650 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12651 --processing_template_decl;
12653 /* Substitute into the template parameters to obtain the real
12654 innermost set of parameters. This step is important if the
12655 innermost set of template parameters contains value
12656 parameters whose types depend on outer template parameters. */
12657 TREE_VEC_LENGTH (partial_args)--;
12658 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12660 flag_access_control = save_access_control;
12663 return fn_type;
12666 /* Return truthvalue if we're processing a template different from
12667 the last one involved in diagnostics. */
12669 problematic_instantiation_changed (void)
12671 return last_template_error_tick != tinst_level_tick;
12674 /* Remember current template involved in diagnostics. */
12675 void
12676 record_last_problematic_instantiation (void)
12678 last_template_error_tick = tinst_level_tick;
12681 tree
12682 current_instantiation (void)
12684 return current_tinst_level;
12687 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12688 type. Return zero for ok, nonzero for disallowed. Issue error and
12689 warning messages under control of COMPLAIN. */
12691 static int
12692 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12694 if (INTEGRAL_TYPE_P (type))
12695 return 0;
12696 else if (POINTER_TYPE_P (type))
12697 return 0;
12698 else if (TYPE_PTR_TO_MEMBER_P (type))
12699 return 0;
12700 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12701 return 0;
12702 else if (TREE_CODE (type) == TYPENAME_TYPE)
12703 return 0;
12705 if (complain & tf_error)
12706 error ("%q#T is not a valid type for a template constant parameter", type);
12707 return 1;
12710 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12711 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12713 static bool
12714 dependent_type_p_r (tree type)
12716 tree scope;
12718 /* [temp.dep.type]
12720 A type is dependent if it is:
12722 -- a template parameter. Template template parameters are types
12723 for us (since TYPE_P holds true for them) so we handle
12724 them here. */
12725 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12726 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12727 return true;
12728 /* -- a qualified-id with a nested-name-specifier which contains a
12729 class-name that names a dependent type or whose unqualified-id
12730 names a dependent type. */
12731 if (TREE_CODE (type) == TYPENAME_TYPE)
12732 return true;
12733 /* -- a cv-qualified type where the cv-unqualified type is
12734 dependent. */
12735 type = TYPE_MAIN_VARIANT (type);
12736 /* -- a compound type constructed from any dependent type. */
12737 if (TYPE_PTR_TO_MEMBER_P (type))
12738 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12739 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12740 (type)));
12741 else if (TREE_CODE (type) == POINTER_TYPE
12742 || TREE_CODE (type) == REFERENCE_TYPE)
12743 return dependent_type_p (TREE_TYPE (type));
12744 else if (TREE_CODE (type) == FUNCTION_TYPE
12745 || TREE_CODE (type) == METHOD_TYPE)
12747 tree arg_type;
12749 if (dependent_type_p (TREE_TYPE (type)))
12750 return true;
12751 for (arg_type = TYPE_ARG_TYPES (type);
12752 arg_type;
12753 arg_type = TREE_CHAIN (arg_type))
12754 if (dependent_type_p (TREE_VALUE (arg_type)))
12755 return true;
12756 return false;
12758 /* -- an array type constructed from any dependent type or whose
12759 size is specified by a constant expression that is
12760 value-dependent. */
12761 if (TREE_CODE (type) == ARRAY_TYPE)
12763 if (TYPE_DOMAIN (type)
12764 && ((value_dependent_expression_p
12765 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12766 || (type_dependent_expression_p
12767 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12768 return true;
12769 return dependent_type_p (TREE_TYPE (type));
12772 /* -- a template-id in which either the template name is a template
12773 parameter ... */
12774 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12775 return true;
12776 /* ... or any of the template arguments is a dependent type or
12777 an expression that is type-dependent or value-dependent. */
12778 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12779 && (any_dependent_template_arguments_p
12780 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12781 return true;
12783 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12784 expression is not type-dependent, then it should already been
12785 have resolved. */
12786 if (TREE_CODE (type) == TYPEOF_TYPE)
12787 return true;
12789 /* The standard does not specifically mention types that are local
12790 to template functions or local classes, but they should be
12791 considered dependent too. For example:
12793 template <int I> void f() {
12794 enum E { a = I };
12795 S<sizeof (E)> s;
12798 The size of `E' cannot be known until the value of `I' has been
12799 determined. Therefore, `E' must be considered dependent. */
12800 scope = TYPE_CONTEXT (type);
12801 if (scope && TYPE_P (scope))
12802 return dependent_type_p (scope);
12803 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12804 return type_dependent_expression_p (scope);
12806 /* Other types are non-dependent. */
12807 return false;
12810 /* Returns TRUE if TYPE is dependent, in the sense of
12811 [temp.dep.type]. */
12813 bool
12814 dependent_type_p (tree type)
12816 /* If there are no template parameters in scope, then there can't be
12817 any dependent types. */
12818 if (!processing_template_decl)
12820 /* If we are not processing a template, then nobody should be
12821 providing us with a dependent type. */
12822 gcc_assert (type);
12823 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12824 return false;
12827 /* If the type is NULL, we have not computed a type for the entity
12828 in question; in that case, the type is dependent. */
12829 if (!type)
12830 return true;
12832 /* Erroneous types can be considered non-dependent. */
12833 if (type == error_mark_node)
12834 return false;
12836 /* If we have not already computed the appropriate value for TYPE,
12837 do so now. */
12838 if (!TYPE_DEPENDENT_P_VALID (type))
12840 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12841 TYPE_DEPENDENT_P_VALID (type) = 1;
12844 return TYPE_DEPENDENT_P (type);
12847 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12849 static bool
12850 dependent_scope_ref_p (tree expression, bool criterion (tree))
12852 tree scope;
12853 tree name;
12855 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12857 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12858 return true;
12860 scope = TREE_OPERAND (expression, 0);
12861 name = TREE_OPERAND (expression, 1);
12863 /* [temp.dep.expr]
12865 An id-expression is type-dependent if it contains a
12866 nested-name-specifier that contains a class-name that names a
12867 dependent type. */
12868 /* The suggested resolution to Core Issue 2 implies that if the
12869 qualifying type is the current class, then we must peek
12870 inside it. */
12871 if (DECL_P (name)
12872 && currently_open_class (scope)
12873 && !criterion (name))
12874 return false;
12875 if (dependent_type_p (scope))
12876 return true;
12878 return false;
12881 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12882 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12883 expression. */
12885 bool
12886 value_dependent_expression_p (tree expression)
12888 if (!processing_template_decl)
12889 return false;
12891 /* A name declared with a dependent type. */
12892 if (DECL_P (expression) && type_dependent_expression_p (expression))
12893 return true;
12895 switch (TREE_CODE (expression))
12897 case IDENTIFIER_NODE:
12898 /* A name that has not been looked up -- must be dependent. */
12899 return true;
12901 case TEMPLATE_PARM_INDEX:
12902 /* A non-type template parm. */
12903 return true;
12905 case CONST_DECL:
12906 /* A non-type template parm. */
12907 if (DECL_TEMPLATE_PARM_P (expression))
12908 return true;
12909 return false;
12911 case VAR_DECL:
12912 /* A constant with integral or enumeration type and is initialized
12913 with an expression that is value-dependent. */
12914 if (DECL_INITIAL (expression)
12915 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12916 && value_dependent_expression_p (DECL_INITIAL (expression)))
12917 return true;
12918 return false;
12920 case DYNAMIC_CAST_EXPR:
12921 case STATIC_CAST_EXPR:
12922 case CONST_CAST_EXPR:
12923 case REINTERPRET_CAST_EXPR:
12924 case CAST_EXPR:
12925 /* These expressions are value-dependent if the type to which
12926 the cast occurs is dependent or the expression being casted
12927 is value-dependent. */
12929 tree type = TREE_TYPE (expression);
12931 if (dependent_type_p (type))
12932 return true;
12934 /* A functional cast has a list of operands. */
12935 expression = TREE_OPERAND (expression, 0);
12936 if (!expression)
12938 /* If there are no operands, it must be an expression such
12939 as "int()". This should not happen for aggregate types
12940 because it would form non-constant expressions. */
12941 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12943 return false;
12946 if (TREE_CODE (expression) == TREE_LIST)
12948 for (; expression; expression = TREE_CHAIN (expression))
12949 if (value_dependent_expression_p (TREE_VALUE (expression)))
12950 return true;
12951 return false;
12954 return value_dependent_expression_p (expression);
12957 case SIZEOF_EXPR:
12958 case ALIGNOF_EXPR:
12959 /* A `sizeof' expression is value-dependent if the operand is
12960 type-dependent. */
12961 expression = TREE_OPERAND (expression, 0);
12962 if (TYPE_P (expression))
12963 return dependent_type_p (expression);
12964 return type_dependent_expression_p (expression);
12966 case SCOPE_REF:
12967 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12969 case COMPONENT_REF:
12970 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12971 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12973 case CALL_EXPR:
12974 /* A CALL_EXPR may appear in a constant expression if it is a
12975 call to a builtin function, e.g., __builtin_constant_p. All
12976 such calls are value-dependent. */
12977 return true;
12979 default:
12980 /* A constant expression is value-dependent if any subexpression is
12981 value-dependent. */
12982 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12984 case tcc_reference:
12985 case tcc_unary:
12986 return (value_dependent_expression_p
12987 (TREE_OPERAND (expression, 0)));
12989 case tcc_comparison:
12990 case tcc_binary:
12991 return ((value_dependent_expression_p
12992 (TREE_OPERAND (expression, 0)))
12993 || (value_dependent_expression_p
12994 (TREE_OPERAND (expression, 1))));
12996 case tcc_expression:
12998 int i;
12999 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
13000 /* In some cases, some of the operands may be missing.
13001 (For example, in the case of PREDECREMENT_EXPR, the
13002 amount to increment by may be missing.) That doesn't
13003 make the expression dependent. */
13004 if (TREE_OPERAND (expression, i)
13005 && (value_dependent_expression_p
13006 (TREE_OPERAND (expression, i))))
13007 return true;
13008 return false;
13011 default:
13012 break;
13016 /* The expression is not value-dependent. */
13017 return false;
13020 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
13021 [temp.dep.expr]. */
13023 bool
13024 type_dependent_expression_p (tree expression)
13026 if (!processing_template_decl)
13027 return false;
13029 if (expression == error_mark_node)
13030 return false;
13032 /* An unresolved name is always dependent. */
13033 if (TREE_CODE (expression) == IDENTIFIER_NODE
13034 || TREE_CODE (expression) == USING_DECL)
13035 return true;
13037 /* Some expression forms are never type-dependent. */
13038 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
13039 || TREE_CODE (expression) == SIZEOF_EXPR
13040 || TREE_CODE (expression) == ALIGNOF_EXPR
13041 || TREE_CODE (expression) == TYPEID_EXPR
13042 || TREE_CODE (expression) == DELETE_EXPR
13043 || TREE_CODE (expression) == VEC_DELETE_EXPR
13044 || TREE_CODE (expression) == THROW_EXPR)
13045 return false;
13047 /* The types of these expressions depends only on the type to which
13048 the cast occurs. */
13049 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
13050 || TREE_CODE (expression) == STATIC_CAST_EXPR
13051 || TREE_CODE (expression) == CONST_CAST_EXPR
13052 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
13053 || TREE_CODE (expression) == CAST_EXPR)
13054 return dependent_type_p (TREE_TYPE (expression));
13056 /* The types of these expressions depends only on the type created
13057 by the expression. */
13058 if (TREE_CODE (expression) == NEW_EXPR
13059 || TREE_CODE (expression) == VEC_NEW_EXPR)
13061 /* For NEW_EXPR tree nodes created inside a template, either
13062 the object type itself or a TREE_LIST may appear as the
13063 operand 1. */
13064 tree type = TREE_OPERAND (expression, 1);
13065 if (TREE_CODE (type) == TREE_LIST)
13066 /* This is an array type. We need to check array dimensions
13067 as well. */
13068 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
13069 || value_dependent_expression_p
13070 (TREE_OPERAND (TREE_VALUE (type), 1));
13071 else
13072 return dependent_type_p (type);
13075 if (TREE_CODE (expression) == SCOPE_REF
13076 && dependent_scope_ref_p (expression,
13077 type_dependent_expression_p))
13078 return true;
13080 if (TREE_CODE (expression) == FUNCTION_DECL
13081 && DECL_LANG_SPECIFIC (expression)
13082 && DECL_TEMPLATE_INFO (expression)
13083 && (any_dependent_template_arguments_p
13084 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
13085 return true;
13087 if (TREE_CODE (expression) == TEMPLATE_DECL
13088 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
13089 return false;
13091 if (TREE_TYPE (expression) == unknown_type_node)
13093 if (TREE_CODE (expression) == ADDR_EXPR)
13094 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
13095 if (TREE_CODE (expression) == COMPONENT_REF
13096 || TREE_CODE (expression) == OFFSET_REF)
13098 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
13099 return true;
13100 expression = TREE_OPERAND (expression, 1);
13101 if (TREE_CODE (expression) == IDENTIFIER_NODE)
13102 return false;
13104 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
13105 if (TREE_CODE (expression) == SCOPE_REF)
13106 return false;
13108 if (TREE_CODE (expression) == BASELINK)
13109 expression = BASELINK_FUNCTIONS (expression);
13111 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
13113 if (any_dependent_template_arguments_p
13114 (TREE_OPERAND (expression, 1)))
13115 return true;
13116 expression = TREE_OPERAND (expression, 0);
13118 gcc_assert (TREE_CODE (expression) == OVERLOAD
13119 || TREE_CODE (expression) == FUNCTION_DECL);
13121 while (expression)
13123 if (type_dependent_expression_p (OVL_CURRENT (expression)))
13124 return true;
13125 expression = OVL_NEXT (expression);
13127 return false;
13130 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
13132 return (dependent_type_p (TREE_TYPE (expression)));
13135 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13136 contains a type-dependent expression. */
13138 bool
13139 any_type_dependent_arguments_p (tree args)
13141 while (args)
13143 tree arg = TREE_VALUE (args);
13145 if (type_dependent_expression_p (arg))
13146 return true;
13147 args = TREE_CHAIN (args);
13149 return false;
13152 /* Returns TRUE if the ARG (a template argument) is dependent. */
13154 static bool
13155 dependent_template_arg_p (tree arg)
13157 if (!processing_template_decl)
13158 return false;
13160 if (TREE_CODE (arg) == TEMPLATE_DECL
13161 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13162 return dependent_template_p (arg);
13163 else if (TYPE_P (arg))
13164 return dependent_type_p (arg);
13165 else
13166 return (type_dependent_expression_p (arg)
13167 || value_dependent_expression_p (arg));
13170 /* Returns true if ARGS (a collection of template arguments) contains
13171 any dependent arguments. */
13173 bool
13174 any_dependent_template_arguments_p (tree args)
13176 int i;
13177 int j;
13179 if (!args)
13180 return false;
13181 if (args == error_mark_node)
13182 return true;
13184 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13186 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13187 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13188 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13189 return true;
13192 return false;
13195 /* Returns TRUE if the template TMPL is dependent. */
13197 bool
13198 dependent_template_p (tree tmpl)
13200 if (TREE_CODE (tmpl) == OVERLOAD)
13202 while (tmpl)
13204 if (dependent_template_p (OVL_FUNCTION (tmpl)))
13205 return true;
13206 tmpl = OVL_CHAIN (tmpl);
13208 return false;
13211 /* Template template parameters are dependent. */
13212 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13213 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13214 return true;
13215 /* So are names that have not been looked up. */
13216 if (TREE_CODE (tmpl) == SCOPE_REF
13217 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
13218 return true;
13219 /* So are member templates of dependent classes. */
13220 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13221 return dependent_type_p (DECL_CONTEXT (tmpl));
13222 return false;
13225 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13227 bool
13228 dependent_template_id_p (tree tmpl, tree args)
13230 return (dependent_template_p (tmpl)
13231 || any_dependent_template_arguments_p (args));
13234 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13235 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13236 can be found. Note that this function peers inside uninstantiated
13237 templates and therefore should be used only in extremely limited
13238 situations. ONLY_CURRENT_P restricts this peering to the currently
13239 open classes hierarchy (which is required when comparing types). */
13241 tree
13242 resolve_typename_type (tree type, bool only_current_p)
13244 tree scope;
13245 tree name;
13246 tree decl;
13247 int quals;
13248 tree pushed_scope;
13250 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
13252 scope = TYPE_CONTEXT (type);
13253 name = TYPE_IDENTIFIER (type);
13255 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13256 it first before we can figure out what NAME refers to. */
13257 if (TREE_CODE (scope) == TYPENAME_TYPE)
13258 scope = resolve_typename_type (scope, only_current_p);
13259 /* If we don't know what SCOPE refers to, then we cannot resolve the
13260 TYPENAME_TYPE. */
13261 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13262 return error_mark_node;
13263 /* If the SCOPE is a template type parameter, we have no way of
13264 resolving the name. */
13265 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13266 return type;
13267 /* If the SCOPE is not the current instantiation, there's no reason
13268 to look inside it. */
13269 if (only_current_p && !currently_open_class (scope))
13270 return error_mark_node;
13271 /* If SCOPE is a partial instantiation, it will not have a valid
13272 TYPE_FIELDS list, so use the original template. */
13273 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
13274 /* Enter the SCOPE so that name lookup will be resolved as if we
13275 were in the class definition. In particular, SCOPE will no
13276 longer be considered a dependent type. */
13277 pushed_scope = push_scope (scope);
13278 /* Look up the declaration. */
13279 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
13280 /* Obtain the set of qualifiers applied to the TYPE. */
13281 quals = cp_type_quals (type);
13282 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13283 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13284 if (!decl)
13285 type = error_mark_node;
13286 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13287 && TREE_CODE (decl) == TYPE_DECL)
13288 type = TREE_TYPE (decl);
13289 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13290 && DECL_CLASS_TEMPLATE_P (decl))
13292 tree tmpl;
13293 tree args;
13294 /* Obtain the template and the arguments. */
13295 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13296 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13297 /* Instantiate the template. */
13298 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
13299 /*entering_scope=*/0, tf_error | tf_user);
13301 else
13302 type = error_mark_node;
13303 /* Qualify the resulting type. */
13304 if (type != error_mark_node && quals)
13305 type = cp_build_qualified_type (type, quals);
13306 /* Leave the SCOPE. */
13307 if (pushed_scope)
13308 pop_scope (pushed_scope);
13310 return type;
13313 /* EXPR is an expression which is not type-dependent. Return a proxy
13314 for EXPR that can be used to compute the types of larger
13315 expressions containing EXPR. */
13317 tree
13318 build_non_dependent_expr (tree expr)
13320 tree inner_expr;
13322 /* Preserve null pointer constants so that the type of things like
13323 "p == 0" where "p" is a pointer can be determined. */
13324 if (null_ptr_cst_p (expr))
13325 return expr;
13326 /* Preserve OVERLOADs; the functions must be available to resolve
13327 types. */
13328 inner_expr = expr;
13329 if (TREE_CODE (inner_expr) == ADDR_EXPR)
13330 inner_expr = TREE_OPERAND (inner_expr, 0);
13331 if (TREE_CODE (inner_expr) == COMPONENT_REF)
13332 inner_expr = TREE_OPERAND (inner_expr, 1);
13333 if (is_overloaded_fn (inner_expr)
13334 || TREE_CODE (inner_expr) == OFFSET_REF)
13335 return expr;
13336 /* There is no need to return a proxy for a variable. */
13337 if (TREE_CODE (expr) == VAR_DECL)
13338 return expr;
13339 /* Preserve string constants; conversions from string constants to
13340 "char *" are allowed, even though normally a "const char *"
13341 cannot be used to initialize a "char *". */
13342 if (TREE_CODE (expr) == STRING_CST)
13343 return expr;
13344 /* Preserve arithmetic constants, as an optimization -- there is no
13345 reason to create a new node. */
13346 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13347 return expr;
13348 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13349 There is at least one place where we want to know that a
13350 particular expression is a throw-expression: when checking a ?:
13351 expression, there are special rules if the second or third
13352 argument is a throw-expression. */
13353 if (TREE_CODE (expr) == THROW_EXPR)
13354 return expr;
13356 if (TREE_CODE (expr) == COND_EXPR)
13357 return build3 (COND_EXPR,
13358 TREE_TYPE (expr),
13359 TREE_OPERAND (expr, 0),
13360 (TREE_OPERAND (expr, 1)
13361 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13362 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13363 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
13364 if (TREE_CODE (expr) == COMPOUND_EXPR
13365 && !COMPOUND_EXPR_OVERLOADED (expr))
13366 return build2 (COMPOUND_EXPR,
13367 TREE_TYPE (expr),
13368 TREE_OPERAND (expr, 0),
13369 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
13371 /* If the type is unknown, it can't really be non-dependent */
13372 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
13374 /* Otherwise, build a NON_DEPENDENT_EXPR.
13376 REFERENCE_TYPEs are not stripped for expressions in templates
13377 because doing so would play havoc with mangling. Consider, for
13378 example:
13380 template <typename T> void f<T& g>() { g(); }
13382 In the body of "f", the expression for "g" will have
13383 REFERENCE_TYPE, even though the standard says that it should
13384 not. The reason is that we must preserve the syntactic form of
13385 the expression so that mangling (say) "f<g>" inside the body of
13386 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13387 stripped here. */
13388 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
13391 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
13392 Return a new TREE_LIST with the various arguments replaced with
13393 equivalent non-dependent expressions. */
13395 tree
13396 build_non_dependent_args (tree args)
13398 tree a;
13399 tree new_args;
13401 new_args = NULL_TREE;
13402 for (a = args; a; a = TREE_CHAIN (a))
13403 new_args = tree_cons (NULL_TREE,
13404 build_non_dependent_expr (TREE_VALUE (a)),
13405 new_args);
13406 return nreverse (new_args);
13409 #include "gt-cp-pt.h"