PR target/4198
[official-gcc.git] / gcc / cp / pt.c
blobb609ecf7eab6cd255c6f16cc9e661328e62e2eb8
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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "tree-inline.h"
39 #include "decl.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "rtl.h"
44 #include "timevar.h"
45 #include "tree-iterator.h"
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work.
54 The TREE_PURPOSE of each entry is either a DECL (for a function or
55 static data member), or a TYPE (for a class) indicating what we are
56 hoping to instantiate. The TREE_VALUE is not used. */
57 static GTY(()) tree pending_templates;
58 static GTY(()) tree last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static GTY(()) varray_type inline_parm_levels;
65 static size_t inline_parm_levels_used;
67 static GTY(()) tree current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
78 local variables. */
79 static htab_t local_specializations;
81 #define UNIFY_ALLOW_NONE 0
82 #define UNIFY_ALLOW_MORE_CV_QUAL 1
83 #define UNIFY_ALLOW_LESS_CV_QUAL 2
84 #define UNIFY_ALLOW_DERIVED 4
85 #define UNIFY_ALLOW_INTEGER 8
86 #define UNIFY_ALLOW_OUTER_LEVEL 16
87 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
88 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
89 #define UNIFY_ALLOW_MAX_CORRECTION 128
91 static void push_access_scope (tree);
92 static void pop_access_scope (tree);
93 static int resolve_overloaded_unification (tree, tree, tree, tree,
94 unification_kind_t, int);
95 static int try_one_overload (tree, tree, tree, tree, tree,
96 unification_kind_t, int, bool);
97 static int unify (tree, tree, tree, tree, int);
98 static void add_pending_template (tree);
99 static void reopen_tinst_level (tree);
100 static tree classtype_mangled_name (tree);
101 static char* mangle_class_name_for_template (const char *, tree, tree);
102 static tree tsubst_initializer_list (tree, tree);
103 static tree get_class_bindings (tree, tree, tree);
104 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
105 static void tsubst_enum (tree, tree, tree);
106 static tree add_to_template_args (tree, tree);
107 static tree add_outermost_template_args (tree, tree);
108 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
109 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
110 static int type_unification_real (tree, tree, tree, tree,
111 int, unification_kind_t, int);
112 static void note_template_header (int);
113 static tree convert_nontype_argument_function (tree, tree);
114 static tree convert_nontype_argument (tree, tree);
115 static tree convert_template_argument (tree, tree, tree,
116 tsubst_flags_t, int, tree);
117 static int for_each_template_parm (tree, tree_fn_t, void*,
118 struct pointer_set_t*);
119 static tree build_template_parm_index (int, int, int, tree, tree);
120 static int inline_needs_template_parms (tree);
121 static void push_inline_template_parms_recursive (tree, int);
122 static tree retrieve_local_specialization (tree);
123 static void register_local_specialization (tree, tree);
124 static tree reduce_template_parm_level (tree, tree, int);
125 static int mark_template_parm (tree, void *);
126 static int template_parm_this_level_p (tree, void *);
127 static tree tsubst_friend_function (tree, tree);
128 static tree tsubst_friend_class (tree, tree);
129 static int can_complete_type_without_circularity (tree);
130 static tree get_bindings (tree, tree, tree, bool);
131 static int template_decl_level (tree);
132 static int check_cv_quals_for_unify (int, tree, tree);
133 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
134 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
135 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
136 static void regenerate_decl_from_template (tree, tree);
137 static tree most_specialized (tree, tree, tree);
138 static tree most_specialized_class (tree, tree);
139 static int template_class_depth_real (tree, int);
140 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
141 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
142 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
143 static void check_specialization_scope (void);
144 static tree process_partial_specialization (tree);
145 static void set_current_access_from_decl (tree);
146 static void check_default_tmpl_args (tree, tree, int, int);
147 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
148 static tree get_template_base (tree, tree, tree, tree);
149 static int verify_class_unification (tree, tree, tree);
150 static tree try_class_unification (tree, tree, tree, tree);
151 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152 tree, tree);
153 static tree determine_specialization (tree, tree, tree *, int, int);
154 static int template_args_equal (tree, tree);
155 static void tsubst_default_arguments (tree);
156 static tree for_each_template_parm_r (tree *, int *, void *);
157 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
158 static void copy_default_args_to_explicit_spec (tree);
159 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
160 static int eq_local_specializations (const void *, const void *);
161 static bool dependent_type_p_r (tree);
162 static tree tsubst (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
166 /* Make the current scope suitable for access checking when we are
167 processing T. T can be FUNCTION_DECL for instantiated function
168 template, or VAR_DECL for static member variable (need by
169 instantiate_decl). */
171 static void
172 push_access_scope (tree t)
174 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
175 || TREE_CODE (t) == VAR_DECL);
177 if (DECL_FRIEND_CONTEXT (t))
178 push_nested_class (DECL_FRIEND_CONTEXT (t));
179 else if (DECL_CLASS_SCOPE_P (t))
180 push_nested_class (DECL_CONTEXT (t));
181 else
182 push_to_top_level ();
184 if (TREE_CODE (t) == FUNCTION_DECL)
186 saved_access_scope = tree_cons
187 (NULL_TREE, current_function_decl, saved_access_scope);
188 current_function_decl = t;
192 /* Restore the scope set up by push_access_scope. T is the node we
193 are processing. */
195 static void
196 pop_access_scope (tree t)
198 if (TREE_CODE (t) == FUNCTION_DECL)
200 current_function_decl = TREE_VALUE (saved_access_scope);
201 saved_access_scope = TREE_CHAIN (saved_access_scope);
204 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
205 pop_nested_class ();
206 else
207 pop_from_top_level ();
210 /* Do any processing required when DECL (a member template
211 declaration) is finished. Returns the TEMPLATE_DECL corresponding
212 to DECL, unless it is a specialization, in which case the DECL
213 itself is returned. */
215 tree
216 finish_member_template_decl (tree decl)
218 if (decl == error_mark_node)
219 return error_mark_node;
221 gcc_assert (DECL_P (decl));
223 if (TREE_CODE (decl) == TYPE_DECL)
225 tree type;
227 type = TREE_TYPE (decl);
228 if (IS_AGGR_TYPE (type)
229 && CLASSTYPE_TEMPLATE_INFO (type)
230 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
232 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
233 check_member_template (tmpl);
234 return tmpl;
236 return NULL_TREE;
238 else if (TREE_CODE (decl) == FIELD_DECL)
239 error ("data member %qD cannot be a member template", decl);
240 else if (DECL_TEMPLATE_INFO (decl))
242 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
244 check_member_template (DECL_TI_TEMPLATE (decl));
245 return DECL_TI_TEMPLATE (decl);
247 else
248 return decl;
250 else
251 error ("invalid member template declaration %qD", decl);
253 return error_mark_node;
256 /* Returns the template nesting level of the indicated class TYPE.
258 For example, in:
259 template <class T>
260 struct A
262 template <class U>
263 struct B {};
266 A<T>::B<U> has depth two, while A<T> has depth one.
267 Both A<T>::B<int> and A<int>::B<U> have depth one, if
268 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
269 specializations.
271 This function is guaranteed to return 0 if passed NULL_TREE so
272 that, for example, `template_class_depth (current_class_type)' is
273 always safe. */
275 static int
276 template_class_depth_real (tree type, int count_specializations)
278 int depth;
280 for (depth = 0;
281 type && TREE_CODE (type) != NAMESPACE_DECL;
282 type = (TREE_CODE (type) == FUNCTION_DECL)
283 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
285 if (TREE_CODE (type) != FUNCTION_DECL)
287 if (CLASSTYPE_TEMPLATE_INFO (type)
288 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
289 && ((count_specializations
290 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
291 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
292 ++depth;
294 else
296 if (DECL_TEMPLATE_INFO (type)
297 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
298 && ((count_specializations
299 && DECL_TEMPLATE_SPECIALIZATION (type))
300 || uses_template_parms (DECL_TI_ARGS (type))))
301 ++depth;
305 return depth;
308 /* Returns the template nesting level of the indicated class TYPE.
309 Like template_class_depth_real, but instantiations do not count in
310 the depth. */
312 int
313 template_class_depth (tree type)
315 return template_class_depth_real (type, /*count_specializations=*/0);
318 /* Returns 1 if processing DECL as part of do_pending_inlines
319 needs us to push template parms. */
321 static int
322 inline_needs_template_parms (tree decl)
324 if (! DECL_TEMPLATE_INFO (decl))
325 return 0;
327 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
328 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
331 /* Subroutine of maybe_begin_member_template_processing.
332 Push the template parms in PARMS, starting from LEVELS steps into the
333 chain, and ending at the beginning, since template parms are listed
334 innermost first. */
336 static void
337 push_inline_template_parms_recursive (tree parmlist, int levels)
339 tree parms = TREE_VALUE (parmlist);
340 int i;
342 if (levels > 1)
343 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
345 ++processing_template_decl;
346 current_template_parms
347 = tree_cons (size_int (processing_template_decl),
348 parms, current_template_parms);
349 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
351 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
352 NULL);
353 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
355 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
356 gcc_assert (DECL_P (parm));
358 switch (TREE_CODE (parm))
360 case TYPE_DECL:
361 case TEMPLATE_DECL:
362 pushdecl (parm);
363 break;
365 case PARM_DECL:
367 /* Make a CONST_DECL as is done in process_template_parm.
368 It is ugly that we recreate this here; the original
369 version built in process_template_parm is no longer
370 available. */
371 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
372 TREE_TYPE (parm));
373 DECL_ARTIFICIAL (decl) = 1;
374 TREE_CONSTANT (decl) = 1;
375 TREE_INVARIANT (decl) = 1;
376 TREE_READONLY (decl) = 1;
377 DECL_INITIAL (decl) = DECL_INITIAL (parm);
378 SET_DECL_TEMPLATE_PARM_P (decl);
379 pushdecl (decl);
381 break;
383 default:
384 gcc_unreachable ();
389 /* Restore the template parameter context for a member template or
390 a friend template defined in a class definition. */
392 void
393 maybe_begin_member_template_processing (tree decl)
395 tree parms;
396 int levels = 0;
398 if (inline_needs_template_parms (decl))
400 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
401 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
403 if (DECL_TEMPLATE_SPECIALIZATION (decl))
405 --levels;
406 parms = TREE_CHAIN (parms);
409 push_inline_template_parms_recursive (parms, levels);
412 /* Remember how many levels of template parameters we pushed so that
413 we can pop them later. */
414 if (!inline_parm_levels)
415 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
416 if (inline_parm_levels_used == inline_parm_levels->num_elements)
417 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
418 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
419 ++inline_parm_levels_used;
422 /* Undo the effects of begin_member_template_processing. */
424 void
425 maybe_end_member_template_processing (void)
427 int i;
429 if (!inline_parm_levels_used)
430 return;
432 --inline_parm_levels_used;
433 for (i = 0;
434 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
435 ++i)
437 --processing_template_decl;
438 current_template_parms = TREE_CHAIN (current_template_parms);
439 poplevel (0, 0, 0);
443 /* Return a new template argument vector which contains all of ARGS,
444 but has as its innermost set of arguments the EXTRA_ARGS. */
446 static tree
447 add_to_template_args (tree args, tree extra_args)
449 tree new_args;
450 int extra_depth;
451 int i;
452 int j;
454 extra_depth = TMPL_ARGS_DEPTH (extra_args);
455 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
457 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
458 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
460 for (j = 1; j <= extra_depth; ++j, ++i)
461 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
463 return new_args;
466 /* Like add_to_template_args, but only the outermost ARGS are added to
467 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
468 (EXTRA_ARGS) levels are added. This function is used to combine
469 the template arguments from a partial instantiation with the
470 template arguments used to attain the full instantiation from the
471 partial instantiation. */
473 static tree
474 add_outermost_template_args (tree args, tree extra_args)
476 tree new_args;
478 /* If there are more levels of EXTRA_ARGS than there are ARGS,
479 something very fishy is going on. */
480 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
482 /* If *all* the new arguments will be the EXTRA_ARGS, just return
483 them. */
484 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
485 return extra_args;
487 /* For the moment, we make ARGS look like it contains fewer levels. */
488 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
490 new_args = add_to_template_args (args, extra_args);
492 /* Now, we restore ARGS to its full dimensions. */
493 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
495 return new_args;
498 /* Return the N levels of innermost template arguments from the ARGS. */
500 tree
501 get_innermost_template_args (tree args, int n)
503 tree new_args;
504 int extra_levels;
505 int i;
507 gcc_assert (n >= 0);
509 /* If N is 1, just return the innermost set of template arguments. */
510 if (n == 1)
511 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
513 /* If we're not removing anything, just return the arguments we were
514 given. */
515 extra_levels = TMPL_ARGS_DEPTH (args) - n;
516 gcc_assert (extra_levels >= 0);
517 if (extra_levels == 0)
518 return args;
520 /* Make a new set of arguments, not containing the outer arguments. */
521 new_args = make_tree_vec (n);
522 for (i = 1; i <= n; ++i)
523 SET_TMPL_ARGS_LEVEL (new_args, i,
524 TMPL_ARGS_LEVEL (args, i + extra_levels));
526 return new_args;
529 /* We've got a template header coming up; push to a new level for storing
530 the parms. */
532 void
533 begin_template_parm_list (void)
535 /* We use a non-tag-transparent scope here, which causes pushtag to
536 put tags in this scope, rather than in the enclosing class or
537 namespace scope. This is the right thing, since we want
538 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
539 global template class, push_template_decl handles putting the
540 TEMPLATE_DECL into top-level scope. For a nested template class,
541 e.g.:
543 template <class T> struct S1 {
544 template <class T> struct S2 {};
547 pushtag contains special code to call pushdecl_with_scope on the
548 TEMPLATE_DECL for S2. */
549 begin_scope (sk_template_parms, NULL);
550 ++processing_template_decl;
551 ++processing_template_parmlist;
552 note_template_header (0);
555 /* This routine is called when a specialization is declared. If it is
556 invalid to declare a specialization here, an error is reported. */
558 static void
559 check_specialization_scope (void)
561 tree scope = current_scope ();
563 /* [temp.expl.spec]
565 An explicit specialization shall be declared in the namespace of
566 which the template is a member, or, for member templates, in the
567 namespace of which the enclosing class or enclosing class
568 template is a member. An explicit specialization of a member
569 function, member class or static data member of a class template
570 shall be declared in the namespace of which the class template
571 is a member. */
572 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
573 error ("explicit specialization in non-namespace scope %qD", scope);
575 /* [temp.expl.spec]
577 In an explicit specialization declaration for a member of a class
578 template or a member template that appears in namespace scope,
579 the member template and some of its enclosing class templates may
580 remain unspecialized, except that the declaration shall not
581 explicitly specialize a class member template if its enclosing
582 class templates are not explicitly specialized as well. */
583 if (current_template_parms)
584 error ("enclosing class templates are not explicitly specialized");
587 /* We've just seen template <>. */
589 void
590 begin_specialization (void)
592 begin_scope (sk_template_spec, NULL);
593 note_template_header (1);
594 check_specialization_scope ();
597 /* Called at then end of processing a declaration preceded by
598 template<>. */
600 void
601 end_specialization (void)
603 finish_scope ();
604 reset_specialization ();
607 /* Any template <>'s that we have seen thus far are not referring to a
608 function specialization. */
610 void
611 reset_specialization (void)
613 processing_specialization = 0;
614 template_header_count = 0;
617 /* We've just seen a template header. If SPECIALIZATION is nonzero,
618 it was of the form template <>. */
620 static void
621 note_template_header (int specialization)
623 processing_specialization = specialization;
624 template_header_count++;
627 /* We're beginning an explicit instantiation. */
629 void
630 begin_explicit_instantiation (void)
632 gcc_assert (!processing_explicit_instantiation);
633 processing_explicit_instantiation = true;
637 void
638 end_explicit_instantiation (void)
640 gcc_assert (processing_explicit_instantiation);
641 processing_explicit_instantiation = false;
644 /* A explicit specialization or partial specialization TMPL is being
645 declared. Check that the namespace in which the specialization is
646 occurring is permissible. Returns false iff it is invalid to
647 specialize TMPL in the current namespace. */
649 static bool
650 check_specialization_namespace (tree tmpl)
652 tree tpl_ns = decl_namespace_context (tmpl);
654 /* [tmpl.expl.spec]
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template is
662 a member. */
663 if (is_associated_namespace (current_namespace, tpl_ns))
664 /* Same or super-using namespace. */
665 return true;
666 else
668 pedwarn ("specialization of %qD in different namespace", tmpl);
669 cp_pedwarn_at (" from definition of %q#D", tmpl);
670 return false;
674 /* The TYPE is being declared. If it is a template type, that means it
675 is a partial specialization. Do appropriate error-checking. */
677 void
678 maybe_process_partial_specialization (tree type)
680 /* TYPE maybe an ERROR_MARK_NODE. */
681 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
683 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
685 /* This is for ordinary explicit specialization and partial
686 specialization of a template class such as:
688 template <> class C<int>;
692 template <class T> class C<T*>;
694 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
696 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
697 && !COMPLETE_TYPE_P (type))
699 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
700 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
701 if (processing_template_decl)
702 push_template_decl (TYPE_MAIN_DECL (type));
704 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
705 error ("specialization of %qT after instantiation", type);
707 else if (CLASS_TYPE_P (type)
708 && !CLASSTYPE_USE_TEMPLATE (type)
709 && CLASSTYPE_TEMPLATE_INFO (type)
710 && context && CLASS_TYPE_P (context)
711 && CLASSTYPE_TEMPLATE_INFO (context))
713 /* This is for an explicit specialization of member class
714 template according to [temp.expl.spec/18]:
716 template <> template <class U> class C<int>::D;
718 The context `C<int>' must be an implicit instantiation.
719 Otherwise this is just a member class template declared
720 earlier like:
722 template <> class C<int> { template <class U> class D; };
723 template <> template <class U> class C<int>::D;
725 In the first case, `C<int>::D' is a specialization of `C<T>::D'
726 while in the second case, `C<int>::D' is a primary template
727 and `C<T>::D' may not exist. */
729 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
730 && !COMPLETE_TYPE_P (type))
732 tree t;
734 if (current_namespace
735 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
737 pedwarn ("specializing %q#T in different namespace", type);
738 cp_pedwarn_at (" from definition of %q#D",
739 CLASSTYPE_TI_TEMPLATE (type));
742 /* Check for invalid specialization after instantiation:
744 template <> template <> class C<int>::D<int>;
745 template <> template <class U> class C<int>::D; */
747 for (t = DECL_TEMPLATE_INSTANTIATIONS
748 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
749 t; t = TREE_CHAIN (t))
750 if (TREE_VALUE (t) != type
751 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
752 error ("specialization %qT after instantiation %qT",
753 type, TREE_VALUE (t));
755 /* Mark TYPE as a specialization. And as a result, we only
756 have one level of template argument for the innermost
757 class template. */
758 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
759 CLASSTYPE_TI_ARGS (type)
760 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
763 else if (processing_specialization)
764 error ("explicit specialization of non-template %qT", type);
767 /* Returns nonzero if we can optimize the retrieval of specializations
768 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
769 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
771 static inline bool
772 optimize_specialization_lookup_p (tree tmpl)
774 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
775 && DECL_CLASS_SCOPE_P (tmpl)
776 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
777 parameter. */
778 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
779 /* The optimized lookup depends on the fact that the
780 template arguments for the member function template apply
781 purely to the containing class, which is not true if the
782 containing class is an explicit or partial
783 specialization. */
784 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
785 && !DECL_MEMBER_TEMPLATE_P (tmpl)
786 && !DECL_CONV_FN_P (tmpl)
787 /* It is possible to have a template that is not a member
788 template and is not a member of a template class:
790 template <typename T>
791 struct S { friend A::f(); };
793 Here, the friend function is a template, but the context does
794 not have template information. The optimized lookup relies
795 on having ARGS be the template arguments for both the class
796 and the function template. */
797 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
800 /* Retrieve the specialization (in the sense of [temp.spec] - a
801 specialization is either an instantiation or an explicit
802 specialization) of TMPL for the given template ARGS. If there is
803 no such specialization, return NULL_TREE. The ARGS are a vector of
804 arguments, or a vector of vectors of arguments, in the case of
805 templates with more than one level of parameters.
807 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
808 then we search for a partial specialization matching ARGS. This
809 parameter is ignored if TMPL is not a class template. */
811 static tree
812 retrieve_specialization (tree tmpl, tree args,
813 bool class_specializations_p)
815 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
817 /* There should be as many levels of arguments as there are
818 levels of parameters. */
819 gcc_assert (TMPL_ARGS_DEPTH (args)
820 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
822 if (optimize_specialization_lookup_p (tmpl))
824 tree class_template;
825 tree class_specialization;
826 VEC(tree) *methods;
827 tree fns;
828 int idx;
830 /* The template arguments actually apply to the containing
831 class. Find the class specialization with those
832 arguments. */
833 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
834 class_specialization
835 = retrieve_specialization (class_template, args,
836 /*class_specializations_p=*/false);
837 if (!class_specialization)
838 return NULL_TREE;
839 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
840 for the specialization. */
841 idx = class_method_index_for_fn (class_specialization, tmpl);
842 if (idx == -1)
843 return NULL_TREE;
844 /* Iterate through the methods with the indicated name, looking
845 for the one that has an instance of TMPL. */
846 methods = CLASSTYPE_METHOD_VEC (class_specialization);
847 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
849 tree fn = OVL_CURRENT (fns);
850 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
851 return fn;
853 return NULL_TREE;
855 else
857 tree *sp;
858 tree *head;
860 /* Class templates store their instantiations on the
861 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
862 DECL_TEMPLATE_SPECIALIZATIONS list. */
863 if (!class_specializations_p
864 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
865 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
866 else
867 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
868 head = sp;
869 /* Iterate through the list until we find a matching template. */
870 while (*sp != NULL_TREE)
872 tree spec = *sp;
874 if (comp_template_args (TREE_PURPOSE (spec), args))
876 /* Use the move-to-front heuristic to speed up future
877 searches. */
878 if (spec != *head)
880 *sp = TREE_CHAIN (*sp);
881 TREE_CHAIN (spec) = *head;
882 *head = spec;
884 return TREE_VALUE (spec);
886 sp = &TREE_CHAIN (spec);
890 return NULL_TREE;
893 /* Like retrieve_specialization, but for local declarations. */
895 static tree
896 retrieve_local_specialization (tree tmpl)
898 tree spec = htab_find_with_hash (local_specializations, tmpl,
899 htab_hash_pointer (tmpl));
900 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
903 /* Returns nonzero iff DECL is a specialization of TMPL. */
906 is_specialization_of (tree decl, tree tmpl)
908 tree t;
910 if (TREE_CODE (decl) == FUNCTION_DECL)
912 for (t = decl;
913 t != NULL_TREE;
914 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
915 if (t == tmpl)
916 return 1;
918 else
920 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
922 for (t = TREE_TYPE (decl);
923 t != NULL_TREE;
924 t = CLASSTYPE_USE_TEMPLATE (t)
925 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
926 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
927 return 1;
930 return 0;
933 /* Returns nonzero iff DECL is a specialization of friend declaration
934 FRIEND according to [temp.friend]. */
936 bool
937 is_specialization_of_friend (tree decl, tree friend)
939 bool need_template = true;
940 int template_depth;
942 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
943 || TREE_CODE (decl) == TYPE_DECL);
945 /* For [temp.friend/6] when FRIEND is an ordinary member function
946 of a template class, we want to check if DECL is a specialization
947 if this. */
948 if (TREE_CODE (friend) == FUNCTION_DECL
949 && DECL_TEMPLATE_INFO (friend)
950 && !DECL_USE_TEMPLATE (friend))
952 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
953 friend = DECL_TI_TEMPLATE (friend);
954 need_template = false;
956 else if (TREE_CODE (friend) == TEMPLATE_DECL
957 && !PRIMARY_TEMPLATE_P (friend))
958 need_template = false;
960 /* There is nothing to do if this is not a template friend. */
961 if (TREE_CODE (friend) != TEMPLATE_DECL)
962 return false;
964 if (is_specialization_of (decl, friend))
965 return true;
967 /* [temp.friend/6]
968 A member of a class template may be declared to be a friend of a
969 non-template class. In this case, the corresponding member of
970 every specialization of the class template is a friend of the
971 class granting friendship.
973 For example, given a template friend declaration
975 template <class T> friend void A<T>::f();
977 the member function below is considered a friend
979 template <> struct A<int> {
980 void f();
983 For this type of template friend, TEMPLATE_DEPTH below will be
984 nonzero. To determine if DECL is a friend of FRIEND, we first
985 check if the enclosing class is a specialization of another. */
987 template_depth = template_class_depth (DECL_CONTEXT (friend));
988 if (template_depth
989 && DECL_CLASS_SCOPE_P (decl)
990 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
991 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
993 /* Next, we check the members themselves. In order to handle
994 a few tricky cases, such as when FRIEND's are
996 template <class T> friend void A<T>::g(T t);
997 template <class T> template <T t> friend void A<T>::h();
999 and DECL's are
1001 void A<int>::g(int);
1002 template <int> void A<int>::h();
1004 we need to figure out ARGS, the template arguments from
1005 the context of DECL. This is required for template substitution
1006 of `T' in the function parameter of `g' and template parameter
1007 of `h' in the above examples. Here ARGS corresponds to `int'. */
1009 tree context = DECL_CONTEXT (decl);
1010 tree args = NULL_TREE;
1011 int current_depth = 0;
1013 while (current_depth < template_depth)
1015 if (CLASSTYPE_TEMPLATE_INFO (context))
1017 if (current_depth == 0)
1018 args = TYPE_TI_ARGS (context);
1019 else
1020 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1021 current_depth++;
1023 context = TYPE_CONTEXT (context);
1026 if (TREE_CODE (decl) == FUNCTION_DECL)
1028 bool is_template;
1029 tree friend_type;
1030 tree decl_type;
1031 tree friend_args_type;
1032 tree decl_args_type;
1034 /* Make sure that both DECL and FRIEND are templates or
1035 non-templates. */
1036 is_template = DECL_TEMPLATE_INFO (decl)
1037 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1038 if (need_template ^ is_template)
1039 return false;
1040 else if (is_template)
1042 /* If both are templates, check template parameter list. */
1043 tree friend_parms
1044 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1045 args, tf_none);
1046 if (!comp_template_parms
1047 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1048 friend_parms))
1049 return false;
1051 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1053 else
1054 decl_type = TREE_TYPE (decl);
1056 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1057 tf_none, NULL_TREE);
1058 if (friend_type == error_mark_node)
1059 return false;
1061 /* Check if return types match. */
1062 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1063 return false;
1065 /* Check if function parameter types match, ignoring the
1066 `this' parameter. */
1067 friend_args_type = TYPE_ARG_TYPES (friend_type);
1068 decl_args_type = TYPE_ARG_TYPES (decl_type);
1069 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1070 friend_args_type = TREE_CHAIN (friend_args_type);
1071 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1072 decl_args_type = TREE_CHAIN (decl_args_type);
1074 return compparms (decl_args_type, friend_args_type);
1076 else
1078 /* DECL is a TYPE_DECL */
1079 bool is_template;
1080 tree decl_type = TREE_TYPE (decl);
1082 /* Make sure that both DECL and FRIEND are templates or
1083 non-templates. */
1084 is_template
1085 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1086 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1088 if (need_template ^ is_template)
1089 return false;
1090 else if (is_template)
1092 tree friend_parms;
1093 /* If both are templates, check the name of the two
1094 TEMPLATE_DECL's first because is_friend didn't. */
1095 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1096 != DECL_NAME (friend))
1097 return false;
1099 /* Now check template parameter list. */
1100 friend_parms
1101 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1102 args, tf_none);
1103 return comp_template_parms
1104 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1105 friend_parms);
1107 else
1108 return (DECL_NAME (decl)
1109 == DECL_NAME (friend));
1112 return false;
1115 /* Register the specialization SPEC as a specialization of TMPL with
1116 the indicated ARGS. Returns SPEC, or an equivalent prior
1117 declaration, if available. */
1119 static tree
1120 register_specialization (tree spec, tree tmpl, tree args)
1122 tree fn;
1124 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1126 if (TREE_CODE (spec) == FUNCTION_DECL
1127 && uses_template_parms (DECL_TI_ARGS (spec)))
1128 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1129 register it; we want the corresponding TEMPLATE_DECL instead.
1130 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1131 the more obvious `uses_template_parms (spec)' to avoid problems
1132 with default function arguments. In particular, given
1133 something like this:
1135 template <class T> void f(T t1, T t = T())
1137 the default argument expression is not substituted for in an
1138 instantiation unless and until it is actually needed. */
1139 return spec;
1141 /* There should be as many levels of arguments as there are
1142 levels of parameters. */
1143 gcc_assert (TMPL_ARGS_DEPTH (args)
1144 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1146 fn = retrieve_specialization (tmpl, args,
1147 /*class_specializations_p=*/false);
1148 /* We can sometimes try to re-register a specialization that we've
1149 already got. In particular, regenerate_decl_from_template calls
1150 duplicate_decls which will update the specialization list. But,
1151 we'll still get called again here anyhow. It's more convenient
1152 to simply allow this than to try to prevent it. */
1153 if (fn == spec)
1154 return spec;
1155 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1157 if (DECL_TEMPLATE_INSTANTIATION (fn))
1159 if (TREE_USED (fn)
1160 || DECL_EXPLICIT_INSTANTIATION (fn))
1162 error ("specialization of %qD after instantiation",
1163 fn);
1164 return spec;
1166 else
1168 /* This situation should occur only if the first
1169 specialization is an implicit instantiation, the
1170 second is an explicit specialization, and the
1171 implicit instantiation has not yet been used. That
1172 situation can occur if we have implicitly
1173 instantiated a member function and then specialized
1174 it later.
1176 We can also wind up here if a friend declaration that
1177 looked like an instantiation turns out to be a
1178 specialization:
1180 template <class T> void foo(T);
1181 class S { friend void foo<>(int) };
1182 template <> void foo(int);
1184 We transform the existing DECL in place so that any
1185 pointers to it become pointers to the updated
1186 declaration.
1188 If there was a definition for the template, but not
1189 for the specialization, we want this to look as if
1190 there were no definition, and vice versa. */
1191 DECL_INITIAL (fn) = NULL_TREE;
1192 duplicate_decls (spec, fn);
1194 return fn;
1197 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1199 if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
1200 /* Dup decl failed, but this is a new definition. Set the
1201 line number so any errors match this new
1202 definition. */
1203 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1205 return fn;
1209 /* A specialization must be declared in the same namespace as the
1210 template it is specializing. */
1211 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1212 && !check_specialization_namespace (tmpl))
1213 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
1215 if (!optimize_specialization_lookup_p (tmpl))
1216 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1217 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1219 return spec;
1222 /* Unregister the specialization SPEC as a specialization of TMPL.
1223 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1224 if the SPEC was listed as a specialization of TMPL. */
1226 bool
1227 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1229 tree* s;
1231 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1232 *s != NULL_TREE;
1233 s = &TREE_CHAIN (*s))
1234 if (TREE_VALUE (*s) == spec)
1236 if (!new_spec)
1237 *s = TREE_CHAIN (*s);
1238 else
1239 TREE_VALUE (*s) = new_spec;
1240 return 1;
1243 return 0;
1246 /* Compare an entry in the local specializations hash table P1 (which
1247 is really a pointer to a TREE_LIST) with P2 (which is really a
1248 DECL). */
1250 static int
1251 eq_local_specializations (const void *p1, const void *p2)
1253 return TREE_VALUE ((tree) p1) == (tree) p2;
1256 /* Hash P1, an entry in the local specializations table. */
1258 static hashval_t
1259 hash_local_specialization (const void* p1)
1261 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1264 /* Like register_specialization, but for local declarations. We are
1265 registering SPEC, an instantiation of TMPL. */
1267 static void
1268 register_local_specialization (tree spec, tree tmpl)
1270 void **slot;
1272 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1273 htab_hash_pointer (tmpl), INSERT);
1274 *slot = build_tree_list (spec, tmpl);
1277 /* Print the list of candidate FNS in an error message. */
1279 void
1280 print_candidates (tree fns)
1282 tree fn;
1284 const char *str = "candidates are:";
1286 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1288 tree f;
1290 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1291 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1292 str = " ";
1296 /* Returns the template (one of the functions given by TEMPLATE_ID)
1297 which can be specialized to match the indicated DECL with the
1298 explicit template args given in TEMPLATE_ID. The DECL may be
1299 NULL_TREE if none is available. In that case, the functions in
1300 TEMPLATE_ID are non-members.
1302 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1303 specialization of a member template.
1305 The TEMPLATE_COUNT is the number of references to qualifying
1306 template classes that appeared in the name of the function. See
1307 check_explicit_specialization for a more accurate description.
1309 The template args (those explicitly specified and those deduced)
1310 are output in a newly created vector *TARGS_OUT.
1312 If it is impossible to determine the result, an error message is
1313 issued. The error_mark_node is returned to indicate failure. */
1315 static tree
1316 determine_specialization (tree template_id,
1317 tree decl,
1318 tree* targs_out,
1319 int need_member_template,
1320 int template_count)
1322 tree fns;
1323 tree targs;
1324 tree explicit_targs;
1325 tree candidates = NULL_TREE;
1326 tree templates = NULL_TREE;
1327 int header_count;
1328 struct cp_binding_level *b;
1330 *targs_out = NULL_TREE;
1332 if (template_id == error_mark_node)
1333 return error_mark_node;
1335 fns = TREE_OPERAND (template_id, 0);
1336 explicit_targs = TREE_OPERAND (template_id, 1);
1338 if (fns == error_mark_node)
1339 return error_mark_node;
1341 /* Check for baselinks. */
1342 if (BASELINK_P (fns))
1343 fns = BASELINK_FUNCTIONS (fns);
1345 if (!is_overloaded_fn (fns))
1347 error ("%qD is not a function template", fns);
1348 return error_mark_node;
1351 /* Count the number of template headers specified for this
1352 specialization. */
1353 header_count = 0;
1354 for (b = current_binding_level;
1355 b->kind == sk_template_parms;
1356 b = b->level_chain)
1357 ++header_count;
1359 for (; fns; fns = OVL_NEXT (fns))
1361 tree fn = OVL_CURRENT (fns);
1363 if (TREE_CODE (fn) == TEMPLATE_DECL)
1365 tree decl_arg_types;
1366 tree fn_arg_types;
1368 /* DECL might be a specialization of FN. */
1370 /* Adjust the type of DECL in case FN is a static member. */
1371 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1372 if (DECL_STATIC_FUNCTION_P (fn)
1373 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1374 decl_arg_types = TREE_CHAIN (decl_arg_types);
1376 /* Check that the number of function parameters matches.
1377 For example,
1378 template <class T> void f(int i = 0);
1379 template <> void f<int>();
1380 The specialization f<int> is invalid but is not caught
1381 by get_bindings below. */
1383 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1384 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1385 continue;
1387 /* For a non-static member function, we need to make sure that
1388 the const qualification is the same. This can be done by
1389 checking the 'this' in the argument list. */
1390 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1391 && !same_type_p (TREE_VALUE (fn_arg_types),
1392 TREE_VALUE (decl_arg_types)))
1393 continue;
1395 /* In case of explicit specialization, we need to check if
1396 the number of template headers appearing in the specialization
1397 is correct. This is usually done in check_explicit_specialization,
1398 but the check done there cannot be exhaustive when specializing
1399 member functions. Consider the following code:
1401 template <> void A<int>::f(int);
1402 template <> template <> void A<int>::f(int);
1404 Assuming that A<int> is not itself an explicit specialization
1405 already, the first line specializes "f" which is a non-template
1406 member function, whilst the second line specializes "f" which
1407 is a template member function. So both lines are syntactically
1408 correct, and check_explicit_specialization does not reject
1409 them.
1411 Here, we can do better, as we are matching the specialization
1412 against the declarations. We count the number of template
1413 headers, and we check if they match TEMPLATE_COUNT + 1
1414 (TEMPLATE_COUNT is the number of qualifying template classes,
1415 plus there must be another header for the member template
1416 itself).
1418 Notice that if header_count is zero, this is not a
1419 specialization but rather a template instantiation, so there
1420 is no check we can perform here. */
1421 if (header_count && header_count != template_count + 1)
1422 continue;
1424 /* Check that the number of template arguments at the
1425 innermost level for DECL is the same as for FN. */
1426 if (current_binding_level->kind == sk_template_parms
1427 && !current_binding_level->explicit_spec_p
1428 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1429 != TREE_VEC_LENGTH (TREE_VALUE (current_template_parms))))
1430 continue;
1432 /* See whether this function might be a specialization of this
1433 template. */
1434 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1436 if (!targs)
1437 /* We cannot deduce template arguments that when used to
1438 specialize TMPL will produce DECL. */
1439 continue;
1441 /* Save this template, and the arguments deduced. */
1442 templates = tree_cons (targs, fn, templates);
1444 else if (need_member_template)
1445 /* FN is an ordinary member function, and we need a
1446 specialization of a member template. */
1448 else if (TREE_CODE (fn) != FUNCTION_DECL)
1449 /* We can get IDENTIFIER_NODEs here in certain erroneous
1450 cases. */
1452 else if (!DECL_FUNCTION_MEMBER_P (fn))
1453 /* This is just an ordinary non-member function. Nothing can
1454 be a specialization of that. */
1456 else if (DECL_ARTIFICIAL (fn))
1457 /* Cannot specialize functions that are created implicitly. */
1459 else
1461 tree decl_arg_types;
1463 /* This is an ordinary member function. However, since
1464 we're here, we can assume it's enclosing class is a
1465 template class. For example,
1467 template <typename T> struct S { void f(); };
1468 template <> void S<int>::f() {}
1470 Here, S<int>::f is a non-template, but S<int> is a
1471 template class. If FN has the same type as DECL, we
1472 might be in business. */
1474 if (!DECL_TEMPLATE_INFO (fn))
1475 /* Its enclosing class is an explicit specialization
1476 of a template class. This is not a candidate. */
1477 continue;
1479 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1480 TREE_TYPE (TREE_TYPE (fn))))
1481 /* The return types differ. */
1482 continue;
1484 /* Adjust the type of DECL in case FN is a static member. */
1485 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1486 if (DECL_STATIC_FUNCTION_P (fn)
1487 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1488 decl_arg_types = TREE_CHAIN (decl_arg_types);
1490 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1491 decl_arg_types))
1492 /* They match! */
1493 candidates = tree_cons (NULL_TREE, fn, candidates);
1497 if (templates && TREE_CHAIN (templates))
1499 /* We have:
1501 [temp.expl.spec]
1503 It is possible for a specialization with a given function
1504 signature to be instantiated from more than one function
1505 template. In such cases, explicit specification of the
1506 template arguments must be used to uniquely identify the
1507 function template specialization being specialized.
1509 Note that here, there's no suggestion that we're supposed to
1510 determine which of the candidate templates is most
1511 specialized. However, we, also have:
1513 [temp.func.order]
1515 Partial ordering of overloaded function template
1516 declarations is used in the following contexts to select
1517 the function template to which a function template
1518 specialization refers:
1520 -- when an explicit specialization refers to a function
1521 template.
1523 So, we do use the partial ordering rules, at least for now.
1524 This extension can only serve to make invalid programs valid,
1525 so it's safe. And, there is strong anecdotal evidence that
1526 the committee intended the partial ordering rules to apply;
1527 the EDG front-end has that behavior, and John Spicer claims
1528 that the committee simply forgot to delete the wording in
1529 [temp.expl.spec]. */
1530 tree tmpl = most_specialized (templates, decl, explicit_targs);
1531 if (tmpl && tmpl != error_mark_node)
1533 targs = get_bindings (tmpl, decl, explicit_targs, /*check_ret=*/true);
1534 templates = tree_cons (targs, tmpl, NULL_TREE);
1538 if (templates == NULL_TREE && candidates == NULL_TREE)
1540 cp_error_at ("template-id %qD for %q+D does not match any template "
1541 "declaration",
1542 template_id, decl);
1543 return error_mark_node;
1545 else if ((templates && TREE_CHAIN (templates))
1546 || (candidates && TREE_CHAIN (candidates))
1547 || (templates && candidates))
1549 cp_error_at ("ambiguous template specialization %qD for %q+D",
1550 template_id, decl);
1551 chainon (candidates, templates);
1552 print_candidates (candidates);
1553 return error_mark_node;
1556 /* We have one, and exactly one, match. */
1557 if (candidates)
1559 /* It was a specialization of an ordinary member function in a
1560 template class. */
1561 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1562 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1565 /* It was a specialization of a template. */
1566 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1567 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1569 *targs_out = copy_node (targs);
1570 SET_TMPL_ARGS_LEVEL (*targs_out,
1571 TMPL_ARGS_DEPTH (*targs_out),
1572 TREE_PURPOSE (templates));
1574 else
1575 *targs_out = TREE_PURPOSE (templates);
1576 return TREE_VALUE (templates);
1579 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1580 but with the default argument values filled in from those in the
1581 TMPL_TYPES. */
1583 static tree
1584 copy_default_args_to_explicit_spec_1 (tree spec_types,
1585 tree tmpl_types)
1587 tree new_spec_types;
1589 if (!spec_types)
1590 return NULL_TREE;
1592 if (spec_types == void_list_node)
1593 return void_list_node;
1595 /* Substitute into the rest of the list. */
1596 new_spec_types =
1597 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1598 TREE_CHAIN (tmpl_types));
1600 /* Add the default argument for this parameter. */
1601 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1602 TREE_VALUE (spec_types),
1603 new_spec_types);
1606 /* DECL is an explicit specialization. Replicate default arguments
1607 from the template it specializes. (That way, code like:
1609 template <class T> void f(T = 3);
1610 template <> void f(double);
1611 void g () { f (); }
1613 works, as required.) An alternative approach would be to look up
1614 the correct default arguments at the call-site, but this approach
1615 is consistent with how implicit instantiations are handled. */
1617 static void
1618 copy_default_args_to_explicit_spec (tree decl)
1620 tree tmpl;
1621 tree spec_types;
1622 tree tmpl_types;
1623 tree new_spec_types;
1624 tree old_type;
1625 tree new_type;
1626 tree t;
1627 tree object_type = NULL_TREE;
1628 tree in_charge = NULL_TREE;
1629 tree vtt = NULL_TREE;
1631 /* See if there's anything we need to do. */
1632 tmpl = DECL_TI_TEMPLATE (decl);
1633 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1634 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1635 if (TREE_PURPOSE (t))
1636 break;
1637 if (!t)
1638 return;
1640 old_type = TREE_TYPE (decl);
1641 spec_types = TYPE_ARG_TYPES (old_type);
1643 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1645 /* Remove the this pointer, but remember the object's type for
1646 CV quals. */
1647 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1648 spec_types = TREE_CHAIN (spec_types);
1649 tmpl_types = TREE_CHAIN (tmpl_types);
1651 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1653 /* DECL may contain more parameters than TMPL due to the extra
1654 in-charge parameter in constructors and destructors. */
1655 in_charge = spec_types;
1656 spec_types = TREE_CHAIN (spec_types);
1658 if (DECL_HAS_VTT_PARM_P (decl))
1660 vtt = spec_types;
1661 spec_types = TREE_CHAIN (spec_types);
1665 /* Compute the merged default arguments. */
1666 new_spec_types =
1667 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1669 /* Compute the new FUNCTION_TYPE. */
1670 if (object_type)
1672 if (vtt)
1673 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1674 TREE_VALUE (vtt),
1675 new_spec_types);
1677 if (in_charge)
1678 /* Put the in-charge parameter back. */
1679 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1680 TREE_VALUE (in_charge),
1681 new_spec_types);
1683 new_type = build_method_type_directly (object_type,
1684 TREE_TYPE (old_type),
1685 new_spec_types);
1687 else
1688 new_type = build_function_type (TREE_TYPE (old_type),
1689 new_spec_types);
1690 new_type = cp_build_type_attribute_variant (new_type,
1691 TYPE_ATTRIBUTES (old_type));
1692 new_type = build_exception_variant (new_type,
1693 TYPE_RAISES_EXCEPTIONS (old_type));
1694 TREE_TYPE (decl) = new_type;
1697 /* Check to see if the function just declared, as indicated in
1698 DECLARATOR, and in DECL, is a specialization of a function
1699 template. We may also discover that the declaration is an explicit
1700 instantiation at this point.
1702 Returns DECL, or an equivalent declaration that should be used
1703 instead if all goes well. Issues an error message if something is
1704 amiss. Returns error_mark_node if the error is not easily
1705 recoverable.
1707 FLAGS is a bitmask consisting of the following flags:
1709 2: The function has a definition.
1710 4: The function is a friend.
1712 The TEMPLATE_COUNT is the number of references to qualifying
1713 template classes that appeared in the name of the function. For
1714 example, in
1716 template <class T> struct S { void f(); };
1717 void S<int>::f();
1719 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1720 classes are not counted in the TEMPLATE_COUNT, so that in
1722 template <class T> struct S {};
1723 template <> struct S<int> { void f(); }
1724 template <> void S<int>::f();
1726 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1727 invalid; there should be no template <>.)
1729 If the function is a specialization, it is marked as such via
1730 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1731 is set up correctly, and it is added to the list of specializations
1732 for that template. */
1734 tree
1735 check_explicit_specialization (tree declarator,
1736 tree decl,
1737 int template_count,
1738 int flags)
1740 int have_def = flags & 2;
1741 int is_friend = flags & 4;
1742 int specialization = 0;
1743 int explicit_instantiation = 0;
1744 int member_specialization = 0;
1745 tree ctype = DECL_CLASS_CONTEXT (decl);
1746 tree dname = DECL_NAME (decl);
1747 tmpl_spec_kind tsk;
1749 if (is_friend)
1751 if (!processing_specialization)
1752 tsk = tsk_none;
1753 else
1754 tsk = tsk_excessive_parms;
1756 else
1757 tsk = current_tmpl_spec_kind (template_count);
1759 switch (tsk)
1761 case tsk_none:
1762 if (processing_specialization)
1764 specialization = 1;
1765 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1767 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1769 if (is_friend)
1770 /* This could be something like:
1772 template <class T> void f(T);
1773 class S { friend void f<>(int); } */
1774 specialization = 1;
1775 else
1777 /* This case handles bogus declarations like template <>
1778 template <class T> void f<int>(); */
1780 error ("template-id %qD in declaration of primary template",
1781 declarator);
1782 return decl;
1785 break;
1787 case tsk_invalid_member_spec:
1788 /* The error has already been reported in
1789 check_specialization_scope. */
1790 return error_mark_node;
1792 case tsk_invalid_expl_inst:
1793 error ("template parameter list used in explicit instantiation");
1795 /* Fall through. */
1797 case tsk_expl_inst:
1798 if (have_def)
1799 error ("definition provided for explicit instantiation");
1801 explicit_instantiation = 1;
1802 break;
1804 case tsk_excessive_parms:
1805 case tsk_insufficient_parms:
1806 if (tsk == tsk_excessive_parms)
1807 error ("too many template parameter lists in declaration of %qD",
1808 decl);
1809 else if (template_header_count)
1810 error("too few template parameter lists in declaration of %qD", decl);
1811 else
1812 error("explicit specialization of %qD must be introduced by "
1813 "%<template <>%>", decl);
1815 /* Fall through. */
1816 case tsk_expl_spec:
1817 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1818 if (ctype)
1819 member_specialization = 1;
1820 else
1821 specialization = 1;
1822 break;
1824 case tsk_template:
1825 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1827 /* This case handles bogus declarations like template <>
1828 template <class T> void f<int>(); */
1830 if (uses_template_parms (declarator))
1831 error ("function template partial specialization %qD "
1832 "is not allowed", declarator);
1833 else
1834 error ("template-id %qD in declaration of primary template",
1835 declarator);
1836 return decl;
1839 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1840 /* This is a specialization of a member template, without
1841 specialization the containing class. Something like:
1843 template <class T> struct S {
1844 template <class U> void f (U);
1846 template <> template <class U> void S<int>::f(U) {}
1848 That's a specialization -- but of the entire template. */
1849 specialization = 1;
1850 break;
1852 default:
1853 gcc_unreachable ();
1856 if (specialization || member_specialization)
1858 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1859 for (; t; t = TREE_CHAIN (t))
1860 if (TREE_PURPOSE (t))
1862 pedwarn
1863 ("default argument specified in explicit specialization");
1864 break;
1866 if (current_lang_name == lang_name_c)
1867 error ("template specialization with C linkage");
1870 if (specialization || member_specialization || explicit_instantiation)
1872 tree tmpl = NULL_TREE;
1873 tree targs = NULL_TREE;
1875 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1876 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1878 tree fns;
1880 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1881 if (ctype)
1882 fns = dname;
1883 else
1885 /* If there is no class context, the explicit instantiation
1886 must be at namespace scope. */
1887 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1889 /* Find the namespace binding, using the declaration
1890 context. */
1891 fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
1892 if (!fns || !is_overloaded_fn (fns))
1894 error ("%qD is not a template function", dname);
1895 fns = error_mark_node;
1899 declarator = lookup_template_function (fns, NULL_TREE);
1902 if (declarator == error_mark_node)
1903 return error_mark_node;
1905 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1907 if (!explicit_instantiation)
1908 /* A specialization in class scope. This is invalid,
1909 but the error will already have been flagged by
1910 check_specialization_scope. */
1911 return error_mark_node;
1912 else
1914 /* It's not valid to write an explicit instantiation in
1915 class scope, e.g.:
1917 class C { template void f(); }
1919 This case is caught by the parser. However, on
1920 something like:
1922 template class C { void f(); };
1924 (which is invalid) we can get here. The error will be
1925 issued later. */
1929 return decl;
1931 else if (ctype != NULL_TREE
1932 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1933 IDENTIFIER_NODE))
1935 /* Find the list of functions in ctype that have the same
1936 name as the declared function. */
1937 tree name = TREE_OPERAND (declarator, 0);
1938 tree fns = NULL_TREE;
1939 int idx;
1941 if (constructor_name_p (name, ctype))
1943 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1945 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1946 : !CLASSTYPE_DESTRUCTORS (ctype))
1948 /* From [temp.expl.spec]:
1950 If such an explicit specialization for the member
1951 of a class template names an implicitly-declared
1952 special member function (clause _special_), the
1953 program is ill-formed.
1955 Similar language is found in [temp.explicit]. */
1956 error ("specialization of implicitly-declared special member function");
1957 return error_mark_node;
1960 name = is_constructor ? ctor_identifier : dtor_identifier;
1963 if (!DECL_CONV_FN_P (decl))
1965 idx = lookup_fnfields_1 (ctype, name);
1966 if (idx >= 0)
1967 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
1969 else
1971 VEC(tree) *methods;
1972 tree ovl;
1974 /* For a type-conversion operator, we cannot do a
1975 name-based lookup. We might be looking for `operator
1976 int' which will be a specialization of `operator T'.
1977 So, we find *all* the conversion operators, and then
1978 select from them. */
1979 fns = NULL_TREE;
1981 methods = CLASSTYPE_METHOD_VEC (ctype);
1982 if (methods)
1983 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1984 VEC_iterate (tree, methods, idx, ovl);
1985 ++idx)
1987 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1988 /* There are no more conversion functions. */
1989 break;
1991 /* Glue all these conversion functions together
1992 with those we already have. */
1993 for (; ovl; ovl = OVL_NEXT (ovl))
1994 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1998 if (fns == NULL_TREE)
2000 error ("no member function %qD declared in %qT", name, ctype);
2001 return error_mark_node;
2003 else
2004 TREE_OPERAND (declarator, 0) = fns;
2007 /* Figure out what exactly is being specialized at this point.
2008 Note that for an explicit instantiation, even one for a
2009 member function, we cannot tell apriori whether the
2010 instantiation is for a member template, or just a member
2011 function of a template class. Even if a member template is
2012 being instantiated, the member template arguments may be
2013 elided if they can be deduced from the rest of the
2014 declaration. */
2015 tmpl = determine_specialization (declarator, decl,
2016 &targs,
2017 member_specialization,
2018 template_count);
2020 if (!tmpl || tmpl == error_mark_node)
2021 /* We couldn't figure out what this declaration was
2022 specializing. */
2023 return error_mark_node;
2024 else
2026 tree gen_tmpl = most_general_template (tmpl);
2028 if (explicit_instantiation)
2030 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2031 is done by do_decl_instantiation later. */
2033 int arg_depth = TMPL_ARGS_DEPTH (targs);
2034 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2036 if (arg_depth > parm_depth)
2038 /* If TMPL is not the most general template (for
2039 example, if TMPL is a friend template that is
2040 injected into namespace scope), then there will
2041 be too many levels of TARGS. Remove some of them
2042 here. */
2043 int i;
2044 tree new_targs;
2046 new_targs = make_tree_vec (parm_depth);
2047 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2048 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2049 = TREE_VEC_ELT (targs, i);
2050 targs = new_targs;
2053 return instantiate_template (tmpl, targs, tf_error);
2056 /* If we thought that the DECL was a member function, but it
2057 turns out to be specializing a static member function,
2058 make DECL a static member function as well. */
2059 if (DECL_STATIC_FUNCTION_P (tmpl)
2060 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2061 revert_static_member_fn (decl);
2063 /* If this is a specialization of a member template of a
2064 template class. In we want to return the TEMPLATE_DECL,
2065 not the specialization of it. */
2066 if (tsk == tsk_template)
2068 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2069 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2070 if (have_def)
2072 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2073 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2074 = DECL_SOURCE_LOCATION (decl);
2075 /* We want to use the argument list specified in the
2076 definition, not in the original declaration. */
2077 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2078 = DECL_ARGUMENTS (decl);
2080 return tmpl;
2083 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2084 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2086 /* Inherit default function arguments from the template
2087 DECL is specializing. */
2088 copy_default_args_to_explicit_spec (decl);
2090 /* This specialization has the same protection as the
2091 template it specializes. */
2092 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2093 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2095 if (is_friend && !have_def)
2096 /* This is not really a declaration of a specialization.
2097 It's just the name of an instantiation. But, it's not
2098 a request for an instantiation, either. */
2099 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2100 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2101 /* This is indeed a specialization. In case of constructors
2102 and destructors, we need in-charge and not-in-charge
2103 versions in V3 ABI. */
2104 clone_function_decl (decl, /*update_method_vec_p=*/0);
2106 /* Register this specialization so that we can find it
2107 again. */
2108 decl = register_specialization (decl, gen_tmpl, targs);
2112 return decl;
2115 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2116 parameters. These are represented in the same format used for
2117 DECL_TEMPLATE_PARMS. */
2120 comp_template_parms (tree parms1, tree parms2)
2122 tree p1;
2123 tree p2;
2125 if (parms1 == parms2)
2126 return 1;
2128 for (p1 = parms1, p2 = parms2;
2129 p1 != NULL_TREE && p2 != NULL_TREE;
2130 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2132 tree t1 = TREE_VALUE (p1);
2133 tree t2 = TREE_VALUE (p2);
2134 int i;
2136 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2137 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2139 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2140 return 0;
2142 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2144 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2145 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2147 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2148 return 0;
2150 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2151 continue;
2152 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2153 return 0;
2157 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2158 /* One set of parameters has more parameters lists than the
2159 other. */
2160 return 0;
2162 return 1;
2165 /* Complain if DECL shadows a template parameter.
2167 [temp.local]: A template-parameter shall not be redeclared within its
2168 scope (including nested scopes). */
2170 void
2171 check_template_shadow (tree decl)
2173 tree olddecl;
2175 /* If we're not in a template, we can't possibly shadow a template
2176 parameter. */
2177 if (!current_template_parms)
2178 return;
2180 /* Figure out what we're shadowing. */
2181 if (TREE_CODE (decl) == OVERLOAD)
2182 decl = OVL_CURRENT (decl);
2183 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2185 /* If there's no previous binding for this name, we're not shadowing
2186 anything, let alone a template parameter. */
2187 if (!olddecl)
2188 return;
2190 /* If we're not shadowing a template parameter, we're done. Note
2191 that OLDDECL might be an OVERLOAD (or perhaps even an
2192 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2193 node. */
2194 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2195 return;
2197 /* We check for decl != olddecl to avoid bogus errors for using a
2198 name inside a class. We check TPFI to avoid duplicate errors for
2199 inline member templates. */
2200 if (decl == olddecl
2201 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2202 return;
2204 cp_error_at ("declaration of %q#D", decl);
2205 cp_error_at (" shadows template parm %q#D", olddecl);
2208 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2209 ORIG_LEVEL, DECL, and TYPE. */
2211 static tree
2212 build_template_parm_index (int index,
2213 int level,
2214 int orig_level,
2215 tree decl,
2216 tree type)
2218 tree t = make_node (TEMPLATE_PARM_INDEX);
2219 TEMPLATE_PARM_IDX (t) = index;
2220 TEMPLATE_PARM_LEVEL (t) = level;
2221 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2222 TEMPLATE_PARM_DECL (t) = decl;
2223 TREE_TYPE (t) = type;
2224 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2225 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2226 TREE_READONLY (t) = TREE_READONLY (decl);
2228 return t;
2231 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2232 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2233 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2234 new one is created. */
2236 static tree
2237 reduce_template_parm_level (tree index, tree type, int levels)
2239 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2240 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2241 != TEMPLATE_PARM_LEVEL (index) - levels))
2243 tree orig_decl = TEMPLATE_PARM_DECL (index);
2244 tree decl, t;
2246 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2247 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2248 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2249 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2250 DECL_ARTIFICIAL (decl) = 1;
2251 SET_DECL_TEMPLATE_PARM_P (decl);
2253 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2254 TEMPLATE_PARM_LEVEL (index) - levels,
2255 TEMPLATE_PARM_ORIG_LEVEL (index),
2256 decl, type);
2257 TEMPLATE_PARM_DESCENDANTS (index) = t;
2259 /* Template template parameters need this. */
2260 DECL_TEMPLATE_PARMS (decl)
2261 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2264 return TEMPLATE_PARM_DESCENDANTS (index);
2267 /* Process information from new template parameter NEXT and append it to the
2268 LIST being built. This new parameter is a non-type parameter iff
2269 IS_NON_TYPE is true. */
2271 tree
2272 process_template_parm (tree list, tree next, bool is_non_type)
2274 tree parm;
2275 tree decl = 0;
2276 tree defval;
2277 int idx;
2279 parm = next;
2280 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2281 defval = TREE_PURPOSE (parm);
2283 if (list)
2285 tree p = TREE_VALUE (tree_last (list));
2287 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2288 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2289 else
2290 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2291 ++idx;
2293 else
2294 idx = 0;
2296 if (is_non_type)
2298 parm = TREE_VALUE (parm);
2300 SET_DECL_TEMPLATE_PARM_P (parm);
2302 /* [temp.param]
2304 The top-level cv-qualifiers on the template-parameter are
2305 ignored when determining its type. */
2306 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2308 /* A template parameter is not modifiable. */
2309 TREE_CONSTANT (parm) = 1;
2310 TREE_INVARIANT (parm) = 1;
2311 TREE_READONLY (parm) = 1;
2312 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2313 TREE_TYPE (parm) = void_type_node;
2314 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2315 TREE_CONSTANT (decl) = 1;
2316 TREE_INVARIANT (decl) = 1;
2317 TREE_READONLY (decl) = 1;
2318 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2319 = build_template_parm_index (idx, processing_template_decl,
2320 processing_template_decl,
2321 decl, TREE_TYPE (parm));
2323 else
2325 tree t;
2326 parm = TREE_VALUE (TREE_VALUE (parm));
2328 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2330 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2331 /* This is for distinguishing between real templates and template
2332 template parameters */
2333 TREE_TYPE (parm) = t;
2334 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2335 decl = parm;
2337 else
2339 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2340 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2341 decl = build_decl (TYPE_DECL, parm, t);
2344 TYPE_NAME (t) = decl;
2345 TYPE_STUB_DECL (t) = decl;
2346 parm = decl;
2347 TEMPLATE_TYPE_PARM_INDEX (t)
2348 = build_template_parm_index (idx, processing_template_decl,
2349 processing_template_decl,
2350 decl, TREE_TYPE (parm));
2352 DECL_ARTIFICIAL (decl) = 1;
2353 SET_DECL_TEMPLATE_PARM_P (decl);
2354 pushdecl (decl);
2355 parm = build_tree_list (defval, parm);
2356 return chainon (list, parm);
2359 /* The end of a template parameter list has been reached. Process the
2360 tree list into a parameter vector, converting each parameter into a more
2361 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2362 as PARM_DECLs. */
2364 tree
2365 end_template_parm_list (tree parms)
2367 int nparms;
2368 tree parm, next;
2369 tree saved_parmlist = make_tree_vec (list_length (parms));
2371 current_template_parms
2372 = tree_cons (size_int (processing_template_decl),
2373 saved_parmlist, current_template_parms);
2375 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2377 next = TREE_CHAIN (parm);
2378 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2379 TREE_CHAIN (parm) = NULL_TREE;
2382 --processing_template_parmlist;
2384 return saved_parmlist;
2387 /* end_template_decl is called after a template declaration is seen. */
2389 void
2390 end_template_decl (void)
2392 reset_specialization ();
2394 if (! processing_template_decl)
2395 return;
2397 /* This matches the pushlevel in begin_template_parm_list. */
2398 finish_scope ();
2400 --processing_template_decl;
2401 current_template_parms = TREE_CHAIN (current_template_parms);
2404 /* Given a template argument vector containing the template PARMS.
2405 The innermost PARMS are given first. */
2407 tree
2408 current_template_args (void)
2410 tree header;
2411 tree args = NULL_TREE;
2412 int length = TMPL_PARMS_DEPTH (current_template_parms);
2413 int l = length;
2415 /* If there is only one level of template parameters, we do not
2416 create a TREE_VEC of TREE_VECs. Instead, we return a single
2417 TREE_VEC containing the arguments. */
2418 if (length > 1)
2419 args = make_tree_vec (length);
2421 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2423 tree a = copy_node (TREE_VALUE (header));
2424 int i;
2426 TREE_TYPE (a) = NULL_TREE;
2427 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2429 tree t = TREE_VEC_ELT (a, i);
2431 /* T will be a list if we are called from within a
2432 begin/end_template_parm_list pair, but a vector directly
2433 if within a begin/end_member_template_processing pair. */
2434 if (TREE_CODE (t) == TREE_LIST)
2436 t = TREE_VALUE (t);
2438 if (TREE_CODE (t) == TYPE_DECL
2439 || TREE_CODE (t) == TEMPLATE_DECL)
2440 t = TREE_TYPE (t);
2441 else
2442 t = DECL_INITIAL (t);
2443 TREE_VEC_ELT (a, i) = t;
2447 if (length > 1)
2448 TREE_VEC_ELT (args, --l) = a;
2449 else
2450 args = a;
2453 return args;
2456 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2457 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2458 a member template. Used by push_template_decl below. */
2460 static tree
2461 build_template_decl (tree decl, tree parms, bool member_template_p)
2463 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2464 DECL_TEMPLATE_PARMS (tmpl) = parms;
2465 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2466 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2467 if (DECL_LANG_SPECIFIC (decl))
2469 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2470 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2471 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2472 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2473 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2474 if (DECL_OVERLOADED_OPERATOR_P (decl))
2475 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2476 DECL_OVERLOADED_OPERATOR_P (decl));
2479 return tmpl;
2482 struct template_parm_data
2484 /* The level of the template parameters we are currently
2485 processing. */
2486 int level;
2488 /* The index of the specialization argument we are currently
2489 processing. */
2490 int current_arg;
2492 /* An array whose size is the number of template parameters. The
2493 elements are nonzero if the parameter has been used in any one
2494 of the arguments processed so far. */
2495 int* parms;
2497 /* An array whose size is the number of template arguments. The
2498 elements are nonzero if the argument makes use of template
2499 parameters of this level. */
2500 int* arg_uses_template_parms;
2503 /* Subroutine of push_template_decl used to see if each template
2504 parameter in a partial specialization is used in the explicit
2505 argument list. If T is of the LEVEL given in DATA (which is
2506 treated as a template_parm_data*), then DATA->PARMS is marked
2507 appropriately. */
2509 static int
2510 mark_template_parm (tree t, void* data)
2512 int level;
2513 int idx;
2514 struct template_parm_data* tpd = (struct template_parm_data*) data;
2516 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2518 level = TEMPLATE_PARM_LEVEL (t);
2519 idx = TEMPLATE_PARM_IDX (t);
2521 else
2523 level = TEMPLATE_TYPE_LEVEL (t);
2524 idx = TEMPLATE_TYPE_IDX (t);
2527 if (level == tpd->level)
2529 tpd->parms[idx] = 1;
2530 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2533 /* Return zero so that for_each_template_parm will continue the
2534 traversal of the tree; we want to mark *every* template parm. */
2535 return 0;
2538 /* Process the partial specialization DECL. */
2540 static tree
2541 process_partial_specialization (tree decl)
2543 tree type = TREE_TYPE (decl);
2544 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2545 tree specargs = CLASSTYPE_TI_ARGS (type);
2546 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2547 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2548 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2549 int nargs = TREE_VEC_LENGTH (inner_args);
2550 int ntparms = TREE_VEC_LENGTH (inner_parms);
2551 int i;
2552 int did_error_intro = 0;
2553 struct template_parm_data tpd;
2554 struct template_parm_data tpd2;
2556 /* We check that each of the template parameters given in the
2557 partial specialization is used in the argument list to the
2558 specialization. For example:
2560 template <class T> struct S;
2561 template <class T> struct S<T*>;
2563 The second declaration is OK because `T*' uses the template
2564 parameter T, whereas
2566 template <class T> struct S<int>;
2568 is no good. Even trickier is:
2570 template <class T>
2571 struct S1
2573 template <class U>
2574 struct S2;
2575 template <class U>
2576 struct S2<T>;
2579 The S2<T> declaration is actually invalid; it is a
2580 full-specialization. Of course,
2582 template <class U>
2583 struct S2<T (*)(U)>;
2585 or some such would have been OK. */
2586 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2587 tpd.parms = alloca (sizeof (int) * ntparms);
2588 memset (tpd.parms, 0, sizeof (int) * ntparms);
2590 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2591 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2592 for (i = 0; i < nargs; ++i)
2594 tpd.current_arg = i;
2595 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2596 &mark_template_parm,
2597 &tpd,
2598 NULL);
2600 for (i = 0; i < ntparms; ++i)
2601 if (tpd.parms[i] == 0)
2603 /* One of the template parms was not used in the
2604 specialization. */
2605 if (!did_error_intro)
2607 error ("template parameters not used in partial specialization:");
2608 did_error_intro = 1;
2611 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2614 /* [temp.class.spec]
2616 The argument list of the specialization shall not be identical to
2617 the implicit argument list of the primary template. */
2618 if (comp_template_args
2619 (inner_args,
2620 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2621 (maintmpl)))))
2622 error ("partial specialization %qT does not specialize any template arguments", type);
2624 /* [temp.class.spec]
2626 A partially specialized non-type argument expression shall not
2627 involve template parameters of the partial specialization except
2628 when the argument expression is a simple identifier.
2630 The type of a template parameter corresponding to a specialized
2631 non-type argument shall not be dependent on a parameter of the
2632 specialization. */
2633 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2634 tpd2.parms = 0;
2635 for (i = 0; i < nargs; ++i)
2637 tree arg = TREE_VEC_ELT (inner_args, i);
2638 if (/* These first two lines are the `non-type' bit. */
2639 !TYPE_P (arg)
2640 && TREE_CODE (arg) != TEMPLATE_DECL
2641 /* This next line is the `argument expression is not just a
2642 simple identifier' condition and also the `specialized
2643 non-type argument' bit. */
2644 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2646 if (tpd.arg_uses_template_parms[i])
2647 error ("template argument %qE involves template parameter(s)", arg);
2648 else
2650 /* Look at the corresponding template parameter,
2651 marking which template parameters its type depends
2652 upon. */
2653 tree type =
2654 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2655 i)));
2657 if (!tpd2.parms)
2659 /* We haven't yet initialized TPD2. Do so now. */
2660 tpd2.arg_uses_template_parms
2661 = alloca (sizeof (int) * nargs);
2662 /* The number of parameters here is the number in the
2663 main template, which, as checked in the assertion
2664 above, is NARGS. */
2665 tpd2.parms = alloca (sizeof (int) * nargs);
2666 tpd2.level =
2667 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2670 /* Mark the template parameters. But this time, we're
2671 looking for the template parameters of the main
2672 template, not in the specialization. */
2673 tpd2.current_arg = i;
2674 tpd2.arg_uses_template_parms[i] = 0;
2675 memset (tpd2.parms, 0, sizeof (int) * nargs);
2676 for_each_template_parm (type,
2677 &mark_template_parm,
2678 &tpd2,
2679 NULL);
2681 if (tpd2.arg_uses_template_parms [i])
2683 /* The type depended on some template parameters.
2684 If they are fully specialized in the
2685 specialization, that's OK. */
2686 int j;
2687 for (j = 0; j < nargs; ++j)
2688 if (tpd2.parms[j] != 0
2689 && tpd.arg_uses_template_parms [j])
2691 error ("type %qT of template argument %qE depends "
2692 "on template parameter(s)",
2693 type,
2694 arg);
2695 break;
2702 if (retrieve_specialization (maintmpl, specargs,
2703 /*class_specializations_p=*/true))
2704 /* We've already got this specialization. */
2705 return decl;
2707 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2708 = tree_cons (inner_args, inner_parms,
2709 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2710 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2711 return decl;
2714 /* Check that a template declaration's use of default arguments is not
2715 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2716 nonzero if DECL is the thing declared by a primary template.
2717 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2719 static void
2720 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2722 const char *msg;
2723 int last_level_to_check;
2724 tree parm_level;
2726 /* [temp.param]
2728 A default template-argument shall not be specified in a
2729 function template declaration or a function template definition, nor
2730 in the template-parameter-list of the definition of a member of a
2731 class template. */
2733 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2734 /* You can't have a function template declaration in a local
2735 scope, nor you can you define a member of a class template in a
2736 local scope. */
2737 return;
2739 if (current_class_type
2740 && !TYPE_BEING_DEFINED (current_class_type)
2741 && DECL_LANG_SPECIFIC (decl)
2742 /* If this is either a friend defined in the scope of the class
2743 or a member function. */
2744 && (DECL_FUNCTION_MEMBER_P (decl)
2745 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2746 : DECL_FRIEND_CONTEXT (decl)
2747 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2748 : false)
2749 /* And, if it was a member function, it really was defined in
2750 the scope of the class. */
2751 && (!DECL_FUNCTION_MEMBER_P (decl)
2752 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2753 /* We already checked these parameters when the template was
2754 declared, so there's no need to do it again now. This function
2755 was defined in class scope, but we're processing it's body now
2756 that the class is complete. */
2757 return;
2759 /* [temp.param]
2761 If a template-parameter has a default template-argument, all
2762 subsequent template-parameters shall have a default
2763 template-argument supplied. */
2764 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2766 tree inner_parms = TREE_VALUE (parm_level);
2767 int ntparms = TREE_VEC_LENGTH (inner_parms);
2768 int seen_def_arg_p = 0;
2769 int i;
2771 for (i = 0; i < ntparms; ++i)
2773 tree parm = TREE_VEC_ELT (inner_parms, i);
2774 if (TREE_PURPOSE (parm))
2775 seen_def_arg_p = 1;
2776 else if (seen_def_arg_p)
2778 error ("no default argument for %qD", TREE_VALUE (parm));
2779 /* For better subsequent error-recovery, we indicate that
2780 there should have been a default argument. */
2781 TREE_PURPOSE (parm) = error_mark_node;
2786 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2787 /* For an ordinary class template, default template arguments are
2788 allowed at the innermost level, e.g.:
2789 template <class T = int>
2790 struct S {};
2791 but, in a partial specialization, they're not allowed even
2792 there, as we have in [temp.class.spec]:
2794 The template parameter list of a specialization shall not
2795 contain default template argument values.
2797 So, for a partial specialization, or for a function template,
2798 we look at all of them. */
2800 else
2801 /* But, for a primary class template that is not a partial
2802 specialization we look at all template parameters except the
2803 innermost ones. */
2804 parms = TREE_CHAIN (parms);
2806 /* Figure out what error message to issue. */
2807 if (TREE_CODE (decl) == FUNCTION_DECL)
2808 msg = "default template arguments may not be used in function templates";
2809 else if (is_partial)
2810 msg = "default template arguments may not be used in partial specializations";
2811 else
2812 msg = "default argument for template parameter for class enclosing %qD";
2814 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2815 /* If we're inside a class definition, there's no need to
2816 examine the parameters to the class itself. On the one
2817 hand, they will be checked when the class is defined, and,
2818 on the other, default arguments are valid in things like:
2819 template <class T = double>
2820 struct S { template <class U> void f(U); };
2821 Here the default argument for `S' has no bearing on the
2822 declaration of `f'. */
2823 last_level_to_check = template_class_depth (current_class_type) + 1;
2824 else
2825 /* Check everything. */
2826 last_level_to_check = 0;
2828 for (parm_level = parms;
2829 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2830 parm_level = TREE_CHAIN (parm_level))
2832 tree inner_parms = TREE_VALUE (parm_level);
2833 int i;
2834 int ntparms;
2836 ntparms = TREE_VEC_LENGTH (inner_parms);
2837 for (i = 0; i < ntparms; ++i)
2838 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2840 if (msg)
2842 error (msg, decl);
2843 msg = 0;
2846 /* Clear out the default argument so that we are not
2847 confused later. */
2848 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2851 /* At this point, if we're still interested in issuing messages,
2852 they must apply to classes surrounding the object declared. */
2853 if (msg)
2854 msg = "default argument for template parameter for class enclosing %qD";
2858 /* Worker for push_template_decl_real, called via
2859 for_each_template_parm. DATA is really an int, indicating the
2860 level of the parameters we are interested in. If T is a template
2861 parameter of that level, return nonzero. */
2863 static int
2864 template_parm_this_level_p (tree t, void* data)
2866 int this_level = *(int *)data;
2867 int level;
2869 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2870 level = TEMPLATE_PARM_LEVEL (t);
2871 else
2872 level = TEMPLATE_TYPE_LEVEL (t);
2873 return level == this_level;
2876 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2877 parameters given by current_template_args, or reuses a
2878 previously existing one, if appropriate. Returns the DECL, or an
2879 equivalent one, if it is replaced via a call to duplicate_decls.
2881 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2883 tree
2884 push_template_decl_real (tree decl, int is_friend)
2886 tree tmpl;
2887 tree args;
2888 tree info;
2889 tree ctx;
2890 int primary;
2891 int is_partial;
2892 int new_template_p = 0;
2893 /* True if the template is a member template, in the sense of
2894 [temp.mem]. */
2895 bool member_template_p = false;
2897 if (decl == error_mark_node)
2898 return decl;
2900 /* See if this is a partial specialization. */
2901 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2902 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2903 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2905 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2907 if (is_friend)
2908 /* For a friend, we want the context of the friend function, not
2909 the type of which it is a friend. */
2910 ctx = DECL_CONTEXT (decl);
2911 else if (CP_DECL_CONTEXT (decl)
2912 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2913 /* In the case of a virtual function, we want the class in which
2914 it is defined. */
2915 ctx = CP_DECL_CONTEXT (decl);
2916 else
2917 /* Otherwise, if we're currently defining some class, the DECL
2918 is assumed to be a member of the class. */
2919 ctx = current_scope ();
2921 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2922 ctx = NULL_TREE;
2924 if (!DECL_CONTEXT (decl))
2925 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2927 /* See if this is a primary template. */
2928 primary = template_parm_scope_p ();
2930 if (primary)
2932 if (DECL_CLASS_SCOPE_P (decl))
2933 member_template_p = true;
2934 if (current_lang_name == lang_name_c)
2935 error ("template with C linkage");
2936 else if (TREE_CODE (decl) == TYPE_DECL
2937 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2938 error ("template class without a name");
2939 else if (TREE_CODE (decl) == FUNCTION_DECL)
2941 if (DECL_DESTRUCTOR_P (decl))
2943 /* [temp.mem]
2945 A destructor shall not be a member template. */
2946 error ("destructor %qD declared as member template", decl);
2947 return error_mark_node;
2949 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
2950 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
2951 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
2952 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
2953 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
2954 == void_list_node)))
2956 /* [basic.stc.dynamic.allocation]
2958 An allocation function can be a function
2959 template. ... Template allocation functions shall
2960 have two or more parameters. */
2961 error ("invalid template declaration of %qD", decl);
2962 return decl;
2965 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2966 && CLASS_TYPE_P (TREE_TYPE (decl)))
2967 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)))
2968 /* OK */;
2969 else
2971 error ("template declaration of %q#D", decl);
2972 return error_mark_node;
2976 /* Check to see that the rules regarding the use of default
2977 arguments are not being violated. */
2978 check_default_tmpl_args (decl, current_template_parms,
2979 primary, is_partial);
2981 if (is_partial)
2982 return process_partial_specialization (decl);
2984 args = current_template_args ();
2986 if (!ctx
2987 || TREE_CODE (ctx) == FUNCTION_DECL
2988 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2989 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2991 if (DECL_LANG_SPECIFIC (decl)
2992 && DECL_TEMPLATE_INFO (decl)
2993 && DECL_TI_TEMPLATE (decl))
2994 tmpl = DECL_TI_TEMPLATE (decl);
2995 /* If DECL is a TYPE_DECL for a class-template, then there won't
2996 be DECL_LANG_SPECIFIC. The information equivalent to
2997 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2998 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2999 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3000 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3002 /* Since a template declaration already existed for this
3003 class-type, we must be redeclaring it here. Make sure
3004 that the redeclaration is valid. */
3005 redeclare_class_template (TREE_TYPE (decl),
3006 current_template_parms);
3007 /* We don't need to create a new TEMPLATE_DECL; just use the
3008 one we already had. */
3009 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3011 else
3013 tmpl = build_template_decl (decl, current_template_parms,
3014 member_template_p);
3015 new_template_p = 1;
3017 if (DECL_LANG_SPECIFIC (decl)
3018 && DECL_TEMPLATE_SPECIALIZATION (decl))
3020 /* A specialization of a member template of a template
3021 class. */
3022 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3023 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3024 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3028 else
3030 tree a, t, current, parms;
3031 int i;
3033 if (TREE_CODE (decl) == TYPE_DECL)
3035 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3036 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3037 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3038 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3039 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3040 else
3042 error ("%qD does not declare a template type", decl);
3043 return decl;
3046 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3048 error ("template definition of non-template %q#D", decl);
3049 return decl;
3051 else
3052 tmpl = DECL_TI_TEMPLATE (decl);
3054 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3055 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3056 && DECL_TEMPLATE_SPECIALIZATION (decl)
3057 && DECL_MEMBER_TEMPLATE_P (tmpl))
3059 tree new_tmpl;
3061 /* The declaration is a specialization of a member
3062 template, declared outside the class. Therefore, the
3063 innermost template arguments will be NULL, so we
3064 replace them with the arguments determined by the
3065 earlier call to check_explicit_specialization. */
3066 args = DECL_TI_ARGS (decl);
3068 new_tmpl
3069 = build_template_decl (decl, current_template_parms,
3070 member_template_p);
3071 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3072 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3073 DECL_TI_TEMPLATE (decl) = new_tmpl;
3074 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3075 DECL_TEMPLATE_INFO (new_tmpl)
3076 = tree_cons (tmpl, args, NULL_TREE);
3078 register_specialization (new_tmpl,
3079 most_general_template (tmpl),
3080 args);
3081 return decl;
3084 /* Make sure the template headers we got make sense. */
3086 parms = DECL_TEMPLATE_PARMS (tmpl);
3087 i = TMPL_PARMS_DEPTH (parms);
3088 if (TMPL_ARGS_DEPTH (args) != i)
3090 error ("expected %d levels of template parms for %q#D, got %d",
3091 i, decl, TMPL_ARGS_DEPTH (args));
3093 else
3094 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3096 a = TMPL_ARGS_LEVEL (args, i);
3097 t = INNERMOST_TEMPLATE_PARMS (parms);
3099 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3101 if (current == decl)
3102 error ("got %d template parameters for %q#D",
3103 TREE_VEC_LENGTH (a), decl);
3104 else
3105 error ("got %d template parameters for %q#T",
3106 TREE_VEC_LENGTH (a), current);
3107 error (" but %d required", TREE_VEC_LENGTH (t));
3110 /* Perhaps we should also check that the parms are used in the
3111 appropriate qualifying scopes in the declarator? */
3113 if (current == decl)
3114 current = ctx;
3115 else
3116 current = TYPE_CONTEXT (current);
3120 DECL_TEMPLATE_RESULT (tmpl) = decl;
3121 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3123 /* Push template declarations for global functions and types. Note
3124 that we do not try to push a global template friend declared in a
3125 template class; such a thing may well depend on the template
3126 parameters of the class. */
3127 if (new_template_p && !ctx
3128 && !(is_friend && template_class_depth (current_class_type) > 0))
3130 tmpl = pushdecl_namespace_level (tmpl);
3131 if (tmpl == error_mark_node)
3132 return error_mark_node;
3134 /* Hide template friend classes that haven't been declared yet. */
3135 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3137 DECL_ANTICIPATED (tmpl) = 1;
3138 DECL_FRIEND_P (tmpl) = 1;
3142 if (primary)
3144 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3145 if (DECL_CONV_FN_P (tmpl))
3147 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3149 /* It is a conversion operator. See if the type converted to
3150 depends on innermost template operands. */
3152 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3153 depth))
3154 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3158 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3159 back to its most general template. If TMPL is a specialization,
3160 ARGS may only have the innermost set of arguments. Add the missing
3161 argument levels if necessary. */
3162 if (DECL_TEMPLATE_INFO (tmpl))
3163 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3165 info = tree_cons (tmpl, args, NULL_TREE);
3167 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3169 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3170 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3171 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3172 /* Don't change the name if we've already set it up. */
3173 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3174 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3176 else if (DECL_LANG_SPECIFIC (decl))
3177 DECL_TEMPLATE_INFO (decl) = info;
3179 return DECL_TEMPLATE_RESULT (tmpl);
3182 tree
3183 push_template_decl (tree decl)
3185 return push_template_decl_real (decl, 0);
3188 /* Called when a class template TYPE is redeclared with the indicated
3189 template PARMS, e.g.:
3191 template <class T> struct S;
3192 template <class T> struct S {}; */
3194 void
3195 redeclare_class_template (tree type, tree parms)
3197 tree tmpl;
3198 tree tmpl_parms;
3199 int i;
3201 if (!TYPE_TEMPLATE_INFO (type))
3203 error ("%qT is not a template type", type);
3204 return;
3207 tmpl = TYPE_TI_TEMPLATE (type);
3208 if (!PRIMARY_TEMPLATE_P (tmpl))
3209 /* The type is nested in some template class. Nothing to worry
3210 about here; there are no new template parameters for the nested
3211 type. */
3212 return;
3214 if (!parms)
3216 error ("template specifiers not specified in declaration of %qD",
3217 tmpl);
3218 return;
3221 parms = INNERMOST_TEMPLATE_PARMS (parms);
3222 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3224 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3226 cp_error_at ("previous declaration %qD", tmpl);
3227 error ("used %d template parameter%s instead of %d",
3228 TREE_VEC_LENGTH (tmpl_parms),
3229 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
3230 TREE_VEC_LENGTH (parms));
3231 return;
3234 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3236 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3237 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3238 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3239 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3241 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3242 TEMPLATE_DECL. */
3243 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3244 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3245 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
3247 cp_error_at ("template parameter %q#D", tmpl_parm);
3248 error ("redeclared here as %q#D", parm);
3249 return;
3252 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3254 /* We have in [temp.param]:
3256 A template-parameter may not be given default arguments
3257 by two different declarations in the same scope. */
3258 error ("redefinition of default argument for %q#D", parm);
3259 error ("%J original definition appeared here", tmpl_parm);
3260 return;
3263 if (parm_default != NULL_TREE)
3264 /* Update the previous template parameters (which are the ones
3265 that will really count) with the new default value. */
3266 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3267 else if (tmpl_default != NULL_TREE)
3268 /* Update the new parameters, too; they'll be used as the
3269 parameters for any members. */
3270 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3274 /* Simplify EXPR if it is a non-dependent expression. Returns the
3275 (possibly simplified) expression. */
3277 tree
3278 fold_non_dependent_expr (tree expr)
3280 /* If we're in a template, but EXPR isn't value dependent, simplify
3281 it. We're supposed to treat:
3283 template <typename T> void f(T[1 + 1]);
3284 template <typename T> void f(T[2]);
3286 as two declarations of the same function, for example. */
3287 if (processing_template_decl
3288 && !type_dependent_expression_p (expr)
3289 && !value_dependent_expression_p (expr))
3291 HOST_WIDE_INT saved_processing_template_decl;
3293 saved_processing_template_decl = processing_template_decl;
3294 processing_template_decl = 0;
3295 expr = tsubst_copy_and_build (expr,
3296 /*args=*/NULL_TREE,
3297 tf_error,
3298 /*in_decl=*/NULL_TREE,
3299 /*function_p=*/false);
3300 processing_template_decl = saved_processing_template_decl;
3302 return expr;
3305 /* EXPR is an expression which is used in a constant-expression context.
3306 For instance, it could be a VAR_DECL with a constant initializer.
3307 Extract the innest constant expression.
3309 This is basically a more powerful version of
3310 integral_constant_value, which can be used also in templates where
3311 initializers can maintain a syntactic rather than semantic form
3312 (even if they are non-dependent, for access-checking purposes). */
3314 tree
3315 fold_decl_constant_value (tree expr)
3317 tree const_expr = expr;
3320 expr = fold_non_dependent_expr (const_expr);
3321 const_expr = integral_constant_value (expr);
3323 while (expr != const_expr);
3325 return expr;
3328 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3329 must be a function or a pointer-to-function type, as specified
3330 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3331 and check that the resulting function has external linkage. */
3333 static tree
3334 convert_nontype_argument_function (tree type, tree expr)
3336 tree fns = expr;
3337 tree fn, fn_no_ptr;
3339 fn = instantiate_type (type, fns, tf_none);
3340 if (fn == error_mark_node)
3341 return error_mark_node;
3343 fn_no_ptr = fn;
3344 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3345 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3347 /* [temp.arg.nontype]/1
3349 A template-argument for a non-type, non-template template-parameter
3350 shall be one of:
3351 [...]
3352 -- the address of an object or function with external linkage. */
3353 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3355 error ("%qE is not a valid template argument for type %qT "
3356 "because function %qD has not external linkage",
3357 expr, type, fn_no_ptr);
3358 return NULL_TREE;
3361 return fn;
3364 /* Attempt to convert the non-type template parameter EXPR to the
3365 indicated TYPE. If the conversion is successful, return the
3366 converted value. If the conversion is unsuccessful, return
3367 NULL_TREE if we issued an error message, or error_mark_node if we
3368 did not. We issue error messages for out-and-out bad template
3369 parameters, but not simply because the conversion failed, since we
3370 might be just trying to do argument deduction. Both TYPE and EXPR
3371 must be non-dependent.
3373 The conversion follows the special rules described in
3374 [temp.arg.nontype], and it is much more strict than an implicit
3375 conversion.
3377 This function is called twice for each template argument (see
3378 lookup_template_class for a more accurate description of this
3379 problem). This means that we need to handle expressions which
3380 are not valid in a C++ source, but can be created from the
3381 first call (for instance, casts to perform conversions). These
3382 hacks can go away after we fix the double coercion problem. */
3384 static tree
3385 convert_nontype_argument (tree type, tree expr)
3387 tree expr_type;
3389 /* Detect immediately string literals as invalid non-type argument.
3390 This special-case is not needed for correctness (we would easily
3391 catch this later), but only to provide better diagnostic for this
3392 common user mistake. As suggested by DR 100, we do not mention
3393 linkage issues in the diagnostic as this is not the point. */
3394 if (TREE_CODE (expr) == STRING_CST)
3396 error ("%qE is not a valid template argument for type %qT "
3397 "because string literals can never be used in this context",
3398 expr, type);
3399 return NULL_TREE;
3402 /* If we are in a template, EXPR may be non-dependent, but still
3403 have a syntactic, rather than semantic, form. For example, EXPR
3404 might be a SCOPE_REF, rather than the VAR_DECL to which the
3405 SCOPE_REF refers. Preserving the qualifying scope is necessary
3406 so that access checking can be performed when the template is
3407 instantiated -- but here we need the resolved form so that we can
3408 convert the argument. */
3409 expr = fold_non_dependent_expr (expr);
3410 expr_type = TREE_TYPE (expr);
3412 /* HACK: Due to double coercion, we can get a
3413 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3414 which is the tree that we built on the first call (see
3415 below when coercing to reference to object or to reference to
3416 function). We just strip everything and get to the arg.
3417 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3418 for examples. */
3419 if (TREE_CODE (expr) == NOP_EXPR)
3421 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3423 /* ??? Maybe we could use convert_from_reference here, but we
3424 would need to relax its constraints because the NOP_EXPR
3425 could actually change the type to something more cv-qualified,
3426 and this is not folded by convert_from_reference. */
3427 tree addr = TREE_OPERAND (expr, 0);
3428 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3429 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3430 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3431 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3432 (TREE_TYPE (expr_type),
3433 TREE_TYPE (TREE_TYPE (addr))));
3435 expr = TREE_OPERAND (addr, 0);
3436 expr_type = TREE_TYPE (expr);
3439 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3440 parameter is a pointer to object, through decay and
3441 qualification conversion. Let's strip everything. */
3442 else if (TYPE_PTROBV_P (type))
3444 STRIP_NOPS (expr);
3445 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3446 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3447 /* Skip the ADDR_EXPR only if it is part of the decay for
3448 an array. Otherwise, it is part of the original argument
3449 in the source code. */
3450 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3451 expr = TREE_OPERAND (expr, 0);
3452 expr_type = TREE_TYPE (expr);
3456 /* [temp.arg.nontype]/5, bullet 1
3458 For a non-type template-parameter of integral or enumeration type,
3459 integral promotions (_conv.prom_) and integral conversions
3460 (_conv.integral_) are applied. */
3461 if (INTEGRAL_TYPE_P (type))
3463 if (!INTEGRAL_TYPE_P (expr_type))
3464 return error_mark_node;
3466 expr = fold_decl_constant_value (expr);
3467 /* Notice that there are constant expressions like '4 % 0' which
3468 do not fold into integer constants. */
3469 if (TREE_CODE (expr) != INTEGER_CST)
3471 error ("%qE is not a valid template argument for type %qT "
3472 "because it is a non-constant expression", expr, type);
3473 return NULL_TREE;
3476 /* At this point, an implicit conversion does what we want,
3477 because we already know that the expression is of integral
3478 type. */
3479 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3480 if (expr == error_mark_node)
3481 return error_mark_node;
3483 /* Conversion was allowed: fold it to a bare integer constant. */
3484 expr = fold (expr);
3486 /* [temp.arg.nontype]/5, bullet 2
3488 For a non-type template-parameter of type pointer to object,
3489 qualification conversions (_conv.qual_) and the array-to-pointer
3490 conversion (_conv.array_) are applied. */
3491 else if (TYPE_PTROBV_P (type))
3493 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3495 A template-argument for a non-type, non-template template-parameter
3496 shall be one of: [...]
3498 -- the name of a non-type template-parameter;
3499 -- the address of an object or function with external linkage, [...]
3500 expressed as "& id-expression" where the & is optional if the name
3501 refers to a function or array, or if the corresponding
3502 template-parameter is a reference.
3504 Here, we do not care about functions, as they are invalid anyway
3505 for a parameter of type pointer-to-object. */
3506 bool constant_address_p =
3507 (TREE_CODE (expr) == ADDR_EXPR
3508 || TREE_CODE (expr_type) == ARRAY_TYPE
3509 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3511 expr = decay_conversion (expr);
3512 if (expr == error_mark_node)
3513 return error_mark_node;
3515 expr = perform_qualification_conversions (type, expr);
3516 if (expr == error_mark_node)
3517 return error_mark_node;
3519 if (!constant_address_p)
3521 error ("%qE is not a valid template argument for type %qT "
3522 "because it is not a constant pointer", expr, type);
3523 return NULL_TREE;
3526 /* [temp.arg.nontype]/5, bullet 3
3528 For a non-type template-parameter of type reference to object, no
3529 conversions apply. The type referred to by the reference may be more
3530 cv-qualified than the (otherwise identical) type of the
3531 template-argument. The template-parameter is bound directly to the
3532 template-argument, which must be an lvalue. */
3533 else if (TYPE_REF_OBJ_P (type))
3535 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3536 expr_type))
3537 return error_mark_node;
3539 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3541 error ("%qE is not a valid template argument for type %qT "
3542 "because of conflicts in cv-qualification", expr, type);
3543 return NULL_TREE;
3546 if (!real_lvalue_p (expr))
3548 error ("%qE is not a valid template argument for type %qT "
3549 "because it is not a lvalue", expr, type);
3550 return NULL_TREE;
3553 /* [temp.arg.nontype]/1
3555 A template-argument for a non-type, non-template template-parameter
3556 shall be one of: [...]
3558 -- the address of an object or function with external linkage. */
3559 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3561 error ("%qE is not a valid template argument for type %qT "
3562 "because object %qD has not external linkage",
3563 expr, type, expr);
3564 return NULL_TREE;
3567 expr = build_nop (type, build_address (expr));
3569 /* [temp.arg.nontype]/5, bullet 4
3571 For a non-type template-parameter of type pointer to function, only
3572 the function-to-pointer conversion (_conv.func_) is applied. If the
3573 template-argument represents a set of overloaded functions (or a
3574 pointer to such), the matching function is selected from the set
3575 (_over.over_). */
3576 else if (TYPE_PTRFN_P (type))
3578 /* If the argument is a template-id, we might not have enough
3579 context information to decay the pointer.
3580 ??? Why static5.C requires decay and subst1.C works fine
3581 even without it? */
3582 if (!type_unknown_p (expr_type))
3584 expr = decay_conversion (expr);
3585 if (expr == error_mark_node)
3586 return error_mark_node;
3589 expr = convert_nontype_argument_function (type, expr);
3590 if (!expr || expr == error_mark_node)
3591 return expr;
3593 /* [temp.arg.nontype]/5, bullet 5
3595 For a non-type template-parameter of type reference to function, no
3596 conversions apply. If the template-argument represents a set of
3597 overloaded functions, the matching function is selected from the set
3598 (_over.over_). */
3599 else if (TYPE_REFFN_P (type))
3601 if (TREE_CODE (expr) == ADDR_EXPR)
3603 error ("%qE is not a valid template argument for type %qT "
3604 "because it is a pointer", expr, type);
3605 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3606 return NULL_TREE;
3609 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3610 if (!expr || expr == error_mark_node)
3611 return expr;
3613 expr = build_nop (type, build_address (expr));
3615 /* [temp.arg.nontype]/5, bullet 6
3617 For a non-type template-parameter of type pointer to member function,
3618 no conversions apply. If the template-argument represents a set of
3619 overloaded member functions, the matching member function is selected
3620 from the set (_over.over_). */
3621 else if (TYPE_PTRMEMFUNC_P (type))
3623 expr = instantiate_type (type, expr, tf_none);
3624 if (expr == error_mark_node)
3625 return error_mark_node;
3627 /* There is no way to disable standard conversions in
3628 resolve_address_of_overloaded_function (called by
3629 instantiate_type). It is possible that the call succeeded by
3630 converting &B::I to &D::I (where B is a base of D), so we need
3631 to reject this conversion here.
3633 Actually, even if there was a way to disable standard conversions,
3634 it would still be better to reject them here so that we can
3635 provide a superior diagnostic. */
3636 if (!same_type_p (TREE_TYPE (expr), type))
3638 /* Make sure we are just one standard conversion off. */
3639 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3640 error ("%qE is not a valid template argument for type %qT "
3641 "because it is of type %qT", expr, type,
3642 TREE_TYPE (expr));
3643 inform ("standard conversions are not allowed in this context");
3644 return NULL_TREE;
3647 /* [temp.arg.nontype]/5, bullet 7
3649 For a non-type template-parameter of type pointer to data member,
3650 qualification conversions (_conv.qual_) are applied. */
3651 else if (TYPE_PTRMEM_P (type))
3653 expr = perform_qualification_conversions (type, expr);
3654 if (expr == error_mark_node)
3655 return expr;
3657 /* A template non-type parameter must be one of the above. */
3658 else
3659 gcc_unreachable ();
3661 /* Sanity check: did we actually convert the argument to the
3662 right type? */
3663 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3664 return expr;
3668 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3669 template template parameters. Both PARM_PARMS and ARG_PARMS are
3670 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3671 or PARM_DECL.
3673 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3674 the case, then extra parameters must have default arguments.
3676 Consider the example:
3677 template <class T, class Allocator = allocator> class vector;
3678 template<template <class U> class TT> class C;
3680 C<vector> is a valid instantiation. PARM_PARMS for the above code
3681 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3682 T and Allocator) and OUTER_ARGS contains the argument that is used to
3683 substitute the TT parameter. */
3685 static int
3686 coerce_template_template_parms (tree parm_parms,
3687 tree arg_parms,
3688 tsubst_flags_t complain,
3689 tree in_decl,
3690 tree outer_args)
3692 int nparms, nargs, i;
3693 tree parm, arg;
3695 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3696 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3698 nparms = TREE_VEC_LENGTH (parm_parms);
3699 nargs = TREE_VEC_LENGTH (arg_parms);
3701 /* The rule here is opposite of coerce_template_parms. */
3702 if (nargs < nparms
3703 || (nargs > nparms
3704 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3705 return 0;
3707 for (i = 0; i < nparms; ++i)
3709 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3710 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3712 if (arg == NULL_TREE || arg == error_mark_node
3713 || parm == NULL_TREE || parm == error_mark_node)
3714 return 0;
3716 if (TREE_CODE (arg) != TREE_CODE (parm))
3717 return 0;
3719 switch (TREE_CODE (parm))
3721 case TYPE_DECL:
3722 break;
3724 case TEMPLATE_DECL:
3725 /* We encounter instantiations of templates like
3726 template <template <template <class> class> class TT>
3727 class C; */
3729 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3730 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3732 if (!coerce_template_template_parms
3733 (parmparm, argparm, complain, in_decl, outer_args))
3734 return 0;
3736 break;
3738 case PARM_DECL:
3739 /* The tsubst call is used to handle cases such as
3741 template <int> class C {};
3742 template <class T, template <T> class TT> class D {};
3743 D<int, C> d;
3745 i.e. the parameter list of TT depends on earlier parameters. */
3746 if (!dependent_type_p (TREE_TYPE (arg))
3747 && !same_type_p
3748 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3749 TREE_TYPE (arg)))
3750 return 0;
3751 break;
3753 default:
3754 gcc_unreachable ();
3757 return 1;
3760 /* Convert the indicated template ARG as necessary to match the
3761 indicated template PARM. Returns the converted ARG, or
3762 error_mark_node if the conversion was unsuccessful. Error and
3763 warning messages are issued under control of COMPLAIN. This
3764 conversion is for the Ith parameter in the parameter list. ARGS is
3765 the full set of template arguments deduced so far. */
3767 static tree
3768 convert_template_argument (tree parm,
3769 tree arg,
3770 tree args,
3771 tsubst_flags_t complain,
3772 int i,
3773 tree in_decl)
3775 tree val;
3776 tree inner_args;
3777 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3779 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3781 if (TREE_CODE (arg) == TREE_LIST
3782 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3784 /* The template argument was the name of some
3785 member function. That's usually
3786 invalid, but static members are OK. In any
3787 case, grab the underlying fields/functions
3788 and issue an error later if required. */
3789 arg = TREE_VALUE (arg);
3790 TREE_TYPE (arg) = unknown_type_node;
3793 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3794 requires_type = (TREE_CODE (parm) == TYPE_DECL
3795 || requires_tmpl_type);
3797 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3798 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3799 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3800 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3802 if (is_tmpl_type
3803 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3804 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3805 arg = TYPE_STUB_DECL (arg);
3807 is_type = TYPE_P (arg) || is_tmpl_type;
3809 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3810 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3812 pedwarn ("to refer to a type member of a template parameter, "
3813 "use %<typename %E%>", arg);
3815 arg = make_typename_type (TREE_OPERAND (arg, 0),
3816 TREE_OPERAND (arg, 1),
3817 typename_type,
3818 complain & tf_error);
3819 is_type = 1;
3821 if (is_type != requires_type)
3823 if (in_decl)
3825 if (complain & tf_error)
3827 error ("type/value mismatch at argument %d in template "
3828 "parameter list for %qD",
3829 i + 1, in_decl);
3830 if (is_type)
3831 error (" expected a constant of type %qT, got %qT",
3832 TREE_TYPE (parm),
3833 (is_tmpl_type ? DECL_NAME (arg) : arg));
3834 else if (requires_tmpl_type)
3835 error (" expected a class template, got %qE", arg);
3836 else
3837 error (" expected a type, got %qE", arg);
3840 return error_mark_node;
3842 if (is_tmpl_type ^ requires_tmpl_type)
3844 if (in_decl && (complain & tf_error))
3846 error ("type/value mismatch at argument %d in template "
3847 "parameter list for %qD",
3848 i + 1, in_decl);
3849 if (is_tmpl_type)
3850 error (" expected a type, got %qT", DECL_NAME (arg));
3851 else
3852 error (" expected a class template, got %qT", arg);
3854 return error_mark_node;
3857 if (is_type)
3859 if (requires_tmpl_type)
3861 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3862 /* The number of argument required is not known yet.
3863 Just accept it for now. */
3864 val = TREE_TYPE (arg);
3865 else
3867 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3868 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3870 if (coerce_template_template_parms (parmparm, argparm,
3871 complain, in_decl,
3872 inner_args))
3874 val = arg;
3876 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3877 TEMPLATE_DECL. */
3878 if (val != error_mark_node
3879 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3880 val = TREE_TYPE (val);
3882 else
3884 if (in_decl && (complain & tf_error))
3886 error ("type/value mismatch at argument %d in "
3887 "template parameter list for %qD",
3888 i + 1, in_decl);
3889 error (" expected a template of type %qD, got %qD",
3890 parm, arg);
3893 val = error_mark_node;
3897 else
3898 val = arg;
3900 else
3902 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3904 if (invalid_nontype_parm_type_p (t, complain))
3905 return error_mark_node;
3907 if (!uses_template_parms (arg) && !uses_template_parms (t))
3908 /* We used to call digest_init here. However, digest_init
3909 will report errors, which we don't want when complain
3910 is zero. More importantly, digest_init will try too
3911 hard to convert things: for example, `0' should not be
3912 converted to pointer type at this point according to
3913 the standard. Accepting this is not merely an
3914 extension, since deciding whether or not these
3915 conversions can occur is part of determining which
3916 function template to call, or whether a given explicit
3917 argument specification is valid. */
3918 val = convert_nontype_argument (t, arg);
3919 else
3920 val = arg;
3922 if (val == NULL_TREE)
3923 val = error_mark_node;
3924 else if (val == error_mark_node && (complain & tf_error))
3925 error ("could not convert template argument %qE to %qT", arg, t);
3928 return val;
3931 /* Convert all template arguments to their appropriate types, and
3932 return a vector containing the innermost resulting template
3933 arguments. If any error occurs, return error_mark_node. Error and
3934 warning messages are issued under control of COMPLAIN.
3936 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3937 provided in ARGLIST, or else trailing parameters must have default
3938 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3939 deduction for any unspecified trailing arguments. */
3941 static tree
3942 coerce_template_parms (tree parms,
3943 tree args,
3944 tree in_decl,
3945 tsubst_flags_t complain,
3946 int require_all_arguments)
3948 int nparms, nargs, i, lost = 0;
3949 tree inner_args;
3950 tree new_args;
3951 tree new_inner_args;
3953 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3954 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
3955 nparms = TREE_VEC_LENGTH (parms);
3957 if (nargs > nparms
3958 || (nargs < nparms
3959 && require_all_arguments
3960 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3962 if (complain & tf_error)
3964 error ("wrong number of template arguments (%d, should be %d)",
3965 nargs, nparms);
3967 if (in_decl)
3968 cp_error_at ("provided for %qD", in_decl);
3971 return error_mark_node;
3974 new_inner_args = make_tree_vec (nparms);
3975 new_args = add_outermost_template_args (args, new_inner_args);
3976 for (i = 0; i < nparms; i++)
3978 tree arg;
3979 tree parm;
3981 /* Get the Ith template parameter. */
3982 parm = TREE_VEC_ELT (parms, i);
3984 /* Calculate the Ith argument. */
3985 if (i < nargs)
3986 arg = TREE_VEC_ELT (inner_args, i);
3987 else if (require_all_arguments)
3988 /* There must be a default arg in this case. */
3989 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
3990 complain, in_decl);
3991 else
3992 break;
3994 gcc_assert (arg);
3995 if (arg == error_mark_node)
3997 if (complain & tf_error)
3998 error ("template argument %d is invalid", i + 1);
4000 else
4001 arg = convert_template_argument (TREE_VALUE (parm),
4002 arg, new_args, complain, i,
4003 in_decl);
4005 if (arg == error_mark_node)
4006 lost++;
4007 TREE_VEC_ELT (new_inner_args, i) = arg;
4010 if (lost)
4011 return error_mark_node;
4013 return new_inner_args;
4016 /* Returns 1 if template args OT and NT are equivalent. */
4018 static int
4019 template_args_equal (tree ot, tree nt)
4021 if (nt == ot)
4022 return 1;
4024 if (TREE_CODE (nt) == TREE_VEC)
4025 /* For member templates */
4026 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4027 else if (TYPE_P (nt))
4028 return TYPE_P (ot) && same_type_p (ot, nt);
4029 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4030 return 0;
4031 else
4032 return cp_tree_equal (ot, nt);
4035 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4036 of template arguments. Returns 0 otherwise. */
4039 comp_template_args (tree oldargs, tree newargs)
4041 int i;
4043 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4044 return 0;
4046 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4048 tree nt = TREE_VEC_ELT (newargs, i);
4049 tree ot = TREE_VEC_ELT (oldargs, i);
4051 if (! template_args_equal (ot, nt))
4052 return 0;
4054 return 1;
4057 /* Given class template name and parameter list, produce a user-friendly name
4058 for the instantiation. */
4060 static char *
4061 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4063 static struct obstack scratch_obstack;
4064 static char *scratch_firstobj;
4065 int i, nparms;
4067 if (!scratch_firstobj)
4068 gcc_obstack_init (&scratch_obstack);
4069 else
4070 obstack_free (&scratch_obstack, scratch_firstobj);
4071 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
4073 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4074 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4076 cat (name);
4077 ccat ('<');
4078 nparms = TREE_VEC_LENGTH (parms);
4079 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4080 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4081 for (i = 0; i < nparms; i++)
4083 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4084 tree arg = TREE_VEC_ELT (arglist, i);
4086 if (i)
4087 ccat (',');
4089 if (TREE_CODE (parm) == TYPE_DECL)
4091 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4092 continue;
4094 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4096 if (TREE_CODE (arg) == TEMPLATE_DECL)
4098 /* Already substituted with real template. Just output
4099 the template name here */
4100 tree context = DECL_CONTEXT (arg);
4101 if (context)
4103 /* The template may be defined in a namespace, or
4104 may be a member template. */
4105 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4106 || CLASS_TYPE_P (context));
4107 cat (decl_as_string (DECL_CONTEXT (arg),
4108 TFF_PLAIN_IDENTIFIER));
4109 cat ("::");
4111 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4113 else
4114 /* Output the parameter declaration. */
4115 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4116 continue;
4118 else
4119 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4121 /* No need to check arglist against parmlist here; we did that
4122 in coerce_template_parms, called from lookup_template_class. */
4123 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4126 char *bufp = obstack_next_free (&scratch_obstack);
4127 int offset = 0;
4128 while (bufp[offset - 1] == ' ')
4129 offset--;
4130 obstack_blank_fast (&scratch_obstack, offset);
4132 /* B<C<char> >, not B<C<char>> */
4133 if (bufp[offset - 1] == '>')
4134 ccat (' ');
4136 ccat ('>');
4137 ccat ('\0');
4138 return (char *) obstack_base (&scratch_obstack);
4141 static tree
4142 classtype_mangled_name (tree t)
4144 if (CLASSTYPE_TEMPLATE_INFO (t)
4145 /* Specializations have already had their names set up in
4146 lookup_template_class. */
4147 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4149 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4151 /* For non-primary templates, the template parameters are
4152 implicit from their surrounding context. */
4153 if (PRIMARY_TEMPLATE_P (tmpl))
4155 tree name = DECL_NAME (tmpl);
4156 char *mangled_name = mangle_class_name_for_template
4157 (IDENTIFIER_POINTER (name),
4158 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4159 CLASSTYPE_TI_ARGS (t));
4160 tree id = get_identifier (mangled_name);
4161 IDENTIFIER_TEMPLATE (id) = name;
4162 return id;
4166 return TYPE_IDENTIFIER (t);
4169 static void
4170 add_pending_template (tree d)
4172 tree ti = (TYPE_P (d)
4173 ? CLASSTYPE_TEMPLATE_INFO (d)
4174 : DECL_TEMPLATE_INFO (d));
4175 tree pt;
4176 int level;
4178 if (TI_PENDING_TEMPLATE_FLAG (ti))
4179 return;
4181 /* We are called both from instantiate_decl, where we've already had a
4182 tinst_level pushed, and instantiate_template, where we haven't.
4183 Compensate. */
4184 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4186 if (level)
4187 push_tinst_level (d);
4189 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4190 if (last_pending_template)
4191 TREE_CHAIN (last_pending_template) = pt;
4192 else
4193 pending_templates = pt;
4195 last_pending_template = pt;
4197 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4199 if (level)
4200 pop_tinst_level ();
4204 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4205 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4206 documentation for TEMPLATE_ID_EXPR. */
4208 tree
4209 lookup_template_function (tree fns, tree arglist)
4211 tree type;
4213 if (fns == error_mark_node || arglist == error_mark_node)
4214 return error_mark_node;
4216 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4217 gcc_assert (fns && (is_overloaded_fn (fns)
4218 || TREE_CODE (fns) == IDENTIFIER_NODE));
4220 if (BASELINK_P (fns))
4222 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4223 unknown_type_node,
4224 BASELINK_FUNCTIONS (fns),
4225 arglist);
4226 return fns;
4229 type = TREE_TYPE (fns);
4230 if (TREE_CODE (fns) == OVERLOAD || !type)
4231 type = unknown_type_node;
4233 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4236 /* Within the scope of a template class S<T>, the name S gets bound
4237 (in build_self_reference) to a TYPE_DECL for the class, not a
4238 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4239 or one of its enclosing classes, and that type is a template,
4240 return the associated TEMPLATE_DECL. Otherwise, the original
4241 DECL is returned. */
4243 tree
4244 maybe_get_template_decl_from_type_decl (tree decl)
4246 return (decl != NULL_TREE
4247 && TREE_CODE (decl) == TYPE_DECL
4248 && DECL_ARTIFICIAL (decl)
4249 && CLASS_TYPE_P (TREE_TYPE (decl))
4250 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4251 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4254 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4255 parameters, find the desired type.
4257 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4259 IN_DECL, if non-NULL, is the template declaration we are trying to
4260 instantiate.
4262 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4263 the class we are looking up.
4265 Issue error and warning messages under control of COMPLAIN.
4267 If the template class is really a local class in a template
4268 function, then the FUNCTION_CONTEXT is the function in which it is
4269 being instantiated.
4271 ??? Note that this function is currently called *twice* for each
4272 template-id: the first time from the parser, while creating the
4273 incomplete type (finish_template_type), and the second type during the
4274 real instantiation (instantiate_template_class). This is surely something
4275 that we want to avoid. It also causes some problems with argument
4276 coercion (see convert_nontype_argument for more information on this). */
4278 tree
4279 lookup_template_class (tree d1,
4280 tree arglist,
4281 tree in_decl,
4282 tree context,
4283 int entering_scope,
4284 tsubst_flags_t complain)
4286 tree template = NULL_TREE, parmlist;
4287 tree t;
4289 timevar_push (TV_NAME_LOOKUP);
4291 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4293 tree value = innermost_non_namespace_value (d1);
4294 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4295 template = value;
4296 else
4298 if (context)
4299 push_decl_namespace (context);
4300 template = lookup_name (d1, /*prefer_type=*/0);
4301 template = maybe_get_template_decl_from_type_decl (template);
4302 if (context)
4303 pop_decl_namespace ();
4305 if (template)
4306 context = DECL_CONTEXT (template);
4308 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4310 tree type = TREE_TYPE (d1);
4312 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4313 an implicit typename for the second A. Deal with it. */
4314 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4315 type = TREE_TYPE (type);
4317 if (CLASSTYPE_TEMPLATE_INFO (type))
4319 template = CLASSTYPE_TI_TEMPLATE (type);
4320 d1 = DECL_NAME (template);
4323 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4324 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4326 template = TYPE_TI_TEMPLATE (d1);
4327 d1 = DECL_NAME (template);
4329 else if (TREE_CODE (d1) == TEMPLATE_DECL
4330 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4332 template = d1;
4333 d1 = DECL_NAME (template);
4334 context = DECL_CONTEXT (template);
4337 /* Issue an error message if we didn't find a template. */
4338 if (! template)
4340 if (complain & tf_error)
4341 error ("%qT is not a template", d1);
4342 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4345 if (TREE_CODE (template) != TEMPLATE_DECL
4346 /* Make sure it's a user visible template, if it was named by
4347 the user. */
4348 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4349 && !PRIMARY_TEMPLATE_P (template)))
4351 if (complain & tf_error)
4353 error ("non-template type %qT used as a template", d1);
4354 if (in_decl)
4355 cp_error_at ("for template declaration %qD", in_decl);
4357 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4360 complain &= ~tf_user;
4362 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4364 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4365 template arguments */
4367 tree parm;
4368 tree arglist2;
4370 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4372 /* Consider an example where a template template parameter declared as
4374 template <class T, class U = std::allocator<T> > class TT
4376 The template parameter level of T and U are one level larger than
4377 of TT. To proper process the default argument of U, say when an
4378 instantiation `TT<int>' is seen, we need to build the full
4379 arguments containing {int} as the innermost level. Outer levels,
4380 available when not appearing as default template argument, can be
4381 obtained from `current_template_args ()'.
4383 Suppose that TT is later substituted with std::vector. The above
4384 instantiation is `TT<int, std::allocator<T> >' with TT at
4385 level 1, and T at level 2, while the template arguments at level 1
4386 becomes {std::vector} and the inner level 2 is {int}. */
4388 if (current_template_parms)
4389 arglist = add_to_template_args (current_template_args (), arglist);
4391 arglist2 = coerce_template_parms (parmlist, arglist, template,
4392 complain, /*require_all_args=*/1);
4393 if (arglist2 == error_mark_node
4394 || (!uses_template_parms (arglist2)
4395 && check_instantiated_args (template, arglist2, complain)))
4396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4398 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4399 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4401 else
4403 tree template_type = TREE_TYPE (template);
4404 tree gen_tmpl;
4405 tree type_decl;
4406 tree found = NULL_TREE;
4407 int arg_depth;
4408 int parm_depth;
4409 int is_partial_instantiation;
4411 gen_tmpl = most_general_template (template);
4412 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4413 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4414 arg_depth = TMPL_ARGS_DEPTH (arglist);
4416 if (arg_depth == 1 && parm_depth > 1)
4418 /* We've been given an incomplete set of template arguments.
4419 For example, given:
4421 template <class T> struct S1 {
4422 template <class U> struct S2 {};
4423 template <class U> struct S2<U*> {};
4426 we will be called with an ARGLIST of `U*', but the
4427 TEMPLATE will be `template <class T> template
4428 <class U> struct S1<T>::S2'. We must fill in the missing
4429 arguments. */
4430 arglist
4431 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4432 arglist);
4433 arg_depth = TMPL_ARGS_DEPTH (arglist);
4436 /* Now we should have enough arguments. */
4437 gcc_assert (parm_depth == arg_depth);
4439 /* From here on, we're only interested in the most general
4440 template. */
4441 template = gen_tmpl;
4443 /* Calculate the BOUND_ARGS. These will be the args that are
4444 actually tsubst'd into the definition to create the
4445 instantiation. */
4446 if (parm_depth > 1)
4448 /* We have multiple levels of arguments to coerce, at once. */
4449 int i;
4450 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4452 tree bound_args = make_tree_vec (parm_depth);
4454 for (i = saved_depth,
4455 t = DECL_TEMPLATE_PARMS (template);
4456 i > 0 && t != NULL_TREE;
4457 --i, t = TREE_CHAIN (t))
4459 tree a = coerce_template_parms (TREE_VALUE (t),
4460 arglist, template,
4461 complain, /*require_all_args=*/1);
4463 /* Don't process further if one of the levels fails. */
4464 if (a == error_mark_node)
4466 /* Restore the ARGLIST to its full size. */
4467 TREE_VEC_LENGTH (arglist) = saved_depth;
4468 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4471 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4473 /* We temporarily reduce the length of the ARGLIST so
4474 that coerce_template_parms will see only the arguments
4475 corresponding to the template parameters it is
4476 examining. */
4477 TREE_VEC_LENGTH (arglist)--;
4480 /* Restore the ARGLIST to its full size. */
4481 TREE_VEC_LENGTH (arglist) = saved_depth;
4483 arglist = bound_args;
4485 else
4486 arglist
4487 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4488 INNERMOST_TEMPLATE_ARGS (arglist),
4489 template,
4490 complain, /*require_all_args=*/1);
4492 if (arglist == error_mark_node)
4493 /* We were unable to bind the arguments. */
4494 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4496 /* In the scope of a template class, explicit references to the
4497 template class refer to the type of the template, not any
4498 instantiation of it. For example, in:
4500 template <class T> class C { void f(C<T>); }
4502 the `C<T>' is just the same as `C'. Outside of the
4503 class, however, such a reference is an instantiation. */
4504 if (comp_template_args (TYPE_TI_ARGS (template_type),
4505 arglist))
4507 found = template_type;
4509 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4511 tree ctx;
4513 for (ctx = current_class_type;
4514 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4515 ctx = (TYPE_P (ctx)
4516 ? TYPE_CONTEXT (ctx)
4517 : DECL_CONTEXT (ctx)))
4518 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4519 goto found_ctx;
4521 /* We're not in the scope of the class, so the
4522 TEMPLATE_TYPE is not the type we want after all. */
4523 found = NULL_TREE;
4524 found_ctx:;
4527 if (found)
4528 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4530 /* If we already have this specialization, return it. */
4531 found = retrieve_specialization (template, arglist,
4532 /*class_specializations_p=*/false);
4533 if (found)
4534 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4536 /* This type is a "partial instantiation" if any of the template
4537 arguments still involve template parameters. Note that we set
4538 IS_PARTIAL_INSTANTIATION for partial specializations as
4539 well. */
4540 is_partial_instantiation = uses_template_parms (arglist);
4542 /* If the deduced arguments are invalid, then the binding
4543 failed. */
4544 if (!is_partial_instantiation
4545 && check_instantiated_args (template,
4546 INNERMOST_TEMPLATE_ARGS (arglist),
4547 complain))
4548 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4550 if (!is_partial_instantiation
4551 && !PRIMARY_TEMPLATE_P (template)
4552 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4554 found = xref_tag_from_type (TREE_TYPE (template),
4555 DECL_NAME (template),
4556 /*tag_scope=*/ts_global);
4557 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4560 context = tsubst (DECL_CONTEXT (template), arglist,
4561 complain, in_decl);
4562 if (!context)
4563 context = global_namespace;
4565 /* Create the type. */
4566 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4568 if (!is_partial_instantiation)
4570 set_current_access_from_decl (TYPE_NAME (template_type));
4571 t = start_enum (TYPE_IDENTIFIER (template_type));
4573 else
4574 /* We don't want to call start_enum for this type, since
4575 the values for the enumeration constants may involve
4576 template parameters. And, no one should be interested
4577 in the enumeration constants for such a type. */
4578 t = make_node (ENUMERAL_TYPE);
4580 else
4582 t = make_aggr_type (TREE_CODE (template_type));
4583 CLASSTYPE_DECLARED_CLASS (t)
4584 = CLASSTYPE_DECLARED_CLASS (template_type);
4585 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4586 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4588 /* A local class. Make sure the decl gets registered properly. */
4589 if (context == current_function_decl)
4590 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4593 /* If we called start_enum or pushtag above, this information
4594 will already be set up. */
4595 if (!TYPE_NAME (t))
4597 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4599 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4600 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4601 TYPE_STUB_DECL (t) = type_decl;
4602 DECL_SOURCE_LOCATION (type_decl)
4603 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4605 else
4606 type_decl = TYPE_NAME (t);
4608 TREE_PRIVATE (type_decl)
4609 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4610 TREE_PROTECTED (type_decl)
4611 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4613 /* Set up the template information. We have to figure out which
4614 template is the immediate parent if this is a full
4615 instantiation. */
4616 if (parm_depth == 1 || is_partial_instantiation
4617 || !PRIMARY_TEMPLATE_P (template))
4618 /* This case is easy; there are no member templates involved. */
4619 found = template;
4620 else
4622 /* This is a full instantiation of a member template. Look
4623 for a partial instantiation of which this is an instance. */
4625 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4626 found; found = TREE_CHAIN (found))
4628 int success;
4629 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4631 /* We only want partial instantiations, here, not
4632 specializations or full instantiations. */
4633 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4634 || !uses_template_parms (TREE_VALUE (found)))
4635 continue;
4637 /* Temporarily reduce by one the number of levels in the
4638 ARGLIST and in FOUND so as to avoid comparing the
4639 last set of arguments. */
4640 TREE_VEC_LENGTH (arglist)--;
4641 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4643 /* See if the arguments match. If they do, then TMPL is
4644 the partial instantiation we want. */
4645 success = comp_template_args (TREE_PURPOSE (found), arglist);
4647 /* Restore the argument vectors to their full size. */
4648 TREE_VEC_LENGTH (arglist)++;
4649 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4651 if (success)
4653 found = tmpl;
4654 break;
4658 if (!found)
4660 /* There was no partial instantiation. This happens
4661 where C<T> is a member template of A<T> and it's used
4662 in something like
4664 template <typename T> struct B { A<T>::C<int> m; };
4665 B<float>;
4667 Create the partial instantiation.
4669 TREE_VEC_LENGTH (arglist)--;
4670 found = tsubst (template, arglist, complain, NULL_TREE);
4671 TREE_VEC_LENGTH (arglist)++;
4675 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4676 DECL_TEMPLATE_INSTANTIATIONS (template)
4677 = tree_cons (arglist, t,
4678 DECL_TEMPLATE_INSTANTIATIONS (template));
4680 if (TREE_CODE (t) == ENUMERAL_TYPE
4681 && !is_partial_instantiation)
4682 /* Now that the type has been registered on the instantiations
4683 list, we set up the enumerators. Because the enumeration
4684 constants may involve the enumeration type itself, we make
4685 sure to register the type first, and then create the
4686 constants. That way, doing tsubst_expr for the enumeration
4687 constants won't result in recursive calls here; we'll find
4688 the instantiation and exit above. */
4689 tsubst_enum (template_type, t, arglist);
4691 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4692 is set up. */
4693 if (TREE_CODE (t) != ENUMERAL_TYPE)
4694 DECL_NAME (type_decl) = classtype_mangled_name (t);
4695 if (is_partial_instantiation)
4696 /* If the type makes use of template parameters, the
4697 code that generates debugging information will crash. */
4698 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4700 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4702 timevar_pop (TV_NAME_LOOKUP);
4705 struct pair_fn_data
4707 tree_fn_t fn;
4708 void *data;
4709 struct pointer_set_t *visited;
4712 /* Called from for_each_template_parm via walk_tree. */
4714 static tree
4715 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4717 tree t = *tp;
4718 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4719 tree_fn_t fn = pfd->fn;
4720 void *data = pfd->data;
4722 if (TYPE_P (t)
4723 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4724 return error_mark_node;
4726 switch (TREE_CODE (t))
4728 case RECORD_TYPE:
4729 if (TYPE_PTRMEMFUNC_P (t))
4730 break;
4731 /* Fall through. */
4733 case UNION_TYPE:
4734 case ENUMERAL_TYPE:
4735 if (!TYPE_TEMPLATE_INFO (t))
4736 *walk_subtrees = 0;
4737 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4738 fn, data, pfd->visited))
4739 return error_mark_node;
4740 break;
4742 case METHOD_TYPE:
4743 /* Since we're not going to walk subtrees, we have to do this
4744 explicitly here. */
4745 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4746 pfd->visited))
4747 return error_mark_node;
4748 /* Fall through. */
4750 case FUNCTION_TYPE:
4751 /* Check the return type. */
4752 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4753 return error_mark_node;
4755 /* Check the parameter types. Since default arguments are not
4756 instantiated until they are needed, the TYPE_ARG_TYPES may
4757 contain expressions that involve template parameters. But,
4758 no-one should be looking at them yet. And, once they're
4759 instantiated, they don't contain template parameters, so
4760 there's no point in looking at them then, either. */
4762 tree parm;
4764 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4765 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4766 pfd->visited))
4767 return error_mark_node;
4769 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4770 want walk_tree walking into them itself. */
4771 *walk_subtrees = 0;
4773 break;
4775 case TYPEOF_TYPE:
4776 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4777 pfd->visited))
4778 return error_mark_node;
4779 break;
4781 case FUNCTION_DECL:
4782 case VAR_DECL:
4783 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4784 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4785 pfd->visited))
4786 return error_mark_node;
4787 /* Fall through. */
4789 case PARM_DECL:
4790 case CONST_DECL:
4791 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4792 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4793 pfd->visited))
4794 return error_mark_node;
4795 if (DECL_CONTEXT (t)
4796 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4797 pfd->visited))
4798 return error_mark_node;
4799 break;
4801 case BOUND_TEMPLATE_TEMPLATE_PARM:
4802 /* Record template parameters such as `T' inside `TT<T>'. */
4803 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4804 return error_mark_node;
4805 /* Fall through. */
4807 case TEMPLATE_TEMPLATE_PARM:
4808 case TEMPLATE_TYPE_PARM:
4809 case TEMPLATE_PARM_INDEX:
4810 if (fn && (*fn)(t, data))
4811 return error_mark_node;
4812 else if (!fn)
4813 return error_mark_node;
4814 break;
4816 case TEMPLATE_DECL:
4817 /* A template template parameter is encountered. */
4818 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4819 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4820 return error_mark_node;
4822 /* Already substituted template template parameter */
4823 *walk_subtrees = 0;
4824 break;
4826 case TYPENAME_TYPE:
4827 if (!fn
4828 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4829 data, pfd->visited))
4830 return error_mark_node;
4831 break;
4833 case CONSTRUCTOR:
4834 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4835 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4836 (TREE_TYPE (t)), fn, data,
4837 pfd->visited))
4838 return error_mark_node;
4839 break;
4841 case INDIRECT_REF:
4842 case COMPONENT_REF:
4843 /* If there's no type, then this thing must be some expression
4844 involving template parameters. */
4845 if (!fn && !TREE_TYPE (t))
4846 return error_mark_node;
4847 break;
4849 case MODOP_EXPR:
4850 case CAST_EXPR:
4851 case REINTERPRET_CAST_EXPR:
4852 case CONST_CAST_EXPR:
4853 case STATIC_CAST_EXPR:
4854 case DYNAMIC_CAST_EXPR:
4855 case ARROW_EXPR:
4856 case DOTSTAR_EXPR:
4857 case TYPEID_EXPR:
4858 case PSEUDO_DTOR_EXPR:
4859 if (!fn)
4860 return error_mark_node;
4861 break;
4863 case BASELINK:
4864 /* If we do not handle this case specially, we end up walking
4865 the BINFO hierarchy, which is circular, and therefore
4866 confuses walk_tree. */
4867 *walk_subtrees = 0;
4868 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4869 pfd->visited))
4870 return error_mark_node;
4871 break;
4873 default:
4874 break;
4877 /* We didn't find any template parameters we liked. */
4878 return NULL_TREE;
4881 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4882 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4883 call FN with the parameter and the DATA.
4884 If FN returns nonzero, the iteration is terminated, and
4885 for_each_template_parm returns 1. Otherwise, the iteration
4886 continues. If FN never returns a nonzero value, the value
4887 returned by for_each_template_parm is 0. If FN is NULL, it is
4888 considered to be the function which always returns 1. */
4890 static int
4891 for_each_template_parm (tree t, tree_fn_t fn, void* data,
4892 struct pointer_set_t *visited)
4894 struct pair_fn_data pfd;
4895 int result;
4897 /* Set up. */
4898 pfd.fn = fn;
4899 pfd.data = data;
4901 /* Walk the tree. (Conceptually, we would like to walk without
4902 duplicates, but for_each_template_parm_r recursively calls
4903 for_each_template_parm, so we would need to reorganize a fair
4904 bit to use walk_tree_without_duplicates, so we keep our own
4905 visited list.) */
4906 if (visited)
4907 pfd.visited = visited;
4908 else
4909 pfd.visited = pointer_set_create ();
4910 result = walk_tree (&t,
4911 for_each_template_parm_r,
4912 &pfd,
4913 pfd.visited) != NULL_TREE;
4915 /* Clean up. */
4916 if (!visited)
4918 pointer_set_destroy (pfd.visited);
4919 pfd.visited = 0;
4922 return result;
4925 /* Returns true if T depends on any template parameter. */
4928 uses_template_parms (tree t)
4930 bool dependent_p;
4931 int saved_processing_template_decl;
4933 saved_processing_template_decl = processing_template_decl;
4934 if (!saved_processing_template_decl)
4935 processing_template_decl = 1;
4936 if (TYPE_P (t))
4937 dependent_p = dependent_type_p (t);
4938 else if (TREE_CODE (t) == TREE_VEC)
4939 dependent_p = any_dependent_template_arguments_p (t);
4940 else if (TREE_CODE (t) == TREE_LIST)
4941 dependent_p = (uses_template_parms (TREE_VALUE (t))
4942 || uses_template_parms (TREE_CHAIN (t)));
4943 else if (DECL_P (t)
4944 || EXPR_P (t)
4945 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
4946 || TREE_CODE (t) == OVERLOAD
4947 || TREE_CODE (t) == BASELINK
4948 || CONSTANT_CLASS_P (t))
4949 dependent_p = (type_dependent_expression_p (t)
4950 || value_dependent_expression_p (t));
4951 else
4953 gcc_assert (t == error_mark_node);
4954 dependent_p = false;
4957 processing_template_decl = saved_processing_template_decl;
4959 return dependent_p;
4962 /* Returns true if T depends on any template parameter with level LEVEL. */
4965 uses_template_parms_level (tree t, int level)
4967 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
4970 static int tinst_depth;
4971 extern int max_tinst_depth;
4972 #ifdef GATHER_STATISTICS
4973 int depth_reached;
4974 #endif
4975 static int tinst_level_tick;
4976 static int last_template_error_tick;
4978 /* We're starting to instantiate D; record the template instantiation context
4979 for diagnostics and to restore it later. */
4982 push_tinst_level (tree d)
4984 tree new;
4986 if (tinst_depth >= max_tinst_depth)
4988 /* If the instantiation in question still has unbound template parms,
4989 we don't really care if we can't instantiate it, so just return.
4990 This happens with base instantiation for implicit `typename'. */
4991 if (uses_template_parms (d))
4992 return 0;
4994 last_template_error_tick = tinst_level_tick;
4995 error ("template instantiation depth exceeds maximum of %d (use "
4996 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
4997 max_tinst_depth, d);
4999 print_instantiation_context ();
5001 return 0;
5004 new = make_tinst_level (d, input_location);
5005 TREE_CHAIN (new) = current_tinst_level;
5006 current_tinst_level = new;
5008 ++tinst_depth;
5009 #ifdef GATHER_STATISTICS
5010 if (tinst_depth > depth_reached)
5011 depth_reached = tinst_depth;
5012 #endif
5014 ++tinst_level_tick;
5015 return 1;
5018 /* We're done instantiating this template; return to the instantiation
5019 context. */
5021 void
5022 pop_tinst_level (void)
5024 tree old = current_tinst_level;
5026 /* Restore the filename and line number stashed away when we started
5027 this instantiation. */
5028 input_location = TINST_LOCATION (old);
5029 current_tinst_level = TREE_CHAIN (old);
5030 --tinst_depth;
5031 ++tinst_level_tick;
5034 /* We're instantiating a deferred template; restore the template
5035 instantiation context in which the instantiation was requested, which
5036 is one step out from LEVEL. */
5038 static void
5039 reopen_tinst_level (tree level)
5041 tree t;
5043 tinst_depth = 0;
5044 for (t = level; t; t = TREE_CHAIN (t))
5045 ++tinst_depth;
5047 current_tinst_level = level;
5048 pop_tinst_level ();
5051 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5052 vector of template arguments, as for tsubst.
5054 Returns an appropriate tsubst'd friend declaration. */
5056 static tree
5057 tsubst_friend_function (tree decl, tree args)
5059 tree new_friend;
5061 if (TREE_CODE (decl) == FUNCTION_DECL
5062 && DECL_TEMPLATE_INSTANTIATION (decl)
5063 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5064 /* This was a friend declared with an explicit template
5065 argument list, e.g.:
5067 friend void f<>(T);
5069 to indicate that f was a template instantiation, not a new
5070 function declaration. Now, we have to figure out what
5071 instantiation of what template. */
5073 tree template_id, arglist, fns;
5074 tree new_args;
5075 tree tmpl;
5076 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5078 /* Friend functions are looked up in the containing namespace scope.
5079 We must enter that scope, to avoid finding member functions of the
5080 current cless with same name. */
5081 push_nested_namespace (ns);
5082 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5083 tf_error | tf_warning, NULL_TREE);
5084 pop_nested_namespace (ns);
5085 arglist = tsubst (DECL_TI_ARGS (decl), args,
5086 tf_error | tf_warning, NULL_TREE);
5087 template_id = lookup_template_function (fns, arglist);
5089 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5090 tmpl = determine_specialization (template_id, new_friend,
5091 &new_args,
5092 /*need_member_template=*/0,
5093 TREE_VEC_LENGTH (args));
5094 return instantiate_template (tmpl, new_args, tf_error);
5097 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
5099 /* The NEW_FRIEND will look like an instantiation, to the
5100 compiler, but is not an instantiation from the point of view of
5101 the language. For example, we might have had:
5103 template <class T> struct S {
5104 template <class U> friend void f(T, U);
5107 Then, in S<int>, template <class U> void f(int, U) is not an
5108 instantiation of anything. */
5109 if (new_friend == error_mark_node)
5110 return error_mark_node;
5112 DECL_USE_TEMPLATE (new_friend) = 0;
5113 if (TREE_CODE (decl) == TEMPLATE_DECL)
5115 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5116 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5117 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5120 /* The mangled name for the NEW_FRIEND is incorrect. The function
5121 is not a template instantiation and should not be mangled like
5122 one. Therefore, we forget the mangling here; we'll recompute it
5123 later if we need it. */
5124 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5126 SET_DECL_RTL (new_friend, NULL_RTX);
5127 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5130 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5132 tree old_decl;
5133 tree new_friend_template_info;
5134 tree new_friend_result_template_info;
5135 tree ns;
5136 int new_friend_is_defn;
5138 /* We must save some information from NEW_FRIEND before calling
5139 duplicate decls since that function will free NEW_FRIEND if
5140 possible. */
5141 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5142 new_friend_is_defn =
5143 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5144 (template_for_substitution (new_friend)))
5145 != NULL_TREE);
5146 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5148 /* This declaration is a `primary' template. */
5149 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5151 new_friend_result_template_info
5152 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5154 else
5155 new_friend_result_template_info = NULL_TREE;
5157 /* Inside pushdecl_namespace_level, we will push into the
5158 current namespace. However, the friend function should go
5159 into the namespace of the template. */
5160 ns = decl_namespace_context (new_friend);
5161 push_nested_namespace (ns);
5162 old_decl = pushdecl_namespace_level (new_friend);
5163 pop_nested_namespace (ns);
5165 if (old_decl != new_friend)
5167 /* This new friend declaration matched an existing
5168 declaration. For example, given:
5170 template <class T> void f(T);
5171 template <class U> class C {
5172 template <class T> friend void f(T) {}
5175 the friend declaration actually provides the definition
5176 of `f', once C has been instantiated for some type. So,
5177 old_decl will be the out-of-class template declaration,
5178 while new_friend is the in-class definition.
5180 But, if `f' was called before this point, the
5181 instantiation of `f' will have DECL_TI_ARGS corresponding
5182 to `T' but not to `U', references to which might appear
5183 in the definition of `f'. Previously, the most general
5184 template for an instantiation of `f' was the out-of-class
5185 version; now it is the in-class version. Therefore, we
5186 run through all specialization of `f', adding to their
5187 DECL_TI_ARGS appropriately. In particular, they need a
5188 new set of outer arguments, corresponding to the
5189 arguments for this class instantiation.
5191 The same situation can arise with something like this:
5193 friend void f(int);
5194 template <class T> class C {
5195 friend void f(T) {}
5198 when `C<int>' is instantiated. Now, `f(int)' is defined
5199 in the class. */
5201 if (!new_friend_is_defn)
5202 /* On the other hand, if the in-class declaration does
5203 *not* provide a definition, then we don't want to alter
5204 existing definitions. We can just leave everything
5205 alone. */
5207 else
5209 /* Overwrite whatever template info was there before, if
5210 any, with the new template information pertaining to
5211 the declaration. */
5212 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5214 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5215 reregister_specialization (new_friend,
5216 most_general_template (old_decl),
5217 old_decl);
5218 else
5220 tree t;
5221 tree new_friend_args;
5223 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5224 = new_friend_result_template_info;
5226 new_friend_args = TI_ARGS (new_friend_template_info);
5227 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5228 t != NULL_TREE;
5229 t = TREE_CHAIN (t))
5231 tree spec = TREE_VALUE (t);
5233 DECL_TI_ARGS (spec)
5234 = add_outermost_template_args (new_friend_args,
5235 DECL_TI_ARGS (spec));
5238 /* Now, since specializations are always supposed to
5239 hang off of the most general template, we must move
5240 them. */
5241 t = most_general_template (old_decl);
5242 if (t != old_decl)
5244 DECL_TEMPLATE_SPECIALIZATIONS (t)
5245 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5246 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5247 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5252 /* The information from NEW_FRIEND has been merged into OLD_DECL
5253 by duplicate_decls. */
5254 new_friend = old_decl;
5257 else
5259 tree context = DECL_CONTEXT (new_friend);
5260 bool dependent_p;
5262 /* In the code
5263 template <class T> class C {
5264 template <class U> friend void C1<U>::f (); // case 1
5265 friend void C2<T>::f (); // case 2
5267 we only need to make sure CONTEXT is a complete type for
5268 case 2. To distinguish between the two cases, we note that
5269 CONTEXT of case 1 remains dependent type after tsubst while
5270 this isn't true for case 2. */
5271 ++processing_template_decl;
5272 dependent_p = dependent_type_p (context);
5273 --processing_template_decl;
5275 if (!dependent_p
5276 && !complete_type_or_else (context, NULL_TREE))
5277 return error_mark_node;
5279 if (COMPLETE_TYPE_P (context))
5281 /* Check to see that the declaration is really present, and,
5282 possibly obtain an improved declaration. */
5283 tree fn = check_classfn (context,
5284 new_friend, NULL_TREE);
5286 if (fn)
5287 new_friend = fn;
5291 return new_friend;
5294 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5295 template arguments, as for tsubst.
5297 Returns an appropriate tsubst'd friend type or error_mark_node on
5298 failure. */
5300 static tree
5301 tsubst_friend_class (tree friend_tmpl, tree args)
5303 tree friend_type;
5304 tree tmpl;
5305 tree context;
5307 context = DECL_CONTEXT (friend_tmpl);
5309 if (context)
5311 if (TREE_CODE (context) == NAMESPACE_DECL)
5312 push_nested_namespace (context);
5313 else
5314 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5317 /* First, we look for a class template. */
5318 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5320 /* But, if we don't find one, it might be because we're in a
5321 situation like this:
5323 template <class T>
5324 struct S {
5325 template <class U>
5326 friend struct S;
5329 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5330 for `S<int>', not the TEMPLATE_DECL. */
5331 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5333 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5334 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5337 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5339 /* The friend template has already been declared. Just
5340 check to see that the declarations match, and install any new
5341 default parameters. We must tsubst the default parameters,
5342 of course. We only need the innermost template parameters
5343 because that is all that redeclare_class_template will look
5344 at. */
5345 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5346 > TMPL_ARGS_DEPTH (args))
5348 tree parms;
5349 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5350 args, tf_error | tf_warning);
5351 redeclare_class_template (TREE_TYPE (tmpl), parms);
5354 friend_type = TREE_TYPE (tmpl);
5356 else
5358 /* The friend template has not already been declared. In this
5359 case, the instantiation of the template class will cause the
5360 injection of this template into the global scope. */
5361 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5363 /* The new TMPL is not an instantiation of anything, so we
5364 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5365 the new type because that is supposed to be the corresponding
5366 template decl, i.e., TMPL. */
5367 DECL_USE_TEMPLATE (tmpl) = 0;
5368 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5369 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5370 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5371 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5373 /* Inject this template into the global scope. */
5374 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5377 if (context)
5379 if (TREE_CODE (context) == NAMESPACE_DECL)
5380 pop_nested_namespace (context);
5381 else
5382 pop_nested_class ();
5385 return friend_type;
5388 /* Returns zero if TYPE cannot be completed later due to circularity.
5389 Otherwise returns one. */
5391 static int
5392 can_complete_type_without_circularity (tree type)
5394 if (type == NULL_TREE || type == error_mark_node)
5395 return 0;
5396 else if (COMPLETE_TYPE_P (type))
5397 return 1;
5398 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5399 return can_complete_type_without_circularity (TREE_TYPE (type));
5400 else if (CLASS_TYPE_P (type)
5401 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5402 return 0;
5403 else
5404 return 1;
5407 tree
5408 instantiate_class_template (tree type)
5410 tree template, args, pattern, t, member;
5411 tree typedecl;
5412 tree pbinfo;
5413 tree base_list;
5415 if (type == error_mark_node)
5416 return error_mark_node;
5418 if (TYPE_BEING_DEFINED (type)
5419 || COMPLETE_TYPE_P (type)
5420 || dependent_type_p (type))
5421 return type;
5423 /* Figure out which template is being instantiated. */
5424 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5425 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5427 /* Figure out which arguments are being used to do the
5428 instantiation. */
5429 args = CLASSTYPE_TI_ARGS (type);
5431 /* Determine what specialization of the original template to
5432 instantiate. */
5433 t = most_specialized_class (template, args);
5434 if (t == error_mark_node)
5436 const char *str = "candidates are:";
5437 error ("ambiguous class template instantiation for %q#T", type);
5438 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5439 t = TREE_CHAIN (t))
5441 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
5443 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5444 str = " ";
5447 TYPE_BEING_DEFINED (type) = 1;
5448 return error_mark_node;
5451 if (t)
5452 pattern = TREE_TYPE (t);
5453 else
5454 pattern = TREE_TYPE (template);
5456 /* If the template we're instantiating is incomplete, then clearly
5457 there's nothing we can do. */
5458 if (!COMPLETE_TYPE_P (pattern))
5459 return type;
5461 /* If we've recursively instantiated too many templates, stop. */
5462 if (! push_tinst_level (type))
5463 return type;
5465 /* Now we're really doing the instantiation. Mark the type as in
5466 the process of being defined. */
5467 TYPE_BEING_DEFINED (type) = 1;
5469 /* We may be in the middle of deferred access check. Disable
5470 it now. */
5471 push_deferring_access_checks (dk_no_deferred);
5473 push_to_top_level ();
5475 if (t)
5477 /* This TYPE is actually an instantiation of a partial
5478 specialization. We replace the innermost set of ARGS with
5479 the arguments appropriate for substitution. For example,
5480 given:
5482 template <class T> struct S {};
5483 template <class T> struct S<T*> {};
5485 and supposing that we are instantiating S<int*>, ARGS will
5486 present be {int*} but we need {int}. */
5487 tree inner_args
5488 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5489 args);
5491 /* If there were multiple levels in ARGS, replacing the
5492 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5493 want, so we make a copy first. */
5494 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5496 args = copy_node (args);
5497 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5499 else
5500 args = inner_args;
5503 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5505 /* Set the input location to the template definition. This is needed
5506 if tsubsting causes an error. */
5507 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (pattern));
5509 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5510 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5511 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5512 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5513 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5514 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5515 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5516 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5517 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5518 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5519 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5520 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5521 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5522 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5523 if (ANON_AGGR_TYPE_P (pattern))
5524 SET_ANON_AGGR_TYPE_P (type);
5526 pbinfo = TYPE_BINFO (pattern);
5528 /* We should never instantiate a nested class before its enclosing
5529 class; we need to look up the nested class by name before we can
5530 instantiate it, and that lookup should instantiate the enclosing
5531 class. */
5532 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5533 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5534 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5536 base_list = NULL_TREE;
5537 if (BINFO_N_BASE_BINFOS (pbinfo))
5539 tree pbase_binfo;
5540 tree context = TYPE_CONTEXT (type);
5541 tree pushed_scope;
5542 int i;
5544 /* We must enter the scope containing the type, as that is where
5545 the accessibility of types named in dependent bases are
5546 looked up from. */
5547 pushed_scope = push_scope (context ? context : global_namespace);
5549 /* Substitute into each of the bases to determine the actual
5550 basetypes. */
5551 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5553 tree base;
5554 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5556 /* Substitute to figure out the base class. */
5557 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5558 if (base == error_mark_node)
5559 continue;
5561 base_list = tree_cons (access, base, base_list);
5562 if (BINFO_VIRTUAL_P (pbase_binfo))
5563 TREE_TYPE (base_list) = integer_type_node;
5566 /* The list is now in reverse order; correct that. */
5567 base_list = nreverse (base_list);
5569 if (pushed_scope)
5570 pop_scope (pushed_scope);
5572 /* Now call xref_basetypes to set up all the base-class
5573 information. */
5574 xref_basetypes (type, base_list);
5577 /* Now that our base classes are set up, enter the scope of the
5578 class, so that name lookups into base classes, etc. will work
5579 correctly. This is precisely analogous to what we do in
5580 begin_class_definition when defining an ordinary non-template
5581 class. */
5582 pushclass (type);
5584 /* Now members are processed in the order of declaration. */
5585 for (member = CLASSTYPE_DECL_LIST (pattern);
5586 member; member = TREE_CHAIN (member))
5588 tree t = TREE_VALUE (member);
5590 if (TREE_PURPOSE (member))
5592 if (TYPE_P (t))
5594 /* Build new CLASSTYPE_NESTED_UTDS. */
5596 tree tag = t;
5597 tree name = TYPE_IDENTIFIER (tag);
5598 tree newtag;
5599 bool class_template_p;
5601 class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE
5602 && TYPE_LANG_SPECIFIC (tag)
5603 && CLASSTYPE_IS_TEMPLATE (tag));
5604 /* If the member is a class template, then -- even after
5605 substitution -- there may be dependent types in the
5606 template argument list for the class. We increment
5607 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5608 that function will assume that no types are dependent
5609 when outside of a template. */
5610 if (class_template_p)
5611 ++processing_template_decl;
5612 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5613 if (class_template_p)
5614 --processing_template_decl;
5615 if (newtag == error_mark_node)
5616 continue;
5618 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5620 if (class_template_p)
5621 /* Unfortunately, lookup_template_class sets
5622 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5623 instantiation (i.e., for the type of a member
5624 template class nested within a template class.)
5625 This behavior is required for
5626 maybe_process_partial_specialization to work
5627 correctly, but is not accurate in this case;
5628 the TAG is not an instantiation of anything.
5629 (The corresponding TEMPLATE_DECL is an
5630 instantiation, but the TYPE is not.) */
5631 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5633 /* Now, we call pushtag to put this NEWTAG into the scope of
5634 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5635 pushtag calling push_template_decl. We don't have to do
5636 this for enums because it will already have been done in
5637 tsubst_enum. */
5638 if (name)
5639 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5640 pushtag (name, newtag, /*tag_scope=*/ts_current);
5643 else if (TREE_CODE (t) == FUNCTION_DECL
5644 || DECL_FUNCTION_TEMPLATE_P (t))
5646 /* Build new TYPE_METHODS. */
5647 tree r;
5649 if (TREE_CODE (t) == TEMPLATE_DECL)
5650 ++processing_template_decl;
5651 r = tsubst (t, args, tf_error, NULL_TREE);
5652 if (TREE_CODE (t) == TEMPLATE_DECL)
5653 --processing_template_decl;
5654 set_current_access_from_decl (r);
5655 grok_special_member_properties (r);
5656 finish_member_declaration (r);
5658 else
5660 /* Build new TYPE_FIELDS. */
5662 if (TREE_CODE (t) != CONST_DECL)
5664 tree r;
5666 /* The the file and line for this declaration, to
5667 assist in error message reporting. Since we
5668 called push_tinst_level above, we don't need to
5669 restore these. */
5670 input_location = DECL_SOURCE_LOCATION (t);
5672 if (TREE_CODE (t) == TEMPLATE_DECL)
5673 ++processing_template_decl;
5674 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5675 if (TREE_CODE (t) == TEMPLATE_DECL)
5676 --processing_template_decl;
5677 if (TREE_CODE (r) == VAR_DECL)
5679 tree init;
5681 if (DECL_INITIALIZED_IN_CLASS_P (r))
5682 init = tsubst_expr (DECL_INITIAL (t), args,
5683 tf_error | tf_warning, NULL_TREE);
5684 else
5685 init = NULL_TREE;
5687 finish_static_data_member_decl
5688 (r, init, /*asmspec_tree=*/NULL_TREE, /*flags=*/0);
5690 if (DECL_INITIALIZED_IN_CLASS_P (r))
5691 check_static_variable_definition (r, TREE_TYPE (r));
5693 else if (TREE_CODE (r) == FIELD_DECL)
5695 /* Determine whether R has a valid type and can be
5696 completed later. If R is invalid, then it is
5697 replaced by error_mark_node so that it will not be
5698 added to TYPE_FIELDS. */
5699 tree rtype = TREE_TYPE (r);
5700 if (can_complete_type_without_circularity (rtype))
5701 complete_type (rtype);
5703 if (!COMPLETE_TYPE_P (rtype))
5705 cxx_incomplete_type_error (r, rtype);
5706 r = error_mark_node;
5710 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5711 such a thing will already have been added to the field
5712 list by tsubst_enum in finish_member_declaration in the
5713 CLASSTYPE_NESTED_UTDS case above. */
5714 if (!(TREE_CODE (r) == TYPE_DECL
5715 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5716 && DECL_ARTIFICIAL (r)))
5718 set_current_access_from_decl (r);
5719 finish_member_declaration (r);
5724 else
5726 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5728 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5730 tree friend_type = t;
5731 bool adjust_processing_template_decl = false;
5733 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5735 /* template <class T> friend class C; */
5736 friend_type = tsubst_friend_class (friend_type, args);
5737 adjust_processing_template_decl = true;
5739 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5741 /* template <class T> friend class C::D; */
5742 friend_type = tsubst (friend_type, args,
5743 tf_error | tf_warning, NULL_TREE);
5744 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5745 friend_type = TREE_TYPE (friend_type);
5746 adjust_processing_template_decl = true;
5748 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5750 /* This could be either
5752 friend class T::C;
5754 when dependent_type_p is false or
5756 template <class U> friend class T::C;
5758 otherwise. */
5759 friend_type = tsubst (friend_type, args,
5760 tf_error | tf_warning, NULL_TREE);
5761 /* Bump processing_template_decl for correct
5762 dependent_type_p calculation. */
5763 ++processing_template_decl;
5764 if (dependent_type_p (friend_type))
5765 adjust_processing_template_decl = true;
5766 --processing_template_decl;
5768 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5769 && hidden_name_p (TYPE_NAME (friend_type)))
5771 /* friend class C;
5773 where C hasn't been declared yet. Let's lookup name
5774 from namespace scope directly, bypassing any name that
5775 come from dependent base class. */
5776 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5778 /* The call to xref_tag_from_type does injection for friend
5779 classes. */
5780 push_nested_namespace (ns);
5781 friend_type =
5782 xref_tag_from_type (friend_type, NULL_TREE,
5783 /*tag_scope=*/ts_current);
5784 pop_nested_namespace (ns);
5786 else if (uses_template_parms (friend_type))
5787 /* friend class C<T>; */
5788 friend_type = tsubst (friend_type, args,
5789 tf_error | tf_warning, NULL_TREE);
5790 /* Otherwise it's
5792 friend class C;
5794 where C is already declared or
5796 friend class C<int>;
5798 We don't have to do anything in these cases. */
5800 if (adjust_processing_template_decl)
5801 /* Trick make_friend_class into realizing that the friend
5802 we're adding is a template, not an ordinary class. It's
5803 important that we use make_friend_class since it will
5804 perform some error-checking and output cross-reference
5805 information. */
5806 ++processing_template_decl;
5808 if (friend_type != error_mark_node)
5809 make_friend_class (type, friend_type, /*complain=*/false);
5811 if (adjust_processing_template_decl)
5812 --processing_template_decl;
5814 else
5816 /* Build new DECL_FRIENDLIST. */
5817 tree r;
5819 /* The the file and line for this declaration, to
5820 assist in error message reporting. Since we
5821 called push_tinst_level above, we don't need to
5822 restore these. */
5823 input_location = DECL_SOURCE_LOCATION (t);
5825 if (TREE_CODE (t) == TEMPLATE_DECL)
5827 ++processing_template_decl;
5828 push_deferring_access_checks (dk_no_check);
5831 r = tsubst_friend_function (t, args);
5832 add_friend (type, r, /*complain=*/false);
5833 if (TREE_CODE (t) == TEMPLATE_DECL)
5835 pop_deferring_access_checks ();
5836 --processing_template_decl;
5842 /* Set the file and line number information to whatever is given for
5843 the class itself. This puts error messages involving generated
5844 implicit functions at a predictable point, and the same point
5845 that would be used for non-template classes. */
5846 typedecl = TYPE_MAIN_DECL (type);
5847 input_location = DECL_SOURCE_LOCATION (typedecl);
5849 unreverse_member_declarations (type);
5850 finish_struct_1 (type);
5851 TYPE_BEING_DEFINED (type) = 0;
5853 /* Now that the class is complete, instantiate default arguments for
5854 any member functions. We don't do this earlier because the
5855 default arguments may reference members of the class. */
5856 if (!PRIMARY_TEMPLATE_P (template))
5857 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5858 if (TREE_CODE (t) == FUNCTION_DECL
5859 /* Implicitly generated member functions will not have template
5860 information; they are not instantiations, but instead are
5861 created "fresh" for each instantiation. */
5862 && DECL_TEMPLATE_INFO (t))
5863 tsubst_default_arguments (t);
5865 popclass ();
5866 pop_from_top_level ();
5867 pop_deferring_access_checks ();
5868 pop_tinst_level ();
5870 /* The vtable for a template class can be emitted in any translation
5871 unit in which the class is instantiated. When there is no key
5872 method, however, finish_struct_1 will already have added TYPE to
5873 the keyed_classes list. */
5874 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
5875 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5877 return type;
5880 static tree
5881 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5883 tree r;
5885 if (!t)
5886 r = t;
5887 else if (TYPE_P (t))
5888 r = tsubst (t, args, complain, in_decl);
5889 else
5891 r = tsubst_expr (t, args, complain, in_decl);
5893 if (!uses_template_parms (r))
5895 /* Sometimes, one of the args was an expression involving a
5896 template constant parameter, like N - 1. Now that we've
5897 tsubst'd, we might have something like 2 - 1. This will
5898 confuse lookup_template_class, so we do constant folding
5899 here. We have to unset processing_template_decl, to fool
5900 tsubst_copy_and_build() into building an actual tree. */
5902 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5903 as simple as it's going to get, and trying to reprocess
5904 the trees will break. Once tsubst_expr et al DTRT for
5905 non-dependent exprs, this code can go away, as the type
5906 will always be set. */
5907 if (!TREE_TYPE (r))
5909 int saved_processing_template_decl = processing_template_decl;
5910 processing_template_decl = 0;
5911 r = tsubst_copy_and_build (r, /*args=*/NULL_TREE,
5912 tf_error, /*in_decl=*/NULL_TREE,
5913 /*function_p=*/false);
5914 processing_template_decl = saved_processing_template_decl;
5916 r = fold (r);
5919 return r;
5922 /* Substitute ARGS into the vector or list of template arguments T. */
5924 static tree
5925 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5927 int len = TREE_VEC_LENGTH (t);
5928 int need_new = 0, i;
5929 tree *elts = alloca (len * sizeof (tree));
5931 for (i = 0; i < len; i++)
5933 tree orig_arg = TREE_VEC_ELT (t, i);
5934 tree new_arg;
5936 if (TREE_CODE (orig_arg) == TREE_VEC)
5937 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5938 else
5939 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
5941 if (new_arg == error_mark_node)
5942 return error_mark_node;
5944 elts[i] = new_arg;
5945 if (new_arg != orig_arg)
5946 need_new = 1;
5949 if (!need_new)
5950 return t;
5952 t = make_tree_vec (len);
5953 for (i = 0; i < len; i++)
5954 TREE_VEC_ELT (t, i) = elts[i];
5956 return t;
5959 /* Return the result of substituting ARGS into the template parameters
5960 given by PARMS. If there are m levels of ARGS and m + n levels of
5961 PARMS, then the result will contain n levels of PARMS. For
5962 example, if PARMS is `template <class T> template <class U>
5963 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5964 result will be `template <int*, double, class V>'. */
5966 static tree
5967 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
5969 tree r = NULL_TREE;
5970 tree* new_parms;
5972 for (new_parms = &r;
5973 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5974 new_parms = &(TREE_CHAIN (*new_parms)),
5975 parms = TREE_CHAIN (parms))
5977 tree new_vec =
5978 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5979 int i;
5981 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5983 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5984 tree default_value = TREE_PURPOSE (tuple);
5985 tree parm_decl = TREE_VALUE (tuple);
5987 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5988 default_value = tsubst_template_arg (default_value, args,
5989 complain, NULL_TREE);
5991 tuple = build_tree_list (default_value, parm_decl);
5992 TREE_VEC_ELT (new_vec, i) = tuple;
5995 *new_parms =
5996 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5997 - TMPL_ARGS_DEPTH (args)),
5998 new_vec, NULL_TREE);
6001 return r;
6004 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6005 type T. If T is not an aggregate or enumeration type, it is
6006 handled as if by tsubst. IN_DECL is as for tsubst. If
6007 ENTERING_SCOPE is nonzero, T is the context for a template which
6008 we are presently tsubst'ing. Return the substituted value. */
6010 static tree
6011 tsubst_aggr_type (tree t,
6012 tree args,
6013 tsubst_flags_t complain,
6014 tree in_decl,
6015 int entering_scope)
6017 if (t == NULL_TREE)
6018 return NULL_TREE;
6020 switch (TREE_CODE (t))
6022 case RECORD_TYPE:
6023 if (TYPE_PTRMEMFUNC_P (t))
6024 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6026 /* Else fall through. */
6027 case ENUMERAL_TYPE:
6028 case UNION_TYPE:
6029 if (TYPE_TEMPLATE_INFO (t))
6031 tree argvec;
6032 tree context;
6033 tree r;
6035 /* First, determine the context for the type we are looking
6036 up. */
6037 context = TYPE_CONTEXT (t);
6038 if (context)
6039 context = tsubst_aggr_type (context, args, complain,
6040 in_decl, /*entering_scope=*/1);
6042 /* Then, figure out what arguments are appropriate for the
6043 type we are trying to find. For example, given:
6045 template <class T> struct S;
6046 template <class T, class U> void f(T, U) { S<U> su; }
6048 and supposing that we are instantiating f<int, double>,
6049 then our ARGS will be {int, double}, but, when looking up
6050 S we only want {double}. */
6051 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6052 complain, in_decl);
6053 if (argvec == error_mark_node)
6054 return error_mark_node;
6056 r = lookup_template_class (t, argvec, in_decl, context,
6057 entering_scope, complain);
6059 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6061 else
6062 /* This is not a template type, so there's nothing to do. */
6063 return t;
6065 default:
6066 return tsubst (t, args, complain, in_decl);
6070 /* Substitute into the default argument ARG (a default argument for
6071 FN), which has the indicated TYPE. */
6073 tree
6074 tsubst_default_argument (tree fn, tree type, tree arg)
6076 tree saved_class_ptr = NULL_TREE;
6077 tree saved_class_ref = NULL_TREE;
6079 /* This default argument came from a template. Instantiate the
6080 default argument here, not in tsubst. In the case of
6081 something like:
6083 template <class T>
6084 struct S {
6085 static T t();
6086 void f(T = t());
6089 we must be careful to do name lookup in the scope of S<T>,
6090 rather than in the current class. */
6091 push_access_scope (fn);
6092 /* The default argument expression should not be considered to be
6093 within the scope of FN. Since push_access_scope sets
6094 current_function_decl, we must explicitly clear it here. */
6095 current_function_decl = NULL_TREE;
6096 /* The "this" pointer is not valid in a default argument. */
6097 if (cfun)
6099 saved_class_ptr = current_class_ptr;
6100 cp_function_chain->x_current_class_ptr = NULL_TREE;
6101 saved_class_ref = current_class_ref;
6102 cp_function_chain->x_current_class_ref = NULL_TREE;
6105 push_deferring_access_checks(dk_no_deferred);
6106 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6107 tf_error | tf_warning, NULL_TREE);
6108 pop_deferring_access_checks();
6110 /* Restore the "this" pointer. */
6111 if (cfun)
6113 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6114 cp_function_chain->x_current_class_ref = saved_class_ref;
6117 pop_access_scope (fn);
6119 /* Make sure the default argument is reasonable. */
6120 arg = check_default_argument (type, arg);
6122 return arg;
6125 /* Substitute into all the default arguments for FN. */
6127 static void
6128 tsubst_default_arguments (tree fn)
6130 tree arg;
6131 tree tmpl_args;
6133 tmpl_args = DECL_TI_ARGS (fn);
6135 /* If this function is not yet instantiated, we certainly don't need
6136 its default arguments. */
6137 if (uses_template_parms (tmpl_args))
6138 return;
6140 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6141 arg;
6142 arg = TREE_CHAIN (arg))
6143 if (TREE_PURPOSE (arg))
6144 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6145 TREE_VALUE (arg),
6146 TREE_PURPOSE (arg));
6149 /* Substitute the ARGS into the T, which is a _DECL. Return the
6150 result of the substitution. Issue error and warning messages under
6151 control of COMPLAIN. */
6153 static tree
6154 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6156 location_t saved_loc;
6157 tree r = NULL_TREE;
6158 tree in_decl = t;
6160 /* Set the filename and linenumber to improve error-reporting. */
6161 saved_loc = input_location;
6162 input_location = DECL_SOURCE_LOCATION (t);
6164 switch (TREE_CODE (t))
6166 case TEMPLATE_DECL:
6168 /* We can get here when processing a member function template,
6169 member class template, and template template parameter of
6170 a template class. */
6171 tree decl = DECL_TEMPLATE_RESULT (t);
6172 tree spec;
6173 tree tmpl_args;
6174 tree full_args;
6176 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6178 /* Template template parameter is treated here. */
6179 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6180 if (new_type == error_mark_node)
6181 return error_mark_node;
6183 r = copy_decl (t);
6184 TREE_CHAIN (r) = NULL_TREE;
6185 TREE_TYPE (r) = new_type;
6186 DECL_TEMPLATE_RESULT (r)
6187 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6188 DECL_TEMPLATE_PARMS (r)
6189 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6190 complain);
6191 TYPE_NAME (new_type) = r;
6192 break;
6195 /* We might already have an instance of this template.
6196 The ARGS are for the surrounding class type, so the
6197 full args contain the tsubst'd args for the context,
6198 plus the innermost args from the template decl. */
6199 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6200 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6201 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6202 full_args = tsubst_template_args (tmpl_args, args,
6203 complain, in_decl);
6205 /* tsubst_template_args doesn't copy the vector if
6206 nothing changed. But, *something* should have
6207 changed. */
6208 gcc_assert (full_args != tmpl_args);
6210 spec = retrieve_specialization (t, full_args,
6211 /*class_specializations_p=*/true);
6212 if (spec != NULL_TREE)
6214 r = spec;
6215 break;
6218 /* Make a new template decl. It will be similar to the
6219 original, but will record the current template arguments.
6220 We also create a new function declaration, which is just
6221 like the old one, but points to this new template, rather
6222 than the old one. */
6223 r = copy_decl (t);
6224 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6225 TREE_CHAIN (r) = NULL_TREE;
6227 DECL_CONTEXT (r)
6228 = tsubst_aggr_type (DECL_CONTEXT (t), args,
6229 complain, in_decl,
6230 /*entering_scope=*/1);
6231 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6233 if (TREE_CODE (decl) == TYPE_DECL)
6235 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6236 if (new_type == error_mark_node)
6237 return error_mark_node;
6239 TREE_TYPE (r) = new_type;
6240 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6241 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6242 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6244 else
6246 tree new_decl = tsubst (decl, args, complain, in_decl);
6247 if (new_decl == error_mark_node)
6248 return error_mark_node;
6250 DECL_TEMPLATE_RESULT (r) = new_decl;
6251 DECL_TI_TEMPLATE (new_decl) = r;
6252 TREE_TYPE (r) = TREE_TYPE (new_decl);
6253 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6256 SET_DECL_IMPLICIT_INSTANTIATION (r);
6257 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6258 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6260 /* The template parameters for this new template are all the
6261 template parameters for the old template, except the
6262 outermost level of parameters. */
6263 DECL_TEMPLATE_PARMS (r)
6264 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6265 complain);
6267 if (PRIMARY_TEMPLATE_P (t))
6268 DECL_PRIMARY_TEMPLATE (r) = r;
6270 if (TREE_CODE (decl) != TYPE_DECL)
6271 /* Record this non-type partial instantiation. */
6272 register_specialization (r, t,
6273 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
6275 break;
6277 case FUNCTION_DECL:
6279 tree ctx;
6280 tree argvec = NULL_TREE;
6281 tree *friends;
6282 tree gen_tmpl;
6283 tree type;
6284 int member;
6285 int args_depth;
6286 int parms_depth;
6288 /* Nobody should be tsubst'ing into non-template functions. */
6289 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6291 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6293 tree spec;
6294 bool dependent_p;
6296 /* If T is not dependent, just return it. We have to
6297 increment PROCESSING_TEMPLATE_DECL because
6298 value_dependent_expression_p assumes that nothing is
6299 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6300 ++processing_template_decl;
6301 dependent_p = value_dependent_expression_p (t);
6302 --processing_template_decl;
6303 if (!dependent_p)
6304 return t;
6306 /* Calculate the most general template of which R is a
6307 specialization, and the complete set of arguments used to
6308 specialize R. */
6309 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6310 argvec = tsubst_template_args (DECL_TI_ARGS
6311 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6312 args, complain, in_decl);
6314 /* Check to see if we already have this specialization. */
6315 spec = retrieve_specialization (gen_tmpl, argvec,
6316 /*class_specializations_p=*/false);
6318 if (spec)
6320 r = spec;
6321 break;
6324 /* We can see more levels of arguments than parameters if
6325 there was a specialization of a member template, like
6326 this:
6328 template <class T> struct S { template <class U> void f(); }
6329 template <> template <class U> void S<int>::f(U);
6331 Here, we'll be substituting into the specialization,
6332 because that's where we can find the code we actually
6333 want to generate, but we'll have enough arguments for
6334 the most general template.
6336 We also deal with the peculiar case:
6338 template <class T> struct S {
6339 template <class U> friend void f();
6341 template <class U> void f() {}
6342 template S<int>;
6343 template void f<double>();
6345 Here, the ARGS for the instantiation of will be {int,
6346 double}. But, we only need as many ARGS as there are
6347 levels of template parameters in CODE_PATTERN. We are
6348 careful not to get fooled into reducing the ARGS in
6349 situations like:
6351 template <class T> struct S { template <class U> void f(U); }
6352 template <class T> template <> void S<T>::f(int) {}
6354 which we can spot because the pattern will be a
6355 specialization in this case. */
6356 args_depth = TMPL_ARGS_DEPTH (args);
6357 parms_depth =
6358 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6359 if (args_depth > parms_depth
6360 && !DECL_TEMPLATE_SPECIALIZATION (t))
6361 args = get_innermost_template_args (args, parms_depth);
6363 else
6365 /* This special case arises when we have something like this:
6367 template <class T> struct S {
6368 friend void f<int>(int, double);
6371 Here, the DECL_TI_TEMPLATE for the friend declaration
6372 will be an IDENTIFIER_NODE. We are being called from
6373 tsubst_friend_function, and we want only to create a
6374 new decl (R) with appropriate types so that we can call
6375 determine_specialization. */
6376 gen_tmpl = NULL_TREE;
6379 if (DECL_CLASS_SCOPE_P (t))
6381 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6382 member = 2;
6383 else
6384 member = 1;
6385 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6386 complain, t, /*entering_scope=*/1);
6388 else
6390 member = 0;
6391 ctx = DECL_CONTEXT (t);
6393 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6394 if (type == error_mark_node)
6395 return error_mark_node;
6397 /* We do NOT check for matching decls pushed separately at this
6398 point, as they may not represent instantiations of this
6399 template, and in any case are considered separate under the
6400 discrete model. */
6401 r = copy_decl (t);
6402 DECL_USE_TEMPLATE (r) = 0;
6403 TREE_TYPE (r) = type;
6404 /* Clear out the mangled name and RTL for the instantiation. */
6405 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6406 SET_DECL_RTL (r, NULL_RTX);
6407 DECL_INITIAL (r) = NULL_TREE;
6408 DECL_CONTEXT (r) = ctx;
6410 if (member && DECL_CONV_FN_P (r))
6411 /* Type-conversion operator. Reconstruct the name, in
6412 case it's the name of one of the template's parameters. */
6413 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6415 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6416 complain, t);
6417 DECL_RESULT (r) = NULL_TREE;
6419 TREE_STATIC (r) = 0;
6420 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6421 DECL_EXTERNAL (r) = 1;
6422 /* If this is an instantiation of a function with internal
6423 linkage, we already know what object file linkage will be
6424 assigned to the instantiation. */
6425 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6426 DECL_DEFER_OUTPUT (r) = 0;
6427 TREE_CHAIN (r) = NULL_TREE;
6428 DECL_PENDING_INLINE_INFO (r) = 0;
6429 DECL_PENDING_INLINE_P (r) = 0;
6430 DECL_SAVED_TREE (r) = NULL_TREE;
6431 TREE_USED (r) = 0;
6432 if (DECL_CLONED_FUNCTION (r))
6434 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6435 args, complain, t);
6436 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6437 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6440 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6441 this in the special friend case mentioned above where
6442 GEN_TMPL is NULL. */
6443 if (gen_tmpl)
6445 DECL_TEMPLATE_INFO (r)
6446 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6447 SET_DECL_IMPLICIT_INSTANTIATION (r);
6448 register_specialization (r, gen_tmpl, argvec);
6450 /* We're not supposed to instantiate default arguments
6451 until they are called, for a template. But, for a
6452 declaration like:
6454 template <class T> void f ()
6455 { extern void g(int i = T()); }
6457 we should do the substitution when the template is
6458 instantiated. We handle the member function case in
6459 instantiate_class_template since the default arguments
6460 might refer to other members of the class. */
6461 if (!member
6462 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6463 && !uses_template_parms (argvec))
6464 tsubst_default_arguments (r);
6467 /* Copy the list of befriending classes. */
6468 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6469 *friends;
6470 friends = &TREE_CHAIN (*friends))
6472 *friends = copy_node (*friends);
6473 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6474 args, complain,
6475 in_decl);
6478 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6480 maybe_retrofit_in_chrg (r);
6481 if (DECL_CONSTRUCTOR_P (r))
6482 grok_ctor_properties (ctx, r);
6483 /* If this is an instantiation of a member template, clone it.
6484 If it isn't, that'll be handled by
6485 clone_constructors_and_destructors. */
6486 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6487 clone_function_decl (r, /*update_method_vec_p=*/0);
6489 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6490 grok_op_properties (r, DECL_FRIEND_P (r),
6491 (complain & tf_error) != 0);
6493 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6494 SET_DECL_FRIEND_CONTEXT (r,
6495 tsubst (DECL_FRIEND_CONTEXT (t),
6496 args, complain, in_decl));
6498 break;
6500 case PARM_DECL:
6502 tree type;
6504 r = copy_node (t);
6505 if (DECL_TEMPLATE_PARM_P (t))
6506 SET_DECL_TEMPLATE_PARM_P (r);
6508 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6509 type = type_decays_to (type);
6510 TREE_TYPE (r) = type;
6511 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6513 if (DECL_INITIAL (r))
6515 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6516 DECL_INITIAL (r) = TREE_TYPE (r);
6517 else
6518 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6519 complain, in_decl);
6522 DECL_CONTEXT (r) = NULL_TREE;
6524 if (!DECL_TEMPLATE_PARM_P (r))
6525 DECL_ARG_TYPE (r) = type_passed_as (type);
6526 if (TREE_CHAIN (t))
6527 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6528 complain, TREE_CHAIN (t));
6530 break;
6532 case FIELD_DECL:
6534 tree type;
6536 r = copy_decl (t);
6537 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6538 if (type == error_mark_node)
6539 return error_mark_node;
6540 TREE_TYPE (r) = type;
6541 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6543 /* We don't have to set DECL_CONTEXT here; it is set by
6544 finish_member_declaration. */
6545 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6546 complain, in_decl);
6547 TREE_CHAIN (r) = NULL_TREE;
6548 if (VOID_TYPE_P (type))
6549 cp_error_at ("instantiation of %qD as type %qT", r, type);
6551 break;
6553 case USING_DECL:
6555 r = copy_node (t);
6556 /* It is not a dependent using decl any more. */
6557 TREE_TYPE (r) = void_type_node;
6558 DECL_INITIAL (r)
6559 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6560 DECL_NAME (r)
6561 = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
6562 TREE_CHAIN (r) = NULL_TREE;
6564 break;
6566 case TYPE_DECL:
6567 case VAR_DECL:
6569 tree argvec = NULL_TREE;
6570 tree gen_tmpl = NULL_TREE;
6571 tree spec;
6572 tree tmpl = NULL_TREE;
6573 tree ctx;
6574 tree type = NULL_TREE;
6575 int local_p;
6577 if (TREE_CODE (t) == TYPE_DECL)
6579 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6580 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6581 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6583 /* If this is the canonical decl, we don't have to
6584 mess with instantiations, and often we can't (for
6585 typename, template type parms and such). Note that
6586 TYPE_NAME is not correct for the above test if
6587 we've copied the type for a typedef. */
6588 r = TYPE_NAME (type);
6589 break;
6593 /* Assume this is a non-local variable. */
6594 local_p = 0;
6596 if (TYPE_P (CP_DECL_CONTEXT (t)))
6597 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6598 complain,
6599 in_decl, /*entering_scope=*/1);
6600 else if (DECL_NAMESPACE_SCOPE_P (t))
6601 ctx = DECL_CONTEXT (t);
6602 else
6604 /* Subsequent calls to pushdecl will fill this in. */
6605 ctx = NULL_TREE;
6606 local_p = 1;
6609 /* Check to see if we already have this specialization. */
6610 if (!local_p)
6612 tmpl = DECL_TI_TEMPLATE (t);
6613 gen_tmpl = most_general_template (tmpl);
6614 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6615 spec = retrieve_specialization (gen_tmpl, argvec,
6616 /*class_specializations_p=*/false);
6618 else
6619 spec = retrieve_local_specialization (t);
6621 if (spec)
6623 r = spec;
6624 break;
6627 r = copy_decl (t);
6628 if (TREE_CODE (r) == VAR_DECL)
6630 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6631 if (type == error_mark_node)
6632 return error_mark_node;
6633 type = complete_type (type);
6634 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6635 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6636 type = check_var_type (DECL_NAME (r), type);
6638 else if (DECL_SELF_REFERENCE_P (t))
6639 SET_DECL_SELF_REFERENCE_P (r);
6640 TREE_TYPE (r) = type;
6641 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6642 DECL_CONTEXT (r) = ctx;
6643 /* Clear out the mangled name and RTL for the instantiation. */
6644 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6645 SET_DECL_RTL (r, NULL_RTX);
6647 /* Don't try to expand the initializer until someone tries to use
6648 this variable; otherwise we run into circular dependencies. */
6649 DECL_INITIAL (r) = NULL_TREE;
6650 SET_DECL_RTL (r, NULL_RTX);
6651 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6653 /* Even if the original location is out of scope, the newly
6654 substituted one is not. */
6655 if (TREE_CODE (r) == VAR_DECL)
6657 DECL_DEAD_FOR_LOCAL (r) = 0;
6658 DECL_INITIALIZED_P (r) = 0;
6661 if (!local_p)
6663 /* A static data member declaration is always marked
6664 external when it is declared in-class, even if an
6665 initializer is present. We mimic the non-template
6666 processing here. */
6667 DECL_EXTERNAL (r) = 1;
6669 register_specialization (r, gen_tmpl, argvec);
6670 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6671 SET_DECL_IMPLICIT_INSTANTIATION (r);
6673 else
6674 register_local_specialization (r, t);
6676 TREE_CHAIN (r) = NULL_TREE;
6677 layout_decl (r, 0);
6679 break;
6681 default:
6682 gcc_unreachable ();
6685 /* Restore the file and line information. */
6686 input_location = saved_loc;
6688 return r;
6691 /* Substitute into the ARG_TYPES of a function type. */
6693 static tree
6694 tsubst_arg_types (tree arg_types,
6695 tree args,
6696 tsubst_flags_t complain,
6697 tree in_decl)
6699 tree remaining_arg_types;
6700 tree type;
6702 if (!arg_types || arg_types == void_list_node)
6703 return arg_types;
6705 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6706 args, complain, in_decl);
6707 if (remaining_arg_types == error_mark_node)
6708 return error_mark_node;
6710 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6711 if (type == error_mark_node)
6712 return error_mark_node;
6713 if (VOID_TYPE_P (type))
6715 if (complain & tf_error)
6717 error ("invalid parameter type %qT", type);
6718 if (in_decl)
6719 cp_error_at ("in declaration %qD", in_decl);
6721 return error_mark_node;
6724 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6725 top-level qualifiers as required. */
6726 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6728 /* Note that we do not substitute into default arguments here. The
6729 standard mandates that they be instantiated only when needed,
6730 which is done in build_over_call. */
6731 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6732 remaining_arg_types);
6736 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6737 *not* handle the exception-specification for FNTYPE, because the
6738 initial substitution of explicitly provided template parameters
6739 during argument deduction forbids substitution into the
6740 exception-specification:
6742 [temp.deduct]
6744 All references in the function type of the function template to the
6745 corresponding template parameters are replaced by the specified tem-
6746 plate argument values. If a substitution in a template parameter or
6747 in the function type of the function template results in an invalid
6748 type, type deduction fails. [Note: The equivalent substitution in
6749 exception specifications is done only when the function is instanti-
6750 ated, at which point a program is ill-formed if the substitution
6751 results in an invalid type.] */
6753 static tree
6754 tsubst_function_type (tree t,
6755 tree args,
6756 tsubst_flags_t complain,
6757 tree in_decl)
6759 tree return_type;
6760 tree arg_types;
6761 tree fntype;
6763 /* The TYPE_CONTEXT is not used for function/method types. */
6764 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
6766 /* Substitute the return type. */
6767 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6768 if (return_type == error_mark_node)
6769 return error_mark_node;
6770 /* The standard does not presently indicate that creation of a
6771 function type with an invalid return type is a deduction failure.
6772 However, that is clearly analogous to creating an array of "void"
6773 or a reference to a reference. This is core issue #486. */
6774 if (TREE_CODE (return_type) == ARRAY_TYPE
6775 || TREE_CODE (return_type) == FUNCTION_TYPE)
6777 if (complain & tf_error)
6779 if (TREE_CODE (return_type) == ARRAY_TYPE)
6780 error ("function returning an array");
6781 else
6782 error ("function returning a function");
6784 return error_mark_node;
6787 /* Substitute the argument types. */
6788 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6789 complain, in_decl);
6790 if (arg_types == error_mark_node)
6791 return error_mark_node;
6793 /* Construct a new type node and return it. */
6794 if (TREE_CODE (t) == FUNCTION_TYPE)
6795 fntype = build_function_type (return_type, arg_types);
6796 else
6798 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6799 if (! IS_AGGR_TYPE (r))
6801 /* [temp.deduct]
6803 Type deduction may fail for any of the following
6804 reasons:
6806 -- Attempting to create "pointer to member of T" when T
6807 is not a class type. */
6808 if (complain & tf_error)
6809 error ("creating pointer to member function of non-class type %qT",
6811 return error_mark_node;
6814 fntype = build_method_type_directly (r, return_type,
6815 TREE_CHAIN (arg_types));
6817 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6818 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6820 return fntype;
6823 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
6824 ARGS into that specification, and return the substituted
6825 specification. If there is no specification, return NULL_TREE. */
6827 static tree
6828 tsubst_exception_specification (tree fntype,
6829 tree args,
6830 tsubst_flags_t complain,
6831 tree in_decl)
6833 tree specs;
6834 tree new_specs;
6836 specs = TYPE_RAISES_EXCEPTIONS (fntype);
6837 new_specs = NULL_TREE;
6838 if (specs)
6840 if (! TREE_VALUE (specs))
6841 new_specs = specs;
6842 else
6843 while (specs)
6845 tree spec;
6846 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
6847 if (spec == error_mark_node)
6848 return spec;
6849 new_specs = add_exception_specifier (new_specs, spec, complain);
6850 specs = TREE_CHAIN (specs);
6853 return new_specs;
6856 /* Substitute into the PARMS of a call-declarator. */
6858 static tree
6859 tsubst_call_declarator_parms (tree parms,
6860 tree args,
6861 tsubst_flags_t complain,
6862 tree in_decl)
6864 tree new_parms;
6865 tree type;
6866 tree defarg;
6868 if (!parms || parms == void_list_node)
6869 return parms;
6871 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6872 args, complain, in_decl);
6874 /* Figure out the type of this parameter. */
6875 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6877 /* Figure out the default argument as well. Note that we use
6878 tsubst_expr since the default argument is really an expression. */
6879 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6881 /* Chain this parameter on to the front of those we have already
6882 processed. We don't use hash_tree_cons because that function
6883 doesn't check TREE_PARMLIST. */
6884 new_parms = tree_cons (defarg, type, new_parms);
6886 return new_parms;
6889 /* Take the tree structure T and replace template parameters used
6890 therein with the argument vector ARGS. IN_DECL is an associated
6891 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6892 Issue error and warning messages under control of COMPLAIN. Note
6893 that we must be relatively non-tolerant of extensions here, in
6894 order to preserve conformance; if we allow substitutions that
6895 should not be allowed, we may allow argument deductions that should
6896 not succeed, and therefore report ambiguous overload situations
6897 where there are none. In theory, we could allow the substitution,
6898 but indicate that it should have failed, and allow our caller to
6899 make sure that the right thing happens, but we don't try to do this
6900 yet.
6902 This function is used for dealing with types, decls and the like;
6903 for expressions, use tsubst_expr or tsubst_copy. */
6905 static tree
6906 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6908 tree type, r;
6910 if (t == NULL_TREE || t == error_mark_node
6911 || t == integer_type_node
6912 || t == void_type_node
6913 || t == char_type_node
6914 || t == unknown_type_node
6915 || TREE_CODE (t) == NAMESPACE_DECL)
6916 return t;
6918 if (DECL_P (t))
6919 return tsubst_decl (t, args, complain);
6921 if (TREE_CODE (t) == IDENTIFIER_NODE)
6922 type = IDENTIFIER_TYPE_VALUE (t);
6923 else
6924 type = TREE_TYPE (t);
6926 gcc_assert (type != unknown_type_node);
6928 if (type
6929 && TREE_CODE (t) != TYPENAME_TYPE
6930 && TREE_CODE (t) != IDENTIFIER_NODE
6931 && TREE_CODE (t) != FUNCTION_TYPE
6932 && TREE_CODE (t) != METHOD_TYPE)
6933 type = tsubst (type, args, complain, in_decl);
6934 if (type == error_mark_node)
6935 return error_mark_node;
6937 switch (TREE_CODE (t))
6939 case RECORD_TYPE:
6940 case UNION_TYPE:
6941 case ENUMERAL_TYPE:
6942 return tsubst_aggr_type (t, args, complain, in_decl,
6943 /*entering_scope=*/0);
6945 case ERROR_MARK:
6946 case IDENTIFIER_NODE:
6947 case VOID_TYPE:
6948 case REAL_TYPE:
6949 case COMPLEX_TYPE:
6950 case VECTOR_TYPE:
6951 case BOOLEAN_TYPE:
6952 case INTEGER_CST:
6953 case REAL_CST:
6954 case STRING_CST:
6955 return t;
6957 case INTEGER_TYPE:
6958 if (t == integer_type_node)
6959 return t;
6961 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6962 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6963 return t;
6966 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6968 /* The array dimension behaves like a non-type template arg,
6969 in that we want to fold it as much as possible. */
6970 max = tsubst_template_arg (omax, args, complain, in_decl);
6971 max = fold_decl_constant_value (max);
6973 if (integer_zerop (omax))
6975 /* Still allow an explicit array of size zero. */
6976 if (pedantic)
6977 pedwarn ("creating array with size zero");
6979 else if (integer_zerop (max)
6980 || (TREE_CODE (max) == INTEGER_CST
6981 && INT_CST_LT (max, integer_zero_node)))
6983 /* [temp.deduct]
6985 Type deduction may fail for any of the following
6986 reasons:
6988 Attempting to create an array with a size that is
6989 zero or negative. */
6990 if (complain & tf_error)
6991 error ("creating array with size zero (%qE)", max);
6993 return error_mark_node;
6996 return compute_array_index_type (NULL_TREE, max);
6999 case TEMPLATE_TYPE_PARM:
7000 case TEMPLATE_TEMPLATE_PARM:
7001 case BOUND_TEMPLATE_TEMPLATE_PARM:
7002 case TEMPLATE_PARM_INDEX:
7004 int idx;
7005 int level;
7006 int levels;
7007 tree arg = NULL_TREE;
7009 r = NULL_TREE;
7011 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7012 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7013 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7014 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7016 idx = TEMPLATE_TYPE_IDX (t);
7017 level = TEMPLATE_TYPE_LEVEL (t);
7019 else
7021 idx = TEMPLATE_PARM_IDX (t);
7022 level = TEMPLATE_PARM_LEVEL (t);
7025 levels = TMPL_ARGS_DEPTH (args);
7026 if (level <= levels)
7027 arg = TMPL_ARG (args, level, idx);
7029 if (arg == error_mark_node)
7030 return error_mark_node;
7031 else if (arg != NULL_TREE)
7033 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7035 gcc_assert (TYPE_P (arg));
7036 return cp_build_qualified_type_real
7037 (arg, cp_type_quals (arg) | cp_type_quals (t),
7038 complain | tf_ignore_bad_quals);
7040 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7042 /* We are processing a type constructed from a
7043 template template parameter. */
7044 tree argvec = tsubst (TYPE_TI_ARGS (t),
7045 args, complain, in_decl);
7046 if (argvec == error_mark_node)
7047 return error_mark_node;
7049 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7050 are resolving nested-types in the signature of a
7051 member function templates. Otherwise ARG is a
7052 TEMPLATE_DECL and is the real template to be
7053 instantiated. */
7054 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7055 arg = TYPE_NAME (arg);
7057 r = lookup_template_class (arg,
7058 argvec, in_decl,
7059 DECL_CONTEXT (arg),
7060 /*entering_scope=*/0,
7061 complain);
7062 return cp_build_qualified_type_real
7063 (r, TYPE_QUALS (t), complain);
7065 else
7066 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7067 return arg;
7070 if (level == 1)
7071 /* This can happen during the attempted tsubst'ing in
7072 unify. This means that we don't yet have any information
7073 about the template parameter in question. */
7074 return t;
7076 /* If we get here, we must have been looking at a parm for a
7077 more deeply nested template. Make a new version of this
7078 template parameter, but with a lower level. */
7079 switch (TREE_CODE (t))
7081 case TEMPLATE_TYPE_PARM:
7082 case TEMPLATE_TEMPLATE_PARM:
7083 case BOUND_TEMPLATE_TEMPLATE_PARM:
7084 if (cp_type_quals (t))
7086 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7087 r = cp_build_qualified_type_real
7088 (r, cp_type_quals (t),
7089 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7090 ? tf_ignore_bad_quals : 0));
7092 else
7094 r = copy_type (t);
7095 TEMPLATE_TYPE_PARM_INDEX (r)
7096 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7097 r, levels);
7098 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7099 TYPE_MAIN_VARIANT (r) = r;
7100 TYPE_POINTER_TO (r) = NULL_TREE;
7101 TYPE_REFERENCE_TO (r) = NULL_TREE;
7103 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7105 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7106 complain, in_decl);
7107 if (argvec == error_mark_node)
7108 return error_mark_node;
7110 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7111 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7114 break;
7116 case TEMPLATE_PARM_INDEX:
7117 r = reduce_template_parm_level (t, type, levels);
7118 break;
7120 default:
7121 gcc_unreachable ();
7124 return r;
7127 case TREE_LIST:
7129 tree purpose, value, chain;
7131 if (t == void_list_node)
7132 return t;
7134 purpose = TREE_PURPOSE (t);
7135 if (purpose)
7137 purpose = tsubst (purpose, args, complain, in_decl);
7138 if (purpose == error_mark_node)
7139 return error_mark_node;
7141 value = TREE_VALUE (t);
7142 if (value)
7144 value = tsubst (value, args, complain, in_decl);
7145 if (value == error_mark_node)
7146 return error_mark_node;
7148 chain = TREE_CHAIN (t);
7149 if (chain && chain != void_type_node)
7151 chain = tsubst (chain, args, complain, in_decl);
7152 if (chain == error_mark_node)
7153 return error_mark_node;
7155 if (purpose == TREE_PURPOSE (t)
7156 && value == TREE_VALUE (t)
7157 && chain == TREE_CHAIN (t))
7158 return t;
7159 return hash_tree_cons (purpose, value, chain);
7162 case TREE_BINFO:
7163 /* We should never be tsubsting a binfo. */
7164 gcc_unreachable ();
7166 case TREE_VEC:
7167 /* A vector of template arguments. */
7168 gcc_assert (!type);
7169 return tsubst_template_args (t, args, complain, in_decl);
7171 case POINTER_TYPE:
7172 case REFERENCE_TYPE:
7174 enum tree_code code;
7176 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7177 return t;
7179 code = TREE_CODE (t);
7182 /* [temp.deduct]
7184 Type deduction may fail for any of the following
7185 reasons:
7187 -- Attempting to create a pointer to reference type.
7188 -- Attempting to create a reference to a reference type or
7189 a reference to void. */
7190 if (TREE_CODE (type) == REFERENCE_TYPE
7191 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7193 static location_t last_loc;
7195 /* We keep track of the last time we issued this error
7196 message to avoid spewing a ton of messages during a
7197 single bad template instantiation. */
7198 if (complain & tf_error
7199 #ifdef USE_MAPPED_LOCATION
7200 && last_loc != input_location
7201 #else
7202 && (last_loc.line != input_line
7203 || last_loc.file != input_filename)
7204 #endif
7207 if (TREE_CODE (type) == VOID_TYPE)
7208 error ("forming reference to void");
7209 else
7210 error ("forming %s to reference type %qT",
7211 (code == POINTER_TYPE) ? "pointer" : "reference",
7212 type);
7213 last_loc = input_location;
7216 return error_mark_node;
7218 else if (code == POINTER_TYPE)
7220 r = build_pointer_type (type);
7221 if (TREE_CODE (type) == METHOD_TYPE)
7222 r = build_ptrmemfunc_type (r);
7224 else
7225 r = build_reference_type (type);
7226 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7228 if (r != error_mark_node)
7229 /* Will this ever be needed for TYPE_..._TO values? */
7230 layout_type (r);
7232 return r;
7234 case OFFSET_TYPE:
7236 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7237 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7239 /* [temp.deduct]
7241 Type deduction may fail for any of the following
7242 reasons:
7244 -- Attempting to create "pointer to member of T" when T
7245 is not a class type. */
7246 if (complain & tf_error)
7247 error ("creating pointer to member of non-class type %qT", r);
7248 return error_mark_node;
7250 if (TREE_CODE (type) == REFERENCE_TYPE)
7252 if (complain & tf_error)
7253 error ("creating pointer to member reference type %qT", type);
7255 return error_mark_node;
7257 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7258 if (TREE_CODE (type) == FUNCTION_TYPE)
7260 /* The type of the implicit object parameter gets its
7261 cv-qualifiers from the FUNCTION_TYPE. */
7262 tree method_type;
7263 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7264 cp_type_quals (type));
7265 tree memptr;
7266 method_type = build_method_type_directly (this_type,
7267 TREE_TYPE (type),
7268 TYPE_ARG_TYPES (type));
7269 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7270 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7271 complain);
7273 else
7274 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7275 TYPE_QUALS (t),
7276 complain);
7278 case FUNCTION_TYPE:
7279 case METHOD_TYPE:
7281 tree fntype;
7282 tree specs;
7283 fntype = tsubst_function_type (t, args, complain, in_decl);
7284 if (fntype == error_mark_node)
7285 return error_mark_node;
7287 /* Substitute the exception specification. */
7288 specs = tsubst_exception_specification (t, args, complain,
7289 in_decl);
7290 if (specs)
7291 fntype = build_exception_variant (fntype, specs);
7292 return fntype;
7294 case ARRAY_TYPE:
7296 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7297 if (domain == error_mark_node)
7298 return error_mark_node;
7300 /* As an optimization, we avoid regenerating the array type if
7301 it will obviously be the same as T. */
7302 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7303 return t;
7305 /* These checks should match the ones in grokdeclarator.
7307 [temp.deduct]
7309 The deduction may fail for any of the following reasons:
7311 -- Attempting to create an array with an element type that
7312 is void, a function type, or a reference type, or [DR337]
7313 an abstract class type. */
7314 if (TREE_CODE (type) == VOID_TYPE
7315 || TREE_CODE (type) == FUNCTION_TYPE
7316 || TREE_CODE (type) == REFERENCE_TYPE)
7318 if (complain & tf_error)
7319 error ("creating array of %qT", type);
7320 return error_mark_node;
7322 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7324 if (complain & tf_error)
7325 error ("creating array of %qT, which is an abstract class type",
7326 type);
7327 return error_mark_node;
7330 r = build_cplus_array_type (type, domain);
7331 return r;
7334 case PLUS_EXPR:
7335 case MINUS_EXPR:
7337 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7338 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7340 if (e1 == error_mark_node || e2 == error_mark_node)
7341 return error_mark_node;
7343 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7346 case NEGATE_EXPR:
7347 case NOP_EXPR:
7349 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7350 if (e == error_mark_node)
7351 return error_mark_node;
7353 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7356 case TYPENAME_TYPE:
7358 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7359 in_decl, /*entering_scope=*/1);
7360 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7361 complain, in_decl);
7363 if (ctx == error_mark_node || f == error_mark_node)
7364 return error_mark_node;
7366 if (!IS_AGGR_TYPE (ctx))
7368 if (complain & tf_error)
7369 error ("%qT is not a class, struct, or union type", ctx);
7370 return error_mark_node;
7372 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7374 /* Normally, make_typename_type does not require that the CTX
7375 have complete type in order to allow things like:
7377 template <class T> struct S { typename S<T>::X Y; };
7379 But, such constructs have already been resolved by this
7380 point, so here CTX really should have complete type, unless
7381 it's a partial instantiation. */
7382 ctx = complete_type (ctx);
7383 if (!COMPLETE_TYPE_P (ctx))
7385 if (complain & tf_error)
7386 cxx_incomplete_type_error (NULL_TREE, ctx);
7387 return error_mark_node;
7391 f = make_typename_type (ctx, f, typename_type,
7392 (complain & tf_error) | tf_keep_type_decl);
7393 if (f == error_mark_node)
7394 return f;
7395 if (TREE_CODE (f) == TYPE_DECL)
7397 complain |= tf_ignore_bad_quals;
7398 f = TREE_TYPE (f);
7401 if (TREE_CODE (f) != TYPENAME_TYPE)
7403 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7404 error ("%qT resolves to %qT, which is not an enumeration type",
7405 t, f);
7406 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7407 error ("%qT resolves to %qT, which is is not a class type",
7408 t, f);
7411 return cp_build_qualified_type_real
7412 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7415 case UNBOUND_CLASS_TEMPLATE:
7417 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7418 in_decl, /*entering_scope=*/1);
7419 tree name = TYPE_IDENTIFIER (t);
7420 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7422 if (ctx == error_mark_node || name == error_mark_node)
7423 return error_mark_node;
7425 if (parm_list)
7426 parm_list = tsubst_template_parms (parm_list, args, complain);
7427 return make_unbound_class_template (ctx, name, parm_list, complain);
7430 case INDIRECT_REF:
7431 case ADDR_EXPR:
7432 case CALL_EXPR:
7433 gcc_unreachable ();
7435 case ARRAY_REF:
7437 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7438 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7439 if (e1 == error_mark_node || e2 == error_mark_node)
7440 return error_mark_node;
7442 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7445 case SCOPE_REF:
7447 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7448 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7449 if (e1 == error_mark_node || e2 == error_mark_node)
7450 return error_mark_node;
7452 return build_nt (TREE_CODE (t), e1, e2);
7455 case TYPEOF_TYPE:
7457 tree type;
7459 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
7460 complain, in_decl));
7461 return cp_build_qualified_type_real (type,
7462 cp_type_quals (t)
7463 | cp_type_quals (type),
7464 complain);
7467 default:
7468 sorry ("use of %qs in template",
7469 tree_code_name [(int) TREE_CODE (t)]);
7470 return error_mark_node;
7474 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7475 type of the expression on the left-hand side of the "." or "->"
7476 operator. */
7478 static tree
7479 tsubst_baselink (tree baselink, tree object_type,
7480 tree args, tsubst_flags_t complain, tree in_decl)
7482 tree name;
7483 tree qualifying_scope;
7484 tree fns;
7485 tree template_args = 0;
7486 bool template_id_p = false;
7488 /* A baselink indicates a function from a base class. The
7489 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7490 non-dependent types; otherwise, the lookup could not have
7491 succeeded. However, they may indicate bases of the template
7492 class, rather than the instantiated class.
7494 In addition, lookups that were not ambiguous before may be
7495 ambiguous now. Therefore, we perform the lookup again. */
7496 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7497 fns = BASELINK_FUNCTIONS (baselink);
7498 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7500 template_id_p = true;
7501 template_args = TREE_OPERAND (fns, 1);
7502 fns = TREE_OPERAND (fns, 0);
7503 if (template_args)
7504 template_args = tsubst_template_args (template_args, args,
7505 complain, in_decl);
7507 name = DECL_NAME (get_first_fn (fns));
7508 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7510 /* If lookup found a single function, mark it as used at this
7511 point. (If it lookup found multiple functions the one selected
7512 later by overload resolution will be marked as used at that
7513 point.) */
7514 if (BASELINK_P (baselink))
7515 fns = BASELINK_FUNCTIONS (baselink);
7516 if (!template_id_p && !really_overloaded_fn (fns))
7517 mark_used (OVL_CURRENT (fns));
7519 /* Add back the template arguments, if present. */
7520 if (BASELINK_P (baselink) && template_id_p)
7521 BASELINK_FUNCTIONS (baselink)
7522 = build_nt (TEMPLATE_ID_EXPR,
7523 BASELINK_FUNCTIONS (baselink),
7524 template_args);
7526 if (!object_type)
7527 object_type = current_class_type;
7528 return adjust_result_of_qualified_name_lookup (baselink,
7529 qualifying_scope,
7530 object_type);
7533 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7534 true if the qualified-id will be a postfix-expression in-and-of
7535 itself; false if more of the postfix-expression follows the
7536 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7537 of "&". */
7539 static tree
7540 tsubst_qualified_id (tree qualified_id, tree args,
7541 tsubst_flags_t complain, tree in_decl,
7542 bool done, bool address_p)
7544 tree expr;
7545 tree scope;
7546 tree name;
7547 bool is_template;
7548 tree template_args;
7550 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7552 /* Figure out what name to look up. */
7553 name = TREE_OPERAND (qualified_id, 1);
7554 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7556 is_template = true;
7557 template_args = TREE_OPERAND (name, 1);
7558 if (template_args)
7559 template_args = tsubst_template_args (template_args, args,
7560 complain, in_decl);
7561 name = TREE_OPERAND (name, 0);
7563 else
7565 is_template = false;
7566 template_args = NULL_TREE;
7569 /* Substitute into the qualifying scope. When there are no ARGS, we
7570 are just trying to simplify a non-dependent expression. In that
7571 case the qualifying scope may be dependent, and, in any case,
7572 substituting will not help. */
7573 scope = TREE_OPERAND (qualified_id, 0);
7574 if (args)
7576 scope = tsubst (scope, args, complain, in_decl);
7577 expr = tsubst_copy (name, args, complain, in_decl);
7579 else
7580 expr = name;
7582 if (dependent_type_p (scope))
7583 return build_nt (SCOPE_REF, scope, expr);
7585 if (!BASELINK_P (name) && !DECL_P (expr))
7587 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7588 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7589 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7591 if (complain & tf_error)
7593 error ("dependent-name %qE is parsed as a non-type, but "
7594 "instantiation yields a type", qualified_id);
7595 inform ("say %<typename %E%> if a type is meant", qualified_id);
7597 return error_mark_node;
7601 if (DECL_P (expr))
7603 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7604 scope);
7605 /* Remember that there was a reference to this entity. */
7606 mark_used (expr);
7609 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7611 if (complain & tf_error)
7612 qualified_name_lookup_error (scope,
7613 TREE_OPERAND (qualified_id, 1),
7614 expr);
7615 return error_mark_node;
7618 if (is_template)
7619 expr = lookup_template_function (expr, template_args);
7621 if (expr == error_mark_node && complain & tf_error)
7622 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7623 expr);
7624 else if (TYPE_P (scope))
7626 expr = (adjust_result_of_qualified_name_lookup
7627 (expr, scope, current_class_type));
7628 expr = finish_qualified_id_expr (scope, expr, done, address_p);
7631 expr = convert_from_reference (expr);
7633 return expr;
7636 /* Like tsubst, but deals with expressions. This function just replaces
7637 template parms; to finish processing the resultant expression, use
7638 tsubst_expr. */
7640 static tree
7641 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7643 enum tree_code code;
7644 tree r;
7646 if (t == NULL_TREE || t == error_mark_node)
7647 return t;
7649 code = TREE_CODE (t);
7651 switch (code)
7653 case PARM_DECL:
7654 r = retrieve_local_specialization (t);
7655 gcc_assert (r != NULL);
7656 mark_used (r);
7657 return r;
7659 case CONST_DECL:
7661 tree enum_type;
7662 tree v;
7664 if (DECL_TEMPLATE_PARM_P (t))
7665 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7666 /* There is no need to substitute into namespace-scope
7667 enumerators. */
7668 if (DECL_NAMESPACE_SCOPE_P (t))
7669 return t;
7670 /* If ARGS is NULL, then T is known to be non-dependent. */
7671 if (args == NULL_TREE)
7672 return integral_constant_value (t);
7674 /* Unfortunately, we cannot just call lookup_name here.
7675 Consider:
7677 template <int I> int f() {
7678 enum E { a = I };
7679 struct S { void g() { E e = a; } };
7682 When we instantiate f<7>::S::g(), say, lookup_name is not
7683 clever enough to find f<7>::a. */
7684 enum_type
7685 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7686 /*entering_scope=*/0);
7688 for (v = TYPE_VALUES (enum_type);
7689 v != NULL_TREE;
7690 v = TREE_CHAIN (v))
7691 if (TREE_PURPOSE (v) == DECL_NAME (t))
7692 return TREE_VALUE (v);
7694 /* We didn't find the name. That should never happen; if
7695 name-lookup found it during preliminary parsing, we
7696 should find it again here during instantiation. */
7697 gcc_unreachable ();
7699 return t;
7701 case FIELD_DECL:
7702 if (DECL_CONTEXT (t))
7704 tree ctx;
7706 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7707 /*entering_scope=*/1);
7708 if (ctx != DECL_CONTEXT (t))
7710 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7711 if (!r)
7713 if (complain & tf_error)
7714 error ("using invalid field %qD", t);
7715 return error_mark_node;
7717 return r;
7721 return t;
7723 case VAR_DECL:
7724 case FUNCTION_DECL:
7725 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7726 || local_variable_p (t))
7727 t = tsubst (t, args, complain, in_decl);
7728 mark_used (t);
7729 return t;
7731 case BASELINK:
7732 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
7734 case TEMPLATE_DECL:
7735 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7736 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7737 args, complain, in_decl);
7738 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
7739 return tsubst (t, args, complain, in_decl);
7740 else if (DECL_CLASS_SCOPE_P (t)
7741 && uses_template_parms (DECL_CONTEXT (t)))
7743 /* Template template argument like the following example need
7744 special treatment:
7746 template <template <class> class TT> struct C {};
7747 template <class T> struct D {
7748 template <class U> struct E {};
7749 C<E> c; // #1
7751 D<int> d; // #2
7753 We are processing the template argument `E' in #1 for
7754 the template instantiation #2. Originally, `E' is a
7755 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7756 have to substitute this with one having context `D<int>'. */
7758 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
7759 return lookup_field (context, DECL_NAME(t), 0, false);
7761 else
7762 /* Ordinary template template argument. */
7763 return t;
7765 case CAST_EXPR:
7766 case REINTERPRET_CAST_EXPR:
7767 case CONST_CAST_EXPR:
7768 case STATIC_CAST_EXPR:
7769 case DYNAMIC_CAST_EXPR:
7770 case NOP_EXPR:
7771 return build1
7772 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7773 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7775 case INDIRECT_REF:
7776 case NEGATE_EXPR:
7777 case TRUTH_NOT_EXPR:
7778 case BIT_NOT_EXPR:
7779 case ADDR_EXPR:
7780 case CONVERT_EXPR: /* Unary + */
7781 case SIZEOF_EXPR:
7782 case ALIGNOF_EXPR:
7783 case ARROW_EXPR:
7784 case THROW_EXPR:
7785 case TYPEID_EXPR:
7786 case REALPART_EXPR:
7787 case IMAGPART_EXPR:
7788 return build1
7789 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7790 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7792 case COMPONENT_REF:
7794 tree object;
7795 tree name;
7797 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7798 name = TREE_OPERAND (t, 1);
7799 if (TREE_CODE (name) == BIT_NOT_EXPR)
7801 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7802 complain, in_decl);
7803 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7805 else if (TREE_CODE (name) == SCOPE_REF
7806 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7808 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7809 complain, in_decl);
7810 name = TREE_OPERAND (name, 1);
7811 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7812 complain, in_decl);
7813 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7814 name = build_nt (SCOPE_REF, base, name);
7816 else if (TREE_CODE (name) == BASELINK)
7817 name = tsubst_baselink (name,
7818 non_reference (TREE_TYPE (object)),
7819 args, complain,
7820 in_decl);
7821 else
7822 name = tsubst_copy (name, args, complain, in_decl);
7823 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
7826 case PLUS_EXPR:
7827 case MINUS_EXPR:
7828 case MULT_EXPR:
7829 case TRUNC_DIV_EXPR:
7830 case CEIL_DIV_EXPR:
7831 case FLOOR_DIV_EXPR:
7832 case ROUND_DIV_EXPR:
7833 case EXACT_DIV_EXPR:
7834 case BIT_AND_EXPR:
7835 case BIT_IOR_EXPR:
7836 case BIT_XOR_EXPR:
7837 case TRUNC_MOD_EXPR:
7838 case FLOOR_MOD_EXPR:
7839 case TRUTH_ANDIF_EXPR:
7840 case TRUTH_ORIF_EXPR:
7841 case TRUTH_AND_EXPR:
7842 case TRUTH_OR_EXPR:
7843 case RSHIFT_EXPR:
7844 case LSHIFT_EXPR:
7845 case RROTATE_EXPR:
7846 case LROTATE_EXPR:
7847 case EQ_EXPR:
7848 case NE_EXPR:
7849 case MAX_EXPR:
7850 case MIN_EXPR:
7851 case LE_EXPR:
7852 case GE_EXPR:
7853 case LT_EXPR:
7854 case GT_EXPR:
7855 case COMPOUND_EXPR:
7856 case SCOPE_REF:
7857 case DOTSTAR_EXPR:
7858 case MEMBER_REF:
7859 case PREDECREMENT_EXPR:
7860 case PREINCREMENT_EXPR:
7861 case POSTDECREMENT_EXPR:
7862 case POSTINCREMENT_EXPR:
7863 return build_nt
7864 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7865 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7867 case ARRAY_REF:
7868 return build_nt
7869 (ARRAY_REF,
7870 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7871 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7872 NULL_TREE, NULL_TREE);
7874 case CALL_EXPR:
7875 return build_nt (code,
7876 tsubst_copy (TREE_OPERAND (t, 0), args,
7877 complain, in_decl),
7878 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7879 in_decl),
7880 NULL_TREE);
7882 case COND_EXPR:
7883 case MODOP_EXPR:
7884 case PSEUDO_DTOR_EXPR:
7886 r = build_nt
7887 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7888 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7889 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7890 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
7891 return r;
7894 case NEW_EXPR:
7896 r = build_nt
7897 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7898 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7899 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7900 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7901 return r;
7904 case DELETE_EXPR:
7906 r = build_nt
7907 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7908 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7909 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7910 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7911 return r;
7914 case TEMPLATE_ID_EXPR:
7916 /* Substituted template arguments */
7917 tree fn = TREE_OPERAND (t, 0);
7918 tree targs = TREE_OPERAND (t, 1);
7920 fn = tsubst_copy (fn, args, complain, in_decl);
7921 if (targs)
7922 targs = tsubst_template_args (targs, args, complain, in_decl);
7924 return lookup_template_function (fn, targs);
7927 case TREE_LIST:
7929 tree purpose, value, chain;
7931 if (t == void_list_node)
7932 return t;
7934 purpose = TREE_PURPOSE (t);
7935 if (purpose)
7936 purpose = tsubst_copy (purpose, args, complain, in_decl);
7937 value = TREE_VALUE (t);
7938 if (value)
7939 value = tsubst_copy (value, args, complain, in_decl);
7940 chain = TREE_CHAIN (t);
7941 if (chain && chain != void_type_node)
7942 chain = tsubst_copy (chain, args, complain, in_decl);
7943 if (purpose == TREE_PURPOSE (t)
7944 && value == TREE_VALUE (t)
7945 && chain == TREE_CHAIN (t))
7946 return t;
7947 return tree_cons (purpose, value, chain);
7950 case RECORD_TYPE:
7951 case UNION_TYPE:
7952 case ENUMERAL_TYPE:
7953 case INTEGER_TYPE:
7954 case TEMPLATE_TYPE_PARM:
7955 case TEMPLATE_TEMPLATE_PARM:
7956 case BOUND_TEMPLATE_TEMPLATE_PARM:
7957 case TEMPLATE_PARM_INDEX:
7958 case POINTER_TYPE:
7959 case REFERENCE_TYPE:
7960 case OFFSET_TYPE:
7961 case FUNCTION_TYPE:
7962 case METHOD_TYPE:
7963 case ARRAY_TYPE:
7964 case TYPENAME_TYPE:
7965 case UNBOUND_CLASS_TEMPLATE:
7966 case TYPEOF_TYPE:
7967 case TYPE_DECL:
7968 return tsubst (t, args, complain, in_decl);
7970 case IDENTIFIER_NODE:
7971 if (IDENTIFIER_TYPENAME_P (t))
7973 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7974 return mangle_conv_op_name_for_type (new_type);
7976 else
7977 return t;
7979 case CONSTRUCTOR:
7981 r = build_constructor
7982 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7983 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
7984 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7985 return r;
7988 case VA_ARG_EXPR:
7989 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7990 in_decl),
7991 tsubst (TREE_TYPE (t), args, complain, in_decl));
7993 case CLEANUP_POINT_EXPR:
7994 /* We shouldn't have built any of these during initial template
7995 generation. Instead, they should be built during instantiation
7996 in response to the saved STMT_IS_FULL_EXPR_P setting. */
7997 gcc_unreachable ();
7999 default:
8000 return t;
8004 /* Like tsubst_copy for expressions, etc. but also does semantic
8005 processing. */
8007 static tree
8008 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8010 tree stmt, tmp;
8012 if (t == NULL_TREE || t == error_mark_node)
8013 return t;
8015 if (EXPR_HAS_LOCATION (t))
8016 input_location = EXPR_LOCATION (t);
8017 if (STATEMENT_CODE_P (TREE_CODE (t)))
8018 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8020 switch (TREE_CODE (t))
8022 case STATEMENT_LIST:
8024 tree_stmt_iterator i;
8025 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8026 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
8027 break;
8030 case CTOR_INITIALIZER:
8031 finish_mem_initializers (tsubst_initializer_list
8032 (TREE_OPERAND (t, 0), args));
8033 break;
8035 case RETURN_EXPR:
8036 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
8037 args, complain, in_decl));
8038 break;
8040 case EXPR_STMT:
8041 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
8042 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8043 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8044 else
8045 finish_expr_stmt (tmp);
8046 break;
8048 case USING_STMT:
8049 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
8050 args, complain, in_decl));
8051 break;
8053 case DECL_EXPR:
8055 tree decl;
8056 tree init;
8058 decl = DECL_EXPR_DECL (t);
8059 if (TREE_CODE (decl) == LABEL_DECL)
8060 finish_label_decl (DECL_NAME (decl));
8061 else if (TREE_CODE (decl) == USING_DECL)
8063 tree scope = DECL_INITIAL (decl);
8064 tree name = DECL_NAME (decl);
8065 tree decl;
8067 scope = tsubst_expr (scope, args, complain, in_decl);
8068 decl = lookup_qualified_name (scope, name,
8069 /*is_type_p=*/false,
8070 /*complain=*/false);
8071 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8072 qualified_name_lookup_error (scope, name, decl);
8073 else
8074 do_local_using_decl (decl, scope, name);
8076 else
8078 init = DECL_INITIAL (decl);
8079 decl = tsubst (decl, args, complain, in_decl);
8080 if (decl != error_mark_node)
8082 if (init)
8083 DECL_INITIAL (decl) = error_mark_node;
8084 /* By marking the declaration as instantiated, we avoid
8085 trying to instantiate it. Since instantiate_decl can't
8086 handle local variables, and since we've already done
8087 all that needs to be done, that's the right thing to
8088 do. */
8089 if (TREE_CODE (decl) == VAR_DECL)
8090 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8091 if (TREE_CODE (decl) == VAR_DECL
8092 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8093 /* Anonymous aggregates are a special case. */
8094 finish_anon_union (decl);
8095 else
8097 maybe_push_decl (decl);
8098 if (TREE_CODE (decl) == VAR_DECL
8099 && DECL_PRETTY_FUNCTION_P (decl))
8101 /* For __PRETTY_FUNCTION__ we have to adjust the
8102 initializer. */
8103 const char *const name
8104 = cxx_printable_name (current_function_decl, 2);
8105 init = cp_fname_init (name, &TREE_TYPE (decl));
8107 else
8108 init = tsubst_expr (init, args, complain, in_decl);
8109 cp_finish_decl (decl, init, NULL_TREE, 0);
8114 /* A DECL_EXPR can also be used as an expression, in the condition
8115 clause of an if/for/while construct. */
8116 return decl;
8119 case FOR_STMT:
8120 stmt = begin_for_stmt ();
8121 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
8122 finish_for_init_stmt (stmt);
8123 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
8124 finish_for_cond (tmp, stmt);
8125 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
8126 finish_for_expr (tmp, stmt);
8127 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
8128 finish_for_stmt (stmt);
8129 break;
8131 case WHILE_STMT:
8132 stmt = begin_while_stmt ();
8133 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
8134 finish_while_stmt_cond (tmp, stmt);
8135 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
8136 finish_while_stmt (stmt);
8137 break;
8139 case DO_STMT:
8140 stmt = begin_do_stmt ();
8141 tsubst_expr (DO_BODY (t), args, complain, in_decl);
8142 finish_do_body (stmt);
8143 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
8144 finish_do_stmt (tmp, stmt);
8145 break;
8147 case IF_STMT:
8148 stmt = begin_if_stmt ();
8149 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
8150 finish_if_stmt_cond (tmp, stmt);
8151 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
8152 finish_then_clause (stmt);
8154 if (ELSE_CLAUSE (t))
8156 begin_else_clause (stmt);
8157 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
8158 finish_else_clause (stmt);
8161 finish_if_stmt (stmt);
8162 break;
8164 case BIND_EXPR:
8165 if (BIND_EXPR_BODY_BLOCK (t))
8166 stmt = begin_function_body ();
8167 else
8168 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8169 ? BCS_TRY_BLOCK : 0);
8171 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
8173 if (BIND_EXPR_BODY_BLOCK (t))
8174 finish_function_body (stmt);
8175 else
8176 finish_compound_stmt (stmt);
8177 break;
8179 case BREAK_STMT:
8180 finish_break_stmt ();
8181 break;
8183 case CONTINUE_STMT:
8184 finish_continue_stmt ();
8185 break;
8187 case SWITCH_STMT:
8188 stmt = begin_switch_stmt ();
8189 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
8190 finish_switch_cond (tmp, stmt);
8191 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
8192 finish_switch_stmt (stmt);
8193 break;
8195 case CASE_LABEL_EXPR:
8196 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
8197 tsubst_expr (CASE_HIGH (t), args, complain,
8198 in_decl));
8199 break;
8201 case LABEL_EXPR:
8202 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8203 break;
8205 case GOTO_EXPR:
8206 tmp = GOTO_DESTINATION (t);
8207 if (TREE_CODE (tmp) != LABEL_DECL)
8208 /* Computed goto's must be tsubst'd into. On the other hand,
8209 non-computed gotos must not be; the identifier in question
8210 will have no binding. */
8211 tmp = tsubst_expr (tmp, args, complain, in_decl);
8212 else
8213 tmp = DECL_NAME (tmp);
8214 finish_goto_stmt (tmp);
8215 break;
8217 case ASM_EXPR:
8218 tmp = finish_asm_stmt
8219 (ASM_VOLATILE_P (t),
8220 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
8221 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
8222 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
8223 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
8225 tree asm_expr = tmp;
8226 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8227 asm_expr = TREE_OPERAND (asm_expr, 0);
8228 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8230 break;
8232 case TRY_BLOCK:
8233 if (CLEANUP_P (t))
8235 stmt = begin_try_block ();
8236 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8237 finish_cleanup_try_block (stmt);
8238 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
8239 complain, in_decl),
8240 stmt);
8242 else
8244 if (FN_TRY_BLOCK_P (t))
8245 stmt = begin_function_try_block ();
8246 else
8247 stmt = begin_try_block ();
8249 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
8251 if (FN_TRY_BLOCK_P (t))
8252 finish_function_try_block (stmt);
8253 else
8254 finish_try_block (stmt);
8256 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
8257 if (FN_TRY_BLOCK_P (t))
8258 finish_function_handler_sequence (stmt);
8259 else
8260 finish_handler_sequence (stmt);
8262 break;
8264 case HANDLER:
8266 tree decl;
8268 stmt = begin_handler ();
8269 if (HANDLER_PARMS (t))
8271 decl = HANDLER_PARMS (t);
8272 decl = tsubst (decl, args, complain, in_decl);
8273 /* Prevent instantiate_decl from trying to instantiate
8274 this variable. We've already done all that needs to be
8275 done. */
8276 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8278 else
8279 decl = NULL_TREE;
8280 finish_handler_parms (decl, stmt);
8281 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
8282 finish_handler (stmt);
8284 break;
8286 case TAG_DEFN:
8287 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8288 break;
8290 default:
8291 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8293 return tsubst_copy_and_build (t, args, complain, in_decl,
8294 /*function_p=*/false);
8297 return NULL_TREE;
8300 /* T is a postfix-expression that is not being used in a function
8301 call. Return the substituted version of T. */
8303 static tree
8304 tsubst_non_call_postfix_expression (tree t, tree args,
8305 tsubst_flags_t complain,
8306 tree in_decl)
8308 if (TREE_CODE (t) == SCOPE_REF)
8309 t = tsubst_qualified_id (t, args, complain, in_decl,
8310 /*done=*/false, /*address_p=*/false);
8311 else
8312 t = tsubst_copy_and_build (t, args, complain, in_decl,
8313 /*function_p=*/false);
8315 return t;
8318 /* Like tsubst but deals with expressions and performs semantic
8319 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8321 tree
8322 tsubst_copy_and_build (tree t,
8323 tree args,
8324 tsubst_flags_t complain,
8325 tree in_decl,
8326 bool function_p)
8328 #define RECUR(NODE) \
8329 tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false)
8331 tree op1;
8333 if (t == NULL_TREE || t == error_mark_node)
8334 return t;
8336 switch (TREE_CODE (t))
8338 case USING_DECL:
8339 t = DECL_NAME (t);
8340 /* Fall through. */
8341 case IDENTIFIER_NODE:
8343 tree decl;
8344 cp_id_kind idk;
8345 tree qualifying_class;
8346 bool non_integral_constant_expression_p;
8347 const char *error_msg;
8349 if (IDENTIFIER_TYPENAME_P (t))
8351 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8352 t = mangle_conv_op_name_for_type (new_type);
8355 /* Look up the name. */
8356 decl = lookup_name (t, 0);
8358 /* By convention, expressions use ERROR_MARK_NODE to indicate
8359 failure, not NULL_TREE. */
8360 if (decl == NULL_TREE)
8361 decl = error_mark_node;
8363 decl = finish_id_expression (t, decl, NULL_TREE,
8364 &idk,
8365 &qualifying_class,
8366 /*integral_constant_expression_p=*/false,
8367 /*allow_non_integral_constant_expression_p=*/false,
8368 &non_integral_constant_expression_p,
8369 &error_msg);
8370 if (error_msg)
8371 error (error_msg);
8372 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8373 decl = unqualified_name_lookup_error (decl);
8374 return decl;
8377 case TEMPLATE_ID_EXPR:
8379 tree object;
8380 tree template = RECUR (TREE_OPERAND (t, 0));
8381 tree targs = TREE_OPERAND (t, 1);
8383 if (targs)
8384 targs = tsubst_template_args (targs, args, complain, in_decl);
8386 if (TREE_CODE (template) == COMPONENT_REF)
8388 object = TREE_OPERAND (template, 0);
8389 template = TREE_OPERAND (template, 1);
8391 else
8392 object = NULL_TREE;
8393 template = lookup_template_function (template, targs);
8395 if (object)
8396 return build3 (COMPONENT_REF, TREE_TYPE (template),
8397 object, template, NULL_TREE);
8398 else
8399 return template;
8402 case INDIRECT_REF:
8404 tree r = RECUR (TREE_OPERAND (t, 0));
8406 if (REFERENCE_REF_P (t))
8408 /* A type conversion to reference type will be enclosed in
8409 such an indirect ref, but the substitution of the cast
8410 will have also added such an indirect ref. */
8411 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8412 r = convert_from_reference (r);
8414 else
8415 r = build_x_indirect_ref (r, "unary *");
8416 return r;
8419 case NOP_EXPR:
8420 return build_nop
8421 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8422 RECUR (TREE_OPERAND (t, 0)));
8424 case CAST_EXPR:
8425 return build_functional_cast
8426 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8427 RECUR (TREE_OPERAND (t, 0)));
8429 case REINTERPRET_CAST_EXPR:
8430 return build_reinterpret_cast
8431 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8432 RECUR (TREE_OPERAND (t, 0)));
8434 case CONST_CAST_EXPR:
8435 return build_const_cast
8436 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8437 RECUR (TREE_OPERAND (t, 0)));
8439 case DYNAMIC_CAST_EXPR:
8440 return build_dynamic_cast
8441 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8442 RECUR (TREE_OPERAND (t, 0)));
8444 case STATIC_CAST_EXPR:
8445 return build_static_cast
8446 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8447 RECUR (TREE_OPERAND (t, 0)));
8449 case POSTDECREMENT_EXPR:
8450 case POSTINCREMENT_EXPR:
8451 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8452 args, complain, in_decl);
8453 return build_x_unary_op (TREE_CODE (t), op1);
8455 case PREDECREMENT_EXPR:
8456 case PREINCREMENT_EXPR:
8457 case NEGATE_EXPR:
8458 case BIT_NOT_EXPR:
8459 case ABS_EXPR:
8460 case TRUTH_NOT_EXPR:
8461 case CONVERT_EXPR: /* Unary + */
8462 case REALPART_EXPR:
8463 case IMAGPART_EXPR:
8464 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
8466 case ADDR_EXPR:
8467 op1 = TREE_OPERAND (t, 0);
8468 if (TREE_CODE (op1) == SCOPE_REF)
8469 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
8470 /*done=*/true, /*address_p=*/true);
8471 else
8472 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
8473 in_decl);
8474 if (TREE_CODE (op1) == LABEL_DECL)
8475 return finish_label_address_expr (DECL_NAME (op1));
8476 return build_x_unary_op (ADDR_EXPR, op1);
8478 case PLUS_EXPR:
8479 case MINUS_EXPR:
8480 case MULT_EXPR:
8481 case TRUNC_DIV_EXPR:
8482 case CEIL_DIV_EXPR:
8483 case FLOOR_DIV_EXPR:
8484 case ROUND_DIV_EXPR:
8485 case EXACT_DIV_EXPR:
8486 case BIT_AND_EXPR:
8487 case BIT_IOR_EXPR:
8488 case BIT_XOR_EXPR:
8489 case TRUNC_MOD_EXPR:
8490 case FLOOR_MOD_EXPR:
8491 case TRUTH_ANDIF_EXPR:
8492 case TRUTH_ORIF_EXPR:
8493 case TRUTH_AND_EXPR:
8494 case TRUTH_OR_EXPR:
8495 case RSHIFT_EXPR:
8496 case LSHIFT_EXPR:
8497 case RROTATE_EXPR:
8498 case LROTATE_EXPR:
8499 case EQ_EXPR:
8500 case NE_EXPR:
8501 case MAX_EXPR:
8502 case MIN_EXPR:
8503 case LE_EXPR:
8504 case GE_EXPR:
8505 case LT_EXPR:
8506 case GT_EXPR:
8507 case MEMBER_REF:
8508 case DOTSTAR_EXPR:
8509 return build_x_binary_op
8510 (TREE_CODE (t),
8511 RECUR (TREE_OPERAND (t, 0)),
8512 RECUR (TREE_OPERAND (t, 1)),
8513 /*overloaded_p=*/NULL);
8515 case SCOPE_REF:
8516 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8517 /*address_p=*/false);
8518 case ARRAY_REF:
8519 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8520 args, complain, in_decl);
8521 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8522 /*overloaded_p=*/NULL);
8524 case SIZEOF_EXPR:
8525 case ALIGNOF_EXPR:
8526 op1 = TREE_OPERAND (t, 0);
8527 if (!args)
8529 /* When there are no ARGS, we are trying to evaluate a
8530 non-dependent expression from the parser. Trying to do
8531 the substitutions may not work. */
8532 if (!TYPE_P (op1))
8533 op1 = TREE_TYPE (op1);
8535 else
8537 ++skip_evaluation;
8538 op1 = RECUR (op1);
8539 --skip_evaluation;
8541 if (TYPE_P (op1))
8542 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
8543 else
8544 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
8546 case MODOP_EXPR:
8548 tree r = build_x_modify_expr
8549 (RECUR (TREE_OPERAND (t, 0)),
8550 TREE_CODE (TREE_OPERAND (t, 1)),
8551 RECUR (TREE_OPERAND (t, 2)));
8552 /* TREE_NO_WARNING must be set if either the expression was
8553 parenthesized or it uses an operator such as >>= rather
8554 than plain assignment. In the former case, it was already
8555 set and must be copied. In the latter case,
8556 build_x_modify_expr sets it and it must not be reset
8557 here. */
8558 if (TREE_NO_WARNING (t))
8559 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8560 return r;
8563 case ARROW_EXPR:
8564 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8565 args, complain, in_decl);
8566 /* Remember that there was a reference to this entity. */
8567 if (DECL_P (op1))
8568 mark_used (op1);
8569 return build_x_arrow (op1);
8571 case NEW_EXPR:
8572 return build_new
8573 (RECUR (TREE_OPERAND (t, 0)),
8574 RECUR (TREE_OPERAND (t, 1)),
8575 RECUR (TREE_OPERAND (t, 2)),
8576 RECUR (TREE_OPERAND (t, 3)),
8577 NEW_EXPR_USE_GLOBAL (t));
8579 case DELETE_EXPR:
8580 return delete_sanity
8581 (RECUR (TREE_OPERAND (t, 0)),
8582 RECUR (TREE_OPERAND (t, 1)),
8583 DELETE_EXPR_USE_VEC (t),
8584 DELETE_EXPR_USE_GLOBAL (t));
8586 case COMPOUND_EXPR:
8587 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
8588 RECUR (TREE_OPERAND (t, 1)));
8590 case CALL_EXPR:
8592 tree function;
8593 tree call_args;
8594 bool qualified_p;
8595 bool koenig_p;
8597 function = TREE_OPERAND (t, 0);
8598 /* When we parsed the expression, we determined whether or
8599 not Koenig lookup should be performed. */
8600 koenig_p = KOENIG_LOOKUP_P (t);
8601 if (TREE_CODE (function) == SCOPE_REF)
8603 qualified_p = true;
8604 function = tsubst_qualified_id (function, args, complain, in_decl,
8605 /*done=*/false,
8606 /*address_p=*/false);
8608 else
8610 qualified_p = (TREE_CODE (function) == COMPONENT_REF
8611 && (TREE_CODE (TREE_OPERAND (function, 1))
8612 == SCOPE_REF));
8613 function = tsubst_copy_and_build (function, args, complain,
8614 in_decl,
8615 !qualified_p);
8616 if (BASELINK_P (function))
8617 qualified_p = true;
8620 call_args = RECUR (TREE_OPERAND (t, 1));
8622 /* We do not perform argument-dependent lookup if normal
8623 lookup finds a non-function, in accordance with the
8624 expected resolution of DR 218. */
8625 if (koenig_p
8626 && ((is_overloaded_fn (function)
8627 /* If lookup found a member function, the Koenig lookup is
8628 not appropriate, even if an unqualified-name was used
8629 to denote the function. */
8630 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
8631 || TREE_CODE (function) == IDENTIFIER_NODE))
8632 function = perform_koenig_lookup (function, call_args);
8634 if (TREE_CODE (function) == IDENTIFIER_NODE)
8636 unqualified_name_lookup_error (function);
8637 return error_mark_node;
8640 /* Remember that there was a reference to this entity. */
8641 if (DECL_P (function))
8642 mark_used (function);
8644 if (TREE_CODE (function) == OFFSET_REF)
8645 return build_offset_ref_call_from_tree (function, call_args);
8646 if (TREE_CODE (function) == COMPONENT_REF)
8648 if (!BASELINK_P (TREE_OPERAND (function, 1)))
8649 return finish_call_expr (function, call_args,
8650 /*disallow_virtual=*/false,
8651 /*koenig_p=*/false);
8652 else
8653 return (build_new_method_call
8654 (TREE_OPERAND (function, 0),
8655 TREE_OPERAND (function, 1),
8656 call_args, NULL_TREE,
8657 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
8659 return finish_call_expr (function, call_args,
8660 /*disallow_virtual=*/qualified_p,
8661 koenig_p);
8664 case COND_EXPR:
8665 return build_x_conditional_expr
8666 (RECUR (TREE_OPERAND (t, 0)),
8667 RECUR (TREE_OPERAND (t, 1)),
8668 RECUR (TREE_OPERAND (t, 2)));
8670 case PSEUDO_DTOR_EXPR:
8671 return finish_pseudo_destructor_expr
8672 (RECUR (TREE_OPERAND (t, 0)),
8673 RECUR (TREE_OPERAND (t, 1)),
8674 RECUR (TREE_OPERAND (t, 2)));
8676 case TREE_LIST:
8678 tree purpose, value, chain;
8680 if (t == void_list_node)
8681 return t;
8683 purpose = TREE_PURPOSE (t);
8684 if (purpose)
8685 purpose = RECUR (purpose);
8686 value = TREE_VALUE (t);
8687 if (value)
8688 value = RECUR (value);
8689 chain = TREE_CHAIN (t);
8690 if (chain && chain != void_type_node)
8691 chain = RECUR (chain);
8692 if (purpose == TREE_PURPOSE (t)
8693 && value == TREE_VALUE (t)
8694 && chain == TREE_CHAIN (t))
8695 return t;
8696 return tree_cons (purpose, value, chain);
8699 case COMPONENT_REF:
8701 tree object;
8702 tree member;
8704 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8705 args, complain, in_decl);
8706 /* Remember that there was a reference to this entity. */
8707 if (DECL_P (object))
8708 mark_used (object);
8710 member = TREE_OPERAND (t, 1);
8711 if (BASELINK_P (member))
8712 member = tsubst_baselink (member,
8713 non_reference (TREE_TYPE (object)),
8714 args, complain, in_decl);
8715 else
8716 member = tsubst_copy (member, args, complain, in_decl);
8718 if (member == error_mark_node)
8719 return error_mark_node;
8720 else if (!CLASS_TYPE_P (TREE_TYPE (object)))
8722 if (TREE_CODE (member) == BIT_NOT_EXPR)
8723 return finish_pseudo_destructor_expr (object,
8724 NULL_TREE,
8725 TREE_TYPE (object));
8726 else if (TREE_CODE (member) == SCOPE_REF
8727 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8728 return finish_pseudo_destructor_expr (object,
8729 object,
8730 TREE_TYPE (object));
8732 else if (TREE_CODE (member) == SCOPE_REF
8733 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8735 tree tmpl;
8736 tree args;
8738 /* Lookup the template functions now that we know what the
8739 scope is. */
8740 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8741 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8742 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
8743 /*is_type_p=*/false,
8744 /*complain=*/false);
8745 if (BASELINK_P (member))
8747 BASELINK_FUNCTIONS (member)
8748 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8749 args);
8750 member = (adjust_result_of_qualified_name_lookup
8751 (member, BINFO_TYPE (BASELINK_BINFO (member)),
8752 TREE_TYPE (object)));
8754 else
8756 qualified_name_lookup_error (TREE_TYPE (object), tmpl,
8757 member);
8758 return error_mark_node;
8761 else if (TREE_CODE (member) == SCOPE_REF
8762 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
8763 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
8765 if (complain & tf_error)
8767 if (TYPE_P (TREE_OPERAND (member, 0)))
8768 error ("%qT is not a class or namespace",
8769 TREE_OPERAND (member, 0));
8770 else
8771 error ("%qD is not a class or namespace",
8772 TREE_OPERAND (member, 0));
8774 return error_mark_node;
8776 else if (TREE_CODE (member) == FIELD_DECL)
8777 return finish_non_static_data_member (member, object, NULL_TREE);
8779 return finish_class_member_access_expr (object, member);
8782 case THROW_EXPR:
8783 return build_throw
8784 (RECUR (TREE_OPERAND (t, 0)));
8786 case CONSTRUCTOR:
8788 tree r;
8789 tree elts;
8790 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8791 bool purpose_p;
8793 /* digest_init will do the wrong thing if we let it. */
8794 if (type && TYPE_PTRMEMFUNC_P (type))
8795 return t;
8797 r = NULL_TREE;
8798 /* We do not want to process the purpose of aggregate
8799 initializers as they are identifier nodes which will be
8800 looked up by digest_init. */
8801 purpose_p = !(type && IS_AGGR_TYPE (type));
8802 for (elts = CONSTRUCTOR_ELTS (t);
8803 elts;
8804 elts = TREE_CHAIN (elts))
8806 tree purpose = TREE_PURPOSE (elts);
8807 tree value = TREE_VALUE (elts);
8809 if (purpose && purpose_p)
8810 purpose = RECUR (purpose);
8811 value = RECUR (value);
8812 r = tree_cons (purpose, value, r);
8815 r = build_constructor (NULL_TREE, nreverse (r));
8816 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8818 if (type)
8819 return digest_init (type, r, 0);
8820 return r;
8823 case TYPEID_EXPR:
8825 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
8826 if (TYPE_P (operand_0))
8827 return get_typeid (operand_0);
8828 return build_typeid (operand_0);
8831 case VAR_DECL:
8832 if (!args)
8833 return t;
8834 /* Fall through */
8836 case PARM_DECL:
8838 tree r = tsubst_copy (t, args, complain, in_decl);
8840 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
8841 /* If the original type was a reference, we'll be wrapped in
8842 the appropriate INDIRECT_REF. */
8843 r = convert_from_reference (r);
8844 return r;
8847 case VA_ARG_EXPR:
8848 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
8849 tsubst_copy (TREE_TYPE (t), args, complain,
8850 in_decl));
8852 case OFFSETOF_EXPR:
8853 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
8855 case STMT_EXPR:
8857 tree old_stmt_expr = cur_stmt_expr;
8858 tree stmt_expr = begin_stmt_expr ();
8860 cur_stmt_expr = stmt_expr;
8861 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
8862 stmt_expr = finish_stmt_expr (stmt_expr, false);
8863 cur_stmt_expr = old_stmt_expr;
8865 return stmt_expr;
8868 case CONST_DECL:
8869 t = tsubst_copy (t, args, complain, in_decl);
8870 /* As in finish_id_expression, we resolve enumeration constants
8871 to their underlying values. */
8872 if (TREE_CODE (t) == CONST_DECL)
8873 return DECL_INITIAL (t);
8874 return t;
8876 default:
8877 return tsubst_copy (t, args, complain, in_decl);
8880 #undef RECUR
8883 /* Verify that the instantiated ARGS are valid. For type arguments,
8884 make sure that the type's linkage is ok. For non-type arguments,
8885 make sure they are constants if they are integral or enumerations.
8886 Emit an error under control of COMPLAIN, and return TRUE on error. */
8888 static bool
8889 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
8891 int ix, len = DECL_NTPARMS (tmpl);
8892 bool result = false;
8893 bool error_p = complain & tf_error;
8895 for (ix = 0; ix != len; ix++)
8897 tree t = TREE_VEC_ELT (args, ix);
8899 if (TYPE_P (t))
8901 /* [basic.link]: A name with no linkage (notably, the name
8902 of a class or enumeration declared in a local scope)
8903 shall not be used to declare an entity with linkage.
8904 This implies that names with no linkage cannot be used as
8905 template arguments. */
8906 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
8908 if (nt)
8910 if (TYPE_ANONYMOUS_P (nt))
8911 error ("%qT is/uses anonymous type", t);
8912 else
8913 error ("%qT uses local type %qT", t, nt);
8914 result = true;
8915 error_p = true;
8917 /* In order to avoid all sorts of complications, we do not
8918 allow variably-modified types as template arguments. */
8919 else if (variably_modified_type_p (t, NULL_TREE))
8921 if (complain & tf_error)
8922 error ("%qT is a variably modified type", t);
8923 result = true;
8926 /* A non-type argument of integral or enumerated type must be a
8927 constant. */
8928 else if (TREE_TYPE (t)
8929 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
8930 && !TREE_CONSTANT (t))
8932 if (complain & tf_error)
8933 error ("integral expression %qE is not constant", t);
8934 result = true;
8937 if (result && error_p)
8938 error (" trying to instantiate %qD", tmpl);
8939 return result;
8942 /* Instantiate the indicated variable or function template TMPL with
8943 the template arguments in TARG_PTR. */
8945 tree
8946 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8948 tree fndecl;
8949 tree gen_tmpl;
8950 tree spec;
8952 if (tmpl == error_mark_node)
8953 return error_mark_node;
8955 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
8957 /* If this function is a clone, handle it specially. */
8958 if (DECL_CLONED_FUNCTION_P (tmpl))
8960 tree spec;
8961 tree clone;
8963 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
8964 complain);
8965 if (spec == error_mark_node)
8966 return error_mark_node;
8968 /* Look for the clone. */
8969 FOR_EACH_CLONE (clone, spec)
8970 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8971 return clone;
8972 /* We should always have found the clone by now. */
8973 gcc_unreachable ();
8974 return NULL_TREE;
8977 /* Check to see if we already have this specialization. */
8978 spec = retrieve_specialization (tmpl, targ_ptr,
8979 /*class_specializations_p=*/false);
8980 if (spec != NULL_TREE)
8981 return spec;
8983 gen_tmpl = most_general_template (tmpl);
8984 if (tmpl != gen_tmpl)
8986 /* The TMPL is a partial instantiation. To get a full set of
8987 arguments we must add the arguments used to perform the
8988 partial instantiation. */
8989 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8990 targ_ptr);
8992 /* Check to see if we already have this specialization. */
8993 spec = retrieve_specialization (gen_tmpl, targ_ptr,
8994 /*class_specializations_p=*/false);
8995 if (spec != NULL_TREE)
8996 return spec;
8999 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9000 complain))
9001 return error_mark_node;
9003 /* We are building a FUNCTION_DECL, during which the access of its
9004 parameters and return types have to be checked. However this
9005 FUNCTION_DECL which is the desired context for access checking
9006 is not built yet. We solve this chicken-and-egg problem by
9007 deferring all checks until we have the FUNCTION_DECL. */
9008 push_deferring_access_checks (dk_deferred);
9010 /* Substitute template parameters. */
9011 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9012 targ_ptr, complain, gen_tmpl);
9014 /* Now we know the specialization, compute access previously
9015 deferred. */
9016 push_access_scope (fndecl);
9017 perform_deferred_access_checks ();
9018 pop_access_scope (fndecl);
9019 pop_deferring_access_checks ();
9021 /* The DECL_TI_TEMPLATE should always be the immediate parent
9022 template, not the most general template. */
9023 DECL_TI_TEMPLATE (fndecl) = tmpl;
9025 /* If we've just instantiated the main entry point for a function,
9026 instantiate all the alternate entry points as well. We do this
9027 by cloning the instantiation of the main entry point, not by
9028 instantiating the template clones. */
9029 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9030 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9032 return fndecl;
9035 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9036 arguments that are being used when calling it. TARGS is a vector
9037 into which the deduced template arguments are placed.
9039 Return zero for success, 2 for an incomplete match that doesn't resolve
9040 all the types, and 1 for complete failure. An error message will be
9041 printed only for an incomplete match.
9043 If FN is a conversion operator, or we are trying to produce a specific
9044 specialization, RETURN_TYPE is the return type desired.
9046 The EXPLICIT_TARGS are explicit template arguments provided via a
9047 template-id.
9049 The parameter STRICT is one of:
9051 DEDUCE_CALL:
9052 We are deducing arguments for a function call, as in
9053 [temp.deduct.call].
9055 DEDUCE_CONV:
9056 We are deducing arguments for a conversion function, as in
9057 [temp.deduct.conv].
9059 DEDUCE_EXACT:
9060 We are deducing arguments when doing an explicit instantiation
9061 as in [temp.explicit], when determining an explicit specialization
9062 as in [temp.expl.spec], or when taking the address of a function
9063 template, as in [temp.deduct.funcaddr]. */
9066 fn_type_unification (tree fn,
9067 tree explicit_targs,
9068 tree targs,
9069 tree args,
9070 tree return_type,
9071 unification_kind_t strict)
9073 tree parms;
9074 tree fntype;
9075 int result;
9077 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9079 fntype = TREE_TYPE (fn);
9080 if (explicit_targs)
9082 /* [temp.deduct]
9084 The specified template arguments must match the template
9085 parameters in kind (i.e., type, nontype, template), and there
9086 must not be more arguments than there are parameters;
9087 otherwise type deduction fails.
9089 Nontype arguments must match the types of the corresponding
9090 nontype template parameters, or must be convertible to the
9091 types of the corresponding nontype parameters as specified in
9092 _temp.arg.nontype_, otherwise type deduction fails.
9094 All references in the function type of the function template
9095 to the corresponding template parameters are replaced by the
9096 specified template argument values. If a substitution in a
9097 template parameter or in the function type of the function
9098 template results in an invalid type, type deduction fails. */
9099 int i;
9100 tree converted_args;
9101 bool incomplete;
9103 if (explicit_targs == error_mark_node)
9104 return 1;
9106 converted_args
9107 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9108 explicit_targs, NULL_TREE, tf_none,
9109 /*require_all_arguments=*/0));
9110 if (converted_args == error_mark_node)
9111 return 1;
9113 /* Substitute the explicit args into the function type. This is
9114 necessary so that, for instance, explicitly declared function
9115 arguments can match null pointed constants. If we were given
9116 an incomplete set of explicit args, we must not do semantic
9117 processing during substitution as we could create partial
9118 instantiations. */
9119 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9120 processing_template_decl += incomplete;
9121 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9122 processing_template_decl -= incomplete;
9124 if (fntype == error_mark_node)
9125 return 1;
9127 /* Place the explicitly specified arguments in TARGS. */
9128 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9129 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9132 parms = TYPE_ARG_TYPES (fntype);
9133 /* Never do unification on the 'this' parameter. */
9134 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9135 parms = TREE_CHAIN (parms);
9137 if (return_type)
9139 /* We've been given a return type to match, prepend it. */
9140 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9141 args = tree_cons (NULL_TREE, return_type, args);
9144 /* We allow incomplete unification without an error message here
9145 because the standard doesn't seem to explicitly prohibit it. Our
9146 callers must be ready to deal with unification failures in any
9147 event. */
9148 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9149 targs, parms, args, /*subr=*/0,
9150 strict, /*allow_incomplete*/1);
9152 if (result == 0)
9153 /* All is well so far. Now, check:
9155 [temp.deduct]
9157 When all template arguments have been deduced, all uses of
9158 template parameters in nondeduced contexts are replaced with
9159 the corresponding deduced argument values. If the
9160 substitution results in an invalid type, as described above,
9161 type deduction fails. */
9162 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9163 == error_mark_node)
9164 return 1;
9166 return result;
9169 /* Adjust types before performing type deduction, as described in
9170 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9171 sections are symmetric. PARM is the type of a function parameter
9172 or the return type of the conversion function. ARG is the type of
9173 the argument passed to the call, or the type of the value
9174 initialized with the result of the conversion function. */
9176 static int
9177 maybe_adjust_types_for_deduction (unification_kind_t strict,
9178 tree* parm,
9179 tree* arg)
9181 int result = 0;
9183 switch (strict)
9185 case DEDUCE_CALL:
9186 break;
9188 case DEDUCE_CONV:
9190 /* Swap PARM and ARG throughout the remainder of this
9191 function; the handling is precisely symmetric since PARM
9192 will initialize ARG rather than vice versa. */
9193 tree* temp = parm;
9194 parm = arg;
9195 arg = temp;
9196 break;
9199 case DEDUCE_EXACT:
9200 /* There is nothing to do in this case. */
9201 return 0;
9203 default:
9204 gcc_unreachable ();
9207 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9209 /* [temp.deduct.call]
9211 If P is not a reference type:
9213 --If A is an array type, the pointer type produced by the
9214 array-to-pointer standard conversion (_conv.array_) is
9215 used in place of A for type deduction; otherwise,
9217 --If A is a function type, the pointer type produced by
9218 the function-to-pointer standard conversion
9219 (_conv.func_) is used in place of A for type deduction;
9220 otherwise,
9222 --If A is a cv-qualified type, the top level
9223 cv-qualifiers of A's type are ignored for type
9224 deduction. */
9225 if (TREE_CODE (*arg) == ARRAY_TYPE)
9226 *arg = build_pointer_type (TREE_TYPE (*arg));
9227 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9228 *arg = build_pointer_type (*arg);
9229 else
9230 *arg = TYPE_MAIN_VARIANT (*arg);
9233 /* [temp.deduct.call]
9235 If P is a cv-qualified type, the top level cv-qualifiers
9236 of P's type are ignored for type deduction. If P is a
9237 reference type, the type referred to by P is used for
9238 type deduction. */
9239 *parm = TYPE_MAIN_VARIANT (*parm);
9240 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9242 *parm = TREE_TYPE (*parm);
9243 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9246 /* DR 322. For conversion deduction, remove a reference type on parm
9247 too (which has been swapped into ARG). */
9248 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9249 *arg = TREE_TYPE (*arg);
9251 return result;
9254 /* Most parms like fn_type_unification.
9256 If SUBR is 1, we're being called recursively (to unify the
9257 arguments of a function or method parameter of a function
9258 template). */
9260 static int
9261 type_unification_real (tree tparms,
9262 tree targs,
9263 tree xparms,
9264 tree xargs,
9265 int subr,
9266 unification_kind_t strict,
9267 int allow_incomplete)
9269 tree parm, arg;
9270 int i;
9271 int ntparms = TREE_VEC_LENGTH (tparms);
9272 int sub_strict;
9273 int saw_undeduced = 0;
9274 tree parms, args;
9276 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9277 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9278 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9279 gcc_assert (ntparms > 0);
9281 switch (strict)
9283 case DEDUCE_CALL:
9284 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9285 | UNIFY_ALLOW_DERIVED);
9286 break;
9288 case DEDUCE_CONV:
9289 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9290 break;
9292 case DEDUCE_EXACT:
9293 sub_strict = UNIFY_ALLOW_NONE;
9294 break;
9296 default:
9297 gcc_unreachable ();
9300 again:
9301 parms = xparms;
9302 args = xargs;
9304 while (parms && parms != void_list_node
9305 && args && args != void_list_node)
9307 parm = TREE_VALUE (parms);
9308 parms = TREE_CHAIN (parms);
9309 arg = TREE_VALUE (args);
9310 args = TREE_CHAIN (args);
9312 if (arg == error_mark_node)
9313 return 1;
9314 if (arg == unknown_type_node)
9315 /* We can't deduce anything from this, but we might get all the
9316 template args from other function args. */
9317 continue;
9319 /* Conversions will be performed on a function argument that
9320 corresponds with a function parameter that contains only
9321 non-deducible template parameters and explicitly specified
9322 template parameters. */
9323 if (!uses_template_parms (parm))
9325 tree type;
9327 if (!TYPE_P (arg))
9328 type = TREE_TYPE (arg);
9329 else
9330 type = arg;
9332 if (strict == DEDUCE_EXACT)
9334 if (same_type_p (parm, type))
9335 continue;
9337 else
9338 /* It might work; we shouldn't check now, because we might
9339 get into infinite recursion. Overload resolution will
9340 handle it. */
9341 continue;
9343 return 1;
9346 if (!TYPE_P (arg))
9348 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9349 if (type_unknown_p (arg))
9351 /* [temp.deduct.type] A template-argument can be deduced from
9352 a pointer to function or pointer to member function
9353 argument if the set of overloaded functions does not
9354 contain function templates and at most one of a set of
9355 overloaded functions provides a unique match. */
9357 if (resolve_overloaded_unification
9358 (tparms, targs, parm, arg, strict, sub_strict)
9359 != 0)
9360 return 1;
9361 continue;
9363 arg = TREE_TYPE (arg);
9364 if (arg == error_mark_node)
9365 return 1;
9369 int arg_strict = sub_strict;
9371 if (!subr)
9372 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9374 if (unify (tparms, targs, parm, arg, arg_strict))
9375 return 1;
9379 /* Fail if we've reached the end of the parm list, and more args
9380 are present, and the parm list isn't variadic. */
9381 if (args && args != void_list_node && parms == void_list_node)
9382 return 1;
9383 /* Fail if parms are left and they don't have default values. */
9384 if (parms && parms != void_list_node
9385 && TREE_PURPOSE (parms) == NULL_TREE)
9386 return 1;
9388 if (!subr)
9389 for (i = 0; i < ntparms; i++)
9390 if (!TREE_VEC_ELT (targs, i))
9392 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9394 /* If this is an undeduced nontype parameter that depends on
9395 a type parameter, try another pass; its type may have been
9396 deduced from a later argument than the one from which
9397 this parameter can be deduced. */
9398 if (TREE_CODE (tparm) == PARM_DECL
9399 && uses_template_parms (TREE_TYPE (tparm))
9400 && !saw_undeduced++)
9401 goto again;
9403 if (!allow_incomplete)
9404 error ("incomplete type unification");
9405 return 2;
9408 return 0;
9411 /* Subroutine of type_unification_real. Args are like the variables at the
9412 call site. ARG is an overloaded function (or template-id); we try
9413 deducing template args from each of the overloads, and if only one
9414 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9416 static int
9417 resolve_overloaded_unification (tree tparms,
9418 tree targs,
9419 tree parm,
9420 tree arg,
9421 unification_kind_t strict,
9422 int sub_strict)
9424 tree tempargs = copy_node (targs);
9425 int good = 0;
9426 bool addr_p;
9428 if (TREE_CODE (arg) == ADDR_EXPR)
9430 arg = TREE_OPERAND (arg, 0);
9431 addr_p = true;
9433 else
9434 addr_p = false;
9436 if (TREE_CODE (arg) == COMPONENT_REF)
9437 /* Handle `&x' where `x' is some static or non-static member
9438 function name. */
9439 arg = TREE_OPERAND (arg, 1);
9441 if (TREE_CODE (arg) == OFFSET_REF)
9442 arg = TREE_OPERAND (arg, 1);
9444 /* Strip baselink information. */
9445 if (BASELINK_P (arg))
9446 arg = BASELINK_FUNCTIONS (arg);
9448 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9450 /* If we got some explicit template args, we need to plug them into
9451 the affected templates before we try to unify, in case the
9452 explicit args will completely resolve the templates in question. */
9454 tree expl_subargs = TREE_OPERAND (arg, 1);
9455 arg = TREE_OPERAND (arg, 0);
9457 for (; arg; arg = OVL_NEXT (arg))
9459 tree fn = OVL_CURRENT (arg);
9460 tree subargs, elem;
9462 if (TREE_CODE (fn) != TEMPLATE_DECL)
9463 continue;
9465 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9466 expl_subargs, /*check_ret=*/false);
9467 if (subargs)
9469 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9470 good += try_one_overload (tparms, targs, tempargs, parm,
9471 elem, strict, sub_strict, addr_p);
9475 else
9477 gcc_assert (TREE_CODE (arg) == OVERLOAD
9478 || TREE_CODE (arg) == FUNCTION_DECL);
9480 for (; arg; arg = OVL_NEXT (arg))
9481 good += try_one_overload (tparms, targs, tempargs, parm,
9482 TREE_TYPE (OVL_CURRENT (arg)),
9483 strict, sub_strict, addr_p);
9486 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9487 to function or pointer to member function argument if the set of
9488 overloaded functions does not contain function templates and at most
9489 one of a set of overloaded functions provides a unique match.
9491 So if we found multiple possibilities, we return success but don't
9492 deduce anything. */
9494 if (good == 1)
9496 int i = TREE_VEC_LENGTH (targs);
9497 for (; i--; )
9498 if (TREE_VEC_ELT (tempargs, i))
9499 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9501 if (good)
9502 return 0;
9504 return 1;
9507 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9508 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9509 different overloads deduce different arguments for a given parm.
9510 ADDR_P is true if the expression for which deduction is being
9511 performed was of the form "& fn" rather than simply "fn".
9513 Returns 1 on success. */
9515 static int
9516 try_one_overload (tree tparms,
9517 tree orig_targs,
9518 tree targs,
9519 tree parm,
9520 tree arg,
9521 unification_kind_t strict,
9522 int sub_strict,
9523 bool addr_p)
9525 int nargs;
9526 tree tempargs;
9527 int i;
9529 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9530 to function or pointer to member function argument if the set of
9531 overloaded functions does not contain function templates and at most
9532 one of a set of overloaded functions provides a unique match.
9534 So if this is a template, just return success. */
9536 if (uses_template_parms (arg))
9537 return 1;
9539 if (TREE_CODE (arg) == METHOD_TYPE)
9540 arg = build_ptrmemfunc_type (build_pointer_type (arg));
9541 else if (addr_p)
9542 arg = build_pointer_type (arg);
9544 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9546 /* We don't copy orig_targs for this because if we have already deduced
9547 some template args from previous args, unify would complain when we
9548 try to deduce a template parameter for the same argument, even though
9549 there isn't really a conflict. */
9550 nargs = TREE_VEC_LENGTH (targs);
9551 tempargs = make_tree_vec (nargs);
9553 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9554 return 0;
9556 /* First make sure we didn't deduce anything that conflicts with
9557 explicitly specified args. */
9558 for (i = nargs; i--; )
9560 tree elt = TREE_VEC_ELT (tempargs, i);
9561 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9563 if (!elt)
9564 /*NOP*/;
9565 else if (uses_template_parms (elt))
9566 /* Since we're unifying against ourselves, we will fill in
9567 template args used in the function parm list with our own
9568 template parms. Discard them. */
9569 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9570 else if (oldelt && !template_args_equal (oldelt, elt))
9571 return 0;
9574 for (i = nargs; i--; )
9576 tree elt = TREE_VEC_ELT (tempargs, i);
9578 if (elt)
9579 TREE_VEC_ELT (targs, i) = elt;
9582 return 1;
9585 /* Verify that nondeduce template argument agrees with the type
9586 obtained from argument deduction. Return nonzero if the
9587 verification fails.
9589 For example:
9591 struct A { typedef int X; };
9592 template <class T, class U> struct C {};
9593 template <class T> struct C<T, typename T::X> {};
9595 Then with the instantiation `C<A, int>', we can deduce that
9596 `T' is `A' but unify () does not check whether `typename T::X'
9597 is `int'. This function ensure that they agree.
9599 TARGS, PARMS are the same as the arguments of unify.
9600 ARGS contains template arguments from all levels. */
9602 static int
9603 verify_class_unification (tree targs, tree parms, tree args)
9605 parms = tsubst (parms, add_outermost_template_args (args, targs),
9606 tf_none, NULL_TREE);
9607 if (parms == error_mark_node)
9608 return 1;
9610 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9613 /* PARM is a template class (perhaps with unbound template
9614 parameters). ARG is a fully instantiated type. If ARG can be
9615 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9616 TARGS are as for unify. */
9618 static tree
9619 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
9621 tree copy_of_targs;
9623 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9624 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
9625 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
9626 return NULL_TREE;
9628 /* We need to make a new template argument vector for the call to
9629 unify. If we used TARGS, we'd clutter it up with the result of
9630 the attempted unification, even if this class didn't work out.
9631 We also don't want to commit ourselves to all the unifications
9632 we've already done, since unification is supposed to be done on
9633 an argument-by-argument basis. In other words, consider the
9634 following pathological case:
9636 template <int I, int J, int K>
9637 struct S {};
9639 template <int I, int J>
9640 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9642 template <int I, int J, int K>
9643 void f(S<I, J, K>, S<I, I, I>);
9645 void g() {
9646 S<0, 0, 0> s0;
9647 S<0, 1, 2> s2;
9649 f(s0, s2);
9652 Now, by the time we consider the unification involving `s2', we
9653 already know that we must have `f<0, 0, 0>'. But, even though
9654 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9655 because there are two ways to unify base classes of S<0, 1, 2>
9656 with S<I, I, I>. If we kept the already deduced knowledge, we
9657 would reject the possibility I=1. */
9658 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9660 /* If unification failed, we're done. */
9661 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9662 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9663 return NULL_TREE;
9665 return arg;
9668 /* Given a template type PARM and a class type ARG, find the unique
9669 base type in ARG that is an instance of PARM. We do not examine
9670 ARG itself; only its base-classes. If there is not exactly one
9671 appropriate base class, return NULL_TREE. PARM may be the type of
9672 a partial specialization, as well as a plain template type. Used
9673 by unify. */
9675 static tree
9676 get_template_base (tree tparms, tree targs, tree parm, tree arg)
9678 tree rval = NULL_TREE;
9679 tree binfo;
9681 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
9683 binfo = TYPE_BINFO (complete_type (arg));
9684 if (!binfo)
9685 /* The type could not be completed. */
9686 return NULL_TREE;
9688 /* Walk in inheritance graph order. The search order is not
9689 important, and this avoids multiple walks of virtual bases. */
9690 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
9692 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
9694 if (r)
9696 /* If there is more than one satisfactory baseclass, then:
9698 [temp.deduct.call]
9700 If they yield more than one possible deduced A, the type
9701 deduction fails.
9703 applies. */
9704 if (rval && !same_type_p (r, rval))
9705 return NULL_TREE;
9707 rval = r;
9711 return rval;
9714 /* Returns the level of DECL, which declares a template parameter. */
9716 static int
9717 template_decl_level (tree decl)
9719 switch (TREE_CODE (decl))
9721 case TYPE_DECL:
9722 case TEMPLATE_DECL:
9723 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9725 case PARM_DECL:
9726 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9728 default:
9729 gcc_unreachable ();
9731 return 0;
9734 /* Decide whether ARG can be unified with PARM, considering only the
9735 cv-qualifiers of each type, given STRICT as documented for unify.
9736 Returns nonzero iff the unification is OK on that basis. */
9738 static int
9739 check_cv_quals_for_unify (int strict, tree arg, tree parm)
9741 int arg_quals = cp_type_quals (arg);
9742 int parm_quals = cp_type_quals (parm);
9744 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9745 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9747 /* Although a CVR qualifier is ignored when being applied to a
9748 substituted template parameter ([8.3.2]/1 for example), that
9749 does not apply during deduction [14.8.2.4]/1, (even though
9750 that is not explicitly mentioned, [14.8.2.4]/9 indicates
9751 this). Except when we're allowing additional CV qualifiers
9752 at the outer level [14.8.2.1]/3,1st bullet. */
9753 if ((TREE_CODE (arg) == REFERENCE_TYPE
9754 || TREE_CODE (arg) == FUNCTION_TYPE
9755 || TREE_CODE (arg) == METHOD_TYPE)
9756 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
9757 return 0;
9759 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9760 && (parm_quals & TYPE_QUAL_RESTRICT))
9761 return 0;
9764 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9765 && (arg_quals & parm_quals) != parm_quals)
9766 return 0;
9768 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9769 && (parm_quals & arg_quals) != arg_quals)
9770 return 0;
9772 return 1;
9775 /* Takes parameters as for type_unification. Returns 0 if the
9776 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9777 bitwise or of the following flags:
9779 UNIFY_ALLOW_NONE:
9780 Require an exact match between PARM and ARG.
9781 UNIFY_ALLOW_MORE_CV_QUAL:
9782 Allow the deduced ARG to be more cv-qualified (by qualification
9783 conversion) than ARG.
9784 UNIFY_ALLOW_LESS_CV_QUAL:
9785 Allow the deduced ARG to be less cv-qualified than ARG.
9786 UNIFY_ALLOW_DERIVED:
9787 Allow the deduced ARG to be a template base class of ARG,
9788 or a pointer to a template base class of the type pointed to by
9789 ARG.
9790 UNIFY_ALLOW_INTEGER:
9791 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9792 case for more information.
9793 UNIFY_ALLOW_OUTER_LEVEL:
9794 This is the outermost level of a deduction. Used to determine validity
9795 of qualification conversions. A valid qualification conversion must
9796 have const qualified pointers leading up to the inner type which
9797 requires additional CV quals, except at the outer level, where const
9798 is not required [conv.qual]. It would be normal to set this flag in
9799 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9800 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9801 This is the outermost level of a deduction, and PARM can be more CV
9802 qualified at this point.
9803 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9804 This is the outermost level of a deduction, and PARM can be less CV
9805 qualified at this point. */
9807 static int
9808 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
9810 int idx;
9811 tree targ;
9812 tree tparm;
9813 int strict_in = strict;
9815 /* I don't think this will do the right thing with respect to types.
9816 But the only case I've seen it in so far has been array bounds, where
9817 signedness is the only information lost, and I think that will be
9818 okay. */
9819 while (TREE_CODE (parm) == NOP_EXPR)
9820 parm = TREE_OPERAND (parm, 0);
9822 if (arg == error_mark_node)
9823 return 1;
9824 if (arg == unknown_type_node)
9825 /* We can't deduce anything from this, but we might get all the
9826 template args from other function args. */
9827 return 0;
9829 /* If PARM uses template parameters, then we can't bail out here,
9830 even if ARG == PARM, since we won't record unifications for the
9831 template parameters. We might need them if we're trying to
9832 figure out which of two things is more specialized. */
9833 if (arg == parm && !uses_template_parms (parm))
9834 return 0;
9836 /* Immediately reject some pairs that won't unify because of
9837 cv-qualification mismatches. */
9838 if (TREE_CODE (arg) == TREE_CODE (parm)
9839 && TYPE_P (arg)
9840 /* It is the elements of the array which hold the cv quals of an array
9841 type, and the elements might be template type parms. We'll check
9842 when we recurse. */
9843 && TREE_CODE (arg) != ARRAY_TYPE
9844 /* We check the cv-qualifiers when unifying with template type
9845 parameters below. We want to allow ARG `const T' to unify with
9846 PARM `T' for example, when computing which of two templates
9847 is more specialized, for example. */
9848 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9849 && !check_cv_quals_for_unify (strict_in, arg, parm))
9850 return 1;
9852 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9853 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9854 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9855 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9856 strict &= ~UNIFY_ALLOW_DERIVED;
9857 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9858 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9860 switch (TREE_CODE (parm))
9862 case TYPENAME_TYPE:
9863 case SCOPE_REF:
9864 case UNBOUND_CLASS_TEMPLATE:
9865 /* In a type which contains a nested-name-specifier, template
9866 argument values cannot be deduced for template parameters used
9867 within the nested-name-specifier. */
9868 return 0;
9870 case TEMPLATE_TYPE_PARM:
9871 case TEMPLATE_TEMPLATE_PARM:
9872 case BOUND_TEMPLATE_TEMPLATE_PARM:
9873 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9875 if (TEMPLATE_TYPE_LEVEL (parm)
9876 != template_decl_level (tparm))
9877 /* The PARM is not one we're trying to unify. Just check
9878 to see if it matches ARG. */
9879 return (TREE_CODE (arg) == TREE_CODE (parm)
9880 && same_type_p (parm, arg)) ? 0 : 1;
9881 idx = TEMPLATE_TYPE_IDX (parm);
9882 targ = TREE_VEC_ELT (targs, idx);
9883 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9885 /* Check for mixed types and values. */
9886 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9887 && TREE_CODE (tparm) != TYPE_DECL)
9888 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9889 && TREE_CODE (tparm) != TEMPLATE_DECL))
9890 return 1;
9892 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9894 /* ARG must be constructed from a template class or a template
9895 template parameter. */
9896 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9897 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9898 return 1;
9901 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9902 tree parmvec = TYPE_TI_ARGS (parm);
9903 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
9904 tree argtmplvec
9905 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9906 int i;
9908 /* The parameter and argument roles have to be switched here
9909 in order to handle default arguments properly. For example,
9910 template<template <class> class TT> void f(TT<int>)
9911 should be able to accept vector<int> which comes from
9912 template <class T, class Allocator = allocator>
9913 class vector. */
9915 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9916 == error_mark_node)
9917 return 1;
9919 /* Deduce arguments T, i from TT<T> or TT<i>.
9920 We check each element of PARMVEC and ARGVEC individually
9921 rather than the whole TREE_VEC since they can have
9922 different number of elements. */
9924 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9926 if (unify (tparms, targs,
9927 TREE_VEC_ELT (parmvec, i),
9928 TREE_VEC_ELT (argvec, i),
9929 UNIFY_ALLOW_NONE))
9930 return 1;
9933 arg = TYPE_TI_TEMPLATE (arg);
9935 /* Fall through to deduce template name. */
9938 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9939 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9941 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9943 /* Simple cases: Value already set, does match or doesn't. */
9944 if (targ != NULL_TREE && template_args_equal (targ, arg))
9945 return 0;
9946 else if (targ)
9947 return 1;
9949 else
9951 /* If PARM is `const T' and ARG is only `int', we don't have
9952 a match unless we are allowing additional qualification.
9953 If ARG is `const int' and PARM is just `T' that's OK;
9954 that binds `const int' to `T'. */
9955 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9956 arg, parm))
9957 return 1;
9959 /* Consider the case where ARG is `const volatile int' and
9960 PARM is `const T'. Then, T should be `volatile int'. */
9961 arg = cp_build_qualified_type_real
9962 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9963 if (arg == error_mark_node)
9964 return 1;
9966 /* Simple cases: Value already set, does match or doesn't. */
9967 if (targ != NULL_TREE && same_type_p (targ, arg))
9968 return 0;
9969 else if (targ)
9970 return 1;
9972 /* Make sure that ARG is not a variable-sized array. (Note
9973 that were talking about variable-sized arrays (like
9974 `int[n]'), rather than arrays of unknown size (like
9975 `int[]').) We'll get very confused by such a type since
9976 the bound of the array will not be computable in an
9977 instantiation. Besides, such types are not allowed in
9978 ISO C++, so we can do as we please here. */
9979 if (variably_modified_type_p (arg, NULL_TREE))
9980 return 1;
9983 TREE_VEC_ELT (targs, idx) = arg;
9984 return 0;
9986 case TEMPLATE_PARM_INDEX:
9987 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9989 if (TEMPLATE_PARM_LEVEL (parm)
9990 != template_decl_level (tparm))
9991 /* The PARM is not one we're trying to unify. Just check
9992 to see if it matches ARG. */
9993 return !(TREE_CODE (arg) == TREE_CODE (parm)
9994 && cp_tree_equal (parm, arg));
9996 idx = TEMPLATE_PARM_IDX (parm);
9997 targ = TREE_VEC_ELT (targs, idx);
9999 if (targ)
10000 return !cp_tree_equal (targ, arg);
10002 /* [temp.deduct.type] If, in the declaration of a function template
10003 with a non-type template-parameter, the non-type
10004 template-parameter is used in an expression in the function
10005 parameter-list and, if the corresponding template-argument is
10006 deduced, the template-argument type shall match the type of the
10007 template-parameter exactly, except that a template-argument
10008 deduced from an array bound may be of any integral type.
10009 The non-type parameter might use already deduced type parameters. */
10010 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10011 if (!TREE_TYPE (arg))
10012 /* Template-parameter dependent expression. Just accept it for now.
10013 It will later be processed in convert_template_argument. */
10015 else if (same_type_p (TREE_TYPE (arg), tparm))
10016 /* OK */;
10017 else if ((strict & UNIFY_ALLOW_INTEGER)
10018 && (TREE_CODE (tparm) == INTEGER_TYPE
10019 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10020 /* Convert the ARG to the type of PARM; the deduced non-type
10021 template argument must exactly match the types of the
10022 corresponding parameter. */
10023 arg = fold (build_nop (TREE_TYPE (parm), arg));
10024 else if (uses_template_parms (tparm))
10025 /* We haven't deduced the type of this parameter yet. Try again
10026 later. */
10027 return 0;
10028 else
10029 return 1;
10031 TREE_VEC_ELT (targs, idx) = arg;
10032 return 0;
10034 case PTRMEM_CST:
10036 /* A pointer-to-member constant can be unified only with
10037 another constant. */
10038 if (TREE_CODE (arg) != PTRMEM_CST)
10039 return 1;
10041 /* Just unify the class member. It would be useless (and possibly
10042 wrong, depending on the strict flags) to unify also
10043 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10044 arg refer to the same variable, even if through different
10045 classes. For instance:
10047 struct A { int x; };
10048 struct B : A { };
10050 Unification of &A::x and &B::x must succeed. */
10051 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10052 PTRMEM_CST_MEMBER (arg), strict);
10055 case POINTER_TYPE:
10057 if (TREE_CODE (arg) != POINTER_TYPE)
10058 return 1;
10060 /* [temp.deduct.call]
10062 A can be another pointer or pointer to member type that can
10063 be converted to the deduced A via a qualification
10064 conversion (_conv.qual_).
10066 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10067 This will allow for additional cv-qualification of the
10068 pointed-to types if appropriate. */
10070 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10071 /* The derived-to-base conversion only persists through one
10072 level of pointers. */
10073 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10075 return unify (tparms, targs, TREE_TYPE (parm),
10076 TREE_TYPE (arg), strict);
10079 case REFERENCE_TYPE:
10080 if (TREE_CODE (arg) != REFERENCE_TYPE)
10081 return 1;
10082 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10083 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10085 case ARRAY_TYPE:
10086 if (TREE_CODE (arg) != ARRAY_TYPE)
10087 return 1;
10088 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10089 != (TYPE_DOMAIN (arg) == NULL_TREE))
10090 return 1;
10091 if (TYPE_DOMAIN (parm) != NULL_TREE)
10093 tree parm_max;
10094 tree arg_max;
10096 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10097 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10099 /* Our representation of array types uses "N - 1" as the
10100 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10101 not an integer constant. */
10102 if (TREE_CODE (parm_max) == MINUS_EXPR)
10104 arg_max = fold_build2 (PLUS_EXPR,
10105 integer_type_node,
10106 arg_max,
10107 TREE_OPERAND (parm_max, 1));
10108 parm_max = TREE_OPERAND (parm_max, 0);
10111 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10112 return 1;
10114 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10115 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10117 case REAL_TYPE:
10118 case COMPLEX_TYPE:
10119 case VECTOR_TYPE:
10120 case INTEGER_TYPE:
10121 case BOOLEAN_TYPE:
10122 case ENUMERAL_TYPE:
10123 case VOID_TYPE:
10124 if (TREE_CODE (arg) != TREE_CODE (parm))
10125 return 1;
10127 /* We have already checked cv-qualification at the top of the
10128 function. */
10129 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10130 return 1;
10132 /* As far as unification is concerned, this wins. Later checks
10133 will invalidate it if necessary. */
10134 return 0;
10136 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10137 /* Type INTEGER_CST can come from ordinary constant template args. */
10138 case INTEGER_CST:
10139 while (TREE_CODE (arg) == NOP_EXPR)
10140 arg = TREE_OPERAND (arg, 0);
10142 if (TREE_CODE (arg) != INTEGER_CST)
10143 return 1;
10144 return !tree_int_cst_equal (parm, arg);
10146 case TREE_VEC:
10148 int i;
10149 if (TREE_CODE (arg) != TREE_VEC)
10150 return 1;
10151 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10152 return 1;
10153 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10154 if (unify (tparms, targs,
10155 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10156 UNIFY_ALLOW_NONE))
10157 return 1;
10158 return 0;
10161 case RECORD_TYPE:
10162 case UNION_TYPE:
10163 if (TREE_CODE (arg) != TREE_CODE (parm))
10164 return 1;
10166 if (TYPE_PTRMEMFUNC_P (parm))
10168 if (!TYPE_PTRMEMFUNC_P (arg))
10169 return 1;
10171 return unify (tparms, targs,
10172 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10173 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10174 strict);
10177 if (CLASSTYPE_TEMPLATE_INFO (parm))
10179 tree t = NULL_TREE;
10181 if (strict_in & UNIFY_ALLOW_DERIVED)
10183 /* First, we try to unify the PARM and ARG directly. */
10184 t = try_class_unification (tparms, targs,
10185 parm, arg);
10187 if (!t)
10189 /* Fallback to the special case allowed in
10190 [temp.deduct.call]:
10192 If P is a class, and P has the form
10193 template-id, then A can be a derived class of
10194 the deduced A. Likewise, if P is a pointer to
10195 a class of the form template-id, A can be a
10196 pointer to a derived class pointed to by the
10197 deduced A. */
10198 t = get_template_base (tparms, targs, parm, arg);
10200 if (!t)
10201 return 1;
10204 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10205 && (CLASSTYPE_TI_TEMPLATE (parm)
10206 == CLASSTYPE_TI_TEMPLATE (arg)))
10207 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10208 Then, we should unify `int' and `U'. */
10209 t = arg;
10210 else
10211 /* There's no chance of unification succeeding. */
10212 return 1;
10214 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10215 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10217 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10218 return 1;
10219 return 0;
10221 case METHOD_TYPE:
10222 case FUNCTION_TYPE:
10223 if (TREE_CODE (arg) != TREE_CODE (parm))
10224 return 1;
10226 if (unify (tparms, targs, TREE_TYPE (parm),
10227 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10228 return 1;
10229 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10230 TYPE_ARG_TYPES (arg), 1,
10231 DEDUCE_EXACT, 0);
10233 case OFFSET_TYPE:
10234 /* Unify a pointer to member with a pointer to member function, which
10235 deduces the type of the member as a function type. */
10236 if (TYPE_PTRMEMFUNC_P (arg))
10238 tree method_type;
10239 tree fntype;
10240 cp_cv_quals cv_quals;
10242 /* Check top-level cv qualifiers */
10243 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10244 return 1;
10246 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10247 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10248 return 1;
10250 /* Determine the type of the function we are unifying against. */
10251 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10252 fntype =
10253 build_function_type (TREE_TYPE (method_type),
10254 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10256 /* Extract the cv-qualifiers of the member function from the
10257 implicit object parameter and place them on the function
10258 type to be restored later. */
10259 cv_quals =
10260 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10261 fntype = build_qualified_type (fntype, cv_quals);
10262 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10265 if (TREE_CODE (arg) != OFFSET_TYPE)
10266 return 1;
10267 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10268 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10269 return 1;
10270 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10271 strict);
10273 case CONST_DECL:
10274 if (DECL_TEMPLATE_PARM_P (parm))
10275 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10276 if (arg != integral_constant_value (parm))
10277 return 1;
10278 return 0;
10280 case FIELD_DECL:
10281 case TEMPLATE_DECL:
10282 /* Matched cases are handled by the ARG == PARM test above. */
10283 return 1;
10285 default:
10286 gcc_assert (EXPR_P (parm));
10288 /* We must be looking at an expression. This can happen with
10289 something like:
10291 template <int I>
10292 void foo(S<I>, S<I + 2>);
10294 This is a "nondeduced context":
10296 [deduct.type]
10298 The nondeduced contexts are:
10300 --A type that is a template-id in which one or more of
10301 the template-arguments is an expression that references
10302 a template-parameter.
10304 In these cases, we assume deduction succeeded, but don't
10305 actually infer any unifications. */
10307 if (!uses_template_parms (parm)
10308 && !template_args_equal (parm, arg))
10309 return 1;
10310 else
10311 return 0;
10315 /* Note that DECL can be defined in this translation unit, if
10316 required. */
10318 static void
10319 mark_definable (tree decl)
10321 tree clone;
10322 DECL_NOT_REALLY_EXTERN (decl) = 1;
10323 FOR_EACH_CLONE (clone, decl)
10324 DECL_NOT_REALLY_EXTERN (clone) = 1;
10327 /* Called if RESULT is explicitly instantiated, or is a member of an
10328 explicitly instantiated class. */
10330 void
10331 mark_decl_instantiated (tree result, int extern_p)
10333 SET_DECL_EXPLICIT_INSTANTIATION (result);
10335 /* If this entity has already been written out, it's too late to
10336 make any modifications. */
10337 if (TREE_ASM_WRITTEN (result))
10338 return;
10340 if (TREE_CODE (result) != FUNCTION_DECL)
10341 /* The TREE_PUBLIC flag for function declarations will have been
10342 set correctly by tsubst. */
10343 TREE_PUBLIC (result) = 1;
10345 /* This might have been set by an earlier implicit instantiation. */
10346 DECL_COMDAT (result) = 0;
10348 if (extern_p)
10349 DECL_NOT_REALLY_EXTERN (result) = 0;
10350 else
10352 mark_definable (result);
10353 /* Always make artificials weak. */
10354 if (DECL_ARTIFICIAL (result) && flag_weak)
10355 comdat_linkage (result);
10356 /* For WIN32 we also want to put explicit instantiations in
10357 linkonce sections. */
10358 else if (TREE_PUBLIC (result))
10359 maybe_make_one_only (result);
10362 /* If EXTERN_P, then this function will not be emitted -- unless
10363 followed by an explicit instantiation, at which point its linkage
10364 will be adjusted. If !EXTERN_P, then this function will be
10365 emitted here. In neither circumstance do we want
10366 import_export_decl to adjust the linkage. */
10367 DECL_INTERFACE_KNOWN (result) = 1;
10370 /* Given two function templates PAT1 and PAT2, return:
10372 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10373 -1 if PAT2 is more specialized than PAT1.
10374 0 if neither is more specialized.
10376 LEN indicates the number of parameters we should consider
10377 (defaulted parameters should not be considered).
10379 The 1998 std underspecified function template partial ordering, and
10380 DR214 addresses the issue. We take pairs of arguments, one from
10381 each of the templates, and deduce them against each other. One of
10382 the templates will be more specialized if all the *other*
10383 template's arguments deduce against its arguments and at least one
10384 of its arguments *does* *not* deduce against the other template's
10385 corresponding argument. Deduction is done as for class templates.
10386 The arguments used in deduction have reference and top level cv
10387 qualifiers removed. Iff both arguments were originally reference
10388 types *and* deduction succeeds in both directions, the template
10389 with the more cv-qualified argument wins for that pairing (if
10390 neither is more cv-qualified, they both are equal). Unlike regular
10391 deduction, after all the arguments have been deduced in this way,
10392 we do *not* verify the deduced template argument values can be
10393 substituted into non-deduced contexts, nor do we have to verify
10394 that all template arguments have been deduced. */
10397 more_specialized_fn (tree pat1, tree pat2, int len)
10399 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10400 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10401 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10402 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10403 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10404 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10405 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10406 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10407 int better1 = 0;
10408 int better2 = 0;
10410 /* If only one is a member function, they are unordered. */
10411 if (DECL_FUNCTION_MEMBER_P (decl1) != DECL_FUNCTION_MEMBER_P (decl2))
10412 return 0;
10414 /* Don't consider 'this' parameter. */
10415 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10416 args1 = TREE_CHAIN (args1);
10417 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10418 args2 = TREE_CHAIN (args2);
10420 /* If only one is a conversion operator, they are unordered. */
10421 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10422 return 0;
10424 /* Consider the return type for a conversion function */
10425 if (DECL_CONV_FN_P (decl1))
10427 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10428 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10429 len++;
10432 processing_template_decl++;
10434 while (len--)
10436 tree arg1 = TREE_VALUE (args1);
10437 tree arg2 = TREE_VALUE (args2);
10438 int deduce1, deduce2;
10439 int quals1 = -1;
10440 int quals2 = -1;
10442 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10444 arg1 = TREE_TYPE (arg1);
10445 quals1 = cp_type_quals (arg1);
10448 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10450 arg2 = TREE_TYPE (arg2);
10451 quals2 = cp_type_quals (arg2);
10454 if ((quals1 < 0) != (quals2 < 0))
10456 /* Only of the args is a reference, see if we should apply
10457 array/function pointer decay to it. This is not part of
10458 DR214, but is, IMHO, consistent with the deduction rules
10459 for the function call itself, and with our earlier
10460 implementation of the underspecified partial ordering
10461 rules. (nathan). */
10462 if (quals1 >= 0)
10464 switch (TREE_CODE (arg1))
10466 case ARRAY_TYPE:
10467 arg1 = TREE_TYPE (arg1);
10468 /* FALLTHROUGH. */
10469 case FUNCTION_TYPE:
10470 arg1 = build_pointer_type (arg1);
10471 break;
10473 default:
10474 break;
10477 else
10479 switch (TREE_CODE (arg2))
10481 case ARRAY_TYPE:
10482 arg2 = TREE_TYPE (arg2);
10483 /* FALLTHROUGH. */
10484 case FUNCTION_TYPE:
10485 arg2 = build_pointer_type (arg2);
10486 break;
10488 default:
10489 break;
10494 arg1 = TYPE_MAIN_VARIANT (arg1);
10495 arg2 = TYPE_MAIN_VARIANT (arg2);
10497 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10498 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10500 if (!deduce1)
10501 better2 = -1;
10502 if (!deduce2)
10503 better1 = -1;
10504 if (better1 < 0 && better2 < 0)
10505 /* We've failed to deduce something in either direction.
10506 These must be unordered. */
10507 break;
10509 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10511 /* Deduces in both directions, see if quals can
10512 disambiguate. Pretend the worse one failed to deduce. */
10513 if ((quals1 & quals2) == quals2)
10514 deduce1 = 0;
10515 if ((quals1 & quals2) == quals1)
10516 deduce2 = 0;
10518 if (deduce1 && !deduce2 && !better2)
10519 better2 = 1;
10520 if (deduce2 && !deduce1 && !better1)
10521 better1 = 1;
10523 args1 = TREE_CHAIN (args1);
10524 args2 = TREE_CHAIN (args2);
10527 processing_template_decl--;
10529 return (better1 > 0) - (better2 > 0);
10532 /* Given two class template specialization list nodes PAT1 and PAT2, return:
10534 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
10535 -1 if PAT2 is more specialized than PAT1.
10536 0 if neither is more specialized.
10538 FULL_ARGS is the full set of template arguments that triggers this
10539 partial ordering. */
10542 more_specialized_class (tree pat1, tree pat2, tree full_args)
10544 tree targs;
10545 int winner = 0;
10547 /* Just like what happens for functions, if we are ordering between
10548 different class template specializations, we may encounter dependent
10549 types in the arguments, and we need our dependency check functions
10550 to behave correctly. */
10551 ++processing_template_decl;
10552 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
10553 add_outermost_template_args (full_args, TREE_PURPOSE (pat2)));
10554 if (targs)
10555 --winner;
10557 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
10558 add_outermost_template_args (full_args, TREE_PURPOSE (pat1)));
10559 if (targs)
10560 ++winner;
10561 --processing_template_decl;
10563 return winner;
10566 /* Return the template arguments that will produce the function signature
10567 DECL from the function template FN, with the explicit template
10568 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
10569 also match. Return NULL_TREE if no satisfactory arguments could be
10570 found. */
10572 static tree
10573 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
10575 int ntparms = DECL_NTPARMS (fn);
10576 tree targs = make_tree_vec (ntparms);
10577 tree decl_type;
10578 tree decl_arg_types;
10580 /* Substitute the explicit template arguments into the type of DECL.
10581 The call to fn_type_unification will handle substitution into the
10582 FN. */
10583 decl_type = TREE_TYPE (decl);
10584 if (explicit_args && uses_template_parms (decl_type))
10586 tree tmpl;
10587 tree converted_args;
10589 if (DECL_TEMPLATE_INFO (decl))
10590 tmpl = DECL_TI_TEMPLATE (decl);
10591 else
10592 /* We can get here for some invalid specializations. */
10593 return NULL_TREE;
10595 converted_args
10596 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10597 explicit_args, NULL_TREE,
10598 tf_none, /*require_all_arguments=*/0));
10599 if (converted_args == error_mark_node)
10600 return NULL_TREE;
10602 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10603 if (decl_type == error_mark_node)
10604 return NULL_TREE;
10607 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10608 /* Never do unification on the 'this' parameter. */
10609 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10610 decl_arg_types = TREE_CHAIN (decl_arg_types);
10612 if (fn_type_unification (fn, explicit_args, targs,
10613 decl_arg_types,
10614 (check_rettype || DECL_CONV_FN_P (fn)
10615 ? TREE_TYPE (decl_type) : NULL_TREE),
10616 DEDUCE_EXACT))
10617 return NULL_TREE;
10619 return targs;
10622 /* Return the innermost template arguments that, when applied to a
10623 template specialization whose innermost template parameters are
10624 TPARMS, and whose specialization arguments are PARMS, yield the
10625 ARGS.
10627 For example, suppose we have:
10629 template <class T, class U> struct S {};
10630 template <class T> struct S<T*, int> {};
10632 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10633 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10634 int}. The resulting vector will be {double}, indicating that `T'
10635 is bound to `double'. */
10637 static tree
10638 get_class_bindings (tree tparms, tree parms, tree args)
10640 int i, ntparms = TREE_VEC_LENGTH (tparms);
10641 tree vec = make_tree_vec (ntparms);
10643 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10644 UNIFY_ALLOW_NONE))
10645 return NULL_TREE;
10647 for (i = 0; i < ntparms; ++i)
10648 if (! TREE_VEC_ELT (vec, i))
10649 return NULL_TREE;
10651 if (verify_class_unification (vec, parms, args))
10652 return NULL_TREE;
10654 return vec;
10657 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10658 Pick the most specialized template, and return the corresponding
10659 instantiation, or if there is no corresponding instantiation, the
10660 template itself. If there is no most specialized template,
10661 error_mark_node is returned. If there are no templates at all,
10662 NULL_TREE is returned. */
10664 tree
10665 most_specialized_instantiation (tree instantiations)
10667 tree fn, champ;
10669 if (!instantiations)
10670 return NULL_TREE;
10672 ++processing_template_decl;
10674 champ = instantiations;
10675 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10677 int fate = 0;
10679 if (get_bindings (TREE_VALUE (champ),
10680 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10681 NULL_TREE, /*check_ret=*/false))
10682 fate--;
10684 if (get_bindings (TREE_VALUE (fn),
10685 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10686 NULL_TREE, /*check_ret=*/false))
10687 fate++;
10689 if (fate != 1)
10691 if (!fate)
10692 /* Equally specialized, move to next function. If there
10693 is no next function, nothing's most specialized. */
10694 fn = TREE_CHAIN (fn);
10695 champ = fn;
10699 if (champ)
10700 /* Now verify that champ is better than everything earlier in the
10701 instantiation list. */
10702 for (fn = instantiations; fn != champ; fn = TREE_CHAIN (fn))
10703 if (get_bindings (TREE_VALUE (champ),
10704 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10705 NULL_TREE, /*check_ret=*/false)
10706 || !get_bindings (TREE_VALUE (fn),
10707 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10708 NULL_TREE, /*check_ret=*/false))
10710 champ = NULL_TREE;
10711 break;
10714 processing_template_decl--;
10716 if (!champ)
10717 return error_mark_node;
10719 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10722 /* Return the most specialized of the list of templates in FNS that can
10723 produce an instantiation matching DECL, given the explicit template
10724 arguments EXPLICIT_ARGS. */
10726 static tree
10727 most_specialized (tree fns, tree decl, tree explicit_args)
10729 tree candidates = NULL_TREE;
10730 tree fn, args;
10732 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10734 tree candidate = TREE_VALUE (fn);
10736 args = get_bindings (candidate, decl, explicit_args, /*check_ret=*/true);
10737 if (args)
10738 candidates = tree_cons (NULL_TREE, candidate, candidates);
10741 return most_specialized_instantiation (candidates);
10744 /* If DECL is a specialization of some template, return the most
10745 general such template. Otherwise, returns NULL_TREE.
10747 For example, given:
10749 template <class T> struct S { template <class U> void f(U); };
10751 if TMPL is `template <class U> void S<int>::f(U)' this will return
10752 the full template. This function will not trace past partial
10753 specializations, however. For example, given in addition:
10755 template <class T> struct S<T*> { template <class U> void f(U); };
10757 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10758 `template <class T> template <class U> S<T*>::f(U)'. */
10760 tree
10761 most_general_template (tree decl)
10763 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10764 an immediate specialization. */
10765 if (TREE_CODE (decl) == FUNCTION_DECL)
10767 if (DECL_TEMPLATE_INFO (decl)) {
10768 decl = DECL_TI_TEMPLATE (decl);
10770 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10771 template friend. */
10772 if (TREE_CODE (decl) != TEMPLATE_DECL)
10773 return NULL_TREE;
10774 } else
10775 return NULL_TREE;
10778 /* Look for more and more general templates. */
10779 while (DECL_TEMPLATE_INFO (decl))
10781 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
10782 (See cp-tree.h for details.) */
10783 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10784 break;
10786 if (CLASS_TYPE_P (TREE_TYPE (decl))
10787 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10788 break;
10790 /* Stop if we run into an explicitly specialized class template. */
10791 if (!DECL_NAMESPACE_SCOPE_P (decl)
10792 && DECL_CONTEXT (decl)
10793 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10794 break;
10796 decl = DECL_TI_TEMPLATE (decl);
10799 return decl;
10802 /* Return the most specialized of the class template specializations
10803 of TMPL which can produce an instantiation matching ARGS, or
10804 error_mark_node if the choice is ambiguous. */
10806 static tree
10807 most_specialized_class (tree tmpl, tree args)
10809 tree list = NULL_TREE;
10810 tree t;
10811 tree champ;
10812 int fate;
10814 tmpl = most_general_template (tmpl);
10815 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10817 tree spec_args
10818 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10819 if (spec_args)
10821 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10822 TREE_TYPE (list) = TREE_TYPE (t);
10826 if (! list)
10827 return NULL_TREE;
10829 t = list;
10830 champ = t;
10831 t = TREE_CHAIN (t);
10832 for (; t; t = TREE_CHAIN (t))
10834 fate = more_specialized_class (champ, t, args);
10835 if (fate == 1)
10837 else
10839 if (fate == 0)
10841 t = TREE_CHAIN (t);
10842 if (! t)
10843 return error_mark_node;
10845 champ = t;
10849 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10851 fate = more_specialized_class (champ, t, args);
10852 if (fate != 1)
10853 return error_mark_node;
10856 return champ;
10859 /* Explicitly instantiate DECL. */
10861 void
10862 do_decl_instantiation (tree decl, tree storage)
10864 tree result = NULL_TREE;
10865 int extern_p = 0;
10867 if (!decl)
10868 /* An error occurred, for which grokdeclarator has already issued
10869 an appropriate message. */
10870 return;
10871 else if (! DECL_LANG_SPECIFIC (decl))
10873 error ("explicit instantiation of non-template %q#D", decl);
10874 return;
10876 else if (TREE_CODE (decl) == VAR_DECL)
10878 /* There is an asymmetry here in the way VAR_DECLs and
10879 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10880 the latter, the DECL we get back will be marked as a
10881 template instantiation, and the appropriate
10882 DECL_TEMPLATE_INFO will be set up. This does not happen for
10883 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10884 should handle VAR_DECLs as it currently handles
10885 FUNCTION_DECLs. */
10886 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10887 if (!result || TREE_CODE (result) != VAR_DECL)
10889 error ("no matching template for %qD found", decl);
10890 return;
10893 else if (TREE_CODE (decl) != FUNCTION_DECL)
10895 error ("explicit instantiation of %q#D", decl);
10896 return;
10898 else
10899 result = decl;
10901 /* Check for various error cases. Note that if the explicit
10902 instantiation is valid the RESULT will currently be marked as an
10903 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10904 until we get here. */
10906 if (DECL_TEMPLATE_SPECIALIZATION (result))
10908 /* DR 259 [temp.spec].
10910 Both an explicit instantiation and a declaration of an explicit
10911 specialization shall not appear in a program unless the explicit
10912 instantiation follows a declaration of the explicit specialization.
10914 For a given set of template parameters, if an explicit
10915 instantiation of a template appears after a declaration of an
10916 explicit specialization for that template, the explicit
10917 instantiation has no effect. */
10918 return;
10920 else if (DECL_EXPLICIT_INSTANTIATION (result))
10922 /* [temp.spec]
10924 No program shall explicitly instantiate any template more
10925 than once.
10927 We check DECL_NOT_REALLY_EXTERN so as not to complain when
10928 the first instantiation was `extern' and the second is not,
10929 and EXTERN_P for the opposite case. */
10930 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
10931 pedwarn ("duplicate explicit instantiation of %q#D", result);
10932 /* If an "extern" explicit instantiation follows an ordinary
10933 explicit instantiation, the template is instantiated. */
10934 if (extern_p)
10935 return;
10937 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10939 error ("no matching template for %qD found", result);
10940 return;
10942 else if (!DECL_TEMPLATE_INFO (result))
10944 pedwarn ("explicit instantiation of non-template %q#D", result);
10945 return;
10948 if (storage == NULL_TREE)
10950 else if (storage == ridpointers[(int) RID_EXTERN])
10952 if (pedantic && !in_system_header)
10953 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
10954 "instantiations");
10955 extern_p = 1;
10957 else
10958 error ("storage class %qD applied to template instantiation", storage);
10960 mark_decl_instantiated (result, extern_p);
10961 if (! extern_p)
10962 instantiate_decl (result, /*defer_ok=*/1, /*undefined_ok=*/0);
10965 void
10966 mark_class_instantiated (tree t, int extern_p)
10968 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10969 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10970 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10971 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10972 if (! extern_p)
10974 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10975 rest_of_type_compilation (t, 1);
10979 /* Called from do_type_instantiation through binding_table_foreach to
10980 do recursive instantiation for the type bound in ENTRY. */
10981 static void
10982 bt_instantiate_type_proc (binding_entry entry, void *data)
10984 tree storage = *(tree *) data;
10986 if (IS_AGGR_TYPE (entry->type)
10987 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
10988 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
10991 /* Called from do_type_instantiation to instantiate a member
10992 (a member function or a static member variable) of an
10993 explicitly instantiated class template. */
10994 static void
10995 instantiate_class_member (tree decl, int extern_p)
10997 mark_decl_instantiated (decl, extern_p);
10998 if (! extern_p)
10999 instantiate_decl (decl, /*defer_ok=*/1, /* undefined_ok=*/1);
11002 /* Perform an explicit instantiation of template class T. STORAGE, if
11003 non-null, is the RID for extern, inline or static. COMPLAIN is
11004 nonzero if this is called from the parser, zero if called recursively,
11005 since the standard is unclear (as detailed below). */
11007 void
11008 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11010 int extern_p = 0;
11011 int nomem_p = 0;
11012 int static_p = 0;
11013 int previous_instantiation_extern_p = 0;
11015 if (TREE_CODE (t) == TYPE_DECL)
11016 t = TREE_TYPE (t);
11018 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11020 error ("explicit instantiation of non-template type %qT", t);
11021 return;
11024 complete_type (t);
11026 if (!COMPLETE_TYPE_P (t))
11028 if (complain & tf_error)
11029 error ("explicit instantiation of %q#T before definition of template",
11031 return;
11034 if (storage != NULL_TREE)
11036 if (pedantic && !in_system_header)
11037 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11038 storage);
11040 if (storage == ridpointers[(int) RID_INLINE])
11041 nomem_p = 1;
11042 else if (storage == ridpointers[(int) RID_EXTERN])
11043 extern_p = 1;
11044 else if (storage == ridpointers[(int) RID_STATIC])
11045 static_p = 1;
11046 else
11048 error ("storage class %qD applied to template instantiation",
11049 storage);
11050 extern_p = 0;
11054 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11056 /* DR 259 [temp.spec].
11058 Both an explicit instantiation and a declaration of an explicit
11059 specialization shall not appear in a program unless the explicit
11060 instantiation follows a declaration of the explicit specialization.
11062 For a given set of template parameters, if an explicit
11063 instantiation of a template appears after a declaration of an
11064 explicit specialization for that template, the explicit
11065 instantiation has no effect. */
11066 return;
11068 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11070 /* [temp.spec]
11072 No program shall explicitly instantiate any template more
11073 than once.
11075 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11076 instantiation was `extern'. If EXTERN_P then the second is.
11077 These cases are OK. */
11078 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11080 if (!previous_instantiation_extern_p && !extern_p
11081 && (complain & tf_error))
11082 pedwarn ("duplicate explicit instantiation of %q#T", t);
11084 /* If we've already instantiated the template, just return now. */
11085 if (!CLASSTYPE_INTERFACE_ONLY (t))
11086 return;
11089 mark_class_instantiated (t, extern_p);
11091 if (nomem_p)
11092 return;
11095 tree tmp;
11097 /* In contrast to implicit instantiation, where only the
11098 declarations, and not the definitions, of members are
11099 instantiated, we have here:
11101 [temp.explicit]
11103 The explicit instantiation of a class template specialization
11104 implies the instantiation of all of its members not
11105 previously explicitly specialized in the translation unit
11106 containing the explicit instantiation.
11108 Of course, we can't instantiate member template classes, since
11109 we don't have any arguments for them. Note that the standard
11110 is unclear on whether the instantiation of the members are
11111 *explicit* instantiations or not. However, the most natural
11112 interpretation is that it should be an explicit instantiation. */
11114 if (! static_p)
11115 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11116 if (TREE_CODE (tmp) == FUNCTION_DECL
11117 && DECL_TEMPLATE_INSTANTIATION (tmp))
11118 instantiate_class_member (tmp, extern_p);
11120 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11121 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11122 instantiate_class_member (tmp, extern_p);
11124 if (CLASSTYPE_NESTED_UTDS (t))
11125 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11126 bt_instantiate_type_proc, &storage);
11130 /* Given a function DECL, which is a specialization of TMPL, modify
11131 DECL to be a re-instantiation of TMPL with the same template
11132 arguments. TMPL should be the template into which tsubst'ing
11133 should occur for DECL, not the most general template.
11135 One reason for doing this is a scenario like this:
11137 template <class T>
11138 void f(const T&, int i);
11140 void g() { f(3, 7); }
11142 template <class T>
11143 void f(const T& t, const int i) { }
11145 Note that when the template is first instantiated, with
11146 instantiate_template, the resulting DECL will have no name for the
11147 first parameter, and the wrong type for the second. So, when we go
11148 to instantiate the DECL, we regenerate it. */
11150 static void
11151 regenerate_decl_from_template (tree decl, tree tmpl)
11153 /* The arguments used to instantiate DECL, from the most general
11154 template. */
11155 tree args;
11156 tree code_pattern;
11158 args = DECL_TI_ARGS (decl);
11159 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11161 /* Make sure that we can see identifiers, and compute access
11162 correctly. */
11163 push_access_scope (decl);
11165 if (TREE_CODE (decl) == FUNCTION_DECL)
11167 tree decl_parm;
11168 tree pattern_parm;
11169 tree specs;
11170 int args_depth;
11171 int parms_depth;
11173 args_depth = TMPL_ARGS_DEPTH (args);
11174 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11175 if (args_depth > parms_depth)
11176 args = get_innermost_template_args (args, parms_depth);
11178 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11179 args, tf_error, NULL_TREE);
11180 if (specs)
11181 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11182 specs);
11184 /* Merge parameter declarations. */
11185 decl_parm = skip_artificial_parms_for (decl,
11186 DECL_ARGUMENTS (decl));
11187 pattern_parm
11188 = skip_artificial_parms_for (code_pattern,
11189 DECL_ARGUMENTS (code_pattern));
11190 while (decl_parm)
11192 tree parm_type;
11194 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11195 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11196 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11197 NULL_TREE);
11198 parm_type = type_decays_to (parm_type);
11199 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11200 TREE_TYPE (decl_parm) = parm_type;
11201 decl_parm = TREE_CHAIN (decl_parm);
11202 pattern_parm = TREE_CHAIN (pattern_parm);
11205 /* Merge additional specifiers from the CODE_PATTERN. */
11206 if (DECL_DECLARED_INLINE_P (code_pattern)
11207 && !DECL_DECLARED_INLINE_P (decl))
11208 DECL_DECLARED_INLINE_P (decl) = 1;
11209 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11210 DECL_INLINE (decl) = 1;
11212 else if (TREE_CODE (decl) == VAR_DECL)
11214 if (!DECL_INITIALIZED_IN_CLASS_P (decl)
11215 && DECL_INITIAL (code_pattern))
11216 DECL_INITIAL (decl) =
11217 tsubst_expr (DECL_INITIAL (code_pattern), args,
11218 tf_error, DECL_TI_TEMPLATE (decl));
11220 else
11221 gcc_unreachable ();
11223 pop_access_scope (decl);
11226 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11227 substituted to get DECL. */
11229 tree
11230 template_for_substitution (tree decl)
11232 tree tmpl = DECL_TI_TEMPLATE (decl);
11234 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11235 for the instantiation. This is not always the most general
11236 template. Consider, for example:
11238 template <class T>
11239 struct S { template <class U> void f();
11240 template <> void f<int>(); };
11242 and an instantiation of S<double>::f<int>. We want TD to be the
11243 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11244 while (/* An instantiation cannot have a definition, so we need a
11245 more general template. */
11246 DECL_TEMPLATE_INSTANTIATION (tmpl)
11247 /* We must also deal with friend templates. Given:
11249 template <class T> struct S {
11250 template <class U> friend void f() {};
11253 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11254 so far as the language is concerned, but that's still
11255 where we get the pattern for the instantiation from. On
11256 other hand, if the definition comes outside the class, say:
11258 template <class T> struct S {
11259 template <class U> friend void f();
11261 template <class U> friend void f() {}
11263 we don't need to look any further. That's what the check for
11264 DECL_INITIAL is for. */
11265 || (TREE_CODE (decl) == FUNCTION_DECL
11266 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11267 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11269 /* The present template, TD, should not be a definition. If it
11270 were a definition, we should be using it! Note that we
11271 cannot restructure the loop to just keep going until we find
11272 a template with a definition, since that might go too far if
11273 a specialization was declared, but not defined. */
11274 gcc_assert (TREE_CODE (decl) != VAR_DECL
11275 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11277 /* Fetch the more general template. */
11278 tmpl = DECL_TI_TEMPLATE (tmpl);
11281 return tmpl;
11284 /* Produce the definition of D, a _DECL generated from a template. If
11285 DEFER_OK is nonzero, then we don't have to actually do the
11286 instantiation now; we just have to do it sometime. Normally it is
11287 an error if this is an explicit instantiation but D is undefined.
11288 If UNDEFINED_OK is nonzero, then instead we treat it as an implicit
11289 instantiation. UNDEFINED_OK is nonzero only if we are being used
11290 to instantiate the members of an explicitly instantiated class
11291 template. */
11294 tree
11295 instantiate_decl (tree d, int defer_ok, int undefined_ok)
11297 tree tmpl = DECL_TI_TEMPLATE (d);
11298 tree gen_args;
11299 tree args;
11300 tree td;
11301 tree code_pattern;
11302 tree spec;
11303 tree gen_tmpl;
11304 int pattern_defined;
11305 int need_push;
11306 location_t saved_loc = input_location;
11308 /* This function should only be used to instantiate templates for
11309 functions and static member variables. */
11310 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11311 || TREE_CODE (d) == VAR_DECL);
11313 /* Variables are never deferred; if instantiation is required, they
11314 are instantiated right away. That allows for better code in the
11315 case that an expression refers to the value of the variable --
11316 if the variable has a constant value the referring expression can
11317 take advantage of that fact. */
11318 if (TREE_CODE (d) == VAR_DECL)
11319 defer_ok = 0;
11321 /* Don't instantiate cloned functions. Instead, instantiate the
11322 functions they cloned. */
11323 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11324 d = DECL_CLONED_FUNCTION (d);
11326 if (DECL_TEMPLATE_INSTANTIATED (d))
11327 /* D has already been instantiated. It might seem reasonable to
11328 check whether or not D is an explicit instantiation, and, if so,
11329 stop here. But when an explicit instantiation is deferred
11330 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11331 is set, even though we still need to do the instantiation. */
11332 return d;
11334 /* If we already have a specialization of this declaration, then
11335 there's no reason to instantiate it. Note that
11336 retrieve_specialization gives us both instantiations and
11337 specializations, so we must explicitly check
11338 DECL_TEMPLATE_SPECIALIZATION. */
11339 gen_tmpl = most_general_template (tmpl);
11340 gen_args = DECL_TI_ARGS (d);
11341 spec = retrieve_specialization (gen_tmpl, gen_args,
11342 /*class_specializations_p=*/false);
11343 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11344 return spec;
11346 /* This needs to happen before any tsubsting. */
11347 if (! push_tinst_level (d))
11348 return d;
11350 timevar_push (TV_PARSE);
11352 /* We may be in the middle of deferred access check. Disable it now. */
11353 push_deferring_access_checks (dk_no_deferred);
11355 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
11356 for the instantiation. */
11357 td = template_for_substitution (d);
11358 code_pattern = DECL_TEMPLATE_RESULT (td);
11360 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11361 || DECL_TEMPLATE_SPECIALIZATION (td))
11362 /* In the case of a friend template whose definition is provided
11363 outside the class, we may have too many arguments. Drop the
11364 ones we don't need. The same is true for specializations. */
11365 args = get_innermost_template_args
11366 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11367 else
11368 args = gen_args;
11370 if (TREE_CODE (d) == FUNCTION_DECL)
11371 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11372 else
11373 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11374 /* Unless an explicit instantiation directive has already determined
11375 the linkage of D, remember that a definition is available for
11376 this entity. */
11377 if (pattern_defined
11378 && !DECL_INTERFACE_KNOWN (d)
11379 && !DECL_NOT_REALLY_EXTERN (d))
11380 mark_definable (d);
11382 input_location = DECL_SOURCE_LOCATION (d);
11384 if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok)
11386 DECL_NOT_REALLY_EXTERN (d) = 0;
11387 SET_DECL_IMPLICIT_INSTANTIATION (d);
11390 if (!defer_ok)
11392 /* Recheck the substitutions to obtain any warning messages
11393 about ignoring cv qualifiers. */
11394 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11395 tree type = TREE_TYPE (gen);
11397 /* Make sure that we can see identifiers, and compute access
11398 correctly. D is already the target FUNCTION_DECL with the
11399 right context. */
11400 push_access_scope (d);
11402 if (TREE_CODE (gen) == FUNCTION_DECL)
11404 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
11405 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11406 tf_error | tf_warning, d);
11407 /* Don't simply tsubst the function type, as that will give
11408 duplicate warnings about poor parameter qualifications.
11409 The function arguments are the same as the decl_arguments
11410 without the top level cv qualifiers. */
11411 type = TREE_TYPE (type);
11413 tsubst (type, gen_args, tf_error | tf_warning, d);
11415 pop_access_scope (d);
11418 /* We should have set up DECL_INITIAL in instantiate_class_template
11419 for in-class definitions of static data members. */
11420 gcc_assert (!(TREE_CODE (d) == VAR_DECL
11421 && DECL_INITIALIZED_IN_CLASS_P (d)
11422 && DECL_INITIAL (d) == NULL_TREE));
11424 /* Do not instantiate templates that we know will be defined
11425 elsewhere. */
11426 if (DECL_INTERFACE_KNOWN (d)
11427 && DECL_REALLY_EXTERN (d)
11428 && ! (TREE_CODE (d) == FUNCTION_DECL
11429 && DECL_INLINE (d)))
11430 goto out;
11431 /* Defer all other templates, unless we have been explicitly
11432 forbidden from doing so. We restore the source position here
11433 because it's used by add_pending_template. */
11434 else if (! pattern_defined || defer_ok)
11436 input_location = saved_loc;
11438 if (at_eof && !pattern_defined
11439 && DECL_EXPLICIT_INSTANTIATION (d))
11440 /* [temp.explicit]
11442 The definition of a non-exported function template, a
11443 non-exported member function template, or a non-exported
11444 member function or static data member of a class template
11445 shall be present in every translation unit in which it is
11446 explicitly instantiated. */
11447 pedwarn
11448 ("explicit instantiation of %qD but no definition available", d);
11450 add_pending_template (d);
11451 goto out;
11453 /* Tell the repository that D is available in this translation unit
11454 -- and see if it is supposed to be instantiated here. */
11455 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11457 /* In a PCH file, despite the fact that the repository hasn't
11458 requested instantiation in the PCH it is still possible that
11459 an instantiation will be required in a file that includes the
11460 PCH. */
11461 if (pch_file)
11462 add_pending_template (d);
11463 /* Instantiate inline functions so that the inliner can do its
11464 job, even though we'll not be emitting a copy of this
11465 function. */
11466 if (!(TREE_CODE (d) == FUNCTION_DECL
11467 && flag_inline_trees
11468 && DECL_DECLARED_INLINE_P (d)))
11469 goto out;
11472 need_push = !cfun || !global_bindings_p ();
11473 if (need_push)
11474 push_to_top_level ();
11476 /* Mark D as instantiated so that recursive calls to
11477 instantiate_decl do not try to instantiate it again. */
11478 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11480 /* Regenerate the declaration in case the template has been modified
11481 by a subsequent redeclaration. */
11482 regenerate_decl_from_template (d, td);
11484 /* We already set the file and line above. Reset them now in case
11485 they changed as a result of calling regenerate_decl_from_template. */
11486 input_location = DECL_SOURCE_LOCATION (d);
11488 if (TREE_CODE (d) == VAR_DECL)
11490 /* Clear out DECL_RTL; whatever was there before may not be right
11491 since we've reset the type of the declaration. */
11492 SET_DECL_RTL (d, NULL_RTX);
11493 DECL_IN_AGGR_P (d) = 0;
11495 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
11496 initializer. That function will defer actual emission until
11497 we have a chance to determine linkage. */
11498 DECL_EXTERNAL (d) = 0;
11500 /* Enter the scope of D so that access-checking works correctly. */
11501 push_nested_class (DECL_CONTEXT (d));
11502 cp_finish_decl (d,
11503 (!DECL_INITIALIZED_IN_CLASS_P (d)
11504 ? DECL_INITIAL (d) : NULL_TREE),
11505 NULL_TREE, 0);
11506 pop_nested_class ();
11508 else if (TREE_CODE (d) == FUNCTION_DECL)
11510 htab_t saved_local_specializations;
11511 tree subst_decl;
11512 tree tmpl_parm;
11513 tree spec_parm;
11515 /* Save away the current list, in case we are instantiating one
11516 template from within the body of another. */
11517 saved_local_specializations = local_specializations;
11519 /* Set up the list of local specializations. */
11520 local_specializations = htab_create (37,
11521 hash_local_specialization,
11522 eq_local_specializations,
11523 NULL);
11525 /* Set up context. */
11526 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11528 /* Create substitution entries for the parameters. */
11529 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11530 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11531 spec_parm = DECL_ARGUMENTS (d);
11532 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11534 register_local_specialization (spec_parm, tmpl_parm);
11535 spec_parm = skip_artificial_parms_for (d, spec_parm);
11536 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11538 while (tmpl_parm)
11540 register_local_specialization (spec_parm, tmpl_parm);
11541 tmpl_parm = TREE_CHAIN (tmpl_parm);
11542 spec_parm = TREE_CHAIN (spec_parm);
11544 gcc_assert (!spec_parm);
11546 /* Substitute into the body of the function. */
11547 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11548 tf_error | tf_warning, tmpl);
11550 /* We don't need the local specializations any more. */
11551 htab_delete (local_specializations);
11552 local_specializations = saved_local_specializations;
11554 /* Finish the function. */
11555 d = finish_function (0);
11556 expand_or_defer_fn (d);
11559 /* We're not deferring instantiation any more. */
11560 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11562 if (need_push)
11563 pop_from_top_level ();
11565 out:
11566 input_location = saved_loc;
11567 pop_deferring_access_checks ();
11568 pop_tinst_level ();
11570 timevar_pop (TV_PARSE);
11572 return d;
11575 /* Run through the list of templates that we wish we could
11576 instantiate, and instantiate any we can. RETRIES is the
11577 number of times we retry pending template instantiation. */
11579 void
11580 instantiate_pending_templates (int retries)
11582 tree *t;
11583 tree last = NULL_TREE;
11584 int reconsider;
11585 location_t saved_loc = input_location;
11587 /* Instantiating templates may trigger vtable generation. This in turn
11588 may require further template instantiations. We place a limit here
11589 to avoid infinite loop. */
11590 if (pending_templates && retries >= max_tinst_depth)
11592 cp_error_at ("template instantiation depth exceeds maximum of %d"
11593 " (use -ftemplate-depth-NN to increase the maximum)"
11594 " instantiating %q+D, possibly from virtual table"
11595 " generation",
11596 max_tinst_depth, TREE_VALUE (pending_templates));
11597 return;
11602 reconsider = 0;
11604 t = &pending_templates;
11605 while (*t)
11607 tree instantiation = TREE_VALUE (*t);
11609 reopen_tinst_level (TREE_PURPOSE (*t));
11611 if (TYPE_P (instantiation))
11613 tree fn;
11615 if (!COMPLETE_TYPE_P (instantiation))
11617 instantiate_class_template (instantiation);
11618 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11619 for (fn = TYPE_METHODS (instantiation);
11621 fn = TREE_CHAIN (fn))
11622 if (! DECL_ARTIFICIAL (fn))
11623 instantiate_decl (fn, /*defer_ok=*/0,
11624 /*undefined_ok=*/0);
11625 if (COMPLETE_TYPE_P (instantiation))
11626 reconsider = 1;
11629 if (COMPLETE_TYPE_P (instantiation))
11630 /* If INSTANTIATION has been instantiated, then we don't
11631 need to consider it again in the future. */
11632 *t = TREE_CHAIN (*t);
11633 else
11635 last = *t;
11636 t = &TREE_CHAIN (*t);
11639 else
11641 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11642 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11644 instantiation = instantiate_decl (instantiation,
11645 /*defer_ok=*/0,
11646 /*undefined_ok=*/0);
11647 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11648 reconsider = 1;
11651 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11652 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11653 /* If INSTANTIATION has been instantiated, then we don't
11654 need to consider it again in the future. */
11655 *t = TREE_CHAIN (*t);
11656 else
11658 last = *t;
11659 t = &TREE_CHAIN (*t);
11662 tinst_depth = 0;
11663 current_tinst_level = NULL_TREE;
11665 last_pending_template = last;
11667 while (reconsider);
11669 input_location = saved_loc;
11672 /* Substitute ARGVEC into T, which is a list of initializers for
11673 either base class or a non-static data member. The TREE_PURPOSEs
11674 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11675 instantiate_decl. */
11677 static tree
11678 tsubst_initializer_list (tree t, tree argvec)
11680 tree inits = NULL_TREE;
11682 for (; t; t = TREE_CHAIN (t))
11684 tree decl;
11685 tree init;
11687 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11688 NULL_TREE);
11689 decl = expand_member_init (decl);
11690 if (decl && !DECL_P (decl))
11691 in_base_initializer = 1;
11693 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11694 NULL_TREE);
11695 in_base_initializer = 0;
11697 if (decl)
11699 init = build_tree_list (decl, init);
11700 TREE_CHAIN (init) = inits;
11701 inits = init;
11704 return inits;
11707 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11709 static void
11710 set_current_access_from_decl (tree decl)
11712 if (TREE_PRIVATE (decl))
11713 current_access_specifier = access_private_node;
11714 else if (TREE_PROTECTED (decl))
11715 current_access_specifier = access_protected_node;
11716 else
11717 current_access_specifier = access_public_node;
11720 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11721 is the instantiation (which should have been created with
11722 start_enum) and ARGS are the template arguments to use. */
11724 static void
11725 tsubst_enum (tree tag, tree newtag, tree args)
11727 tree e;
11729 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11731 tree value;
11732 tree decl;
11734 decl = TREE_VALUE (e);
11735 /* Note that in a template enum, the TREE_VALUE is the
11736 CONST_DECL, not the corresponding INTEGER_CST. */
11737 value = tsubst_expr (DECL_INITIAL (decl),
11738 args, tf_error | tf_warning,
11739 NULL_TREE);
11741 /* Give this enumeration constant the correct access. */
11742 set_current_access_from_decl (decl);
11744 /* Actually build the enumerator itself. */
11745 build_enumerator (DECL_NAME (decl), value, newtag);
11748 finish_enum (newtag);
11749 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11750 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11753 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11754 its type -- but without substituting the innermost set of template
11755 arguments. So, innermost set of template parameters will appear in
11756 the type. */
11758 tree
11759 get_mostly_instantiated_function_type (tree decl)
11761 tree fn_type;
11762 tree tmpl;
11763 tree targs;
11764 tree tparms;
11765 int parm_depth;
11767 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11768 targs = DECL_TI_ARGS (decl);
11769 tparms = DECL_TEMPLATE_PARMS (tmpl);
11770 parm_depth = TMPL_PARMS_DEPTH (tparms);
11772 /* There should be as many levels of arguments as there are levels
11773 of parameters. */
11774 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
11776 fn_type = TREE_TYPE (tmpl);
11778 if (parm_depth == 1)
11779 /* No substitution is necessary. */
11781 else
11783 int i, save_access_control;
11784 tree partial_args;
11786 /* Replace the innermost level of the TARGS with NULL_TREEs to
11787 let tsubst know not to substitute for those parameters. */
11788 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11789 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11790 SET_TMPL_ARGS_LEVEL (partial_args, i,
11791 TMPL_ARGS_LEVEL (targs, i));
11792 SET_TMPL_ARGS_LEVEL (partial_args,
11793 TMPL_ARGS_DEPTH (targs),
11794 make_tree_vec (DECL_NTPARMS (tmpl)));
11796 /* Disable access control as this function is used only during
11797 name-mangling. */
11798 save_access_control = flag_access_control;
11799 flag_access_control = 0;
11801 ++processing_template_decl;
11802 /* Now, do the (partial) substitution to figure out the
11803 appropriate function type. */
11804 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11805 --processing_template_decl;
11807 /* Substitute into the template parameters to obtain the real
11808 innermost set of parameters. This step is important if the
11809 innermost set of template parameters contains value
11810 parameters whose types depend on outer template parameters. */
11811 TREE_VEC_LENGTH (partial_args)--;
11812 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11814 flag_access_control = save_access_control;
11817 return fn_type;
11820 /* Return truthvalue if we're processing a template different from
11821 the last one involved in diagnostics. */
11823 problematic_instantiation_changed (void)
11825 return last_template_error_tick != tinst_level_tick;
11828 /* Remember current template involved in diagnostics. */
11829 void
11830 record_last_problematic_instantiation (void)
11832 last_template_error_tick = tinst_level_tick;
11835 tree
11836 current_instantiation (void)
11838 return current_tinst_level;
11841 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11842 type. Return zero for ok, nonzero for disallowed. Issue error and
11843 warning messages under control of COMPLAIN. */
11845 static int
11846 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
11848 if (INTEGRAL_TYPE_P (type))
11849 return 0;
11850 else if (POINTER_TYPE_P (type))
11851 return 0;
11852 else if (TYPE_PTR_TO_MEMBER_P (type))
11853 return 0;
11854 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11855 return 0;
11856 else if (TREE_CODE (type) == TYPENAME_TYPE)
11857 return 0;
11859 if (complain & tf_error)
11860 error ("%q#T is not a valid type for a template constant parameter", type);
11861 return 1;
11864 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11865 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11867 static bool
11868 dependent_type_p_r (tree type)
11870 tree scope;
11872 /* [temp.dep.type]
11874 A type is dependent if it is:
11876 -- a template parameter. Template template parameters are types
11877 for us (since TYPE_P holds true for them) so we handle
11878 them here. */
11879 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11880 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
11881 return true;
11882 /* -- a qualified-id with a nested-name-specifier which contains a
11883 class-name that names a dependent type or whose unqualified-id
11884 names a dependent type. */
11885 if (TREE_CODE (type) == TYPENAME_TYPE)
11886 return true;
11887 /* -- a cv-qualified type where the cv-unqualified type is
11888 dependent. */
11889 type = TYPE_MAIN_VARIANT (type);
11890 /* -- a compound type constructed from any dependent type. */
11891 if (TYPE_PTR_TO_MEMBER_P (type))
11892 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11893 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11894 (type)));
11895 else if (TREE_CODE (type) == POINTER_TYPE
11896 || TREE_CODE (type) == REFERENCE_TYPE)
11897 return dependent_type_p (TREE_TYPE (type));
11898 else if (TREE_CODE (type) == FUNCTION_TYPE
11899 || TREE_CODE (type) == METHOD_TYPE)
11901 tree arg_type;
11903 if (dependent_type_p (TREE_TYPE (type)))
11904 return true;
11905 for (arg_type = TYPE_ARG_TYPES (type);
11906 arg_type;
11907 arg_type = TREE_CHAIN (arg_type))
11908 if (dependent_type_p (TREE_VALUE (arg_type)))
11909 return true;
11910 return false;
11912 /* -- an array type constructed from any dependent type or whose
11913 size is specified by a constant expression that is
11914 value-dependent. */
11915 if (TREE_CODE (type) == ARRAY_TYPE)
11917 if (TYPE_DOMAIN (type)
11918 && ((value_dependent_expression_p
11919 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11920 || (type_dependent_expression_p
11921 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11922 return true;
11923 return dependent_type_p (TREE_TYPE (type));
11926 /* -- a template-id in which either the template name is a template
11927 parameter ... */
11928 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11929 return true;
11930 /* ... or any of the template arguments is a dependent type or
11931 an expression that is type-dependent or value-dependent. */
11932 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11933 && (any_dependent_template_arguments_p
11934 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
11935 return true;
11937 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11938 expression is not type-dependent, then it should already been
11939 have resolved. */
11940 if (TREE_CODE (type) == TYPEOF_TYPE)
11941 return true;
11943 /* The standard does not specifically mention types that are local
11944 to template functions or local classes, but they should be
11945 considered dependent too. For example:
11947 template <int I> void f() {
11948 enum E { a = I };
11949 S<sizeof (E)> s;
11952 The size of `E' cannot be known until the value of `I' has been
11953 determined. Therefore, `E' must be considered dependent. */
11954 scope = TYPE_CONTEXT (type);
11955 if (scope && TYPE_P (scope))
11956 return dependent_type_p (scope);
11957 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11958 return type_dependent_expression_p (scope);
11960 /* Other types are non-dependent. */
11961 return false;
11964 /* Returns TRUE if TYPE is dependent, in the sense of
11965 [temp.dep.type]. */
11967 bool
11968 dependent_type_p (tree type)
11970 /* If there are no template parameters in scope, then there can't be
11971 any dependent types. */
11972 if (!processing_template_decl)
11973 return false;
11975 /* If the type is NULL, we have not computed a type for the entity
11976 in question; in that case, the type is dependent. */
11977 if (!type)
11978 return true;
11980 /* Erroneous types can be considered non-dependent. */
11981 if (type == error_mark_node)
11982 return false;
11984 /* If we have not already computed the appropriate value for TYPE,
11985 do so now. */
11986 if (!TYPE_DEPENDENT_P_VALID (type))
11988 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11989 TYPE_DEPENDENT_P_VALID (type) = 1;
11992 return TYPE_DEPENDENT_P (type);
11995 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
11997 static bool
11998 dependent_scope_ref_p (tree expression, bool criterion (tree))
12000 tree scope;
12001 tree name;
12003 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12005 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12006 return true;
12008 scope = TREE_OPERAND (expression, 0);
12009 name = TREE_OPERAND (expression, 1);
12011 /* [temp.dep.expr]
12013 An id-expression is type-dependent if it contains a
12014 nested-name-specifier that contains a class-name that names a
12015 dependent type. */
12016 /* The suggested resolution to Core Issue 2 implies that if the
12017 qualifying type is the current class, then we must peek
12018 inside it. */
12019 if (DECL_P (name)
12020 && currently_open_class (scope)
12021 && !criterion (name))
12022 return false;
12023 if (dependent_type_p (scope))
12024 return true;
12026 return false;
12029 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12030 [temp.dep.constexpr] */
12032 bool
12033 value_dependent_expression_p (tree expression)
12035 if (!processing_template_decl)
12036 return false;
12038 /* A name declared with a dependent type. */
12039 if (DECL_P (expression) && type_dependent_expression_p (expression))
12040 return true;
12042 switch (TREE_CODE (expression))
12044 case IDENTIFIER_NODE:
12045 /* A name that has not been looked up -- must be dependent. */
12046 return true;
12048 case TEMPLATE_PARM_INDEX:
12049 /* A non-type template parm. */
12050 return true;
12052 case CONST_DECL:
12053 /* A non-type template parm. */
12054 if (DECL_TEMPLATE_PARM_P (expression))
12055 return true;
12056 return false;
12058 case VAR_DECL:
12059 /* A constant with integral or enumeration type and is initialized
12060 with an expression that is value-dependent. */
12061 if (DECL_INITIAL (expression)
12062 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12063 && value_dependent_expression_p (DECL_INITIAL (expression)))
12064 return true;
12065 return false;
12067 case DYNAMIC_CAST_EXPR:
12068 case STATIC_CAST_EXPR:
12069 case CONST_CAST_EXPR:
12070 case REINTERPRET_CAST_EXPR:
12071 case CAST_EXPR:
12072 /* These expressions are value-dependent if the type to which
12073 the cast occurs is dependent or the expression being casted
12074 is value-dependent. */
12076 tree type = TREE_TYPE (expression);
12078 if (dependent_type_p (type))
12079 return true;
12081 /* A functional cast has a list of operands. */
12082 expression = TREE_OPERAND (expression, 0);
12083 if (!expression)
12085 /* If there are no operands, it must be an expression such
12086 as "int()". This should not happen for aggregate types
12087 because it would form non-constant expressions. */
12088 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12090 return false;
12093 if (TREE_CODE (expression) == TREE_LIST)
12095 for (; expression; expression = TREE_CHAIN (expression))
12096 if (value_dependent_expression_p (TREE_VALUE (expression)))
12097 return true;
12098 return false;
12101 return value_dependent_expression_p (expression);
12104 case SIZEOF_EXPR:
12105 case ALIGNOF_EXPR:
12106 /* A `sizeof' expression is value-dependent if the operand is
12107 type-dependent. */
12108 expression = TREE_OPERAND (expression, 0);
12109 if (TYPE_P (expression))
12110 return dependent_type_p (expression);
12111 return type_dependent_expression_p (expression);
12113 case SCOPE_REF:
12114 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12116 case COMPONENT_REF:
12117 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12118 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12120 case CALL_EXPR:
12121 /* A CALL_EXPR is value-dependent if any argument is
12122 value-dependent. Why do we have to handle CALL_EXPRs in this
12123 function at all? First, some function calls, those for which
12124 value_dependent_expression_p is true, man appear in constant
12125 expressions. Second, there appear to be bugs which result in
12126 other CALL_EXPRs reaching this point. */
12128 tree function = TREE_OPERAND (expression, 0);
12129 tree args = TREE_OPERAND (expression, 1);
12131 if (value_dependent_expression_p (function))
12132 return true;
12134 if (! args)
12135 return false;
12137 if (TREE_CODE (args) == TREE_LIST)
12139 for (; args; args = TREE_CHAIN (args))
12140 if (value_dependent_expression_p (TREE_VALUE (args)))
12141 return true;
12142 return false;
12145 return value_dependent_expression_p (args);
12148 default:
12149 /* A constant expression is value-dependent if any subexpression is
12150 value-dependent. */
12151 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12153 case tcc_reference:
12154 case tcc_unary:
12155 return (value_dependent_expression_p
12156 (TREE_OPERAND (expression, 0)));
12158 case tcc_comparison:
12159 case tcc_binary:
12160 return ((value_dependent_expression_p
12161 (TREE_OPERAND (expression, 0)))
12162 || (value_dependent_expression_p
12163 (TREE_OPERAND (expression, 1))));
12165 case tcc_expression:
12167 int i;
12168 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12169 /* In some cases, some of the operands may be missing.
12170 (For example, in the case of PREDECREMENT_EXPR, the
12171 amount to increment by may be missing.) That doesn't
12172 make the expression dependent. */
12173 if (TREE_OPERAND (expression, i)
12174 && (value_dependent_expression_p
12175 (TREE_OPERAND (expression, i))))
12176 return true;
12177 return false;
12180 default:
12181 break;
12185 /* The expression is not value-dependent. */
12186 return false;
12189 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12190 [temp.dep.expr]. */
12192 bool
12193 type_dependent_expression_p (tree expression)
12195 if (!processing_template_decl)
12196 return false;
12198 if (expression == error_mark_node)
12199 return false;
12201 /* An unresolved name is always dependent. */
12202 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12203 return true;
12205 /* Some expression forms are never type-dependent. */
12206 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12207 || TREE_CODE (expression) == SIZEOF_EXPR
12208 || TREE_CODE (expression) == ALIGNOF_EXPR
12209 || TREE_CODE (expression) == TYPEID_EXPR
12210 || TREE_CODE (expression) == DELETE_EXPR
12211 || TREE_CODE (expression) == VEC_DELETE_EXPR
12212 || TREE_CODE (expression) == THROW_EXPR)
12213 return false;
12215 /* The types of these expressions depends only on the type to which
12216 the cast occurs. */
12217 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12218 || TREE_CODE (expression) == STATIC_CAST_EXPR
12219 || TREE_CODE (expression) == CONST_CAST_EXPR
12220 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12221 || TREE_CODE (expression) == CAST_EXPR)
12222 return dependent_type_p (TREE_TYPE (expression));
12224 /* The types of these expressions depends only on the type created
12225 by the expression. */
12226 if (TREE_CODE (expression) == NEW_EXPR
12227 || TREE_CODE (expression) == VEC_NEW_EXPR)
12229 /* For NEW_EXPR tree nodes created inside a template, either
12230 the object type itself or a TREE_LIST may appear as the
12231 operand 1. */
12232 tree type = TREE_OPERAND (expression, 1);
12233 if (TREE_CODE (type) == TREE_LIST)
12234 /* This is an array type. We need to check array dimensions
12235 as well. */
12236 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12237 || value_dependent_expression_p
12238 (TREE_OPERAND (TREE_VALUE (type), 1));
12239 else
12240 return dependent_type_p (type);
12243 if (TREE_CODE (expression) == SCOPE_REF
12244 && dependent_scope_ref_p (expression,
12245 type_dependent_expression_p))
12246 return true;
12248 if (TREE_CODE (expression) == FUNCTION_DECL
12249 && DECL_LANG_SPECIFIC (expression)
12250 && DECL_TEMPLATE_INFO (expression)
12251 && (any_dependent_template_arguments_p
12252 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12253 return true;
12255 if (TREE_CODE (expression) == TEMPLATE_DECL
12256 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12257 return false;
12259 if (TREE_TYPE (expression) == unknown_type_node)
12261 if (TREE_CODE (expression) == ADDR_EXPR)
12262 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12263 if (TREE_CODE (expression) == COMPONENT_REF
12264 || TREE_CODE (expression) == OFFSET_REF)
12266 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12267 return true;
12268 expression = TREE_OPERAND (expression, 1);
12269 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12270 return false;
12272 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12273 if (TREE_CODE (expression) == SCOPE_REF)
12274 return false;
12276 if (TREE_CODE (expression) == BASELINK)
12277 expression = BASELINK_FUNCTIONS (expression);
12279 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12281 if (any_dependent_template_arguments_p
12282 (TREE_OPERAND (expression, 1)))
12283 return true;
12284 expression = TREE_OPERAND (expression, 0);
12286 gcc_assert (TREE_CODE (expression) == OVERLOAD);
12288 while (expression)
12290 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12291 return true;
12292 expression = OVL_NEXT (expression);
12294 return false;
12297 return (dependent_type_p (TREE_TYPE (expression)));
12300 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12301 contains a type-dependent expression. */
12303 bool
12304 any_type_dependent_arguments_p (tree args)
12306 while (args)
12308 tree arg = TREE_VALUE (args);
12310 if (type_dependent_expression_p (arg))
12311 return true;
12312 args = TREE_CHAIN (args);
12314 return false;
12317 /* Returns TRUE if the ARG (a template argument) is dependent. */
12319 static bool
12320 dependent_template_arg_p (tree arg)
12322 if (!processing_template_decl)
12323 return false;
12325 if (TREE_CODE (arg) == TEMPLATE_DECL
12326 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12327 return dependent_template_p (arg);
12328 else if (TYPE_P (arg))
12329 return dependent_type_p (arg);
12330 else
12331 return (type_dependent_expression_p (arg)
12332 || value_dependent_expression_p (arg));
12335 /* Returns true if ARGS (a collection of template arguments) contains
12336 any dependent arguments. */
12338 bool
12339 any_dependent_template_arguments_p (tree args)
12341 int i;
12342 int j;
12344 if (!args)
12345 return false;
12347 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12349 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12350 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12351 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12352 return true;
12355 return false;
12358 /* Returns TRUE if the template TMPL is dependent. */
12360 bool
12361 dependent_template_p (tree tmpl)
12363 if (TREE_CODE (tmpl) == OVERLOAD)
12365 while (tmpl)
12367 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12368 return true;
12369 tmpl = OVL_CHAIN (tmpl);
12371 return false;
12374 /* Template template parameters are dependent. */
12375 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12376 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12377 return true;
12378 /* So are names that have not been looked up. */
12379 if (TREE_CODE (tmpl) == SCOPE_REF
12380 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12381 return true;
12382 /* So are member templates of dependent classes. */
12383 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12384 return dependent_type_p (DECL_CONTEXT (tmpl));
12385 return false;
12388 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
12390 bool
12391 dependent_template_id_p (tree tmpl, tree args)
12393 return (dependent_template_p (tmpl)
12394 || any_dependent_template_arguments_p (args));
12397 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
12398 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
12399 can be found. Note that this function peers inside uninstantiated
12400 templates and therefore should be used only in extremely limited
12401 situations. ONLY_CURRENT_P restricts this peering to the currently
12402 open classes hierarchy (which is required when comparing types). */
12404 tree
12405 resolve_typename_type (tree type, bool only_current_p)
12407 tree scope;
12408 tree name;
12409 tree decl;
12410 int quals;
12411 tree pushed_scope;
12413 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12415 scope = TYPE_CONTEXT (type);
12416 name = TYPE_IDENTIFIER (type);
12418 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
12419 it first before we can figure out what NAME refers to. */
12420 if (TREE_CODE (scope) == TYPENAME_TYPE)
12421 scope = resolve_typename_type (scope, only_current_p);
12422 /* If we don't know what SCOPE refers to, then we cannot resolve the
12423 TYPENAME_TYPE. */
12424 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12425 return error_mark_node;
12426 /* If the SCOPE is a template type parameter, we have no way of
12427 resolving the name. */
12428 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12429 return type;
12430 /* If the SCOPE is not the current instantiation, there's no reason
12431 to look inside it. */
12432 if (only_current_p && !currently_open_class (scope))
12433 return error_mark_node;
12434 /* If SCOPE is a partial instantiation, it will not have a valid
12435 TYPE_FIELDS list, so use the original template. */
12436 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12437 /* Enter the SCOPE so that name lookup will be resolved as if we
12438 were in the class definition. In particular, SCOPE will no
12439 longer be considered a dependent type. */
12440 pushed_scope = push_scope (scope);
12441 /* Look up the declaration. */
12442 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
12443 /* Obtain the set of qualifiers applied to the TYPE. */
12444 quals = cp_type_quals (type);
12445 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
12446 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
12447 if (!decl)
12448 type = error_mark_node;
12449 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12450 && TREE_CODE (decl) == TYPE_DECL)
12451 type = TREE_TYPE (decl);
12452 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12453 && DECL_CLASS_TEMPLATE_P (decl))
12455 tree tmpl;
12456 tree args;
12457 /* Obtain the template and the arguments. */
12458 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12459 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12460 /* Instantiate the template. */
12461 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12462 /*entering_scope=*/0, tf_error | tf_user);
12464 else
12465 type = error_mark_node;
12466 /* Qualify the resulting type. */
12467 if (type != error_mark_node && quals)
12468 type = cp_build_qualified_type (type, quals);
12469 /* Leave the SCOPE. */
12470 if (pushed_scope)
12471 pop_scope (pushed_scope);
12473 return type;
12476 /* EXPR is an expression which is not type-dependent. Return a proxy
12477 for EXPR that can be used to compute the types of larger
12478 expressions containing EXPR. */
12480 tree
12481 build_non_dependent_expr (tree expr)
12483 tree inner_expr;
12485 /* Preserve null pointer constants so that the type of things like
12486 "p == 0" where "p" is a pointer can be determined. */
12487 if (null_ptr_cst_p (expr))
12488 return expr;
12489 /* Preserve OVERLOADs; the functions must be available to resolve
12490 types. */
12491 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12492 TREE_OPERAND (expr, 0) : expr);
12493 if (TREE_CODE (inner_expr) == OVERLOAD
12494 || TREE_CODE (inner_expr) == FUNCTION_DECL
12495 || TREE_CODE (inner_expr) == TEMPLATE_DECL
12496 || TREE_CODE (inner_expr) == TEMPLATE_ID_EXPR
12497 || TREE_CODE (inner_expr) == OFFSET_REF)
12498 return expr;
12499 /* There is no need to return a proxy for a variable. */
12500 if (TREE_CODE (expr) == VAR_DECL)
12501 return expr;
12502 /* Preserve string constants; conversions from string constants to
12503 "char *" are allowed, even though normally a "const char *"
12504 cannot be used to initialize a "char *". */
12505 if (TREE_CODE (expr) == STRING_CST)
12506 return expr;
12507 /* Preserve arithmetic constants, as an optimization -- there is no
12508 reason to create a new node. */
12509 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12510 return expr;
12511 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
12512 There is at least one place where we want to know that a
12513 particular expression is a throw-expression: when checking a ?:
12514 expression, there are special rules if the second or third
12515 argument is a throw-expression. */
12516 if (TREE_CODE (expr) == THROW_EXPR)
12517 return expr;
12519 if (TREE_CODE (expr) == COND_EXPR)
12520 return build3 (COND_EXPR,
12521 TREE_TYPE (expr),
12522 TREE_OPERAND (expr, 0),
12523 (TREE_OPERAND (expr, 1)
12524 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12525 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12526 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12527 if (TREE_CODE (expr) == COMPOUND_EXPR
12528 && !COMPOUND_EXPR_OVERLOADED (expr))
12529 return build2 (COMPOUND_EXPR,
12530 TREE_TYPE (expr),
12531 TREE_OPERAND (expr, 0),
12532 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12534 /* Otherwise, build a NON_DEPENDENT_EXPR.
12536 REFERENCE_TYPEs are not stripped for expressions in templates
12537 because doing so would play havoc with mangling. Consider, for
12538 example:
12540 template <typename T> void f<T& g>() { g(); }
12542 In the body of "f", the expression for "g" will have
12543 REFERENCE_TYPE, even though the standard says that it should
12544 not. The reason is that we must preserve the syntactic form of
12545 the expression so that mangling (say) "f<g>" inside the body of
12546 "f" works out correctly. Therefore, the REFERENCE_TYPE is
12547 stripped here. */
12548 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12551 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
12552 Return a new TREE_LIST with the various arguments replaced with
12553 equivalent non-dependent expressions. */
12555 tree
12556 build_non_dependent_args (tree args)
12558 tree a;
12559 tree new_args;
12561 new_args = NULL_TREE;
12562 for (a = args; a; a = TREE_CHAIN (a))
12563 new_args = tree_cons (NULL_TREE,
12564 build_non_dependent_expr (TREE_VALUE (a)),
12565 new_args);
12566 return nreverse (new_args);
12569 #include "gt-cp-pt.h"